blob: 36793f7a754b4822285f85e0aa71e311e23f64bf [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) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001548 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001549}
1550
1551static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001552 if (ssl->ctx->signed_cert_timestamp_list_length == 0) {
1553 return 1;
1554 }
1555
1556 CBB contents;
1557 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1558 CBB_add_u16_length_prefixed(out, &contents) &&
1559 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1560 ssl->ctx->signed_cert_timestamp_list_length) &&
1561 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001562}
1563
1564
Adam Langleyf18e4532015-07-10 13:39:53 -07001565/* Application-level Protocol Negotiation.
1566 *
1567 * https://tools.ietf.org/html/rfc7301 */
1568
1569static void ext_alpn_init(SSL *ssl) {
1570 OPENSSL_free(ssl->s3->alpn_selected);
1571 ssl->s3->alpn_selected = NULL;
1572}
1573
1574static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1575 if (ssl->alpn_client_proto_list == NULL ||
1576 ssl->s3->initial_handshake_complete) {
1577 return 1;
1578 }
1579
1580 CBB contents, proto_list;
1581 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1582 !CBB_add_u16_length_prefixed(out, &contents) ||
1583 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1584 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1585 ssl->alpn_client_proto_list_len) ||
1586 !CBB_flush(out)) {
1587 return 0;
1588 }
1589
1590 return 1;
1591}
1592
1593static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1594 CBS *contents) {
1595 if (contents == NULL) {
1596 return 1;
1597 }
1598
1599 assert(!ssl->s3->initial_handshake_complete);
1600 assert(ssl->alpn_client_proto_list != NULL);
1601
David Benjamin76c2efc2015-08-31 14:24:29 -04001602 if (ssl->s3->next_proto_neg_seen) {
1603 /* NPN and ALPN may not be negotiated in the same connection. */
1604 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1605 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1606 return 0;
1607 }
1608
Adam Langleyf18e4532015-07-10 13:39:53 -07001609 /* The extension data consists of a ProtocolNameList which must have
1610 * exactly one ProtocolName. Each of these is length-prefixed. */
1611 CBS protocol_name_list, protocol_name;
1612 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1613 CBS_len(contents) != 0 ||
1614 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1615 /* Empty protocol names are forbidden. */
1616 CBS_len(&protocol_name) == 0 ||
1617 CBS_len(&protocol_name_list) != 0) {
1618 return 0;
1619 }
1620
1621 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1622 &ssl->s3->alpn_selected_len)) {
1623 *out_alert = SSL_AD_INTERNAL_ERROR;
1624 return 0;
1625 }
1626
1627 return 1;
1628}
1629
1630static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1631 CBS *contents) {
1632 if (contents == NULL) {
1633 return 1;
1634 }
1635
1636 if (ssl->ctx->alpn_select_cb == NULL ||
1637 ssl->s3->initial_handshake_complete) {
1638 return 1;
1639 }
1640
1641 /* ALPN takes precedence over NPN. */
1642 ssl->s3->next_proto_neg_seen = 0;
1643
1644 CBS protocol_name_list;
1645 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1646 CBS_len(contents) != 0 ||
1647 CBS_len(&protocol_name_list) < 2) {
1648 return 0;
1649 }
1650
1651 /* Validate the protocol list. */
1652 CBS protocol_name_list_copy = protocol_name_list;
1653 while (CBS_len(&protocol_name_list_copy) > 0) {
1654 CBS protocol_name;
1655
1656 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1657 /* Empty protocol names are forbidden. */
1658 CBS_len(&protocol_name) == 0) {
1659 return 0;
1660 }
1661 }
1662
1663 const uint8_t *selected;
1664 uint8_t selected_len;
1665 if (ssl->ctx->alpn_select_cb(
1666 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1667 CBS_len(&protocol_name_list),
1668 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1669 OPENSSL_free(ssl->s3->alpn_selected);
1670 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1671 if (ssl->s3->alpn_selected == NULL) {
1672 *out_alert = SSL_AD_INTERNAL_ERROR;
1673 return 0;
1674 }
1675 ssl->s3->alpn_selected_len = selected_len;
1676 }
1677
1678 return 1;
1679}
1680
1681static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1682 if (ssl->s3->alpn_selected == NULL) {
1683 return 1;
1684 }
1685
1686 CBB contents, proto_list, proto;
1687 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1688 !CBB_add_u16_length_prefixed(out, &contents) ||
1689 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1690 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1691 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1692 !CBB_flush(out)) {
1693 return 0;
1694 }
1695
1696 return 1;
1697}
1698
1699
Adam Langley49c7af12015-07-10 14:33:46 -07001700/* Channel ID.
1701 *
1702 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1703
1704static void ext_channel_id_init(SSL *ssl) {
1705 ssl->s3->tlsext_channel_id_valid = 0;
1706}
1707
1708static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1709 if (!ssl->tlsext_channel_id_enabled ||
1710 SSL_IS_DTLS(ssl)) {
1711 return 1;
1712 }
1713
1714 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1715 !CBB_add_u16(out, 0 /* length */)) {
1716 return 0;
1717 }
1718
1719 return 1;
1720}
1721
1722static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1723 CBS *contents) {
1724 if (contents == NULL) {
1725 return 1;
1726 }
1727
1728 assert(!SSL_IS_DTLS(ssl));
1729 assert(ssl->tlsext_channel_id_enabled);
1730
1731 if (CBS_len(contents) != 0) {
1732 return 0;
1733 }
1734
1735 ssl->s3->tlsext_channel_id_valid = 1;
1736 return 1;
1737}
1738
1739static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1740 CBS *contents) {
1741 if (contents == NULL ||
1742 !ssl->tlsext_channel_id_enabled ||
1743 SSL_IS_DTLS(ssl)) {
1744 return 1;
1745 }
1746
1747 if (CBS_len(contents) != 0) {
1748 return 0;
1749 }
1750
1751 ssl->s3->tlsext_channel_id_valid = 1;
1752 return 1;
1753}
1754
1755static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1756 if (!ssl->s3->tlsext_channel_id_valid) {
1757 return 1;
1758 }
1759
1760 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1761 !CBB_add_u16(out, 0 /* length */)) {
1762 return 0;
1763 }
1764
1765 return 1;
1766}
1767
Adam Langley391250d2015-07-15 19:06:07 -07001768
1769/* Secure Real-time Transport Protocol (SRTP) extension.
1770 *
1771 * https://tools.ietf.org/html/rfc5764 */
1772
Adam Langley391250d2015-07-15 19:06:07 -07001773
1774static void ext_srtp_init(SSL *ssl) {
1775 ssl->srtp_profile = NULL;
1776}
1777
1778static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1779 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1780 if (profiles == NULL) {
1781 return 1;
1782 }
1783 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1784 if (num_profiles == 0) {
1785 return 1;
1786 }
1787
1788 CBB contents, profile_ids;
1789 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1790 !CBB_add_u16_length_prefixed(out, &contents) ||
1791 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1792 return 0;
1793 }
1794
1795 size_t i;
1796 for (i = 0; i < num_profiles; i++) {
1797 if (!CBB_add_u16(&profile_ids,
1798 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1799 return 0;
1800 }
1801 }
1802
1803 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1804 !CBB_flush(out)) {
1805 return 0;
1806 }
1807
1808 return 1;
1809}
1810
1811static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1812 CBS *contents) {
1813 if (contents == NULL) {
1814 return 1;
1815 }
1816
1817 /* The extension consists of a u16-prefixed profile ID list containing a
1818 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1819 *
1820 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1821 CBS profile_ids, srtp_mki;
1822 uint16_t profile_id;
1823 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1824 !CBS_get_u16(&profile_ids, &profile_id) ||
1825 CBS_len(&profile_ids) != 0 ||
1826 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1827 CBS_len(contents) != 0) {
1828 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1829 return 0;
1830 }
1831
1832 if (CBS_len(&srtp_mki) != 0) {
1833 /* Must be no MKI, since we never offer one. */
1834 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1835 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1836 return 0;
1837 }
1838
1839 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1840
1841 /* Check to see if the server gave us something we support (and presumably
1842 * offered). */
1843 size_t i;
1844 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1845 const SRTP_PROTECTION_PROFILE *profile =
1846 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1847
1848 if (profile->id == profile_id) {
1849 ssl->srtp_profile = profile;
1850 return 1;
1851 }
1852 }
1853
1854 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1855 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1856 return 0;
1857}
1858
1859static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1860 CBS *contents) {
1861 if (contents == NULL) {
1862 return 1;
1863 }
1864
1865 CBS profile_ids, srtp_mki;
1866 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1867 CBS_len(&profile_ids) < 2 ||
1868 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1869 CBS_len(contents) != 0) {
1870 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1871 return 0;
1872 }
1873 /* Discard the MKI value for now. */
1874
1875 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1876 SSL_get_srtp_profiles(ssl);
1877
1878 /* Pick the server's most preferred profile. */
1879 size_t i;
1880 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1881 const SRTP_PROTECTION_PROFILE *server_profile =
1882 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1883
1884 CBS profile_ids_tmp;
1885 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1886
1887 while (CBS_len(&profile_ids_tmp) > 0) {
1888 uint16_t profile_id;
1889 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1890 return 0;
1891 }
1892
1893 if (server_profile->id == profile_id) {
1894 ssl->srtp_profile = server_profile;
1895 return 1;
1896 }
1897 }
1898 }
1899
1900 return 1;
1901}
1902
1903static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1904 if (ssl->srtp_profile == NULL) {
1905 return 1;
1906 }
1907
1908 CBB contents, profile_ids;
1909 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1910 !CBB_add_u16_length_prefixed(out, &contents) ||
1911 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1912 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1913 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1914 !CBB_flush(out)) {
1915 return 0;
1916 }
1917
1918 return 1;
1919}
1920
Adam Langleybdd5d662015-07-20 16:19:08 -07001921
1922/* EC point formats.
1923 *
1924 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1925
1926static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1927 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1928 return 0;
1929 }
1930
1931 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1932
1933 size_t i;
1934 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1935 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1936
1937 const uint32_t alg_k = cipher->algorithm_mkey;
1938 const uint32_t alg_a = cipher->algorithm_auth;
1939 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1940 return 1;
1941 }
1942 }
1943
1944 return 0;
1945}
1946
Adam Langleybdd5d662015-07-20 16:19:08 -07001947static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001948 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001949 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1950 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001951 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1952 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001953 !CBB_flush(out)) {
1954 return 0;
1955 }
1956
1957 return 1;
1958}
1959
1960static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1961 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1962 return 1;
1963 }
1964
1965 return ext_ec_point_add_extension(ssl, out);
1966}
1967
1968static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1969 CBS *contents) {
1970 if (contents == NULL) {
1971 return 1;
1972 }
1973
1974 CBS ec_point_format_list;
1975 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1976 CBS_len(contents) != 0) {
1977 return 0;
1978 }
1979
David Benjaminfc059942015-07-30 23:01:59 -04001980 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1981 * point format. */
1982 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1983 CBS_len(&ec_point_format_list)) == NULL) {
1984 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001985 return 0;
1986 }
1987
1988 return 1;
1989}
1990
1991static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1992 CBS *contents) {
1993 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1994}
1995
1996static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1997 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1998 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001999 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07002000
2001 if (!using_ecc) {
2002 return 1;
2003 }
2004
2005 return ext_ec_point_add_extension(ssl, out);
2006}
2007
Adam Langley273d49c2015-07-20 16:38:52 -07002008
2009/* EC supported curves.
2010 *
2011 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2012
2013static void ext_ec_curves_init(SSL *ssl) {
2014 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2015 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2016 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2017}
2018
2019static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2020 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2021 return 1;
2022 }
2023
2024 CBB contents, curves_bytes;
2025 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2026 !CBB_add_u16_length_prefixed(out, &contents) ||
2027 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2028 return 0;
2029 }
2030
2031 const uint16_t *curves;
2032 size_t curves_len;
2033 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2034
2035 size_t i;
2036 for (i = 0; i < curves_len; i++) {
2037 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2038 return 0;
2039 }
2040 }
2041
2042 return CBB_flush(out);
2043}
2044
2045static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2046 CBS *contents) {
2047 /* This extension is not expected to be echoed by servers and is ignored. */
2048 return 1;
2049}
2050
2051static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2052 CBS *contents) {
2053 if (contents == NULL) {
2054 return 1;
2055 }
2056
2057 CBS elliptic_curve_list;
2058 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2059 CBS_len(&elliptic_curve_list) == 0 ||
2060 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2061 CBS_len(contents) != 0) {
2062 return 0;
2063 }
2064
2065 ssl->s3->tmp.peer_ellipticcurvelist =
2066 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2067
2068 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2069 *out_alert = SSL_AD_INTERNAL_ERROR;
2070 return 0;
2071 }
2072
2073 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2074 size_t i;
2075 for (i = 0; i < num_curves; i++) {
2076 if (!CBS_get_u16(&elliptic_curve_list,
2077 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2078 goto err;
2079 }
2080 }
2081
2082 assert(CBS_len(&elliptic_curve_list) == 0);
2083 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2084
2085 return 1;
2086
2087err:
2088 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2089 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2090 *out_alert = SSL_AD_INTERNAL_ERROR;
2091 return 0;
2092}
2093
2094static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2095 /* Servers don't echo this extension. */
2096 return 1;
2097}
2098
2099
Adam Langley614c66a2015-06-12 15:26:58 -07002100/* kExtensions contains all the supported extensions. */
2101static const struct tls_extension kExtensions[] = {
2102 {
Adam Langley5021b222015-06-12 18:27:58 -07002103 /* The renegotiation extension must always be at index zero because the
2104 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2105 * sent as an SCSV. */
2106 TLSEXT_TYPE_renegotiate,
2107 NULL,
2108 ext_ri_add_clienthello,
2109 ext_ri_parse_serverhello,
2110 ext_ri_parse_clienthello,
2111 ext_ri_add_serverhello,
2112 },
2113 {
Adam Langley614c66a2015-06-12 15:26:58 -07002114 TLSEXT_TYPE_server_name,
2115 ext_sni_init,
2116 ext_sni_add_clienthello,
2117 ext_sni_parse_serverhello,
2118 ext_sni_parse_clienthello,
2119 ext_sni_add_serverhello,
2120 },
Adam Langley0a056712015-07-01 15:03:33 -07002121 {
2122 TLSEXT_TYPE_extended_master_secret,
2123 ext_ems_init,
2124 ext_ems_add_clienthello,
2125 ext_ems_parse_serverhello,
2126 ext_ems_parse_clienthello,
2127 ext_ems_add_serverhello,
2128 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002129 {
2130 TLSEXT_TYPE_session_ticket,
2131 NULL,
2132 ext_ticket_add_clienthello,
2133 ext_ticket_parse_serverhello,
2134 ext_ticket_parse_clienthello,
2135 ext_ticket_add_serverhello,
2136 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002137 {
2138 TLSEXT_TYPE_signature_algorithms,
2139 NULL,
2140 ext_sigalgs_add_clienthello,
2141 ext_sigalgs_parse_serverhello,
2142 ext_sigalgs_parse_clienthello,
2143 ext_sigalgs_add_serverhello,
2144 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002145 {
2146 TLSEXT_TYPE_status_request,
2147 ext_ocsp_init,
2148 ext_ocsp_add_clienthello,
2149 ext_ocsp_parse_serverhello,
2150 ext_ocsp_parse_clienthello,
2151 ext_ocsp_add_serverhello,
2152 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002153 {
2154 TLSEXT_TYPE_next_proto_neg,
2155 ext_npn_init,
2156 ext_npn_add_clienthello,
2157 ext_npn_parse_serverhello,
2158 ext_npn_parse_clienthello,
2159 ext_npn_add_serverhello,
2160 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002161 {
2162 TLSEXT_TYPE_certificate_timestamp,
2163 NULL,
2164 ext_sct_add_clienthello,
2165 ext_sct_parse_serverhello,
2166 ext_sct_parse_clienthello,
2167 ext_sct_add_serverhello,
2168 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002169 {
2170 TLSEXT_TYPE_application_layer_protocol_negotiation,
2171 ext_alpn_init,
2172 ext_alpn_add_clienthello,
2173 ext_alpn_parse_serverhello,
2174 ext_alpn_parse_clienthello,
2175 ext_alpn_add_serverhello,
2176 },
Adam Langley49c7af12015-07-10 14:33:46 -07002177 {
2178 TLSEXT_TYPE_channel_id,
2179 ext_channel_id_init,
2180 ext_channel_id_add_clienthello,
2181 ext_channel_id_parse_serverhello,
2182 ext_channel_id_parse_clienthello,
2183 ext_channel_id_add_serverhello,
2184 },
Adam Langley391250d2015-07-15 19:06:07 -07002185 {
2186 TLSEXT_TYPE_srtp,
2187 ext_srtp_init,
2188 ext_srtp_add_clienthello,
2189 ext_srtp_parse_serverhello,
2190 ext_srtp_parse_clienthello,
2191 ext_srtp_add_serverhello,
2192 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002193 {
2194 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002195 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002196 ext_ec_point_add_clienthello,
2197 ext_ec_point_parse_serverhello,
2198 ext_ec_point_parse_clienthello,
2199 ext_ec_point_add_serverhello,
2200 },
Adam Langley273d49c2015-07-20 16:38:52 -07002201 {
2202 TLSEXT_TYPE_elliptic_curves,
2203 ext_ec_curves_init,
2204 ext_ec_curves_add_clienthello,
2205 ext_ec_curves_parse_serverhello,
2206 ext_ec_curves_parse_clienthello,
2207 ext_ec_curves_add_serverhello,
2208 },
Adam Langley614c66a2015-06-12 15:26:58 -07002209};
2210
2211#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2212
Adam Langley4cfa96b2015-07-01 11:56:55 -07002213OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2214 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002215 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002216OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2217 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2218 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002219 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002220
Adam Langley614c66a2015-06-12 15:26:58 -07002221static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2222 uint16_t value) {
2223 unsigned i;
2224 for (i = 0; i < kNumExtensions; i++) {
2225 if (kExtensions[i].value == value) {
2226 *out_index = i;
2227 return &kExtensions[i];
2228 }
2229 }
2230
2231 return NULL;
2232}
2233
Adam Langley09505632015-07-30 18:10:13 -07002234int SSL_extension_supported(unsigned extension_value) {
2235 uint32_t index;
2236 return extension_value == TLSEXT_TYPE_padding ||
2237 tls_extension_find(&index, extension_value) != NULL;
2238}
2239
Adam Langleyb0c235e2014-06-20 12:00:00 -07002240/* header_len is the length of the ClientHello header written so far, used to
2241 * compute padding. It does not include the record header. Pass 0 if no padding
2242 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002243uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2244 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2246 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002247 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langley33ad2b52015-07-20 17:43:53 -07002250 CBB cbb, extensions;
2251 CBB_zero(&cbb);
2252 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2253 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2254 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langley614c66a2015-06-12 15:26:58 -07002257 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002258 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langley614c66a2015-06-12 15:26:58 -07002260 size_t i;
2261 for (i = 0; i < kNumExtensions; i++) {
2262 if (kExtensions[i].init != NULL) {
2263 kExtensions[i].init(s);
2264 }
2265 }
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langley614c66a2015-06-12 15:26:58 -07002267 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002268 const size_t len_before = CBB_len(&extensions);
2269 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2270 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2271 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2272 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langley33ad2b52015-07-20 17:43:53 -07002275 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002276 s->s3->tmp.extensions.sent |= (1u << i);
2277 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 }
Adam Langley75712922014-10-10 16:23:43 -07002279
Adam Langley09505632015-07-30 18:10:13 -07002280 if (!custom_ext_add_clienthello(s, &extensions)) {
2281 goto err;
2282 }
2283
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002285 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 if (header_len > 0xff && header_len < 0x200) {
2287 /* Add padding to workaround bugs in F5 terminators. See
2288 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2289 *
2290 * NB: because this code works out the length of all existing extensions
2291 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002292 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 /* Extensions take at least four bytes to encode. Always include least
2294 * one byte of data if including the extension. WebSphere Application
2295 * Server 7.0 is intolerant to the last extension being zero-length. */
2296 if (padding_len >= 4 + 1) {
2297 padding_len -= 4;
2298 } else {
2299 padding_len = 1;
2300 }
Adam Langley95c29f32014-06-20 12:00:00 -07002301
Adam Langley33ad2b52015-07-20 17:43:53 -07002302 uint8_t *padding_bytes;
2303 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2304 !CBB_add_u16(&extensions, padding_len) ||
2305 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2306 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 }
Adam Langley75712922014-10-10 16:23:43 -07002308
Adam Langley33ad2b52015-07-20 17:43:53 -07002309 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 }
2311 }
Adam Langley75712922014-10-10 16:23:43 -07002312
Adam Langley33ad2b52015-07-20 17:43:53 -07002313 if (!CBB_flush(&cbb)) {
2314 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 }
Adam Langley95c29f32014-06-20 12:00:00 -07002316
Adam Langley33ad2b52015-07-20 17:43:53 -07002317 uint8_t *ret = buf;
2318 const size_t cbb_len = CBB_len(&cbb);
2319 /* If only two bytes have been written then the extensions are actually empty
2320 * and those two bytes are the zero length. In that case, we don't bother
2321 * sending the extensions length. */
2322 if (cbb_len > 2) {
2323 ret += cbb_len;
2324 }
2325
2326 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002327 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002328
2329err:
2330 CBB_cleanup(&cbb);
2331 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2332 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002333}
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langley614c66a2015-06-12 15:26:58 -07002335uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2336 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002337 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2338 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002339 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002340 }
Adam Langley95c29f32014-06-20 12:00:00 -07002341
Adam Langley33ad2b52015-07-20 17:43:53 -07002342 CBB cbb, extensions;
2343 CBB_zero(&cbb);
2344 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2345 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2346 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002347 }
2348
2349 unsigned i;
2350 for (i = 0; i < kNumExtensions; i++) {
2351 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2352 /* Don't send extensions that were not received. */
2353 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002354 }
Adam Langley95c29f32014-06-20 12:00:00 -07002355
Adam Langley33ad2b52015-07-20 17:43:53 -07002356 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2357 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2358 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2359 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002360 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 }
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langley09505632015-07-30 18:10:13 -07002363 if (!custom_ext_add_serverhello(s, &extensions)) {
2364 goto err;
2365 }
2366
Adam Langley33ad2b52015-07-20 17:43:53 -07002367 if (!CBB_flush(&cbb)) {
2368 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002369 }
2370
Adam Langley33ad2b52015-07-20 17:43:53 -07002371 uint8_t *ret = buf;
2372 const size_t cbb_len = CBB_len(&cbb);
2373 /* If only two bytes have been written then the extensions are actually empty
2374 * and those two bytes are the zero length. In that case, we don't bother
2375 * sending the extensions length. */
2376 if (cbb_len > 2) {
2377 ret += cbb_len;
2378 }
2379
2380 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002381 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002382
2383err:
2384 CBB_cleanup(&cbb);
2385 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2386 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002390 size_t i;
2391 for (i = 0; i < kNumExtensions; i++) {
2392 if (kExtensions[i].init != NULL) {
2393 kExtensions[i].init(s);
2394 }
2395 }
2396
2397 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002398 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002399 /* The renegotiation extension must always be at index zero because the
2400 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2401 * sent as an SCSV. */
2402 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002405 if (CBS_len(cbs) != 0) {
2406 /* Decode the extensions block and check it is valid. */
2407 CBS extensions;
2408 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2409 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002410 *out_alert = SSL_AD_DECODE_ERROR;
2411 return 0;
2412 }
Adam Langley95c29f32014-06-20 12:00:00 -07002413
Adam Langley33ad2b52015-07-20 17:43:53 -07002414 while (CBS_len(&extensions) != 0) {
2415 uint16_t type;
2416 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langley33ad2b52015-07-20 17:43:53 -07002418 /* Decode the next extension. */
2419 if (!CBS_get_u16(&extensions, &type) ||
2420 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2421 *out_alert = SSL_AD_DECODE_ERROR;
2422 return 0;
2423 }
2424
2425 unsigned ext_index;
2426 const struct tls_extension *const ext =
2427 tls_extension_find(&ext_index, type);
2428
2429 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002430 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2431 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2432 return 0;
2433 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002434 continue;
2435 }
2436
Adam Langley614c66a2015-06-12 15:26:58 -07002437 s->s3->tmp.extensions.received |= (1u << ext_index);
2438 uint8_t alert = SSL_AD_DECODE_ERROR;
2439 if (!ext->parse_clienthello(s, &alert, &extension)) {
2440 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002441 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2442 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002443 return 0;
2444 }
Adam Langley614c66a2015-06-12 15:26:58 -07002445 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 }
Adam Langley75712922014-10-10 16:23:43 -07002447
Adam Langley614c66a2015-06-12 15:26:58 -07002448 for (i = 0; i < kNumExtensions; i++) {
2449 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2450 /* Extension wasn't observed so call the callback with a NULL
2451 * parameter. */
2452 uint8_t alert = SSL_AD_DECODE_ERROR;
2453 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002454 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2455 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002456 *out_alert = alert;
2457 return 0;
2458 }
2459 }
2460 }
2461
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 return 1;
2463}
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2466 int alert = -1;
2467 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2468 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2469 return 0;
2470 }
Adam Langley95c29f32014-06-20 12:00:00 -07002471
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002473 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 return 0;
2475 }
Adam Langley95c29f32014-06-20 12:00:00 -07002476
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 return 1;
2478}
Adam Langley95c29f32014-06-20 12:00:00 -07002479
Adam Langleyfcf25832014-12-18 17:42:32 -08002480static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002481 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002482 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002483
Adam Langley33ad2b52015-07-20 17:43:53 -07002484 if (CBS_len(cbs) != 0) {
2485 /* Decode the extensions block and check it is valid. */
2486 CBS extensions;
2487 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2488 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 *out_alert = SSL_AD_DECODE_ERROR;
2490 return 0;
2491 }
Adam Langley95c29f32014-06-20 12:00:00 -07002492
Adam Langley614c66a2015-06-12 15:26:58 -07002493
Adam Langley33ad2b52015-07-20 17:43:53 -07002494 while (CBS_len(&extensions) != 0) {
2495 uint16_t type;
2496 CBS extension;
2497
2498 /* Decode the next extension. */
2499 if (!CBS_get_u16(&extensions, &type) ||
2500 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2501 *out_alert = SSL_AD_DECODE_ERROR;
2502 return 0;
2503 }
2504
2505 unsigned ext_index;
2506 const struct tls_extension *const ext =
2507 tls_extension_find(&ext_index, type);
2508
Adam Langley09505632015-07-30 18:10:13 -07002509 if (ext == NULL) {
2510 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2511 return 0;
2512 }
2513 continue;
2514 }
2515
2516 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2517 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002518 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002519 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 *out_alert = SSL_AD_DECODE_ERROR;
2521 return 0;
2522 }
David Benjamin03973092014-06-24 23:27:17 -04002523
Adam Langley614c66a2015-06-12 15:26:58 -07002524 received |= (1u << ext_index);
2525
2526 uint8_t alert = SSL_AD_DECODE_ERROR;
2527 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002528 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2529 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002530 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 return 0;
2532 }
Adam Langley614c66a2015-06-12 15:26:58 -07002533 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 }
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langley33ad2b52015-07-20 17:43:53 -07002536 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002537 for (i = 0; i < kNumExtensions; i++) {
2538 if (!(received & (1u << i))) {
2539 /* Extension wasn't observed so call the callback with a NULL
2540 * parameter. */
2541 uint8_t alert = SSL_AD_DECODE_ERROR;
2542 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002543 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2544 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002545 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 return 0;
2547 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 }
2549 }
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 return 1;
2552}
Adam Langley95c29f32014-06-20 12:00:00 -07002553
Adam Langleyfcf25832014-12-18 17:42:32 -08002554static int ssl_check_clienthello_tlsext(SSL *s) {
2555 int ret = SSL_TLSEXT_ERR_NOACK;
2556 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2559 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2562 ret = s->ctx->tlsext_servername_callback(s, &al,
2563 s->ctx->tlsext_servername_arg);
2564 } else if (s->initial_ctx != NULL &&
2565 s->initial_ctx->tlsext_servername_callback != 0) {
2566 ret = s->initial_ctx->tlsext_servername_callback(
2567 s, &al, s->initial_ctx->tlsext_servername_arg);
2568 }
Adam Langley95c29f32014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 switch (ret) {
2571 case SSL_TLSEXT_ERR_ALERT_FATAL:
2572 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2573 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 case SSL_TLSEXT_ERR_ALERT_WARNING:
2576 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2577 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002580 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002581 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583 default:
2584 return 1;
2585 }
2586}
Adam Langleyed8270a2014-09-02 13:52:56 -07002587
Adam Langleyfcf25832014-12-18 17:42:32 -08002588static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002589 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2593 ret = s->ctx->tlsext_servername_callback(s, &al,
2594 s->ctx->tlsext_servername_arg);
2595 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002596 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002597 ret = s->initial_ctx->tlsext_servername_callback(
2598 s, &al, s->initial_ctx->tlsext_servername_arg);
2599 }
Adam Langley95c29f32014-06-20 12:00:00 -07002600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 switch (ret) {
2602 case SSL_TLSEXT_ERR_ALERT_FATAL:
2603 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2604 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002605
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 case SSL_TLSEXT_ERR_ALERT_WARNING:
2607 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2608 return 1;
2609
2610 default:
2611 return 1;
2612 }
2613}
2614
2615int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2616 int alert = -1;
2617 if (s->version < SSL3_VERSION) {
2618 return 1;
2619 }
2620
2621 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2622 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2623 return 0;
2624 }
2625
2626 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002627 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002628 return 0;
2629 }
2630
2631 return 1;
2632}
Adam Langley95c29f32014-06-20 12:00:00 -07002633
David Benjamine3aa1d92015-06-16 15:34:50 -04002634int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2635 int *out_send_ticket, const uint8_t *ticket,
2636 size_t ticket_len, const uint8_t *session_id,
2637 size_t session_id_len) {
2638 int ret = 1; /* Most errors are non-fatal. */
2639 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2640 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002641
David Benjamine3aa1d92015-06-16 15:34:50 -04002642 HMAC_CTX hmac_ctx;
2643 HMAC_CTX_init(&hmac_ctx);
2644 EVP_CIPHER_CTX cipher_ctx;
2645 EVP_CIPHER_CTX_init(&cipher_ctx);
2646
2647 *out_send_ticket = 0;
2648 *out_session = NULL;
2649
2650 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2651 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002652 }
2653
David Benjamine3aa1d92015-06-16 15:34:50 -04002654 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002656 *out_send_ticket = 1;
2657 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 }
2659
David Benjaminadcc3952015-04-26 13:07:57 -04002660 /* Ensure there is room for the key name and the largest IV
2661 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2662 * the maximum IV length should be well under the minimum size for the
2663 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002664 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2665 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002666 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002667 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002668
David Benjamine3aa1d92015-06-16 15:34:50 -04002669 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2670 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2671 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2672 0 /* decrypt */);
2673 if (cb_ret < 0) {
2674 ret = 0;
2675 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002677 if (cb_ret == 0) {
2678 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002679 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002680 if (cb_ret == 2) {
2681 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 }
2683 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002684 /* Check the key name matches. */
2685 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2686 SSL_TICKET_KEY_NAME_LEN) != 0) {
2687 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002689 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2690 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002692 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2693 ssl_ctx->tlsext_tick_aes_key, iv)) {
2694 ret = 0;
2695 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 }
2697 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002698 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002699
David Benjamine3aa1d92015-06-16 15:34:50 -04002700 /* Check the MAC at the end of the ticket. */
2701 uint8_t mac[EVP_MAX_MD_SIZE];
2702 size_t mac_len = HMAC_size(&hmac_ctx);
2703 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002704 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002705 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002707 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2708 HMAC_Final(&hmac_ctx, mac, NULL);
2709 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2710 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 }
2712
David Benjamine3aa1d92015-06-16 15:34:50 -04002713 /* Decrypt the session data. */
2714 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2715 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2716 mac_len;
2717 plaintext = OPENSSL_malloc(ciphertext_len);
2718 if (plaintext == NULL) {
2719 ret = 0;
2720 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002722 if (ciphertext_len >= INT_MAX) {
2723 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002725 int len1, len2;
2726 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2727 (int)ciphertext_len) ||
2728 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2729 ERR_clear_error(); /* Don't leave an error on the queue. */
2730 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002731 }
2732
David Benjamine3aa1d92015-06-16 15:34:50 -04002733 /* Decode the session. */
2734 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2735 if (session == NULL) {
2736 ERR_clear_error(); /* Don't leave an error on the queue. */
2737 goto done;
2738 }
2739
2740 /* Copy the client's session ID into the new session, to denote the ticket has
2741 * been accepted. */
2742 memcpy(session->session_id, session_id, session_id_len);
2743 session->session_id_length = session_id_len;
2744
2745 *out_session = session;
2746
2747done:
2748 OPENSSL_free(plaintext);
2749 HMAC_CTX_cleanup(&hmac_ctx);
2750 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2751 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002752}
Adam Langley95c29f32014-06-20 12:00:00 -07002753
2754/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002755typedef struct {
2756 int nid;
2757 int id;
2758} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002759
Adam Langleyfcf25832014-12-18 17:42:32 -08002760static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2761 {NID_sha1, TLSEXT_hash_sha1},
2762 {NID_sha224, TLSEXT_hash_sha224},
2763 {NID_sha256, TLSEXT_hash_sha256},
2764 {NID_sha384, TLSEXT_hash_sha384},
2765 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002766
Adam Langleyfcf25832014-12-18 17:42:32 -08002767static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2768 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002769
Adam Langleyfcf25832014-12-18 17:42:32 -08002770static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2771 size_t i;
2772 for (i = 0; i < tlen; i++) {
2773 if (table[i].nid == nid) {
2774 return table[i].id;
2775 }
2776 }
Adam Langley95c29f32014-06-20 12:00:00 -07002777
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 return -1;
2779}
Adam Langley95c29f32014-06-20 12:00:00 -07002780
David Benjaminb4d65fd2015-05-29 17:11:21 -04002781int tls12_get_sigid(int pkey_type) {
2782 return tls12_find_id(pkey_type, tls12_sig,
2783 sizeof(tls12_sig) / sizeof(tls12_lookup));
2784}
2785
David Benjamind1d80782015-07-05 11:54:09 -04002786int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002787 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002788
Adam Langleyfcf25832014-12-18 17:42:32 -08002789 if (!md) {
2790 return 0;
2791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2794 sizeof(tls12_md) / sizeof(tls12_lookup));
2795 if (md_id == -1) {
2796 return 0;
2797 }
Adam Langley95c29f32014-06-20 12:00:00 -07002798
David Benjamind1d80782015-07-05 11:54:09 -04002799 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 if (sig_id == -1) {
2801 return 0;
2802 }
Adam Langley95c29f32014-06-20 12:00:00 -07002803
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 p[0] = (uint8_t)md_id;
2805 p[1] = (uint8_t)sig_id;
2806 return 1;
2807}
2808
Adam Langleyfcf25832014-12-18 17:42:32 -08002809const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2810 switch (hash_alg) {
2811 case TLSEXT_hash_md5:
2812 return EVP_md5();
2813
2814 case TLSEXT_hash_sha1:
2815 return EVP_sha1();
2816
2817 case TLSEXT_hash_sha224:
2818 return EVP_sha224();
2819
2820 case TLSEXT_hash_sha256:
2821 return EVP_sha256();
2822
2823 case TLSEXT_hash_sha384:
2824 return EVP_sha384();
2825
2826 case TLSEXT_hash_sha512:
2827 return EVP_sha512();
2828
2829 default:
2830 return NULL;
2831 }
2832}
Adam Langley95c29f32014-06-20 12:00:00 -07002833
David Benjaminec2f27d2014-11-13 19:17:25 -05002834/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2835 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002836static int tls12_get_pkey_type(uint8_t sig_alg) {
2837 switch (sig_alg) {
2838 case TLSEXT_signature_rsa:
2839 return EVP_PKEY_RSA;
2840
2841 case TLSEXT_signature_ecdsa:
2842 return EVP_PKEY_EC;
2843
2844 default:
2845 return -1;
2846 }
2847}
Adam Langley95c29f32014-06-20 12:00:00 -07002848
Adam Langley95c29f32014-06-20 12:00:00 -07002849/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002850static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2851 size_t preflen, const uint8_t *allow,
2852 size_t allowlen) {
2853 const uint8_t *ptmp, *atmp;
2854 size_t i, j, nmatch = 0;
2855
2856 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2857 /* Skip disabled hashes or signature algorithms */
2858 if (tls12_get_hash(ptmp[0]) == NULL ||
2859 tls12_get_pkey_type(ptmp[1]) == -1) {
2860 continue;
2861 }
2862
2863 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2864 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2865 nmatch++;
2866 if (shsig) {
2867 shsig->rhash = ptmp[0];
2868 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002869 shsig++;
2870 }
2871
2872 break;
2873 }
2874 }
2875 }
2876
2877 return nmatch;
2878}
Adam Langley95c29f32014-06-20 12:00:00 -07002879
2880/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002881static int tls1_set_shared_sigalgs(SSL *s) {
2882 const uint8_t *pref, *allow, *conf;
2883 size_t preflen, allowlen, conflen;
2884 size_t nmatch;
2885 TLS_SIGALGS *salgs = NULL;
2886 CERT *c = s->cert;
2887
David Benjamin2755a3e2015-04-22 16:17:58 -04002888 OPENSSL_free(c->shared_sigalgs);
2889 c->shared_sigalgs = NULL;
2890 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002891
David Benjamin3dd90162015-08-09 11:26:40 -04002892 conflen = tls12_get_psigalgs(s, &conf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002893
2894 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2895 pref = conf;
2896 preflen = conflen;
2897 allow = c->peer_sigalgs;
2898 allowlen = c->peer_sigalgslen;
2899 } else {
2900 allow = conf;
2901 allowlen = conflen;
2902 pref = c->peer_sigalgs;
2903 preflen = c->peer_sigalgslen;
2904 }
2905
2906 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2907 if (!nmatch) {
2908 return 1;
2909 }
2910
2911 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2912 if (!salgs) {
2913 return 0;
2914 }
2915
2916 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2917 c->shared_sigalgs = salgs;
2918 c->shared_sigalgslen = nmatch;
2919 return 1;
2920}
Adam Langley95c29f32014-06-20 12:00:00 -07002921
2922/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002923int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2924 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002925
Adam Langleyfcf25832014-12-18 17:42:32 -08002926 /* Extension ignored for inappropriate versions */
2927 if (!SSL_USE_SIGALGS(s)) {
2928 return 1;
2929 }
David Benjamincd996942014-07-20 16:23:51 -04002930
David Benjamin61c0d4e2015-03-19 14:24:37 -04002931 if (CBS_len(sigalgs) % 2 != 0 ||
2932 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2933 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 return 0;
2935 }
Adam Langley95c29f32014-06-20 12:00:00 -07002936
Adam Langleyfcf25832014-12-18 17:42:32 -08002937 return 1;
2938}
David Benjaminec2f27d2014-11-13 19:17:25 -05002939
David Benjamind1d80782015-07-05 11:54:09 -04002940const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2941 CERT *cert = ssl->cert;
2942 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002944
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002946 for (i = 0; i < cert->shared_sigalgslen; i++) {
2947 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002949 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2950 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002951 continue;
2952 }
2953 return md;
2954 }
Adam Langley95c29f32014-06-20 12:00:00 -07002955
Adam Langleyfcf25832014-12-18 17:42:32 -08002956 /* If no suitable digest may be found, default to SHA-1. */
2957 return EVP_sha1();
2958}
Adam Langley95c29f32014-06-20 12:00:00 -07002959
David Benjamind6a4ae92015-08-06 11:10:51 -04002960int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2961 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002963
2964 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002965 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2966 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002968
David Benjamind6a4ae92015-08-06 11:10:51 -04002969 static const char kClientIDMagic[] = "TLS Channel ID signature";
2970 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2971
2972 if (ssl->hit) {
2973 static const char kResumptionMagic[] = "Resumption";
2974 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2975 if (ssl->session->original_handshake_hash_len == 0) {
2976 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2977 goto err;
2978 }
2979 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2980 ssl->session->original_handshake_hash_len);
2981 }
2982
2983 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2984 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2985 sizeof(handshake_hash));
2986 if (handshake_hash_len < 0) {
2987 goto err;
2988 }
2989 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2990 unsigned len_u;
2991 EVP_DigestFinal_ex(&ctx, out, &len_u);
2992 *out_len = len_u;
2993
2994 ret = 1;
2995
2996err:
2997 EVP_MD_CTX_cleanup(&ctx);
2998 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002999}
Adam Langley1258b6a2014-06-20 12:00:00 -07003000
3001/* tls1_record_handshake_hashes_for_channel_id records the current handshake
3002 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08003003int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
3004 int digest_len;
3005 /* This function should never be called for a resumed session because the
3006 * handshake hashes that we wish to record are for the original, full
3007 * handshake. */
3008 if (s->hit) {
3009 return -1;
3010 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003011
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 digest_len =
3013 tls1_handshake_digest(s, s->session->original_handshake_hash,
3014 sizeof(s->session->original_handshake_hash));
3015 if (digest_len < 0) {
3016 return -1;
3017 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003018
Adam Langleyfcf25832014-12-18 17:42:32 -08003019 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003020
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 return 1;
3022}