blob: 5d490b537f8ed45cbfb47c71104e778199930e47 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjaminf0ae1702015-04-07 23:05:04 -0400109#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700110#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400111#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400112#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin03973092014-06-24 23:27:17 -0400114#include <openssl/bytestring.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <openssl/evp.h>
117#include <openssl/hmac.h>
118#include <openssl/mem.h>
119#include <openssl/obj.h>
120#include <openssl/rand.h>
121
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800123
124
125static int tls_decrypt_ticket(SSL *s, const uint8_t *tick, int ticklen,
126 const uint8_t *sess_id, int sesslen,
127 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400128static int ssl_check_clienthello_tlsext(SSL *s);
129static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
David Benjamin338fcaf2014-12-11 01:20:52 -0500131const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500132 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800137 tls1_cert_verify_mac,
138 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
140 tls1_alert_code,
141 tls1_export_keying_material,
142 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800143};
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin338fcaf2014-12-11 01:20:52 -0500145const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500146 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 tls1_setup_key_block,
148 tls1_generate_master_secret,
149 tls1_change_cipher_state,
150 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 tls1_cert_verify_mac,
152 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
153 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
154 tls1_alert_code,
155 tls1_export_keying_material,
156 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800157};
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin338fcaf2014-12-11 01:20:52 -0500159const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500160 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800161 tls1_setup_key_block,
162 tls1_generate_master_secret,
163 tls1_change_cipher_state,
164 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 tls1_cert_verify_mac,
166 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
167 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
168 tls1_alert_code,
169 tls1_export_keying_material,
170 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
171 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800172};
Adam Langley95c29f32014-06-20 12:00:00 -0700173
Adam Langleyfcf25832014-12-18 17:42:32 -0800174static int compare_uint16_t(const void *p1, const void *p2) {
175 uint16_t u1 = *((const uint16_t *)p1);
176 uint16_t u2 = *((const uint16_t *)p2);
177 if (u1 < u2) {
178 return -1;
179 } else if (u1 > u2) {
180 return 1;
181 } else {
182 return 0;
183 }
184}
David Benjamin35a7a442014-07-05 00:23:20 -0400185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
187 * more than one extension of the same type in a ClientHello or ServerHello.
188 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400189 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800190static int tls1_check_duplicate_extensions(const CBS *cbs) {
191 CBS extensions = *cbs;
192 size_t num_extensions = 0, i = 0;
193 uint16_t *extension_types = NULL;
194 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 /* First pass: count the extensions. */
197 while (CBS_len(&extensions) > 0) {
198 uint16_t type;
199 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 if (!CBS_get_u16(&extensions, &type) ||
202 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
203 goto done;
204 }
David Benjamin35a7a442014-07-05 00:23:20 -0400205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 num_extensions++;
207 }
David Benjamin35a7a442014-07-05 00:23:20 -0400208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 if (num_extensions == 0) {
210 return 1;
211 }
David Benjamin9a373592014-07-25 04:27:53 -0400212
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 extension_types =
214 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
215 if (extension_types == NULL) {
216 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
217 ERR_R_MALLOC_FAILURE);
218 goto done;
219 }
David Benjamin35a7a442014-07-05 00:23:20 -0400220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 /* Second pass: gather the extension types. */
222 extensions = *cbs;
223 for (i = 0; i < num_extensions; i++) {
224 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
227 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
228 /* This should not happen. */
229 goto done;
230 }
231 }
232 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 /* Sort the extensions and make sure there are no duplicates. */
235 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
236 for (i = 1; i < num_extensions; i++) {
237 if (extension_types[i - 1] == extension_types[i]) {
238 goto done;
239 }
240 }
David Benjamin35a7a442014-07-05 00:23:20 -0400241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 ret = 1;
243
David Benjamin35a7a442014-07-05 00:23:20 -0400244done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400245 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 return ret;
247}
David Benjamin35a7a442014-07-05 00:23:20 -0400248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
250 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (/* Skip client version. */
255 !CBS_skip(&client_hello, 2) ||
256 /* Skip client nonce. */
257 !CBS_skip(&client_hello, 32) ||
258 /* Extract session_id. */
259 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
260 return 0;
261 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 ctx->session_id = CBS_data(&session_id);
264 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 /* Skip past DTLS cookie */
267 if (SSL_IS_DTLS(ctx->ssl)) {
268 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
271 return 0;
272 }
273 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 /* Extract cipher_suites. */
276 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
277 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
278 return 0;
279 }
280 ctx->cipher_suites = CBS_data(&cipher_suites);
281 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 /* Extract compression_methods. */
284 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
285 CBS_len(&compression_methods) < 1) {
286 return 0;
287 }
288 ctx->compression_methods = CBS_data(&compression_methods);
289 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 /* If the ClientHello ends here then it's valid, but doesn't have any
292 * extensions. (E.g. SSLv3.) */
293 if (CBS_len(&client_hello) == 0) {
294 ctx->extensions = NULL;
295 ctx->extensions_len = 0;
296 return 1;
297 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 /* Extract extensions and check it is valid. */
300 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
301 !tls1_check_duplicate_extensions(&extensions) ||
302 CBS_len(&client_hello) != 0) {
303 return 0;
304 }
305 ctx->extensions = CBS_data(&extensions);
306 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 return 1;
309}
Adam Langleydc9b1412014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311char SSL_early_callback_ctx_extension_get(
312 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
313 const uint8_t **out_data, size_t *out_len) {
314 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 while (CBS_len(&extensions) != 0) {
319 uint16_t type;
320 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 /* Decode the next extension. */
323 if (!CBS_get_u16(&extensions, &type) ||
324 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
325 return 0;
326 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 if (type == extension_type) {
329 *out_data = CBS_data(&extension);
330 *out_len = CBS_len(&extension);
331 return 1;
332 }
333 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700334
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 return 0;
336}
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin52e5bac2014-12-27 02:27:35 -0500338struct tls_curve {
339 uint16_t curve_id;
340 int nid;
341};
342
David Benjamin70bd80a2014-12-27 03:06:46 -0500343/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500344static const struct tls_curve tls_curves[] = {
345 {21, NID_secp224r1},
346 {23, NID_X9_62_prime256v1},
347 {24, NID_secp384r1},
348 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800349};
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351static const uint8_t ecformats_default[] = {
352 TLSEXT_ECPOINTFORMAT_uncompressed,
353};
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Adam Langleyfcf25832014-12-18 17:42:32 -0800355static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400356 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500357 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800358};
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500361 size_t i;
362 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
363 if (curve_id == tls_curves[i].curve_id) {
364 return tls_curves[i].nid;
365 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500367 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368}
Adam Langley95c29f32014-06-20 12:00:00 -0700369
David Benjamin70bd80a2014-12-27 03:06:46 -0500370int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500372 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
373 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500374 *out_curve_id = tls_curves[i].curve_id;
375 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 0;
379}
380
381/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
382 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
383 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400384static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 const uint16_t **out_curve_ids,
386 size_t *out_curve_ids_len) {
387 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400388 /* Only clients send a curve list, so this function is only called
389 * on the server. */
390 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
392 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
393 return;
394 }
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 *out_curve_ids = s->tlsext_ellipticcurvelist;
397 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
398 if (!*out_curve_ids) {
399 *out_curve_ids = eccurves_default;
400 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
401 }
402}
David Benjamined439582014-07-14 19:13:02 -0400403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
405 uint8_t curve_type;
406 uint16_t curve_id;
407 const uint16_t *curves;
408 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 /* Only support named curves. */
411 if (!CBS_get_u8(cbs, &curve_type) ||
412 curve_type != NAMED_CURVE_TYPE ||
413 !CBS_get_u16(cbs, &curve_id)) {
414 return 0;
415 }
David Benjamined439582014-07-14 19:13:02 -0400416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 tls1_get_curvelist(s, 0, &curves, &curves_len);
418 for (i = 0; i < curves_len; i++) {
419 if (curve_id == curves[i]) {
420 *out_curve_id = curve_id;
421 return 1;
422 }
423 }
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 return 0;
426}
David Benjamin072334d2014-07-13 16:24:27 -0400427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400429 const uint16_t *curves, *peer_curves, *pref, *supp;
430 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 /* Can't do anything on client side */
433 if (s->server == 0) {
434 return NID_undef;
435 }
436
David Benjamin55a43642015-04-20 14:45:55 -0400437 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
438 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800439
David Benjamin55a43642015-04-20 14:45:55 -0400440 if (peer_curves_len == 0) {
441 /* Clients are not required to send a supported_curves extension. In this
442 * case, the server is free to pick any curve it likes. See RFC 4492,
443 * section 4, paragraph 3. */
444 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
445 }
446
447 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
448 pref = curves;
449 pref_len = curves_len;
450 supp = peer_curves;
451 supp_len = peer_curves_len;
452 } else {
453 pref = peer_curves;
454 pref_len = peer_curves_len;
455 supp = curves;
456 supp_len = curves_len;
457 }
458
459 for (i = 0; i < pref_len; i++) {
460 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 if (pref[i] == supp[j]) {
462 return tls1_ec_curve_id2nid(pref[i]);
463 }
464 }
465 }
466
467 return NID_undef;
468}
Adam Langley95c29f32014-06-20 12:00:00 -0700469
David Benjamin072334d2014-07-13 16:24:27 -0400470int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 const int *curves, size_t ncurves) {
472 uint16_t *curve_ids;
473 size_t i;
474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
476 if (curve_ids == NULL) {
477 return 0;
478 }
479
480 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500481 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 OPENSSL_free(curve_ids);
483 return 0;
484 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 }
486
David Benjamin2755a3e2015-04-22 16:17:58 -0400487 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 *out_curve_ids = curve_ids;
489 *out_curve_ids_len = ncurves;
490
491 return 1;
492}
Adam Langley95c29f32014-06-20 12:00:00 -0700493
David Benjamin072334d2014-07-13 16:24:27 -0400494/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
495 * TLS curve ID and point format, respectively, for |ec|. It returns one on
496 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800497static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
498 uint8_t *out_comp_id, EC_KEY *ec) {
499 int nid;
500 uint16_t id;
501 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 if (ec == NULL) {
504 return 0;
505 }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 grp = EC_KEY_get0_group(ec);
508 if (grp == NULL) {
509 return 0;
510 }
David Benjamin072334d2014-07-13 16:24:27 -0400511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 /* Determine curve ID */
513 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500514 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 return 0;
516 }
David Benjamin072334d2014-07-13 16:24:27 -0400517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
519 *out_curve_id = id;
520
521 if (out_comp_id) {
522 if (EC_KEY_get0_public_key(ec) == NULL) {
523 return 0;
524 }
525 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
526 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
527 } else {
528 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
529 }
530 }
531
532 return 1;
533}
David Benjamin072334d2014-07-13 16:24:27 -0400534
David Benjamin42e9a772014-09-02 23:18:44 -0400535/* tls1_check_point_format returns one if |comp_id| is consistent with the
536 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800537static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
538 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
539 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
540 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 /* If point formats extension present check it, otherwise everything is
543 * supported (see RFC4492). */
544 if (p == NULL) {
545 return 1;
546 }
David Benjamin42e9a772014-09-02 23:18:44 -0400547
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 for (i = 0; i < plen; i++) {
549 if (comp_id == p[i]) {
550 return 1;
551 }
552 }
David Benjamin42e9a772014-09-02 23:18:44 -0400553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 return 0;
555}
556
557/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
558 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400559 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800560static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
561 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400562 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400565 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
566 if (get_peer_curves && !s->server) {
567 /* Servers do not present a preference list so, if we are a client, only
568 * check our list. */
569 continue;
570 }
571
572 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
573 if (get_peer_curves && curves_len == 0) {
574 /* Clients are not required to send a supported_curves extension. In this
575 * case, the server is free to pick any curve it likes. See RFC 4492,
576 * section 4, paragraph 3. */
577 continue;
578 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 for (i = 0; i < curves_len; i++) {
580 if (curves[i] == curve_id) {
581 break;
582 }
583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 if (i == curves_len) {
586 return 0;
587 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 }
David Benjamin033e5f42014-11-13 18:47:41 -0500589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 return 1;
591}
David Benjamin033e5f42014-11-13 18:47:41 -0500592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
594 size_t *pformatslen) {
595 /* If we have a custom point format list use it otherwise use default */
596 if (s->tlsext_ecpointformatlist) {
597 *pformats = s->tlsext_ecpointformatlist;
598 *pformatslen = s->tlsext_ecpointformatlist_length;
599 } else {
600 *pformats = ecformats_default;
601 *pformatslen = sizeof(ecformats_default);
602 }
603}
604
605int tls1_check_ec_cert(SSL *s, X509 *x) {
606 int ret = 0;
607 EVP_PKEY *pkey = X509_get_pubkey(x);
608 uint16_t curve_id;
609 uint8_t comp_id;
610
611 if (!pkey ||
612 pkey->type != EVP_PKEY_EC ||
613 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
614 !tls1_check_curve_id(s, curve_id) ||
615 !tls1_check_point_format(s, comp_id)) {
616 goto done;
617 }
618
619 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500620
621done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400622 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 return ret;
624}
David Benjamin42e9a772014-09-02 23:18:44 -0400625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400627 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400628 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400629 * the curve. */
630 uint16_t curve_id;
631 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
632 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 }
634
David Benjamindd978782015-04-24 15:20:13 -0400635 if (s->cert->ecdh_tmp_cb != NULL) {
636 /* Assume the callback will provide an acceptable curve. */
637 return 1;
638 }
639
640 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
641 * there is a shared curve. */
642 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800643}
Adam Langley95c29f32014-06-20 12:00:00 -0700644
645/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langley95c29f32014-06-20 12:00:00 -0700648#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langley95c29f32014-06-20 12:00:00 -0700650#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700653
David Benjamincff64722014-08-19 19:54:46 -0400654static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 tlsext_sigalg(TLSEXT_hash_sha512)
656 tlsext_sigalg(TLSEXT_hash_sha384)
657 tlsext_sigalg(TLSEXT_hash_sha256)
658 tlsext_sigalg(TLSEXT_hash_sha224)
659 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700660};
David Benjamin05da6e12014-07-12 20:42:55 -0400661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
663 /* If server use client authentication sigalgs if not NULL */
664 if (s->server && s->cert->client_sigalgs) {
665 *psigs = s->cert->client_sigalgs;
666 return s->cert->client_sigalgslen;
667 } else if (s->cert->conf_sigalgs) {
668 *psigs = s->cert->conf_sigalgs;
669 return s->cert->conf_sigalgslen;
670 } else {
671 *psigs = tls12_sigalgs;
672 return sizeof(tls12_sigalgs);
673 }
674}
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
677 * checks it is consistent with |s|'s sent supported signature algorithms and,
678 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
679 * returns 0 and writes an alert into |*out_alert|. */
680int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
681 CBS *cbs, EVP_PKEY *pkey) {
682 const uint8_t *sent_sigs;
683 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400684 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 uint8_t hash, signature;
686
687 /* Should never happen */
688 if (sigalg == -1) {
689 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
690 *out_alert = SSL_AD_INTERNAL_ERROR;
691 return 0;
692 }
693
694 if (!CBS_get_u8(cbs, &hash) ||
695 !CBS_get_u8(cbs, &signature)) {
696 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
697 *out_alert = SSL_AD_DECODE_ERROR;
698 return 0;
699 }
700
701 /* Check key type is consistent with signature */
702 if (sigalg != signature) {
703 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
704 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
705 return 0;
706 }
707
708 if (pkey->type == EVP_PKEY_EC) {
709 uint16_t curve_id;
710 uint8_t comp_id;
711 /* Check compression and curve matches extensions */
712 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
713 *out_alert = SSL_AD_INTERNAL_ERROR;
714 return 0;
715 }
716
717 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
718 !tls1_check_point_format(s, comp_id))) {
719 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
720 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
721 return 0;
722 }
723 }
724
725 /* Check signature matches a type we sent */
726 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
727 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
728 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
729 break;
730 }
731 }
732
733 /* Allow fallback to SHA-1. */
734 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
735 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
736 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
737 return 0;
738 }
739
740 *out_md = tls12_get_hash(hash);
741 if (*out_md == NULL) {
742 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
743 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
744 return 0;
745 }
746
747 return 1;
748}
749
750/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
751 * supported or doesn't appear in supported signature algorithms. Unlike
752 * ssl_cipher_get_disabled this applies to a specific session and not global
753 * settings. */
754void ssl_set_client_disabled(SSL *s) {
755 CERT *c = s->cert;
756 const uint8_t *sigalgs;
757 size_t i, sigalgslen;
758 int have_rsa = 0, have_ecdsa = 0;
759 c->mask_a = 0;
760 c->mask_k = 0;
761
762 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
763 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
764 c->mask_ssl = SSL_TLSV1_2;
765 } else {
766 c->mask_ssl = 0;
767 }
768
769 /* Now go through all signature algorithms seeing if we support any for RSA,
770 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
771 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
772 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
773 switch (sigalgs[1]) {
774 case TLSEXT_signature_rsa:
775 have_rsa = 1;
776 break;
777
778 case TLSEXT_signature_ecdsa:
779 have_ecdsa = 1;
780 break;
781 }
782 }
783
784 /* Disable auth if we don't include any appropriate signature algorithms. */
785 if (!have_rsa) {
786 c->mask_a |= SSL_aRSA;
787 }
788 if (!have_ecdsa) {
789 c->mask_a |= SSL_aECDSA;
790 }
791
792 /* with PSK there must be client callback set */
793 if (!s->psk_client_callback) {
794 c->mask_a |= SSL_aPSK;
795 c->mask_k |= SSL_kPSK;
796 }
797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langley614c66a2015-06-12 15:26:58 -0700799/* tls_extension represents a TLS extension that is handled internally. The
800 * |init| function is called for each handshake, before any other functions of
801 * the extension. Then the add and parse callbacks are called as needed.
802 *
803 * The parse callbacks receive a |CBS| that contains the contents of the
804 * extension (i.e. not including the type and length bytes). If an extension is
805 * not received then the parse callbacks will be called with a NULL CBS so that
806 * they can do any processing needed to handle the absence of an extension.
807 *
808 * The add callbacks receive a |CBB| to which the extension can be appended but
809 * the function is responsible for appending the type and length bytes too.
810 *
811 * All callbacks return one for success and zero for error. If a parse function
812 * returns zero then a fatal alert with value |*out_alert| will be sent. If
813 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
814struct tls_extension {
815 uint16_t value;
816 void (*init)(SSL *ssl);
817
818 int (*add_clienthello)(SSL *ssl, CBB *out);
819 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
820
821 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
822 int (*add_serverhello)(SSL *ssl, CBB *out);
823};
824
825
826/* Server name indication (SNI).
827 *
828 * https://tools.ietf.org/html/rfc6066#section-3. */
829
830static void ext_sni_init(SSL *ssl) {
831 ssl->s3->tmp.should_ack_sni = 0;
832}
833
834static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
835 if (ssl->tlsext_hostname == NULL) {
836 return 1;
837 }
838
839 CBB contents, server_name_list, name;
840 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
841 !CBB_add_u16_length_prefixed(out, &contents) ||
842 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
843 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
844 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
845 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
846 strlen(ssl->tlsext_hostname)) ||
847 !CBB_flush(out)) {
848 return 0;
849 }
850
851 return 1;
852}
853
854static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
855 if (contents == NULL) {
856 return 1;
857 }
858
859 if (CBS_len(contents) != 0) {
860 return 0;
861 }
862
863 assert(ssl->tlsext_hostname != NULL);
864
865 if (!ssl->hit) {
866 assert(ssl->session->tlsext_hostname == NULL);
867 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
868 if (!ssl->session->tlsext_hostname) {
869 *out_alert = SSL_AD_INTERNAL_ERROR;
870 return 0;
871 }
872 }
873
874 return 1;
875}
876
877static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
878 if (contents == NULL) {
879 return 1;
880 }
881
882 /* The servername extension is treated as follows:
883 *
884 * - Only the hostname type is supported with a maximum length of 255.
885 * - The servername is rejected if too long or if it contains zeros, in
886 * which case an fatal alert is generated.
887 * - The servername field is maintained together with the session cache.
888 * - When a session is resumed, the servername callback is invoked in order
889 * to allow the application to position itself to the right context.
890 * - The servername is acknowledged if it is new for a session or when
891 * it is identical to a previously used for the same session.
892 * Applications can control the behaviour. They can at any time
893 * set a 'desirable' servername for a new SSL object. This can be the
894 * case for example with HTTPS when a Host: header field is received and
895 * a renegotiation is requested. In this case, a possible servername
896 * presented in the new client hello is only acknowledged if it matches
897 * the value of the Host: field.
898 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
899 * if they provide for changing an explicit servername context for the
900 * session,
901 * i.e. when the session has been established with a servername extension.
902 */
903
904 CBS server_name_list;
905 char have_seen_host_name = 0;
906
907 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
908 CBS_len(&server_name_list) == 0 ||
909 CBS_len(contents) != 0) {
910 return 0;
911 }
912
913 /* Decode each ServerName in the extension. */
914 while (CBS_len(&server_name_list) > 0) {
915 uint8_t name_type;
916 CBS host_name;
917
918 if (!CBS_get_u8(&server_name_list, &name_type) ||
919 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
920 return 0;
921 }
922
923 /* Only host_name is supported. */
924 if (name_type != TLSEXT_NAMETYPE_host_name) {
925 continue;
926 }
927
928 if (have_seen_host_name) {
929 /* The ServerNameList MUST NOT contain more than one name of the same
930 * name_type. */
931 return 0;
932 }
933
934 have_seen_host_name = 1;
935
936 if (CBS_len(&host_name) == 0 ||
937 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
938 CBS_contains_zero_byte(&host_name)) {
939 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
940 return 0;
941 }
942
943 if (!ssl->hit) {
944 assert(ssl->session->tlsext_hostname == NULL);
945 if (ssl->session->tlsext_hostname) {
946 /* This should be impossible. */
947 return 0;
948 }
949
950 /* Copy the hostname as a string. */
951 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
952 *out_alert = SSL_AD_INTERNAL_ERROR;
953 return 0;
954 }
955
956 ssl->s3->tmp.should_ack_sni = 1;
957 }
958 }
959
960 return 1;
961}
962
963static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
964 if (ssl->hit ||
965 !ssl->s3->tmp.should_ack_sni ||
966 ssl->session->tlsext_hostname == NULL) {
967 return 1;
968 }
969
970 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
971 !CBB_add_u16(out, 0 /* length */)) {
972 return 0;
973 }
974
975 return 1;
976}
977
978
979/* kExtensions contains all the supported extensions. */
980static const struct tls_extension kExtensions[] = {
981 {
982 TLSEXT_TYPE_server_name,
983 ext_sni_init,
984 ext_sni_add_clienthello,
985 ext_sni_parse_serverhello,
986 ext_sni_parse_clienthello,
987 ext_sni_add_serverhello,
988 },
989};
990
991#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
992
993static const struct tls_extension *tls_extension_find(uint32_t *out_index,
994 uint16_t value) {
995 unsigned i;
996 for (i = 0; i < kNumExtensions; i++) {
997 if (kExtensions[i].value == value) {
998 *out_index = i;
999 return &kExtensions[i];
1000 }
1001 }
1002
1003 return NULL;
1004}
1005
Adam Langleyb0c235e2014-06-20 12:00:00 -07001006/* header_len is the length of the ClientHello header written so far, used to
1007 * compute padding. It does not include the record header. Pass 0 if no padding
1008 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07001009uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
1010 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 int extdatalen = 0;
1012 uint8_t *ret = buf;
1013 uint8_t *orig = buf;
1014 /* See if we support any ECC ciphersuites */
1015 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1018 size_t i;
David Benjamin107db582015-04-08 00:41:59 -04001019 uint32_t alg_k, alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1023 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 alg_k = c->algorithm_mkey;
1026 alg_a = c->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001027 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 using_ecc = 1;
1029 break;
1030 }
1031 }
1032 }
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1035 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
1036 return orig;
1037 }
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 if (ret >= limit) {
1042 return NULL; /* should never occur. */
1043 }
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langley614c66a2015-06-12 15:26:58 -07001045 OPENSSL_COMPILE_ASSERT(
1046 kNumExtensions <= sizeof(s->s3->tmp.extensions.sent) * 8,
1047 too_many_extensions_for_bitset);
1048 s->s3->tmp.extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langley614c66a2015-06-12 15:26:58 -07001050 size_t i;
1051 for (i = 0; i < kNumExtensions; i++) {
1052 if (kExtensions[i].init != NULL) {
1053 kExtensions[i].init(s);
1054 }
1055 }
Adam Langley95c29f32014-06-20 12:00:00 -07001056
Adam Langley614c66a2015-06-12 15:26:58 -07001057 CBB cbb;
1058 if (!CBB_init_fixed(&cbb, ret, limit - ret)) {
1059 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1060 return NULL;
1061 }
1062
1063 for (i = 0; i < kNumExtensions; i++) {
1064 const size_t space_before = CBB_len(&cbb);
1065 if (!kExtensions[i].add_clienthello(s, &cbb)) {
1066 CBB_cleanup(&cbb);
1067 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 return NULL;
1069 }
Adam Langley614c66a2015-06-12 15:26:58 -07001070 const size_t space_after = CBB_len(&cbb);
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langley614c66a2015-06-12 15:26:58 -07001072 if (space_after != space_before) {
1073 s->s3->tmp.extensions.sent |= (1u << i);
1074 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 }
Adam Langley75712922014-10-10 16:23:43 -07001076
Adam Langley614c66a2015-06-12 15:26:58 -07001077 ret = limit - CBB_len(&cbb);
1078 CBB_cleanup(&cbb);
1079
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 /* Add RI if renegotiating */
David Benjamine6df0542015-05-12 22:02:08 -04001081 if (s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 int el;
David Benjamind1681e62014-11-20 14:54:14 -05001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1085 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1086 return NULL;
1087 }
Adam Langley95c29f32014-06-20 12:00:00 -07001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 if ((limit - ret - 4 - el) < 0) {
1090 return NULL;
1091 }
Adam Langley95c29f32014-06-20 12:00:00 -07001092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 s2n(TLSEXT_TYPE_renegotiate, ret);
1094 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1097 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1098 return NULL;
1099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 ret += el;
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 /* Add extended master secret. */
1105 if (s->version != SSL3_VERSION) {
1106 if (limit - ret - 4 < 0) {
1107 return NULL;
1108 }
1109 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1110 s2n(0, ret);
1111 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001112
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1114 int ticklen = 0;
David Benjamin4b27d9f2015-05-12 22:42:52 -04001115 /* Renegotiation does not participate in session resumption. However, still
1116 * advertise the extension to avoid potentially breaking servers which carry
1117 * over the state from the previous handshake, such as OpenSSL servers
1118 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1119 if (!s->s3->initial_handshake_complete && s->session != NULL &&
1120 s->session->tlsext_tick != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001121 ticklen = s->session->tlsext_ticklen;
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 /* Check for enough room 2 for extension type, 2 for len rest for
1125 * ticket. */
1126 if ((long)(limit - ret - 4 - ticklen) < 0) {
1127 return NULL;
1128 }
1129 s2n(TLSEXT_TYPE_session_ticket, ret);
1130 s2n(ticklen, ret);
1131 if (ticklen) {
1132 memcpy(ret, s->session->tlsext_tick, ticklen);
1133 ret += ticklen;
1134 }
1135 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001136
David Benjamin6ae7f072015-01-26 10:22:13 -05001137 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 size_t salglen;
1139 const uint8_t *salg;
1140 salglen = tls12_get_psigalgs(s, &salg);
1141 if ((size_t)(limit - ret) < salglen + 6) {
1142 return NULL;
1143 }
1144 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1145 s2n(salglen + 2, ret);
1146 s2n(salglen, ret);
1147 memcpy(ret, salg, salglen);
1148 ret += salglen;
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 if (s->ocsp_stapling_enabled) {
1152 /* The status_request extension is excessively extensible at every layer.
1153 * On the client, only support requesting OCSP responses with an empty
1154 * responder_id_list and no extensions. */
1155 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
1156 return NULL;
1157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 s2n(TLSEXT_TYPE_status_request, ret);
1160 s2n(1 + 2 + 2, ret);
1161 /* status_type */
1162 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1163 /* responder_id_list - empty */
1164 s2n(0, ret);
1165 /* request_extensions - empty */
1166 s2n(0, ret);
1167 }
Adam Langley95c29f32014-06-20 12:00:00 -07001168
David Benjamine6df0542015-05-12 22:02:08 -04001169 if (s->ctx->next_proto_select_cb && !s->s3->initial_handshake_complete &&
David Benjamin78e69782014-12-19 04:52:17 -05001170 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 /* The client advertises an emtpy extension to indicate its support for
1172 * Next Protocol Negotiation */
1173 if (limit - ret - 4 < 0) {
1174 return NULL;
1175 }
1176 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1177 s2n(0, ret);
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179
David Benjamine6df0542015-05-12 22:02:08 -04001180 if (s->signed_cert_timestamps_enabled) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 /* The client advertises an empty extension to indicate its support for
1182 * certificate timestamps. */
1183 if (limit - ret - 4 < 0) {
1184 return NULL;
1185 }
1186 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
1187 s2n(0, ret);
1188 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001189
David Benjamine6df0542015-05-12 22:02:08 -04001190 if (s->alpn_client_proto_list && !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
1192 return NULL;
1193 }
1194 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1195 s2n(2 + s->alpn_client_proto_list_len, ret);
1196 s2n(s->alpn_client_proto_list_len, ret);
1197 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1198 ret += s->alpn_client_proto_list_len;
1199 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001200
David Benjamin78e69782014-12-19 04:52:17 -05001201 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001202 /* The client advertises an emtpy extension to indicate its support for
1203 * Channel ID. */
1204 if (limit - ret - 4 < 0) {
1205 return NULL;
1206 }
1207 if (s->ctx->tlsext_channel_id_enabled_new) {
1208 s2n(TLSEXT_TYPE_channel_id_new, ret);
1209 } else {
1210 s2n(TLSEXT_TYPE_channel_id, ret);
1211 }
1212 s2n(0, ret);
1213 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001214
Adam Langleyfcf25832014-12-18 17:42:32 -08001215 if (SSL_get_srtp_profiles(s)) {
1216 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -07001217
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -07001219
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 if ((limit - ret - 4 - el) < 0) {
1221 return NULL;
1222 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 s2n(TLSEXT_TYPE_use_srtp, ret);
1225 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001226
Adam Langleyfcf25832014-12-18 17:42:32 -08001227 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1228 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1229 return NULL;
1230 }
1231 ret += el;
1232 }
Adam Langley95c29f32014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 if (using_ecc) {
1235 /* Add TLS extension ECPointFormats to the ClientHello message */
1236 long lenmax;
1237 const uint8_t *formats;
1238 const uint16_t *curves;
Adam Langley614c66a2015-06-12 15:26:58 -07001239 size_t formats_len, curves_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001240
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001242
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 lenmax = limit - ret - 5;
1244 if (lenmax < 0) {
1245 return NULL;
1246 }
1247 if (formats_len > (size_t)lenmax) {
1248 return NULL;
1249 }
1250 if (formats_len > 255) {
1251 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1252 return NULL;
1253 }
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1256 s2n(formats_len + 1, ret);
1257 *(ret++) = (uint8_t)formats_len;
1258 memcpy(ret, formats, formats_len);
1259 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 /* Add TLS extension EllipticCurves to the ClientHello message */
1262 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001263
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 lenmax = limit - ret - 6;
1265 if (lenmax < 0) {
1266 return NULL;
1267 }
1268 if (curves_len * 2 > (size_t)lenmax) {
1269 return NULL;
1270 }
1271 if (curves_len * 2 > 65532) {
1272 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1273 return NULL;
1274 }
Adam Langley95c29f32014-06-20 12:00:00 -07001275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1277 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 s2n(curves_len * 2, ret);
1280 for (i = 0; i < curves_len; i++) {
1281 s2n(curves[i], ret);
1282 }
1283 }
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 if (header_len > 0) {
1286 size_t clienthello_minsize = 0;
1287 header_len += ret - orig;
1288 if (header_len > 0xff && header_len < 0x200) {
1289 /* Add padding to workaround bugs in F5 terminators. See
1290 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1291 *
1292 * NB: because this code works out the length of all existing extensions
1293 * it MUST always appear last. */
1294 clienthello_minsize = 0x200;
1295 }
1296 if (s->fastradio_padding) {
1297 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1298 * into DCH (high data rate) state in 3G networks. Note that when
1299 * fastradio_padding is enabled, even if the header_len is less than 255
1300 * bytes, the padding will be applied regardless. This is slightly
1301 * different from the TLS padding extension suggested in
1302 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1303 clienthello_minsize = 0x400;
1304 }
1305 if (header_len < clienthello_minsize) {
1306 size_t padding_len = clienthello_minsize - header_len;
1307 /* Extensions take at least four bytes to encode. Always include least
1308 * one byte of data if including the extension. WebSphere Application
1309 * Server 7.0 is intolerant to the last extension being zero-length. */
1310 if (padding_len >= 4 + 1) {
1311 padding_len -= 4;
1312 } else {
1313 padding_len = 1;
1314 }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 if (limit - ret - 4 - (long)padding_len < 0) {
1317 return NULL;
1318 }
Adam Langley75712922014-10-10 16:23:43 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 s2n(TLSEXT_TYPE_padding, ret);
1321 s2n(padding_len, ret);
1322 memset(ret, 0, padding_len);
1323 ret += padding_len;
1324 }
1325 }
Adam Langley75712922014-10-10 16:23:43 -07001326
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 extdatalen = ret - orig - 2;
1328 if (extdatalen == 0) {
1329 return orig;
1330 }
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 s2n(extdatalen, orig);
1333 return ret;
1334}
Adam Langley95c29f32014-06-20 12:00:00 -07001335
Adam Langley614c66a2015-06-12 15:26:58 -07001336uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
1337 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 int extdatalen = 0;
1339 uint8_t *orig = buf;
1340 uint8_t *ret = buf;
1341 int next_proto_neg_seen;
David Benjamin107db582015-04-08 00:41:59 -04001342 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1343 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001344 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001346
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1348 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1349 return orig;
1350 }
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 ret += 2;
1353 if (ret >= limit) {
1354 return NULL; /* should never happen. */
1355 }
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langley614c66a2015-06-12 15:26:58 -07001357 CBB cbb;
1358 if (!CBB_init_fixed(&cbb, ret, limit - ret)) {
1359 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1360 return NULL;
1361 }
1362
1363 unsigned i;
1364 for (i = 0; i < kNumExtensions; i++) {
1365 if (!(s->s3->tmp.extensions.received & (1u << i))) {
1366 /* Don't send extensions that were not received. */
1367 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 }
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langley614c66a2015-06-12 15:26:58 -07001370 if (!kExtensions[i].add_serverhello(s, &cbb)) {
1371 CBB_cleanup(&cbb);
1372 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1373 return NULL;
1374 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 }
Adam Langley95c29f32014-06-20 12:00:00 -07001376
Adam Langley614c66a2015-06-12 15:26:58 -07001377 ret = limit - CBB_len(&cbb);
1378 CBB_cleanup(&cbb);
1379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 if (s->s3->send_connection_binding) {
1381 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1384 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1385 return NULL;
1386 }
Adam Langley95c29f32014-06-20 12:00:00 -07001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 if ((limit - ret - 4 - el) < 0) {
1389 return NULL;
1390 }
Adam Langley95c29f32014-06-20 12:00:00 -07001391
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 s2n(TLSEXT_TYPE_renegotiate, ret);
1393 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001394
Adam Langleyfcf25832014-12-18 17:42:32 -08001395 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1396 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1397 return NULL;
1398 }
Adam Langley95c29f32014-06-20 12:00:00 -07001399
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 ret += el;
1401 }
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 if (s->s3->tmp.extended_master_secret) {
1404 if ((long)(limit - ret - 4) < 0) {
1405 return NULL;
1406 }
Adam Langley95c29f32014-06-20 12:00:00 -07001407
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1409 s2n(0, ret);
1410 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001411
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 if (using_ecc) {
1413 const uint8_t *plist;
1414 size_t plistlen;
1415 /* Add TLS extension ECPointFormats to the ServerHello message */
1416 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001417
Adam Langleyfcf25832014-12-18 17:42:32 -08001418 tls1_get_formatlist(s, &plist, &plistlen);
1419
1420 lenmax = limit - ret - 5;
1421 if (lenmax < 0) {
1422 return NULL;
1423 }
1424 if (plistlen > (size_t)lenmax) {
1425 return NULL;
1426 }
1427 if (plistlen > 255) {
1428 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1429 return NULL;
1430 }
1431
1432 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1433 s2n(plistlen + 1, ret);
1434 *(ret++) = (uint8_t)plistlen;
1435 memcpy(ret, plist, plistlen);
1436 ret += plistlen;
1437 }
1438 /* Currently the server should not respond with a SupportedCurves extension */
1439
1440 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1441 if ((long)(limit - ret - 4) < 0) {
1442 return NULL;
1443 }
1444 s2n(TLSEXT_TYPE_session_ticket, ret);
1445 s2n(0, ret);
1446 }
1447
1448 if (s->s3->tmp.certificate_status_expected) {
1449 if ((long)(limit - ret - 4) < 0) {
1450 return NULL;
1451 }
1452 s2n(TLSEXT_TYPE_status_request, ret);
1453 s2n(0, ret);
1454 }
1455
1456 if (s->srtp_profile) {
1457 int el;
1458
1459 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1460
1461 if ((limit - ret - 4 - el) < 0) {
1462 return NULL;
1463 }
1464
1465 s2n(TLSEXT_TYPE_use_srtp, ret);
1466 s2n(el, ret);
1467
1468 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1469 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1470 return NULL;
1471 }
1472 ret += el;
1473 }
1474
1475 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1476 s->s3->next_proto_neg_seen = 0;
1477 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1478 const uint8_t *npa;
1479 unsigned int npalen;
1480 int r;
1481
1482 r = s->ctx->next_protos_advertised_cb(
1483 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1484 if (r == SSL_TLSEXT_ERR_OK) {
1485 if ((long)(limit - ret - 4 - npalen) < 0) {
1486 return NULL;
1487 }
1488 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1489 s2n(npalen, ret);
1490 memcpy(ret, npa, npalen);
1491 ret += npalen;
1492 s->s3->next_proto_neg_seen = 1;
1493 }
1494 }
1495
1496 if (s->s3->alpn_selected) {
1497 const uint8_t *selected = s->s3->alpn_selected;
1498 size_t len = s->s3->alpn_selected_len;
1499
1500 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1501 return NULL;
1502 }
1503 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1504 s2n(3 + len, ret);
1505 s2n(1 + len, ret);
1506 *ret++ = len;
1507 memcpy(ret, selected, len);
1508 ret += len;
1509 }
1510
1511 /* If the client advertised support for Channel ID, and we have it
1512 * enabled, then we want to echo it back. */
1513 if (s->s3->tlsext_channel_id_valid) {
1514 if (limit - ret - 4 < 0) {
1515 return NULL;
1516 }
1517 if (s->s3->tlsext_channel_id_new) {
1518 s2n(TLSEXT_TYPE_channel_id_new, ret);
1519 } else {
1520 s2n(TLSEXT_TYPE_channel_id, ret);
1521 }
1522 s2n(0, ret);
1523 }
1524
1525 extdatalen = ret - orig - 2;
1526 if (extdatalen == 0) {
1527 return orig;
1528 }
1529
1530 s2n(extdatalen, orig);
1531 return ret;
1532}
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langley95c29f32014-06-20 12:00:00 -07001534/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1535 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001536 * cbs: the contents of the extension, not including the type and length.
1537 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001538 * return.
1539 *
David Benjamindc72ff72014-06-25 12:36:10 -04001540 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001541static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1542 CBS protocol_name_list, protocol_name_list_copy;
1543 const uint8_t *selected;
1544 uint8_t selected_len;
1545 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 if (s->ctx->alpn_select_cb == NULL) {
1548 return 1;
1549 }
Adam Langley95c29f32014-06-20 12:00:00 -07001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1552 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1553 goto parse_error;
1554 }
Adam Langley95c29f32014-06-20 12:00:00 -07001555
Adam Langleyfcf25832014-12-18 17:42:32 -08001556 /* Validate the protocol list. */
1557 protocol_name_list_copy = protocol_name_list;
1558 while (CBS_len(&protocol_name_list_copy) > 0) {
1559 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1562 goto parse_error;
1563 }
1564 }
Adam Langley95c29f32014-06-20 12:00:00 -07001565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 r = s->ctx->alpn_select_cb(
1567 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1568 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1569 if (r == SSL_TLSEXT_ERR_OK) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001570 OPENSSL_free(s->s3->alpn_selected);
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1572 if (!s->s3->alpn_selected) {
1573 *out_alert = SSL_AD_INTERNAL_ERROR;
1574 return 0;
1575 }
1576 s->s3->alpn_selected_len = selected_len;
1577 }
1578
1579 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001580
1581parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 *out_alert = SSL_AD_DECODE_ERROR;
1583 return 0;
1584}
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1587 int renegotiate_seen = 0;
1588 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001589
David Benjamind1d7d3d2015-01-11 20:30:21 -05001590 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001591 s->s3->next_proto_neg_seen = 0;
1592 s->s3->tmp.certificate_status_expected = 0;
1593 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001594
David Benjamin2755a3e2015-04-22 16:17:58 -04001595 OPENSSL_free(s->s3->alpn_selected);
1596 s->s3->alpn_selected = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 /* Clear any signature algorithms extension received */
David Benjamin2755a3e2015-04-22 16:17:58 -04001599 OPENSSL_free(s->cert->peer_sigalgs);
1600 s->cert->peer_sigalgs = NULL;
1601 s->cert->peer_sigalgslen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001602
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 /* Clear any shared signature algorithms */
David Benjamin2755a3e2015-04-22 16:17:58 -04001604 OPENSSL_free(s->cert->shared_sigalgs);
1605 s->cert->shared_sigalgs = NULL;
1606 s->cert->shared_sigalgslen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001607
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 /* Clear ECC extensions */
David Benjamin2755a3e2015-04-22 16:17:58 -04001609 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1610 s->s3->tmp.peer_ecpointformatlist = NULL;
1611 s->s3->tmp.peer_ecpointformatlist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001612
David Benjamin2755a3e2015-04-22 16:17:58 -04001613 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1614 s->s3->tmp.peer_ellipticcurvelist = NULL;
1615 s->s3->tmp.peer_ellipticcurvelist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001616
Adam Langley614c66a2015-06-12 15:26:58 -07001617 size_t i;
1618 for (i = 0; i < kNumExtensions; i++) {
1619 if (kExtensions[i].init != NULL) {
1620 kExtensions[i].init(s);
1621 }
1622 }
1623
1624 s->s3->tmp.extensions.received = 0;
1625
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 /* There may be no extensions. */
1627 if (CBS_len(cbs) == 0) {
Adam Langley614c66a2015-06-12 15:26:58 -07001628 goto no_extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 }
David Benjamindc72ff72014-06-25 12:36:10 -04001630
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 /* Decode the extensions block and check it is valid. */
1632 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1633 !tls1_check_duplicate_extensions(&extensions)) {
1634 *out_alert = SSL_AD_DECODE_ERROR;
1635 return 0;
1636 }
David Benjamindc72ff72014-06-25 12:36:10 -04001637
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 while (CBS_len(&extensions) != 0) {
1639 uint16_t type;
1640 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001641
Adam Langleyfcf25832014-12-18 17:42:32 -08001642 /* Decode the next extension. */
1643 if (!CBS_get_u16(&extensions, &type) ||
1644 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1645 *out_alert = SSL_AD_DECODE_ERROR;
1646 return 0;
1647 }
Adam Langley95c29f32014-06-20 12:00:00 -07001648
Adam Langley614c66a2015-06-12 15:26:58 -07001649 OPENSSL_COMPILE_ASSERT(
1650 kNumExtensions <= sizeof(s->s3->tmp.extensions.received) * 8,
1651 too_many_extensions_for_bitset);
David Benjamindc72ff72014-06-25 12:36:10 -04001652
Adam Langley614c66a2015-06-12 15:26:58 -07001653 unsigned ext_index;
1654 const struct tls_extension *const ext =
1655 tls_extension_find(&ext_index, type);
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langley614c66a2015-06-12 15:26:58 -07001657 if (ext != NULL) {
1658 s->s3->tmp.extensions.received |= (1u << ext_index);
1659 uint8_t alert = SSL_AD_DECODE_ERROR;
1660 if (!ext->parse_clienthello(s, &alert, &extension)) {
1661 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 return 0;
1663 }
David Benjamindc72ff72014-06-25 12:36:10 -04001664
Adam Langley614c66a2015-06-12 15:26:58 -07001665 continue;
1666 }
David Benjamindc72ff72014-06-25 12:36:10 -04001667
Adam Langley614c66a2015-06-12 15:26:58 -07001668 if (type == TLSEXT_TYPE_ec_point_formats) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1672 CBS_len(&extension) != 0) {
1673 *out_alert = SSL_AD_DECODE_ERROR;
1674 return 0;
1675 }
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1678 &s->s3->tmp.peer_ecpointformatlist_length)) {
1679 *out_alert = SSL_AD_INTERNAL_ERROR;
1680 return 0;
1681 }
1682 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1683 CBS elliptic_curve_list;
Adam Langley614c66a2015-06-12 15:26:58 -07001684 size_t num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1687 CBS_len(&elliptic_curve_list) == 0 ||
1688 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1689 CBS_len(&extension) != 0) {
1690 *out_alert = SSL_AD_DECODE_ERROR;
1691 return 0;
1692 }
David Benjamindc72ff72014-06-25 12:36:10 -04001693
David Benjamin2755a3e2015-04-22 16:17:58 -04001694 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1695 s->s3->tmp.peer_ellipticcurvelist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001696
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 s->s3->tmp.peer_ellipticcurvelist =
1698 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1701 *out_alert = SSL_AD_INTERNAL_ERROR;
1702 return 0;
1703 }
Adam Langley95c29f32014-06-20 12:00:00 -07001704
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 num_curves = CBS_len(&elliptic_curve_list) / 2;
1706 for (i = 0; i < num_curves; i++) {
1707 if (!CBS_get_u16(&elliptic_curve_list,
1708 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1709 *out_alert = SSL_AD_INTERNAL_ERROR;
1710 return 0;
1711 }
1712 }
David Benjamindc72ff72014-06-25 12:36:10 -04001713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 if (CBS_len(&elliptic_curve_list) != 0) {
1715 *out_alert = SSL_AD_INTERNAL_ERROR;
1716 return 0;
1717 }
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1720 } else if (type == TLSEXT_TYPE_renegotiate) {
1721 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1722 return 0;
1723 }
1724 renegotiate_seen = 1;
1725 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1726 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 if (!CBS_get_u16_length_prefixed(&extension,
1729 &supported_signature_algorithms) ||
1730 CBS_len(&extension) != 0) {
1731 *out_alert = SSL_AD_DECODE_ERROR;
1732 return 0;
1733 }
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 /* Ensure the signature algorithms are non-empty. It contains a list of
1736 * SignatureAndHashAlgorithms which are two bytes each. */
1737 if (CBS_len(&supported_signature_algorithms) == 0 ||
1738 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1739 *out_alert = SSL_AD_DECODE_ERROR;
1740 return 0;
1741 }
David Benjamindc72ff72014-06-25 12:36:10 -04001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1744 *out_alert = SSL_AD_DECODE_ERROR;
1745 return 0;
1746 }
1747 /* If sigalgs received and no shared algorithms fatal error. */
1748 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001749 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1751 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1752 return 0;
1753 }
1754 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamine6df0542015-05-12 22:02:08 -04001755 !s->s3->initial_handshake_complete &&
1756 s->s3->alpn_selected == NULL && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 /* The extension must be empty. */
1758 if (CBS_len(&extension) != 0) {
1759 *out_alert = SSL_AD_DECODE_ERROR;
1760 return 0;
1761 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 s->s3->next_proto_neg_seen = 1;
1763 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
David Benjamine6df0542015-05-12 22:02:08 -04001764 s->ctx->alpn_select_cb && !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1766 return 0;
1767 }
1768 /* ALPN takes precedence over NPN. */
1769 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001770 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1771 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 /* The extension must be empty. */
1773 if (CBS_len(&extension) != 0) {
1774 *out_alert = SSL_AD_DECODE_ERROR;
1775 return 0;
1776 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 s->s3->tlsext_channel_id_valid = 1;
1779 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001780 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 /* The extension must be empty. */
1782 if (CBS_len(&extension) != 0) {
1783 *out_alert = SSL_AD_DECODE_ERROR;
1784 return 0;
1785 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 s->s3->tlsext_channel_id_valid = 1;
1788 s->s3->tlsext_channel_id_new = 1;
1789 } else if (type == TLSEXT_TYPE_use_srtp) {
1790 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1791 return 0;
1792 }
1793 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1794 s->version != SSL3_VERSION) {
1795 if (CBS_len(&extension) != 0) {
1796 *out_alert = SSL_AD_DECODE_ERROR;
1797 return 0;
1798 }
Adam Langley75712922014-10-10 16:23:43 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 s->s3->tmp.extended_master_secret = 1;
1801 }
1802 }
Adam Langley75712922014-10-10 16:23:43 -07001803
Adam Langley614c66a2015-06-12 15:26:58 -07001804no_extensions:
1805 for (i = 0; i < kNumExtensions; i++) {
1806 if (!(s->s3->tmp.extensions.received & (1u << i))) {
1807 /* Extension wasn't observed so call the callback with a NULL
1808 * parameter. */
1809 uint8_t alert = SSL_AD_DECODE_ERROR;
1810 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
1811 *out_alert = alert;
1812 return 0;
1813 }
1814 }
1815 }
1816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001818
David Benjamin20f6e972015-05-15 21:51:49 -04001819 if (!renegotiate_seen && s->s3->initial_handshake_complete &&
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1821 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1822 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1823 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1824 return 0;
1825 }
Adam Langley95c29f32014-06-20 12:00:00 -07001826
Adam Langleyfcf25832014-12-18 17:42:32 -08001827 return 1;
1828}
Adam Langley95c29f32014-06-20 12:00:00 -07001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1831 int alert = -1;
1832 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1833 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1834 return 0;
1835 }
Adam Langley95c29f32014-06-20 12:00:00 -07001836
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 if (ssl_check_clienthello_tlsext(s) <= 0) {
1838 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1839 SSL_R_CLIENTHELLO_TLSEXT);
1840 return 0;
1841 }
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 return 1;
1844}
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langley95c29f32014-06-20 12:00:00 -07001846/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 * elements of zero length are allowed and the set of elements must exactly
1848 * fill the length of the block. */
1849static char ssl_next_proto_validate(const CBS *cbs) {
1850 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 while (CBS_len(&copy) != 0) {
1853 CBS proto;
1854 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1855 return 0;
1856 }
1857 }
Adam Langley95c29f32014-06-20 12:00:00 -07001858
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 return 1;
1860}
Adam Langley95c29f32014-06-20 12:00:00 -07001861
Adam Langleyfcf25832014-12-18 17:42:32 -08001862static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 int renegotiate_seen = 0;
1864 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001865
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 /* TODO(davidben): Move all of these to some per-handshake state that gets
1867 * systematically reset on a new handshake; perhaps allocate it fresh each
1868 * time so it's not even kept around post-handshake. */
1869 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 s->tlsext_ticket_expected = 0;
1871 s->s3->tmp.certificate_status_expected = 0;
1872 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001873 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001874
David Benjamin2755a3e2015-04-22 16:17:58 -04001875 OPENSSL_free(s->s3->alpn_selected);
1876 s->s3->alpn_selected = NULL;
David Benjamina19fc252014-10-19 00:14:36 -04001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 /* Clear ECC extensions */
David Benjamin2755a3e2015-04-22 16:17:58 -04001879 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1880 s->s3->tmp.peer_ecpointformatlist = NULL;
1881 s->s3->tmp.peer_ecpointformatlist_length = 0;
David Benjamin03973092014-06-24 23:27:17 -04001882
Adam Langley614c66a2015-06-12 15:26:58 -07001883 uint32_t received = 0;
1884 size_t i;
1885
1886 OPENSSL_COMPILE_ASSERT(
1887 kNumExtensions <= sizeof(received) * 8,
1888 too_many_extensions_for_bitset);
1889
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 /* There may be no extensions. */
1891 if (CBS_len(cbs) == 0) {
Adam Langley614c66a2015-06-12 15:26:58 -07001892 goto no_extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -08001893 }
Adam Langley95c29f32014-06-20 12:00:00 -07001894
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 /* Decode the extensions block and check it is valid. */
1896 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1897 !tls1_check_duplicate_extensions(&extensions)) {
1898 *out_alert = SSL_AD_DECODE_ERROR;
1899 return 0;
1900 }
Adam Langley95c29f32014-06-20 12:00:00 -07001901
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 while (CBS_len(&extensions) != 0) {
1903 uint16_t type;
1904 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 /* Decode the next extension. */
1907 if (!CBS_get_u16(&extensions, &type) ||
1908 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1909 *out_alert = SSL_AD_DECODE_ERROR;
1910 return 0;
1911 }
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langley614c66a2015-06-12 15:26:58 -07001913 unsigned ext_index;
1914 const struct tls_extension *const ext =
1915 tls_extension_find(&ext_index, type);
1916
1917 /* While we have extensions that don't use tls_extension this conditional
1918 * needs to be guarded on |ext != NULL|. In the future, ext being NULL will
1919 * be fatal. */
1920 if (ext != NULL) {
1921 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
1922 /* Received an extension that was never sent. */
1923 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1924 SSL_R_UNEXPECTED_EXTENSION);
1925 ERR_add_error_dataf("ext:%u", (unsigned) type);
Adam Langleyfcf25832014-12-18 17:42:32 -08001926 *out_alert = SSL_AD_DECODE_ERROR;
1927 return 0;
1928 }
David Benjamin03973092014-06-24 23:27:17 -04001929
Adam Langley614c66a2015-06-12 15:26:58 -07001930 received |= (1u << ext_index);
1931
1932 uint8_t alert = SSL_AD_DECODE_ERROR;
1933 if (!ext->parse_serverhello(s, &alert, &extension)) {
1934 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 return 0;
1936 }
David Benjamin03973092014-06-24 23:27:17 -04001937
Adam Langley614c66a2015-06-12 15:26:58 -07001938 continue;
1939 }
1940
1941 if (type == TLSEXT_TYPE_ec_point_formats) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1945 CBS_len(&extension) != 0) {
1946 *out_alert = SSL_AD_DECODE_ERROR;
1947 return 0;
1948 }
Adam Langley95c29f32014-06-20 12:00:00 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1951 &s->s3->tmp.peer_ecpointformatlist_length)) {
1952 *out_alert = SSL_AD_INTERNAL_ERROR;
1953 return 0;
1954 }
1955 } else if (type == TLSEXT_TYPE_session_ticket) {
1956 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1957 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1958 return 0;
1959 }
David Benjamin03973092014-06-24 23:27:17 -04001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961 s->tlsext_ticket_expected = 1;
1962 } else if (type == TLSEXT_TYPE_status_request) {
1963 /* The extension MUST be empty and may only sent if we've requested a
1964 * status request message. */
1965 if (CBS_len(&extension) != 0) {
1966 *out_alert = SSL_AD_DECODE_ERROR;
1967 return 0;
1968 }
David Benjamin03973092014-06-24 23:27:17 -04001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 if (!s->ocsp_stapling_enabled) {
1971 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1972 return 0;
1973 }
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 /* Set a flag to expect a CertificateStatus message */
1976 s->s3->tmp.certificate_status_expected = 1;
1977 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamine6df0542015-05-12 22:02:08 -04001978 !s->s3->initial_handshake_complete && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 uint8_t *selected;
1980 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982 /* We must have requested it. */
1983 if (s->ctx->next_proto_select_cb == NULL) {
1984 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1985 return 0;
1986 }
David Benjamin03973092014-06-24 23:27:17 -04001987
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 /* The data must be valid. */
1989 if (!ssl_next_proto_validate(&extension)) {
1990 *out_alert = SSL_AD_DECODE_ERROR;
1991 return 0;
1992 }
Adam Langley95c29f32014-06-20 12:00:00 -07001993
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 if (s->ctx->next_proto_select_cb(
1995 s, &selected, &selected_len, CBS_data(&extension),
1996 CBS_len(&extension),
1997 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1998 *out_alert = SSL_AD_INTERNAL_ERROR;
1999 return 0;
2000 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2003 if (s->next_proto_negotiated == NULL) {
2004 *out_alert = SSL_AD_INTERNAL_ERROR;
2005 return 0;
2006 }
Adam Langley75712922014-10-10 16:23:43 -07002007
Adam Langleyfcf25832014-12-18 17:42:32 -08002008 s->next_proto_negotiated_len = selected_len;
2009 s->s3->next_proto_neg_seen = 1;
David Benjamine6df0542015-05-12 22:02:08 -04002010 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2011 !s->s3->initial_handshake_complete) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 /* We must have requested it. */
2015 if (s->alpn_client_proto_list == NULL) {
2016 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2017 return 0;
2018 }
Adam Langley95c29f32014-06-20 12:00:00 -07002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 /* The extension data consists of a ProtocolNameList which must have
2021 * exactly one ProtocolName. Each of these is length-prefixed. */
2022 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2023 CBS_len(&extension) != 0 ||
2024 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2025 CBS_len(&protocol_name_list) != 0) {
2026 *out_alert = SSL_AD_DECODE_ERROR;
2027 return 0;
2028 }
Adam Langley95c29f32014-06-20 12:00:00 -07002029
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
2031 &s->s3->alpn_selected_len)) {
2032 *out_alert = SSL_AD_INTERNAL_ERROR;
2033 return 0;
2034 }
David Benjamin78e69782014-12-19 04:52:17 -05002035 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002036 if (CBS_len(&extension) != 0) {
2037 *out_alert = SSL_AD_DECODE_ERROR;
2038 return 0;
2039 }
Adam Langley95c29f32014-06-20 12:00:00 -07002040
Adam Langleyfcf25832014-12-18 17:42:32 -08002041 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05002042 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 if (CBS_len(&extension) != 0) {
2044 *out_alert = SSL_AD_DECODE_ERROR;
2045 return 0;
2046 }
Adam Langley95c29f32014-06-20 12:00:00 -07002047
Adam Langleyfcf25832014-12-18 17:42:32 -08002048 s->s3->tlsext_channel_id_valid = 1;
2049 s->s3->tlsext_channel_id_new = 1;
2050 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
2051 if (CBS_len(&extension) == 0) {
2052 *out_alert = SSL_AD_DECODE_ERROR;
2053 return 0;
2054 }
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 /* Session resumption uses the original session information. */
2057 if (!s->hit &&
2058 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
2059 &s->session->tlsext_signed_cert_timestamp_list_length)) {
2060 *out_alert = SSL_AD_INTERNAL_ERROR;
2061 return 0;
2062 }
2063 } else if (type == TLSEXT_TYPE_renegotiate) {
2064 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
2065 return 0;
2066 }
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 renegotiate_seen = 1;
2069 } else if (type == TLSEXT_TYPE_use_srtp) {
2070 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
2071 return 0;
2072 }
2073 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2074 if (/* It is invalid for the server to select EMS and
2075 SSLv3. */
2076 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
2077 *out_alert = SSL_AD_DECODE_ERROR;
2078 return 0;
2079 }
Adam Langley95c29f32014-06-20 12:00:00 -07002080
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 s->s3->tmp.extended_master_secret = 1;
2082 }
2083 }
Adam Langley95c29f32014-06-20 12:00:00 -07002084
Adam Langley614c66a2015-06-12 15:26:58 -07002085no_extensions:
2086 for (i = 0; i < kNumExtensions; i++) {
2087 if (!(received & (1u << i))) {
2088 /* Extension wasn't observed so call the callback with a NULL
2089 * parameter. */
2090 uint8_t alert = SSL_AD_DECODE_ERROR;
2091 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
2092 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 return 0;
2094 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 }
2096 }
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
2099 * attack we should *always* see RI even on initial server hello because the
2100 * client doesn't see any renegotiation during an attack. However this would
2101 * mean we could not connect to any server which doesn't support RI so for
2102 * the immediate future tolerate RI absence on initial connect only. */
2103 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
2104 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2105 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2106 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
2107 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2108 return 0;
2109 }
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 return 1;
2112}
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07002117
Adam Langleyfcf25832014-12-18 17:42:32 -08002118static int ssl_check_clienthello_tlsext(SSL *s) {
2119 int ret = SSL_TLSEXT_ERR_NOACK;
2120 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002121
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2123 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2126 ret = s->ctx->tlsext_servername_callback(s, &al,
2127 s->ctx->tlsext_servername_arg);
2128 } else if (s->initial_ctx != NULL &&
2129 s->initial_ctx->tlsext_servername_callback != 0) {
2130 ret = s->initial_ctx->tlsext_servername_callback(
2131 s, &al, s->initial_ctx->tlsext_servername_arg);
2132 }
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 switch (ret) {
2135 case SSL_TLSEXT_ERR_ALERT_FATAL:
2136 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2137 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 case SSL_TLSEXT_ERR_ALERT_WARNING:
2140 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2141 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002144 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002145 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 default:
2148 return 1;
2149 }
2150}
Adam Langleyed8270a2014-09-02 13:52:56 -07002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152static int ssl_check_serverhello_tlsext(SSL *s) {
2153 int ret = SSL_TLSEXT_ERR_NOACK;
2154 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002155
Adam Langleyfcf25832014-12-18 17:42:32 -08002156 /* If we are client and using an elliptic curve cryptography cipher suite,
2157 * then if server returns an EC point formats lists extension it must contain
2158 * uncompressed. */
David Benjamin107db582015-04-08 00:41:59 -04002159 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2160 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04002161 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08002162 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
2163 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
2164 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2165 return -1;
2166 }
2167 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002168
Adam Langleyfcf25832014-12-18 17:42:32 -08002169 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2170 ret = s->ctx->tlsext_servername_callback(s, &al,
2171 s->ctx->tlsext_servername_arg);
2172 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002173 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 ret = s->initial_ctx->tlsext_servername_callback(
2175 s, &al, s->initial_ctx->tlsext_servername_arg);
2176 }
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 switch (ret) {
2179 case SSL_TLSEXT_ERR_ALERT_FATAL:
2180 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2181 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 case SSL_TLSEXT_ERR_ALERT_WARNING:
2184 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2185 return 1;
2186
2187 default:
2188 return 1;
2189 }
2190}
2191
2192int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2193 int alert = -1;
2194 if (s->version < SSL3_VERSION) {
2195 return 1;
2196 }
2197
2198 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2199 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2200 return 0;
2201 }
2202
2203 if (ssl_check_serverhello_tlsext(s) <= 0) {
2204 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2205 SSL_R_SERVERHELLO_TLSEXT);
2206 return 0;
2207 }
2208
2209 return 1;
2210}
Adam Langley95c29f32014-06-20 12:00:00 -07002211
2212/* Since the server cache lookup is done early on in the processing of the
2213 * ClientHello, and other operations depend on the result, we need to handle
2214 * any TLS session ticket extension at the same time.
2215 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002216 * ctx: contains the early callback context, which is the result of a
2217 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002218 * ret: (output) on return, if a ticket was decrypted, then this is set to
2219 * point to the resulting session.
2220 *
Adam Langley95c29f32014-06-20 12:00:00 -07002221 * Returns:
2222 * -1: fatal error, either from parsing or decrypting the ticket.
2223 * 0: no ticket was found (or was ignored, based on settings).
2224 * 1: a zero length extension was found, indicating that the client supports
2225 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002226 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2227 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002228 * 3: a ticket was successfully decrypted and *ret was set.
2229 *
2230 * Side effects:
2231 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2232 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002233 * but the client either doesn't have a session ticket or we couldn't use
2234 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2235 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002236 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002237int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 SSL_SESSION **ret) {
2239 *ret = NULL;
2240 s->tlsext_ticket_expected = 0;
2241 const uint8_t *data;
2242 size_t len;
2243 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 /* If tickets disabled behave as if no ticket present to permit stateful
2246 * resumption. */
2247 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2248 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2249 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2250 &data, &len)) {
2251 return 0;
2252 }
2253
2254 if (len == 0) {
2255 /* The client will accept a ticket but doesn't currently have one. */
2256 s->tlsext_ticket_expected = 1;
2257 return 1;
2258 }
2259
2260 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2261 ret);
2262 switch (r) {
2263 case 2: /* ticket couldn't be decrypted */
2264 s->tlsext_ticket_expected = 1;
2265 return 2;
2266
2267 case 3: /* ticket was decrypted */
2268 return r;
2269
2270 case 4: /* ticket decrypted but need to renew */
2271 s->tlsext_ticket_expected = 1;
2272 return 3;
2273
2274 default: /* fatal error */
2275 return -1;
2276 }
2277}
Adam Langley95c29f32014-06-20 12:00:00 -07002278
2279/* tls_decrypt_ticket attempts to decrypt a session ticket.
2280 *
2281 * etick: points to the body of the session ticket extension.
2282 * eticklen: the length of the session tickets extenion.
2283 * sess_id: points at the session ID.
2284 * sesslen: the length of the session ID.
2285 * psess: (output) on return, if a ticket was decrypted, then this is set to
2286 * point to the resulting session.
2287 *
2288 * Returns:
2289 * -1: fatal error, either from parsing or decrypting the ticket.
2290 * 2: the ticket couldn't be decrypted.
2291 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 * 4: same as 3, but the ticket needs to be renewed. */
2293static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2294 const uint8_t *sess_id, int sesslen,
2295 SSL_SESSION **psess) {
2296 SSL_SESSION *sess;
2297 uint8_t *sdec;
2298 const uint8_t *p;
2299 int slen, mlen, renew_ticket = 0;
2300 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2301 HMAC_CTX hctx;
2302 EVP_CIPHER_CTX ctx;
2303 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002304
David Benjaminadcc3952015-04-26 13:07:57 -04002305 /* Ensure there is room for the key name and the largest IV
2306 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2307 * the maximum IV length should be well under the minimum size for the
2308 * session material and HMAC. */
2309 if (eticklen < 16 + EVP_MAX_IV_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 return 2;
2311 }
2312
2313 /* Initialize session ticket encryption and HMAC contexts */
2314 HMAC_CTX_init(&hctx);
2315 EVP_CIPHER_CTX_init(&ctx);
2316 if (tctx->tlsext_ticket_key_cb) {
2317 uint8_t *nctick = (uint8_t *)etick;
David Benjaminadcc3952015-04-26 13:07:57 -04002318 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx,
2319 0 /* decrypt */);
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 if (rv < 0) {
2321 return -1;
2322 }
2323 if (rv == 0) {
2324 return 2;
2325 }
2326 if (rv == 2) {
2327 renew_ticket = 1;
2328 }
2329 } else {
2330 /* Check key name matches */
2331 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2332 return 2;
2333 }
2334 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2335 NULL) ||
2336 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2337 tctx->tlsext_tick_aes_key, etick + 16)) {
2338 HMAC_CTX_cleanup(&hctx);
2339 EVP_CIPHER_CTX_cleanup(&ctx);
2340 return -1;
2341 }
2342 }
2343
David Benjaminadcc3952015-04-26 13:07:57 -04002344 /* First, check the MAC. The MAC is at the end of the ticket. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 mlen = HMAC_size(&hctx);
Adam Langley3457cd92015-05-05 12:04:01 -07002346 if ((size_t) eticklen < 16 + EVP_CIPHER_CTX_iv_length(&ctx) + 1 + mlen) {
David Benjaminadcc3952015-04-26 13:07:57 -04002347 /* The ticket must be large enough for key name, IV, data, and MAC. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 HMAC_CTX_cleanup(&hctx);
2349 EVP_CIPHER_CTX_cleanup(&ctx);
David Benjaminadcc3952015-04-26 13:07:57 -04002350 return 2;
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 }
2352 eticklen -= mlen;
2353 /* Check HMAC of encrypted ticket */
2354 HMAC_Update(&hctx, etick, eticklen);
2355 HMAC_Final(&hctx, tick_hmac, NULL);
2356 HMAC_CTX_cleanup(&hctx);
2357 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2358 EVP_CIPHER_CTX_cleanup(&ctx);
2359 return 2;
2360 }
2361
2362 /* Attempt to decrypt session data */
2363 /* Move p after IV to start of encrypted ticket, update length */
2364 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2365 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2366 sdec = OPENSSL_malloc(eticklen);
2367 if (!sdec) {
2368 EVP_CIPHER_CTX_cleanup(&ctx);
2369 return -1;
2370 }
2371 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2372 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2373 EVP_CIPHER_CTX_cleanup(&ctx);
2374 OPENSSL_free(sdec);
2375 return 2;
2376 }
2377 slen += mlen;
2378 EVP_CIPHER_CTX_cleanup(&ctx);
2379 p = sdec;
2380
David Benjaminfd67aa82015-06-15 19:41:48 -04002381 sess = SSL_SESSION_from_bytes(sdec, slen);
Adam Langleyfcf25832014-12-18 17:42:32 -08002382 OPENSSL_free(sdec);
2383 if (sess) {
2384 /* The session ID, if non-empty, is used by some clients to detect that the
2385 * ticket has been accepted. So we copy it to the session structure. If it
2386 * is empty set length to zero as required by standard. */
2387 if (sesslen) {
2388 memcpy(sess->session_id, sess_id, sesslen);
2389 }
2390 sess->session_id_length = sesslen;
2391 *psess = sess;
2392 if (renew_ticket) {
2393 return 4;
2394 }
2395 return 3;
2396 }
2397
2398 ERR_clear_error();
2399 /* For session parse failure, indicate that we need to send a new ticket. */
2400 return 2;
2401}
Adam Langley95c29f32014-06-20 12:00:00 -07002402
2403/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002404typedef struct {
2405 int nid;
2406 int id;
2407} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2410 {NID_sha1, TLSEXT_hash_sha1},
2411 {NID_sha224, TLSEXT_hash_sha224},
2412 {NID_sha256, TLSEXT_hash_sha256},
2413 {NID_sha384, TLSEXT_hash_sha384},
2414 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2417 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002418
Adam Langleyfcf25832014-12-18 17:42:32 -08002419static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2420 size_t i;
2421 for (i = 0; i < tlen; i++) {
2422 if (table[i].nid == nid) {
2423 return table[i].id;
2424 }
2425 }
Adam Langley95c29f32014-06-20 12:00:00 -07002426
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 return -1;
2428}
Adam Langley95c29f32014-06-20 12:00:00 -07002429
Adam Langleyfcf25832014-12-18 17:42:32 -08002430static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2431 size_t i;
2432 for (i = 0; i < tlen; i++) {
2433 if (table[i].id == id) {
2434 return table[i].nid;
2435 }
2436 }
Adam Langley95c29f32014-06-20 12:00:00 -07002437
Adam Langleyfcf25832014-12-18 17:42:32 -08002438 return NID_undef;
2439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
David Benjaminb4d65fd2015-05-29 17:11:21 -04002441int tls12_get_sigid(int pkey_type) {
2442 return tls12_find_id(pkey_type, tls12_sig,
2443 sizeof(tls12_sig) / sizeof(tls12_lookup));
2444}
2445
2446int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_PKEY *pk,
2447 const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 if (!md) {
2451 return 0;
2452 }
Adam Langley95c29f32014-06-20 12:00:00 -07002453
Adam Langleyfcf25832014-12-18 17:42:32 -08002454 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2455 sizeof(tls12_md) / sizeof(tls12_lookup));
2456 if (md_id == -1) {
2457 return 0;
2458 }
Adam Langley95c29f32014-06-20 12:00:00 -07002459
David Benjaminb4d65fd2015-05-29 17:11:21 -04002460 sig_id = tls12_get_sigid(ssl_private_key_type(ssl, pk));
Adam Langleyfcf25832014-12-18 17:42:32 -08002461 if (sig_id == -1) {
2462 return 0;
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 p[0] = (uint8_t)md_id;
2466 p[1] = (uint8_t)sig_id;
2467 return 1;
2468}
2469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2471 switch (hash_alg) {
2472 case TLSEXT_hash_md5:
2473 return EVP_md5();
2474
2475 case TLSEXT_hash_sha1:
2476 return EVP_sha1();
2477
2478 case TLSEXT_hash_sha224:
2479 return EVP_sha224();
2480
2481 case TLSEXT_hash_sha256:
2482 return EVP_sha256();
2483
2484 case TLSEXT_hash_sha384:
2485 return EVP_sha384();
2486
2487 case TLSEXT_hash_sha512:
2488 return EVP_sha512();
2489
2490 default:
2491 return NULL;
2492 }
2493}
Adam Langley95c29f32014-06-20 12:00:00 -07002494
David Benjaminec2f27d2014-11-13 19:17:25 -05002495/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2496 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002497static int tls12_get_pkey_type(uint8_t sig_alg) {
2498 switch (sig_alg) {
2499 case TLSEXT_signature_rsa:
2500 return EVP_PKEY_RSA;
2501
2502 case TLSEXT_signature_ecdsa:
2503 return EVP_PKEY_EC;
2504
2505 default:
2506 return -1;
2507 }
2508}
Adam Langley95c29f32014-06-20 12:00:00 -07002509
2510/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2511static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 int *psignhash_nid, const uint8_t *data) {
2513 int sign_nid = 0, hash_nid = 0;
2514 if (!phash_nid && !psign_nid && !psignhash_nid) {
2515 return;
2516 }
2517
2518 if (phash_nid || psignhash_nid) {
2519 hash_nid = tls12_find_nid(data[0], tls12_md,
2520 sizeof(tls12_md) / sizeof(tls12_lookup));
2521 if (phash_nid) {
2522 *phash_nid = hash_nid;
2523 }
2524 }
2525
2526 if (psign_nid || psignhash_nid) {
2527 sign_nid = tls12_find_nid(data[1], tls12_sig,
2528 sizeof(tls12_sig) / sizeof(tls12_lookup));
2529 if (psign_nid) {
2530 *psign_nid = sign_nid;
2531 }
2532 }
2533
2534 if (psignhash_nid) {
2535 if (sign_nid && hash_nid) {
2536 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2537 } else {
2538 *psignhash_nid = NID_undef;
2539 }
2540 }
2541}
2542
Adam Langley95c29f32014-06-20 12:00:00 -07002543/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002544static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2545 size_t preflen, const uint8_t *allow,
2546 size_t allowlen) {
2547 const uint8_t *ptmp, *atmp;
2548 size_t i, j, nmatch = 0;
2549
2550 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2551 /* Skip disabled hashes or signature algorithms */
2552 if (tls12_get_hash(ptmp[0]) == NULL ||
2553 tls12_get_pkey_type(ptmp[1]) == -1) {
2554 continue;
2555 }
2556
2557 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2558 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2559 nmatch++;
2560 if (shsig) {
2561 shsig->rhash = ptmp[0];
2562 shsig->rsign = ptmp[1];
2563 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2564 &shsig->signandhash_nid, ptmp);
2565 shsig++;
2566 }
2567
2568 break;
2569 }
2570 }
2571 }
2572
2573 return nmatch;
2574}
Adam Langley95c29f32014-06-20 12:00:00 -07002575
2576/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002577static int tls1_set_shared_sigalgs(SSL *s) {
2578 const uint8_t *pref, *allow, *conf;
2579 size_t preflen, allowlen, conflen;
2580 size_t nmatch;
2581 TLS_SIGALGS *salgs = NULL;
2582 CERT *c = s->cert;
2583
David Benjamin2755a3e2015-04-22 16:17:58 -04002584 OPENSSL_free(c->shared_sigalgs);
2585 c->shared_sigalgs = NULL;
2586 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002587
2588 /* If client use client signature algorithms if not NULL */
2589 if (!s->server && c->client_sigalgs) {
2590 conf = c->client_sigalgs;
2591 conflen = c->client_sigalgslen;
2592 } else if (c->conf_sigalgs) {
2593 conf = c->conf_sigalgs;
2594 conflen = c->conf_sigalgslen;
2595 } else {
2596 conflen = tls12_get_psigalgs(s, &conf);
2597 }
2598
2599 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2600 pref = conf;
2601 preflen = conflen;
2602 allow = c->peer_sigalgs;
2603 allowlen = c->peer_sigalgslen;
2604 } else {
2605 allow = conf;
2606 allowlen = conflen;
2607 pref = c->peer_sigalgs;
2608 preflen = c->peer_sigalgslen;
2609 }
2610
2611 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2612 if (!nmatch) {
2613 return 1;
2614 }
2615
2616 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2617 if (!salgs) {
2618 return 0;
2619 }
2620
2621 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2622 c->shared_sigalgs = salgs;
2623 c->shared_sigalgslen = nmatch;
2624 return 1;
2625}
Adam Langley95c29f32014-06-20 12:00:00 -07002626
2627/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002628int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2629 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 /* Extension ignored for inappropriate versions */
2632 if (!SSL_USE_SIGALGS(s)) {
2633 return 1;
2634 }
David Benjamincd996942014-07-20 16:23:51 -04002635
David Benjamin61c0d4e2015-03-19 14:24:37 -04002636 if (CBS_len(sigalgs) % 2 != 0 ||
2637 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2638 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 return 0;
2640 }
Adam Langley95c29f32014-06-20 12:00:00 -07002641
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 return 1;
2643}
David Benjaminec2f27d2014-11-13 19:17:25 -05002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2646 CERT *c = s->cert;
David Benjaminb4d65fd2015-05-29 17:11:21 -04002647 int type = ssl_private_key_type(s, pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002649
Adam Langleyfcf25832014-12-18 17:42:32 -08002650 /* Select the first shared digest supported by our key. */
2651 for (i = 0; i < c->shared_sigalgslen; i++) {
2652 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2653 if (md == NULL ||
2654 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
David Benjaminb4d65fd2015-05-29 17:11:21 -04002655 !ssl_private_key_supports_digest(s, pkey, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 continue;
2657 }
2658 return md;
2659 }
Adam Langley95c29f32014-06-20 12:00:00 -07002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 /* If no suitable digest may be found, default to SHA-1. */
2662 return EVP_sha1();
2663}
Adam Langley95c29f32014-06-20 12:00:00 -07002664
Adam Langleyfcf25832014-12-18 17:42:32 -08002665int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2666 uint8_t *rsig, uint8_t *rhash) {
2667 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 if (psig == NULL) {
2670 return 0;
2671 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002672
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 if (idx >= 0) {
2674 idx <<= 1;
2675 if (idx >= (int)s->cert->peer_sigalgslen) {
2676 return 0;
2677 }
2678 psig += idx;
2679 if (rhash) {
2680 *rhash = psig[0];
2681 }
2682 if (rsig) {
2683 *rsig = psig[1];
2684 }
2685 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2686 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 return s->cert->peer_sigalgslen / 2;
2689}
Adam Langley1258b6a2014-06-20 12:00:00 -07002690
Adam Langleyfcf25832014-12-18 17:42:32 -08002691int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2692 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2693 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002694
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2696 return 0;
2697 }
2698
2699 shsigalgs += idx;
2700 if (phash) {
2701 *phash = shsigalgs->hash_nid;
2702 }
2703 if (psign) {
2704 *psign = shsigalgs->sign_nid;
2705 }
2706 if (psignhash) {
2707 *psignhash = shsigalgs->signandhash_nid;
2708 }
2709 if (rsig) {
2710 *rsig = shsigalgs->rsign;
2711 }
2712 if (rhash) {
2713 *rhash = shsigalgs->rhash;
2714 }
2715
2716 return s->cert->shared_sigalgslen;
2717}
2718
2719/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2720 * given SSL connection and writes it to |md|. */
2721int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2722 EVP_MD_CTX ctx;
2723 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2724 unsigned temp_digest_len;
2725 int i;
2726 static const char kClientIDMagic[] = "TLS Channel ID signature";
2727
2728 if (s->s3->handshake_buffer &&
2729 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2730 return 0;
2731 }
2732
2733 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2734
2735 if (s->hit && s->s3->tlsext_channel_id_new) {
2736 static const char kResumptionMagic[] = "Resumption";
2737 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2738 if (s->session->original_handshake_hash_len == 0) {
2739 return 0;
2740 }
2741 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2742 s->session->original_handshake_hash_len);
2743 }
2744
2745 EVP_MD_CTX_init(&ctx);
2746 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2747 if (s->s3->handshake_dgst[i] == NULL) {
2748 continue;
2749 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002750 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2751 EVP_MD_CTX_cleanup(&ctx);
2752 return 0;
2753 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2755 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2756 }
2757 EVP_MD_CTX_cleanup(&ctx);
2758
2759 return 1;
2760}
Adam Langley1258b6a2014-06-20 12:00:00 -07002761
2762/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2763 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002764int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2765 int digest_len;
2766 /* This function should never be called for a resumed session because the
2767 * handshake hashes that we wish to record are for the original, full
2768 * handshake. */
2769 if (s->hit) {
2770 return -1;
2771 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002772
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 /* It only makes sense to call this function if Channel IDs have been
2774 * negotiated. */
2775 if (!s->s3->tlsext_channel_id_new) {
2776 return -1;
2777 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002778
Adam Langleyfcf25832014-12-18 17:42:32 -08002779 digest_len =
2780 tls1_handshake_digest(s, s->session->original_handshake_hash,
2781 sizeof(s->session->original_handshake_hash));
2782 if (digest_len < 0) {
2783 return -1;
2784 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002787
Adam Langleyfcf25832014-12-18 17:42:32 -08002788 return 1;
2789}
Adam Langley95c29f32014-06-20 12:00:00 -07002790
Adam Langleyfcf25832014-12-18 17:42:32 -08002791int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2792 int client) {
2793 uint8_t *sigalgs, *sptr;
2794 int rhash, rsign;
2795 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 if (salglen & 1) {
2798 return 0;
2799 }
Adam Langley95c29f32014-06-20 12:00:00 -07002800
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 sigalgs = OPENSSL_malloc(salglen);
2802 if (sigalgs == NULL) {
2803 return 0;
2804 }
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2807 rhash = tls12_find_id(*psig_nids++, tls12_md,
2808 sizeof(tls12_md) / sizeof(tls12_lookup));
2809 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2810 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002811
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 if (rhash == -1 || rsign == -1) {
2813 goto err;
2814 }
2815 *sptr++ = rhash;
2816 *sptr++ = rsign;
2817 }
2818
2819 if (client) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002820 OPENSSL_free(c->client_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 c->client_sigalgs = sigalgs;
2822 c->client_sigalgslen = salglen;
2823 } else {
David Benjamin2755a3e2015-04-22 16:17:58 -04002824 OPENSSL_free(c->conf_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 c->conf_sigalgs = sigalgs;
2826 c->conf_sigalgslen = salglen;
2827 }
2828
2829 return 1;
2830
2831err:
2832 OPENSSL_free(sigalgs);
2833 return 0;
2834}