blob: 63bba599245f0a7032124d0b0195e3bc819a2678 [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>
David Benjamine3aa1d92015-06-16 15:34:50 -0400110#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400112#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114
David Benjamin03973092014-06-24 23:27:17 -0400115#include <openssl/bytestring.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400116#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700117#include <openssl/evp.h>
118#include <openssl/hmac.h>
119#include <openssl/mem.h>
120#include <openssl/obj.h>
121#include <openssl/rand.h>
122
David Benjamin2ee94aa2015-04-07 22:38:30 -0400123#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800124
125
David Benjamin6c7aed02014-08-27 16:42:38 -0400126static int ssl_check_clienthello_tlsext(SSL *s);
127static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjamin338fcaf2014-12-11 01:20:52 -0500129const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500130 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800131 tls1_setup_key_block,
132 tls1_generate_master_secret,
133 tls1_change_cipher_state,
134 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800141};
Adam Langley95c29f32014-06-20 12:00:00 -0700142
David Benjamin338fcaf2014-12-11 01:20:52 -0500143const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500144 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800145 tls1_setup_key_block,
146 tls1_generate_master_secret,
147 tls1_change_cipher_state,
148 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_cert_verify_mac,
150 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
151 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
152 tls1_alert_code,
153 tls1_export_keying_material,
154 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800155};
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamin338fcaf2014-12-11 01:20:52 -0500157const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500158 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 tls1_setup_key_block,
160 tls1_generate_master_secret,
161 tls1_change_cipher_state,
162 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 tls1_cert_verify_mac,
164 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
165 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
166 tls1_alert_code,
167 tls1_export_keying_material,
168 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
169 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800170};
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172static int compare_uint16_t(const void *p1, const void *p2) {
173 uint16_t u1 = *((const uint16_t *)p1);
174 uint16_t u2 = *((const uint16_t *)p2);
175 if (u1 < u2) {
176 return -1;
177 } else if (u1 > u2) {
178 return 1;
179 } else {
180 return 0;
181 }
182}
David Benjamin35a7a442014-07-05 00:23:20 -0400183
Adam Langleyfcf25832014-12-18 17:42:32 -0800184/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
185 * more than one extension of the same type in a ClientHello or ServerHello.
186 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400187 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800188static int tls1_check_duplicate_extensions(const CBS *cbs) {
189 CBS extensions = *cbs;
190 size_t num_extensions = 0, i = 0;
191 uint16_t *extension_types = NULL;
192 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400193
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 /* First pass: count the extensions. */
195 while (CBS_len(&extensions) > 0) {
196 uint16_t type;
197 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 if (!CBS_get_u16(&extensions, &type) ||
200 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
201 goto done;
202 }
David Benjamin35a7a442014-07-05 00:23:20 -0400203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 num_extensions++;
205 }
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 if (num_extensions == 0) {
208 return 1;
209 }
David Benjamin9a373592014-07-25 04:27:53 -0400210
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 extension_types =
212 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
213 if (extension_types == NULL) {
214 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
215 ERR_R_MALLOC_FAILURE);
216 goto done;
217 }
David Benjamin35a7a442014-07-05 00:23:20 -0400218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 /* Second pass: gather the extension types. */
220 extensions = *cbs;
221 for (i = 0; i < num_extensions; i++) {
222 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
225 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
226 /* This should not happen. */
227 goto done;
228 }
229 }
230 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400231
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 /* Sort the extensions and make sure there are no duplicates. */
233 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
234 for (i = 1; i < num_extensions; i++) {
235 if (extension_types[i - 1] == extension_types[i]) {
236 goto done;
237 }
238 }
David Benjamin35a7a442014-07-05 00:23:20 -0400239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 ret = 1;
241
David Benjamin35a7a442014-07-05 00:23:20 -0400242done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400243 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 return ret;
245}
David Benjamin35a7a442014-07-05 00:23:20 -0400246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
248 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 if (/* Skip client version. */
253 !CBS_skip(&client_hello, 2) ||
254 /* Skip client nonce. */
255 !CBS_skip(&client_hello, 32) ||
256 /* Extract session_id. */
257 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
258 return 0;
259 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 ctx->session_id = CBS_data(&session_id);
262 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 /* Skip past DTLS cookie */
265 if (SSL_IS_DTLS(ctx->ssl)) {
266 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
269 return 0;
270 }
271 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 /* Extract cipher_suites. */
274 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
275 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
276 return 0;
277 }
278 ctx->cipher_suites = CBS_data(&cipher_suites);
279 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700280
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 /* Extract compression_methods. */
282 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
283 CBS_len(&compression_methods) < 1) {
284 return 0;
285 }
286 ctx->compression_methods = CBS_data(&compression_methods);
287 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 /* If the ClientHello ends here then it's valid, but doesn't have any
290 * extensions. (E.g. SSLv3.) */
291 if (CBS_len(&client_hello) == 0) {
292 ctx->extensions = NULL;
293 ctx->extensions_len = 0;
294 return 1;
295 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700296
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 /* Extract extensions and check it is valid. */
298 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
299 !tls1_check_duplicate_extensions(&extensions) ||
300 CBS_len(&client_hello) != 0) {
301 return 0;
302 }
303 ctx->extensions = CBS_data(&extensions);
304 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700305
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 return 1;
307}
Adam Langleydc9b1412014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309char SSL_early_callback_ctx_extension_get(
310 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
311 const uint8_t **out_data, size_t *out_len) {
312 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 while (CBS_len(&extensions) != 0) {
317 uint16_t type;
318 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 /* Decode the next extension. */
321 if (!CBS_get_u16(&extensions, &type) ||
322 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
323 return 0;
324 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700325
Adam Langleyfcf25832014-12-18 17:42:32 -0800326 if (type == extension_type) {
327 *out_data = CBS_data(&extension);
328 *out_len = CBS_len(&extension);
329 return 1;
330 }
331 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 return 0;
334}
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin52e5bac2014-12-27 02:27:35 -0500336struct tls_curve {
337 uint16_t curve_id;
338 int nid;
339};
340
David Benjamin70bd80a2014-12-27 03:06:46 -0500341/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500342static const struct tls_curve tls_curves[] = {
343 {21, NID_secp224r1},
344 {23, NID_X9_62_prime256v1},
345 {24, NID_secp384r1},
346 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800347};
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langleyfcf25832014-12-18 17:42:32 -0800349static const uint8_t ecformats_default[] = {
350 TLSEXT_ECPOINTFORMAT_uncompressed,
351};
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400354 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500355 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800356};
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langleyfcf25832014-12-18 17:42:32 -0800358int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500359 size_t i;
360 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
361 if (curve_id == tls_curves[i].curve_id) {
362 return tls_curves[i].nid;
363 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500365 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366}
Adam Langley95c29f32014-06-20 12:00:00 -0700367
David Benjamin70bd80a2014-12-27 03:06:46 -0500368int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500370 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
371 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500372 *out_curve_id = tls_curves[i].curve_id;
373 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 }
375 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 0;
377}
378
379/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
380 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
381 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400382static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 const uint16_t **out_curve_ids,
384 size_t *out_curve_ids_len) {
385 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400386 /* Only clients send a curve list, so this function is only called
387 * on the server. */
388 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
390 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
391 return;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 *out_curve_ids = s->tlsext_ellipticcurvelist;
395 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
396 if (!*out_curve_ids) {
397 *out_curve_ids = eccurves_default;
398 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
399 }
400}
David Benjamined439582014-07-14 19:13:02 -0400401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
403 uint8_t curve_type;
404 uint16_t curve_id;
405 const uint16_t *curves;
406 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 /* Only support named curves. */
409 if (!CBS_get_u8(cbs, &curve_type) ||
410 curve_type != NAMED_CURVE_TYPE ||
411 !CBS_get_u16(cbs, &curve_id)) {
412 return 0;
413 }
David Benjamined439582014-07-14 19:13:02 -0400414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 tls1_get_curvelist(s, 0, &curves, &curves_len);
416 for (i = 0; i < curves_len; i++) {
417 if (curve_id == curves[i]) {
418 *out_curve_id = curve_id;
419 return 1;
420 }
421 }
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 return 0;
424}
David Benjamin072334d2014-07-13 16:24:27 -0400425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400427 const uint16_t *curves, *peer_curves, *pref, *supp;
428 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 /* Can't do anything on client side */
431 if (s->server == 0) {
432 return NID_undef;
433 }
434
David Benjamin55a43642015-04-20 14:45:55 -0400435 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
436 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800437
David Benjamin55a43642015-04-20 14:45:55 -0400438 if (peer_curves_len == 0) {
439 /* Clients are not required to send a supported_curves extension. In this
440 * case, the server is free to pick any curve it likes. See RFC 4492,
441 * section 4, paragraph 3. */
442 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
443 }
444
445 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
446 pref = curves;
447 pref_len = curves_len;
448 supp = peer_curves;
449 supp_len = peer_curves_len;
450 } else {
451 pref = peer_curves;
452 pref_len = peer_curves_len;
453 supp = curves;
454 supp_len = curves_len;
455 }
456
457 for (i = 0; i < pref_len; i++) {
458 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 if (pref[i] == supp[j]) {
460 return tls1_ec_curve_id2nid(pref[i]);
461 }
462 }
463 }
464
465 return NID_undef;
466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
David Benjamin072334d2014-07-13 16:24:27 -0400468int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 const int *curves, size_t ncurves) {
470 uint16_t *curve_ids;
471 size_t i;
472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
474 if (curve_ids == NULL) {
475 return 0;
476 }
477
478 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500479 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 OPENSSL_free(curve_ids);
481 return 0;
482 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 }
484
David Benjamin2755a3e2015-04-22 16:17:58 -0400485 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 *out_curve_ids = curve_ids;
487 *out_curve_ids_len = ncurves;
488
489 return 1;
490}
Adam Langley95c29f32014-06-20 12:00:00 -0700491
David Benjamin072334d2014-07-13 16:24:27 -0400492/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
493 * TLS curve ID and point format, respectively, for |ec|. It returns one on
494 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800495static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
496 uint8_t *out_comp_id, EC_KEY *ec) {
497 int nid;
498 uint16_t id;
499 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 if (ec == NULL) {
502 return 0;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 grp = EC_KEY_get0_group(ec);
506 if (grp == NULL) {
507 return 0;
508 }
David Benjamin072334d2014-07-13 16:24:27 -0400509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 /* Determine curve ID */
511 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500512 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 return 0;
514 }
David Benjamin072334d2014-07-13 16:24:27 -0400515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
517 *out_curve_id = id;
518
519 if (out_comp_id) {
520 if (EC_KEY_get0_public_key(ec) == NULL) {
521 return 0;
522 }
523 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
524 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
525 } else {
526 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
527 }
528 }
529
530 return 1;
531}
David Benjamin072334d2014-07-13 16:24:27 -0400532
David Benjamin42e9a772014-09-02 23:18:44 -0400533/* tls1_check_point_format returns one if |comp_id| is consistent with the
534 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800535static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
536 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
537 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
538 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 /* If point formats extension present check it, otherwise everything is
541 * supported (see RFC4492). */
542 if (p == NULL) {
543 return 1;
544 }
David Benjamin42e9a772014-09-02 23:18:44 -0400545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 for (i = 0; i < plen; i++) {
547 if (comp_id == p[i]) {
548 return 1;
549 }
550 }
David Benjamin42e9a772014-09-02 23:18:44 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 return 0;
553}
554
555/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
556 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400557 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800558static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
559 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400560 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400563 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
564 if (get_peer_curves && !s->server) {
565 /* Servers do not present a preference list so, if we are a client, only
566 * check our list. */
567 continue;
568 }
569
570 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
571 if (get_peer_curves && curves_len == 0) {
572 /* Clients are not required to send a supported_curves extension. In this
573 * case, the server is free to pick any curve it likes. See RFC 4492,
574 * section 4, paragraph 3. */
575 continue;
576 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 for (i = 0; i < curves_len; i++) {
578 if (curves[i] == curve_id) {
579 break;
580 }
581 }
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 if (i == curves_len) {
584 return 0;
585 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 }
David Benjamin033e5f42014-11-13 18:47:41 -0500587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 return 1;
589}
David Benjamin033e5f42014-11-13 18:47:41 -0500590
Adam Langleyfcf25832014-12-18 17:42:32 -0800591static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
592 size_t *pformatslen) {
593 /* If we have a custom point format list use it otherwise use default */
594 if (s->tlsext_ecpointformatlist) {
595 *pformats = s->tlsext_ecpointformatlist;
596 *pformatslen = s->tlsext_ecpointformatlist_length;
597 } else {
598 *pformats = ecformats_default;
599 *pformatslen = sizeof(ecformats_default);
600 }
601}
602
603int tls1_check_ec_cert(SSL *s, X509 *x) {
604 int ret = 0;
605 EVP_PKEY *pkey = X509_get_pubkey(x);
606 uint16_t curve_id;
607 uint8_t comp_id;
608
609 if (!pkey ||
610 pkey->type != EVP_PKEY_EC ||
611 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
612 !tls1_check_curve_id(s, curve_id) ||
613 !tls1_check_point_format(s, comp_id)) {
614 goto done;
615 }
616
617 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500618
619done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400620 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 return ret;
622}
David Benjamin42e9a772014-09-02 23:18:44 -0400623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400625 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400626 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400627 * the curve. */
628 uint16_t curve_id;
629 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
630 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 }
632
David Benjamindd978782015-04-24 15:20:13 -0400633 if (s->cert->ecdh_tmp_cb != NULL) {
634 /* Assume the callback will provide an acceptable curve. */
635 return 1;
636 }
637
638 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
639 * there is a shared curve. */
640 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800641}
Adam Langley95c29f32014-06-20 12:00:00 -0700642
643/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langley95c29f32014-06-20 12:00:00 -0700646#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langley95c29f32014-06-20 12:00:00 -0700648#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700651
David Benjamincff64722014-08-19 19:54:46 -0400652static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 tlsext_sigalg(TLSEXT_hash_sha512)
654 tlsext_sigalg(TLSEXT_hash_sha384)
655 tlsext_sigalg(TLSEXT_hash_sha256)
656 tlsext_sigalg(TLSEXT_hash_sha224)
657 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700658};
David Benjamin05da6e12014-07-12 20:42:55 -0400659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
661 /* If server use client authentication sigalgs if not NULL */
662 if (s->server && s->cert->client_sigalgs) {
663 *psigs = s->cert->client_sigalgs;
664 return s->cert->client_sigalgslen;
665 } else if (s->cert->conf_sigalgs) {
666 *psigs = s->cert->conf_sigalgs;
667 return s->cert->conf_sigalgslen;
668 } else {
669 *psigs = tls12_sigalgs;
670 return sizeof(tls12_sigalgs);
671 }
672}
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
675 * checks it is consistent with |s|'s sent supported signature algorithms and,
676 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
677 * returns 0 and writes an alert into |*out_alert|. */
678int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
679 CBS *cbs, EVP_PKEY *pkey) {
680 const uint8_t *sent_sigs;
681 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400682 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 uint8_t hash, signature;
684
685 /* Should never happen */
686 if (sigalg == -1) {
687 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
688 *out_alert = SSL_AD_INTERNAL_ERROR;
689 return 0;
690 }
691
692 if (!CBS_get_u8(cbs, &hash) ||
693 !CBS_get_u8(cbs, &signature)) {
694 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
695 *out_alert = SSL_AD_DECODE_ERROR;
696 return 0;
697 }
698
699 /* Check key type is consistent with signature */
700 if (sigalg != signature) {
701 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
702 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
703 return 0;
704 }
705
706 if (pkey->type == EVP_PKEY_EC) {
707 uint16_t curve_id;
708 uint8_t comp_id;
709 /* Check compression and curve matches extensions */
710 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
711 *out_alert = SSL_AD_INTERNAL_ERROR;
712 return 0;
713 }
714
715 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
716 !tls1_check_point_format(s, comp_id))) {
717 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
718 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
719 return 0;
720 }
721 }
722
723 /* Check signature matches a type we sent */
724 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
725 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
726 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
727 break;
728 }
729 }
730
731 /* Allow fallback to SHA-1. */
732 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
733 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
734 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
735 return 0;
736 }
737
738 *out_md = tls12_get_hash(hash);
739 if (*out_md == NULL) {
740 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
741 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
742 return 0;
743 }
744
745 return 1;
746}
747
748/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
749 * supported or doesn't appear in supported signature algorithms. Unlike
750 * ssl_cipher_get_disabled this applies to a specific session and not global
751 * settings. */
752void ssl_set_client_disabled(SSL *s) {
753 CERT *c = s->cert;
754 const uint8_t *sigalgs;
755 size_t i, sigalgslen;
756 int have_rsa = 0, have_ecdsa = 0;
757 c->mask_a = 0;
758 c->mask_k = 0;
759
760 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
761 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
762 c->mask_ssl = SSL_TLSV1_2;
763 } else {
764 c->mask_ssl = 0;
765 }
766
767 /* Now go through all signature algorithms seeing if we support any for RSA,
768 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
769 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
770 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
771 switch (sigalgs[1]) {
772 case TLSEXT_signature_rsa:
773 have_rsa = 1;
774 break;
775
776 case TLSEXT_signature_ecdsa:
777 have_ecdsa = 1;
778 break;
779 }
780 }
781
782 /* Disable auth if we don't include any appropriate signature algorithms. */
783 if (!have_rsa) {
784 c->mask_a |= SSL_aRSA;
785 }
786 if (!have_ecdsa) {
787 c->mask_a |= SSL_aECDSA;
788 }
789
790 /* with PSK there must be client callback set */
791 if (!s->psk_client_callback) {
792 c->mask_a |= SSL_aPSK;
793 c->mask_k |= SSL_kPSK;
794 }
795}
Adam Langley95c29f32014-06-20 12:00:00 -0700796
Adam Langley614c66a2015-06-12 15:26:58 -0700797/* tls_extension represents a TLS extension that is handled internally. The
798 * |init| function is called for each handshake, before any other functions of
799 * the extension. Then the add and parse callbacks are called as needed.
800 *
801 * The parse callbacks receive a |CBS| that contains the contents of the
802 * extension (i.e. not including the type and length bytes). If an extension is
803 * not received then the parse callbacks will be called with a NULL CBS so that
804 * they can do any processing needed to handle the absence of an extension.
805 *
806 * The add callbacks receive a |CBB| to which the extension can be appended but
807 * the function is responsible for appending the type and length bytes too.
808 *
809 * All callbacks return one for success and zero for error. If a parse function
810 * returns zero then a fatal alert with value |*out_alert| will be sent. If
811 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
812struct tls_extension {
813 uint16_t value;
814 void (*init)(SSL *ssl);
815
816 int (*add_clienthello)(SSL *ssl, CBB *out);
817 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
818
819 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
820 int (*add_serverhello)(SSL *ssl, CBB *out);
821};
822
823
824/* Server name indication (SNI).
825 *
826 * https://tools.ietf.org/html/rfc6066#section-3. */
827
828static void ext_sni_init(SSL *ssl) {
829 ssl->s3->tmp.should_ack_sni = 0;
830}
831
832static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
833 if (ssl->tlsext_hostname == NULL) {
834 return 1;
835 }
836
837 CBB contents, server_name_list, name;
838 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
839 !CBB_add_u16_length_prefixed(out, &contents) ||
840 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
841 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
842 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
843 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
844 strlen(ssl->tlsext_hostname)) ||
845 !CBB_flush(out)) {
846 return 0;
847 }
848
849 return 1;
850}
851
852static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
853 if (contents == NULL) {
854 return 1;
855 }
856
857 if (CBS_len(contents) != 0) {
858 return 0;
859 }
860
861 assert(ssl->tlsext_hostname != NULL);
862
863 if (!ssl->hit) {
864 assert(ssl->session->tlsext_hostname == NULL);
865 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
866 if (!ssl->session->tlsext_hostname) {
867 *out_alert = SSL_AD_INTERNAL_ERROR;
868 return 0;
869 }
870 }
871
872 return 1;
873}
874
875static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
876 if (contents == NULL) {
877 return 1;
878 }
879
880 /* The servername extension is treated as follows:
881 *
882 * - Only the hostname type is supported with a maximum length of 255.
883 * - The servername is rejected if too long or if it contains zeros, in
884 * which case an fatal alert is generated.
885 * - The servername field is maintained together with the session cache.
886 * - When a session is resumed, the servername callback is invoked in order
887 * to allow the application to position itself to the right context.
888 * - The servername is acknowledged if it is new for a session or when
889 * it is identical to a previously used for the same session.
890 * Applications can control the behaviour. They can at any time
891 * set a 'desirable' servername for a new SSL object. This can be the
892 * case for example with HTTPS when a Host: header field is received and
893 * a renegotiation is requested. In this case, a possible servername
894 * presented in the new client hello is only acknowledged if it matches
895 * the value of the Host: field.
896 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
897 * if they provide for changing an explicit servername context for the
898 * session,
899 * i.e. when the session has been established with a servername extension.
900 */
901
902 CBS server_name_list;
903 char have_seen_host_name = 0;
904
905 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
906 CBS_len(&server_name_list) == 0 ||
907 CBS_len(contents) != 0) {
908 return 0;
909 }
910
911 /* Decode each ServerName in the extension. */
912 while (CBS_len(&server_name_list) > 0) {
913 uint8_t name_type;
914 CBS host_name;
915
916 if (!CBS_get_u8(&server_name_list, &name_type) ||
917 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
918 return 0;
919 }
920
921 /* Only host_name is supported. */
922 if (name_type != TLSEXT_NAMETYPE_host_name) {
923 continue;
924 }
925
926 if (have_seen_host_name) {
927 /* The ServerNameList MUST NOT contain more than one name of the same
928 * name_type. */
929 return 0;
930 }
931
932 have_seen_host_name = 1;
933
934 if (CBS_len(&host_name) == 0 ||
935 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
936 CBS_contains_zero_byte(&host_name)) {
937 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
938 return 0;
939 }
940
941 if (!ssl->hit) {
942 assert(ssl->session->tlsext_hostname == NULL);
943 if (ssl->session->tlsext_hostname) {
944 /* This should be impossible. */
945 return 0;
946 }
947
948 /* Copy the hostname as a string. */
949 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
950 *out_alert = SSL_AD_INTERNAL_ERROR;
951 return 0;
952 }
953
954 ssl->s3->tmp.should_ack_sni = 1;
955 }
956 }
957
958 return 1;
959}
960
961static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
962 if (ssl->hit ||
963 !ssl->s3->tmp.should_ack_sni ||
964 ssl->session->tlsext_hostname == NULL) {
965 return 1;
966 }
967
968 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
969 !CBB_add_u16(out, 0 /* length */)) {
970 return 0;
971 }
972
973 return 1;
974}
975
976
Adam Langley5021b222015-06-12 18:27:58 -0700977/* Renegotiation indication.
978 *
979 * https://tools.ietf.org/html/rfc5746 */
980
981static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
982 CBB contents, prev_finished;
983 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
984 !CBB_add_u16_length_prefixed(out, &contents) ||
985 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
986 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
987 ssl->s3->previous_client_finished_len) ||
988 !CBB_flush(out)) {
989 return 0;
990 }
991
992 return 1;
993}
994
995static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
996 CBS *contents) {
997 if (contents == NULL) {
998 /* No renegotiation extension received.
999 *
1000 * Strictly speaking if we want to avoid an attack we should *always* see
1001 * RI even on initial ServerHello because the client doesn't see any
1002 * renegotiation during an attack. However this would mean we could not
1003 * connect to any server which doesn't support RI.
1004 *
1005 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
1006 * defined. */
1007 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
1008 return 1;
1009 }
1010
1011 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1012 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello,
1013 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1014 return 0;
1015 }
1016
1017 const size_t expected_len = ssl->s3->previous_client_finished_len +
1018 ssl->s3->previous_server_finished_len;
1019
1020 /* Check for logic errors */
1021 assert(!expected_len || ssl->s3->previous_client_finished_len);
1022 assert(!expected_len || ssl->s3->previous_server_finished_len);
1023
1024 /* Parse out the extension contents. */
1025 CBS renegotiated_connection;
1026 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1027 CBS_len(contents) != 0) {
1028 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello,
1029 SSL_R_RENEGOTIATION_ENCODING_ERR);
1030 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1031 return 0;
1032 }
1033
1034 /* Check that the extension matches. */
1035 if (CBS_len(&renegotiated_connection) != expected_len) {
1036 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello,
1037 SSL_R_RENEGOTIATION_MISMATCH);
1038 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1039 return 0;
1040 }
1041
1042 const uint8_t *d = CBS_data(&renegotiated_connection);
1043 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1044 ssl->s3->previous_client_finished_len)) {
1045 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello,
1046 SSL_R_RENEGOTIATION_MISMATCH);
1047 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1048 return 0;
1049 }
1050 d += ssl->s3->previous_client_finished_len;
1051
1052 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1053 ssl->s3->previous_server_finished_len)) {
1054 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello,
1055 SSL_R_RENEGOTIATION_MISMATCH);
1056 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1057 return 0;
1058 }
1059 ssl->s3->send_connection_binding = 1;
1060
1061 return 1;
1062}
1063
1064static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1065 CBS *contents) {
1066 /* Renegotiation isn't supported as a server so this function should never be
1067 * called after the initial handshake. */
1068 assert(!ssl->s3->initial_handshake_complete);
1069
1070 CBS fake_contents;
1071 static const uint8_t kFakeExtension[] = {0};
1072
1073 if (contents == NULL) {
1074 if (ssl->s3->send_connection_binding) {
1075 /* The renegotiation SCSV was received so pretend that we received a
1076 * renegotiation extension. */
1077 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1078 contents = &fake_contents;
1079 /* We require that the renegotiation extension is at index zero of
1080 * kExtensions. */
1081 ssl->s3->tmp.extensions.received |= (1u << 0);
1082 } else {
1083 return 1;
1084 }
1085 }
1086
1087 CBS renegotiated_connection;
1088
1089 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1090 CBS_len(contents) != 0) {
1091 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_clienthello,
1092 SSL_R_RENEGOTIATION_ENCODING_ERR);
1093 return 0;
1094 }
1095
1096 /* Check that the extension matches */
1097 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1098 ssl->s3->previous_client_finished_len)) {
1099 OPENSSL_PUT_ERROR(SSL, ext_ri_parse_clienthello,
1100 SSL_R_RENEGOTIATION_MISMATCH);
1101 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1102 return 0;
1103 }
1104
1105 ssl->s3->send_connection_binding = 1;
1106
1107 return 1;
1108}
1109
1110static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1111 CBB contents, prev_finished;
1112 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1113 !CBB_add_u16_length_prefixed(out, &contents) ||
1114 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1115 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1116 ssl->s3->previous_client_finished_len) ||
1117 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1118 ssl->s3->previous_server_finished_len) ||
1119 !CBB_flush(out)) {
1120 return 0;
1121 }
1122
1123 return 1;
1124}
1125
Adam Langley0a056712015-07-01 15:03:33 -07001126
1127/* Extended Master Secret.
1128 *
1129 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1130
1131static void ext_ems_init(SSL *ssl) {
1132 ssl->s3->tmp.extended_master_secret = 0;
1133}
1134
1135static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1136 if (ssl->version == SSL3_VERSION) {
1137 return 1;
1138 }
1139
1140 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1141 !CBB_add_u16(out, 0 /* length */)) {
1142 return 0;
1143 }
1144
1145 return 1;
1146}
1147
1148static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1149 CBS *contents) {
1150 if (contents == NULL) {
1151 return 1;
1152 }
1153
1154 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1155 return 0;
1156 }
1157
1158 ssl->s3->tmp.extended_master_secret = 1;
1159 return 1;
1160}
1161
1162static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1163 if (ssl->version == SSL3_VERSION || contents == NULL) {
1164 return 1;
1165 }
1166
1167 if (CBS_len(contents) != 0) {
1168 return 0;
1169 }
1170
1171 ssl->s3->tmp.extended_master_secret = 1;
1172 return 1;
1173}
1174
1175static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1176 if (!ssl->s3->tmp.extended_master_secret) {
1177 return 1;
1178 }
1179
1180 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1181 !CBB_add_u16(out, 0 /* length */)) {
1182 return 0;
1183 }
1184
1185 return 1;
1186}
1187
Adam Langley614c66a2015-06-12 15:26:58 -07001188/* kExtensions contains all the supported extensions. */
1189static const struct tls_extension kExtensions[] = {
1190 {
Adam Langley5021b222015-06-12 18:27:58 -07001191 /* The renegotiation extension must always be at index zero because the
1192 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1193 * sent as an SCSV. */
1194 TLSEXT_TYPE_renegotiate,
1195 NULL,
1196 ext_ri_add_clienthello,
1197 ext_ri_parse_serverhello,
1198 ext_ri_parse_clienthello,
1199 ext_ri_add_serverhello,
1200 },
1201 {
Adam Langley614c66a2015-06-12 15:26:58 -07001202 TLSEXT_TYPE_server_name,
1203 ext_sni_init,
1204 ext_sni_add_clienthello,
1205 ext_sni_parse_serverhello,
1206 ext_sni_parse_clienthello,
1207 ext_sni_add_serverhello,
1208 },
Adam Langley0a056712015-07-01 15:03:33 -07001209 {
1210 TLSEXT_TYPE_extended_master_secret,
1211 ext_ems_init,
1212 ext_ems_add_clienthello,
1213 ext_ems_parse_serverhello,
1214 ext_ems_parse_clienthello,
1215 ext_ems_add_serverhello,
1216 },
Adam Langley614c66a2015-06-12 15:26:58 -07001217};
1218
1219#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
1220
Adam Langley4cfa96b2015-07-01 11:56:55 -07001221OPENSSL_COMPILE_ASSERT(kNumExtensions <=
1222 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
1223 too_many_extensions_for_bitset);
1224OPENSSL_COMPILE_ASSERT(kNumExtensions <=
1225 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
1226 8,
1227 too_many_extensions_for_bitset);
1228
Adam Langley614c66a2015-06-12 15:26:58 -07001229static const struct tls_extension *tls_extension_find(uint32_t *out_index,
1230 uint16_t value) {
1231 unsigned i;
1232 for (i = 0; i < kNumExtensions; i++) {
1233 if (kExtensions[i].value == value) {
1234 *out_index = i;
1235 return &kExtensions[i];
1236 }
1237 }
1238
1239 return NULL;
1240}
1241
Adam Langleyb0c235e2014-06-20 12:00:00 -07001242/* header_len is the length of the ClientHello header written so far, used to
1243 * compute padding. It does not include the record header. Pass 0 if no padding
1244 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07001245uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
1246 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 int extdatalen = 0;
1248 uint8_t *ret = buf;
1249 uint8_t *orig = buf;
1250 /* See if we support any ECC ciphersuites */
1251 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001252
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1254 size_t i;
David Benjamin107db582015-04-08 00:41:59 -04001255 uint32_t alg_k, alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001257
Adam Langleyfcf25832014-12-18 17:42:32 -08001258 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1259 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 alg_k = c->algorithm_mkey;
1262 alg_a = c->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001263 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 using_ecc = 1;
1265 break;
1266 }
1267 }
1268 }
Adam Langley95c29f32014-06-20 12:00:00 -07001269
Adam Langleyfcf25832014-12-18 17:42:32 -08001270 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1271 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
1272 return orig;
1273 }
Adam Langley95c29f32014-06-20 12:00:00 -07001274
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001276
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 if (ret >= limit) {
1278 return NULL; /* should never occur. */
1279 }
Adam Langley95c29f32014-06-20 12:00:00 -07001280
Adam Langley614c66a2015-06-12 15:26:58 -07001281 s->s3->tmp.extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001282
Adam Langley614c66a2015-06-12 15:26:58 -07001283 size_t i;
1284 for (i = 0; i < kNumExtensions; i++) {
1285 if (kExtensions[i].init != NULL) {
1286 kExtensions[i].init(s);
1287 }
1288 }
Adam Langley95c29f32014-06-20 12:00:00 -07001289
Adam Langley614c66a2015-06-12 15:26:58 -07001290 CBB cbb;
1291 if (!CBB_init_fixed(&cbb, ret, limit - ret)) {
1292 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1293 return NULL;
1294 }
1295
1296 for (i = 0; i < kNumExtensions; i++) {
David Benjamind822ed82015-07-09 01:06:51 -04001297 const size_t len_before = CBB_len(&cbb);
Adam Langley614c66a2015-06-12 15:26:58 -07001298 if (!kExtensions[i].add_clienthello(s, &cbb)) {
1299 CBB_cleanup(&cbb);
1300 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 return NULL;
1302 }
David Benjamind822ed82015-07-09 01:06:51 -04001303 const size_t len_after = CBB_len(&cbb);
Adam Langley95c29f32014-06-20 12:00:00 -07001304
David Benjamind822ed82015-07-09 01:06:51 -04001305 if (len_after != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07001306 s->s3->tmp.extensions.sent |= (1u << i);
1307 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001308 }
Adam Langley75712922014-10-10 16:23:43 -07001309
David Benjamind822ed82015-07-09 01:06:51 -04001310 ret += CBB_len(&cbb);
Adam Langley614c66a2015-06-12 15:26:58 -07001311 CBB_cleanup(&cbb);
1312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1314 int ticklen = 0;
David Benjamin4b27d9f2015-05-12 22:42:52 -04001315 /* Renegotiation does not participate in session resumption. However, still
1316 * advertise the extension to avoid potentially breaking servers which carry
1317 * over the state from the previous handshake, such as OpenSSL servers
1318 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1319 if (!s->s3->initial_handshake_complete && s->session != NULL &&
1320 s->session->tlsext_tick != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 ticklen = s->session->tlsext_ticklen;
1322 }
Adam Langley95c29f32014-06-20 12:00:00 -07001323
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 /* Check for enough room 2 for extension type, 2 for len rest for
1325 * ticket. */
1326 if ((long)(limit - ret - 4 - ticklen) < 0) {
1327 return NULL;
1328 }
1329 s2n(TLSEXT_TYPE_session_ticket, ret);
1330 s2n(ticklen, ret);
1331 if (ticklen) {
1332 memcpy(ret, s->session->tlsext_tick, ticklen);
1333 ret += ticklen;
1334 }
1335 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001336
David Benjamin6ae7f072015-01-26 10:22:13 -05001337 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 size_t salglen;
1339 const uint8_t *salg;
1340 salglen = tls12_get_psigalgs(s, &salg);
1341 if ((size_t)(limit - ret) < salglen + 6) {
1342 return NULL;
1343 }
1344 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1345 s2n(salglen + 2, ret);
1346 s2n(salglen, ret);
1347 memcpy(ret, salg, salglen);
1348 ret += salglen;
1349 }
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 if (s->ocsp_stapling_enabled) {
1352 /* The status_request extension is excessively extensible at every layer.
1353 * On the client, only support requesting OCSP responses with an empty
1354 * responder_id_list and no extensions. */
1355 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
1356 return NULL;
1357 }
Adam Langley95c29f32014-06-20 12:00:00 -07001358
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 s2n(TLSEXT_TYPE_status_request, ret);
1360 s2n(1 + 2 + 2, ret);
1361 /* status_type */
1362 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1363 /* responder_id_list - empty */
1364 s2n(0, ret);
1365 /* request_extensions - empty */
1366 s2n(0, ret);
1367 }
Adam Langley95c29f32014-06-20 12:00:00 -07001368
David Benjamine6df0542015-05-12 22:02:08 -04001369 if (s->ctx->next_proto_select_cb && !s->s3->initial_handshake_complete &&
David Benjamin78e69782014-12-19 04:52:17 -05001370 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 /* The client advertises an emtpy extension to indicate its support for
1372 * Next Protocol Negotiation */
1373 if (limit - ret - 4 < 0) {
1374 return NULL;
1375 }
1376 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1377 s2n(0, ret);
1378 }
Adam Langley95c29f32014-06-20 12:00:00 -07001379
David Benjamine6df0542015-05-12 22:02:08 -04001380 if (s->signed_cert_timestamps_enabled) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 /* The client advertises an empty extension to indicate its support for
1382 * certificate timestamps. */
1383 if (limit - ret - 4 < 0) {
1384 return NULL;
1385 }
1386 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
1387 s2n(0, ret);
1388 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001389
David Benjamine6df0542015-05-12 22:02:08 -04001390 if (s->alpn_client_proto_list && !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
1392 return NULL;
1393 }
1394 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1395 s2n(2 + s->alpn_client_proto_list_len, ret);
1396 s2n(s->alpn_client_proto_list_len, ret);
1397 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1398 ret += s->alpn_client_proto_list_len;
1399 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001400
David Benjamin78e69782014-12-19 04:52:17 -05001401 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 /* The client advertises an emtpy extension to indicate its support for
1403 * Channel ID. */
1404 if (limit - ret - 4 < 0) {
1405 return NULL;
1406 }
1407 if (s->ctx->tlsext_channel_id_enabled_new) {
1408 s2n(TLSEXT_TYPE_channel_id_new, ret);
1409 } else {
1410 s2n(TLSEXT_TYPE_channel_id, ret);
1411 }
1412 s2n(0, ret);
1413 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001414
Adam Langleyfcf25832014-12-18 17:42:32 -08001415 if (SSL_get_srtp_profiles(s)) {
1416 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -07001417
Adam Langleyfcf25832014-12-18 17:42:32 -08001418 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -07001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 if ((limit - ret - 4 - el) < 0) {
1421 return NULL;
1422 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001423
Adam Langleyfcf25832014-12-18 17:42:32 -08001424 s2n(TLSEXT_TYPE_use_srtp, ret);
1425 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001426
Adam Langleyfcf25832014-12-18 17:42:32 -08001427 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1428 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1429 return NULL;
1430 }
1431 ret += el;
1432 }
Adam Langley95c29f32014-06-20 12:00:00 -07001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 if (using_ecc) {
1435 /* Add TLS extension ECPointFormats to the ClientHello message */
1436 long lenmax;
1437 const uint8_t *formats;
1438 const uint16_t *curves;
Adam Langley614c66a2015-06-12 15:26:58 -07001439 size_t formats_len, curves_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 lenmax = limit - ret - 5;
1444 if (lenmax < 0) {
1445 return NULL;
1446 }
1447 if (formats_len > (size_t)lenmax) {
1448 return NULL;
1449 }
1450 if (formats_len > 255) {
1451 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1452 return NULL;
1453 }
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1456 s2n(formats_len + 1, ret);
1457 *(ret++) = (uint8_t)formats_len;
1458 memcpy(ret, formats, formats_len);
1459 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001460
Adam Langleyfcf25832014-12-18 17:42:32 -08001461 /* Add TLS extension EllipticCurves to the ClientHello message */
1462 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001463
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 lenmax = limit - ret - 6;
1465 if (lenmax < 0) {
1466 return NULL;
1467 }
1468 if (curves_len * 2 > (size_t)lenmax) {
1469 return NULL;
1470 }
1471 if (curves_len * 2 > 65532) {
1472 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1473 return NULL;
1474 }
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1477 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 s2n(curves_len * 2, ret);
1480 for (i = 0; i < curves_len; i++) {
1481 s2n(curves[i], ret);
1482 }
1483 }
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 if (header_len > 0) {
1486 size_t clienthello_minsize = 0;
1487 header_len += ret - orig;
1488 if (header_len > 0xff && header_len < 0x200) {
1489 /* Add padding to workaround bugs in F5 terminators. See
1490 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1491 *
1492 * NB: because this code works out the length of all existing extensions
1493 * it MUST always appear last. */
1494 clienthello_minsize = 0x200;
1495 }
1496 if (s->fastradio_padding) {
1497 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1498 * into DCH (high data rate) state in 3G networks. Note that when
1499 * fastradio_padding is enabled, even if the header_len is less than 255
1500 * bytes, the padding will be applied regardless. This is slightly
1501 * different from the TLS padding extension suggested in
1502 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1503 clienthello_minsize = 0x400;
1504 }
1505 if (header_len < clienthello_minsize) {
1506 size_t padding_len = clienthello_minsize - header_len;
1507 /* Extensions take at least four bytes to encode. Always include least
1508 * one byte of data if including the extension. WebSphere Application
1509 * Server 7.0 is intolerant to the last extension being zero-length. */
1510 if (padding_len >= 4 + 1) {
1511 padding_len -= 4;
1512 } else {
1513 padding_len = 1;
1514 }
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 if (limit - ret - 4 - (long)padding_len < 0) {
1517 return NULL;
1518 }
Adam Langley75712922014-10-10 16:23:43 -07001519
Adam Langleyfcf25832014-12-18 17:42:32 -08001520 s2n(TLSEXT_TYPE_padding, ret);
1521 s2n(padding_len, ret);
1522 memset(ret, 0, padding_len);
1523 ret += padding_len;
1524 }
1525 }
Adam Langley75712922014-10-10 16:23:43 -07001526
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 extdatalen = ret - orig - 2;
1528 if (extdatalen == 0) {
1529 return orig;
1530 }
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 s2n(extdatalen, orig);
1533 return ret;
1534}
Adam Langley95c29f32014-06-20 12:00:00 -07001535
Adam Langley614c66a2015-06-12 15:26:58 -07001536uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
1537 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 int extdatalen = 0;
1539 uint8_t *orig = buf;
1540 uint8_t *ret = buf;
1541 int next_proto_neg_seen;
David Benjamin107db582015-04-08 00:41:59 -04001542 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1543 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001544 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1548 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1549 return orig;
1550 }
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 ret += 2;
1553 if (ret >= limit) {
1554 return NULL; /* should never happen. */
1555 }
Adam Langley95c29f32014-06-20 12:00:00 -07001556
Adam Langley614c66a2015-06-12 15:26:58 -07001557 CBB cbb;
1558 if (!CBB_init_fixed(&cbb, ret, limit - ret)) {
1559 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1560 return NULL;
1561 }
1562
1563 unsigned i;
1564 for (i = 0; i < kNumExtensions; i++) {
1565 if (!(s->s3->tmp.extensions.received & (1u << i))) {
1566 /* Don't send extensions that were not received. */
1567 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 }
Adam Langley95c29f32014-06-20 12:00:00 -07001569
Adam Langley614c66a2015-06-12 15:26:58 -07001570 if (!kExtensions[i].add_serverhello(s, &cbb)) {
1571 CBB_cleanup(&cbb);
1572 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1573 return NULL;
1574 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 }
Adam Langley95c29f32014-06-20 12:00:00 -07001576
David Benjamind822ed82015-07-09 01:06:51 -04001577 ret += CBB_len(&cbb);
Adam Langley614c66a2015-06-12 15:26:58 -07001578 CBB_cleanup(&cbb);
1579
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 if (using_ecc) {
1581 const uint8_t *plist;
1582 size_t plistlen;
1583 /* Add TLS extension ECPointFormats to the ServerHello message */
1584 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 tls1_get_formatlist(s, &plist, &plistlen);
1587
1588 lenmax = limit - ret - 5;
1589 if (lenmax < 0) {
1590 return NULL;
1591 }
1592 if (plistlen > (size_t)lenmax) {
1593 return NULL;
1594 }
1595 if (plistlen > 255) {
1596 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1597 return NULL;
1598 }
1599
1600 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1601 s2n(plistlen + 1, ret);
1602 *(ret++) = (uint8_t)plistlen;
1603 memcpy(ret, plist, plistlen);
1604 ret += plistlen;
1605 }
1606 /* Currently the server should not respond with a SupportedCurves extension */
1607
1608 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1609 if ((long)(limit - ret - 4) < 0) {
1610 return NULL;
1611 }
1612 s2n(TLSEXT_TYPE_session_ticket, ret);
1613 s2n(0, ret);
1614 }
1615
1616 if (s->s3->tmp.certificate_status_expected) {
1617 if ((long)(limit - ret - 4) < 0) {
1618 return NULL;
1619 }
1620 s2n(TLSEXT_TYPE_status_request, ret);
1621 s2n(0, ret);
1622 }
1623
1624 if (s->srtp_profile) {
1625 int el;
1626
1627 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1628
1629 if ((limit - ret - 4 - el) < 0) {
1630 return NULL;
1631 }
1632
1633 s2n(TLSEXT_TYPE_use_srtp, ret);
1634 s2n(el, ret);
1635
1636 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1637 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1638 return NULL;
1639 }
1640 ret += el;
1641 }
1642
1643 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1644 s->s3->next_proto_neg_seen = 0;
1645 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1646 const uint8_t *npa;
1647 unsigned int npalen;
1648 int r;
1649
1650 r = s->ctx->next_protos_advertised_cb(
1651 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1652 if (r == SSL_TLSEXT_ERR_OK) {
1653 if ((long)(limit - ret - 4 - npalen) < 0) {
1654 return NULL;
1655 }
1656 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1657 s2n(npalen, ret);
1658 memcpy(ret, npa, npalen);
1659 ret += npalen;
1660 s->s3->next_proto_neg_seen = 1;
1661 }
1662 }
1663
1664 if (s->s3->alpn_selected) {
1665 const uint8_t *selected = s->s3->alpn_selected;
1666 size_t len = s->s3->alpn_selected_len;
1667
1668 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1669 return NULL;
1670 }
1671 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1672 s2n(3 + len, ret);
1673 s2n(1 + len, ret);
1674 *ret++ = len;
1675 memcpy(ret, selected, len);
1676 ret += len;
1677 }
1678
1679 /* If the client advertised support for Channel ID, and we have it
1680 * enabled, then we want to echo it back. */
1681 if (s->s3->tlsext_channel_id_valid) {
1682 if (limit - ret - 4 < 0) {
1683 return NULL;
1684 }
1685 if (s->s3->tlsext_channel_id_new) {
1686 s2n(TLSEXT_TYPE_channel_id_new, ret);
1687 } else {
1688 s2n(TLSEXT_TYPE_channel_id, ret);
1689 }
1690 s2n(0, ret);
1691 }
1692
1693 extdatalen = ret - orig - 2;
1694 if (extdatalen == 0) {
1695 return orig;
1696 }
1697
1698 s2n(extdatalen, orig);
1699 return ret;
1700}
Adam Langley95c29f32014-06-20 12:00:00 -07001701
Adam Langley95c29f32014-06-20 12:00:00 -07001702/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1703 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001704 * cbs: the contents of the extension, not including the type and length.
1705 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001706 * return.
1707 *
David Benjamindc72ff72014-06-25 12:36:10 -04001708 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001709static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1710 CBS protocol_name_list, protocol_name_list_copy;
1711 const uint8_t *selected;
1712 uint8_t selected_len;
1713 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 if (s->ctx->alpn_select_cb == NULL) {
1716 return 1;
1717 }
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1720 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1721 goto parse_error;
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 /* Validate the protocol list. */
1725 protocol_name_list_copy = protocol_name_list;
1726 while (CBS_len(&protocol_name_list_copy) > 0) {
1727 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001728
Adam Langleyefb0e162015-07-09 11:35:04 -07001729 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1730 /* Empty protocol names are forbidden. */
1731 CBS_len(&protocol_name) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 goto parse_error;
1733 }
1734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 r = s->ctx->alpn_select_cb(
1737 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1738 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1739 if (r == SSL_TLSEXT_ERR_OK) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001740 OPENSSL_free(s->s3->alpn_selected);
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1742 if (!s->s3->alpn_selected) {
1743 *out_alert = SSL_AD_INTERNAL_ERROR;
1744 return 0;
1745 }
1746 s->s3->alpn_selected_len = selected_len;
1747 }
1748
1749 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001750
1751parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 *out_alert = SSL_AD_DECODE_ERROR;
1753 return 0;
1754}
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001758
David Benjamind1d7d3d2015-01-11 20:30:21 -05001759 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 s->s3->next_proto_neg_seen = 0;
1761 s->s3->tmp.certificate_status_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001762
David Benjamin2755a3e2015-04-22 16:17:58 -04001763 OPENSSL_free(s->s3->alpn_selected);
1764 s->s3->alpn_selected = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 /* Clear any signature algorithms extension received */
David Benjamin2755a3e2015-04-22 16:17:58 -04001767 OPENSSL_free(s->cert->peer_sigalgs);
1768 s->cert->peer_sigalgs = NULL;
1769 s->cert->peer_sigalgslen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 /* Clear any shared signature algorithms */
David Benjamin2755a3e2015-04-22 16:17:58 -04001772 OPENSSL_free(s->cert->shared_sigalgs);
1773 s->cert->shared_sigalgs = NULL;
1774 s->cert->shared_sigalgslen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 /* Clear ECC extensions */
David Benjamin2755a3e2015-04-22 16:17:58 -04001777 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1778 s->s3->tmp.peer_ecpointformatlist = NULL;
1779 s->s3->tmp.peer_ecpointformatlist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001780
David Benjamin2755a3e2015-04-22 16:17:58 -04001781 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1782 s->s3->tmp.peer_ellipticcurvelist = NULL;
1783 s->s3->tmp.peer_ellipticcurvelist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001784
Adam Langley614c66a2015-06-12 15:26:58 -07001785 size_t i;
1786 for (i = 0; i < kNumExtensions; i++) {
1787 if (kExtensions[i].init != NULL) {
1788 kExtensions[i].init(s);
1789 }
1790 }
1791
1792 s->s3->tmp.extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07001793 /* The renegotiation extension must always be at index zero because the
1794 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1795 * sent as an SCSV. */
1796 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 /* There may be no extensions. */
1799 if (CBS_len(cbs) == 0) {
Adam Langley614c66a2015-06-12 15:26:58 -07001800 goto no_extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -08001801 }
David Benjamindc72ff72014-06-25 12:36:10 -04001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 /* Decode the extensions block and check it is valid. */
1804 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1805 !tls1_check_duplicate_extensions(&extensions)) {
1806 *out_alert = SSL_AD_DECODE_ERROR;
1807 return 0;
1808 }
David Benjamindc72ff72014-06-25 12:36:10 -04001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 while (CBS_len(&extensions) != 0) {
1811 uint16_t type;
1812 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001813
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 /* Decode the next extension. */
1815 if (!CBS_get_u16(&extensions, &type) ||
1816 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1817 *out_alert = SSL_AD_DECODE_ERROR;
1818 return 0;
1819 }
Adam Langley95c29f32014-06-20 12:00:00 -07001820
Adam Langley614c66a2015-06-12 15:26:58 -07001821 unsigned ext_index;
1822 const struct tls_extension *const ext =
1823 tls_extension_find(&ext_index, type);
Adam Langley95c29f32014-06-20 12:00:00 -07001824
Adam Langley614c66a2015-06-12 15:26:58 -07001825 if (ext != NULL) {
1826 s->s3->tmp.extensions.received |= (1u << ext_index);
1827 uint8_t alert = SSL_AD_DECODE_ERROR;
1828 if (!ext->parse_clienthello(s, &alert, &extension)) {
1829 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001830 return 0;
1831 }
David Benjamindc72ff72014-06-25 12:36:10 -04001832
Adam Langley614c66a2015-06-12 15:26:58 -07001833 continue;
1834 }
David Benjamindc72ff72014-06-25 12:36:10 -04001835
Adam Langley614c66a2015-06-12 15:26:58 -07001836 if (type == TLSEXT_TYPE_ec_point_formats) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1840 CBS_len(&extension) != 0) {
1841 *out_alert = SSL_AD_DECODE_ERROR;
1842 return 0;
1843 }
Adam Langley95c29f32014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1846 &s->s3->tmp.peer_ecpointformatlist_length)) {
1847 *out_alert = SSL_AD_INTERNAL_ERROR;
1848 return 0;
1849 }
1850 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1851 CBS elliptic_curve_list;
Adam Langley614c66a2015-06-12 15:26:58 -07001852 size_t num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1855 CBS_len(&elliptic_curve_list) == 0 ||
1856 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1857 CBS_len(&extension) != 0) {
1858 *out_alert = SSL_AD_DECODE_ERROR;
1859 return 0;
1860 }
David Benjamindc72ff72014-06-25 12:36:10 -04001861
David Benjamin2755a3e2015-04-22 16:17:58 -04001862 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1863 s->s3->tmp.peer_ellipticcurvelist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001864
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 s->s3->tmp.peer_ellipticcurvelist =
1866 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001867
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1869 *out_alert = SSL_AD_INTERNAL_ERROR;
1870 return 0;
1871 }
Adam Langley95c29f32014-06-20 12:00:00 -07001872
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 num_curves = CBS_len(&elliptic_curve_list) / 2;
1874 for (i = 0; i < num_curves; i++) {
1875 if (!CBS_get_u16(&elliptic_curve_list,
1876 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1877 *out_alert = SSL_AD_INTERNAL_ERROR;
1878 return 0;
1879 }
1880 }
David Benjamindc72ff72014-06-25 12:36:10 -04001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 if (CBS_len(&elliptic_curve_list) != 0) {
1883 *out_alert = SSL_AD_INTERNAL_ERROR;
1884 return 0;
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1889 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 if (!CBS_get_u16_length_prefixed(&extension,
1892 &supported_signature_algorithms) ||
1893 CBS_len(&extension) != 0) {
1894 *out_alert = SSL_AD_DECODE_ERROR;
1895 return 0;
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 /* Ensure the signature algorithms are non-empty. It contains a list of
1899 * SignatureAndHashAlgorithms which are two bytes each. */
1900 if (CBS_len(&supported_signature_algorithms) == 0 ||
1901 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1902 *out_alert = SSL_AD_DECODE_ERROR;
1903 return 0;
1904 }
David Benjamindc72ff72014-06-25 12:36:10 -04001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1907 *out_alert = SSL_AD_DECODE_ERROR;
1908 return 0;
1909 }
1910 /* If sigalgs received and no shared algorithms fatal error. */
1911 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001912 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1914 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1915 return 0;
1916 }
1917 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamine6df0542015-05-12 22:02:08 -04001918 !s->s3->initial_handshake_complete &&
1919 s->s3->alpn_selected == NULL && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001920 /* The extension must be empty. */
1921 if (CBS_len(&extension) != 0) {
1922 *out_alert = SSL_AD_DECODE_ERROR;
1923 return 0;
1924 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 s->s3->next_proto_neg_seen = 1;
1926 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
David Benjamine6df0542015-05-12 22:02:08 -04001927 s->ctx->alpn_select_cb && !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1929 return 0;
1930 }
1931 /* ALPN takes precedence over NPN. */
1932 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001933 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1934 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 /* The extension must be empty. */
1936 if (CBS_len(&extension) != 0) {
1937 *out_alert = SSL_AD_DECODE_ERROR;
1938 return 0;
1939 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 s->s3->tlsext_channel_id_valid = 1;
1942 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001943 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001944 /* The extension must be empty. */
1945 if (CBS_len(&extension) != 0) {
1946 *out_alert = SSL_AD_DECODE_ERROR;
1947 return 0;
1948 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 s->s3->tlsext_channel_id_valid = 1;
1951 s->s3->tlsext_channel_id_new = 1;
1952 } else if (type == TLSEXT_TYPE_use_srtp) {
1953 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1954 return 0;
1955 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 }
1957 }
Adam Langley75712922014-10-10 16:23:43 -07001958
Adam Langley614c66a2015-06-12 15:26:58 -07001959no_extensions:
1960 for (i = 0; i < kNumExtensions; i++) {
1961 if (!(s->s3->tmp.extensions.received & (1u << i))) {
1962 /* Extension wasn't observed so call the callback with a NULL
1963 * parameter. */
1964 uint8_t alert = SSL_AD_DECODE_ERROR;
1965 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
1966 *out_alert = alert;
1967 return 0;
1968 }
1969 }
1970 }
1971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972 return 1;
1973}
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1976 int alert = -1;
1977 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1978 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1979 return 0;
1980 }
Adam Langley95c29f32014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982 if (ssl_check_clienthello_tlsext(s) <= 0) {
1983 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1984 SSL_R_CLIENTHELLO_TLSEXT);
1985 return 0;
1986 }
Adam Langley95c29f32014-06-20 12:00:00 -07001987
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 return 1;
1989}
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langley95c29f32014-06-20 12:00:00 -07001991/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 * elements of zero length are allowed and the set of elements must exactly
1993 * fill the length of the block. */
1994static char ssl_next_proto_validate(const CBS *cbs) {
1995 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 while (CBS_len(&copy) != 0) {
1998 CBS proto;
1999 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
2000 return 0;
2001 }
2002 }
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 return 1;
2005}
Adam Langley95c29f32014-06-20 12:00:00 -07002006
Adam Langleyfcf25832014-12-18 17:42:32 -08002007static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002008 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 /* TODO(davidben): Move all of these to some per-handshake state that gets
2011 * systematically reset on a new handshake; perhaps allocate it fresh each
2012 * time so it's not even kept around post-handshake. */
2013 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 s->tlsext_ticket_expected = 0;
2015 s->s3->tmp.certificate_status_expected = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05002016 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002017
David Benjamin2755a3e2015-04-22 16:17:58 -04002018 OPENSSL_free(s->s3->alpn_selected);
2019 s->s3->alpn_selected = NULL;
David Benjamina19fc252014-10-19 00:14:36 -04002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021 /* Clear ECC extensions */
David Benjamin2755a3e2015-04-22 16:17:58 -04002022 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
2023 s->s3->tmp.peer_ecpointformatlist = NULL;
2024 s->s3->tmp.peer_ecpointformatlist_length = 0;
David Benjamin03973092014-06-24 23:27:17 -04002025
Adam Langley614c66a2015-06-12 15:26:58 -07002026 uint32_t received = 0;
2027 size_t i;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002028 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002029
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 /* There may be no extensions. */
2031 if (CBS_len(cbs) == 0) {
Adam Langley614c66a2015-06-12 15:26:58 -07002032 goto no_extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 }
Adam Langley95c29f32014-06-20 12:00:00 -07002034
Adam Langleyfcf25832014-12-18 17:42:32 -08002035 /* Decode the extensions block and check it is valid. */
2036 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2037 !tls1_check_duplicate_extensions(&extensions)) {
2038 *out_alert = SSL_AD_DECODE_ERROR;
2039 return 0;
2040 }
Adam Langley95c29f32014-06-20 12:00:00 -07002041
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 while (CBS_len(&extensions) != 0) {
2043 uint16_t type;
2044 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 /* Decode the next extension. */
2047 if (!CBS_get_u16(&extensions, &type) ||
2048 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2049 *out_alert = SSL_AD_DECODE_ERROR;
2050 return 0;
2051 }
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langley614c66a2015-06-12 15:26:58 -07002053 unsigned ext_index;
2054 const struct tls_extension *const ext =
2055 tls_extension_find(&ext_index, type);
2056
2057 /* While we have extensions that don't use tls_extension this conditional
2058 * needs to be guarded on |ext != NULL|. In the future, ext being NULL will
2059 * be fatal. */
2060 if (ext != NULL) {
2061 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2062 /* Received an extension that was never sent. */
2063 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
2064 SSL_R_UNEXPECTED_EXTENSION);
2065 ERR_add_error_dataf("ext:%u", (unsigned) type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 *out_alert = SSL_AD_DECODE_ERROR;
2067 return 0;
2068 }
David Benjamin03973092014-06-24 23:27:17 -04002069
Adam Langley614c66a2015-06-12 15:26:58 -07002070 received |= (1u << ext_index);
2071
2072 uint8_t alert = SSL_AD_DECODE_ERROR;
2073 if (!ext->parse_serverhello(s, &alert, &extension)) {
2074 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 return 0;
2076 }
David Benjamin03973092014-06-24 23:27:17 -04002077
Adam Langley614c66a2015-06-12 15:26:58 -07002078 continue;
2079 }
2080
2081 if (type == TLSEXT_TYPE_ec_point_formats) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002082 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04002083
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
2085 CBS_len(&extension) != 0) {
2086 *out_alert = SSL_AD_DECODE_ERROR;
2087 return 0;
2088 }
Adam Langley95c29f32014-06-20 12:00:00 -07002089
Adam Langleyfcf25832014-12-18 17:42:32 -08002090 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
2091 &s->s3->tmp.peer_ecpointformatlist_length)) {
2092 *out_alert = SSL_AD_INTERNAL_ERROR;
2093 return 0;
2094 }
2095 } else if (type == TLSEXT_TYPE_session_ticket) {
2096 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
2097 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2098 return 0;
2099 }
David Benjamin03973092014-06-24 23:27:17 -04002100
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 s->tlsext_ticket_expected = 1;
2102 } else if (type == TLSEXT_TYPE_status_request) {
2103 /* The extension MUST be empty and may only sent if we've requested a
2104 * status request message. */
2105 if (CBS_len(&extension) != 0) {
2106 *out_alert = SSL_AD_DECODE_ERROR;
2107 return 0;
2108 }
David Benjamin03973092014-06-24 23:27:17 -04002109
Adam Langleyfcf25832014-12-18 17:42:32 -08002110 if (!s->ocsp_stapling_enabled) {
2111 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2112 return 0;
2113 }
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 /* Set a flag to expect a CertificateStatus message */
2116 s->s3->tmp.certificate_status_expected = 1;
2117 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamine6df0542015-05-12 22:02:08 -04002118 !s->s3->initial_handshake_complete && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002119 uint8_t *selected;
2120 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04002121
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 /* We must have requested it. */
2123 if (s->ctx->next_proto_select_cb == NULL) {
2124 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2125 return 0;
2126 }
David Benjamin03973092014-06-24 23:27:17 -04002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 /* The data must be valid. */
2129 if (!ssl_next_proto_validate(&extension)) {
2130 *out_alert = SSL_AD_DECODE_ERROR;
2131 return 0;
2132 }
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 if (s->ctx->next_proto_select_cb(
2135 s, &selected, &selected_len, CBS_data(&extension),
2136 CBS_len(&extension),
2137 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
2138 *out_alert = SSL_AD_INTERNAL_ERROR;
2139 return 0;
2140 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2143 if (s->next_proto_negotiated == NULL) {
2144 *out_alert = SSL_AD_INTERNAL_ERROR;
2145 return 0;
2146 }
Adam Langley75712922014-10-10 16:23:43 -07002147
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 s->next_proto_negotiated_len = selected_len;
2149 s->s3->next_proto_neg_seen = 1;
David Benjamine6df0542015-05-12 22:02:08 -04002150 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2151 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07002153
Adam Langleyfcf25832014-12-18 17:42:32 -08002154 /* We must have requested it. */
2155 if (s->alpn_client_proto_list == NULL) {
2156 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2157 return 0;
2158 }
Adam Langley95c29f32014-06-20 12:00:00 -07002159
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 /* The extension data consists of a ProtocolNameList which must have
2161 * exactly one ProtocolName. Each of these is length-prefixed. */
2162 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2163 CBS_len(&extension) != 0 ||
2164 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
Adam Langleyefb0e162015-07-09 11:35:04 -07002165 /* Empty protocol names are forbidden. */
2166 CBS_len(&protocol_name) == 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002167 CBS_len(&protocol_name_list) != 0) {
2168 *out_alert = SSL_AD_DECODE_ERROR;
2169 return 0;
2170 }
Adam Langley95c29f32014-06-20 12:00:00 -07002171
Adam Langleyfcf25832014-12-18 17:42:32 -08002172 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
2173 &s->s3->alpn_selected_len)) {
2174 *out_alert = SSL_AD_INTERNAL_ERROR;
2175 return 0;
2176 }
David Benjamin78e69782014-12-19 04:52:17 -05002177 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 if (CBS_len(&extension) != 0) {
2179 *out_alert = SSL_AD_DECODE_ERROR;
2180 return 0;
2181 }
Adam Langley95c29f32014-06-20 12:00:00 -07002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05002184 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 if (CBS_len(&extension) != 0) {
2186 *out_alert = SSL_AD_DECODE_ERROR;
2187 return 0;
2188 }
Adam Langley95c29f32014-06-20 12:00:00 -07002189
Adam Langleyfcf25832014-12-18 17:42:32 -08002190 s->s3->tlsext_channel_id_valid = 1;
2191 s->s3->tlsext_channel_id_new = 1;
2192 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
2193 if (CBS_len(&extension) == 0) {
2194 *out_alert = SSL_AD_DECODE_ERROR;
2195 return 0;
2196 }
Adam Langley95c29f32014-06-20 12:00:00 -07002197
Adam Langleyfcf25832014-12-18 17:42:32 -08002198 /* Session resumption uses the original session information. */
2199 if (!s->hit &&
2200 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
2201 &s->session->tlsext_signed_cert_timestamp_list_length)) {
2202 *out_alert = SSL_AD_INTERNAL_ERROR;
2203 return 0;
2204 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 } else if (type == TLSEXT_TYPE_use_srtp) {
2206 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
2207 return 0;
2208 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 }
2210 }
Adam Langley95c29f32014-06-20 12:00:00 -07002211
Adam Langley614c66a2015-06-12 15:26:58 -07002212no_extensions:
2213 for (i = 0; i < kNumExtensions; i++) {
2214 if (!(received & (1u << i))) {
2215 /* Extension wasn't observed so call the callback with a NULL
2216 * parameter. */
2217 uint8_t alert = SSL_AD_DECODE_ERROR;
2218 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
2219 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002220 return 0;
2221 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 }
2223 }
Adam Langley95c29f32014-06-20 12:00:00 -07002224
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 return 1;
2226}
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232static int ssl_check_clienthello_tlsext(SSL *s) {
2233 int ret = SSL_TLSEXT_ERR_NOACK;
2234 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2237 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2240 ret = s->ctx->tlsext_servername_callback(s, &al,
2241 s->ctx->tlsext_servername_arg);
2242 } else if (s->initial_ctx != NULL &&
2243 s->initial_ctx->tlsext_servername_callback != 0) {
2244 ret = s->initial_ctx->tlsext_servername_callback(
2245 s, &al, s->initial_ctx->tlsext_servername_arg);
2246 }
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 switch (ret) {
2249 case SSL_TLSEXT_ERR_ALERT_FATAL:
2250 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2251 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 case SSL_TLSEXT_ERR_ALERT_WARNING:
2254 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2255 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002258 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002260
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 default:
2262 return 1;
2263 }
2264}
Adam Langleyed8270a2014-09-02 13:52:56 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266static int ssl_check_serverhello_tlsext(SSL *s) {
2267 int ret = SSL_TLSEXT_ERR_NOACK;
2268 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 /* If we are client and using an elliptic curve cryptography cipher suite,
2271 * then if server returns an EC point formats lists extension it must contain
2272 * uncompressed. */
David Benjamin107db582015-04-08 00:41:59 -04002273 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2274 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04002275 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
2277 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
2278 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2279 return -1;
2280 }
2281 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2284 ret = s->ctx->tlsext_servername_callback(s, &al,
2285 s->ctx->tlsext_servername_arg);
2286 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002287 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 ret = s->initial_ctx->tlsext_servername_callback(
2289 s, &al, s->initial_ctx->tlsext_servername_arg);
2290 }
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 switch (ret) {
2293 case SSL_TLSEXT_ERR_ALERT_FATAL:
2294 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2295 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 case SSL_TLSEXT_ERR_ALERT_WARNING:
2298 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2299 return 1;
2300
2301 default:
2302 return 1;
2303 }
2304}
2305
2306int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2307 int alert = -1;
2308 if (s->version < SSL3_VERSION) {
2309 return 1;
2310 }
2311
2312 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2313 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2314 return 0;
2315 }
2316
2317 if (ssl_check_serverhello_tlsext(s) <= 0) {
2318 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2319 SSL_R_SERVERHELLO_TLSEXT);
2320 return 0;
2321 }
2322
2323 return 1;
2324}
Adam Langley95c29f32014-06-20 12:00:00 -07002325
David Benjamine3aa1d92015-06-16 15:34:50 -04002326int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2327 int *out_send_ticket, const uint8_t *ticket,
2328 size_t ticket_len, const uint8_t *session_id,
2329 size_t session_id_len) {
2330 int ret = 1; /* Most errors are non-fatal. */
2331 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2332 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002333
David Benjamine3aa1d92015-06-16 15:34:50 -04002334 HMAC_CTX hmac_ctx;
2335 HMAC_CTX_init(&hmac_ctx);
2336 EVP_CIPHER_CTX cipher_ctx;
2337 EVP_CIPHER_CTX_init(&cipher_ctx);
2338
2339 *out_send_ticket = 0;
2340 *out_session = NULL;
2341
2342 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2343 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 }
2345
David Benjamine3aa1d92015-06-16 15:34:50 -04002346 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002348 *out_send_ticket = 1;
2349 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 }
2351
David Benjaminadcc3952015-04-26 13:07:57 -04002352 /* Ensure there is room for the key name and the largest IV
2353 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2354 * the maximum IV length should be well under the minimum size for the
2355 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002356 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2357 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002359 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002360
David Benjamine3aa1d92015-06-16 15:34:50 -04002361 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2362 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2363 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2364 0 /* decrypt */);
2365 if (cb_ret < 0) {
2366 ret = 0;
2367 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002369 if (cb_ret == 0) {
2370 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002372 if (cb_ret == 2) {
2373 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 }
2375 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002376 /* Check the key name matches. */
2377 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2378 SSL_TICKET_KEY_NAME_LEN) != 0) {
2379 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002381 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2382 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002383 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002384 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2385 ssl_ctx->tlsext_tick_aes_key, iv)) {
2386 ret = 0;
2387 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002388 }
2389 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002390 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002391
David Benjamine3aa1d92015-06-16 15:34:50 -04002392 /* Check the MAC at the end of the ticket. */
2393 uint8_t mac[EVP_MAX_MD_SIZE];
2394 size_t mac_len = HMAC_size(&hmac_ctx);
2395 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002396 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002397 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002399 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2400 HMAC_Final(&hmac_ctx, mac, NULL);
2401 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2402 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 }
2404
David Benjamine3aa1d92015-06-16 15:34:50 -04002405 /* Decrypt the session data. */
2406 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2407 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2408 mac_len;
2409 plaintext = OPENSSL_malloc(ciphertext_len);
2410 if (plaintext == NULL) {
2411 ret = 0;
2412 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002414 if (ciphertext_len >= INT_MAX) {
2415 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002417 int len1, len2;
2418 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2419 (int)ciphertext_len) ||
2420 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2421 ERR_clear_error(); /* Don't leave an error on the queue. */
2422 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 }
2424
David Benjamine3aa1d92015-06-16 15:34:50 -04002425 /* Decode the session. */
2426 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2427 if (session == NULL) {
2428 ERR_clear_error(); /* Don't leave an error on the queue. */
2429 goto done;
2430 }
2431
2432 /* Copy the client's session ID into the new session, to denote the ticket has
2433 * been accepted. */
2434 memcpy(session->session_id, session_id, session_id_len);
2435 session->session_id_length = session_id_len;
2436
2437 *out_session = session;
2438
2439done:
2440 OPENSSL_free(plaintext);
2441 HMAC_CTX_cleanup(&hmac_ctx);
2442 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2443 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002444}
Adam Langley95c29f32014-06-20 12:00:00 -07002445
2446/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002447typedef struct {
2448 int nid;
2449 int id;
2450} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002451
Adam Langleyfcf25832014-12-18 17:42:32 -08002452static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2453 {NID_sha1, TLSEXT_hash_sha1},
2454 {NID_sha224, TLSEXT_hash_sha224},
2455 {NID_sha256, TLSEXT_hash_sha256},
2456 {NID_sha384, TLSEXT_hash_sha384},
2457 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2460 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2463 size_t i;
2464 for (i = 0; i < tlen; i++) {
2465 if (table[i].nid == nid) {
2466 return table[i].id;
2467 }
2468 }
Adam Langley95c29f32014-06-20 12:00:00 -07002469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 return -1;
2471}
Adam Langley95c29f32014-06-20 12:00:00 -07002472
David Benjaminb4d65fd2015-05-29 17:11:21 -04002473int tls12_get_sigid(int pkey_type) {
2474 return tls12_find_id(pkey_type, tls12_sig,
2475 sizeof(tls12_sig) / sizeof(tls12_lookup));
2476}
2477
David Benjamind1d80782015-07-05 11:54:09 -04002478int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002479 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 if (!md) {
2482 return 0;
2483 }
Adam Langley95c29f32014-06-20 12:00:00 -07002484
Adam Langleyfcf25832014-12-18 17:42:32 -08002485 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2486 sizeof(tls12_md) / sizeof(tls12_lookup));
2487 if (md_id == -1) {
2488 return 0;
2489 }
Adam Langley95c29f32014-06-20 12:00:00 -07002490
David Benjamind1d80782015-07-05 11:54:09 -04002491 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 if (sig_id == -1) {
2493 return 0;
2494 }
Adam Langley95c29f32014-06-20 12:00:00 -07002495
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 p[0] = (uint8_t)md_id;
2497 p[1] = (uint8_t)sig_id;
2498 return 1;
2499}
2500
Adam Langleyfcf25832014-12-18 17:42:32 -08002501const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2502 switch (hash_alg) {
2503 case TLSEXT_hash_md5:
2504 return EVP_md5();
2505
2506 case TLSEXT_hash_sha1:
2507 return EVP_sha1();
2508
2509 case TLSEXT_hash_sha224:
2510 return EVP_sha224();
2511
2512 case TLSEXT_hash_sha256:
2513 return EVP_sha256();
2514
2515 case TLSEXT_hash_sha384:
2516 return EVP_sha384();
2517
2518 case TLSEXT_hash_sha512:
2519 return EVP_sha512();
2520
2521 default:
2522 return NULL;
2523 }
2524}
Adam Langley95c29f32014-06-20 12:00:00 -07002525
David Benjaminec2f27d2014-11-13 19:17:25 -05002526/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2527 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002528static int tls12_get_pkey_type(uint8_t sig_alg) {
2529 switch (sig_alg) {
2530 case TLSEXT_signature_rsa:
2531 return EVP_PKEY_RSA;
2532
2533 case TLSEXT_signature_ecdsa:
2534 return EVP_PKEY_EC;
2535
2536 default:
2537 return -1;
2538 }
2539}
Adam Langley95c29f32014-06-20 12:00:00 -07002540
Adam Langley95c29f32014-06-20 12:00:00 -07002541/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002542static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2543 size_t preflen, const uint8_t *allow,
2544 size_t allowlen) {
2545 const uint8_t *ptmp, *atmp;
2546 size_t i, j, nmatch = 0;
2547
2548 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2549 /* Skip disabled hashes or signature algorithms */
2550 if (tls12_get_hash(ptmp[0]) == NULL ||
2551 tls12_get_pkey_type(ptmp[1]) == -1) {
2552 continue;
2553 }
2554
2555 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2556 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2557 nmatch++;
2558 if (shsig) {
2559 shsig->rhash = ptmp[0];
2560 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 shsig++;
2562 }
2563
2564 break;
2565 }
2566 }
2567 }
2568
2569 return nmatch;
2570}
Adam Langley95c29f32014-06-20 12:00:00 -07002571
2572/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002573static int tls1_set_shared_sigalgs(SSL *s) {
2574 const uint8_t *pref, *allow, *conf;
2575 size_t preflen, allowlen, conflen;
2576 size_t nmatch;
2577 TLS_SIGALGS *salgs = NULL;
2578 CERT *c = s->cert;
2579
David Benjamin2755a3e2015-04-22 16:17:58 -04002580 OPENSSL_free(c->shared_sigalgs);
2581 c->shared_sigalgs = NULL;
2582 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002583
2584 /* If client use client signature algorithms if not NULL */
2585 if (!s->server && c->client_sigalgs) {
2586 conf = c->client_sigalgs;
2587 conflen = c->client_sigalgslen;
2588 } else if (c->conf_sigalgs) {
2589 conf = c->conf_sigalgs;
2590 conflen = c->conf_sigalgslen;
2591 } else {
2592 conflen = tls12_get_psigalgs(s, &conf);
2593 }
2594
2595 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2596 pref = conf;
2597 preflen = conflen;
2598 allow = c->peer_sigalgs;
2599 allowlen = c->peer_sigalgslen;
2600 } else {
2601 allow = conf;
2602 allowlen = conflen;
2603 pref = c->peer_sigalgs;
2604 preflen = c->peer_sigalgslen;
2605 }
2606
2607 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2608 if (!nmatch) {
2609 return 1;
2610 }
2611
2612 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2613 if (!salgs) {
2614 return 0;
2615 }
2616
2617 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2618 c->shared_sigalgs = salgs;
2619 c->shared_sigalgslen = nmatch;
2620 return 1;
2621}
Adam Langley95c29f32014-06-20 12:00:00 -07002622
2623/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002624int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2625 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002626
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 /* Extension ignored for inappropriate versions */
2628 if (!SSL_USE_SIGALGS(s)) {
2629 return 1;
2630 }
David Benjamincd996942014-07-20 16:23:51 -04002631
David Benjamin61c0d4e2015-03-19 14:24:37 -04002632 if (CBS_len(sigalgs) % 2 != 0 ||
2633 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2634 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 return 0;
2636 }
Adam Langley95c29f32014-06-20 12:00:00 -07002637
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 return 1;
2639}
David Benjaminec2f27d2014-11-13 19:17:25 -05002640
David Benjamind1d80782015-07-05 11:54:09 -04002641const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2642 CERT *cert = ssl->cert;
2643 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002644 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002647 for (i = 0; i < cert->shared_sigalgslen; i++) {
2648 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002650 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2651 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002652 continue;
2653 }
2654 return md;
2655 }
Adam Langley95c29f32014-06-20 12:00:00 -07002656
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 /* If no suitable digest may be found, default to SHA-1. */
2658 return EVP_sha1();
2659}
Adam Langley95c29f32014-06-20 12:00:00 -07002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2662 * given SSL connection and writes it to |md|. */
2663int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2664 EVP_MD_CTX ctx;
2665 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2666 unsigned temp_digest_len;
2667 int i;
2668 static const char kClientIDMagic[] = "TLS Channel ID signature";
2669
2670 if (s->s3->handshake_buffer &&
2671 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2672 return 0;
2673 }
2674
2675 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2676
2677 if (s->hit && s->s3->tlsext_channel_id_new) {
2678 static const char kResumptionMagic[] = "Resumption";
2679 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2680 if (s->session->original_handshake_hash_len == 0) {
2681 return 0;
2682 }
2683 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2684 s->session->original_handshake_hash_len);
2685 }
2686
2687 EVP_MD_CTX_init(&ctx);
2688 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2689 if (s->s3->handshake_dgst[i] == NULL) {
2690 continue;
2691 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002692 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2693 EVP_MD_CTX_cleanup(&ctx);
2694 return 0;
2695 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2697 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2698 }
2699 EVP_MD_CTX_cleanup(&ctx);
2700
2701 return 1;
2702}
Adam Langley1258b6a2014-06-20 12:00:00 -07002703
2704/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2705 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002706int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2707 int digest_len;
2708 /* This function should never be called for a resumed session because the
2709 * handshake hashes that we wish to record are for the original, full
2710 * handshake. */
2711 if (s->hit) {
2712 return -1;
2713 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002714
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 /* It only makes sense to call this function if Channel IDs have been
2716 * negotiated. */
2717 if (!s->s3->tlsext_channel_id_new) {
2718 return -1;
2719 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 digest_len =
2722 tls1_handshake_digest(s, s->session->original_handshake_hash,
2723 sizeof(s->session->original_handshake_hash));
2724 if (digest_len < 0) {
2725 return -1;
2726 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002727
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 return 1;
2731}
Adam Langley95c29f32014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2734 int client) {
2735 uint8_t *sigalgs, *sptr;
2736 int rhash, rsign;
2737 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002738
Adam Langleyfcf25832014-12-18 17:42:32 -08002739 if (salglen & 1) {
2740 return 0;
2741 }
Adam Langley95c29f32014-06-20 12:00:00 -07002742
Adam Langleyfcf25832014-12-18 17:42:32 -08002743 sigalgs = OPENSSL_malloc(salglen);
2744 if (sigalgs == NULL) {
2745 return 0;
2746 }
Adam Langley95c29f32014-06-20 12:00:00 -07002747
Adam Langleyfcf25832014-12-18 17:42:32 -08002748 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2749 rhash = tls12_find_id(*psig_nids++, tls12_md,
2750 sizeof(tls12_md) / sizeof(tls12_lookup));
2751 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2752 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 if (rhash == -1 || rsign == -1) {
2755 goto err;
2756 }
2757 *sptr++ = rhash;
2758 *sptr++ = rsign;
2759 }
2760
2761 if (client) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002762 OPENSSL_free(c->client_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 c->client_sigalgs = sigalgs;
2764 c->client_sigalgslen = salglen;
2765 } else {
David Benjamin2755a3e2015-04-22 16:17:58 -04002766 OPENSSL_free(c->conf_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 c->conf_sigalgs = sigalgs;
2768 c->conf_sigalgslen = salglen;
2769 }
2770
2771 return 1;
2772
2773err:
2774 OPENSSL_free(sigalgs);
2775 return 0;
2776}