blob: 12c67b9505d82ec7a268e3a6c664c0f7376f305b [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 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400897 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700898 unsigned long alg_k, alg_a;
899 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
900
901 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
902 {
David Benjamin6f260012014-08-15 13:49:12 -0400903 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700904
905 alg_k = c->algorithm_mkey;
906 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400907 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700908 {
909 using_ecc = 1;
910 break;
911 }
912 }
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
915 /* don't add extensions for SSLv3 unless doing secure renegotiation */
916 if (s->client_version == SSL3_VERSION
917 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700918 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700919
920 ret+=2;
921
922 if (ret>=limit) return NULL; /* this really never occurs, but ... */
923
924 if (s->tlsext_hostname != NULL)
925 {
926 /* Add TLS extension servername to the Client Hello message */
927 unsigned long size_str;
928 long lenmax;
929
930 /* check for enough space.
931 4 for the servername type and entension length
932 2 for servernamelist length
933 1 for the hostname type
934 2 for hostname length
935 + hostname length
936 */
937
938 if ((lenmax = limit - ret - 9) < 0
939 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
940 return NULL;
941
942 /* extension type and length */
943 s2n(TLSEXT_TYPE_server_name,ret);
944 s2n(size_str+5,ret);
945
946 /* length of servername list */
947 s2n(size_str+3,ret);
948
949 /* hostname type, length and hostname */
950 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
951 s2n(size_str,ret);
952 memcpy(ret, s->tlsext_hostname, size_str);
953 ret+=size_str;
954 }
955
956 /* Add RI if renegotiating */
957 if (s->renegotiate)
958 {
959 int el;
960
961 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
962 {
963 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
964 return NULL;
965 }
966
Adam Langleyb0c235e2014-06-20 12:00:00 -0700967 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700968
969 s2n(TLSEXT_TYPE_renegotiate,ret);
970 s2n(el,ret);
971
972 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
973 {
974 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
975 return NULL;
976 }
977
978 ret += el;
979 }
980
Adam Langley95c29f32014-06-20 12:00:00 -0700981 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
982 {
983 int ticklen;
984 if (!s->new_session && s->session && s->session->tlsext_tick)
985 ticklen = s->session->tlsext_ticklen;
986 else if (s->session && s->tlsext_session_ticket &&
987 s->tlsext_session_ticket->data)
988 {
David Benjamin072c9532014-07-26 11:44:25 -0400989 s->session->tlsext_tick = BUF_memdup(
990 s->tlsext_session_ticket->data,
991 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700992 if (!s->session->tlsext_tick)
993 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -0400994 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700995 s->session->tlsext_ticklen = ticklen;
996 }
997 else
998 ticklen = 0;
999 if (ticklen == 0 && s->tlsext_session_ticket &&
1000 s->tlsext_session_ticket->data == NULL)
1001 goto skip_ext;
1002 /* Check for enough room 2 for extension type, 2 for len
1003 * rest for ticket
1004 */
1005 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1006 s2n(TLSEXT_TYPE_session_ticket,ret);
1007 s2n(ticklen,ret);
1008 if (ticklen)
1009 {
1010 memcpy(ret, s->session->tlsext_tick, ticklen);
1011 ret += ticklen;
1012 }
1013 }
1014 skip_ext:
1015
1016 if (SSL_USE_SIGALGS(s))
1017 {
1018 size_t salglen;
1019 const unsigned char *salg;
1020 salglen = tls12_get_psigalgs(s, &salg);
1021 if ((size_t)(limit - ret) < salglen + 6)
1022 return NULL;
1023 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1024 s2n(salglen + 2, ret);
1025 s2n(salglen, ret);
1026 memcpy(ret, salg, salglen);
1027 ret += salglen;
1028 }
1029
David Benjamin6c7aed02014-08-27 16:42:38 -04001030 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001031 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001032 /* The status_request extension is excessively extensible at
1033 * every layer. On the client, only support requesting OCSP
1034 * responses with an empty responder_id_list and no
1035 * extensions. */
1036 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langley95c29f32014-06-20 12:00:00 -07001038 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001039 s2n(1 + 2 + 2, ret);
1040 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001041 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001042 /* responder_id_list - empty */
1043 s2n(0, ret);
1044 /* request_extensions - empty */
1045 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001046 }
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langley95c29f32014-06-20 12:00:00 -07001048 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1049 {
1050 /* The client advertises an emtpy extension to indicate its
1051 * support for Next Protocol Negotiation */
1052 if (limit - ret - 4 < 0)
1053 return NULL;
1054 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1055 s2n(0,ret);
1056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001058 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1059 {
1060 /* The client advertises an empty extension to indicate its support for
1061 * certificate timestamps. */
1062 if (limit - ret - 4 < 0)
1063 return NULL;
1064 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1065 s2n(0,ret);
1066 }
1067
Adam Langley95c29f32014-06-20 12:00:00 -07001068 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1069 {
1070 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1071 return NULL;
1072 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1073 s2n(2 + s->alpn_client_proto_list_len,ret);
1074 s2n(s->alpn_client_proto_list_len,ret);
1075 memcpy(ret, s->alpn_client_proto_list,
1076 s->alpn_client_proto_list_len);
1077 ret += s->alpn_client_proto_list_len;
1078 }
1079
Adam Langley1258b6a2014-06-20 12:00:00 -07001080 if (s->tlsext_channel_id_enabled)
1081 {
1082 /* The client advertises an emtpy extension to indicate its
1083 * support for Channel ID. */
1084 if (limit - ret - 4 < 0)
1085 return NULL;
1086 if (s->ctx->tlsext_channel_id_enabled_new)
1087 s2n(TLSEXT_TYPE_channel_id_new,ret);
1088 else
1089 s2n(TLSEXT_TYPE_channel_id,ret);
1090 s2n(0,ret);
1091 }
1092
Adam Langley95c29f32014-06-20 12:00:00 -07001093 if(SSL_get_srtp_profiles(s))
1094 {
1095 int el;
1096
1097 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1098
Adam Langleyb0c235e2014-06-20 12:00:00 -07001099 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001100
1101 s2n(TLSEXT_TYPE_use_srtp,ret);
1102 s2n(el,ret);
1103
David Benjamin120a6742014-08-30 14:54:37 -04001104 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001105 {
1106 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1107 return NULL;
1108 }
1109 ret += el;
1110 }
1111
Adam Langleyc3174b72014-06-20 12:00:00 -07001112 if (using_ecc)
1113 {
1114 /* Add TLS extension ECPointFormats to the ClientHello message */
1115 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001116 const uint8_t *formats;
1117 const uint16_t *curves;
1118 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001119
David Benjamin072334d2014-07-13 16:24:27 -04001120 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001121
1122 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001123 if (formats_len > (size_t)lenmax) return NULL;
1124 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001125 {
1126 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1127 return NULL;
1128 }
1129
1130 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001131 s2n(formats_len + 1,ret);
1132 *(ret++) = (unsigned char)formats_len;
1133 memcpy(ret, formats, formats_len);
1134 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001135
1136 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001137 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001138
1139 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001140 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1141 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001142 {
1143 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1144 return NULL;
1145 }
1146
1147 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001148 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001149
1150 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1151 * elliptic_curve_list, but the examples use two bytes.
1152 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1153 * resolves this to two bytes.
1154 */
David Benjamin072334d2014-07-13 16:24:27 -04001155 s2n(curves_len * 2, ret);
1156 for (i = 0; i < curves_len; i++)
1157 {
1158 s2n(curves[i], ret);
1159 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001160 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001161
Adam Langley95c29f32014-06-20 12:00:00 -07001162#ifdef TLSEXT_TYPE_padding
1163 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001164 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001165 *
1166 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001167 * extensions it MUST always appear last. */
1168 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001169 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001170 header_len += ret - orig;
1171 if (header_len > 0xff && header_len < 0x200)
1172 {
1173 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001174 /* Extensions take at least four bytes to encode. Always
1175 * include least one byte of data if including the
1176 * extension. WebSphere Application Server 7.0 is
1177 * intolerant to the last extension being zero-length. */
1178 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001179 padding_len -= 4;
1180 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001181 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001182 if (limit - ret - 4 - (long)padding_len < 0)
1183 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyb0c235e2014-06-20 12:00:00 -07001185 s2n(TLSEXT_TYPE_padding, ret);
1186 s2n(padding_len, ret);
1187 memset(ret, 0, padding_len);
1188 ret += padding_len;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190 }
1191#endif
1192
Adam Langleyb0c235e2014-06-20 12:00:00 -07001193 if ((extdatalen = ret-orig-2)== 0)
1194 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyb0c235e2014-06-20 12:00:00 -07001196 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001197 return ret;
1198 }
1199
Adam Langleyb0c235e2014-06-20 12:00:00 -07001200unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001201 {
1202 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001203 unsigned char *orig = buf;
1204 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001205 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001206 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1207 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001208 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
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;
David Benjamina9ca90a2014-09-26 18:53:43 -04002179 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2180 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002181 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002182 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2183 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002184 }
2185 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002186
2187 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2188 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2189 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2190 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2191
Adam Langley95c29f32014-06-20 12:00:00 -07002192 switch (ret)
2193 {
2194 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002195 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002196 return -1;
2197
2198 case SSL_TLSEXT_ERR_ALERT_WARNING:
2199 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002200 return 1;
2201
2202 default:
2203 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002204 }
2205 }
2206
David Benjamin03973092014-06-24 23:27:17 -04002207int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002208 {
David Benjamin03973092014-06-24 23:27:17 -04002209 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002210 if (s->version < SSL3_VERSION)
2211 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002212
2213 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002214 {
David Benjamin03973092014-06-24 23:27:17 -04002215 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002216 return 0;
2217 }
2218
David Benjamin03973092014-06-24 23:27:17 -04002219 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002220 {
David Benjamin9d28c752014-07-05 00:43:48 -04002221 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002222 return 0;
2223 }
David Benjamin03973092014-06-24 23:27:17 -04002224
Adam Langley95c29f32014-06-20 12:00:00 -07002225 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002226 }
Adam Langley95c29f32014-06-20 12:00:00 -07002227
2228/* Since the server cache lookup is done early on in the processing of the
2229 * ClientHello, and other operations depend on the result, we need to handle
2230 * any TLS session ticket extension at the same time.
2231 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002232 * ctx: contains the early callback context, which is the result of a
2233 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002234 * ret: (output) on return, if a ticket was decrypted, then this is set to
2235 * point to the resulting session.
2236 *
2237 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2238 * ciphersuite, in which case we have no use for session tickets and one will
2239 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2240 *
2241 * Returns:
2242 * -1: fatal error, either from parsing or decrypting the ticket.
2243 * 0: no ticket was found (or was ignored, based on settings).
2244 * 1: a zero length extension was found, indicating that the client supports
2245 * session tickets but doesn't currently have one to offer.
2246 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2247 * couldn't be decrypted because of a non-fatal error.
2248 * 3: a ticket was successfully decrypted and *ret was set.
2249 *
2250 * Side effects:
2251 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2252 * a new session ticket to the client because the client indicated support
2253 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2254 * a session ticket or we couldn't use the one it gave us, or if
2255 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2256 * Otherwise, s->tlsext_ticket_expected is set to 0.
2257 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002258int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2259 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002260 {
Adam Langley95c29f32014-06-20 12:00:00 -07002261 *ret = NULL;
2262 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002263 const unsigned char *data;
2264 size_t len;
2265 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002266
2267 /* If tickets disabled behave as if no ticket present
2268 * to permit stateful resumption.
2269 */
2270 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2271 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002272 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002273 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002274 if (!SSL_early_callback_ctx_extension_get(
2275 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002276 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002277 return 0;
2278 }
2279 if (len == 0)
2280 {
2281 /* The client will accept a ticket but doesn't
2282 * currently have one. */
2283 s->tlsext_ticket_expected = 1;
2284 return 1;
2285 }
2286 if (s->tls_session_secret_cb)
2287 {
2288 /* Indicate that the ticket couldn't be
2289 * decrypted rather than generating the session
2290 * from ticket now, trigger abbreviated
2291 * handshake based on external mechanism to
2292 * calculate the master secret later. */
2293 return 2;
2294 }
2295 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2296 ctx->session_id_len, ret);
2297 switch (r)
2298 {
2299 case 2: /* ticket couldn't be decrypted */
2300 s->tlsext_ticket_expected = 1;
2301 return 2;
2302 case 3: /* ticket was decrypted */
2303 return r;
2304 case 4: /* ticket decrypted but need to renew */
2305 s->tlsext_ticket_expected = 1;
2306 return 3;
2307 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002308 return -1;
2309 }
Adam Langley95c29f32014-06-20 12:00:00 -07002310 }
2311
2312/* tls_decrypt_ticket attempts to decrypt a session ticket.
2313 *
2314 * etick: points to the body of the session ticket extension.
2315 * eticklen: the length of the session tickets extenion.
2316 * sess_id: points at the session ID.
2317 * sesslen: the length of the session ID.
2318 * psess: (output) on return, if a ticket was decrypted, then this is set to
2319 * point to the resulting session.
2320 *
2321 * Returns:
2322 * -1: fatal error, either from parsing or decrypting the ticket.
2323 * 2: the ticket couldn't be decrypted.
2324 * 3: a ticket was successfully decrypted and *psess was set.
2325 * 4: same as 3, but the ticket needs to be renewed.
2326 */
2327static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2328 const unsigned char *sess_id, int sesslen,
2329 SSL_SESSION **psess)
2330 {
2331 SSL_SESSION *sess;
2332 unsigned char *sdec;
2333 const unsigned char *p;
2334 int slen, mlen, renew_ticket = 0;
2335 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2336 HMAC_CTX hctx;
2337 EVP_CIPHER_CTX ctx;
2338 SSL_CTX *tctx = s->initial_ctx;
2339 /* Need at least keyname + iv + some encrypted data */
2340 if (eticklen < 48)
2341 return 2;
2342 /* Initialize session ticket encryption and HMAC contexts */
2343 HMAC_CTX_init(&hctx);
2344 EVP_CIPHER_CTX_init(&ctx);
2345 if (tctx->tlsext_ticket_key_cb)
2346 {
2347 unsigned char *nctick = (unsigned char *)etick;
2348 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2349 &ctx, &hctx, 0);
2350 if (rv < 0)
2351 return -1;
2352 if (rv == 0)
2353 return 2;
2354 if (rv == 2)
2355 renew_ticket = 1;
2356 }
2357 else
2358 {
2359 /* Check key name matches */
2360 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2361 return 2;
2362 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2363 tlsext_tick_md(), NULL);
2364 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2365 tctx->tlsext_tick_aes_key, etick + 16);
2366 }
2367 /* Attempt to process session ticket, first conduct sanity and
2368 * integrity checks on ticket.
2369 */
2370 mlen = HMAC_size(&hctx);
2371 if (mlen < 0)
2372 {
2373 EVP_CIPHER_CTX_cleanup(&ctx);
2374 return -1;
2375 }
2376 eticklen -= mlen;
2377 /* Check HMAC of encrypted ticket */
2378 HMAC_Update(&hctx, etick, eticklen);
2379 HMAC_Final(&hctx, tick_hmac, NULL);
2380 HMAC_CTX_cleanup(&hctx);
2381 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002382 {
2383 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002384 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002385 }
Adam Langley95c29f32014-06-20 12:00:00 -07002386 /* Attempt to decrypt session data */
2387 /* Move p after IV to start of encrypted ticket, update length */
2388 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2389 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2390 sdec = OPENSSL_malloc(eticklen);
2391 if (!sdec)
2392 {
2393 EVP_CIPHER_CTX_cleanup(&ctx);
2394 return -1;
2395 }
2396 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2397 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002398 {
2399 EVP_CIPHER_CTX_cleanup(&ctx);
2400 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002401 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002402 }
Adam Langley95c29f32014-06-20 12:00:00 -07002403 slen += mlen;
2404 EVP_CIPHER_CTX_cleanup(&ctx);
2405 p = sdec;
2406
2407 sess = d2i_SSL_SESSION(NULL, &p, slen);
2408 OPENSSL_free(sdec);
2409 if (sess)
2410 {
2411 /* The session ID, if non-empty, is used by some clients to
2412 * detect that the ticket has been accepted. So we copy it to
2413 * the session structure. If it is empty set length to zero
2414 * as required by standard.
2415 */
2416 if (sesslen)
2417 memcpy(sess->session_id, sess_id, sesslen);
2418 sess->session_id_length = sesslen;
2419 *psess = sess;
2420 if (renew_ticket)
2421 return 4;
2422 else
2423 return 3;
2424 }
2425 ERR_clear_error();
2426 /* For session parse failure, indicate that we need to send a new
2427 * ticket. */
2428 return 2;
2429 }
2430
2431/* Tables to translate from NIDs to TLS v1.2 ids */
2432
2433typedef struct
2434 {
2435 int nid;
2436 int id;
2437 } tls12_lookup;
2438
David Benjamincff64722014-08-19 19:54:46 -04002439static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002440 {NID_md5, TLSEXT_hash_md5},
2441 {NID_sha1, TLSEXT_hash_sha1},
2442 {NID_sha224, TLSEXT_hash_sha224},
2443 {NID_sha256, TLSEXT_hash_sha256},
2444 {NID_sha384, TLSEXT_hash_sha384},
2445 {NID_sha512, TLSEXT_hash_sha512}
2446};
2447
David Benjamincff64722014-08-19 19:54:46 -04002448static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002449 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002450 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2451};
2452
David Benjamincff64722014-08-19 19:54:46 -04002453static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002454 {
2455 size_t i;
2456 for (i = 0; i < tlen; i++)
2457 {
2458 if (table[i].nid == nid)
2459 return table[i].id;
2460 }
2461 return -1;
2462 }
2463
David Benjamincff64722014-08-19 19:54:46 -04002464static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002465 {
2466 size_t i;
2467 for (i = 0; i < tlen; i++)
2468 {
2469 if ((table[i].id) == id)
2470 return table[i].nid;
2471 }
2472 return NID_undef;
2473 }
2474
2475int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2476 {
2477 int sig_id, md_id;
2478 if (!md)
2479 return 0;
2480 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2481 sizeof(tls12_md)/sizeof(tls12_lookup));
2482 if (md_id == -1)
2483 return 0;
2484 sig_id = tls12_get_sigid(pk);
2485 if (sig_id == -1)
2486 return 0;
2487 p[0] = (unsigned char)md_id;
2488 p[1] = (unsigned char)sig_id;
2489 return 1;
2490 }
2491
2492int tls12_get_sigid(const EVP_PKEY *pk)
2493 {
2494 return tls12_find_id(pk->type, tls12_sig,
2495 sizeof(tls12_sig)/sizeof(tls12_lookup));
2496 }
2497
2498const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2499 {
2500 switch(hash_alg)
2501 {
Adam Langley95c29f32014-06-20 12:00:00 -07002502 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002503 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002504 case TLSEXT_hash_sha1:
2505 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002506 case TLSEXT_hash_sha224:
2507 return EVP_sha224();
2508
2509 case TLSEXT_hash_sha256:
2510 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002511 case TLSEXT_hash_sha384:
2512 return EVP_sha384();
2513
2514 case TLSEXT_hash_sha512:
2515 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002516 default:
2517 return NULL;
2518
2519 }
2520 }
2521
2522static int tls12_get_pkey_idx(unsigned char sig_alg)
2523 {
2524 switch(sig_alg)
2525 {
Adam Langley95c29f32014-06-20 12:00:00 -07002526 case TLSEXT_signature_rsa:
2527 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002528 case TLSEXT_signature_ecdsa:
2529 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002530 }
2531 return -1;
2532 }
2533
2534/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2535static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2536 int *psignhash_nid, const unsigned char *data)
2537 {
2538 int sign_nid = 0, hash_nid = 0;
2539 if (!phash_nid && !psign_nid && !psignhash_nid)
2540 return;
2541 if (phash_nid || psignhash_nid)
2542 {
2543 hash_nid = tls12_find_nid(data[0], tls12_md,
2544 sizeof(tls12_md)/sizeof(tls12_lookup));
2545 if (phash_nid)
2546 *phash_nid = hash_nid;
2547 }
2548 if (psign_nid || psignhash_nid)
2549 {
2550 sign_nid = tls12_find_nid(data[1], tls12_sig,
2551 sizeof(tls12_sig)/sizeof(tls12_lookup));
2552 if (psign_nid)
2553 *psign_nid = sign_nid;
2554 }
2555 if (psignhash_nid)
2556 {
2557 if (sign_nid && hash_nid)
2558 OBJ_find_sigid_by_algs(psignhash_nid,
2559 hash_nid, sign_nid);
2560 else
2561 *psignhash_nid = NID_undef;
2562 }
2563 }
2564/* Given preference and allowed sigalgs set shared sigalgs */
2565static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2566 const unsigned char *pref, size_t preflen,
2567 const unsigned char *allow, size_t allowlen)
2568 {
2569 const unsigned char *ptmp, *atmp;
2570 size_t i, j, nmatch = 0;
2571 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2572 {
2573 /* Skip disabled hashes or signature algorithms */
2574 if (tls12_get_hash(ptmp[0]) == NULL)
2575 continue;
2576 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2577 continue;
2578 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2579 {
2580 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2581 {
2582 nmatch++;
2583 if (shsig)
2584 {
2585 shsig->rhash = ptmp[0];
2586 shsig->rsign = ptmp[1];
2587 tls1_lookup_sigalg(&shsig->hash_nid,
2588 &shsig->sign_nid,
2589 &shsig->signandhash_nid,
2590 ptmp);
2591 shsig++;
2592 }
2593 break;
2594 }
2595 }
2596 }
2597 return nmatch;
2598 }
2599
2600/* Set shared signature algorithms for SSL structures */
2601static int tls1_set_shared_sigalgs(SSL *s)
2602 {
2603 const unsigned char *pref, *allow, *conf;
2604 size_t preflen, allowlen, conflen;
2605 size_t nmatch;
2606 TLS_SIGALGS *salgs = NULL;
2607 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002608 if (c->shared_sigalgs)
2609 {
2610 OPENSSL_free(c->shared_sigalgs);
2611 c->shared_sigalgs = NULL;
2612 }
Adam Langley95c29f32014-06-20 12:00:00 -07002613 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002614 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002615 {
2616 conf = c->client_sigalgs;
2617 conflen = c->client_sigalgslen;
2618 }
David Benjamin335d10d2014-08-06 19:56:33 -04002619 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002620 {
2621 conf = c->conf_sigalgs;
2622 conflen = c->conf_sigalgslen;
2623 }
2624 else
2625 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002626 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002627 {
2628 pref = conf;
2629 preflen = conflen;
2630 allow = c->peer_sigalgs;
2631 allowlen = c->peer_sigalgslen;
2632 }
2633 else
2634 {
2635 allow = conf;
2636 allowlen = conflen;
2637 pref = c->peer_sigalgs;
2638 preflen = c->peer_sigalgslen;
2639 }
2640 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2641 if (!nmatch)
2642 return 1;
2643 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2644 if (!salgs)
2645 return 0;
2646 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2647 c->shared_sigalgs = salgs;
2648 c->shared_sigalgslen = nmatch;
2649 return 1;
2650 }
2651
2652
2653/* Set preferred digest for each key type */
2654
David Benjamincd996942014-07-20 16:23:51 -04002655int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002656 {
2657 int idx;
2658 size_t i;
2659 const EVP_MD *md;
2660 CERT *c = s->cert;
2661 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002662
Adam Langley95c29f32014-06-20 12:00:00 -07002663 /* Extension ignored for inappropriate versions */
2664 if (!SSL_USE_SIGALGS(s))
2665 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002666 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002667 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002668 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002669 /* Should never happen */
2670 if (!c)
2671 return 0;
2672
David Benjamincd996942014-07-20 16:23:51 -04002673 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002674 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002675
2676 tls1_set_shared_sigalgs(s);
2677
Adam Langley95c29f32014-06-20 12:00:00 -07002678 for (i = 0, sigptr = c->shared_sigalgs;
2679 i < c->shared_sigalgslen; i++, sigptr++)
2680 {
2681 idx = tls12_get_pkey_idx(sigptr->rsign);
2682 if (idx > 0 && c->pkeys[idx].digest == NULL)
2683 {
2684 md = tls12_get_hash(sigptr->rhash);
2685 c->pkeys[idx].digest = md;
2686 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2687 if (idx == SSL_PKEY_RSA_SIGN)
2688 {
2689 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2690 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2691 }
2692 }
2693
2694 }
2695 /* In strict mode leave unset digests as NULL to indicate we can't
2696 * use the certificate for signing.
2697 */
2698 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
2699 {
2700 /* Set any remaining keys to default values. NOTE: if alg is
2701 * not supported it stays as NULL.
2702 */
Adam Langley95c29f32014-06-20 12:00:00 -07002703 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2704 {
2705 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2706 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2707 }
Adam Langley95c29f32014-06-20 12:00:00 -07002708 if (!c->pkeys[SSL_PKEY_ECC].digest)
2709 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002710 }
2711 return 1;
2712 }
2713
2714
2715int SSL_get_sigalgs(SSL *s, int idx,
2716 int *psign, int *phash, int *psignhash,
2717 unsigned char *rsig, unsigned char *rhash)
2718 {
2719 const unsigned char *psig = s->cert->peer_sigalgs;
2720 if (psig == NULL)
2721 return 0;
2722 if (idx >= 0)
2723 {
2724 idx <<= 1;
2725 if (idx >= (int)s->cert->peer_sigalgslen)
2726 return 0;
2727 psig += idx;
2728 if (rhash)
2729 *rhash = psig[0];
2730 if (rsig)
2731 *rsig = psig[1];
2732 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2733 }
2734 return s->cert->peer_sigalgslen / 2;
2735 }
2736
2737int SSL_get_shared_sigalgs(SSL *s, int idx,
2738 int *psign, int *phash, int *psignhash,
2739 unsigned char *rsig, unsigned char *rhash)
2740 {
2741 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2742 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2743 return 0;
2744 shsigalgs += idx;
2745 if (phash)
2746 *phash = shsigalgs->hash_nid;
2747 if (psign)
2748 *psign = shsigalgs->sign_nid;
2749 if (psignhash)
2750 *psignhash = shsigalgs->signandhash_nid;
2751 if (rsig)
2752 *rsig = shsigalgs->rsign;
2753 if (rhash)
2754 *rhash = shsigalgs->rhash;
2755 return s->cert->shared_sigalgslen;
2756 }
2757
Adam Langley1258b6a2014-06-20 12:00:00 -07002758/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2759 * SSL connection and writes it to |md|. */
2760int
2761tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2762 {
2763 EVP_MD_CTX ctx;
2764 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2765 unsigned temp_digest_len;
2766 int i;
2767 static const char kClientIDMagic[] = "TLS Channel ID signature";
2768
2769 if (s->s3->handshake_buffer)
2770 if (!ssl3_digest_cached_records(s))
2771 return 0;
2772
2773 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2774
2775 if (s->hit && s->s3->tlsext_channel_id_new)
2776 {
2777 static const char kResumptionMagic[] = "Resumption";
2778 EVP_DigestUpdate(md, kResumptionMagic,
2779 sizeof(kResumptionMagic));
2780 if (s->session->original_handshake_hash_len == 0)
2781 return 0;
2782 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2783 s->session->original_handshake_hash_len);
2784 }
2785
2786 EVP_MD_CTX_init(&ctx);
2787 for (i = 0; i < SSL_MAX_DIGEST; i++)
2788 {
2789 if (s->s3->handshake_dgst[i] == NULL)
2790 continue;
2791 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2792 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2793 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2794 }
2795 EVP_MD_CTX_cleanup(&ctx);
2796
2797 return 1;
2798 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002799
2800/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2801 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2802int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2803 {
2804 int digest_len;
2805 /* This function should never be called for a resumed session because
2806 * the handshake hashes that we wish to record are for the original,
2807 * full handshake. */
2808 if (s->hit)
2809 return -1;
2810 /* It only makes sense to call this function if Channel IDs have been
2811 * negotiated. */
2812 if (!s->s3->tlsext_channel_id_new)
2813 return -1;
2814
2815 digest_len = tls1_handshake_digest(
2816 s, s->session->original_handshake_hash,
2817 sizeof(s->session->original_handshake_hash));
2818 if (digest_len < 0)
2819 return -1;
2820
2821 s->session->original_handshake_hash_len = digest_len;
2822
2823 return 1;
2824 }
2825
Adam Langley95c29f32014-06-20 12:00:00 -07002826int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2827 {
2828 unsigned char *sigalgs, *sptr;
2829 int rhash, rsign;
2830 size_t i;
2831 if (salglen & 1)
2832 return 0;
2833 sigalgs = OPENSSL_malloc(salglen);
2834 if (sigalgs == NULL)
2835 return 0;
2836 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2837 {
2838 rhash = tls12_find_id(*psig_nids++, tls12_md,
2839 sizeof(tls12_md)/sizeof(tls12_lookup));
2840 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2841 sizeof(tls12_sig)/sizeof(tls12_lookup));
2842
2843 if (rhash == -1 || rsign == -1)
2844 goto err;
2845 *sptr++ = rhash;
2846 *sptr++ = rsign;
2847 }
2848
2849 if (client)
2850 {
2851 if (c->client_sigalgs)
2852 OPENSSL_free(c->client_sigalgs);
2853 c->client_sigalgs = sigalgs;
2854 c->client_sigalgslen = salglen;
2855 }
2856 else
2857 {
2858 if (c->conf_sigalgs)
2859 OPENSSL_free(c->conf_sigalgs);
2860 c->conf_sigalgs = sigalgs;
2861 c->conf_sigalgslen = salglen;
2862 }
2863
2864 return 1;
2865
2866 err:
2867 OPENSSL_free(sigalgs);
2868 return 0;
2869 }
2870
2871static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
2872 {
2873 int sig_nid;
2874 size_t i;
2875 if (default_nid == -1)
2876 return 1;
2877 sig_nid = X509_get_signature_nid(x);
2878 if (default_nid)
2879 return sig_nid == default_nid ? 1 : 0;
2880 for (i = 0; i < c->shared_sigalgslen; i++)
2881 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
2882 return 1;
2883 return 0;
2884 }
2885/* Check to see if a certificate issuer name matches list of CA names */
2886static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2887 {
2888 X509_NAME *nm;
David Benjaminfb3ff2c2014-09-30 21:00:38 -04002889 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002890 nm = X509_get_issuer_name(x);
2891 for (i = 0; i < sk_X509_NAME_num(names); i++)
2892 {
2893 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2894 return 1;
2895 }
2896 return 0;
2897 }
2898
2899/* Check certificate chain is consistent with TLS extensions and is
2900 * usable by server. This servers two purposes: it allows users to
2901 * check chains before passing them to the server and it allows the
2902 * server to check chains before attempting to use them.
2903 */
2904
2905/* Flags which need to be set for a certificate when stict mode not set */
2906
2907#define CERT_PKEY_VALID_FLAGS \
2908 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2909/* Strict mode flags */
2910#define CERT_PKEY_STRICT_FLAGS \
2911 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2912 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2913
2914int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2915 int idx)
2916 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -04002917 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002918 int rv = 0;
2919 int check_flags = 0, strict_mode;
2920 CERT_PKEY *cpk = NULL;
2921 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002922 /* idx == -1 means checking server chains */
2923 if (idx != -1)
2924 {
2925 /* idx == -2 means checking client certificate chains */
2926 if (idx == -2)
2927 {
2928 cpk = c->key;
2929 idx = cpk - c->pkeys;
2930 }
2931 else
2932 cpk = c->pkeys + idx;
2933 x = cpk->x509;
2934 pk = cpk->privatekey;
2935 chain = cpk->chain;
2936 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2937 /* If no cert or key, forget it */
2938 if (!x || !pk)
2939 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -07002940 }
2941 else
2942 {
2943 if (!x || !pk)
2944 goto end;
2945 idx = ssl_cert_type(x, pk);
2946 if (idx == -1)
2947 goto end;
2948 cpk = c->pkeys + idx;
2949 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
2950 check_flags = CERT_PKEY_STRICT_FLAGS;
2951 else
2952 check_flags = CERT_PKEY_VALID_FLAGS;
2953 strict_mode = 1;
2954 }
2955
Adam Langley95c29f32014-06-20 12:00:00 -07002956 /* Check all signature algorithms are consistent with
2957 * signature algorithms extension if TLS 1.2 or later
2958 * and strict mode.
2959 */
2960 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
2961 {
2962 int default_nid;
2963 unsigned char rsign = 0;
2964 if (c->peer_sigalgs)
2965 default_nid = 0;
2966 /* If no sigalgs extension use defaults from RFC5246 */
2967 else
2968 {
2969 switch(idx)
2970 {
2971 case SSL_PKEY_RSA_ENC:
2972 case SSL_PKEY_RSA_SIGN:
Adam Langley95c29f32014-06-20 12:00:00 -07002973 rsign = TLSEXT_signature_rsa;
2974 default_nid = NID_sha1WithRSAEncryption;
2975 break;
2976
Adam Langley95c29f32014-06-20 12:00:00 -07002977 case SSL_PKEY_ECC:
2978 rsign = TLSEXT_signature_ecdsa;
2979 default_nid = NID_ecdsa_with_SHA1;
2980 break;
2981
2982 default:
2983 default_nid = -1;
2984 break;
2985 }
2986 }
2987 /* If peer sent no signature algorithms extension and we
2988 * have set preferred signature algorithms check we support
2989 * sha1.
2990 */
2991 if (default_nid > 0 && c->conf_sigalgs)
2992 {
2993 size_t j;
2994 const unsigned char *p = c->conf_sigalgs;
2995 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
2996 {
2997 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
2998 break;
2999 }
3000 if (j == c->conf_sigalgslen)
3001 {
3002 if (check_flags)
3003 goto skip_sigs;
3004 else
3005 goto end;
3006 }
3007 }
3008 /* Check signature algorithm of each cert in chain */
3009 if (!tls1_check_sig_alg(c, x, default_nid))
3010 {
3011 if (!check_flags) goto end;
3012 }
3013 else
3014 rv |= CERT_PKEY_EE_SIGNATURE;
3015 rv |= CERT_PKEY_CA_SIGNATURE;
3016 for (i = 0; i < sk_X509_num(chain); i++)
3017 {
3018 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3019 default_nid))
3020 {
3021 if (check_flags)
3022 {
3023 rv &= ~CERT_PKEY_CA_SIGNATURE;
3024 break;
3025 }
3026 else
3027 goto end;
3028 }
3029 }
3030 }
3031 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3032 else if(check_flags)
3033 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3034 skip_sigs:
3035 /* Check cert parameters are consistent */
3036 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3037 rv |= CERT_PKEY_EE_PARAM;
3038 else if (!check_flags)
3039 goto end;
3040 if (!s->server)
3041 rv |= CERT_PKEY_CA_PARAM;
3042 /* In strict mode check rest of chain too */
3043 else if (strict_mode)
3044 {
3045 rv |= CERT_PKEY_CA_PARAM;
3046 for (i = 0; i < sk_X509_num(chain); i++)
3047 {
3048 X509 *ca = sk_X509_value(chain, i);
3049 if (!tls1_check_cert_param(s, ca, 0))
3050 {
3051 if (check_flags)
3052 {
3053 rv &= ~CERT_PKEY_CA_PARAM;
3054 break;
3055 }
3056 else
3057 goto end;
3058 }
3059 }
3060 }
3061 if (!s->server && strict_mode)
3062 {
3063 STACK_OF(X509_NAME) *ca_dn;
David Benjamin676d1e72014-07-08 14:34:10 -04003064 uint8_t check_type = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003065 switch (pk->type)
3066 {
3067 case EVP_PKEY_RSA:
3068 check_type = TLS_CT_RSA_SIGN;
3069 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003070 case EVP_PKEY_EC:
3071 check_type = TLS_CT_ECDSA_SIGN;
3072 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003073 }
3074 if (check_type)
3075 {
David Benjamin676d1e72014-07-08 14:34:10 -04003076 if (s->s3->tmp.certificate_types &&
3077 memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07003078 {
Adam Langley95c29f32014-06-20 12:00:00 -07003079 rv |= CERT_PKEY_CERT_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -07003080 }
3081 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3082 goto end;
3083 }
3084 else
3085 rv |= CERT_PKEY_CERT_TYPE;
3086
3087
3088 ca_dn = s->s3->tmp.ca_names;
3089
3090 if (!sk_X509_NAME_num(ca_dn))
3091 rv |= CERT_PKEY_ISSUER_NAME;
3092
3093 if (!(rv & CERT_PKEY_ISSUER_NAME))
3094 {
3095 if (ssl_check_ca_name(ca_dn, x))
3096 rv |= CERT_PKEY_ISSUER_NAME;
3097 }
3098 if (!(rv & CERT_PKEY_ISSUER_NAME))
3099 {
3100 for (i = 0; i < sk_X509_num(chain); i++)
3101 {
3102 X509 *xtmp = sk_X509_value(chain, i);
3103 if (ssl_check_ca_name(ca_dn, xtmp))
3104 {
3105 rv |= CERT_PKEY_ISSUER_NAME;
3106 break;
3107 }
3108 }
3109 }
3110 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3111 goto end;
3112 }
3113 else
3114 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3115
3116 if (!check_flags || (rv & check_flags) == check_flags)
3117 rv |= CERT_PKEY_VALID;
3118
3119 end:
3120
3121 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3122 {
3123 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3124 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3125 else if (cpk->digest)
3126 rv |= CERT_PKEY_SIGN;
3127 }
3128 else
3129 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3130
3131 /* When checking a CERT_PKEY structure all flags are irrelevant
3132 * if the chain is invalid.
3133 */
3134 if (!check_flags)
3135 {
3136 if (rv & CERT_PKEY_VALID)
3137 cpk->valid_flags = rv;
3138 else
3139 {
3140 /* Preserve explicit sign flag, clear rest */
3141 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
3142 return 0;
3143 }
3144 }
3145 return rv;
3146 }
3147
3148/* Set validity of certificates in an SSL structure */
3149void tls1_set_cert_validity(SSL *s)
3150 {
3151 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3152 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
Adam Langley95c29f32014-06-20 12:00:00 -07003153 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3154 }
3155/* User level utiity function to check a chain is suitable */
3156int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3157 {
3158 return tls1_check_chain(s, x, pk, chain, -1);
3159 }
3160