blob: 747b7f6df4d81b6628df63de71b2f722e571be3c [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 Lietar62be8ac2015-09-16 10:03:30 +01001367 /* The extension shouldn't be sent when resuming sessions. */
1368 if (ssl->hit ||
1369 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001370 ssl->ctx->ocsp_response_length == 0) {
1371 return 1;
1372 }
1373
1374 ssl->s3->tmp.certificate_status_expected = 1;
1375
1376 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1377 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001378}
1379
1380
Adam Langley97dfcbf2015-07-01 18:35:20 -07001381/* Next protocol negotiation.
1382 *
1383 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1384
1385static void ext_npn_init(SSL *ssl) {
1386 ssl->s3->next_proto_neg_seen = 0;
1387}
1388
1389static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1390 if (ssl->s3->initial_handshake_complete ||
1391 ssl->ctx->next_proto_select_cb == NULL ||
1392 SSL_IS_DTLS(ssl)) {
1393 return 1;
1394 }
1395
1396 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1397 !CBB_add_u16(out, 0 /* length */)) {
1398 return 0;
1399 }
1400
1401 return 1;
1402}
1403
1404static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1405 CBS *contents) {
1406 if (contents == NULL) {
1407 return 1;
1408 }
1409
1410 /* If any of these are false then we should never have sent the NPN
1411 * extension in the ClientHello and thus this function should never have been
1412 * called. */
1413 assert(!ssl->s3->initial_handshake_complete);
1414 assert(!SSL_IS_DTLS(ssl));
1415 assert(ssl->ctx->next_proto_select_cb != NULL);
1416
David Benjamin76c2efc2015-08-31 14:24:29 -04001417 if (ssl->s3->alpn_selected != NULL) {
1418 /* NPN and ALPN may not be negotiated in the same connection. */
1419 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1420 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1421 return 0;
1422 }
1423
Adam Langley97dfcbf2015-07-01 18:35:20 -07001424 const uint8_t *const orig_contents = CBS_data(contents);
1425 const size_t orig_len = CBS_len(contents);
1426
1427 while (CBS_len(contents) != 0) {
1428 CBS proto;
1429 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1430 CBS_len(&proto) == 0) {
1431 return 0;
1432 }
1433 }
1434
1435 uint8_t *selected;
1436 uint8_t selected_len;
1437 if (ssl->ctx->next_proto_select_cb(
1438 ssl, &selected, &selected_len, orig_contents, orig_len,
1439 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1440 *out_alert = SSL_AD_INTERNAL_ERROR;
1441 return 0;
1442 }
1443
1444 OPENSSL_free(ssl->next_proto_negotiated);
1445 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1446 if (ssl->next_proto_negotiated == NULL) {
1447 *out_alert = SSL_AD_INTERNAL_ERROR;
1448 return 0;
1449 }
1450
1451 ssl->next_proto_negotiated_len = selected_len;
1452 ssl->s3->next_proto_neg_seen = 1;
1453
1454 return 1;
1455}
1456
1457static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1458 CBS *contents) {
1459 if (contents != NULL && CBS_len(contents) != 0) {
1460 return 0;
1461 }
1462
1463 if (contents == NULL ||
1464 ssl->s3->initial_handshake_complete ||
1465 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1466 * afterwards, parsing the ALPN extension will clear
1467 * |next_proto_neg_seen|. */
1468 ssl->s3->alpn_selected != NULL ||
1469 ssl->ctx->next_protos_advertised_cb == NULL ||
1470 SSL_IS_DTLS(ssl)) {
1471 return 1;
1472 }
1473
1474 ssl->s3->next_proto_neg_seen = 1;
1475 return 1;
1476}
1477
1478static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1479 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1480 * parsed. */
1481 if (!ssl->s3->next_proto_neg_seen) {
1482 return 1;
1483 }
1484
1485 const uint8_t *npa;
1486 unsigned npa_len;
1487
1488 if (ssl->ctx->next_protos_advertised_cb(
1489 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1490 SSL_TLSEXT_ERR_OK) {
1491 ssl->s3->next_proto_neg_seen = 0;
1492 return 1;
1493 }
1494
1495 CBB contents;
1496 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1497 !CBB_add_u16_length_prefixed(out, &contents) ||
1498 !CBB_add_bytes(&contents, npa, npa_len) ||
1499 !CBB_flush(out)) {
1500 return 0;
1501 }
1502
1503 return 1;
1504}
1505
1506
Adam Langleyab8d87d2015-07-10 12:21:39 -07001507/* Signed certificate timestamps.
1508 *
1509 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1510
1511static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1512 if (!ssl->signed_cert_timestamps_enabled) {
1513 return 1;
1514 }
1515
1516 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1517 !CBB_add_u16(out, 0 /* length */)) {
1518 return 0;
1519 }
1520
1521 return 1;
1522}
1523
1524static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1525 CBS *contents) {
1526 if (contents == NULL) {
1527 return 1;
1528 }
1529
1530 /* If this is false then we should never have sent the SCT extension in the
1531 * ClientHello and thus this function should never have been called. */
1532 assert(ssl->signed_cert_timestamps_enabled);
1533
1534 if (CBS_len(contents) == 0) {
1535 *out_alert = SSL_AD_DECODE_ERROR;
1536 return 0;
1537 }
1538
1539 /* Session resumption uses the original session information. */
1540 if (!ssl->hit &&
1541 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1542 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1543 *out_alert = SSL_AD_INTERNAL_ERROR;
1544 return 0;
1545 }
1546
1547 return 1;
1548}
1549
1550static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1551 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001552 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001553}
1554
1555static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001556 /* The extension shouldn't be sent when resuming sessions. */
1557 if (ssl->hit ||
1558 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001559 return 1;
1560 }
1561
1562 CBB contents;
1563 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1564 CBB_add_u16_length_prefixed(out, &contents) &&
1565 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1566 ssl->ctx->signed_cert_timestamp_list_length) &&
1567 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001568}
1569
1570
Adam Langleyf18e4532015-07-10 13:39:53 -07001571/* Application-level Protocol Negotiation.
1572 *
1573 * https://tools.ietf.org/html/rfc7301 */
1574
1575static void ext_alpn_init(SSL *ssl) {
1576 OPENSSL_free(ssl->s3->alpn_selected);
1577 ssl->s3->alpn_selected = NULL;
1578}
1579
1580static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1581 if (ssl->alpn_client_proto_list == NULL ||
1582 ssl->s3->initial_handshake_complete) {
1583 return 1;
1584 }
1585
1586 CBB contents, proto_list;
1587 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1588 !CBB_add_u16_length_prefixed(out, &contents) ||
1589 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1590 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1591 ssl->alpn_client_proto_list_len) ||
1592 !CBB_flush(out)) {
1593 return 0;
1594 }
1595
1596 return 1;
1597}
1598
1599static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1600 CBS *contents) {
1601 if (contents == NULL) {
1602 return 1;
1603 }
1604
1605 assert(!ssl->s3->initial_handshake_complete);
1606 assert(ssl->alpn_client_proto_list != NULL);
1607
David Benjamin76c2efc2015-08-31 14:24:29 -04001608 if (ssl->s3->next_proto_neg_seen) {
1609 /* NPN and ALPN may not be negotiated in the same connection. */
1610 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1611 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1612 return 0;
1613 }
1614
Adam Langleyf18e4532015-07-10 13:39:53 -07001615 /* The extension data consists of a ProtocolNameList which must have
1616 * exactly one ProtocolName. Each of these is length-prefixed. */
1617 CBS protocol_name_list, protocol_name;
1618 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1619 CBS_len(contents) != 0 ||
1620 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1621 /* Empty protocol names are forbidden. */
1622 CBS_len(&protocol_name) == 0 ||
1623 CBS_len(&protocol_name_list) != 0) {
1624 return 0;
1625 }
1626
1627 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1628 &ssl->s3->alpn_selected_len)) {
1629 *out_alert = SSL_AD_INTERNAL_ERROR;
1630 return 0;
1631 }
1632
1633 return 1;
1634}
1635
1636static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1637 CBS *contents) {
1638 if (contents == NULL) {
1639 return 1;
1640 }
1641
1642 if (ssl->ctx->alpn_select_cb == NULL ||
1643 ssl->s3->initial_handshake_complete) {
1644 return 1;
1645 }
1646
1647 /* ALPN takes precedence over NPN. */
1648 ssl->s3->next_proto_neg_seen = 0;
1649
1650 CBS protocol_name_list;
1651 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1652 CBS_len(contents) != 0 ||
1653 CBS_len(&protocol_name_list) < 2) {
1654 return 0;
1655 }
1656
1657 /* Validate the protocol list. */
1658 CBS protocol_name_list_copy = protocol_name_list;
1659 while (CBS_len(&protocol_name_list_copy) > 0) {
1660 CBS protocol_name;
1661
1662 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1663 /* Empty protocol names are forbidden. */
1664 CBS_len(&protocol_name) == 0) {
1665 return 0;
1666 }
1667 }
1668
1669 const uint8_t *selected;
1670 uint8_t selected_len;
1671 if (ssl->ctx->alpn_select_cb(
1672 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1673 CBS_len(&protocol_name_list),
1674 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1675 OPENSSL_free(ssl->s3->alpn_selected);
1676 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1677 if (ssl->s3->alpn_selected == NULL) {
1678 *out_alert = SSL_AD_INTERNAL_ERROR;
1679 return 0;
1680 }
1681 ssl->s3->alpn_selected_len = selected_len;
1682 }
1683
1684 return 1;
1685}
1686
1687static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1688 if (ssl->s3->alpn_selected == NULL) {
1689 return 1;
1690 }
1691
1692 CBB contents, proto_list, proto;
1693 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1694 !CBB_add_u16_length_prefixed(out, &contents) ||
1695 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1696 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1697 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1698 !CBB_flush(out)) {
1699 return 0;
1700 }
1701
1702 return 1;
1703}
1704
1705
Adam Langley49c7af12015-07-10 14:33:46 -07001706/* Channel ID.
1707 *
1708 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1709
1710static void ext_channel_id_init(SSL *ssl) {
1711 ssl->s3->tlsext_channel_id_valid = 0;
1712}
1713
1714static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1715 if (!ssl->tlsext_channel_id_enabled ||
1716 SSL_IS_DTLS(ssl)) {
1717 return 1;
1718 }
1719
1720 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1721 !CBB_add_u16(out, 0 /* length */)) {
1722 return 0;
1723 }
1724
1725 return 1;
1726}
1727
1728static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1729 CBS *contents) {
1730 if (contents == NULL) {
1731 return 1;
1732 }
1733
1734 assert(!SSL_IS_DTLS(ssl));
1735 assert(ssl->tlsext_channel_id_enabled);
1736
1737 if (CBS_len(contents) != 0) {
1738 return 0;
1739 }
1740
1741 ssl->s3->tlsext_channel_id_valid = 1;
1742 return 1;
1743}
1744
1745static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1746 CBS *contents) {
1747 if (contents == NULL ||
1748 !ssl->tlsext_channel_id_enabled ||
1749 SSL_IS_DTLS(ssl)) {
1750 return 1;
1751 }
1752
1753 if (CBS_len(contents) != 0) {
1754 return 0;
1755 }
1756
1757 ssl->s3->tlsext_channel_id_valid = 1;
1758 return 1;
1759}
1760
1761static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1762 if (!ssl->s3->tlsext_channel_id_valid) {
1763 return 1;
1764 }
1765
1766 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1767 !CBB_add_u16(out, 0 /* length */)) {
1768 return 0;
1769 }
1770
1771 return 1;
1772}
1773
Adam Langley391250d2015-07-15 19:06:07 -07001774
1775/* Secure Real-time Transport Protocol (SRTP) extension.
1776 *
1777 * https://tools.ietf.org/html/rfc5764 */
1778
Adam Langley391250d2015-07-15 19:06:07 -07001779
1780static void ext_srtp_init(SSL *ssl) {
1781 ssl->srtp_profile = NULL;
1782}
1783
1784static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1785 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1786 if (profiles == NULL) {
1787 return 1;
1788 }
1789 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1790 if (num_profiles == 0) {
1791 return 1;
1792 }
1793
1794 CBB contents, profile_ids;
1795 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1796 !CBB_add_u16_length_prefixed(out, &contents) ||
1797 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1798 return 0;
1799 }
1800
1801 size_t i;
1802 for (i = 0; i < num_profiles; i++) {
1803 if (!CBB_add_u16(&profile_ids,
1804 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1805 return 0;
1806 }
1807 }
1808
1809 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1810 !CBB_flush(out)) {
1811 return 0;
1812 }
1813
1814 return 1;
1815}
1816
1817static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1818 CBS *contents) {
1819 if (contents == NULL) {
1820 return 1;
1821 }
1822
1823 /* The extension consists of a u16-prefixed profile ID list containing a
1824 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1825 *
1826 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1827 CBS profile_ids, srtp_mki;
1828 uint16_t profile_id;
1829 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1830 !CBS_get_u16(&profile_ids, &profile_id) ||
1831 CBS_len(&profile_ids) != 0 ||
1832 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1833 CBS_len(contents) != 0) {
1834 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1835 return 0;
1836 }
1837
1838 if (CBS_len(&srtp_mki) != 0) {
1839 /* Must be no MKI, since we never offer one. */
1840 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1841 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1842 return 0;
1843 }
1844
1845 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1846
1847 /* Check to see if the server gave us something we support (and presumably
1848 * offered). */
1849 size_t i;
1850 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1851 const SRTP_PROTECTION_PROFILE *profile =
1852 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1853
1854 if (profile->id == profile_id) {
1855 ssl->srtp_profile = profile;
1856 return 1;
1857 }
1858 }
1859
1860 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1861 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1862 return 0;
1863}
1864
1865static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1866 CBS *contents) {
1867 if (contents == NULL) {
1868 return 1;
1869 }
1870
1871 CBS profile_ids, srtp_mki;
1872 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1873 CBS_len(&profile_ids) < 2 ||
1874 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1875 CBS_len(contents) != 0) {
1876 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1877 return 0;
1878 }
1879 /* Discard the MKI value for now. */
1880
1881 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1882 SSL_get_srtp_profiles(ssl);
1883
1884 /* Pick the server's most preferred profile. */
1885 size_t i;
1886 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1887 const SRTP_PROTECTION_PROFILE *server_profile =
1888 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1889
1890 CBS profile_ids_tmp;
1891 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1892
1893 while (CBS_len(&profile_ids_tmp) > 0) {
1894 uint16_t profile_id;
1895 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1896 return 0;
1897 }
1898
1899 if (server_profile->id == profile_id) {
1900 ssl->srtp_profile = server_profile;
1901 return 1;
1902 }
1903 }
1904 }
1905
1906 return 1;
1907}
1908
1909static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1910 if (ssl->srtp_profile == NULL) {
1911 return 1;
1912 }
1913
1914 CBB contents, profile_ids;
1915 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1916 !CBB_add_u16_length_prefixed(out, &contents) ||
1917 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1918 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1919 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1920 !CBB_flush(out)) {
1921 return 0;
1922 }
1923
1924 return 1;
1925}
1926
Adam Langleybdd5d662015-07-20 16:19:08 -07001927
1928/* EC point formats.
1929 *
1930 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1931
1932static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1933 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1934 return 0;
1935 }
1936
1937 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1938
1939 size_t i;
1940 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1941 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1942
1943 const uint32_t alg_k = cipher->algorithm_mkey;
1944 const uint32_t alg_a = cipher->algorithm_auth;
1945 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1946 return 1;
1947 }
1948 }
1949
1950 return 0;
1951}
1952
Adam Langleybdd5d662015-07-20 16:19:08 -07001953static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001954 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001955 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1956 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001957 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1958 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001959 !CBB_flush(out)) {
1960 return 0;
1961 }
1962
1963 return 1;
1964}
1965
1966static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1967 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1968 return 1;
1969 }
1970
1971 return ext_ec_point_add_extension(ssl, out);
1972}
1973
1974static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1975 CBS *contents) {
1976 if (contents == NULL) {
1977 return 1;
1978 }
1979
1980 CBS ec_point_format_list;
1981 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1982 CBS_len(contents) != 0) {
1983 return 0;
1984 }
1985
David Benjaminfc059942015-07-30 23:01:59 -04001986 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1987 * point format. */
1988 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1989 CBS_len(&ec_point_format_list)) == NULL) {
1990 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001991 return 0;
1992 }
1993
1994 return 1;
1995}
1996
1997static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1998 CBS *contents) {
1999 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
2000}
2001
2002static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
2003 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
2004 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04002005 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07002006
2007 if (!using_ecc) {
2008 return 1;
2009 }
2010
2011 return ext_ec_point_add_extension(ssl, out);
2012}
2013
Adam Langley273d49c2015-07-20 16:38:52 -07002014
2015/* EC supported curves.
2016 *
2017 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2018
2019static void ext_ec_curves_init(SSL *ssl) {
2020 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2021 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2022 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2023}
2024
2025static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2026 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2027 return 1;
2028 }
2029
2030 CBB contents, curves_bytes;
2031 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2032 !CBB_add_u16_length_prefixed(out, &contents) ||
2033 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2034 return 0;
2035 }
2036
2037 const uint16_t *curves;
2038 size_t curves_len;
2039 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2040
2041 size_t i;
2042 for (i = 0; i < curves_len; i++) {
2043 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2044 return 0;
2045 }
2046 }
2047
2048 return CBB_flush(out);
2049}
2050
2051static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2052 CBS *contents) {
2053 /* This extension is not expected to be echoed by servers and is ignored. */
2054 return 1;
2055}
2056
2057static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2058 CBS *contents) {
2059 if (contents == NULL) {
2060 return 1;
2061 }
2062
2063 CBS elliptic_curve_list;
2064 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2065 CBS_len(&elliptic_curve_list) == 0 ||
2066 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2067 CBS_len(contents) != 0) {
2068 return 0;
2069 }
2070
2071 ssl->s3->tmp.peer_ellipticcurvelist =
2072 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2073
2074 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2075 *out_alert = SSL_AD_INTERNAL_ERROR;
2076 return 0;
2077 }
2078
2079 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2080 size_t i;
2081 for (i = 0; i < num_curves; i++) {
2082 if (!CBS_get_u16(&elliptic_curve_list,
2083 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2084 goto err;
2085 }
2086 }
2087
2088 assert(CBS_len(&elliptic_curve_list) == 0);
2089 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2090
2091 return 1;
2092
2093err:
2094 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2095 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2096 *out_alert = SSL_AD_INTERNAL_ERROR;
2097 return 0;
2098}
2099
2100static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2101 /* Servers don't echo this extension. */
2102 return 1;
2103}
2104
2105
Adam Langley614c66a2015-06-12 15:26:58 -07002106/* kExtensions contains all the supported extensions. */
2107static const struct tls_extension kExtensions[] = {
2108 {
Adam Langley5021b222015-06-12 18:27:58 -07002109 /* The renegotiation extension must always be at index zero because the
2110 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2111 * sent as an SCSV. */
2112 TLSEXT_TYPE_renegotiate,
2113 NULL,
2114 ext_ri_add_clienthello,
2115 ext_ri_parse_serverhello,
2116 ext_ri_parse_clienthello,
2117 ext_ri_add_serverhello,
2118 },
2119 {
Adam Langley614c66a2015-06-12 15:26:58 -07002120 TLSEXT_TYPE_server_name,
2121 ext_sni_init,
2122 ext_sni_add_clienthello,
2123 ext_sni_parse_serverhello,
2124 ext_sni_parse_clienthello,
2125 ext_sni_add_serverhello,
2126 },
Adam Langley0a056712015-07-01 15:03:33 -07002127 {
2128 TLSEXT_TYPE_extended_master_secret,
2129 ext_ems_init,
2130 ext_ems_add_clienthello,
2131 ext_ems_parse_serverhello,
2132 ext_ems_parse_clienthello,
2133 ext_ems_add_serverhello,
2134 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002135 {
2136 TLSEXT_TYPE_session_ticket,
2137 NULL,
2138 ext_ticket_add_clienthello,
2139 ext_ticket_parse_serverhello,
2140 ext_ticket_parse_clienthello,
2141 ext_ticket_add_serverhello,
2142 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002143 {
2144 TLSEXT_TYPE_signature_algorithms,
2145 NULL,
2146 ext_sigalgs_add_clienthello,
2147 ext_sigalgs_parse_serverhello,
2148 ext_sigalgs_parse_clienthello,
2149 ext_sigalgs_add_serverhello,
2150 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002151 {
2152 TLSEXT_TYPE_status_request,
2153 ext_ocsp_init,
2154 ext_ocsp_add_clienthello,
2155 ext_ocsp_parse_serverhello,
2156 ext_ocsp_parse_clienthello,
2157 ext_ocsp_add_serverhello,
2158 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002159 {
2160 TLSEXT_TYPE_next_proto_neg,
2161 ext_npn_init,
2162 ext_npn_add_clienthello,
2163 ext_npn_parse_serverhello,
2164 ext_npn_parse_clienthello,
2165 ext_npn_add_serverhello,
2166 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002167 {
2168 TLSEXT_TYPE_certificate_timestamp,
2169 NULL,
2170 ext_sct_add_clienthello,
2171 ext_sct_parse_serverhello,
2172 ext_sct_parse_clienthello,
2173 ext_sct_add_serverhello,
2174 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002175 {
2176 TLSEXT_TYPE_application_layer_protocol_negotiation,
2177 ext_alpn_init,
2178 ext_alpn_add_clienthello,
2179 ext_alpn_parse_serverhello,
2180 ext_alpn_parse_clienthello,
2181 ext_alpn_add_serverhello,
2182 },
Adam Langley49c7af12015-07-10 14:33:46 -07002183 {
2184 TLSEXT_TYPE_channel_id,
2185 ext_channel_id_init,
2186 ext_channel_id_add_clienthello,
2187 ext_channel_id_parse_serverhello,
2188 ext_channel_id_parse_clienthello,
2189 ext_channel_id_add_serverhello,
2190 },
Adam Langley391250d2015-07-15 19:06:07 -07002191 {
2192 TLSEXT_TYPE_srtp,
2193 ext_srtp_init,
2194 ext_srtp_add_clienthello,
2195 ext_srtp_parse_serverhello,
2196 ext_srtp_parse_clienthello,
2197 ext_srtp_add_serverhello,
2198 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002199 {
2200 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002201 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002202 ext_ec_point_add_clienthello,
2203 ext_ec_point_parse_serverhello,
2204 ext_ec_point_parse_clienthello,
2205 ext_ec_point_add_serverhello,
2206 },
Adam Langley273d49c2015-07-20 16:38:52 -07002207 {
2208 TLSEXT_TYPE_elliptic_curves,
2209 ext_ec_curves_init,
2210 ext_ec_curves_add_clienthello,
2211 ext_ec_curves_parse_serverhello,
2212 ext_ec_curves_parse_clienthello,
2213 ext_ec_curves_add_serverhello,
2214 },
Adam Langley614c66a2015-06-12 15:26:58 -07002215};
2216
2217#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2218
Adam Langley4cfa96b2015-07-01 11:56:55 -07002219OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2220 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002221 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002222OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2223 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2224 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002225 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002226
Adam Langley614c66a2015-06-12 15:26:58 -07002227static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2228 uint16_t value) {
2229 unsigned i;
2230 for (i = 0; i < kNumExtensions; i++) {
2231 if (kExtensions[i].value == value) {
2232 *out_index = i;
2233 return &kExtensions[i];
2234 }
2235 }
2236
2237 return NULL;
2238}
2239
Adam Langley09505632015-07-30 18:10:13 -07002240int SSL_extension_supported(unsigned extension_value) {
2241 uint32_t index;
2242 return extension_value == TLSEXT_TYPE_padding ||
2243 tls_extension_find(&index, extension_value) != NULL;
2244}
2245
Adam Langleyb0c235e2014-06-20 12:00:00 -07002246/* header_len is the length of the ClientHello header written so far, used to
2247 * compute padding. It does not include the record header. Pass 0 if no padding
2248 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002249uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2250 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002251 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2252 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002253 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002254 }
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langley33ad2b52015-07-20 17:43:53 -07002256 CBB cbb, extensions;
2257 CBB_zero(&cbb);
2258 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2259 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2260 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langley614c66a2015-06-12 15:26:58 -07002263 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002264 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langley614c66a2015-06-12 15:26:58 -07002266 size_t i;
2267 for (i = 0; i < kNumExtensions; i++) {
2268 if (kExtensions[i].init != NULL) {
2269 kExtensions[i].init(s);
2270 }
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langley614c66a2015-06-12 15:26:58 -07002273 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002274 const size_t len_before = CBB_len(&extensions);
2275 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2276 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2277 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2278 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 }
Adam Langley95c29f32014-06-20 12:00:00 -07002280
Adam Langley33ad2b52015-07-20 17:43:53 -07002281 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002282 s->s3->tmp.extensions.sent |= (1u << i);
2283 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 }
Adam Langley75712922014-10-10 16:23:43 -07002285
Adam Langley09505632015-07-30 18:10:13 -07002286 if (!custom_ext_add_clienthello(s, &extensions)) {
2287 goto err;
2288 }
2289
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002291 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 if (header_len > 0xff && header_len < 0x200) {
2293 /* Add padding to workaround bugs in F5 terminators. See
2294 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2295 *
2296 * NB: because this code works out the length of all existing extensions
2297 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002298 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 /* Extensions take at least four bytes to encode. Always include least
2300 * one byte of data if including the extension. WebSphere Application
2301 * Server 7.0 is intolerant to the last extension being zero-length. */
2302 if (padding_len >= 4 + 1) {
2303 padding_len -= 4;
2304 } else {
2305 padding_len = 1;
2306 }
Adam Langley95c29f32014-06-20 12:00:00 -07002307
Adam Langley33ad2b52015-07-20 17:43:53 -07002308 uint8_t *padding_bytes;
2309 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2310 !CBB_add_u16(&extensions, padding_len) ||
2311 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2312 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002313 }
Adam Langley75712922014-10-10 16:23:43 -07002314
Adam Langley33ad2b52015-07-20 17:43:53 -07002315 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 }
2317 }
Adam Langley75712922014-10-10 16:23:43 -07002318
Adam Langley33ad2b52015-07-20 17:43:53 -07002319 if (!CBB_flush(&cbb)) {
2320 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 }
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langley33ad2b52015-07-20 17:43:53 -07002323 uint8_t *ret = buf;
2324 const size_t cbb_len = CBB_len(&cbb);
2325 /* If only two bytes have been written then the extensions are actually empty
2326 * and those two bytes are the zero length. In that case, we don't bother
2327 * sending the extensions length. */
2328 if (cbb_len > 2) {
2329 ret += cbb_len;
2330 }
2331
2332 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002334
2335err:
2336 CBB_cleanup(&cbb);
2337 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2338 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002339}
Adam Langley95c29f32014-06-20 12:00:00 -07002340
Adam Langley614c66a2015-06-12 15:26:58 -07002341uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2342 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002343 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2344 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002345 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 }
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langley33ad2b52015-07-20 17:43:53 -07002348 CBB cbb, extensions;
2349 CBB_zero(&cbb);
2350 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2351 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2352 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002353 }
2354
2355 unsigned i;
2356 for (i = 0; i < kNumExtensions; i++) {
2357 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2358 /* Don't send extensions that were not received. */
2359 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002360 }
Adam Langley95c29f32014-06-20 12:00:00 -07002361
Adam Langley33ad2b52015-07-20 17:43:53 -07002362 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2363 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2364 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2365 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002366 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 }
Adam Langley95c29f32014-06-20 12:00:00 -07002368
Adam Langley09505632015-07-30 18:10:13 -07002369 if (!custom_ext_add_serverhello(s, &extensions)) {
2370 goto err;
2371 }
2372
Adam Langley33ad2b52015-07-20 17:43:53 -07002373 if (!CBB_flush(&cbb)) {
2374 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 }
2376
Adam Langley33ad2b52015-07-20 17:43:53 -07002377 uint8_t *ret = buf;
2378 const size_t cbb_len = CBB_len(&cbb);
2379 /* If only two bytes have been written then the extensions are actually empty
2380 * and those two bytes are the zero length. In that case, we don't bother
2381 * sending the extensions length. */
2382 if (cbb_len > 2) {
2383 ret += cbb_len;
2384 }
2385
2386 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002388
2389err:
2390 CBB_cleanup(&cbb);
2391 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2392 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002393}
Adam Langley95c29f32014-06-20 12:00:00 -07002394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002396 size_t i;
2397 for (i = 0; i < kNumExtensions; i++) {
2398 if (kExtensions[i].init != NULL) {
2399 kExtensions[i].init(s);
2400 }
2401 }
2402
2403 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002404 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002405 /* The renegotiation extension must always be at index zero because the
2406 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2407 * sent as an SCSV. */
2408 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002409
Adam Langleyfcf25832014-12-18 17:42:32 -08002410 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002411 if (CBS_len(cbs) != 0) {
2412 /* Decode the extensions block and check it is valid. */
2413 CBS extensions;
2414 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2415 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 *out_alert = SSL_AD_DECODE_ERROR;
2417 return 0;
2418 }
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langley33ad2b52015-07-20 17:43:53 -07002420 while (CBS_len(&extensions) != 0) {
2421 uint16_t type;
2422 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002423
Adam Langley33ad2b52015-07-20 17:43:53 -07002424 /* Decode the next extension. */
2425 if (!CBS_get_u16(&extensions, &type) ||
2426 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2427 *out_alert = SSL_AD_DECODE_ERROR;
2428 return 0;
2429 }
2430
2431 unsigned ext_index;
2432 const struct tls_extension *const ext =
2433 tls_extension_find(&ext_index, type);
2434
2435 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002436 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2437 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2438 return 0;
2439 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002440 continue;
2441 }
2442
Adam Langley614c66a2015-06-12 15:26:58 -07002443 s->s3->tmp.extensions.received |= (1u << ext_index);
2444 uint8_t alert = SSL_AD_DECODE_ERROR;
2445 if (!ext->parse_clienthello(s, &alert, &extension)) {
2446 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002447 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2448 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002449 return 0;
2450 }
Adam Langley614c66a2015-06-12 15:26:58 -07002451 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002452 }
Adam Langley75712922014-10-10 16:23:43 -07002453
Adam Langley614c66a2015-06-12 15:26:58 -07002454 for (i = 0; i < kNumExtensions; i++) {
2455 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2456 /* Extension wasn't observed so call the callback with a NULL
2457 * parameter. */
2458 uint8_t alert = SSL_AD_DECODE_ERROR;
2459 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002460 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2461 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002462 *out_alert = alert;
2463 return 0;
2464 }
2465 }
2466 }
2467
Adam Langleyfcf25832014-12-18 17:42:32 -08002468 return 1;
2469}
Adam Langley95c29f32014-06-20 12:00:00 -07002470
Adam Langleyfcf25832014-12-18 17:42:32 -08002471int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2472 int alert = -1;
2473 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2474 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2475 return 0;
2476 }
Adam Langley95c29f32014-06-20 12:00:00 -07002477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002479 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 return 0;
2481 }
Adam Langley95c29f32014-06-20 12:00:00 -07002482
Adam Langleyfcf25832014-12-18 17:42:32 -08002483 return 1;
2484}
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002487 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002488 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002489
Adam Langley33ad2b52015-07-20 17:43:53 -07002490 if (CBS_len(cbs) != 0) {
2491 /* Decode the extensions block and check it is valid. */
2492 CBS extensions;
2493 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2494 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002495 *out_alert = SSL_AD_DECODE_ERROR;
2496 return 0;
2497 }
Adam Langley95c29f32014-06-20 12:00:00 -07002498
Adam Langley614c66a2015-06-12 15:26:58 -07002499
Adam Langley33ad2b52015-07-20 17:43:53 -07002500 while (CBS_len(&extensions) != 0) {
2501 uint16_t type;
2502 CBS extension;
2503
2504 /* Decode the next extension. */
2505 if (!CBS_get_u16(&extensions, &type) ||
2506 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2507 *out_alert = SSL_AD_DECODE_ERROR;
2508 return 0;
2509 }
2510
2511 unsigned ext_index;
2512 const struct tls_extension *const ext =
2513 tls_extension_find(&ext_index, type);
2514
Adam Langley09505632015-07-30 18:10:13 -07002515 if (ext == NULL) {
2516 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2517 return 0;
2518 }
2519 continue;
2520 }
2521
2522 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2523 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002524 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002525 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 *out_alert = SSL_AD_DECODE_ERROR;
2527 return 0;
2528 }
David Benjamin03973092014-06-24 23:27:17 -04002529
Adam Langley614c66a2015-06-12 15:26:58 -07002530 received |= (1u << ext_index);
2531
2532 uint8_t alert = SSL_AD_DECODE_ERROR;
2533 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002534 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2535 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002536 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 return 0;
2538 }
Adam Langley614c66a2015-06-12 15:26:58 -07002539 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002540 }
Adam Langley95c29f32014-06-20 12:00:00 -07002541
Adam Langley33ad2b52015-07-20 17:43:53 -07002542 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002543 for (i = 0; i < kNumExtensions; i++) {
2544 if (!(received & (1u << i))) {
2545 /* Extension wasn't observed so call the callback with a NULL
2546 * parameter. */
2547 uint8_t alert = SSL_AD_DECODE_ERROR;
2548 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002549 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2550 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002551 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 return 0;
2553 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002554 }
2555 }
Adam Langley95c29f32014-06-20 12:00:00 -07002556
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 return 1;
2558}
Adam Langley95c29f32014-06-20 12:00:00 -07002559
Adam Langleyfcf25832014-12-18 17:42:32 -08002560static int ssl_check_clienthello_tlsext(SSL *s) {
2561 int ret = SSL_TLSEXT_ERR_NOACK;
2562 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2565 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2568 ret = s->ctx->tlsext_servername_callback(s, &al,
2569 s->ctx->tlsext_servername_arg);
2570 } else if (s->initial_ctx != NULL &&
2571 s->initial_ctx->tlsext_servername_callback != 0) {
2572 ret = s->initial_ctx->tlsext_servername_callback(
2573 s, &al, s->initial_ctx->tlsext_servername_arg);
2574 }
Adam Langley95c29f32014-06-20 12:00:00 -07002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 switch (ret) {
2577 case SSL_TLSEXT_ERR_ALERT_FATAL:
2578 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2579 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581 case SSL_TLSEXT_ERR_ALERT_WARNING:
2582 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2583 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002586 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002587 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 default:
2590 return 1;
2591 }
2592}
Adam Langleyed8270a2014-09-02 13:52:56 -07002593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002595 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002596 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2599 ret = s->ctx->tlsext_servername_callback(s, &al,
2600 s->ctx->tlsext_servername_arg);
2601 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002602 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 ret = s->initial_ctx->tlsext_servername_callback(
2604 s, &al, s->initial_ctx->tlsext_servername_arg);
2605 }
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 switch (ret) {
2608 case SSL_TLSEXT_ERR_ALERT_FATAL:
2609 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2610 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002611
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 case SSL_TLSEXT_ERR_ALERT_WARNING:
2613 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2614 return 1;
2615
2616 default:
2617 return 1;
2618 }
2619}
2620
2621int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2622 int alert = -1;
2623 if (s->version < SSL3_VERSION) {
2624 return 1;
2625 }
2626
2627 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2628 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2629 return 0;
2630 }
2631
2632 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002633 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 return 0;
2635 }
2636
2637 return 1;
2638}
Adam Langley95c29f32014-06-20 12:00:00 -07002639
David Benjamine3aa1d92015-06-16 15:34:50 -04002640int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2641 int *out_send_ticket, const uint8_t *ticket,
2642 size_t ticket_len, const uint8_t *session_id,
2643 size_t session_id_len) {
2644 int ret = 1; /* Most errors are non-fatal. */
2645 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2646 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamine3aa1d92015-06-16 15:34:50 -04002648 HMAC_CTX hmac_ctx;
2649 HMAC_CTX_init(&hmac_ctx);
2650 EVP_CIPHER_CTX cipher_ctx;
2651 EVP_CIPHER_CTX_init(&cipher_ctx);
2652
2653 *out_send_ticket = 0;
2654 *out_session = NULL;
2655
2656 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2657 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 }
2659
David Benjamine3aa1d92015-06-16 15:34:50 -04002660 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002662 *out_send_ticket = 1;
2663 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002664 }
2665
David Benjaminadcc3952015-04-26 13:07:57 -04002666 /* Ensure there is room for the key name and the largest IV
2667 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2668 * the maximum IV length should be well under the minimum size for the
2669 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002670 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2671 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002673 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002674
David Benjamine3aa1d92015-06-16 15:34:50 -04002675 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2676 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2677 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2678 0 /* decrypt */);
2679 if (cb_ret < 0) {
2680 ret = 0;
2681 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002683 if (cb_ret == 0) {
2684 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002685 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002686 if (cb_ret == 2) {
2687 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 }
2689 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002690 /* Check the key name matches. */
2691 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2692 SSL_TICKET_KEY_NAME_LEN) != 0) {
2693 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002695 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2696 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002697 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002698 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2699 ssl_ctx->tlsext_tick_aes_key, iv)) {
2700 ret = 0;
2701 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002702 }
2703 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002704 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002705
David Benjamine3aa1d92015-06-16 15:34:50 -04002706 /* Check the MAC at the end of the ticket. */
2707 uint8_t mac[EVP_MAX_MD_SIZE];
2708 size_t mac_len = HMAC_size(&hmac_ctx);
2709 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002710 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002711 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002712 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002713 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2714 HMAC_Final(&hmac_ctx, mac, NULL);
2715 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2716 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 }
2718
David Benjamine3aa1d92015-06-16 15:34:50 -04002719 /* Decrypt the session data. */
2720 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2721 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2722 mac_len;
2723 plaintext = OPENSSL_malloc(ciphertext_len);
2724 if (plaintext == NULL) {
2725 ret = 0;
2726 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002727 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002728 if (ciphertext_len >= INT_MAX) {
2729 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002731 int len1, len2;
2732 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2733 (int)ciphertext_len) ||
2734 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2735 ERR_clear_error(); /* Don't leave an error on the queue. */
2736 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002737 }
2738
David Benjamine3aa1d92015-06-16 15:34:50 -04002739 /* Decode the session. */
2740 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2741 if (session == NULL) {
2742 ERR_clear_error(); /* Don't leave an error on the queue. */
2743 goto done;
2744 }
2745
2746 /* Copy the client's session ID into the new session, to denote the ticket has
2747 * been accepted. */
2748 memcpy(session->session_id, session_id, session_id_len);
2749 session->session_id_length = session_id_len;
2750
2751 *out_session = session;
2752
2753done:
2754 OPENSSL_free(plaintext);
2755 HMAC_CTX_cleanup(&hmac_ctx);
2756 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2757 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002758}
Adam Langley95c29f32014-06-20 12:00:00 -07002759
2760/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002761typedef struct {
2762 int nid;
2763 int id;
2764} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2767 {NID_sha1, TLSEXT_hash_sha1},
2768 {NID_sha224, TLSEXT_hash_sha224},
2769 {NID_sha256, TLSEXT_hash_sha256},
2770 {NID_sha384, TLSEXT_hash_sha384},
2771 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002772
Adam Langleyfcf25832014-12-18 17:42:32 -08002773static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2774 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002775
Adam Langleyfcf25832014-12-18 17:42:32 -08002776static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2777 size_t i;
2778 for (i = 0; i < tlen; i++) {
2779 if (table[i].nid == nid) {
2780 return table[i].id;
2781 }
2782 }
Adam Langley95c29f32014-06-20 12:00:00 -07002783
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 return -1;
2785}
Adam Langley95c29f32014-06-20 12:00:00 -07002786
David Benjaminb4d65fd2015-05-29 17:11:21 -04002787int tls12_get_sigid(int pkey_type) {
2788 return tls12_find_id(pkey_type, tls12_sig,
2789 sizeof(tls12_sig) / sizeof(tls12_lookup));
2790}
2791
David Benjamind1d80782015-07-05 11:54:09 -04002792int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002794
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 if (!md) {
2796 return 0;
2797 }
Adam Langley95c29f32014-06-20 12:00:00 -07002798
Adam Langleyfcf25832014-12-18 17:42:32 -08002799 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2800 sizeof(tls12_md) / sizeof(tls12_lookup));
2801 if (md_id == -1) {
2802 return 0;
2803 }
Adam Langley95c29f32014-06-20 12:00:00 -07002804
David Benjamind1d80782015-07-05 11:54:09 -04002805 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 if (sig_id == -1) {
2807 return 0;
2808 }
Adam Langley95c29f32014-06-20 12:00:00 -07002809
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 p[0] = (uint8_t)md_id;
2811 p[1] = (uint8_t)sig_id;
2812 return 1;
2813}
2814
Adam Langleyfcf25832014-12-18 17:42:32 -08002815const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2816 switch (hash_alg) {
2817 case TLSEXT_hash_md5:
2818 return EVP_md5();
2819
2820 case TLSEXT_hash_sha1:
2821 return EVP_sha1();
2822
2823 case TLSEXT_hash_sha224:
2824 return EVP_sha224();
2825
2826 case TLSEXT_hash_sha256:
2827 return EVP_sha256();
2828
2829 case TLSEXT_hash_sha384:
2830 return EVP_sha384();
2831
2832 case TLSEXT_hash_sha512:
2833 return EVP_sha512();
2834
2835 default:
2836 return NULL;
2837 }
2838}
Adam Langley95c29f32014-06-20 12:00:00 -07002839
David Benjaminec2f27d2014-11-13 19:17:25 -05002840/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2841 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002842static int tls12_get_pkey_type(uint8_t sig_alg) {
2843 switch (sig_alg) {
2844 case TLSEXT_signature_rsa:
2845 return EVP_PKEY_RSA;
2846
2847 case TLSEXT_signature_ecdsa:
2848 return EVP_PKEY_EC;
2849
2850 default:
2851 return -1;
2852 }
2853}
Adam Langley95c29f32014-06-20 12:00:00 -07002854
Adam Langley95c29f32014-06-20 12:00:00 -07002855/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002856static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2857 size_t preflen, const uint8_t *allow,
2858 size_t allowlen) {
2859 const uint8_t *ptmp, *atmp;
2860 size_t i, j, nmatch = 0;
2861
2862 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2863 /* Skip disabled hashes or signature algorithms */
2864 if (tls12_get_hash(ptmp[0]) == NULL ||
2865 tls12_get_pkey_type(ptmp[1]) == -1) {
2866 continue;
2867 }
2868
2869 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2870 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2871 nmatch++;
2872 if (shsig) {
2873 shsig->rhash = ptmp[0];
2874 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 shsig++;
2876 }
2877
2878 break;
2879 }
2880 }
2881 }
2882
2883 return nmatch;
2884}
Adam Langley95c29f32014-06-20 12:00:00 -07002885
2886/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002887static int tls1_set_shared_sigalgs(SSL *s) {
2888 const uint8_t *pref, *allow, *conf;
2889 size_t preflen, allowlen, conflen;
2890 size_t nmatch;
2891 TLS_SIGALGS *salgs = NULL;
2892 CERT *c = s->cert;
2893
David Benjamin2755a3e2015-04-22 16:17:58 -04002894 OPENSSL_free(c->shared_sigalgs);
2895 c->shared_sigalgs = NULL;
2896 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002897
David Benjamin3dd90162015-08-09 11:26:40 -04002898 conflen = tls12_get_psigalgs(s, &conf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002899
2900 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2901 pref = conf;
2902 preflen = conflen;
2903 allow = c->peer_sigalgs;
2904 allowlen = c->peer_sigalgslen;
2905 } else {
2906 allow = conf;
2907 allowlen = conflen;
2908 pref = c->peer_sigalgs;
2909 preflen = c->peer_sigalgslen;
2910 }
2911
2912 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2913 if (!nmatch) {
2914 return 1;
2915 }
2916
2917 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2918 if (!salgs) {
2919 return 0;
2920 }
2921
2922 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2923 c->shared_sigalgs = salgs;
2924 c->shared_sigalgslen = nmatch;
2925 return 1;
2926}
Adam Langley95c29f32014-06-20 12:00:00 -07002927
2928/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002929int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2930 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002931
Adam Langleyfcf25832014-12-18 17:42:32 -08002932 /* Extension ignored for inappropriate versions */
2933 if (!SSL_USE_SIGALGS(s)) {
2934 return 1;
2935 }
David Benjamincd996942014-07-20 16:23:51 -04002936
David Benjamin61c0d4e2015-03-19 14:24:37 -04002937 if (CBS_len(sigalgs) % 2 != 0 ||
2938 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2939 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002940 return 0;
2941 }
Adam Langley95c29f32014-06-20 12:00:00 -07002942
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 return 1;
2944}
David Benjaminec2f27d2014-11-13 19:17:25 -05002945
David Benjamind1d80782015-07-05 11:54:09 -04002946const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2947 CERT *cert = ssl->cert;
2948 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002950
Adam Langleyfcf25832014-12-18 17:42:32 -08002951 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002952 for (i = 0; i < cert->shared_sigalgslen; i++) {
2953 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002954 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002955 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2956 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002957 continue;
2958 }
2959 return md;
2960 }
Adam Langley95c29f32014-06-20 12:00:00 -07002961
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 /* If no suitable digest may be found, default to SHA-1. */
2963 return EVP_sha1();
2964}
Adam Langley95c29f32014-06-20 12:00:00 -07002965
David Benjamind6a4ae92015-08-06 11:10:51 -04002966int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2967 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969
2970 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002971 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2972 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002974
David Benjamind6a4ae92015-08-06 11:10:51 -04002975 static const char kClientIDMagic[] = "TLS Channel ID signature";
2976 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2977
2978 if (ssl->hit) {
2979 static const char kResumptionMagic[] = "Resumption";
2980 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2981 if (ssl->session->original_handshake_hash_len == 0) {
2982 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2983 goto err;
2984 }
2985 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2986 ssl->session->original_handshake_hash_len);
2987 }
2988
2989 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2990 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2991 sizeof(handshake_hash));
2992 if (handshake_hash_len < 0) {
2993 goto err;
2994 }
2995 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2996 unsigned len_u;
2997 EVP_DigestFinal_ex(&ctx, out, &len_u);
2998 *out_len = len_u;
2999
3000 ret = 1;
3001
3002err:
3003 EVP_MD_CTX_cleanup(&ctx);
3004 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003005}
Adam Langley1258b6a2014-06-20 12:00:00 -07003006
3007/* tls1_record_handshake_hashes_for_channel_id records the current handshake
3008 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08003009int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
3010 int digest_len;
3011 /* This function should never be called for a resumed session because the
3012 * handshake hashes that we wish to record are for the original, full
3013 * handshake. */
3014 if (s->hit) {
3015 return -1;
3016 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003017
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 digest_len =
3019 tls1_handshake_digest(s, s->session->original_handshake_hash,
3020 sizeof(s->session->original_handshake_hash));
3021 if (digest_len < 0) {
3022 return -1;
3023 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003024
Adam Langleyfcf25832014-12-18 17:42:32 -08003025 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003026
Adam Langleyfcf25832014-12-18 17:42:32 -08003027 return 1;
3028}