blob: fcf2b0497ea39f72a9c12befe1e5ed4be2e25301 [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
David Benjaminf0ae1702015-04-07 23:05:04 -0400109#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700110#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400111#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400112#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin03973092014-06-24 23:27:17 -0400114#include <openssl/bytestring.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <openssl/evp.h>
117#include <openssl/hmac.h>
118#include <openssl/mem.h>
119#include <openssl/obj.h>
120#include <openssl/rand.h>
121
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800123
124
125static int tls_decrypt_ticket(SSL *s, const uint8_t *tick, int ticklen,
126 const uint8_t *sess_id, int sesslen,
127 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400128static int ssl_check_clienthello_tlsext(SSL *s);
129static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
David Benjamin338fcaf2014-12-11 01:20:52 -0500131const SSL3_ENC_METHOD TLSv1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800132 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500133 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800134 tls1_setup_key_block,
135 tls1_generate_master_secret,
136 tls1_change_cipher_state,
137 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_alert_code,
142 tls1_export_keying_material,
143 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800144};
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin338fcaf2014-12-11 01:20:52 -0500146const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500148 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 tls1_cert_verify_mac,
154 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
155 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
156 tls1_alert_code,
157 tls1_export_keying_material,
158 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159};
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin338fcaf2014-12-11 01:20:52 -0500161const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500163 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 tls1_setup_key_block,
165 tls1_generate_master_secret,
166 tls1_change_cipher_state,
167 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 tls1_cert_verify_mac,
169 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
170 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
171 tls1_alert_code,
172 tls1_export_keying_material,
173 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
174 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800175};
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177static int compare_uint16_t(const void *p1, const void *p2) {
178 uint16_t u1 = *((const uint16_t *)p1);
179 uint16_t u2 = *((const uint16_t *)p2);
180 if (u1 < u2) {
181 return -1;
182 } else if (u1 > u2) {
183 return 1;
184 } else {
185 return 0;
186 }
187}
David Benjamin35a7a442014-07-05 00:23:20 -0400188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
190 * more than one extension of the same type in a ClientHello or ServerHello.
191 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400192 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800193static int tls1_check_duplicate_extensions(const CBS *cbs) {
194 CBS extensions = *cbs;
195 size_t num_extensions = 0, i = 0;
196 uint16_t *extension_types = NULL;
197 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 /* First pass: count the extensions. */
200 while (CBS_len(&extensions) > 0) {
201 uint16_t type;
202 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 if (!CBS_get_u16(&extensions, &type) ||
205 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
206 goto done;
207 }
David Benjamin35a7a442014-07-05 00:23:20 -0400208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 num_extensions++;
210 }
David Benjamin35a7a442014-07-05 00:23:20 -0400211
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 if (num_extensions == 0) {
213 return 1;
214 }
David Benjamin9a373592014-07-25 04:27:53 -0400215
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 extension_types =
217 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
218 if (extension_types == NULL) {
219 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
220 ERR_R_MALLOC_FAILURE);
221 goto done;
222 }
David Benjamin35a7a442014-07-05 00:23:20 -0400223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 /* Second pass: gather the extension types. */
225 extensions = *cbs;
226 for (i = 0; i < num_extensions; i++) {
227 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
230 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
231 /* This should not happen. */
232 goto done;
233 }
234 }
235 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400236
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 /* Sort the extensions and make sure there are no duplicates. */
238 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
239 for (i = 1; i < num_extensions; i++) {
240 if (extension_types[i - 1] == extension_types[i]) {
241 goto done;
242 }
243 }
David Benjamin35a7a442014-07-05 00:23:20 -0400244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ret = 1;
246
David Benjamin35a7a442014-07-05 00:23:20 -0400247done:
David Benjamin6eb000d2015-02-11 01:17:41 -0500248 if (extension_types) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 OPENSSL_free(extension_types);
David Benjamin6eb000d2015-02-11 01:17:41 -0500250 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 return ret;
252}
David Benjamin35a7a442014-07-05 00:23:20 -0400253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
255 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (/* Skip client version. */
260 !CBS_skip(&client_hello, 2) ||
261 /* Skip client nonce. */
262 !CBS_skip(&client_hello, 32) ||
263 /* Extract session_id. */
264 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
265 return 0;
266 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 ctx->session_id = CBS_data(&session_id);
269 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 /* Skip past DTLS cookie */
272 if (SSL_IS_DTLS(ctx->ssl)) {
273 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
276 return 0;
277 }
278 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 /* Extract cipher_suites. */
281 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
282 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
283 return 0;
284 }
285 ctx->cipher_suites = CBS_data(&cipher_suites);
286 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 /* Extract compression_methods. */
289 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
290 CBS_len(&compression_methods) < 1) {
291 return 0;
292 }
293 ctx->compression_methods = CBS_data(&compression_methods);
294 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 /* If the ClientHello ends here then it's valid, but doesn't have any
297 * extensions. (E.g. SSLv3.) */
298 if (CBS_len(&client_hello) == 0) {
299 ctx->extensions = NULL;
300 ctx->extensions_len = 0;
301 return 1;
302 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700303
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 /* Extract extensions and check it is valid. */
305 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
306 !tls1_check_duplicate_extensions(&extensions) ||
307 CBS_len(&client_hello) != 0) {
308 return 0;
309 }
310 ctx->extensions = CBS_data(&extensions);
311 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 return 1;
314}
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316char SSL_early_callback_ctx_extension_get(
317 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
318 const uint8_t **out_data, size_t *out_len) {
319 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700322
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 while (CBS_len(&extensions) != 0) {
324 uint16_t type;
325 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 /* Decode the next extension. */
328 if (!CBS_get_u16(&extensions, &type) ||
329 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
330 return 0;
331 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (type == extension_type) {
334 *out_data = CBS_data(&extension);
335 *out_len = CBS_len(&extension);
336 return 1;
337 }
338 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 return 0;
341}
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamin52e5bac2014-12-27 02:27:35 -0500343struct tls_curve {
344 uint16_t curve_id;
345 int nid;
346};
347
David Benjamin70bd80a2014-12-27 03:06:46 -0500348/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500349static const struct tls_curve tls_curves[] = {
350 {21, NID_secp224r1},
351 {23, NID_X9_62_prime256v1},
352 {24, NID_secp384r1},
353 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800354};
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356static const uint8_t ecformats_default[] = {
357 TLSEXT_ECPOINTFORMAT_uncompressed,
358};
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360static const uint16_t eccurves_default[] = {
David Benjamin52e5bac2014-12-27 02:27:35 -0500361 23, /* X9_64_prime256v1 */
362 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800363};
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500366 size_t i;
367 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
368 if (curve_id == tls_curves[i].curve_id) {
369 return tls_curves[i].nid;
370 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500372 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373}
Adam Langley95c29f32014-06-20 12:00:00 -0700374
David Benjamin70bd80a2014-12-27 03:06:46 -0500375int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500377 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
378 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500379 *out_curve_id = tls_curves[i].curve_id;
380 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
382 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 return 0;
384}
385
386/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
387 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
388 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400389static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 const uint16_t **out_curve_ids,
391 size_t *out_curve_ids_len) {
392 if (get_peer_curves) {
393 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
394 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
395 return;
396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 *out_curve_ids = s->tlsext_ellipticcurvelist;
399 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
400 if (!*out_curve_ids) {
401 *out_curve_ids = eccurves_default;
402 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
403 }
404}
David Benjamined439582014-07-14 19:13:02 -0400405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
407 uint8_t curve_type;
408 uint16_t curve_id;
409 const uint16_t *curves;
410 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 /* Only support named curves. */
413 if (!CBS_get_u8(cbs, &curve_type) ||
414 curve_type != NAMED_CURVE_TYPE ||
415 !CBS_get_u16(cbs, &curve_id)) {
416 return 0;
417 }
David Benjamined439582014-07-14 19:13:02 -0400418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 tls1_get_curvelist(s, 0, &curves, &curves_len);
420 for (i = 0; i < curves_len; i++) {
421 if (curve_id == curves[i]) {
422 *out_curve_id = curve_id;
423 return 1;
424 }
425 }
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 return 0;
428}
David Benjamin072334d2014-07-13 16:24:27 -0400429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430int tls1_get_shared_curve(SSL *s) {
431 const uint16_t *pref, *supp;
432 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400433
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 /* Can't do anything on client side */
435 if (s->server == 0) {
436 return NID_undef;
437 }
438
439 /* Return first preference shared curve */
440 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp,
441 &supplen);
442 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
443 &preflen);
444
445 for (i = 0; i < preflen; i++) {
446 for (j = 0; j < supplen; j++) {
447 if (pref[i] == supp[j]) {
448 return tls1_ec_curve_id2nid(pref[i]);
449 }
450 }
451 }
452
453 return NID_undef;
454}
Adam Langley95c29f32014-06-20 12:00:00 -0700455
David Benjamin072334d2014-07-13 16:24:27 -0400456int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 const int *curves, size_t ncurves) {
458 uint16_t *curve_ids;
459 size_t i;
460
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
462 if (curve_ids == NULL) {
463 return 0;
464 }
465
466 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500467 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 OPENSSL_free(curve_ids);
469 return 0;
470 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 }
472
473 if (*out_curve_ids) {
474 OPENSSL_free(*out_curve_ids);
475 }
476 *out_curve_ids = curve_ids;
477 *out_curve_ids_len = ncurves;
478
479 return 1;
480}
Adam Langley95c29f32014-06-20 12:00:00 -0700481
David Benjamin072334d2014-07-13 16:24:27 -0400482/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
483 * TLS curve ID and point format, respectively, for |ec|. It returns one on
484 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800485static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
486 uint8_t *out_comp_id, EC_KEY *ec) {
487 int nid;
488 uint16_t id;
489 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 if (ec == NULL) {
492 return 0;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 grp = EC_KEY_get0_group(ec);
496 if (grp == NULL) {
497 return 0;
498 }
David Benjamin072334d2014-07-13 16:24:27 -0400499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 /* Determine curve ID */
501 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500502 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 return 0;
504 }
David Benjamin072334d2014-07-13 16:24:27 -0400505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
507 *out_curve_id = id;
508
509 if (out_comp_id) {
510 if (EC_KEY_get0_public_key(ec) == NULL) {
511 return 0;
512 }
513 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
514 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
515 } else {
516 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
517 }
518 }
519
520 return 1;
521}
David Benjamin072334d2014-07-13 16:24:27 -0400522
David Benjamin42e9a772014-09-02 23:18:44 -0400523/* tls1_check_point_format returns one if |comp_id| is consistent with the
524 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800525static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
526 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
527 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
528 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 /* If point formats extension present check it, otherwise everything is
531 * supported (see RFC4492). */
532 if (p == NULL) {
533 return 1;
534 }
David Benjamin42e9a772014-09-02 23:18:44 -0400535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 for (i = 0; i < plen; i++) {
537 if (comp_id == p[i]) {
538 return 1;
539 }
540 }
David Benjamin42e9a772014-09-02 23:18:44 -0400541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 return 0;
543}
544
545/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
546 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400547 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800548static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
549 const uint16_t *curves;
550 size_t curves_len, i, j;
David Benjamin42e9a772014-09-02 23:18:44 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 /* Check against our list, then the peer's list. */
553 for (j = 0; j <= 1; j++) {
554 tls1_get_curvelist(s, j, &curves, &curves_len);
555 for (i = 0; i < curves_len; i++) {
556 if (curves[i] == curve_id) {
557 break;
558 }
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 if (i == curves_len) {
562 return 0;
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 /* Servers do not present a preference list so, if we are a client, only
566 * check our list. */
567 if (!s->server) {
568 return 1;
569 }
570 }
David Benjamin033e5f42014-11-13 18:47:41 -0500571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 return 1;
573}
David Benjamin033e5f42014-11-13 18:47:41 -0500574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
576 size_t *pformatslen) {
577 /* If we have a custom point format list use it otherwise use default */
578 if (s->tlsext_ecpointformatlist) {
579 *pformats = s->tlsext_ecpointformatlist;
580 *pformatslen = s->tlsext_ecpointformatlist_length;
581 } else {
582 *pformats = ecformats_default;
583 *pformatslen = sizeof(ecformats_default);
584 }
585}
586
587int tls1_check_ec_cert(SSL *s, X509 *x) {
588 int ret = 0;
589 EVP_PKEY *pkey = X509_get_pubkey(x);
590 uint16_t curve_id;
591 uint8_t comp_id;
592
593 if (!pkey ||
594 pkey->type != EVP_PKEY_EC ||
595 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
596 !tls1_check_curve_id(s, curve_id) ||
597 !tls1_check_point_format(s, comp_id)) {
598 goto done;
599 }
600
601 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500602
603done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 if (pkey) {
605 EVP_PKEY_free(pkey);
606 }
607 return ret;
608}
David Benjamin42e9a772014-09-02 23:18:44 -0400609
Adam Langleyfcf25832014-12-18 17:42:32 -0800610int tls1_check_ec_tmp_key(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 if (s->cert->ecdh_tmp_auto) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400612 /* If using |ecdh_tmp_auto|, ECDH is acceptable if there is a shared
613 * curve. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 return tls1_get_shared_curve(s) != NID_undef;
615 }
Adam Langley95c29f32014-06-20 12:00:00 -0700616
David Benjaminc0f763b2015-03-27 02:05:39 -0400617 if (s->cert->ecdh_nid != NID_undef) {
618 /* If the curve is preconfigured, ECDH is acceptable if the peer supports
619 * the curve. */
620 uint16_t curve_id;
621 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
622 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 }
624
David Benjaminc0f763b2015-03-27 02:05:39 -0400625 /* Otherwise, assume the callback will provide an acceptable curve. */
626 return s->cert->ecdh_tmp_cb != NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800627}
Adam Langley95c29f32014-06-20 12:00:00 -0700628
629/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langley95c29f32014-06-20 12:00:00 -0700632#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langley95c29f32014-06-20 12:00:00 -0700634#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700637
David Benjamincff64722014-08-19 19:54:46 -0400638static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 tlsext_sigalg(TLSEXT_hash_sha512)
640 tlsext_sigalg(TLSEXT_hash_sha384)
641 tlsext_sigalg(TLSEXT_hash_sha256)
642 tlsext_sigalg(TLSEXT_hash_sha224)
643 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700644};
David Benjamin05da6e12014-07-12 20:42:55 -0400645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
647 /* If server use client authentication sigalgs if not NULL */
648 if (s->server && s->cert->client_sigalgs) {
649 *psigs = s->cert->client_sigalgs;
650 return s->cert->client_sigalgslen;
651 } else if (s->cert->conf_sigalgs) {
652 *psigs = s->cert->conf_sigalgs;
653 return s->cert->conf_sigalgslen;
654 } else {
655 *psigs = tls12_sigalgs;
656 return sizeof(tls12_sigalgs);
657 }
658}
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
661 * checks it is consistent with |s|'s sent supported signature algorithms and,
662 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
663 * returns 0 and writes an alert into |*out_alert|. */
664int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
665 CBS *cbs, EVP_PKEY *pkey) {
666 const uint8_t *sent_sigs;
667 size_t sent_sigslen, i;
668 int sigalg = tls12_get_sigid(pkey);
669 uint8_t hash, signature;
670
671 /* Should never happen */
672 if (sigalg == -1) {
673 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
674 *out_alert = SSL_AD_INTERNAL_ERROR;
675 return 0;
676 }
677
678 if (!CBS_get_u8(cbs, &hash) ||
679 !CBS_get_u8(cbs, &signature)) {
680 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
681 *out_alert = SSL_AD_DECODE_ERROR;
682 return 0;
683 }
684
685 /* Check key type is consistent with signature */
686 if (sigalg != signature) {
687 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
688 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
689 return 0;
690 }
691
692 if (pkey->type == EVP_PKEY_EC) {
693 uint16_t curve_id;
694 uint8_t comp_id;
695 /* Check compression and curve matches extensions */
696 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
697 *out_alert = SSL_AD_INTERNAL_ERROR;
698 return 0;
699 }
700
701 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
702 !tls1_check_point_format(s, comp_id))) {
703 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
704 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
705 return 0;
706 }
707 }
708
709 /* Check signature matches a type we sent */
710 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
711 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
712 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
713 break;
714 }
715 }
716
717 /* Allow fallback to SHA-1. */
718 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
719 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
720 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
721 return 0;
722 }
723
724 *out_md = tls12_get_hash(hash);
725 if (*out_md == NULL) {
726 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
727 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
728 return 0;
729 }
730
731 return 1;
732}
733
734/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
735 * supported or doesn't appear in supported signature algorithms. Unlike
736 * ssl_cipher_get_disabled this applies to a specific session and not global
737 * settings. */
738void ssl_set_client_disabled(SSL *s) {
739 CERT *c = s->cert;
740 const uint8_t *sigalgs;
741 size_t i, sigalgslen;
742 int have_rsa = 0, have_ecdsa = 0;
743 c->mask_a = 0;
744 c->mask_k = 0;
745
746 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
747 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
748 c->mask_ssl = SSL_TLSV1_2;
749 } else {
750 c->mask_ssl = 0;
751 }
752
753 /* Now go through all signature algorithms seeing if we support any for RSA,
754 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
755 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
756 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
757 switch (sigalgs[1]) {
758 case TLSEXT_signature_rsa:
759 have_rsa = 1;
760 break;
761
762 case TLSEXT_signature_ecdsa:
763 have_ecdsa = 1;
764 break;
765 }
766 }
767
768 /* Disable auth if we don't include any appropriate signature algorithms. */
769 if (!have_rsa) {
770 c->mask_a |= SSL_aRSA;
771 }
772 if (!have_ecdsa) {
773 c->mask_a |= SSL_aECDSA;
774 }
775
776 /* with PSK there must be client callback set */
777 if (!s->psk_client_callback) {
778 c->mask_a |= SSL_aPSK;
779 c->mask_k |= SSL_kPSK;
780 }
781}
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyb0c235e2014-06-20 12:00:00 -0700783/* header_len is the length of the ClientHello header written so far, used to
784 * compute padding. It does not include the record header. Pass 0 if no padding
785 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800786uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
787 size_t header_len) {
788 int extdatalen = 0;
789 uint8_t *ret = buf;
790 uint8_t *orig = buf;
791 /* See if we support any ECC ciphersuites */
792 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
795 size_t i;
David Benjamin107db582015-04-08 00:41:59 -0400796 uint32_t alg_k, alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800797 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
800 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 alg_k = c->algorithm_mkey;
803 alg_a = c->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -0400804 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 using_ecc = 1;
806 break;
807 }
808 }
809 }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 /* don't add extensions for SSLv3 unless doing secure renegotiation */
812 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
813 return orig;
814 }
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 if (ret >= limit) {
819 return NULL; /* should never occur. */
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 if (s->tlsext_hostname != NULL) {
823 /* Add TLS extension servername to the Client Hello message */
824 unsigned long size_str;
825 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 /* check for enough space.
828 4 for the servername type and entension length
829 2 for servernamelist length
830 1 for the hostname type
831 2 for hostname length
832 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 lenmax = limit - ret - 9;
835 size_str = strlen(s->tlsext_hostname);
836 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
837 return NULL;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 /* extension type and length */
841 s2n(TLSEXT_TYPE_server_name, ret);
842 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 /* length of servername list */
845 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 /* hostname type, length and hostname */
848 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
849 s2n(size_str, ret);
850 memcpy(ret, s->tlsext_hostname, size_str);
851 ret += size_str;
852 }
Adam Langley75712922014-10-10 16:23:43 -0700853
Adam Langleyfcf25832014-12-18 17:42:32 -0800854 /* Add RI if renegotiating */
855 if (s->renegotiate) {
856 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
859 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
860 return NULL;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 if ((limit - ret - 4 - el) < 0) {
864 return NULL;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 s2n(TLSEXT_TYPE_renegotiate, ret);
868 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
871 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
872 return NULL;
873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 ret += el;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 /* Add extended master secret. */
879 if (s->version != SSL3_VERSION) {
880 if (limit - ret - 4 < 0) {
881 return NULL;
882 }
883 s2n(TLSEXT_TYPE_extended_master_secret, ret);
884 s2n(0, ret);
885 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
888 int ticklen = 0;
889 if (!s->new_session && s->session && s->session->tlsext_tick) {
890 ticklen = s->session->tlsext_ticklen;
891 }
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 /* Check for enough room 2 for extension type, 2 for len rest for
894 * ticket. */
895 if ((long)(limit - ret - 4 - ticklen) < 0) {
896 return NULL;
897 }
898 s2n(TLSEXT_TYPE_session_ticket, ret);
899 s2n(ticklen, ret);
900 if (ticklen) {
901 memcpy(ret, s->session->tlsext_tick, ticklen);
902 ret += ticklen;
903 }
904 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700905
David Benjamin6ae7f072015-01-26 10:22:13 -0500906 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 size_t salglen;
908 const uint8_t *salg;
909 salglen = tls12_get_psigalgs(s, &salg);
910 if ((size_t)(limit - ret) < salglen + 6) {
911 return NULL;
912 }
913 s2n(TLSEXT_TYPE_signature_algorithms, ret);
914 s2n(salglen + 2, ret);
915 s2n(salglen, ret);
916 memcpy(ret, salg, salglen);
917 ret += salglen;
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 if (s->ocsp_stapling_enabled) {
921 /* The status_request extension is excessively extensible at every layer.
922 * On the client, only support requesting OCSP responses with an empty
923 * responder_id_list and no extensions. */
924 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
925 return NULL;
926 }
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 s2n(TLSEXT_TYPE_status_request, ret);
929 s2n(1 + 2 + 2, ret);
930 /* status_type */
931 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
932 /* responder_id_list - empty */
933 s2n(0, ret);
934 /* request_extensions - empty */
935 s2n(0, ret);
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
David Benjamin78e69782014-12-19 04:52:17 -0500938 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len &&
939 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 /* The client advertises an emtpy extension to indicate its support for
941 * Next Protocol Negotiation */
942 if (limit - ret - 4 < 0) {
943 return NULL;
944 }
945 s2n(TLSEXT_TYPE_next_proto_neg, ret);
946 s2n(0, ret);
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
950 /* The client advertises an empty extension to indicate its support for
951 * certificate timestamps. */
952 if (limit - ret - 4 < 0) {
953 return NULL;
954 }
955 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
956 s2n(0, ret);
957 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700958
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
960 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
961 return NULL;
962 }
963 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
964 s2n(2 + s->alpn_client_proto_list_len, ret);
965 s2n(s->alpn_client_proto_list_len, ret);
966 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
967 ret += s->alpn_client_proto_list_len;
968 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700969
David Benjamin78e69782014-12-19 04:52:17 -0500970 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 /* The client advertises an emtpy extension to indicate its support for
972 * Channel ID. */
973 if (limit - ret - 4 < 0) {
974 return NULL;
975 }
976 if (s->ctx->tlsext_channel_id_enabled_new) {
977 s2n(TLSEXT_TYPE_channel_id_new, ret);
978 } else {
979 s2n(TLSEXT_TYPE_channel_id, ret);
980 }
981 s2n(0, ret);
982 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 if (SSL_get_srtp_profiles(s)) {
985 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -0700986
Adam Langleyfcf25832014-12-18 17:42:32 -0800987 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -0700988
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 if ((limit - ret - 4 - el) < 0) {
990 return NULL;
991 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700992
Adam Langleyfcf25832014-12-18 17:42:32 -0800993 s2n(TLSEXT_TYPE_use_srtp, ret);
994 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
997 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
998 return NULL;
999 }
1000 ret += el;
1001 }
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 if (using_ecc) {
1004 /* Add TLS extension ECPointFormats to the ClientHello message */
1005 long lenmax;
1006 const uint8_t *formats;
1007 const uint16_t *curves;
1008 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 lenmax = limit - ret - 5;
1013 if (lenmax < 0) {
1014 return NULL;
1015 }
1016 if (formats_len > (size_t)lenmax) {
1017 return NULL;
1018 }
1019 if (formats_len > 255) {
1020 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1021 return NULL;
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1025 s2n(formats_len + 1, ret);
1026 *(ret++) = (uint8_t)formats_len;
1027 memcpy(ret, formats, formats_len);
1028 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 /* Add TLS extension EllipticCurves to the ClientHello message */
1031 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 lenmax = limit - ret - 6;
1034 if (lenmax < 0) {
1035 return NULL;
1036 }
1037 if (curves_len * 2 > (size_t)lenmax) {
1038 return NULL;
1039 }
1040 if (curves_len * 2 > 65532) {
1041 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1042 return NULL;
1043 }
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langleyfcf25832014-12-18 17:42:32 -08001045 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1046 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 s2n(curves_len * 2, ret);
1049 for (i = 0; i < curves_len; i++) {
1050 s2n(curves[i], ret);
1051 }
1052 }
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 if (header_len > 0) {
1055 size_t clienthello_minsize = 0;
1056 header_len += ret - orig;
1057 if (header_len > 0xff && header_len < 0x200) {
1058 /* Add padding to workaround bugs in F5 terminators. See
1059 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1060 *
1061 * NB: because this code works out the length of all existing extensions
1062 * it MUST always appear last. */
1063 clienthello_minsize = 0x200;
1064 }
1065 if (s->fastradio_padding) {
1066 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1067 * into DCH (high data rate) state in 3G networks. Note that when
1068 * fastradio_padding is enabled, even if the header_len is less than 255
1069 * bytes, the padding will be applied regardless. This is slightly
1070 * different from the TLS padding extension suggested in
1071 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1072 clienthello_minsize = 0x400;
1073 }
1074 if (header_len < clienthello_minsize) {
1075 size_t padding_len = clienthello_minsize - header_len;
1076 /* Extensions take at least four bytes to encode. Always include least
1077 * one byte of data if including the extension. WebSphere Application
1078 * Server 7.0 is intolerant to the last extension being zero-length. */
1079 if (padding_len >= 4 + 1) {
1080 padding_len -= 4;
1081 } else {
1082 padding_len = 1;
1083 }
Adam Langley95c29f32014-06-20 12:00:00 -07001084
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 if (limit - ret - 4 - (long)padding_len < 0) {
1086 return NULL;
1087 }
Adam Langley75712922014-10-10 16:23:43 -07001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 s2n(TLSEXT_TYPE_padding, ret);
1090 s2n(padding_len, ret);
1091 memset(ret, 0, padding_len);
1092 ret += padding_len;
1093 }
1094 }
Adam Langley75712922014-10-10 16:23:43 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 extdatalen = ret - orig - 2;
1097 if (extdatalen == 0) {
1098 return orig;
1099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 s2n(extdatalen, orig);
1102 return ret;
1103}
Adam Langley95c29f32014-06-20 12:00:00 -07001104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1106 int extdatalen = 0;
1107 uint8_t *orig = buf;
1108 uint8_t *ret = buf;
1109 int next_proto_neg_seen;
David Benjamin107db582015-04-08 00:41:59 -04001110 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1111 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001112 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001114
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1116 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1117 return orig;
1118 }
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 ret += 2;
1121 if (ret >= limit) {
1122 return NULL; /* should never happen. */
1123 }
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1126 if ((long)(limit - ret - 4) < 0) {
1127 return NULL;
1128 }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 s2n(TLSEXT_TYPE_server_name, ret);
1131 s2n(0, ret);
1132 }
Adam Langley95c29f32014-06-20 12:00:00 -07001133
Adam Langleyfcf25832014-12-18 17:42:32 -08001134 if (s->s3->send_connection_binding) {
1135 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1138 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1139 return NULL;
1140 }
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 if ((limit - ret - 4 - el) < 0) {
1143 return NULL;
1144 }
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 s2n(TLSEXT_TYPE_renegotiate, ret);
1147 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1150 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1151 return NULL;
1152 }
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 ret += el;
1155 }
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 if (s->s3->tmp.extended_master_secret) {
1158 if ((long)(limit - ret - 4) < 0) {
1159 return NULL;
1160 }
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1163 s2n(0, ret);
1164 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 if (using_ecc) {
1167 const uint8_t *plist;
1168 size_t plistlen;
1169 /* Add TLS extension ECPointFormats to the ServerHello message */
1170 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 tls1_get_formatlist(s, &plist, &plistlen);
1173
1174 lenmax = limit - ret - 5;
1175 if (lenmax < 0) {
1176 return NULL;
1177 }
1178 if (plistlen > (size_t)lenmax) {
1179 return NULL;
1180 }
1181 if (plistlen > 255) {
1182 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1183 return NULL;
1184 }
1185
1186 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1187 s2n(plistlen + 1, ret);
1188 *(ret++) = (uint8_t)plistlen;
1189 memcpy(ret, plist, plistlen);
1190 ret += plistlen;
1191 }
1192 /* Currently the server should not respond with a SupportedCurves extension */
1193
1194 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1195 if ((long)(limit - ret - 4) < 0) {
1196 return NULL;
1197 }
1198 s2n(TLSEXT_TYPE_session_ticket, ret);
1199 s2n(0, ret);
1200 }
1201
1202 if (s->s3->tmp.certificate_status_expected) {
1203 if ((long)(limit - ret - 4) < 0) {
1204 return NULL;
1205 }
1206 s2n(TLSEXT_TYPE_status_request, ret);
1207 s2n(0, ret);
1208 }
1209
1210 if (s->srtp_profile) {
1211 int el;
1212
1213 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1214
1215 if ((limit - ret - 4 - el) < 0) {
1216 return NULL;
1217 }
1218
1219 s2n(TLSEXT_TYPE_use_srtp, ret);
1220 s2n(el, ret);
1221
1222 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1223 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1224 return NULL;
1225 }
1226 ret += el;
1227 }
1228
1229 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1230 s->s3->next_proto_neg_seen = 0;
1231 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1232 const uint8_t *npa;
1233 unsigned int npalen;
1234 int r;
1235
1236 r = s->ctx->next_protos_advertised_cb(
1237 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1238 if (r == SSL_TLSEXT_ERR_OK) {
1239 if ((long)(limit - ret - 4 - npalen) < 0) {
1240 return NULL;
1241 }
1242 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1243 s2n(npalen, ret);
1244 memcpy(ret, npa, npalen);
1245 ret += npalen;
1246 s->s3->next_proto_neg_seen = 1;
1247 }
1248 }
1249
1250 if (s->s3->alpn_selected) {
1251 const uint8_t *selected = s->s3->alpn_selected;
1252 size_t len = s->s3->alpn_selected_len;
1253
1254 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1255 return NULL;
1256 }
1257 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1258 s2n(3 + len, ret);
1259 s2n(1 + len, ret);
1260 *ret++ = len;
1261 memcpy(ret, selected, len);
1262 ret += len;
1263 }
1264
1265 /* If the client advertised support for Channel ID, and we have it
1266 * enabled, then we want to echo it back. */
1267 if (s->s3->tlsext_channel_id_valid) {
1268 if (limit - ret - 4 < 0) {
1269 return NULL;
1270 }
1271 if (s->s3->tlsext_channel_id_new) {
1272 s2n(TLSEXT_TYPE_channel_id_new, ret);
1273 } else {
1274 s2n(TLSEXT_TYPE_channel_id, ret);
1275 }
1276 s2n(0, ret);
1277 }
1278
1279 extdatalen = ret - orig - 2;
1280 if (extdatalen == 0) {
1281 return orig;
1282 }
1283
1284 s2n(extdatalen, orig);
1285 return ret;
1286}
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langley95c29f32014-06-20 12:00:00 -07001288/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1289 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001290 * cbs: the contents of the extension, not including the type and length.
1291 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001292 * return.
1293 *
David Benjamindc72ff72014-06-25 12:36:10 -04001294 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001295static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1296 CBS protocol_name_list, protocol_name_list_copy;
1297 const uint8_t *selected;
1298 uint8_t selected_len;
1299 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001300
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 if (s->ctx->alpn_select_cb == NULL) {
1302 return 1;
1303 }
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1306 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1307 goto parse_error;
1308 }
Adam Langley95c29f32014-06-20 12:00:00 -07001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 /* Validate the protocol list. */
1311 protocol_name_list_copy = protocol_name_list;
1312 while (CBS_len(&protocol_name_list_copy) > 0) {
1313 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001314
Adam Langleyfcf25832014-12-18 17:42:32 -08001315 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1316 goto parse_error;
1317 }
1318 }
Adam Langley95c29f32014-06-20 12:00:00 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 r = s->ctx->alpn_select_cb(
1321 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1322 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1323 if (r == SSL_TLSEXT_ERR_OK) {
1324 if (s->s3->alpn_selected) {
1325 OPENSSL_free(s->s3->alpn_selected);
1326 }
1327 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1328 if (!s->s3->alpn_selected) {
1329 *out_alert = SSL_AD_INTERNAL_ERROR;
1330 return 0;
1331 }
1332 s->s3->alpn_selected_len = selected_len;
1333 }
1334
1335 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001336
1337parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 *out_alert = SSL_AD_DECODE_ERROR;
1339 return 0;
1340}
Adam Langley95c29f32014-06-20 12:00:00 -07001341
Adam Langleyfcf25832014-12-18 17:42:32 -08001342static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1343 int renegotiate_seen = 0;
1344 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001347 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 s->s3->next_proto_neg_seen = 0;
1349 s->s3->tmp.certificate_status_expected = 0;
1350 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 if (s->s3->alpn_selected) {
1353 OPENSSL_free(s->s3->alpn_selected);
1354 s->s3->alpn_selected = NULL;
1355 }
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 /* Clear any signature algorithms extension received */
1358 if (s->cert->peer_sigalgs) {
1359 OPENSSL_free(s->cert->peer_sigalgs);
1360 s->cert->peer_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04001361 s->cert->peer_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 }
Adam Langley95c29f32014-06-20 12:00:00 -07001363
Adam Langleyfcf25832014-12-18 17:42:32 -08001364 /* Clear any shared signature algorithms */
1365 if (s->cert->shared_sigalgs) {
1366 OPENSSL_free(s->cert->shared_sigalgs);
1367 s->cert->shared_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04001368 s->cert->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 }
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 /* Clear ECC extensions */
1372 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1373 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1374 s->s3->tmp.peer_ecpointformatlist = NULL;
1375 s->s3->tmp.peer_ecpointformatlist_length = 0;
1376 }
David Benjamindc72ff72014-06-25 12:36:10 -04001377
Adam Langleyfcf25832014-12-18 17:42:32 -08001378 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1379 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1380 s->s3->tmp.peer_ellipticcurvelist = NULL;
1381 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1382 }
David Benjamindc72ff72014-06-25 12:36:10 -04001383
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 /* There may be no extensions. */
1385 if (CBS_len(cbs) == 0) {
1386 goto ri_check;
1387 }
David Benjamindc72ff72014-06-25 12:36:10 -04001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 /* Decode the extensions block and check it is valid. */
1390 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1391 !tls1_check_duplicate_extensions(&extensions)) {
1392 *out_alert = SSL_AD_DECODE_ERROR;
1393 return 0;
1394 }
David Benjamindc72ff72014-06-25 12:36:10 -04001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 while (CBS_len(&extensions) != 0) {
1397 uint16_t type;
1398 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001399
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 /* Decode the next extension. */
1401 if (!CBS_get_u16(&extensions, &type) ||
1402 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1403 *out_alert = SSL_AD_DECODE_ERROR;
1404 return 0;
1405 }
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 if (s->tlsext_debug_cb) {
1408 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1409 CBS_len(&extension), s->tlsext_debug_arg);
1410 }
Adam Langley95c29f32014-06-20 12:00:00 -07001411
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 - Only the hostname type is supported with a maximum length of 255.
1415 - The servername is rejected if too long or if it contains zeros, in
1416 which case an fatal alert is generated.
1417 - The servername field is maintained together with the session cache.
1418 - When a session is resumed, the servername call back invoked in order
1419 to allow the application to position itself to the right context.
1420 - The servername is acknowledged if it is new for a session or when
1421 it is identical to a previously used for the same session.
1422 Applications can control the behaviour. They can at any time
1423 set a 'desirable' servername for a new SSL object. This can be the
1424 case for example with HTTPS when a Host: header field is received and
1425 a renegotiation is requested. In this case, a possible servername
1426 presented in the new client hello is only acknowledged if it matches
1427 the value of the Host: field.
1428 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1429 if they provide for changing an explicit servername context for the
1430 session,
1431 i.e. when the session has been established with a servername extension.
1432 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 if (type == TLSEXT_TYPE_server_name) {
1435 CBS server_name_list;
1436 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001437
Adam Langleyfcf25832014-12-18 17:42:32 -08001438 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1439 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1440 *out_alert = SSL_AD_DECODE_ERROR;
1441 return 0;
1442 }
David Benjamindc72ff72014-06-25 12:36:10 -04001443
Adam Langleyfcf25832014-12-18 17:42:32 -08001444 /* Decode each ServerName in the extension. */
1445 while (CBS_len(&server_name_list) > 0) {
1446 uint8_t name_type;
1447 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001448
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 /* Decode the NameType. */
1450 if (!CBS_get_u8(&server_name_list, &name_type)) {
1451 *out_alert = SSL_AD_DECODE_ERROR;
1452 return 0;
1453 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 /* Only host_name is supported. */
1456 if (name_type != TLSEXT_NAMETYPE_host_name) {
1457 continue;
1458 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460 if (have_seen_host_name) {
1461 /* The ServerNameList MUST NOT contain more than one name of the same
1462 * name_type. */
1463 *out_alert = SSL_AD_DECODE_ERROR;
1464 return 0;
1465 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001466
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1470 CBS_len(&host_name) < 1) {
1471 *out_alert = SSL_AD_DECODE_ERROR;
1472 return 0;
1473 }
Adam Langley95c29f32014-06-20 12:00:00 -07001474
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1476 CBS_contains_zero_byte(&host_name)) {
1477 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1478 return 0;
1479 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001480
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 if (!s->hit) {
1482 assert(s->session->tlsext_hostname == NULL);
1483 if (s->session->tlsext_hostname) {
1484 /* This should be impossible. */
1485 *out_alert = SSL_AD_DECODE_ERROR;
1486 return 0;
1487 }
Adam Langley95c29f32014-06-20 12:00:00 -07001488
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 /* Copy the hostname as a string. */
1490 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1491 *out_alert = SSL_AD_INTERNAL_ERROR;
1492 return 0;
1493 }
Adam Langley95c29f32014-06-20 12:00:00 -07001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 s->should_ack_sni = 1;
1496 }
1497 }
1498 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1499 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001500
Adam Langleyfcf25832014-12-18 17:42:32 -08001501 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1502 CBS_len(&extension) != 0) {
1503 *out_alert = SSL_AD_DECODE_ERROR;
1504 return 0;
1505 }
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1508 &s->s3->tmp.peer_ecpointformatlist_length)) {
1509 *out_alert = SSL_AD_INTERNAL_ERROR;
1510 return 0;
1511 }
1512 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1513 CBS elliptic_curve_list;
1514 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1517 CBS_len(&elliptic_curve_list) == 0 ||
1518 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1519 CBS_len(&extension) != 0) {
1520 *out_alert = SSL_AD_DECODE_ERROR;
1521 return 0;
1522 }
David Benjamindc72ff72014-06-25 12:36:10 -04001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 if (s->s3->tmp.peer_ellipticcurvelist) {
1525 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1526 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1527 }
David Benjamindc72ff72014-06-25 12:36:10 -04001528
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 s->s3->tmp.peer_ellipticcurvelist =
1530 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1533 *out_alert = SSL_AD_INTERNAL_ERROR;
1534 return 0;
1535 }
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 num_curves = CBS_len(&elliptic_curve_list) / 2;
1538 for (i = 0; i < num_curves; i++) {
1539 if (!CBS_get_u16(&elliptic_curve_list,
1540 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1541 *out_alert = SSL_AD_INTERNAL_ERROR;
1542 return 0;
1543 }
1544 }
David Benjamindc72ff72014-06-25 12:36:10 -04001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 if (CBS_len(&elliptic_curve_list) != 0) {
1547 *out_alert = SSL_AD_INTERNAL_ERROR;
1548 return 0;
1549 }
Adam Langley95c29f32014-06-20 12:00:00 -07001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1552 } else if (type == TLSEXT_TYPE_renegotiate) {
1553 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1554 return 0;
1555 }
1556 renegotiate_seen = 1;
1557 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1558 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001559
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 if (!CBS_get_u16_length_prefixed(&extension,
1561 &supported_signature_algorithms) ||
1562 CBS_len(&extension) != 0) {
1563 *out_alert = SSL_AD_DECODE_ERROR;
1564 return 0;
1565 }
Adam Langley95c29f32014-06-20 12:00:00 -07001566
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 /* Ensure the signature algorithms are non-empty. It contains a list of
1568 * SignatureAndHashAlgorithms which are two bytes each. */
1569 if (CBS_len(&supported_signature_algorithms) == 0 ||
1570 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1571 *out_alert = SSL_AD_DECODE_ERROR;
1572 return 0;
1573 }
David Benjamindc72ff72014-06-25 12:36:10 -04001574
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1576 *out_alert = SSL_AD_DECODE_ERROR;
1577 return 0;
1578 }
1579 /* If sigalgs received and no shared algorithms fatal error. */
1580 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001581 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1583 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1584 return 0;
1585 }
1586 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001587 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL &&
1588 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 /* The extension must be empty. */
1590 if (CBS_len(&extension) != 0) {
1591 *out_alert = SSL_AD_DECODE_ERROR;
1592 return 0;
1593 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 /* We shouldn't accept this extension on a renegotiation.
1596 *
1597 * s->new_session will be set on renegotiation, but we probably shouldn't
1598 * rely that it couldn't be set on the initial renegotation too in
1599 * certain cases (when there's some other reason to disallow resuming an
1600 * earlier session -- the current code won't be doing anything like that,
1601 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001602
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 * A valid sign that there's been a previous handshake in this connection
1604 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1605 * that will happen in the Hello protocol round, well before a new
1606 * Finished message could have been computed.) */
1607 s->s3->next_proto_neg_seen = 1;
1608 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1609 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1610 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1611 return 0;
1612 }
1613 /* ALPN takes precedence over NPN. */
1614 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001615 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1616 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 /* The extension must be empty. */
1618 if (CBS_len(&extension) != 0) {
1619 *out_alert = SSL_AD_DECODE_ERROR;
1620 return 0;
1621 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001622
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 s->s3->tlsext_channel_id_valid = 1;
1624 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001625 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 /* The extension must be empty. */
1627 if (CBS_len(&extension) != 0) {
1628 *out_alert = SSL_AD_DECODE_ERROR;
1629 return 0;
1630 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 s->s3->tlsext_channel_id_valid = 1;
1633 s->s3->tlsext_channel_id_new = 1;
1634 } else if (type == TLSEXT_TYPE_use_srtp) {
1635 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1636 return 0;
1637 }
1638 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1639 s->version != SSL3_VERSION) {
1640 if (CBS_len(&extension) != 0) {
1641 *out_alert = SSL_AD_DECODE_ERROR;
1642 return 0;
1643 }
Adam Langley75712922014-10-10 16:23:43 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 s->s3->tmp.extended_master_secret = 1;
1646 }
1647 }
Adam Langley75712922014-10-10 16:23:43 -07001648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649ri_check:
1650 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 if (!renegotiate_seen && s->renegotiate &&
1653 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1654 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1655 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1656 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1657 return 0;
1658 }
Adam Langley95c29f32014-06-20 12:00:00 -07001659
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 return 1;
1661}
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1664 int alert = -1;
1665 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1666 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1667 return 0;
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 if (ssl_check_clienthello_tlsext(s) <= 0) {
1671 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1672 SSL_R_CLIENTHELLO_TLSEXT);
1673 return 0;
1674 }
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 return 1;
1677}
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langley95c29f32014-06-20 12:00:00 -07001679/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 * elements of zero length are allowed and the set of elements must exactly
1681 * fill the length of the block. */
1682static char ssl_next_proto_validate(const CBS *cbs) {
1683 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 while (CBS_len(&copy) != 0) {
1686 CBS proto;
1687 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1688 return 0;
1689 }
1690 }
Adam Langley95c29f32014-06-20 12:00:00 -07001691
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 return 1;
1693}
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1696 int tlsext_servername = 0;
1697 int renegotiate_seen = 0;
1698 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 /* TODO(davidben): Move all of these to some per-handshake state that gets
1701 * systematically reset on a new handshake; perhaps allocate it fresh each
1702 * time so it's not even kept around post-handshake. */
1703 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 s->tlsext_ticket_expected = 0;
1705 s->s3->tmp.certificate_status_expected = 0;
1706 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001707 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 if (s->s3->alpn_selected) {
1710 OPENSSL_free(s->s3->alpn_selected);
1711 s->s3->alpn_selected = NULL;
1712 }
David Benjamina19fc252014-10-19 00:14:36 -04001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 /* Clear ECC extensions */
1715 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1716 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1717 s->s3->tmp.peer_ecpointformatlist = NULL;
1718 s->s3->tmp.peer_ecpointformatlist_length = 0;
1719 }
David Benjamin03973092014-06-24 23:27:17 -04001720
Adam Langleyfcf25832014-12-18 17:42:32 -08001721 /* There may be no extensions. */
1722 if (CBS_len(cbs) == 0) {
1723 goto ri_check;
1724 }
Adam Langley95c29f32014-06-20 12:00:00 -07001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 /* Decode the extensions block and check it is valid. */
1727 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1728 !tls1_check_duplicate_extensions(&extensions)) {
1729 *out_alert = SSL_AD_DECODE_ERROR;
1730 return 0;
1731 }
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 while (CBS_len(&extensions) != 0) {
1734 uint16_t type;
1735 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 /* Decode the next extension. */
1738 if (!CBS_get_u16(&extensions, &type) ||
1739 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1740 *out_alert = SSL_AD_DECODE_ERROR;
1741 return 0;
1742 }
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 if (s->tlsext_debug_cb) {
1745 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1746 CBS_len(&extension), s->tlsext_debug_arg);
1747 }
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 if (type == TLSEXT_TYPE_server_name) {
1750 /* The extension must be empty. */
1751 if (CBS_len(&extension) != 0) {
1752 *out_alert = SSL_AD_DECODE_ERROR;
1753 return 0;
1754 }
David Benjamin03973092014-06-24 23:27:17 -04001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 /* We must have sent it in ClientHello. */
1757 if (s->tlsext_hostname == NULL) {
1758 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1759 return 0;
1760 }
David Benjamin03973092014-06-24 23:27:17 -04001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 tlsext_servername = 1;
1763 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1764 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1767 CBS_len(&extension) != 0) {
1768 *out_alert = SSL_AD_DECODE_ERROR;
1769 return 0;
1770 }
Adam Langley95c29f32014-06-20 12:00:00 -07001771
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1773 &s->s3->tmp.peer_ecpointformatlist_length)) {
1774 *out_alert = SSL_AD_INTERNAL_ERROR;
1775 return 0;
1776 }
1777 } else if (type == TLSEXT_TYPE_session_ticket) {
1778 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1779 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1780 return 0;
1781 }
David Benjamin03973092014-06-24 23:27:17 -04001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 s->tlsext_ticket_expected = 1;
1784 } else if (type == TLSEXT_TYPE_status_request) {
1785 /* The extension MUST be empty and may only sent if we've requested a
1786 * status request message. */
1787 if (CBS_len(&extension) != 0) {
1788 *out_alert = SSL_AD_DECODE_ERROR;
1789 return 0;
1790 }
David Benjamin03973092014-06-24 23:27:17 -04001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 if (!s->ocsp_stapling_enabled) {
1793 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1794 return 0;
1795 }
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 /* Set a flag to expect a CertificateStatus message */
1798 s->s3->tmp.certificate_status_expected = 1;
1799 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001800 s->s3->tmp.finish_md_len == 0 &&
1801 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 uint8_t *selected;
1803 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 /* We must have requested it. */
1806 if (s->ctx->next_proto_select_cb == NULL) {
1807 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1808 return 0;
1809 }
David Benjamin03973092014-06-24 23:27:17 -04001810
Adam Langleyfcf25832014-12-18 17:42:32 -08001811 /* The data must be valid. */
1812 if (!ssl_next_proto_validate(&extension)) {
1813 *out_alert = SSL_AD_DECODE_ERROR;
1814 return 0;
1815 }
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 if (s->ctx->next_proto_select_cb(
1818 s, &selected, &selected_len, CBS_data(&extension),
1819 CBS_len(&extension),
1820 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1821 *out_alert = SSL_AD_INTERNAL_ERROR;
1822 return 0;
1823 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1826 if (s->next_proto_negotiated == NULL) {
1827 *out_alert = SSL_AD_INTERNAL_ERROR;
1828 return 0;
1829 }
Adam Langley75712922014-10-10 16:23:43 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 s->next_proto_negotiated_len = selected_len;
1832 s->s3->next_proto_neg_seen = 1;
1833 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1834 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 /* We must have requested it. */
1837 if (s->alpn_client_proto_list == NULL) {
1838 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1839 return 0;
1840 }
Adam Langley95c29f32014-06-20 12:00:00 -07001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 /* The extension data consists of a ProtocolNameList which must have
1843 * exactly one ProtocolName. Each of these is length-prefixed. */
1844 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1845 CBS_len(&extension) != 0 ||
1846 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1847 CBS_len(&protocol_name_list) != 0) {
1848 *out_alert = SSL_AD_DECODE_ERROR;
1849 return 0;
1850 }
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1853 &s->s3->alpn_selected_len)) {
1854 *out_alert = SSL_AD_INTERNAL_ERROR;
1855 return 0;
1856 }
David Benjamin78e69782014-12-19 04:52:17 -05001857 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 if (CBS_len(&extension) != 0) {
1859 *out_alert = SSL_AD_DECODE_ERROR;
1860 return 0;
1861 }
Adam Langley95c29f32014-06-20 12:00:00 -07001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05001864 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 if (CBS_len(&extension) != 0) {
1866 *out_alert = SSL_AD_DECODE_ERROR;
1867 return 0;
1868 }
Adam Langley95c29f32014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 s->s3->tlsext_channel_id_valid = 1;
1871 s->s3->tlsext_channel_id_new = 1;
1872 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1873 if (CBS_len(&extension) == 0) {
1874 *out_alert = SSL_AD_DECODE_ERROR;
1875 return 0;
1876 }
Adam Langley95c29f32014-06-20 12:00:00 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 /* Session resumption uses the original session information. */
1879 if (!s->hit &&
1880 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1881 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1882 *out_alert = SSL_AD_INTERNAL_ERROR;
1883 return 0;
1884 }
1885 } else if (type == TLSEXT_TYPE_renegotiate) {
1886 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1887 return 0;
1888 }
Adam Langley95c29f32014-06-20 12:00:00 -07001889
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 renegotiate_seen = 1;
1891 } else if (type == TLSEXT_TYPE_use_srtp) {
1892 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1893 return 0;
1894 }
1895 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1896 if (/* It is invalid for the server to select EMS and
1897 SSLv3. */
1898 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1899 *out_alert = SSL_AD_DECODE_ERROR;
1900 return 0;
1901 }
Adam Langley95c29f32014-06-20 12:00:00 -07001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 s->s3->tmp.extended_master_secret = 1;
1904 }
1905 }
Adam Langley95c29f32014-06-20 12:00:00 -07001906
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1908 if (s->session->tlsext_hostname == NULL) {
1909 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1910 if (!s->session->tlsext_hostname) {
1911 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1912 return 0;
1913 }
1914 } else {
1915 *out_alert = SSL_AD_DECODE_ERROR;
1916 return 0;
1917 }
1918 }
Adam Langley95c29f32014-06-20 12:00:00 -07001919
Adam Langleyfcf25832014-12-18 17:42:32 -08001920ri_check:
1921 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1922 * attack we should *always* see RI even on initial server hello because the
1923 * client doesn't see any renegotiation during an attack. However this would
1924 * mean we could not connect to any server which doesn't support RI so for
1925 * the immediate future tolerate RI absence on initial connect only. */
1926 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1927 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1928 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1929 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1930 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1931 return 0;
1932 }
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 return 1;
1935}
Adam Langley95c29f32014-06-20 12:00:00 -07001936
Adam Langleyfcf25832014-12-18 17:42:32 -08001937int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001938
Adam Langleyfcf25832014-12-18 17:42:32 -08001939int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941static int ssl_check_clienthello_tlsext(SSL *s) {
1942 int ret = SSL_TLSEXT_ERR_NOACK;
1943 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001944
Adam Langleyfcf25832014-12-18 17:42:32 -08001945 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1946 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1949 ret = s->ctx->tlsext_servername_callback(s, &al,
1950 s->ctx->tlsext_servername_arg);
1951 } else if (s->initial_ctx != NULL &&
1952 s->initial_ctx->tlsext_servername_callback != 0) {
1953 ret = s->initial_ctx->tlsext_servername_callback(
1954 s, &al, s->initial_ctx->tlsext_servername_arg);
1955 }
Adam Langley95c29f32014-06-20 12:00:00 -07001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957 switch (ret) {
1958 case SSL_TLSEXT_ERR_ALERT_FATAL:
1959 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1960 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 case SSL_TLSEXT_ERR_ALERT_WARNING:
1963 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1964 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 case SSL_TLSEXT_ERR_NOACK:
1967 s->should_ack_sni = 0;
1968 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 default:
1971 return 1;
1972 }
1973}
Adam Langleyed8270a2014-09-02 13:52:56 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975static int ssl_check_serverhello_tlsext(SSL *s) {
1976 int ret = SSL_TLSEXT_ERR_NOACK;
1977 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 /* If we are client and using an elliptic curve cryptography cipher suite,
1980 * then if server returns an EC point formats lists extension it must contain
1981 * uncompressed. */
David Benjamin107db582015-04-08 00:41:59 -04001982 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1983 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001984 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
1986 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
1987 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1988 return -1;
1989 }
1990 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1993 ret = s->ctx->tlsext_servername_callback(s, &al,
1994 s->ctx->tlsext_servername_arg);
1995 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04001996 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 ret = s->initial_ctx->tlsext_servername_callback(
1998 s, &al, s->initial_ctx->tlsext_servername_arg);
1999 }
Adam Langley95c29f32014-06-20 12:00:00 -07002000
Adam Langleyfcf25832014-12-18 17:42:32 -08002001 switch (ret) {
2002 case SSL_TLSEXT_ERR_ALERT_FATAL:
2003 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2004 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002005
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 case SSL_TLSEXT_ERR_ALERT_WARNING:
2007 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2008 return 1;
2009
2010 default:
2011 return 1;
2012 }
2013}
2014
2015int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2016 int alert = -1;
2017 if (s->version < SSL3_VERSION) {
2018 return 1;
2019 }
2020
2021 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2022 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2023 return 0;
2024 }
2025
2026 if (ssl_check_serverhello_tlsext(s) <= 0) {
2027 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2028 SSL_R_SERVERHELLO_TLSEXT);
2029 return 0;
2030 }
2031
2032 return 1;
2033}
Adam Langley95c29f32014-06-20 12:00:00 -07002034
2035/* Since the server cache lookup is done early on in the processing of the
2036 * ClientHello, and other operations depend on the result, we need to handle
2037 * any TLS session ticket extension at the same time.
2038 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002039 * ctx: contains the early callback context, which is the result of a
2040 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002041 * ret: (output) on return, if a ticket was decrypted, then this is set to
2042 * point to the resulting session.
2043 *
Adam Langley95c29f32014-06-20 12:00:00 -07002044 * Returns:
2045 * -1: fatal error, either from parsing or decrypting the ticket.
2046 * 0: no ticket was found (or was ignored, based on settings).
2047 * 1: a zero length extension was found, indicating that the client supports
2048 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002049 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2050 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002051 * 3: a ticket was successfully decrypted and *ret was set.
2052 *
2053 * Side effects:
2054 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2055 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002056 * but the client either doesn't have a session ticket or we couldn't use
2057 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2058 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002059 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002060int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002061 SSL_SESSION **ret) {
2062 *ret = NULL;
2063 s->tlsext_ticket_expected = 0;
2064 const uint8_t *data;
2065 size_t len;
2066 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 /* If tickets disabled behave as if no ticket present to permit stateful
2069 * resumption. */
2070 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2071 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2072 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2073 &data, &len)) {
2074 return 0;
2075 }
2076
2077 if (len == 0) {
2078 /* The client will accept a ticket but doesn't currently have one. */
2079 s->tlsext_ticket_expected = 1;
2080 return 1;
2081 }
2082
2083 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2084 ret);
2085 switch (r) {
2086 case 2: /* ticket couldn't be decrypted */
2087 s->tlsext_ticket_expected = 1;
2088 return 2;
2089
2090 case 3: /* ticket was decrypted */
2091 return r;
2092
2093 case 4: /* ticket decrypted but need to renew */
2094 s->tlsext_ticket_expected = 1;
2095 return 3;
2096
2097 default: /* fatal error */
2098 return -1;
2099 }
2100}
Adam Langley95c29f32014-06-20 12:00:00 -07002101
2102/* tls_decrypt_ticket attempts to decrypt a session ticket.
2103 *
2104 * etick: points to the body of the session ticket extension.
2105 * eticklen: the length of the session tickets extenion.
2106 * sess_id: points at the session ID.
2107 * sesslen: the length of the session ID.
2108 * psess: (output) on return, if a ticket was decrypted, then this is set to
2109 * point to the resulting session.
2110 *
2111 * Returns:
2112 * -1: fatal error, either from parsing or decrypting the ticket.
2113 * 2: the ticket couldn't be decrypted.
2114 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 * 4: same as 3, but the ticket needs to be renewed. */
2116static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2117 const uint8_t *sess_id, int sesslen,
2118 SSL_SESSION **psess) {
2119 SSL_SESSION *sess;
2120 uint8_t *sdec;
2121 const uint8_t *p;
2122 int slen, mlen, renew_ticket = 0;
2123 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2124 HMAC_CTX hctx;
2125 EVP_CIPHER_CTX ctx;
2126 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 /* Need at least keyname + iv + some encrypted data */
2129 if (eticklen < 48) {
2130 return 2;
2131 }
2132
2133 /* Initialize session ticket encryption and HMAC contexts */
2134 HMAC_CTX_init(&hctx);
2135 EVP_CIPHER_CTX_init(&ctx);
2136 if (tctx->tlsext_ticket_key_cb) {
2137 uint8_t *nctick = (uint8_t *)etick;
2138 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2139 if (rv < 0) {
2140 return -1;
2141 }
2142 if (rv == 0) {
2143 return 2;
2144 }
2145 if (rv == 2) {
2146 renew_ticket = 1;
2147 }
2148 } else {
2149 /* Check key name matches */
2150 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2151 return 2;
2152 }
2153 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2154 NULL) ||
2155 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2156 tctx->tlsext_tick_aes_key, etick + 16)) {
2157 HMAC_CTX_cleanup(&hctx);
2158 EVP_CIPHER_CTX_cleanup(&ctx);
2159 return -1;
2160 }
2161 }
2162
2163 /* Attempt to process session ticket, first conduct sanity and integrity
2164 * checks on ticket. */
2165 mlen = HMAC_size(&hctx);
2166 if (mlen < 0) {
2167 HMAC_CTX_cleanup(&hctx);
2168 EVP_CIPHER_CTX_cleanup(&ctx);
2169 return -1;
2170 }
2171 eticklen -= mlen;
2172 /* Check HMAC of encrypted ticket */
2173 HMAC_Update(&hctx, etick, eticklen);
2174 HMAC_Final(&hctx, tick_hmac, NULL);
2175 HMAC_CTX_cleanup(&hctx);
2176 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2177 EVP_CIPHER_CTX_cleanup(&ctx);
2178 return 2;
2179 }
2180
2181 /* Attempt to decrypt session data */
2182 /* Move p after IV to start of encrypted ticket, update length */
2183 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2184 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2185 sdec = OPENSSL_malloc(eticklen);
2186 if (!sdec) {
2187 EVP_CIPHER_CTX_cleanup(&ctx);
2188 return -1;
2189 }
2190 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2191 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2192 EVP_CIPHER_CTX_cleanup(&ctx);
2193 OPENSSL_free(sdec);
2194 return 2;
2195 }
2196 slen += mlen;
2197 EVP_CIPHER_CTX_cleanup(&ctx);
2198 p = sdec;
2199
2200 sess = d2i_SSL_SESSION(NULL, &p, slen);
2201 OPENSSL_free(sdec);
2202 if (sess) {
2203 /* The session ID, if non-empty, is used by some clients to detect that the
2204 * ticket has been accepted. So we copy it to the session structure. If it
2205 * is empty set length to zero as required by standard. */
2206 if (sesslen) {
2207 memcpy(sess->session_id, sess_id, sesslen);
2208 }
2209 sess->session_id_length = sesslen;
2210 *psess = sess;
2211 if (renew_ticket) {
2212 return 4;
2213 }
2214 return 3;
2215 }
2216
2217 ERR_clear_error();
2218 /* For session parse failure, indicate that we need to send a new ticket. */
2219 return 2;
2220}
Adam Langley95c29f32014-06-20 12:00:00 -07002221
2222/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002223typedef struct {
2224 int nid;
2225 int id;
2226} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2229 {NID_sha1, TLSEXT_hash_sha1},
2230 {NID_sha224, TLSEXT_hash_sha224},
2231 {NID_sha256, TLSEXT_hash_sha256},
2232 {NID_sha384, TLSEXT_hash_sha384},
2233 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002234
Adam Langleyfcf25832014-12-18 17:42:32 -08002235static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2236 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002237
Adam Langleyfcf25832014-12-18 17:42:32 -08002238static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2239 size_t i;
2240 for (i = 0; i < tlen; i++) {
2241 if (table[i].nid == nid) {
2242 return table[i].id;
2243 }
2244 }
Adam Langley95c29f32014-06-20 12:00:00 -07002245
Adam Langleyfcf25832014-12-18 17:42:32 -08002246 return -1;
2247}
Adam Langley95c29f32014-06-20 12:00:00 -07002248
Adam Langleyfcf25832014-12-18 17:42:32 -08002249static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2250 size_t i;
2251 for (i = 0; i < tlen; i++) {
2252 if (table[i].id == id) {
2253 return table[i].nid;
2254 }
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 return NID_undef;
2258}
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2261 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 if (!md) {
2264 return 0;
2265 }
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2268 sizeof(tls12_md) / sizeof(tls12_lookup));
2269 if (md_id == -1) {
2270 return 0;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 sig_id = tls12_get_sigid(pk);
2274 if (sig_id == -1) {
2275 return 0;
2276 }
Adam Langley95c29f32014-06-20 12:00:00 -07002277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 p[0] = (uint8_t)md_id;
2279 p[1] = (uint8_t)sig_id;
2280 return 1;
2281}
2282
2283int tls12_get_sigid(const EVP_PKEY *pk) {
2284 return tls12_find_id(pk->type, tls12_sig,
2285 sizeof(tls12_sig) / sizeof(tls12_lookup));
2286}
2287
2288const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2289 switch (hash_alg) {
2290 case TLSEXT_hash_md5:
2291 return EVP_md5();
2292
2293 case TLSEXT_hash_sha1:
2294 return EVP_sha1();
2295
2296 case TLSEXT_hash_sha224:
2297 return EVP_sha224();
2298
2299 case TLSEXT_hash_sha256:
2300 return EVP_sha256();
2301
2302 case TLSEXT_hash_sha384:
2303 return EVP_sha384();
2304
2305 case TLSEXT_hash_sha512:
2306 return EVP_sha512();
2307
2308 default:
2309 return NULL;
2310 }
2311}
Adam Langley95c29f32014-06-20 12:00:00 -07002312
David Benjaminec2f27d2014-11-13 19:17:25 -05002313/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2314 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002315static int tls12_get_pkey_type(uint8_t sig_alg) {
2316 switch (sig_alg) {
2317 case TLSEXT_signature_rsa:
2318 return EVP_PKEY_RSA;
2319
2320 case TLSEXT_signature_ecdsa:
2321 return EVP_PKEY_EC;
2322
2323 default:
2324 return -1;
2325 }
2326}
Adam Langley95c29f32014-06-20 12:00:00 -07002327
2328/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2329static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 int *psignhash_nid, const uint8_t *data) {
2331 int sign_nid = 0, hash_nid = 0;
2332 if (!phash_nid && !psign_nid && !psignhash_nid) {
2333 return;
2334 }
2335
2336 if (phash_nid || psignhash_nid) {
2337 hash_nid = tls12_find_nid(data[0], tls12_md,
2338 sizeof(tls12_md) / sizeof(tls12_lookup));
2339 if (phash_nid) {
2340 *phash_nid = hash_nid;
2341 }
2342 }
2343
2344 if (psign_nid || psignhash_nid) {
2345 sign_nid = tls12_find_nid(data[1], tls12_sig,
2346 sizeof(tls12_sig) / sizeof(tls12_lookup));
2347 if (psign_nid) {
2348 *psign_nid = sign_nid;
2349 }
2350 }
2351
2352 if (psignhash_nid) {
2353 if (sign_nid && hash_nid) {
2354 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2355 } else {
2356 *psignhash_nid = NID_undef;
2357 }
2358 }
2359}
2360
Adam Langley95c29f32014-06-20 12:00:00 -07002361/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002362static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2363 size_t preflen, const uint8_t *allow,
2364 size_t allowlen) {
2365 const uint8_t *ptmp, *atmp;
2366 size_t i, j, nmatch = 0;
2367
2368 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2369 /* Skip disabled hashes or signature algorithms */
2370 if (tls12_get_hash(ptmp[0]) == NULL ||
2371 tls12_get_pkey_type(ptmp[1]) == -1) {
2372 continue;
2373 }
2374
2375 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2376 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2377 nmatch++;
2378 if (shsig) {
2379 shsig->rhash = ptmp[0];
2380 shsig->rsign = ptmp[1];
2381 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2382 &shsig->signandhash_nid, ptmp);
2383 shsig++;
2384 }
2385
2386 break;
2387 }
2388 }
2389 }
2390
2391 return nmatch;
2392}
Adam Langley95c29f32014-06-20 12:00:00 -07002393
2394/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002395static int tls1_set_shared_sigalgs(SSL *s) {
2396 const uint8_t *pref, *allow, *conf;
2397 size_t preflen, allowlen, conflen;
2398 size_t nmatch;
2399 TLS_SIGALGS *salgs = NULL;
2400 CERT *c = s->cert;
2401
2402 if (c->shared_sigalgs) {
2403 OPENSSL_free(c->shared_sigalgs);
2404 c->shared_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04002405 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 }
2407
2408 /* If client use client signature algorithms if not NULL */
2409 if (!s->server && c->client_sigalgs) {
2410 conf = c->client_sigalgs;
2411 conflen = c->client_sigalgslen;
2412 } else if (c->conf_sigalgs) {
2413 conf = c->conf_sigalgs;
2414 conflen = c->conf_sigalgslen;
2415 } else {
2416 conflen = tls12_get_psigalgs(s, &conf);
2417 }
2418
2419 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2420 pref = conf;
2421 preflen = conflen;
2422 allow = c->peer_sigalgs;
2423 allowlen = c->peer_sigalgslen;
2424 } else {
2425 allow = conf;
2426 allowlen = conflen;
2427 pref = c->peer_sigalgs;
2428 preflen = c->peer_sigalgslen;
2429 }
2430
2431 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2432 if (!nmatch) {
2433 return 1;
2434 }
2435
2436 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2437 if (!salgs) {
2438 return 0;
2439 }
2440
2441 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2442 c->shared_sigalgs = salgs;
2443 c->shared_sigalgslen = nmatch;
2444 return 1;
2445}
Adam Langley95c29f32014-06-20 12:00:00 -07002446
2447/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002448int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2449 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002450
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 /* Extension ignored for inappropriate versions */
2452 if (!SSL_USE_SIGALGS(s)) {
2453 return 1;
2454 }
David Benjamincd996942014-07-20 16:23:51 -04002455
David Benjamin61c0d4e2015-03-19 14:24:37 -04002456 if (CBS_len(sigalgs) % 2 != 0 ||
2457 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2458 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 return 0;
2460 }
Adam Langley95c29f32014-06-20 12:00:00 -07002461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 return 1;
2463}
David Benjaminec2f27d2014-11-13 19:17:25 -05002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2466 CERT *c = s->cert;
2467 int type = EVP_PKEY_id(pkey);
2468 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 /* Select the first shared digest supported by our key. */
2471 for (i = 0; i < c->shared_sigalgslen; i++) {
2472 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2473 if (md == NULL ||
2474 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2475 !EVP_PKEY_supports_digest(pkey, md)) {
2476 continue;
2477 }
2478 return md;
2479 }
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 /* If no suitable digest may be found, default to SHA-1. */
2482 return EVP_sha1();
2483}
Adam Langley95c29f32014-06-20 12:00:00 -07002484
Adam Langleyfcf25832014-12-18 17:42:32 -08002485int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2486 uint8_t *rsig, uint8_t *rhash) {
2487 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002488
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 if (psig == NULL) {
2490 return 0;
2491 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002492
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 if (idx >= 0) {
2494 idx <<= 1;
2495 if (idx >= (int)s->cert->peer_sigalgslen) {
2496 return 0;
2497 }
2498 psig += idx;
2499 if (rhash) {
2500 *rhash = psig[0];
2501 }
2502 if (rsig) {
2503 *rsig = psig[1];
2504 }
2505 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2506 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 return s->cert->peer_sigalgslen / 2;
2509}
Adam Langley1258b6a2014-06-20 12:00:00 -07002510
Adam Langleyfcf25832014-12-18 17:42:32 -08002511int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2512 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2513 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2516 return 0;
2517 }
2518
2519 shsigalgs += idx;
2520 if (phash) {
2521 *phash = shsigalgs->hash_nid;
2522 }
2523 if (psign) {
2524 *psign = shsigalgs->sign_nid;
2525 }
2526 if (psignhash) {
2527 *psignhash = shsigalgs->signandhash_nid;
2528 }
2529 if (rsig) {
2530 *rsig = shsigalgs->rsign;
2531 }
2532 if (rhash) {
2533 *rhash = shsigalgs->rhash;
2534 }
2535
2536 return s->cert->shared_sigalgslen;
2537}
2538
2539/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2540 * given SSL connection and writes it to |md|. */
2541int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2542 EVP_MD_CTX ctx;
2543 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2544 unsigned temp_digest_len;
2545 int i;
2546 static const char kClientIDMagic[] = "TLS Channel ID signature";
2547
2548 if (s->s3->handshake_buffer &&
2549 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2550 return 0;
2551 }
2552
2553 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2554
2555 if (s->hit && s->s3->tlsext_channel_id_new) {
2556 static const char kResumptionMagic[] = "Resumption";
2557 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2558 if (s->session->original_handshake_hash_len == 0) {
2559 return 0;
2560 }
2561 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2562 s->session->original_handshake_hash_len);
2563 }
2564
2565 EVP_MD_CTX_init(&ctx);
2566 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2567 if (s->s3->handshake_dgst[i] == NULL) {
2568 continue;
2569 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002570 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2571 EVP_MD_CTX_cleanup(&ctx);
2572 return 0;
2573 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2575 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2576 }
2577 EVP_MD_CTX_cleanup(&ctx);
2578
2579 return 1;
2580}
Adam Langley1258b6a2014-06-20 12:00:00 -07002581
2582/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2583 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002584int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2585 int digest_len;
2586 /* This function should never be called for a resumed session because the
2587 * handshake hashes that we wish to record are for the original, full
2588 * handshake. */
2589 if (s->hit) {
2590 return -1;
2591 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593 /* It only makes sense to call this function if Channel IDs have been
2594 * negotiated. */
2595 if (!s->s3->tlsext_channel_id_new) {
2596 return -1;
2597 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 digest_len =
2600 tls1_handshake_digest(s, s->session->original_handshake_hash,
2601 sizeof(s->session->original_handshake_hash));
2602 if (digest_len < 0) {
2603 return -1;
2604 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002605
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002607
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 return 1;
2609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2612 int client) {
2613 uint8_t *sigalgs, *sptr;
2614 int rhash, rsign;
2615 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 if (salglen & 1) {
2618 return 0;
2619 }
Adam Langley95c29f32014-06-20 12:00:00 -07002620
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 sigalgs = OPENSSL_malloc(salglen);
2622 if (sigalgs == NULL) {
2623 return 0;
2624 }
Adam Langley95c29f32014-06-20 12:00:00 -07002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2627 rhash = tls12_find_id(*psig_nids++, tls12_md,
2628 sizeof(tls12_md) / sizeof(tls12_lookup));
2629 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2630 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002631
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 if (rhash == -1 || rsign == -1) {
2633 goto err;
2634 }
2635 *sptr++ = rhash;
2636 *sptr++ = rsign;
2637 }
2638
2639 if (client) {
2640 if (c->client_sigalgs) {
2641 OPENSSL_free(c->client_sigalgs);
2642 }
2643 c->client_sigalgs = sigalgs;
2644 c->client_sigalgslen = salglen;
2645 } else {
2646 if (c->conf_sigalgs) {
2647 OPENSSL_free(c->conf_sigalgs);
2648 }
2649 c->conf_sigalgs = sigalgs;
2650 c->conf_sigalgslen = salglen;
2651 }
2652
2653 return 1;
2654
2655err:
2656 OPENSSL_free(sigalgs);
2657 return 0;
2658}