blob: 40b97521e8e18b2b2b30cde442e8d5c36316ea49 [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,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001333 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001334 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) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001348 if (contents == NULL) {
1349 return 1;
1350 }
1351
1352 uint8_t status_type;
1353 if (!CBS_get_u8(contents, &status_type)) {
1354 return 0;
1355 }
1356
1357 /* We cannot decide whether OCSP stapling will occur yet because the correct
1358 * SSL_CTX might not have been selected. */
1359 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1360
Adam Langleybb0bd042015-07-01 16:21:03 -07001361 return 1;
1362}
1363
1364static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001365 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1366 ssl->ctx->ocsp_response_length == 0) {
1367 return 1;
1368 }
1369
1370 ssl->s3->tmp.certificate_status_expected = 1;
1371
1372 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1373 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001374}
1375
1376
Adam Langley97dfcbf2015-07-01 18:35:20 -07001377/* Next protocol negotiation.
1378 *
1379 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1380
1381static void ext_npn_init(SSL *ssl) {
1382 ssl->s3->next_proto_neg_seen = 0;
1383}
1384
1385static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1386 if (ssl->s3->initial_handshake_complete ||
1387 ssl->ctx->next_proto_select_cb == NULL ||
1388 SSL_IS_DTLS(ssl)) {
1389 return 1;
1390 }
1391
1392 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1393 !CBB_add_u16(out, 0 /* length */)) {
1394 return 0;
1395 }
1396
1397 return 1;
1398}
1399
1400static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1401 CBS *contents) {
1402 if (contents == NULL) {
1403 return 1;
1404 }
1405
1406 /* If any of these are false then we should never have sent the NPN
1407 * extension in the ClientHello and thus this function should never have been
1408 * called. */
1409 assert(!ssl->s3->initial_handshake_complete);
1410 assert(!SSL_IS_DTLS(ssl));
1411 assert(ssl->ctx->next_proto_select_cb != NULL);
1412
David Benjamin76c2efc2015-08-31 14:24:29 -04001413 if (ssl->s3->alpn_selected != NULL) {
1414 /* NPN and ALPN may not be negotiated in the same connection. */
1415 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1416 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1417 return 0;
1418 }
1419
Adam Langley97dfcbf2015-07-01 18:35:20 -07001420 const uint8_t *const orig_contents = CBS_data(contents);
1421 const size_t orig_len = CBS_len(contents);
1422
1423 while (CBS_len(contents) != 0) {
1424 CBS proto;
1425 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1426 CBS_len(&proto) == 0) {
1427 return 0;
1428 }
1429 }
1430
1431 uint8_t *selected;
1432 uint8_t selected_len;
1433 if (ssl->ctx->next_proto_select_cb(
1434 ssl, &selected, &selected_len, orig_contents, orig_len,
1435 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1436 *out_alert = SSL_AD_INTERNAL_ERROR;
1437 return 0;
1438 }
1439
1440 OPENSSL_free(ssl->next_proto_negotiated);
1441 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1442 if (ssl->next_proto_negotiated == NULL) {
1443 *out_alert = SSL_AD_INTERNAL_ERROR;
1444 return 0;
1445 }
1446
1447 ssl->next_proto_negotiated_len = selected_len;
1448 ssl->s3->next_proto_neg_seen = 1;
1449
1450 return 1;
1451}
1452
1453static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1454 CBS *contents) {
1455 if (contents != NULL && CBS_len(contents) != 0) {
1456 return 0;
1457 }
1458
1459 if (contents == NULL ||
1460 ssl->s3->initial_handshake_complete ||
1461 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1462 * afterwards, parsing the ALPN extension will clear
1463 * |next_proto_neg_seen|. */
1464 ssl->s3->alpn_selected != NULL ||
1465 ssl->ctx->next_protos_advertised_cb == NULL ||
1466 SSL_IS_DTLS(ssl)) {
1467 return 1;
1468 }
1469
1470 ssl->s3->next_proto_neg_seen = 1;
1471 return 1;
1472}
1473
1474static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1475 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1476 * parsed. */
1477 if (!ssl->s3->next_proto_neg_seen) {
1478 return 1;
1479 }
1480
1481 const uint8_t *npa;
1482 unsigned npa_len;
1483
1484 if (ssl->ctx->next_protos_advertised_cb(
1485 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1486 SSL_TLSEXT_ERR_OK) {
1487 ssl->s3->next_proto_neg_seen = 0;
1488 return 1;
1489 }
1490
1491 CBB contents;
1492 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1493 !CBB_add_u16_length_prefixed(out, &contents) ||
1494 !CBB_add_bytes(&contents, npa, npa_len) ||
1495 !CBB_flush(out)) {
1496 return 0;
1497 }
1498
1499 return 1;
1500}
1501
1502
Adam Langleyab8d87d2015-07-10 12:21:39 -07001503/* Signed certificate timestamps.
1504 *
1505 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1506
1507static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1508 if (!ssl->signed_cert_timestamps_enabled) {
1509 return 1;
1510 }
1511
1512 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1513 !CBB_add_u16(out, 0 /* length */)) {
1514 return 0;
1515 }
1516
1517 return 1;
1518}
1519
1520static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1521 CBS *contents) {
1522 if (contents == NULL) {
1523 return 1;
1524 }
1525
1526 /* If this is false then we should never have sent the SCT extension in the
1527 * ClientHello and thus this function should never have been called. */
1528 assert(ssl->signed_cert_timestamps_enabled);
1529
1530 if (CBS_len(contents) == 0) {
1531 *out_alert = SSL_AD_DECODE_ERROR;
1532 return 0;
1533 }
1534
1535 /* Session resumption uses the original session information. */
1536 if (!ssl->hit &&
1537 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1538 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1539 *out_alert = SSL_AD_INTERNAL_ERROR;
1540 return 0;
1541 }
1542
1543 return 1;
1544}
1545
1546static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1547 CBS *contents) {
1548 /* The SCT extension is not supported as a server. */
1549 return 1;
1550}
1551
1552static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1553 /* The SCT extension is not supported as a server. */
1554 return 1;
1555}
1556
1557
Adam Langleyf18e4532015-07-10 13:39:53 -07001558/* Application-level Protocol Negotiation.
1559 *
1560 * https://tools.ietf.org/html/rfc7301 */
1561
1562static void ext_alpn_init(SSL *ssl) {
1563 OPENSSL_free(ssl->s3->alpn_selected);
1564 ssl->s3->alpn_selected = NULL;
1565}
1566
1567static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1568 if (ssl->alpn_client_proto_list == NULL ||
1569 ssl->s3->initial_handshake_complete) {
1570 return 1;
1571 }
1572
1573 CBB contents, proto_list;
1574 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1575 !CBB_add_u16_length_prefixed(out, &contents) ||
1576 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1577 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1578 ssl->alpn_client_proto_list_len) ||
1579 !CBB_flush(out)) {
1580 return 0;
1581 }
1582
1583 return 1;
1584}
1585
1586static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1587 CBS *contents) {
1588 if (contents == NULL) {
1589 return 1;
1590 }
1591
1592 assert(!ssl->s3->initial_handshake_complete);
1593 assert(ssl->alpn_client_proto_list != NULL);
1594
David Benjamin76c2efc2015-08-31 14:24:29 -04001595 if (ssl->s3->next_proto_neg_seen) {
1596 /* NPN and ALPN may not be negotiated in the same connection. */
1597 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1598 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1599 return 0;
1600 }
1601
Adam Langleyf18e4532015-07-10 13:39:53 -07001602 /* The extension data consists of a ProtocolNameList which must have
1603 * exactly one ProtocolName. Each of these is length-prefixed. */
1604 CBS protocol_name_list, protocol_name;
1605 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1606 CBS_len(contents) != 0 ||
1607 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1608 /* Empty protocol names are forbidden. */
1609 CBS_len(&protocol_name) == 0 ||
1610 CBS_len(&protocol_name_list) != 0) {
1611 return 0;
1612 }
1613
1614 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1615 &ssl->s3->alpn_selected_len)) {
1616 *out_alert = SSL_AD_INTERNAL_ERROR;
1617 return 0;
1618 }
1619
1620 return 1;
1621}
1622
1623static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1624 CBS *contents) {
1625 if (contents == NULL) {
1626 return 1;
1627 }
1628
1629 if (ssl->ctx->alpn_select_cb == NULL ||
1630 ssl->s3->initial_handshake_complete) {
1631 return 1;
1632 }
1633
1634 /* ALPN takes precedence over NPN. */
1635 ssl->s3->next_proto_neg_seen = 0;
1636
1637 CBS protocol_name_list;
1638 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1639 CBS_len(contents) != 0 ||
1640 CBS_len(&protocol_name_list) < 2) {
1641 return 0;
1642 }
1643
1644 /* Validate the protocol list. */
1645 CBS protocol_name_list_copy = protocol_name_list;
1646 while (CBS_len(&protocol_name_list_copy) > 0) {
1647 CBS protocol_name;
1648
1649 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1650 /* Empty protocol names are forbidden. */
1651 CBS_len(&protocol_name) == 0) {
1652 return 0;
1653 }
1654 }
1655
1656 const uint8_t *selected;
1657 uint8_t selected_len;
1658 if (ssl->ctx->alpn_select_cb(
1659 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1660 CBS_len(&protocol_name_list),
1661 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1662 OPENSSL_free(ssl->s3->alpn_selected);
1663 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1664 if (ssl->s3->alpn_selected == NULL) {
1665 *out_alert = SSL_AD_INTERNAL_ERROR;
1666 return 0;
1667 }
1668 ssl->s3->alpn_selected_len = selected_len;
1669 }
1670
1671 return 1;
1672}
1673
1674static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1675 if (ssl->s3->alpn_selected == NULL) {
1676 return 1;
1677 }
1678
1679 CBB contents, proto_list, proto;
1680 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1681 !CBB_add_u16_length_prefixed(out, &contents) ||
1682 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1683 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1684 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1685 !CBB_flush(out)) {
1686 return 0;
1687 }
1688
1689 return 1;
1690}
1691
1692
Adam Langley49c7af12015-07-10 14:33:46 -07001693/* Channel ID.
1694 *
1695 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1696
1697static void ext_channel_id_init(SSL *ssl) {
1698 ssl->s3->tlsext_channel_id_valid = 0;
1699}
1700
1701static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1702 if (!ssl->tlsext_channel_id_enabled ||
1703 SSL_IS_DTLS(ssl)) {
1704 return 1;
1705 }
1706
1707 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1708 !CBB_add_u16(out, 0 /* length */)) {
1709 return 0;
1710 }
1711
1712 return 1;
1713}
1714
1715static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1716 CBS *contents) {
1717 if (contents == NULL) {
1718 return 1;
1719 }
1720
1721 assert(!SSL_IS_DTLS(ssl));
1722 assert(ssl->tlsext_channel_id_enabled);
1723
1724 if (CBS_len(contents) != 0) {
1725 return 0;
1726 }
1727
1728 ssl->s3->tlsext_channel_id_valid = 1;
1729 return 1;
1730}
1731
1732static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1733 CBS *contents) {
1734 if (contents == NULL ||
1735 !ssl->tlsext_channel_id_enabled ||
1736 SSL_IS_DTLS(ssl)) {
1737 return 1;
1738 }
1739
1740 if (CBS_len(contents) != 0) {
1741 return 0;
1742 }
1743
1744 ssl->s3->tlsext_channel_id_valid = 1;
1745 return 1;
1746}
1747
1748static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1749 if (!ssl->s3->tlsext_channel_id_valid) {
1750 return 1;
1751 }
1752
1753 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1754 !CBB_add_u16(out, 0 /* length */)) {
1755 return 0;
1756 }
1757
1758 return 1;
1759}
1760
Adam Langley391250d2015-07-15 19:06:07 -07001761
1762/* Secure Real-time Transport Protocol (SRTP) extension.
1763 *
1764 * https://tools.ietf.org/html/rfc5764 */
1765
Adam Langley391250d2015-07-15 19:06:07 -07001766
1767static void ext_srtp_init(SSL *ssl) {
1768 ssl->srtp_profile = NULL;
1769}
1770
1771static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1772 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1773 if (profiles == NULL) {
1774 return 1;
1775 }
1776 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1777 if (num_profiles == 0) {
1778 return 1;
1779 }
1780
1781 CBB contents, profile_ids;
1782 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1783 !CBB_add_u16_length_prefixed(out, &contents) ||
1784 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1785 return 0;
1786 }
1787
1788 size_t i;
1789 for (i = 0; i < num_profiles; i++) {
1790 if (!CBB_add_u16(&profile_ids,
1791 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1792 return 0;
1793 }
1794 }
1795
1796 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1797 !CBB_flush(out)) {
1798 return 0;
1799 }
1800
1801 return 1;
1802}
1803
1804static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1805 CBS *contents) {
1806 if (contents == NULL) {
1807 return 1;
1808 }
1809
1810 /* The extension consists of a u16-prefixed profile ID list containing a
1811 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1812 *
1813 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1814 CBS profile_ids, srtp_mki;
1815 uint16_t profile_id;
1816 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1817 !CBS_get_u16(&profile_ids, &profile_id) ||
1818 CBS_len(&profile_ids) != 0 ||
1819 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1820 CBS_len(contents) != 0) {
1821 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1822 return 0;
1823 }
1824
1825 if (CBS_len(&srtp_mki) != 0) {
1826 /* Must be no MKI, since we never offer one. */
1827 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1828 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1829 return 0;
1830 }
1831
1832 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1833
1834 /* Check to see if the server gave us something we support (and presumably
1835 * offered). */
1836 size_t i;
1837 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1838 const SRTP_PROTECTION_PROFILE *profile =
1839 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1840
1841 if (profile->id == profile_id) {
1842 ssl->srtp_profile = profile;
1843 return 1;
1844 }
1845 }
1846
1847 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1848 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1849 return 0;
1850}
1851
1852static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1853 CBS *contents) {
1854 if (contents == NULL) {
1855 return 1;
1856 }
1857
1858 CBS profile_ids, srtp_mki;
1859 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1860 CBS_len(&profile_ids) < 2 ||
1861 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1862 CBS_len(contents) != 0) {
1863 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1864 return 0;
1865 }
1866 /* Discard the MKI value for now. */
1867
1868 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1869 SSL_get_srtp_profiles(ssl);
1870
1871 /* Pick the server's most preferred profile. */
1872 size_t i;
1873 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1874 const SRTP_PROTECTION_PROFILE *server_profile =
1875 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1876
1877 CBS profile_ids_tmp;
1878 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1879
1880 while (CBS_len(&profile_ids_tmp) > 0) {
1881 uint16_t profile_id;
1882 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1883 return 0;
1884 }
1885
1886 if (server_profile->id == profile_id) {
1887 ssl->srtp_profile = server_profile;
1888 return 1;
1889 }
1890 }
1891 }
1892
1893 return 1;
1894}
1895
1896static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1897 if (ssl->srtp_profile == NULL) {
1898 return 1;
1899 }
1900
1901 CBB contents, profile_ids;
1902 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1903 !CBB_add_u16_length_prefixed(out, &contents) ||
1904 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1905 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1906 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1907 !CBB_flush(out)) {
1908 return 0;
1909 }
1910
1911 return 1;
1912}
1913
Adam Langleybdd5d662015-07-20 16:19:08 -07001914
1915/* EC point formats.
1916 *
1917 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1918
1919static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1920 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1921 return 0;
1922 }
1923
1924 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1925
1926 size_t i;
1927 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1928 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1929
1930 const uint32_t alg_k = cipher->algorithm_mkey;
1931 const uint32_t alg_a = cipher->algorithm_auth;
1932 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1933 return 1;
1934 }
1935 }
1936
1937 return 0;
1938}
1939
Adam Langleybdd5d662015-07-20 16:19:08 -07001940static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001941 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001942 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1943 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001944 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1945 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001946 !CBB_flush(out)) {
1947 return 0;
1948 }
1949
1950 return 1;
1951}
1952
1953static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1954 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1955 return 1;
1956 }
1957
1958 return ext_ec_point_add_extension(ssl, out);
1959}
1960
1961static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1962 CBS *contents) {
1963 if (contents == NULL) {
1964 return 1;
1965 }
1966
1967 CBS ec_point_format_list;
1968 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1969 CBS_len(contents) != 0) {
1970 return 0;
1971 }
1972
David Benjaminfc059942015-07-30 23:01:59 -04001973 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1974 * point format. */
1975 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1976 CBS_len(&ec_point_format_list)) == NULL) {
1977 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001978 return 0;
1979 }
1980
1981 return 1;
1982}
1983
1984static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1985 CBS *contents) {
1986 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1987}
1988
1989static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1990 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1991 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001992 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001993
1994 if (!using_ecc) {
1995 return 1;
1996 }
1997
1998 return ext_ec_point_add_extension(ssl, out);
1999}
2000
Adam Langley273d49c2015-07-20 16:38:52 -07002001
2002/* EC supported curves.
2003 *
2004 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2005
2006static void ext_ec_curves_init(SSL *ssl) {
2007 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2008 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2009 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2010}
2011
2012static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2013 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2014 return 1;
2015 }
2016
2017 CBB contents, curves_bytes;
2018 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2019 !CBB_add_u16_length_prefixed(out, &contents) ||
2020 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2021 return 0;
2022 }
2023
2024 const uint16_t *curves;
2025 size_t curves_len;
2026 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2027
2028 size_t i;
2029 for (i = 0; i < curves_len; i++) {
2030 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2031 return 0;
2032 }
2033 }
2034
2035 return CBB_flush(out);
2036}
2037
2038static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2039 CBS *contents) {
2040 /* This extension is not expected to be echoed by servers and is ignored. */
2041 return 1;
2042}
2043
2044static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2045 CBS *contents) {
2046 if (contents == NULL) {
2047 return 1;
2048 }
2049
2050 CBS elliptic_curve_list;
2051 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2052 CBS_len(&elliptic_curve_list) == 0 ||
2053 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2054 CBS_len(contents) != 0) {
2055 return 0;
2056 }
2057
2058 ssl->s3->tmp.peer_ellipticcurvelist =
2059 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2060
2061 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2062 *out_alert = SSL_AD_INTERNAL_ERROR;
2063 return 0;
2064 }
2065
2066 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2067 size_t i;
2068 for (i = 0; i < num_curves; i++) {
2069 if (!CBS_get_u16(&elliptic_curve_list,
2070 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2071 goto err;
2072 }
2073 }
2074
2075 assert(CBS_len(&elliptic_curve_list) == 0);
2076 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2077
2078 return 1;
2079
2080err:
2081 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2082 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2083 *out_alert = SSL_AD_INTERNAL_ERROR;
2084 return 0;
2085}
2086
2087static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2088 /* Servers don't echo this extension. */
2089 return 1;
2090}
2091
2092
Adam Langley614c66a2015-06-12 15:26:58 -07002093/* kExtensions contains all the supported extensions. */
2094static const struct tls_extension kExtensions[] = {
2095 {
Adam Langley5021b222015-06-12 18:27:58 -07002096 /* The renegotiation extension must always be at index zero because the
2097 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2098 * sent as an SCSV. */
2099 TLSEXT_TYPE_renegotiate,
2100 NULL,
2101 ext_ri_add_clienthello,
2102 ext_ri_parse_serverhello,
2103 ext_ri_parse_clienthello,
2104 ext_ri_add_serverhello,
2105 },
2106 {
Adam Langley614c66a2015-06-12 15:26:58 -07002107 TLSEXT_TYPE_server_name,
2108 ext_sni_init,
2109 ext_sni_add_clienthello,
2110 ext_sni_parse_serverhello,
2111 ext_sni_parse_clienthello,
2112 ext_sni_add_serverhello,
2113 },
Adam Langley0a056712015-07-01 15:03:33 -07002114 {
2115 TLSEXT_TYPE_extended_master_secret,
2116 ext_ems_init,
2117 ext_ems_add_clienthello,
2118 ext_ems_parse_serverhello,
2119 ext_ems_parse_clienthello,
2120 ext_ems_add_serverhello,
2121 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002122 {
2123 TLSEXT_TYPE_session_ticket,
2124 NULL,
2125 ext_ticket_add_clienthello,
2126 ext_ticket_parse_serverhello,
2127 ext_ticket_parse_clienthello,
2128 ext_ticket_add_serverhello,
2129 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002130 {
2131 TLSEXT_TYPE_signature_algorithms,
2132 NULL,
2133 ext_sigalgs_add_clienthello,
2134 ext_sigalgs_parse_serverhello,
2135 ext_sigalgs_parse_clienthello,
2136 ext_sigalgs_add_serverhello,
2137 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002138 {
2139 TLSEXT_TYPE_status_request,
2140 ext_ocsp_init,
2141 ext_ocsp_add_clienthello,
2142 ext_ocsp_parse_serverhello,
2143 ext_ocsp_parse_clienthello,
2144 ext_ocsp_add_serverhello,
2145 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002146 {
2147 TLSEXT_TYPE_next_proto_neg,
2148 ext_npn_init,
2149 ext_npn_add_clienthello,
2150 ext_npn_parse_serverhello,
2151 ext_npn_parse_clienthello,
2152 ext_npn_add_serverhello,
2153 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002154 {
2155 TLSEXT_TYPE_certificate_timestamp,
2156 NULL,
2157 ext_sct_add_clienthello,
2158 ext_sct_parse_serverhello,
2159 ext_sct_parse_clienthello,
2160 ext_sct_add_serverhello,
2161 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002162 {
2163 TLSEXT_TYPE_application_layer_protocol_negotiation,
2164 ext_alpn_init,
2165 ext_alpn_add_clienthello,
2166 ext_alpn_parse_serverhello,
2167 ext_alpn_parse_clienthello,
2168 ext_alpn_add_serverhello,
2169 },
Adam Langley49c7af12015-07-10 14:33:46 -07002170 {
2171 TLSEXT_TYPE_channel_id,
2172 ext_channel_id_init,
2173 ext_channel_id_add_clienthello,
2174 ext_channel_id_parse_serverhello,
2175 ext_channel_id_parse_clienthello,
2176 ext_channel_id_add_serverhello,
2177 },
Adam Langley391250d2015-07-15 19:06:07 -07002178 {
2179 TLSEXT_TYPE_srtp,
2180 ext_srtp_init,
2181 ext_srtp_add_clienthello,
2182 ext_srtp_parse_serverhello,
2183 ext_srtp_parse_clienthello,
2184 ext_srtp_add_serverhello,
2185 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002186 {
2187 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002188 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002189 ext_ec_point_add_clienthello,
2190 ext_ec_point_parse_serverhello,
2191 ext_ec_point_parse_clienthello,
2192 ext_ec_point_add_serverhello,
2193 },
Adam Langley273d49c2015-07-20 16:38:52 -07002194 {
2195 TLSEXT_TYPE_elliptic_curves,
2196 ext_ec_curves_init,
2197 ext_ec_curves_add_clienthello,
2198 ext_ec_curves_parse_serverhello,
2199 ext_ec_curves_parse_clienthello,
2200 ext_ec_curves_add_serverhello,
2201 },
Adam Langley614c66a2015-06-12 15:26:58 -07002202};
2203
2204#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2205
Adam Langley4cfa96b2015-07-01 11:56:55 -07002206OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2207 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002208 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002209OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2210 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2211 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002212 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002213
Adam Langley614c66a2015-06-12 15:26:58 -07002214static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2215 uint16_t value) {
2216 unsigned i;
2217 for (i = 0; i < kNumExtensions; i++) {
2218 if (kExtensions[i].value == value) {
2219 *out_index = i;
2220 return &kExtensions[i];
2221 }
2222 }
2223
2224 return NULL;
2225}
2226
Adam Langley09505632015-07-30 18:10:13 -07002227int SSL_extension_supported(unsigned extension_value) {
2228 uint32_t index;
2229 return extension_value == TLSEXT_TYPE_padding ||
2230 tls_extension_find(&index, extension_value) != NULL;
2231}
2232
Adam Langleyb0c235e2014-06-20 12:00:00 -07002233/* header_len is the length of the ClientHello header written so far, used to
2234 * compute padding. It does not include the record header. Pass 0 if no padding
2235 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002236uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2237 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2239 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002240 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langley33ad2b52015-07-20 17:43:53 -07002243 CBB cbb, extensions;
2244 CBB_zero(&cbb);
2245 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2246 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2247 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langley614c66a2015-06-12 15:26:58 -07002250 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002251 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langley614c66a2015-06-12 15:26:58 -07002253 size_t i;
2254 for (i = 0; i < kNumExtensions; i++) {
2255 if (kExtensions[i].init != NULL) {
2256 kExtensions[i].init(s);
2257 }
2258 }
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langley614c66a2015-06-12 15:26:58 -07002260 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002261 const size_t len_before = CBB_len(&extensions);
2262 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2263 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2264 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2265 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langley33ad2b52015-07-20 17:43:53 -07002268 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002269 s->s3->tmp.extensions.sent |= (1u << i);
2270 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 }
Adam Langley75712922014-10-10 16:23:43 -07002272
Adam Langley09505632015-07-30 18:10:13 -07002273 if (!custom_ext_add_clienthello(s, &extensions)) {
2274 goto err;
2275 }
2276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002278 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 if (header_len > 0xff && header_len < 0x200) {
2280 /* Add padding to workaround bugs in F5 terminators. See
2281 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2282 *
2283 * NB: because this code works out the length of all existing extensions
2284 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002285 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 /* Extensions take at least four bytes to encode. Always include least
2287 * one byte of data if including the extension. WebSphere Application
2288 * Server 7.0 is intolerant to the last extension being zero-length. */
2289 if (padding_len >= 4 + 1) {
2290 padding_len -= 4;
2291 } else {
2292 padding_len = 1;
2293 }
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langley33ad2b52015-07-20 17:43:53 -07002295 uint8_t *padding_bytes;
2296 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2297 !CBB_add_u16(&extensions, padding_len) ||
2298 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2299 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 }
Adam Langley75712922014-10-10 16:23:43 -07002301
Adam Langley33ad2b52015-07-20 17:43:53 -07002302 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002303 }
2304 }
Adam Langley75712922014-10-10 16:23:43 -07002305
Adam Langley33ad2b52015-07-20 17:43:53 -07002306 if (!CBB_flush(&cbb)) {
2307 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002308 }
Adam Langley95c29f32014-06-20 12:00:00 -07002309
Adam Langley33ad2b52015-07-20 17:43:53 -07002310 uint8_t *ret = buf;
2311 const size_t cbb_len = CBB_len(&cbb);
2312 /* If only two bytes have been written then the extensions are actually empty
2313 * and those two bytes are the zero length. In that case, we don't bother
2314 * sending the extensions length. */
2315 if (cbb_len > 2) {
2316 ret += cbb_len;
2317 }
2318
2319 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002321
2322err:
2323 CBB_cleanup(&cbb);
2324 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2325 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002326}
Adam Langley95c29f32014-06-20 12:00:00 -07002327
Adam Langley614c66a2015-06-12 15:26:58 -07002328uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2329 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2331 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002332 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 }
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langley33ad2b52015-07-20 17:43:53 -07002335 CBB cbb, extensions;
2336 CBB_zero(&cbb);
2337 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2338 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2339 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002340 }
2341
2342 unsigned i;
2343 for (i = 0; i < kNumExtensions; i++) {
2344 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2345 /* Don't send extensions that were not received. */
2346 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 }
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langley33ad2b52015-07-20 17:43:53 -07002349 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2350 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2351 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2352 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002353 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002354 }
Adam Langley95c29f32014-06-20 12:00:00 -07002355
Adam Langley09505632015-07-30 18:10:13 -07002356 if (!custom_ext_add_serverhello(s, &extensions)) {
2357 goto err;
2358 }
2359
Adam Langley33ad2b52015-07-20 17:43:53 -07002360 if (!CBB_flush(&cbb)) {
2361 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002362 }
2363
Adam Langley33ad2b52015-07-20 17:43:53 -07002364 uint8_t *ret = buf;
2365 const size_t cbb_len = CBB_len(&cbb);
2366 /* If only two bytes have been written then the extensions are actually empty
2367 * and those two bytes are the zero length. In that case, we don't bother
2368 * sending the extensions length. */
2369 if (cbb_len > 2) {
2370 ret += cbb_len;
2371 }
2372
2373 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002375
2376err:
2377 CBB_cleanup(&cbb);
2378 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2379 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002380}
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002383 size_t i;
2384 for (i = 0; i < kNumExtensions; i++) {
2385 if (kExtensions[i].init != NULL) {
2386 kExtensions[i].init(s);
2387 }
2388 }
2389
2390 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002391 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002392 /* The renegotiation extension must always be at index zero because the
2393 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2394 * sent as an SCSV. */
2395 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002396
Adam Langleyfcf25832014-12-18 17:42:32 -08002397 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002398 if (CBS_len(cbs) != 0) {
2399 /* Decode the extensions block and check it is valid. */
2400 CBS extensions;
2401 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2402 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 *out_alert = SSL_AD_DECODE_ERROR;
2404 return 0;
2405 }
Adam Langley95c29f32014-06-20 12:00:00 -07002406
Adam Langley33ad2b52015-07-20 17:43:53 -07002407 while (CBS_len(&extensions) != 0) {
2408 uint16_t type;
2409 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langley33ad2b52015-07-20 17:43:53 -07002411 /* Decode the next extension. */
2412 if (!CBS_get_u16(&extensions, &type) ||
2413 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2414 *out_alert = SSL_AD_DECODE_ERROR;
2415 return 0;
2416 }
2417
2418 unsigned ext_index;
2419 const struct tls_extension *const ext =
2420 tls_extension_find(&ext_index, type);
2421
2422 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002423 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2424 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2425 return 0;
2426 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002427 continue;
2428 }
2429
Adam Langley614c66a2015-06-12 15:26:58 -07002430 s->s3->tmp.extensions.received |= (1u << ext_index);
2431 uint8_t alert = SSL_AD_DECODE_ERROR;
2432 if (!ext->parse_clienthello(s, &alert, &extension)) {
2433 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002434 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2435 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002436 return 0;
2437 }
Adam Langley614c66a2015-06-12 15:26:58 -07002438 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002439 }
Adam Langley75712922014-10-10 16:23:43 -07002440
Adam Langley614c66a2015-06-12 15:26:58 -07002441 for (i = 0; i < kNumExtensions; i++) {
2442 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2443 /* Extension wasn't observed so call the callback with a NULL
2444 * parameter. */
2445 uint8_t alert = SSL_AD_DECODE_ERROR;
2446 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002447 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2448 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002449 *out_alert = alert;
2450 return 0;
2451 }
2452 }
2453 }
2454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 return 1;
2456}
Adam Langley95c29f32014-06-20 12:00:00 -07002457
Adam Langleyfcf25832014-12-18 17:42:32 -08002458int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2459 int alert = -1;
2460 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2461 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2462 return 0;
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002466 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002467 return 0;
2468 }
Adam Langley95c29f32014-06-20 12:00:00 -07002469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 return 1;
2471}
Adam Langley95c29f32014-06-20 12:00:00 -07002472
Adam Langleyfcf25832014-12-18 17:42:32 -08002473static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002474 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002475 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002476
Adam Langley33ad2b52015-07-20 17:43:53 -07002477 if (CBS_len(cbs) != 0) {
2478 /* Decode the extensions block and check it is valid. */
2479 CBS extensions;
2480 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2481 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002482 *out_alert = SSL_AD_DECODE_ERROR;
2483 return 0;
2484 }
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langley614c66a2015-06-12 15:26:58 -07002486
Adam Langley33ad2b52015-07-20 17:43:53 -07002487 while (CBS_len(&extensions) != 0) {
2488 uint16_t type;
2489 CBS extension;
2490
2491 /* Decode the next extension. */
2492 if (!CBS_get_u16(&extensions, &type) ||
2493 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2494 *out_alert = SSL_AD_DECODE_ERROR;
2495 return 0;
2496 }
2497
2498 unsigned ext_index;
2499 const struct tls_extension *const ext =
2500 tls_extension_find(&ext_index, type);
2501
Adam Langley09505632015-07-30 18:10:13 -07002502 if (ext == NULL) {
2503 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2504 return 0;
2505 }
2506 continue;
2507 }
2508
2509 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2510 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002511 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002512 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002513 *out_alert = SSL_AD_DECODE_ERROR;
2514 return 0;
2515 }
David Benjamin03973092014-06-24 23:27:17 -04002516
Adam Langley614c66a2015-06-12 15:26:58 -07002517 received |= (1u << ext_index);
2518
2519 uint8_t alert = SSL_AD_DECODE_ERROR;
2520 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002521 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2522 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002523 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 return 0;
2525 }
Adam Langley614c66a2015-06-12 15:26:58 -07002526 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 }
Adam Langley95c29f32014-06-20 12:00:00 -07002528
Adam Langley33ad2b52015-07-20 17:43:53 -07002529 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002530 for (i = 0; i < kNumExtensions; i++) {
2531 if (!(received & (1u << i))) {
2532 /* Extension wasn't observed so call the callback with a NULL
2533 * parameter. */
2534 uint8_t alert = SSL_AD_DECODE_ERROR;
2535 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002536 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2537 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002538 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 return 0;
2540 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 }
2542 }
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544 return 1;
2545}
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547static int ssl_check_clienthello_tlsext(SSL *s) {
2548 int ret = SSL_TLSEXT_ERR_NOACK;
2549 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2552 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002553
Adam Langleyfcf25832014-12-18 17:42:32 -08002554 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2555 ret = s->ctx->tlsext_servername_callback(s, &al,
2556 s->ctx->tlsext_servername_arg);
2557 } else if (s->initial_ctx != NULL &&
2558 s->initial_ctx->tlsext_servername_callback != 0) {
2559 ret = s->initial_ctx->tlsext_servername_callback(
2560 s, &al, s->initial_ctx->tlsext_servername_arg);
2561 }
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 switch (ret) {
2564 case SSL_TLSEXT_ERR_ALERT_FATAL:
2565 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2566 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 case SSL_TLSEXT_ERR_ALERT_WARNING:
2569 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2570 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002571
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002573 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 default:
2577 return 1;
2578 }
2579}
Adam Langleyed8270a2014-09-02 13:52:56 -07002580
Adam Langleyfcf25832014-12-18 17:42:32 -08002581static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002582 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002583 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2586 ret = s->ctx->tlsext_servername_callback(s, &al,
2587 s->ctx->tlsext_servername_arg);
2588 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002589 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 ret = s->initial_ctx->tlsext_servername_callback(
2591 s, &al, s->initial_ctx->tlsext_servername_arg);
2592 }
Adam Langley95c29f32014-06-20 12:00:00 -07002593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 switch (ret) {
2595 case SSL_TLSEXT_ERR_ALERT_FATAL:
2596 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2597 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 case SSL_TLSEXT_ERR_ALERT_WARNING:
2600 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2601 return 1;
2602
2603 default:
2604 return 1;
2605 }
2606}
2607
2608int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2609 int alert = -1;
2610 if (s->version < SSL3_VERSION) {
2611 return 1;
2612 }
2613
2614 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2615 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2616 return 0;
2617 }
2618
2619 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002620 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 return 0;
2622 }
2623
2624 return 1;
2625}
Adam Langley95c29f32014-06-20 12:00:00 -07002626
David Benjamine3aa1d92015-06-16 15:34:50 -04002627int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2628 int *out_send_ticket, const uint8_t *ticket,
2629 size_t ticket_len, const uint8_t *session_id,
2630 size_t session_id_len) {
2631 int ret = 1; /* Most errors are non-fatal. */
2632 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2633 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002634
David Benjamine3aa1d92015-06-16 15:34:50 -04002635 HMAC_CTX hmac_ctx;
2636 HMAC_CTX_init(&hmac_ctx);
2637 EVP_CIPHER_CTX cipher_ctx;
2638 EVP_CIPHER_CTX_init(&cipher_ctx);
2639
2640 *out_send_ticket = 0;
2641 *out_session = NULL;
2642
2643 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2644 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
2646
David Benjamine3aa1d92015-06-16 15:34:50 -04002647 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002649 *out_send_ticket = 1;
2650 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 }
2652
David Benjaminadcc3952015-04-26 13:07:57 -04002653 /* Ensure there is room for the key name and the largest IV
2654 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2655 * the maximum IV length should be well under the minimum size for the
2656 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002657 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2658 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002660 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002661
David Benjamine3aa1d92015-06-16 15:34:50 -04002662 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2663 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2664 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2665 0 /* decrypt */);
2666 if (cb_ret < 0) {
2667 ret = 0;
2668 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002670 if (cb_ret == 0) {
2671 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002673 if (cb_ret == 2) {
2674 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002675 }
2676 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002677 /* Check the key name matches. */
2678 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2679 SSL_TICKET_KEY_NAME_LEN) != 0) {
2680 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002682 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2683 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002685 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2686 ssl_ctx->tlsext_tick_aes_key, iv)) {
2687 ret = 0;
2688 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 }
2690 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002691 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002692
David Benjamine3aa1d92015-06-16 15:34:50 -04002693 /* Check the MAC at the end of the ticket. */
2694 uint8_t mac[EVP_MAX_MD_SIZE];
2695 size_t mac_len = HMAC_size(&hmac_ctx);
2696 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002697 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002698 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002700 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2701 HMAC_Final(&hmac_ctx, mac, NULL);
2702 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2703 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 }
2705
David Benjamine3aa1d92015-06-16 15:34:50 -04002706 /* Decrypt the session data. */
2707 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2708 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2709 mac_len;
2710 plaintext = OPENSSL_malloc(ciphertext_len);
2711 if (plaintext == NULL) {
2712 ret = 0;
2713 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002715 if (ciphertext_len >= INT_MAX) {
2716 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002718 int len1, len2;
2719 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2720 (int)ciphertext_len) ||
2721 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2722 ERR_clear_error(); /* Don't leave an error on the queue. */
2723 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
2725
David Benjamine3aa1d92015-06-16 15:34:50 -04002726 /* Decode the session. */
2727 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2728 if (session == NULL) {
2729 ERR_clear_error(); /* Don't leave an error on the queue. */
2730 goto done;
2731 }
2732
2733 /* Copy the client's session ID into the new session, to denote the ticket has
2734 * been accepted. */
2735 memcpy(session->session_id, session_id, session_id_len);
2736 session->session_id_length = session_id_len;
2737
2738 *out_session = session;
2739
2740done:
2741 OPENSSL_free(plaintext);
2742 HMAC_CTX_cleanup(&hmac_ctx);
2743 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2744 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
2747/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002748typedef struct {
2749 int nid;
2750 int id;
2751} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Adam Langleyfcf25832014-12-18 17:42:32 -08002753static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2754 {NID_sha1, TLSEXT_hash_sha1},
2755 {NID_sha224, TLSEXT_hash_sha224},
2756 {NID_sha256, TLSEXT_hash_sha256},
2757 {NID_sha384, TLSEXT_hash_sha384},
2758 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002759
Adam Langleyfcf25832014-12-18 17:42:32 -08002760static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2761 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002762
Adam Langleyfcf25832014-12-18 17:42:32 -08002763static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2764 size_t i;
2765 for (i = 0; i < tlen; i++) {
2766 if (table[i].nid == nid) {
2767 return table[i].id;
2768 }
2769 }
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 return -1;
2772}
Adam Langley95c29f32014-06-20 12:00:00 -07002773
David Benjaminb4d65fd2015-05-29 17:11:21 -04002774int tls12_get_sigid(int pkey_type) {
2775 return tls12_find_id(pkey_type, tls12_sig,
2776 sizeof(tls12_sig) / sizeof(tls12_lookup));
2777}
2778
David Benjamind1d80782015-07-05 11:54:09 -04002779int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002781
Adam Langleyfcf25832014-12-18 17:42:32 -08002782 if (!md) {
2783 return 0;
2784 }
Adam Langley95c29f32014-06-20 12:00:00 -07002785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2787 sizeof(tls12_md) / sizeof(tls12_lookup));
2788 if (md_id == -1) {
2789 return 0;
2790 }
Adam Langley95c29f32014-06-20 12:00:00 -07002791
David Benjamind1d80782015-07-05 11:54:09 -04002792 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 if (sig_id == -1) {
2794 return 0;
2795 }
Adam Langley95c29f32014-06-20 12:00:00 -07002796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 p[0] = (uint8_t)md_id;
2798 p[1] = (uint8_t)sig_id;
2799 return 1;
2800}
2801
Adam Langleyfcf25832014-12-18 17:42:32 -08002802const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2803 switch (hash_alg) {
2804 case TLSEXT_hash_md5:
2805 return EVP_md5();
2806
2807 case TLSEXT_hash_sha1:
2808 return EVP_sha1();
2809
2810 case TLSEXT_hash_sha224:
2811 return EVP_sha224();
2812
2813 case TLSEXT_hash_sha256:
2814 return EVP_sha256();
2815
2816 case TLSEXT_hash_sha384:
2817 return EVP_sha384();
2818
2819 case TLSEXT_hash_sha512:
2820 return EVP_sha512();
2821
2822 default:
2823 return NULL;
2824 }
2825}
Adam Langley95c29f32014-06-20 12:00:00 -07002826
David Benjaminec2f27d2014-11-13 19:17:25 -05002827/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2828 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002829static int tls12_get_pkey_type(uint8_t sig_alg) {
2830 switch (sig_alg) {
2831 case TLSEXT_signature_rsa:
2832 return EVP_PKEY_RSA;
2833
2834 case TLSEXT_signature_ecdsa:
2835 return EVP_PKEY_EC;
2836
2837 default:
2838 return -1;
2839 }
2840}
Adam Langley95c29f32014-06-20 12:00:00 -07002841
Adam Langley95c29f32014-06-20 12:00:00 -07002842/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002843static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2844 size_t preflen, const uint8_t *allow,
2845 size_t allowlen) {
2846 const uint8_t *ptmp, *atmp;
2847 size_t i, j, nmatch = 0;
2848
2849 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2850 /* Skip disabled hashes or signature algorithms */
2851 if (tls12_get_hash(ptmp[0]) == NULL ||
2852 tls12_get_pkey_type(ptmp[1]) == -1) {
2853 continue;
2854 }
2855
2856 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2857 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2858 nmatch++;
2859 if (shsig) {
2860 shsig->rhash = ptmp[0];
2861 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 shsig++;
2863 }
2864
2865 break;
2866 }
2867 }
2868 }
2869
2870 return nmatch;
2871}
Adam Langley95c29f32014-06-20 12:00:00 -07002872
2873/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002874static int tls1_set_shared_sigalgs(SSL *s) {
2875 const uint8_t *pref, *allow, *conf;
2876 size_t preflen, allowlen, conflen;
2877 size_t nmatch;
2878 TLS_SIGALGS *salgs = NULL;
2879 CERT *c = s->cert;
2880
David Benjamin2755a3e2015-04-22 16:17:58 -04002881 OPENSSL_free(c->shared_sigalgs);
2882 c->shared_sigalgs = NULL;
2883 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002884
David Benjamin3dd90162015-08-09 11:26:40 -04002885 conflen = tls12_get_psigalgs(s, &conf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002886
2887 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2888 pref = conf;
2889 preflen = conflen;
2890 allow = c->peer_sigalgs;
2891 allowlen = c->peer_sigalgslen;
2892 } else {
2893 allow = conf;
2894 allowlen = conflen;
2895 pref = c->peer_sigalgs;
2896 preflen = c->peer_sigalgslen;
2897 }
2898
2899 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2900 if (!nmatch) {
2901 return 1;
2902 }
2903
2904 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2905 if (!salgs) {
2906 return 0;
2907 }
2908
2909 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2910 c->shared_sigalgs = salgs;
2911 c->shared_sigalgslen = nmatch;
2912 return 1;
2913}
Adam Langley95c29f32014-06-20 12:00:00 -07002914
2915/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002916int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2917 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002918
Adam Langleyfcf25832014-12-18 17:42:32 -08002919 /* Extension ignored for inappropriate versions */
2920 if (!SSL_USE_SIGALGS(s)) {
2921 return 1;
2922 }
David Benjamincd996942014-07-20 16:23:51 -04002923
David Benjamin61c0d4e2015-03-19 14:24:37 -04002924 if (CBS_len(sigalgs) % 2 != 0 ||
2925 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2926 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 return 0;
2928 }
Adam Langley95c29f32014-06-20 12:00:00 -07002929
Adam Langleyfcf25832014-12-18 17:42:32 -08002930 return 1;
2931}
David Benjaminec2f27d2014-11-13 19:17:25 -05002932
David Benjamind1d80782015-07-05 11:54:09 -04002933const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2934 CERT *cert = ssl->cert;
2935 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002937
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002939 for (i = 0; i < cert->shared_sigalgslen; i++) {
2940 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002942 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2943 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002944 continue;
2945 }
2946 return md;
2947 }
Adam Langley95c29f32014-06-20 12:00:00 -07002948
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 /* If no suitable digest may be found, default to SHA-1. */
2950 return EVP_sha1();
2951}
Adam Langley95c29f32014-06-20 12:00:00 -07002952
David Benjamind6a4ae92015-08-06 11:10:51 -04002953int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2954 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002956
2957 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002958 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2959 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002961
David Benjamind6a4ae92015-08-06 11:10:51 -04002962 static const char kClientIDMagic[] = "TLS Channel ID signature";
2963 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2964
2965 if (ssl->hit) {
2966 static const char kResumptionMagic[] = "Resumption";
2967 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2968 if (ssl->session->original_handshake_hash_len == 0) {
2969 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2970 goto err;
2971 }
2972 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2973 ssl->session->original_handshake_hash_len);
2974 }
2975
2976 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2977 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2978 sizeof(handshake_hash));
2979 if (handshake_hash_len < 0) {
2980 goto err;
2981 }
2982 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2983 unsigned len_u;
2984 EVP_DigestFinal_ex(&ctx, out, &len_u);
2985 *out_len = len_u;
2986
2987 ret = 1;
2988
2989err:
2990 EVP_MD_CTX_cleanup(&ctx);
2991 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002992}
Adam Langley1258b6a2014-06-20 12:00:00 -07002993
2994/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2995 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002996int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2997 int digest_len;
2998 /* This function should never be called for a resumed session because the
2999 * handshake hashes that we wish to record are for the original, full
3000 * handshake. */
3001 if (s->hit) {
3002 return -1;
3003 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003004
Adam Langleyfcf25832014-12-18 17:42:32 -08003005 digest_len =
3006 tls1_handshake_digest(s, s->session->original_handshake_hash,
3007 sizeof(s->session->original_handshake_hash));
3008 if (digest_len < 0) {
3009 return -1;
3010 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003011
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003013
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 return 1;
3015}