blob: 5201b2ea1455c1b42eb682e0bcf396ffcde92544 [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 Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400114#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116
David Benjamin03973092014-06-24 23:27:17 -0400117#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400118#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/evp.h>
121#include <openssl/hmac.h>
122#include <openssl/mem.h>
123#include <openssl/obj.h>
124#include <openssl/rand.h>
125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin6c7aed02014-08-27 16:42:38 -0400129static int ssl_check_clienthello_tlsext(SSL *s);
130static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
David Benjamin338fcaf2014-12-11 01:20:52 -0500132const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500133 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800134 tls1_setup_key_block,
135 tls1_generate_master_secret,
136 tls1_change_cipher_state,
137 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_alert_code,
142 tls1_export_keying_material,
143 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800144};
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin338fcaf2014-12-11 01:20:52 -0500146const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500147 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800148 tls1_setup_key_block,
149 tls1_generate_master_secret,
150 tls1_change_cipher_state,
151 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800152 tls1_cert_verify_mac,
153 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
154 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
155 tls1_alert_code,
156 tls1_export_keying_material,
157 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800158};
Adam Langley95c29f32014-06-20 12:00:00 -0700159
David Benjamin338fcaf2014-12-11 01:20:52 -0500160const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500161 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 tls1_setup_key_block,
163 tls1_generate_master_secret,
164 tls1_change_cipher_state,
165 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800166 tls1_cert_verify_mac,
167 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
168 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
169 tls1_alert_code,
170 tls1_export_keying_material,
171 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
172 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800173};
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175static int compare_uint16_t(const void *p1, const void *p2) {
176 uint16_t u1 = *((const uint16_t *)p1);
177 uint16_t u2 = *((const uint16_t *)p2);
178 if (u1 < u2) {
179 return -1;
180 } else if (u1 > u2) {
181 return 1;
182 } else {
183 return 0;
184 }
185}
David Benjamin35a7a442014-07-05 00:23:20 -0400186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
188 * more than one extension of the same type in a ClientHello or ServerHello.
189 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400190 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800191static int tls1_check_duplicate_extensions(const CBS *cbs) {
192 CBS extensions = *cbs;
193 size_t num_extensions = 0, i = 0;
194 uint16_t *extension_types = NULL;
195 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 /* First pass: count the extensions. */
198 while (CBS_len(&extensions) > 0) {
199 uint16_t type;
200 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 if (!CBS_get_u16(&extensions, &type) ||
203 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
204 goto done;
205 }
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 num_extensions++;
208 }
David Benjamin35a7a442014-07-05 00:23:20 -0400209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (num_extensions == 0) {
211 return 1;
212 }
David Benjamin9a373592014-07-25 04:27:53 -0400213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 extension_types =
215 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
216 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400217 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 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;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200341 const char curve_name[8];
David Benjamin52e5bac2014-12-27 02:27:35 -0500342};
343
David Benjamin70bd80a2014-12-27 03:06:46 -0500344/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500345static const struct tls_curve tls_curves[] = {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200346 {21, NID_secp224r1, "P-224"},
347 {23, NID_X9_62_prime256v1, "P-256"},
348 {24, NID_secp384r1, "P-384"},
349 {25, NID_secp521r1, "P-521"},
Adam Langleyfcf25832014-12-18 17:42:32 -0800350};
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400353 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500354 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800355};
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500358 size_t i;
359 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
360 if (curve_id == tls_curves[i].curve_id) {
361 return tls_curves[i].nid;
362 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500364 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
Adam Langley95c29f32014-06-20 12:00:00 -0700366
David Benjamin70bd80a2014-12-27 03:06:46 -0500367int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500369 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
370 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500371 *out_curve_id = tls_curves[i].curve_id;
372 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 }
374 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376}
377
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200378const char* tls1_ec_curve_id2name(uint16_t curve_id) {
379 size_t i;
380 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
381 if (curve_id == tls_curves[i].curve_id) {
382 return tls_curves[i].curve_name;
383 }
384 }
385 return NULL;
386}
387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
389 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
390 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400391static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 const uint16_t **out_curve_ids,
393 size_t *out_curve_ids_len) {
394 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400395 /* Only clients send a curve list, so this function is only called
396 * on the server. */
397 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
399 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
400 return;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 *out_curve_ids = s->tlsext_ellipticcurvelist;
404 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
405 if (!*out_curve_ids) {
406 *out_curve_ids = eccurves_default;
407 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
408 }
409}
David Benjamined439582014-07-14 19:13:02 -0400410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
412 uint8_t curve_type;
413 uint16_t curve_id;
414 const uint16_t *curves;
415 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 /* Only support named curves. */
418 if (!CBS_get_u8(cbs, &curve_type) ||
419 curve_type != NAMED_CURVE_TYPE ||
420 !CBS_get_u16(cbs, &curve_id)) {
421 return 0;
422 }
David Benjamined439582014-07-14 19:13:02 -0400423
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 tls1_get_curvelist(s, 0, &curves, &curves_len);
425 for (i = 0; i < curves_len; i++) {
426 if (curve_id == curves[i]) {
427 *out_curve_id = curve_id;
428 return 1;
429 }
430 }
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 return 0;
433}
David Benjamin072334d2014-07-13 16:24:27 -0400434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400436 const uint16_t *curves, *peer_curves, *pref, *supp;
437 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400438
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 /* Can't do anything on client side */
440 if (s->server == 0) {
441 return NID_undef;
442 }
443
David Benjamin55a43642015-04-20 14:45:55 -0400444 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
445 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800446
David Benjamin55a43642015-04-20 14:45:55 -0400447 if (peer_curves_len == 0) {
448 /* Clients are not required to send a supported_curves extension. In this
449 * case, the server is free to pick any curve it likes. See RFC 4492,
450 * section 4, paragraph 3. */
451 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
452 }
453
454 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
455 pref = curves;
456 pref_len = curves_len;
457 supp = peer_curves;
458 supp_len = peer_curves_len;
459 } else {
460 pref = peer_curves;
461 pref_len = peer_curves_len;
462 supp = curves;
463 supp_len = curves_len;
464 }
465
466 for (i = 0; i < pref_len; i++) {
467 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (pref[i] == supp[j]) {
469 return tls1_ec_curve_id2nid(pref[i]);
470 }
471 }
472 }
473
474 return NID_undef;
475}
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjamin072334d2014-07-13 16:24:27 -0400477int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 const int *curves, size_t ncurves) {
479 uint16_t *curve_ids;
480 size_t i;
481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
483 if (curve_ids == NULL) {
484 return 0;
485 }
486
487 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500488 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 OPENSSL_free(curve_ids);
490 return 0;
491 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 }
493
David Benjamin2755a3e2015-04-22 16:17:58 -0400494 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 *out_curve_ids = curve_ids;
496 *out_curve_ids_len = ncurves;
497
498 return 1;
499}
Adam Langley95c29f32014-06-20 12:00:00 -0700500
David Benjamin072334d2014-07-13 16:24:27 -0400501/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
502 * TLS curve ID and point format, respectively, for |ec|. It returns one on
503 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800504static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
505 uint8_t *out_comp_id, EC_KEY *ec) {
506 int nid;
507 uint16_t id;
508 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 if (ec == NULL) {
511 return 0;
512 }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 grp = EC_KEY_get0_group(ec);
515 if (grp == NULL) {
516 return 0;
517 }
David Benjamin072334d2014-07-13 16:24:27 -0400518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 /* Determine curve ID */
520 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500521 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 return 0;
523 }
David Benjamin072334d2014-07-13 16:24:27 -0400524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
526 *out_curve_id = id;
527
528 if (out_comp_id) {
529 if (EC_KEY_get0_public_key(ec) == NULL) {
530 return 0;
531 }
532 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
533 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
534 } else {
535 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
536 }
537 }
538
539 return 1;
540}
David Benjamin072334d2014-07-13 16:24:27 -0400541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
543 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400544 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800545static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
546 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400547 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400550 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
551 if (get_peer_curves && !s->server) {
552 /* Servers do not present a preference list so, if we are a client, only
553 * check our list. */
554 continue;
555 }
556
557 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
558 if (get_peer_curves && curves_len == 0) {
559 /* Clients are not required to send a supported_curves extension. In this
560 * case, the server is free to pick any curve it likes. See RFC 4492,
561 * section 4, paragraph 3. */
562 continue;
563 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 for (i = 0; i < curves_len; i++) {
565 if (curves[i] == curve_id) {
566 break;
567 }
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 if (i == curves_len) {
571 return 0;
572 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 }
David Benjamin033e5f42014-11-13 18:47:41 -0500574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 return 1;
576}
David Benjamin033e5f42014-11-13 18:47:41 -0500577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578int tls1_check_ec_cert(SSL *s, X509 *x) {
579 int ret = 0;
580 EVP_PKEY *pkey = X509_get_pubkey(x);
581 uint16_t curve_id;
582 uint8_t comp_id;
583
584 if (!pkey ||
585 pkey->type != EVP_PKEY_EC ||
586 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
587 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400588 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 goto done;
590 }
591
592 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500593
594done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400595 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 return ret;
597}
David Benjamin42e9a772014-09-02 23:18:44 -0400598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400600 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400601 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400602 * the curve. */
603 uint16_t curve_id;
604 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
605 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 }
607
David Benjamindd978782015-04-24 15:20:13 -0400608 if (s->cert->ecdh_tmp_cb != NULL) {
609 /* Assume the callback will provide an acceptable curve. */
610 return 1;
611 }
612
613 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
614 * there is a shared curve. */
615 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800616}
Adam Langley95c29f32014-06-20 12:00:00 -0700617
618/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langley95c29f32014-06-20 12:00:00 -0700621#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langley95c29f32014-06-20 12:00:00 -0700623#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700626
David Benjamincff64722014-08-19 19:54:46 -0400627static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 tlsext_sigalg(TLSEXT_hash_sha512)
629 tlsext_sigalg(TLSEXT_hash_sha384)
630 tlsext_sigalg(TLSEXT_hash_sha256)
631 tlsext_sigalg(TLSEXT_hash_sha224)
632 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700633};
David Benjamin05da6e12014-07-12 20:42:55 -0400634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400636 *psigs = tls12_sigalgs;
637 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800638}
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
641 * checks it is consistent with |s|'s sent supported signature algorithms and,
642 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
643 * returns 0 and writes an alert into |*out_alert|. */
644int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
645 CBS *cbs, EVP_PKEY *pkey) {
646 const uint8_t *sent_sigs;
647 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400648 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 uint8_t hash, signature;
650
651 /* Should never happen */
652 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400653 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 *out_alert = SSL_AD_INTERNAL_ERROR;
655 return 0;
656 }
657
658 if (!CBS_get_u8(cbs, &hash) ||
659 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400660 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 *out_alert = SSL_AD_DECODE_ERROR;
662 return 0;
663 }
664
665 /* Check key type is consistent with signature */
666 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
669 return 0;
670 }
671
672 if (pkey->type == EVP_PKEY_EC) {
673 uint16_t curve_id;
674 uint8_t comp_id;
675 /* Check compression and curve matches extensions */
676 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
677 *out_alert = SSL_AD_INTERNAL_ERROR;
678 return 0;
679 }
680
681 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400682 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400683 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
685 return 0;
686 }
687 }
688
689 /* Check signature matches a type we sent */
690 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
691 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
692 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
693 break;
694 }
695 }
696
697 /* Allow fallback to SHA-1. */
698 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400699 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
701 return 0;
702 }
703
704 *out_md = tls12_get_hash(hash);
705 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400706 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
708 return 0;
709 }
710
711 return 1;
712}
713
714/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
715 * supported or doesn't appear in supported signature algorithms. Unlike
716 * ssl_cipher_get_disabled this applies to a specific session and not global
717 * settings. */
718void ssl_set_client_disabled(SSL *s) {
719 CERT *c = s->cert;
720 const uint8_t *sigalgs;
721 size_t i, sigalgslen;
722 int have_rsa = 0, have_ecdsa = 0;
723 c->mask_a = 0;
724 c->mask_k = 0;
725
726 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
727 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
728 c->mask_ssl = SSL_TLSV1_2;
729 } else {
730 c->mask_ssl = 0;
731 }
732
733 /* Now go through all signature algorithms seeing if we support any for RSA,
734 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
735 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
736 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
737 switch (sigalgs[1]) {
738 case TLSEXT_signature_rsa:
739 have_rsa = 1;
740 break;
741
742 case TLSEXT_signature_ecdsa:
743 have_ecdsa = 1;
744 break;
745 }
746 }
747
748 /* Disable auth if we don't include any appropriate signature algorithms. */
749 if (!have_rsa) {
750 c->mask_a |= SSL_aRSA;
751 }
752 if (!have_ecdsa) {
753 c->mask_a |= SSL_aECDSA;
754 }
755
756 /* with PSK there must be client callback set */
757 if (!s->psk_client_callback) {
758 c->mask_a |= SSL_aPSK;
759 c->mask_k |= SSL_kPSK;
760 }
761}
Adam Langley95c29f32014-06-20 12:00:00 -0700762
Adam Langley614c66a2015-06-12 15:26:58 -0700763/* tls_extension represents a TLS extension that is handled internally. The
764 * |init| function is called for each handshake, before any other functions of
765 * the extension. Then the add and parse callbacks are called as needed.
766 *
767 * The parse callbacks receive a |CBS| that contains the contents of the
768 * extension (i.e. not including the type and length bytes). If an extension is
769 * not received then the parse callbacks will be called with a NULL CBS so that
770 * they can do any processing needed to handle the absence of an extension.
771 *
772 * The add callbacks receive a |CBB| to which the extension can be appended but
773 * the function is responsible for appending the type and length bytes too.
774 *
775 * All callbacks return one for success and zero for error. If a parse function
776 * returns zero then a fatal alert with value |*out_alert| will be sent. If
777 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
778struct tls_extension {
779 uint16_t value;
780 void (*init)(SSL *ssl);
781
782 int (*add_clienthello)(SSL *ssl, CBB *out);
783 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
784
785 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
786 int (*add_serverhello)(SSL *ssl, CBB *out);
787};
788
789
790/* Server name indication (SNI).
791 *
792 * https://tools.ietf.org/html/rfc6066#section-3. */
793
794static void ext_sni_init(SSL *ssl) {
795 ssl->s3->tmp.should_ack_sni = 0;
796}
797
798static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
799 if (ssl->tlsext_hostname == NULL) {
800 return 1;
801 }
802
803 CBB contents, server_name_list, name;
804 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
805 !CBB_add_u16_length_prefixed(out, &contents) ||
806 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
807 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
808 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
809 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
810 strlen(ssl->tlsext_hostname)) ||
811 !CBB_flush(out)) {
812 return 0;
813 }
814
815 return 1;
816}
817
818static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
819 if (contents == NULL) {
820 return 1;
821 }
822
823 if (CBS_len(contents) != 0) {
824 return 0;
825 }
826
827 assert(ssl->tlsext_hostname != NULL);
828
829 if (!ssl->hit) {
830 assert(ssl->session->tlsext_hostname == NULL);
831 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
832 if (!ssl->session->tlsext_hostname) {
833 *out_alert = SSL_AD_INTERNAL_ERROR;
834 return 0;
835 }
836 }
837
838 return 1;
839}
840
841static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
842 if (contents == NULL) {
843 return 1;
844 }
845
846 /* The servername extension is treated as follows:
847 *
848 * - Only the hostname type is supported with a maximum length of 255.
849 * - The servername is rejected if too long or if it contains zeros, in
850 * which case an fatal alert is generated.
851 * - The servername field is maintained together with the session cache.
852 * - When a session is resumed, the servername callback is invoked in order
853 * to allow the application to position itself to the right context.
854 * - The servername is acknowledged if it is new for a session or when
855 * it is identical to a previously used for the same session.
856 * Applications can control the behaviour. They can at any time
857 * set a 'desirable' servername for a new SSL object. This can be the
858 * case for example with HTTPS when a Host: header field is received and
859 * a renegotiation is requested. In this case, a possible servername
860 * presented in the new client hello is only acknowledged if it matches
861 * the value of the Host: field.
862 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
863 * if they provide for changing an explicit servername context for the
864 * session,
865 * i.e. when the session has been established with a servername extension.
866 */
867
868 CBS server_name_list;
869 char have_seen_host_name = 0;
870
871 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
872 CBS_len(&server_name_list) == 0 ||
873 CBS_len(contents) != 0) {
874 return 0;
875 }
876
877 /* Decode each ServerName in the extension. */
878 while (CBS_len(&server_name_list) > 0) {
879 uint8_t name_type;
880 CBS host_name;
881
882 if (!CBS_get_u8(&server_name_list, &name_type) ||
883 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
884 return 0;
885 }
886
887 /* Only host_name is supported. */
888 if (name_type != TLSEXT_NAMETYPE_host_name) {
889 continue;
890 }
891
892 if (have_seen_host_name) {
893 /* The ServerNameList MUST NOT contain more than one name of the same
894 * name_type. */
895 return 0;
896 }
897
898 have_seen_host_name = 1;
899
900 if (CBS_len(&host_name) == 0 ||
901 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
902 CBS_contains_zero_byte(&host_name)) {
903 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
904 return 0;
905 }
906
907 if (!ssl->hit) {
908 assert(ssl->session->tlsext_hostname == NULL);
909 if (ssl->session->tlsext_hostname) {
910 /* This should be impossible. */
911 return 0;
912 }
913
914 /* Copy the hostname as a string. */
915 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
916 *out_alert = SSL_AD_INTERNAL_ERROR;
917 return 0;
918 }
919
920 ssl->s3->tmp.should_ack_sni = 1;
921 }
922 }
923
924 return 1;
925}
926
927static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
928 if (ssl->hit ||
929 !ssl->s3->tmp.should_ack_sni ||
930 ssl->session->tlsext_hostname == NULL) {
931 return 1;
932 }
933
934 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
935 !CBB_add_u16(out, 0 /* length */)) {
936 return 0;
937 }
938
939 return 1;
940}
941
942
Adam Langley5021b222015-06-12 18:27:58 -0700943/* Renegotiation indication.
944 *
945 * https://tools.ietf.org/html/rfc5746 */
946
947static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
948 CBB contents, prev_finished;
949 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
950 !CBB_add_u16_length_prefixed(out, &contents) ||
951 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
952 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
953 ssl->s3->previous_client_finished_len) ||
954 !CBB_flush(out)) {
955 return 0;
956 }
957
958 return 1;
959}
960
961static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
962 CBS *contents) {
963 if (contents == NULL) {
964 /* No renegotiation extension received.
965 *
966 * Strictly speaking if we want to avoid an attack we should *always* see
967 * RI even on initial ServerHello because the client doesn't see any
968 * renegotiation during an attack. However this would mean we could not
969 * connect to any server which doesn't support RI.
970 *
971 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
972 * defined. */
973 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
974 return 1;
975 }
976
977 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400978 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700979 return 0;
980 }
981
982 const size_t expected_len = ssl->s3->previous_client_finished_len +
983 ssl->s3->previous_server_finished_len;
984
985 /* Check for logic errors */
986 assert(!expected_len || ssl->s3->previous_client_finished_len);
987 assert(!expected_len || ssl->s3->previous_server_finished_len);
988
989 /* Parse out the extension contents. */
990 CBS renegotiated_connection;
991 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
992 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400993 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700994 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
995 return 0;
996 }
997
998 /* Check that the extension matches. */
999 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -04001000 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001001 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1002 return 0;
1003 }
1004
1005 const uint8_t *d = CBS_data(&renegotiated_connection);
1006 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1007 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001008 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001009 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1010 return 0;
1011 }
1012 d += ssl->s3->previous_client_finished_len;
1013
1014 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1015 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001016 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001017 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1018 return 0;
1019 }
1020 ssl->s3->send_connection_binding = 1;
1021
1022 return 1;
1023}
1024
1025static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1026 CBS *contents) {
1027 /* Renegotiation isn't supported as a server so this function should never be
1028 * called after the initial handshake. */
1029 assert(!ssl->s3->initial_handshake_complete);
1030
1031 CBS fake_contents;
1032 static const uint8_t kFakeExtension[] = {0};
1033
1034 if (contents == NULL) {
1035 if (ssl->s3->send_connection_binding) {
1036 /* The renegotiation SCSV was received so pretend that we received a
1037 * renegotiation extension. */
1038 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1039 contents = &fake_contents;
1040 /* We require that the renegotiation extension is at index zero of
1041 * kExtensions. */
1042 ssl->s3->tmp.extensions.received |= (1u << 0);
1043 } else {
1044 return 1;
1045 }
1046 }
1047
1048 CBS renegotiated_connection;
1049
1050 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1051 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001052 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001053 return 0;
1054 }
1055
1056 /* Check that the extension matches */
1057 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1058 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001059 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001060 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1061 return 0;
1062 }
1063
1064 ssl->s3->send_connection_binding = 1;
1065
1066 return 1;
1067}
1068
1069static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1070 CBB contents, prev_finished;
1071 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1072 !CBB_add_u16_length_prefixed(out, &contents) ||
1073 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1074 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1075 ssl->s3->previous_client_finished_len) ||
1076 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1077 ssl->s3->previous_server_finished_len) ||
1078 !CBB_flush(out)) {
1079 return 0;
1080 }
1081
1082 return 1;
1083}
1084
Adam Langley0a056712015-07-01 15:03:33 -07001085
1086/* Extended Master Secret.
1087 *
1088 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1089
1090static void ext_ems_init(SSL *ssl) {
1091 ssl->s3->tmp.extended_master_secret = 0;
1092}
1093
1094static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1095 if (ssl->version == SSL3_VERSION) {
1096 return 1;
1097 }
1098
1099 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1100 !CBB_add_u16(out, 0 /* length */)) {
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
1107static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1108 CBS *contents) {
1109 if (contents == NULL) {
1110 return 1;
1111 }
1112
1113 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1114 return 0;
1115 }
1116
1117 ssl->s3->tmp.extended_master_secret = 1;
1118 return 1;
1119}
1120
1121static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1122 if (ssl->version == SSL3_VERSION || contents == NULL) {
1123 return 1;
1124 }
1125
1126 if (CBS_len(contents) != 0) {
1127 return 0;
1128 }
1129
1130 ssl->s3->tmp.extended_master_secret = 1;
1131 return 1;
1132}
1133
1134static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1135 if (!ssl->s3->tmp.extended_master_secret) {
1136 return 1;
1137 }
1138
1139 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1140 !CBB_add_u16(out, 0 /* length */)) {
1141 return 0;
1142 }
1143
1144 return 1;
1145}
1146
Adam Langley9b05bc52015-07-01 15:25:33 -07001147
1148/* Session tickets.
1149 *
1150 * https://tools.ietf.org/html/rfc5077 */
1151
1152static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1153 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1154 return 1;
1155 }
1156
1157 const uint8_t *ticket_data = NULL;
1158 int ticket_len = 0;
1159
1160 /* Renegotiation does not participate in session resumption. However, still
1161 * advertise the extension to avoid potentially breaking servers which carry
1162 * over the state from the previous handshake, such as OpenSSL servers
1163 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1164 if (!ssl->s3->initial_handshake_complete &&
1165 ssl->session != NULL &&
1166 ssl->session->tlsext_tick != NULL) {
1167 ticket_data = ssl->session->tlsext_tick;
1168 ticket_len = ssl->session->tlsext_ticklen;
1169 }
1170
1171 CBB ticket;
1172 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1173 !CBB_add_u16_length_prefixed(out, &ticket) ||
1174 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1175 !CBB_flush(out)) {
1176 return 0;
1177 }
1178
1179 return 1;
1180}
1181
1182static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1183 CBS *contents) {
1184 ssl->tlsext_ticket_expected = 0;
1185
1186 if (contents == NULL) {
1187 return 1;
1188 }
1189
1190 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1191 * this function should never be called, even if the server tries to send the
1192 * extension. */
1193 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1194
1195 if (CBS_len(contents) != 0) {
1196 return 0;
1197 }
1198
1199 ssl->tlsext_ticket_expected = 1;
1200 return 1;
1201}
1202
1203static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1204 /* This function isn't used because the ticket extension from the client is
1205 * handled in ssl_sess.c. */
1206 return 1;
1207}
1208
1209static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1210 if (!ssl->tlsext_ticket_expected) {
1211 return 1;
1212 }
1213
1214 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1215 * true. */
1216 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1217
1218 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1219 !CBB_add_u16(out, 0 /* length */)) {
1220 return 0;
1221 }
1222
1223 return 1;
1224}
1225
1226
Adam Langley2e857bd2015-07-01 16:09:19 -07001227/* Signature Algorithms.
1228 *
1229 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1230
1231static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1232 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1233 return 1;
1234 }
1235
1236 const uint8_t *sigalgs_data;
1237 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1238
1239 CBB contents, sigalgs;
1240 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1241 !CBB_add_u16_length_prefixed(out, &contents) ||
1242 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1243 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1244 !CBB_flush(out)) {
1245 return 0;
1246 }
1247
1248 return 1;
1249}
1250
1251static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1252 CBS *contents) {
1253 if (contents != NULL) {
1254 /* Servers MUST NOT send this extension. */
1255 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001256 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001257 return 0;
1258 }
1259
1260 return 1;
1261}
1262
1263static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1264 CBS *contents) {
1265 OPENSSL_free(ssl->cert->peer_sigalgs);
1266 ssl->cert->peer_sigalgs = NULL;
1267 ssl->cert->peer_sigalgslen = 0;
1268
1269 OPENSSL_free(ssl->cert->shared_sigalgs);
1270 ssl->cert->shared_sigalgs = NULL;
1271 ssl->cert->shared_sigalgslen = 0;
1272
1273 if (contents == NULL) {
1274 return 1;
1275 }
1276
1277 CBS supported_signature_algorithms;
1278 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1279 CBS_len(contents) != 0) {
1280 return 0;
1281 }
1282
1283 /* Ensure the signature algorithms are non-empty. It contains a list of
1284 * SignatureAndHashAlgorithms which are two bytes each. */
1285 if (CBS_len(&supported_signature_algorithms) == 0 ||
1286 (CBS_len(&supported_signature_algorithms) % 2) != 0 ||
1287 !tls1_process_sigalgs(ssl, &supported_signature_algorithms)) {
1288 return 0;
1289 }
1290
1291 /* It's a fatal error if the signature_algorithms extension is received and
1292 * there are no shared algorithms. */
1293 if (ssl->cert->peer_sigalgs && !ssl->cert->shared_sigalgs) {
David Benjamin3570d732015-06-29 00:28:17 -04001294 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
Adam Langley2e857bd2015-07-01 16:09:19 -07001295 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1296 return 0;
1297 }
1298
1299 return 1;
1300}
1301
1302static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1303 /* Servers MUST NOT send this extension. */
1304 return 1;
1305}
1306
1307
Adam Langleybb0bd042015-07-01 16:21:03 -07001308/* OCSP Stapling.
1309 *
1310 * https://tools.ietf.org/html/rfc6066#section-8 */
1311
1312static void ext_ocsp_init(SSL *ssl) {
1313 ssl->s3->tmp.certificate_status_expected = 0;
1314}
1315
1316static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1317 if (!ssl->ocsp_stapling_enabled) {
1318 return 1;
1319 }
1320
1321 CBB contents;
1322 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1323 !CBB_add_u16_length_prefixed(out, &contents) ||
1324 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1325 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1326 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1327 !CBB_flush(out)) {
1328 return 0;
1329 }
1330
1331 return 1;
1332}
1333
1334static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001335 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001336 if (contents == NULL) {
1337 return 1;
1338 }
1339
1340 if (CBS_len(contents) != 0) {
1341 return 0;
1342 }
1343
1344 ssl->s3->tmp.certificate_status_expected = 1;
1345 return 1;
1346}
1347
1348static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1349 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001350 if (contents == NULL) {
1351 return 1;
1352 }
1353
1354 uint8_t status_type;
1355 if (!CBS_get_u8(contents, &status_type)) {
1356 return 0;
1357 }
1358
1359 /* We cannot decide whether OCSP stapling will occur yet because the correct
1360 * SSL_CTX might not have been selected. */
1361 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1362
Adam Langleybb0bd042015-07-01 16:21:03 -07001363 return 1;
1364}
1365
1366static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001367 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1368 ssl->ctx->ocsp_response_length == 0) {
1369 return 1;
1370 }
1371
1372 ssl->s3->tmp.certificate_status_expected = 1;
1373
1374 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1375 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001376}
1377
1378
Adam Langley97dfcbf2015-07-01 18:35:20 -07001379/* Next protocol negotiation.
1380 *
1381 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1382
1383static void ext_npn_init(SSL *ssl) {
1384 ssl->s3->next_proto_neg_seen = 0;
1385}
1386
1387static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1388 if (ssl->s3->initial_handshake_complete ||
1389 ssl->ctx->next_proto_select_cb == NULL ||
1390 SSL_IS_DTLS(ssl)) {
1391 return 1;
1392 }
1393
1394 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1395 !CBB_add_u16(out, 0 /* length */)) {
1396 return 0;
1397 }
1398
1399 return 1;
1400}
1401
1402static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1403 CBS *contents) {
1404 if (contents == NULL) {
1405 return 1;
1406 }
1407
1408 /* If any of these are false then we should never have sent the NPN
1409 * extension in the ClientHello and thus this function should never have been
1410 * called. */
1411 assert(!ssl->s3->initial_handshake_complete);
1412 assert(!SSL_IS_DTLS(ssl));
1413 assert(ssl->ctx->next_proto_select_cb != NULL);
1414
David Benjamin76c2efc2015-08-31 14:24:29 -04001415 if (ssl->s3->alpn_selected != NULL) {
1416 /* NPN and ALPN may not be negotiated in the same connection. */
1417 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1418 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1419 return 0;
1420 }
1421
Adam Langley97dfcbf2015-07-01 18:35:20 -07001422 const uint8_t *const orig_contents = CBS_data(contents);
1423 const size_t orig_len = CBS_len(contents);
1424
1425 while (CBS_len(contents) != 0) {
1426 CBS proto;
1427 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1428 CBS_len(&proto) == 0) {
1429 return 0;
1430 }
1431 }
1432
1433 uint8_t *selected;
1434 uint8_t selected_len;
1435 if (ssl->ctx->next_proto_select_cb(
1436 ssl, &selected, &selected_len, orig_contents, orig_len,
1437 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1438 *out_alert = SSL_AD_INTERNAL_ERROR;
1439 return 0;
1440 }
1441
1442 OPENSSL_free(ssl->next_proto_negotiated);
1443 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1444 if (ssl->next_proto_negotiated == NULL) {
1445 *out_alert = SSL_AD_INTERNAL_ERROR;
1446 return 0;
1447 }
1448
1449 ssl->next_proto_negotiated_len = selected_len;
1450 ssl->s3->next_proto_neg_seen = 1;
1451
1452 return 1;
1453}
1454
1455static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1456 CBS *contents) {
1457 if (contents != NULL && CBS_len(contents) != 0) {
1458 return 0;
1459 }
1460
1461 if (contents == NULL ||
1462 ssl->s3->initial_handshake_complete ||
1463 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1464 * afterwards, parsing the ALPN extension will clear
1465 * |next_proto_neg_seen|. */
1466 ssl->s3->alpn_selected != NULL ||
1467 ssl->ctx->next_protos_advertised_cb == NULL ||
1468 SSL_IS_DTLS(ssl)) {
1469 return 1;
1470 }
1471
1472 ssl->s3->next_proto_neg_seen = 1;
1473 return 1;
1474}
1475
1476static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1477 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1478 * parsed. */
1479 if (!ssl->s3->next_proto_neg_seen) {
1480 return 1;
1481 }
1482
1483 const uint8_t *npa;
1484 unsigned npa_len;
1485
1486 if (ssl->ctx->next_protos_advertised_cb(
1487 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1488 SSL_TLSEXT_ERR_OK) {
1489 ssl->s3->next_proto_neg_seen = 0;
1490 return 1;
1491 }
1492
1493 CBB contents;
1494 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1495 !CBB_add_u16_length_prefixed(out, &contents) ||
1496 !CBB_add_bytes(&contents, npa, npa_len) ||
1497 !CBB_flush(out)) {
1498 return 0;
1499 }
1500
1501 return 1;
1502}
1503
1504
Adam Langleyab8d87d2015-07-10 12:21:39 -07001505/* Signed certificate timestamps.
1506 *
1507 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1508
1509static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1510 if (!ssl->signed_cert_timestamps_enabled) {
1511 return 1;
1512 }
1513
1514 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1515 !CBB_add_u16(out, 0 /* length */)) {
1516 return 0;
1517 }
1518
1519 return 1;
1520}
1521
1522static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1523 CBS *contents) {
1524 if (contents == NULL) {
1525 return 1;
1526 }
1527
1528 /* If this is false then we should never have sent the SCT extension in the
1529 * ClientHello and thus this function should never have been called. */
1530 assert(ssl->signed_cert_timestamps_enabled);
1531
1532 if (CBS_len(contents) == 0) {
1533 *out_alert = SSL_AD_DECODE_ERROR;
1534 return 0;
1535 }
1536
1537 /* Session resumption uses the original session information. */
1538 if (!ssl->hit &&
1539 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1540 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1541 *out_alert = SSL_AD_INTERNAL_ERROR;
1542 return 0;
1543 }
1544
1545 return 1;
1546}
1547
1548static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1549 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001550 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001551}
1552
1553static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001554 if (ssl->ctx->signed_cert_timestamp_list_length == 0) {
1555 return 1;
1556 }
1557
1558 CBB contents;
1559 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1560 CBB_add_u16_length_prefixed(out, &contents) &&
1561 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1562 ssl->ctx->signed_cert_timestamp_list_length) &&
1563 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001564}
1565
1566
Adam Langleyf18e4532015-07-10 13:39:53 -07001567/* Application-level Protocol Negotiation.
1568 *
1569 * https://tools.ietf.org/html/rfc7301 */
1570
1571static void ext_alpn_init(SSL *ssl) {
1572 OPENSSL_free(ssl->s3->alpn_selected);
1573 ssl->s3->alpn_selected = NULL;
1574}
1575
1576static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1577 if (ssl->alpn_client_proto_list == NULL ||
1578 ssl->s3->initial_handshake_complete) {
1579 return 1;
1580 }
1581
1582 CBB contents, proto_list;
1583 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1584 !CBB_add_u16_length_prefixed(out, &contents) ||
1585 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1586 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1587 ssl->alpn_client_proto_list_len) ||
1588 !CBB_flush(out)) {
1589 return 0;
1590 }
1591
1592 return 1;
1593}
1594
1595static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1596 CBS *contents) {
1597 if (contents == NULL) {
1598 return 1;
1599 }
1600
1601 assert(!ssl->s3->initial_handshake_complete);
1602 assert(ssl->alpn_client_proto_list != NULL);
1603
David Benjamin76c2efc2015-08-31 14:24:29 -04001604 if (ssl->s3->next_proto_neg_seen) {
1605 /* NPN and ALPN may not be negotiated in the same connection. */
1606 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1607 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1608 return 0;
1609 }
1610
Adam Langleyf18e4532015-07-10 13:39:53 -07001611 /* The extension data consists of a ProtocolNameList which must have
1612 * exactly one ProtocolName. Each of these is length-prefixed. */
1613 CBS protocol_name_list, protocol_name;
1614 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1615 CBS_len(contents) != 0 ||
1616 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1617 /* Empty protocol names are forbidden. */
1618 CBS_len(&protocol_name) == 0 ||
1619 CBS_len(&protocol_name_list) != 0) {
1620 return 0;
1621 }
1622
1623 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1624 &ssl->s3->alpn_selected_len)) {
1625 *out_alert = SSL_AD_INTERNAL_ERROR;
1626 return 0;
1627 }
1628
1629 return 1;
1630}
1631
1632static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1633 CBS *contents) {
1634 if (contents == NULL) {
1635 return 1;
1636 }
1637
1638 if (ssl->ctx->alpn_select_cb == NULL ||
1639 ssl->s3->initial_handshake_complete) {
1640 return 1;
1641 }
1642
1643 /* ALPN takes precedence over NPN. */
1644 ssl->s3->next_proto_neg_seen = 0;
1645
1646 CBS protocol_name_list;
1647 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1648 CBS_len(contents) != 0 ||
1649 CBS_len(&protocol_name_list) < 2) {
1650 return 0;
1651 }
1652
1653 /* Validate the protocol list. */
1654 CBS protocol_name_list_copy = protocol_name_list;
1655 while (CBS_len(&protocol_name_list_copy) > 0) {
1656 CBS protocol_name;
1657
1658 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1659 /* Empty protocol names are forbidden. */
1660 CBS_len(&protocol_name) == 0) {
1661 return 0;
1662 }
1663 }
1664
1665 const uint8_t *selected;
1666 uint8_t selected_len;
1667 if (ssl->ctx->alpn_select_cb(
1668 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1669 CBS_len(&protocol_name_list),
1670 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1671 OPENSSL_free(ssl->s3->alpn_selected);
1672 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1673 if (ssl->s3->alpn_selected == NULL) {
1674 *out_alert = SSL_AD_INTERNAL_ERROR;
1675 return 0;
1676 }
1677 ssl->s3->alpn_selected_len = selected_len;
1678 }
1679
1680 return 1;
1681}
1682
1683static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1684 if (ssl->s3->alpn_selected == NULL) {
1685 return 1;
1686 }
1687
1688 CBB contents, proto_list, proto;
1689 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1690 !CBB_add_u16_length_prefixed(out, &contents) ||
1691 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1692 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1693 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1694 !CBB_flush(out)) {
1695 return 0;
1696 }
1697
1698 return 1;
1699}
1700
1701
Adam Langley49c7af12015-07-10 14:33:46 -07001702/* Channel ID.
1703 *
1704 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1705
1706static void ext_channel_id_init(SSL *ssl) {
1707 ssl->s3->tlsext_channel_id_valid = 0;
1708}
1709
1710static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1711 if (!ssl->tlsext_channel_id_enabled ||
1712 SSL_IS_DTLS(ssl)) {
1713 return 1;
1714 }
1715
1716 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1717 !CBB_add_u16(out, 0 /* length */)) {
1718 return 0;
1719 }
1720
1721 return 1;
1722}
1723
1724static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1725 CBS *contents) {
1726 if (contents == NULL) {
1727 return 1;
1728 }
1729
1730 assert(!SSL_IS_DTLS(ssl));
1731 assert(ssl->tlsext_channel_id_enabled);
1732
1733 if (CBS_len(contents) != 0) {
1734 return 0;
1735 }
1736
1737 ssl->s3->tlsext_channel_id_valid = 1;
1738 return 1;
1739}
1740
1741static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1742 CBS *contents) {
1743 if (contents == NULL ||
1744 !ssl->tlsext_channel_id_enabled ||
1745 SSL_IS_DTLS(ssl)) {
1746 return 1;
1747 }
1748
1749 if (CBS_len(contents) != 0) {
1750 return 0;
1751 }
1752
1753 ssl->s3->tlsext_channel_id_valid = 1;
1754 return 1;
1755}
1756
1757static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1758 if (!ssl->s3->tlsext_channel_id_valid) {
1759 return 1;
1760 }
1761
1762 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1763 !CBB_add_u16(out, 0 /* length */)) {
1764 return 0;
1765 }
1766
1767 return 1;
1768}
1769
Adam Langley391250d2015-07-15 19:06:07 -07001770
1771/* Secure Real-time Transport Protocol (SRTP) extension.
1772 *
1773 * https://tools.ietf.org/html/rfc5764 */
1774
Adam Langley391250d2015-07-15 19:06:07 -07001775
1776static void ext_srtp_init(SSL *ssl) {
1777 ssl->srtp_profile = NULL;
1778}
1779
1780static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1781 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1782 if (profiles == NULL) {
1783 return 1;
1784 }
1785 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1786 if (num_profiles == 0) {
1787 return 1;
1788 }
1789
1790 CBB contents, profile_ids;
1791 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1792 !CBB_add_u16_length_prefixed(out, &contents) ||
1793 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1794 return 0;
1795 }
1796
1797 size_t i;
1798 for (i = 0; i < num_profiles; i++) {
1799 if (!CBB_add_u16(&profile_ids,
1800 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1801 return 0;
1802 }
1803 }
1804
1805 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1806 !CBB_flush(out)) {
1807 return 0;
1808 }
1809
1810 return 1;
1811}
1812
1813static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1814 CBS *contents) {
1815 if (contents == NULL) {
1816 return 1;
1817 }
1818
1819 /* The extension consists of a u16-prefixed profile ID list containing a
1820 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1821 *
1822 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1823 CBS profile_ids, srtp_mki;
1824 uint16_t profile_id;
1825 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1826 !CBS_get_u16(&profile_ids, &profile_id) ||
1827 CBS_len(&profile_ids) != 0 ||
1828 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1829 CBS_len(contents) != 0) {
1830 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1831 return 0;
1832 }
1833
1834 if (CBS_len(&srtp_mki) != 0) {
1835 /* Must be no MKI, since we never offer one. */
1836 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1837 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1838 return 0;
1839 }
1840
1841 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1842
1843 /* Check to see if the server gave us something we support (and presumably
1844 * offered). */
1845 size_t i;
1846 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1847 const SRTP_PROTECTION_PROFILE *profile =
1848 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1849
1850 if (profile->id == profile_id) {
1851 ssl->srtp_profile = profile;
1852 return 1;
1853 }
1854 }
1855
1856 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1857 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1858 return 0;
1859}
1860
1861static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1862 CBS *contents) {
1863 if (contents == NULL) {
1864 return 1;
1865 }
1866
1867 CBS profile_ids, srtp_mki;
1868 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1869 CBS_len(&profile_ids) < 2 ||
1870 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1871 CBS_len(contents) != 0) {
1872 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1873 return 0;
1874 }
1875 /* Discard the MKI value for now. */
1876
1877 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1878 SSL_get_srtp_profiles(ssl);
1879
1880 /* Pick the server's most preferred profile. */
1881 size_t i;
1882 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1883 const SRTP_PROTECTION_PROFILE *server_profile =
1884 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1885
1886 CBS profile_ids_tmp;
1887 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1888
1889 while (CBS_len(&profile_ids_tmp) > 0) {
1890 uint16_t profile_id;
1891 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1892 return 0;
1893 }
1894
1895 if (server_profile->id == profile_id) {
1896 ssl->srtp_profile = server_profile;
1897 return 1;
1898 }
1899 }
1900 }
1901
1902 return 1;
1903}
1904
1905static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1906 if (ssl->srtp_profile == NULL) {
1907 return 1;
1908 }
1909
1910 CBB contents, profile_ids;
1911 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1912 !CBB_add_u16_length_prefixed(out, &contents) ||
1913 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1914 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1915 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1916 !CBB_flush(out)) {
1917 return 0;
1918 }
1919
1920 return 1;
1921}
1922
Adam Langleybdd5d662015-07-20 16:19:08 -07001923
1924/* EC point formats.
1925 *
1926 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1927
1928static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1929 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1930 return 0;
1931 }
1932
1933 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1934
1935 size_t i;
1936 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1937 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1938
1939 const uint32_t alg_k = cipher->algorithm_mkey;
1940 const uint32_t alg_a = cipher->algorithm_auth;
1941 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1942 return 1;
1943 }
1944 }
1945
1946 return 0;
1947}
1948
Adam Langleybdd5d662015-07-20 16:19:08 -07001949static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001950 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001951 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1952 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001953 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1954 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001955 !CBB_flush(out)) {
1956 return 0;
1957 }
1958
1959 return 1;
1960}
1961
1962static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1963 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1964 return 1;
1965 }
1966
1967 return ext_ec_point_add_extension(ssl, out);
1968}
1969
1970static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1971 CBS *contents) {
1972 if (contents == NULL) {
1973 return 1;
1974 }
1975
1976 CBS ec_point_format_list;
1977 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1978 CBS_len(contents) != 0) {
1979 return 0;
1980 }
1981
David Benjaminfc059942015-07-30 23:01:59 -04001982 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1983 * point format. */
1984 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1985 CBS_len(&ec_point_format_list)) == NULL) {
1986 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001987 return 0;
1988 }
1989
1990 return 1;
1991}
1992
1993static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1994 CBS *contents) {
1995 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1996}
1997
1998static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1999 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
2000 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04002001 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07002002
2003 if (!using_ecc) {
2004 return 1;
2005 }
2006
2007 return ext_ec_point_add_extension(ssl, out);
2008}
2009
Adam Langley273d49c2015-07-20 16:38:52 -07002010
2011/* EC supported curves.
2012 *
2013 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2014
2015static void ext_ec_curves_init(SSL *ssl) {
2016 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2017 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2018 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2019}
2020
2021static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2022 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2023 return 1;
2024 }
2025
2026 CBB contents, curves_bytes;
2027 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2028 !CBB_add_u16_length_prefixed(out, &contents) ||
2029 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2030 return 0;
2031 }
2032
2033 const uint16_t *curves;
2034 size_t curves_len;
2035 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2036
2037 size_t i;
2038 for (i = 0; i < curves_len; i++) {
2039 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2040 return 0;
2041 }
2042 }
2043
2044 return CBB_flush(out);
2045}
2046
2047static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2048 CBS *contents) {
2049 /* This extension is not expected to be echoed by servers and is ignored. */
2050 return 1;
2051}
2052
2053static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2054 CBS *contents) {
2055 if (contents == NULL) {
2056 return 1;
2057 }
2058
2059 CBS elliptic_curve_list;
2060 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2061 CBS_len(&elliptic_curve_list) == 0 ||
2062 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2063 CBS_len(contents) != 0) {
2064 return 0;
2065 }
2066
2067 ssl->s3->tmp.peer_ellipticcurvelist =
2068 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2069
2070 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2071 *out_alert = SSL_AD_INTERNAL_ERROR;
2072 return 0;
2073 }
2074
2075 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2076 size_t i;
2077 for (i = 0; i < num_curves; i++) {
2078 if (!CBS_get_u16(&elliptic_curve_list,
2079 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2080 goto err;
2081 }
2082 }
2083
2084 assert(CBS_len(&elliptic_curve_list) == 0);
2085 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2086
2087 return 1;
2088
2089err:
2090 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2091 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2092 *out_alert = SSL_AD_INTERNAL_ERROR;
2093 return 0;
2094}
2095
2096static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2097 /* Servers don't echo this extension. */
2098 return 1;
2099}
2100
2101
Adam Langley614c66a2015-06-12 15:26:58 -07002102/* kExtensions contains all the supported extensions. */
2103static const struct tls_extension kExtensions[] = {
2104 {
Adam Langley5021b222015-06-12 18:27:58 -07002105 /* The renegotiation extension must always be at index zero because the
2106 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2107 * sent as an SCSV. */
2108 TLSEXT_TYPE_renegotiate,
2109 NULL,
2110 ext_ri_add_clienthello,
2111 ext_ri_parse_serverhello,
2112 ext_ri_parse_clienthello,
2113 ext_ri_add_serverhello,
2114 },
2115 {
Adam Langley614c66a2015-06-12 15:26:58 -07002116 TLSEXT_TYPE_server_name,
2117 ext_sni_init,
2118 ext_sni_add_clienthello,
2119 ext_sni_parse_serverhello,
2120 ext_sni_parse_clienthello,
2121 ext_sni_add_serverhello,
2122 },
Adam Langley0a056712015-07-01 15:03:33 -07002123 {
2124 TLSEXT_TYPE_extended_master_secret,
2125 ext_ems_init,
2126 ext_ems_add_clienthello,
2127 ext_ems_parse_serverhello,
2128 ext_ems_parse_clienthello,
2129 ext_ems_add_serverhello,
2130 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002131 {
2132 TLSEXT_TYPE_session_ticket,
2133 NULL,
2134 ext_ticket_add_clienthello,
2135 ext_ticket_parse_serverhello,
2136 ext_ticket_parse_clienthello,
2137 ext_ticket_add_serverhello,
2138 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002139 {
2140 TLSEXT_TYPE_signature_algorithms,
2141 NULL,
2142 ext_sigalgs_add_clienthello,
2143 ext_sigalgs_parse_serverhello,
2144 ext_sigalgs_parse_clienthello,
2145 ext_sigalgs_add_serverhello,
2146 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002147 {
2148 TLSEXT_TYPE_status_request,
2149 ext_ocsp_init,
2150 ext_ocsp_add_clienthello,
2151 ext_ocsp_parse_serverhello,
2152 ext_ocsp_parse_clienthello,
2153 ext_ocsp_add_serverhello,
2154 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002155 {
2156 TLSEXT_TYPE_next_proto_neg,
2157 ext_npn_init,
2158 ext_npn_add_clienthello,
2159 ext_npn_parse_serverhello,
2160 ext_npn_parse_clienthello,
2161 ext_npn_add_serverhello,
2162 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002163 {
2164 TLSEXT_TYPE_certificate_timestamp,
2165 NULL,
2166 ext_sct_add_clienthello,
2167 ext_sct_parse_serverhello,
2168 ext_sct_parse_clienthello,
2169 ext_sct_add_serverhello,
2170 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002171 {
2172 TLSEXT_TYPE_application_layer_protocol_negotiation,
2173 ext_alpn_init,
2174 ext_alpn_add_clienthello,
2175 ext_alpn_parse_serverhello,
2176 ext_alpn_parse_clienthello,
2177 ext_alpn_add_serverhello,
2178 },
Adam Langley49c7af12015-07-10 14:33:46 -07002179 {
2180 TLSEXT_TYPE_channel_id,
2181 ext_channel_id_init,
2182 ext_channel_id_add_clienthello,
2183 ext_channel_id_parse_serverhello,
2184 ext_channel_id_parse_clienthello,
2185 ext_channel_id_add_serverhello,
2186 },
Adam Langley391250d2015-07-15 19:06:07 -07002187 {
2188 TLSEXT_TYPE_srtp,
2189 ext_srtp_init,
2190 ext_srtp_add_clienthello,
2191 ext_srtp_parse_serverhello,
2192 ext_srtp_parse_clienthello,
2193 ext_srtp_add_serverhello,
2194 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002195 {
2196 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002197 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002198 ext_ec_point_add_clienthello,
2199 ext_ec_point_parse_serverhello,
2200 ext_ec_point_parse_clienthello,
2201 ext_ec_point_add_serverhello,
2202 },
Adam Langley273d49c2015-07-20 16:38:52 -07002203 {
2204 TLSEXT_TYPE_elliptic_curves,
2205 ext_ec_curves_init,
2206 ext_ec_curves_add_clienthello,
2207 ext_ec_curves_parse_serverhello,
2208 ext_ec_curves_parse_clienthello,
2209 ext_ec_curves_add_serverhello,
2210 },
Adam Langley614c66a2015-06-12 15:26:58 -07002211};
2212
2213#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2214
Adam Langley4cfa96b2015-07-01 11:56:55 -07002215OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2216 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002217 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002218OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2219 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2220 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002221 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002222
Adam Langley614c66a2015-06-12 15:26:58 -07002223static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2224 uint16_t value) {
2225 unsigned i;
2226 for (i = 0; i < kNumExtensions; i++) {
2227 if (kExtensions[i].value == value) {
2228 *out_index = i;
2229 return &kExtensions[i];
2230 }
2231 }
2232
2233 return NULL;
2234}
2235
Adam Langley09505632015-07-30 18:10:13 -07002236int SSL_extension_supported(unsigned extension_value) {
2237 uint32_t index;
2238 return extension_value == TLSEXT_TYPE_padding ||
2239 tls_extension_find(&index, extension_value) != NULL;
2240}
2241
Adam Langleyb0c235e2014-06-20 12:00:00 -07002242/* header_len is the length of the ClientHello header written so far, used to
2243 * compute padding. It does not include the record header. Pass 0 if no padding
2244 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002245uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2246 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2248 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002249 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 }
Adam Langley95c29f32014-06-20 12:00:00 -07002251
Adam Langley33ad2b52015-07-20 17:43:53 -07002252 CBB cbb, extensions;
2253 CBB_zero(&cbb);
2254 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2255 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2256 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 }
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langley614c66a2015-06-12 15:26:58 -07002259 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002260 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langley614c66a2015-06-12 15:26:58 -07002262 size_t i;
2263 for (i = 0; i < kNumExtensions; i++) {
2264 if (kExtensions[i].init != NULL) {
2265 kExtensions[i].init(s);
2266 }
2267 }
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langley614c66a2015-06-12 15:26:58 -07002269 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002270 const size_t len_before = CBB_len(&extensions);
2271 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2272 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2273 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2274 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langley33ad2b52015-07-20 17:43:53 -07002277 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002278 s->s3->tmp.extensions.sent |= (1u << i);
2279 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002280 }
Adam Langley75712922014-10-10 16:23:43 -07002281
Adam Langley09505632015-07-30 18:10:13 -07002282 if (!custom_ext_add_clienthello(s, &extensions)) {
2283 goto err;
2284 }
2285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002287 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 if (header_len > 0xff && header_len < 0x200) {
2289 /* Add padding to workaround bugs in F5 terminators. See
2290 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2291 *
2292 * NB: because this code works out the length of all existing extensions
2293 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002294 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 /* Extensions take at least four bytes to encode. Always include least
2296 * one byte of data if including the extension. WebSphere Application
2297 * Server 7.0 is intolerant to the last extension being zero-length. */
2298 if (padding_len >= 4 + 1) {
2299 padding_len -= 4;
2300 } else {
2301 padding_len = 1;
2302 }
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langley33ad2b52015-07-20 17:43:53 -07002304 uint8_t *padding_bytes;
2305 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2306 !CBB_add_u16(&extensions, padding_len) ||
2307 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2308 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002309 }
Adam Langley75712922014-10-10 16:23:43 -07002310
Adam Langley33ad2b52015-07-20 17:43:53 -07002311 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 }
2313 }
Adam Langley75712922014-10-10 16:23:43 -07002314
Adam Langley33ad2b52015-07-20 17:43:53 -07002315 if (!CBB_flush(&cbb)) {
2316 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002317 }
Adam Langley95c29f32014-06-20 12:00:00 -07002318
Adam Langley33ad2b52015-07-20 17:43:53 -07002319 uint8_t *ret = buf;
2320 const size_t cbb_len = CBB_len(&cbb);
2321 /* If only two bytes have been written then the extensions are actually empty
2322 * and those two bytes are the zero length. In that case, we don't bother
2323 * sending the extensions length. */
2324 if (cbb_len > 2) {
2325 ret += cbb_len;
2326 }
2327
2328 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002330
2331err:
2332 CBB_cleanup(&cbb);
2333 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2334 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002335}
Adam Langley95c29f32014-06-20 12:00:00 -07002336
Adam Langley614c66a2015-06-12 15:26:58 -07002337uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2338 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2340 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002341 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002342 }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langley33ad2b52015-07-20 17:43:53 -07002344 CBB cbb, extensions;
2345 CBB_zero(&cbb);
2346 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2347 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2348 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002349 }
2350
2351 unsigned i;
2352 for (i = 0; i < kNumExtensions; i++) {
2353 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2354 /* Don't send extensions that were not received. */
2355 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002356 }
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langley33ad2b52015-07-20 17:43:53 -07002358 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2359 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2360 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2361 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002362 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 }
Adam Langley95c29f32014-06-20 12:00:00 -07002364
Adam Langley09505632015-07-30 18:10:13 -07002365 if (!custom_ext_add_serverhello(s, &extensions)) {
2366 goto err;
2367 }
2368
Adam Langley33ad2b52015-07-20 17:43:53 -07002369 if (!CBB_flush(&cbb)) {
2370 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 }
2372
Adam Langley33ad2b52015-07-20 17:43:53 -07002373 uint8_t *ret = buf;
2374 const size_t cbb_len = CBB_len(&cbb);
2375 /* If only two bytes have been written then the extensions are actually empty
2376 * and those two bytes are the zero length. In that case, we don't bother
2377 * sending the extensions length. */
2378 if (cbb_len > 2) {
2379 ret += cbb_len;
2380 }
2381
2382 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002383 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002384
2385err:
2386 CBB_cleanup(&cbb);
2387 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2388 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002389}
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002392 size_t i;
2393 for (i = 0; i < kNumExtensions; i++) {
2394 if (kExtensions[i].init != NULL) {
2395 kExtensions[i].init(s);
2396 }
2397 }
2398
2399 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002400 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002401 /* The renegotiation extension must always be at index zero because the
2402 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2403 * sent as an SCSV. */
2404 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002407 if (CBS_len(cbs) != 0) {
2408 /* Decode the extensions block and check it is valid. */
2409 CBS extensions;
2410 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2411 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002412 *out_alert = SSL_AD_DECODE_ERROR;
2413 return 0;
2414 }
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langley33ad2b52015-07-20 17:43:53 -07002416 while (CBS_len(&extensions) != 0) {
2417 uint16_t type;
2418 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langley33ad2b52015-07-20 17:43:53 -07002420 /* Decode the next extension. */
2421 if (!CBS_get_u16(&extensions, &type) ||
2422 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2423 *out_alert = SSL_AD_DECODE_ERROR;
2424 return 0;
2425 }
2426
2427 unsigned ext_index;
2428 const struct tls_extension *const ext =
2429 tls_extension_find(&ext_index, type);
2430
2431 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002432 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2433 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2434 return 0;
2435 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002436 continue;
2437 }
2438
Adam Langley614c66a2015-06-12 15:26:58 -07002439 s->s3->tmp.extensions.received |= (1u << ext_index);
2440 uint8_t alert = SSL_AD_DECODE_ERROR;
2441 if (!ext->parse_clienthello(s, &alert, &extension)) {
2442 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002443 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2444 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002445 return 0;
2446 }
Adam Langley614c66a2015-06-12 15:26:58 -07002447 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 }
Adam Langley75712922014-10-10 16:23:43 -07002449
Adam Langley614c66a2015-06-12 15:26:58 -07002450 for (i = 0; i < kNumExtensions; i++) {
2451 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2452 /* Extension wasn't observed so call the callback with a NULL
2453 * parameter. */
2454 uint8_t alert = SSL_AD_DECODE_ERROR;
2455 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002456 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2457 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002458 *out_alert = alert;
2459 return 0;
2460 }
2461 }
2462 }
2463
Adam Langleyfcf25832014-12-18 17:42:32 -08002464 return 1;
2465}
Adam Langley95c29f32014-06-20 12:00:00 -07002466
Adam Langleyfcf25832014-12-18 17:42:32 -08002467int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2468 int alert = -1;
2469 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2470 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2471 return 0;
2472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002475 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 return 0;
2477 }
Adam Langley95c29f32014-06-20 12:00:00 -07002478
Adam Langleyfcf25832014-12-18 17:42:32 -08002479 return 1;
2480}
Adam Langley95c29f32014-06-20 12:00:00 -07002481
Adam Langleyfcf25832014-12-18 17:42:32 -08002482static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002483 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002484 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002485
Adam Langley33ad2b52015-07-20 17:43:53 -07002486 if (CBS_len(cbs) != 0) {
2487 /* Decode the extensions block and check it is valid. */
2488 CBS extensions;
2489 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2490 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002491 *out_alert = SSL_AD_DECODE_ERROR;
2492 return 0;
2493 }
Adam Langley95c29f32014-06-20 12:00:00 -07002494
Adam Langley614c66a2015-06-12 15:26:58 -07002495
Adam Langley33ad2b52015-07-20 17:43:53 -07002496 while (CBS_len(&extensions) != 0) {
2497 uint16_t type;
2498 CBS extension;
2499
2500 /* Decode the next extension. */
2501 if (!CBS_get_u16(&extensions, &type) ||
2502 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2503 *out_alert = SSL_AD_DECODE_ERROR;
2504 return 0;
2505 }
2506
2507 unsigned ext_index;
2508 const struct tls_extension *const ext =
2509 tls_extension_find(&ext_index, type);
2510
Adam Langley09505632015-07-30 18:10:13 -07002511 if (ext == NULL) {
2512 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2513 return 0;
2514 }
2515 continue;
2516 }
2517
2518 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2519 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002520 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002521 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 *out_alert = SSL_AD_DECODE_ERROR;
2523 return 0;
2524 }
David Benjamin03973092014-06-24 23:27:17 -04002525
Adam Langley614c66a2015-06-12 15:26:58 -07002526 received |= (1u << ext_index);
2527
2528 uint8_t alert = SSL_AD_DECODE_ERROR;
2529 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002530 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2531 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002532 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return 0;
2534 }
Adam Langley614c66a2015-06-12 15:26:58 -07002535 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 }
Adam Langley95c29f32014-06-20 12:00:00 -07002537
Adam Langley33ad2b52015-07-20 17:43:53 -07002538 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002539 for (i = 0; i < kNumExtensions; i++) {
2540 if (!(received & (1u << i))) {
2541 /* Extension wasn't observed so call the callback with a NULL
2542 * parameter. */
2543 uint8_t alert = SSL_AD_DECODE_ERROR;
2544 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002545 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2546 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002547 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 return 0;
2549 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 }
2551 }
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553 return 1;
2554}
Adam Langley95c29f32014-06-20 12:00:00 -07002555
Adam Langleyfcf25832014-12-18 17:42:32 -08002556static int ssl_check_clienthello_tlsext(SSL *s) {
2557 int ret = SSL_TLSEXT_ERR_NOACK;
2558 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002559
Adam Langleyfcf25832014-12-18 17:42:32 -08002560 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2561 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2564 ret = s->ctx->tlsext_servername_callback(s, &al,
2565 s->ctx->tlsext_servername_arg);
2566 } else if (s->initial_ctx != NULL &&
2567 s->initial_ctx->tlsext_servername_callback != 0) {
2568 ret = s->initial_ctx->tlsext_servername_callback(
2569 s, &al, s->initial_ctx->tlsext_servername_arg);
2570 }
Adam Langley95c29f32014-06-20 12:00:00 -07002571
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 switch (ret) {
2573 case SSL_TLSEXT_ERR_ALERT_FATAL:
2574 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2575 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 case SSL_TLSEXT_ERR_ALERT_WARNING:
2578 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2579 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002582 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002583 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 default:
2586 return 1;
2587 }
2588}
Adam Langleyed8270a2014-09-02 13:52:56 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002591 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2595 ret = s->ctx->tlsext_servername_callback(s, &al,
2596 s->ctx->tlsext_servername_arg);
2597 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002598 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 ret = s->initial_ctx->tlsext_servername_callback(
2600 s, &al, s->initial_ctx->tlsext_servername_arg);
2601 }
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 switch (ret) {
2604 case SSL_TLSEXT_ERR_ALERT_FATAL:
2605 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2606 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002607
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 case SSL_TLSEXT_ERR_ALERT_WARNING:
2609 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2610 return 1;
2611
2612 default:
2613 return 1;
2614 }
2615}
2616
2617int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2618 int alert = -1;
2619 if (s->version < SSL3_VERSION) {
2620 return 1;
2621 }
2622
2623 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2624 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2625 return 0;
2626 }
2627
2628 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002629 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002630 return 0;
2631 }
2632
2633 return 1;
2634}
Adam Langley95c29f32014-06-20 12:00:00 -07002635
David Benjamine3aa1d92015-06-16 15:34:50 -04002636int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2637 int *out_send_ticket, const uint8_t *ticket,
2638 size_t ticket_len, const uint8_t *session_id,
2639 size_t session_id_len) {
2640 int ret = 1; /* Most errors are non-fatal. */
2641 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2642 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002643
David Benjamine3aa1d92015-06-16 15:34:50 -04002644 HMAC_CTX hmac_ctx;
2645 HMAC_CTX_init(&hmac_ctx);
2646 EVP_CIPHER_CTX cipher_ctx;
2647 EVP_CIPHER_CTX_init(&cipher_ctx);
2648
2649 *out_send_ticket = 0;
2650 *out_session = NULL;
2651
2652 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2653 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 }
2655
David Benjamine3aa1d92015-06-16 15:34:50 -04002656 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002658 *out_send_ticket = 1;
2659 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 }
2661
David Benjaminadcc3952015-04-26 13:07:57 -04002662 /* Ensure there is room for the key name and the largest IV
2663 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2664 * the maximum IV length should be well under the minimum size for the
2665 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002666 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2667 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002669 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002670
David Benjamine3aa1d92015-06-16 15:34:50 -04002671 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2672 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2673 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2674 0 /* decrypt */);
2675 if (cb_ret < 0) {
2676 ret = 0;
2677 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002679 if (cb_ret == 0) {
2680 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002682 if (cb_ret == 2) {
2683 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 }
2685 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002686 /* Check the key name matches. */
2687 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2688 SSL_TICKET_KEY_NAME_LEN) != 0) {
2689 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002691 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2692 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002694 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2695 ssl_ctx->tlsext_tick_aes_key, iv)) {
2696 ret = 0;
2697 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 }
2699 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002700 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002701
David Benjamine3aa1d92015-06-16 15:34:50 -04002702 /* Check the MAC at the end of the ticket. */
2703 uint8_t mac[EVP_MAX_MD_SIZE];
2704 size_t mac_len = HMAC_size(&hmac_ctx);
2705 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002706 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002707 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002709 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2710 HMAC_Final(&hmac_ctx, mac, NULL);
2711 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2712 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 }
2714
David Benjamine3aa1d92015-06-16 15:34:50 -04002715 /* Decrypt the session data. */
2716 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2717 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2718 mac_len;
2719 plaintext = OPENSSL_malloc(ciphertext_len);
2720 if (plaintext == NULL) {
2721 ret = 0;
2722 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002724 if (ciphertext_len >= INT_MAX) {
2725 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002726 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002727 int len1, len2;
2728 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2729 (int)ciphertext_len) ||
2730 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2731 ERR_clear_error(); /* Don't leave an error on the queue. */
2732 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 }
2734
David Benjamine3aa1d92015-06-16 15:34:50 -04002735 /* Decode the session. */
2736 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2737 if (session == NULL) {
2738 ERR_clear_error(); /* Don't leave an error on the queue. */
2739 goto done;
2740 }
2741
2742 /* Copy the client's session ID into the new session, to denote the ticket has
2743 * been accepted. */
2744 memcpy(session->session_id, session_id, session_id_len);
2745 session->session_id_length = session_id_len;
2746
2747 *out_session = session;
2748
2749done:
2750 OPENSSL_free(plaintext);
2751 HMAC_CTX_cleanup(&hmac_ctx);
2752 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2753 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002754}
Adam Langley95c29f32014-06-20 12:00:00 -07002755
2756/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002757typedef struct {
2758 int nid;
2759 int id;
2760} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002761
Adam Langleyfcf25832014-12-18 17:42:32 -08002762static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2763 {NID_sha1, TLSEXT_hash_sha1},
2764 {NID_sha224, TLSEXT_hash_sha224},
2765 {NID_sha256, TLSEXT_hash_sha256},
2766 {NID_sha384, TLSEXT_hash_sha384},
2767 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002768
Adam Langleyfcf25832014-12-18 17:42:32 -08002769static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2770 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002771
Adam Langleyfcf25832014-12-18 17:42:32 -08002772static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2773 size_t i;
2774 for (i = 0; i < tlen; i++) {
2775 if (table[i].nid == nid) {
2776 return table[i].id;
2777 }
2778 }
Adam Langley95c29f32014-06-20 12:00:00 -07002779
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 return -1;
2781}
Adam Langley95c29f32014-06-20 12:00:00 -07002782
David Benjaminb4d65fd2015-05-29 17:11:21 -04002783int tls12_get_sigid(int pkey_type) {
2784 return tls12_find_id(pkey_type, tls12_sig,
2785 sizeof(tls12_sig) / sizeof(tls12_lookup));
2786}
2787
David Benjamind1d80782015-07-05 11:54:09 -04002788int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002789 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002790
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 if (!md) {
2792 return 0;
2793 }
Adam Langley95c29f32014-06-20 12:00:00 -07002794
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2796 sizeof(tls12_md) / sizeof(tls12_lookup));
2797 if (md_id == -1) {
2798 return 0;
2799 }
Adam Langley95c29f32014-06-20 12:00:00 -07002800
David Benjamind1d80782015-07-05 11:54:09 -04002801 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 if (sig_id == -1) {
2803 return 0;
2804 }
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 p[0] = (uint8_t)md_id;
2807 p[1] = (uint8_t)sig_id;
2808 return 1;
2809}
2810
Adam Langleyfcf25832014-12-18 17:42:32 -08002811const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2812 switch (hash_alg) {
2813 case TLSEXT_hash_md5:
2814 return EVP_md5();
2815
2816 case TLSEXT_hash_sha1:
2817 return EVP_sha1();
2818
2819 case TLSEXT_hash_sha224:
2820 return EVP_sha224();
2821
2822 case TLSEXT_hash_sha256:
2823 return EVP_sha256();
2824
2825 case TLSEXT_hash_sha384:
2826 return EVP_sha384();
2827
2828 case TLSEXT_hash_sha512:
2829 return EVP_sha512();
2830
2831 default:
2832 return NULL;
2833 }
2834}
Adam Langley95c29f32014-06-20 12:00:00 -07002835
David Benjaminec2f27d2014-11-13 19:17:25 -05002836/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2837 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002838static int tls12_get_pkey_type(uint8_t sig_alg) {
2839 switch (sig_alg) {
2840 case TLSEXT_signature_rsa:
2841 return EVP_PKEY_RSA;
2842
2843 case TLSEXT_signature_ecdsa:
2844 return EVP_PKEY_EC;
2845
2846 default:
2847 return -1;
2848 }
2849}
Adam Langley95c29f32014-06-20 12:00:00 -07002850
Adam Langley95c29f32014-06-20 12:00:00 -07002851/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002852static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2853 size_t preflen, const uint8_t *allow,
2854 size_t allowlen) {
2855 const uint8_t *ptmp, *atmp;
2856 size_t i, j, nmatch = 0;
2857
2858 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2859 /* Skip disabled hashes or signature algorithms */
2860 if (tls12_get_hash(ptmp[0]) == NULL ||
2861 tls12_get_pkey_type(ptmp[1]) == -1) {
2862 continue;
2863 }
2864
2865 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2866 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2867 nmatch++;
2868 if (shsig) {
2869 shsig->rhash = ptmp[0];
2870 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002871 shsig++;
2872 }
2873
2874 break;
2875 }
2876 }
2877 }
2878
2879 return nmatch;
2880}
Adam Langley95c29f32014-06-20 12:00:00 -07002881
2882/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002883static int tls1_set_shared_sigalgs(SSL *s) {
2884 const uint8_t *pref, *allow, *conf;
2885 size_t preflen, allowlen, conflen;
2886 size_t nmatch;
2887 TLS_SIGALGS *salgs = NULL;
2888 CERT *c = s->cert;
2889
David Benjamin2755a3e2015-04-22 16:17:58 -04002890 OPENSSL_free(c->shared_sigalgs);
2891 c->shared_sigalgs = NULL;
2892 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002893
David Benjamin3dd90162015-08-09 11:26:40 -04002894 conflen = tls12_get_psigalgs(s, &conf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002895
2896 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2897 pref = conf;
2898 preflen = conflen;
2899 allow = c->peer_sigalgs;
2900 allowlen = c->peer_sigalgslen;
2901 } else {
2902 allow = conf;
2903 allowlen = conflen;
2904 pref = c->peer_sigalgs;
2905 preflen = c->peer_sigalgslen;
2906 }
2907
2908 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2909 if (!nmatch) {
2910 return 1;
2911 }
2912
2913 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2914 if (!salgs) {
2915 return 0;
2916 }
2917
2918 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2919 c->shared_sigalgs = salgs;
2920 c->shared_sigalgslen = nmatch;
2921 return 1;
2922}
Adam Langley95c29f32014-06-20 12:00:00 -07002923
2924/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002925int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2926 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002927
Adam Langleyfcf25832014-12-18 17:42:32 -08002928 /* Extension ignored for inappropriate versions */
2929 if (!SSL_USE_SIGALGS(s)) {
2930 return 1;
2931 }
David Benjamincd996942014-07-20 16:23:51 -04002932
David Benjamin61c0d4e2015-03-19 14:24:37 -04002933 if (CBS_len(sigalgs) % 2 != 0 ||
2934 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2935 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 return 0;
2937 }
Adam Langley95c29f32014-06-20 12:00:00 -07002938
Adam Langleyfcf25832014-12-18 17:42:32 -08002939 return 1;
2940}
David Benjaminec2f27d2014-11-13 19:17:25 -05002941
David Benjamind1d80782015-07-05 11:54:09 -04002942const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2943 CERT *cert = ssl->cert;
2944 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002946
Adam Langleyfcf25832014-12-18 17:42:32 -08002947 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002948 for (i = 0; i < cert->shared_sigalgslen; i++) {
2949 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002951 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2952 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002953 continue;
2954 }
2955 return md;
2956 }
Adam Langley95c29f32014-06-20 12:00:00 -07002957
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 /* If no suitable digest may be found, default to SHA-1. */
2959 return EVP_sha1();
2960}
Adam Langley95c29f32014-06-20 12:00:00 -07002961
David Benjamind6a4ae92015-08-06 11:10:51 -04002962int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2963 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002965
2966 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002967 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2968 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002970
David Benjamind6a4ae92015-08-06 11:10:51 -04002971 static const char kClientIDMagic[] = "TLS Channel ID signature";
2972 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2973
2974 if (ssl->hit) {
2975 static const char kResumptionMagic[] = "Resumption";
2976 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2977 if (ssl->session->original_handshake_hash_len == 0) {
2978 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2979 goto err;
2980 }
2981 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2982 ssl->session->original_handshake_hash_len);
2983 }
2984
2985 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2986 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2987 sizeof(handshake_hash));
2988 if (handshake_hash_len < 0) {
2989 goto err;
2990 }
2991 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2992 unsigned len_u;
2993 EVP_DigestFinal_ex(&ctx, out, &len_u);
2994 *out_len = len_u;
2995
2996 ret = 1;
2997
2998err:
2999 EVP_MD_CTX_cleanup(&ctx);
3000 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003001}
Adam Langley1258b6a2014-06-20 12:00:00 -07003002
3003/* tls1_record_handshake_hashes_for_channel_id records the current handshake
3004 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08003005int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
3006 int digest_len;
3007 /* This function should never be called for a resumed session because the
3008 * handshake hashes that we wish to record are for the original, full
3009 * handshake. */
3010 if (s->hit) {
3011 return -1;
3012 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003013
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 digest_len =
3015 tls1_handshake_digest(s, s->session->original_handshake_hash,
3016 sizeof(s->session->original_handshake_hash));
3017 if (digest_len < 0) {
3018 return -1;
3019 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003020
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003022
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 return 1;
3024}