blob: 8fdd8137b5b3a082c74cfba3372357fc66918638 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400110#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
112
David Benjamin03973092014-06-24 23:27:17 -0400113#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/mem.h>
117#include <openssl/obj.h>
118#include <openssl/rand.h>
119
120#include "ssl_locl.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400124static int ssl_check_clienthello_tlsext(SSL *s);
125static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
127SSL3_ENC_METHOD TLSv1_enc_data={
128 tls1_enc,
129 tls1_mac,
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
141 SSL3_HM_HEADER_LENGTH,
142 ssl3_set_handshake_header,
143 ssl3_handshake_write
144 };
145
146SSL3_ENC_METHOD TLSv1_1_enc_data={
147 tls1_enc,
148 tls1_mac,
149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
153 TLS1_FINISH_MAC_LENGTH,
154 tls1_cert_verify_mac,
155 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
156 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
157 tls1_alert_code,
158 tls1_export_keying_material,
159 SSL_ENC_FLAG_EXPLICIT_IV,
160 SSL3_HM_HEADER_LENGTH,
161 ssl3_set_handshake_header,
162 ssl3_handshake_write
163 };
164
165SSL3_ENC_METHOD TLSv1_2_enc_data={
166 tls1_enc,
167 tls1_mac,
168 tls1_setup_key_block,
169 tls1_generate_master_secret,
170 tls1_change_cipher_state,
171 tls1_final_finish_mac,
172 TLS1_FINISH_MAC_LENGTH,
173 tls1_cert_verify_mac,
174 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
175 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
176 tls1_alert_code,
177 tls1_export_keying_material,
178 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
179 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
180 SSL3_HM_HEADER_LENGTH,
181 ssl3_set_handshake_header,
182 ssl3_handshake_write
183 };
184
David Benjamin35a7a442014-07-05 00:23:20 -0400185static int compare_uint16_t(const void *p1, const void *p2)
186 {
187 uint16_t u1 = *((const uint16_t*)p1);
188 uint16_t u2 = *((const uint16_t*)p2);
189 if (u1 < u2)
190 {
191 return -1;
192 }
193 else if (u1 > u2)
194 {
195 return 1;
196 }
197 else
198 {
199 return 0;
200 }
201 }
202
203/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
204 * than one extension of the same type in a ClientHello or ServerHello. This
205 * function does an initial scan over the extensions block to filter those
206 * out. */
207static int tls1_check_duplicate_extensions(const CBS *cbs)
208 {
209 CBS extensions = *cbs;
210 size_t num_extensions = 0, i = 0;
211 uint16_t *extension_types = NULL;
212 int ret = 0;
213
214 /* First pass: count the extensions. */
215 while (CBS_len(&extensions) > 0)
216 {
217 uint16_t type;
218 CBS extension;
219
220 if (!CBS_get_u16(&extensions, &type) ||
221 !CBS_get_u16_length_prefixed(&extensions, &extension))
222 {
223 goto done;
224 }
225
226 num_extensions++;
227 }
228
David Benjamin9a373592014-07-25 04:27:53 -0400229 if (num_extensions == 0)
230 {
231 return 1;
232 }
233
David Benjamin35a7a442014-07-05 00:23:20 -0400234 extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
235 if (extension_types == NULL)
236 {
237 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
238 goto done;
239 }
240
241 /* Second pass: gather the extension types. */
242 extensions = *cbs;
243 for (i = 0; i < num_extensions; i++)
244 {
245 CBS extension;
246
247 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
248 !CBS_get_u16_length_prefixed(&extensions, &extension))
249 {
250 /* This should not happen. */
251 goto done;
252 }
253 }
254 assert(CBS_len(&extensions) == 0);
255
256 /* Sort the extensions and make sure there are no duplicates. */
257 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
258 for (i = 1; i < num_extensions; i++)
259 {
260 if (extension_types[i-1] == extension_types[i])
261 {
262 goto done;
263 }
264 }
265
266 ret = 1;
267done:
268 if (extension_types)
269 OPENSSL_free(extension_types);
270 return ret;
271 }
272
Adam Langleydc9b1412014-06-20 12:00:00 -0700273char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
274 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400275 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
276
277 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700278
279 /* Skip client version. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400280 if (!CBS_skip(&client_hello, 2))
Adam Langleydc9b1412014-06-20 12:00:00 -0700281 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
283 /* Skip client nonce. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400284 if (!CBS_skip(&client_hello, 32))
Adam Langleydc9b1412014-06-20 12:00:00 -0700285 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700286
David Benjamin8f2c20e2014-07-09 09:30:38 -0400287 /* Extract session_id. */
288 if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
Adam Langleydc9b1412014-06-20 12:00:00 -0700289 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 ctx->session_id = CBS_data(&session_id);
291 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
293 /* Skip past DTLS cookie */
David Benjamin09bd58d2014-08-12 21:22:28 -0400294 if (SSL_IS_DTLS(ctx->ssl))
Adam Langleydc9b1412014-06-20 12:00:00 -0700295 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400296 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700297
David Benjamin8f2c20e2014-07-09 09:30:38 -0400298 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
Adam Langleydc9b1412014-06-20 12:00:00 -0700299 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700300 }
301
David Benjamin8f2c20e2014-07-09 09:30:38 -0400302 /* Extract cipher_suites. */
303 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
304 CBS_len(&cipher_suites) < 2 ||
305 (CBS_len(&cipher_suites) & 1) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700306 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400307 ctx->cipher_suites = CBS_data(&cipher_suites);
308 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700309
David Benjamin8f2c20e2014-07-09 09:30:38 -0400310 /* Extract compression_methods. */
311 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
312 CBS_len(&compression_methods) < 1)
Adam Langleydc9b1412014-06-20 12:00:00 -0700313 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400314 ctx->compression_methods = CBS_data(&compression_methods);
315 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700316
317 /* If the ClientHello ends here then it's valid, but doesn't have any
318 * extensions. (E.g. SSLv3.) */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400319 if (CBS_len(&client_hello) == 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700320 {
321 ctx->extensions = NULL;
322 ctx->extensions_len = 0;
323 return 1;
324 }
325
David Benjamin8f2c20e2014-07-09 09:30:38 -0400326 /* Extract extensions and check it is valid. */
327 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
328 !tls1_check_duplicate_extensions(&extensions) ||
329 CBS_len(&client_hello) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700330 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400331 ctx->extensions = CBS_data(&extensions);
332 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700333
Adam Langleydc9b1412014-06-20 12:00:00 -0700334 return 1;
Adam Langleydc9b1412014-06-20 12:00:00 -0700335 }
336
337char
338SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
339 uint16_t extension_type,
340 const unsigned char **out_data,
341 size_t *out_len)
342 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400343 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700344
David Benjamin8f2c20e2014-07-09 09:30:38 -0400345 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
346
347 while (CBS_len(&extensions) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700348 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400349 uint16_t type;
350 CBS extension;
Adam Langleydc9b1412014-06-20 12:00:00 -0700351
David Benjamin8f2c20e2014-07-09 09:30:38 -0400352 /* Decode the next extension. */
353 if (!CBS_get_u16(&extensions, &type) ||
354 !CBS_get_u16_length_prefixed(&extensions, &extension))
Adam Langleydc9b1412014-06-20 12:00:00 -0700355 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700356
David Benjamin8f2c20e2014-07-09 09:30:38 -0400357 if (type == extension_type)
Adam Langleydc9b1412014-06-20 12:00:00 -0700358 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400359 *out_data = CBS_data(&extension);
360 *out_len = CBS_len(&extension);
Adam Langleydc9b1412014-06-20 12:00:00 -0700361 return 1;
362 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700363 }
364
365 return 0;
366 }
367
Adam Langley95c29f32014-06-20 12:00:00 -0700368
David Benjamincff64722014-08-19 19:54:46 -0400369static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700370 {
371 NID_sect163k1, /* sect163k1 (1) */
372 NID_sect163r1, /* sect163r1 (2) */
373 NID_sect163r2, /* sect163r2 (3) */
374 NID_sect193r1, /* sect193r1 (4) */
375 NID_sect193r2, /* sect193r2 (5) */
376 NID_sect233k1, /* sect233k1 (6) */
377 NID_sect233r1, /* sect233r1 (7) */
378 NID_sect239k1, /* sect239k1 (8) */
379 NID_sect283k1, /* sect283k1 (9) */
380 NID_sect283r1, /* sect283r1 (10) */
381 NID_sect409k1, /* sect409k1 (11) */
382 NID_sect409r1, /* sect409r1 (12) */
383 NID_sect571k1, /* sect571k1 (13) */
384 NID_sect571r1, /* sect571r1 (14) */
385 NID_secp160k1, /* secp160k1 (15) */
386 NID_secp160r1, /* secp160r1 (16) */
387 NID_secp160r2, /* secp160r2 (17) */
388 NID_secp192k1, /* secp192k1 (18) */
389 NID_X9_62_prime192v1, /* secp192r1 (19) */
390 NID_secp224k1, /* secp224k1 (20) */
391 NID_secp224r1, /* secp224r1 (21) */
392 NID_secp256k1, /* secp256k1 (22) */
393 NID_X9_62_prime256v1, /* secp256r1 (23) */
394 NID_secp384r1, /* secp384r1 (24) */
395 NID_secp521r1, /* secp521r1 (25) */
396 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
397 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
398 NID_brainpoolP512r1 /* brainpool512r1 (28) */
399 };
400
David Benjamin072334d2014-07-13 16:24:27 -0400401static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700402 {
403 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700404 };
405
David Benjamin072334d2014-07-13 16:24:27 -0400406static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700407 {
David Benjamin072334d2014-07-13 16:24:27 -0400408 23, /* secp256r1 (23) */
409 24, /* secp384r1 (24) */
410 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700411 };
412
David Benjamin072334d2014-07-13 16:24:27 -0400413int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700414 {
415 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
David Benjamin072334d2014-07-13 16:24:27 -0400416 if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
417 return OBJ_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700418 return nid_list[curve_id-1];
419 }
420
David Benjamin072334d2014-07-13 16:24:27 -0400421uint16_t tls1_ec_nid2curve_id(int nid)
Adam Langley95c29f32014-06-20 12:00:00 -0700422 {
David Benjamin072334d2014-07-13 16:24:27 -0400423 size_t i;
424 for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700425 {
David Benjamin072334d2014-07-13 16:24:27 -0400426 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
427 if (nid == nid_list[i])
428 return i + 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700429 }
David Benjamin072334d2014-07-13 16:24:27 -0400430 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
431 * will never be allocated. */
432 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700433 }
David Benjamin072334d2014-07-13 16:24:27 -0400434
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
David Benjamin6c7aed02014-08-27 16:42:38 -04001028 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001029 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001030 /* The status_request extension is excessively extensible at
1031 * every layer. On the client, only support requesting OCSP
1032 * responses with an empty responder_id_list and no
1033 * extensions. */
1034 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
Adam Langley95c29f32014-06-20 12:00:00 -07001036 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001037 s2n(1 + 2 + 2, ret);
1038 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001039 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001040 /* responder_id_list - empty */
1041 s2n(0, ret);
1042 /* request_extensions - empty */
1043 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langley95c29f32014-06-20 12:00:00 -07001046 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1047 {
1048 /* The client advertises an emtpy extension to indicate its
1049 * support for Next Protocol Negotiation */
1050 if (limit - ret - 4 < 0)
1051 return NULL;
1052 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1053 s2n(0,ret);
1054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001056 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1057 {
1058 /* The client advertises an empty extension to indicate its support for
1059 * certificate timestamps. */
1060 if (limit - ret - 4 < 0)
1061 return NULL;
1062 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1063 s2n(0,ret);
1064 }
1065
Adam Langley95c29f32014-06-20 12:00:00 -07001066 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1067 {
1068 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1069 return NULL;
1070 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1071 s2n(2 + s->alpn_client_proto_list_len,ret);
1072 s2n(s->alpn_client_proto_list_len,ret);
1073 memcpy(ret, s->alpn_client_proto_list,
1074 s->alpn_client_proto_list_len);
1075 ret += s->alpn_client_proto_list_len;
1076 }
1077
Adam Langley1258b6a2014-06-20 12:00:00 -07001078 if (s->tlsext_channel_id_enabled)
1079 {
1080 /* The client advertises an emtpy extension to indicate its
1081 * support for Channel ID. */
1082 if (limit - ret - 4 < 0)
1083 return NULL;
1084 if (s->ctx->tlsext_channel_id_enabled_new)
1085 s2n(TLSEXT_TYPE_channel_id_new,ret);
1086 else
1087 s2n(TLSEXT_TYPE_channel_id,ret);
1088 s2n(0,ret);
1089 }
1090
Adam Langley95c29f32014-06-20 12:00:00 -07001091 if(SSL_get_srtp_profiles(s))
1092 {
1093 int el;
1094
1095 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1096
Adam Langleyb0c235e2014-06-20 12:00:00 -07001097 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001098
1099 s2n(TLSEXT_TYPE_use_srtp,ret);
1100 s2n(el,ret);
1101
1102 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1103 {
1104 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1105 return NULL;
1106 }
1107 ret += el;
1108 }
1109
Adam Langleyc3174b72014-06-20 12:00:00 -07001110 if (using_ecc)
1111 {
1112 /* Add TLS extension ECPointFormats to the ClientHello message */
1113 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001114 const uint8_t *formats;
1115 const uint16_t *curves;
1116 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001117
David Benjamin072334d2014-07-13 16:24:27 -04001118 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001119
1120 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001121 if (formats_len > (size_t)lenmax) return NULL;
1122 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001123 {
1124 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1125 return NULL;
1126 }
1127
1128 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001129 s2n(formats_len + 1,ret);
1130 *(ret++) = (unsigned char)formats_len;
1131 memcpy(ret, formats, formats_len);
1132 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001133
1134 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001135 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001136
1137 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001138 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1139 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001140 {
1141 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1142 return NULL;
1143 }
1144
1145 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001146 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001147
1148 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1149 * elliptic_curve_list, but the examples use two bytes.
1150 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1151 * resolves this to two bytes.
1152 */
David Benjamin072334d2014-07-13 16:24:27 -04001153 s2n(curves_len * 2, ret);
1154 for (i = 0; i < curves_len; i++)
1155 {
1156 s2n(curves[i], ret);
1157 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001158 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001159
Adam Langley95c29f32014-06-20 12:00:00 -07001160#ifdef TLSEXT_TYPE_padding
1161 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001162 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001163 *
1164 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001165 * extensions it MUST always appear last. */
1166 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001167 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001168 header_len += ret - orig;
1169 if (header_len > 0xff && header_len < 0x200)
1170 {
1171 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001172 /* Extensions take at least four bytes to encode. Always
1173 * include least one byte of data if including the
1174 * extension. WebSphere Application Server 7.0 is
1175 * intolerant to the last extension being zero-length. */
1176 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001177 padding_len -= 4;
1178 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001179 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001180 if (limit - ret - 4 - (long)padding_len < 0)
1181 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyb0c235e2014-06-20 12:00:00 -07001183 s2n(TLSEXT_TYPE_padding, ret);
1184 s2n(padding_len, ret);
1185 memset(ret, 0, padding_len);
1186 ret += padding_len;
1187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188 }
1189#endif
1190
Adam Langleyb0c235e2014-06-20 12:00:00 -07001191 if ((extdatalen = ret-orig-2)== 0)
1192 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyb0c235e2014-06-20 12:00:00 -07001194 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001195 return ret;
1196 }
1197
Adam Langleyb0c235e2014-06-20 12:00:00 -07001198unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001199 {
1200 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001201 unsigned char *orig = buf;
1202 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001203 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001204 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1205 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001206 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
Adam Langley95c29f32014-06-20 12:00:00 -07001207 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001208 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1209 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001210 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001211
1212 ret+=2;
1213 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1214
1215 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1216 {
1217 if ((long)(limit - ret - 4) < 0) return NULL;
1218
1219 s2n(TLSEXT_TYPE_server_name,ret);
1220 s2n(0,ret);
1221 }
1222
1223 if(s->s3->send_connection_binding)
1224 {
1225 int el;
1226
1227 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1228 {
1229 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1230 return NULL;
1231 }
1232
Adam Langleyb0c235e2014-06-20 12:00:00 -07001233 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001234
1235 s2n(TLSEXT_TYPE_renegotiate,ret);
1236 s2n(el,ret);
1237
1238 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1239 {
1240 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1241 return NULL;
1242 }
1243
1244 ret += el;
1245 }
1246
Adam Langley95c29f32014-06-20 12:00:00 -07001247 if (using_ecc)
1248 {
1249 const unsigned char *plist;
1250 size_t plistlen;
1251 /* Add TLS extension ECPointFormats to the ServerHello message */
1252 long lenmax;
1253
1254 tls1_get_formatlist(s, &plist, &plistlen);
1255
1256 if ((lenmax = limit - ret - 5) < 0) return NULL;
1257 if (plistlen > (size_t)lenmax) return NULL;
1258 if (plistlen > 255)
1259 {
1260 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1261 return NULL;
1262 }
1263
1264 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1265 s2n(plistlen + 1,ret);
1266 *(ret++) = (unsigned char) plistlen;
1267 memcpy(ret, plist, plistlen);
1268 ret+=plistlen;
1269
1270 }
1271 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001272
1273 if (s->tlsext_ticket_expected
1274 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1275 {
1276 if ((long)(limit - ret - 4) < 0) return NULL;
1277 s2n(TLSEXT_TYPE_session_ticket,ret);
1278 s2n(0,ret);
1279 }
1280
David Benjamin6c7aed02014-08-27 16:42:38 -04001281 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001282 {
1283 if ((long)(limit - ret - 4) < 0) return NULL;
1284 s2n(TLSEXT_TYPE_status_request,ret);
1285 s2n(0,ret);
1286 }
1287
Adam Langley95c29f32014-06-20 12:00:00 -07001288 if(s->srtp_profile)
1289 {
1290 int el;
1291
1292 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1293
Adam Langleyb0c235e2014-06-20 12:00:00 -07001294 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001295
1296 s2n(TLSEXT_TYPE_use_srtp,ret);
1297 s2n(el,ret);
1298
1299 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1300 {
1301 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1302 return NULL;
1303 }
1304 ret+=el;
1305 }
1306
Adam Langley95c29f32014-06-20 12:00:00 -07001307 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1308 s->s3->next_proto_neg_seen = 0;
1309 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1310 {
1311 const unsigned char *npa;
1312 unsigned int npalen;
1313 int r;
1314
1315 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1316 if (r == SSL_TLSEXT_ERR_OK)
1317 {
1318 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1319 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1320 s2n(npalen,ret);
1321 memcpy(ret, npa, npalen);
1322 ret += npalen;
1323 s->s3->next_proto_neg_seen = 1;
1324 }
1325 }
Adam Langley95c29f32014-06-20 12:00:00 -07001326
Adam Langley95c29f32014-06-20 12:00:00 -07001327 if (s->s3->alpn_selected)
1328 {
David Benjamin03973092014-06-24 23:27:17 -04001329 const uint8_t *selected = s->s3->alpn_selected;
1330 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001331
1332 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1333 return NULL;
1334 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1335 s2n(3 + len,ret);
1336 s2n(1 + len,ret);
1337 *ret++ = len;
1338 memcpy(ret, selected, len);
1339 ret += len;
1340 }
1341
Adam Langley1258b6a2014-06-20 12:00:00 -07001342 /* If the client advertised support for Channel ID, and we have it
1343 * enabled, then we want to echo it back. */
1344 if (s->s3->tlsext_channel_id_valid)
1345 {
1346 if (limit - ret - 4 < 0)
1347 return NULL;
1348 if (s->s3->tlsext_channel_id_new)
1349 s2n(TLSEXT_TYPE_channel_id_new,ret);
1350 else
1351 s2n(TLSEXT_TYPE_channel_id,ret);
1352 s2n(0,ret);
1353 }
1354
Adam Langleyb0c235e2014-06-20 12:00:00 -07001355 if ((extdatalen = ret-orig-2) == 0)
1356 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langleyb0c235e2014-06-20 12:00:00 -07001358 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001359 return ret;
1360 }
1361
Adam Langley95c29f32014-06-20 12:00:00 -07001362/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1363 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001364 * cbs: the contents of the extension, not including the type and length.
1365 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001366 * return.
1367 *
David Benjamindc72ff72014-06-25 12:36:10 -04001368 * returns: 1 on success. */
1369static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001370 {
Adam Langleyded93582014-07-31 15:23:51 -07001371 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001372 const unsigned char *selected;
1373 unsigned char selected_len;
1374 int r;
1375
1376 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001377 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001378
David Benjamindc72ff72014-06-25 12:36:10 -04001379 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1380 CBS_len(cbs) != 0 ||
1381 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001382 goto parse_error;
1383
David Benjamindc72ff72014-06-25 12:36:10 -04001384 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001385 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001386 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001387 {
David Benjamindc72ff72014-06-25 12:36:10 -04001388 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001389
David Benjamindc72ff72014-06-25 12:36:10 -04001390 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001391 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001392 }
1393
David Benjamindc72ff72014-06-25 12:36:10 -04001394 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1395 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1396 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001397 if (r == SSL_TLSEXT_ERR_OK) {
1398 if (s->s3->alpn_selected)
1399 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001400 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001401 if (!s->s3->alpn_selected)
1402 {
David Benjamindc72ff72014-06-25 12:36:10 -04001403 *out_alert = SSL_AD_INTERNAL_ERROR;
1404 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001405 }
Adam Langley95c29f32014-06-20 12:00:00 -07001406 s->s3->alpn_selected_len = selected_len;
1407 }
David Benjamindc72ff72014-06-25 12:36:10 -04001408 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001409
1410parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001411 *out_alert = SSL_AD_DECODE_ERROR;
1412 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001413 }
1414
David Benjamindc72ff72014-06-25 12:36:10 -04001415static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001416 {
Adam Langley95c29f32014-06-20 12:00:00 -07001417 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001418 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001419 size_t i;
1420
1421 s->servername_done = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001422 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001423 s->s3->tmp.certificate_status_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langley95c29f32014-06-20 12:00:00 -07001425 if (s->s3->alpn_selected)
1426 {
1427 OPENSSL_free(s->s3->alpn_selected);
1428 s->s3->alpn_selected = NULL;
1429 }
1430
Adam Langley95c29f32014-06-20 12:00:00 -07001431 /* Clear any signature algorithms extension received */
1432 if (s->cert->peer_sigalgs)
1433 {
1434 OPENSSL_free(s->cert->peer_sigalgs);
1435 s->cert->peer_sigalgs = NULL;
1436 }
1437 /* Clear any shared sigtnature algorithms */
1438 if (s->cert->shared_sigalgs)
1439 {
1440 OPENSSL_free(s->cert->shared_sigalgs);
1441 s->cert->shared_sigalgs = NULL;
1442 }
1443 /* Clear certificate digests and validity flags */
1444 for (i = 0; i < SSL_PKEY_NUM; i++)
1445 {
1446 s->cert->pkeys[i].digest = NULL;
1447 s->cert->pkeys[i].valid_flags = 0;
1448 }
1449
David Benjamindc72ff72014-06-25 12:36:10 -04001450 /* There may be no extensions. */
1451 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001452 {
David Benjamindc72ff72014-06-25 12:36:10 -04001453 goto ri_check;
1454 }
Adam Langley95c29f32014-06-20 12:00:00 -07001455
David Benjamin35a7a442014-07-05 00:23:20 -04001456 /* Decode the extensions block and check it is valid. */
1457 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1458 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001459 {
1460 *out_alert = SSL_AD_DECODE_ERROR;
1461 return 0;
1462 }
1463
David Benjamindc72ff72014-06-25 12:36:10 -04001464 while (CBS_len(&extensions) != 0)
1465 {
1466 uint16_t type;
1467 CBS extension;
1468
1469 /* Decode the next extension. */
1470 if (!CBS_get_u16(&extensions, &type) ||
1471 !CBS_get_u16_length_prefixed(&extensions, &extension))
1472 {
1473 *out_alert = SSL_AD_DECODE_ERROR;
1474 return 0;
1475 }
1476
Adam Langley95c29f32014-06-20 12:00:00 -07001477 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001478 {
1479 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1480 CBS_len(&extension), s->tlsext_debug_arg);
1481 }
1482
Adam Langley95c29f32014-06-20 12:00:00 -07001483/* The servername extension is treated as follows:
1484
1485 - Only the hostname type is supported with a maximum length of 255.
1486 - The servername is rejected if too long or if it contains zeros,
1487 in which case an fatal alert is generated.
1488 - The servername field is maintained together with the session cache.
1489 - When a session is resumed, the servername call back invoked in order
1490 to allow the application to position itself to the right context.
1491 - The servername is acknowledged if it is new for a session or when
1492 it is identical to a previously used for the same session.
1493 Applications can control the behaviour. They can at any time
1494 set a 'desirable' servername for a new SSL object. This can be the
1495 case for example with HTTPS when a Host: header field is received and
1496 a renegotiation is requested. In this case, a possible servername
1497 presented in the new client hello is only acknowledged if it matches
1498 the value of the Host: field.
1499 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1500 if they provide for changing an explicit servername context for the session,
1501 i.e. when the session has been established with a servername extension.
1502 - On session reconnect, the servername extension may be absent.
1503
1504*/
1505
1506 if (type == TLSEXT_TYPE_server_name)
1507 {
David Benjamindc72ff72014-06-25 12:36:10 -04001508 CBS server_name_list;
1509
1510 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1511 CBS_len(&server_name_list) < 1 ||
1512 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001513 {
David Benjamindc72ff72014-06-25 12:36:10 -04001514 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001515 return 0;
1516 }
Adam Langley95c29f32014-06-20 12:00:00 -07001517
David Benjamindc72ff72014-06-25 12:36:10 -04001518 /* Decode each ServerName in the extension. */
1519 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001520 {
David Benjamindc72ff72014-06-25 12:36:10 -04001521 uint8_t name_type;
1522 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001523
David Benjamindc72ff72014-06-25 12:36:10 -04001524 /* Decode the NameType. */
1525 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001526 {
David Benjamindc72ff72014-06-25 12:36:10 -04001527 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001528 return 0;
1529 }
David Benjamindc72ff72014-06-25 12:36:10 -04001530
David Benjamindc72ff72014-06-25 12:36:10 -04001531 /* Only host_name is supported. */
1532 if (name_type != TLSEXT_NAMETYPE_host_name)
1533 continue;
1534
1535 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001536 {
David Benjamindc72ff72014-06-25 12:36:10 -04001537 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001538 {
David Benjamindc72ff72014-06-25 12:36:10 -04001539 /* The ServerNameList MUST NOT
1540 contain more than one name of
1541 the same name_type. */
1542 *out_alert = SSL_AD_DECODE_ERROR;
1543 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001544 }
Adam Langley95c29f32014-06-20 12:00:00 -07001545
David Benjamindc72ff72014-06-25 12:36:10 -04001546 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1547 CBS_len(&host_name) < 1)
1548 {
1549 *out_alert = SSL_AD_DECODE_ERROR;
1550 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
David Benjamined439582014-07-14 19:13:02 -04001553 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1554 CBS_contains_zero_byte(&host_name))
David Benjamindc72ff72014-06-25 12:36:10 -04001555 {
1556 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1557 return 0;
1558 }
1559
1560 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001561 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001562 {
1563 *out_alert = SSL_AD_INTERNAL_ERROR;
1564 return 0;
1565 }
1566 s->servername_done = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001567 }
David Benjamindc72ff72014-06-25 12:36:10 -04001568 else
1569 {
1570 s->servername_done = s->session->tlsext_hostname
1571 && strlen(s->session->tlsext_hostname) == CBS_len(&host_name)
1572 && strncmp(s->session->tlsext_hostname,
1573 (char *)CBS_data(&host_name), CBS_len(&host_name)) == 0;
1574 }
Adam Langley95c29f32014-06-20 12:00:00 -07001575 }
Adam Langley95c29f32014-06-20 12:00:00 -07001576 }
1577
Adam Langley95c29f32014-06-20 12:00:00 -07001578 else if (type == TLSEXT_TYPE_ec_point_formats)
1579 {
David Benjamindc72ff72014-06-25 12:36:10 -04001580 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001581
David Benjamindc72ff72014-06-25 12:36:10 -04001582 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1583 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001584 {
David Benjamindc72ff72014-06-25 12:36:10 -04001585 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001586 return 0;
1587 }
David Benjamindc72ff72014-06-25 12:36:10 -04001588
Adam Langley95c29f32014-06-20 12:00:00 -07001589 if (!s->hit)
1590 {
David Benjamindc72ff72014-06-25 12:36:10 -04001591 if (!CBS_stow(&ec_point_format_list,
1592 &s->session->tlsext_ecpointformatlist,
1593 &s->session->tlsext_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001594 {
David Benjamindc72ff72014-06-25 12:36:10 -04001595 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001596 return 0;
1597 }
Adam Langley95c29f32014-06-20 12:00:00 -07001598 }
Adam Langley95c29f32014-06-20 12:00:00 -07001599 }
1600 else if (type == TLSEXT_TYPE_elliptic_curves)
1601 {
David Benjamindc72ff72014-06-25 12:36:10 -04001602 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001603 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001604
David Benjamindc72ff72014-06-25 12:36:10 -04001605 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001606 CBS_len(&elliptic_curve_list) == 0 ||
1607 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001608 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001609 {
David Benjamindc72ff72014-06-25 12:36:10 -04001610 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001611 return 0;
1612 }
David Benjamindc72ff72014-06-25 12:36:10 -04001613
Adam Langley95c29f32014-06-20 12:00:00 -07001614 if (!s->hit)
1615 {
David Benjamin072334d2014-07-13 16:24:27 -04001616 if (s->session->tlsext_ellipticcurvelist)
1617 {
1618 OPENSSL_free(s->session->tlsext_ellipticcurvelist);
1619 s->session->tlsext_ellipticcurvelist_length = 0;
1620 }
1621 s->session->tlsext_ellipticcurvelist =
1622 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1623 if (s->session->tlsext_ellipticcurvelist == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001624 {
David Benjamindc72ff72014-06-25 12:36:10 -04001625 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001626 return 0;
1627 }
David Benjamin072334d2014-07-13 16:24:27 -04001628 num_curves = CBS_len(&elliptic_curve_list) / 2;
1629 for (i = 0; i < num_curves; i++)
1630 {
1631 if (!CBS_get_u16(&elliptic_curve_list,
1632 &s->session->tlsext_ellipticcurvelist[i]))
1633 {
1634 *out_alert = SSL_AD_INTERNAL_ERROR;
1635 return 0;
1636 }
1637 }
1638 if (CBS_len(&elliptic_curve_list) != 0)
1639 {
1640 *out_alert = SSL_AD_INTERNAL_ERROR;
1641 return 0;
1642 }
1643 s->session->tlsext_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001644 }
Adam Langley95c29f32014-06-20 12:00:00 -07001645 }
Adam Langley95c29f32014-06-20 12:00:00 -07001646 else if (type == TLSEXT_TYPE_session_ticket)
1647 {
1648 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001649 !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 -07001650 {
David Benjamindc72ff72014-06-25 12:36:10 -04001651 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001652 return 0;
1653 }
1654 }
1655 else if (type == TLSEXT_TYPE_renegotiate)
1656 {
David Benjamindc72ff72014-06-25 12:36:10 -04001657 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001658 return 0;
1659 renegotiate_seen = 1;
1660 }
1661 else if (type == TLSEXT_TYPE_signature_algorithms)
1662 {
David Benjamindc72ff72014-06-25 12:36:10 -04001663 CBS supported_signature_algorithms;
1664
David Benjamindc72ff72014-06-25 12:36:10 -04001665 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1666 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001667 {
David Benjamindc72ff72014-06-25 12:36:10 -04001668 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001669 return 0;
1670 }
David Benjamindc72ff72014-06-25 12:36:10 -04001671
1672 /* Ensure the signature algorithms are non-empty. It
1673 * contains a list of SignatureAndHashAlgorithms
1674 * which are two bytes each. */
1675 if (CBS_len(&supported_signature_algorithms) == 0 ||
1676 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001677 {
David Benjamindc72ff72014-06-25 12:36:10 -04001678 *out_alert = SSL_AD_DECODE_ERROR;
1679 return 0;
1680 }
1681
David Benjamincd996942014-07-20 16:23:51 -04001682 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001683 {
1684 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001685 return 0;
1686 }
1687 /* If sigalgs received and no shared algorithms fatal
1688 * error.
1689 */
1690 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1691 {
1692 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001693 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001694 return 0;
1695 }
1696 }
1697
Adam Langley95c29f32014-06-20 12:00:00 -07001698 else if (type == TLSEXT_TYPE_next_proto_neg &&
1699 s->s3->tmp.finish_md_len == 0 &&
1700 s->s3->alpn_selected == NULL)
1701 {
David Benjamindc72ff72014-06-25 12:36:10 -04001702 /* The extension must be empty. */
1703 if (CBS_len(&extension) != 0)
1704 {
1705 *out_alert = SSL_AD_DECODE_ERROR;
1706 return 0;
1707 }
1708
Adam Langley95c29f32014-06-20 12:00:00 -07001709 /* We shouldn't accept this extension on a
1710 * renegotiation.
1711 *
1712 * s->new_session will be set on renegotiation, but we
1713 * probably shouldn't rely that it couldn't be set on
1714 * the initial renegotation too in certain cases (when
1715 * there's some other reason to disallow resuming an
1716 * earlier session -- the current code won't be doing
1717 * anything like that, but this might change).
1718
1719 * A valid sign that there's been a previous handshake
1720 * in this connection is if s->s3->tmp.finish_md_len >
1721 * 0. (We are talking about a check that will happen
1722 * in the Hello protocol round, well before a new
1723 * Finished message could have been computed.) */
1724 s->s3->next_proto_neg_seen = 1;
1725 }
Adam Langley95c29f32014-06-20 12:00:00 -07001726
1727 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1728 s->ctx->alpn_select_cb &&
1729 s->s3->tmp.finish_md_len == 0)
1730 {
David Benjamindc72ff72014-06-25 12:36:10 -04001731 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001732 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001733 /* ALPN takes precedence over NPN. */
1734 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001735 }
1736
Adam Langley1258b6a2014-06-20 12:00:00 -07001737 else if (type == TLSEXT_TYPE_channel_id &&
1738 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001739 {
1740 /* The extension must be empty. */
1741 if (CBS_len(&extension) != 0)
1742 {
1743 *out_alert = SSL_AD_DECODE_ERROR;
1744 return 0;
1745 }
1746
Adam Langley1258b6a2014-06-20 12:00:00 -07001747 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001748 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001749
1750 else if (type == TLSEXT_TYPE_channel_id_new &&
1751 s->tlsext_channel_id_enabled)
1752 {
David Benjamindc72ff72014-06-25 12:36:10 -04001753 /* The extension must be empty. */
1754 if (CBS_len(&extension) != 0)
1755 {
1756 *out_alert = SSL_AD_DECODE_ERROR;
1757 return 0;
1758 }
1759
Adam Langley1258b6a2014-06-20 12:00:00 -07001760 s->s3->tlsext_channel_id_valid = 1;
1761 s->s3->tlsext_channel_id_new = 1;
1762 }
1763
1764
Adam Langley95c29f32014-06-20 12:00:00 -07001765 /* session ticket processed earlier */
1766 else if (type == TLSEXT_TYPE_use_srtp)
1767 {
David Benjamindc72ff72014-06-25 12:36:10 -04001768 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001769 return 0;
1770 }
Adam Langley95c29f32014-06-20 12:00:00 -07001771 }
1772
Adam Langley95c29f32014-06-20 12:00:00 -07001773 ri_check:
1774
1775 /* Need RI if renegotiating */
1776
1777 if (!renegotiate_seen && s->renegotiate &&
1778 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1779 {
David Benjamindc72ff72014-06-25 12:36:10 -04001780 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley95c29f32014-06-20 12:00:00 -07001781 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1782 return 0;
1783 }
1784 /* If no signature algorithms extension set default values */
1785 if (!s->cert->peer_sigalgs)
1786 ssl_cert_set_default_md(s->cert);
1787
1788 return 1;
1789 }
1790
David Benjamindc72ff72014-06-25 12:36:10 -04001791int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001792 {
David Benjamindc72ff72014-06-25 12:36:10 -04001793 int alert = -1;
1794 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001795 {
David Benjamindc72ff72014-06-25 12:36:10 -04001796 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001797 return 0;
1798 }
1799
David Benjamin6c7aed02014-08-27 16:42:38 -04001800 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001801 {
David Benjamin9d28c752014-07-05 00:43:48 -04001802 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001803 return 0;
1804 }
1805 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001806 }
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langley95c29f32014-06-20 12:00:00 -07001808/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1809 * elements of zero length are allowed and the set of elements must exactly fill
1810 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001811static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001812 {
David Benjamin03973092014-06-24 23:27:17 -04001813 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001814
David Benjamin03973092014-06-24 23:27:17 -04001815 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001816 {
David Benjamin03973092014-06-24 23:27:17 -04001817 CBS proto;
1818 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1819 CBS_len(&proto) == 0)
1820 {
Adam Langley95c29f32014-06-20 12:00:00 -07001821 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001822 }
Adam Langley95c29f32014-06-20 12:00:00 -07001823 }
David Benjamin03973092014-06-24 23:27:17 -04001824 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001825 }
Adam Langley95c29f32014-06-20 12:00:00 -07001826
David Benjamin03973092014-06-24 23:27:17 -04001827static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001828 {
Adam Langley95c29f32014-06-20 12:00:00 -07001829 int tlsext_servername = 0;
1830 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001831 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001832
David Benjamin6c7aed02014-08-27 16:42:38 -04001833 /* TODO(davidben): Move all of these to some per-handshake state that
1834 * gets systematically reset on a new handshake; perhaps allocate it
1835 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001836 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
David Benjamin6c7aed02014-08-27 16:42:38 -04001838 s->tlsext_ticket_expected = 0;
1839 s->s3->tmp.certificate_status_expected = 0;
David Benjamin64442872014-07-21 17:43:45 -04001840
Adam Langley95c29f32014-06-20 12:00:00 -07001841 if (s->s3->alpn_selected)
1842 {
1843 OPENSSL_free(s->s3->alpn_selected);
1844 s->s3->alpn_selected = NULL;
1845 }
1846
David Benjamin03973092014-06-24 23:27:17 -04001847 /* There may be no extensions. */
1848 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001849 {
David Benjamin03973092014-06-24 23:27:17 -04001850 goto ri_check;
1851 }
1852
David Benjamin35a7a442014-07-05 00:23:20 -04001853 /* Decode the extensions block and check it is valid. */
1854 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1855 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001856 {
1857 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001858 return 0;
1859 }
1860
David Benjamin03973092014-06-24 23:27:17 -04001861 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001862 {
David Benjamin03973092014-06-24 23:27:17 -04001863 uint16_t type;
1864 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001865
David Benjamin03973092014-06-24 23:27:17 -04001866 /* Decode the next extension. */
1867 if (!CBS_get_u16(&extensions, &type) ||
1868 !CBS_get_u16_length_prefixed(&extensions, &extension))
1869 {
1870 *out_alert = SSL_AD_DECODE_ERROR;
1871 return 0;
1872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
1874 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001875 {
1876 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1877 CBS_len(&extension), s->tlsext_debug_arg);
1878 }
Adam Langley95c29f32014-06-20 12:00:00 -07001879
1880 if (type == TLSEXT_TYPE_server_name)
1881 {
David Benjamin03973092014-06-24 23:27:17 -04001882 /* The extension must be empty. */
1883 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001884 {
David Benjamin03973092014-06-24 23:27:17 -04001885 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001886 return 0;
1887 }
David Benjamin03973092014-06-24 23:27:17 -04001888 /* We must have sent it in ClientHello. */
1889 if (s->tlsext_hostname == NULL)
1890 {
1891 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1892 return 0;
1893 }
1894 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001895 }
Adam Langley95c29f32014-06-20 12:00:00 -07001896 else if (type == TLSEXT_TYPE_ec_point_formats)
1897 {
David Benjamin03973092014-06-24 23:27:17 -04001898 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001899
David Benjamin03973092014-06-24 23:27:17 -04001900 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1901 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001902 {
David Benjamin03973092014-06-24 23:27:17 -04001903 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001904 return 0;
1905 }
David Benjamin03973092014-06-24 23:27:17 -04001906
Adam Langley5ba06a72014-08-06 17:27:31 -07001907 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001908 {
Adam Langley5ba06a72014-08-06 17:27:31 -07001909 if (!CBS_stow(&ec_point_format_list,
1910 &s->session->tlsext_ecpointformatlist,
1911 &s->session->tlsext_ecpointformatlist_length))
1912 {
1913 *out_alert = SSL_AD_INTERNAL_ERROR;
1914 return 0;
1915 }
Adam Langley95c29f32014-06-20 12:00:00 -07001916 }
Adam Langley95c29f32014-06-20 12:00:00 -07001917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918 else if (type == TLSEXT_TYPE_session_ticket)
1919 {
1920 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001921 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1922 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001923 {
David Benjamin03973092014-06-24 23:27:17 -04001924 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001925 return 0;
1926 }
David Benjamin03973092014-06-24 23:27:17 -04001927
1928 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001929 {
David Benjamin03973092014-06-24 23:27:17 -04001930 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001931 return 0;
1932 }
David Benjamin03973092014-06-24 23:27:17 -04001933
Adam Langley95c29f32014-06-20 12:00:00 -07001934 s->tlsext_ticket_expected = 1;
1935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936 else if (type == TLSEXT_TYPE_status_request)
1937 {
David Benjamin03973092014-06-24 23:27:17 -04001938 /* The extension MUST be empty and may only sent if
1939 * we've requested a status request message. */
1940 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001941 {
David Benjamin03973092014-06-24 23:27:17 -04001942 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001943 return 0;
1944 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001945 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001946 {
1947 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1948 return 0;
1949 }
1950 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001951 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001952 }
David Benjamin03973092014-06-24 23:27:17 -04001953 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1954 unsigned char *selected;
1955 unsigned char selected_len;
1956
1957 /* We must have requested it. */
1958 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001959 {
David Benjamin03973092014-06-24 23:27:17 -04001960 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1961 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001962 }
Adam Langley95c29f32014-06-20 12:00:00 -07001963
David Benjamin03973092014-06-24 23:27:17 -04001964 /* The data must be valid. */
1965 if (!ssl_next_proto_validate(&extension))
1966 {
1967 *out_alert = SSL_AD_DECODE_ERROR;
1968 return 0;
1969 }
1970
1971 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
1972 CBS_data(&extension), CBS_len(&extension),
1973 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1974 {
1975 *out_alert = SSL_AD_INTERNAL_ERROR;
1976 return 0;
1977 }
1978
1979 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1980 if (s->next_proto_negotiated == NULL)
1981 {
1982 *out_alert = SSL_AD_INTERNAL_ERROR;
1983 return 0;
1984 }
1985 s->next_proto_negotiated_len = selected_len;
1986 s->s3->next_proto_neg_seen = 1;
1987 }
Adam Langley95c29f32014-06-20 12:00:00 -07001988 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
1989 {
David Benjamin03973092014-06-24 23:27:17 -04001990 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001991
1992 /* We must have requested it. */
1993 if (s->alpn_client_proto_list == NULL)
1994 {
David Benjamin03973092014-06-24 23:27:17 -04001995 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001996 return 0;
1997 }
David Benjamin03973092014-06-24 23:27:17 -04001998
1999 /* The extension data consists of a ProtocolNameList
2000 * which must have exactly one ProtocolName. Each of
2001 * these is length-prefixed. */
2002 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2003 CBS_len(&extension) != 0 ||
2004 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2005 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002006 {
David Benjamin03973092014-06-24 23:27:17 -04002007 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002008 return 0;
2009 }
David Benjamin03973092014-06-24 23:27:17 -04002010
2011 if (!CBS_stow(&protocol_name,
2012 &s->s3->alpn_selected,
2013 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002014 {
David Benjamin03973092014-06-24 23:27:17 -04002015 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002016 return 0;
2017 }
Adam Langley95c29f32014-06-20 12:00:00 -07002018 }
2019
Adam Langley1258b6a2014-06-20 12:00:00 -07002020 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002021 {
2022 if (CBS_len(&extension) != 0)
2023 {
2024 *out_alert = SSL_AD_DECODE_ERROR;
2025 return 0;
2026 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002027 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002028 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002029 else if (type == TLSEXT_TYPE_channel_id_new)
2030 {
David Benjamin03973092014-06-24 23:27:17 -04002031 if (CBS_len(&extension) != 0)
2032 {
2033 *out_alert = SSL_AD_DECODE_ERROR;
2034 return 0;
2035 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002036 s->s3->tlsext_channel_id_valid = 1;
2037 s->s3->tlsext_channel_id_new = 1;
2038 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002039 else if (type == TLSEXT_TYPE_certificate_timestamp)
2040 {
2041 if (CBS_len(&extension) == 0)
2042 {
2043 *out_alert = SSL_AD_DECODE_ERROR;
2044 return 0;
2045 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002046
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002047 /* Session resumption uses the original session information. */
2048 if (!s->hit)
2049 {
2050 if (!CBS_stow(&extension,
2051 &s->session->tlsext_signed_cert_timestamp_list,
2052 &s->session->tlsext_signed_cert_timestamp_list_length))
2053 {
2054 *out_alert = SSL_AD_INTERNAL_ERROR;
2055 return 0;
2056 }
2057 }
2058 }
Adam Langley95c29f32014-06-20 12:00:00 -07002059 else if (type == TLSEXT_TYPE_renegotiate)
2060 {
David Benjamin03973092014-06-24 23:27:17 -04002061 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002062 return 0;
2063 renegotiate_seen = 1;
2064 }
Adam Langley95c29f32014-06-20 12:00:00 -07002065 else if (type == TLSEXT_TYPE_use_srtp)
2066 {
David Benjamin03973092014-06-24 23:27:17 -04002067 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002068 return 0;
2069 }
Adam Langley95c29f32014-06-20 12:00:00 -07002070 }
2071
2072 if (!s->hit && tlsext_servername == 1)
2073 {
2074 if (s->tlsext_hostname)
2075 {
2076 if (s->session->tlsext_hostname == NULL)
2077 {
2078 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2079 if (!s->session->tlsext_hostname)
2080 {
David Benjamin03973092014-06-24 23:27:17 -04002081 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002082 return 0;
2083 }
2084 }
2085 else
2086 {
David Benjamin03973092014-06-24 23:27:17 -04002087 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002088 return 0;
2089 }
2090 }
2091 }
2092
Adam Langley95c29f32014-06-20 12:00:00 -07002093 ri_check:
2094
2095 /* Determine if we need to see RI. Strictly speaking if we want to
2096 * avoid an attack we should *always* see RI even on initial server
2097 * hello because the client doesn't see any renegotiation during an
2098 * attack. However this would mean we could not connect to any server
2099 * which doesn't support RI so for the immediate future tolerate RI
2100 * absence on initial connect only.
2101 */
2102 if (!renegotiate_seen
2103 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2104 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2105 {
David Benjamin03973092014-06-24 23:27:17 -04002106 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002107 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002108 return 0;
2109 }
2110
2111 return 1;
2112 }
2113
2114
2115int ssl_prepare_clienthello_tlsext(SSL *s)
2116 {
Adam Langley95c29f32014-06-20 12:00:00 -07002117 return 1;
2118 }
2119
2120int ssl_prepare_serverhello_tlsext(SSL *s)
2121 {
2122 return 1;
2123 }
2124
David Benjamin6c7aed02014-08-27 16:42:38 -04002125static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002126 {
2127 int ret=SSL_TLSEXT_ERR_NOACK;
2128 int al = SSL_AD_UNRECOGNIZED_NAME;
2129
Adam Langley95c29f32014-06-20 12:00:00 -07002130 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2131 * ssl3_choose_cipher in s3_lib.c.
2132 */
2133 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2134 * ssl3_choose_cipher in s3_lib.c.
2135 */
Adam Langley95c29f32014-06-20 12:00:00 -07002136
2137 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2138 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2139 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2140 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2141
Adam Langley95c29f32014-06-20 12:00:00 -07002142 switch (ret)
2143 {
2144 case SSL_TLSEXT_ERR_ALERT_FATAL:
2145 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2146 return -1;
2147
2148 case SSL_TLSEXT_ERR_ALERT_WARNING:
2149 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2150 return 1;
2151
2152 case SSL_TLSEXT_ERR_NOACK:
2153 s->servername_done=0;
2154 default:
2155 return 1;
2156 }
2157 }
2158
David Benjamin6c7aed02014-08-27 16:42:38 -04002159static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002160 {
2161 int ret=SSL_TLSEXT_ERR_NOACK;
2162 int al = SSL_AD_UNRECOGNIZED_NAME;
2163
Adam Langley95c29f32014-06-20 12:00:00 -07002164 /* If we are client and using an elliptic curve cryptography cipher
2165 * suite, then if server returns an EC point formats lists extension
2166 * it must contain uncompressed.
2167 */
2168 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2169 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2170 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2171 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
David Benjamin0da0e182014-08-19 16:20:28 -04002172 ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07002173 {
2174 /* we are using an ECC cipher */
2175 size_t i;
2176 unsigned char *list;
2177 int found_uncompressed = 0;
2178 list = s->session->tlsext_ecpointformatlist;
2179 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2180 {
2181 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
2182 {
2183 found_uncompressed = 1;
2184 break;
2185 }
2186 }
2187 if (!found_uncompressed)
2188 {
2189 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2190 return -1;
2191 }
2192 }
2193 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002194
2195 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2196 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2197 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2198 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2199
Adam Langley95c29f32014-06-20 12:00:00 -07002200 switch (ret)
2201 {
2202 case SSL_TLSEXT_ERR_ALERT_FATAL:
2203 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2204 return -1;
2205
2206 case SSL_TLSEXT_ERR_ALERT_WARNING:
2207 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2208 return 1;
2209
2210 case SSL_TLSEXT_ERR_NOACK:
2211 s->servername_done=0;
2212 default:
2213 return 1;
2214 }
2215 }
2216
David Benjamin03973092014-06-24 23:27:17 -04002217int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002218 {
David Benjamin03973092014-06-24 23:27:17 -04002219 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002220 if (s->version < SSL3_VERSION)
2221 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002222
2223 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002224 {
David Benjamin03973092014-06-24 23:27:17 -04002225 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002226 return 0;
2227 }
2228
David Benjamin03973092014-06-24 23:27:17 -04002229 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002230 {
David Benjamin9d28c752014-07-05 00:43:48 -04002231 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002232 return 0;
2233 }
David Benjamin03973092014-06-24 23:27:17 -04002234
Adam Langley95c29f32014-06-20 12:00:00 -07002235 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002236 }
Adam Langley95c29f32014-06-20 12:00:00 -07002237
2238/* Since the server cache lookup is done early on in the processing of the
2239 * ClientHello, and other operations depend on the result, we need to handle
2240 * any TLS session ticket extension at the same time.
2241 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002242 * ctx: contains the early callback context, which is the result of a
2243 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002244 * ret: (output) on return, if a ticket was decrypted, then this is set to
2245 * point to the resulting session.
2246 *
2247 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2248 * ciphersuite, in which case we have no use for session tickets and one will
2249 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2250 *
2251 * Returns:
2252 * -1: fatal error, either from parsing or decrypting the ticket.
2253 * 0: no ticket was found (or was ignored, based on settings).
2254 * 1: a zero length extension was found, indicating that the client supports
2255 * session tickets but doesn't currently have one to offer.
2256 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2257 * couldn't be decrypted because of a non-fatal error.
2258 * 3: a ticket was successfully decrypted and *ret was set.
2259 *
2260 * Side effects:
2261 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2262 * a new session ticket to the client because the client indicated support
2263 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2264 * a session ticket or we couldn't use the one it gave us, or if
2265 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2266 * Otherwise, s->tlsext_ticket_expected is set to 0.
2267 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002268int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2269 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002270 {
Adam Langley95c29f32014-06-20 12:00:00 -07002271 *ret = NULL;
2272 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002273 const unsigned char *data;
2274 size_t len;
2275 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002276
2277 /* If tickets disabled behave as if no ticket present
2278 * to permit stateful resumption.
2279 */
2280 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2281 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002282 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002283 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002284 if (!SSL_early_callback_ctx_extension_get(
2285 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002286 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002287 return 0;
2288 }
2289 if (len == 0)
2290 {
2291 /* The client will accept a ticket but doesn't
2292 * currently have one. */
2293 s->tlsext_ticket_expected = 1;
2294 return 1;
2295 }
2296 if (s->tls_session_secret_cb)
2297 {
2298 /* Indicate that the ticket couldn't be
2299 * decrypted rather than generating the session
2300 * from ticket now, trigger abbreviated
2301 * handshake based on external mechanism to
2302 * calculate the master secret later. */
2303 return 2;
2304 }
2305 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2306 ctx->session_id_len, ret);
2307 switch (r)
2308 {
2309 case 2: /* ticket couldn't be decrypted */
2310 s->tlsext_ticket_expected = 1;
2311 return 2;
2312 case 3: /* ticket was decrypted */
2313 return r;
2314 case 4: /* ticket decrypted but need to renew */
2315 s->tlsext_ticket_expected = 1;
2316 return 3;
2317 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002318 return -1;
2319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320 }
2321
2322/* tls_decrypt_ticket attempts to decrypt a session ticket.
2323 *
2324 * etick: points to the body of the session ticket extension.
2325 * eticklen: the length of the session tickets extenion.
2326 * sess_id: points at the session ID.
2327 * sesslen: the length of the session ID.
2328 * psess: (output) on return, if a ticket was decrypted, then this is set to
2329 * point to the resulting session.
2330 *
2331 * Returns:
2332 * -1: fatal error, either from parsing or decrypting the ticket.
2333 * 2: the ticket couldn't be decrypted.
2334 * 3: a ticket was successfully decrypted and *psess was set.
2335 * 4: same as 3, but the ticket needs to be renewed.
2336 */
2337static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2338 const unsigned char *sess_id, int sesslen,
2339 SSL_SESSION **psess)
2340 {
2341 SSL_SESSION *sess;
2342 unsigned char *sdec;
2343 const unsigned char *p;
2344 int slen, mlen, renew_ticket = 0;
2345 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2346 HMAC_CTX hctx;
2347 EVP_CIPHER_CTX ctx;
2348 SSL_CTX *tctx = s->initial_ctx;
2349 /* Need at least keyname + iv + some encrypted data */
2350 if (eticklen < 48)
2351 return 2;
2352 /* Initialize session ticket encryption and HMAC contexts */
2353 HMAC_CTX_init(&hctx);
2354 EVP_CIPHER_CTX_init(&ctx);
2355 if (tctx->tlsext_ticket_key_cb)
2356 {
2357 unsigned char *nctick = (unsigned char *)etick;
2358 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2359 &ctx, &hctx, 0);
2360 if (rv < 0)
2361 return -1;
2362 if (rv == 0)
2363 return 2;
2364 if (rv == 2)
2365 renew_ticket = 1;
2366 }
2367 else
2368 {
2369 /* Check key name matches */
2370 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2371 return 2;
2372 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2373 tlsext_tick_md(), NULL);
2374 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2375 tctx->tlsext_tick_aes_key, etick + 16);
2376 }
2377 /* Attempt to process session ticket, first conduct sanity and
2378 * integrity checks on ticket.
2379 */
2380 mlen = HMAC_size(&hctx);
2381 if (mlen < 0)
2382 {
2383 EVP_CIPHER_CTX_cleanup(&ctx);
2384 return -1;
2385 }
2386 eticklen -= mlen;
2387 /* Check HMAC of encrypted ticket */
2388 HMAC_Update(&hctx, etick, eticklen);
2389 HMAC_Final(&hctx, tick_hmac, NULL);
2390 HMAC_CTX_cleanup(&hctx);
2391 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
2392 return 2;
2393 /* Attempt to decrypt session data */
2394 /* Move p after IV to start of encrypted ticket, update length */
2395 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2396 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2397 sdec = OPENSSL_malloc(eticklen);
2398 if (!sdec)
2399 {
2400 EVP_CIPHER_CTX_cleanup(&ctx);
2401 return -1;
2402 }
2403 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2404 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002405 {
2406 EVP_CIPHER_CTX_cleanup(&ctx);
2407 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002408 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002409 }
Adam Langley95c29f32014-06-20 12:00:00 -07002410 slen += mlen;
2411 EVP_CIPHER_CTX_cleanup(&ctx);
2412 p = sdec;
2413
2414 sess = d2i_SSL_SESSION(NULL, &p, slen);
2415 OPENSSL_free(sdec);
2416 if (sess)
2417 {
2418 /* The session ID, if non-empty, is used by some clients to
2419 * detect that the ticket has been accepted. So we copy it to
2420 * the session structure. If it is empty set length to zero
2421 * as required by standard.
2422 */
2423 if (sesslen)
2424 memcpy(sess->session_id, sess_id, sesslen);
2425 sess->session_id_length = sesslen;
2426 *psess = sess;
2427 if (renew_ticket)
2428 return 4;
2429 else
2430 return 3;
2431 }
2432 ERR_clear_error();
2433 /* For session parse failure, indicate that we need to send a new
2434 * ticket. */
2435 return 2;
2436 }
2437
2438/* Tables to translate from NIDs to TLS v1.2 ids */
2439
2440typedef struct
2441 {
2442 int nid;
2443 int id;
2444 } tls12_lookup;
2445
David Benjamincff64722014-08-19 19:54:46 -04002446static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002447 {NID_md5, TLSEXT_hash_md5},
2448 {NID_sha1, TLSEXT_hash_sha1},
2449 {NID_sha224, TLSEXT_hash_sha224},
2450 {NID_sha256, TLSEXT_hash_sha256},
2451 {NID_sha384, TLSEXT_hash_sha384},
2452 {NID_sha512, TLSEXT_hash_sha512}
2453};
2454
David Benjamincff64722014-08-19 19:54:46 -04002455static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002456 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002457 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2458};
2459
David Benjamincff64722014-08-19 19:54:46 -04002460static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002461 {
2462 size_t i;
2463 for (i = 0; i < tlen; i++)
2464 {
2465 if (table[i].nid == nid)
2466 return table[i].id;
2467 }
2468 return -1;
2469 }
2470
David Benjamincff64722014-08-19 19:54:46 -04002471static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002472 {
2473 size_t i;
2474 for (i = 0; i < tlen; i++)
2475 {
2476 if ((table[i].id) == id)
2477 return table[i].nid;
2478 }
2479 return NID_undef;
2480 }
2481
2482int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2483 {
2484 int sig_id, md_id;
2485 if (!md)
2486 return 0;
2487 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2488 sizeof(tls12_md)/sizeof(tls12_lookup));
2489 if (md_id == -1)
2490 return 0;
2491 sig_id = tls12_get_sigid(pk);
2492 if (sig_id == -1)
2493 return 0;
2494 p[0] = (unsigned char)md_id;
2495 p[1] = (unsigned char)sig_id;
2496 return 1;
2497 }
2498
2499int tls12_get_sigid(const EVP_PKEY *pk)
2500 {
2501 return tls12_find_id(pk->type, tls12_sig,
2502 sizeof(tls12_sig)/sizeof(tls12_lookup));
2503 }
2504
2505const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2506 {
2507 switch(hash_alg)
2508 {
Adam Langley95c29f32014-06-20 12:00:00 -07002509 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002510 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002511 case TLSEXT_hash_sha1:
2512 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002513 case TLSEXT_hash_sha224:
2514 return EVP_sha224();
2515
2516 case TLSEXT_hash_sha256:
2517 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002518 case TLSEXT_hash_sha384:
2519 return EVP_sha384();
2520
2521 case TLSEXT_hash_sha512:
2522 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002523 default:
2524 return NULL;
2525
2526 }
2527 }
2528
2529static int tls12_get_pkey_idx(unsigned char sig_alg)
2530 {
2531 switch(sig_alg)
2532 {
Adam Langley95c29f32014-06-20 12:00:00 -07002533 case TLSEXT_signature_rsa:
2534 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002535 case TLSEXT_signature_ecdsa:
2536 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002537 }
2538 return -1;
2539 }
2540
2541/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2542static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2543 int *psignhash_nid, const unsigned char *data)
2544 {
2545 int sign_nid = 0, hash_nid = 0;
2546 if (!phash_nid && !psign_nid && !psignhash_nid)
2547 return;
2548 if (phash_nid || psignhash_nid)
2549 {
2550 hash_nid = tls12_find_nid(data[0], tls12_md,
2551 sizeof(tls12_md)/sizeof(tls12_lookup));
2552 if (phash_nid)
2553 *phash_nid = hash_nid;
2554 }
2555 if (psign_nid || psignhash_nid)
2556 {
2557 sign_nid = tls12_find_nid(data[1], tls12_sig,
2558 sizeof(tls12_sig)/sizeof(tls12_lookup));
2559 if (psign_nid)
2560 *psign_nid = sign_nid;
2561 }
2562 if (psignhash_nid)
2563 {
2564 if (sign_nid && hash_nid)
2565 OBJ_find_sigid_by_algs(psignhash_nid,
2566 hash_nid, sign_nid);
2567 else
2568 *psignhash_nid = NID_undef;
2569 }
2570 }
2571/* Given preference and allowed sigalgs set shared sigalgs */
2572static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2573 const unsigned char *pref, size_t preflen,
2574 const unsigned char *allow, size_t allowlen)
2575 {
2576 const unsigned char *ptmp, *atmp;
2577 size_t i, j, nmatch = 0;
2578 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2579 {
2580 /* Skip disabled hashes or signature algorithms */
2581 if (tls12_get_hash(ptmp[0]) == NULL)
2582 continue;
2583 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2584 continue;
2585 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2586 {
2587 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2588 {
2589 nmatch++;
2590 if (shsig)
2591 {
2592 shsig->rhash = ptmp[0];
2593 shsig->rsign = ptmp[1];
2594 tls1_lookup_sigalg(&shsig->hash_nid,
2595 &shsig->sign_nid,
2596 &shsig->signandhash_nid,
2597 ptmp);
2598 shsig++;
2599 }
2600 break;
2601 }
2602 }
2603 }
2604 return nmatch;
2605 }
2606
2607/* Set shared signature algorithms for SSL structures */
2608static int tls1_set_shared_sigalgs(SSL *s)
2609 {
2610 const unsigned char *pref, *allow, *conf;
2611 size_t preflen, allowlen, conflen;
2612 size_t nmatch;
2613 TLS_SIGALGS *salgs = NULL;
2614 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002615 if (c->shared_sigalgs)
2616 {
2617 OPENSSL_free(c->shared_sigalgs);
2618 c->shared_sigalgs = NULL;
2619 }
Adam Langley95c29f32014-06-20 12:00:00 -07002620 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002621 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002622 {
2623 conf = c->client_sigalgs;
2624 conflen = c->client_sigalgslen;
2625 }
David Benjamin335d10d2014-08-06 19:56:33 -04002626 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002627 {
2628 conf = c->conf_sigalgs;
2629 conflen = c->conf_sigalgslen;
2630 }
2631 else
2632 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002633 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002634 {
2635 pref = conf;
2636 preflen = conflen;
2637 allow = c->peer_sigalgs;
2638 allowlen = c->peer_sigalgslen;
2639 }
2640 else
2641 {
2642 allow = conf;
2643 allowlen = conflen;
2644 pref = c->peer_sigalgs;
2645 preflen = c->peer_sigalgslen;
2646 }
2647 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2648 if (!nmatch)
2649 return 1;
2650 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2651 if (!salgs)
2652 return 0;
2653 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2654 c->shared_sigalgs = salgs;
2655 c->shared_sigalgslen = nmatch;
2656 return 1;
2657 }
2658
2659
2660/* Set preferred digest for each key type */
2661
David Benjamincd996942014-07-20 16:23:51 -04002662int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002663 {
2664 int idx;
2665 size_t i;
2666 const EVP_MD *md;
2667 CERT *c = s->cert;
2668 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002669
Adam Langley95c29f32014-06-20 12:00:00 -07002670 /* Extension ignored for inappropriate versions */
2671 if (!SSL_USE_SIGALGS(s))
2672 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002673 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002674 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002675 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002676 /* Should never happen */
2677 if (!c)
2678 return 0;
2679
David Benjamincd996942014-07-20 16:23:51 -04002680 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002681 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002682
2683 tls1_set_shared_sigalgs(s);
2684
2685#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2686 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2687 {
2688 /* Use first set signature preference to force message
2689 * digest, ignoring any peer preferences.
2690 */
2691 const unsigned char *sigs = NULL;
2692 if (s->server)
2693 sigs = c->conf_sigalgs;
2694 else
2695 sigs = c->client_sigalgs;
2696 if (sigs)
2697 {
2698 idx = tls12_get_pkey_idx(sigs[1]);
2699 md = tls12_get_hash(sigs[0]);
2700 c->pkeys[idx].digest = md;
2701 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2702 if (idx == SSL_PKEY_RSA_SIGN)
2703 {
2704 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2705 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2706 }
2707 }
2708 }
2709#endif
2710
2711 for (i = 0, sigptr = c->shared_sigalgs;
2712 i < c->shared_sigalgslen; i++, sigptr++)
2713 {
2714 idx = tls12_get_pkey_idx(sigptr->rsign);
2715 if (idx > 0 && c->pkeys[idx].digest == NULL)
2716 {
2717 md = tls12_get_hash(sigptr->rhash);
2718 c->pkeys[idx].digest = md;
2719 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2720 if (idx == SSL_PKEY_RSA_SIGN)
2721 {
2722 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2723 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2724 }
2725 }
2726
2727 }
2728 /* In strict mode leave unset digests as NULL to indicate we can't
2729 * use the certificate for signing.
2730 */
2731 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
2732 {
2733 /* Set any remaining keys to default values. NOTE: if alg is
2734 * not supported it stays as NULL.
2735 */
Adam Langley95c29f32014-06-20 12:00:00 -07002736 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2737 {
2738 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2739 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2740 }
Adam Langley95c29f32014-06-20 12:00:00 -07002741 if (!c->pkeys[SSL_PKEY_ECC].digest)
2742 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002743 }
2744 return 1;
2745 }
2746
2747
2748int SSL_get_sigalgs(SSL *s, int idx,
2749 int *psign, int *phash, int *psignhash,
2750 unsigned char *rsig, unsigned char *rhash)
2751 {
2752 const unsigned char *psig = s->cert->peer_sigalgs;
2753 if (psig == NULL)
2754 return 0;
2755 if (idx >= 0)
2756 {
2757 idx <<= 1;
2758 if (idx >= (int)s->cert->peer_sigalgslen)
2759 return 0;
2760 psig += idx;
2761 if (rhash)
2762 *rhash = psig[0];
2763 if (rsig)
2764 *rsig = psig[1];
2765 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2766 }
2767 return s->cert->peer_sigalgslen / 2;
2768 }
2769
2770int SSL_get_shared_sigalgs(SSL *s, int idx,
2771 int *psign, int *phash, int *psignhash,
2772 unsigned char *rsig, unsigned char *rhash)
2773 {
2774 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2775 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2776 return 0;
2777 shsigalgs += idx;
2778 if (phash)
2779 *phash = shsigalgs->hash_nid;
2780 if (psign)
2781 *psign = shsigalgs->sign_nid;
2782 if (psignhash)
2783 *psignhash = shsigalgs->signandhash_nid;
2784 if (rsig)
2785 *rsig = shsigalgs->rsign;
2786 if (rhash)
2787 *rhash = shsigalgs->rhash;
2788 return s->cert->shared_sigalgslen;
2789 }
2790
Adam Langley1258b6a2014-06-20 12:00:00 -07002791/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2792 * SSL connection and writes it to |md|. */
2793int
2794tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2795 {
2796 EVP_MD_CTX ctx;
2797 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2798 unsigned temp_digest_len;
2799 int i;
2800 static const char kClientIDMagic[] = "TLS Channel ID signature";
2801
2802 if (s->s3->handshake_buffer)
2803 if (!ssl3_digest_cached_records(s))
2804 return 0;
2805
2806 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2807
2808 if (s->hit && s->s3->tlsext_channel_id_new)
2809 {
2810 static const char kResumptionMagic[] = "Resumption";
2811 EVP_DigestUpdate(md, kResumptionMagic,
2812 sizeof(kResumptionMagic));
2813 if (s->session->original_handshake_hash_len == 0)
2814 return 0;
2815 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2816 s->session->original_handshake_hash_len);
2817 }
2818
2819 EVP_MD_CTX_init(&ctx);
2820 for (i = 0; i < SSL_MAX_DIGEST; i++)
2821 {
2822 if (s->s3->handshake_dgst[i] == NULL)
2823 continue;
2824 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2825 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2826 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2827 }
2828 EVP_MD_CTX_cleanup(&ctx);
2829
2830 return 1;
2831 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002832
2833/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2834 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2835int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2836 {
2837 int digest_len;
2838 /* This function should never be called for a resumed session because
2839 * the handshake hashes that we wish to record are for the original,
2840 * full handshake. */
2841 if (s->hit)
2842 return -1;
2843 /* It only makes sense to call this function if Channel IDs have been
2844 * negotiated. */
2845 if (!s->s3->tlsext_channel_id_new)
2846 return -1;
2847
2848 digest_len = tls1_handshake_digest(
2849 s, s->session->original_handshake_hash,
2850 sizeof(s->session->original_handshake_hash));
2851 if (digest_len < 0)
2852 return -1;
2853
2854 s->session->original_handshake_hash_len = digest_len;
2855
2856 return 1;
2857 }
2858
Adam Langley95c29f32014-06-20 12:00:00 -07002859int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2860 {
2861 unsigned char *sigalgs, *sptr;
2862 int rhash, rsign;
2863 size_t i;
2864 if (salglen & 1)
2865 return 0;
2866 sigalgs = OPENSSL_malloc(salglen);
2867 if (sigalgs == NULL)
2868 return 0;
2869 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2870 {
2871 rhash = tls12_find_id(*psig_nids++, tls12_md,
2872 sizeof(tls12_md)/sizeof(tls12_lookup));
2873 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2874 sizeof(tls12_sig)/sizeof(tls12_lookup));
2875
2876 if (rhash == -1 || rsign == -1)
2877 goto err;
2878 *sptr++ = rhash;
2879 *sptr++ = rsign;
2880 }
2881
2882 if (client)
2883 {
2884 if (c->client_sigalgs)
2885 OPENSSL_free(c->client_sigalgs);
2886 c->client_sigalgs = sigalgs;
2887 c->client_sigalgslen = salglen;
2888 }
2889 else
2890 {
2891 if (c->conf_sigalgs)
2892 OPENSSL_free(c->conf_sigalgs);
2893 c->conf_sigalgs = sigalgs;
2894 c->conf_sigalgslen = salglen;
2895 }
2896
2897 return 1;
2898
2899 err:
2900 OPENSSL_free(sigalgs);
2901 return 0;
2902 }
2903
2904static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
2905 {
2906 int sig_nid;
2907 size_t i;
2908 if (default_nid == -1)
2909 return 1;
2910 sig_nid = X509_get_signature_nid(x);
2911 if (default_nid)
2912 return sig_nid == default_nid ? 1 : 0;
2913 for (i = 0; i < c->shared_sigalgslen; i++)
2914 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
2915 return 1;
2916 return 0;
2917 }
2918/* Check to see if a certificate issuer name matches list of CA names */
2919static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2920 {
2921 X509_NAME *nm;
2922 int i;
2923 nm = X509_get_issuer_name(x);
2924 for (i = 0; i < sk_X509_NAME_num(names); i++)
2925 {
2926 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2927 return 1;
2928 }
2929 return 0;
2930 }
2931
2932/* Check certificate chain is consistent with TLS extensions and is
2933 * usable by server. This servers two purposes: it allows users to
2934 * check chains before passing them to the server and it allows the
2935 * server to check chains before attempting to use them.
2936 */
2937
2938/* Flags which need to be set for a certificate when stict mode not set */
2939
2940#define CERT_PKEY_VALID_FLAGS \
2941 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2942/* Strict mode flags */
2943#define CERT_PKEY_STRICT_FLAGS \
2944 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2945 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2946
2947int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2948 int idx)
2949 {
2950 int i;
2951 int rv = 0;
2952 int check_flags = 0, strict_mode;
2953 CERT_PKEY *cpk = NULL;
2954 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002955 /* idx == -1 means checking server chains */
2956 if (idx != -1)
2957 {
2958 /* idx == -2 means checking client certificate chains */
2959 if (idx == -2)
2960 {
2961 cpk = c->key;
2962 idx = cpk - c->pkeys;
2963 }
2964 else
2965 cpk = c->pkeys + idx;
2966 x = cpk->x509;
2967 pk = cpk->privatekey;
2968 chain = cpk->chain;
2969 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2970 /* If no cert or key, forget it */
2971 if (!x || !pk)
2972 goto end;
2973#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2974 /* Allow any certificate to pass test */
2975 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2976 {
2977 rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
2978 cpk->valid_flags = rv;
2979 return rv;
2980 }
2981#endif
2982 }
2983 else
2984 {
2985 if (!x || !pk)
2986 goto end;
2987 idx = ssl_cert_type(x, pk);
2988 if (idx == -1)
2989 goto end;
2990 cpk = c->pkeys + idx;
2991 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
2992 check_flags = CERT_PKEY_STRICT_FLAGS;
2993 else
2994 check_flags = CERT_PKEY_VALID_FLAGS;
2995 strict_mode = 1;
2996 }
2997
Adam Langley95c29f32014-06-20 12:00:00 -07002998 /* Check all signature algorithms are consistent with
2999 * signature algorithms extension if TLS 1.2 or later
3000 * and strict mode.
3001 */
3002 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
3003 {
3004 int default_nid;
3005 unsigned char rsign = 0;
3006 if (c->peer_sigalgs)
3007 default_nid = 0;
3008 /* If no sigalgs extension use defaults from RFC5246 */
3009 else
3010 {
3011 switch(idx)
3012 {
3013 case SSL_PKEY_RSA_ENC:
3014 case SSL_PKEY_RSA_SIGN:
Adam Langley95c29f32014-06-20 12:00:00 -07003015 rsign = TLSEXT_signature_rsa;
3016 default_nid = NID_sha1WithRSAEncryption;
3017 break;
3018
Adam Langley95c29f32014-06-20 12:00:00 -07003019 case SSL_PKEY_ECC:
3020 rsign = TLSEXT_signature_ecdsa;
3021 default_nid = NID_ecdsa_with_SHA1;
3022 break;
3023
3024 default:
3025 default_nid = -1;
3026 break;
3027 }
3028 }
3029 /* If peer sent no signature algorithms extension and we
3030 * have set preferred signature algorithms check we support
3031 * sha1.
3032 */
3033 if (default_nid > 0 && c->conf_sigalgs)
3034 {
3035 size_t j;
3036 const unsigned char *p = c->conf_sigalgs;
3037 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
3038 {
3039 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3040 break;
3041 }
3042 if (j == c->conf_sigalgslen)
3043 {
3044 if (check_flags)
3045 goto skip_sigs;
3046 else
3047 goto end;
3048 }
3049 }
3050 /* Check signature algorithm of each cert in chain */
3051 if (!tls1_check_sig_alg(c, x, default_nid))
3052 {
3053 if (!check_flags) goto end;
3054 }
3055 else
3056 rv |= CERT_PKEY_EE_SIGNATURE;
3057 rv |= CERT_PKEY_CA_SIGNATURE;
3058 for (i = 0; i < sk_X509_num(chain); i++)
3059 {
3060 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3061 default_nid))
3062 {
3063 if (check_flags)
3064 {
3065 rv &= ~CERT_PKEY_CA_SIGNATURE;
3066 break;
3067 }
3068 else
3069 goto end;
3070 }
3071 }
3072 }
3073 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3074 else if(check_flags)
3075 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3076 skip_sigs:
3077 /* Check cert parameters are consistent */
3078 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3079 rv |= CERT_PKEY_EE_PARAM;
3080 else if (!check_flags)
3081 goto end;
3082 if (!s->server)
3083 rv |= CERT_PKEY_CA_PARAM;
3084 /* In strict mode check rest of chain too */
3085 else if (strict_mode)
3086 {
3087 rv |= CERT_PKEY_CA_PARAM;
3088 for (i = 0; i < sk_X509_num(chain); i++)
3089 {
3090 X509 *ca = sk_X509_value(chain, i);
3091 if (!tls1_check_cert_param(s, ca, 0))
3092 {
3093 if (check_flags)
3094 {
3095 rv &= ~CERT_PKEY_CA_PARAM;
3096 break;
3097 }
3098 else
3099 goto end;
3100 }
3101 }
3102 }
3103 if (!s->server && strict_mode)
3104 {
3105 STACK_OF(X509_NAME) *ca_dn;
David Benjamin676d1e72014-07-08 14:34:10 -04003106 uint8_t check_type = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003107 switch (pk->type)
3108 {
3109 case EVP_PKEY_RSA:
3110 check_type = TLS_CT_RSA_SIGN;
3111 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003112 case EVP_PKEY_EC:
3113 check_type = TLS_CT_ECDSA_SIGN;
3114 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003115 }
3116 if (check_type)
3117 {
David Benjamin676d1e72014-07-08 14:34:10 -04003118 if (s->s3->tmp.certificate_types &&
3119 memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07003120 {
Adam Langley95c29f32014-06-20 12:00:00 -07003121 rv |= CERT_PKEY_CERT_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -07003122 }
3123 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3124 goto end;
3125 }
3126 else
3127 rv |= CERT_PKEY_CERT_TYPE;
3128
3129
3130 ca_dn = s->s3->tmp.ca_names;
3131
3132 if (!sk_X509_NAME_num(ca_dn))
3133 rv |= CERT_PKEY_ISSUER_NAME;
3134
3135 if (!(rv & CERT_PKEY_ISSUER_NAME))
3136 {
3137 if (ssl_check_ca_name(ca_dn, x))
3138 rv |= CERT_PKEY_ISSUER_NAME;
3139 }
3140 if (!(rv & CERT_PKEY_ISSUER_NAME))
3141 {
3142 for (i = 0; i < sk_X509_num(chain); i++)
3143 {
3144 X509 *xtmp = sk_X509_value(chain, i);
3145 if (ssl_check_ca_name(ca_dn, xtmp))
3146 {
3147 rv |= CERT_PKEY_ISSUER_NAME;
3148 break;
3149 }
3150 }
3151 }
3152 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3153 goto end;
3154 }
3155 else
3156 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3157
3158 if (!check_flags || (rv & check_flags) == check_flags)
3159 rv |= CERT_PKEY_VALID;
3160
3161 end:
3162
3163 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3164 {
3165 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3166 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3167 else if (cpk->digest)
3168 rv |= CERT_PKEY_SIGN;
3169 }
3170 else
3171 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3172
3173 /* When checking a CERT_PKEY structure all flags are irrelevant
3174 * if the chain is invalid.
3175 */
3176 if (!check_flags)
3177 {
3178 if (rv & CERT_PKEY_VALID)
3179 cpk->valid_flags = rv;
3180 else
3181 {
3182 /* Preserve explicit sign flag, clear rest */
3183 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
3184 return 0;
3185 }
3186 }
3187 return rv;
3188 }
3189
3190/* Set validity of certificates in an SSL structure */
3191void tls1_set_cert_validity(SSL *s)
3192 {
3193 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3194 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
Adam Langley95c29f32014-06-20 12:00:00 -07003195 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3196 }
3197/* User level utiity function to check a chain is suitable */
3198int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3199 {
3200 return tls1_check_chain(s, x, pk, chain, -1);
3201 }
3202