blob: cafcfa29dc87ccce07069b5b7fe122d74343856a [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);
124static int ssl_check_clienthello_tlsext_early(SSL *s);
125int 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
435/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the list
436 * of allowed curve IDs. If |get_client_curves| is non-zero, return the client
437 * curve list. Otherwise, return the preferred list. */
438static void tls1_get_curvelist(SSL *s, int get_client_curves,
439 const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700440 {
David Benjamin072334d2014-07-13 16:24:27 -0400441 if (get_client_curves)
Adam Langley95c29f32014-06-20 12:00:00 -0700442 {
David Benjamin072334d2014-07-13 16:24:27 -0400443 *out_curve_ids = s->session->tlsext_ellipticcurvelist;
444 *out_curve_ids_len = s->session->tlsext_ellipticcurvelist_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700445 return;
446 }
Adam Langley95c29f32014-06-20 12:00:00 -0700447
David Benjamin335d10d2014-08-06 19:56:33 -0400448 *out_curve_ids = s->tlsext_ellipticcurvelist;
449 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
David Benjamin072334d2014-07-13 16:24:27 -0400450 if (!*out_curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700451 {
David Benjamin072334d2014-07-13 16:24:27 -0400452 *out_curve_ids = eccurves_default;
David Benjamin0eb5a2d2014-07-25 02:40:43 -0400453 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
Adam Langley95c29f32014-06-20 12:00:00 -0700454 }
455 }
David Benjamined439582014-07-14 19:13:02 -0400456
David Benjamined439582014-07-14 19:13:02 -0400457int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700458 {
David Benjamined439582014-07-14 19:13:02 -0400459 uint8_t curve_type;
460 uint16_t curve_id;
David Benjamin072334d2014-07-13 16:24:27 -0400461 const uint16_t *curves;
462 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400463
464 /* Only support named curves. */
465 if (!CBS_get_u8(cbs, &curve_type) ||
466 curve_type != NAMED_CURVE_TYPE ||
467 !CBS_get_u16(cbs, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700468 return 0;
David Benjamined439582014-07-14 19:13:02 -0400469
David Benjamin072334d2014-07-13 16:24:27 -0400470 tls1_get_curvelist(s, 0, &curves, &curves_len);
471 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700472 {
David Benjamin072334d2014-07-13 16:24:27 -0400473 if (curve_id == curves[i])
David Benjamined439582014-07-14 19:13:02 -0400474 {
475 *out_curve_id = curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700476 return 1;
David Benjamined439582014-07-14 19:13:02 -0400477 }
Adam Langley95c29f32014-06-20 12:00:00 -0700478 }
479 return 0;
480 }
481
David Benjamin072334d2014-07-13 16:24:27 -0400482int tls1_get_shared_curve(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700483 {
David Benjamin072334d2014-07-13 16:24:27 -0400484 const uint16_t *pref, *supp;
Adam Langley95c29f32014-06-20 12:00:00 -0700485 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400486
Adam Langley95c29f32014-06-20 12:00:00 -0700487 /* Can't do anything on client side */
488 if (s->server == 0)
David Benjamin072334d2014-07-13 16:24:27 -0400489 return NID_undef;
490
David Benjamin335d10d2014-08-06 19:56:33 -0400491 /* Return first preference shared curve */
Adam Langley95c29f32014-06-20 12:00:00 -0700492 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
493 &supp, &supplen);
494 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
495 &pref, &preflen);
David Benjamin072334d2014-07-13 16:24:27 -0400496 for (i = 0; i < preflen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700497 {
David Benjamin072334d2014-07-13 16:24:27 -0400498 for (j = 0; j < supplen; j++)
Adam Langley95c29f32014-06-20 12:00:00 -0700499 {
David Benjamin072334d2014-07-13 16:24:27 -0400500 if (pref[i] == supp[j])
501 return tls1_ec_curve_id2nid(pref[i]);
Adam Langley95c29f32014-06-20 12:00:00 -0700502 }
503 }
David Benjamin072334d2014-07-13 16:24:27 -0400504 return NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700505 }
506
David Benjamin072334d2014-07-13 16:24:27 -0400507/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
508 *
509 * (a) 0 is not a valid curve ID.
510 *
511 * (b) The largest curve ID is 31.
512 *
513 * Those implementations must be revised before adding support for curve IDs
514 * that break these assumptions. */
515OPENSSL_COMPILE_ASSERT(
516 (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
517
518int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
519 const int *curves, size_t ncurves)
Adam Langley95c29f32014-06-20 12:00:00 -0700520 {
David Benjamin072334d2014-07-13 16:24:27 -0400521 uint16_t *curve_ids;
Adam Langley95c29f32014-06-20 12:00:00 -0700522 size_t i;
523 /* Bitmap of curves included to detect duplicates: only works
524 * while curve ids < 32
525 */
David Benjamin072334d2014-07-13 16:24:27 -0400526 uint32_t dup_list = 0;
527 curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
528 if (!curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700529 return 0;
David Benjamin072334d2014-07-13 16:24:27 -0400530 for (i = 0; i < ncurves; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700531 {
David Benjamin072334d2014-07-13 16:24:27 -0400532 uint32_t idmask;
533 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700534 id = tls1_ec_nid2curve_id(curves[i]);
David Benjamin072334d2014-07-13 16:24:27 -0400535 idmask = ((uint32_t)1) << id;
Adam Langley95c29f32014-06-20 12:00:00 -0700536 if (!id || (dup_list & idmask))
537 {
David Benjamin072334d2014-07-13 16:24:27 -0400538 OPENSSL_free(curve_ids);
Adam Langley95c29f32014-06-20 12:00:00 -0700539 return 0;
540 }
541 dup_list |= idmask;
David Benjamin072334d2014-07-13 16:24:27 -0400542 curve_ids[i] = id;
Adam Langley95c29f32014-06-20 12:00:00 -0700543 }
David Benjamin072334d2014-07-13 16:24:27 -0400544 if (*out_curve_ids)
545 OPENSSL_free(*out_curve_ids);
546 *out_curve_ids = curve_ids;
547 *out_curve_ids_len = ncurves;
Adam Langley95c29f32014-06-20 12:00:00 -0700548 return 1;
549 }
550
David Benjamin072334d2014-07-13 16:24:27 -0400551/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
552 * TLS curve ID and point format, respectively, for |ec|. It returns one on
553 * success and zero on failure. */
554static 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 -0700555 {
Adam Langley95c29f32014-06-20 12:00:00 -0700556 int nid;
David Benjamin072334d2014-07-13 16:24:27 -0400557 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700558 const EC_GROUP *grp;
559 if (!ec)
560 return 0;
561
Adam Langley95c29f32014-06-20 12:00:00 -0700562 grp = EC_KEY_get0_group(ec);
563 if (!grp)
564 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700565
566 /* Determine curve ID */
David Benjamin072334d2014-07-13 16:24:27 -0400567 nid = EC_GROUP_get_curve_name(grp);
568 id = tls1_ec_nid2curve_id(nid);
569 if (!id)
570 return 0;
571
572 /* Set the named curve ID. Arbitrary explicit curves are not
573 * supported. */
574 *out_curve_id = id;
575
576 if (out_comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700577 {
578 if (EC_KEY_get0_public_key(ec) == NULL)
579 return 0;
580 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
David Benjamin072334d2014-07-13 16:24:27 -0400581 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
Adam Langley95c29f32014-06-20 12:00:00 -0700582 else
David Benjamin072334d2014-07-13 16:24:27 -0400583 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
Adam Langley95c29f32014-06-20 12:00:00 -0700584 }
585 return 1;
586 }
David Benjamin072334d2014-07-13 16:24:27 -0400587
Adam Langley95c29f32014-06-20 12:00:00 -0700588/* Check an EC key is compatible with extensions */
589static int tls1_check_ec_key(SSL *s,
David Benjamin072334d2014-07-13 16:24:27 -0400590 const uint16_t *curve_id, const uint8_t *comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700591 {
David Benjamin072334d2014-07-13 16:24:27 -0400592 const uint16_t *curves;
593 size_t curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -0700594 int j;
595 /* If point formats extension present check it, otherwise everything
596 * is supported (see RFC4492).
597 */
598 if (comp_id && s->session->tlsext_ecpointformatlist)
599 {
David Benjamin072334d2014-07-13 16:24:27 -0400600 uint8_t *p = s->session->tlsext_ecpointformatlist;
601 size_t plen = s->session->tlsext_ecpointformatlist_length;
602 for (i = 0; i < plen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700603 {
David Benjamin072334d2014-07-13 16:24:27 -0400604 if (*comp_id == p[i])
Adam Langley95c29f32014-06-20 12:00:00 -0700605 break;
606 }
607 if (i == plen)
608 return 0;
609 }
610 if (!curve_id)
611 return 1;
612 /* Check curve is consistent with client and server preferences */
613 for (j = 0; j <= 1; j++)
614 {
David Benjamin072334d2014-07-13 16:24:27 -0400615 tls1_get_curvelist(s, j, &curves, &curves_len);
616 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700617 {
David Benjamin072334d2014-07-13 16:24:27 -0400618 if (curves[i] == *curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700619 break;
620 }
David Benjamin072334d2014-07-13 16:24:27 -0400621 if (i == curves_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700622 return 0;
623 /* For clients can only check sent curve list */
624 if (!s->server)
625 return 1;
626 }
627 return 1;
628 }
629
630static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
631 size_t *pformatslen)
632 {
633 /* If we have a custom point format list use it otherwise
634 * use default */
635 if (s->tlsext_ecpointformatlist)
636 {
637 *pformats = s->tlsext_ecpointformatlist;
638 *pformatslen = s->tlsext_ecpointformatlist_length;
639 }
640 else
641 {
642 *pformats = ecformats_default;
David Benjamin335d10d2014-08-06 19:56:33 -0400643 *pformatslen = sizeof(ecformats_default);
Adam Langley95c29f32014-06-20 12:00:00 -0700644 }
645 }
646
647/* Check cert parameters compatible with extensions: currently just checks
648 * EC certificates have compatible curves and compression.
649 */
650static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
651 {
David Benjamin072334d2014-07-13 16:24:27 -0400652 uint8_t comp_id;
653 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700654 EVP_PKEY *pkey;
655 int rv;
656 pkey = X509_get_pubkey(x);
657 if (!pkey)
658 return 0;
659 /* If not EC nothing to do */
660 if (pkey->type != EVP_PKEY_EC)
661 {
662 EVP_PKEY_free(pkey);
663 return 1;
664 }
David Benjamin072334d2014-07-13 16:24:27 -0400665 rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec);
Adam Langley95c29f32014-06-20 12:00:00 -0700666 EVP_PKEY_free(pkey);
667 if (!rv)
668 return 0;
669 /* Can't check curve_id for client certs as we don't have a
670 * supported curves extension.
671 */
David Benjamin335d10d2014-08-06 19:56:33 -0400672 return tls1_check_ec_key(s, s->server ? &curve_id : NULL, &comp_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700673 }
674/* Check EC temporary key is compatible with client extensions */
675int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
676 {
David Benjamin072334d2014-07-13 16:24:27 -0400677 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700678 EC_KEY *ec = s->cert->ecdh_tmp;
679#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
680 /* Allow any curve: not just those peer supports */
681 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
682 return 1;
683#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700684 if (s->cert->ecdh_tmp_auto)
685 {
686 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400687 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700688 }
689 if (!ec)
690 {
691 if (s->cert->ecdh_tmp_cb)
692 return 1;
693 else
694 return 0;
695 }
David Benjamin072334d2014-07-13 16:24:27 -0400696 if (!tls1_curve_params_from_ec_key(&curve_id, NULL, ec))
Adam Langley95c29f32014-06-20 12:00:00 -0700697 return 0;
698/* Set this to allow use of invalid curves for testing */
699#if 0
700 return 1;
701#else
David Benjamin072334d2014-07-13 16:24:27 -0400702 return tls1_check_ec_key(s, &curve_id, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700703#endif
704 }
705
Adam Langley95c29f32014-06-20 12:00:00 -0700706
Adam Langley95c29f32014-06-20 12:00:00 -0700707
708/* List of supported signature algorithms and hashes. Should make this
709 * customisable at some point, for now include everything we support.
710 */
711
Adam Langley95c29f32014-06-20 12:00:00 -0700712#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langley95c29f32014-06-20 12:00:00 -0700714#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700715
716#define tlsext_sigalg(md) \
717 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700718 tlsext_sigalg_ecdsa(md)
719
David Benjamincff64722014-08-19 19:54:46 -0400720static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700721 tlsext_sigalg(TLSEXT_hash_sha512)
722 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700723 tlsext_sigalg(TLSEXT_hash_sha256)
724 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700725 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700726};
Adam Langley95c29f32014-06-20 12:00:00 -0700727size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
728 {
Adam Langley95c29f32014-06-20 12:00:00 -0700729 /* If server use client authentication sigalgs if not NULL */
730 if (s->server && s->cert->client_sigalgs)
731 {
732 *psigs = s->cert->client_sigalgs;
733 return s->cert->client_sigalgslen;
734 }
735 else if (s->cert->conf_sigalgs)
736 {
737 *psigs = s->cert->conf_sigalgs;
738 return s->cert->conf_sigalgslen;
739 }
740 else
741 {
742 *psigs = tls12_sigalgs;
743 return sizeof(tls12_sigalgs);
744 }
745 }
David Benjamin05da6e12014-07-12 20:42:55 -0400746
747/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
748 * |cbs|. It checks it is consistent with |s|'s sent supported
749 * signature algorithms and, if so, writes the relevant digest into
750 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
751 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700752 */
David Benjamin05da6e12014-07-12 20:42:55 -0400753int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
754 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700755 {
756 const unsigned char *sent_sigs;
757 size_t sent_sigslen, i;
758 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400759 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700760 /* Should never happen */
761 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400762 {
763 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
764 *out_alert = SSL_AD_INTERNAL_ERROR;
765 return 0;
766 }
767 if (!CBS_get_u8(cbs, &hash) ||
768 !CBS_get_u8(cbs, &signature))
769 {
770 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
771 *out_alert = SSL_AD_DECODE_ERROR;
772 return 0;
773 }
Adam Langley95c29f32014-06-20 12:00:00 -0700774 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400775 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700776 {
777 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400778 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700779 return 0;
780 }
Adam Langley95c29f32014-06-20 12:00:00 -0700781 if (pkey->type == EVP_PKEY_EC)
782 {
David Benjamin072334d2014-07-13 16:24:27 -0400783 uint16_t curve_id;
784 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700785 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400786 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400787 {
788 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700789 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400790 }
David Benjamin072334d2014-07-13 16:24:27 -0400791 if (!s->server && !tls1_check_ec_key(s, &curve_id, &comp_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700792 {
793 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
David Benjamin05da6e12014-07-12 20:42:55 -0400794 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700795 return 0;
796 }
David Benjamin05da6e12014-07-12 20:42:55 -0400797 }
Adam Langley95c29f32014-06-20 12:00:00 -0700798
799 /* Check signature matches a type we sent */
800 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
801 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
802 {
David Benjamin05da6e12014-07-12 20:42:55 -0400803 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700804 break;
805 }
806 /* Allow fallback to SHA1 if not strict mode */
David Benjamin05da6e12014-07-12 20:42:55 -0400807 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 -0700808 {
809 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400810 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700811 return 0;
812 }
David Benjamin05da6e12014-07-12 20:42:55 -0400813 *out_md = tls12_get_hash(hash);
814 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700815 {
816 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400817 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700818 return 0;
819 }
820 /* Store the digest used so applications can retrieve it if they
821 * wish.
822 */
823 if (s->session && s->session->sess_cert)
David Benjamin05da6e12014-07-12 20:42:55 -0400824 s->session->sess_cert->peer_key->digest = *out_md;
Adam Langley95c29f32014-06-20 12:00:00 -0700825 return 1;
826 }
827/* Get a mask of disabled algorithms: an algorithm is disabled
828 * if it isn't supported or doesn't appear in supported signature
829 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
830 * session and not global settings.
831 *
832 */
833void ssl_set_client_disabled(SSL *s)
834 {
835 CERT *c = s->cert;
836 const unsigned char *sigalgs;
837 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400838 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700839 c->mask_a = 0;
840 c->mask_k = 0;
841 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
842 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
843 c->mask_ssl = SSL_TLSV1_2;
844 else
845 c->mask_ssl = 0;
846 /* Now go through all signature algorithms seeing if we support
847 * any for RSA, DSA, ECDSA. Do this for all versions not just
848 * TLS 1.2.
849 */
850 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
851 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
852 {
853 switch(sigalgs[1])
854 {
Adam Langley95c29f32014-06-20 12:00:00 -0700855 case TLSEXT_signature_rsa:
856 have_rsa = 1;
857 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700858 case TLSEXT_signature_ecdsa:
859 have_ecdsa = 1;
860 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700861 }
862 }
David Benjamin0da0e182014-08-19 16:20:28 -0400863 /* Disable auth if we don't include any appropriate signature
864 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700865 */
866 if (!have_rsa)
867 {
868 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870 if (!have_ecdsa)
871 {
872 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874 /* with PSK there must be client callback set */
875 if (!s->psk_client_callback)
876 {
877 c->mask_a |= SSL_aPSK;
878 c->mask_k |= SSL_kPSK;
879 }
Adam Langley95c29f32014-06-20 12:00:00 -0700880 c->valid = 1;
881 }
882
Adam Langleyb0c235e2014-06-20 12:00:00 -0700883/* header_len is the length of the ClientHello header written so far, used to
884 * compute padding. It does not include the record header. Pass 0 if no padding
885 * is to be done. */
886unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700887 {
888 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700889 unsigned char *ret = buf;
890 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700891 /* See if we support any ECC ciphersuites */
892 int using_ecc = 0;
893 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
894 {
895 int i;
896 unsigned long alg_k, alg_a;
897 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
898
899 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
900 {
David Benjamin6f260012014-08-15 13:49:12 -0400901 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700902
903 alg_k = c->algorithm_mkey;
904 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400905 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700906 {
907 using_ecc = 1;
908 break;
909 }
910 }
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
913 /* don't add extensions for SSLv3 unless doing secure renegotiation */
914 if (s->client_version == SSL3_VERSION
915 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700916 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700917
918 ret+=2;
919
920 if (ret>=limit) return NULL; /* this really never occurs, but ... */
921
922 if (s->tlsext_hostname != NULL)
923 {
924 /* Add TLS extension servername to the Client Hello message */
925 unsigned long size_str;
926 long lenmax;
927
928 /* check for enough space.
929 4 for the servername type and entension length
930 2 for servernamelist length
931 1 for the hostname type
932 2 for hostname length
933 + hostname length
934 */
935
936 if ((lenmax = limit - ret - 9) < 0
937 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
938 return NULL;
939
940 /* extension type and length */
941 s2n(TLSEXT_TYPE_server_name,ret);
942 s2n(size_str+5,ret);
943
944 /* length of servername list */
945 s2n(size_str+3,ret);
946
947 /* hostname type, length and hostname */
948 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
949 s2n(size_str,ret);
950 memcpy(ret, s->tlsext_hostname, size_str);
951 ret+=size_str;
952 }
953
954 /* Add RI if renegotiating */
955 if (s->renegotiate)
956 {
957 int el;
958
959 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
960 {
961 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
962 return NULL;
963 }
964
Adam Langleyb0c235e2014-06-20 12:00:00 -0700965 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700966
967 s2n(TLSEXT_TYPE_renegotiate,ret);
968 s2n(el,ret);
969
970 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
971 {
972 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
973 return NULL;
974 }
975
976 ret += el;
977 }
978
Adam Langley95c29f32014-06-20 12:00:00 -0700979 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
980 {
981 int ticklen;
982 if (!s->new_session && s->session && s->session->tlsext_tick)
983 ticklen = s->session->tlsext_ticklen;
984 else if (s->session && s->tlsext_session_ticket &&
985 s->tlsext_session_ticket->data)
986 {
David Benjamin072c9532014-07-26 11:44:25 -0400987 s->session->tlsext_tick = BUF_memdup(
988 s->tlsext_session_ticket->data,
989 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700990 if (!s->session->tlsext_tick)
991 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -0400992 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700993 s->session->tlsext_ticklen = ticklen;
994 }
995 else
996 ticklen = 0;
997 if (ticklen == 0 && s->tlsext_session_ticket &&
998 s->tlsext_session_ticket->data == NULL)
999 goto skip_ext;
1000 /* Check for enough room 2 for extension type, 2 for len
1001 * rest for ticket
1002 */
1003 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1004 s2n(TLSEXT_TYPE_session_ticket,ret);
1005 s2n(ticklen,ret);
1006 if (ticklen)
1007 {
1008 memcpy(ret, s->session->tlsext_tick, ticklen);
1009 ret += ticklen;
1010 }
1011 }
1012 skip_ext:
1013
1014 if (SSL_USE_SIGALGS(s))
1015 {
1016 size_t salglen;
1017 const unsigned char *salg;
1018 salglen = tls12_get_psigalgs(s, &salg);
1019 if ((size_t)(limit - ret) < salglen + 6)
1020 return NULL;
1021 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1022 s2n(salglen + 2, ret);
1023 s2n(salglen, ret);
1024 memcpy(ret, salg, salglen);
1025 ret += salglen;
1026 }
1027
Adam Langley95c29f32014-06-20 12:00:00 -07001028 /* TODO(fork): we probably want OCSP stapling, but it currently pulls in a lot of code. */
1029#if 0
1030 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1031 {
1032 int i;
1033 long extlen, idlen, itmp;
1034 OCSP_RESPID *id;
1035
1036 idlen = 0;
1037 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1038 {
1039 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1040 itmp = i2d_OCSP_RESPID(id, NULL);
1041 if (itmp <= 0)
1042 return NULL;
1043 idlen += itmp + 2;
1044 }
1045
1046 if (s->tlsext_ocsp_exts)
1047 {
1048 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1049 if (extlen < 0)
1050 return NULL;
1051 }
1052 else
1053 extlen = 0;
1054
1055 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1056 s2n(TLSEXT_TYPE_status_request, ret);
1057 if (extlen + idlen > 0xFFF0)
1058 return NULL;
1059 s2n(extlen + idlen + 5, ret);
1060 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1061 s2n(idlen, ret);
1062 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1063 {
1064 /* save position of id len */
1065 unsigned char *q = ret;
1066 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1067 /* skip over id len */
1068 ret += 2;
1069 itmp = i2d_OCSP_RESPID(id, &ret);
1070 /* write id len */
1071 s2n(itmp, q);
1072 }
1073 s2n(extlen, ret);
1074 if (extlen > 0)
1075 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1076 }
1077#endif
1078
Adam Langley95c29f32014-06-20 12:00:00 -07001079 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1080 {
1081 /* The client advertises an emtpy extension to indicate its
1082 * support for Next Protocol Negotiation */
1083 if (limit - ret - 4 < 0)
1084 return NULL;
1085 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1086 s2n(0,ret);
1087 }
Adam Langley95c29f32014-06-20 12:00:00 -07001088
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001089 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1090 {
1091 /* The client advertises an empty extension to indicate its support for
1092 * certificate timestamps. */
1093 if (limit - ret - 4 < 0)
1094 return NULL;
1095 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1096 s2n(0,ret);
1097 }
1098
Adam Langley95c29f32014-06-20 12:00:00 -07001099 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1100 {
1101 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1102 return NULL;
1103 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1104 s2n(2 + s->alpn_client_proto_list_len,ret);
1105 s2n(s->alpn_client_proto_list_len,ret);
1106 memcpy(ret, s->alpn_client_proto_list,
1107 s->alpn_client_proto_list_len);
1108 ret += s->alpn_client_proto_list_len;
1109 }
1110
Adam Langley1258b6a2014-06-20 12:00:00 -07001111 if (s->tlsext_channel_id_enabled)
1112 {
1113 /* The client advertises an emtpy extension to indicate its
1114 * support for Channel ID. */
1115 if (limit - ret - 4 < 0)
1116 return NULL;
1117 if (s->ctx->tlsext_channel_id_enabled_new)
1118 s2n(TLSEXT_TYPE_channel_id_new,ret);
1119 else
1120 s2n(TLSEXT_TYPE_channel_id,ret);
1121 s2n(0,ret);
1122 }
1123
Adam Langley95c29f32014-06-20 12:00:00 -07001124 if(SSL_get_srtp_profiles(s))
1125 {
1126 int el;
1127
1128 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1129
Adam Langleyb0c235e2014-06-20 12:00:00 -07001130 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001131
1132 s2n(TLSEXT_TYPE_use_srtp,ret);
1133 s2n(el,ret);
1134
1135 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1136 {
1137 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1138 return NULL;
1139 }
1140 ret += el;
1141 }
1142
Adam Langleyc3174b72014-06-20 12:00:00 -07001143 if (using_ecc)
1144 {
1145 /* Add TLS extension ECPointFormats to the ClientHello message */
1146 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001147 const uint8_t *formats;
1148 const uint16_t *curves;
1149 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001150
David Benjamin072334d2014-07-13 16:24:27 -04001151 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001152
1153 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001154 if (formats_len > (size_t)lenmax) return NULL;
1155 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001156 {
1157 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1158 return NULL;
1159 }
1160
1161 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001162 s2n(formats_len + 1,ret);
1163 *(ret++) = (unsigned char)formats_len;
1164 memcpy(ret, formats, formats_len);
1165 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001166
1167 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001168 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001169
1170 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001171 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1172 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001173 {
1174 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1175 return NULL;
1176 }
1177
1178 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001179 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001180
1181 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1182 * elliptic_curve_list, but the examples use two bytes.
1183 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1184 * resolves this to two bytes.
1185 */
David Benjamin072334d2014-07-13 16:24:27 -04001186 s2n(curves_len * 2, ret);
1187 for (i = 0; i < curves_len; i++)
1188 {
1189 s2n(curves[i], ret);
1190 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001191 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001192
Adam Langley95c29f32014-06-20 12:00:00 -07001193#ifdef TLSEXT_TYPE_padding
1194 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001195 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001196 *
1197 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001198 * extensions it MUST always appear last. */
1199 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001200 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001201 header_len += ret - orig;
1202 if (header_len > 0xff && header_len < 0x200)
1203 {
1204 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001205 /* Extensions take at least four bytes to encode. Always
1206 * include least one byte of data if including the
1207 * extension. WebSphere Application Server 7.0 is
1208 * intolerant to the last extension being zero-length. */
1209 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001210 padding_len -= 4;
1211 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001212 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001213 if (limit - ret - 4 - (long)padding_len < 0)
1214 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langleyb0c235e2014-06-20 12:00:00 -07001216 s2n(TLSEXT_TYPE_padding, ret);
1217 s2n(padding_len, ret);
1218 memset(ret, 0, padding_len);
1219 ret += padding_len;
1220 }
Adam Langley95c29f32014-06-20 12:00:00 -07001221 }
1222#endif
1223
Adam Langleyb0c235e2014-06-20 12:00:00 -07001224 if ((extdatalen = ret-orig-2)== 0)
1225 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001226
Adam Langleyb0c235e2014-06-20 12:00:00 -07001227 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001228 return ret;
1229 }
1230
Adam Langleyb0c235e2014-06-20 12:00:00 -07001231unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001232 {
1233 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001234 unsigned char *orig = buf;
1235 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001236 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001237 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1238 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001239 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
Adam Langley95c29f32014-06-20 12:00:00 -07001240 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001241 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1242 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001243 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001244
1245 ret+=2;
1246 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1247
1248 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1249 {
1250 if ((long)(limit - ret - 4) < 0) return NULL;
1251
1252 s2n(TLSEXT_TYPE_server_name,ret);
1253 s2n(0,ret);
1254 }
1255
1256 if(s->s3->send_connection_binding)
1257 {
1258 int el;
1259
1260 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1261 {
1262 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1263 return NULL;
1264 }
1265
Adam Langleyb0c235e2014-06-20 12:00:00 -07001266 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001267
1268 s2n(TLSEXT_TYPE_renegotiate,ret);
1269 s2n(el,ret);
1270
1271 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1272 {
1273 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1274 return NULL;
1275 }
1276
1277 ret += el;
1278 }
1279
Adam Langley95c29f32014-06-20 12:00:00 -07001280 if (using_ecc)
1281 {
1282 const unsigned char *plist;
1283 size_t plistlen;
1284 /* Add TLS extension ECPointFormats to the ServerHello message */
1285 long lenmax;
1286
1287 tls1_get_formatlist(s, &plist, &plistlen);
1288
1289 if ((lenmax = limit - ret - 5) < 0) return NULL;
1290 if (plistlen > (size_t)lenmax) return NULL;
1291 if (plistlen > 255)
1292 {
1293 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1294 return NULL;
1295 }
1296
1297 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1298 s2n(plistlen + 1,ret);
1299 *(ret++) = (unsigned char) plistlen;
1300 memcpy(ret, plist, plistlen);
1301 ret+=plistlen;
1302
1303 }
1304 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001305
1306 if (s->tlsext_ticket_expected
1307 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1308 {
1309 if ((long)(limit - ret - 4) < 0) return NULL;
1310 s2n(TLSEXT_TYPE_session_ticket,ret);
1311 s2n(0,ret);
1312 }
1313
1314 if (s->tlsext_status_expected)
1315 {
1316 if ((long)(limit - ret - 4) < 0) return NULL;
1317 s2n(TLSEXT_TYPE_status_request,ret);
1318 s2n(0,ret);
1319 }
1320
Adam Langley95c29f32014-06-20 12:00:00 -07001321 if(s->srtp_profile)
1322 {
1323 int el;
1324
1325 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1326
Adam Langleyb0c235e2014-06-20 12:00:00 -07001327 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001328
1329 s2n(TLSEXT_TYPE_use_srtp,ret);
1330 s2n(el,ret);
1331
1332 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1333 {
1334 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1335 return NULL;
1336 }
1337 ret+=el;
1338 }
1339
Adam Langley95c29f32014-06-20 12:00:00 -07001340 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1341 s->s3->next_proto_neg_seen = 0;
1342 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1343 {
1344 const unsigned char *npa;
1345 unsigned int npalen;
1346 int r;
1347
1348 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1349 if (r == SSL_TLSEXT_ERR_OK)
1350 {
1351 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1352 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1353 s2n(npalen,ret);
1354 memcpy(ret, npa, npalen);
1355 ret += npalen;
1356 s->s3->next_proto_neg_seen = 1;
1357 }
1358 }
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langley95c29f32014-06-20 12:00:00 -07001360 if (s->s3->alpn_selected)
1361 {
David Benjamin03973092014-06-24 23:27:17 -04001362 const uint8_t *selected = s->s3->alpn_selected;
1363 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001364
1365 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1366 return NULL;
1367 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1368 s2n(3 + len,ret);
1369 s2n(1 + len,ret);
1370 *ret++ = len;
1371 memcpy(ret, selected, len);
1372 ret += len;
1373 }
1374
Adam Langley1258b6a2014-06-20 12:00:00 -07001375 /* If the client advertised support for Channel ID, and we have it
1376 * enabled, then we want to echo it back. */
1377 if (s->s3->tlsext_channel_id_valid)
1378 {
1379 if (limit - ret - 4 < 0)
1380 return NULL;
1381 if (s->s3->tlsext_channel_id_new)
1382 s2n(TLSEXT_TYPE_channel_id_new,ret);
1383 else
1384 s2n(TLSEXT_TYPE_channel_id,ret);
1385 s2n(0,ret);
1386 }
1387
Adam Langleyb0c235e2014-06-20 12:00:00 -07001388 if ((extdatalen = ret-orig-2) == 0)
1389 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001390
Adam Langleyb0c235e2014-06-20 12:00:00 -07001391 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001392 return ret;
1393 }
1394
Adam Langley95c29f32014-06-20 12:00:00 -07001395/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1396 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001397 * cbs: the contents of the extension, not including the type and length.
1398 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001399 * return.
1400 *
David Benjamindc72ff72014-06-25 12:36:10 -04001401 * returns: 1 on success. */
1402static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001403 {
Adam Langleyded93582014-07-31 15:23:51 -07001404 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001405 const unsigned char *selected;
1406 unsigned char selected_len;
1407 int r;
1408
1409 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001410 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001411
David Benjamindc72ff72014-06-25 12:36:10 -04001412 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1413 CBS_len(cbs) != 0 ||
1414 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001415 goto parse_error;
1416
David Benjamindc72ff72014-06-25 12:36:10 -04001417 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001418 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001419 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001420 {
David Benjamindc72ff72014-06-25 12:36:10 -04001421 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001422
David Benjamindc72ff72014-06-25 12:36:10 -04001423 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001424 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001425 }
1426
David Benjamindc72ff72014-06-25 12:36:10 -04001427 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1428 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1429 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001430 if (r == SSL_TLSEXT_ERR_OK) {
1431 if (s->s3->alpn_selected)
1432 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001433 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001434 if (!s->s3->alpn_selected)
1435 {
David Benjamindc72ff72014-06-25 12:36:10 -04001436 *out_alert = SSL_AD_INTERNAL_ERROR;
1437 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001438 }
Adam Langley95c29f32014-06-20 12:00:00 -07001439 s->s3->alpn_selected_len = selected_len;
1440 }
David Benjamindc72ff72014-06-25 12:36:10 -04001441 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001442
1443parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001444 *out_alert = SSL_AD_DECODE_ERROR;
1445 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001446 }
1447
David Benjamindc72ff72014-06-25 12:36:10 -04001448static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001449 {
Adam Langley95c29f32014-06-20 12:00:00 -07001450 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001451 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001452 size_t i;
1453
1454 s->servername_done = 0;
1455 s->tlsext_status_type = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001456 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langley95c29f32014-06-20 12:00:00 -07001458 if (s->s3->alpn_selected)
1459 {
1460 OPENSSL_free(s->s3->alpn_selected);
1461 s->s3->alpn_selected = NULL;
1462 }
1463
Adam Langley95c29f32014-06-20 12:00:00 -07001464 /* Clear any signature algorithms extension received */
1465 if (s->cert->peer_sigalgs)
1466 {
1467 OPENSSL_free(s->cert->peer_sigalgs);
1468 s->cert->peer_sigalgs = NULL;
1469 }
1470 /* Clear any shared sigtnature algorithms */
1471 if (s->cert->shared_sigalgs)
1472 {
1473 OPENSSL_free(s->cert->shared_sigalgs);
1474 s->cert->shared_sigalgs = NULL;
1475 }
1476 /* Clear certificate digests and validity flags */
1477 for (i = 0; i < SSL_PKEY_NUM; i++)
1478 {
1479 s->cert->pkeys[i].digest = NULL;
1480 s->cert->pkeys[i].valid_flags = 0;
1481 }
1482
David Benjamin35a7a442014-07-05 00:23:20 -04001483 /* TODO(fork): we probably want OCSP stapling support, but this pulls in
1484 * a lot of code. */
1485#if 0
1486 /* Clear OCSP state. */
1487 s->tlsext_status_type = -1;
1488 if (s->tlsext_ocsp_ids)
1489 {
1490 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
1491 s->tlsext_ocsp_ids = NULL;
1492 }
1493 if (s->tlsext_ocsp_exts)
1494 {
1495 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);
1496 s->tlsext_ocsp_exts = NULL;
1497 }
1498#endif
1499
David Benjamindc72ff72014-06-25 12:36:10 -04001500 /* There may be no extensions. */
1501 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001502 {
David Benjamindc72ff72014-06-25 12:36:10 -04001503 goto ri_check;
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
David Benjamin35a7a442014-07-05 00:23:20 -04001506 /* Decode the extensions block and check it is valid. */
1507 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1508 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001509 {
1510 *out_alert = SSL_AD_DECODE_ERROR;
1511 return 0;
1512 }
1513
David Benjamindc72ff72014-06-25 12:36:10 -04001514 while (CBS_len(&extensions) != 0)
1515 {
1516 uint16_t type;
1517 CBS extension;
1518
1519 /* Decode the next extension. */
1520 if (!CBS_get_u16(&extensions, &type) ||
1521 !CBS_get_u16_length_prefixed(&extensions, &extension))
1522 {
1523 *out_alert = SSL_AD_DECODE_ERROR;
1524 return 0;
1525 }
1526
Adam Langley95c29f32014-06-20 12:00:00 -07001527 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001528 {
1529 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1530 CBS_len(&extension), s->tlsext_debug_arg);
1531 }
1532
Adam Langley95c29f32014-06-20 12:00:00 -07001533/* The servername extension is treated as follows:
1534
1535 - Only the hostname type is supported with a maximum length of 255.
1536 - The servername is rejected if too long or if it contains zeros,
1537 in which case an fatal alert is generated.
1538 - The servername field is maintained together with the session cache.
1539 - When a session is resumed, the servername call back invoked in order
1540 to allow the application to position itself to the right context.
1541 - The servername is acknowledged if it is new for a session or when
1542 it is identical to a previously used for the same session.
1543 Applications can control the behaviour. They can at any time
1544 set a 'desirable' servername for a new SSL object. This can be the
1545 case for example with HTTPS when a Host: header field is received and
1546 a renegotiation is requested. In this case, a possible servername
1547 presented in the new client hello is only acknowledged if it matches
1548 the value of the Host: field.
1549 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1550 if they provide for changing an explicit servername context for the session,
1551 i.e. when the session has been established with a servername extension.
1552 - On session reconnect, the servername extension may be absent.
1553
1554*/
1555
1556 if (type == TLSEXT_TYPE_server_name)
1557 {
David Benjamindc72ff72014-06-25 12:36:10 -04001558 CBS server_name_list;
1559
1560 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1561 CBS_len(&server_name_list) < 1 ||
1562 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001563 {
David Benjamindc72ff72014-06-25 12:36:10 -04001564 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001565 return 0;
1566 }
Adam Langley95c29f32014-06-20 12:00:00 -07001567
David Benjamindc72ff72014-06-25 12:36:10 -04001568 /* Decode each ServerName in the extension. */
1569 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001570 {
David Benjamindc72ff72014-06-25 12:36:10 -04001571 uint8_t name_type;
1572 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001573
David Benjamindc72ff72014-06-25 12:36:10 -04001574 /* Decode the NameType. */
1575 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001576 {
David Benjamindc72ff72014-06-25 12:36:10 -04001577 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001578 return 0;
1579 }
David Benjamindc72ff72014-06-25 12:36:10 -04001580
David Benjamindc72ff72014-06-25 12:36:10 -04001581 /* Only host_name is supported. */
1582 if (name_type != TLSEXT_NAMETYPE_host_name)
1583 continue;
1584
1585 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001586 {
David Benjamindc72ff72014-06-25 12:36:10 -04001587 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001588 {
David Benjamindc72ff72014-06-25 12:36:10 -04001589 /* The ServerNameList MUST NOT
1590 contain more than one name of
1591 the same name_type. */
1592 *out_alert = SSL_AD_DECODE_ERROR;
1593 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001594 }
Adam Langley95c29f32014-06-20 12:00:00 -07001595
David Benjamindc72ff72014-06-25 12:36:10 -04001596 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1597 CBS_len(&host_name) < 1)
1598 {
1599 *out_alert = SSL_AD_DECODE_ERROR;
1600 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001601 }
Adam Langley95c29f32014-06-20 12:00:00 -07001602
David Benjamined439582014-07-14 19:13:02 -04001603 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1604 CBS_contains_zero_byte(&host_name))
David Benjamindc72ff72014-06-25 12:36:10 -04001605 {
1606 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1607 return 0;
1608 }
1609
1610 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001611 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001612 {
1613 *out_alert = SSL_AD_INTERNAL_ERROR;
1614 return 0;
1615 }
1616 s->servername_done = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001617 }
David Benjamindc72ff72014-06-25 12:36:10 -04001618 else
1619 {
1620 s->servername_done = s->session->tlsext_hostname
1621 && strlen(s->session->tlsext_hostname) == CBS_len(&host_name)
1622 && strncmp(s->session->tlsext_hostname,
1623 (char *)CBS_data(&host_name), CBS_len(&host_name)) == 0;
1624 }
Adam Langley95c29f32014-06-20 12:00:00 -07001625 }
Adam Langley95c29f32014-06-20 12:00:00 -07001626 }
1627
Adam Langley95c29f32014-06-20 12:00:00 -07001628 else if (type == TLSEXT_TYPE_ec_point_formats)
1629 {
David Benjamindc72ff72014-06-25 12:36:10 -04001630 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001631
David Benjamindc72ff72014-06-25 12:36:10 -04001632 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1633 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001634 {
David Benjamindc72ff72014-06-25 12:36:10 -04001635 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001636 return 0;
1637 }
David Benjamindc72ff72014-06-25 12:36:10 -04001638
Adam Langley95c29f32014-06-20 12:00:00 -07001639 if (!s->hit)
1640 {
David Benjamindc72ff72014-06-25 12:36:10 -04001641 if (!CBS_stow(&ec_point_format_list,
1642 &s->session->tlsext_ecpointformatlist,
1643 &s->session->tlsext_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001644 {
David Benjamindc72ff72014-06-25 12:36:10 -04001645 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001646 return 0;
1647 }
Adam Langley95c29f32014-06-20 12:00:00 -07001648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649 }
1650 else if (type == TLSEXT_TYPE_elliptic_curves)
1651 {
David Benjamindc72ff72014-06-25 12:36:10 -04001652 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001653 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001654
David Benjamindc72ff72014-06-25 12:36:10 -04001655 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001656 CBS_len(&elliptic_curve_list) == 0 ||
1657 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001658 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001659 {
David Benjamindc72ff72014-06-25 12:36:10 -04001660 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001661 return 0;
1662 }
David Benjamindc72ff72014-06-25 12:36:10 -04001663
Adam Langley95c29f32014-06-20 12:00:00 -07001664 if (!s->hit)
1665 {
David Benjamin072334d2014-07-13 16:24:27 -04001666 if (s->session->tlsext_ellipticcurvelist)
1667 {
1668 OPENSSL_free(s->session->tlsext_ellipticcurvelist);
1669 s->session->tlsext_ellipticcurvelist_length = 0;
1670 }
1671 s->session->tlsext_ellipticcurvelist =
1672 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1673 if (s->session->tlsext_ellipticcurvelist == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001674 {
David Benjamindc72ff72014-06-25 12:36:10 -04001675 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001676 return 0;
1677 }
David Benjamin072334d2014-07-13 16:24:27 -04001678 num_curves = CBS_len(&elliptic_curve_list) / 2;
1679 for (i = 0; i < num_curves; i++)
1680 {
1681 if (!CBS_get_u16(&elliptic_curve_list,
1682 &s->session->tlsext_ellipticcurvelist[i]))
1683 {
1684 *out_alert = SSL_AD_INTERNAL_ERROR;
1685 return 0;
1686 }
1687 }
1688 if (CBS_len(&elliptic_curve_list) != 0)
1689 {
1690 *out_alert = SSL_AD_INTERNAL_ERROR;
1691 return 0;
1692 }
1693 s->session->tlsext_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001694 }
Adam Langley95c29f32014-06-20 12:00:00 -07001695 }
Adam Langley95c29f32014-06-20 12:00:00 -07001696 else if (type == TLSEXT_TYPE_session_ticket)
1697 {
1698 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001699 !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 -07001700 {
David Benjamindc72ff72014-06-25 12:36:10 -04001701 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001702 return 0;
1703 }
1704 }
1705 else if (type == TLSEXT_TYPE_renegotiate)
1706 {
David Benjamindc72ff72014-06-25 12:36:10 -04001707 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001708 return 0;
1709 renegotiate_seen = 1;
1710 }
1711 else if (type == TLSEXT_TYPE_signature_algorithms)
1712 {
David Benjamindc72ff72014-06-25 12:36:10 -04001713 CBS supported_signature_algorithms;
1714
David Benjamindc72ff72014-06-25 12:36:10 -04001715 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1716 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001717 {
David Benjamindc72ff72014-06-25 12:36:10 -04001718 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001719 return 0;
1720 }
David Benjamindc72ff72014-06-25 12:36:10 -04001721
1722 /* Ensure the signature algorithms are non-empty. It
1723 * contains a list of SignatureAndHashAlgorithms
1724 * which are two bytes each. */
1725 if (CBS_len(&supported_signature_algorithms) == 0 ||
1726 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001727 {
David Benjamindc72ff72014-06-25 12:36:10 -04001728 *out_alert = SSL_AD_DECODE_ERROR;
1729 return 0;
1730 }
1731
David Benjamincd996942014-07-20 16:23:51 -04001732 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001733 {
1734 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001735 return 0;
1736 }
1737 /* If sigalgs received and no shared algorithms fatal
1738 * error.
1739 */
1740 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1741 {
1742 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001743 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001744 return 0;
1745 }
1746 }
1747
1748 /* TODO(fork): we probably want OCSP stapling support, but this pulls in a lot of code. */
1749#if 0
1750 else if (type == TLSEXT_TYPE_status_request)
1751 {
David Benjamindc72ff72014-06-25 12:36:10 -04001752 uint8_t status_type;
1753 CBS responder_id_list;
1754 CBS request_extensions;
1755
David Benjamindc72ff72014-06-25 12:36:10 -04001756 if (!CBS_get_u8(&extension, &status_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001757 {
David Benjamindc72ff72014-06-25 12:36:10 -04001758 *out_alert = SSL_AD_DECODE_ERROR;
1759 return 0;
1760 }
Adam Langley95c29f32014-06-20 12:00:00 -07001761
David Benjamindc72ff72014-06-25 12:36:10 -04001762 /* Only OCSP is supported. */
1763 if (status_type != TLSEXT_STATUSTYPE_ocsp)
1764 continue;
Adam Langley95c29f32014-06-20 12:00:00 -07001765
David Benjamindc72ff72014-06-25 12:36:10 -04001766 s->tlsext_status_type = status_type;
1767
1768 /* Extension consists of a responder_id_list and
1769 * request_extensions. */
1770 if (!CBS_get_u16_length_prefixed(&extension, &responder_id_list) ||
David Benjamin8f3234b2014-07-16 13:09:22 -04001771 !CBS_get_u16_length_prefixed(&extension, &request_extensions) ||
David Benjamindc72ff72014-06-25 12:36:10 -04001772 CBS_len(&extension) != 0)
1773 {
1774 *out_alert = SSL_AD_DECODE_ERROR;
1775 return 0;
1776 }
1777
1778 if (CBS_len(&responder_id_list) > 0)
1779 {
1780 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
1781 if (s->tlsext_ocsp_ids == NULL)
1782 {
1783 *out_alert = SSL_AD_INTERNAL_ERROR;
1784 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001785 }
1786 }
David Benjamindc72ff72014-06-25 12:36:10 -04001787
1788 /* Parse out the responder IDs. */
1789 while (CBS_len(&responder_id_list) > 0)
1790 {
1791 CBS responder_id;
1792 OCSP_RESPID *id;
1793 const uint8_t *data;
1794
1795 /* Each ResponderID must have size at least 1. */
1796 if (!CBS_get_u16_length_prefixed(&responder_id_list, &responder_id) ||
1797 CBS_len(&responder_id) < 1)
1798 {
1799 *out_alert = SSL_AD_DECODE_ERROR;
1800 return 0;
1801 }
1802
1803 /* TODO(fork): Add CBS versions of d2i_FOO_BAR. */
1804 data = CBS_data(&responder_id);
1805 id = d2i_OCSP_RESPID(NULL, &data, CBS_len(&responder_id));
1806 if (!id)
1807 {
1808 *out_alert = SSL_AD_DECODE_ERROR;
1809 return 0;
1810 }
1811 if (!CBS_skip(&responder_id, data - CBS_data(&responder_id)))
1812 {
1813 /* This should never happen. */
1814 *out_alert = SSL_AD_INTERNAL_ERROR;
1815 OCSP_RESPID_free(id);
1816 return 0;
1817 }
1818 if (CBS_len(&responder_id) != 0)
1819 {
1820 *out_alert = SSL_AD_DECODE_ERROR;
1821 OCSP_RESPID_free(id);
1822 return 0;
1823 }
1824
1825 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id))
1826 {
1827 *out_alert = SSL_AD_INTERNAL_ERROR;
1828 OCSP_RESPID_free(id);
1829 return 0;
1830 }
1831 }
1832
1833 /* Parse out request_extensions. */
1834 if (CBS_len(&request_extensions) > 0)
1835 {
1836 const uint8_t *data;
1837
1838 data = CBS_data(&request_extensions);
1839 s->tlsext_ocsp_exts = d2i_X509_EXTENSIONS(NULL,
1840 &data, CBS_len(&request_extensions));
1841 if (s->tlsext_ocsp_exts == NULL)
1842 {
1843 *out_alert = SSL_AD_DECODE_ERROR;
1844 return 0;
1845 }
1846 if (!CBS_skip(&request_extensions, data - CBS_data(&request_extensions)))
1847 {
1848 /* This should never happen. */
1849 *out_alert = SSL_AD_INTERNAL_ERROR;
1850 return 0;
1851 }
1852 if (CBS_len(&request_extensions) != 0)
1853 {
1854 *out_alert = SSL_AD_DECODE_ERROR;
1855 return 0;
1856 }
1857 }
Adam Langley95c29f32014-06-20 12:00:00 -07001858 }
1859#endif
1860
Adam Langley95c29f32014-06-20 12:00:00 -07001861 else if (type == TLSEXT_TYPE_next_proto_neg &&
1862 s->s3->tmp.finish_md_len == 0 &&
1863 s->s3->alpn_selected == NULL)
1864 {
David Benjamindc72ff72014-06-25 12:36:10 -04001865 /* The extension must be empty. */
1866 if (CBS_len(&extension) != 0)
1867 {
1868 *out_alert = SSL_AD_DECODE_ERROR;
1869 return 0;
1870 }
1871
Adam Langley95c29f32014-06-20 12:00:00 -07001872 /* We shouldn't accept this extension on a
1873 * renegotiation.
1874 *
1875 * s->new_session will be set on renegotiation, but we
1876 * probably shouldn't rely that it couldn't be set on
1877 * the initial renegotation too in certain cases (when
1878 * there's some other reason to disallow resuming an
1879 * earlier session -- the current code won't be doing
1880 * anything like that, but this might change).
1881
1882 * A valid sign that there's been a previous handshake
1883 * in this connection is if s->s3->tmp.finish_md_len >
1884 * 0. (We are talking about a check that will happen
1885 * in the Hello protocol round, well before a new
1886 * Finished message could have been computed.) */
1887 s->s3->next_proto_neg_seen = 1;
1888 }
Adam Langley95c29f32014-06-20 12:00:00 -07001889
1890 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1891 s->ctx->alpn_select_cb &&
1892 s->s3->tmp.finish_md_len == 0)
1893 {
David Benjamindc72ff72014-06-25 12:36:10 -04001894 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001895 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001896 /* ALPN takes precedence over NPN. */
1897 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001898 }
1899
Adam Langley1258b6a2014-06-20 12:00:00 -07001900 else if (type == TLSEXT_TYPE_channel_id &&
1901 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001902 {
1903 /* The extension must be empty. */
1904 if (CBS_len(&extension) != 0)
1905 {
1906 *out_alert = SSL_AD_DECODE_ERROR;
1907 return 0;
1908 }
1909
Adam Langley1258b6a2014-06-20 12:00:00 -07001910 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001911 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001912
1913 else if (type == TLSEXT_TYPE_channel_id_new &&
1914 s->tlsext_channel_id_enabled)
1915 {
David Benjamindc72ff72014-06-25 12:36:10 -04001916 /* The extension must be empty. */
1917 if (CBS_len(&extension) != 0)
1918 {
1919 *out_alert = SSL_AD_DECODE_ERROR;
1920 return 0;
1921 }
1922
Adam Langley1258b6a2014-06-20 12:00:00 -07001923 s->s3->tlsext_channel_id_valid = 1;
1924 s->s3->tlsext_channel_id_new = 1;
1925 }
1926
1927
Adam Langley95c29f32014-06-20 12:00:00 -07001928 /* session ticket processed earlier */
1929 else if (type == TLSEXT_TYPE_use_srtp)
1930 {
David Benjamindc72ff72014-06-25 12:36:10 -04001931 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001932 return 0;
1933 }
Adam Langley95c29f32014-06-20 12:00:00 -07001934 }
1935
Adam Langley95c29f32014-06-20 12:00:00 -07001936 ri_check:
1937
1938 /* Need RI if renegotiating */
1939
1940 if (!renegotiate_seen && s->renegotiate &&
1941 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1942 {
David Benjamindc72ff72014-06-25 12:36:10 -04001943 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley95c29f32014-06-20 12:00:00 -07001944 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1945 return 0;
1946 }
1947 /* If no signature algorithms extension set default values */
1948 if (!s->cert->peer_sigalgs)
1949 ssl_cert_set_default_md(s->cert);
1950
1951 return 1;
1952 }
1953
David Benjamindc72ff72014-06-25 12:36:10 -04001954int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001955 {
David Benjamindc72ff72014-06-25 12:36:10 -04001956 int alert = -1;
1957 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001958 {
David Benjamindc72ff72014-06-25 12:36:10 -04001959 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001960 return 0;
1961 }
1962
1963 if (ssl_check_clienthello_tlsext_early(s) <= 0)
1964 {
David Benjamin9d28c752014-07-05 00:43:48 -04001965 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001966 return 0;
1967 }
1968 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001969 }
Adam Langley95c29f32014-06-20 12:00:00 -07001970
Adam Langley95c29f32014-06-20 12:00:00 -07001971/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1972 * elements of zero length are allowed and the set of elements must exactly fill
1973 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001974static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001975 {
David Benjamin03973092014-06-24 23:27:17 -04001976 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001977
David Benjamin03973092014-06-24 23:27:17 -04001978 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001979 {
David Benjamin03973092014-06-24 23:27:17 -04001980 CBS proto;
1981 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1982 CBS_len(&proto) == 0)
1983 {
Adam Langley95c29f32014-06-20 12:00:00 -07001984 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001985 }
Adam Langley95c29f32014-06-20 12:00:00 -07001986 }
David Benjamin03973092014-06-24 23:27:17 -04001987 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001988 }
Adam Langley95c29f32014-06-20 12:00:00 -07001989
David Benjamin03973092014-06-24 23:27:17 -04001990static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001991 {
Adam Langley95c29f32014-06-20 12:00:00 -07001992 int tlsext_servername = 0;
1993 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001994 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001995
Adam Langley95c29f32014-06-20 12:00:00 -07001996 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001997
David Benjamin64442872014-07-21 17:43:45 -04001998 s->tlsext_ticket_expected = 0;
1999
Adam Langley95c29f32014-06-20 12:00:00 -07002000 if (s->s3->alpn_selected)
2001 {
2002 OPENSSL_free(s->s3->alpn_selected);
2003 s->s3->alpn_selected = NULL;
2004 }
2005
David Benjamin03973092014-06-24 23:27:17 -04002006 /* There may be no extensions. */
2007 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002008 {
David Benjamin03973092014-06-24 23:27:17 -04002009 goto ri_check;
2010 }
2011
David Benjamin35a7a442014-07-05 00:23:20 -04002012 /* Decode the extensions block and check it is valid. */
2013 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2014 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04002015 {
2016 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002017 return 0;
2018 }
2019
David Benjamin03973092014-06-24 23:27:17 -04002020 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002021 {
David Benjamin03973092014-06-24 23:27:17 -04002022 uint16_t type;
2023 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002024
David Benjamin03973092014-06-24 23:27:17 -04002025 /* Decode the next extension. */
2026 if (!CBS_get_u16(&extensions, &type) ||
2027 !CBS_get_u16_length_prefixed(&extensions, &extension))
2028 {
2029 *out_alert = SSL_AD_DECODE_ERROR;
2030 return 0;
2031 }
Adam Langley95c29f32014-06-20 12:00:00 -07002032
2033 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04002034 {
2035 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
2036 CBS_len(&extension), s->tlsext_debug_arg);
2037 }
Adam Langley95c29f32014-06-20 12:00:00 -07002038
2039 if (type == TLSEXT_TYPE_server_name)
2040 {
David Benjamin03973092014-06-24 23:27:17 -04002041 /* The extension must be empty. */
2042 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002043 {
David Benjamin03973092014-06-24 23:27:17 -04002044 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002045 return 0;
2046 }
David Benjamin03973092014-06-24 23:27:17 -04002047 /* We must have sent it in ClientHello. */
2048 if (s->tlsext_hostname == NULL)
2049 {
2050 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2051 return 0;
2052 }
2053 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002054 }
Adam Langley95c29f32014-06-20 12:00:00 -07002055 else if (type == TLSEXT_TYPE_ec_point_formats)
2056 {
David Benjamin03973092014-06-24 23:27:17 -04002057 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07002058
David Benjamin03973092014-06-24 23:27:17 -04002059 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
2060 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002061 {
David Benjamin03973092014-06-24 23:27:17 -04002062 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002063 return 0;
2064 }
David Benjamin03973092014-06-24 23:27:17 -04002065
Adam Langley5ba06a72014-08-06 17:27:31 -07002066 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07002067 {
Adam Langley5ba06a72014-08-06 17:27:31 -07002068 if (!CBS_stow(&ec_point_format_list,
2069 &s->session->tlsext_ecpointformatlist,
2070 &s->session->tlsext_ecpointformatlist_length))
2071 {
2072 *out_alert = SSL_AD_INTERNAL_ERROR;
2073 return 0;
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075 }
Adam Langley95c29f32014-06-20 12:00:00 -07002076 }
Adam Langley95c29f32014-06-20 12:00:00 -07002077 else if (type == TLSEXT_TYPE_session_ticket)
2078 {
2079 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04002080 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
2081 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07002082 {
David Benjamin03973092014-06-24 23:27:17 -04002083 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002084 return 0;
2085 }
David Benjamin03973092014-06-24 23:27:17 -04002086
2087 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002088 {
David Benjamin03973092014-06-24 23:27:17 -04002089 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002090 return 0;
2091 }
David Benjamin03973092014-06-24 23:27:17 -04002092
Adam Langley95c29f32014-06-20 12:00:00 -07002093 s->tlsext_ticket_expected = 1;
2094 }
Adam Langley95c29f32014-06-20 12:00:00 -07002095 else if (type == TLSEXT_TYPE_status_request)
2096 {
David Benjamin03973092014-06-24 23:27:17 -04002097 /* The extension MUST be empty and may only sent if
2098 * we've requested a status request message. */
2099 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002100 {
David Benjamin03973092014-06-24 23:27:17 -04002101 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002102 return 0;
2103 }
David Benjamin03973092014-06-24 23:27:17 -04002104 if (s->tlsext_status_type == -1)
2105 {
2106 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2107 return 0;
2108 }
2109 /* Set a flag to expect a CertificateStatus message */
Adam Langley95c29f32014-06-20 12:00:00 -07002110 s->tlsext_status_expected = 1;
2111 }
David Benjamin03973092014-06-24 23:27:17 -04002112 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
2113 unsigned char *selected;
2114 unsigned char selected_len;
2115
2116 /* We must have requested it. */
2117 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002118 {
David Benjamin03973092014-06-24 23:27:17 -04002119 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2120 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002121 }
Adam Langley95c29f32014-06-20 12:00:00 -07002122
David Benjamin03973092014-06-24 23:27:17 -04002123 /* The data must be valid. */
2124 if (!ssl_next_proto_validate(&extension))
2125 {
2126 *out_alert = SSL_AD_DECODE_ERROR;
2127 return 0;
2128 }
2129
2130 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
2131 CBS_data(&extension), CBS_len(&extension),
2132 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2133 {
2134 *out_alert = SSL_AD_INTERNAL_ERROR;
2135 return 0;
2136 }
2137
2138 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2139 if (s->next_proto_negotiated == NULL)
2140 {
2141 *out_alert = SSL_AD_INTERNAL_ERROR;
2142 return 0;
2143 }
2144 s->next_proto_negotiated_len = selected_len;
2145 s->s3->next_proto_neg_seen = 1;
2146 }
Adam Langley95c29f32014-06-20 12:00:00 -07002147 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2148 {
David Benjamin03973092014-06-24 23:27:17 -04002149 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002150
2151 /* We must have requested it. */
2152 if (s->alpn_client_proto_list == NULL)
2153 {
David Benjamin03973092014-06-24 23:27:17 -04002154 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002155 return 0;
2156 }
David Benjamin03973092014-06-24 23:27:17 -04002157
2158 /* The extension data consists of a ProtocolNameList
2159 * which must have exactly one ProtocolName. Each of
2160 * these is length-prefixed. */
2161 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2162 CBS_len(&extension) != 0 ||
2163 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2164 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002165 {
David Benjamin03973092014-06-24 23:27:17 -04002166 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002167 return 0;
2168 }
David Benjamin03973092014-06-24 23:27:17 -04002169
2170 if (!CBS_stow(&protocol_name,
2171 &s->s3->alpn_selected,
2172 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002173 {
David Benjamin03973092014-06-24 23:27:17 -04002174 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002175 return 0;
2176 }
Adam Langley95c29f32014-06-20 12:00:00 -07002177 }
2178
Adam Langley1258b6a2014-06-20 12:00:00 -07002179 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002180 {
2181 if (CBS_len(&extension) != 0)
2182 {
2183 *out_alert = SSL_AD_DECODE_ERROR;
2184 return 0;
2185 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002186 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002187 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002188 else if (type == TLSEXT_TYPE_channel_id_new)
2189 {
David Benjamin03973092014-06-24 23:27:17 -04002190 if (CBS_len(&extension) != 0)
2191 {
2192 *out_alert = SSL_AD_DECODE_ERROR;
2193 return 0;
2194 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002195 s->s3->tlsext_channel_id_valid = 1;
2196 s->s3->tlsext_channel_id_new = 1;
2197 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002198 else if (type == TLSEXT_TYPE_certificate_timestamp)
2199 {
2200 if (CBS_len(&extension) == 0)
2201 {
2202 *out_alert = SSL_AD_DECODE_ERROR;
2203 return 0;
2204 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002205
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002206 /* Session resumption uses the original session information. */
2207 if (!s->hit)
2208 {
2209 if (!CBS_stow(&extension,
2210 &s->session->tlsext_signed_cert_timestamp_list,
2211 &s->session->tlsext_signed_cert_timestamp_list_length))
2212 {
2213 *out_alert = SSL_AD_INTERNAL_ERROR;
2214 return 0;
2215 }
2216 }
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218 else if (type == TLSEXT_TYPE_renegotiate)
2219 {
David Benjamin03973092014-06-24 23:27:17 -04002220 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002221 return 0;
2222 renegotiate_seen = 1;
2223 }
Adam Langley95c29f32014-06-20 12:00:00 -07002224 else if (type == TLSEXT_TYPE_use_srtp)
2225 {
David Benjamin03973092014-06-24 23:27:17 -04002226 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002227 return 0;
2228 }
Adam Langley95c29f32014-06-20 12:00:00 -07002229 }
2230
2231 if (!s->hit && tlsext_servername == 1)
2232 {
2233 if (s->tlsext_hostname)
2234 {
2235 if (s->session->tlsext_hostname == NULL)
2236 {
2237 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2238 if (!s->session->tlsext_hostname)
2239 {
David Benjamin03973092014-06-24 23:27:17 -04002240 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002241 return 0;
2242 }
2243 }
2244 else
2245 {
David Benjamin03973092014-06-24 23:27:17 -04002246 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002247 return 0;
2248 }
2249 }
2250 }
2251
Adam Langley95c29f32014-06-20 12:00:00 -07002252 ri_check:
2253
2254 /* Determine if we need to see RI. Strictly speaking if we want to
2255 * avoid an attack we should *always* see RI even on initial server
2256 * hello because the client doesn't see any renegotiation during an
2257 * attack. However this would mean we could not connect to any server
2258 * which doesn't support RI so for the immediate future tolerate RI
2259 * absence on initial connect only.
2260 */
2261 if (!renegotiate_seen
2262 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2263 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2264 {
David Benjamin03973092014-06-24 23:27:17 -04002265 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002266 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002267 return 0;
2268 }
2269
2270 return 1;
2271 }
2272
2273
2274int ssl_prepare_clienthello_tlsext(SSL *s)
2275 {
Adam Langley95c29f32014-06-20 12:00:00 -07002276 return 1;
2277 }
2278
2279int ssl_prepare_serverhello_tlsext(SSL *s)
2280 {
2281 return 1;
2282 }
2283
2284static int ssl_check_clienthello_tlsext_early(SSL *s)
2285 {
2286 int ret=SSL_TLSEXT_ERR_NOACK;
2287 int al = SSL_AD_UNRECOGNIZED_NAME;
2288
Adam Langley95c29f32014-06-20 12:00:00 -07002289 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2290 * ssl3_choose_cipher in s3_lib.c.
2291 */
2292 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2293 * ssl3_choose_cipher in s3_lib.c.
2294 */
Adam Langley95c29f32014-06-20 12:00:00 -07002295
2296 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2297 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2298 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2299 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2300
Adam Langley95c29f32014-06-20 12:00:00 -07002301 switch (ret)
2302 {
2303 case SSL_TLSEXT_ERR_ALERT_FATAL:
2304 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2305 return -1;
2306
2307 case SSL_TLSEXT_ERR_ALERT_WARNING:
2308 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2309 return 1;
2310
2311 case SSL_TLSEXT_ERR_NOACK:
2312 s->servername_done=0;
2313 default:
2314 return 1;
2315 }
2316 }
2317
2318int ssl_check_clienthello_tlsext_late(SSL *s)
2319 {
2320 int ret = SSL_TLSEXT_ERR_OK;
2321 int al;
2322
2323 /* If status request then ask callback what to do.
2324 * Note: this must be called after servername callbacks in case
2325 * the certificate has changed, and must be called after the cipher
2326 * has been chosen because this may influence which certificate is sent
2327 */
2328 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2329 {
2330 int r;
2331 CERT_PKEY *certpkey;
2332 certpkey = ssl_get_server_send_pkey(s);
2333 /* If no certificate can't return certificate status */
2334 if (certpkey == NULL)
2335 {
2336 s->tlsext_status_expected = 0;
2337 return 1;
2338 }
2339 /* Set current certificate to one we will use so
2340 * SSL_get_certificate et al can pick it up.
2341 */
2342 s->cert->key = certpkey;
2343 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2344 switch (r)
2345 {
2346 /* We don't want to send a status request response */
2347 case SSL_TLSEXT_ERR_NOACK:
2348 s->tlsext_status_expected = 0;
2349 break;
2350 /* status request response should be sent */
2351 case SSL_TLSEXT_ERR_OK:
2352 if (s->tlsext_ocsp_resp)
2353 s->tlsext_status_expected = 1;
2354 else
2355 s->tlsext_status_expected = 0;
2356 break;
2357 /* something bad happened */
2358 case SSL_TLSEXT_ERR_ALERT_FATAL:
2359 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2360 al = SSL_AD_INTERNAL_ERROR;
2361 goto err;
2362 }
2363 }
2364 else
2365 s->tlsext_status_expected = 0;
2366
2367 err:
2368 switch (ret)
2369 {
2370 case SSL_TLSEXT_ERR_ALERT_FATAL:
2371 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2372 return -1;
2373
2374 case SSL_TLSEXT_ERR_ALERT_WARNING:
2375 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2376 return 1;
2377
2378 default:
2379 return 1;
2380 }
2381 }
2382
2383int ssl_check_serverhello_tlsext(SSL *s)
2384 {
2385 int ret=SSL_TLSEXT_ERR_NOACK;
2386 int al = SSL_AD_UNRECOGNIZED_NAME;
2387
Adam Langley95c29f32014-06-20 12:00:00 -07002388 /* If we are client and using an elliptic curve cryptography cipher
2389 * suite, then if server returns an EC point formats lists extension
2390 * it must contain uncompressed.
2391 */
2392 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2393 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2394 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2395 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
David Benjamin0da0e182014-08-19 16:20:28 -04002396 ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07002397 {
2398 /* we are using an ECC cipher */
2399 size_t i;
2400 unsigned char *list;
2401 int found_uncompressed = 0;
2402 list = s->session->tlsext_ecpointformatlist;
2403 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2404 {
2405 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
2406 {
2407 found_uncompressed = 1;
2408 break;
2409 }
2410 }
2411 if (!found_uncompressed)
2412 {
2413 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2414 return -1;
2415 }
2416 }
2417 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002418
2419 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2420 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2421 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2422 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2423
Adam Langley95c29f32014-06-20 12:00:00 -07002424 /* If we've requested certificate status and we wont get one
2425 * tell the callback
2426 */
2427 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2428 && s->ctx && s->ctx->tlsext_status_cb)
2429 {
2430 int r;
2431 /* Set resp to NULL, resplen to -1 so callback knows
2432 * there is no response.
2433 */
2434 if (s->tlsext_ocsp_resp)
2435 {
2436 OPENSSL_free(s->tlsext_ocsp_resp);
2437 s->tlsext_ocsp_resp = NULL;
2438 }
2439 s->tlsext_ocsp_resplen = -1;
2440 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2441 if (r == 0)
2442 {
2443 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2444 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2445 }
2446 if (r < 0)
2447 {
2448 al = SSL_AD_INTERNAL_ERROR;
2449 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2450 }
2451 }
2452
2453 switch (ret)
2454 {
2455 case SSL_TLSEXT_ERR_ALERT_FATAL:
2456 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2457 return -1;
2458
2459 case SSL_TLSEXT_ERR_ALERT_WARNING:
2460 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2461 return 1;
2462
2463 case SSL_TLSEXT_ERR_NOACK:
2464 s->servername_done=0;
2465 default:
2466 return 1;
2467 }
2468 }
2469
David Benjamin03973092014-06-24 23:27:17 -04002470int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002471 {
David Benjamin03973092014-06-24 23:27:17 -04002472 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002473 if (s->version < SSL3_VERSION)
2474 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002475
2476 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002477 {
David Benjamin03973092014-06-24 23:27:17 -04002478 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002479 return 0;
2480 }
2481
David Benjamin03973092014-06-24 23:27:17 -04002482 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002483 {
David Benjamin9d28c752014-07-05 00:43:48 -04002484 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002485 return 0;
2486 }
David Benjamin03973092014-06-24 23:27:17 -04002487
Adam Langley95c29f32014-06-20 12:00:00 -07002488 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002489 }
Adam Langley95c29f32014-06-20 12:00:00 -07002490
2491/* Since the server cache lookup is done early on in the processing of the
2492 * ClientHello, and other operations depend on the result, we need to handle
2493 * any TLS session ticket extension at the same time.
2494 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002495 * ctx: contains the early callback context, which is the result of a
2496 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002497 * ret: (output) on return, if a ticket was decrypted, then this is set to
2498 * point to the resulting session.
2499 *
2500 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2501 * ciphersuite, in which case we have no use for session tickets and one will
2502 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2503 *
2504 * Returns:
2505 * -1: fatal error, either from parsing or decrypting the ticket.
2506 * 0: no ticket was found (or was ignored, based on settings).
2507 * 1: a zero length extension was found, indicating that the client supports
2508 * session tickets but doesn't currently have one to offer.
2509 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2510 * couldn't be decrypted because of a non-fatal error.
2511 * 3: a ticket was successfully decrypted and *ret was set.
2512 *
2513 * Side effects:
2514 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2515 * a new session ticket to the client because the client indicated support
2516 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2517 * a session ticket or we couldn't use the one it gave us, or if
2518 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2519 * Otherwise, s->tlsext_ticket_expected is set to 0.
2520 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002521int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2522 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002523 {
Adam Langley95c29f32014-06-20 12:00:00 -07002524 *ret = NULL;
2525 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002526 const unsigned char *data;
2527 size_t len;
2528 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002529
2530 /* If tickets disabled behave as if no ticket present
2531 * to permit stateful resumption.
2532 */
2533 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2534 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002535 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002536 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002537 if (!SSL_early_callback_ctx_extension_get(
2538 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002539 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002540 return 0;
2541 }
2542 if (len == 0)
2543 {
2544 /* The client will accept a ticket but doesn't
2545 * currently have one. */
2546 s->tlsext_ticket_expected = 1;
2547 return 1;
2548 }
2549 if (s->tls_session_secret_cb)
2550 {
2551 /* Indicate that the ticket couldn't be
2552 * decrypted rather than generating the session
2553 * from ticket now, trigger abbreviated
2554 * handshake based on external mechanism to
2555 * calculate the master secret later. */
2556 return 2;
2557 }
2558 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2559 ctx->session_id_len, ret);
2560 switch (r)
2561 {
2562 case 2: /* ticket couldn't be decrypted */
2563 s->tlsext_ticket_expected = 1;
2564 return 2;
2565 case 3: /* ticket was decrypted */
2566 return r;
2567 case 4: /* ticket decrypted but need to renew */
2568 s->tlsext_ticket_expected = 1;
2569 return 3;
2570 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002571 return -1;
2572 }
Adam Langley95c29f32014-06-20 12:00:00 -07002573 }
2574
2575/* tls_decrypt_ticket attempts to decrypt a session ticket.
2576 *
2577 * etick: points to the body of the session ticket extension.
2578 * eticklen: the length of the session tickets extenion.
2579 * sess_id: points at the session ID.
2580 * sesslen: the length of the session ID.
2581 * psess: (output) on return, if a ticket was decrypted, then this is set to
2582 * point to the resulting session.
2583 *
2584 * Returns:
2585 * -1: fatal error, either from parsing or decrypting the ticket.
2586 * 2: the ticket couldn't be decrypted.
2587 * 3: a ticket was successfully decrypted and *psess was set.
2588 * 4: same as 3, but the ticket needs to be renewed.
2589 */
2590static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2591 const unsigned char *sess_id, int sesslen,
2592 SSL_SESSION **psess)
2593 {
2594 SSL_SESSION *sess;
2595 unsigned char *sdec;
2596 const unsigned char *p;
2597 int slen, mlen, renew_ticket = 0;
2598 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2599 HMAC_CTX hctx;
2600 EVP_CIPHER_CTX ctx;
2601 SSL_CTX *tctx = s->initial_ctx;
2602 /* Need at least keyname + iv + some encrypted data */
2603 if (eticklen < 48)
2604 return 2;
2605 /* Initialize session ticket encryption and HMAC contexts */
2606 HMAC_CTX_init(&hctx);
2607 EVP_CIPHER_CTX_init(&ctx);
2608 if (tctx->tlsext_ticket_key_cb)
2609 {
2610 unsigned char *nctick = (unsigned char *)etick;
2611 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2612 &ctx, &hctx, 0);
2613 if (rv < 0)
2614 return -1;
2615 if (rv == 0)
2616 return 2;
2617 if (rv == 2)
2618 renew_ticket = 1;
2619 }
2620 else
2621 {
2622 /* Check key name matches */
2623 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2624 return 2;
2625 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2626 tlsext_tick_md(), NULL);
2627 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2628 tctx->tlsext_tick_aes_key, etick + 16);
2629 }
2630 /* Attempt to process session ticket, first conduct sanity and
2631 * integrity checks on ticket.
2632 */
2633 mlen = HMAC_size(&hctx);
2634 if (mlen < 0)
2635 {
2636 EVP_CIPHER_CTX_cleanup(&ctx);
2637 return -1;
2638 }
2639 eticklen -= mlen;
2640 /* Check HMAC of encrypted ticket */
2641 HMAC_Update(&hctx, etick, eticklen);
2642 HMAC_Final(&hctx, tick_hmac, NULL);
2643 HMAC_CTX_cleanup(&hctx);
2644 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
2645 return 2;
2646 /* Attempt to decrypt session data */
2647 /* Move p after IV to start of encrypted ticket, update length */
2648 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2649 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2650 sdec = OPENSSL_malloc(eticklen);
2651 if (!sdec)
2652 {
2653 EVP_CIPHER_CTX_cleanup(&ctx);
2654 return -1;
2655 }
2656 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2657 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002658 {
2659 EVP_CIPHER_CTX_cleanup(&ctx);
2660 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002661 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002662 }
Adam Langley95c29f32014-06-20 12:00:00 -07002663 slen += mlen;
2664 EVP_CIPHER_CTX_cleanup(&ctx);
2665 p = sdec;
2666
2667 sess = d2i_SSL_SESSION(NULL, &p, slen);
2668 OPENSSL_free(sdec);
2669 if (sess)
2670 {
2671 /* The session ID, if non-empty, is used by some clients to
2672 * detect that the ticket has been accepted. So we copy it to
2673 * the session structure. If it is empty set length to zero
2674 * as required by standard.
2675 */
2676 if (sesslen)
2677 memcpy(sess->session_id, sess_id, sesslen);
2678 sess->session_id_length = sesslen;
2679 *psess = sess;
2680 if (renew_ticket)
2681 return 4;
2682 else
2683 return 3;
2684 }
2685 ERR_clear_error();
2686 /* For session parse failure, indicate that we need to send a new
2687 * ticket. */
2688 return 2;
2689 }
2690
2691/* Tables to translate from NIDs to TLS v1.2 ids */
2692
2693typedef struct
2694 {
2695 int nid;
2696 int id;
2697 } tls12_lookup;
2698
David Benjamincff64722014-08-19 19:54:46 -04002699static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002700 {NID_md5, TLSEXT_hash_md5},
2701 {NID_sha1, TLSEXT_hash_sha1},
2702 {NID_sha224, TLSEXT_hash_sha224},
2703 {NID_sha256, TLSEXT_hash_sha256},
2704 {NID_sha384, TLSEXT_hash_sha384},
2705 {NID_sha512, TLSEXT_hash_sha512}
2706};
2707
David Benjamincff64722014-08-19 19:54:46 -04002708static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002709 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002710 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2711};
2712
David Benjamincff64722014-08-19 19:54:46 -04002713static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002714 {
2715 size_t i;
2716 for (i = 0; i < tlen; i++)
2717 {
2718 if (table[i].nid == nid)
2719 return table[i].id;
2720 }
2721 return -1;
2722 }
2723
David Benjamincff64722014-08-19 19:54:46 -04002724static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002725 {
2726 size_t i;
2727 for (i = 0; i < tlen; i++)
2728 {
2729 if ((table[i].id) == id)
2730 return table[i].nid;
2731 }
2732 return NID_undef;
2733 }
2734
2735int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2736 {
2737 int sig_id, md_id;
2738 if (!md)
2739 return 0;
2740 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2741 sizeof(tls12_md)/sizeof(tls12_lookup));
2742 if (md_id == -1)
2743 return 0;
2744 sig_id = tls12_get_sigid(pk);
2745 if (sig_id == -1)
2746 return 0;
2747 p[0] = (unsigned char)md_id;
2748 p[1] = (unsigned char)sig_id;
2749 return 1;
2750 }
2751
2752int tls12_get_sigid(const EVP_PKEY *pk)
2753 {
2754 return tls12_find_id(pk->type, tls12_sig,
2755 sizeof(tls12_sig)/sizeof(tls12_lookup));
2756 }
2757
2758const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2759 {
2760 switch(hash_alg)
2761 {
Adam Langley95c29f32014-06-20 12:00:00 -07002762 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002763 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002764 case TLSEXT_hash_sha1:
2765 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002766 case TLSEXT_hash_sha224:
2767 return EVP_sha224();
2768
2769 case TLSEXT_hash_sha256:
2770 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002771 case TLSEXT_hash_sha384:
2772 return EVP_sha384();
2773
2774 case TLSEXT_hash_sha512:
2775 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002776 default:
2777 return NULL;
2778
2779 }
2780 }
2781
2782static int tls12_get_pkey_idx(unsigned char sig_alg)
2783 {
2784 switch(sig_alg)
2785 {
Adam Langley95c29f32014-06-20 12:00:00 -07002786 case TLSEXT_signature_rsa:
2787 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002788 case TLSEXT_signature_ecdsa:
2789 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002790 }
2791 return -1;
2792 }
2793
2794/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2795static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2796 int *psignhash_nid, const unsigned char *data)
2797 {
2798 int sign_nid = 0, hash_nid = 0;
2799 if (!phash_nid && !psign_nid && !psignhash_nid)
2800 return;
2801 if (phash_nid || psignhash_nid)
2802 {
2803 hash_nid = tls12_find_nid(data[0], tls12_md,
2804 sizeof(tls12_md)/sizeof(tls12_lookup));
2805 if (phash_nid)
2806 *phash_nid = hash_nid;
2807 }
2808 if (psign_nid || psignhash_nid)
2809 {
2810 sign_nid = tls12_find_nid(data[1], tls12_sig,
2811 sizeof(tls12_sig)/sizeof(tls12_lookup));
2812 if (psign_nid)
2813 *psign_nid = sign_nid;
2814 }
2815 if (psignhash_nid)
2816 {
2817 if (sign_nid && hash_nid)
2818 OBJ_find_sigid_by_algs(psignhash_nid,
2819 hash_nid, sign_nid);
2820 else
2821 *psignhash_nid = NID_undef;
2822 }
2823 }
2824/* Given preference and allowed sigalgs set shared sigalgs */
2825static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2826 const unsigned char *pref, size_t preflen,
2827 const unsigned char *allow, size_t allowlen)
2828 {
2829 const unsigned char *ptmp, *atmp;
2830 size_t i, j, nmatch = 0;
2831 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2832 {
2833 /* Skip disabled hashes or signature algorithms */
2834 if (tls12_get_hash(ptmp[0]) == NULL)
2835 continue;
2836 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2837 continue;
2838 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2839 {
2840 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2841 {
2842 nmatch++;
2843 if (shsig)
2844 {
2845 shsig->rhash = ptmp[0];
2846 shsig->rsign = ptmp[1];
2847 tls1_lookup_sigalg(&shsig->hash_nid,
2848 &shsig->sign_nid,
2849 &shsig->signandhash_nid,
2850 ptmp);
2851 shsig++;
2852 }
2853 break;
2854 }
2855 }
2856 }
2857 return nmatch;
2858 }
2859
2860/* Set shared signature algorithms for SSL structures */
2861static int tls1_set_shared_sigalgs(SSL *s)
2862 {
2863 const unsigned char *pref, *allow, *conf;
2864 size_t preflen, allowlen, conflen;
2865 size_t nmatch;
2866 TLS_SIGALGS *salgs = NULL;
2867 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002868 if (c->shared_sigalgs)
2869 {
2870 OPENSSL_free(c->shared_sigalgs);
2871 c->shared_sigalgs = NULL;
2872 }
Adam Langley95c29f32014-06-20 12:00:00 -07002873 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002874 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002875 {
2876 conf = c->client_sigalgs;
2877 conflen = c->client_sigalgslen;
2878 }
David Benjamin335d10d2014-08-06 19:56:33 -04002879 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002880 {
2881 conf = c->conf_sigalgs;
2882 conflen = c->conf_sigalgslen;
2883 }
2884 else
2885 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002886 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002887 {
2888 pref = conf;
2889 preflen = conflen;
2890 allow = c->peer_sigalgs;
2891 allowlen = c->peer_sigalgslen;
2892 }
2893 else
2894 {
2895 allow = conf;
2896 allowlen = conflen;
2897 pref = c->peer_sigalgs;
2898 preflen = c->peer_sigalgslen;
2899 }
2900 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2901 if (!nmatch)
2902 return 1;
2903 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2904 if (!salgs)
2905 return 0;
2906 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2907 c->shared_sigalgs = salgs;
2908 c->shared_sigalgslen = nmatch;
2909 return 1;
2910 }
2911
2912
2913/* Set preferred digest for each key type */
2914
David Benjamincd996942014-07-20 16:23:51 -04002915int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002916 {
2917 int idx;
2918 size_t i;
2919 const EVP_MD *md;
2920 CERT *c = s->cert;
2921 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002922
Adam Langley95c29f32014-06-20 12:00:00 -07002923 /* Extension ignored for inappropriate versions */
2924 if (!SSL_USE_SIGALGS(s))
2925 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002926 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002927 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002928 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002929 /* Should never happen */
2930 if (!c)
2931 return 0;
2932
David Benjamincd996942014-07-20 16:23:51 -04002933 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002934 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002935
2936 tls1_set_shared_sigalgs(s);
2937
2938#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2939 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2940 {
2941 /* Use first set signature preference to force message
2942 * digest, ignoring any peer preferences.
2943 */
2944 const unsigned char *sigs = NULL;
2945 if (s->server)
2946 sigs = c->conf_sigalgs;
2947 else
2948 sigs = c->client_sigalgs;
2949 if (sigs)
2950 {
2951 idx = tls12_get_pkey_idx(sigs[1]);
2952 md = tls12_get_hash(sigs[0]);
2953 c->pkeys[idx].digest = md;
2954 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2955 if (idx == SSL_PKEY_RSA_SIGN)
2956 {
2957 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2958 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2959 }
2960 }
2961 }
2962#endif
2963
2964 for (i = 0, sigptr = c->shared_sigalgs;
2965 i < c->shared_sigalgslen; i++, sigptr++)
2966 {
2967 idx = tls12_get_pkey_idx(sigptr->rsign);
2968 if (idx > 0 && c->pkeys[idx].digest == NULL)
2969 {
2970 md = tls12_get_hash(sigptr->rhash);
2971 c->pkeys[idx].digest = md;
2972 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2973 if (idx == SSL_PKEY_RSA_SIGN)
2974 {
2975 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2976 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2977 }
2978 }
2979
2980 }
2981 /* In strict mode leave unset digests as NULL to indicate we can't
2982 * use the certificate for signing.
2983 */
2984 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
2985 {
2986 /* Set any remaining keys to default values. NOTE: if alg is
2987 * not supported it stays as NULL.
2988 */
Adam Langley95c29f32014-06-20 12:00:00 -07002989 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2990 {
2991 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2992 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2993 }
Adam Langley95c29f32014-06-20 12:00:00 -07002994 if (!c->pkeys[SSL_PKEY_ECC].digest)
2995 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002996 }
2997 return 1;
2998 }
2999
3000
3001int SSL_get_sigalgs(SSL *s, int idx,
3002 int *psign, int *phash, int *psignhash,
3003 unsigned char *rsig, unsigned char *rhash)
3004 {
3005 const unsigned char *psig = s->cert->peer_sigalgs;
3006 if (psig == NULL)
3007 return 0;
3008 if (idx >= 0)
3009 {
3010 idx <<= 1;
3011 if (idx >= (int)s->cert->peer_sigalgslen)
3012 return 0;
3013 psig += idx;
3014 if (rhash)
3015 *rhash = psig[0];
3016 if (rsig)
3017 *rsig = psig[1];
3018 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3019 }
3020 return s->cert->peer_sigalgslen / 2;
3021 }
3022
3023int SSL_get_shared_sigalgs(SSL *s, int idx,
3024 int *psign, int *phash, int *psignhash,
3025 unsigned char *rsig, unsigned char *rhash)
3026 {
3027 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3028 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3029 return 0;
3030 shsigalgs += idx;
3031 if (phash)
3032 *phash = shsigalgs->hash_nid;
3033 if (psign)
3034 *psign = shsigalgs->sign_nid;
3035 if (psignhash)
3036 *psignhash = shsigalgs->signandhash_nid;
3037 if (rsig)
3038 *rsig = shsigalgs->rsign;
3039 if (rhash)
3040 *rhash = shsigalgs->rhash;
3041 return s->cert->shared_sigalgslen;
3042 }
3043
Adam Langley1258b6a2014-06-20 12:00:00 -07003044/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
3045 * SSL connection and writes it to |md|. */
3046int
3047tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
3048 {
3049 EVP_MD_CTX ctx;
3050 unsigned char temp_digest[EVP_MAX_MD_SIZE];
3051 unsigned temp_digest_len;
3052 int i;
3053 static const char kClientIDMagic[] = "TLS Channel ID signature";
3054
3055 if (s->s3->handshake_buffer)
3056 if (!ssl3_digest_cached_records(s))
3057 return 0;
3058
3059 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
3060
3061 if (s->hit && s->s3->tlsext_channel_id_new)
3062 {
3063 static const char kResumptionMagic[] = "Resumption";
3064 EVP_DigestUpdate(md, kResumptionMagic,
3065 sizeof(kResumptionMagic));
3066 if (s->session->original_handshake_hash_len == 0)
3067 return 0;
3068 EVP_DigestUpdate(md, s->session->original_handshake_hash,
3069 s->session->original_handshake_hash_len);
3070 }
3071
3072 EVP_MD_CTX_init(&ctx);
3073 for (i = 0; i < SSL_MAX_DIGEST; i++)
3074 {
3075 if (s->s3->handshake_dgst[i] == NULL)
3076 continue;
3077 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
3078 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
3079 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
3080 }
3081 EVP_MD_CTX_cleanup(&ctx);
3082
3083 return 1;
3084 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003085
3086/* tls1_record_handshake_hashes_for_channel_id records the current handshake
3087 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
3088int tls1_record_handshake_hashes_for_channel_id(SSL *s)
3089 {
3090 int digest_len;
3091 /* This function should never be called for a resumed session because
3092 * the handshake hashes that we wish to record are for the original,
3093 * full handshake. */
3094 if (s->hit)
3095 return -1;
3096 /* It only makes sense to call this function if Channel IDs have been
3097 * negotiated. */
3098 if (!s->s3->tlsext_channel_id_new)
3099 return -1;
3100
3101 digest_len = tls1_handshake_digest(
3102 s, s->session->original_handshake_hash,
3103 sizeof(s->session->original_handshake_hash));
3104 if (digest_len < 0)
3105 return -1;
3106
3107 s->session->original_handshake_hash_len = digest_len;
3108
3109 return 1;
3110 }
3111
Adam Langley95c29f32014-06-20 12:00:00 -07003112int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3113 {
3114 unsigned char *sigalgs, *sptr;
3115 int rhash, rsign;
3116 size_t i;
3117 if (salglen & 1)
3118 return 0;
3119 sigalgs = OPENSSL_malloc(salglen);
3120 if (sigalgs == NULL)
3121 return 0;
3122 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
3123 {
3124 rhash = tls12_find_id(*psig_nids++, tls12_md,
3125 sizeof(tls12_md)/sizeof(tls12_lookup));
3126 rsign = tls12_find_id(*psig_nids++, tls12_sig,
3127 sizeof(tls12_sig)/sizeof(tls12_lookup));
3128
3129 if (rhash == -1 || rsign == -1)
3130 goto err;
3131 *sptr++ = rhash;
3132 *sptr++ = rsign;
3133 }
3134
3135 if (client)
3136 {
3137 if (c->client_sigalgs)
3138 OPENSSL_free(c->client_sigalgs);
3139 c->client_sigalgs = sigalgs;
3140 c->client_sigalgslen = salglen;
3141 }
3142 else
3143 {
3144 if (c->conf_sigalgs)
3145 OPENSSL_free(c->conf_sigalgs);
3146 c->conf_sigalgs = sigalgs;
3147 c->conf_sigalgslen = salglen;
3148 }
3149
3150 return 1;
3151
3152 err:
3153 OPENSSL_free(sigalgs);
3154 return 0;
3155 }
3156
3157static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3158 {
3159 int sig_nid;
3160 size_t i;
3161 if (default_nid == -1)
3162 return 1;
3163 sig_nid = X509_get_signature_nid(x);
3164 if (default_nid)
3165 return sig_nid == default_nid ? 1 : 0;
3166 for (i = 0; i < c->shared_sigalgslen; i++)
3167 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3168 return 1;
3169 return 0;
3170 }
3171/* Check to see if a certificate issuer name matches list of CA names */
3172static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3173 {
3174 X509_NAME *nm;
3175 int i;
3176 nm = X509_get_issuer_name(x);
3177 for (i = 0; i < sk_X509_NAME_num(names); i++)
3178 {
3179 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3180 return 1;
3181 }
3182 return 0;
3183 }
3184
3185/* Check certificate chain is consistent with TLS extensions and is
3186 * usable by server. This servers two purposes: it allows users to
3187 * check chains before passing them to the server and it allows the
3188 * server to check chains before attempting to use them.
3189 */
3190
3191/* Flags which need to be set for a certificate when stict mode not set */
3192
3193#define CERT_PKEY_VALID_FLAGS \
3194 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3195/* Strict mode flags */
3196#define CERT_PKEY_STRICT_FLAGS \
3197 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3198 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3199
3200int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3201 int idx)
3202 {
3203 int i;
3204 int rv = 0;
3205 int check_flags = 0, strict_mode;
3206 CERT_PKEY *cpk = NULL;
3207 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07003208 /* idx == -1 means checking server chains */
3209 if (idx != -1)
3210 {
3211 /* idx == -2 means checking client certificate chains */
3212 if (idx == -2)
3213 {
3214 cpk = c->key;
3215 idx = cpk - c->pkeys;
3216 }
3217 else
3218 cpk = c->pkeys + idx;
3219 x = cpk->x509;
3220 pk = cpk->privatekey;
3221 chain = cpk->chain;
3222 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3223 /* If no cert or key, forget it */
3224 if (!x || !pk)
3225 goto end;
3226#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3227 /* Allow any certificate to pass test */
3228 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
3229 {
3230 rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
3231 cpk->valid_flags = rv;
3232 return rv;
3233 }
3234#endif
3235 }
3236 else
3237 {
3238 if (!x || !pk)
3239 goto end;
3240 idx = ssl_cert_type(x, pk);
3241 if (idx == -1)
3242 goto end;
3243 cpk = c->pkeys + idx;
3244 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3245 check_flags = CERT_PKEY_STRICT_FLAGS;
3246 else
3247 check_flags = CERT_PKEY_VALID_FLAGS;
3248 strict_mode = 1;
3249 }
3250
Adam Langley95c29f32014-06-20 12:00:00 -07003251 /* Check all signature algorithms are consistent with
3252 * signature algorithms extension if TLS 1.2 or later
3253 * and strict mode.
3254 */
3255 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
3256 {
3257 int default_nid;
3258 unsigned char rsign = 0;
3259 if (c->peer_sigalgs)
3260 default_nid = 0;
3261 /* If no sigalgs extension use defaults from RFC5246 */
3262 else
3263 {
3264 switch(idx)
3265 {
3266 case SSL_PKEY_RSA_ENC:
3267 case SSL_PKEY_RSA_SIGN:
Adam Langley95c29f32014-06-20 12:00:00 -07003268 rsign = TLSEXT_signature_rsa;
3269 default_nid = NID_sha1WithRSAEncryption;
3270 break;
3271
Adam Langley95c29f32014-06-20 12:00:00 -07003272 case SSL_PKEY_ECC:
3273 rsign = TLSEXT_signature_ecdsa;
3274 default_nid = NID_ecdsa_with_SHA1;
3275 break;
3276
3277 default:
3278 default_nid = -1;
3279 break;
3280 }
3281 }
3282 /* If peer sent no signature algorithms extension and we
3283 * have set preferred signature algorithms check we support
3284 * sha1.
3285 */
3286 if (default_nid > 0 && c->conf_sigalgs)
3287 {
3288 size_t j;
3289 const unsigned char *p = c->conf_sigalgs;
3290 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
3291 {
3292 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3293 break;
3294 }
3295 if (j == c->conf_sigalgslen)
3296 {
3297 if (check_flags)
3298 goto skip_sigs;
3299 else
3300 goto end;
3301 }
3302 }
3303 /* Check signature algorithm of each cert in chain */
3304 if (!tls1_check_sig_alg(c, x, default_nid))
3305 {
3306 if (!check_flags) goto end;
3307 }
3308 else
3309 rv |= CERT_PKEY_EE_SIGNATURE;
3310 rv |= CERT_PKEY_CA_SIGNATURE;
3311 for (i = 0; i < sk_X509_num(chain); i++)
3312 {
3313 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3314 default_nid))
3315 {
3316 if (check_flags)
3317 {
3318 rv &= ~CERT_PKEY_CA_SIGNATURE;
3319 break;
3320 }
3321 else
3322 goto end;
3323 }
3324 }
3325 }
3326 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3327 else if(check_flags)
3328 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3329 skip_sigs:
3330 /* Check cert parameters are consistent */
3331 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3332 rv |= CERT_PKEY_EE_PARAM;
3333 else if (!check_flags)
3334 goto end;
3335 if (!s->server)
3336 rv |= CERT_PKEY_CA_PARAM;
3337 /* In strict mode check rest of chain too */
3338 else if (strict_mode)
3339 {
3340 rv |= CERT_PKEY_CA_PARAM;
3341 for (i = 0; i < sk_X509_num(chain); i++)
3342 {
3343 X509 *ca = sk_X509_value(chain, i);
3344 if (!tls1_check_cert_param(s, ca, 0))
3345 {
3346 if (check_flags)
3347 {
3348 rv &= ~CERT_PKEY_CA_PARAM;
3349 break;
3350 }
3351 else
3352 goto end;
3353 }
3354 }
3355 }
3356 if (!s->server && strict_mode)
3357 {
3358 STACK_OF(X509_NAME) *ca_dn;
David Benjamin676d1e72014-07-08 14:34:10 -04003359 uint8_t check_type = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003360 switch (pk->type)
3361 {
3362 case EVP_PKEY_RSA:
3363 check_type = TLS_CT_RSA_SIGN;
3364 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003365 case EVP_PKEY_EC:
3366 check_type = TLS_CT_ECDSA_SIGN;
3367 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003368 }
3369 if (check_type)
3370 {
David Benjamin676d1e72014-07-08 14:34:10 -04003371 if (s->s3->tmp.certificate_types &&
3372 memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07003373 {
Adam Langley95c29f32014-06-20 12:00:00 -07003374 rv |= CERT_PKEY_CERT_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -07003375 }
3376 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3377 goto end;
3378 }
3379 else
3380 rv |= CERT_PKEY_CERT_TYPE;
3381
3382
3383 ca_dn = s->s3->tmp.ca_names;
3384
3385 if (!sk_X509_NAME_num(ca_dn))
3386 rv |= CERT_PKEY_ISSUER_NAME;
3387
3388 if (!(rv & CERT_PKEY_ISSUER_NAME))
3389 {
3390 if (ssl_check_ca_name(ca_dn, x))
3391 rv |= CERT_PKEY_ISSUER_NAME;
3392 }
3393 if (!(rv & CERT_PKEY_ISSUER_NAME))
3394 {
3395 for (i = 0; i < sk_X509_num(chain); i++)
3396 {
3397 X509 *xtmp = sk_X509_value(chain, i);
3398 if (ssl_check_ca_name(ca_dn, xtmp))
3399 {
3400 rv |= CERT_PKEY_ISSUER_NAME;
3401 break;
3402 }
3403 }
3404 }
3405 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3406 goto end;
3407 }
3408 else
3409 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3410
3411 if (!check_flags || (rv & check_flags) == check_flags)
3412 rv |= CERT_PKEY_VALID;
3413
3414 end:
3415
3416 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3417 {
3418 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3419 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3420 else if (cpk->digest)
3421 rv |= CERT_PKEY_SIGN;
3422 }
3423 else
3424 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3425
3426 /* When checking a CERT_PKEY structure all flags are irrelevant
3427 * if the chain is invalid.
3428 */
3429 if (!check_flags)
3430 {
3431 if (rv & CERT_PKEY_VALID)
3432 cpk->valid_flags = rv;
3433 else
3434 {
3435 /* Preserve explicit sign flag, clear rest */
3436 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
3437 return 0;
3438 }
3439 }
3440 return rv;
3441 }
3442
3443/* Set validity of certificates in an SSL structure */
3444void tls1_set_cert_validity(SSL *s)
3445 {
3446 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3447 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
Adam Langley95c29f32014-06-20 12:00:00 -07003448 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3449 }
3450/* User level utiity function to check a chain is suitable */
3451int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3452 {
3453 return tls1_check_chain(s, x, pk, chain, -1);
3454 }
3455