blob: 799f24021602d03d8be79dcf0b38b0a631fde671 [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 Langleyfcf25832014-12-18 17:42:32 -0800121
122
123static int tls_decrypt_ticket(SSL *s, const uint8_t *tick, int ticklen,
124 const uint8_t *sess_id, int sesslen,
125 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400126static int ssl_check_clienthello_tlsext(SSL *s);
127static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjamin338fcaf2014-12-11 01:20:52 -0500129const SSL3_ENC_METHOD TLSv1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800130 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500131 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800132 tls1_setup_key_block,
133 tls1_generate_master_secret,
134 tls1_change_cipher_state,
135 tls1_final_finish_mac,
136 TLS1_FINISH_MAC_LENGTH,
137 tls1_cert_verify_mac,
138 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
140 tls1_alert_code,
141 tls1_export_keying_material,
142 0,
143 SSL3_HM_HEADER_LENGTH,
144 ssl3_set_handshake_header,
145 ssl3_handshake_write,
146};
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjamin338fcaf2014-12-11 01:20:52 -0500148const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500150 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 tls1_setup_key_block,
152 tls1_generate_master_secret,
153 tls1_change_cipher_state,
154 tls1_final_finish_mac,
155 TLS1_FINISH_MAC_LENGTH,
156 tls1_cert_verify_mac,
157 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
158 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
159 tls1_alert_code,
160 tls1_export_keying_material,
161 SSL_ENC_FLAG_EXPLICIT_IV,
162 SSL3_HM_HEADER_LENGTH,
163 ssl3_set_handshake_header,
164 ssl3_handshake_write,
165};
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin338fcaf2014-12-11 01:20:52 -0500167const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500169 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 tls1_setup_key_block,
171 tls1_generate_master_secret,
172 tls1_change_cipher_state,
173 tls1_final_finish_mac,
174 TLS1_FINISH_MAC_LENGTH,
175 tls1_cert_verify_mac,
176 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
177 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
178 tls1_alert_code,
179 tls1_export_keying_material,
180 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
181 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
182 SSL3_HM_HEADER_LENGTH,
183 ssl3_set_handshake_header,
184 ssl3_handshake_write,
185};
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187static int compare_uint16_t(const void *p1, const void *p2) {
188 uint16_t u1 = *((const uint16_t *)p1);
189 uint16_t u2 = *((const uint16_t *)p2);
190 if (u1 < u2) {
191 return -1;
192 } else if (u1 > u2) {
193 return 1;
194 } else {
195 return 0;
196 }
197}
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
200 * more than one extension of the same type in a ClientHello or ServerHello.
201 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400202 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800203static int tls1_check_duplicate_extensions(const CBS *cbs) {
204 CBS extensions = *cbs;
205 size_t num_extensions = 0, i = 0;
206 uint16_t *extension_types = NULL;
207 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 /* First pass: count the extensions. */
210 while (CBS_len(&extensions) > 0) {
211 uint16_t type;
212 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (!CBS_get_u16(&extensions, &type) ||
215 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
216 goto done;
217 }
David Benjamin35a7a442014-07-05 00:23:20 -0400218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 num_extensions++;
220 }
David Benjamin35a7a442014-07-05 00:23:20 -0400221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 if (num_extensions == 0) {
223 return 1;
224 }
David Benjamin9a373592014-07-25 04:27:53 -0400225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 extension_types =
227 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
228 if (extension_types == NULL) {
229 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
230 ERR_R_MALLOC_FAILURE);
231 goto done;
232 }
David Benjamin35a7a442014-07-05 00:23:20 -0400233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 /* Second pass: gather the extension types. */
235 extensions = *cbs;
236 for (i = 0; i < num_extensions; i++) {
237 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
240 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
241 /* This should not happen. */
242 goto done;
243 }
244 }
245 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* Sort the extensions and make sure there are no duplicates. */
248 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
249 for (i = 1; i < num_extensions; i++) {
250 if (extension_types[i - 1] == extension_types[i]) {
251 goto done;
252 }
253 }
David Benjamin35a7a442014-07-05 00:23:20 -0400254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 ret = 1;
256
David Benjamin35a7a442014-07-05 00:23:20 -0400257done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (extension_types)
259 OPENSSL_free(extension_types);
260 return ret;
261}
David Benjamin35a7a442014-07-05 00:23:20 -0400262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
264 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 if (/* Skip client version. */
269 !CBS_skip(&client_hello, 2) ||
270 /* Skip client nonce. */
271 !CBS_skip(&client_hello, 32) ||
272 /* Extract session_id. */
273 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
274 return 0;
275 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 ctx->session_id = CBS_data(&session_id);
278 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 /* Skip past DTLS cookie */
281 if (SSL_IS_DTLS(ctx->ssl)) {
282 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
285 return 0;
286 }
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 /* Extract cipher_suites. */
290 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
291 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
292 return 0;
293 }
294 ctx->cipher_suites = CBS_data(&cipher_suites);
295 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700296
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 /* Extract compression_methods. */
298 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
299 CBS_len(&compression_methods) < 1) {
300 return 0;
301 }
302 ctx->compression_methods = CBS_data(&compression_methods);
303 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 /* If the ClientHello ends here then it's valid, but doesn't have any
306 * extensions. (E.g. SSLv3.) */
307 if (CBS_len(&client_hello) == 0) {
308 ctx->extensions = NULL;
309 ctx->extensions_len = 0;
310 return 1;
311 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 /* Extract extensions and check it is valid. */
314 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
315 !tls1_check_duplicate_extensions(&extensions) ||
316 CBS_len(&client_hello) != 0) {
317 return 0;
318 }
319 ctx->extensions = CBS_data(&extensions);
320 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 return 1;
323}
Adam Langleydc9b1412014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325char SSL_early_callback_ctx_extension_get(
326 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
327 const uint8_t **out_data, size_t *out_len) {
328 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 while (CBS_len(&extensions) != 0) {
333 uint16_t type;
334 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400335
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 /* Decode the next extension. */
337 if (!CBS_get_u16(&extensions, &type) ||
338 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
339 return 0;
340 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 if (type == extension_type) {
343 *out_data = CBS_data(&extension);
344 *out_len = CBS_len(&extension);
345 return 1;
346 }
347 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700348
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 return 0;
350}
Adam Langley95c29f32014-06-20 12:00:00 -0700351
David Benjamine518f652014-10-13 16:12:45 -0400352/* ECC curves from RFC4492 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800353static const int nid_list[] = {
354 NID_sect163k1, /* sect163k1 (1) */
355 NID_sect163r1, /* sect163r1 (2) */
356 NID_sect163r2, /* sect163r2 (3) */
357 NID_sect193r1, /* sect193r1 (4) */
358 NID_sect193r2, /* sect193r2 (5) */
359 NID_sect233k1, /* sect233k1 (6) */
360 NID_sect233r1, /* sect233r1 (7) */
361 NID_sect239k1, /* sect239k1 (8) */
362 NID_sect283k1, /* sect283k1 (9) */
363 NID_sect283r1, /* sect283r1 (10) */
364 NID_sect409k1, /* sect409k1 (11) */
365 NID_sect409r1, /* sect409r1 (12) */
366 NID_sect571k1, /* sect571k1 (13) */
367 NID_sect571r1, /* sect571r1 (14) */
368 NID_secp160k1, /* secp160k1 (15) */
369 NID_secp160r1, /* secp160r1 (16) */
370 NID_secp160r2, /* secp160r2 (17) */
371 NID_secp192k1, /* secp192k1 (18) */
372 NID_X9_62_prime192v1, /* secp192r1 (19) */
373 NID_secp224k1, /* secp224k1 (20) */
374 NID_secp224r1, /* secp224r1 (21) */
375 NID_secp256k1, /* secp256k1 (22) */
376 NID_X9_62_prime256v1, /* secp256r1 (23) */
377 NID_secp384r1, /* secp384r1 (24) */
378 NID_secp521r1, /* secp521r1 (25) */
379 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
380 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
381 NID_brainpoolP512r1 /* brainpool512r1 (28) */
382};
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384static const uint8_t ecformats_default[] = {
385 TLSEXT_ECPOINTFORMAT_uncompressed,
386};
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388static const uint16_t eccurves_default[] = {
389 23, /* secp256r1 (23) */
390 24, /* secp384r1 (24) */
391 25, /* secp521r1 (25) */
392};
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394int tls1_ec_curve_id2nid(uint16_t curve_id) {
395 if (curve_id < 1 || curve_id > sizeof(nid_list) / sizeof(nid_list[0])) {
396 return OBJ_undef;
397 }
398 return nid_list[curve_id - 1];
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401uint16_t tls1_ec_nid2curve_id(int nid) {
402 size_t i;
403 for (i = 0; i < sizeof(nid_list) / sizeof(nid_list[0]); i++) {
404 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
405 if (nid == nid_list[i]) {
406 return i + 1;
407 }
408 }
David Benjamin072334d2014-07-13 16:24:27 -0400409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0 will
411 * never be allocated. */
412 return 0;
413}
414
415/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
416 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
417 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400418static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 const uint16_t **out_curve_ids,
420 size_t *out_curve_ids_len) {
421 if (get_peer_curves) {
422 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
423 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
424 return;
425 }
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 *out_curve_ids = s->tlsext_ellipticcurvelist;
428 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
429 if (!*out_curve_ids) {
430 *out_curve_ids = eccurves_default;
431 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
432 }
433}
David Benjamined439582014-07-14 19:13:02 -0400434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
436 uint8_t curve_type;
437 uint16_t curve_id;
438 const uint16_t *curves;
439 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400440
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 /* Only support named curves. */
442 if (!CBS_get_u8(cbs, &curve_type) ||
443 curve_type != NAMED_CURVE_TYPE ||
444 !CBS_get_u16(cbs, &curve_id)) {
445 return 0;
446 }
David Benjamined439582014-07-14 19:13:02 -0400447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 tls1_get_curvelist(s, 0, &curves, &curves_len);
449 for (i = 0; i < curves_len; i++) {
450 if (curve_id == curves[i]) {
451 *out_curve_id = curve_id;
452 return 1;
453 }
454 }
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 return 0;
457}
David Benjamin072334d2014-07-13 16:24:27 -0400458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459int tls1_get_shared_curve(SSL *s) {
460 const uint16_t *pref, *supp;
461 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 /* Can't do anything on client side */
464 if (s->server == 0) {
465 return NID_undef;
466 }
467
468 /* Return first preference shared curve */
469 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp,
470 &supplen);
471 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
472 &preflen);
473
474 for (i = 0; i < preflen; i++) {
475 for (j = 0; j < supplen; j++) {
476 if (pref[i] == supp[j]) {
477 return tls1_ec_curve_id2nid(pref[i]);
478 }
479 }
480 }
481
482 return NID_undef;
483}
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin072334d2014-07-13 16:24:27 -0400485/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
486 *
487 * (a) 0 is not a valid curve ID.
488 *
489 * (b) The largest curve ID is 31.
490 *
491 * Those implementations must be revised before adding support for curve IDs
492 * that break these assumptions. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800493OPENSSL_COMPILE_ASSERT((sizeof(nid_list) / sizeof(nid_list[0])) < 32,
494 small_curve_ids);
David Benjamin072334d2014-07-13 16:24:27 -0400495
496int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 const int *curves, size_t ncurves) {
498 uint16_t *curve_ids;
499 size_t i;
500
501 /* Bitmap of curves included to detect duplicates: only works
502 * while curve ids < 32. */
503 uint32_t dup_list = 0;
504 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
505 if (curve_ids == NULL) {
506 return 0;
507 }
508
509 for (i = 0; i < ncurves; i++) {
510 uint32_t idmask;
511 uint16_t id;
512 id = tls1_ec_nid2curve_id(curves[i]);
513 idmask = ((uint32_t)1) << id;
514 if (!id || (dup_list & idmask)) {
515 OPENSSL_free(curve_ids);
516 return 0;
517 }
518 dup_list |= idmask;
519 curve_ids[i] = id;
520 }
521
522 if (*out_curve_ids) {
523 OPENSSL_free(*out_curve_ids);
524 }
525 *out_curve_ids = curve_ids;
526 *out_curve_ids_len = ncurves;
527
528 return 1;
529}
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin072334d2014-07-13 16:24:27 -0400531/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
532 * TLS curve ID and point format, respectively, for |ec|. It returns one on
533 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800534static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
535 uint8_t *out_comp_id, EC_KEY *ec) {
536 int nid;
537 uint16_t id;
538 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 if (ec == NULL) {
541 return 0;
542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 grp = EC_KEY_get0_group(ec);
545 if (grp == NULL) {
546 return 0;
547 }
David Benjamin072334d2014-07-13 16:24:27 -0400548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 /* Determine curve ID */
550 nid = EC_GROUP_get_curve_name(grp);
551 id = tls1_ec_nid2curve_id(nid);
552 if (!id) {
553 return 0;
554 }
David Benjamin072334d2014-07-13 16:24:27 -0400555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
557 *out_curve_id = id;
558
559 if (out_comp_id) {
560 if (EC_KEY_get0_public_key(ec) == NULL) {
561 return 0;
562 }
563 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
564 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
565 } else {
566 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
567 }
568 }
569
570 return 1;
571}
David Benjamin072334d2014-07-13 16:24:27 -0400572
David Benjamin42e9a772014-09-02 23:18:44 -0400573/* tls1_check_point_format returns one if |comp_id| is consistent with the
574 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800575static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
576 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
577 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
578 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 /* If point formats extension present check it, otherwise everything is
581 * supported (see RFC4492). */
582 if (p == NULL) {
583 return 1;
584 }
David Benjamin42e9a772014-09-02 23:18:44 -0400585
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 for (i = 0; i < plen; i++) {
587 if (comp_id == p[i]) {
588 return 1;
589 }
590 }
David Benjamin42e9a772014-09-02 23:18:44 -0400591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 return 0;
593}
594
595/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
596 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400597 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800598static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
599 const uint16_t *curves;
600 size_t curves_len, i, j;
David Benjamin42e9a772014-09-02 23:18:44 -0400601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 /* Check against our list, then the peer's list. */
603 for (j = 0; j <= 1; j++) {
604 tls1_get_curvelist(s, j, &curves, &curves_len);
605 for (i = 0; i < curves_len; i++) {
606 if (curves[i] == curve_id) {
607 break;
608 }
609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 if (i == curves_len) {
612 return 0;
613 }
Adam Langley95c29f32014-06-20 12:00:00 -0700614
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 /* Servers do not present a preference list so, if we are a client, only
616 * check our list. */
617 if (!s->server) {
618 return 1;
619 }
620 }
David Benjamin033e5f42014-11-13 18:47:41 -0500621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 return 1;
623}
David Benjamin033e5f42014-11-13 18:47:41 -0500624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
626 size_t *pformatslen) {
627 /* If we have a custom point format list use it otherwise use default */
628 if (s->tlsext_ecpointformatlist) {
629 *pformats = s->tlsext_ecpointformatlist;
630 *pformatslen = s->tlsext_ecpointformatlist_length;
631 } else {
632 *pformats = ecformats_default;
633 *pformatslen = sizeof(ecformats_default);
634 }
635}
636
637int tls1_check_ec_cert(SSL *s, X509 *x) {
638 int ret = 0;
639 EVP_PKEY *pkey = X509_get_pubkey(x);
640 uint16_t curve_id;
641 uint8_t comp_id;
642
643 if (!pkey ||
644 pkey->type != EVP_PKEY_EC ||
645 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
646 !tls1_check_curve_id(s, curve_id) ||
647 !tls1_check_point_format(s, comp_id)) {
648 goto done;
649 }
650
651 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500652
653done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 if (pkey) {
655 EVP_PKEY_free(pkey);
656 }
657 return ret;
658}
David Benjamin42e9a772014-09-02 23:18:44 -0400659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660int tls1_check_ec_tmp_key(SSL *s) {
661 uint16_t curve_id;
662 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 if (s->cert->ecdh_tmp_auto) {
665 /* Need a shared curve */
666 return tls1_get_shared_curve(s) != NID_undef;
667 }
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 if (!ec) {
670 if (s->cert->ecdh_tmp_cb) {
671 return 1;
672 }
673 return 0;
674 }
675
676 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
677 tls1_check_curve_id(s, curve_id);
678}
Adam Langley95c29f32014-06-20 12:00:00 -0700679
680/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700682
Adam Langley95c29f32014-06-20 12:00:00 -0700683#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langley95c29f32014-06-20 12:00:00 -0700685#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langleyfcf25832014-12-18 17:42:32 -0800687#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700688
David Benjamincff64722014-08-19 19:54:46 -0400689static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 tlsext_sigalg(TLSEXT_hash_sha512)
691 tlsext_sigalg(TLSEXT_hash_sha384)
692 tlsext_sigalg(TLSEXT_hash_sha256)
693 tlsext_sigalg(TLSEXT_hash_sha224)
694 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700695};
David Benjamin05da6e12014-07-12 20:42:55 -0400696
Adam Langleyfcf25832014-12-18 17:42:32 -0800697size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
698 /* If server use client authentication sigalgs if not NULL */
699 if (s->server && s->cert->client_sigalgs) {
700 *psigs = s->cert->client_sigalgs;
701 return s->cert->client_sigalgslen;
702 } else if (s->cert->conf_sigalgs) {
703 *psigs = s->cert->conf_sigalgs;
704 return s->cert->conf_sigalgslen;
705 } else {
706 *psigs = tls12_sigalgs;
707 return sizeof(tls12_sigalgs);
708 }
709}
Adam Langley95c29f32014-06-20 12:00:00 -0700710
Adam Langleyfcf25832014-12-18 17:42:32 -0800711/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
712 * checks it is consistent with |s|'s sent supported signature algorithms and,
713 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
714 * returns 0 and writes an alert into |*out_alert|. */
715int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
716 CBS *cbs, EVP_PKEY *pkey) {
717 const uint8_t *sent_sigs;
718 size_t sent_sigslen, i;
719 int sigalg = tls12_get_sigid(pkey);
720 uint8_t hash, signature;
721
722 /* Should never happen */
723 if (sigalg == -1) {
724 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
725 *out_alert = SSL_AD_INTERNAL_ERROR;
726 return 0;
727 }
728
729 if (!CBS_get_u8(cbs, &hash) ||
730 !CBS_get_u8(cbs, &signature)) {
731 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
732 *out_alert = SSL_AD_DECODE_ERROR;
733 return 0;
734 }
735
736 /* Check key type is consistent with signature */
737 if (sigalg != signature) {
738 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
739 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
740 return 0;
741 }
742
743 if (pkey->type == EVP_PKEY_EC) {
744 uint16_t curve_id;
745 uint8_t comp_id;
746 /* Check compression and curve matches extensions */
747 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
748 *out_alert = SSL_AD_INTERNAL_ERROR;
749 return 0;
750 }
751
752 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
753 !tls1_check_point_format(s, comp_id))) {
754 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
755 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
756 return 0;
757 }
758 }
759
760 /* Check signature matches a type we sent */
761 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
762 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
763 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
764 break;
765 }
766 }
767
768 /* Allow fallback to SHA-1. */
769 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
770 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
771 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
772 return 0;
773 }
774
775 *out_md = tls12_get_hash(hash);
776 if (*out_md == NULL) {
777 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
778 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
779 return 0;
780 }
781
782 return 1;
783}
784
785/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
786 * supported or doesn't appear in supported signature algorithms. Unlike
787 * ssl_cipher_get_disabled this applies to a specific session and not global
788 * settings. */
789void ssl_set_client_disabled(SSL *s) {
790 CERT *c = s->cert;
791 const uint8_t *sigalgs;
792 size_t i, sigalgslen;
793 int have_rsa = 0, have_ecdsa = 0;
794 c->mask_a = 0;
795 c->mask_k = 0;
796
797 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
798 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
799 c->mask_ssl = SSL_TLSV1_2;
800 } else {
801 c->mask_ssl = 0;
802 }
803
804 /* Now go through all signature algorithms seeing if we support any for RSA,
805 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
806 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
807 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
808 switch (sigalgs[1]) {
809 case TLSEXT_signature_rsa:
810 have_rsa = 1;
811 break;
812
813 case TLSEXT_signature_ecdsa:
814 have_ecdsa = 1;
815 break;
816 }
817 }
818
819 /* Disable auth if we don't include any appropriate signature algorithms. */
820 if (!have_rsa) {
821 c->mask_a |= SSL_aRSA;
822 }
823 if (!have_ecdsa) {
824 c->mask_a |= SSL_aECDSA;
825 }
826
827 /* with PSK there must be client callback set */
828 if (!s->psk_client_callback) {
829 c->mask_a |= SSL_aPSK;
830 c->mask_k |= SSL_kPSK;
831 }
832}
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyb0c235e2014-06-20 12:00:00 -0700834/* header_len is the length of the ClientHello header written so far, used to
835 * compute padding. It does not include the record header. Pass 0 if no padding
836 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800837uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
838 size_t header_len) {
839 int extdatalen = 0;
840 uint8_t *ret = buf;
841 uint8_t *orig = buf;
842 /* See if we support any ECC ciphersuites */
843 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
846 size_t i;
847 unsigned long alg_k, alg_a;
848 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
851 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 alg_k = c->algorithm_mkey;
854 alg_a = c->algorithm_auth;
855 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) {
856 using_ecc = 1;
857 break;
858 }
859 }
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 /* don't add extensions for SSLv3 unless doing secure renegotiation */
863 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
864 return orig;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (ret >= limit) {
870 return NULL; /* should never occur. */
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 if (s->tlsext_hostname != NULL) {
874 /* Add TLS extension servername to the Client Hello message */
875 unsigned long size_str;
876 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 /* check for enough space.
879 4 for the servername type and entension length
880 2 for servernamelist length
881 1 for the hostname type
882 2 for hostname length
883 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 lenmax = limit - ret - 9;
886 size_str = strlen(s->tlsext_hostname);
887 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
888 return NULL;
889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 /* extension type and length */
892 s2n(TLSEXT_TYPE_server_name, ret);
893 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 /* length of servername list */
896 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 /* hostname type, length and hostname */
899 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
900 s2n(size_str, ret);
901 memcpy(ret, s->tlsext_hostname, size_str);
902 ret += size_str;
903 }
Adam Langley75712922014-10-10 16:23:43 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 /* Add RI if renegotiating */
906 if (s->renegotiate) {
907 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
910 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
911 return NULL;
912 }
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 if ((limit - ret - 4 - el) < 0) {
915 return NULL;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 s2n(TLSEXT_TYPE_renegotiate, ret);
919 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
922 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
923 return NULL;
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 ret += el;
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 /* Add extended master secret. */
930 if (s->version != SSL3_VERSION) {
931 if (limit - ret - 4 < 0) {
932 return NULL;
933 }
934 s2n(TLSEXT_TYPE_extended_master_secret, ret);
935 s2n(0, ret);
936 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
939 int ticklen = 0;
940 if (!s->new_session && s->session && s->session->tlsext_tick) {
941 ticklen = s->session->tlsext_ticklen;
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 /* Check for enough room 2 for extension type, 2 for len rest for
945 * ticket. */
946 if ((long)(limit - ret - 4 - ticklen) < 0) {
947 return NULL;
948 }
949 s2n(TLSEXT_TYPE_session_ticket, ret);
950 s2n(ticklen, ret);
951 if (ticklen) {
952 memcpy(ret, s->session->tlsext_tick, ticklen);
953 ret += ticklen;
954 }
955 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 if (SSL_USE_SIGALGS(s)) {
958 size_t salglen;
959 const uint8_t *salg;
960 salglen = tls12_get_psigalgs(s, &salg);
961 if ((size_t)(limit - ret) < salglen + 6) {
962 return NULL;
963 }
964 s2n(TLSEXT_TYPE_signature_algorithms, ret);
965 s2n(salglen + 2, ret);
966 s2n(salglen, ret);
967 memcpy(ret, salg, salglen);
968 ret += salglen;
969 }
Adam Langley95c29f32014-06-20 12:00:00 -0700970
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 if (s->ocsp_stapling_enabled) {
972 /* The status_request extension is excessively extensible at every layer.
973 * On the client, only support requesting OCSP responses with an empty
974 * responder_id_list and no extensions. */
975 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
976 return NULL;
977 }
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 s2n(TLSEXT_TYPE_status_request, ret);
980 s2n(1 + 2 + 2, ret);
981 /* status_type */
982 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
983 /* responder_id_list - empty */
984 s2n(0, ret);
985 /* request_extensions - empty */
986 s2n(0, ret);
987 }
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
990 /* The client advertises an emtpy extension to indicate its support for
991 * Next Protocol Negotiation */
992 if (limit - ret - 4 < 0) {
993 return NULL;
994 }
995 s2n(TLSEXT_TYPE_next_proto_neg, ret);
996 s2n(0, ret);
997 }
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
1000 /* The client advertises an empty extension to indicate its support for
1001 * certificate timestamps. */
1002 if (limit - ret - 4 < 0) {
1003 return NULL;
1004 }
1005 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
1006 s2n(0, ret);
1007 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1010 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
1011 return NULL;
1012 }
1013 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1014 s2n(2 + s->alpn_client_proto_list_len, ret);
1015 s2n(s->alpn_client_proto_list_len, ret);
1016 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1017 ret += s->alpn_client_proto_list_len;
1018 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 if (s->tlsext_channel_id_enabled) {
1021 /* The client advertises an emtpy extension to indicate its support for
1022 * Channel ID. */
1023 if (limit - ret - 4 < 0) {
1024 return NULL;
1025 }
1026 if (s->ctx->tlsext_channel_id_enabled_new) {
1027 s2n(TLSEXT_TYPE_channel_id_new, ret);
1028 } else {
1029 s2n(TLSEXT_TYPE_channel_id, ret);
1030 }
1031 s2n(0, ret);
1032 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 if (SSL_get_srtp_profiles(s)) {
1035 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 if ((limit - ret - 4 - el) < 0) {
1040 return NULL;
1041 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 s2n(TLSEXT_TYPE_use_srtp, ret);
1044 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1047 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1048 return NULL;
1049 }
1050 ret += el;
1051 }
Adam Langley95c29f32014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 if (using_ecc) {
1054 /* Add TLS extension ECPointFormats to the ClientHello message */
1055 long lenmax;
1056 const uint8_t *formats;
1057 const uint16_t *curves;
1058 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001059
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 lenmax = limit - ret - 5;
1063 if (lenmax < 0) {
1064 return NULL;
1065 }
1066 if (formats_len > (size_t)lenmax) {
1067 return NULL;
1068 }
1069 if (formats_len > 255) {
1070 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1071 return NULL;
1072 }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1075 s2n(formats_len + 1, ret);
1076 *(ret++) = (uint8_t)formats_len;
1077 memcpy(ret, formats, formats_len);
1078 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001079
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 /* Add TLS extension EllipticCurves to the ClientHello message */
1081 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 lenmax = limit - ret - 6;
1084 if (lenmax < 0) {
1085 return NULL;
1086 }
1087 if (curves_len * 2 > (size_t)lenmax) {
1088 return NULL;
1089 }
1090 if (curves_len * 2 > 65532) {
1091 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1092 return NULL;
1093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1096 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 s2n(curves_len * 2, ret);
1099 for (i = 0; i < curves_len; i++) {
1100 s2n(curves[i], ret);
1101 }
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 if (header_len > 0) {
1105 size_t clienthello_minsize = 0;
1106 header_len += ret - orig;
1107 if (header_len > 0xff && header_len < 0x200) {
1108 /* Add padding to workaround bugs in F5 terminators. See
1109 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1110 *
1111 * NB: because this code works out the length of all existing extensions
1112 * it MUST always appear last. */
1113 clienthello_minsize = 0x200;
1114 }
1115 if (s->fastradio_padding) {
1116 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1117 * into DCH (high data rate) state in 3G networks. Note that when
1118 * fastradio_padding is enabled, even if the header_len is less than 255
1119 * bytes, the padding will be applied regardless. This is slightly
1120 * different from the TLS padding extension suggested in
1121 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1122 clienthello_minsize = 0x400;
1123 }
1124 if (header_len < clienthello_minsize) {
1125 size_t padding_len = clienthello_minsize - header_len;
1126 /* Extensions take at least four bytes to encode. Always include least
1127 * one byte of data if including the extension. WebSphere Application
1128 * Server 7.0 is intolerant to the last extension being zero-length. */
1129 if (padding_len >= 4 + 1) {
1130 padding_len -= 4;
1131 } else {
1132 padding_len = 1;
1133 }
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 if (limit - ret - 4 - (long)padding_len < 0) {
1136 return NULL;
1137 }
Adam Langley75712922014-10-10 16:23:43 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 s2n(TLSEXT_TYPE_padding, ret);
1140 s2n(padding_len, ret);
1141 memset(ret, 0, padding_len);
1142 ret += padding_len;
1143 }
1144 }
Adam Langley75712922014-10-10 16:23:43 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 extdatalen = ret - orig - 2;
1147 if (extdatalen == 0) {
1148 return orig;
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 s2n(extdatalen, orig);
1152 return ret;
1153}
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1156 int extdatalen = 0;
1157 uint8_t *orig = buf;
1158 uint8_t *ret = buf;
1159 int next_proto_neg_seen;
1160 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1161 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1162 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
1163 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1166 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1167 return orig;
1168 }
Adam Langley95c29f32014-06-20 12:00:00 -07001169
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 ret += 2;
1171 if (ret >= limit) {
1172 return NULL; /* should never happen. */
1173 }
Adam Langley95c29f32014-06-20 12:00:00 -07001174
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1176 if ((long)(limit - ret - 4) < 0) {
1177 return NULL;
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 s2n(TLSEXT_TYPE_server_name, ret);
1181 s2n(0, ret);
1182 }
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 if (s->s3->send_connection_binding) {
1185 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1188 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1189 return NULL;
1190 }
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 if ((limit - ret - 4 - el) < 0) {
1193 return NULL;
1194 }
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyfcf25832014-12-18 17:42:32 -08001196 s2n(TLSEXT_TYPE_renegotiate, ret);
1197 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1200 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1201 return NULL;
1202 }
Adam Langley95c29f32014-06-20 12:00:00 -07001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 ret += el;
1205 }
Adam Langley95c29f32014-06-20 12:00:00 -07001206
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 if (s->s3->tmp.extended_master_secret) {
1208 if ((long)(limit - ret - 4) < 0) {
1209 return NULL;
1210 }
Adam Langley95c29f32014-06-20 12:00:00 -07001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1213 s2n(0, ret);
1214 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 if (using_ecc) {
1217 const uint8_t *plist;
1218 size_t plistlen;
1219 /* Add TLS extension ECPointFormats to the ServerHello message */
1220 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 tls1_get_formatlist(s, &plist, &plistlen);
1223
1224 lenmax = limit - ret - 5;
1225 if (lenmax < 0) {
1226 return NULL;
1227 }
1228 if (plistlen > (size_t)lenmax) {
1229 return NULL;
1230 }
1231 if (plistlen > 255) {
1232 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1233 return NULL;
1234 }
1235
1236 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1237 s2n(plistlen + 1, ret);
1238 *(ret++) = (uint8_t)plistlen;
1239 memcpy(ret, plist, plistlen);
1240 ret += plistlen;
1241 }
1242 /* Currently the server should not respond with a SupportedCurves extension */
1243
1244 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1245 if ((long)(limit - ret - 4) < 0) {
1246 return NULL;
1247 }
1248 s2n(TLSEXT_TYPE_session_ticket, ret);
1249 s2n(0, ret);
1250 }
1251
1252 if (s->s3->tmp.certificate_status_expected) {
1253 if ((long)(limit - ret - 4) < 0) {
1254 return NULL;
1255 }
1256 s2n(TLSEXT_TYPE_status_request, ret);
1257 s2n(0, ret);
1258 }
1259
1260 if (s->srtp_profile) {
1261 int el;
1262
1263 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1264
1265 if ((limit - ret - 4 - el) < 0) {
1266 return NULL;
1267 }
1268
1269 s2n(TLSEXT_TYPE_use_srtp, ret);
1270 s2n(el, ret);
1271
1272 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1273 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1274 return NULL;
1275 }
1276 ret += el;
1277 }
1278
1279 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1280 s->s3->next_proto_neg_seen = 0;
1281 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1282 const uint8_t *npa;
1283 unsigned int npalen;
1284 int r;
1285
1286 r = s->ctx->next_protos_advertised_cb(
1287 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1288 if (r == SSL_TLSEXT_ERR_OK) {
1289 if ((long)(limit - ret - 4 - npalen) < 0) {
1290 return NULL;
1291 }
1292 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1293 s2n(npalen, ret);
1294 memcpy(ret, npa, npalen);
1295 ret += npalen;
1296 s->s3->next_proto_neg_seen = 1;
1297 }
1298 }
1299
1300 if (s->s3->alpn_selected) {
1301 const uint8_t *selected = s->s3->alpn_selected;
1302 size_t len = s->s3->alpn_selected_len;
1303
1304 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1305 return NULL;
1306 }
1307 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1308 s2n(3 + len, ret);
1309 s2n(1 + len, ret);
1310 *ret++ = len;
1311 memcpy(ret, selected, len);
1312 ret += len;
1313 }
1314
1315 /* If the client advertised support for Channel ID, and we have it
1316 * enabled, then we want to echo it back. */
1317 if (s->s3->tlsext_channel_id_valid) {
1318 if (limit - ret - 4 < 0) {
1319 return NULL;
1320 }
1321 if (s->s3->tlsext_channel_id_new) {
1322 s2n(TLSEXT_TYPE_channel_id_new, ret);
1323 } else {
1324 s2n(TLSEXT_TYPE_channel_id, ret);
1325 }
1326 s2n(0, ret);
1327 }
1328
1329 extdatalen = ret - orig - 2;
1330 if (extdatalen == 0) {
1331 return orig;
1332 }
1333
1334 s2n(extdatalen, orig);
1335 return ret;
1336}
Adam Langley95c29f32014-06-20 12:00:00 -07001337
Adam Langley95c29f32014-06-20 12:00:00 -07001338/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1339 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001340 * cbs: the contents of the extension, not including the type and length.
1341 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001342 * return.
1343 *
David Benjamindc72ff72014-06-25 12:36:10 -04001344 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001345static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1346 CBS protocol_name_list, protocol_name_list_copy;
1347 const uint8_t *selected;
1348 uint8_t selected_len;
1349 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 if (s->ctx->alpn_select_cb == NULL) {
1352 return 1;
1353 }
Adam Langley95c29f32014-06-20 12:00:00 -07001354
Adam Langleyfcf25832014-12-18 17:42:32 -08001355 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1356 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1357 goto parse_error;
1358 }
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 /* Validate the protocol list. */
1361 protocol_name_list_copy = protocol_name_list;
1362 while (CBS_len(&protocol_name_list_copy) > 0) {
1363 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1366 goto parse_error;
1367 }
1368 }
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 r = s->ctx->alpn_select_cb(
1371 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1372 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1373 if (r == SSL_TLSEXT_ERR_OK) {
1374 if (s->s3->alpn_selected) {
1375 OPENSSL_free(s->s3->alpn_selected);
1376 }
1377 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1378 if (!s->s3->alpn_selected) {
1379 *out_alert = SSL_AD_INTERNAL_ERROR;
1380 return 0;
1381 }
1382 s->s3->alpn_selected_len = selected_len;
1383 }
1384
1385 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001386
1387parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 *out_alert = SSL_AD_DECODE_ERROR;
1389 return 0;
1390}
Adam Langley95c29f32014-06-20 12:00:00 -07001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1393 int renegotiate_seen = 0;
1394 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001397 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 s->s3->next_proto_neg_seen = 0;
1399 s->s3->tmp.certificate_status_expected = 0;
1400 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 if (s->s3->alpn_selected) {
1403 OPENSSL_free(s->s3->alpn_selected);
1404 s->s3->alpn_selected = NULL;
1405 }
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 /* Clear any signature algorithms extension received */
1408 if (s->cert->peer_sigalgs) {
1409 OPENSSL_free(s->cert->peer_sigalgs);
1410 s->cert->peer_sigalgs = NULL;
1411 }
Adam Langley95c29f32014-06-20 12:00:00 -07001412
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 /* Clear any shared signature algorithms */
1414 if (s->cert->shared_sigalgs) {
1415 OPENSSL_free(s->cert->shared_sigalgs);
1416 s->cert->shared_sigalgs = NULL;
1417 }
Adam Langley95c29f32014-06-20 12:00:00 -07001418
Adam Langleyfcf25832014-12-18 17:42:32 -08001419 /* Clear ECC extensions */
1420 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1421 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1422 s->s3->tmp.peer_ecpointformatlist = NULL;
1423 s->s3->tmp.peer_ecpointformatlist_length = 0;
1424 }
David Benjamindc72ff72014-06-25 12:36:10 -04001425
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1427 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1428 s->s3->tmp.peer_ellipticcurvelist = NULL;
1429 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1430 }
David Benjamindc72ff72014-06-25 12:36:10 -04001431
Adam Langleyfcf25832014-12-18 17:42:32 -08001432 /* There may be no extensions. */
1433 if (CBS_len(cbs) == 0) {
1434 goto ri_check;
1435 }
David Benjamindc72ff72014-06-25 12:36:10 -04001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 /* Decode the extensions block and check it is valid. */
1438 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1439 !tls1_check_duplicate_extensions(&extensions)) {
1440 *out_alert = SSL_AD_DECODE_ERROR;
1441 return 0;
1442 }
David Benjamindc72ff72014-06-25 12:36:10 -04001443
Adam Langleyfcf25832014-12-18 17:42:32 -08001444 while (CBS_len(&extensions) != 0) {
1445 uint16_t type;
1446 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001447
Adam Langleyfcf25832014-12-18 17:42:32 -08001448 /* Decode the next extension. */
1449 if (!CBS_get_u16(&extensions, &type) ||
1450 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1451 *out_alert = SSL_AD_DECODE_ERROR;
1452 return 0;
1453 }
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 if (s->tlsext_debug_cb) {
1456 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1457 CBS_len(&extension), s->tlsext_debug_arg);
1458 }
Adam Langley95c29f32014-06-20 12:00:00 -07001459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 - Only the hostname type is supported with a maximum length of 255.
1463 - The servername is rejected if too long or if it contains zeros, in
1464 which case an fatal alert is generated.
1465 - The servername field is maintained together with the session cache.
1466 - When a session is resumed, the servername call back invoked in order
1467 to allow the application to position itself to the right context.
1468 - The servername is acknowledged if it is new for a session or when
1469 it is identical to a previously used for the same session.
1470 Applications can control the behaviour. They can at any time
1471 set a 'desirable' servername for a new SSL object. This can be the
1472 case for example with HTTPS when a Host: header field is received and
1473 a renegotiation is requested. In this case, a possible servername
1474 presented in the new client hello is only acknowledged if it matches
1475 the value of the Host: field.
1476 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1477 if they provide for changing an explicit servername context for the
1478 session,
1479 i.e. when the session has been established with a servername extension.
1480 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 if (type == TLSEXT_TYPE_server_name) {
1483 CBS server_name_list;
1484 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1487 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1488 *out_alert = SSL_AD_DECODE_ERROR;
1489 return 0;
1490 }
David Benjamindc72ff72014-06-25 12:36:10 -04001491
Adam Langleyfcf25832014-12-18 17:42:32 -08001492 /* Decode each ServerName in the extension. */
1493 while (CBS_len(&server_name_list) > 0) {
1494 uint8_t name_type;
1495 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001496
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 /* Decode the NameType. */
1498 if (!CBS_get_u8(&server_name_list, &name_type)) {
1499 *out_alert = SSL_AD_DECODE_ERROR;
1500 return 0;
1501 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 /* Only host_name is supported. */
1504 if (name_type != TLSEXT_NAMETYPE_host_name) {
1505 continue;
1506 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001507
Adam Langleyfcf25832014-12-18 17:42:32 -08001508 if (have_seen_host_name) {
1509 /* The ServerNameList MUST NOT contain more than one name of the same
1510 * name_type. */
1511 *out_alert = SSL_AD_DECODE_ERROR;
1512 return 0;
1513 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001514
Adam Langleyfcf25832014-12-18 17:42:32 -08001515 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1518 CBS_len(&host_name) < 1) {
1519 *out_alert = SSL_AD_DECODE_ERROR;
1520 return 0;
1521 }
Adam Langley95c29f32014-06-20 12:00:00 -07001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1524 CBS_contains_zero_byte(&host_name)) {
1525 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1526 return 0;
1527 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001528
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 if (!s->hit) {
1530 assert(s->session->tlsext_hostname == NULL);
1531 if (s->session->tlsext_hostname) {
1532 /* This should be impossible. */
1533 *out_alert = SSL_AD_DECODE_ERROR;
1534 return 0;
1535 }
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 /* Copy the hostname as a string. */
1538 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1539 *out_alert = SSL_AD_INTERNAL_ERROR;
1540 return 0;
1541 }
Adam Langley95c29f32014-06-20 12:00:00 -07001542
Adam Langleyfcf25832014-12-18 17:42:32 -08001543 s->should_ack_sni = 1;
1544 }
1545 }
1546 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1547 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001548
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1550 CBS_len(&extension) != 0) {
1551 *out_alert = SSL_AD_DECODE_ERROR;
1552 return 0;
1553 }
Adam Langley95c29f32014-06-20 12:00:00 -07001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1556 &s->s3->tmp.peer_ecpointformatlist_length)) {
1557 *out_alert = SSL_AD_INTERNAL_ERROR;
1558 return 0;
1559 }
1560 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1561 CBS elliptic_curve_list;
1562 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001563
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1565 CBS_len(&elliptic_curve_list) == 0 ||
1566 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1567 CBS_len(&extension) != 0) {
1568 *out_alert = SSL_AD_DECODE_ERROR;
1569 return 0;
1570 }
David Benjamindc72ff72014-06-25 12:36:10 -04001571
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 if (s->s3->tmp.peer_ellipticcurvelist) {
1573 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1574 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1575 }
David Benjamindc72ff72014-06-25 12:36:10 -04001576
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 s->s3->tmp.peer_ellipticcurvelist =
1578 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001579
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1581 *out_alert = SSL_AD_INTERNAL_ERROR;
1582 return 0;
1583 }
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 num_curves = CBS_len(&elliptic_curve_list) / 2;
1586 for (i = 0; i < num_curves; i++) {
1587 if (!CBS_get_u16(&elliptic_curve_list,
1588 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1589 *out_alert = SSL_AD_INTERNAL_ERROR;
1590 return 0;
1591 }
1592 }
David Benjamindc72ff72014-06-25 12:36:10 -04001593
Adam Langleyfcf25832014-12-18 17:42:32 -08001594 if (CBS_len(&elliptic_curve_list) != 0) {
1595 *out_alert = SSL_AD_INTERNAL_ERROR;
1596 return 0;
1597 }
Adam Langley95c29f32014-06-20 12:00:00 -07001598
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1600 } else if (type == TLSEXT_TYPE_renegotiate) {
1601 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1602 return 0;
1603 }
1604 renegotiate_seen = 1;
1605 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1606 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001607
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 if (!CBS_get_u16_length_prefixed(&extension,
1609 &supported_signature_algorithms) ||
1610 CBS_len(&extension) != 0) {
1611 *out_alert = SSL_AD_DECODE_ERROR;
1612 return 0;
1613 }
Adam Langley95c29f32014-06-20 12:00:00 -07001614
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 /* Ensure the signature algorithms are non-empty. It contains a list of
1616 * SignatureAndHashAlgorithms which are two bytes each. */
1617 if (CBS_len(&supported_signature_algorithms) == 0 ||
1618 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1619 *out_alert = SSL_AD_DECODE_ERROR;
1620 return 0;
1621 }
David Benjamindc72ff72014-06-25 12:36:10 -04001622
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1624 *out_alert = SSL_AD_DECODE_ERROR;
1625 return 0;
1626 }
1627 /* If sigalgs received and no shared algorithms fatal error. */
1628 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
1629 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext,
1630 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1631 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1632 return 0;
1633 }
1634 } else if (type == TLSEXT_TYPE_next_proto_neg &&
1635 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL) {
1636 /* The extension must be empty. */
1637 if (CBS_len(&extension) != 0) {
1638 *out_alert = SSL_AD_DECODE_ERROR;
1639 return 0;
1640 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001641
Adam Langleyfcf25832014-12-18 17:42:32 -08001642 /* We shouldn't accept this extension on a renegotiation.
1643 *
1644 * s->new_session will be set on renegotiation, but we probably shouldn't
1645 * rely that it couldn't be set on the initial renegotation too in
1646 * certain cases (when there's some other reason to disallow resuming an
1647 * earlier session -- the current code won't be doing anything like that,
1648 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 * A valid sign that there's been a previous handshake in this connection
1651 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1652 * that will happen in the Hello protocol round, well before a new
1653 * Finished message could have been computed.) */
1654 s->s3->next_proto_neg_seen = 1;
1655 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1656 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1657 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1658 return 0;
1659 }
1660 /* ALPN takes precedence over NPN. */
1661 s->s3->next_proto_neg_seen = 0;
1662 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled) {
1663 /* The extension must be empty. */
1664 if (CBS_len(&extension) != 0) {
1665 *out_alert = SSL_AD_DECODE_ERROR;
1666 return 0;
1667 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 s->s3->tlsext_channel_id_valid = 1;
1670 } else if (type == TLSEXT_TYPE_channel_id_new &&
1671 s->tlsext_channel_id_enabled) {
1672 /* The extension must be empty. */
1673 if (CBS_len(&extension) != 0) {
1674 *out_alert = SSL_AD_DECODE_ERROR;
1675 return 0;
1676 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 s->s3->tlsext_channel_id_valid = 1;
1679 s->s3->tlsext_channel_id_new = 1;
1680 } else if (type == TLSEXT_TYPE_use_srtp) {
1681 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1682 return 0;
1683 }
1684 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1685 s->version != SSL3_VERSION) {
1686 if (CBS_len(&extension) != 0) {
1687 *out_alert = SSL_AD_DECODE_ERROR;
1688 return 0;
1689 }
Adam Langley75712922014-10-10 16:23:43 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 s->s3->tmp.extended_master_secret = 1;
1692 }
1693 }
Adam Langley75712922014-10-10 16:23:43 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695ri_check:
1696 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001697
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 if (!renegotiate_seen && s->renegotiate &&
1699 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1700 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1701 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1702 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1703 return 0;
1704 }
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 return 1;
1707}
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Adam Langleyfcf25832014-12-18 17:42:32 -08001709int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1710 int alert = -1;
1711 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1712 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1713 return 0;
1714 }
Adam Langley95c29f32014-06-20 12:00:00 -07001715
Adam Langleyfcf25832014-12-18 17:42:32 -08001716 if (ssl_check_clienthello_tlsext(s) <= 0) {
1717 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1718 SSL_R_CLIENTHELLO_TLSEXT);
1719 return 0;
1720 }
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 return 1;
1723}
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langley95c29f32014-06-20 12:00:00 -07001725/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 * elements of zero length are allowed and the set of elements must exactly
1727 * fill the length of the block. */
1728static char ssl_next_proto_validate(const CBS *cbs) {
1729 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001730
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 while (CBS_len(&copy) != 0) {
1732 CBS proto;
1733 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1734 return 0;
1735 }
1736 }
Adam Langley95c29f32014-06-20 12:00:00 -07001737
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 return 1;
1739}
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1742 int tlsext_servername = 0;
1743 int renegotiate_seen = 0;
1744 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 /* TODO(davidben): Move all of these to some per-handshake state that gets
1747 * systematically reset on a new handshake; perhaps allocate it fresh each
1748 * time so it's not even kept around post-handshake. */
1749 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 s->tlsext_ticket_expected = 0;
1751 s->s3->tmp.certificate_status_expected = 0;
1752 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001753 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 if (s->s3->alpn_selected) {
1756 OPENSSL_free(s->s3->alpn_selected);
1757 s->s3->alpn_selected = NULL;
1758 }
David Benjamina19fc252014-10-19 00:14:36 -04001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 /* Clear ECC extensions */
1761 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1762 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1763 s->s3->tmp.peer_ecpointformatlist = NULL;
1764 s->s3->tmp.peer_ecpointformatlist_length = 0;
1765 }
David Benjamin03973092014-06-24 23:27:17 -04001766
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 /* There may be no extensions. */
1768 if (CBS_len(cbs) == 0) {
1769 goto ri_check;
1770 }
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 /* Decode the extensions block and check it is valid. */
1773 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1774 !tls1_check_duplicate_extensions(&extensions)) {
1775 *out_alert = SSL_AD_DECODE_ERROR;
1776 return 0;
1777 }
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 while (CBS_len(&extensions) != 0) {
1780 uint16_t type;
1781 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 /* Decode the next extension. */
1784 if (!CBS_get_u16(&extensions, &type) ||
1785 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1786 *out_alert = SSL_AD_DECODE_ERROR;
1787 return 0;
1788 }
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 if (s->tlsext_debug_cb) {
1791 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1792 CBS_len(&extension), s->tlsext_debug_arg);
1793 }
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 if (type == TLSEXT_TYPE_server_name) {
1796 /* The extension must be empty. */
1797 if (CBS_len(&extension) != 0) {
1798 *out_alert = SSL_AD_DECODE_ERROR;
1799 return 0;
1800 }
David Benjamin03973092014-06-24 23:27:17 -04001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 /* We must have sent it in ClientHello. */
1803 if (s->tlsext_hostname == NULL) {
1804 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1805 return 0;
1806 }
David Benjamin03973092014-06-24 23:27:17 -04001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 tlsext_servername = 1;
1809 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1810 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1813 CBS_len(&extension) != 0) {
1814 *out_alert = SSL_AD_DECODE_ERROR;
1815 return 0;
1816 }
Adam Langley95c29f32014-06-20 12:00:00 -07001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1819 &s->s3->tmp.peer_ecpointformatlist_length)) {
1820 *out_alert = SSL_AD_INTERNAL_ERROR;
1821 return 0;
1822 }
1823 } else if (type == TLSEXT_TYPE_session_ticket) {
1824 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1825 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1826 return 0;
1827 }
David Benjamin03973092014-06-24 23:27:17 -04001828
Adam Langleyfcf25832014-12-18 17:42:32 -08001829 s->tlsext_ticket_expected = 1;
1830 } else if (type == TLSEXT_TYPE_status_request) {
1831 /* The extension MUST be empty and may only sent if we've requested a
1832 * status request message. */
1833 if (CBS_len(&extension) != 0) {
1834 *out_alert = SSL_AD_DECODE_ERROR;
1835 return 0;
1836 }
David Benjamin03973092014-06-24 23:27:17 -04001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 if (!s->ocsp_stapling_enabled) {
1839 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1840 return 0;
1841 }
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 /* Set a flag to expect a CertificateStatus message */
1844 s->s3->tmp.certificate_status_expected = 1;
1845 } else if (type == TLSEXT_TYPE_next_proto_neg &&
1846 s->s3->tmp.finish_md_len == 0) {
1847 uint8_t *selected;
1848 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850 /* We must have requested it. */
1851 if (s->ctx->next_proto_select_cb == NULL) {
1852 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1853 return 0;
1854 }
David Benjamin03973092014-06-24 23:27:17 -04001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 /* The data must be valid. */
1857 if (!ssl_next_proto_validate(&extension)) {
1858 *out_alert = SSL_AD_DECODE_ERROR;
1859 return 0;
1860 }
Adam Langley95c29f32014-06-20 12:00:00 -07001861
Adam Langleyfcf25832014-12-18 17:42:32 -08001862 if (s->ctx->next_proto_select_cb(
1863 s, &selected, &selected_len, CBS_data(&extension),
1864 CBS_len(&extension),
1865 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1866 *out_alert = SSL_AD_INTERNAL_ERROR;
1867 return 0;
1868 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1871 if (s->next_proto_negotiated == NULL) {
1872 *out_alert = SSL_AD_INTERNAL_ERROR;
1873 return 0;
1874 }
Adam Langley75712922014-10-10 16:23:43 -07001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 s->next_proto_negotiated_len = selected_len;
1877 s->s3->next_proto_neg_seen = 1;
1878 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1879 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 /* We must have requested it. */
1882 if (s->alpn_client_proto_list == NULL) {
1883 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1884 return 0;
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 /* The extension data consists of a ProtocolNameList which must have
1888 * exactly one ProtocolName. Each of these is length-prefixed. */
1889 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1890 CBS_len(&extension) != 0 ||
1891 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1892 CBS_len(&protocol_name_list) != 0) {
1893 *out_alert = SSL_AD_DECODE_ERROR;
1894 return 0;
1895 }
Adam Langley95c29f32014-06-20 12:00:00 -07001896
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1898 &s->s3->alpn_selected_len)) {
1899 *out_alert = SSL_AD_INTERNAL_ERROR;
1900 return 0;
1901 }
1902 } else if (type == TLSEXT_TYPE_channel_id) {
1903 if (CBS_len(&extension) != 0) {
1904 *out_alert = SSL_AD_DECODE_ERROR;
1905 return 0;
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 s->s3->tlsext_channel_id_valid = 1;
1909 } else if (type == TLSEXT_TYPE_channel_id_new) {
1910 if (CBS_len(&extension) != 0) {
1911 *out_alert = SSL_AD_DECODE_ERROR;
1912 return 0;
1913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 s->s3->tlsext_channel_id_valid = 1;
1916 s->s3->tlsext_channel_id_new = 1;
1917 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1918 if (CBS_len(&extension) == 0) {
1919 *out_alert = SSL_AD_DECODE_ERROR;
1920 return 0;
1921 }
Adam Langley95c29f32014-06-20 12:00:00 -07001922
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 /* Session resumption uses the original session information. */
1924 if (!s->hit &&
1925 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1926 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1927 *out_alert = SSL_AD_INTERNAL_ERROR;
1928 return 0;
1929 }
1930 } else if (type == TLSEXT_TYPE_renegotiate) {
1931 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1932 return 0;
1933 }
Adam Langley95c29f32014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 renegotiate_seen = 1;
1936 } else if (type == TLSEXT_TYPE_use_srtp) {
1937 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1938 return 0;
1939 }
1940 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1941 if (/* It is invalid for the server to select EMS and
1942 SSLv3. */
1943 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1944 *out_alert = SSL_AD_DECODE_ERROR;
1945 return 0;
1946 }
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 s->s3->tmp.extended_master_secret = 1;
1949 }
1950 }
Adam Langley95c29f32014-06-20 12:00:00 -07001951
Adam Langleyfcf25832014-12-18 17:42:32 -08001952 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1953 if (s->session->tlsext_hostname == NULL) {
1954 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1955 if (!s->session->tlsext_hostname) {
1956 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1957 return 0;
1958 }
1959 } else {
1960 *out_alert = SSL_AD_DECODE_ERROR;
1961 return 0;
1962 }
1963 }
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965ri_check:
1966 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1967 * attack we should *always* see RI even on initial server hello because the
1968 * client doesn't see any renegotiation during an attack. However this would
1969 * mean we could not connect to any server which doesn't support RI so for
1970 * the immediate future tolerate RI absence on initial connect only. */
1971 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1972 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1973 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1974 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1975 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1976 return 0;
1977 }
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 return 1;
1980}
Adam Langley95c29f32014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986static int ssl_check_clienthello_tlsext(SSL *s) {
1987 int ret = SSL_TLSEXT_ERR_NOACK;
1988 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001989
Adam Langleyfcf25832014-12-18 17:42:32 -08001990 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1991 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1994 ret = s->ctx->tlsext_servername_callback(s, &al,
1995 s->ctx->tlsext_servername_arg);
1996 } else if (s->initial_ctx != NULL &&
1997 s->initial_ctx->tlsext_servername_callback != 0) {
1998 ret = s->initial_ctx->tlsext_servername_callback(
1999 s, &al, s->initial_ctx->tlsext_servername_arg);
2000 }
Adam Langley95c29f32014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 switch (ret) {
2003 case SSL_TLSEXT_ERR_ALERT_FATAL:
2004 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2005 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007 case SSL_TLSEXT_ERR_ALERT_WARNING:
2008 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2009 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002010
Adam Langleyfcf25832014-12-18 17:42:32 -08002011 case SSL_TLSEXT_ERR_NOACK:
2012 s->should_ack_sni = 0;
2013 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002014
Adam Langleyfcf25832014-12-18 17:42:32 -08002015 default:
2016 return 1;
2017 }
2018}
Adam Langleyed8270a2014-09-02 13:52:56 -07002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020static int ssl_check_serverhello_tlsext(SSL *s) {
2021 int ret = SSL_TLSEXT_ERR_NOACK;
2022 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 /* If we are client and using an elliptic curve cryptography cipher suite,
2025 * then if server returns an EC point formats lists extension it must contain
2026 * uncompressed. */
2027 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2028 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2029 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2030 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
2031 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
2032 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2033 return -1;
2034 }
2035 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002036
Adam Langleyfcf25832014-12-18 17:42:32 -08002037 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2038 ret = s->ctx->tlsext_servername_callback(s, &al,
2039 s->ctx->tlsext_servername_arg);
2040 } else if (s->initial_ctx != NULL &&
2041 s->initial_ctx->tlsext_servername_callback != 0) {
2042 ret = s->initial_ctx->tlsext_servername_callback(
2043 s, &al, s->initial_ctx->tlsext_servername_arg);
2044 }
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 switch (ret) {
2047 case SSL_TLSEXT_ERR_ALERT_FATAL:
2048 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2049 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 case SSL_TLSEXT_ERR_ALERT_WARNING:
2052 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2053 return 1;
2054
2055 default:
2056 return 1;
2057 }
2058}
2059
2060int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2061 int alert = -1;
2062 if (s->version < SSL3_VERSION) {
2063 return 1;
2064 }
2065
2066 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2067 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2068 return 0;
2069 }
2070
2071 if (ssl_check_serverhello_tlsext(s) <= 0) {
2072 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2073 SSL_R_SERVERHELLO_TLSEXT);
2074 return 0;
2075 }
2076
2077 return 1;
2078}
Adam Langley95c29f32014-06-20 12:00:00 -07002079
2080/* Since the server cache lookup is done early on in the processing of the
2081 * ClientHello, and other operations depend on the result, we need to handle
2082 * any TLS session ticket extension at the same time.
2083 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002084 * ctx: contains the early callback context, which is the result of a
2085 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002086 * ret: (output) on return, if a ticket was decrypted, then this is set to
2087 * point to the resulting session.
2088 *
Adam Langley95c29f32014-06-20 12:00:00 -07002089 * Returns:
2090 * -1: fatal error, either from parsing or decrypting the ticket.
2091 * 0: no ticket was found (or was ignored, based on settings).
2092 * 1: a zero length extension was found, indicating that the client supports
2093 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002094 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2095 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002096 * 3: a ticket was successfully decrypted and *ret was set.
2097 *
2098 * Side effects:
2099 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2100 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002101 * but the client either doesn't have a session ticket or we couldn't use
2102 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2103 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002104 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002105int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 SSL_SESSION **ret) {
2107 *ret = NULL;
2108 s->tlsext_ticket_expected = 0;
2109 const uint8_t *data;
2110 size_t len;
2111 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002112
Adam Langleyfcf25832014-12-18 17:42:32 -08002113 /* If tickets disabled behave as if no ticket present to permit stateful
2114 * resumption. */
2115 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2116 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2117 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2118 &data, &len)) {
2119 return 0;
2120 }
2121
2122 if (len == 0) {
2123 /* The client will accept a ticket but doesn't currently have one. */
2124 s->tlsext_ticket_expected = 1;
2125 return 1;
2126 }
2127
2128 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2129 ret);
2130 switch (r) {
2131 case 2: /* ticket couldn't be decrypted */
2132 s->tlsext_ticket_expected = 1;
2133 return 2;
2134
2135 case 3: /* ticket was decrypted */
2136 return r;
2137
2138 case 4: /* ticket decrypted but need to renew */
2139 s->tlsext_ticket_expected = 1;
2140 return 3;
2141
2142 default: /* fatal error */
2143 return -1;
2144 }
2145}
Adam Langley95c29f32014-06-20 12:00:00 -07002146
2147/* tls_decrypt_ticket attempts to decrypt a session ticket.
2148 *
2149 * etick: points to the body of the session ticket extension.
2150 * eticklen: the length of the session tickets extenion.
2151 * sess_id: points at the session ID.
2152 * sesslen: the length of the session ID.
2153 * psess: (output) on return, if a ticket was decrypted, then this is set to
2154 * point to the resulting session.
2155 *
2156 * Returns:
2157 * -1: fatal error, either from parsing or decrypting the ticket.
2158 * 2: the ticket couldn't be decrypted.
2159 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 * 4: same as 3, but the ticket needs to be renewed. */
2161static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2162 const uint8_t *sess_id, int sesslen,
2163 SSL_SESSION **psess) {
2164 SSL_SESSION *sess;
2165 uint8_t *sdec;
2166 const uint8_t *p;
2167 int slen, mlen, renew_ticket = 0;
2168 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2169 HMAC_CTX hctx;
2170 EVP_CIPHER_CTX ctx;
2171 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002172
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 /* Need at least keyname + iv + some encrypted data */
2174 if (eticklen < 48) {
2175 return 2;
2176 }
2177
2178 /* Initialize session ticket encryption and HMAC contexts */
2179 HMAC_CTX_init(&hctx);
2180 EVP_CIPHER_CTX_init(&ctx);
2181 if (tctx->tlsext_ticket_key_cb) {
2182 uint8_t *nctick = (uint8_t *)etick;
2183 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2184 if (rv < 0) {
2185 return -1;
2186 }
2187 if (rv == 0) {
2188 return 2;
2189 }
2190 if (rv == 2) {
2191 renew_ticket = 1;
2192 }
2193 } else {
2194 /* Check key name matches */
2195 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2196 return 2;
2197 }
2198 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2199 NULL) ||
2200 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2201 tctx->tlsext_tick_aes_key, etick + 16)) {
2202 HMAC_CTX_cleanup(&hctx);
2203 EVP_CIPHER_CTX_cleanup(&ctx);
2204 return -1;
2205 }
2206 }
2207
2208 /* Attempt to process session ticket, first conduct sanity and integrity
2209 * checks on ticket. */
2210 mlen = HMAC_size(&hctx);
2211 if (mlen < 0) {
2212 HMAC_CTX_cleanup(&hctx);
2213 EVP_CIPHER_CTX_cleanup(&ctx);
2214 return -1;
2215 }
2216 eticklen -= mlen;
2217 /* Check HMAC of encrypted ticket */
2218 HMAC_Update(&hctx, etick, eticklen);
2219 HMAC_Final(&hctx, tick_hmac, NULL);
2220 HMAC_CTX_cleanup(&hctx);
2221 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2222 EVP_CIPHER_CTX_cleanup(&ctx);
2223 return 2;
2224 }
2225
2226 /* Attempt to decrypt session data */
2227 /* Move p after IV to start of encrypted ticket, update length */
2228 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2229 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2230 sdec = OPENSSL_malloc(eticklen);
2231 if (!sdec) {
2232 EVP_CIPHER_CTX_cleanup(&ctx);
2233 return -1;
2234 }
2235 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2236 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2237 EVP_CIPHER_CTX_cleanup(&ctx);
2238 OPENSSL_free(sdec);
2239 return 2;
2240 }
2241 slen += mlen;
2242 EVP_CIPHER_CTX_cleanup(&ctx);
2243 p = sdec;
2244
2245 sess = d2i_SSL_SESSION(NULL, &p, slen);
2246 OPENSSL_free(sdec);
2247 if (sess) {
2248 /* The session ID, if non-empty, is used by some clients to detect that the
2249 * ticket has been accepted. So we copy it to the session structure. If it
2250 * is empty set length to zero as required by standard. */
2251 if (sesslen) {
2252 memcpy(sess->session_id, sess_id, sesslen);
2253 }
2254 sess->session_id_length = sesslen;
2255 *psess = sess;
2256 if (renew_ticket) {
2257 return 4;
2258 }
2259 return 3;
2260 }
2261
2262 ERR_clear_error();
2263 /* For session parse failure, indicate that we need to send a new ticket. */
2264 return 2;
2265}
Adam Langley95c29f32014-06-20 12:00:00 -07002266
2267/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002268typedef struct {
2269 int nid;
2270 int id;
2271} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2274 {NID_sha1, TLSEXT_hash_sha1},
2275 {NID_sha224, TLSEXT_hash_sha224},
2276 {NID_sha256, TLSEXT_hash_sha256},
2277 {NID_sha384, TLSEXT_hash_sha384},
2278 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002279
Adam Langleyfcf25832014-12-18 17:42:32 -08002280static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2281 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2284 size_t i;
2285 for (i = 0; i < tlen; i++) {
2286 if (table[i].nid == nid) {
2287 return table[i].id;
2288 }
2289 }
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 return -1;
2292}
Adam Langley95c29f32014-06-20 12:00:00 -07002293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2295 size_t i;
2296 for (i = 0; i < tlen; i++) {
2297 if (table[i].id == id) {
2298 return table[i].nid;
2299 }
2300 }
Adam Langley95c29f32014-06-20 12:00:00 -07002301
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 return NID_undef;
2303}
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2306 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002307
Adam Langleyfcf25832014-12-18 17:42:32 -08002308 if (!md) {
2309 return 0;
2310 }
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2313 sizeof(tls12_md) / sizeof(tls12_lookup));
2314 if (md_id == -1) {
2315 return 0;
2316 }
Adam Langley95c29f32014-06-20 12:00:00 -07002317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 sig_id = tls12_get_sigid(pk);
2319 if (sig_id == -1) {
2320 return 0;
2321 }
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 p[0] = (uint8_t)md_id;
2324 p[1] = (uint8_t)sig_id;
2325 return 1;
2326}
2327
2328int tls12_get_sigid(const EVP_PKEY *pk) {
2329 return tls12_find_id(pk->type, tls12_sig,
2330 sizeof(tls12_sig) / sizeof(tls12_lookup));
2331}
2332
2333const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2334 switch (hash_alg) {
2335 case TLSEXT_hash_md5:
2336 return EVP_md5();
2337
2338 case TLSEXT_hash_sha1:
2339 return EVP_sha1();
2340
2341 case TLSEXT_hash_sha224:
2342 return EVP_sha224();
2343
2344 case TLSEXT_hash_sha256:
2345 return EVP_sha256();
2346
2347 case TLSEXT_hash_sha384:
2348 return EVP_sha384();
2349
2350 case TLSEXT_hash_sha512:
2351 return EVP_sha512();
2352
2353 default:
2354 return NULL;
2355 }
2356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
David Benjaminec2f27d2014-11-13 19:17:25 -05002358/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2359 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002360static int tls12_get_pkey_type(uint8_t sig_alg) {
2361 switch (sig_alg) {
2362 case TLSEXT_signature_rsa:
2363 return EVP_PKEY_RSA;
2364
2365 case TLSEXT_signature_ecdsa:
2366 return EVP_PKEY_EC;
2367
2368 default:
2369 return -1;
2370 }
2371}
Adam Langley95c29f32014-06-20 12:00:00 -07002372
2373/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2374static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 int *psignhash_nid, const uint8_t *data) {
2376 int sign_nid = 0, hash_nid = 0;
2377 if (!phash_nid && !psign_nid && !psignhash_nid) {
2378 return;
2379 }
2380
2381 if (phash_nid || psignhash_nid) {
2382 hash_nid = tls12_find_nid(data[0], tls12_md,
2383 sizeof(tls12_md) / sizeof(tls12_lookup));
2384 if (phash_nid) {
2385 *phash_nid = hash_nid;
2386 }
2387 }
2388
2389 if (psign_nid || psignhash_nid) {
2390 sign_nid = tls12_find_nid(data[1], tls12_sig,
2391 sizeof(tls12_sig) / sizeof(tls12_lookup));
2392 if (psign_nid) {
2393 *psign_nid = sign_nid;
2394 }
2395 }
2396
2397 if (psignhash_nid) {
2398 if (sign_nid && hash_nid) {
2399 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2400 } else {
2401 *psignhash_nid = NID_undef;
2402 }
2403 }
2404}
2405
Adam Langley95c29f32014-06-20 12:00:00 -07002406/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002407static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2408 size_t preflen, const uint8_t *allow,
2409 size_t allowlen) {
2410 const uint8_t *ptmp, *atmp;
2411 size_t i, j, nmatch = 0;
2412
2413 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2414 /* Skip disabled hashes or signature algorithms */
2415 if (tls12_get_hash(ptmp[0]) == NULL ||
2416 tls12_get_pkey_type(ptmp[1]) == -1) {
2417 continue;
2418 }
2419
2420 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2421 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2422 nmatch++;
2423 if (shsig) {
2424 shsig->rhash = ptmp[0];
2425 shsig->rsign = ptmp[1];
2426 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2427 &shsig->signandhash_nid, ptmp);
2428 shsig++;
2429 }
2430
2431 break;
2432 }
2433 }
2434 }
2435
2436 return nmatch;
2437}
Adam Langley95c29f32014-06-20 12:00:00 -07002438
2439/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002440static int tls1_set_shared_sigalgs(SSL *s) {
2441 const uint8_t *pref, *allow, *conf;
2442 size_t preflen, allowlen, conflen;
2443 size_t nmatch;
2444 TLS_SIGALGS *salgs = NULL;
2445 CERT *c = s->cert;
2446
2447 if (c->shared_sigalgs) {
2448 OPENSSL_free(c->shared_sigalgs);
2449 c->shared_sigalgs = NULL;
2450 }
2451
2452 /* If client use client signature algorithms if not NULL */
2453 if (!s->server && c->client_sigalgs) {
2454 conf = c->client_sigalgs;
2455 conflen = c->client_sigalgslen;
2456 } else if (c->conf_sigalgs) {
2457 conf = c->conf_sigalgs;
2458 conflen = c->conf_sigalgslen;
2459 } else {
2460 conflen = tls12_get_psigalgs(s, &conf);
2461 }
2462
2463 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2464 pref = conf;
2465 preflen = conflen;
2466 allow = c->peer_sigalgs;
2467 allowlen = c->peer_sigalgslen;
2468 } else {
2469 allow = conf;
2470 allowlen = conflen;
2471 pref = c->peer_sigalgs;
2472 preflen = c->peer_sigalgslen;
2473 }
2474
2475 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2476 if (!nmatch) {
2477 return 1;
2478 }
2479
2480 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2481 if (!salgs) {
2482 return 0;
2483 }
2484
2485 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2486 c->shared_sigalgs = salgs;
2487 c->shared_sigalgslen = nmatch;
2488 return 1;
2489}
Adam Langley95c29f32014-06-20 12:00:00 -07002490
2491/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002492int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2493 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002494
Adam Langleyfcf25832014-12-18 17:42:32 -08002495 /* Extension ignored for inappropriate versions */
2496 if (!SSL_USE_SIGALGS(s)) {
2497 return 1;
2498 }
David Benjamincd996942014-07-20 16:23:51 -04002499
Adam Langleyfcf25832014-12-18 17:42:32 -08002500 /* Length must be even */
2501 if (CBS_len(sigalgs) % 2 != 0) {
2502 return 0;
2503 }
Adam Langley95c29f32014-06-20 12:00:00 -07002504
Adam Langleyfcf25832014-12-18 17:42:32 -08002505 /* Should never happen */
2506 if (!c) {
2507 return 0;
2508 }
Adam Langley95c29f32014-06-20 12:00:00 -07002509
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen)) {
2511 return 0;
2512 }
Adam Langley95c29f32014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 tls1_set_shared_sigalgs(s);
2515 return 1;
2516}
David Benjaminec2f27d2014-11-13 19:17:25 -05002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2519 CERT *c = s->cert;
2520 int type = EVP_PKEY_id(pkey);
2521 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523 /* Select the first shared digest supported by our key. */
2524 for (i = 0; i < c->shared_sigalgslen; i++) {
2525 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2526 if (md == NULL ||
2527 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2528 !EVP_PKEY_supports_digest(pkey, md)) {
2529 continue;
2530 }
2531 return md;
2532 }
Adam Langley95c29f32014-06-20 12:00:00 -07002533
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 /* If no suitable digest may be found, default to SHA-1. */
2535 return EVP_sha1();
2536}
Adam Langley95c29f32014-06-20 12:00:00 -07002537
Adam Langleyfcf25832014-12-18 17:42:32 -08002538int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2539 uint8_t *rsig, uint8_t *rhash) {
2540 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542 if (psig == NULL) {
2543 return 0;
2544 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 if (idx >= 0) {
2547 idx <<= 1;
2548 if (idx >= (int)s->cert->peer_sigalgslen) {
2549 return 0;
2550 }
2551 psig += idx;
2552 if (rhash) {
2553 *rhash = psig[0];
2554 }
2555 if (rsig) {
2556 *rsig = psig[1];
2557 }
2558 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2559 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 return s->cert->peer_sigalgslen / 2;
2562}
Adam Langley1258b6a2014-06-20 12:00:00 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2565 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2566 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2569 return 0;
2570 }
2571
2572 shsigalgs += idx;
2573 if (phash) {
2574 *phash = shsigalgs->hash_nid;
2575 }
2576 if (psign) {
2577 *psign = shsigalgs->sign_nid;
2578 }
2579 if (psignhash) {
2580 *psignhash = shsigalgs->signandhash_nid;
2581 }
2582 if (rsig) {
2583 *rsig = shsigalgs->rsign;
2584 }
2585 if (rhash) {
2586 *rhash = shsigalgs->rhash;
2587 }
2588
2589 return s->cert->shared_sigalgslen;
2590}
2591
2592/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2593 * given SSL connection and writes it to |md|. */
2594int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2595 EVP_MD_CTX ctx;
2596 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2597 unsigned temp_digest_len;
2598 int i;
2599 static const char kClientIDMagic[] = "TLS Channel ID signature";
2600
2601 if (s->s3->handshake_buffer &&
2602 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2603 return 0;
2604 }
2605
2606 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2607
2608 if (s->hit && s->s3->tlsext_channel_id_new) {
2609 static const char kResumptionMagic[] = "Resumption";
2610 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2611 if (s->session->original_handshake_hash_len == 0) {
2612 return 0;
2613 }
2614 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2615 s->session->original_handshake_hash_len);
2616 }
2617
2618 EVP_MD_CTX_init(&ctx);
2619 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2620 if (s->s3->handshake_dgst[i] == NULL) {
2621 continue;
2622 }
2623 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2624 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2625 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2626 }
2627 EVP_MD_CTX_cleanup(&ctx);
2628
2629 return 1;
2630}
Adam Langley1258b6a2014-06-20 12:00:00 -07002631
2632/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2633 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002634int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2635 int digest_len;
2636 /* This function should never be called for a resumed session because the
2637 * handshake hashes that we wish to record are for the original, full
2638 * handshake. */
2639 if (s->hit) {
2640 return -1;
2641 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 /* It only makes sense to call this function if Channel IDs have been
2644 * negotiated. */
2645 if (!s->s3->tlsext_channel_id_new) {
2646 return -1;
2647 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002648
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 digest_len =
2650 tls1_handshake_digest(s, s->session->original_handshake_hash,
2651 sizeof(s->session->original_handshake_hash));
2652 if (digest_len < 0) {
2653 return -1;
2654 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002655
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002657
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 return 1;
2659}
Adam Langley95c29f32014-06-20 12:00:00 -07002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2662 int client) {
2663 uint8_t *sigalgs, *sptr;
2664 int rhash, rsign;
2665 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002666
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 if (salglen & 1) {
2668 return 0;
2669 }
Adam Langley95c29f32014-06-20 12:00:00 -07002670
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 sigalgs = OPENSSL_malloc(salglen);
2672 if (sigalgs == NULL) {
2673 return 0;
2674 }
Adam Langley95c29f32014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2677 rhash = tls12_find_id(*psig_nids++, tls12_md,
2678 sizeof(tls12_md) / sizeof(tls12_lookup));
2679 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2680 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002681
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 if (rhash == -1 || rsign == -1) {
2683 goto err;
2684 }
2685 *sptr++ = rhash;
2686 *sptr++ = rsign;
2687 }
2688
2689 if (client) {
2690 if (c->client_sigalgs) {
2691 OPENSSL_free(c->client_sigalgs);
2692 }
2693 c->client_sigalgs = sigalgs;
2694 c->client_sigalgslen = salglen;
2695 } else {
2696 if (c->conf_sigalgs) {
2697 OPENSSL_free(c->conf_sigalgs);
2698 }
2699 c->conf_sigalgs = sigalgs;
2700 c->conf_sigalgslen = salglen;
2701 }
2702
2703 return 1;
2704
2705err:
2706 OPENSSL_free(sigalgs);
2707 return 0;
2708}