blob: f41f0cf301442ba22ea907f67a3cd58501eab852 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400110#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
112
David Benjamin03973092014-06-24 23:27:17 -0400113#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/mem.h>
117#include <openssl/obj.h>
118#include <openssl/rand.h>
119
120#include "ssl_locl.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400124static int ssl_check_clienthello_tlsext(SSL *s);
125static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
127SSL3_ENC_METHOD TLSv1_enc_data={
128 tls1_enc,
129 tls1_mac,
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
141 SSL3_HM_HEADER_LENGTH,
142 ssl3_set_handshake_header,
143 ssl3_handshake_write
144 };
145
146SSL3_ENC_METHOD TLSv1_1_enc_data={
147 tls1_enc,
148 tls1_mac,
149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
153 TLS1_FINISH_MAC_LENGTH,
154 tls1_cert_verify_mac,
155 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
156 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
157 tls1_alert_code,
158 tls1_export_keying_material,
159 SSL_ENC_FLAG_EXPLICIT_IV,
160 SSL3_HM_HEADER_LENGTH,
161 ssl3_set_handshake_header,
162 ssl3_handshake_write
163 };
164
165SSL3_ENC_METHOD TLSv1_2_enc_data={
166 tls1_enc,
167 tls1_mac,
168 tls1_setup_key_block,
169 tls1_generate_master_secret,
170 tls1_change_cipher_state,
171 tls1_final_finish_mac,
172 TLS1_FINISH_MAC_LENGTH,
173 tls1_cert_verify_mac,
174 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
175 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
176 tls1_alert_code,
177 tls1_export_keying_material,
178 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
179 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
180 SSL3_HM_HEADER_LENGTH,
181 ssl3_set_handshake_header,
182 ssl3_handshake_write
183 };
184
David Benjamin35a7a442014-07-05 00:23:20 -0400185static int compare_uint16_t(const void *p1, const void *p2)
186 {
187 uint16_t u1 = *((const uint16_t*)p1);
188 uint16_t u2 = *((const uint16_t*)p2);
189 if (u1 < u2)
190 {
191 return -1;
192 }
193 else if (u1 > u2)
194 {
195 return 1;
196 }
197 else
198 {
199 return 0;
200 }
201 }
202
203/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
204 * than one extension of the same type in a ClientHello or ServerHello. This
205 * function does an initial scan over the extensions block to filter those
206 * out. */
207static int tls1_check_duplicate_extensions(const CBS *cbs)
208 {
209 CBS extensions = *cbs;
210 size_t num_extensions = 0, i = 0;
211 uint16_t *extension_types = NULL;
212 int ret = 0;
213
214 /* First pass: count the extensions. */
215 while (CBS_len(&extensions) > 0)
216 {
217 uint16_t type;
218 CBS extension;
219
220 if (!CBS_get_u16(&extensions, &type) ||
221 !CBS_get_u16_length_prefixed(&extensions, &extension))
222 {
223 goto done;
224 }
225
226 num_extensions++;
227 }
228
David Benjamin9a373592014-07-25 04:27:53 -0400229 if (num_extensions == 0)
230 {
231 return 1;
232 }
233
David Benjamin35a7a442014-07-05 00:23:20 -0400234 extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
235 if (extension_types == NULL)
236 {
237 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
238 goto done;
239 }
240
241 /* Second pass: gather the extension types. */
242 extensions = *cbs;
243 for (i = 0; i < num_extensions; i++)
244 {
245 CBS extension;
246
247 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
248 !CBS_get_u16_length_prefixed(&extensions, &extension))
249 {
250 /* This should not happen. */
251 goto done;
252 }
253 }
254 assert(CBS_len(&extensions) == 0);
255
256 /* Sort the extensions and make sure there are no duplicates. */
257 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
258 for (i = 1; i < num_extensions; i++)
259 {
260 if (extension_types[i-1] == extension_types[i])
261 {
262 goto done;
263 }
264 }
265
266 ret = 1;
267done:
268 if (extension_types)
269 OPENSSL_free(extension_types);
270 return ret;
271 }
272
Adam Langleydc9b1412014-06-20 12:00:00 -0700273char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
274 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400275 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
276
277 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700278
279 /* Skip client version. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400280 if (!CBS_skip(&client_hello, 2))
Adam Langleydc9b1412014-06-20 12:00:00 -0700281 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
283 /* Skip client nonce. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400284 if (!CBS_skip(&client_hello, 32))
Adam Langleydc9b1412014-06-20 12:00:00 -0700285 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700286
David Benjamin8f2c20e2014-07-09 09:30:38 -0400287 /* Extract session_id. */
288 if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
Adam Langleydc9b1412014-06-20 12:00:00 -0700289 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 ctx->session_id = CBS_data(&session_id);
291 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
293 /* Skip past DTLS cookie */
David Benjamin09bd58d2014-08-12 21:22:28 -0400294 if (SSL_IS_DTLS(ctx->ssl))
Adam Langleydc9b1412014-06-20 12:00:00 -0700295 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400296 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700297
David Benjamin8f2c20e2014-07-09 09:30:38 -0400298 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
Adam Langleydc9b1412014-06-20 12:00:00 -0700299 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700300 }
301
David Benjamin8f2c20e2014-07-09 09:30:38 -0400302 /* Extract cipher_suites. */
303 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
304 CBS_len(&cipher_suites) < 2 ||
305 (CBS_len(&cipher_suites) & 1) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700306 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400307 ctx->cipher_suites = CBS_data(&cipher_suites);
308 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700309
David Benjamin8f2c20e2014-07-09 09:30:38 -0400310 /* Extract compression_methods. */
311 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
312 CBS_len(&compression_methods) < 1)
Adam Langleydc9b1412014-06-20 12:00:00 -0700313 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400314 ctx->compression_methods = CBS_data(&compression_methods);
315 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700316
317 /* If the ClientHello ends here then it's valid, but doesn't have any
318 * extensions. (E.g. SSLv3.) */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400319 if (CBS_len(&client_hello) == 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700320 {
321 ctx->extensions = NULL;
322 ctx->extensions_len = 0;
323 return 1;
324 }
325
David Benjamin8f2c20e2014-07-09 09:30:38 -0400326 /* Extract extensions and check it is valid. */
327 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
328 !tls1_check_duplicate_extensions(&extensions) ||
329 CBS_len(&client_hello) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700330 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400331 ctx->extensions = CBS_data(&extensions);
332 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700333
Adam Langleydc9b1412014-06-20 12:00:00 -0700334 return 1;
Adam Langleydc9b1412014-06-20 12:00:00 -0700335 }
336
337char
338SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
339 uint16_t extension_type,
340 const unsigned char **out_data,
341 size_t *out_len)
342 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400343 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700344
David Benjamin8f2c20e2014-07-09 09:30:38 -0400345 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
346
347 while (CBS_len(&extensions) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700348 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400349 uint16_t type;
350 CBS extension;
Adam Langleydc9b1412014-06-20 12:00:00 -0700351
David Benjamin8f2c20e2014-07-09 09:30:38 -0400352 /* Decode the next extension. */
353 if (!CBS_get_u16(&extensions, &type) ||
354 !CBS_get_u16_length_prefixed(&extensions, &extension))
Adam Langleydc9b1412014-06-20 12:00:00 -0700355 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700356
David Benjamin8f2c20e2014-07-09 09:30:38 -0400357 if (type == extension_type)
Adam Langleydc9b1412014-06-20 12:00:00 -0700358 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400359 *out_data = CBS_data(&extension);
360 *out_len = CBS_len(&extension);
Adam Langleydc9b1412014-06-20 12:00:00 -0700361 return 1;
362 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700363 }
364
365 return 0;
366 }
367
Adam Langley95c29f32014-06-20 12:00:00 -0700368
David Benjamincff64722014-08-19 19:54:46 -0400369static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700370 {
371 NID_sect163k1, /* sect163k1 (1) */
372 NID_sect163r1, /* sect163r1 (2) */
373 NID_sect163r2, /* sect163r2 (3) */
374 NID_sect193r1, /* sect193r1 (4) */
375 NID_sect193r2, /* sect193r2 (5) */
376 NID_sect233k1, /* sect233k1 (6) */
377 NID_sect233r1, /* sect233r1 (7) */
378 NID_sect239k1, /* sect239k1 (8) */
379 NID_sect283k1, /* sect283k1 (9) */
380 NID_sect283r1, /* sect283r1 (10) */
381 NID_sect409k1, /* sect409k1 (11) */
382 NID_sect409r1, /* sect409r1 (12) */
383 NID_sect571k1, /* sect571k1 (13) */
384 NID_sect571r1, /* sect571r1 (14) */
385 NID_secp160k1, /* secp160k1 (15) */
386 NID_secp160r1, /* secp160r1 (16) */
387 NID_secp160r2, /* secp160r2 (17) */
388 NID_secp192k1, /* secp192k1 (18) */
389 NID_X9_62_prime192v1, /* secp192r1 (19) */
390 NID_secp224k1, /* secp224k1 (20) */
391 NID_secp224r1, /* secp224r1 (21) */
392 NID_secp256k1, /* secp256k1 (22) */
393 NID_X9_62_prime256v1, /* secp256r1 (23) */
394 NID_secp384r1, /* secp384r1 (24) */
395 NID_secp521r1, /* secp521r1 (25) */
396 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
397 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
398 NID_brainpoolP512r1 /* brainpool512r1 (28) */
399 };
400
David Benjamin072334d2014-07-13 16:24:27 -0400401static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700402 {
403 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700404 };
405
David Benjamin072334d2014-07-13 16:24:27 -0400406static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700407 {
David Benjamin072334d2014-07-13 16:24:27 -0400408 23, /* secp256r1 (23) */
409 24, /* secp384r1 (24) */
410 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700411 };
412
David Benjamin072334d2014-07-13 16:24:27 -0400413int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700414 {
415 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
David Benjamin072334d2014-07-13 16:24:27 -0400416 if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
417 return OBJ_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700418 return nid_list[curve_id-1];
419 }
420
David Benjamin072334d2014-07-13 16:24:27 -0400421uint16_t tls1_ec_nid2curve_id(int nid)
Adam Langley95c29f32014-06-20 12:00:00 -0700422 {
David Benjamin072334d2014-07-13 16:24:27 -0400423 size_t i;
424 for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700425 {
David Benjamin072334d2014-07-13 16:24:27 -0400426 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
427 if (nid == nid_list[i])
428 return i + 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700429 }
David Benjamin072334d2014-07-13 16:24:27 -0400430 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
431 * will never be allocated. */
432 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700433 }
David Benjamin072334d2014-07-13 16:24:27 -0400434
David Benjamin42e9a772014-09-02 23:18:44 -0400435/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len|
436 * to the list of allowed curve IDs. If |get_peer_curves| is non-zero,
437 * return the peer's curve list. Otherwise, return the preferred
438 * list. */
439static void tls1_get_curvelist(SSL *s, int get_peer_curves,
David Benjamin072334d2014-07-13 16:24:27 -0400440 const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700441 {
David Benjamin42e9a772014-09-02 23:18:44 -0400442 if (get_peer_curves)
Adam Langley95c29f32014-06-20 12:00:00 -0700443 {
David Benjamin072334d2014-07-13 16:24:27 -0400444 *out_curve_ids = s->session->tlsext_ellipticcurvelist;
445 *out_curve_ids_len = s->session->tlsext_ellipticcurvelist_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700446 return;
447 }
Adam Langley95c29f32014-06-20 12:00:00 -0700448
David Benjamin335d10d2014-08-06 19:56:33 -0400449 *out_curve_ids = s->tlsext_ellipticcurvelist;
450 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
David Benjamin072334d2014-07-13 16:24:27 -0400451 if (!*out_curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700452 {
David Benjamin072334d2014-07-13 16:24:27 -0400453 *out_curve_ids = eccurves_default;
David Benjamin0eb5a2d2014-07-25 02:40:43 -0400454 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
Adam Langley95c29f32014-06-20 12:00:00 -0700455 }
456 }
David Benjamined439582014-07-14 19:13:02 -0400457
David Benjamined439582014-07-14 19:13:02 -0400458int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700459 {
David Benjamined439582014-07-14 19:13:02 -0400460 uint8_t curve_type;
461 uint16_t curve_id;
David Benjamin072334d2014-07-13 16:24:27 -0400462 const uint16_t *curves;
463 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400464
465 /* Only support named curves. */
466 if (!CBS_get_u8(cbs, &curve_type) ||
467 curve_type != NAMED_CURVE_TYPE ||
468 !CBS_get_u16(cbs, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700469 return 0;
David Benjamined439582014-07-14 19:13:02 -0400470
David Benjamin072334d2014-07-13 16:24:27 -0400471 tls1_get_curvelist(s, 0, &curves, &curves_len);
472 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700473 {
David Benjamin072334d2014-07-13 16:24:27 -0400474 if (curve_id == curves[i])
David Benjamined439582014-07-14 19:13:02 -0400475 {
476 *out_curve_id = curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700477 return 1;
David Benjamined439582014-07-14 19:13:02 -0400478 }
Adam Langley95c29f32014-06-20 12:00:00 -0700479 }
480 return 0;
481 }
482
David Benjamin072334d2014-07-13 16:24:27 -0400483int tls1_get_shared_curve(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700484 {
David Benjamin072334d2014-07-13 16:24:27 -0400485 const uint16_t *pref, *supp;
Adam Langley95c29f32014-06-20 12:00:00 -0700486 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400487
Adam Langley95c29f32014-06-20 12:00:00 -0700488 /* Can't do anything on client side */
489 if (s->server == 0)
David Benjamin072334d2014-07-13 16:24:27 -0400490 return NID_undef;
491
David Benjamin335d10d2014-08-06 19:56:33 -0400492 /* Return first preference shared curve */
Adam Langley95c29f32014-06-20 12:00:00 -0700493 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
494 &supp, &supplen);
495 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
496 &pref, &preflen);
David Benjamin072334d2014-07-13 16:24:27 -0400497 for (i = 0; i < preflen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700498 {
David Benjamin072334d2014-07-13 16:24:27 -0400499 for (j = 0; j < supplen; j++)
Adam Langley95c29f32014-06-20 12:00:00 -0700500 {
David Benjamin072334d2014-07-13 16:24:27 -0400501 if (pref[i] == supp[j])
502 return tls1_ec_curve_id2nid(pref[i]);
Adam Langley95c29f32014-06-20 12:00:00 -0700503 }
504 }
David Benjamin072334d2014-07-13 16:24:27 -0400505 return NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700506 }
507
David Benjamin072334d2014-07-13 16:24:27 -0400508/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
509 *
510 * (a) 0 is not a valid curve ID.
511 *
512 * (b) The largest curve ID is 31.
513 *
514 * Those implementations must be revised before adding support for curve IDs
515 * that break these assumptions. */
516OPENSSL_COMPILE_ASSERT(
517 (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
518
519int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
520 const int *curves, size_t ncurves)
Adam Langley95c29f32014-06-20 12:00:00 -0700521 {
David Benjamin072334d2014-07-13 16:24:27 -0400522 uint16_t *curve_ids;
Adam Langley95c29f32014-06-20 12:00:00 -0700523 size_t i;
524 /* Bitmap of curves included to detect duplicates: only works
525 * while curve ids < 32
526 */
David Benjamin072334d2014-07-13 16:24:27 -0400527 uint32_t dup_list = 0;
528 curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
529 if (!curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700530 return 0;
David Benjamin072334d2014-07-13 16:24:27 -0400531 for (i = 0; i < ncurves; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700532 {
David Benjamin072334d2014-07-13 16:24:27 -0400533 uint32_t idmask;
534 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700535 id = tls1_ec_nid2curve_id(curves[i]);
David Benjamin072334d2014-07-13 16:24:27 -0400536 idmask = ((uint32_t)1) << id;
Adam Langley95c29f32014-06-20 12:00:00 -0700537 if (!id || (dup_list & idmask))
538 {
David Benjamin072334d2014-07-13 16:24:27 -0400539 OPENSSL_free(curve_ids);
Adam Langley95c29f32014-06-20 12:00:00 -0700540 return 0;
541 }
542 dup_list |= idmask;
David Benjamin072334d2014-07-13 16:24:27 -0400543 curve_ids[i] = id;
Adam Langley95c29f32014-06-20 12:00:00 -0700544 }
David Benjamin072334d2014-07-13 16:24:27 -0400545 if (*out_curve_ids)
546 OPENSSL_free(*out_curve_ids);
547 *out_curve_ids = curve_ids;
548 *out_curve_ids_len = ncurves;
Adam Langley95c29f32014-06-20 12:00:00 -0700549 return 1;
550 }
551
David Benjamin072334d2014-07-13 16:24:27 -0400552/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
553 * TLS curve ID and point format, respectively, for |ec|. It returns one on
554 * success and zero on failure. */
555static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, uint8_t *out_comp_id, EC_KEY *ec)
Adam Langley95c29f32014-06-20 12:00:00 -0700556 {
Adam Langley95c29f32014-06-20 12:00:00 -0700557 int nid;
David Benjamin072334d2014-07-13 16:24:27 -0400558 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700559 const EC_GROUP *grp;
560 if (!ec)
561 return 0;
562
Adam Langley95c29f32014-06-20 12:00:00 -0700563 grp = EC_KEY_get0_group(ec);
564 if (!grp)
565 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700566
567 /* Determine curve ID */
David Benjamin072334d2014-07-13 16:24:27 -0400568 nid = EC_GROUP_get_curve_name(grp);
569 id = tls1_ec_nid2curve_id(nid);
570 if (!id)
571 return 0;
572
573 /* Set the named curve ID. Arbitrary explicit curves are not
574 * supported. */
575 *out_curve_id = id;
576
577 if (out_comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700578 {
579 if (EC_KEY_get0_public_key(ec) == NULL)
580 return 0;
581 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
David Benjamin072334d2014-07-13 16:24:27 -0400582 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
Adam Langley95c29f32014-06-20 12:00:00 -0700583 else
David Benjamin072334d2014-07-13 16:24:27 -0400584 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
Adam Langley95c29f32014-06-20 12:00:00 -0700585 }
586 return 1;
587 }
David Benjamin072334d2014-07-13 16:24:27 -0400588
David Benjamin42e9a772014-09-02 23:18:44 -0400589/* tls1_check_point_format returns one if |comp_id| is consistent with the
590 * peer's point format preferences. */
591static int tls1_check_point_format(SSL *s, uint8_t comp_id)
592 {
593 uint8_t *p = s->session->tlsext_ecpointformatlist;
594 size_t plen = s->session->tlsext_ecpointformatlist_length;
595 size_t i;
596
597 /* If point formats extension present check it, otherwise everything
598 * is supported (see RFC4492). */
599 if (p == NULL)
600 return 1;
601
602 for (i = 0; i < plen; i++)
603 {
604 if (comp_id == p[i])
605 return 1;
606 }
607 return 0;
608 }
609
610/* tls1_check_curve_id returns one if |curve_id| is consistent with both our and
611 * the peer's curve preferences. Note: if called as the client, only our
612 * preferences are checked; the peer (the server) does not send preferences. */
613static int tls1_check_curve_id(SSL *s, uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700614 {
David Benjamin072334d2014-07-13 16:24:27 -0400615 const uint16_t *curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400616 size_t curves_len, i, j;
617
618 /* Check against our list, then the peer's list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700619 for (j = 0; j <= 1; j++)
620 {
David Benjamin072334d2014-07-13 16:24:27 -0400621 tls1_get_curvelist(s, j, &curves, &curves_len);
622 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700623 {
David Benjamin42e9a772014-09-02 23:18:44 -0400624 if (curves[i] == curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700625 break;
626 }
David Benjamin072334d2014-07-13 16:24:27 -0400627 if (i == curves_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700628 return 0;
David Benjamin42e9a772014-09-02 23:18:44 -0400629 /* Servers do not present a preference list so, if we are a
630 * client, only check our list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700631 if (!s->server)
632 return 1;
633 }
634 return 1;
635 }
636
637static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
638 size_t *pformatslen)
639 {
640 /* If we have a custom point format list use it otherwise
641 * use default */
642 if (s->tlsext_ecpointformatlist)
643 {
644 *pformats = s->tlsext_ecpointformatlist;
645 *pformatslen = s->tlsext_ecpointformatlist_length;
646 }
647 else
648 {
649 *pformats = ecformats_default;
David Benjamin335d10d2014-08-06 19:56:33 -0400650 *pformatslen = sizeof(ecformats_default);
Adam Langley95c29f32014-06-20 12:00:00 -0700651 }
652 }
653
654/* Check cert parameters compatible with extensions: currently just checks
655 * EC certificates have compatible curves and compression.
656 */
657static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
658 {
David Benjamin072334d2014-07-13 16:24:27 -0400659 uint8_t comp_id;
660 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700661 EVP_PKEY *pkey;
662 int rv;
663 pkey = X509_get_pubkey(x);
664 if (!pkey)
665 return 0;
666 /* If not EC nothing to do */
667 if (pkey->type != EVP_PKEY_EC)
668 {
669 EVP_PKEY_free(pkey);
670 return 1;
671 }
David Benjamin072334d2014-07-13 16:24:27 -0400672 rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec);
Adam Langley95c29f32014-06-20 12:00:00 -0700673 EVP_PKEY_free(pkey);
674 if (!rv)
675 return 0;
676 /* Can't check curve_id for client certs as we don't have a
David Benjamin42e9a772014-09-02 23:18:44 -0400677 * supported curves extension. */
678 if (s->server && !tls1_check_curve_id(s, curve_id))
679 return 0;
680 return tls1_check_point_format(s, comp_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700681 }
David Benjamin42e9a772014-09-02 23:18:44 -0400682
Adam Langley95c29f32014-06-20 12:00:00 -0700683/* Check EC temporary key is compatible with client extensions */
David Benjamin42e9a772014-09-02 23:18:44 -0400684int tls1_check_ec_tmp_key(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700685 {
David Benjamin072334d2014-07-13 16:24:27 -0400686 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700687 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700688 if (s->cert->ecdh_tmp_auto)
689 {
690 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400691 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700692 }
693 if (!ec)
694 {
695 if (s->cert->ecdh_tmp_cb)
696 return 1;
697 else
698 return 0;
699 }
David Benjamin42e9a772014-09-02 23:18:44 -0400700 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
701 tls1_check_curve_id(s, curve_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700702 }
703
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langley95c29f32014-06-20 12:00:00 -0700705
706/* List of supported signature algorithms and hashes. Should make this
707 * customisable at some point, for now include everything we support.
708 */
709
Adam Langley95c29f32014-06-20 12:00:00 -0700710#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langley95c29f32014-06-20 12:00:00 -0700712#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700713
714#define tlsext_sigalg(md) \
715 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700716 tlsext_sigalg_ecdsa(md)
717
David Benjamincff64722014-08-19 19:54:46 -0400718static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700719 tlsext_sigalg(TLSEXT_hash_sha512)
720 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700721 tlsext_sigalg(TLSEXT_hash_sha256)
722 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700723 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700724};
Adam Langley95c29f32014-06-20 12:00:00 -0700725size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
726 {
Adam Langley95c29f32014-06-20 12:00:00 -0700727 /* If server use client authentication sigalgs if not NULL */
728 if (s->server && s->cert->client_sigalgs)
729 {
730 *psigs = s->cert->client_sigalgs;
731 return s->cert->client_sigalgslen;
732 }
733 else if (s->cert->conf_sigalgs)
734 {
735 *psigs = s->cert->conf_sigalgs;
736 return s->cert->conf_sigalgslen;
737 }
738 else
739 {
740 *psigs = tls12_sigalgs;
741 return sizeof(tls12_sigalgs);
742 }
743 }
David Benjamin05da6e12014-07-12 20:42:55 -0400744
745/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
746 * |cbs|. It checks it is consistent with |s|'s sent supported
747 * signature algorithms and, if so, writes the relevant digest into
748 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
749 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700750 */
David Benjamin05da6e12014-07-12 20:42:55 -0400751int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
752 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700753 {
754 const unsigned char *sent_sigs;
755 size_t sent_sigslen, i;
756 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400757 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700758 /* Should never happen */
759 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400760 {
761 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
762 *out_alert = SSL_AD_INTERNAL_ERROR;
763 return 0;
764 }
765 if (!CBS_get_u8(cbs, &hash) ||
766 !CBS_get_u8(cbs, &signature))
767 {
768 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
769 *out_alert = SSL_AD_DECODE_ERROR;
770 return 0;
771 }
Adam Langley95c29f32014-06-20 12:00:00 -0700772 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400773 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700774 {
775 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400776 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700777 return 0;
778 }
Adam Langley95c29f32014-06-20 12:00:00 -0700779 if (pkey->type == EVP_PKEY_EC)
780 {
David Benjamin072334d2014-07-13 16:24:27 -0400781 uint16_t curve_id;
782 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700783 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400784 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400785 {
786 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700787 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400788 }
David Benjamin42e9a772014-09-02 23:18:44 -0400789 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700790 {
David Benjamin42e9a772014-09-02 23:18:44 -0400791 if (!tls1_check_curve_id(s, curve_id) ||
792 !tls1_check_point_format(s, comp_id))
793 {
794 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
795 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
796 return 0;
797 }
Adam Langley95c29f32014-06-20 12:00:00 -0700798 }
David Benjamin05da6e12014-07-12 20:42:55 -0400799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
801 /* Check signature matches a type we sent */
802 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
803 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
804 {
David Benjamin05da6e12014-07-12 20:42:55 -0400805 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700806 break;
807 }
808 /* Allow fallback to SHA1 if not strict mode */
David Benjamin05da6e12014-07-12 20:42:55 -0400809 if (i == sent_sigslen && (hash != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
Adam Langley95c29f32014-06-20 12:00:00 -0700810 {
811 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400812 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700813 return 0;
814 }
David Benjamin05da6e12014-07-12 20:42:55 -0400815 *out_md = tls12_get_hash(hash);
816 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700817 {
818 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400819 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700820 return 0;
821 }
822 /* Store the digest used so applications can retrieve it if they
823 * wish.
824 */
825 if (s->session && s->session->sess_cert)
David Benjamin05da6e12014-07-12 20:42:55 -0400826 s->session->sess_cert->peer_key->digest = *out_md;
Adam Langley95c29f32014-06-20 12:00:00 -0700827 return 1;
828 }
829/* Get a mask of disabled algorithms: an algorithm is disabled
830 * if it isn't supported or doesn't appear in supported signature
831 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
832 * session and not global settings.
833 *
834 */
835void ssl_set_client_disabled(SSL *s)
836 {
837 CERT *c = s->cert;
838 const unsigned char *sigalgs;
839 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400840 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700841 c->mask_a = 0;
842 c->mask_k = 0;
843 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
844 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
845 c->mask_ssl = SSL_TLSV1_2;
846 else
847 c->mask_ssl = 0;
848 /* Now go through all signature algorithms seeing if we support
849 * any for RSA, DSA, ECDSA. Do this for all versions not just
850 * TLS 1.2.
851 */
852 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
853 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
854 {
855 switch(sigalgs[1])
856 {
Adam Langley95c29f32014-06-20 12:00:00 -0700857 case TLSEXT_signature_rsa:
858 have_rsa = 1;
859 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700860 case TLSEXT_signature_ecdsa:
861 have_ecdsa = 1;
862 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700863 }
864 }
David Benjamin0da0e182014-08-19 16:20:28 -0400865 /* Disable auth if we don't include any appropriate signature
866 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700867 */
868 if (!have_rsa)
869 {
870 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872 if (!have_ecdsa)
873 {
874 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700875 }
Adam Langley95c29f32014-06-20 12:00:00 -0700876 /* with PSK there must be client callback set */
877 if (!s->psk_client_callback)
878 {
879 c->mask_a |= SSL_aPSK;
880 c->mask_k |= SSL_kPSK;
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882 c->valid = 1;
883 }
884
Adam Langleyb0c235e2014-06-20 12:00:00 -0700885/* header_len is the length of the ClientHello header written so far, used to
886 * compute padding. It does not include the record header. Pass 0 if no padding
887 * is to be done. */
888unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700889 {
890 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700891 unsigned char *ret = buf;
892 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700893 /* See if we support any ECC ciphersuites */
894 int using_ecc = 0;
895 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
896 {
897 int i;
898 unsigned long alg_k, alg_a;
899 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
900
901 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
902 {
David Benjamin6f260012014-08-15 13:49:12 -0400903 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700904
905 alg_k = c->algorithm_mkey;
906 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400907 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700908 {
909 using_ecc = 1;
910 break;
911 }
912 }
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
915 /* don't add extensions for SSLv3 unless doing secure renegotiation */
916 if (s->client_version == SSL3_VERSION
917 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700918 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700919
920 ret+=2;
921
922 if (ret>=limit) return NULL; /* this really never occurs, but ... */
923
924 if (s->tlsext_hostname != NULL)
925 {
926 /* Add TLS extension servername to the Client Hello message */
927 unsigned long size_str;
928 long lenmax;
929
930 /* check for enough space.
931 4 for the servername type and entension length
932 2 for servernamelist length
933 1 for the hostname type
934 2 for hostname length
935 + hostname length
936 */
937
938 if ((lenmax = limit - ret - 9) < 0
939 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
940 return NULL;
941
942 /* extension type and length */
943 s2n(TLSEXT_TYPE_server_name,ret);
944 s2n(size_str+5,ret);
945
946 /* length of servername list */
947 s2n(size_str+3,ret);
948
949 /* hostname type, length and hostname */
950 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
951 s2n(size_str,ret);
952 memcpy(ret, s->tlsext_hostname, size_str);
953 ret+=size_str;
954 }
955
956 /* Add RI if renegotiating */
957 if (s->renegotiate)
958 {
959 int el;
960
961 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
962 {
963 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
964 return NULL;
965 }
966
Adam Langleyb0c235e2014-06-20 12:00:00 -0700967 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700968
969 s2n(TLSEXT_TYPE_renegotiate,ret);
970 s2n(el,ret);
971
972 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
973 {
974 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
975 return NULL;
976 }
977
978 ret += el;
979 }
980
Adam Langley95c29f32014-06-20 12:00:00 -0700981 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
982 {
983 int ticklen;
984 if (!s->new_session && s->session && s->session->tlsext_tick)
985 ticklen = s->session->tlsext_ticklen;
986 else if (s->session && s->tlsext_session_ticket &&
987 s->tlsext_session_ticket->data)
988 {
David Benjamin072c9532014-07-26 11:44:25 -0400989 s->session->tlsext_tick = BUF_memdup(
990 s->tlsext_session_ticket->data,
991 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700992 if (!s->session->tlsext_tick)
993 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -0400994 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700995 s->session->tlsext_ticklen = ticklen;
996 }
997 else
998 ticklen = 0;
999 if (ticklen == 0 && s->tlsext_session_ticket &&
1000 s->tlsext_session_ticket->data == NULL)
1001 goto skip_ext;
1002 /* Check for enough room 2 for extension type, 2 for len
1003 * rest for ticket
1004 */
1005 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1006 s2n(TLSEXT_TYPE_session_ticket,ret);
1007 s2n(ticklen,ret);
1008 if (ticklen)
1009 {
1010 memcpy(ret, s->session->tlsext_tick, ticklen);
1011 ret += ticklen;
1012 }
1013 }
1014 skip_ext:
1015
1016 if (SSL_USE_SIGALGS(s))
1017 {
1018 size_t salglen;
1019 const unsigned char *salg;
1020 salglen = tls12_get_psigalgs(s, &salg);
1021 if ((size_t)(limit - ret) < salglen + 6)
1022 return NULL;
1023 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1024 s2n(salglen + 2, ret);
1025 s2n(salglen, ret);
1026 memcpy(ret, salg, salglen);
1027 ret += salglen;
1028 }
1029
David Benjamin6c7aed02014-08-27 16:42:38 -04001030 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001031 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001032 /* The status_request extension is excessively extensible at
1033 * every layer. On the client, only support requesting OCSP
1034 * responses with an empty responder_id_list and no
1035 * extensions. */
1036 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langley95c29f32014-06-20 12:00:00 -07001038 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001039 s2n(1 + 2 + 2, ret);
1040 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001041 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001042 /* responder_id_list - empty */
1043 s2n(0, ret);
1044 /* request_extensions - empty */
1045 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001046 }
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langley95c29f32014-06-20 12:00:00 -07001048 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1049 {
1050 /* The client advertises an emtpy extension to indicate its
1051 * support for Next Protocol Negotiation */
1052 if (limit - ret - 4 < 0)
1053 return NULL;
1054 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1055 s2n(0,ret);
1056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001058 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1059 {
1060 /* The client advertises an empty extension to indicate its support for
1061 * certificate timestamps. */
1062 if (limit - ret - 4 < 0)
1063 return NULL;
1064 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1065 s2n(0,ret);
1066 }
1067
Adam Langley95c29f32014-06-20 12:00:00 -07001068 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1069 {
1070 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1071 return NULL;
1072 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1073 s2n(2 + s->alpn_client_proto_list_len,ret);
1074 s2n(s->alpn_client_proto_list_len,ret);
1075 memcpy(ret, s->alpn_client_proto_list,
1076 s->alpn_client_proto_list_len);
1077 ret += s->alpn_client_proto_list_len;
1078 }
1079
Adam Langley1258b6a2014-06-20 12:00:00 -07001080 if (s->tlsext_channel_id_enabled)
1081 {
1082 /* The client advertises an emtpy extension to indicate its
1083 * support for Channel ID. */
1084 if (limit - ret - 4 < 0)
1085 return NULL;
1086 if (s->ctx->tlsext_channel_id_enabled_new)
1087 s2n(TLSEXT_TYPE_channel_id_new,ret);
1088 else
1089 s2n(TLSEXT_TYPE_channel_id,ret);
1090 s2n(0,ret);
1091 }
1092
Adam Langley95c29f32014-06-20 12:00:00 -07001093 if(SSL_get_srtp_profiles(s))
1094 {
1095 int el;
1096
1097 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1098
Adam Langleyb0c235e2014-06-20 12:00:00 -07001099 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001100
1101 s2n(TLSEXT_TYPE_use_srtp,ret);
1102 s2n(el,ret);
1103
David Benjamin120a6742014-08-30 14:54:37 -04001104 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001105 {
1106 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1107 return NULL;
1108 }
1109 ret += el;
1110 }
1111
Adam Langleyc3174b72014-06-20 12:00:00 -07001112 if (using_ecc)
1113 {
1114 /* Add TLS extension ECPointFormats to the ClientHello message */
1115 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001116 const uint8_t *formats;
1117 const uint16_t *curves;
1118 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001119
David Benjamin072334d2014-07-13 16:24:27 -04001120 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001121
1122 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001123 if (formats_len > (size_t)lenmax) return NULL;
1124 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001125 {
1126 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1127 return NULL;
1128 }
1129
1130 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001131 s2n(formats_len + 1,ret);
1132 *(ret++) = (unsigned char)formats_len;
1133 memcpy(ret, formats, formats_len);
1134 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001135
1136 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001137 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001138
1139 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001140 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1141 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001142 {
1143 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1144 return NULL;
1145 }
1146
1147 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001148 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001149
1150 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1151 * elliptic_curve_list, but the examples use two bytes.
1152 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1153 * resolves this to two bytes.
1154 */
David Benjamin072334d2014-07-13 16:24:27 -04001155 s2n(curves_len * 2, ret);
1156 for (i = 0; i < curves_len; i++)
1157 {
1158 s2n(curves[i], ret);
1159 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001160 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001161
Adam Langley95c29f32014-06-20 12:00:00 -07001162#ifdef TLSEXT_TYPE_padding
1163 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001164 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001165 *
1166 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001167 * extensions it MUST always appear last. */
1168 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001169 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001170 header_len += ret - orig;
1171 if (header_len > 0xff && header_len < 0x200)
1172 {
1173 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001174 /* Extensions take at least four bytes to encode. Always
1175 * include least one byte of data if including the
1176 * extension. WebSphere Application Server 7.0 is
1177 * intolerant to the last extension being zero-length. */
1178 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001179 padding_len -= 4;
1180 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001181 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001182 if (limit - ret - 4 - (long)padding_len < 0)
1183 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyb0c235e2014-06-20 12:00:00 -07001185 s2n(TLSEXT_TYPE_padding, ret);
1186 s2n(padding_len, ret);
1187 memset(ret, 0, padding_len);
1188 ret += padding_len;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190 }
1191#endif
1192
Adam Langleyb0c235e2014-06-20 12:00:00 -07001193 if ((extdatalen = ret-orig-2)== 0)
1194 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyb0c235e2014-06-20 12:00:00 -07001196 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001197 return ret;
1198 }
1199
Adam Langleyb0c235e2014-06-20 12:00:00 -07001200unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001201 {
1202 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001203 unsigned char *orig = buf;
1204 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001205 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001206 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1207 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001208 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
Adam Langley95c29f32014-06-20 12:00:00 -07001209 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001210 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1211 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001212 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001213
1214 ret+=2;
1215 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1216
Adam Langleyed8270a2014-09-02 13:52:56 -07001217 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001218 {
1219 if ((long)(limit - ret - 4) < 0) return NULL;
1220
1221 s2n(TLSEXT_TYPE_server_name,ret);
1222 s2n(0,ret);
1223 }
1224
1225 if(s->s3->send_connection_binding)
1226 {
1227 int el;
1228
1229 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1230 {
1231 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1232 return NULL;
1233 }
1234
Adam Langleyb0c235e2014-06-20 12:00:00 -07001235 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001236
1237 s2n(TLSEXT_TYPE_renegotiate,ret);
1238 s2n(el,ret);
1239
1240 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1241 {
1242 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1243 return NULL;
1244 }
1245
1246 ret += el;
1247 }
1248
Adam Langley95c29f32014-06-20 12:00:00 -07001249 if (using_ecc)
1250 {
1251 const unsigned char *plist;
1252 size_t plistlen;
1253 /* Add TLS extension ECPointFormats to the ServerHello message */
1254 long lenmax;
1255
1256 tls1_get_formatlist(s, &plist, &plistlen);
1257
1258 if ((lenmax = limit - ret - 5) < 0) return NULL;
1259 if (plistlen > (size_t)lenmax) return NULL;
1260 if (plistlen > 255)
1261 {
1262 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1263 return NULL;
1264 }
1265
1266 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1267 s2n(plistlen + 1,ret);
1268 *(ret++) = (unsigned char) plistlen;
1269 memcpy(ret, plist, plistlen);
1270 ret+=plistlen;
1271
1272 }
1273 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001274
1275 if (s->tlsext_ticket_expected
1276 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1277 {
1278 if ((long)(limit - ret - 4) < 0) return NULL;
1279 s2n(TLSEXT_TYPE_session_ticket,ret);
1280 s2n(0,ret);
1281 }
1282
David Benjamin6c7aed02014-08-27 16:42:38 -04001283 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001284 {
1285 if ((long)(limit - ret - 4) < 0) return NULL;
1286 s2n(TLSEXT_TYPE_status_request,ret);
1287 s2n(0,ret);
1288 }
1289
Adam Langley95c29f32014-06-20 12:00:00 -07001290 if(s->srtp_profile)
1291 {
1292 int el;
1293
1294 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1295
Adam Langleyb0c235e2014-06-20 12:00:00 -07001296 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001297
1298 s2n(TLSEXT_TYPE_use_srtp,ret);
1299 s2n(el,ret);
1300
David Benjamin120a6742014-08-30 14:54:37 -04001301 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001302 {
1303 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1304 return NULL;
1305 }
1306 ret+=el;
1307 }
1308
Adam Langley95c29f32014-06-20 12:00:00 -07001309 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1310 s->s3->next_proto_neg_seen = 0;
1311 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1312 {
1313 const unsigned char *npa;
1314 unsigned int npalen;
1315 int r;
1316
1317 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1318 if (r == SSL_TLSEXT_ERR_OK)
1319 {
1320 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1321 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1322 s2n(npalen,ret);
1323 memcpy(ret, npa, npalen);
1324 ret += npalen;
1325 s->s3->next_proto_neg_seen = 1;
1326 }
1327 }
Adam Langley95c29f32014-06-20 12:00:00 -07001328
Adam Langley95c29f32014-06-20 12:00:00 -07001329 if (s->s3->alpn_selected)
1330 {
David Benjamin03973092014-06-24 23:27:17 -04001331 const uint8_t *selected = s->s3->alpn_selected;
1332 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001333
1334 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1335 return NULL;
1336 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1337 s2n(3 + len,ret);
1338 s2n(1 + len,ret);
1339 *ret++ = len;
1340 memcpy(ret, selected, len);
1341 ret += len;
1342 }
1343
Adam Langley1258b6a2014-06-20 12:00:00 -07001344 /* If the client advertised support for Channel ID, and we have it
1345 * enabled, then we want to echo it back. */
1346 if (s->s3->tlsext_channel_id_valid)
1347 {
1348 if (limit - ret - 4 < 0)
1349 return NULL;
1350 if (s->s3->tlsext_channel_id_new)
1351 s2n(TLSEXT_TYPE_channel_id_new,ret);
1352 else
1353 s2n(TLSEXT_TYPE_channel_id,ret);
1354 s2n(0,ret);
1355 }
1356
Adam Langleyb0c235e2014-06-20 12:00:00 -07001357 if ((extdatalen = ret-orig-2) == 0)
1358 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langleyb0c235e2014-06-20 12:00:00 -07001360 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001361 return ret;
1362 }
1363
Adam Langley95c29f32014-06-20 12:00:00 -07001364/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1365 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001366 * cbs: the contents of the extension, not including the type and length.
1367 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001368 * return.
1369 *
David Benjamindc72ff72014-06-25 12:36:10 -04001370 * returns: 1 on success. */
1371static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001372 {
Adam Langleyded93582014-07-31 15:23:51 -07001373 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001374 const unsigned char *selected;
1375 unsigned char selected_len;
1376 int r;
1377
1378 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001379 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001380
David Benjamindc72ff72014-06-25 12:36:10 -04001381 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1382 CBS_len(cbs) != 0 ||
1383 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001384 goto parse_error;
1385
David Benjamindc72ff72014-06-25 12:36:10 -04001386 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001387 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001388 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001389 {
David Benjamindc72ff72014-06-25 12:36:10 -04001390 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001391
David Benjamindc72ff72014-06-25 12:36:10 -04001392 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001393 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001394 }
1395
David Benjamindc72ff72014-06-25 12:36:10 -04001396 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1397 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1398 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001399 if (r == SSL_TLSEXT_ERR_OK) {
1400 if (s->s3->alpn_selected)
1401 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001402 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001403 if (!s->s3->alpn_selected)
1404 {
David Benjamindc72ff72014-06-25 12:36:10 -04001405 *out_alert = SSL_AD_INTERNAL_ERROR;
1406 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408 s->s3->alpn_selected_len = selected_len;
1409 }
David Benjamindc72ff72014-06-25 12:36:10 -04001410 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001411
1412parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001413 *out_alert = SSL_AD_DECODE_ERROR;
1414 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001415 }
1416
David Benjamindc72ff72014-06-25 12:36:10 -04001417static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001418 {
Adam Langley95c29f32014-06-20 12:00:00 -07001419 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001420 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001421 size_t i;
1422
Adam Langleyed8270a2014-09-02 13:52:56 -07001423 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001424 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001425 s->s3->tmp.certificate_status_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001426
Adam Langley95c29f32014-06-20 12:00:00 -07001427 if (s->s3->alpn_selected)
1428 {
1429 OPENSSL_free(s->s3->alpn_selected);
1430 s->s3->alpn_selected = NULL;
1431 }
1432
Adam Langley95c29f32014-06-20 12:00:00 -07001433 /* Clear any signature algorithms extension received */
1434 if (s->cert->peer_sigalgs)
1435 {
1436 OPENSSL_free(s->cert->peer_sigalgs);
1437 s->cert->peer_sigalgs = NULL;
1438 }
1439 /* Clear any shared sigtnature algorithms */
1440 if (s->cert->shared_sigalgs)
1441 {
1442 OPENSSL_free(s->cert->shared_sigalgs);
1443 s->cert->shared_sigalgs = NULL;
1444 }
1445 /* Clear certificate digests and validity flags */
1446 for (i = 0; i < SSL_PKEY_NUM; i++)
1447 {
1448 s->cert->pkeys[i].digest = NULL;
1449 s->cert->pkeys[i].valid_flags = 0;
1450 }
1451
David Benjamindc72ff72014-06-25 12:36:10 -04001452 /* There may be no extensions. */
1453 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001454 {
David Benjamindc72ff72014-06-25 12:36:10 -04001455 goto ri_check;
1456 }
Adam Langley95c29f32014-06-20 12:00:00 -07001457
David Benjamin35a7a442014-07-05 00:23:20 -04001458 /* Decode the extensions block and check it is valid. */
1459 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1460 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001461 {
1462 *out_alert = SSL_AD_DECODE_ERROR;
1463 return 0;
1464 }
1465
David Benjamindc72ff72014-06-25 12:36:10 -04001466 while (CBS_len(&extensions) != 0)
1467 {
1468 uint16_t type;
1469 CBS extension;
1470
1471 /* Decode the next extension. */
1472 if (!CBS_get_u16(&extensions, &type) ||
1473 !CBS_get_u16_length_prefixed(&extensions, &extension))
1474 {
1475 *out_alert = SSL_AD_DECODE_ERROR;
1476 return 0;
1477 }
1478
Adam Langley95c29f32014-06-20 12:00:00 -07001479 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001480 {
1481 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1482 CBS_len(&extension), s->tlsext_debug_arg);
1483 }
1484
Adam Langley95c29f32014-06-20 12:00:00 -07001485/* The servername extension is treated as follows:
1486
1487 - Only the hostname type is supported with a maximum length of 255.
1488 - The servername is rejected if too long or if it contains zeros,
1489 in which case an fatal alert is generated.
1490 - The servername field is maintained together with the session cache.
1491 - When a session is resumed, the servername call back invoked in order
1492 to allow the application to position itself to the right context.
1493 - The servername is acknowledged if it is new for a session or when
1494 it is identical to a previously used for the same session.
1495 Applications can control the behaviour. They can at any time
1496 set a 'desirable' servername for a new SSL object. This can be the
1497 case for example with HTTPS when a Host: header field is received and
1498 a renegotiation is requested. In this case, a possible servername
1499 presented in the new client hello is only acknowledged if it matches
1500 the value of the Host: field.
1501 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1502 if they provide for changing an explicit servername context for the session,
1503 i.e. when the session has been established with a servername extension.
1504 - On session reconnect, the servername extension may be absent.
1505
1506*/
1507
1508 if (type == TLSEXT_TYPE_server_name)
1509 {
David Benjamindc72ff72014-06-25 12:36:10 -04001510 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001511 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001512
1513 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1514 CBS_len(&server_name_list) < 1 ||
1515 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001516 {
David Benjamindc72ff72014-06-25 12:36:10 -04001517 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001518 return 0;
1519 }
Adam Langley95c29f32014-06-20 12:00:00 -07001520
David Benjamindc72ff72014-06-25 12:36:10 -04001521 /* Decode each ServerName in the extension. */
1522 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001523 {
David Benjamindc72ff72014-06-25 12:36:10 -04001524 uint8_t name_type;
1525 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001526
David Benjamindc72ff72014-06-25 12:36:10 -04001527 /* Decode the NameType. */
1528 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001529 {
David Benjamindc72ff72014-06-25 12:36:10 -04001530 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001531 return 0;
1532 }
David Benjamindc72ff72014-06-25 12:36:10 -04001533
David Benjamindc72ff72014-06-25 12:36:10 -04001534 /* Only host_name is supported. */
1535 if (name_type != TLSEXT_NAMETYPE_host_name)
1536 continue;
1537
Adam Langleyed8270a2014-09-02 13:52:56 -07001538 if (have_seen_host_name)
1539 {
1540 /* The ServerNameList MUST NOT contain
1541 * more than one name of the same
1542 * name_type. */
1543 *out_alert = SSL_AD_DECODE_ERROR;
1544 return 0;
1545 }
1546
1547 have_seen_host_name = 1;
1548
1549 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1550 CBS_len(&host_name) < 1)
1551 {
1552 *out_alert = SSL_AD_DECODE_ERROR;
1553 return 0;
1554 }
1555
1556 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1557 CBS_contains_zero_byte(&host_name))
1558 {
1559 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1560 return 0;
1561 }
1562
David Benjamindc72ff72014-06-25 12:36:10 -04001563 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001564 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001565 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001566 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001567 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001568 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001569 *out_alert = SSL_AD_DECODE_ERROR;
1570 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001571 }
Adam Langley95c29f32014-06-20 12:00:00 -07001572
David Benjamindc72ff72014-06-25 12:36:10 -04001573 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001574 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001575 {
1576 *out_alert = SSL_AD_INTERNAL_ERROR;
1577 return 0;
1578 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001579
1580 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001581 }
Adam Langley95c29f32014-06-20 12:00:00 -07001582 }
Adam Langley95c29f32014-06-20 12:00:00 -07001583 }
1584
Adam Langley95c29f32014-06-20 12:00:00 -07001585 else if (type == TLSEXT_TYPE_ec_point_formats)
1586 {
David Benjamindc72ff72014-06-25 12:36:10 -04001587 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001588
David Benjamindc72ff72014-06-25 12:36:10 -04001589 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1590 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001591 {
David Benjamindc72ff72014-06-25 12:36:10 -04001592 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001593 return 0;
1594 }
David Benjamindc72ff72014-06-25 12:36:10 -04001595
Adam Langley95c29f32014-06-20 12:00:00 -07001596 if (!s->hit)
1597 {
David Benjamindc72ff72014-06-25 12:36:10 -04001598 if (!CBS_stow(&ec_point_format_list,
1599 &s->session->tlsext_ecpointformatlist,
1600 &s->session->tlsext_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001601 {
David Benjamindc72ff72014-06-25 12:36:10 -04001602 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001603 return 0;
1604 }
Adam Langley95c29f32014-06-20 12:00:00 -07001605 }
Adam Langley95c29f32014-06-20 12:00:00 -07001606 }
1607 else if (type == TLSEXT_TYPE_elliptic_curves)
1608 {
David Benjamindc72ff72014-06-25 12:36:10 -04001609 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001610 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001611
David Benjamindc72ff72014-06-25 12:36:10 -04001612 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001613 CBS_len(&elliptic_curve_list) == 0 ||
1614 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001615 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001616 {
David Benjamindc72ff72014-06-25 12:36:10 -04001617 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001618 return 0;
1619 }
David Benjamindc72ff72014-06-25 12:36:10 -04001620
Adam Langley95c29f32014-06-20 12:00:00 -07001621 if (!s->hit)
1622 {
David Benjamin072334d2014-07-13 16:24:27 -04001623 if (s->session->tlsext_ellipticcurvelist)
1624 {
1625 OPENSSL_free(s->session->tlsext_ellipticcurvelist);
1626 s->session->tlsext_ellipticcurvelist_length = 0;
1627 }
1628 s->session->tlsext_ellipticcurvelist =
1629 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1630 if (s->session->tlsext_ellipticcurvelist == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001631 {
David Benjamindc72ff72014-06-25 12:36:10 -04001632 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001633 return 0;
1634 }
David Benjamin072334d2014-07-13 16:24:27 -04001635 num_curves = CBS_len(&elliptic_curve_list) / 2;
1636 for (i = 0; i < num_curves; i++)
1637 {
1638 if (!CBS_get_u16(&elliptic_curve_list,
1639 &s->session->tlsext_ellipticcurvelist[i]))
1640 {
1641 *out_alert = SSL_AD_INTERNAL_ERROR;
1642 return 0;
1643 }
1644 }
1645 if (CBS_len(&elliptic_curve_list) != 0)
1646 {
1647 *out_alert = SSL_AD_INTERNAL_ERROR;
1648 return 0;
1649 }
1650 s->session->tlsext_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001651 }
Adam Langley95c29f32014-06-20 12:00:00 -07001652 }
Adam Langley95c29f32014-06-20 12:00:00 -07001653 else if (type == TLSEXT_TYPE_session_ticket)
1654 {
1655 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001656 !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 -07001657 {
David Benjamindc72ff72014-06-25 12:36:10 -04001658 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001659 return 0;
1660 }
1661 }
1662 else if (type == TLSEXT_TYPE_renegotiate)
1663 {
David Benjamindc72ff72014-06-25 12:36:10 -04001664 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001665 return 0;
1666 renegotiate_seen = 1;
1667 }
1668 else if (type == TLSEXT_TYPE_signature_algorithms)
1669 {
David Benjamindc72ff72014-06-25 12:36:10 -04001670 CBS supported_signature_algorithms;
1671
David Benjamindc72ff72014-06-25 12:36:10 -04001672 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1673 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001674 {
David Benjamindc72ff72014-06-25 12:36:10 -04001675 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001676 return 0;
1677 }
David Benjamindc72ff72014-06-25 12:36:10 -04001678
1679 /* Ensure the signature algorithms are non-empty. It
1680 * contains a list of SignatureAndHashAlgorithms
1681 * which are two bytes each. */
1682 if (CBS_len(&supported_signature_algorithms) == 0 ||
1683 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001684 {
David Benjamindc72ff72014-06-25 12:36:10 -04001685 *out_alert = SSL_AD_DECODE_ERROR;
1686 return 0;
1687 }
1688
David Benjamincd996942014-07-20 16:23:51 -04001689 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001690 {
1691 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001692 return 0;
1693 }
1694 /* If sigalgs received and no shared algorithms fatal
1695 * error.
1696 */
1697 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1698 {
1699 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001700 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001701 return 0;
1702 }
1703 }
1704
Adam Langley95c29f32014-06-20 12:00:00 -07001705 else if (type == TLSEXT_TYPE_next_proto_neg &&
1706 s->s3->tmp.finish_md_len == 0 &&
1707 s->s3->alpn_selected == NULL)
1708 {
David Benjamindc72ff72014-06-25 12:36:10 -04001709 /* The extension must be empty. */
1710 if (CBS_len(&extension) != 0)
1711 {
1712 *out_alert = SSL_AD_DECODE_ERROR;
1713 return 0;
1714 }
1715
Adam Langley95c29f32014-06-20 12:00:00 -07001716 /* We shouldn't accept this extension on a
1717 * renegotiation.
1718 *
1719 * s->new_session will be set on renegotiation, but we
1720 * probably shouldn't rely that it couldn't be set on
1721 * the initial renegotation too in certain cases (when
1722 * there's some other reason to disallow resuming an
1723 * earlier session -- the current code won't be doing
1724 * anything like that, but this might change).
1725
1726 * A valid sign that there's been a previous handshake
1727 * in this connection is if s->s3->tmp.finish_md_len >
1728 * 0. (We are talking about a check that will happen
1729 * in the Hello protocol round, well before a new
1730 * Finished message could have been computed.) */
1731 s->s3->next_proto_neg_seen = 1;
1732 }
Adam Langley95c29f32014-06-20 12:00:00 -07001733
1734 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1735 s->ctx->alpn_select_cb &&
1736 s->s3->tmp.finish_md_len == 0)
1737 {
David Benjamindc72ff72014-06-25 12:36:10 -04001738 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001739 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001740 /* ALPN takes precedence over NPN. */
1741 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001742 }
1743
Adam Langley1258b6a2014-06-20 12:00:00 -07001744 else if (type == TLSEXT_TYPE_channel_id &&
1745 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001746 {
1747 /* The extension must be empty. */
1748 if (CBS_len(&extension) != 0)
1749 {
1750 *out_alert = SSL_AD_DECODE_ERROR;
1751 return 0;
1752 }
1753
Adam Langley1258b6a2014-06-20 12:00:00 -07001754 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001755 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001756
1757 else if (type == TLSEXT_TYPE_channel_id_new &&
1758 s->tlsext_channel_id_enabled)
1759 {
David Benjamindc72ff72014-06-25 12:36:10 -04001760 /* The extension must be empty. */
1761 if (CBS_len(&extension) != 0)
1762 {
1763 *out_alert = SSL_AD_DECODE_ERROR;
1764 return 0;
1765 }
1766
Adam Langley1258b6a2014-06-20 12:00:00 -07001767 s->s3->tlsext_channel_id_valid = 1;
1768 s->s3->tlsext_channel_id_new = 1;
1769 }
1770
1771
Adam Langley95c29f32014-06-20 12:00:00 -07001772 /* session ticket processed earlier */
1773 else if (type == TLSEXT_TYPE_use_srtp)
1774 {
David Benjamindc72ff72014-06-25 12:36:10 -04001775 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001776 return 0;
1777 }
Adam Langley95c29f32014-06-20 12:00:00 -07001778 }
1779
Adam Langley95c29f32014-06-20 12:00:00 -07001780 ri_check:
1781
1782 /* Need RI if renegotiating */
1783
1784 if (!renegotiate_seen && s->renegotiate &&
1785 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1786 {
David Benjamindc72ff72014-06-25 12:36:10 -04001787 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001788 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001789 return 0;
1790 }
1791 /* If no signature algorithms extension set default values */
1792 if (!s->cert->peer_sigalgs)
1793 ssl_cert_set_default_md(s->cert);
1794
1795 return 1;
1796 }
1797
David Benjamindc72ff72014-06-25 12:36:10 -04001798int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001799 {
David Benjamindc72ff72014-06-25 12:36:10 -04001800 int alert = -1;
1801 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001802 {
David Benjamindc72ff72014-06-25 12:36:10 -04001803 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001804 return 0;
1805 }
1806
David Benjamin6c7aed02014-08-27 16:42:38 -04001807 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001808 {
David Benjamin9d28c752014-07-05 00:43:48 -04001809 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001810 return 0;
1811 }
1812 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001813 }
Adam Langley95c29f32014-06-20 12:00:00 -07001814
Adam Langley95c29f32014-06-20 12:00:00 -07001815/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1816 * elements of zero length are allowed and the set of elements must exactly fill
1817 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001818static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001819 {
David Benjamin03973092014-06-24 23:27:17 -04001820 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001821
David Benjamin03973092014-06-24 23:27:17 -04001822 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001823 {
David Benjamin03973092014-06-24 23:27:17 -04001824 CBS proto;
1825 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1826 CBS_len(&proto) == 0)
1827 {
Adam Langley95c29f32014-06-20 12:00:00 -07001828 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001829 }
Adam Langley95c29f32014-06-20 12:00:00 -07001830 }
David Benjamin03973092014-06-24 23:27:17 -04001831 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
David Benjamin03973092014-06-24 23:27:17 -04001834static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001835 {
Adam Langley95c29f32014-06-20 12:00:00 -07001836 int tlsext_servername = 0;
1837 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001838 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
David Benjamin6c7aed02014-08-27 16:42:38 -04001840 /* TODO(davidben): Move all of these to some per-handshake state that
1841 * gets systematically reset on a new handshake; perhaps allocate it
1842 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001843 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001844
David Benjamin6c7aed02014-08-27 16:42:38 -04001845 s->tlsext_ticket_expected = 0;
1846 s->s3->tmp.certificate_status_expected = 0;
David Benjamin64442872014-07-21 17:43:45 -04001847
Adam Langley95c29f32014-06-20 12:00:00 -07001848 if (s->s3->alpn_selected)
1849 {
1850 OPENSSL_free(s->s3->alpn_selected);
1851 s->s3->alpn_selected = NULL;
1852 }
1853
David Benjamin03973092014-06-24 23:27:17 -04001854 /* There may be no extensions. */
1855 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001856 {
David Benjamin03973092014-06-24 23:27:17 -04001857 goto ri_check;
1858 }
1859
David Benjamin35a7a442014-07-05 00:23:20 -04001860 /* Decode the extensions block and check it is valid. */
1861 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1862 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001863 {
1864 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001865 return 0;
1866 }
1867
David Benjamin03973092014-06-24 23:27:17 -04001868 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001869 {
David Benjamin03973092014-06-24 23:27:17 -04001870 uint16_t type;
1871 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001872
David Benjamin03973092014-06-24 23:27:17 -04001873 /* Decode the next extension. */
1874 if (!CBS_get_u16(&extensions, &type) ||
1875 !CBS_get_u16_length_prefixed(&extensions, &extension))
1876 {
1877 *out_alert = SSL_AD_DECODE_ERROR;
1878 return 0;
1879 }
Adam Langley95c29f32014-06-20 12:00:00 -07001880
1881 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001882 {
1883 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1884 CBS_len(&extension), s->tlsext_debug_arg);
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
1887 if (type == TLSEXT_TYPE_server_name)
1888 {
David Benjamin03973092014-06-24 23:27:17 -04001889 /* The extension must be empty. */
1890 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001891 {
David Benjamin03973092014-06-24 23:27:17 -04001892 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001893 return 0;
1894 }
David Benjamin03973092014-06-24 23:27:17 -04001895 /* We must have sent it in ClientHello. */
1896 if (s->tlsext_hostname == NULL)
1897 {
1898 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1899 return 0;
1900 }
1901 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001902 }
Adam Langley95c29f32014-06-20 12:00:00 -07001903 else if (type == TLSEXT_TYPE_ec_point_formats)
1904 {
David Benjamin03973092014-06-24 23:27:17 -04001905 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001906
David Benjamin03973092014-06-24 23:27:17 -04001907 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1908 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001909 {
David Benjamin03973092014-06-24 23:27:17 -04001910 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001911 return 0;
1912 }
David Benjamin03973092014-06-24 23:27:17 -04001913
Adam Langley5ba06a72014-08-06 17:27:31 -07001914 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001915 {
Adam Langley5ba06a72014-08-06 17:27:31 -07001916 if (!CBS_stow(&ec_point_format_list,
1917 &s->session->tlsext_ecpointformatlist,
1918 &s->session->tlsext_ecpointformatlist_length))
1919 {
1920 *out_alert = SSL_AD_INTERNAL_ERROR;
1921 return 0;
1922 }
Adam Langley95c29f32014-06-20 12:00:00 -07001923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924 }
Adam Langley95c29f32014-06-20 12:00:00 -07001925 else if (type == TLSEXT_TYPE_session_ticket)
1926 {
1927 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001928 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1929 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001930 {
David Benjamin03973092014-06-24 23:27:17 -04001931 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001932 return 0;
1933 }
David Benjamin03973092014-06-24 23:27:17 -04001934
1935 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001936 {
David Benjamin03973092014-06-24 23:27:17 -04001937 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001938 return 0;
1939 }
David Benjamin03973092014-06-24 23:27:17 -04001940
Adam Langley95c29f32014-06-20 12:00:00 -07001941 s->tlsext_ticket_expected = 1;
1942 }
Adam Langley95c29f32014-06-20 12:00:00 -07001943 else if (type == TLSEXT_TYPE_status_request)
1944 {
David Benjamin03973092014-06-24 23:27:17 -04001945 /* The extension MUST be empty and may only sent if
1946 * we've requested a status request message. */
1947 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001948 {
David Benjamin03973092014-06-24 23:27:17 -04001949 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001950 return 0;
1951 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001952 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001953 {
1954 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1955 return 0;
1956 }
1957 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001958 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001959 }
David Benjamin03973092014-06-24 23:27:17 -04001960 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1961 unsigned char *selected;
1962 unsigned char selected_len;
1963
1964 /* We must have requested it. */
1965 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001966 {
David Benjamin03973092014-06-24 23:27:17 -04001967 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1968 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001969 }
Adam Langley95c29f32014-06-20 12:00:00 -07001970
David Benjamin03973092014-06-24 23:27:17 -04001971 /* The data must be valid. */
1972 if (!ssl_next_proto_validate(&extension))
1973 {
1974 *out_alert = SSL_AD_DECODE_ERROR;
1975 return 0;
1976 }
1977
1978 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
1979 CBS_data(&extension), CBS_len(&extension),
1980 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1981 {
1982 *out_alert = SSL_AD_INTERNAL_ERROR;
1983 return 0;
1984 }
1985
1986 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1987 if (s->next_proto_negotiated == NULL)
1988 {
1989 *out_alert = SSL_AD_INTERNAL_ERROR;
1990 return 0;
1991 }
1992 s->next_proto_negotiated_len = selected_len;
1993 s->s3->next_proto_neg_seen = 1;
1994 }
Adam Langley95c29f32014-06-20 12:00:00 -07001995 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
1996 {
David Benjamin03973092014-06-24 23:27:17 -04001997 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001998
1999 /* We must have requested it. */
2000 if (s->alpn_client_proto_list == NULL)
2001 {
David Benjamin03973092014-06-24 23:27:17 -04002002 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002003 return 0;
2004 }
David Benjamin03973092014-06-24 23:27:17 -04002005
2006 /* The extension data consists of a ProtocolNameList
2007 * which must have exactly one ProtocolName. Each of
2008 * these is length-prefixed. */
2009 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2010 CBS_len(&extension) != 0 ||
2011 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2012 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002013 {
David Benjamin03973092014-06-24 23:27:17 -04002014 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002015 return 0;
2016 }
David Benjamin03973092014-06-24 23:27:17 -04002017
2018 if (!CBS_stow(&protocol_name,
2019 &s->s3->alpn_selected,
2020 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002021 {
David Benjamin03973092014-06-24 23:27:17 -04002022 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002023 return 0;
2024 }
Adam Langley95c29f32014-06-20 12:00:00 -07002025 }
2026
Adam Langley1258b6a2014-06-20 12:00:00 -07002027 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002028 {
2029 if (CBS_len(&extension) != 0)
2030 {
2031 *out_alert = SSL_AD_DECODE_ERROR;
2032 return 0;
2033 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002034 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002035 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002036 else if (type == TLSEXT_TYPE_channel_id_new)
2037 {
David Benjamin03973092014-06-24 23:27:17 -04002038 if (CBS_len(&extension) != 0)
2039 {
2040 *out_alert = SSL_AD_DECODE_ERROR;
2041 return 0;
2042 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002043 s->s3->tlsext_channel_id_valid = 1;
2044 s->s3->tlsext_channel_id_new = 1;
2045 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002046 else if (type == TLSEXT_TYPE_certificate_timestamp)
2047 {
2048 if (CBS_len(&extension) == 0)
2049 {
2050 *out_alert = SSL_AD_DECODE_ERROR;
2051 return 0;
2052 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002053
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002054 /* Session resumption uses the original session information. */
2055 if (!s->hit)
2056 {
2057 if (!CBS_stow(&extension,
2058 &s->session->tlsext_signed_cert_timestamp_list,
2059 &s->session->tlsext_signed_cert_timestamp_list_length))
2060 {
2061 *out_alert = SSL_AD_INTERNAL_ERROR;
2062 return 0;
2063 }
2064 }
2065 }
Adam Langley95c29f32014-06-20 12:00:00 -07002066 else if (type == TLSEXT_TYPE_renegotiate)
2067 {
David Benjamin03973092014-06-24 23:27:17 -04002068 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002069 return 0;
2070 renegotiate_seen = 1;
2071 }
Adam Langley95c29f32014-06-20 12:00:00 -07002072 else if (type == TLSEXT_TYPE_use_srtp)
2073 {
David Benjamin03973092014-06-24 23:27:17 -04002074 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002075 return 0;
2076 }
Adam Langley95c29f32014-06-20 12:00:00 -07002077 }
2078
2079 if (!s->hit && tlsext_servername == 1)
2080 {
2081 if (s->tlsext_hostname)
2082 {
2083 if (s->session->tlsext_hostname == NULL)
2084 {
2085 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2086 if (!s->session->tlsext_hostname)
2087 {
David Benjamin03973092014-06-24 23:27:17 -04002088 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002089 return 0;
2090 }
2091 }
2092 else
2093 {
David Benjamin03973092014-06-24 23:27:17 -04002094 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002095 return 0;
2096 }
2097 }
2098 }
2099
Adam Langley95c29f32014-06-20 12:00:00 -07002100 ri_check:
2101
2102 /* Determine if we need to see RI. Strictly speaking if we want to
2103 * avoid an attack we should *always* see RI even on initial server
2104 * hello because the client doesn't see any renegotiation during an
2105 * attack. However this would mean we could not connect to any server
2106 * which doesn't support RI so for the immediate future tolerate RI
2107 * absence on initial connect only.
2108 */
2109 if (!renegotiate_seen
2110 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2111 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2112 {
David Benjamin03973092014-06-24 23:27:17 -04002113 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002114 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002115 return 0;
2116 }
2117
2118 return 1;
2119 }
2120
2121
2122int ssl_prepare_clienthello_tlsext(SSL *s)
2123 {
Adam Langley95c29f32014-06-20 12:00:00 -07002124 return 1;
2125 }
2126
2127int ssl_prepare_serverhello_tlsext(SSL *s)
2128 {
2129 return 1;
2130 }
2131
David Benjamin6c7aed02014-08-27 16:42:38 -04002132static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002133 {
2134 int ret=SSL_TLSEXT_ERR_NOACK;
2135 int al = SSL_AD_UNRECOGNIZED_NAME;
2136
Adam Langley95c29f32014-06-20 12:00:00 -07002137 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2138 * ssl3_choose_cipher in s3_lib.c.
2139 */
2140 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2141 * ssl3_choose_cipher in s3_lib.c.
2142 */
Adam Langley95c29f32014-06-20 12:00:00 -07002143
2144 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2145 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2146 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2147 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2148
Adam Langley95c29f32014-06-20 12:00:00 -07002149 switch (ret)
2150 {
2151 case SSL_TLSEXT_ERR_ALERT_FATAL:
2152 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2153 return -1;
2154
2155 case SSL_TLSEXT_ERR_ALERT_WARNING:
2156 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002157 return 1;
2158
Adam Langley95c29f32014-06-20 12:00:00 -07002159 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002160 s->should_ack_sni = 0;
2161 return 1;
2162
2163 default:
2164 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002165 }
2166 }
2167
David Benjamin6c7aed02014-08-27 16:42:38 -04002168static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002169 {
2170 int ret=SSL_TLSEXT_ERR_NOACK;
2171 int al = SSL_AD_UNRECOGNIZED_NAME;
2172
Adam Langley95c29f32014-06-20 12:00:00 -07002173 /* If we are client and using an elliptic curve cryptography cipher
2174 * suite, then if server returns an EC point formats lists extension
2175 * it must contain uncompressed.
2176 */
2177 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2178 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2179 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2180 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
David Benjamin0da0e182014-08-19 16:20:28 -04002181 ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07002182 {
2183 /* we are using an ECC cipher */
2184 size_t i;
2185 unsigned char *list;
2186 int found_uncompressed = 0;
2187 list = s->session->tlsext_ecpointformatlist;
2188 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2189 {
2190 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
2191 {
2192 found_uncompressed = 1;
2193 break;
2194 }
2195 }
2196 if (!found_uncompressed)
2197 {
David Benjamin172fc2c2014-09-06 13:09:47 -04002198 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
Adam Langley95c29f32014-06-20 12:00:00 -07002199 return -1;
2200 }
2201 }
2202 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002203
2204 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2205 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2206 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2207 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2208
Adam Langley95c29f32014-06-20 12:00:00 -07002209 switch (ret)
2210 {
2211 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002212 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002213 return -1;
2214
2215 case SSL_TLSEXT_ERR_ALERT_WARNING:
2216 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002217 return 1;
2218
2219 default:
2220 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002221 }
2222 }
2223
David Benjamin03973092014-06-24 23:27:17 -04002224int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002225 {
David Benjamin03973092014-06-24 23:27:17 -04002226 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002227 if (s->version < SSL3_VERSION)
2228 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002229
2230 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002231 {
David Benjamin03973092014-06-24 23:27:17 -04002232 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002233 return 0;
2234 }
2235
David Benjamin03973092014-06-24 23:27:17 -04002236 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002237 {
David Benjamin9d28c752014-07-05 00:43:48 -04002238 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002239 return 0;
2240 }
David Benjamin03973092014-06-24 23:27:17 -04002241
Adam Langley95c29f32014-06-20 12:00:00 -07002242 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002243 }
Adam Langley95c29f32014-06-20 12:00:00 -07002244
2245/* Since the server cache lookup is done early on in the processing of the
2246 * ClientHello, and other operations depend on the result, we need to handle
2247 * any TLS session ticket extension at the same time.
2248 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002249 * ctx: contains the early callback context, which is the result of a
2250 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002251 * ret: (output) on return, if a ticket was decrypted, then this is set to
2252 * point to the resulting session.
2253 *
2254 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2255 * ciphersuite, in which case we have no use for session tickets and one will
2256 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2257 *
2258 * Returns:
2259 * -1: fatal error, either from parsing or decrypting the ticket.
2260 * 0: no ticket was found (or was ignored, based on settings).
2261 * 1: a zero length extension was found, indicating that the client supports
2262 * session tickets but doesn't currently have one to offer.
2263 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2264 * couldn't be decrypted because of a non-fatal error.
2265 * 3: a ticket was successfully decrypted and *ret was set.
2266 *
2267 * Side effects:
2268 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2269 * a new session ticket to the client because the client indicated support
2270 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2271 * a session ticket or we couldn't use the one it gave us, or if
2272 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2273 * Otherwise, s->tlsext_ticket_expected is set to 0.
2274 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002275int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2276 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002277 {
Adam Langley95c29f32014-06-20 12:00:00 -07002278 *ret = NULL;
2279 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002280 const unsigned char *data;
2281 size_t len;
2282 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002283
2284 /* If tickets disabled behave as if no ticket present
2285 * to permit stateful resumption.
2286 */
2287 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2288 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002289 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002290 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002291 if (!SSL_early_callback_ctx_extension_get(
2292 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002293 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002294 return 0;
2295 }
2296 if (len == 0)
2297 {
2298 /* The client will accept a ticket but doesn't
2299 * currently have one. */
2300 s->tlsext_ticket_expected = 1;
2301 return 1;
2302 }
2303 if (s->tls_session_secret_cb)
2304 {
2305 /* Indicate that the ticket couldn't be
2306 * decrypted rather than generating the session
2307 * from ticket now, trigger abbreviated
2308 * handshake based on external mechanism to
2309 * calculate the master secret later. */
2310 return 2;
2311 }
2312 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2313 ctx->session_id_len, ret);
2314 switch (r)
2315 {
2316 case 2: /* ticket couldn't be decrypted */
2317 s->tlsext_ticket_expected = 1;
2318 return 2;
2319 case 3: /* ticket was decrypted */
2320 return r;
2321 case 4: /* ticket decrypted but need to renew */
2322 s->tlsext_ticket_expected = 1;
2323 return 3;
2324 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002325 return -1;
2326 }
Adam Langley95c29f32014-06-20 12:00:00 -07002327 }
2328
2329/* tls_decrypt_ticket attempts to decrypt a session ticket.
2330 *
2331 * etick: points to the body of the session ticket extension.
2332 * eticklen: the length of the session tickets extenion.
2333 * sess_id: points at the session ID.
2334 * sesslen: the length of the session ID.
2335 * psess: (output) on return, if a ticket was decrypted, then this is set to
2336 * point to the resulting session.
2337 *
2338 * Returns:
2339 * -1: fatal error, either from parsing or decrypting the ticket.
2340 * 2: the ticket couldn't be decrypted.
2341 * 3: a ticket was successfully decrypted and *psess was set.
2342 * 4: same as 3, but the ticket needs to be renewed.
2343 */
2344static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2345 const unsigned char *sess_id, int sesslen,
2346 SSL_SESSION **psess)
2347 {
2348 SSL_SESSION *sess;
2349 unsigned char *sdec;
2350 const unsigned char *p;
2351 int slen, mlen, renew_ticket = 0;
2352 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2353 HMAC_CTX hctx;
2354 EVP_CIPHER_CTX ctx;
2355 SSL_CTX *tctx = s->initial_ctx;
2356 /* Need at least keyname + iv + some encrypted data */
2357 if (eticklen < 48)
2358 return 2;
2359 /* Initialize session ticket encryption and HMAC contexts */
2360 HMAC_CTX_init(&hctx);
2361 EVP_CIPHER_CTX_init(&ctx);
2362 if (tctx->tlsext_ticket_key_cb)
2363 {
2364 unsigned char *nctick = (unsigned char *)etick;
2365 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2366 &ctx, &hctx, 0);
2367 if (rv < 0)
2368 return -1;
2369 if (rv == 0)
2370 return 2;
2371 if (rv == 2)
2372 renew_ticket = 1;
2373 }
2374 else
2375 {
2376 /* Check key name matches */
2377 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2378 return 2;
2379 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2380 tlsext_tick_md(), NULL);
2381 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2382 tctx->tlsext_tick_aes_key, etick + 16);
2383 }
2384 /* Attempt to process session ticket, first conduct sanity and
2385 * integrity checks on ticket.
2386 */
2387 mlen = HMAC_size(&hctx);
2388 if (mlen < 0)
2389 {
2390 EVP_CIPHER_CTX_cleanup(&ctx);
2391 return -1;
2392 }
2393 eticklen -= mlen;
2394 /* Check HMAC of encrypted ticket */
2395 HMAC_Update(&hctx, etick, eticklen);
2396 HMAC_Final(&hctx, tick_hmac, NULL);
2397 HMAC_CTX_cleanup(&hctx);
2398 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
2399 return 2;
2400 /* Attempt to decrypt session data */
2401 /* Move p after IV to start of encrypted ticket, update length */
2402 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2403 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2404 sdec = OPENSSL_malloc(eticklen);
2405 if (!sdec)
2406 {
2407 EVP_CIPHER_CTX_cleanup(&ctx);
2408 return -1;
2409 }
2410 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2411 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002412 {
2413 EVP_CIPHER_CTX_cleanup(&ctx);
2414 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002415 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002416 }
Adam Langley95c29f32014-06-20 12:00:00 -07002417 slen += mlen;
2418 EVP_CIPHER_CTX_cleanup(&ctx);
2419 p = sdec;
2420
2421 sess = d2i_SSL_SESSION(NULL, &p, slen);
2422 OPENSSL_free(sdec);
2423 if (sess)
2424 {
2425 /* The session ID, if non-empty, is used by some clients to
2426 * detect that the ticket has been accepted. So we copy it to
2427 * the session structure. If it is empty set length to zero
2428 * as required by standard.
2429 */
2430 if (sesslen)
2431 memcpy(sess->session_id, sess_id, sesslen);
2432 sess->session_id_length = sesslen;
2433 *psess = sess;
2434 if (renew_ticket)
2435 return 4;
2436 else
2437 return 3;
2438 }
2439 ERR_clear_error();
2440 /* For session parse failure, indicate that we need to send a new
2441 * ticket. */
2442 return 2;
2443 }
2444
2445/* Tables to translate from NIDs to TLS v1.2 ids */
2446
2447typedef struct
2448 {
2449 int nid;
2450 int id;
2451 } tls12_lookup;
2452
David Benjamincff64722014-08-19 19:54:46 -04002453static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002454 {NID_md5, TLSEXT_hash_md5},
2455 {NID_sha1, TLSEXT_hash_sha1},
2456 {NID_sha224, TLSEXT_hash_sha224},
2457 {NID_sha256, TLSEXT_hash_sha256},
2458 {NID_sha384, TLSEXT_hash_sha384},
2459 {NID_sha512, TLSEXT_hash_sha512}
2460};
2461
David Benjamincff64722014-08-19 19:54:46 -04002462static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002463 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002464 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2465};
2466
David Benjamincff64722014-08-19 19:54:46 -04002467static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002468 {
2469 size_t i;
2470 for (i = 0; i < tlen; i++)
2471 {
2472 if (table[i].nid == nid)
2473 return table[i].id;
2474 }
2475 return -1;
2476 }
2477
David Benjamincff64722014-08-19 19:54:46 -04002478static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002479 {
2480 size_t i;
2481 for (i = 0; i < tlen; i++)
2482 {
2483 if ((table[i].id) == id)
2484 return table[i].nid;
2485 }
2486 return NID_undef;
2487 }
2488
2489int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2490 {
2491 int sig_id, md_id;
2492 if (!md)
2493 return 0;
2494 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2495 sizeof(tls12_md)/sizeof(tls12_lookup));
2496 if (md_id == -1)
2497 return 0;
2498 sig_id = tls12_get_sigid(pk);
2499 if (sig_id == -1)
2500 return 0;
2501 p[0] = (unsigned char)md_id;
2502 p[1] = (unsigned char)sig_id;
2503 return 1;
2504 }
2505
2506int tls12_get_sigid(const EVP_PKEY *pk)
2507 {
2508 return tls12_find_id(pk->type, tls12_sig,
2509 sizeof(tls12_sig)/sizeof(tls12_lookup));
2510 }
2511
2512const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2513 {
2514 switch(hash_alg)
2515 {
Adam Langley95c29f32014-06-20 12:00:00 -07002516 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002517 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002518 case TLSEXT_hash_sha1:
2519 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002520 case TLSEXT_hash_sha224:
2521 return EVP_sha224();
2522
2523 case TLSEXT_hash_sha256:
2524 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002525 case TLSEXT_hash_sha384:
2526 return EVP_sha384();
2527
2528 case TLSEXT_hash_sha512:
2529 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002530 default:
2531 return NULL;
2532
2533 }
2534 }
2535
2536static int tls12_get_pkey_idx(unsigned char sig_alg)
2537 {
2538 switch(sig_alg)
2539 {
Adam Langley95c29f32014-06-20 12:00:00 -07002540 case TLSEXT_signature_rsa:
2541 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002542 case TLSEXT_signature_ecdsa:
2543 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002544 }
2545 return -1;
2546 }
2547
2548/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2549static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2550 int *psignhash_nid, const unsigned char *data)
2551 {
2552 int sign_nid = 0, hash_nid = 0;
2553 if (!phash_nid && !psign_nid && !psignhash_nid)
2554 return;
2555 if (phash_nid || psignhash_nid)
2556 {
2557 hash_nid = tls12_find_nid(data[0], tls12_md,
2558 sizeof(tls12_md)/sizeof(tls12_lookup));
2559 if (phash_nid)
2560 *phash_nid = hash_nid;
2561 }
2562 if (psign_nid || psignhash_nid)
2563 {
2564 sign_nid = tls12_find_nid(data[1], tls12_sig,
2565 sizeof(tls12_sig)/sizeof(tls12_lookup));
2566 if (psign_nid)
2567 *psign_nid = sign_nid;
2568 }
2569 if (psignhash_nid)
2570 {
2571 if (sign_nid && hash_nid)
2572 OBJ_find_sigid_by_algs(psignhash_nid,
2573 hash_nid, sign_nid);
2574 else
2575 *psignhash_nid = NID_undef;
2576 }
2577 }
2578/* Given preference and allowed sigalgs set shared sigalgs */
2579static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2580 const unsigned char *pref, size_t preflen,
2581 const unsigned char *allow, size_t allowlen)
2582 {
2583 const unsigned char *ptmp, *atmp;
2584 size_t i, j, nmatch = 0;
2585 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2586 {
2587 /* Skip disabled hashes or signature algorithms */
2588 if (tls12_get_hash(ptmp[0]) == NULL)
2589 continue;
2590 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2591 continue;
2592 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2593 {
2594 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2595 {
2596 nmatch++;
2597 if (shsig)
2598 {
2599 shsig->rhash = ptmp[0];
2600 shsig->rsign = ptmp[1];
2601 tls1_lookup_sigalg(&shsig->hash_nid,
2602 &shsig->sign_nid,
2603 &shsig->signandhash_nid,
2604 ptmp);
2605 shsig++;
2606 }
2607 break;
2608 }
2609 }
2610 }
2611 return nmatch;
2612 }
2613
2614/* Set shared signature algorithms for SSL structures */
2615static int tls1_set_shared_sigalgs(SSL *s)
2616 {
2617 const unsigned char *pref, *allow, *conf;
2618 size_t preflen, allowlen, conflen;
2619 size_t nmatch;
2620 TLS_SIGALGS *salgs = NULL;
2621 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002622 if (c->shared_sigalgs)
2623 {
2624 OPENSSL_free(c->shared_sigalgs);
2625 c->shared_sigalgs = NULL;
2626 }
Adam Langley95c29f32014-06-20 12:00:00 -07002627 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002628 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002629 {
2630 conf = c->client_sigalgs;
2631 conflen = c->client_sigalgslen;
2632 }
David Benjamin335d10d2014-08-06 19:56:33 -04002633 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002634 {
2635 conf = c->conf_sigalgs;
2636 conflen = c->conf_sigalgslen;
2637 }
2638 else
2639 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002640 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002641 {
2642 pref = conf;
2643 preflen = conflen;
2644 allow = c->peer_sigalgs;
2645 allowlen = c->peer_sigalgslen;
2646 }
2647 else
2648 {
2649 allow = conf;
2650 allowlen = conflen;
2651 pref = c->peer_sigalgs;
2652 preflen = c->peer_sigalgslen;
2653 }
2654 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2655 if (!nmatch)
2656 return 1;
2657 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2658 if (!salgs)
2659 return 0;
2660 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2661 c->shared_sigalgs = salgs;
2662 c->shared_sigalgslen = nmatch;
2663 return 1;
2664 }
2665
2666
2667/* Set preferred digest for each key type */
2668
David Benjamincd996942014-07-20 16:23:51 -04002669int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002670 {
2671 int idx;
2672 size_t i;
2673 const EVP_MD *md;
2674 CERT *c = s->cert;
2675 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002676
Adam Langley95c29f32014-06-20 12:00:00 -07002677 /* Extension ignored for inappropriate versions */
2678 if (!SSL_USE_SIGALGS(s))
2679 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002680 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002681 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002682 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002683 /* Should never happen */
2684 if (!c)
2685 return 0;
2686
David Benjamincd996942014-07-20 16:23:51 -04002687 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002688 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002689
2690 tls1_set_shared_sigalgs(s);
2691
2692#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2693 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2694 {
2695 /* Use first set signature preference to force message
2696 * digest, ignoring any peer preferences.
2697 */
2698 const unsigned char *sigs = NULL;
2699 if (s->server)
2700 sigs = c->conf_sigalgs;
2701 else
2702 sigs = c->client_sigalgs;
2703 if (sigs)
2704 {
2705 idx = tls12_get_pkey_idx(sigs[1]);
2706 md = tls12_get_hash(sigs[0]);
2707 c->pkeys[idx].digest = md;
2708 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2709 if (idx == SSL_PKEY_RSA_SIGN)
2710 {
2711 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2712 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2713 }
2714 }
2715 }
2716#endif
2717
2718 for (i = 0, sigptr = c->shared_sigalgs;
2719 i < c->shared_sigalgslen; i++, sigptr++)
2720 {
2721 idx = tls12_get_pkey_idx(sigptr->rsign);
2722 if (idx > 0 && c->pkeys[idx].digest == NULL)
2723 {
2724 md = tls12_get_hash(sigptr->rhash);
2725 c->pkeys[idx].digest = md;
2726 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2727 if (idx == SSL_PKEY_RSA_SIGN)
2728 {
2729 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2730 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2731 }
2732 }
2733
2734 }
2735 /* In strict mode leave unset digests as NULL to indicate we can't
2736 * use the certificate for signing.
2737 */
2738 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
2739 {
2740 /* Set any remaining keys to default values. NOTE: if alg is
2741 * not supported it stays as NULL.
2742 */
Adam Langley95c29f32014-06-20 12:00:00 -07002743 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2744 {
2745 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2746 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2747 }
Adam Langley95c29f32014-06-20 12:00:00 -07002748 if (!c->pkeys[SSL_PKEY_ECC].digest)
2749 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002750 }
2751 return 1;
2752 }
2753
2754
2755int SSL_get_sigalgs(SSL *s, int idx,
2756 int *psign, int *phash, int *psignhash,
2757 unsigned char *rsig, unsigned char *rhash)
2758 {
2759 const unsigned char *psig = s->cert->peer_sigalgs;
2760 if (psig == NULL)
2761 return 0;
2762 if (idx >= 0)
2763 {
2764 idx <<= 1;
2765 if (idx >= (int)s->cert->peer_sigalgslen)
2766 return 0;
2767 psig += idx;
2768 if (rhash)
2769 *rhash = psig[0];
2770 if (rsig)
2771 *rsig = psig[1];
2772 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2773 }
2774 return s->cert->peer_sigalgslen / 2;
2775 }
2776
2777int SSL_get_shared_sigalgs(SSL *s, int idx,
2778 int *psign, int *phash, int *psignhash,
2779 unsigned char *rsig, unsigned char *rhash)
2780 {
2781 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2782 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2783 return 0;
2784 shsigalgs += idx;
2785 if (phash)
2786 *phash = shsigalgs->hash_nid;
2787 if (psign)
2788 *psign = shsigalgs->sign_nid;
2789 if (psignhash)
2790 *psignhash = shsigalgs->signandhash_nid;
2791 if (rsig)
2792 *rsig = shsigalgs->rsign;
2793 if (rhash)
2794 *rhash = shsigalgs->rhash;
2795 return s->cert->shared_sigalgslen;
2796 }
2797
Adam Langley1258b6a2014-06-20 12:00:00 -07002798/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2799 * SSL connection and writes it to |md|. */
2800int
2801tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2802 {
2803 EVP_MD_CTX ctx;
2804 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2805 unsigned temp_digest_len;
2806 int i;
2807 static const char kClientIDMagic[] = "TLS Channel ID signature";
2808
2809 if (s->s3->handshake_buffer)
2810 if (!ssl3_digest_cached_records(s))
2811 return 0;
2812
2813 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2814
2815 if (s->hit && s->s3->tlsext_channel_id_new)
2816 {
2817 static const char kResumptionMagic[] = "Resumption";
2818 EVP_DigestUpdate(md, kResumptionMagic,
2819 sizeof(kResumptionMagic));
2820 if (s->session->original_handshake_hash_len == 0)
2821 return 0;
2822 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2823 s->session->original_handshake_hash_len);
2824 }
2825
2826 EVP_MD_CTX_init(&ctx);
2827 for (i = 0; i < SSL_MAX_DIGEST; i++)
2828 {
2829 if (s->s3->handshake_dgst[i] == NULL)
2830 continue;
2831 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2832 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2833 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2834 }
2835 EVP_MD_CTX_cleanup(&ctx);
2836
2837 return 1;
2838 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002839
2840/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2841 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2842int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2843 {
2844 int digest_len;
2845 /* This function should never be called for a resumed session because
2846 * the handshake hashes that we wish to record are for the original,
2847 * full handshake. */
2848 if (s->hit)
2849 return -1;
2850 /* It only makes sense to call this function if Channel IDs have been
2851 * negotiated. */
2852 if (!s->s3->tlsext_channel_id_new)
2853 return -1;
2854
2855 digest_len = tls1_handshake_digest(
2856 s, s->session->original_handshake_hash,
2857 sizeof(s->session->original_handshake_hash));
2858 if (digest_len < 0)
2859 return -1;
2860
2861 s->session->original_handshake_hash_len = digest_len;
2862
2863 return 1;
2864 }
2865
Adam Langley95c29f32014-06-20 12:00:00 -07002866int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2867 {
2868 unsigned char *sigalgs, *sptr;
2869 int rhash, rsign;
2870 size_t i;
2871 if (salglen & 1)
2872 return 0;
2873 sigalgs = OPENSSL_malloc(salglen);
2874 if (sigalgs == NULL)
2875 return 0;
2876 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2877 {
2878 rhash = tls12_find_id(*psig_nids++, tls12_md,
2879 sizeof(tls12_md)/sizeof(tls12_lookup));
2880 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2881 sizeof(tls12_sig)/sizeof(tls12_lookup));
2882
2883 if (rhash == -1 || rsign == -1)
2884 goto err;
2885 *sptr++ = rhash;
2886 *sptr++ = rsign;
2887 }
2888
2889 if (client)
2890 {
2891 if (c->client_sigalgs)
2892 OPENSSL_free(c->client_sigalgs);
2893 c->client_sigalgs = sigalgs;
2894 c->client_sigalgslen = salglen;
2895 }
2896 else
2897 {
2898 if (c->conf_sigalgs)
2899 OPENSSL_free(c->conf_sigalgs);
2900 c->conf_sigalgs = sigalgs;
2901 c->conf_sigalgslen = salglen;
2902 }
2903
2904 return 1;
2905
2906 err:
2907 OPENSSL_free(sigalgs);
2908 return 0;
2909 }
2910
2911static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
2912 {
2913 int sig_nid;
2914 size_t i;
2915 if (default_nid == -1)
2916 return 1;
2917 sig_nid = X509_get_signature_nid(x);
2918 if (default_nid)
2919 return sig_nid == default_nid ? 1 : 0;
2920 for (i = 0; i < c->shared_sigalgslen; i++)
2921 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
2922 return 1;
2923 return 0;
2924 }
2925/* Check to see if a certificate issuer name matches list of CA names */
2926static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2927 {
2928 X509_NAME *nm;
2929 int i;
2930 nm = X509_get_issuer_name(x);
2931 for (i = 0; i < sk_X509_NAME_num(names); i++)
2932 {
2933 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2934 return 1;
2935 }
2936 return 0;
2937 }
2938
2939/* Check certificate chain is consistent with TLS extensions and is
2940 * usable by server. This servers two purposes: it allows users to
2941 * check chains before passing them to the server and it allows the
2942 * server to check chains before attempting to use them.
2943 */
2944
2945/* Flags which need to be set for a certificate when stict mode not set */
2946
2947#define CERT_PKEY_VALID_FLAGS \
2948 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2949/* Strict mode flags */
2950#define CERT_PKEY_STRICT_FLAGS \
2951 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2952 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2953
2954int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2955 int idx)
2956 {
2957 int i;
2958 int rv = 0;
2959 int check_flags = 0, strict_mode;
2960 CERT_PKEY *cpk = NULL;
2961 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002962 /* idx == -1 means checking server chains */
2963 if (idx != -1)
2964 {
2965 /* idx == -2 means checking client certificate chains */
2966 if (idx == -2)
2967 {
2968 cpk = c->key;
2969 idx = cpk - c->pkeys;
2970 }
2971 else
2972 cpk = c->pkeys + idx;
2973 x = cpk->x509;
2974 pk = cpk->privatekey;
2975 chain = cpk->chain;
2976 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2977 /* If no cert or key, forget it */
2978 if (!x || !pk)
2979 goto end;
2980#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2981 /* Allow any certificate to pass test */
2982 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2983 {
2984 rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
2985 cpk->valid_flags = rv;
2986 return rv;
2987 }
2988#endif
2989 }
2990 else
2991 {
2992 if (!x || !pk)
2993 goto end;
2994 idx = ssl_cert_type(x, pk);
2995 if (idx == -1)
2996 goto end;
2997 cpk = c->pkeys + idx;
2998 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
2999 check_flags = CERT_PKEY_STRICT_FLAGS;
3000 else
3001 check_flags = CERT_PKEY_VALID_FLAGS;
3002 strict_mode = 1;
3003 }
3004
Adam Langley95c29f32014-06-20 12:00:00 -07003005 /* Check all signature algorithms are consistent with
3006 * signature algorithms extension if TLS 1.2 or later
3007 * and strict mode.
3008 */
3009 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
3010 {
3011 int default_nid;
3012 unsigned char rsign = 0;
3013 if (c->peer_sigalgs)
3014 default_nid = 0;
3015 /* If no sigalgs extension use defaults from RFC5246 */
3016 else
3017 {
3018 switch(idx)
3019 {
3020 case SSL_PKEY_RSA_ENC:
3021 case SSL_PKEY_RSA_SIGN:
Adam Langley95c29f32014-06-20 12:00:00 -07003022 rsign = TLSEXT_signature_rsa;
3023 default_nid = NID_sha1WithRSAEncryption;
3024 break;
3025
Adam Langley95c29f32014-06-20 12:00:00 -07003026 case SSL_PKEY_ECC:
3027 rsign = TLSEXT_signature_ecdsa;
3028 default_nid = NID_ecdsa_with_SHA1;
3029 break;
3030
3031 default:
3032 default_nid = -1;
3033 break;
3034 }
3035 }
3036 /* If peer sent no signature algorithms extension and we
3037 * have set preferred signature algorithms check we support
3038 * sha1.
3039 */
3040 if (default_nid > 0 && c->conf_sigalgs)
3041 {
3042 size_t j;
3043 const unsigned char *p = c->conf_sigalgs;
3044 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
3045 {
3046 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3047 break;
3048 }
3049 if (j == c->conf_sigalgslen)
3050 {
3051 if (check_flags)
3052 goto skip_sigs;
3053 else
3054 goto end;
3055 }
3056 }
3057 /* Check signature algorithm of each cert in chain */
3058 if (!tls1_check_sig_alg(c, x, default_nid))
3059 {
3060 if (!check_flags) goto end;
3061 }
3062 else
3063 rv |= CERT_PKEY_EE_SIGNATURE;
3064 rv |= CERT_PKEY_CA_SIGNATURE;
3065 for (i = 0; i < sk_X509_num(chain); i++)
3066 {
3067 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3068 default_nid))
3069 {
3070 if (check_flags)
3071 {
3072 rv &= ~CERT_PKEY_CA_SIGNATURE;
3073 break;
3074 }
3075 else
3076 goto end;
3077 }
3078 }
3079 }
3080 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3081 else if(check_flags)
3082 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3083 skip_sigs:
3084 /* Check cert parameters are consistent */
3085 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3086 rv |= CERT_PKEY_EE_PARAM;
3087 else if (!check_flags)
3088 goto end;
3089 if (!s->server)
3090 rv |= CERT_PKEY_CA_PARAM;
3091 /* In strict mode check rest of chain too */
3092 else if (strict_mode)
3093 {
3094 rv |= CERT_PKEY_CA_PARAM;
3095 for (i = 0; i < sk_X509_num(chain); i++)
3096 {
3097 X509 *ca = sk_X509_value(chain, i);
3098 if (!tls1_check_cert_param(s, ca, 0))
3099 {
3100 if (check_flags)
3101 {
3102 rv &= ~CERT_PKEY_CA_PARAM;
3103 break;
3104 }
3105 else
3106 goto end;
3107 }
3108 }
3109 }
3110 if (!s->server && strict_mode)
3111 {
3112 STACK_OF(X509_NAME) *ca_dn;
David Benjamin676d1e72014-07-08 14:34:10 -04003113 uint8_t check_type = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003114 switch (pk->type)
3115 {
3116 case EVP_PKEY_RSA:
3117 check_type = TLS_CT_RSA_SIGN;
3118 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003119 case EVP_PKEY_EC:
3120 check_type = TLS_CT_ECDSA_SIGN;
3121 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003122 }
3123 if (check_type)
3124 {
David Benjamin676d1e72014-07-08 14:34:10 -04003125 if (s->s3->tmp.certificate_types &&
3126 memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07003127 {
Adam Langley95c29f32014-06-20 12:00:00 -07003128 rv |= CERT_PKEY_CERT_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -07003129 }
3130 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3131 goto end;
3132 }
3133 else
3134 rv |= CERT_PKEY_CERT_TYPE;
3135
3136
3137 ca_dn = s->s3->tmp.ca_names;
3138
3139 if (!sk_X509_NAME_num(ca_dn))
3140 rv |= CERT_PKEY_ISSUER_NAME;
3141
3142 if (!(rv & CERT_PKEY_ISSUER_NAME))
3143 {
3144 if (ssl_check_ca_name(ca_dn, x))
3145 rv |= CERT_PKEY_ISSUER_NAME;
3146 }
3147 if (!(rv & CERT_PKEY_ISSUER_NAME))
3148 {
3149 for (i = 0; i < sk_X509_num(chain); i++)
3150 {
3151 X509 *xtmp = sk_X509_value(chain, i);
3152 if (ssl_check_ca_name(ca_dn, xtmp))
3153 {
3154 rv |= CERT_PKEY_ISSUER_NAME;
3155 break;
3156 }
3157 }
3158 }
3159 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3160 goto end;
3161 }
3162 else
3163 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3164
3165 if (!check_flags || (rv & check_flags) == check_flags)
3166 rv |= CERT_PKEY_VALID;
3167
3168 end:
3169
3170 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3171 {
3172 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3173 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3174 else if (cpk->digest)
3175 rv |= CERT_PKEY_SIGN;
3176 }
3177 else
3178 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3179
3180 /* When checking a CERT_PKEY structure all flags are irrelevant
3181 * if the chain is invalid.
3182 */
3183 if (!check_flags)
3184 {
3185 if (rv & CERT_PKEY_VALID)
3186 cpk->valid_flags = rv;
3187 else
3188 {
3189 /* Preserve explicit sign flag, clear rest */
3190 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
3191 return 0;
3192 }
3193 }
3194 return rv;
3195 }
3196
3197/* Set validity of certificates in an SSL structure */
3198void tls1_set_cert_validity(SSL *s)
3199 {
3200 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3201 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
Adam Langley95c29f32014-06-20 12:00:00 -07003202 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3203 }
3204/* User level utiity function to check a chain is suitable */
3205int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3206 {
3207 return tls1_check_chain(s, x, pk, chain, -1);
3208 }
3209