blob: 339c765fc41cca24a17691ab10426efd48b8106d [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 Benjamin52e5bac2014-12-27 02:27:35 -0500352struct tls_curve {
353 uint16_t curve_id;
354 int nid;
355};
356
David Benjamin70bd80a2014-12-27 03:06:46 -0500357/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500358static const struct tls_curve tls_curves[] = {
359 {21, NID_secp224r1},
360 {23, NID_X9_62_prime256v1},
361 {24, NID_secp384r1},
362 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800363};
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365static const uint8_t ecformats_default[] = {
366 TLSEXT_ECPOINTFORMAT_uncompressed,
367};
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369static const uint16_t eccurves_default[] = {
David Benjamin52e5bac2014-12-27 02:27:35 -0500370 23, /* X9_64_prime256v1 */
371 24, /* secp384r1 */
372 25, /* secp521r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800373};
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500376 size_t i;
377 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
378 if (curve_id == tls_curves[i].curve_id) {
379 return tls_curves[i].nid;
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500382 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383}
Adam Langley95c29f32014-06-20 12:00:00 -0700384
David Benjamin70bd80a2014-12-27 03:06:46 -0500385int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500387 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
388 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500389 *out_curve_id = tls_curves[i].curve_id;
390 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 }
392 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 return 0;
394}
395
396/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
397 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
398 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400399static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 const uint16_t **out_curve_ids,
401 size_t *out_curve_ids_len) {
402 if (get_peer_curves) {
403 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
404 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
405 return;
406 }
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 *out_curve_ids = s->tlsext_ellipticcurvelist;
409 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
410 if (!*out_curve_ids) {
411 *out_curve_ids = eccurves_default;
412 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
413 }
414}
David Benjamined439582014-07-14 19:13:02 -0400415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
417 uint8_t curve_type;
418 uint16_t curve_id;
419 const uint16_t *curves;
420 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 /* Only support named curves. */
423 if (!CBS_get_u8(cbs, &curve_type) ||
424 curve_type != NAMED_CURVE_TYPE ||
425 !CBS_get_u16(cbs, &curve_id)) {
426 return 0;
427 }
David Benjamined439582014-07-14 19:13:02 -0400428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 tls1_get_curvelist(s, 0, &curves, &curves_len);
430 for (i = 0; i < curves_len; i++) {
431 if (curve_id == curves[i]) {
432 *out_curve_id = curve_id;
433 return 1;
434 }
435 }
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 return 0;
438}
David Benjamin072334d2014-07-13 16:24:27 -0400439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440int tls1_get_shared_curve(SSL *s) {
441 const uint16_t *pref, *supp;
442 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400443
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 /* Can't do anything on client side */
445 if (s->server == 0) {
446 return NID_undef;
447 }
448
449 /* Return first preference shared curve */
450 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp,
451 &supplen);
452 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
453 &preflen);
454
455 for (i = 0; i < preflen; i++) {
456 for (j = 0; j < supplen; j++) {
457 if (pref[i] == supp[j]) {
458 return tls1_ec_curve_id2nid(pref[i]);
459 }
460 }
461 }
462
463 return NID_undef;
464}
Adam Langley95c29f32014-06-20 12:00:00 -0700465
David Benjamin072334d2014-07-13 16:24:27 -0400466int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 const int *curves, size_t ncurves) {
468 uint16_t *curve_ids;
469 size_t i;
470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
472 if (curve_ids == NULL) {
473 return 0;
474 }
475
476 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500477 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 OPENSSL_free(curve_ids);
479 return 0;
480 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 }
482
483 if (*out_curve_ids) {
484 OPENSSL_free(*out_curve_ids);
485 }
486 *out_curve_ids = curve_ids;
487 *out_curve_ids_len = ncurves;
488
489 return 1;
490}
Adam Langley95c29f32014-06-20 12:00:00 -0700491
David Benjamin072334d2014-07-13 16:24:27 -0400492/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
493 * TLS curve ID and point format, respectively, for |ec|. It returns one on
494 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800495static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
496 uint8_t *out_comp_id, EC_KEY *ec) {
497 int nid;
498 uint16_t id;
499 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 if (ec == NULL) {
502 return 0;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 grp = EC_KEY_get0_group(ec);
506 if (grp == NULL) {
507 return 0;
508 }
David Benjamin072334d2014-07-13 16:24:27 -0400509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 /* Determine curve ID */
511 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500512 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 return 0;
514 }
David Benjamin072334d2014-07-13 16:24:27 -0400515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
517 *out_curve_id = id;
518
519 if (out_comp_id) {
520 if (EC_KEY_get0_public_key(ec) == NULL) {
521 return 0;
522 }
523 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
524 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
525 } else {
526 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
527 }
528 }
529
530 return 1;
531}
David Benjamin072334d2014-07-13 16:24:27 -0400532
David Benjamin42e9a772014-09-02 23:18:44 -0400533/* tls1_check_point_format returns one if |comp_id| is consistent with the
534 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800535static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
536 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
537 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
538 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 /* If point formats extension present check it, otherwise everything is
541 * supported (see RFC4492). */
542 if (p == NULL) {
543 return 1;
544 }
David Benjamin42e9a772014-09-02 23:18:44 -0400545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 for (i = 0; i < plen; i++) {
547 if (comp_id == p[i]) {
548 return 1;
549 }
550 }
David Benjamin42e9a772014-09-02 23:18:44 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 return 0;
553}
554
555/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
556 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400557 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800558static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
559 const uint16_t *curves;
560 size_t curves_len, i, j;
David Benjamin42e9a772014-09-02 23:18:44 -0400561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 /* Check against our list, then the peer's list. */
563 for (j = 0; j <= 1; j++) {
564 tls1_get_curvelist(s, j, &curves, &curves_len);
565 for (i = 0; i < curves_len; i++) {
566 if (curves[i] == curve_id) {
567 break;
568 }
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 if (i == curves_len) {
572 return 0;
573 }
Adam Langley95c29f32014-06-20 12:00:00 -0700574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 /* Servers do not present a preference list so, if we are a client, only
576 * check our list. */
577 if (!s->server) {
578 return 1;
579 }
580 }
David Benjamin033e5f42014-11-13 18:47:41 -0500581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 return 1;
583}
David Benjamin033e5f42014-11-13 18:47:41 -0500584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
586 size_t *pformatslen) {
587 /* If we have a custom point format list use it otherwise use default */
588 if (s->tlsext_ecpointformatlist) {
589 *pformats = s->tlsext_ecpointformatlist;
590 *pformatslen = s->tlsext_ecpointformatlist_length;
591 } else {
592 *pformats = ecformats_default;
593 *pformatslen = sizeof(ecformats_default);
594 }
595}
596
597int tls1_check_ec_cert(SSL *s, X509 *x) {
598 int ret = 0;
599 EVP_PKEY *pkey = X509_get_pubkey(x);
600 uint16_t curve_id;
601 uint8_t comp_id;
602
603 if (!pkey ||
604 pkey->type != EVP_PKEY_EC ||
605 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
606 !tls1_check_curve_id(s, curve_id) ||
607 !tls1_check_point_format(s, comp_id)) {
608 goto done;
609 }
610
611 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500612
613done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 if (pkey) {
615 EVP_PKEY_free(pkey);
616 }
617 return ret;
618}
David Benjamin42e9a772014-09-02 23:18:44 -0400619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620int tls1_check_ec_tmp_key(SSL *s) {
621 uint16_t curve_id;
622 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 if (s->cert->ecdh_tmp_auto) {
625 /* Need a shared curve */
626 return tls1_get_shared_curve(s) != NID_undef;
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 if (!ec) {
630 if (s->cert->ecdh_tmp_cb) {
631 return 1;
632 }
633 return 0;
634 }
635
636 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
637 tls1_check_curve_id(s, curve_id);
638}
Adam Langley95c29f32014-06-20 12:00:00 -0700639
640/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langley95c29f32014-06-20 12:00:00 -0700643#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langley95c29f32014-06-20 12:00:00 -0700645#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjamincff64722014-08-19 19:54:46 -0400649static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 tlsext_sigalg(TLSEXT_hash_sha512)
651 tlsext_sigalg(TLSEXT_hash_sha384)
652 tlsext_sigalg(TLSEXT_hash_sha256)
653 tlsext_sigalg(TLSEXT_hash_sha224)
654 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700655};
David Benjamin05da6e12014-07-12 20:42:55 -0400656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
658 /* If server use client authentication sigalgs if not NULL */
659 if (s->server && s->cert->client_sigalgs) {
660 *psigs = s->cert->client_sigalgs;
661 return s->cert->client_sigalgslen;
662 } else if (s->cert->conf_sigalgs) {
663 *psigs = s->cert->conf_sigalgs;
664 return s->cert->conf_sigalgslen;
665 } else {
666 *psigs = tls12_sigalgs;
667 return sizeof(tls12_sigalgs);
668 }
669}
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
672 * checks it is consistent with |s|'s sent supported signature algorithms and,
673 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
674 * returns 0 and writes an alert into |*out_alert|. */
675int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
676 CBS *cbs, EVP_PKEY *pkey) {
677 const uint8_t *sent_sigs;
678 size_t sent_sigslen, i;
679 int sigalg = tls12_get_sigid(pkey);
680 uint8_t hash, signature;
681
682 /* Should never happen */
683 if (sigalg == -1) {
684 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
685 *out_alert = SSL_AD_INTERNAL_ERROR;
686 return 0;
687 }
688
689 if (!CBS_get_u8(cbs, &hash) ||
690 !CBS_get_u8(cbs, &signature)) {
691 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
692 *out_alert = SSL_AD_DECODE_ERROR;
693 return 0;
694 }
695
696 /* Check key type is consistent with signature */
697 if (sigalg != signature) {
698 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
699 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
700 return 0;
701 }
702
703 if (pkey->type == EVP_PKEY_EC) {
704 uint16_t curve_id;
705 uint8_t comp_id;
706 /* Check compression and curve matches extensions */
707 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
708 *out_alert = SSL_AD_INTERNAL_ERROR;
709 return 0;
710 }
711
712 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
713 !tls1_check_point_format(s, comp_id))) {
714 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
715 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
716 return 0;
717 }
718 }
719
720 /* Check signature matches a type we sent */
721 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
722 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
723 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
724 break;
725 }
726 }
727
728 /* Allow fallback to SHA-1. */
729 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
730 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
731 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
732 return 0;
733 }
734
735 *out_md = tls12_get_hash(hash);
736 if (*out_md == NULL) {
737 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
738 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
739 return 0;
740 }
741
742 return 1;
743}
744
745/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
746 * supported or doesn't appear in supported signature algorithms. Unlike
747 * ssl_cipher_get_disabled this applies to a specific session and not global
748 * settings. */
749void ssl_set_client_disabled(SSL *s) {
750 CERT *c = s->cert;
751 const uint8_t *sigalgs;
752 size_t i, sigalgslen;
753 int have_rsa = 0, have_ecdsa = 0;
754 c->mask_a = 0;
755 c->mask_k = 0;
756
757 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
758 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
759 c->mask_ssl = SSL_TLSV1_2;
760 } else {
761 c->mask_ssl = 0;
762 }
763
764 /* Now go through all signature algorithms seeing if we support any for RSA,
765 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
766 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
767 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
768 switch (sigalgs[1]) {
769 case TLSEXT_signature_rsa:
770 have_rsa = 1;
771 break;
772
773 case TLSEXT_signature_ecdsa:
774 have_ecdsa = 1;
775 break;
776 }
777 }
778
779 /* Disable auth if we don't include any appropriate signature algorithms. */
780 if (!have_rsa) {
781 c->mask_a |= SSL_aRSA;
782 }
783 if (!have_ecdsa) {
784 c->mask_a |= SSL_aECDSA;
785 }
786
787 /* with PSK there must be client callback set */
788 if (!s->psk_client_callback) {
789 c->mask_a |= SSL_aPSK;
790 c->mask_k |= SSL_kPSK;
791 }
792}
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyb0c235e2014-06-20 12:00:00 -0700794/* header_len is the length of the ClientHello header written so far, used to
795 * compute padding. It does not include the record header. Pass 0 if no padding
796 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800797uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
798 size_t header_len) {
799 int extdatalen = 0;
800 uint8_t *ret = buf;
801 uint8_t *orig = buf;
802 /* See if we support any ECC ciphersuites */
803 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
806 size_t i;
807 unsigned long alg_k, alg_a;
808 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
811 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 alg_k = c->algorithm_mkey;
814 alg_a = c->algorithm_auth;
815 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) {
816 using_ecc = 1;
817 break;
818 }
819 }
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 /* don't add extensions for SSLv3 unless doing secure renegotiation */
823 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
824 return orig;
825 }
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 if (ret >= limit) {
830 return NULL; /* should never occur. */
831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 if (s->tlsext_hostname != NULL) {
834 /* Add TLS extension servername to the Client Hello message */
835 unsigned long size_str;
836 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 /* check for enough space.
839 4 for the servername type and entension length
840 2 for servernamelist length
841 1 for the hostname type
842 2 for hostname length
843 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 lenmax = limit - ret - 9;
846 size_str = strlen(s->tlsext_hostname);
847 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
848 return NULL;
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 /* extension type and length */
852 s2n(TLSEXT_TYPE_server_name, ret);
853 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 /* length of servername list */
856 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 /* hostname type, length and hostname */
859 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
860 s2n(size_str, ret);
861 memcpy(ret, s->tlsext_hostname, size_str);
862 ret += size_str;
863 }
Adam Langley75712922014-10-10 16:23:43 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 /* Add RI if renegotiating */
866 if (s->renegotiate) {
867 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
870 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
871 return NULL;
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 if ((limit - ret - 4 - el) < 0) {
875 return NULL;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 s2n(TLSEXT_TYPE_renegotiate, ret);
879 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
882 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
883 return NULL;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 ret += el;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 /* Add extended master secret. */
890 if (s->version != SSL3_VERSION) {
891 if (limit - ret - 4 < 0) {
892 return NULL;
893 }
894 s2n(TLSEXT_TYPE_extended_master_secret, ret);
895 s2n(0, ret);
896 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
899 int ticklen = 0;
900 if (!s->new_session && s->session && s->session->tlsext_tick) {
901 ticklen = s->session->tlsext_ticklen;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 /* Check for enough room 2 for extension type, 2 for len rest for
905 * ticket. */
906 if ((long)(limit - ret - 4 - ticklen) < 0) {
907 return NULL;
908 }
909 s2n(TLSEXT_TYPE_session_ticket, ret);
910 s2n(ticklen, ret);
911 if (ticklen) {
912 memcpy(ret, s->session->tlsext_tick, ticklen);
913 ret += ticklen;
914 }
915 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700916
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 if (SSL_USE_SIGALGS(s)) {
918 size_t salglen;
919 const uint8_t *salg;
920 salglen = tls12_get_psigalgs(s, &salg);
921 if ((size_t)(limit - ret) < salglen + 6) {
922 return NULL;
923 }
924 s2n(TLSEXT_TYPE_signature_algorithms, ret);
925 s2n(salglen + 2, ret);
926 s2n(salglen, ret);
927 memcpy(ret, salg, salglen);
928 ret += salglen;
929 }
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 if (s->ocsp_stapling_enabled) {
932 /* The status_request extension is excessively extensible at every layer.
933 * On the client, only support requesting OCSP responses with an empty
934 * responder_id_list and no extensions. */
935 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
936 return NULL;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 s2n(TLSEXT_TYPE_status_request, ret);
940 s2n(1 + 2 + 2, ret);
941 /* status_type */
942 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
943 /* responder_id_list - empty */
944 s2n(0, ret);
945 /* request_extensions - empty */
946 s2n(0, ret);
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
950 /* The client advertises an emtpy extension to indicate its support for
951 * Next Protocol Negotiation */
952 if (limit - ret - 4 < 0) {
953 return NULL;
954 }
955 s2n(TLSEXT_TYPE_next_proto_neg, ret);
956 s2n(0, ret);
957 }
Adam Langley95c29f32014-06-20 12:00:00 -0700958
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
960 /* The client advertises an empty extension to indicate its support for
961 * certificate timestamps. */
962 if (limit - ret - 4 < 0) {
963 return NULL;
964 }
965 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
966 s2n(0, ret);
967 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
970 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
971 return NULL;
972 }
973 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
974 s2n(2 + s->alpn_client_proto_list_len, ret);
975 s2n(s->alpn_client_proto_list_len, ret);
976 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
977 ret += s->alpn_client_proto_list_len;
978 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700979
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 if (s->tlsext_channel_id_enabled) {
981 /* The client advertises an emtpy extension to indicate its support for
982 * Channel ID. */
983 if (limit - ret - 4 < 0) {
984 return NULL;
985 }
986 if (s->ctx->tlsext_channel_id_enabled_new) {
987 s2n(TLSEXT_TYPE_channel_id_new, ret);
988 } else {
989 s2n(TLSEXT_TYPE_channel_id, ret);
990 }
991 s2n(0, ret);
992 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700993
Adam Langleyfcf25832014-12-18 17:42:32 -0800994 if (SSL_get_srtp_profiles(s)) {
995 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -0700996
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 if ((limit - ret - 4 - el) < 0) {
1000 return NULL;
1001 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 s2n(TLSEXT_TYPE_use_srtp, ret);
1004 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1007 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1008 return NULL;
1009 }
1010 ret += el;
1011 }
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 if (using_ecc) {
1014 /* Add TLS extension ECPointFormats to the ClientHello message */
1015 long lenmax;
1016 const uint8_t *formats;
1017 const uint16_t *curves;
1018 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 lenmax = limit - ret - 5;
1023 if (lenmax < 0) {
1024 return NULL;
1025 }
1026 if (formats_len > (size_t)lenmax) {
1027 return NULL;
1028 }
1029 if (formats_len > 255) {
1030 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1031 return NULL;
1032 }
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1035 s2n(formats_len + 1, ret);
1036 *(ret++) = (uint8_t)formats_len;
1037 memcpy(ret, formats, formats_len);
1038 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 /* Add TLS extension EllipticCurves to the ClientHello message */
1041 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 lenmax = limit - ret - 6;
1044 if (lenmax < 0) {
1045 return NULL;
1046 }
1047 if (curves_len * 2 > (size_t)lenmax) {
1048 return NULL;
1049 }
1050 if (curves_len * 2 > 65532) {
1051 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1052 return NULL;
1053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1056 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001057
Adam Langleyfcf25832014-12-18 17:42:32 -08001058 s2n(curves_len * 2, ret);
1059 for (i = 0; i < curves_len; i++) {
1060 s2n(curves[i], ret);
1061 }
1062 }
Adam Langley95c29f32014-06-20 12:00:00 -07001063
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 if (header_len > 0) {
1065 size_t clienthello_minsize = 0;
1066 header_len += ret - orig;
1067 if (header_len > 0xff && header_len < 0x200) {
1068 /* Add padding to workaround bugs in F5 terminators. See
1069 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1070 *
1071 * NB: because this code works out the length of all existing extensions
1072 * it MUST always appear last. */
1073 clienthello_minsize = 0x200;
1074 }
1075 if (s->fastradio_padding) {
1076 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1077 * into DCH (high data rate) state in 3G networks. Note that when
1078 * fastradio_padding is enabled, even if the header_len is less than 255
1079 * bytes, the padding will be applied regardless. This is slightly
1080 * different from the TLS padding extension suggested in
1081 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1082 clienthello_minsize = 0x400;
1083 }
1084 if (header_len < clienthello_minsize) {
1085 size_t padding_len = clienthello_minsize - header_len;
1086 /* Extensions take at least four bytes to encode. Always include least
1087 * one byte of data if including the extension. WebSphere Application
1088 * Server 7.0 is intolerant to the last extension being zero-length. */
1089 if (padding_len >= 4 + 1) {
1090 padding_len -= 4;
1091 } else {
1092 padding_len = 1;
1093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 if (limit - ret - 4 - (long)padding_len < 0) {
1096 return NULL;
1097 }
Adam Langley75712922014-10-10 16:23:43 -07001098
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 s2n(TLSEXT_TYPE_padding, ret);
1100 s2n(padding_len, ret);
1101 memset(ret, 0, padding_len);
1102 ret += padding_len;
1103 }
1104 }
Adam Langley75712922014-10-10 16:23:43 -07001105
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 extdatalen = ret - orig - 2;
1107 if (extdatalen == 0) {
1108 return orig;
1109 }
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 s2n(extdatalen, orig);
1112 return ret;
1113}
Adam Langley95c29f32014-06-20 12:00:00 -07001114
Adam Langleyfcf25832014-12-18 17:42:32 -08001115uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1116 int extdatalen = 0;
1117 uint8_t *orig = buf;
1118 uint8_t *ret = buf;
1119 int next_proto_neg_seen;
1120 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1121 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1122 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
1123 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1126 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1127 return orig;
1128 }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 ret += 2;
1131 if (ret >= limit) {
1132 return NULL; /* should never happen. */
1133 }
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1136 if ((long)(limit - ret - 4) < 0) {
1137 return NULL;
1138 }
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 s2n(TLSEXT_TYPE_server_name, ret);
1141 s2n(0, ret);
1142 }
Adam Langley95c29f32014-06-20 12:00:00 -07001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144 if (s->s3->send_connection_binding) {
1145 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1148 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1149 return NULL;
1150 }
Adam Langley95c29f32014-06-20 12:00:00 -07001151
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 if ((limit - ret - 4 - el) < 0) {
1153 return NULL;
1154 }
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 s2n(TLSEXT_TYPE_renegotiate, ret);
1157 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1160 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1161 return NULL;
1162 }
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 ret += el;
1165 }
Adam Langley95c29f32014-06-20 12:00:00 -07001166
Adam Langleyfcf25832014-12-18 17:42:32 -08001167 if (s->s3->tmp.extended_master_secret) {
1168 if ((long)(limit - ret - 4) < 0) {
1169 return NULL;
1170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1173 s2n(0, ret);
1174 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 if (using_ecc) {
1177 const uint8_t *plist;
1178 size_t plistlen;
1179 /* Add TLS extension ECPointFormats to the ServerHello message */
1180 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001181
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 tls1_get_formatlist(s, &plist, &plistlen);
1183
1184 lenmax = limit - ret - 5;
1185 if (lenmax < 0) {
1186 return NULL;
1187 }
1188 if (plistlen > (size_t)lenmax) {
1189 return NULL;
1190 }
1191 if (plistlen > 255) {
1192 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1193 return NULL;
1194 }
1195
1196 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1197 s2n(plistlen + 1, ret);
1198 *(ret++) = (uint8_t)plistlen;
1199 memcpy(ret, plist, plistlen);
1200 ret += plistlen;
1201 }
1202 /* Currently the server should not respond with a SupportedCurves extension */
1203
1204 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1205 if ((long)(limit - ret - 4) < 0) {
1206 return NULL;
1207 }
1208 s2n(TLSEXT_TYPE_session_ticket, ret);
1209 s2n(0, ret);
1210 }
1211
1212 if (s->s3->tmp.certificate_status_expected) {
1213 if ((long)(limit - ret - 4) < 0) {
1214 return NULL;
1215 }
1216 s2n(TLSEXT_TYPE_status_request, ret);
1217 s2n(0, ret);
1218 }
1219
1220 if (s->srtp_profile) {
1221 int el;
1222
1223 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1224
1225 if ((limit - ret - 4 - el) < 0) {
1226 return NULL;
1227 }
1228
1229 s2n(TLSEXT_TYPE_use_srtp, ret);
1230 s2n(el, ret);
1231
1232 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1233 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1234 return NULL;
1235 }
1236 ret += el;
1237 }
1238
1239 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1240 s->s3->next_proto_neg_seen = 0;
1241 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1242 const uint8_t *npa;
1243 unsigned int npalen;
1244 int r;
1245
1246 r = s->ctx->next_protos_advertised_cb(
1247 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1248 if (r == SSL_TLSEXT_ERR_OK) {
1249 if ((long)(limit - ret - 4 - npalen) < 0) {
1250 return NULL;
1251 }
1252 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1253 s2n(npalen, ret);
1254 memcpy(ret, npa, npalen);
1255 ret += npalen;
1256 s->s3->next_proto_neg_seen = 1;
1257 }
1258 }
1259
1260 if (s->s3->alpn_selected) {
1261 const uint8_t *selected = s->s3->alpn_selected;
1262 size_t len = s->s3->alpn_selected_len;
1263
1264 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1265 return NULL;
1266 }
1267 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1268 s2n(3 + len, ret);
1269 s2n(1 + len, ret);
1270 *ret++ = len;
1271 memcpy(ret, selected, len);
1272 ret += len;
1273 }
1274
1275 /* If the client advertised support for Channel ID, and we have it
1276 * enabled, then we want to echo it back. */
1277 if (s->s3->tlsext_channel_id_valid) {
1278 if (limit - ret - 4 < 0) {
1279 return NULL;
1280 }
1281 if (s->s3->tlsext_channel_id_new) {
1282 s2n(TLSEXT_TYPE_channel_id_new, ret);
1283 } else {
1284 s2n(TLSEXT_TYPE_channel_id, ret);
1285 }
1286 s2n(0, ret);
1287 }
1288
1289 extdatalen = ret - orig - 2;
1290 if (extdatalen == 0) {
1291 return orig;
1292 }
1293
1294 s2n(extdatalen, orig);
1295 return ret;
1296}
Adam Langley95c29f32014-06-20 12:00:00 -07001297
Adam Langley95c29f32014-06-20 12:00:00 -07001298/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1299 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001300 * cbs: the contents of the extension, not including the type and length.
1301 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001302 * return.
1303 *
David Benjamindc72ff72014-06-25 12:36:10 -04001304 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001305static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1306 CBS protocol_name_list, protocol_name_list_copy;
1307 const uint8_t *selected;
1308 uint8_t selected_len;
1309 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001310
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 if (s->ctx->alpn_select_cb == NULL) {
1312 return 1;
1313 }
Adam Langley95c29f32014-06-20 12:00:00 -07001314
Adam Langleyfcf25832014-12-18 17:42:32 -08001315 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1316 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1317 goto parse_error;
1318 }
Adam Langley95c29f32014-06-20 12:00:00 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 /* Validate the protocol list. */
1321 protocol_name_list_copy = protocol_name_list;
1322 while (CBS_len(&protocol_name_list_copy) > 0) {
1323 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1326 goto parse_error;
1327 }
1328 }
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 r = s->ctx->alpn_select_cb(
1331 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1332 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1333 if (r == SSL_TLSEXT_ERR_OK) {
1334 if (s->s3->alpn_selected) {
1335 OPENSSL_free(s->s3->alpn_selected);
1336 }
1337 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1338 if (!s->s3->alpn_selected) {
1339 *out_alert = SSL_AD_INTERNAL_ERROR;
1340 return 0;
1341 }
1342 s->s3->alpn_selected_len = selected_len;
1343 }
1344
1345 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001346
1347parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 *out_alert = SSL_AD_DECODE_ERROR;
1349 return 0;
1350}
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1353 int renegotiate_seen = 0;
1354 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001357 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 s->s3->next_proto_neg_seen = 0;
1359 s->s3->tmp.certificate_status_expected = 0;
1360 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001361
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 if (s->s3->alpn_selected) {
1363 OPENSSL_free(s->s3->alpn_selected);
1364 s->s3->alpn_selected = NULL;
1365 }
Adam Langley95c29f32014-06-20 12:00:00 -07001366
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 /* Clear any signature algorithms extension received */
1368 if (s->cert->peer_sigalgs) {
1369 OPENSSL_free(s->cert->peer_sigalgs);
1370 s->cert->peer_sigalgs = NULL;
1371 }
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 /* Clear any shared signature algorithms */
1374 if (s->cert->shared_sigalgs) {
1375 OPENSSL_free(s->cert->shared_sigalgs);
1376 s->cert->shared_sigalgs = NULL;
1377 }
Adam Langley95c29f32014-06-20 12:00:00 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 /* Clear ECC extensions */
1380 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1381 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1382 s->s3->tmp.peer_ecpointformatlist = NULL;
1383 s->s3->tmp.peer_ecpointformatlist_length = 0;
1384 }
David Benjamindc72ff72014-06-25 12:36:10 -04001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1387 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1388 s->s3->tmp.peer_ellipticcurvelist = NULL;
1389 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1390 }
David Benjamindc72ff72014-06-25 12:36:10 -04001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 /* There may be no extensions. */
1393 if (CBS_len(cbs) == 0) {
1394 goto ri_check;
1395 }
David Benjamindc72ff72014-06-25 12:36:10 -04001396
Adam Langleyfcf25832014-12-18 17:42:32 -08001397 /* Decode the extensions block and check it is valid. */
1398 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1399 !tls1_check_duplicate_extensions(&extensions)) {
1400 *out_alert = SSL_AD_DECODE_ERROR;
1401 return 0;
1402 }
David Benjamindc72ff72014-06-25 12:36:10 -04001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 while (CBS_len(&extensions) != 0) {
1405 uint16_t type;
1406 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001407
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 /* Decode the next extension. */
1409 if (!CBS_get_u16(&extensions, &type) ||
1410 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1411 *out_alert = SSL_AD_DECODE_ERROR;
1412 return 0;
1413 }
Adam Langley95c29f32014-06-20 12:00:00 -07001414
Adam Langleyfcf25832014-12-18 17:42:32 -08001415 if (s->tlsext_debug_cb) {
1416 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1417 CBS_len(&extension), s->tlsext_debug_arg);
1418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001421
Adam Langleyfcf25832014-12-18 17:42:32 -08001422 - Only the hostname type is supported with a maximum length of 255.
1423 - The servername is rejected if too long or if it contains zeros, in
1424 which case an fatal alert is generated.
1425 - The servername field is maintained together with the session cache.
1426 - When a session is resumed, the servername call back invoked in order
1427 to allow the application to position itself to the right context.
1428 - The servername is acknowledged if it is new for a session or when
1429 it is identical to a previously used for the same session.
1430 Applications can control the behaviour. They can at any time
1431 set a 'desirable' servername for a new SSL object. This can be the
1432 case for example with HTTPS when a Host: header field is received and
1433 a renegotiation is requested. In this case, a possible servername
1434 presented in the new client hello is only acknowledged if it matches
1435 the value of the Host: field.
1436 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1437 if they provide for changing an explicit servername context for the
1438 session,
1439 i.e. when the session has been established with a servername extension.
1440 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442 if (type == TLSEXT_TYPE_server_name) {
1443 CBS server_name_list;
1444 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1447 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1448 *out_alert = SSL_AD_DECODE_ERROR;
1449 return 0;
1450 }
David Benjamindc72ff72014-06-25 12:36:10 -04001451
Adam Langleyfcf25832014-12-18 17:42:32 -08001452 /* Decode each ServerName in the extension. */
1453 while (CBS_len(&server_name_list) > 0) {
1454 uint8_t name_type;
1455 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001456
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 /* Decode the NameType. */
1458 if (!CBS_get_u8(&server_name_list, &name_type)) {
1459 *out_alert = SSL_AD_DECODE_ERROR;
1460 return 0;
1461 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001462
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 /* Only host_name is supported. */
1464 if (name_type != TLSEXT_NAMETYPE_host_name) {
1465 continue;
1466 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001467
Adam Langleyfcf25832014-12-18 17:42:32 -08001468 if (have_seen_host_name) {
1469 /* The ServerNameList MUST NOT contain more than one name of the same
1470 * name_type. */
1471 *out_alert = SSL_AD_DECODE_ERROR;
1472 return 0;
1473 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001474
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001476
Adam Langleyfcf25832014-12-18 17:42:32 -08001477 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1478 CBS_len(&host_name) < 1) {
1479 *out_alert = SSL_AD_DECODE_ERROR;
1480 return 0;
1481 }
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langleyfcf25832014-12-18 17:42:32 -08001483 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1484 CBS_contains_zero_byte(&host_name)) {
1485 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1486 return 0;
1487 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 if (!s->hit) {
1490 assert(s->session->tlsext_hostname == NULL);
1491 if (s->session->tlsext_hostname) {
1492 /* This should be impossible. */
1493 *out_alert = SSL_AD_DECODE_ERROR;
1494 return 0;
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 /* Copy the hostname as a string. */
1498 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1499 *out_alert = SSL_AD_INTERNAL_ERROR;
1500 return 0;
1501 }
Adam Langley95c29f32014-06-20 12:00:00 -07001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 s->should_ack_sni = 1;
1504 }
1505 }
1506 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1507 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001508
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1510 CBS_len(&extension) != 0) {
1511 *out_alert = SSL_AD_DECODE_ERROR;
1512 return 0;
1513 }
Adam Langley95c29f32014-06-20 12:00:00 -07001514
Adam Langleyfcf25832014-12-18 17:42:32 -08001515 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1516 &s->s3->tmp.peer_ecpointformatlist_length)) {
1517 *out_alert = SSL_AD_INTERNAL_ERROR;
1518 return 0;
1519 }
1520 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1521 CBS elliptic_curve_list;
1522 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1525 CBS_len(&elliptic_curve_list) == 0 ||
1526 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1527 CBS_len(&extension) != 0) {
1528 *out_alert = SSL_AD_DECODE_ERROR;
1529 return 0;
1530 }
David Benjamindc72ff72014-06-25 12:36:10 -04001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 if (s->s3->tmp.peer_ellipticcurvelist) {
1533 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1534 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1535 }
David Benjamindc72ff72014-06-25 12:36:10 -04001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 s->s3->tmp.peer_ellipticcurvelist =
1538 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001539
Adam Langleyfcf25832014-12-18 17:42:32 -08001540 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1541 *out_alert = SSL_AD_INTERNAL_ERROR;
1542 return 0;
1543 }
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 num_curves = CBS_len(&elliptic_curve_list) / 2;
1546 for (i = 0; i < num_curves; i++) {
1547 if (!CBS_get_u16(&elliptic_curve_list,
1548 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1549 *out_alert = SSL_AD_INTERNAL_ERROR;
1550 return 0;
1551 }
1552 }
David Benjamindc72ff72014-06-25 12:36:10 -04001553
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 if (CBS_len(&elliptic_curve_list) != 0) {
1555 *out_alert = SSL_AD_INTERNAL_ERROR;
1556 return 0;
1557 }
Adam Langley95c29f32014-06-20 12:00:00 -07001558
Adam Langleyfcf25832014-12-18 17:42:32 -08001559 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1560 } else if (type == TLSEXT_TYPE_renegotiate) {
1561 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1562 return 0;
1563 }
1564 renegotiate_seen = 1;
1565 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1566 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001567
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 if (!CBS_get_u16_length_prefixed(&extension,
1569 &supported_signature_algorithms) ||
1570 CBS_len(&extension) != 0) {
1571 *out_alert = SSL_AD_DECODE_ERROR;
1572 return 0;
1573 }
Adam Langley95c29f32014-06-20 12:00:00 -07001574
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 /* Ensure the signature algorithms are non-empty. It contains a list of
1576 * SignatureAndHashAlgorithms which are two bytes each. */
1577 if (CBS_len(&supported_signature_algorithms) == 0 ||
1578 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1579 *out_alert = SSL_AD_DECODE_ERROR;
1580 return 0;
1581 }
David Benjamindc72ff72014-06-25 12:36:10 -04001582
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1584 *out_alert = SSL_AD_DECODE_ERROR;
1585 return 0;
1586 }
1587 /* If sigalgs received and no shared algorithms fatal error. */
1588 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
1589 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext,
1590 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1591 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1592 return 0;
1593 }
1594 } else if (type == TLSEXT_TYPE_next_proto_neg &&
1595 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL) {
1596 /* The extension must be empty. */
1597 if (CBS_len(&extension) != 0) {
1598 *out_alert = SSL_AD_DECODE_ERROR;
1599 return 0;
1600 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001601
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 /* We shouldn't accept this extension on a renegotiation.
1603 *
1604 * s->new_session will be set on renegotiation, but we probably shouldn't
1605 * rely that it couldn't be set on the initial renegotation too in
1606 * certain cases (when there's some other reason to disallow resuming an
1607 * earlier session -- the current code won't be doing anything like that,
1608 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 * A valid sign that there's been a previous handshake in this connection
1611 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1612 * that will happen in the Hello protocol round, well before a new
1613 * Finished message could have been computed.) */
1614 s->s3->next_proto_neg_seen = 1;
1615 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1616 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1617 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1618 return 0;
1619 }
1620 /* ALPN takes precedence over NPN. */
1621 s->s3->next_proto_neg_seen = 0;
1622 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled) {
1623 /* The extension must be empty. */
1624 if (CBS_len(&extension) != 0) {
1625 *out_alert = SSL_AD_DECODE_ERROR;
1626 return 0;
1627 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001628
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 s->s3->tlsext_channel_id_valid = 1;
1630 } else if (type == TLSEXT_TYPE_channel_id_new &&
1631 s->tlsext_channel_id_enabled) {
1632 /* The extension must be empty. */
1633 if (CBS_len(&extension) != 0) {
1634 *out_alert = SSL_AD_DECODE_ERROR;
1635 return 0;
1636 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001637
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 s->s3->tlsext_channel_id_valid = 1;
1639 s->s3->tlsext_channel_id_new = 1;
1640 } else if (type == TLSEXT_TYPE_use_srtp) {
1641 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1642 return 0;
1643 }
1644 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1645 s->version != SSL3_VERSION) {
1646 if (CBS_len(&extension) != 0) {
1647 *out_alert = SSL_AD_DECODE_ERROR;
1648 return 0;
1649 }
Adam Langley75712922014-10-10 16:23:43 -07001650
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 s->s3->tmp.extended_master_secret = 1;
1652 }
1653 }
Adam Langley75712922014-10-10 16:23:43 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655ri_check:
1656 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 if (!renegotiate_seen && s->renegotiate &&
1659 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1660 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1661 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1662 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1663 return 0;
1664 }
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 return 1;
1667}
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1670 int alert = -1;
1671 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1672 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1673 return 0;
1674 }
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 if (ssl_check_clienthello_tlsext(s) <= 0) {
1677 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1678 SSL_R_CLIENTHELLO_TLSEXT);
1679 return 0;
1680 }
Adam Langley95c29f32014-06-20 12:00:00 -07001681
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 return 1;
1683}
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langley95c29f32014-06-20 12:00:00 -07001685/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 * elements of zero length are allowed and the set of elements must exactly
1687 * fill the length of the block. */
1688static char ssl_next_proto_validate(const CBS *cbs) {
1689 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 while (CBS_len(&copy) != 0) {
1692 CBS proto;
1693 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1694 return 0;
1695 }
1696 }
Adam Langley95c29f32014-06-20 12:00:00 -07001697
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 return 1;
1699}
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1702 int tlsext_servername = 0;
1703 int renegotiate_seen = 0;
1704 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 /* TODO(davidben): Move all of these to some per-handshake state that gets
1707 * systematically reset on a new handshake; perhaps allocate it fresh each
1708 * time so it's not even kept around post-handshake. */
1709 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 s->tlsext_ticket_expected = 0;
1711 s->s3->tmp.certificate_status_expected = 0;
1712 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001713 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 if (s->s3->alpn_selected) {
1716 OPENSSL_free(s->s3->alpn_selected);
1717 s->s3->alpn_selected = NULL;
1718 }
David Benjamina19fc252014-10-19 00:14:36 -04001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 /* Clear ECC extensions */
1721 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1722 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1723 s->s3->tmp.peer_ecpointformatlist = NULL;
1724 s->s3->tmp.peer_ecpointformatlist_length = 0;
1725 }
David Benjamin03973092014-06-24 23:27:17 -04001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 /* There may be no extensions. */
1728 if (CBS_len(cbs) == 0) {
1729 goto ri_check;
1730 }
Adam Langley95c29f32014-06-20 12:00:00 -07001731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 /* Decode the extensions block and check it is valid. */
1733 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1734 !tls1_check_duplicate_extensions(&extensions)) {
1735 *out_alert = SSL_AD_DECODE_ERROR;
1736 return 0;
1737 }
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 while (CBS_len(&extensions) != 0) {
1740 uint16_t type;
1741 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 /* Decode the next extension. */
1744 if (!CBS_get_u16(&extensions, &type) ||
1745 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1746 *out_alert = SSL_AD_DECODE_ERROR;
1747 return 0;
1748 }
Adam Langley95c29f32014-06-20 12:00:00 -07001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 if (s->tlsext_debug_cb) {
1751 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1752 CBS_len(&extension), s->tlsext_debug_arg);
1753 }
Adam Langley95c29f32014-06-20 12:00:00 -07001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 if (type == TLSEXT_TYPE_server_name) {
1756 /* The extension must be empty. */
1757 if (CBS_len(&extension) != 0) {
1758 *out_alert = SSL_AD_DECODE_ERROR;
1759 return 0;
1760 }
David Benjamin03973092014-06-24 23:27:17 -04001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 /* We must have sent it in ClientHello. */
1763 if (s->tlsext_hostname == NULL) {
1764 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1765 return 0;
1766 }
David Benjamin03973092014-06-24 23:27:17 -04001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 tlsext_servername = 1;
1769 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1770 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1773 CBS_len(&extension) != 0) {
1774 *out_alert = SSL_AD_DECODE_ERROR;
1775 return 0;
1776 }
Adam Langley95c29f32014-06-20 12:00:00 -07001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1779 &s->s3->tmp.peer_ecpointformatlist_length)) {
1780 *out_alert = SSL_AD_INTERNAL_ERROR;
1781 return 0;
1782 }
1783 } else if (type == TLSEXT_TYPE_session_ticket) {
1784 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1785 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1786 return 0;
1787 }
David Benjamin03973092014-06-24 23:27:17 -04001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 s->tlsext_ticket_expected = 1;
1790 } else if (type == TLSEXT_TYPE_status_request) {
1791 /* The extension MUST be empty and may only sent if we've requested a
1792 * status request message. */
1793 if (CBS_len(&extension) != 0) {
1794 *out_alert = SSL_AD_DECODE_ERROR;
1795 return 0;
1796 }
David Benjamin03973092014-06-24 23:27:17 -04001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 if (!s->ocsp_stapling_enabled) {
1799 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1800 return 0;
1801 }
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 /* Set a flag to expect a CertificateStatus message */
1804 s->s3->tmp.certificate_status_expected = 1;
1805 } else if (type == TLSEXT_TYPE_next_proto_neg &&
1806 s->s3->tmp.finish_md_len == 0) {
1807 uint8_t *selected;
1808 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 /* We must have requested it. */
1811 if (s->ctx->next_proto_select_cb == NULL) {
1812 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1813 return 0;
1814 }
David Benjamin03973092014-06-24 23:27:17 -04001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 /* The data must be valid. */
1817 if (!ssl_next_proto_validate(&extension)) {
1818 *out_alert = SSL_AD_DECODE_ERROR;
1819 return 0;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 if (s->ctx->next_proto_select_cb(
1823 s, &selected, &selected_len, CBS_data(&extension),
1824 CBS_len(&extension),
1825 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1826 *out_alert = SSL_AD_INTERNAL_ERROR;
1827 return 0;
1828 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1831 if (s->next_proto_negotiated == NULL) {
1832 *out_alert = SSL_AD_INTERNAL_ERROR;
1833 return 0;
1834 }
Adam Langley75712922014-10-10 16:23:43 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 s->next_proto_negotiated_len = selected_len;
1837 s->s3->next_proto_neg_seen = 1;
1838 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1839 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 /* We must have requested it. */
1842 if (s->alpn_client_proto_list == NULL) {
1843 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1844 return 0;
1845 }
Adam Langley95c29f32014-06-20 12:00:00 -07001846
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 /* The extension data consists of a ProtocolNameList which must have
1848 * exactly one ProtocolName. Each of these is length-prefixed. */
1849 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1850 CBS_len(&extension) != 0 ||
1851 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1852 CBS_len(&protocol_name_list) != 0) {
1853 *out_alert = SSL_AD_DECODE_ERROR;
1854 return 0;
1855 }
Adam Langley95c29f32014-06-20 12:00:00 -07001856
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1858 &s->s3->alpn_selected_len)) {
1859 *out_alert = SSL_AD_INTERNAL_ERROR;
1860 return 0;
1861 }
1862 } else if (type == TLSEXT_TYPE_channel_id) {
1863 if (CBS_len(&extension) != 0) {
1864 *out_alert = SSL_AD_DECODE_ERROR;
1865 return 0;
1866 }
Adam Langley95c29f32014-06-20 12:00:00 -07001867
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 s->s3->tlsext_channel_id_valid = 1;
1869 } else if (type == TLSEXT_TYPE_channel_id_new) {
1870 if (CBS_len(&extension) != 0) {
1871 *out_alert = SSL_AD_DECODE_ERROR;
1872 return 0;
1873 }
Adam Langley95c29f32014-06-20 12:00:00 -07001874
Adam Langleyfcf25832014-12-18 17:42:32 -08001875 s->s3->tlsext_channel_id_valid = 1;
1876 s->s3->tlsext_channel_id_new = 1;
1877 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1878 if (CBS_len(&extension) == 0) {
1879 *out_alert = SSL_AD_DECODE_ERROR;
1880 return 0;
1881 }
Adam Langley95c29f32014-06-20 12:00:00 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 /* Session resumption uses the original session information. */
1884 if (!s->hit &&
1885 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1886 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1887 *out_alert = SSL_AD_INTERNAL_ERROR;
1888 return 0;
1889 }
1890 } else if (type == TLSEXT_TYPE_renegotiate) {
1891 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1892 return 0;
1893 }
Adam Langley95c29f32014-06-20 12:00:00 -07001894
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 renegotiate_seen = 1;
1896 } else if (type == TLSEXT_TYPE_use_srtp) {
1897 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1898 return 0;
1899 }
1900 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1901 if (/* It is invalid for the server to select EMS and
1902 SSLv3. */
1903 s->version == SSL3_VERSION || 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->tmp.extended_master_secret = 1;
1909 }
1910 }
Adam Langley95c29f32014-06-20 12:00:00 -07001911
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1913 if (s->session->tlsext_hostname == NULL) {
1914 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1915 if (!s->session->tlsext_hostname) {
1916 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1917 return 0;
1918 }
1919 } else {
1920 *out_alert = SSL_AD_DECODE_ERROR;
1921 return 0;
1922 }
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925ri_check:
1926 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1927 * attack we should *always* see RI even on initial server hello because the
1928 * client doesn't see any renegotiation during an attack. However this would
1929 * mean we could not connect to any server which doesn't support RI so for
1930 * the immediate future tolerate RI absence on initial connect only. */
1931 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1932 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1933 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1934 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1935 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1936 return 0;
1937 }
Adam Langley95c29f32014-06-20 12:00:00 -07001938
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 return 1;
1940}
Adam Langley95c29f32014-06-20 12:00:00 -07001941
Adam Langleyfcf25832014-12-18 17:42:32 -08001942int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946static int ssl_check_clienthello_tlsext(SSL *s) {
1947 int ret = SSL_TLSEXT_ERR_NOACK;
1948 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1951 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001952
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1954 ret = s->ctx->tlsext_servername_callback(s, &al,
1955 s->ctx->tlsext_servername_arg);
1956 } else if (s->initial_ctx != NULL &&
1957 s->initial_ctx->tlsext_servername_callback != 0) {
1958 ret = s->initial_ctx->tlsext_servername_callback(
1959 s, &al, s->initial_ctx->tlsext_servername_arg);
1960 }
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 switch (ret) {
1963 case SSL_TLSEXT_ERR_ALERT_FATAL:
1964 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1965 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 case SSL_TLSEXT_ERR_ALERT_WARNING:
1968 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1969 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001970
Adam Langleyfcf25832014-12-18 17:42:32 -08001971 case SSL_TLSEXT_ERR_NOACK:
1972 s->should_ack_sni = 0;
1973 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 default:
1976 return 1;
1977 }
1978}
Adam Langleyed8270a2014-09-02 13:52:56 -07001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980static int ssl_check_serverhello_tlsext(SSL *s) {
1981 int ret = SSL_TLSEXT_ERR_NOACK;
1982 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984 /* If we are client and using an elliptic curve cryptography cipher suite,
1985 * then if server returns an EC point formats lists extension it must contain
1986 * uncompressed. */
1987 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1988 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1989 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
1990 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
1991 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
1992 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1993 return -1;
1994 }
1995 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1998 ret = s->ctx->tlsext_servername_callback(s, &al,
1999 s->ctx->tlsext_servername_arg);
2000 } else if (s->initial_ctx != NULL &&
2001 s->initial_ctx->tlsext_servername_callback != 0) {
2002 ret = s->initial_ctx->tlsext_servername_callback(
2003 s, &al, s->initial_ctx->tlsext_servername_arg);
2004 }
Adam Langley95c29f32014-06-20 12:00:00 -07002005
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 switch (ret) {
2007 case SSL_TLSEXT_ERR_ALERT_FATAL:
2008 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2009 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002010
Adam Langleyfcf25832014-12-18 17:42:32 -08002011 case SSL_TLSEXT_ERR_ALERT_WARNING:
2012 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2013 return 1;
2014
2015 default:
2016 return 1;
2017 }
2018}
2019
2020int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2021 int alert = -1;
2022 if (s->version < SSL3_VERSION) {
2023 return 1;
2024 }
2025
2026 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2027 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2028 return 0;
2029 }
2030
2031 if (ssl_check_serverhello_tlsext(s) <= 0) {
2032 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2033 SSL_R_SERVERHELLO_TLSEXT);
2034 return 0;
2035 }
2036
2037 return 1;
2038}
Adam Langley95c29f32014-06-20 12:00:00 -07002039
2040/* Since the server cache lookup is done early on in the processing of the
2041 * ClientHello, and other operations depend on the result, we need to handle
2042 * any TLS session ticket extension at the same time.
2043 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002044 * ctx: contains the early callback context, which is the result of a
2045 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002046 * ret: (output) on return, if a ticket was decrypted, then this is set to
2047 * point to the resulting session.
2048 *
Adam Langley95c29f32014-06-20 12:00:00 -07002049 * Returns:
2050 * -1: fatal error, either from parsing or decrypting the ticket.
2051 * 0: no ticket was found (or was ignored, based on settings).
2052 * 1: a zero length extension was found, indicating that the client supports
2053 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002054 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2055 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002056 * 3: a ticket was successfully decrypted and *ret was set.
2057 *
2058 * Side effects:
2059 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2060 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002061 * but the client either doesn't have a session ticket or we couldn't use
2062 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2063 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002064 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002065int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 SSL_SESSION **ret) {
2067 *ret = NULL;
2068 s->tlsext_ticket_expected = 0;
2069 const uint8_t *data;
2070 size_t len;
2071 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002072
Adam Langleyfcf25832014-12-18 17:42:32 -08002073 /* If tickets disabled behave as if no ticket present to permit stateful
2074 * resumption. */
2075 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2076 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2077 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2078 &data, &len)) {
2079 return 0;
2080 }
2081
2082 if (len == 0) {
2083 /* The client will accept a ticket but doesn't currently have one. */
2084 s->tlsext_ticket_expected = 1;
2085 return 1;
2086 }
2087
2088 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2089 ret);
2090 switch (r) {
2091 case 2: /* ticket couldn't be decrypted */
2092 s->tlsext_ticket_expected = 1;
2093 return 2;
2094
2095 case 3: /* ticket was decrypted */
2096 return r;
2097
2098 case 4: /* ticket decrypted but need to renew */
2099 s->tlsext_ticket_expected = 1;
2100 return 3;
2101
2102 default: /* fatal error */
2103 return -1;
2104 }
2105}
Adam Langley95c29f32014-06-20 12:00:00 -07002106
2107/* tls_decrypt_ticket attempts to decrypt a session ticket.
2108 *
2109 * etick: points to the body of the session ticket extension.
2110 * eticklen: the length of the session tickets extenion.
2111 * sess_id: points at the session ID.
2112 * sesslen: the length of the session ID.
2113 * psess: (output) on return, if a ticket was decrypted, then this is set to
2114 * point to the resulting session.
2115 *
2116 * Returns:
2117 * -1: fatal error, either from parsing or decrypting the ticket.
2118 * 2: the ticket couldn't be decrypted.
2119 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 * 4: same as 3, but the ticket needs to be renewed. */
2121static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2122 const uint8_t *sess_id, int sesslen,
2123 SSL_SESSION **psess) {
2124 SSL_SESSION *sess;
2125 uint8_t *sdec;
2126 const uint8_t *p;
2127 int slen, mlen, renew_ticket = 0;
2128 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2129 HMAC_CTX hctx;
2130 EVP_CIPHER_CTX ctx;
2131 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002132
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 /* Need at least keyname + iv + some encrypted data */
2134 if (eticklen < 48) {
2135 return 2;
2136 }
2137
2138 /* Initialize session ticket encryption and HMAC contexts */
2139 HMAC_CTX_init(&hctx);
2140 EVP_CIPHER_CTX_init(&ctx);
2141 if (tctx->tlsext_ticket_key_cb) {
2142 uint8_t *nctick = (uint8_t *)etick;
2143 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2144 if (rv < 0) {
2145 return -1;
2146 }
2147 if (rv == 0) {
2148 return 2;
2149 }
2150 if (rv == 2) {
2151 renew_ticket = 1;
2152 }
2153 } else {
2154 /* Check key name matches */
2155 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2156 return 2;
2157 }
2158 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2159 NULL) ||
2160 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2161 tctx->tlsext_tick_aes_key, etick + 16)) {
2162 HMAC_CTX_cleanup(&hctx);
2163 EVP_CIPHER_CTX_cleanup(&ctx);
2164 return -1;
2165 }
2166 }
2167
2168 /* Attempt to process session ticket, first conduct sanity and integrity
2169 * checks on ticket. */
2170 mlen = HMAC_size(&hctx);
2171 if (mlen < 0) {
2172 HMAC_CTX_cleanup(&hctx);
2173 EVP_CIPHER_CTX_cleanup(&ctx);
2174 return -1;
2175 }
2176 eticklen -= mlen;
2177 /* Check HMAC of encrypted ticket */
2178 HMAC_Update(&hctx, etick, eticklen);
2179 HMAC_Final(&hctx, tick_hmac, NULL);
2180 HMAC_CTX_cleanup(&hctx);
2181 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2182 EVP_CIPHER_CTX_cleanup(&ctx);
2183 return 2;
2184 }
2185
2186 /* Attempt to decrypt session data */
2187 /* Move p after IV to start of encrypted ticket, update length */
2188 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2189 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2190 sdec = OPENSSL_malloc(eticklen);
2191 if (!sdec) {
2192 EVP_CIPHER_CTX_cleanup(&ctx);
2193 return -1;
2194 }
2195 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2196 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2197 EVP_CIPHER_CTX_cleanup(&ctx);
2198 OPENSSL_free(sdec);
2199 return 2;
2200 }
2201 slen += mlen;
2202 EVP_CIPHER_CTX_cleanup(&ctx);
2203 p = sdec;
2204
2205 sess = d2i_SSL_SESSION(NULL, &p, slen);
2206 OPENSSL_free(sdec);
2207 if (sess) {
2208 /* The session ID, if non-empty, is used by some clients to detect that the
2209 * ticket has been accepted. So we copy it to the session structure. If it
2210 * is empty set length to zero as required by standard. */
2211 if (sesslen) {
2212 memcpy(sess->session_id, sess_id, sesslen);
2213 }
2214 sess->session_id_length = sesslen;
2215 *psess = sess;
2216 if (renew_ticket) {
2217 return 4;
2218 }
2219 return 3;
2220 }
2221
2222 ERR_clear_error();
2223 /* For session parse failure, indicate that we need to send a new ticket. */
2224 return 2;
2225}
Adam Langley95c29f32014-06-20 12:00:00 -07002226
2227/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002228typedef struct {
2229 int nid;
2230 int id;
2231} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002232
Adam Langleyfcf25832014-12-18 17:42:32 -08002233static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2234 {NID_sha1, TLSEXT_hash_sha1},
2235 {NID_sha224, TLSEXT_hash_sha224},
2236 {NID_sha256, TLSEXT_hash_sha256},
2237 {NID_sha384, TLSEXT_hash_sha384},
2238 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2241 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2244 size_t i;
2245 for (i = 0; i < tlen; i++) {
2246 if (table[i].nid == nid) {
2247 return table[i].id;
2248 }
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 return -1;
2252}
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langleyfcf25832014-12-18 17:42:32 -08002254static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2255 size_t i;
2256 for (i = 0; i < tlen; i++) {
2257 if (table[i].id == id) {
2258 return table[i].nid;
2259 }
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 return NID_undef;
2263}
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2266 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 if (!md) {
2269 return 0;
2270 }
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2273 sizeof(tls12_md) / sizeof(tls12_lookup));
2274 if (md_id == -1) {
2275 return 0;
2276 }
Adam Langley95c29f32014-06-20 12:00:00 -07002277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 sig_id = tls12_get_sigid(pk);
2279 if (sig_id == -1) {
2280 return 0;
2281 }
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 p[0] = (uint8_t)md_id;
2284 p[1] = (uint8_t)sig_id;
2285 return 1;
2286}
2287
2288int tls12_get_sigid(const EVP_PKEY *pk) {
2289 return tls12_find_id(pk->type, tls12_sig,
2290 sizeof(tls12_sig) / sizeof(tls12_lookup));
2291}
2292
2293const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2294 switch (hash_alg) {
2295 case TLSEXT_hash_md5:
2296 return EVP_md5();
2297
2298 case TLSEXT_hash_sha1:
2299 return EVP_sha1();
2300
2301 case TLSEXT_hash_sha224:
2302 return EVP_sha224();
2303
2304 case TLSEXT_hash_sha256:
2305 return EVP_sha256();
2306
2307 case TLSEXT_hash_sha384:
2308 return EVP_sha384();
2309
2310 case TLSEXT_hash_sha512:
2311 return EVP_sha512();
2312
2313 default:
2314 return NULL;
2315 }
2316}
Adam Langley95c29f32014-06-20 12:00:00 -07002317
David Benjaminec2f27d2014-11-13 19:17:25 -05002318/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2319 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002320static int tls12_get_pkey_type(uint8_t sig_alg) {
2321 switch (sig_alg) {
2322 case TLSEXT_signature_rsa:
2323 return EVP_PKEY_RSA;
2324
2325 case TLSEXT_signature_ecdsa:
2326 return EVP_PKEY_EC;
2327
2328 default:
2329 return -1;
2330 }
2331}
Adam Langley95c29f32014-06-20 12:00:00 -07002332
2333/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2334static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002335 int *psignhash_nid, const uint8_t *data) {
2336 int sign_nid = 0, hash_nid = 0;
2337 if (!phash_nid && !psign_nid && !psignhash_nid) {
2338 return;
2339 }
2340
2341 if (phash_nid || psignhash_nid) {
2342 hash_nid = tls12_find_nid(data[0], tls12_md,
2343 sizeof(tls12_md) / sizeof(tls12_lookup));
2344 if (phash_nid) {
2345 *phash_nid = hash_nid;
2346 }
2347 }
2348
2349 if (psign_nid || psignhash_nid) {
2350 sign_nid = tls12_find_nid(data[1], tls12_sig,
2351 sizeof(tls12_sig) / sizeof(tls12_lookup));
2352 if (psign_nid) {
2353 *psign_nid = sign_nid;
2354 }
2355 }
2356
2357 if (psignhash_nid) {
2358 if (sign_nid && hash_nid) {
2359 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2360 } else {
2361 *psignhash_nid = NID_undef;
2362 }
2363 }
2364}
2365
Adam Langley95c29f32014-06-20 12:00:00 -07002366/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002367static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2368 size_t preflen, const uint8_t *allow,
2369 size_t allowlen) {
2370 const uint8_t *ptmp, *atmp;
2371 size_t i, j, nmatch = 0;
2372
2373 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2374 /* Skip disabled hashes or signature algorithms */
2375 if (tls12_get_hash(ptmp[0]) == NULL ||
2376 tls12_get_pkey_type(ptmp[1]) == -1) {
2377 continue;
2378 }
2379
2380 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2381 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2382 nmatch++;
2383 if (shsig) {
2384 shsig->rhash = ptmp[0];
2385 shsig->rsign = ptmp[1];
2386 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2387 &shsig->signandhash_nid, ptmp);
2388 shsig++;
2389 }
2390
2391 break;
2392 }
2393 }
2394 }
2395
2396 return nmatch;
2397}
Adam Langley95c29f32014-06-20 12:00:00 -07002398
2399/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002400static int tls1_set_shared_sigalgs(SSL *s) {
2401 const uint8_t *pref, *allow, *conf;
2402 size_t preflen, allowlen, conflen;
2403 size_t nmatch;
2404 TLS_SIGALGS *salgs = NULL;
2405 CERT *c = s->cert;
2406
2407 if (c->shared_sigalgs) {
2408 OPENSSL_free(c->shared_sigalgs);
2409 c->shared_sigalgs = NULL;
2410 }
2411
2412 /* If client use client signature algorithms if not NULL */
2413 if (!s->server && c->client_sigalgs) {
2414 conf = c->client_sigalgs;
2415 conflen = c->client_sigalgslen;
2416 } else if (c->conf_sigalgs) {
2417 conf = c->conf_sigalgs;
2418 conflen = c->conf_sigalgslen;
2419 } else {
2420 conflen = tls12_get_psigalgs(s, &conf);
2421 }
2422
2423 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2424 pref = conf;
2425 preflen = conflen;
2426 allow = c->peer_sigalgs;
2427 allowlen = c->peer_sigalgslen;
2428 } else {
2429 allow = conf;
2430 allowlen = conflen;
2431 pref = c->peer_sigalgs;
2432 preflen = c->peer_sigalgslen;
2433 }
2434
2435 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2436 if (!nmatch) {
2437 return 1;
2438 }
2439
2440 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2441 if (!salgs) {
2442 return 0;
2443 }
2444
2445 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2446 c->shared_sigalgs = salgs;
2447 c->shared_sigalgslen = nmatch;
2448 return 1;
2449}
Adam Langley95c29f32014-06-20 12:00:00 -07002450
2451/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002452int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2453 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 /* Extension ignored for inappropriate versions */
2456 if (!SSL_USE_SIGALGS(s)) {
2457 return 1;
2458 }
David Benjamincd996942014-07-20 16:23:51 -04002459
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 /* Length must be even */
2461 if (CBS_len(sigalgs) % 2 != 0) {
2462 return 0;
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 /* Should never happen */
2466 if (!c) {
2467 return 0;
2468 }
Adam Langley95c29f32014-06-20 12:00:00 -07002469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen)) {
2471 return 0;
2472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 tls1_set_shared_sigalgs(s);
2475 return 1;
2476}
David Benjaminec2f27d2014-11-13 19:17:25 -05002477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2479 CERT *c = s->cert;
2480 int type = EVP_PKEY_id(pkey);
2481 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002482
Adam Langleyfcf25832014-12-18 17:42:32 -08002483 /* Select the first shared digest supported by our key. */
2484 for (i = 0; i < c->shared_sigalgslen; i++) {
2485 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2486 if (md == NULL ||
2487 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2488 !EVP_PKEY_supports_digest(pkey, md)) {
2489 continue;
2490 }
2491 return md;
2492 }
Adam Langley95c29f32014-06-20 12:00:00 -07002493
Adam Langleyfcf25832014-12-18 17:42:32 -08002494 /* If no suitable digest may be found, default to SHA-1. */
2495 return EVP_sha1();
2496}
Adam Langley95c29f32014-06-20 12:00:00 -07002497
Adam Langleyfcf25832014-12-18 17:42:32 -08002498int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2499 uint8_t *rsig, uint8_t *rhash) {
2500 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002501
Adam Langleyfcf25832014-12-18 17:42:32 -08002502 if (psig == NULL) {
2503 return 0;
2504 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 if (idx >= 0) {
2507 idx <<= 1;
2508 if (idx >= (int)s->cert->peer_sigalgslen) {
2509 return 0;
2510 }
2511 psig += idx;
2512 if (rhash) {
2513 *rhash = psig[0];
2514 }
2515 if (rsig) {
2516 *rsig = psig[1];
2517 }
2518 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2519 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002520
Adam Langleyfcf25832014-12-18 17:42:32 -08002521 return s->cert->peer_sigalgslen / 2;
2522}
Adam Langley1258b6a2014-06-20 12:00:00 -07002523
Adam Langleyfcf25832014-12-18 17:42:32 -08002524int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2525 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2526 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2529 return 0;
2530 }
2531
2532 shsigalgs += idx;
2533 if (phash) {
2534 *phash = shsigalgs->hash_nid;
2535 }
2536 if (psign) {
2537 *psign = shsigalgs->sign_nid;
2538 }
2539 if (psignhash) {
2540 *psignhash = shsigalgs->signandhash_nid;
2541 }
2542 if (rsig) {
2543 *rsig = shsigalgs->rsign;
2544 }
2545 if (rhash) {
2546 *rhash = shsigalgs->rhash;
2547 }
2548
2549 return s->cert->shared_sigalgslen;
2550}
2551
2552/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2553 * given SSL connection and writes it to |md|. */
2554int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2555 EVP_MD_CTX ctx;
2556 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2557 unsigned temp_digest_len;
2558 int i;
2559 static const char kClientIDMagic[] = "TLS Channel ID signature";
2560
2561 if (s->s3->handshake_buffer &&
2562 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2563 return 0;
2564 }
2565
2566 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2567
2568 if (s->hit && s->s3->tlsext_channel_id_new) {
2569 static const char kResumptionMagic[] = "Resumption";
2570 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2571 if (s->session->original_handshake_hash_len == 0) {
2572 return 0;
2573 }
2574 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2575 s->session->original_handshake_hash_len);
2576 }
2577
2578 EVP_MD_CTX_init(&ctx);
2579 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2580 if (s->s3->handshake_dgst[i] == NULL) {
2581 continue;
2582 }
2583 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2584 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2585 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2586 }
2587 EVP_MD_CTX_cleanup(&ctx);
2588
2589 return 1;
2590}
Adam Langley1258b6a2014-06-20 12:00:00 -07002591
2592/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2593 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002594int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2595 int digest_len;
2596 /* This function should never be called for a resumed session because the
2597 * handshake hashes that we wish to record are for the original, full
2598 * handshake. */
2599 if (s->hit) {
2600 return -1;
2601 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 /* It only makes sense to call this function if Channel IDs have been
2604 * negotiated. */
2605 if (!s->s3->tlsext_channel_id_new) {
2606 return -1;
2607 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002608
Adam Langleyfcf25832014-12-18 17:42:32 -08002609 digest_len =
2610 tls1_handshake_digest(s, s->session->original_handshake_hash,
2611 sizeof(s->session->original_handshake_hash));
2612 if (digest_len < 0) {
2613 return -1;
2614 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 return 1;
2619}
Adam Langley95c29f32014-06-20 12:00:00 -07002620
Adam Langleyfcf25832014-12-18 17:42:32 -08002621int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2622 int client) {
2623 uint8_t *sigalgs, *sptr;
2624 int rhash, rsign;
2625 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002626
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 if (salglen & 1) {
2628 return 0;
2629 }
Adam Langley95c29f32014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 sigalgs = OPENSSL_malloc(salglen);
2632 if (sigalgs == NULL) {
2633 return 0;
2634 }
Adam Langley95c29f32014-06-20 12:00:00 -07002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2637 rhash = tls12_find_id(*psig_nids++, tls12_md,
2638 sizeof(tls12_md) / sizeof(tls12_lookup));
2639 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2640 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002641
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 if (rhash == -1 || rsign == -1) {
2643 goto err;
2644 }
2645 *sptr++ = rhash;
2646 *sptr++ = rsign;
2647 }
2648
2649 if (client) {
2650 if (c->client_sigalgs) {
2651 OPENSSL_free(c->client_sigalgs);
2652 }
2653 c->client_sigalgs = sigalgs;
2654 c->client_sigalgslen = salglen;
2655 } else {
2656 if (c->conf_sigalgs) {
2657 OPENSSL_free(c->conf_sigalgs);
2658 }
2659 c->conf_sigalgs = sigalgs;
2660 c->conf_sigalgslen = salglen;
2661 }
2662
2663 return 1;
2664
2665err:
2666 OPENSSL_free(sigalgs);
2667 return 0;
2668}