blob: 9a290283a501b9d27193001492072ba8b285d022 [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>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700125#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin6c7aed02014-08-27 16:42:38 -0400130static int ssl_check_clienthello_tlsext(SSL *s);
131static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin338fcaf2014-12-11 01:20:52 -0500133const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500134 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 tls1_setup_key_block,
136 tls1_generate_master_secret,
137 tls1_change_cipher_state,
138 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800139 tls1_cert_verify_mac,
140 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
141 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
142 tls1_alert_code,
143 tls1_export_keying_material,
144 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800145};
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin338fcaf2014-12-11 01:20:52 -0500147const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500148 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 tls1_cert_verify_mac,
154 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
155 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
156 tls1_alert_code,
157 tls1_export_keying_material,
158 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159};
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin338fcaf2014-12-11 01:20:52 -0500161const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500162 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 tls1_setup_key_block,
164 tls1_generate_master_secret,
165 tls1_change_cipher_state,
166 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 tls1_cert_verify_mac,
168 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
169 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
170 tls1_alert_code,
171 tls1_export_keying_material,
David Benjaminc7817d82015-11-05 18:28:33 -0500172 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF,
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
David Benjamin6e807652015-11-02 12:02:20 -0500643int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
644 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 const uint8_t *sent_sigs;
646 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400647 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800648
649 /* Should never happen */
650 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400651 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 *out_alert = SSL_AD_INTERNAL_ERROR;
653 return 0;
654 }
655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 /* Check key type is consistent with signature */
657 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400658 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
660 return 0;
661 }
662
663 if (pkey->type == EVP_PKEY_EC) {
664 uint16_t curve_id;
665 uint8_t comp_id;
666 /* Check compression and curve matches extensions */
667 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
668 *out_alert = SSL_AD_INTERNAL_ERROR;
669 return 0;
670 }
671
David Benjamin6e807652015-11-02 12:02:20 -0500672 if (ssl->server && (!tls1_check_curve_id(ssl, curve_id) ||
673 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400674 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
676 return 0;
677 }
678 }
679
680 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500681 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
683 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
684 break;
685 }
686 }
687
David Benjamin788be4a2015-10-30 17:50:57 -0400688 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400689 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
691 return 0;
692 }
693
694 *out_md = tls12_get_hash(hash);
695 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400696 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800697 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
698 return 0;
699 }
700
701 return 1;
702}
703
704/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
705 * supported or doesn't appear in supported signature algorithms. Unlike
706 * ssl_cipher_get_disabled this applies to a specific session and not global
707 * settings. */
708void ssl_set_client_disabled(SSL *s) {
709 CERT *c = s->cert;
710 const uint8_t *sigalgs;
711 size_t i, sigalgslen;
712 int have_rsa = 0, have_ecdsa = 0;
713 c->mask_a = 0;
714 c->mask_k = 0;
715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716 /* Now go through all signature algorithms seeing if we support any for RSA,
717 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
718 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
719 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
720 switch (sigalgs[1]) {
721 case TLSEXT_signature_rsa:
722 have_rsa = 1;
723 break;
724
725 case TLSEXT_signature_ecdsa:
726 have_ecdsa = 1;
727 break;
728 }
729 }
730
731 /* Disable auth if we don't include any appropriate signature algorithms. */
732 if (!have_rsa) {
733 c->mask_a |= SSL_aRSA;
734 }
735 if (!have_ecdsa) {
736 c->mask_a |= SSL_aECDSA;
737 }
738
739 /* with PSK there must be client callback set */
740 if (!s->psk_client_callback) {
741 c->mask_a |= SSL_aPSK;
742 c->mask_k |= SSL_kPSK;
743 }
744}
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Adam Langley614c66a2015-06-12 15:26:58 -0700746/* tls_extension represents a TLS extension that is handled internally. The
747 * |init| function is called for each handshake, before any other functions of
748 * the extension. Then the add and parse callbacks are called as needed.
749 *
750 * The parse callbacks receive a |CBS| that contains the contents of the
751 * extension (i.e. not including the type and length bytes). If an extension is
752 * not received then the parse callbacks will be called with a NULL CBS so that
753 * they can do any processing needed to handle the absence of an extension.
754 *
755 * The add callbacks receive a |CBB| to which the extension can be appended but
756 * the function is responsible for appending the type and length bytes too.
757 *
758 * All callbacks return one for success and zero for error. If a parse function
759 * returns zero then a fatal alert with value |*out_alert| will be sent. If
760 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
761struct tls_extension {
762 uint16_t value;
763 void (*init)(SSL *ssl);
764
765 int (*add_clienthello)(SSL *ssl, CBB *out);
766 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
767
768 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
769 int (*add_serverhello)(SSL *ssl, CBB *out);
770};
771
772
773/* Server name indication (SNI).
774 *
775 * https://tools.ietf.org/html/rfc6066#section-3. */
776
777static void ext_sni_init(SSL *ssl) {
778 ssl->s3->tmp.should_ack_sni = 0;
779}
780
781static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
782 if (ssl->tlsext_hostname == NULL) {
783 return 1;
784 }
785
786 CBB contents, server_name_list, name;
787 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
788 !CBB_add_u16_length_prefixed(out, &contents) ||
789 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
790 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
791 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
792 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
793 strlen(ssl->tlsext_hostname)) ||
794 !CBB_flush(out)) {
795 return 0;
796 }
797
798 return 1;
799}
800
801static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
802 if (contents == NULL) {
803 return 1;
804 }
805
806 if (CBS_len(contents) != 0) {
807 return 0;
808 }
809
810 assert(ssl->tlsext_hostname != NULL);
811
812 if (!ssl->hit) {
813 assert(ssl->session->tlsext_hostname == NULL);
814 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
815 if (!ssl->session->tlsext_hostname) {
816 *out_alert = SSL_AD_INTERNAL_ERROR;
817 return 0;
818 }
819 }
820
821 return 1;
822}
823
824static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
825 if (contents == NULL) {
826 return 1;
827 }
828
829 /* The servername extension is treated as follows:
830 *
831 * - Only the hostname type is supported with a maximum length of 255.
832 * - The servername is rejected if too long or if it contains zeros, in
833 * which case an fatal alert is generated.
834 * - The servername field is maintained together with the session cache.
835 * - When a session is resumed, the servername callback is invoked in order
836 * to allow the application to position itself to the right context.
837 * - The servername is acknowledged if it is new for a session or when
838 * it is identical to a previously used for the same session.
839 * Applications can control the behaviour. They can at any time
840 * set a 'desirable' servername for a new SSL object. This can be the
841 * case for example with HTTPS when a Host: header field is received and
842 * a renegotiation is requested. In this case, a possible servername
843 * presented in the new client hello is only acknowledged if it matches
844 * the value of the Host: field.
845 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
846 * if they provide for changing an explicit servername context for the
847 * session,
848 * i.e. when the session has been established with a servername extension.
849 */
850
851 CBS server_name_list;
852 char have_seen_host_name = 0;
853
854 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
855 CBS_len(&server_name_list) == 0 ||
856 CBS_len(contents) != 0) {
857 return 0;
858 }
859
860 /* Decode each ServerName in the extension. */
861 while (CBS_len(&server_name_list) > 0) {
862 uint8_t name_type;
863 CBS host_name;
864
865 if (!CBS_get_u8(&server_name_list, &name_type) ||
866 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
867 return 0;
868 }
869
870 /* Only host_name is supported. */
871 if (name_type != TLSEXT_NAMETYPE_host_name) {
872 continue;
873 }
874
875 if (have_seen_host_name) {
876 /* The ServerNameList MUST NOT contain more than one name of the same
877 * name_type. */
878 return 0;
879 }
880
881 have_seen_host_name = 1;
882
883 if (CBS_len(&host_name) == 0 ||
884 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
885 CBS_contains_zero_byte(&host_name)) {
886 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
887 return 0;
888 }
889
890 if (!ssl->hit) {
891 assert(ssl->session->tlsext_hostname == NULL);
892 if (ssl->session->tlsext_hostname) {
893 /* This should be impossible. */
894 return 0;
895 }
896
897 /* Copy the hostname as a string. */
898 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
899 *out_alert = SSL_AD_INTERNAL_ERROR;
900 return 0;
901 }
902
903 ssl->s3->tmp.should_ack_sni = 1;
904 }
905 }
906
907 return 1;
908}
909
910static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
911 if (ssl->hit ||
912 !ssl->s3->tmp.should_ack_sni ||
913 ssl->session->tlsext_hostname == NULL) {
914 return 1;
915 }
916
917 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
918 !CBB_add_u16(out, 0 /* length */)) {
919 return 0;
920 }
921
922 return 1;
923}
924
925
Adam Langley5021b222015-06-12 18:27:58 -0700926/* Renegotiation indication.
927 *
928 * https://tools.ietf.org/html/rfc5746 */
929
930static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
931 CBB contents, prev_finished;
932 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
933 !CBB_add_u16_length_prefixed(out, &contents) ||
934 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
935 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
936 ssl->s3->previous_client_finished_len) ||
937 !CBB_flush(out)) {
938 return 0;
939 }
940
941 return 1;
942}
943
944static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
945 CBS *contents) {
946 if (contents == NULL) {
947 /* No renegotiation extension received.
948 *
949 * Strictly speaking if we want to avoid an attack we should *always* see
950 * RI even on initial ServerHello because the client doesn't see any
951 * renegotiation during an attack. However this would mean we could not
952 * connect to any server which doesn't support RI.
953 *
954 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
955 * defined. */
956 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
957 return 1;
958 }
959
960 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400961 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700962 return 0;
963 }
964
965 const size_t expected_len = ssl->s3->previous_client_finished_len +
966 ssl->s3->previous_server_finished_len;
967
968 /* Check for logic errors */
969 assert(!expected_len || ssl->s3->previous_client_finished_len);
970 assert(!expected_len || ssl->s3->previous_server_finished_len);
971
972 /* Parse out the extension contents. */
973 CBS renegotiated_connection;
974 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
975 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400976 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700977 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
978 return 0;
979 }
980
981 /* Check that the extension matches. */
982 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400983 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700984 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
985 return 0;
986 }
987
988 const uint8_t *d = CBS_data(&renegotiated_connection);
989 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
990 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400991 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700992 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
993 return 0;
994 }
995 d += ssl->s3->previous_client_finished_len;
996
997 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
998 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400999 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001000 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1001 return 0;
1002 }
1003 ssl->s3->send_connection_binding = 1;
1004
1005 return 1;
1006}
1007
1008static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1009 CBS *contents) {
1010 /* Renegotiation isn't supported as a server so this function should never be
1011 * called after the initial handshake. */
1012 assert(!ssl->s3->initial_handshake_complete);
1013
1014 CBS fake_contents;
1015 static const uint8_t kFakeExtension[] = {0};
1016
1017 if (contents == NULL) {
1018 if (ssl->s3->send_connection_binding) {
1019 /* The renegotiation SCSV was received so pretend that we received a
1020 * renegotiation extension. */
1021 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1022 contents = &fake_contents;
1023 /* We require that the renegotiation extension is at index zero of
1024 * kExtensions. */
1025 ssl->s3->tmp.extensions.received |= (1u << 0);
1026 } else {
1027 return 1;
1028 }
1029 }
1030
1031 CBS renegotiated_connection;
1032
1033 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1034 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001035 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001036 return 0;
1037 }
1038
1039 /* Check that the extension matches */
1040 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1041 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001042 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001043 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1044 return 0;
1045 }
1046
1047 ssl->s3->send_connection_binding = 1;
1048
1049 return 1;
1050}
1051
1052static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1053 CBB contents, prev_finished;
1054 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1055 !CBB_add_u16_length_prefixed(out, &contents) ||
1056 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1057 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1058 ssl->s3->previous_client_finished_len) ||
1059 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1060 ssl->s3->previous_server_finished_len) ||
1061 !CBB_flush(out)) {
1062 return 0;
1063 }
1064
1065 return 1;
1066}
1067
Adam Langley0a056712015-07-01 15:03:33 -07001068
1069/* Extended Master Secret.
1070 *
1071 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1072
1073static void ext_ems_init(SSL *ssl) {
1074 ssl->s3->tmp.extended_master_secret = 0;
1075}
1076
1077static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1078 if (ssl->version == SSL3_VERSION) {
1079 return 1;
1080 }
1081
1082 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1083 !CBB_add_u16(out, 0 /* length */)) {
1084 return 0;
1085 }
1086
1087 return 1;
1088}
1089
1090static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1091 CBS *contents) {
1092 if (contents == NULL) {
1093 return 1;
1094 }
1095
1096 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1097 return 0;
1098 }
1099
1100 ssl->s3->tmp.extended_master_secret = 1;
1101 return 1;
1102}
1103
1104static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1105 if (ssl->version == SSL3_VERSION || contents == NULL) {
1106 return 1;
1107 }
1108
1109 if (CBS_len(contents) != 0) {
1110 return 0;
1111 }
1112
1113 ssl->s3->tmp.extended_master_secret = 1;
1114 return 1;
1115}
1116
1117static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1118 if (!ssl->s3->tmp.extended_master_secret) {
1119 return 1;
1120 }
1121
1122 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1123 !CBB_add_u16(out, 0 /* length */)) {
1124 return 0;
1125 }
1126
1127 return 1;
1128}
1129
Adam Langley9b05bc52015-07-01 15:25:33 -07001130
1131/* Session tickets.
1132 *
1133 * https://tools.ietf.org/html/rfc5077 */
1134
1135static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1136 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1137 return 1;
1138 }
1139
1140 const uint8_t *ticket_data = NULL;
1141 int ticket_len = 0;
1142
1143 /* Renegotiation does not participate in session resumption. However, still
1144 * advertise the extension to avoid potentially breaking servers which carry
1145 * over the state from the previous handshake, such as OpenSSL servers
1146 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1147 if (!ssl->s3->initial_handshake_complete &&
1148 ssl->session != NULL &&
1149 ssl->session->tlsext_tick != NULL) {
1150 ticket_data = ssl->session->tlsext_tick;
1151 ticket_len = ssl->session->tlsext_ticklen;
1152 }
1153
1154 CBB ticket;
1155 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1156 !CBB_add_u16_length_prefixed(out, &ticket) ||
1157 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1158 !CBB_flush(out)) {
1159 return 0;
1160 }
1161
1162 return 1;
1163}
1164
1165static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1166 CBS *contents) {
1167 ssl->tlsext_ticket_expected = 0;
1168
1169 if (contents == NULL) {
1170 return 1;
1171 }
1172
1173 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1174 * this function should never be called, even if the server tries to send the
1175 * extension. */
1176 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1177
1178 if (CBS_len(contents) != 0) {
1179 return 0;
1180 }
1181
1182 ssl->tlsext_ticket_expected = 1;
1183 return 1;
1184}
1185
1186static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1187 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001188 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001189 return 1;
1190}
1191
1192static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1193 if (!ssl->tlsext_ticket_expected) {
1194 return 1;
1195 }
1196
1197 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1198 * true. */
1199 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1200
1201 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1202 !CBB_add_u16(out, 0 /* length */)) {
1203 return 0;
1204 }
1205
1206 return 1;
1207}
1208
1209
Adam Langley2e857bd2015-07-01 16:09:19 -07001210/* Signature Algorithms.
1211 *
1212 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1213
1214static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1215 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1216 return 1;
1217 }
1218
1219 const uint8_t *sigalgs_data;
1220 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1221
1222 CBB contents, sigalgs;
1223 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1224 !CBB_add_u16_length_prefixed(out, &contents) ||
1225 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1226 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1227 !CBB_flush(out)) {
1228 return 0;
1229 }
1230
1231 return 1;
1232}
1233
1234static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1235 CBS *contents) {
1236 if (contents != NULL) {
1237 /* Servers MUST NOT send this extension. */
1238 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001239 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001240 return 0;
1241 }
1242
1243 return 1;
1244}
1245
1246static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1247 CBS *contents) {
1248 OPENSSL_free(ssl->cert->peer_sigalgs);
1249 ssl->cert->peer_sigalgs = NULL;
1250 ssl->cert->peer_sigalgslen = 0;
1251
Adam Langley2e857bd2015-07-01 16:09:19 -07001252 if (contents == NULL) {
1253 return 1;
1254 }
1255
1256 CBS supported_signature_algorithms;
1257 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001258 CBS_len(contents) != 0 ||
1259 CBS_len(&supported_signature_algorithms) == 0 ||
1260 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001261 return 0;
1262 }
1263
1264 return 1;
1265}
1266
1267static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1268 /* Servers MUST NOT send this extension. */
1269 return 1;
1270}
1271
1272
Adam Langleybb0bd042015-07-01 16:21:03 -07001273/* OCSP Stapling.
1274 *
1275 * https://tools.ietf.org/html/rfc6066#section-8 */
1276
1277static void ext_ocsp_init(SSL *ssl) {
1278 ssl->s3->tmp.certificate_status_expected = 0;
1279}
1280
1281static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1282 if (!ssl->ocsp_stapling_enabled) {
1283 return 1;
1284 }
1285
1286 CBB contents;
1287 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1288 !CBB_add_u16_length_prefixed(out, &contents) ||
1289 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1290 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1291 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1292 !CBB_flush(out)) {
1293 return 0;
1294 }
1295
1296 return 1;
1297}
1298
1299static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001300 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001301 if (contents == NULL) {
1302 return 1;
1303 }
1304
1305 if (CBS_len(contents) != 0) {
1306 return 0;
1307 }
1308
1309 ssl->s3->tmp.certificate_status_expected = 1;
1310 return 1;
1311}
1312
1313static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1314 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001315 if (contents == NULL) {
1316 return 1;
1317 }
1318
1319 uint8_t status_type;
1320 if (!CBS_get_u8(contents, &status_type)) {
1321 return 0;
1322 }
1323
1324 /* We cannot decide whether OCSP stapling will occur yet because the correct
1325 * SSL_CTX might not have been selected. */
1326 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1327
Adam Langleybb0bd042015-07-01 16:21:03 -07001328 return 1;
1329}
1330
1331static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001332 /* The extension shouldn't be sent when resuming sessions. */
1333 if (ssl->hit ||
1334 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001335 ssl->ctx->ocsp_response_length == 0) {
1336 return 1;
1337 }
1338
1339 ssl->s3->tmp.certificate_status_expected = 1;
1340
1341 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1342 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001343}
1344
1345
Adam Langley97dfcbf2015-07-01 18:35:20 -07001346/* Next protocol negotiation.
1347 *
1348 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1349
1350static void ext_npn_init(SSL *ssl) {
1351 ssl->s3->next_proto_neg_seen = 0;
1352}
1353
1354static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1355 if (ssl->s3->initial_handshake_complete ||
1356 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001357 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001358 SSL_IS_DTLS(ssl)) {
1359 return 1;
1360 }
1361
1362 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1363 !CBB_add_u16(out, 0 /* length */)) {
1364 return 0;
1365 }
1366
1367 return 1;
1368}
1369
1370static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1371 CBS *contents) {
1372 if (contents == NULL) {
1373 return 1;
1374 }
1375
1376 /* If any of these are false then we should never have sent the NPN
1377 * extension in the ClientHello and thus this function should never have been
1378 * called. */
1379 assert(!ssl->s3->initial_handshake_complete);
1380 assert(!SSL_IS_DTLS(ssl));
1381 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001382 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001383
David Benjamin76c2efc2015-08-31 14:24:29 -04001384 if (ssl->s3->alpn_selected != NULL) {
1385 /* NPN and ALPN may not be negotiated in the same connection. */
1386 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1387 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1388 return 0;
1389 }
1390
Adam Langley97dfcbf2015-07-01 18:35:20 -07001391 const uint8_t *const orig_contents = CBS_data(contents);
1392 const size_t orig_len = CBS_len(contents);
1393
1394 while (CBS_len(contents) != 0) {
1395 CBS proto;
1396 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1397 CBS_len(&proto) == 0) {
1398 return 0;
1399 }
1400 }
1401
1402 uint8_t *selected;
1403 uint8_t selected_len;
1404 if (ssl->ctx->next_proto_select_cb(
1405 ssl, &selected, &selected_len, orig_contents, orig_len,
1406 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1407 *out_alert = SSL_AD_INTERNAL_ERROR;
1408 return 0;
1409 }
1410
1411 OPENSSL_free(ssl->next_proto_negotiated);
1412 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1413 if (ssl->next_proto_negotiated == NULL) {
1414 *out_alert = SSL_AD_INTERNAL_ERROR;
1415 return 0;
1416 }
1417
1418 ssl->next_proto_negotiated_len = selected_len;
1419 ssl->s3->next_proto_neg_seen = 1;
1420
1421 return 1;
1422}
1423
1424static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1425 CBS *contents) {
1426 if (contents != NULL && CBS_len(contents) != 0) {
1427 return 0;
1428 }
1429
1430 if (contents == NULL ||
1431 ssl->s3->initial_handshake_complete ||
1432 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1433 * afterwards, parsing the ALPN extension will clear
1434 * |next_proto_neg_seen|. */
1435 ssl->s3->alpn_selected != NULL ||
1436 ssl->ctx->next_protos_advertised_cb == NULL ||
1437 SSL_IS_DTLS(ssl)) {
1438 return 1;
1439 }
1440
1441 ssl->s3->next_proto_neg_seen = 1;
1442 return 1;
1443}
1444
1445static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1446 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1447 * parsed. */
1448 if (!ssl->s3->next_proto_neg_seen) {
1449 return 1;
1450 }
1451
1452 const uint8_t *npa;
1453 unsigned npa_len;
1454
1455 if (ssl->ctx->next_protos_advertised_cb(
1456 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1457 SSL_TLSEXT_ERR_OK) {
1458 ssl->s3->next_proto_neg_seen = 0;
1459 return 1;
1460 }
1461
1462 CBB contents;
1463 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1464 !CBB_add_u16_length_prefixed(out, &contents) ||
1465 !CBB_add_bytes(&contents, npa, npa_len) ||
1466 !CBB_flush(out)) {
1467 return 0;
1468 }
1469
1470 return 1;
1471}
1472
1473
Adam Langleyab8d87d2015-07-10 12:21:39 -07001474/* Signed certificate timestamps.
1475 *
1476 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1477
1478static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1479 if (!ssl->signed_cert_timestamps_enabled) {
1480 return 1;
1481 }
1482
1483 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1484 !CBB_add_u16(out, 0 /* length */)) {
1485 return 0;
1486 }
1487
1488 return 1;
1489}
1490
1491static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1492 CBS *contents) {
1493 if (contents == NULL) {
1494 return 1;
1495 }
1496
1497 /* If this is false then we should never have sent the SCT extension in the
1498 * ClientHello and thus this function should never have been called. */
1499 assert(ssl->signed_cert_timestamps_enabled);
1500
1501 if (CBS_len(contents) == 0) {
1502 *out_alert = SSL_AD_DECODE_ERROR;
1503 return 0;
1504 }
1505
1506 /* Session resumption uses the original session information. */
1507 if (!ssl->hit &&
1508 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1509 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1510 *out_alert = SSL_AD_INTERNAL_ERROR;
1511 return 0;
1512 }
1513
1514 return 1;
1515}
1516
1517static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1518 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001519 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001520}
1521
1522static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001523 /* The extension shouldn't be sent when resuming sessions. */
1524 if (ssl->hit ||
1525 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001526 return 1;
1527 }
1528
1529 CBB contents;
1530 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1531 CBB_add_u16_length_prefixed(out, &contents) &&
1532 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1533 ssl->ctx->signed_cert_timestamp_list_length) &&
1534 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001535}
1536
1537
Adam Langleyf18e4532015-07-10 13:39:53 -07001538/* Application-level Protocol Negotiation.
1539 *
1540 * https://tools.ietf.org/html/rfc7301 */
1541
1542static void ext_alpn_init(SSL *ssl) {
1543 OPENSSL_free(ssl->s3->alpn_selected);
1544 ssl->s3->alpn_selected = NULL;
1545}
1546
1547static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1548 if (ssl->alpn_client_proto_list == NULL ||
1549 ssl->s3->initial_handshake_complete) {
1550 return 1;
1551 }
1552
1553 CBB contents, proto_list;
1554 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1555 !CBB_add_u16_length_prefixed(out, &contents) ||
1556 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1557 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1558 ssl->alpn_client_proto_list_len) ||
1559 !CBB_flush(out)) {
1560 return 0;
1561 }
1562
1563 return 1;
1564}
1565
1566static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1567 CBS *contents) {
1568 if (contents == NULL) {
1569 return 1;
1570 }
1571
1572 assert(!ssl->s3->initial_handshake_complete);
1573 assert(ssl->alpn_client_proto_list != NULL);
1574
David Benjamin76c2efc2015-08-31 14:24:29 -04001575 if (ssl->s3->next_proto_neg_seen) {
1576 /* NPN and ALPN may not be negotiated in the same connection. */
1577 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1578 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1579 return 0;
1580 }
1581
Adam Langleyf18e4532015-07-10 13:39:53 -07001582 /* The extension data consists of a ProtocolNameList which must have
1583 * exactly one ProtocolName. Each of these is length-prefixed. */
1584 CBS protocol_name_list, protocol_name;
1585 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1586 CBS_len(contents) != 0 ||
1587 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1588 /* Empty protocol names are forbidden. */
1589 CBS_len(&protocol_name) == 0 ||
1590 CBS_len(&protocol_name_list) != 0) {
1591 return 0;
1592 }
1593
1594 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1595 &ssl->s3->alpn_selected_len)) {
1596 *out_alert = SSL_AD_INTERNAL_ERROR;
1597 return 0;
1598 }
1599
1600 return 1;
1601}
1602
1603static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1604 CBS *contents) {
1605 if (contents == NULL) {
1606 return 1;
1607 }
1608
1609 if (ssl->ctx->alpn_select_cb == NULL ||
1610 ssl->s3->initial_handshake_complete) {
1611 return 1;
1612 }
1613
1614 /* ALPN takes precedence over NPN. */
1615 ssl->s3->next_proto_neg_seen = 0;
1616
1617 CBS protocol_name_list;
1618 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1619 CBS_len(contents) != 0 ||
1620 CBS_len(&protocol_name_list) < 2) {
1621 return 0;
1622 }
1623
1624 /* Validate the protocol list. */
1625 CBS protocol_name_list_copy = protocol_name_list;
1626 while (CBS_len(&protocol_name_list_copy) > 0) {
1627 CBS protocol_name;
1628
1629 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1630 /* Empty protocol names are forbidden. */
1631 CBS_len(&protocol_name) == 0) {
1632 return 0;
1633 }
1634 }
1635
1636 const uint8_t *selected;
1637 uint8_t selected_len;
1638 if (ssl->ctx->alpn_select_cb(
1639 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1640 CBS_len(&protocol_name_list),
1641 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1642 OPENSSL_free(ssl->s3->alpn_selected);
1643 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1644 if (ssl->s3->alpn_selected == NULL) {
1645 *out_alert = SSL_AD_INTERNAL_ERROR;
1646 return 0;
1647 }
1648 ssl->s3->alpn_selected_len = selected_len;
1649 }
1650
1651 return 1;
1652}
1653
1654static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1655 if (ssl->s3->alpn_selected == NULL) {
1656 return 1;
1657 }
1658
1659 CBB contents, proto_list, proto;
1660 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1661 !CBB_add_u16_length_prefixed(out, &contents) ||
1662 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1663 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1664 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1665 !CBB_flush(out)) {
1666 return 0;
1667 }
1668
1669 return 1;
1670}
1671
1672
Adam Langley49c7af12015-07-10 14:33:46 -07001673/* Channel ID.
1674 *
1675 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1676
1677static void ext_channel_id_init(SSL *ssl) {
1678 ssl->s3->tlsext_channel_id_valid = 0;
1679}
1680
1681static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1682 if (!ssl->tlsext_channel_id_enabled ||
1683 SSL_IS_DTLS(ssl)) {
1684 return 1;
1685 }
1686
1687 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1688 !CBB_add_u16(out, 0 /* length */)) {
1689 return 0;
1690 }
1691
1692 return 1;
1693}
1694
1695static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1696 CBS *contents) {
1697 if (contents == NULL) {
1698 return 1;
1699 }
1700
1701 assert(!SSL_IS_DTLS(ssl));
1702 assert(ssl->tlsext_channel_id_enabled);
1703
1704 if (CBS_len(contents) != 0) {
1705 return 0;
1706 }
1707
1708 ssl->s3->tlsext_channel_id_valid = 1;
1709 return 1;
1710}
1711
1712static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1713 CBS *contents) {
1714 if (contents == NULL ||
1715 !ssl->tlsext_channel_id_enabled ||
1716 SSL_IS_DTLS(ssl)) {
1717 return 1;
1718 }
1719
1720 if (CBS_len(contents) != 0) {
1721 return 0;
1722 }
1723
1724 ssl->s3->tlsext_channel_id_valid = 1;
1725 return 1;
1726}
1727
1728static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1729 if (!ssl->s3->tlsext_channel_id_valid) {
1730 return 1;
1731 }
1732
1733 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1734 !CBB_add_u16(out, 0 /* length */)) {
1735 return 0;
1736 }
1737
1738 return 1;
1739}
1740
Adam Langley391250d2015-07-15 19:06:07 -07001741
1742/* Secure Real-time Transport Protocol (SRTP) extension.
1743 *
1744 * https://tools.ietf.org/html/rfc5764 */
1745
Adam Langley391250d2015-07-15 19:06:07 -07001746
1747static void ext_srtp_init(SSL *ssl) {
1748 ssl->srtp_profile = NULL;
1749}
1750
1751static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1752 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1753 if (profiles == NULL) {
1754 return 1;
1755 }
1756 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1757 if (num_profiles == 0) {
1758 return 1;
1759 }
1760
1761 CBB contents, profile_ids;
1762 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1763 !CBB_add_u16_length_prefixed(out, &contents) ||
1764 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1765 return 0;
1766 }
1767
1768 size_t i;
1769 for (i = 0; i < num_profiles; i++) {
1770 if (!CBB_add_u16(&profile_ids,
1771 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1772 return 0;
1773 }
1774 }
1775
1776 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1777 !CBB_flush(out)) {
1778 return 0;
1779 }
1780
1781 return 1;
1782}
1783
1784static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1785 CBS *contents) {
1786 if (contents == NULL) {
1787 return 1;
1788 }
1789
1790 /* The extension consists of a u16-prefixed profile ID list containing a
1791 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1792 *
1793 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1794 CBS profile_ids, srtp_mki;
1795 uint16_t profile_id;
1796 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1797 !CBS_get_u16(&profile_ids, &profile_id) ||
1798 CBS_len(&profile_ids) != 0 ||
1799 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1800 CBS_len(contents) != 0) {
1801 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1802 return 0;
1803 }
1804
1805 if (CBS_len(&srtp_mki) != 0) {
1806 /* Must be no MKI, since we never offer one. */
1807 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1808 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1809 return 0;
1810 }
1811
1812 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1813
1814 /* Check to see if the server gave us something we support (and presumably
1815 * offered). */
1816 size_t i;
1817 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1818 const SRTP_PROTECTION_PROFILE *profile =
1819 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1820
1821 if (profile->id == profile_id) {
1822 ssl->srtp_profile = profile;
1823 return 1;
1824 }
1825 }
1826
1827 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1828 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1829 return 0;
1830}
1831
1832static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1833 CBS *contents) {
1834 if (contents == NULL) {
1835 return 1;
1836 }
1837
1838 CBS profile_ids, srtp_mki;
1839 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1840 CBS_len(&profile_ids) < 2 ||
1841 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1842 CBS_len(contents) != 0) {
1843 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1844 return 0;
1845 }
1846 /* Discard the MKI value for now. */
1847
1848 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1849 SSL_get_srtp_profiles(ssl);
1850
1851 /* Pick the server's most preferred profile. */
1852 size_t i;
1853 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1854 const SRTP_PROTECTION_PROFILE *server_profile =
1855 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1856
1857 CBS profile_ids_tmp;
1858 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1859
1860 while (CBS_len(&profile_ids_tmp) > 0) {
1861 uint16_t profile_id;
1862 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1863 return 0;
1864 }
1865
1866 if (server_profile->id == profile_id) {
1867 ssl->srtp_profile = server_profile;
1868 return 1;
1869 }
1870 }
1871 }
1872
1873 return 1;
1874}
1875
1876static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1877 if (ssl->srtp_profile == NULL) {
1878 return 1;
1879 }
1880
1881 CBB contents, profile_ids;
1882 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1883 !CBB_add_u16_length_prefixed(out, &contents) ||
1884 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1885 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1886 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1887 !CBB_flush(out)) {
1888 return 0;
1889 }
1890
1891 return 1;
1892}
1893
Adam Langleybdd5d662015-07-20 16:19:08 -07001894
1895/* EC point formats.
1896 *
1897 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1898
1899static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1900 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1901 return 0;
1902 }
1903
1904 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1905
1906 size_t i;
1907 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1908 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1909
1910 const uint32_t alg_k = cipher->algorithm_mkey;
1911 const uint32_t alg_a = cipher->algorithm_auth;
1912 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1913 return 1;
1914 }
1915 }
1916
1917 return 0;
1918}
1919
Adam Langleybdd5d662015-07-20 16:19:08 -07001920static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001921 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001922 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1923 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001924 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1925 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001926 !CBB_flush(out)) {
1927 return 0;
1928 }
1929
1930 return 1;
1931}
1932
1933static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1934 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1935 return 1;
1936 }
1937
1938 return ext_ec_point_add_extension(ssl, out);
1939}
1940
1941static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1942 CBS *contents) {
1943 if (contents == NULL) {
1944 return 1;
1945 }
1946
1947 CBS ec_point_format_list;
1948 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1949 CBS_len(contents) != 0) {
1950 return 0;
1951 }
1952
David Benjaminfc059942015-07-30 23:01:59 -04001953 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1954 * point format. */
1955 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1956 CBS_len(&ec_point_format_list)) == NULL) {
1957 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001958 return 0;
1959 }
1960
1961 return 1;
1962}
1963
1964static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1965 CBS *contents) {
1966 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1967}
1968
1969static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1970 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1971 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001972 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001973
1974 if (!using_ecc) {
1975 return 1;
1976 }
1977
1978 return ext_ec_point_add_extension(ssl, out);
1979}
1980
Adam Langley273d49c2015-07-20 16:38:52 -07001981
1982/* EC supported curves.
1983 *
1984 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1985
1986static void ext_ec_curves_init(SSL *ssl) {
1987 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1988 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1989 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1990}
1991
1992static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1993 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1994 return 1;
1995 }
1996
1997 CBB contents, curves_bytes;
1998 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1999 !CBB_add_u16_length_prefixed(out, &contents) ||
2000 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2001 return 0;
2002 }
2003
2004 const uint16_t *curves;
2005 size_t curves_len;
2006 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2007
2008 size_t i;
2009 for (i = 0; i < curves_len; i++) {
2010 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2011 return 0;
2012 }
2013 }
2014
2015 return CBB_flush(out);
2016}
2017
2018static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2019 CBS *contents) {
2020 /* This extension is not expected to be echoed by servers and is ignored. */
2021 return 1;
2022}
2023
2024static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2025 CBS *contents) {
2026 if (contents == NULL) {
2027 return 1;
2028 }
2029
2030 CBS elliptic_curve_list;
2031 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2032 CBS_len(&elliptic_curve_list) == 0 ||
2033 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2034 CBS_len(contents) != 0) {
2035 return 0;
2036 }
2037
2038 ssl->s3->tmp.peer_ellipticcurvelist =
2039 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2040
2041 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2042 *out_alert = SSL_AD_INTERNAL_ERROR;
2043 return 0;
2044 }
2045
2046 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2047 size_t i;
2048 for (i = 0; i < num_curves; i++) {
2049 if (!CBS_get_u16(&elliptic_curve_list,
2050 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2051 goto err;
2052 }
2053 }
2054
2055 assert(CBS_len(&elliptic_curve_list) == 0);
2056 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2057
2058 return 1;
2059
2060err:
2061 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2062 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2063 *out_alert = SSL_AD_INTERNAL_ERROR;
2064 return 0;
2065}
2066
2067static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2068 /* Servers don't echo this extension. */
2069 return 1;
2070}
2071
2072
Adam Langley614c66a2015-06-12 15:26:58 -07002073/* kExtensions contains all the supported extensions. */
2074static const struct tls_extension kExtensions[] = {
2075 {
Adam Langley5021b222015-06-12 18:27:58 -07002076 /* The renegotiation extension must always be at index zero because the
2077 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2078 * sent as an SCSV. */
2079 TLSEXT_TYPE_renegotiate,
2080 NULL,
2081 ext_ri_add_clienthello,
2082 ext_ri_parse_serverhello,
2083 ext_ri_parse_clienthello,
2084 ext_ri_add_serverhello,
2085 },
2086 {
Adam Langley614c66a2015-06-12 15:26:58 -07002087 TLSEXT_TYPE_server_name,
2088 ext_sni_init,
2089 ext_sni_add_clienthello,
2090 ext_sni_parse_serverhello,
2091 ext_sni_parse_clienthello,
2092 ext_sni_add_serverhello,
2093 },
Adam Langley0a056712015-07-01 15:03:33 -07002094 {
2095 TLSEXT_TYPE_extended_master_secret,
2096 ext_ems_init,
2097 ext_ems_add_clienthello,
2098 ext_ems_parse_serverhello,
2099 ext_ems_parse_clienthello,
2100 ext_ems_add_serverhello,
2101 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002102 {
2103 TLSEXT_TYPE_session_ticket,
2104 NULL,
2105 ext_ticket_add_clienthello,
2106 ext_ticket_parse_serverhello,
2107 ext_ticket_parse_clienthello,
2108 ext_ticket_add_serverhello,
2109 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002110 {
2111 TLSEXT_TYPE_signature_algorithms,
2112 NULL,
2113 ext_sigalgs_add_clienthello,
2114 ext_sigalgs_parse_serverhello,
2115 ext_sigalgs_parse_clienthello,
2116 ext_sigalgs_add_serverhello,
2117 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002118 {
2119 TLSEXT_TYPE_status_request,
2120 ext_ocsp_init,
2121 ext_ocsp_add_clienthello,
2122 ext_ocsp_parse_serverhello,
2123 ext_ocsp_parse_clienthello,
2124 ext_ocsp_add_serverhello,
2125 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002126 {
2127 TLSEXT_TYPE_next_proto_neg,
2128 ext_npn_init,
2129 ext_npn_add_clienthello,
2130 ext_npn_parse_serverhello,
2131 ext_npn_parse_clienthello,
2132 ext_npn_add_serverhello,
2133 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002134 {
2135 TLSEXT_TYPE_certificate_timestamp,
2136 NULL,
2137 ext_sct_add_clienthello,
2138 ext_sct_parse_serverhello,
2139 ext_sct_parse_clienthello,
2140 ext_sct_add_serverhello,
2141 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002142 {
2143 TLSEXT_TYPE_application_layer_protocol_negotiation,
2144 ext_alpn_init,
2145 ext_alpn_add_clienthello,
2146 ext_alpn_parse_serverhello,
2147 ext_alpn_parse_clienthello,
2148 ext_alpn_add_serverhello,
2149 },
Adam Langley49c7af12015-07-10 14:33:46 -07002150 {
2151 TLSEXT_TYPE_channel_id,
2152 ext_channel_id_init,
2153 ext_channel_id_add_clienthello,
2154 ext_channel_id_parse_serverhello,
2155 ext_channel_id_parse_clienthello,
2156 ext_channel_id_add_serverhello,
2157 },
Adam Langley391250d2015-07-15 19:06:07 -07002158 {
2159 TLSEXT_TYPE_srtp,
2160 ext_srtp_init,
2161 ext_srtp_add_clienthello,
2162 ext_srtp_parse_serverhello,
2163 ext_srtp_parse_clienthello,
2164 ext_srtp_add_serverhello,
2165 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002166 {
2167 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002168 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002169 ext_ec_point_add_clienthello,
2170 ext_ec_point_parse_serverhello,
2171 ext_ec_point_parse_clienthello,
2172 ext_ec_point_add_serverhello,
2173 },
Adam Langley273d49c2015-07-20 16:38:52 -07002174 {
2175 TLSEXT_TYPE_elliptic_curves,
2176 ext_ec_curves_init,
2177 ext_ec_curves_add_clienthello,
2178 ext_ec_curves_parse_serverhello,
2179 ext_ec_curves_parse_clienthello,
2180 ext_ec_curves_add_serverhello,
2181 },
Adam Langley614c66a2015-06-12 15:26:58 -07002182};
2183
2184#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2185
Adam Langley4cfa96b2015-07-01 11:56:55 -07002186OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2187 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002188 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002189OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2190 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2191 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002192 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002193
Adam Langley614c66a2015-06-12 15:26:58 -07002194static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2195 uint16_t value) {
2196 unsigned i;
2197 for (i = 0; i < kNumExtensions; i++) {
2198 if (kExtensions[i].value == value) {
2199 *out_index = i;
2200 return &kExtensions[i];
2201 }
2202 }
2203
2204 return NULL;
2205}
2206
Adam Langley09505632015-07-30 18:10:13 -07002207int SSL_extension_supported(unsigned extension_value) {
2208 uint32_t index;
2209 return extension_value == TLSEXT_TYPE_padding ||
2210 tls_extension_find(&index, extension_value) != NULL;
2211}
2212
David Benjamine8d53502015-10-10 14:13:23 -04002213int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002215 if (ssl->client_version == SSL3_VERSION &&
2216 !ssl->s3->send_connection_binding) {
2217 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 }
Adam Langley95c29f32014-06-20 12:00:00 -07002219
David Benjamine8d53502015-10-10 14:13:23 -04002220 size_t orig_len = CBB_len(out);
2221 CBB extensions;
2222 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002223 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 }
Adam Langley95c29f32014-06-20 12:00:00 -07002225
David Benjamine8d53502015-10-10 14:13:23 -04002226 ssl->s3->tmp.extensions.sent = 0;
2227 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langley614c66a2015-06-12 15:26:58 -07002229 size_t i;
2230 for (i = 0; i < kNumExtensions; i++) {
2231 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002232 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002233 }
2234 }
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langley614c66a2015-06-12 15:26:58 -07002236 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002237 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002238 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002239 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2240 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2241 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langley33ad2b52015-07-20 17:43:53 -07002244 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002245 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002246 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 }
Adam Langley75712922014-10-10 16:23:43 -07002248
David Benjamine8d53502015-10-10 14:13:23 -04002249 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002250 goto err;
2251 }
2252
David Benjamine8d53502015-10-10 14:13:23 -04002253 if (!SSL_IS_DTLS(ssl)) {
2254 header_len += CBB_len(&extensions) - orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002255 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002256 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 *
2258 * NB: because this code works out the length of all existing extensions
2259 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002260 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 /* Extensions take at least four bytes to encode. Always include least
2262 * one byte of data if including the extension. WebSphere Application
2263 * Server 7.0 is intolerant to the last extension being zero-length. */
2264 if (padding_len >= 4 + 1) {
2265 padding_len -= 4;
2266 } else {
2267 padding_len = 1;
2268 }
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langley33ad2b52015-07-20 17:43:53 -07002270 uint8_t *padding_bytes;
2271 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2272 !CBB_add_u16(&extensions, padding_len) ||
2273 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2274 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 }
Adam Langley75712922014-10-10 16:23:43 -07002276
Adam Langley33ad2b52015-07-20 17:43:53 -07002277 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 }
2279 }
Adam Langley75712922014-10-10 16:23:43 -07002280
Adam Langley33ad2b52015-07-20 17:43:53 -07002281 /* If only two bytes have been written then the extensions are actually empty
2282 * and those two bytes are the zero length. In that case, we don't bother
2283 * sending the extensions length. */
David Benjamine8d53502015-10-10 14:13:23 -04002284 if (CBB_len(&extensions) - orig_len == 2) {
2285 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002286 }
2287
David Benjamine8d53502015-10-10 14:13:23 -04002288 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002289
2290err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002291 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002292 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293}
Adam Langley95c29f32014-06-20 12:00:00 -07002294
David Benjamin56380462015-10-10 14:59:09 -04002295int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
2296 const size_t orig_len = CBB_len(out);
2297
2298 CBB extensions;
2299 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002300 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002301 }
2302
2303 unsigned i;
2304 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002305 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002306 /* Don't send extensions that were not received. */
2307 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002308 }
Adam Langley95c29f32014-06-20 12:00:00 -07002309
David Benjamin56380462015-10-10 14:59:09 -04002310 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002311 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2312 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2313 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002314 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 }
Adam Langley95c29f32014-06-20 12:00:00 -07002316
David Benjamin56380462015-10-10 14:59:09 -04002317 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002318 goto err;
2319 }
2320
Adam Langley33ad2b52015-07-20 17:43:53 -07002321 /* If only two bytes have been written then the extensions are actually empty
2322 * and those two bytes are the zero length. In that case, we don't bother
2323 * sending the extensions length. */
David Benjamin56380462015-10-10 14:59:09 -04002324 if (CBB_len(&extensions) - orig_len == 2) {
2325 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002326 }
2327
David Benjamin56380462015-10-10 14:59:09 -04002328 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002329
2330err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002331 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002332 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002336 size_t i;
2337 for (i = 0; i < kNumExtensions; i++) {
2338 if (kExtensions[i].init != NULL) {
2339 kExtensions[i].init(s);
2340 }
2341 }
2342
2343 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002344 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002345 /* The renegotiation extension must always be at index zero because the
2346 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2347 * sent as an SCSV. */
2348 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002349
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002351 if (CBS_len(cbs) != 0) {
2352 /* Decode the extensions block and check it is valid. */
2353 CBS extensions;
2354 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2355 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002356 *out_alert = SSL_AD_DECODE_ERROR;
2357 return 0;
2358 }
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langley33ad2b52015-07-20 17:43:53 -07002360 while (CBS_len(&extensions) != 0) {
2361 uint16_t type;
2362 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002363
Adam Langley33ad2b52015-07-20 17:43:53 -07002364 /* Decode the next extension. */
2365 if (!CBS_get_u16(&extensions, &type) ||
2366 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2367 *out_alert = SSL_AD_DECODE_ERROR;
2368 return 0;
2369 }
2370
David Benjaminc7ce9772015-10-09 19:32:41 -04002371 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2372 * ambiguous. Ignore all but the renegotiation_info extension. */
2373 if (s->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2374 continue;
2375 }
2376
Adam Langley33ad2b52015-07-20 17:43:53 -07002377 unsigned ext_index;
2378 const struct tls_extension *const ext =
2379 tls_extension_find(&ext_index, type);
2380
2381 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002382 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2383 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2384 return 0;
2385 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002386 continue;
2387 }
2388
Adam Langley614c66a2015-06-12 15:26:58 -07002389 s->s3->tmp.extensions.received |= (1u << ext_index);
2390 uint8_t alert = SSL_AD_DECODE_ERROR;
2391 if (!ext->parse_clienthello(s, &alert, &extension)) {
2392 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002393 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2394 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 return 0;
2396 }
Adam Langley614c66a2015-06-12 15:26:58 -07002397 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 }
Adam Langley75712922014-10-10 16:23:43 -07002399
Adam Langley614c66a2015-06-12 15:26:58 -07002400 for (i = 0; i < kNumExtensions; i++) {
2401 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2402 /* Extension wasn't observed so call the callback with a NULL
2403 * parameter. */
2404 uint8_t alert = SSL_AD_DECODE_ERROR;
2405 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002406 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2407 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002408 *out_alert = alert;
2409 return 0;
2410 }
2411 }
2412 }
2413
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 return 1;
2415}
Adam Langley95c29f32014-06-20 12:00:00 -07002416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2418 int alert = -1;
2419 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2420 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2421 return 0;
2422 }
Adam Langley95c29f32014-06-20 12:00:00 -07002423
Adam Langleyfcf25832014-12-18 17:42:32 -08002424 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002425 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002426 return 0;
2427 }
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 return 1;
2430}
Adam Langley95c29f32014-06-20 12:00:00 -07002431
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002432OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2433
Adam Langleyfcf25832014-12-18 17:42:32 -08002434static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002435 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002436
Adam Langley33ad2b52015-07-20 17:43:53 -07002437 if (CBS_len(cbs) != 0) {
2438 /* Decode the extensions block and check it is valid. */
2439 CBS extensions;
2440 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2441 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 *out_alert = SSL_AD_DECODE_ERROR;
2443 return 0;
2444 }
Adam Langley95c29f32014-06-20 12:00:00 -07002445
Adam Langley614c66a2015-06-12 15:26:58 -07002446
Adam Langley33ad2b52015-07-20 17:43:53 -07002447 while (CBS_len(&extensions) != 0) {
2448 uint16_t type;
2449 CBS extension;
2450
2451 /* Decode the next extension. */
2452 if (!CBS_get_u16(&extensions, &type) ||
2453 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2454 *out_alert = SSL_AD_DECODE_ERROR;
2455 return 0;
2456 }
2457
2458 unsigned ext_index;
2459 const struct tls_extension *const ext =
2460 tls_extension_find(&ext_index, type);
2461
Adam Langley09505632015-07-30 18:10:13 -07002462 if (ext == NULL) {
2463 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2464 return 0;
2465 }
2466 continue;
2467 }
2468
2469 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2470 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002471 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002472 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002473 *out_alert = SSL_AD_DECODE_ERROR;
2474 return 0;
2475 }
David Benjamin03973092014-06-24 23:27:17 -04002476
Adam Langley614c66a2015-06-12 15:26:58 -07002477 received |= (1u << ext_index);
2478
2479 uint8_t alert = SSL_AD_DECODE_ERROR;
2480 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002481 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2482 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002483 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 return 0;
2485 }
Adam Langley614c66a2015-06-12 15:26:58 -07002486 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 }
Adam Langley95c29f32014-06-20 12:00:00 -07002488
Adam Langley33ad2b52015-07-20 17:43:53 -07002489 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002490 for (i = 0; i < kNumExtensions; i++) {
2491 if (!(received & (1u << i))) {
2492 /* Extension wasn't observed so call the callback with a NULL
2493 * parameter. */
2494 uint8_t alert = SSL_AD_DECODE_ERROR;
2495 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002496 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2497 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002498 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002499 return 0;
2500 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 }
2502 }
Adam Langley95c29f32014-06-20 12:00:00 -07002503
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 return 1;
2505}
Adam Langley95c29f32014-06-20 12:00:00 -07002506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507static int ssl_check_clienthello_tlsext(SSL *s) {
2508 int ret = SSL_TLSEXT_ERR_NOACK;
2509 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002510
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2512 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2515 ret = s->ctx->tlsext_servername_callback(s, &al,
2516 s->ctx->tlsext_servername_arg);
2517 } else if (s->initial_ctx != NULL &&
2518 s->initial_ctx->tlsext_servername_callback != 0) {
2519 ret = s->initial_ctx->tlsext_servername_callback(
2520 s, &al, s->initial_ctx->tlsext_servername_arg);
2521 }
Adam Langley95c29f32014-06-20 12:00:00 -07002522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523 switch (ret) {
2524 case SSL_TLSEXT_ERR_ALERT_FATAL:
2525 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2526 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 case SSL_TLSEXT_ERR_ALERT_WARNING:
2529 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2530 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002533 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 default:
2537 return 1;
2538 }
2539}
Adam Langleyed8270a2014-09-02 13:52:56 -07002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002542 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2546 ret = s->ctx->tlsext_servername_callback(s, &al,
2547 s->ctx->tlsext_servername_arg);
2548 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002549 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 ret = s->initial_ctx->tlsext_servername_callback(
2551 s, &al, s->initial_ctx->tlsext_servername_arg);
2552 }
Adam Langley95c29f32014-06-20 12:00:00 -07002553
Adam Langleyfcf25832014-12-18 17:42:32 -08002554 switch (ret) {
2555 case SSL_TLSEXT_ERR_ALERT_FATAL:
2556 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2557 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559 case SSL_TLSEXT_ERR_ALERT_WARNING:
2560 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2561 return 1;
2562
2563 default:
2564 return 1;
2565 }
2566}
2567
2568int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2569 int alert = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2571 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2572 return 0;
2573 }
2574
2575 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002576 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 return 0;
2578 }
2579
2580 return 1;
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
David Benjamine3aa1d92015-06-16 15:34:50 -04002583int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2584 int *out_send_ticket, const uint8_t *ticket,
2585 size_t ticket_len, const uint8_t *session_id,
2586 size_t session_id_len) {
2587 int ret = 1; /* Most errors are non-fatal. */
2588 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2589 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002590
David Benjamine3aa1d92015-06-16 15:34:50 -04002591 HMAC_CTX hmac_ctx;
2592 HMAC_CTX_init(&hmac_ctx);
2593 EVP_CIPHER_CTX cipher_ctx;
2594 EVP_CIPHER_CTX_init(&cipher_ctx);
2595
2596 *out_send_ticket = 0;
2597 *out_session = NULL;
2598
2599 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2600 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 }
2602
David Benjamine3aa1d92015-06-16 15:34:50 -04002603 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002604 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002605 *out_send_ticket = 1;
2606 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 }
2608
David Benjaminadcc3952015-04-26 13:07:57 -04002609 /* Ensure there is room for the key name and the largest IV
2610 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2611 * the maximum IV length should be well under the minimum size for the
2612 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002613 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2614 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002616 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002617
David Benjamine3aa1d92015-06-16 15:34:50 -04002618 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2619 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2620 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2621 0 /* decrypt */);
2622 if (cb_ret < 0) {
2623 ret = 0;
2624 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002626 if (cb_ret == 0) {
2627 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002628 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002629 if (cb_ret == 2) {
2630 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 }
2632 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002633 /* Check the key name matches. */
2634 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2635 SSL_TICKET_KEY_NAME_LEN) != 0) {
2636 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002638 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2639 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002640 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002641 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2642 ssl_ctx->tlsext_tick_aes_key, iv)) {
2643 ret = 0;
2644 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
2646 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002647 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002648
David Benjamine3aa1d92015-06-16 15:34:50 -04002649 /* Check the MAC at the end of the ticket. */
2650 uint8_t mac[EVP_MAX_MD_SIZE];
2651 size_t mac_len = HMAC_size(&hmac_ctx);
2652 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002653 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002654 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002656 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2657 HMAC_Final(&hmac_ctx, mac, NULL);
2658 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2659 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 }
2661
David Benjamine3aa1d92015-06-16 15:34:50 -04002662 /* Decrypt the session data. */
2663 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2664 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2665 mac_len;
2666 plaintext = OPENSSL_malloc(ciphertext_len);
2667 if (plaintext == NULL) {
2668 ret = 0;
2669 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002670 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002671 if (ciphertext_len >= INT_MAX) {
2672 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002674 int len1, len2;
2675 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2676 (int)ciphertext_len) ||
2677 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2678 ERR_clear_error(); /* Don't leave an error on the queue. */
2679 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 }
2681
David Benjamine3aa1d92015-06-16 15:34:50 -04002682 /* Decode the session. */
2683 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2684 if (session == NULL) {
2685 ERR_clear_error(); /* Don't leave an error on the queue. */
2686 goto done;
2687 }
2688
2689 /* Copy the client's session ID into the new session, to denote the ticket has
2690 * been accepted. */
2691 memcpy(session->session_id, session_id, session_id_len);
2692 session->session_id_length = session_id_len;
2693
2694 *out_session = session;
2695
2696done:
2697 OPENSSL_free(plaintext);
2698 HMAC_CTX_cleanup(&hmac_ctx);
2699 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2700 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002701}
Adam Langley95c29f32014-06-20 12:00:00 -07002702
2703/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002704typedef struct {
2705 int nid;
2706 int id;
2707} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002708
Adam Langleyfcf25832014-12-18 17:42:32 -08002709static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2710 {NID_sha1, TLSEXT_hash_sha1},
2711 {NID_sha224, TLSEXT_hash_sha224},
2712 {NID_sha256, TLSEXT_hash_sha256},
2713 {NID_sha384, TLSEXT_hash_sha384},
2714 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002715
Adam Langleyfcf25832014-12-18 17:42:32 -08002716static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2717 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002718
Adam Langleyfcf25832014-12-18 17:42:32 -08002719static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2720 size_t i;
2721 for (i = 0; i < tlen; i++) {
2722 if (table[i].nid == nid) {
2723 return table[i].id;
2724 }
2725 }
Adam Langley95c29f32014-06-20 12:00:00 -07002726
Adam Langleyfcf25832014-12-18 17:42:32 -08002727 return -1;
2728}
Adam Langley95c29f32014-06-20 12:00:00 -07002729
David Benjaminb4d65fd2015-05-29 17:11:21 -04002730int tls12_get_sigid(int pkey_type) {
2731 return tls12_find_id(pkey_type, tls12_sig,
2732 sizeof(tls12_sig) / sizeof(tls12_lookup));
2733}
2734
David Benjamind1d80782015-07-05 11:54:09 -04002735int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002737
Adam Langleyfcf25832014-12-18 17:42:32 -08002738 if (!md) {
2739 return 0;
2740 }
Adam Langley95c29f32014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2743 sizeof(tls12_md) / sizeof(tls12_lookup));
2744 if (md_id == -1) {
2745 return 0;
2746 }
Adam Langley95c29f32014-06-20 12:00:00 -07002747
David Benjamind1d80782015-07-05 11:54:09 -04002748 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002749 if (sig_id == -1) {
2750 return 0;
2751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 p[0] = (uint8_t)md_id;
2754 p[1] = (uint8_t)sig_id;
2755 return 1;
2756}
2757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2759 switch (hash_alg) {
2760 case TLSEXT_hash_md5:
2761 return EVP_md5();
2762
2763 case TLSEXT_hash_sha1:
2764 return EVP_sha1();
2765
2766 case TLSEXT_hash_sha224:
2767 return EVP_sha224();
2768
2769 case TLSEXT_hash_sha256:
2770 return EVP_sha256();
2771
2772 case TLSEXT_hash_sha384:
2773 return EVP_sha384();
2774
2775 case TLSEXT_hash_sha512:
2776 return EVP_sha512();
2777
2778 default:
2779 return NULL;
2780 }
2781}
Adam Langley95c29f32014-06-20 12:00:00 -07002782
David Benjaminec2f27d2014-11-13 19:17:25 -05002783/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2784 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002785static int tls12_get_pkey_type(uint8_t sig_alg) {
2786 switch (sig_alg) {
2787 case TLSEXT_signature_rsa:
2788 return EVP_PKEY_RSA;
2789
2790 case TLSEXT_signature_ecdsa:
2791 return EVP_PKEY_EC;
2792
2793 default:
2794 return -1;
2795 }
2796}
Adam Langley95c29f32014-06-20 12:00:00 -07002797
Steven Valdez0d62f262015-09-04 12:41:04 -04002798OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2799 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002800
Steven Valdez0d62f262015-09-04 12:41:04 -04002801int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002803 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 return 1;
2805 }
David Benjamincd996942014-07-20 16:23:51 -04002806
Steven Valdez0d62f262015-09-04 12:41:04 -04002807 CERT *const cert = ssl->cert;
2808 OPENSSL_free(cert->peer_sigalgs);
2809 cert->peer_sigalgs = NULL;
2810 cert->peer_sigalgslen = 0;
2811
2812 size_t num_sigalgs = CBS_len(in_sigalgs);
2813
2814 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 return 0;
2816 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002817 num_sigalgs /= 2;
2818
2819 /* supported_signature_algorithms in the certificate request is
2820 * allowed to be empty. */
2821 if (num_sigalgs == 0) {
2822 return 1;
2823 }
2824
2825 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2826 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2827 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2828 if (cert->peer_sigalgs == NULL) {
2829 return 0;
2830 }
2831 cert->peer_sigalgslen = num_sigalgs;
2832
2833 CBS sigalgs;
2834 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2835
2836 size_t i;
2837 for (i = 0; i < num_sigalgs; i++) {
2838 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2839 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2840 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2841 return 0;
2842 }
2843 }
Adam Langley95c29f32014-06-20 12:00:00 -07002844
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 return 1;
2846}
David Benjaminec2f27d2014-11-13 19:17:25 -05002847
David Benjamind1d80782015-07-05 11:54:09 -04002848const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2849 CERT *cert = ssl->cert;
2850 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002851 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002852
Steven Valdez0d62f262015-09-04 12:41:04 -04002853 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2854 NID_sha224, NID_sha1};
2855
2856 const int *digest_nids = kDefaultDigestList;
2857 size_t num_digest_nids =
2858 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2859 if (cert->digest_nids != NULL) {
2860 digest_nids = cert->digest_nids;
2861 num_digest_nids = cert->num_digest_nids;
2862 }
2863
2864 for (i = 0; i < num_digest_nids; i++) {
2865 const int digest_nid = digest_nids[i];
2866 for (j = 0; j < cert->peer_sigalgslen; j++) {
2867 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2868 if (md == NULL ||
2869 digest_nid != EVP_MD_type(md) ||
2870 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2871 continue;
2872 }
2873
2874 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002876 }
Adam Langley95c29f32014-06-20 12:00:00 -07002877
Adam Langleyfcf25832014-12-18 17:42:32 -08002878 /* If no suitable digest may be found, default to SHA-1. */
2879 return EVP_sha1();
2880}
Adam Langley95c29f32014-06-20 12:00:00 -07002881
David Benjamind6a4ae92015-08-06 11:10:51 -04002882int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2883 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002884 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002885
2886 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002887 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2888 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002889 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002890
David Benjamind6a4ae92015-08-06 11:10:51 -04002891 static const char kClientIDMagic[] = "TLS Channel ID signature";
2892 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2893
2894 if (ssl->hit) {
2895 static const char kResumptionMagic[] = "Resumption";
2896 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2897 if (ssl->session->original_handshake_hash_len == 0) {
2898 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2899 goto err;
2900 }
2901 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2902 ssl->session->original_handshake_hash_len);
2903 }
2904
2905 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2906 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2907 sizeof(handshake_hash));
2908 if (handshake_hash_len < 0) {
2909 goto err;
2910 }
2911 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2912 unsigned len_u;
2913 EVP_DigestFinal_ex(&ctx, out, &len_u);
2914 *out_len = len_u;
2915
2916 ret = 1;
2917
2918err:
2919 EVP_MD_CTX_cleanup(&ctx);
2920 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002921}
Adam Langley1258b6a2014-06-20 12:00:00 -07002922
2923/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2924 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002925int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2926 int digest_len;
2927 /* This function should never be called for a resumed session because the
2928 * handshake hashes that we wish to record are for the original, full
2929 * handshake. */
2930 if (s->hit) {
2931 return -1;
2932 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002933
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 digest_len =
2935 tls1_handshake_digest(s, s->session->original_handshake_hash,
2936 sizeof(s->session->original_handshake_hash));
2937 if (digest_len < 0) {
2938 return -1;
2939 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002940
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002942
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 return 1;
2944}