blob: 8efca84b62da25dc0a91d0170ced2422db6a4696 [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
David Benjamind4c2bce2015-10-17 12:28:18 -0400311int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 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 Langleyd98dc132015-09-23 16:41:33 -0700355#if defined(BORINGSSL_ANDROID_SYSTEM)
356 25, /* secp521r1 */
357#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800358};
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500361 size_t i;
362 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
363 if (curve_id == tls_curves[i].curve_id) {
364 return tls_curves[i].nid;
365 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500367 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368}
Adam Langley95c29f32014-06-20 12:00:00 -0700369
David Benjamin70bd80a2014-12-27 03:06:46 -0500370int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500372 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
373 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500374 *out_curve_id = tls_curves[i].curve_id;
375 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 0;
379}
380
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200381const char* tls1_ec_curve_id2name(uint16_t curve_id) {
382 size_t i;
383 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
384 if (curve_id == tls_curves[i].curve_id) {
385 return tls_curves[i].curve_name;
386 }
387 }
388 return NULL;
389}
390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
392 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
393 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400394static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 const uint16_t **out_curve_ids,
396 size_t *out_curve_ids_len) {
397 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400398 /* Only clients send a curve list, so this function is only called
399 * on the server. */
400 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
402 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
403 return;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 *out_curve_ids = s->tlsext_ellipticcurvelist;
407 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
408 if (!*out_curve_ids) {
409 *out_curve_ids = eccurves_default;
410 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
411 }
412}
David Benjamined439582014-07-14 19:13:02 -0400413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
415 uint8_t curve_type;
416 uint16_t curve_id;
417 const uint16_t *curves;
418 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 /* Only support named curves. */
421 if (!CBS_get_u8(cbs, &curve_type) ||
422 curve_type != NAMED_CURVE_TYPE ||
423 !CBS_get_u16(cbs, &curve_id)) {
424 return 0;
425 }
David Benjamined439582014-07-14 19:13:02 -0400426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 tls1_get_curvelist(s, 0, &curves, &curves_len);
428 for (i = 0; i < curves_len; i++) {
429 if (curve_id == curves[i]) {
430 *out_curve_id = curve_id;
431 return 1;
432 }
433 }
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 return 0;
436}
David Benjamin072334d2014-07-13 16:24:27 -0400437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400439 const uint16_t *curves, *peer_curves, *pref, *supp;
440 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400441
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 /* Can't do anything on client side */
443 if (s->server == 0) {
444 return NID_undef;
445 }
446
David Benjamin55a43642015-04-20 14:45:55 -0400447 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
448 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800449
David Benjamin55a43642015-04-20 14:45:55 -0400450 if (peer_curves_len == 0) {
451 /* Clients are not required to send a supported_curves extension. In this
452 * case, the server is free to pick any curve it likes. See RFC 4492,
453 * section 4, paragraph 3. */
454 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
455 }
456
457 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
458 pref = curves;
459 pref_len = curves_len;
460 supp = peer_curves;
461 supp_len = peer_curves_len;
462 } else {
463 pref = peer_curves;
464 pref_len = peer_curves_len;
465 supp = curves;
466 supp_len = curves_len;
467 }
468
469 for (i = 0; i < pref_len; i++) {
470 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 if (pref[i] == supp[j]) {
472 return tls1_ec_curve_id2nid(pref[i]);
473 }
474 }
475 }
476
477 return NID_undef;
478}
Adam Langley95c29f32014-06-20 12:00:00 -0700479
David Benjamin072334d2014-07-13 16:24:27 -0400480int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 const int *curves, size_t ncurves) {
482 uint16_t *curve_ids;
483 size_t i;
484
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
486 if (curve_ids == NULL) {
487 return 0;
488 }
489
490 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500491 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 OPENSSL_free(curve_ids);
493 return 0;
494 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 }
496
David Benjamin2755a3e2015-04-22 16:17:58 -0400497 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 *out_curve_ids = curve_ids;
499 *out_curve_ids_len = ncurves;
500
501 return 1;
502}
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamin072334d2014-07-13 16:24:27 -0400504/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
505 * TLS curve ID and point format, respectively, for |ec|. It returns one on
506 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800507static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
508 uint8_t *out_comp_id, EC_KEY *ec) {
509 int nid;
510 uint16_t id;
511 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 if (ec == NULL) {
514 return 0;
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 grp = EC_KEY_get0_group(ec);
518 if (grp == NULL) {
519 return 0;
520 }
David Benjamin072334d2014-07-13 16:24:27 -0400521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 /* Determine curve ID */
523 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500524 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 return 0;
526 }
David Benjamin072334d2014-07-13 16:24:27 -0400527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
529 *out_curve_id = id;
530
531 if (out_comp_id) {
532 if (EC_KEY_get0_public_key(ec) == NULL) {
533 return 0;
534 }
535 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
536 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
537 } else {
538 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
539 }
540 }
541
542 return 1;
543}
David Benjamin072334d2014-07-13 16:24:27 -0400544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
546 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400547 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800548static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
549 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400550 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400553 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
554 if (get_peer_curves && !s->server) {
555 /* Servers do not present a preference list so, if we are a client, only
556 * check our list. */
557 continue;
558 }
559
560 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
561 if (get_peer_curves && curves_len == 0) {
562 /* Clients are not required to send a supported_curves extension. In this
563 * case, the server is free to pick any curve it likes. See RFC 4492,
564 * section 4, paragraph 3. */
565 continue;
566 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 for (i = 0; i < curves_len; i++) {
568 if (curves[i] == curve_id) {
569 break;
570 }
571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 if (i == curves_len) {
574 return 0;
575 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 }
David Benjamin033e5f42014-11-13 18:47:41 -0500577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 return 1;
579}
David Benjamin033e5f42014-11-13 18:47:41 -0500580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581int tls1_check_ec_cert(SSL *s, X509 *x) {
582 int ret = 0;
583 EVP_PKEY *pkey = X509_get_pubkey(x);
584 uint16_t curve_id;
585 uint8_t comp_id;
586
587 if (!pkey ||
588 pkey->type != EVP_PKEY_EC ||
589 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
590 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400591 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 goto done;
593 }
594
595 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500596
597done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400598 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 return ret;
600}
David Benjamin42e9a772014-09-02 23:18:44 -0400601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400603 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400604 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400605 * the curve. */
606 uint16_t curve_id;
607 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
608 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 }
610
David Benjamindd978782015-04-24 15:20:13 -0400611 if (s->cert->ecdh_tmp_cb != NULL) {
612 /* Assume the callback will provide an acceptable curve. */
613 return 1;
614 }
615
616 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
617 * there is a shared curve. */
618 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800619}
Adam Langley95c29f32014-06-20 12:00:00 -0700620
621/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langley95c29f32014-06-20 12:00:00 -0700624#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langley95c29f32014-06-20 12:00:00 -0700626#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700629
David Benjamincff64722014-08-19 19:54:46 -0400630static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 tlsext_sigalg(TLSEXT_hash_sha512)
632 tlsext_sigalg(TLSEXT_hash_sha384)
633 tlsext_sigalg(TLSEXT_hash_sha256)
634 tlsext_sigalg(TLSEXT_hash_sha224)
635 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700636};
David Benjamin05da6e12014-07-12 20:42:55 -0400637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400639 *psigs = tls12_sigalgs;
640 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800641}
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
644 * checks it is consistent with |s|'s sent supported signature algorithms and,
645 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
646 * returns 0 and writes an alert into |*out_alert|. */
647int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
648 CBS *cbs, EVP_PKEY *pkey) {
649 const uint8_t *sent_sigs;
650 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400651 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 uint8_t hash, signature;
653
654 /* Should never happen */
655 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400656 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 *out_alert = SSL_AD_INTERNAL_ERROR;
658 return 0;
659 }
660
661 if (!CBS_get_u8(cbs, &hash) ||
662 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 *out_alert = SSL_AD_DECODE_ERROR;
665 return 0;
666 }
667
668 /* Check key type is consistent with signature */
669 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400670 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
672 return 0;
673 }
674
675 if (pkey->type == EVP_PKEY_EC) {
676 uint16_t curve_id;
677 uint8_t comp_id;
678 /* Check compression and curve matches extensions */
679 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
680 *out_alert = SSL_AD_INTERNAL_ERROR;
681 return 0;
682 }
683
684 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400685 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400686 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800687 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
688 return 0;
689 }
690 }
691
692 /* Check signature matches a type we sent */
693 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
694 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
695 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
696 break;
697 }
698 }
699
700 /* Allow fallback to SHA-1. */
701 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400702 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
704 return 0;
705 }
706
707 *out_md = tls12_get_hash(hash);
708 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400709 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
711 return 0;
712 }
713
714 return 1;
715}
716
717/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
718 * supported or doesn't appear in supported signature algorithms. Unlike
719 * ssl_cipher_get_disabled this applies to a specific session and not global
720 * settings. */
721void ssl_set_client_disabled(SSL *s) {
722 CERT *c = s->cert;
723 const uint8_t *sigalgs;
724 size_t i, sigalgslen;
725 int have_rsa = 0, have_ecdsa = 0;
726 c->mask_a = 0;
727 c->mask_k = 0;
728
729 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
730 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
731 c->mask_ssl = SSL_TLSV1_2;
732 } else {
733 c->mask_ssl = 0;
734 }
735
736 /* Now go through all signature algorithms seeing if we support any for RSA,
737 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
738 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
739 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
740 switch (sigalgs[1]) {
741 case TLSEXT_signature_rsa:
742 have_rsa = 1;
743 break;
744
745 case TLSEXT_signature_ecdsa:
746 have_ecdsa = 1;
747 break;
748 }
749 }
750
751 /* Disable auth if we don't include any appropriate signature algorithms. */
752 if (!have_rsa) {
753 c->mask_a |= SSL_aRSA;
754 }
755 if (!have_ecdsa) {
756 c->mask_a |= SSL_aECDSA;
757 }
758
759 /* with PSK there must be client callback set */
760 if (!s->psk_client_callback) {
761 c->mask_a |= SSL_aPSK;
762 c->mask_k |= SSL_kPSK;
763 }
764}
Adam Langley95c29f32014-06-20 12:00:00 -0700765
Adam Langley614c66a2015-06-12 15:26:58 -0700766/* tls_extension represents a TLS extension that is handled internally. The
767 * |init| function is called for each handshake, before any other functions of
768 * the extension. Then the add and parse callbacks are called as needed.
769 *
770 * The parse callbacks receive a |CBS| that contains the contents of the
771 * extension (i.e. not including the type and length bytes). If an extension is
772 * not received then the parse callbacks will be called with a NULL CBS so that
773 * they can do any processing needed to handle the absence of an extension.
774 *
775 * The add callbacks receive a |CBB| to which the extension can be appended but
776 * the function is responsible for appending the type and length bytes too.
777 *
778 * All callbacks return one for success and zero for error. If a parse function
779 * returns zero then a fatal alert with value |*out_alert| will be sent. If
780 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
781struct tls_extension {
782 uint16_t value;
783 void (*init)(SSL *ssl);
784
785 int (*add_clienthello)(SSL *ssl, CBB *out);
786 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
787
788 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
789 int (*add_serverhello)(SSL *ssl, CBB *out);
790};
791
792
793/* Server name indication (SNI).
794 *
795 * https://tools.ietf.org/html/rfc6066#section-3. */
796
797static void ext_sni_init(SSL *ssl) {
798 ssl->s3->tmp.should_ack_sni = 0;
799}
800
801static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
802 if (ssl->tlsext_hostname == NULL) {
803 return 1;
804 }
805
806 CBB contents, server_name_list, name;
807 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
808 !CBB_add_u16_length_prefixed(out, &contents) ||
809 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
810 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
811 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
812 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
813 strlen(ssl->tlsext_hostname)) ||
814 !CBB_flush(out)) {
815 return 0;
816 }
817
818 return 1;
819}
820
821static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
822 if (contents == NULL) {
823 return 1;
824 }
825
826 if (CBS_len(contents) != 0) {
827 return 0;
828 }
829
830 assert(ssl->tlsext_hostname != NULL);
831
832 if (!ssl->hit) {
833 assert(ssl->session->tlsext_hostname == NULL);
834 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
835 if (!ssl->session->tlsext_hostname) {
836 *out_alert = SSL_AD_INTERNAL_ERROR;
837 return 0;
838 }
839 }
840
841 return 1;
842}
843
844static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
845 if (contents == NULL) {
846 return 1;
847 }
848
849 /* The servername extension is treated as follows:
850 *
851 * - Only the hostname type is supported with a maximum length of 255.
852 * - The servername is rejected if too long or if it contains zeros, in
853 * which case an fatal alert is generated.
854 * - The servername field is maintained together with the session cache.
855 * - When a session is resumed, the servername callback is invoked in order
856 * to allow the application to position itself to the right context.
857 * - The servername is acknowledged if it is new for a session or when
858 * it is identical to a previously used for the same session.
859 * Applications can control the behaviour. They can at any time
860 * set a 'desirable' servername for a new SSL object. This can be the
861 * case for example with HTTPS when a Host: header field is received and
862 * a renegotiation is requested. In this case, a possible servername
863 * presented in the new client hello is only acknowledged if it matches
864 * the value of the Host: field.
865 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
866 * if they provide for changing an explicit servername context for the
867 * session,
868 * i.e. when the session has been established with a servername extension.
869 */
870
871 CBS server_name_list;
872 char have_seen_host_name = 0;
873
874 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
875 CBS_len(&server_name_list) == 0 ||
876 CBS_len(contents) != 0) {
877 return 0;
878 }
879
880 /* Decode each ServerName in the extension. */
881 while (CBS_len(&server_name_list) > 0) {
882 uint8_t name_type;
883 CBS host_name;
884
885 if (!CBS_get_u8(&server_name_list, &name_type) ||
886 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
887 return 0;
888 }
889
890 /* Only host_name is supported. */
891 if (name_type != TLSEXT_NAMETYPE_host_name) {
892 continue;
893 }
894
895 if (have_seen_host_name) {
896 /* The ServerNameList MUST NOT contain more than one name of the same
897 * name_type. */
898 return 0;
899 }
900
901 have_seen_host_name = 1;
902
903 if (CBS_len(&host_name) == 0 ||
904 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
905 CBS_contains_zero_byte(&host_name)) {
906 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
907 return 0;
908 }
909
910 if (!ssl->hit) {
911 assert(ssl->session->tlsext_hostname == NULL);
912 if (ssl->session->tlsext_hostname) {
913 /* This should be impossible. */
914 return 0;
915 }
916
917 /* Copy the hostname as a string. */
918 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
919 *out_alert = SSL_AD_INTERNAL_ERROR;
920 return 0;
921 }
922
923 ssl->s3->tmp.should_ack_sni = 1;
924 }
925 }
926
927 return 1;
928}
929
930static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
931 if (ssl->hit ||
932 !ssl->s3->tmp.should_ack_sni ||
933 ssl->session->tlsext_hostname == NULL) {
934 return 1;
935 }
936
937 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
938 !CBB_add_u16(out, 0 /* length */)) {
939 return 0;
940 }
941
942 return 1;
943}
944
945
Adam Langley5021b222015-06-12 18:27:58 -0700946/* Renegotiation indication.
947 *
948 * https://tools.ietf.org/html/rfc5746 */
949
950static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
951 CBB contents, prev_finished;
952 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
953 !CBB_add_u16_length_prefixed(out, &contents) ||
954 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
955 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
956 ssl->s3->previous_client_finished_len) ||
957 !CBB_flush(out)) {
958 return 0;
959 }
960
961 return 1;
962}
963
964static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
965 CBS *contents) {
966 if (contents == NULL) {
967 /* No renegotiation extension received.
968 *
969 * Strictly speaking if we want to avoid an attack we should *always* see
970 * RI even on initial ServerHello because the client doesn't see any
971 * renegotiation during an attack. However this would mean we could not
972 * connect to any server which doesn't support RI.
973 *
974 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
975 * defined. */
976 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
977 return 1;
978 }
979
980 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400981 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700982 return 0;
983 }
984
985 const size_t expected_len = ssl->s3->previous_client_finished_len +
986 ssl->s3->previous_server_finished_len;
987
988 /* Check for logic errors */
989 assert(!expected_len || ssl->s3->previous_client_finished_len);
990 assert(!expected_len || ssl->s3->previous_server_finished_len);
991
992 /* Parse out the extension contents. */
993 CBS renegotiated_connection;
994 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
995 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700997 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
998 return 0;
999 }
1000
1001 /* Check that the extension matches. */
1002 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001004 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1005 return 0;
1006 }
1007
1008 const uint8_t *d = CBS_data(&renegotiated_connection);
1009 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1010 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001012 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1013 return 0;
1014 }
1015 d += ssl->s3->previous_client_finished_len;
1016
1017 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1018 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001019 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001020 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1021 return 0;
1022 }
1023 ssl->s3->send_connection_binding = 1;
1024
1025 return 1;
1026}
1027
1028static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1029 CBS *contents) {
1030 /* Renegotiation isn't supported as a server so this function should never be
1031 * called after the initial handshake. */
1032 assert(!ssl->s3->initial_handshake_complete);
1033
1034 CBS fake_contents;
1035 static const uint8_t kFakeExtension[] = {0};
1036
1037 if (contents == NULL) {
1038 if (ssl->s3->send_connection_binding) {
1039 /* The renegotiation SCSV was received so pretend that we received a
1040 * renegotiation extension. */
1041 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1042 contents = &fake_contents;
1043 /* We require that the renegotiation extension is at index zero of
1044 * kExtensions. */
1045 ssl->s3->tmp.extensions.received |= (1u << 0);
1046 } else {
1047 return 1;
1048 }
1049 }
1050
1051 CBS renegotiated_connection;
1052
1053 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1054 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001055 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001056 return 0;
1057 }
1058
1059 /* Check that the extension matches */
1060 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1061 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001062 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001063 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1064 return 0;
1065 }
1066
1067 ssl->s3->send_connection_binding = 1;
1068
1069 return 1;
1070}
1071
1072static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1073 CBB contents, prev_finished;
1074 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1075 !CBB_add_u16_length_prefixed(out, &contents) ||
1076 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1077 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1078 ssl->s3->previous_client_finished_len) ||
1079 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1080 ssl->s3->previous_server_finished_len) ||
1081 !CBB_flush(out)) {
1082 return 0;
1083 }
1084
1085 return 1;
1086}
1087
Adam Langley0a056712015-07-01 15:03:33 -07001088
1089/* Extended Master Secret.
1090 *
1091 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1092
1093static void ext_ems_init(SSL *ssl) {
1094 ssl->s3->tmp.extended_master_secret = 0;
1095}
1096
1097static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1098 if (ssl->version == SSL3_VERSION) {
1099 return 1;
1100 }
1101
1102 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1103 !CBB_add_u16(out, 0 /* length */)) {
1104 return 0;
1105 }
1106
1107 return 1;
1108}
1109
1110static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1111 CBS *contents) {
1112 if (contents == NULL) {
1113 return 1;
1114 }
1115
1116 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1117 return 0;
1118 }
1119
1120 ssl->s3->tmp.extended_master_secret = 1;
1121 return 1;
1122}
1123
1124static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1125 if (ssl->version == SSL3_VERSION || contents == NULL) {
1126 return 1;
1127 }
1128
1129 if (CBS_len(contents) != 0) {
1130 return 0;
1131 }
1132
1133 ssl->s3->tmp.extended_master_secret = 1;
1134 return 1;
1135}
1136
1137static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1138 if (!ssl->s3->tmp.extended_master_secret) {
1139 return 1;
1140 }
1141
1142 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1143 !CBB_add_u16(out, 0 /* length */)) {
1144 return 0;
1145 }
1146
1147 return 1;
1148}
1149
Adam Langley9b05bc52015-07-01 15:25:33 -07001150
1151/* Session tickets.
1152 *
1153 * https://tools.ietf.org/html/rfc5077 */
1154
1155static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1156 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1157 return 1;
1158 }
1159
1160 const uint8_t *ticket_data = NULL;
1161 int ticket_len = 0;
1162
1163 /* Renegotiation does not participate in session resumption. However, still
1164 * advertise the extension to avoid potentially breaking servers which carry
1165 * over the state from the previous handshake, such as OpenSSL servers
1166 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1167 if (!ssl->s3->initial_handshake_complete &&
1168 ssl->session != NULL &&
1169 ssl->session->tlsext_tick != NULL) {
1170 ticket_data = ssl->session->tlsext_tick;
1171 ticket_len = ssl->session->tlsext_ticklen;
1172 }
1173
1174 CBB ticket;
1175 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1176 !CBB_add_u16_length_prefixed(out, &ticket) ||
1177 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1178 !CBB_flush(out)) {
1179 return 0;
1180 }
1181
1182 return 1;
1183}
1184
1185static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1186 CBS *contents) {
1187 ssl->tlsext_ticket_expected = 0;
1188
1189 if (contents == NULL) {
1190 return 1;
1191 }
1192
1193 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1194 * this function should never be called, even if the server tries to send the
1195 * extension. */
1196 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1197
1198 if (CBS_len(contents) != 0) {
1199 return 0;
1200 }
1201
1202 ssl->tlsext_ticket_expected = 1;
1203 return 1;
1204}
1205
1206static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1207 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001208 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001209 return 1;
1210}
1211
1212static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1213 if (!ssl->tlsext_ticket_expected) {
1214 return 1;
1215 }
1216
1217 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1218 * true. */
1219 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1220
1221 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1222 !CBB_add_u16(out, 0 /* length */)) {
1223 return 0;
1224 }
1225
1226 return 1;
1227}
1228
1229
Adam Langley2e857bd2015-07-01 16:09:19 -07001230/* Signature Algorithms.
1231 *
1232 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1233
1234static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1235 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1236 return 1;
1237 }
1238
1239 const uint8_t *sigalgs_data;
1240 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1241
1242 CBB contents, sigalgs;
1243 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1244 !CBB_add_u16_length_prefixed(out, &contents) ||
1245 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1246 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1247 !CBB_flush(out)) {
1248 return 0;
1249 }
1250
1251 return 1;
1252}
1253
1254static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1255 CBS *contents) {
1256 if (contents != NULL) {
1257 /* Servers MUST NOT send this extension. */
1258 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001259 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001260 return 0;
1261 }
1262
1263 return 1;
1264}
1265
1266static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1267 CBS *contents) {
1268 OPENSSL_free(ssl->cert->peer_sigalgs);
1269 ssl->cert->peer_sigalgs = NULL;
1270 ssl->cert->peer_sigalgslen = 0;
1271
Adam Langley2e857bd2015-07-01 16:09:19 -07001272 if (contents == NULL) {
1273 return 1;
1274 }
1275
1276 CBS supported_signature_algorithms;
1277 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001278 CBS_len(contents) != 0 ||
1279 CBS_len(&supported_signature_algorithms) == 0 ||
1280 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001281 return 0;
1282 }
1283
1284 return 1;
1285}
1286
1287static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1288 /* Servers MUST NOT send this extension. */
1289 return 1;
1290}
1291
1292
Adam Langleybb0bd042015-07-01 16:21:03 -07001293/* OCSP Stapling.
1294 *
1295 * https://tools.ietf.org/html/rfc6066#section-8 */
1296
1297static void ext_ocsp_init(SSL *ssl) {
1298 ssl->s3->tmp.certificate_status_expected = 0;
1299}
1300
1301static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1302 if (!ssl->ocsp_stapling_enabled) {
1303 return 1;
1304 }
1305
1306 CBB contents;
1307 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1308 !CBB_add_u16_length_prefixed(out, &contents) ||
1309 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1310 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1311 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1312 !CBB_flush(out)) {
1313 return 0;
1314 }
1315
1316 return 1;
1317}
1318
1319static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001320 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001321 if (contents == NULL) {
1322 return 1;
1323 }
1324
1325 if (CBS_len(contents) != 0) {
1326 return 0;
1327 }
1328
1329 ssl->s3->tmp.certificate_status_expected = 1;
1330 return 1;
1331}
1332
1333static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1334 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001335 if (contents == NULL) {
1336 return 1;
1337 }
1338
1339 uint8_t status_type;
1340 if (!CBS_get_u8(contents, &status_type)) {
1341 return 0;
1342 }
1343
1344 /* We cannot decide whether OCSP stapling will occur yet because the correct
1345 * SSL_CTX might not have been selected. */
1346 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1347
Adam Langleybb0bd042015-07-01 16:21:03 -07001348 return 1;
1349}
1350
1351static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001352 /* The extension shouldn't be sent when resuming sessions. */
1353 if (ssl->hit ||
1354 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001355 ssl->ctx->ocsp_response_length == 0) {
1356 return 1;
1357 }
1358
1359 ssl->s3->tmp.certificate_status_expected = 1;
1360
1361 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1362 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001363}
1364
1365
Adam Langley97dfcbf2015-07-01 18:35:20 -07001366/* Next protocol negotiation.
1367 *
1368 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1369
1370static void ext_npn_init(SSL *ssl) {
1371 ssl->s3->next_proto_neg_seen = 0;
1372}
1373
1374static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1375 if (ssl->s3->initial_handshake_complete ||
1376 ssl->ctx->next_proto_select_cb == NULL ||
1377 SSL_IS_DTLS(ssl)) {
1378 return 1;
1379 }
1380
1381 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1382 !CBB_add_u16(out, 0 /* length */)) {
1383 return 0;
1384 }
1385
1386 return 1;
1387}
1388
1389static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1390 CBS *contents) {
1391 if (contents == NULL) {
1392 return 1;
1393 }
1394
1395 /* If any of these are false then we should never have sent the NPN
1396 * extension in the ClientHello and thus this function should never have been
1397 * called. */
1398 assert(!ssl->s3->initial_handshake_complete);
1399 assert(!SSL_IS_DTLS(ssl));
1400 assert(ssl->ctx->next_proto_select_cb != NULL);
1401
David Benjamin76c2efc2015-08-31 14:24:29 -04001402 if (ssl->s3->alpn_selected != NULL) {
1403 /* NPN and ALPN may not be negotiated in the same connection. */
1404 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1405 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1406 return 0;
1407 }
1408
Adam Langley97dfcbf2015-07-01 18:35:20 -07001409 const uint8_t *const orig_contents = CBS_data(contents);
1410 const size_t orig_len = CBS_len(contents);
1411
1412 while (CBS_len(contents) != 0) {
1413 CBS proto;
1414 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1415 CBS_len(&proto) == 0) {
1416 return 0;
1417 }
1418 }
1419
1420 uint8_t *selected;
1421 uint8_t selected_len;
1422 if (ssl->ctx->next_proto_select_cb(
1423 ssl, &selected, &selected_len, orig_contents, orig_len,
1424 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1425 *out_alert = SSL_AD_INTERNAL_ERROR;
1426 return 0;
1427 }
1428
1429 OPENSSL_free(ssl->next_proto_negotiated);
1430 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1431 if (ssl->next_proto_negotiated == NULL) {
1432 *out_alert = SSL_AD_INTERNAL_ERROR;
1433 return 0;
1434 }
1435
1436 ssl->next_proto_negotiated_len = selected_len;
1437 ssl->s3->next_proto_neg_seen = 1;
1438
1439 return 1;
1440}
1441
1442static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1443 CBS *contents) {
1444 if (contents != NULL && CBS_len(contents) != 0) {
1445 return 0;
1446 }
1447
1448 if (contents == NULL ||
1449 ssl->s3->initial_handshake_complete ||
1450 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1451 * afterwards, parsing the ALPN extension will clear
1452 * |next_proto_neg_seen|. */
1453 ssl->s3->alpn_selected != NULL ||
1454 ssl->ctx->next_protos_advertised_cb == NULL ||
1455 SSL_IS_DTLS(ssl)) {
1456 return 1;
1457 }
1458
1459 ssl->s3->next_proto_neg_seen = 1;
1460 return 1;
1461}
1462
1463static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1464 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1465 * parsed. */
1466 if (!ssl->s3->next_proto_neg_seen) {
1467 return 1;
1468 }
1469
1470 const uint8_t *npa;
1471 unsigned npa_len;
1472
1473 if (ssl->ctx->next_protos_advertised_cb(
1474 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1475 SSL_TLSEXT_ERR_OK) {
1476 ssl->s3->next_proto_neg_seen = 0;
1477 return 1;
1478 }
1479
1480 CBB contents;
1481 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1482 !CBB_add_u16_length_prefixed(out, &contents) ||
1483 !CBB_add_bytes(&contents, npa, npa_len) ||
1484 !CBB_flush(out)) {
1485 return 0;
1486 }
1487
1488 return 1;
1489}
1490
1491
Adam Langleyab8d87d2015-07-10 12:21:39 -07001492/* Signed certificate timestamps.
1493 *
1494 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1495
1496static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1497 if (!ssl->signed_cert_timestamps_enabled) {
1498 return 1;
1499 }
1500
1501 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1502 !CBB_add_u16(out, 0 /* length */)) {
1503 return 0;
1504 }
1505
1506 return 1;
1507}
1508
1509static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1510 CBS *contents) {
1511 if (contents == NULL) {
1512 return 1;
1513 }
1514
1515 /* If this is false then we should never have sent the SCT extension in the
1516 * ClientHello and thus this function should never have been called. */
1517 assert(ssl->signed_cert_timestamps_enabled);
1518
1519 if (CBS_len(contents) == 0) {
1520 *out_alert = SSL_AD_DECODE_ERROR;
1521 return 0;
1522 }
1523
1524 /* Session resumption uses the original session information. */
1525 if (!ssl->hit &&
1526 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1527 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1528 *out_alert = SSL_AD_INTERNAL_ERROR;
1529 return 0;
1530 }
1531
1532 return 1;
1533}
1534
1535static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1536 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001537 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001538}
1539
1540static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001541 /* The extension shouldn't be sent when resuming sessions. */
1542 if (ssl->hit ||
1543 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001544 return 1;
1545 }
1546
1547 CBB contents;
1548 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1549 CBB_add_u16_length_prefixed(out, &contents) &&
1550 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1551 ssl->ctx->signed_cert_timestamp_list_length) &&
1552 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001553}
1554
1555
Adam Langleyf18e4532015-07-10 13:39:53 -07001556/* Application-level Protocol Negotiation.
1557 *
1558 * https://tools.ietf.org/html/rfc7301 */
1559
1560static void ext_alpn_init(SSL *ssl) {
1561 OPENSSL_free(ssl->s3->alpn_selected);
1562 ssl->s3->alpn_selected = NULL;
1563}
1564
1565static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1566 if (ssl->alpn_client_proto_list == NULL ||
1567 ssl->s3->initial_handshake_complete) {
1568 return 1;
1569 }
1570
1571 CBB contents, proto_list;
1572 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1573 !CBB_add_u16_length_prefixed(out, &contents) ||
1574 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1575 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1576 ssl->alpn_client_proto_list_len) ||
1577 !CBB_flush(out)) {
1578 return 0;
1579 }
1580
1581 return 1;
1582}
1583
1584static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1585 CBS *contents) {
1586 if (contents == NULL) {
1587 return 1;
1588 }
1589
1590 assert(!ssl->s3->initial_handshake_complete);
1591 assert(ssl->alpn_client_proto_list != NULL);
1592
David Benjamin76c2efc2015-08-31 14:24:29 -04001593 if (ssl->s3->next_proto_neg_seen) {
1594 /* NPN and ALPN may not be negotiated in the same connection. */
1595 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1596 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1597 return 0;
1598 }
1599
Adam Langleyf18e4532015-07-10 13:39:53 -07001600 /* The extension data consists of a ProtocolNameList which must have
1601 * exactly one ProtocolName. Each of these is length-prefixed. */
1602 CBS protocol_name_list, protocol_name;
1603 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1604 CBS_len(contents) != 0 ||
1605 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1606 /* Empty protocol names are forbidden. */
1607 CBS_len(&protocol_name) == 0 ||
1608 CBS_len(&protocol_name_list) != 0) {
1609 return 0;
1610 }
1611
1612 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1613 &ssl->s3->alpn_selected_len)) {
1614 *out_alert = SSL_AD_INTERNAL_ERROR;
1615 return 0;
1616 }
1617
1618 return 1;
1619}
1620
1621static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1622 CBS *contents) {
1623 if (contents == NULL) {
1624 return 1;
1625 }
1626
1627 if (ssl->ctx->alpn_select_cb == NULL ||
1628 ssl->s3->initial_handshake_complete) {
1629 return 1;
1630 }
1631
1632 /* ALPN takes precedence over NPN. */
1633 ssl->s3->next_proto_neg_seen = 0;
1634
1635 CBS protocol_name_list;
1636 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1637 CBS_len(contents) != 0 ||
1638 CBS_len(&protocol_name_list) < 2) {
1639 return 0;
1640 }
1641
1642 /* Validate the protocol list. */
1643 CBS protocol_name_list_copy = protocol_name_list;
1644 while (CBS_len(&protocol_name_list_copy) > 0) {
1645 CBS protocol_name;
1646
1647 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1648 /* Empty protocol names are forbidden. */
1649 CBS_len(&protocol_name) == 0) {
1650 return 0;
1651 }
1652 }
1653
1654 const uint8_t *selected;
1655 uint8_t selected_len;
1656 if (ssl->ctx->alpn_select_cb(
1657 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1658 CBS_len(&protocol_name_list),
1659 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1660 OPENSSL_free(ssl->s3->alpn_selected);
1661 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1662 if (ssl->s3->alpn_selected == NULL) {
1663 *out_alert = SSL_AD_INTERNAL_ERROR;
1664 return 0;
1665 }
1666 ssl->s3->alpn_selected_len = selected_len;
1667 }
1668
1669 return 1;
1670}
1671
1672static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1673 if (ssl->s3->alpn_selected == NULL) {
1674 return 1;
1675 }
1676
1677 CBB contents, proto_list, proto;
1678 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1679 !CBB_add_u16_length_prefixed(out, &contents) ||
1680 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1681 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1682 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1683 !CBB_flush(out)) {
1684 return 0;
1685 }
1686
1687 return 1;
1688}
1689
1690
Adam Langley49c7af12015-07-10 14:33:46 -07001691/* Channel ID.
1692 *
1693 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1694
1695static void ext_channel_id_init(SSL *ssl) {
1696 ssl->s3->tlsext_channel_id_valid = 0;
1697}
1698
1699static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1700 if (!ssl->tlsext_channel_id_enabled ||
1701 SSL_IS_DTLS(ssl)) {
1702 return 1;
1703 }
1704
1705 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1706 !CBB_add_u16(out, 0 /* length */)) {
1707 return 0;
1708 }
1709
1710 return 1;
1711}
1712
1713static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1714 CBS *contents) {
1715 if (contents == NULL) {
1716 return 1;
1717 }
1718
1719 assert(!SSL_IS_DTLS(ssl));
1720 assert(ssl->tlsext_channel_id_enabled);
1721
1722 if (CBS_len(contents) != 0) {
1723 return 0;
1724 }
1725
1726 ssl->s3->tlsext_channel_id_valid = 1;
1727 return 1;
1728}
1729
1730static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1731 CBS *contents) {
1732 if (contents == NULL ||
1733 !ssl->tlsext_channel_id_enabled ||
1734 SSL_IS_DTLS(ssl)) {
1735 return 1;
1736 }
1737
1738 if (CBS_len(contents) != 0) {
1739 return 0;
1740 }
1741
1742 ssl->s3->tlsext_channel_id_valid = 1;
1743 return 1;
1744}
1745
1746static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1747 if (!ssl->s3->tlsext_channel_id_valid) {
1748 return 1;
1749 }
1750
1751 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1752 !CBB_add_u16(out, 0 /* length */)) {
1753 return 0;
1754 }
1755
1756 return 1;
1757}
1758
Adam Langley391250d2015-07-15 19:06:07 -07001759
1760/* Secure Real-time Transport Protocol (SRTP) extension.
1761 *
1762 * https://tools.ietf.org/html/rfc5764 */
1763
Adam Langley391250d2015-07-15 19:06:07 -07001764
1765static void ext_srtp_init(SSL *ssl) {
1766 ssl->srtp_profile = NULL;
1767}
1768
1769static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1770 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1771 if (profiles == NULL) {
1772 return 1;
1773 }
1774 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1775 if (num_profiles == 0) {
1776 return 1;
1777 }
1778
1779 CBB contents, profile_ids;
1780 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1781 !CBB_add_u16_length_prefixed(out, &contents) ||
1782 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1783 return 0;
1784 }
1785
1786 size_t i;
1787 for (i = 0; i < num_profiles; i++) {
1788 if (!CBB_add_u16(&profile_ids,
1789 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1790 return 0;
1791 }
1792 }
1793
1794 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1795 !CBB_flush(out)) {
1796 return 0;
1797 }
1798
1799 return 1;
1800}
1801
1802static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1803 CBS *contents) {
1804 if (contents == NULL) {
1805 return 1;
1806 }
1807
1808 /* The extension consists of a u16-prefixed profile ID list containing a
1809 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1810 *
1811 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1812 CBS profile_ids, srtp_mki;
1813 uint16_t profile_id;
1814 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1815 !CBS_get_u16(&profile_ids, &profile_id) ||
1816 CBS_len(&profile_ids) != 0 ||
1817 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1818 CBS_len(contents) != 0) {
1819 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1820 return 0;
1821 }
1822
1823 if (CBS_len(&srtp_mki) != 0) {
1824 /* Must be no MKI, since we never offer one. */
1825 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1826 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1827 return 0;
1828 }
1829
1830 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1831
1832 /* Check to see if the server gave us something we support (and presumably
1833 * offered). */
1834 size_t i;
1835 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1836 const SRTP_PROTECTION_PROFILE *profile =
1837 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1838
1839 if (profile->id == profile_id) {
1840 ssl->srtp_profile = profile;
1841 return 1;
1842 }
1843 }
1844
1845 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1846 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1847 return 0;
1848}
1849
1850static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1851 CBS *contents) {
1852 if (contents == NULL) {
1853 return 1;
1854 }
1855
1856 CBS profile_ids, srtp_mki;
1857 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1858 CBS_len(&profile_ids) < 2 ||
1859 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1860 CBS_len(contents) != 0) {
1861 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1862 return 0;
1863 }
1864 /* Discard the MKI value for now. */
1865
1866 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1867 SSL_get_srtp_profiles(ssl);
1868
1869 /* Pick the server's most preferred profile. */
1870 size_t i;
1871 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1872 const SRTP_PROTECTION_PROFILE *server_profile =
1873 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1874
1875 CBS profile_ids_tmp;
1876 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1877
1878 while (CBS_len(&profile_ids_tmp) > 0) {
1879 uint16_t profile_id;
1880 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1881 return 0;
1882 }
1883
1884 if (server_profile->id == profile_id) {
1885 ssl->srtp_profile = server_profile;
1886 return 1;
1887 }
1888 }
1889 }
1890
1891 return 1;
1892}
1893
1894static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1895 if (ssl->srtp_profile == NULL) {
1896 return 1;
1897 }
1898
1899 CBB contents, profile_ids;
1900 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1901 !CBB_add_u16_length_prefixed(out, &contents) ||
1902 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1903 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1904 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1905 !CBB_flush(out)) {
1906 return 0;
1907 }
1908
1909 return 1;
1910}
1911
Adam Langleybdd5d662015-07-20 16:19:08 -07001912
1913/* EC point formats.
1914 *
1915 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1916
1917static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1918 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1919 return 0;
1920 }
1921
1922 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1923
1924 size_t i;
1925 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1926 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1927
1928 const uint32_t alg_k = cipher->algorithm_mkey;
1929 const uint32_t alg_a = cipher->algorithm_auth;
1930 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1931 return 1;
1932 }
1933 }
1934
1935 return 0;
1936}
1937
Adam Langleybdd5d662015-07-20 16:19:08 -07001938static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001939 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001940 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1941 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001942 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1943 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001944 !CBB_flush(out)) {
1945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1952 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1953 return 1;
1954 }
1955
1956 return ext_ec_point_add_extension(ssl, out);
1957}
1958
1959static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1960 CBS *contents) {
1961 if (contents == NULL) {
1962 return 1;
1963 }
1964
1965 CBS ec_point_format_list;
1966 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1967 CBS_len(contents) != 0) {
1968 return 0;
1969 }
1970
David Benjaminfc059942015-07-30 23:01:59 -04001971 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1972 * point format. */
1973 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1974 CBS_len(&ec_point_format_list)) == NULL) {
1975 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001976 return 0;
1977 }
1978
1979 return 1;
1980}
1981
1982static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1983 CBS *contents) {
1984 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1985}
1986
1987static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1988 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1989 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001990 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001991
1992 if (!using_ecc) {
1993 return 1;
1994 }
1995
1996 return ext_ec_point_add_extension(ssl, out);
1997}
1998
Adam Langley273d49c2015-07-20 16:38:52 -07001999
2000/* EC supported curves.
2001 *
2002 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2003
2004static void ext_ec_curves_init(SSL *ssl) {
2005 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2006 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2007 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2008}
2009
2010static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2011 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2012 return 1;
2013 }
2014
2015 CBB contents, curves_bytes;
2016 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2017 !CBB_add_u16_length_prefixed(out, &contents) ||
2018 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2019 return 0;
2020 }
2021
2022 const uint16_t *curves;
2023 size_t curves_len;
2024 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2025
2026 size_t i;
2027 for (i = 0; i < curves_len; i++) {
2028 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2029 return 0;
2030 }
2031 }
2032
2033 return CBB_flush(out);
2034}
2035
2036static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2037 CBS *contents) {
2038 /* This extension is not expected to be echoed by servers and is ignored. */
2039 return 1;
2040}
2041
2042static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2043 CBS *contents) {
2044 if (contents == NULL) {
2045 return 1;
2046 }
2047
2048 CBS elliptic_curve_list;
2049 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2050 CBS_len(&elliptic_curve_list) == 0 ||
2051 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2052 CBS_len(contents) != 0) {
2053 return 0;
2054 }
2055
2056 ssl->s3->tmp.peer_ellipticcurvelist =
2057 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2058
2059 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2060 *out_alert = SSL_AD_INTERNAL_ERROR;
2061 return 0;
2062 }
2063
2064 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2065 size_t i;
2066 for (i = 0; i < num_curves; i++) {
2067 if (!CBS_get_u16(&elliptic_curve_list,
2068 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2069 goto err;
2070 }
2071 }
2072
2073 assert(CBS_len(&elliptic_curve_list) == 0);
2074 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2075
2076 return 1;
2077
2078err:
2079 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2080 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2081 *out_alert = SSL_AD_INTERNAL_ERROR;
2082 return 0;
2083}
2084
2085static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2086 /* Servers don't echo this extension. */
2087 return 1;
2088}
2089
2090
Adam Langley614c66a2015-06-12 15:26:58 -07002091/* kExtensions contains all the supported extensions. */
2092static const struct tls_extension kExtensions[] = {
2093 {
Adam Langley5021b222015-06-12 18:27:58 -07002094 /* The renegotiation extension must always be at index zero because the
2095 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2096 * sent as an SCSV. */
2097 TLSEXT_TYPE_renegotiate,
2098 NULL,
2099 ext_ri_add_clienthello,
2100 ext_ri_parse_serverhello,
2101 ext_ri_parse_clienthello,
2102 ext_ri_add_serverhello,
2103 },
2104 {
Adam Langley614c66a2015-06-12 15:26:58 -07002105 TLSEXT_TYPE_server_name,
2106 ext_sni_init,
2107 ext_sni_add_clienthello,
2108 ext_sni_parse_serverhello,
2109 ext_sni_parse_clienthello,
2110 ext_sni_add_serverhello,
2111 },
Adam Langley0a056712015-07-01 15:03:33 -07002112 {
2113 TLSEXT_TYPE_extended_master_secret,
2114 ext_ems_init,
2115 ext_ems_add_clienthello,
2116 ext_ems_parse_serverhello,
2117 ext_ems_parse_clienthello,
2118 ext_ems_add_serverhello,
2119 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002120 {
2121 TLSEXT_TYPE_session_ticket,
2122 NULL,
2123 ext_ticket_add_clienthello,
2124 ext_ticket_parse_serverhello,
2125 ext_ticket_parse_clienthello,
2126 ext_ticket_add_serverhello,
2127 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002128 {
2129 TLSEXT_TYPE_signature_algorithms,
2130 NULL,
2131 ext_sigalgs_add_clienthello,
2132 ext_sigalgs_parse_serverhello,
2133 ext_sigalgs_parse_clienthello,
2134 ext_sigalgs_add_serverhello,
2135 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002136 {
2137 TLSEXT_TYPE_status_request,
2138 ext_ocsp_init,
2139 ext_ocsp_add_clienthello,
2140 ext_ocsp_parse_serverhello,
2141 ext_ocsp_parse_clienthello,
2142 ext_ocsp_add_serverhello,
2143 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002144 {
2145 TLSEXT_TYPE_next_proto_neg,
2146 ext_npn_init,
2147 ext_npn_add_clienthello,
2148 ext_npn_parse_serverhello,
2149 ext_npn_parse_clienthello,
2150 ext_npn_add_serverhello,
2151 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002152 {
2153 TLSEXT_TYPE_certificate_timestamp,
2154 NULL,
2155 ext_sct_add_clienthello,
2156 ext_sct_parse_serverhello,
2157 ext_sct_parse_clienthello,
2158 ext_sct_add_serverhello,
2159 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002160 {
2161 TLSEXT_TYPE_application_layer_protocol_negotiation,
2162 ext_alpn_init,
2163 ext_alpn_add_clienthello,
2164 ext_alpn_parse_serverhello,
2165 ext_alpn_parse_clienthello,
2166 ext_alpn_add_serverhello,
2167 },
Adam Langley49c7af12015-07-10 14:33:46 -07002168 {
2169 TLSEXT_TYPE_channel_id,
2170 ext_channel_id_init,
2171 ext_channel_id_add_clienthello,
2172 ext_channel_id_parse_serverhello,
2173 ext_channel_id_parse_clienthello,
2174 ext_channel_id_add_serverhello,
2175 },
Adam Langley391250d2015-07-15 19:06:07 -07002176 {
2177 TLSEXT_TYPE_srtp,
2178 ext_srtp_init,
2179 ext_srtp_add_clienthello,
2180 ext_srtp_parse_serverhello,
2181 ext_srtp_parse_clienthello,
2182 ext_srtp_add_serverhello,
2183 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002184 {
2185 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002186 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002187 ext_ec_point_add_clienthello,
2188 ext_ec_point_parse_serverhello,
2189 ext_ec_point_parse_clienthello,
2190 ext_ec_point_add_serverhello,
2191 },
Adam Langley273d49c2015-07-20 16:38:52 -07002192 {
2193 TLSEXT_TYPE_elliptic_curves,
2194 ext_ec_curves_init,
2195 ext_ec_curves_add_clienthello,
2196 ext_ec_curves_parse_serverhello,
2197 ext_ec_curves_parse_clienthello,
2198 ext_ec_curves_add_serverhello,
2199 },
Adam Langley614c66a2015-06-12 15:26:58 -07002200};
2201
2202#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2203
Adam Langley4cfa96b2015-07-01 11:56:55 -07002204OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2205 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002206 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002207OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2208 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2209 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002210 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002211
Adam Langley614c66a2015-06-12 15:26:58 -07002212static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2213 uint16_t value) {
2214 unsigned i;
2215 for (i = 0; i < kNumExtensions; i++) {
2216 if (kExtensions[i].value == value) {
2217 *out_index = i;
2218 return &kExtensions[i];
2219 }
2220 }
2221
2222 return NULL;
2223}
2224
Adam Langley09505632015-07-30 18:10:13 -07002225int SSL_extension_supported(unsigned extension_value) {
2226 uint32_t index;
2227 return extension_value == TLSEXT_TYPE_padding ||
2228 tls_extension_find(&index, extension_value) != NULL;
2229}
2230
David Benjamine8d53502015-10-10 14:13:23 -04002231int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002232 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002233 if (ssl->client_version == SSL3_VERSION &&
2234 !ssl->s3->send_connection_binding) {
2235 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 }
Adam Langley95c29f32014-06-20 12:00:00 -07002237
David Benjamine8d53502015-10-10 14:13:23 -04002238 size_t orig_len = CBB_len(out);
2239 CBB extensions;
2240 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002241 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
David Benjamine8d53502015-10-10 14:13:23 -04002244 ssl->s3->tmp.extensions.sent = 0;
2245 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langley614c66a2015-06-12 15:26:58 -07002247 size_t i;
2248 for (i = 0; i < kNumExtensions; i++) {
2249 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002250 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002251 }
2252 }
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langley614c66a2015-06-12 15:26:58 -07002254 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002255 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002256 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002257 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2258 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2259 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langley33ad2b52015-07-20 17:43:53 -07002262 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002263 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002264 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 }
Adam Langley75712922014-10-10 16:23:43 -07002266
David Benjamine8d53502015-10-10 14:13:23 -04002267 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002268 goto err;
2269 }
2270
David Benjamine8d53502015-10-10 14:13:23 -04002271 if (!SSL_IS_DTLS(ssl)) {
2272 header_len += CBB_len(&extensions) - orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002274 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 *
2276 * NB: because this code works out the length of all existing extensions
2277 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002278 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 /* Extensions take at least four bytes to encode. Always include least
2280 * one byte of data if including the extension. WebSphere Application
2281 * Server 7.0 is intolerant to the last extension being zero-length. */
2282 if (padding_len >= 4 + 1) {
2283 padding_len -= 4;
2284 } else {
2285 padding_len = 1;
2286 }
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langley33ad2b52015-07-20 17:43:53 -07002288 uint8_t *padding_bytes;
2289 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2290 !CBB_add_u16(&extensions, padding_len) ||
2291 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2292 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 }
Adam Langley75712922014-10-10 16:23:43 -07002294
Adam Langley33ad2b52015-07-20 17:43:53 -07002295 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002296 }
2297 }
Adam Langley75712922014-10-10 16:23:43 -07002298
Adam Langley33ad2b52015-07-20 17:43:53 -07002299 /* If only two bytes have been written then the extensions are actually empty
2300 * and those two bytes are the zero length. In that case, we don't bother
2301 * sending the extensions length. */
David Benjamine8d53502015-10-10 14:13:23 -04002302 if (CBB_len(&extensions) - orig_len == 2) {
2303 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002304 }
2305
David Benjamine8d53502015-10-10 14:13:23 -04002306 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002307
2308err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002309 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002310 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002311}
Adam Langley95c29f32014-06-20 12:00:00 -07002312
David Benjamin56380462015-10-10 14:59:09 -04002313int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
2314 const size_t orig_len = CBB_len(out);
2315
2316 CBB extensions;
2317 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002318 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002319 }
2320
2321 unsigned i;
2322 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002323 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002324 /* Don't send extensions that were not received. */
2325 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 }
Adam Langley95c29f32014-06-20 12:00:00 -07002327
David Benjamin56380462015-10-10 14:59:09 -04002328 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002329 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2330 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2331 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002332 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 }
Adam Langley95c29f32014-06-20 12:00:00 -07002334
David Benjamin56380462015-10-10 14:59:09 -04002335 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002336 goto err;
2337 }
2338
Adam Langley33ad2b52015-07-20 17:43:53 -07002339 /* If only two bytes have been written then the extensions are actually empty
2340 * and those two bytes are the zero length. In that case, we don't bother
2341 * sending the extensions length. */
David Benjamin56380462015-10-10 14:59:09 -04002342 if (CBB_len(&extensions) - orig_len == 2) {
2343 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002344 }
2345
David Benjamin56380462015-10-10 14:59:09 -04002346 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002347
2348err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002349 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002350 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002354 size_t i;
2355 for (i = 0; i < kNumExtensions; i++) {
2356 if (kExtensions[i].init != NULL) {
2357 kExtensions[i].init(s);
2358 }
2359 }
2360
2361 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002362 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002363 /* The renegotiation extension must always be at index zero because the
2364 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2365 * sent as an SCSV. */
2366 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002369 if (CBS_len(cbs) != 0) {
2370 /* Decode the extensions block and check it is valid. */
2371 CBS extensions;
2372 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2373 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 *out_alert = SSL_AD_DECODE_ERROR;
2375 return 0;
2376 }
Adam Langley95c29f32014-06-20 12:00:00 -07002377
Adam Langley33ad2b52015-07-20 17:43:53 -07002378 while (CBS_len(&extensions) != 0) {
2379 uint16_t type;
2380 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langley33ad2b52015-07-20 17:43:53 -07002382 /* Decode the next extension. */
2383 if (!CBS_get_u16(&extensions, &type) ||
2384 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2385 *out_alert = SSL_AD_DECODE_ERROR;
2386 return 0;
2387 }
2388
David Benjaminc7ce9772015-10-09 19:32:41 -04002389 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2390 * ambiguous. Ignore all but the renegotiation_info extension. */
2391 if (s->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2392 continue;
2393 }
2394
Adam Langley33ad2b52015-07-20 17:43:53 -07002395 unsigned ext_index;
2396 const struct tls_extension *const ext =
2397 tls_extension_find(&ext_index, type);
2398
2399 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002400 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2401 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2402 return 0;
2403 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002404 continue;
2405 }
2406
Adam Langley614c66a2015-06-12 15:26:58 -07002407 s->s3->tmp.extensions.received |= (1u << ext_index);
2408 uint8_t alert = SSL_AD_DECODE_ERROR;
2409 if (!ext->parse_clienthello(s, &alert, &extension)) {
2410 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002411 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2412 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 return 0;
2414 }
Adam Langley614c66a2015-06-12 15:26:58 -07002415 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 }
Adam Langley75712922014-10-10 16:23:43 -07002417
Adam Langley614c66a2015-06-12 15:26:58 -07002418 for (i = 0; i < kNumExtensions; i++) {
2419 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2420 /* Extension wasn't observed so call the callback with a NULL
2421 * parameter. */
2422 uint8_t alert = SSL_AD_DECODE_ERROR;
2423 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002424 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2425 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002426 *out_alert = alert;
2427 return 0;
2428 }
2429 }
2430 }
2431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432 return 1;
2433}
Adam Langley95c29f32014-06-20 12:00:00 -07002434
Adam Langleyfcf25832014-12-18 17:42:32 -08002435int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2436 int alert = -1;
2437 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2438 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2439 return 0;
2440 }
Adam Langley95c29f32014-06-20 12:00:00 -07002441
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002443 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 return 0;
2445 }
Adam Langley95c29f32014-06-20 12:00:00 -07002446
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 return 1;
2448}
Adam Langley95c29f32014-06-20 12:00:00 -07002449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002451 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002452 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002453
Adam Langley33ad2b52015-07-20 17:43:53 -07002454 if (CBS_len(cbs) != 0) {
2455 /* Decode the extensions block and check it is valid. */
2456 CBS extensions;
2457 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2458 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 *out_alert = SSL_AD_DECODE_ERROR;
2460 return 0;
2461 }
Adam Langley95c29f32014-06-20 12:00:00 -07002462
Adam Langley614c66a2015-06-12 15:26:58 -07002463
Adam Langley33ad2b52015-07-20 17:43:53 -07002464 while (CBS_len(&extensions) != 0) {
2465 uint16_t type;
2466 CBS extension;
2467
2468 /* Decode the next extension. */
2469 if (!CBS_get_u16(&extensions, &type) ||
2470 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2471 *out_alert = SSL_AD_DECODE_ERROR;
2472 return 0;
2473 }
2474
2475 unsigned ext_index;
2476 const struct tls_extension *const ext =
2477 tls_extension_find(&ext_index, type);
2478
Adam Langley09505632015-07-30 18:10:13 -07002479 if (ext == NULL) {
2480 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2481 return 0;
2482 }
2483 continue;
2484 }
2485
2486 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2487 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002488 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002489 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002490 *out_alert = SSL_AD_DECODE_ERROR;
2491 return 0;
2492 }
David Benjamin03973092014-06-24 23:27:17 -04002493
Adam Langley614c66a2015-06-12 15:26:58 -07002494 received |= (1u << ext_index);
2495
2496 uint8_t alert = SSL_AD_DECODE_ERROR;
2497 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002498 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2499 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002500 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 return 0;
2502 }
Adam Langley614c66a2015-06-12 15:26:58 -07002503 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 }
Adam Langley95c29f32014-06-20 12:00:00 -07002505
Adam Langley33ad2b52015-07-20 17:43:53 -07002506 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002507 for (i = 0; i < kNumExtensions; i++) {
2508 if (!(received & (1u << i))) {
2509 /* Extension wasn't observed so call the callback with a NULL
2510 * parameter. */
2511 uint8_t alert = SSL_AD_DECODE_ERROR;
2512 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002513 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2514 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002515 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 return 0;
2517 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 }
2519 }
Adam Langley95c29f32014-06-20 12:00:00 -07002520
Adam Langleyfcf25832014-12-18 17:42:32 -08002521 return 1;
2522}
Adam Langley95c29f32014-06-20 12:00:00 -07002523
Adam Langleyfcf25832014-12-18 17:42:32 -08002524static int ssl_check_clienthello_tlsext(SSL *s) {
2525 int ret = SSL_TLSEXT_ERR_NOACK;
2526 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2529 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002530
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2532 ret = s->ctx->tlsext_servername_callback(s, &al,
2533 s->ctx->tlsext_servername_arg);
2534 } else if (s->initial_ctx != NULL &&
2535 s->initial_ctx->tlsext_servername_callback != 0) {
2536 ret = s->initial_ctx->tlsext_servername_callback(
2537 s, &al, s->initial_ctx->tlsext_servername_arg);
2538 }
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540 switch (ret) {
2541 case SSL_TLSEXT_ERR_ALERT_FATAL:
2542 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2543 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545 case SSL_TLSEXT_ERR_ALERT_WARNING:
2546 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2547 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002550 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langleyfcf25832014-12-18 17:42:32 -08002553 default:
2554 return 1;
2555 }
2556}
Adam Langleyed8270a2014-09-02 13:52:56 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002559 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002560 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002561
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2563 ret = s->ctx->tlsext_servername_callback(s, &al,
2564 s->ctx->tlsext_servername_arg);
2565 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002566 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 ret = s->initial_ctx->tlsext_servername_callback(
2568 s, &al, s->initial_ctx->tlsext_servername_arg);
2569 }
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 switch (ret) {
2572 case SSL_TLSEXT_ERR_ALERT_FATAL:
2573 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2574 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 case SSL_TLSEXT_ERR_ALERT_WARNING:
2577 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2578 return 1;
2579
2580 default:
2581 return 1;
2582 }
2583}
2584
2585int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2586 int alert = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002587 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2588 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2589 return 0;
2590 }
2591
2592 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002593 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 return 0;
2595 }
2596
2597 return 1;
2598}
Adam Langley95c29f32014-06-20 12:00:00 -07002599
David Benjamine3aa1d92015-06-16 15:34:50 -04002600int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2601 int *out_send_ticket, const uint8_t *ticket,
2602 size_t ticket_len, const uint8_t *session_id,
2603 size_t session_id_len) {
2604 int ret = 1; /* Most errors are non-fatal. */
2605 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2606 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002607
David Benjamine3aa1d92015-06-16 15:34:50 -04002608 HMAC_CTX hmac_ctx;
2609 HMAC_CTX_init(&hmac_ctx);
2610 EVP_CIPHER_CTX cipher_ctx;
2611 EVP_CIPHER_CTX_init(&cipher_ctx);
2612
2613 *out_send_ticket = 0;
2614 *out_session = NULL;
2615
2616 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2617 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 }
2619
David Benjamine3aa1d92015-06-16 15:34:50 -04002620 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002622 *out_send_ticket = 1;
2623 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 }
2625
David Benjaminadcc3952015-04-26 13:07:57 -04002626 /* Ensure there is room for the key name and the largest IV
2627 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2628 * the maximum IV length should be well under the minimum size for the
2629 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002630 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2631 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002633 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002634
David Benjamine3aa1d92015-06-16 15:34:50 -04002635 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2636 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2637 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2638 0 /* decrypt */);
2639 if (cb_ret < 0) {
2640 ret = 0;
2641 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002643 if (cb_ret == 0) {
2644 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002646 if (cb_ret == 2) {
2647 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 }
2649 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002650 /* Check the key name matches. */
2651 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2652 SSL_TICKET_KEY_NAME_LEN) != 0) {
2653 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002655 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2656 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002658 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2659 ssl_ctx->tlsext_tick_aes_key, iv)) {
2660 ret = 0;
2661 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 }
2663 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002664 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002665
David Benjamine3aa1d92015-06-16 15:34:50 -04002666 /* Check the MAC at the end of the ticket. */
2667 uint8_t mac[EVP_MAX_MD_SIZE];
2668 size_t mac_len = HMAC_size(&hmac_ctx);
2669 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002670 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002671 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002673 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2674 HMAC_Final(&hmac_ctx, mac, NULL);
2675 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2676 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002677 }
2678
David Benjamine3aa1d92015-06-16 15:34:50 -04002679 /* Decrypt the session data. */
2680 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2681 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2682 mac_len;
2683 plaintext = OPENSSL_malloc(ciphertext_len);
2684 if (plaintext == NULL) {
2685 ret = 0;
2686 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002688 if (ciphertext_len >= INT_MAX) {
2689 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002691 int len1, len2;
2692 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2693 (int)ciphertext_len) ||
2694 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2695 ERR_clear_error(); /* Don't leave an error on the queue. */
2696 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002697 }
2698
David Benjamine3aa1d92015-06-16 15:34:50 -04002699 /* Decode the session. */
2700 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2701 if (session == NULL) {
2702 ERR_clear_error(); /* Don't leave an error on the queue. */
2703 goto done;
2704 }
2705
2706 /* Copy the client's session ID into the new session, to denote the ticket has
2707 * been accepted. */
2708 memcpy(session->session_id, session_id, session_id_len);
2709 session->session_id_length = session_id_len;
2710
2711 *out_session = session;
2712
2713done:
2714 OPENSSL_free(plaintext);
2715 HMAC_CTX_cleanup(&hmac_ctx);
2716 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2717 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002718}
Adam Langley95c29f32014-06-20 12:00:00 -07002719
2720/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002721typedef struct {
2722 int nid;
2723 int id;
2724} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2727 {NID_sha1, TLSEXT_hash_sha1},
2728 {NID_sha224, TLSEXT_hash_sha224},
2729 {NID_sha256, TLSEXT_hash_sha256},
2730 {NID_sha384, TLSEXT_hash_sha384},
2731 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2734 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002735
Adam Langleyfcf25832014-12-18 17:42:32 -08002736static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2737 size_t i;
2738 for (i = 0; i < tlen; i++) {
2739 if (table[i].nid == nid) {
2740 return table[i].id;
2741 }
2742 }
Adam Langley95c29f32014-06-20 12:00:00 -07002743
Adam Langleyfcf25832014-12-18 17:42:32 -08002744 return -1;
2745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
David Benjaminb4d65fd2015-05-29 17:11:21 -04002747int tls12_get_sigid(int pkey_type) {
2748 return tls12_find_id(pkey_type, tls12_sig,
2749 sizeof(tls12_sig) / sizeof(tls12_lookup));
2750}
2751
David Benjamind1d80782015-07-05 11:54:09 -04002752int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002754
Adam Langleyfcf25832014-12-18 17:42:32 -08002755 if (!md) {
2756 return 0;
2757 }
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2760 sizeof(tls12_md) / sizeof(tls12_lookup));
2761 if (md_id == -1) {
2762 return 0;
2763 }
Adam Langley95c29f32014-06-20 12:00:00 -07002764
David Benjamind1d80782015-07-05 11:54:09 -04002765 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 if (sig_id == -1) {
2767 return 0;
2768 }
Adam Langley95c29f32014-06-20 12:00:00 -07002769
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 p[0] = (uint8_t)md_id;
2771 p[1] = (uint8_t)sig_id;
2772 return 1;
2773}
2774
Adam Langleyfcf25832014-12-18 17:42:32 -08002775const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2776 switch (hash_alg) {
2777 case TLSEXT_hash_md5:
2778 return EVP_md5();
2779
2780 case TLSEXT_hash_sha1:
2781 return EVP_sha1();
2782
2783 case TLSEXT_hash_sha224:
2784 return EVP_sha224();
2785
2786 case TLSEXT_hash_sha256:
2787 return EVP_sha256();
2788
2789 case TLSEXT_hash_sha384:
2790 return EVP_sha384();
2791
2792 case TLSEXT_hash_sha512:
2793 return EVP_sha512();
2794
2795 default:
2796 return NULL;
2797 }
2798}
Adam Langley95c29f32014-06-20 12:00:00 -07002799
David Benjaminec2f27d2014-11-13 19:17:25 -05002800/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2801 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002802static int tls12_get_pkey_type(uint8_t sig_alg) {
2803 switch (sig_alg) {
2804 case TLSEXT_signature_rsa:
2805 return EVP_PKEY_RSA;
2806
2807 case TLSEXT_signature_ecdsa:
2808 return EVP_PKEY_EC;
2809
2810 default:
2811 return -1;
2812 }
2813}
Adam Langley95c29f32014-06-20 12:00:00 -07002814
Steven Valdez0d62f262015-09-04 12:41:04 -04002815OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2816 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002817
Steven Valdez0d62f262015-09-04 12:41:04 -04002818int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002820 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 return 1;
2822 }
David Benjamincd996942014-07-20 16:23:51 -04002823
Steven Valdez0d62f262015-09-04 12:41:04 -04002824 CERT *const cert = ssl->cert;
2825 OPENSSL_free(cert->peer_sigalgs);
2826 cert->peer_sigalgs = NULL;
2827 cert->peer_sigalgslen = 0;
2828
2829 size_t num_sigalgs = CBS_len(in_sigalgs);
2830
2831 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 return 0;
2833 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002834 num_sigalgs /= 2;
2835
2836 /* supported_signature_algorithms in the certificate request is
2837 * allowed to be empty. */
2838 if (num_sigalgs == 0) {
2839 return 1;
2840 }
2841
2842 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2843 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2844 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2845 if (cert->peer_sigalgs == NULL) {
2846 return 0;
2847 }
2848 cert->peer_sigalgslen = num_sigalgs;
2849
2850 CBS sigalgs;
2851 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2852
2853 size_t i;
2854 for (i = 0; i < num_sigalgs; i++) {
2855 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2856 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2857 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2858 return 0;
2859 }
2860 }
Adam Langley95c29f32014-06-20 12:00:00 -07002861
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 return 1;
2863}
David Benjaminec2f27d2014-11-13 19:17:25 -05002864
David Benjamind1d80782015-07-05 11:54:09 -04002865const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2866 CERT *cert = ssl->cert;
2867 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002868 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002869
Steven Valdez0d62f262015-09-04 12:41:04 -04002870 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2871 NID_sha224, NID_sha1};
2872
2873 const int *digest_nids = kDefaultDigestList;
2874 size_t num_digest_nids =
2875 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2876 if (cert->digest_nids != NULL) {
2877 digest_nids = cert->digest_nids;
2878 num_digest_nids = cert->num_digest_nids;
2879 }
2880
2881 for (i = 0; i < num_digest_nids; i++) {
2882 const int digest_nid = digest_nids[i];
2883 for (j = 0; j < cert->peer_sigalgslen; j++) {
2884 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2885 if (md == NULL ||
2886 digest_nid != EVP_MD_type(md) ||
2887 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2888 continue;
2889 }
2890
2891 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002892 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 }
Adam Langley95c29f32014-06-20 12:00:00 -07002894
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 /* If no suitable digest may be found, default to SHA-1. */
2896 return EVP_sha1();
2897}
Adam Langley95c29f32014-06-20 12:00:00 -07002898
David Benjamind6a4ae92015-08-06 11:10:51 -04002899int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2900 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002901 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002902
2903 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002904 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2905 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002906 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002907
David Benjamind6a4ae92015-08-06 11:10:51 -04002908 static const char kClientIDMagic[] = "TLS Channel ID signature";
2909 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2910
2911 if (ssl->hit) {
2912 static const char kResumptionMagic[] = "Resumption";
2913 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2914 if (ssl->session->original_handshake_hash_len == 0) {
2915 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2916 goto err;
2917 }
2918 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2919 ssl->session->original_handshake_hash_len);
2920 }
2921
2922 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2923 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2924 sizeof(handshake_hash));
2925 if (handshake_hash_len < 0) {
2926 goto err;
2927 }
2928 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2929 unsigned len_u;
2930 EVP_DigestFinal_ex(&ctx, out, &len_u);
2931 *out_len = len_u;
2932
2933 ret = 1;
2934
2935err:
2936 EVP_MD_CTX_cleanup(&ctx);
2937 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002938}
Adam Langley1258b6a2014-06-20 12:00:00 -07002939
2940/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2941 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002942int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2943 int digest_len;
2944 /* This function should never be called for a resumed session because the
2945 * handshake hashes that we wish to record are for the original, full
2946 * handshake. */
2947 if (s->hit) {
2948 return -1;
2949 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002950
Adam Langleyfcf25832014-12-18 17:42:32 -08002951 digest_len =
2952 tls1_handshake_digest(s, s->session->original_handshake_hash,
2953 sizeof(s->session->original_handshake_hash));
2954 if (digest_len < 0) {
2955 return -1;
2956 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002957
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002959
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 return 1;
2961}