blob: 3902f8f0fcb14bca77555917a9956eb88b162858 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjaminf0ae1702015-04-07 23:05:04 -0400109#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400110#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400112#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114
David Benjamin03973092014-06-24 23:27:17 -0400115#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400116#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400117#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118#include <openssl/evp.h>
119#include <openssl/hmac.h>
120#include <openssl/mem.h>
121#include <openssl/obj.h>
122#include <openssl/rand.h>
123
David Benjamin2ee94aa2015-04-07 22:38:30 -0400124#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800125
126
David Benjamin6c7aed02014-08-27 16:42:38 -0400127static int ssl_check_clienthello_tlsext(SSL *s);
128static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700129
David Benjamin338fcaf2014-12-11 01:20:52 -0500130const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500131 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800132 tls1_setup_key_block,
133 tls1_generate_master_secret,
134 tls1_change_cipher_state,
135 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800136 tls1_cert_verify_mac,
137 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
138 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
139 tls1_alert_code,
140 tls1_export_keying_material,
141 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800142};
Adam Langley95c29f32014-06-20 12:00:00 -0700143
David Benjamin338fcaf2014-12-11 01:20:52 -0500144const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500145 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800146 tls1_setup_key_block,
147 tls1_generate_master_secret,
148 tls1_change_cipher_state,
149 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800150 tls1_cert_verify_mac,
151 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
152 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
153 tls1_alert_code,
154 tls1_export_keying_material,
155 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800156};
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjamin338fcaf2014-12-11 01:20:52 -0500158const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500159 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 tls1_setup_key_block,
161 tls1_generate_master_secret,
162 tls1_change_cipher_state,
163 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 tls1_cert_verify_mac,
165 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
166 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
167 tls1_alert_code,
168 tls1_export_keying_material,
169 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
170 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800171};
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173static int compare_uint16_t(const void *p1, const void *p2) {
174 uint16_t u1 = *((const uint16_t *)p1);
175 uint16_t u2 = *((const uint16_t *)p2);
176 if (u1 < u2) {
177 return -1;
178 } else if (u1 > u2) {
179 return 1;
180 } else {
181 return 0;
182 }
183}
David Benjamin35a7a442014-07-05 00:23:20 -0400184
Adam Langleyfcf25832014-12-18 17:42:32 -0800185/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
186 * more than one extension of the same type in a ClientHello or ServerHello.
187 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400188 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800189static int tls1_check_duplicate_extensions(const CBS *cbs) {
190 CBS extensions = *cbs;
191 size_t num_extensions = 0, i = 0;
192 uint16_t *extension_types = NULL;
193 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400194
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 /* First pass: count the extensions. */
196 while (CBS_len(&extensions) > 0) {
197 uint16_t type;
198 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 if (!CBS_get_u16(&extensions, &type) ||
201 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
202 goto done;
203 }
David Benjamin35a7a442014-07-05 00:23:20 -0400204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 num_extensions++;
206 }
David Benjamin35a7a442014-07-05 00:23:20 -0400207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 if (num_extensions == 0) {
209 return 1;
210 }
David Benjamin9a373592014-07-25 04:27:53 -0400211
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 extension_types =
213 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
214 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400215 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 goto done;
217 }
David Benjamin35a7a442014-07-05 00:23:20 -0400218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 /* Second pass: gather the extension types. */
220 extensions = *cbs;
221 for (i = 0; i < num_extensions; i++) {
222 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
225 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
226 /* This should not happen. */
227 goto done;
228 }
229 }
230 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400231
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 /* Sort the extensions and make sure there are no duplicates. */
233 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
234 for (i = 1; i < num_extensions; i++) {
235 if (extension_types[i - 1] == extension_types[i]) {
236 goto done;
237 }
238 }
David Benjamin35a7a442014-07-05 00:23:20 -0400239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 ret = 1;
241
David Benjamin35a7a442014-07-05 00:23:20 -0400242done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400243 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 return ret;
245}
David Benjamin35a7a442014-07-05 00:23:20 -0400246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
248 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 if (/* Skip client version. */
253 !CBS_skip(&client_hello, 2) ||
254 /* Skip client nonce. */
255 !CBS_skip(&client_hello, 32) ||
256 /* Extract session_id. */
257 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
258 return 0;
259 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 ctx->session_id = CBS_data(&session_id);
262 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 /* Skip past DTLS cookie */
265 if (SSL_IS_DTLS(ctx->ssl)) {
266 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
269 return 0;
270 }
271 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 /* Extract cipher_suites. */
274 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
275 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
276 return 0;
277 }
278 ctx->cipher_suites = CBS_data(&cipher_suites);
279 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700280
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 /* Extract compression_methods. */
282 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
283 CBS_len(&compression_methods) < 1) {
284 return 0;
285 }
286 ctx->compression_methods = CBS_data(&compression_methods);
287 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 /* If the ClientHello ends here then it's valid, but doesn't have any
290 * extensions. (E.g. SSLv3.) */
291 if (CBS_len(&client_hello) == 0) {
292 ctx->extensions = NULL;
293 ctx->extensions_len = 0;
294 return 1;
295 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700296
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 /* Extract extensions and check it is valid. */
298 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
299 !tls1_check_duplicate_extensions(&extensions) ||
300 CBS_len(&client_hello) != 0) {
301 return 0;
302 }
303 ctx->extensions = CBS_data(&extensions);
304 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700305
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 return 1;
307}
Adam Langleydc9b1412014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309char SSL_early_callback_ctx_extension_get(
310 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
311 const uint8_t **out_data, size_t *out_len) {
312 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 while (CBS_len(&extensions) != 0) {
317 uint16_t type;
318 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 /* Decode the next extension. */
321 if (!CBS_get_u16(&extensions, &type) ||
322 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
323 return 0;
324 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700325
Adam Langleyfcf25832014-12-18 17:42:32 -0800326 if (type == extension_type) {
327 *out_data = CBS_data(&extension);
328 *out_len = CBS_len(&extension);
329 return 1;
330 }
331 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 return 0;
334}
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin52e5bac2014-12-27 02:27:35 -0500336struct tls_curve {
337 uint16_t curve_id;
338 int nid;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200339 const char curve_name[8];
David Benjamin52e5bac2014-12-27 02:27:35 -0500340};
341
David Benjamin70bd80a2014-12-27 03:06:46 -0500342/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500343static const struct tls_curve tls_curves[] = {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200344 {21, NID_secp224r1, "P-224"},
345 {23, NID_X9_62_prime256v1, "P-256"},
346 {24, NID_secp384r1, "P-384"},
347 {25, NID_secp521r1, "P-521"},
Adam Langleyfcf25832014-12-18 17:42:32 -0800348};
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langleyfcf25832014-12-18 17:42:32 -0800350static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400351 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500352 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800353};
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Adam Langleyfcf25832014-12-18 17:42:32 -0800355int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500356 size_t i;
357 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
358 if (curve_id == tls_curves[i].curve_id) {
359 return tls_curves[i].nid;
360 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500362 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800363}
Adam Langley95c29f32014-06-20 12:00:00 -0700364
David Benjamin70bd80a2014-12-27 03:06:46 -0500365int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500367 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
368 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500369 *out_curve_id = tls_curves[i].curve_id;
370 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 }
372 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 0;
374}
375
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200376const char* tls1_ec_curve_id2name(uint16_t curve_id) {
377 size_t i;
378 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
379 if (curve_id == tls_curves[i].curve_id) {
380 return tls_curves[i].curve_name;
381 }
382 }
383 return NULL;
384}
385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
387 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
388 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400389static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 const uint16_t **out_curve_ids,
391 size_t *out_curve_ids_len) {
392 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400393 /* Only clients send a curve list, so this function is only called
394 * on the server. */
395 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
397 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
398 return;
399 }
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 *out_curve_ids = s->tlsext_ellipticcurvelist;
402 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
403 if (!*out_curve_ids) {
404 *out_curve_ids = eccurves_default;
405 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
406 }
407}
David Benjamined439582014-07-14 19:13:02 -0400408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
410 uint8_t curve_type;
411 uint16_t curve_id;
412 const uint16_t *curves;
413 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 /* Only support named curves. */
416 if (!CBS_get_u8(cbs, &curve_type) ||
417 curve_type != NAMED_CURVE_TYPE ||
418 !CBS_get_u16(cbs, &curve_id)) {
419 return 0;
420 }
David Benjamined439582014-07-14 19:13:02 -0400421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 tls1_get_curvelist(s, 0, &curves, &curves_len);
423 for (i = 0; i < curves_len; i++) {
424 if (curve_id == curves[i]) {
425 *out_curve_id = curve_id;
426 return 1;
427 }
428 }
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 return 0;
431}
David Benjamin072334d2014-07-13 16:24:27 -0400432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400434 const uint16_t *curves, *peer_curves, *pref, *supp;
435 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 /* Can't do anything on client side */
438 if (s->server == 0) {
439 return NID_undef;
440 }
441
David Benjamin55a43642015-04-20 14:45:55 -0400442 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
443 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800444
David Benjamin55a43642015-04-20 14:45:55 -0400445 if (peer_curves_len == 0) {
446 /* Clients are not required to send a supported_curves extension. In this
447 * case, the server is free to pick any curve it likes. See RFC 4492,
448 * section 4, paragraph 3. */
449 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
450 }
451
452 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
453 pref = curves;
454 pref_len = curves_len;
455 supp = peer_curves;
456 supp_len = peer_curves_len;
457 } else {
458 pref = peer_curves;
459 pref_len = peer_curves_len;
460 supp = curves;
461 supp_len = curves_len;
462 }
463
464 for (i = 0; i < pref_len; i++) {
465 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 if (pref[i] == supp[j]) {
467 return tls1_ec_curve_id2nid(pref[i]);
468 }
469 }
470 }
471
472 return NID_undef;
473}
Adam Langley95c29f32014-06-20 12:00:00 -0700474
David Benjamin072334d2014-07-13 16:24:27 -0400475int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 const int *curves, size_t ncurves) {
477 uint16_t *curve_ids;
478 size_t i;
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
481 if (curve_ids == NULL) {
482 return 0;
483 }
484
485 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500486 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 OPENSSL_free(curve_ids);
488 return 0;
489 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 }
491
David Benjamin2755a3e2015-04-22 16:17:58 -0400492 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 *out_curve_ids = curve_ids;
494 *out_curve_ids_len = ncurves;
495
496 return 1;
497}
Adam Langley95c29f32014-06-20 12:00:00 -0700498
David Benjamin072334d2014-07-13 16:24:27 -0400499/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
500 * TLS curve ID and point format, respectively, for |ec|. It returns one on
501 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800502static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
503 uint8_t *out_comp_id, EC_KEY *ec) {
504 int nid;
505 uint16_t id;
506 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 if (ec == NULL) {
509 return 0;
510 }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 grp = EC_KEY_get0_group(ec);
513 if (grp == NULL) {
514 return 0;
515 }
David Benjamin072334d2014-07-13 16:24:27 -0400516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 /* Determine curve ID */
518 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500519 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 return 0;
521 }
David Benjamin072334d2014-07-13 16:24:27 -0400522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
524 *out_curve_id = id;
525
526 if (out_comp_id) {
527 if (EC_KEY_get0_public_key(ec) == NULL) {
528 return 0;
529 }
530 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
531 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
532 } else {
533 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
534 }
535 }
536
537 return 1;
538}
David Benjamin072334d2014-07-13 16:24:27 -0400539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
541 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400542 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800543static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
544 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400545 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400548 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
549 if (get_peer_curves && !s->server) {
550 /* Servers do not present a preference list so, if we are a client, only
551 * check our list. */
552 continue;
553 }
554
555 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
556 if (get_peer_curves && curves_len == 0) {
557 /* Clients are not required to send a supported_curves extension. In this
558 * case, the server is free to pick any curve it likes. See RFC 4492,
559 * section 4, paragraph 3. */
560 continue;
561 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 for (i = 0; i < curves_len; i++) {
563 if (curves[i] == curve_id) {
564 break;
565 }
566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 if (i == curves_len) {
569 return 0;
570 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 }
David Benjamin033e5f42014-11-13 18:47:41 -0500572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 return 1;
574}
David Benjamin033e5f42014-11-13 18:47:41 -0500575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576int tls1_check_ec_cert(SSL *s, X509 *x) {
577 int ret = 0;
578 EVP_PKEY *pkey = X509_get_pubkey(x);
579 uint16_t curve_id;
580 uint8_t comp_id;
581
582 if (!pkey ||
583 pkey->type != EVP_PKEY_EC ||
584 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
585 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400586 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 goto done;
588 }
589
590 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500591
592done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400593 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 return ret;
595}
David Benjamin42e9a772014-09-02 23:18:44 -0400596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400598 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400599 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400600 * the curve. */
601 uint16_t curve_id;
602 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
603 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 }
605
David Benjamindd978782015-04-24 15:20:13 -0400606 if (s->cert->ecdh_tmp_cb != NULL) {
607 /* Assume the callback will provide an acceptable curve. */
608 return 1;
609 }
610
611 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
612 * there is a shared curve. */
613 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800614}
Adam Langley95c29f32014-06-20 12:00:00 -0700615
616/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langley95c29f32014-06-20 12:00:00 -0700619#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langley95c29f32014-06-20 12:00:00 -0700621#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700624
David Benjamincff64722014-08-19 19:54:46 -0400625static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 tlsext_sigalg(TLSEXT_hash_sha512)
627 tlsext_sigalg(TLSEXT_hash_sha384)
628 tlsext_sigalg(TLSEXT_hash_sha256)
629 tlsext_sigalg(TLSEXT_hash_sha224)
630 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700631};
David Benjamin05da6e12014-07-12 20:42:55 -0400632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400634 *psigs = tls12_sigalgs;
635 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800636}
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
639 * checks it is consistent with |s|'s sent supported signature algorithms and,
640 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
641 * returns 0 and writes an alert into |*out_alert|. */
642int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
643 CBS *cbs, EVP_PKEY *pkey) {
644 const uint8_t *sent_sigs;
645 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400646 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 uint8_t hash, signature;
648
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
656 if (!CBS_get_u8(cbs, &hash) ||
657 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400658 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 *out_alert = SSL_AD_DECODE_ERROR;
660 return 0;
661 }
662
663 /* Check key type is consistent with signature */
664 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400665 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
667 return 0;
668 }
669
670 if (pkey->type == EVP_PKEY_EC) {
671 uint16_t curve_id;
672 uint8_t comp_id;
673 /* Check compression and curve matches extensions */
674 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
675 *out_alert = SSL_AD_INTERNAL_ERROR;
676 return 0;
677 }
678
679 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400680 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400681 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
683 return 0;
684 }
685 }
686
687 /* Check signature matches a type we sent */
688 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
689 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
690 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
691 break;
692 }
693 }
694
695 /* Allow fallback to SHA-1. */
696 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400697 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
699 return 0;
700 }
701
702 *out_md = tls12_get_hash(hash);
703 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400704 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800705 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
706 return 0;
707 }
708
709 return 1;
710}
711
712/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
713 * supported or doesn't appear in supported signature algorithms. Unlike
714 * ssl_cipher_get_disabled this applies to a specific session and not global
715 * settings. */
716void ssl_set_client_disabled(SSL *s) {
717 CERT *c = s->cert;
718 const uint8_t *sigalgs;
719 size_t i, sigalgslen;
720 int have_rsa = 0, have_ecdsa = 0;
721 c->mask_a = 0;
722 c->mask_k = 0;
723
724 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
725 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
726 c->mask_ssl = SSL_TLSV1_2;
727 } else {
728 c->mask_ssl = 0;
729 }
730
731 /* Now go through all signature algorithms seeing if we support any for RSA,
732 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
733 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
734 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
735 switch (sigalgs[1]) {
736 case TLSEXT_signature_rsa:
737 have_rsa = 1;
738 break;
739
740 case TLSEXT_signature_ecdsa:
741 have_ecdsa = 1;
742 break;
743 }
744 }
745
746 /* Disable auth if we don't include any appropriate signature algorithms. */
747 if (!have_rsa) {
748 c->mask_a |= SSL_aRSA;
749 }
750 if (!have_ecdsa) {
751 c->mask_a |= SSL_aECDSA;
752 }
753
754 /* with PSK there must be client callback set */
755 if (!s->psk_client_callback) {
756 c->mask_a |= SSL_aPSK;
757 c->mask_k |= SSL_kPSK;
758 }
759}
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langley614c66a2015-06-12 15:26:58 -0700761/* tls_extension represents a TLS extension that is handled internally. The
762 * |init| function is called for each handshake, before any other functions of
763 * the extension. Then the add and parse callbacks are called as needed.
764 *
765 * The parse callbacks receive a |CBS| that contains the contents of the
766 * extension (i.e. not including the type and length bytes). If an extension is
767 * not received then the parse callbacks will be called with a NULL CBS so that
768 * they can do any processing needed to handle the absence of an extension.
769 *
770 * The add callbacks receive a |CBB| to which the extension can be appended but
771 * the function is responsible for appending the type and length bytes too.
772 *
773 * All callbacks return one for success and zero for error. If a parse function
774 * returns zero then a fatal alert with value |*out_alert| will be sent. If
775 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
776struct tls_extension {
777 uint16_t value;
778 void (*init)(SSL *ssl);
779
780 int (*add_clienthello)(SSL *ssl, CBB *out);
781 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
782
783 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
784 int (*add_serverhello)(SSL *ssl, CBB *out);
785};
786
787
788/* Server name indication (SNI).
789 *
790 * https://tools.ietf.org/html/rfc6066#section-3. */
791
792static void ext_sni_init(SSL *ssl) {
793 ssl->s3->tmp.should_ack_sni = 0;
794}
795
796static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
797 if (ssl->tlsext_hostname == NULL) {
798 return 1;
799 }
800
801 CBB contents, server_name_list, name;
802 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
803 !CBB_add_u16_length_prefixed(out, &contents) ||
804 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
805 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
806 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
807 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
808 strlen(ssl->tlsext_hostname)) ||
809 !CBB_flush(out)) {
810 return 0;
811 }
812
813 return 1;
814}
815
816static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
817 if (contents == NULL) {
818 return 1;
819 }
820
821 if (CBS_len(contents) != 0) {
822 return 0;
823 }
824
825 assert(ssl->tlsext_hostname != NULL);
826
827 if (!ssl->hit) {
828 assert(ssl->session->tlsext_hostname == NULL);
829 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
830 if (!ssl->session->tlsext_hostname) {
831 *out_alert = SSL_AD_INTERNAL_ERROR;
832 return 0;
833 }
834 }
835
836 return 1;
837}
838
839static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
840 if (contents == NULL) {
841 return 1;
842 }
843
844 /* The servername extension is treated as follows:
845 *
846 * - Only the hostname type is supported with a maximum length of 255.
847 * - The servername is rejected if too long or if it contains zeros, in
848 * which case an fatal alert is generated.
849 * - The servername field is maintained together with the session cache.
850 * - When a session is resumed, the servername callback is invoked in order
851 * to allow the application to position itself to the right context.
852 * - The servername is acknowledged if it is new for a session or when
853 * it is identical to a previously used for the same session.
854 * Applications can control the behaviour. They can at any time
855 * set a 'desirable' servername for a new SSL object. This can be the
856 * case for example with HTTPS when a Host: header field is received and
857 * a renegotiation is requested. In this case, a possible servername
858 * presented in the new client hello is only acknowledged if it matches
859 * the value of the Host: field.
860 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
861 * if they provide for changing an explicit servername context for the
862 * session,
863 * i.e. when the session has been established with a servername extension.
864 */
865
866 CBS server_name_list;
867 char have_seen_host_name = 0;
868
869 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
870 CBS_len(&server_name_list) == 0 ||
871 CBS_len(contents) != 0) {
872 return 0;
873 }
874
875 /* Decode each ServerName in the extension. */
876 while (CBS_len(&server_name_list) > 0) {
877 uint8_t name_type;
878 CBS host_name;
879
880 if (!CBS_get_u8(&server_name_list, &name_type) ||
881 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
882 return 0;
883 }
884
885 /* Only host_name is supported. */
886 if (name_type != TLSEXT_NAMETYPE_host_name) {
887 continue;
888 }
889
890 if (have_seen_host_name) {
891 /* The ServerNameList MUST NOT contain more than one name of the same
892 * name_type. */
893 return 0;
894 }
895
896 have_seen_host_name = 1;
897
898 if (CBS_len(&host_name) == 0 ||
899 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
900 CBS_contains_zero_byte(&host_name)) {
901 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
902 return 0;
903 }
904
905 if (!ssl->hit) {
906 assert(ssl->session->tlsext_hostname == NULL);
907 if (ssl->session->tlsext_hostname) {
908 /* This should be impossible. */
909 return 0;
910 }
911
912 /* Copy the hostname as a string. */
913 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
914 *out_alert = SSL_AD_INTERNAL_ERROR;
915 return 0;
916 }
917
918 ssl->s3->tmp.should_ack_sni = 1;
919 }
920 }
921
922 return 1;
923}
924
925static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
926 if (ssl->hit ||
927 !ssl->s3->tmp.should_ack_sni ||
928 ssl->session->tlsext_hostname == NULL) {
929 return 1;
930 }
931
932 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
933 !CBB_add_u16(out, 0 /* length */)) {
934 return 0;
935 }
936
937 return 1;
938}
939
940
Adam Langley5021b222015-06-12 18:27:58 -0700941/* Renegotiation indication.
942 *
943 * https://tools.ietf.org/html/rfc5746 */
944
945static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
946 CBB contents, prev_finished;
947 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
948 !CBB_add_u16_length_prefixed(out, &contents) ||
949 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
950 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
951 ssl->s3->previous_client_finished_len) ||
952 !CBB_flush(out)) {
953 return 0;
954 }
955
956 return 1;
957}
958
959static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
960 CBS *contents) {
961 if (contents == NULL) {
962 /* No renegotiation extension received.
963 *
964 * Strictly speaking if we want to avoid an attack we should *always* see
965 * RI even on initial ServerHello because the client doesn't see any
966 * renegotiation during an attack. However this would mean we could not
967 * connect to any server which doesn't support RI.
968 *
969 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
970 * defined. */
971 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
972 return 1;
973 }
974
975 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400976 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700977 return 0;
978 }
979
980 const size_t expected_len = ssl->s3->previous_client_finished_len +
981 ssl->s3->previous_server_finished_len;
982
983 /* Check for logic errors */
984 assert(!expected_len || ssl->s3->previous_client_finished_len);
985 assert(!expected_len || ssl->s3->previous_server_finished_len);
986
987 /* Parse out the extension contents. */
988 CBS renegotiated_connection;
989 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
990 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400991 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700992 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
993 return 0;
994 }
995
996 /* Check that the extension matches. */
997 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400998 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700999 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1000 return 0;
1001 }
1002
1003 const uint8_t *d = CBS_data(&renegotiated_connection);
1004 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1005 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001006 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001007 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1008 return 0;
1009 }
1010 d += ssl->s3->previous_client_finished_len;
1011
1012 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1013 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001014 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001015 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1016 return 0;
1017 }
1018 ssl->s3->send_connection_binding = 1;
1019
1020 return 1;
1021}
1022
1023static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1024 CBS *contents) {
1025 /* Renegotiation isn't supported as a server so this function should never be
1026 * called after the initial handshake. */
1027 assert(!ssl->s3->initial_handshake_complete);
1028
1029 CBS fake_contents;
1030 static const uint8_t kFakeExtension[] = {0};
1031
1032 if (contents == NULL) {
1033 if (ssl->s3->send_connection_binding) {
1034 /* The renegotiation SCSV was received so pretend that we received a
1035 * renegotiation extension. */
1036 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1037 contents = &fake_contents;
1038 /* We require that the renegotiation extension is at index zero of
1039 * kExtensions. */
1040 ssl->s3->tmp.extensions.received |= (1u << 0);
1041 } else {
1042 return 1;
1043 }
1044 }
1045
1046 CBS renegotiated_connection;
1047
1048 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1049 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001050 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001051 return 0;
1052 }
1053
1054 /* Check that the extension matches */
1055 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1056 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001057 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001058 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1059 return 0;
1060 }
1061
1062 ssl->s3->send_connection_binding = 1;
1063
1064 return 1;
1065}
1066
1067static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1068 CBB contents, prev_finished;
1069 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1070 !CBB_add_u16_length_prefixed(out, &contents) ||
1071 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1072 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1073 ssl->s3->previous_client_finished_len) ||
1074 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1075 ssl->s3->previous_server_finished_len) ||
1076 !CBB_flush(out)) {
1077 return 0;
1078 }
1079
1080 return 1;
1081}
1082
Adam Langley0a056712015-07-01 15:03:33 -07001083
1084/* Extended Master Secret.
1085 *
1086 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1087
1088static void ext_ems_init(SSL *ssl) {
1089 ssl->s3->tmp.extended_master_secret = 0;
1090}
1091
1092static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1093 if (ssl->version == SSL3_VERSION) {
1094 return 1;
1095 }
1096
1097 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1098 !CBB_add_u16(out, 0 /* length */)) {
1099 return 0;
1100 }
1101
1102 return 1;
1103}
1104
1105static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1106 CBS *contents) {
1107 if (contents == NULL) {
1108 return 1;
1109 }
1110
1111 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1112 return 0;
1113 }
1114
1115 ssl->s3->tmp.extended_master_secret = 1;
1116 return 1;
1117}
1118
1119static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1120 if (ssl->version == SSL3_VERSION || contents == NULL) {
1121 return 1;
1122 }
1123
1124 if (CBS_len(contents) != 0) {
1125 return 0;
1126 }
1127
1128 ssl->s3->tmp.extended_master_secret = 1;
1129 return 1;
1130}
1131
1132static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1133 if (!ssl->s3->tmp.extended_master_secret) {
1134 return 1;
1135 }
1136
1137 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1138 !CBB_add_u16(out, 0 /* length */)) {
1139 return 0;
1140 }
1141
1142 return 1;
1143}
1144
Adam Langley9b05bc52015-07-01 15:25:33 -07001145
1146/* Session tickets.
1147 *
1148 * https://tools.ietf.org/html/rfc5077 */
1149
1150static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1151 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1152 return 1;
1153 }
1154
1155 const uint8_t *ticket_data = NULL;
1156 int ticket_len = 0;
1157
1158 /* Renegotiation does not participate in session resumption. However, still
1159 * advertise the extension to avoid potentially breaking servers which carry
1160 * over the state from the previous handshake, such as OpenSSL servers
1161 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1162 if (!ssl->s3->initial_handshake_complete &&
1163 ssl->session != NULL &&
1164 ssl->session->tlsext_tick != NULL) {
1165 ticket_data = ssl->session->tlsext_tick;
1166 ticket_len = ssl->session->tlsext_ticklen;
1167 }
1168
1169 CBB ticket;
1170 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1171 !CBB_add_u16_length_prefixed(out, &ticket) ||
1172 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1173 !CBB_flush(out)) {
1174 return 0;
1175 }
1176
1177 return 1;
1178}
1179
1180static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1181 CBS *contents) {
1182 ssl->tlsext_ticket_expected = 0;
1183
1184 if (contents == NULL) {
1185 return 1;
1186 }
1187
1188 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1189 * this function should never be called, even if the server tries to send the
1190 * extension. */
1191 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1192
1193 if (CBS_len(contents) != 0) {
1194 return 0;
1195 }
1196
1197 ssl->tlsext_ticket_expected = 1;
1198 return 1;
1199}
1200
1201static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1202 /* This function isn't used because the ticket extension from the client is
1203 * handled in ssl_sess.c. */
1204 return 1;
1205}
1206
1207static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1208 if (!ssl->tlsext_ticket_expected) {
1209 return 1;
1210 }
1211
1212 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1213 * true. */
1214 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1215
1216 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1217 !CBB_add_u16(out, 0 /* length */)) {
1218 return 0;
1219 }
1220
1221 return 1;
1222}
1223
1224
Adam Langley2e857bd2015-07-01 16:09:19 -07001225/* Signature Algorithms.
1226 *
1227 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1228
1229static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1230 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1231 return 1;
1232 }
1233
1234 const uint8_t *sigalgs_data;
1235 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1236
1237 CBB contents, sigalgs;
1238 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1239 !CBB_add_u16_length_prefixed(out, &contents) ||
1240 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1241 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1242 !CBB_flush(out)) {
1243 return 0;
1244 }
1245
1246 return 1;
1247}
1248
1249static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1250 CBS *contents) {
1251 if (contents != NULL) {
1252 /* Servers MUST NOT send this extension. */
1253 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001254 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001255 return 0;
1256 }
1257
1258 return 1;
1259}
1260
1261static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1262 CBS *contents) {
1263 OPENSSL_free(ssl->cert->peer_sigalgs);
1264 ssl->cert->peer_sigalgs = NULL;
1265 ssl->cert->peer_sigalgslen = 0;
1266
1267 OPENSSL_free(ssl->cert->shared_sigalgs);
1268 ssl->cert->shared_sigalgs = NULL;
1269 ssl->cert->shared_sigalgslen = 0;
1270
1271 if (contents == NULL) {
1272 return 1;
1273 }
1274
1275 CBS supported_signature_algorithms;
1276 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1277 CBS_len(contents) != 0) {
1278 return 0;
1279 }
1280
1281 /* Ensure the signature algorithms are non-empty. It contains a list of
1282 * SignatureAndHashAlgorithms which are two bytes each. */
1283 if (CBS_len(&supported_signature_algorithms) == 0 ||
1284 (CBS_len(&supported_signature_algorithms) % 2) != 0 ||
1285 !tls1_process_sigalgs(ssl, &supported_signature_algorithms)) {
1286 return 0;
1287 }
1288
1289 /* It's a fatal error if the signature_algorithms extension is received and
1290 * there are no shared algorithms. */
1291 if (ssl->cert->peer_sigalgs && !ssl->cert->shared_sigalgs) {
David Benjamin3570d732015-06-29 00:28:17 -04001292 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
Adam Langley2e857bd2015-07-01 16:09:19 -07001293 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1294 return 0;
1295 }
1296
1297 return 1;
1298}
1299
1300static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1301 /* Servers MUST NOT send this extension. */
1302 return 1;
1303}
1304
1305
Adam Langleybb0bd042015-07-01 16:21:03 -07001306/* OCSP Stapling.
1307 *
1308 * https://tools.ietf.org/html/rfc6066#section-8 */
1309
1310static void ext_ocsp_init(SSL *ssl) {
1311 ssl->s3->tmp.certificate_status_expected = 0;
1312}
1313
1314static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1315 if (!ssl->ocsp_stapling_enabled) {
1316 return 1;
1317 }
1318
1319 CBB contents;
1320 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1321 !CBB_add_u16_length_prefixed(out, &contents) ||
1322 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1323 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1324 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1325 !CBB_flush(out)) {
1326 return 0;
1327 }
1328
1329 return 1;
1330}
1331
1332static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1333 CBS *contents) {
1334 if (contents == NULL) {
1335 return 1;
1336 }
1337
1338 if (CBS_len(contents) != 0) {
1339 return 0;
1340 }
1341
1342 ssl->s3->tmp.certificate_status_expected = 1;
1343 return 1;
1344}
1345
1346static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1347 CBS *contents) {
1348 /* OCSP stapling as a server is not supported. */
1349 return 1;
1350}
1351
1352static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
1353 /* OCSP stapling as a server is not supported. */
1354 return 1;
1355}
1356
1357
Adam Langley97dfcbf2015-07-01 18:35:20 -07001358/* Next protocol negotiation.
1359 *
1360 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1361
1362static void ext_npn_init(SSL *ssl) {
1363 ssl->s3->next_proto_neg_seen = 0;
1364}
1365
1366static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1367 if (ssl->s3->initial_handshake_complete ||
1368 ssl->ctx->next_proto_select_cb == NULL ||
1369 SSL_IS_DTLS(ssl)) {
1370 return 1;
1371 }
1372
1373 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1374 !CBB_add_u16(out, 0 /* length */)) {
1375 return 0;
1376 }
1377
1378 return 1;
1379}
1380
1381static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1382 CBS *contents) {
1383 if (contents == NULL) {
1384 return 1;
1385 }
1386
1387 /* If any of these are false then we should never have sent the NPN
1388 * extension in the ClientHello and thus this function should never have been
1389 * called. */
1390 assert(!ssl->s3->initial_handshake_complete);
1391 assert(!SSL_IS_DTLS(ssl));
1392 assert(ssl->ctx->next_proto_select_cb != NULL);
1393
1394 const uint8_t *const orig_contents = CBS_data(contents);
1395 const size_t orig_len = CBS_len(contents);
1396
1397 while (CBS_len(contents) != 0) {
1398 CBS proto;
1399 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1400 CBS_len(&proto) == 0) {
1401 return 0;
1402 }
1403 }
1404
1405 uint8_t *selected;
1406 uint8_t selected_len;
1407 if (ssl->ctx->next_proto_select_cb(
1408 ssl, &selected, &selected_len, orig_contents, orig_len,
1409 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1410 *out_alert = SSL_AD_INTERNAL_ERROR;
1411 return 0;
1412 }
1413
1414 OPENSSL_free(ssl->next_proto_negotiated);
1415 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1416 if (ssl->next_proto_negotiated == NULL) {
1417 *out_alert = SSL_AD_INTERNAL_ERROR;
1418 return 0;
1419 }
1420
1421 ssl->next_proto_negotiated_len = selected_len;
1422 ssl->s3->next_proto_neg_seen = 1;
1423
1424 return 1;
1425}
1426
1427static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1428 CBS *contents) {
1429 if (contents != NULL && CBS_len(contents) != 0) {
1430 return 0;
1431 }
1432
1433 if (contents == NULL ||
1434 ssl->s3->initial_handshake_complete ||
1435 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1436 * afterwards, parsing the ALPN extension will clear
1437 * |next_proto_neg_seen|. */
1438 ssl->s3->alpn_selected != NULL ||
1439 ssl->ctx->next_protos_advertised_cb == NULL ||
1440 SSL_IS_DTLS(ssl)) {
1441 return 1;
1442 }
1443
1444 ssl->s3->next_proto_neg_seen = 1;
1445 return 1;
1446}
1447
1448static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1449 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1450 * parsed. */
1451 if (!ssl->s3->next_proto_neg_seen) {
1452 return 1;
1453 }
1454
1455 const uint8_t *npa;
1456 unsigned npa_len;
1457
1458 if (ssl->ctx->next_protos_advertised_cb(
1459 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1460 SSL_TLSEXT_ERR_OK) {
1461 ssl->s3->next_proto_neg_seen = 0;
1462 return 1;
1463 }
1464
1465 CBB contents;
1466 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1467 !CBB_add_u16_length_prefixed(out, &contents) ||
1468 !CBB_add_bytes(&contents, npa, npa_len) ||
1469 !CBB_flush(out)) {
1470 return 0;
1471 }
1472
1473 return 1;
1474}
1475
1476
Adam Langleyab8d87d2015-07-10 12:21:39 -07001477/* Signed certificate timestamps.
1478 *
1479 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1480
1481static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1482 if (!ssl->signed_cert_timestamps_enabled) {
1483 return 1;
1484 }
1485
1486 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1487 !CBB_add_u16(out, 0 /* length */)) {
1488 return 0;
1489 }
1490
1491 return 1;
1492}
1493
1494static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1495 CBS *contents) {
1496 if (contents == NULL) {
1497 return 1;
1498 }
1499
1500 /* If this is false then we should never have sent the SCT extension in the
1501 * ClientHello and thus this function should never have been called. */
1502 assert(ssl->signed_cert_timestamps_enabled);
1503
1504 if (CBS_len(contents) == 0) {
1505 *out_alert = SSL_AD_DECODE_ERROR;
1506 return 0;
1507 }
1508
1509 /* Session resumption uses the original session information. */
1510 if (!ssl->hit &&
1511 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1512 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1513 *out_alert = SSL_AD_INTERNAL_ERROR;
1514 return 0;
1515 }
1516
1517 return 1;
1518}
1519
1520static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1521 CBS *contents) {
1522 /* The SCT extension is not supported as a server. */
1523 return 1;
1524}
1525
1526static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1527 /* The SCT extension is not supported as a server. */
1528 return 1;
1529}
1530
1531
Adam Langleyf18e4532015-07-10 13:39:53 -07001532/* Application-level Protocol Negotiation.
1533 *
1534 * https://tools.ietf.org/html/rfc7301 */
1535
1536static void ext_alpn_init(SSL *ssl) {
1537 OPENSSL_free(ssl->s3->alpn_selected);
1538 ssl->s3->alpn_selected = NULL;
1539}
1540
1541static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1542 if (ssl->alpn_client_proto_list == NULL ||
1543 ssl->s3->initial_handshake_complete) {
1544 return 1;
1545 }
1546
1547 CBB contents, proto_list;
1548 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1549 !CBB_add_u16_length_prefixed(out, &contents) ||
1550 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1551 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1552 ssl->alpn_client_proto_list_len) ||
1553 !CBB_flush(out)) {
1554 return 0;
1555 }
1556
1557 return 1;
1558}
1559
1560static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1561 CBS *contents) {
1562 if (contents == NULL) {
1563 return 1;
1564 }
1565
1566 assert(!ssl->s3->initial_handshake_complete);
1567 assert(ssl->alpn_client_proto_list != NULL);
1568
1569 /* The extension data consists of a ProtocolNameList which must have
1570 * exactly one ProtocolName. Each of these is length-prefixed. */
1571 CBS protocol_name_list, protocol_name;
1572 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1573 CBS_len(contents) != 0 ||
1574 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1575 /* Empty protocol names are forbidden. */
1576 CBS_len(&protocol_name) == 0 ||
1577 CBS_len(&protocol_name_list) != 0) {
1578 return 0;
1579 }
1580
1581 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1582 &ssl->s3->alpn_selected_len)) {
1583 *out_alert = SSL_AD_INTERNAL_ERROR;
1584 return 0;
1585 }
1586
1587 return 1;
1588}
1589
1590static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1591 CBS *contents) {
1592 if (contents == NULL) {
1593 return 1;
1594 }
1595
1596 if (ssl->ctx->alpn_select_cb == NULL ||
1597 ssl->s3->initial_handshake_complete) {
1598 return 1;
1599 }
1600
1601 /* ALPN takes precedence over NPN. */
1602 ssl->s3->next_proto_neg_seen = 0;
1603
1604 CBS protocol_name_list;
1605 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1606 CBS_len(contents) != 0 ||
1607 CBS_len(&protocol_name_list) < 2) {
1608 return 0;
1609 }
1610
1611 /* Validate the protocol list. */
1612 CBS protocol_name_list_copy = protocol_name_list;
1613 while (CBS_len(&protocol_name_list_copy) > 0) {
1614 CBS protocol_name;
1615
1616 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1617 /* Empty protocol names are forbidden. */
1618 CBS_len(&protocol_name) == 0) {
1619 return 0;
1620 }
1621 }
1622
1623 const uint8_t *selected;
1624 uint8_t selected_len;
1625 if (ssl->ctx->alpn_select_cb(
1626 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1627 CBS_len(&protocol_name_list),
1628 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1629 OPENSSL_free(ssl->s3->alpn_selected);
1630 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1631 if (ssl->s3->alpn_selected == NULL) {
1632 *out_alert = SSL_AD_INTERNAL_ERROR;
1633 return 0;
1634 }
1635 ssl->s3->alpn_selected_len = selected_len;
1636 }
1637
1638 return 1;
1639}
1640
1641static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1642 if (ssl->s3->alpn_selected == NULL) {
1643 return 1;
1644 }
1645
1646 CBB contents, proto_list, proto;
1647 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1648 !CBB_add_u16_length_prefixed(out, &contents) ||
1649 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1650 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1651 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1652 !CBB_flush(out)) {
1653 return 0;
1654 }
1655
1656 return 1;
1657}
1658
1659
Adam Langley49c7af12015-07-10 14:33:46 -07001660/* Channel ID.
1661 *
1662 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1663
1664static void ext_channel_id_init(SSL *ssl) {
1665 ssl->s3->tlsext_channel_id_valid = 0;
1666}
1667
1668static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1669 if (!ssl->tlsext_channel_id_enabled ||
1670 SSL_IS_DTLS(ssl)) {
1671 return 1;
1672 }
1673
1674 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1675 !CBB_add_u16(out, 0 /* length */)) {
1676 return 0;
1677 }
1678
1679 return 1;
1680}
1681
1682static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1683 CBS *contents) {
1684 if (contents == NULL) {
1685 return 1;
1686 }
1687
1688 assert(!SSL_IS_DTLS(ssl));
1689 assert(ssl->tlsext_channel_id_enabled);
1690
1691 if (CBS_len(contents) != 0) {
1692 return 0;
1693 }
1694
1695 ssl->s3->tlsext_channel_id_valid = 1;
1696 return 1;
1697}
1698
1699static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1700 CBS *contents) {
1701 if (contents == NULL ||
1702 !ssl->tlsext_channel_id_enabled ||
1703 SSL_IS_DTLS(ssl)) {
1704 return 1;
1705 }
1706
1707 if (CBS_len(contents) != 0) {
1708 return 0;
1709 }
1710
1711 ssl->s3->tlsext_channel_id_valid = 1;
1712 return 1;
1713}
1714
1715static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1716 if (!ssl->s3->tlsext_channel_id_valid) {
1717 return 1;
1718 }
1719
1720 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1721 !CBB_add_u16(out, 0 /* length */)) {
1722 return 0;
1723 }
1724
1725 return 1;
1726}
1727
Adam Langley391250d2015-07-15 19:06:07 -07001728
1729/* Secure Real-time Transport Protocol (SRTP) extension.
1730 *
1731 * https://tools.ietf.org/html/rfc5764 */
1732
Adam Langley391250d2015-07-15 19:06:07 -07001733
1734static void ext_srtp_init(SSL *ssl) {
1735 ssl->srtp_profile = NULL;
1736}
1737
1738static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1739 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1740 if (profiles == NULL) {
1741 return 1;
1742 }
1743 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1744 if (num_profiles == 0) {
1745 return 1;
1746 }
1747
1748 CBB contents, profile_ids;
1749 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1750 !CBB_add_u16_length_prefixed(out, &contents) ||
1751 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1752 return 0;
1753 }
1754
1755 size_t i;
1756 for (i = 0; i < num_profiles; i++) {
1757 if (!CBB_add_u16(&profile_ids,
1758 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1759 return 0;
1760 }
1761 }
1762
1763 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1764 !CBB_flush(out)) {
1765 return 0;
1766 }
1767
1768 return 1;
1769}
1770
1771static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1772 CBS *contents) {
1773 if (contents == NULL) {
1774 return 1;
1775 }
1776
1777 /* The extension consists of a u16-prefixed profile ID list containing a
1778 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1779 *
1780 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1781 CBS profile_ids, srtp_mki;
1782 uint16_t profile_id;
1783 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1784 !CBS_get_u16(&profile_ids, &profile_id) ||
1785 CBS_len(&profile_ids) != 0 ||
1786 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1787 CBS_len(contents) != 0) {
1788 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1789 return 0;
1790 }
1791
1792 if (CBS_len(&srtp_mki) != 0) {
1793 /* Must be no MKI, since we never offer one. */
1794 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1795 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1796 return 0;
1797 }
1798
1799 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1800
1801 /* Check to see if the server gave us something we support (and presumably
1802 * offered). */
1803 size_t i;
1804 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1805 const SRTP_PROTECTION_PROFILE *profile =
1806 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1807
1808 if (profile->id == profile_id) {
1809 ssl->srtp_profile = profile;
1810 return 1;
1811 }
1812 }
1813
1814 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1815 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1816 return 0;
1817}
1818
1819static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1820 CBS *contents) {
1821 if (contents == NULL) {
1822 return 1;
1823 }
1824
1825 CBS profile_ids, srtp_mki;
1826 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1827 CBS_len(&profile_ids) < 2 ||
1828 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1829 CBS_len(contents) != 0) {
1830 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1831 return 0;
1832 }
1833 /* Discard the MKI value for now. */
1834
1835 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1836 SSL_get_srtp_profiles(ssl);
1837
1838 /* Pick the server's most preferred profile. */
1839 size_t i;
1840 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1841 const SRTP_PROTECTION_PROFILE *server_profile =
1842 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1843
1844 CBS profile_ids_tmp;
1845 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1846
1847 while (CBS_len(&profile_ids_tmp) > 0) {
1848 uint16_t profile_id;
1849 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1850 return 0;
1851 }
1852
1853 if (server_profile->id == profile_id) {
1854 ssl->srtp_profile = server_profile;
1855 return 1;
1856 }
1857 }
1858 }
1859
1860 return 1;
1861}
1862
1863static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1864 if (ssl->srtp_profile == NULL) {
1865 return 1;
1866 }
1867
1868 CBB contents, profile_ids;
1869 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1870 !CBB_add_u16_length_prefixed(out, &contents) ||
1871 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1872 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1873 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1874 !CBB_flush(out)) {
1875 return 0;
1876 }
1877
1878 return 1;
1879}
1880
Adam Langleybdd5d662015-07-20 16:19:08 -07001881
1882/* EC point formats.
1883 *
1884 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1885
1886static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1887 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1888 return 0;
1889 }
1890
1891 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1892
1893 size_t i;
1894 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1895 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1896
1897 const uint32_t alg_k = cipher->algorithm_mkey;
1898 const uint32_t alg_a = cipher->algorithm_auth;
1899 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1900 return 1;
1901 }
1902 }
1903
1904 return 0;
1905}
1906
Adam Langleybdd5d662015-07-20 16:19:08 -07001907static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001908 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001909 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1910 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001911 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1912 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001913 !CBB_flush(out)) {
1914 return 0;
1915 }
1916
1917 return 1;
1918}
1919
1920static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1921 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1922 return 1;
1923 }
1924
1925 return ext_ec_point_add_extension(ssl, out);
1926}
1927
1928static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1929 CBS *contents) {
1930 if (contents == NULL) {
1931 return 1;
1932 }
1933
1934 CBS ec_point_format_list;
1935 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1936 CBS_len(contents) != 0) {
1937 return 0;
1938 }
1939
David Benjaminfc059942015-07-30 23:01:59 -04001940 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1941 * point format. */
1942 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1943 CBS_len(&ec_point_format_list)) == NULL) {
1944 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1952 CBS *contents) {
1953 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1954}
1955
1956static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1957 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1958 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001959 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001960
1961 if (!using_ecc) {
1962 return 1;
1963 }
1964
1965 return ext_ec_point_add_extension(ssl, out);
1966}
1967
Adam Langley273d49c2015-07-20 16:38:52 -07001968
1969/* EC supported curves.
1970 *
1971 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1972
1973static void ext_ec_curves_init(SSL *ssl) {
1974 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1975 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1976 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1977}
1978
1979static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1980 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1981 return 1;
1982 }
1983
1984 CBB contents, curves_bytes;
1985 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1986 !CBB_add_u16_length_prefixed(out, &contents) ||
1987 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1988 return 0;
1989 }
1990
1991 const uint16_t *curves;
1992 size_t curves_len;
1993 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1994
1995 size_t i;
1996 for (i = 0; i < curves_len; i++) {
1997 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1998 return 0;
1999 }
2000 }
2001
2002 return CBB_flush(out);
2003}
2004
2005static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2006 CBS *contents) {
2007 /* This extension is not expected to be echoed by servers and is ignored. */
2008 return 1;
2009}
2010
2011static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2012 CBS *contents) {
2013 if (contents == NULL) {
2014 return 1;
2015 }
2016
2017 CBS elliptic_curve_list;
2018 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2019 CBS_len(&elliptic_curve_list) == 0 ||
2020 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2021 CBS_len(contents) != 0) {
2022 return 0;
2023 }
2024
2025 ssl->s3->tmp.peer_ellipticcurvelist =
2026 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2027
2028 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2029 *out_alert = SSL_AD_INTERNAL_ERROR;
2030 return 0;
2031 }
2032
2033 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2034 size_t i;
2035 for (i = 0; i < num_curves; i++) {
2036 if (!CBS_get_u16(&elliptic_curve_list,
2037 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2038 goto err;
2039 }
2040 }
2041
2042 assert(CBS_len(&elliptic_curve_list) == 0);
2043 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2044
2045 return 1;
2046
2047err:
2048 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2049 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2050 *out_alert = SSL_AD_INTERNAL_ERROR;
2051 return 0;
2052}
2053
2054static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2055 /* Servers don't echo this extension. */
2056 return 1;
2057}
2058
2059
Adam Langley614c66a2015-06-12 15:26:58 -07002060/* kExtensions contains all the supported extensions. */
2061static const struct tls_extension kExtensions[] = {
2062 {
Adam Langley5021b222015-06-12 18:27:58 -07002063 /* The renegotiation extension must always be at index zero because the
2064 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2065 * sent as an SCSV. */
2066 TLSEXT_TYPE_renegotiate,
2067 NULL,
2068 ext_ri_add_clienthello,
2069 ext_ri_parse_serverhello,
2070 ext_ri_parse_clienthello,
2071 ext_ri_add_serverhello,
2072 },
2073 {
Adam Langley614c66a2015-06-12 15:26:58 -07002074 TLSEXT_TYPE_server_name,
2075 ext_sni_init,
2076 ext_sni_add_clienthello,
2077 ext_sni_parse_serverhello,
2078 ext_sni_parse_clienthello,
2079 ext_sni_add_serverhello,
2080 },
Adam Langley0a056712015-07-01 15:03:33 -07002081 {
2082 TLSEXT_TYPE_extended_master_secret,
2083 ext_ems_init,
2084 ext_ems_add_clienthello,
2085 ext_ems_parse_serverhello,
2086 ext_ems_parse_clienthello,
2087 ext_ems_add_serverhello,
2088 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002089 {
2090 TLSEXT_TYPE_session_ticket,
2091 NULL,
2092 ext_ticket_add_clienthello,
2093 ext_ticket_parse_serverhello,
2094 ext_ticket_parse_clienthello,
2095 ext_ticket_add_serverhello,
2096 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002097 {
2098 TLSEXT_TYPE_signature_algorithms,
2099 NULL,
2100 ext_sigalgs_add_clienthello,
2101 ext_sigalgs_parse_serverhello,
2102 ext_sigalgs_parse_clienthello,
2103 ext_sigalgs_add_serverhello,
2104 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002105 {
2106 TLSEXT_TYPE_status_request,
2107 ext_ocsp_init,
2108 ext_ocsp_add_clienthello,
2109 ext_ocsp_parse_serverhello,
2110 ext_ocsp_parse_clienthello,
2111 ext_ocsp_add_serverhello,
2112 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002113 {
2114 TLSEXT_TYPE_next_proto_neg,
2115 ext_npn_init,
2116 ext_npn_add_clienthello,
2117 ext_npn_parse_serverhello,
2118 ext_npn_parse_clienthello,
2119 ext_npn_add_serverhello,
2120 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002121 {
2122 TLSEXT_TYPE_certificate_timestamp,
2123 NULL,
2124 ext_sct_add_clienthello,
2125 ext_sct_parse_serverhello,
2126 ext_sct_parse_clienthello,
2127 ext_sct_add_serverhello,
2128 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002129 {
2130 TLSEXT_TYPE_application_layer_protocol_negotiation,
2131 ext_alpn_init,
2132 ext_alpn_add_clienthello,
2133 ext_alpn_parse_serverhello,
2134 ext_alpn_parse_clienthello,
2135 ext_alpn_add_serverhello,
2136 },
Adam Langley49c7af12015-07-10 14:33:46 -07002137 {
2138 TLSEXT_TYPE_channel_id,
2139 ext_channel_id_init,
2140 ext_channel_id_add_clienthello,
2141 ext_channel_id_parse_serverhello,
2142 ext_channel_id_parse_clienthello,
2143 ext_channel_id_add_serverhello,
2144 },
Adam Langley391250d2015-07-15 19:06:07 -07002145 {
2146 TLSEXT_TYPE_srtp,
2147 ext_srtp_init,
2148 ext_srtp_add_clienthello,
2149 ext_srtp_parse_serverhello,
2150 ext_srtp_parse_clienthello,
2151 ext_srtp_add_serverhello,
2152 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002153 {
2154 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002155 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002156 ext_ec_point_add_clienthello,
2157 ext_ec_point_parse_serverhello,
2158 ext_ec_point_parse_clienthello,
2159 ext_ec_point_add_serverhello,
2160 },
Adam Langley273d49c2015-07-20 16:38:52 -07002161 {
2162 TLSEXT_TYPE_elliptic_curves,
2163 ext_ec_curves_init,
2164 ext_ec_curves_add_clienthello,
2165 ext_ec_curves_parse_serverhello,
2166 ext_ec_curves_parse_clienthello,
2167 ext_ec_curves_add_serverhello,
2168 },
Adam Langley614c66a2015-06-12 15:26:58 -07002169};
2170
2171#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2172
Adam Langley4cfa96b2015-07-01 11:56:55 -07002173OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2174 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002175 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002176OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2177 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2178 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002179 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002180
Adam Langley614c66a2015-06-12 15:26:58 -07002181static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2182 uint16_t value) {
2183 unsigned i;
2184 for (i = 0; i < kNumExtensions; i++) {
2185 if (kExtensions[i].value == value) {
2186 *out_index = i;
2187 return &kExtensions[i];
2188 }
2189 }
2190
2191 return NULL;
2192}
2193
Adam Langley09505632015-07-30 18:10:13 -07002194int SSL_extension_supported(unsigned extension_value) {
2195 uint32_t index;
2196 return extension_value == TLSEXT_TYPE_padding ||
2197 tls_extension_find(&index, extension_value) != NULL;
2198}
2199
Adam Langleyb0c235e2014-06-20 12:00:00 -07002200/* header_len is the length of the ClientHello header written so far, used to
2201 * compute padding. It does not include the record header. Pass 0 if no padding
2202 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002203uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2204 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2206 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002207 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002208 }
Adam Langley95c29f32014-06-20 12:00:00 -07002209
Adam Langley33ad2b52015-07-20 17:43:53 -07002210 CBB cbb, extensions;
2211 CBB_zero(&cbb);
2212 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2213 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2214 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 }
Adam Langley95c29f32014-06-20 12:00:00 -07002216
Adam Langley614c66a2015-06-12 15:26:58 -07002217 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002218 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002219
Adam Langley614c66a2015-06-12 15:26:58 -07002220 size_t i;
2221 for (i = 0; i < kNumExtensions; i++) {
2222 if (kExtensions[i].init != NULL) {
2223 kExtensions[i].init(s);
2224 }
2225 }
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langley614c66a2015-06-12 15:26:58 -07002227 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002228 const size_t len_before = CBB_len(&extensions);
2229 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2230 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2231 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2232 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002233 }
Adam Langley95c29f32014-06-20 12:00:00 -07002234
Adam Langley33ad2b52015-07-20 17:43:53 -07002235 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002236 s->s3->tmp.extensions.sent |= (1u << i);
2237 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 }
Adam Langley75712922014-10-10 16:23:43 -07002239
Adam Langley09505632015-07-30 18:10:13 -07002240 if (!custom_ext_add_clienthello(s, &extensions)) {
2241 goto err;
2242 }
2243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002245 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002246 if (header_len > 0xff && header_len < 0x200) {
2247 /* Add padding to workaround bugs in F5 terminators. See
2248 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2249 *
2250 * NB: because this code works out the length of all existing extensions
2251 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002252 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 /* Extensions take at least four bytes to encode. Always include least
2254 * one byte of data if including the extension. WebSphere Application
2255 * Server 7.0 is intolerant to the last extension being zero-length. */
2256 if (padding_len >= 4 + 1) {
2257 padding_len -= 4;
2258 } else {
2259 padding_len = 1;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langley33ad2b52015-07-20 17:43:53 -07002262 uint8_t *padding_bytes;
2263 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2264 !CBB_add_u16(&extensions, padding_len) ||
2265 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2266 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 }
Adam Langley75712922014-10-10 16:23:43 -07002268
Adam Langley33ad2b52015-07-20 17:43:53 -07002269 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 }
2271 }
Adam Langley75712922014-10-10 16:23:43 -07002272
Adam Langley33ad2b52015-07-20 17:43:53 -07002273 if (!CBB_flush(&cbb)) {
2274 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langley33ad2b52015-07-20 17:43:53 -07002277 uint8_t *ret = buf;
2278 const size_t cbb_len = CBB_len(&cbb);
2279 /* If only two bytes have been written then the extensions are actually empty
2280 * and those two bytes are the zero length. In that case, we don't bother
2281 * sending the extensions length. */
2282 if (cbb_len > 2) {
2283 ret += cbb_len;
2284 }
2285
2286 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002287 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002288
2289err:
2290 CBB_cleanup(&cbb);
2291 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2292 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293}
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langley614c66a2015-06-12 15:26:58 -07002295uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2296 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2298 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002299 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 }
Adam Langley95c29f32014-06-20 12:00:00 -07002301
Adam Langley33ad2b52015-07-20 17:43:53 -07002302 CBB cbb, extensions;
2303 CBB_zero(&cbb);
2304 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2305 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2306 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002307 }
2308
2309 unsigned i;
2310 for (i = 0; i < kNumExtensions; i++) {
2311 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2312 /* Don't send extensions that were not received. */
2313 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 }
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langley33ad2b52015-07-20 17:43:53 -07002316 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2317 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2318 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2319 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002320 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 }
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langley09505632015-07-30 18:10:13 -07002323 if (!custom_ext_add_serverhello(s, &extensions)) {
2324 goto err;
2325 }
2326
Adam Langley33ad2b52015-07-20 17:43:53 -07002327 if (!CBB_flush(&cbb)) {
2328 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 }
2330
Adam Langley33ad2b52015-07-20 17:43:53 -07002331 uint8_t *ret = buf;
2332 const size_t cbb_len = CBB_len(&cbb);
2333 /* If only two bytes have been written then the extensions are actually empty
2334 * and those two bytes are the zero length. In that case, we don't bother
2335 * sending the extensions length. */
2336 if (cbb_len > 2) {
2337 ret += cbb_len;
2338 }
2339
2340 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002341 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002342
2343err:
2344 CBB_cleanup(&cbb);
2345 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2346 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002347}
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyfcf25832014-12-18 17:42:32 -08002349static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002350 size_t i;
2351 for (i = 0; i < kNumExtensions; i++) {
2352 if (kExtensions[i].init != NULL) {
2353 kExtensions[i].init(s);
2354 }
2355 }
2356
2357 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002358 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002359 /* The renegotiation extension must always be at index zero because the
2360 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2361 * sent as an SCSV. */
2362 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002365 if (CBS_len(cbs) != 0) {
2366 /* Decode the extensions block and check it is valid. */
2367 CBS extensions;
2368 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2369 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 *out_alert = SSL_AD_DECODE_ERROR;
2371 return 0;
2372 }
Adam Langley95c29f32014-06-20 12:00:00 -07002373
Adam Langley33ad2b52015-07-20 17:43:53 -07002374 while (CBS_len(&extensions) != 0) {
2375 uint16_t type;
2376 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002377
Adam Langley33ad2b52015-07-20 17:43:53 -07002378 /* Decode the next extension. */
2379 if (!CBS_get_u16(&extensions, &type) ||
2380 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2381 *out_alert = SSL_AD_DECODE_ERROR;
2382 return 0;
2383 }
2384
2385 unsigned ext_index;
2386 const struct tls_extension *const ext =
2387 tls_extension_find(&ext_index, type);
2388
2389 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002390 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2391 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2392 return 0;
2393 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002394 continue;
2395 }
2396
Adam Langley614c66a2015-06-12 15:26:58 -07002397 s->s3->tmp.extensions.received |= (1u << ext_index);
2398 uint8_t alert = SSL_AD_DECODE_ERROR;
2399 if (!ext->parse_clienthello(s, &alert, &extension)) {
2400 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002401 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2402 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 return 0;
2404 }
Adam Langley614c66a2015-06-12 15:26:58 -07002405 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 }
Adam Langley75712922014-10-10 16:23:43 -07002407
Adam Langley614c66a2015-06-12 15:26:58 -07002408 for (i = 0; i < kNumExtensions; i++) {
2409 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2410 /* Extension wasn't observed so call the callback with a NULL
2411 * parameter. */
2412 uint8_t alert = SSL_AD_DECODE_ERROR;
2413 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002414 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2415 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002416 *out_alert = alert;
2417 return 0;
2418 }
2419 }
2420 }
2421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 return 1;
2423}
Adam Langley95c29f32014-06-20 12:00:00 -07002424
Adam Langleyfcf25832014-12-18 17:42:32 -08002425int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2426 int alert = -1;
2427 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2428 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2429 return 0;
2430 }
Adam Langley95c29f32014-06-20 12:00:00 -07002431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002433 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 return 0;
2435 }
Adam Langley95c29f32014-06-20 12:00:00 -07002436
Adam Langleyfcf25832014-12-18 17:42:32 -08002437 return 1;
2438}
Adam Langley95c29f32014-06-20 12:00:00 -07002439
Adam Langleyfcf25832014-12-18 17:42:32 -08002440static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002441 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002442 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002443
Adam Langley33ad2b52015-07-20 17:43:53 -07002444 if (CBS_len(cbs) != 0) {
2445 /* Decode the extensions block and check it is valid. */
2446 CBS extensions;
2447 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2448 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002449 *out_alert = SSL_AD_DECODE_ERROR;
2450 return 0;
2451 }
Adam Langley95c29f32014-06-20 12:00:00 -07002452
Adam Langley614c66a2015-06-12 15:26:58 -07002453
Adam Langley33ad2b52015-07-20 17:43:53 -07002454 while (CBS_len(&extensions) != 0) {
2455 uint16_t type;
2456 CBS extension;
2457
2458 /* Decode the next extension. */
2459 if (!CBS_get_u16(&extensions, &type) ||
2460 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2461 *out_alert = SSL_AD_DECODE_ERROR;
2462 return 0;
2463 }
2464
2465 unsigned ext_index;
2466 const struct tls_extension *const ext =
2467 tls_extension_find(&ext_index, type);
2468
Adam Langley09505632015-07-30 18:10:13 -07002469 if (ext == NULL) {
2470 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2471 return 0;
2472 }
2473 continue;
2474 }
2475
2476 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2477 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002478 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002479 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 *out_alert = SSL_AD_DECODE_ERROR;
2481 return 0;
2482 }
David Benjamin03973092014-06-24 23:27:17 -04002483
Adam Langley614c66a2015-06-12 15:26:58 -07002484 received |= (1u << ext_index);
2485
2486 uint8_t alert = SSL_AD_DECODE_ERROR;
2487 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002488 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2489 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002490 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002491 return 0;
2492 }
Adam Langley614c66a2015-06-12 15:26:58 -07002493 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002494 }
Adam Langley95c29f32014-06-20 12:00:00 -07002495
Adam Langley33ad2b52015-07-20 17:43:53 -07002496 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002497 for (i = 0; i < kNumExtensions; i++) {
2498 if (!(received & (1u << i))) {
2499 /* Extension wasn't observed so call the callback with a NULL
2500 * parameter. */
2501 uint8_t alert = SSL_AD_DECODE_ERROR;
2502 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002503 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2504 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002505 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 return 0;
2507 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 }
2509 }
Adam Langley95c29f32014-06-20 12:00:00 -07002510
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 return 1;
2512}
Adam Langley95c29f32014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514static int ssl_check_clienthello_tlsext(SSL *s) {
2515 int ret = SSL_TLSEXT_ERR_NOACK;
2516 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2519 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002520
Adam Langleyfcf25832014-12-18 17:42:32 -08002521 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2522 ret = s->ctx->tlsext_servername_callback(s, &al,
2523 s->ctx->tlsext_servername_arg);
2524 } else if (s->initial_ctx != NULL &&
2525 s->initial_ctx->tlsext_servername_callback != 0) {
2526 ret = s->initial_ctx->tlsext_servername_callback(
2527 s, &al, s->initial_ctx->tlsext_servername_arg);
2528 }
Adam Langley95c29f32014-06-20 12:00:00 -07002529
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 switch (ret) {
2531 case SSL_TLSEXT_ERR_ALERT_FATAL:
2532 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2533 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 case SSL_TLSEXT_ERR_ALERT_WARNING:
2536 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2537 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002540 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 default:
2544 return 1;
2545 }
2546}
Adam Langleyed8270a2014-09-02 13:52:56 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002549 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002551
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2553 ret = s->ctx->tlsext_servername_callback(s, &al,
2554 s->ctx->tlsext_servername_arg);
2555 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002556 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 ret = s->initial_ctx->tlsext_servername_callback(
2558 s, &al, s->initial_ctx->tlsext_servername_arg);
2559 }
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 switch (ret) {
2562 case SSL_TLSEXT_ERR_ALERT_FATAL:
2563 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2564 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002565
Adam Langleyfcf25832014-12-18 17:42:32 -08002566 case SSL_TLSEXT_ERR_ALERT_WARNING:
2567 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2568 return 1;
2569
2570 default:
2571 return 1;
2572 }
2573}
2574
2575int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2576 int alert = -1;
2577 if (s->version < SSL3_VERSION) {
2578 return 1;
2579 }
2580
2581 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2582 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2583 return 0;
2584 }
2585
2586 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002587 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002588 return 0;
2589 }
2590
2591 return 1;
2592}
Adam Langley95c29f32014-06-20 12:00:00 -07002593
David Benjamine3aa1d92015-06-16 15:34:50 -04002594int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2595 int *out_send_ticket, const uint8_t *ticket,
2596 size_t ticket_len, const uint8_t *session_id,
2597 size_t session_id_len) {
2598 int ret = 1; /* Most errors are non-fatal. */
2599 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2600 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002601
David Benjamine3aa1d92015-06-16 15:34:50 -04002602 HMAC_CTX hmac_ctx;
2603 HMAC_CTX_init(&hmac_ctx);
2604 EVP_CIPHER_CTX cipher_ctx;
2605 EVP_CIPHER_CTX_init(&cipher_ctx);
2606
2607 *out_send_ticket = 0;
2608 *out_session = NULL;
2609
2610 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2611 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 }
2613
David Benjamine3aa1d92015-06-16 15:34:50 -04002614 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002616 *out_send_ticket = 1;
2617 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 }
2619
David Benjaminadcc3952015-04-26 13:07:57 -04002620 /* Ensure there is room for the key name and the largest IV
2621 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2622 * the maximum IV length should be well under the minimum size for the
2623 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002624 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2625 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002627 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002628
David Benjamine3aa1d92015-06-16 15:34:50 -04002629 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2630 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2631 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2632 0 /* decrypt */);
2633 if (cb_ret < 0) {
2634 ret = 0;
2635 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002637 if (cb_ret == 0) {
2638 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002640 if (cb_ret == 2) {
2641 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 }
2643 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002644 /* Check the key name matches. */
2645 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2646 SSL_TICKET_KEY_NAME_LEN) != 0) {
2647 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002649 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2650 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002652 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2653 ssl_ctx->tlsext_tick_aes_key, iv)) {
2654 ret = 0;
2655 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 }
2657 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002658 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002659
David Benjamine3aa1d92015-06-16 15:34:50 -04002660 /* Check the MAC at the end of the ticket. */
2661 uint8_t mac[EVP_MAX_MD_SIZE];
2662 size_t mac_len = HMAC_size(&hmac_ctx);
2663 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002664 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002665 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002666 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002667 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2668 HMAC_Final(&hmac_ctx, mac, NULL);
2669 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2670 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 }
2672
David Benjamine3aa1d92015-06-16 15:34:50 -04002673 /* Decrypt the session data. */
2674 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2675 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2676 mac_len;
2677 plaintext = OPENSSL_malloc(ciphertext_len);
2678 if (plaintext == NULL) {
2679 ret = 0;
2680 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002682 if (ciphertext_len >= INT_MAX) {
2683 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002685 int len1, len2;
2686 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2687 (int)ciphertext_len) ||
2688 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2689 ERR_clear_error(); /* Don't leave an error on the queue. */
2690 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 }
2692
David Benjamine3aa1d92015-06-16 15:34:50 -04002693 /* Decode the session. */
2694 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2695 if (session == NULL) {
2696 ERR_clear_error(); /* Don't leave an error on the queue. */
2697 goto done;
2698 }
2699
2700 /* Copy the client's session ID into the new session, to denote the ticket has
2701 * been accepted. */
2702 memcpy(session->session_id, session_id, session_id_len);
2703 session->session_id_length = session_id_len;
2704
2705 *out_session = session;
2706
2707done:
2708 OPENSSL_free(plaintext);
2709 HMAC_CTX_cleanup(&hmac_ctx);
2710 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2711 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002712}
Adam Langley95c29f32014-06-20 12:00:00 -07002713
2714/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002715typedef struct {
2716 int nid;
2717 int id;
2718} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002719
Adam Langleyfcf25832014-12-18 17:42:32 -08002720static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2721 {NID_sha1, TLSEXT_hash_sha1},
2722 {NID_sha224, TLSEXT_hash_sha224},
2723 {NID_sha256, TLSEXT_hash_sha256},
2724 {NID_sha384, TLSEXT_hash_sha384},
2725 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002726
Adam Langleyfcf25832014-12-18 17:42:32 -08002727static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2728 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2731 size_t i;
2732 for (i = 0; i < tlen; i++) {
2733 if (table[i].nid == nid) {
2734 return table[i].id;
2735 }
2736 }
Adam Langley95c29f32014-06-20 12:00:00 -07002737
Adam Langleyfcf25832014-12-18 17:42:32 -08002738 return -1;
2739}
Adam Langley95c29f32014-06-20 12:00:00 -07002740
David Benjaminb4d65fd2015-05-29 17:11:21 -04002741int tls12_get_sigid(int pkey_type) {
2742 return tls12_find_id(pkey_type, tls12_sig,
2743 sizeof(tls12_sig) / sizeof(tls12_lookup));
2744}
2745
David Benjamind1d80782015-07-05 11:54:09 -04002746int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002747 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002748
Adam Langleyfcf25832014-12-18 17:42:32 -08002749 if (!md) {
2750 return 0;
2751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2754 sizeof(tls12_md) / sizeof(tls12_lookup));
2755 if (md_id == -1) {
2756 return 0;
2757 }
Adam Langley95c29f32014-06-20 12:00:00 -07002758
David Benjamind1d80782015-07-05 11:54:09 -04002759 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 if (sig_id == -1) {
2761 return 0;
2762 }
Adam Langley95c29f32014-06-20 12:00:00 -07002763
Adam Langleyfcf25832014-12-18 17:42:32 -08002764 p[0] = (uint8_t)md_id;
2765 p[1] = (uint8_t)sig_id;
2766 return 1;
2767}
2768
Adam Langleyfcf25832014-12-18 17:42:32 -08002769const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2770 switch (hash_alg) {
2771 case TLSEXT_hash_md5:
2772 return EVP_md5();
2773
2774 case TLSEXT_hash_sha1:
2775 return EVP_sha1();
2776
2777 case TLSEXT_hash_sha224:
2778 return EVP_sha224();
2779
2780 case TLSEXT_hash_sha256:
2781 return EVP_sha256();
2782
2783 case TLSEXT_hash_sha384:
2784 return EVP_sha384();
2785
2786 case TLSEXT_hash_sha512:
2787 return EVP_sha512();
2788
2789 default:
2790 return NULL;
2791 }
2792}
Adam Langley95c29f32014-06-20 12:00:00 -07002793
David Benjaminec2f27d2014-11-13 19:17:25 -05002794/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2795 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002796static int tls12_get_pkey_type(uint8_t sig_alg) {
2797 switch (sig_alg) {
2798 case TLSEXT_signature_rsa:
2799 return EVP_PKEY_RSA;
2800
2801 case TLSEXT_signature_ecdsa:
2802 return EVP_PKEY_EC;
2803
2804 default:
2805 return -1;
2806 }
2807}
Adam Langley95c29f32014-06-20 12:00:00 -07002808
Adam Langley95c29f32014-06-20 12:00:00 -07002809/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002810static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2811 size_t preflen, const uint8_t *allow,
2812 size_t allowlen) {
2813 const uint8_t *ptmp, *atmp;
2814 size_t i, j, nmatch = 0;
2815
2816 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2817 /* Skip disabled hashes or signature algorithms */
2818 if (tls12_get_hash(ptmp[0]) == NULL ||
2819 tls12_get_pkey_type(ptmp[1]) == -1) {
2820 continue;
2821 }
2822
2823 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2824 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2825 nmatch++;
2826 if (shsig) {
2827 shsig->rhash = ptmp[0];
2828 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 shsig++;
2830 }
2831
2832 break;
2833 }
2834 }
2835 }
2836
2837 return nmatch;
2838}
Adam Langley95c29f32014-06-20 12:00:00 -07002839
2840/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002841static int tls1_set_shared_sigalgs(SSL *s) {
2842 const uint8_t *pref, *allow, *conf;
2843 size_t preflen, allowlen, conflen;
2844 size_t nmatch;
2845 TLS_SIGALGS *salgs = NULL;
2846 CERT *c = s->cert;
2847
David Benjamin2755a3e2015-04-22 16:17:58 -04002848 OPENSSL_free(c->shared_sigalgs);
2849 c->shared_sigalgs = NULL;
2850 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002851
David Benjamin3dd90162015-08-09 11:26:40 -04002852 conflen = tls12_get_psigalgs(s, &conf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002853
2854 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2855 pref = conf;
2856 preflen = conflen;
2857 allow = c->peer_sigalgs;
2858 allowlen = c->peer_sigalgslen;
2859 } else {
2860 allow = conf;
2861 allowlen = conflen;
2862 pref = c->peer_sigalgs;
2863 preflen = c->peer_sigalgslen;
2864 }
2865
2866 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2867 if (!nmatch) {
2868 return 1;
2869 }
2870
2871 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2872 if (!salgs) {
2873 return 0;
2874 }
2875
2876 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2877 c->shared_sigalgs = salgs;
2878 c->shared_sigalgslen = nmatch;
2879 return 1;
2880}
Adam Langley95c29f32014-06-20 12:00:00 -07002881
2882/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002883int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2884 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002885
Adam Langleyfcf25832014-12-18 17:42:32 -08002886 /* Extension ignored for inappropriate versions */
2887 if (!SSL_USE_SIGALGS(s)) {
2888 return 1;
2889 }
David Benjamincd996942014-07-20 16:23:51 -04002890
David Benjamin61c0d4e2015-03-19 14:24:37 -04002891 if (CBS_len(sigalgs) % 2 != 0 ||
2892 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2893 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002894 return 0;
2895 }
Adam Langley95c29f32014-06-20 12:00:00 -07002896
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 return 1;
2898}
David Benjaminec2f27d2014-11-13 19:17:25 -05002899
David Benjamind1d80782015-07-05 11:54:09 -04002900const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2901 CERT *cert = ssl->cert;
2902 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002904
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002906 for (i = 0; i < cert->shared_sigalgslen; i++) {
2907 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002908 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002909 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2910 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002911 continue;
2912 }
2913 return md;
2914 }
Adam Langley95c29f32014-06-20 12:00:00 -07002915
Adam Langleyfcf25832014-12-18 17:42:32 -08002916 /* If no suitable digest may be found, default to SHA-1. */
2917 return EVP_sha1();
2918}
Adam Langley95c29f32014-06-20 12:00:00 -07002919
David Benjamind6a4ae92015-08-06 11:10:51 -04002920int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2921 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002922 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002923
2924 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002925 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2926 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002928
David Benjamind6a4ae92015-08-06 11:10:51 -04002929 static const char kClientIDMagic[] = "TLS Channel ID signature";
2930 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2931
2932 if (ssl->hit) {
2933 static const char kResumptionMagic[] = "Resumption";
2934 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2935 if (ssl->session->original_handshake_hash_len == 0) {
2936 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2937 goto err;
2938 }
2939 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2940 ssl->session->original_handshake_hash_len);
2941 }
2942
2943 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2944 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2945 sizeof(handshake_hash));
2946 if (handshake_hash_len < 0) {
2947 goto err;
2948 }
2949 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2950 unsigned len_u;
2951 EVP_DigestFinal_ex(&ctx, out, &len_u);
2952 *out_len = len_u;
2953
2954 ret = 1;
2955
2956err:
2957 EVP_MD_CTX_cleanup(&ctx);
2958 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002959}
Adam Langley1258b6a2014-06-20 12:00:00 -07002960
2961/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2962 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002963int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2964 int digest_len;
2965 /* This function should never be called for a resumed session because the
2966 * handshake hashes that we wish to record are for the original, full
2967 * handshake. */
2968 if (s->hit) {
2969 return -1;
2970 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002971
Adam Langleyfcf25832014-12-18 17:42:32 -08002972 digest_len =
2973 tls1_handshake_digest(s, s->session->original_handshake_hash,
2974 sizeof(s->session->original_handshake_hash));
2975 if (digest_len < 0) {
2976 return -1;
2977 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002978
Adam Langleyfcf25832014-12-18 17:42:32 -08002979 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002980
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 return 1;
2982}