blob: 7a13f8ec51ec665c58d7bb30448dda638528c00e [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) {
634 /* If server use client authentication sigalgs if not NULL */
635 if (s->server && s->cert->client_sigalgs) {
636 *psigs = s->cert->client_sigalgs;
637 return s->cert->client_sigalgslen;
638 } else if (s->cert->conf_sigalgs) {
639 *psigs = s->cert->conf_sigalgs;
640 return s->cert->conf_sigalgslen;
641 } else {
642 *psigs = tls12_sigalgs;
643 return sizeof(tls12_sigalgs);
644 }
645}
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
648 * checks it is consistent with |s|'s sent supported signature algorithms and,
649 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
650 * returns 0 and writes an alert into |*out_alert|. */
651int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
652 CBS *cbs, EVP_PKEY *pkey) {
653 const uint8_t *sent_sigs;
654 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400655 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 uint8_t hash, signature;
657
658 /* Should never happen */
659 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400660 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 *out_alert = SSL_AD_INTERNAL_ERROR;
662 return 0;
663 }
664
665 if (!CBS_get_u8(cbs, &hash) ||
666 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 *out_alert = SSL_AD_DECODE_ERROR;
669 return 0;
670 }
671
672 /* Check key type is consistent with signature */
673 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400674 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
676 return 0;
677 }
678
679 if (pkey->type == EVP_PKEY_EC) {
680 uint16_t curve_id;
681 uint8_t comp_id;
682 /* Check compression and curve matches extensions */
683 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
684 *out_alert = SSL_AD_INTERNAL_ERROR;
685 return 0;
686 }
687
688 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400689 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400690 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
692 return 0;
693 }
694 }
695
696 /* Check signature matches a type we sent */
697 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
698 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
699 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
700 break;
701 }
702 }
703
704 /* Allow fallback to SHA-1. */
705 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400706 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
708 return 0;
709 }
710
711 *out_md = tls12_get_hash(hash);
712 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400713 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
715 return 0;
716 }
717
718 return 1;
719}
720
721/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
722 * supported or doesn't appear in supported signature algorithms. Unlike
723 * ssl_cipher_get_disabled this applies to a specific session and not global
724 * settings. */
725void ssl_set_client_disabled(SSL *s) {
726 CERT *c = s->cert;
727 const uint8_t *sigalgs;
728 size_t i, sigalgslen;
729 int have_rsa = 0, have_ecdsa = 0;
730 c->mask_a = 0;
731 c->mask_k = 0;
732
733 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
734 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
735 c->mask_ssl = SSL_TLSV1_2;
736 } else {
737 c->mask_ssl = 0;
738 }
739
740 /* Now go through all signature algorithms seeing if we support any for RSA,
741 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
742 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
743 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
744 switch (sigalgs[1]) {
745 case TLSEXT_signature_rsa:
746 have_rsa = 1;
747 break;
748
749 case TLSEXT_signature_ecdsa:
750 have_ecdsa = 1;
751 break;
752 }
753 }
754
755 /* Disable auth if we don't include any appropriate signature algorithms. */
756 if (!have_rsa) {
757 c->mask_a |= SSL_aRSA;
758 }
759 if (!have_ecdsa) {
760 c->mask_a |= SSL_aECDSA;
761 }
762
763 /* with PSK there must be client callback set */
764 if (!s->psk_client_callback) {
765 c->mask_a |= SSL_aPSK;
766 c->mask_k |= SSL_kPSK;
767 }
768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langley614c66a2015-06-12 15:26:58 -0700770/* tls_extension represents a TLS extension that is handled internally. The
771 * |init| function is called for each handshake, before any other functions of
772 * the extension. Then the add and parse callbacks are called as needed.
773 *
774 * The parse callbacks receive a |CBS| that contains the contents of the
775 * extension (i.e. not including the type and length bytes). If an extension is
776 * not received then the parse callbacks will be called with a NULL CBS so that
777 * they can do any processing needed to handle the absence of an extension.
778 *
779 * The add callbacks receive a |CBB| to which the extension can be appended but
780 * the function is responsible for appending the type and length bytes too.
781 *
782 * All callbacks return one for success and zero for error. If a parse function
783 * returns zero then a fatal alert with value |*out_alert| will be sent. If
784 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
785struct tls_extension {
786 uint16_t value;
787 void (*init)(SSL *ssl);
788
789 int (*add_clienthello)(SSL *ssl, CBB *out);
790 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
791
792 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
793 int (*add_serverhello)(SSL *ssl, CBB *out);
794};
795
796
797/* Server name indication (SNI).
798 *
799 * https://tools.ietf.org/html/rfc6066#section-3. */
800
801static void ext_sni_init(SSL *ssl) {
802 ssl->s3->tmp.should_ack_sni = 0;
803}
804
805static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
806 if (ssl->tlsext_hostname == NULL) {
807 return 1;
808 }
809
810 CBB contents, server_name_list, name;
811 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
812 !CBB_add_u16_length_prefixed(out, &contents) ||
813 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
814 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
815 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
816 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
817 strlen(ssl->tlsext_hostname)) ||
818 !CBB_flush(out)) {
819 return 0;
820 }
821
822 return 1;
823}
824
825static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
826 if (contents == NULL) {
827 return 1;
828 }
829
830 if (CBS_len(contents) != 0) {
831 return 0;
832 }
833
834 assert(ssl->tlsext_hostname != NULL);
835
836 if (!ssl->hit) {
837 assert(ssl->session->tlsext_hostname == NULL);
838 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
839 if (!ssl->session->tlsext_hostname) {
840 *out_alert = SSL_AD_INTERNAL_ERROR;
841 return 0;
842 }
843 }
844
845 return 1;
846}
847
848static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
849 if (contents == NULL) {
850 return 1;
851 }
852
853 /* The servername extension is treated as follows:
854 *
855 * - Only the hostname type is supported with a maximum length of 255.
856 * - The servername is rejected if too long or if it contains zeros, in
857 * which case an fatal alert is generated.
858 * - The servername field is maintained together with the session cache.
859 * - When a session is resumed, the servername callback is invoked in order
860 * to allow the application to position itself to the right context.
861 * - The servername is acknowledged if it is new for a session or when
862 * it is identical to a previously used for the same session.
863 * Applications can control the behaviour. They can at any time
864 * set a 'desirable' servername for a new SSL object. This can be the
865 * case for example with HTTPS when a Host: header field is received and
866 * a renegotiation is requested. In this case, a possible servername
867 * presented in the new client hello is only acknowledged if it matches
868 * the value of the Host: field.
869 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
870 * if they provide for changing an explicit servername context for the
871 * session,
872 * i.e. when the session has been established with a servername extension.
873 */
874
875 CBS server_name_list;
876 char have_seen_host_name = 0;
877
878 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
879 CBS_len(&server_name_list) == 0 ||
880 CBS_len(contents) != 0) {
881 return 0;
882 }
883
884 /* Decode each ServerName in the extension. */
885 while (CBS_len(&server_name_list) > 0) {
886 uint8_t name_type;
887 CBS host_name;
888
889 if (!CBS_get_u8(&server_name_list, &name_type) ||
890 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
891 return 0;
892 }
893
894 /* Only host_name is supported. */
895 if (name_type != TLSEXT_NAMETYPE_host_name) {
896 continue;
897 }
898
899 if (have_seen_host_name) {
900 /* The ServerNameList MUST NOT contain more than one name of the same
901 * name_type. */
902 return 0;
903 }
904
905 have_seen_host_name = 1;
906
907 if (CBS_len(&host_name) == 0 ||
908 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
909 CBS_contains_zero_byte(&host_name)) {
910 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
911 return 0;
912 }
913
914 if (!ssl->hit) {
915 assert(ssl->session->tlsext_hostname == NULL);
916 if (ssl->session->tlsext_hostname) {
917 /* This should be impossible. */
918 return 0;
919 }
920
921 /* Copy the hostname as a string. */
922 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
923 *out_alert = SSL_AD_INTERNAL_ERROR;
924 return 0;
925 }
926
927 ssl->s3->tmp.should_ack_sni = 1;
928 }
929 }
930
931 return 1;
932}
933
934static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
935 if (ssl->hit ||
936 !ssl->s3->tmp.should_ack_sni ||
937 ssl->session->tlsext_hostname == NULL) {
938 return 1;
939 }
940
941 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
942 !CBB_add_u16(out, 0 /* length */)) {
943 return 0;
944 }
945
946 return 1;
947}
948
949
Adam Langley5021b222015-06-12 18:27:58 -0700950/* Renegotiation indication.
951 *
952 * https://tools.ietf.org/html/rfc5746 */
953
954static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
955 CBB contents, prev_finished;
956 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
957 !CBB_add_u16_length_prefixed(out, &contents) ||
958 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
959 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
960 ssl->s3->previous_client_finished_len) ||
961 !CBB_flush(out)) {
962 return 0;
963 }
964
965 return 1;
966}
967
968static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
969 CBS *contents) {
970 if (contents == NULL) {
971 /* No renegotiation extension received.
972 *
973 * Strictly speaking if we want to avoid an attack we should *always* see
974 * RI even on initial ServerHello because the client doesn't see any
975 * renegotiation during an attack. However this would mean we could not
976 * connect to any server which doesn't support RI.
977 *
978 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
979 * defined. */
980 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
981 return 1;
982 }
983
984 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400985 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700986 return 0;
987 }
988
989 const size_t expected_len = ssl->s3->previous_client_finished_len +
990 ssl->s3->previous_server_finished_len;
991
992 /* Check for logic errors */
993 assert(!expected_len || ssl->s3->previous_client_finished_len);
994 assert(!expected_len || ssl->s3->previous_server_finished_len);
995
996 /* Parse out the extension contents. */
997 CBS renegotiated_connection;
998 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
999 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001000 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001001 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1002 return 0;
1003 }
1004
1005 /* Check that the extension matches. */
1006 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -04001007 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001008 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1009 return 0;
1010 }
1011
1012 const uint8_t *d = CBS_data(&renegotiated_connection);
1013 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1014 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001015 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001016 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1017 return 0;
1018 }
1019 d += ssl->s3->previous_client_finished_len;
1020
1021 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1022 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001023 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001024 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1025 return 0;
1026 }
1027 ssl->s3->send_connection_binding = 1;
1028
1029 return 1;
1030}
1031
1032static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1033 CBS *contents) {
1034 /* Renegotiation isn't supported as a server so this function should never be
1035 * called after the initial handshake. */
1036 assert(!ssl->s3->initial_handshake_complete);
1037
1038 CBS fake_contents;
1039 static const uint8_t kFakeExtension[] = {0};
1040
1041 if (contents == NULL) {
1042 if (ssl->s3->send_connection_binding) {
1043 /* The renegotiation SCSV was received so pretend that we received a
1044 * renegotiation extension. */
1045 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1046 contents = &fake_contents;
1047 /* We require that the renegotiation extension is at index zero of
1048 * kExtensions. */
1049 ssl->s3->tmp.extensions.received |= (1u << 0);
1050 } else {
1051 return 1;
1052 }
1053 }
1054
1055 CBS renegotiated_connection;
1056
1057 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1058 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001059 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001060 return 0;
1061 }
1062
1063 /* Check that the extension matches */
1064 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1065 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001066 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001067 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1068 return 0;
1069 }
1070
1071 ssl->s3->send_connection_binding = 1;
1072
1073 return 1;
1074}
1075
1076static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1077 CBB contents, prev_finished;
1078 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1079 !CBB_add_u16_length_prefixed(out, &contents) ||
1080 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1081 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1082 ssl->s3->previous_client_finished_len) ||
1083 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1084 ssl->s3->previous_server_finished_len) ||
1085 !CBB_flush(out)) {
1086 return 0;
1087 }
1088
1089 return 1;
1090}
1091
Adam Langley0a056712015-07-01 15:03:33 -07001092
1093/* Extended Master Secret.
1094 *
1095 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1096
1097static void ext_ems_init(SSL *ssl) {
1098 ssl->s3->tmp.extended_master_secret = 0;
1099}
1100
1101static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1102 if (ssl->version == SSL3_VERSION) {
1103 return 1;
1104 }
1105
1106 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1107 !CBB_add_u16(out, 0 /* length */)) {
1108 return 0;
1109 }
1110
1111 return 1;
1112}
1113
1114static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1115 CBS *contents) {
1116 if (contents == NULL) {
1117 return 1;
1118 }
1119
1120 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1121 return 0;
1122 }
1123
1124 ssl->s3->tmp.extended_master_secret = 1;
1125 return 1;
1126}
1127
1128static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1129 if (ssl->version == SSL3_VERSION || contents == NULL) {
1130 return 1;
1131 }
1132
1133 if (CBS_len(contents) != 0) {
1134 return 0;
1135 }
1136
1137 ssl->s3->tmp.extended_master_secret = 1;
1138 return 1;
1139}
1140
1141static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1142 if (!ssl->s3->tmp.extended_master_secret) {
1143 return 1;
1144 }
1145
1146 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1147 !CBB_add_u16(out, 0 /* length */)) {
1148 return 0;
1149 }
1150
1151 return 1;
1152}
1153
Adam Langley9b05bc52015-07-01 15:25:33 -07001154
1155/* Session tickets.
1156 *
1157 * https://tools.ietf.org/html/rfc5077 */
1158
1159static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1160 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1161 return 1;
1162 }
1163
1164 const uint8_t *ticket_data = NULL;
1165 int ticket_len = 0;
1166
1167 /* Renegotiation does not participate in session resumption. However, still
1168 * advertise the extension to avoid potentially breaking servers which carry
1169 * over the state from the previous handshake, such as OpenSSL servers
1170 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1171 if (!ssl->s3->initial_handshake_complete &&
1172 ssl->session != NULL &&
1173 ssl->session->tlsext_tick != NULL) {
1174 ticket_data = ssl->session->tlsext_tick;
1175 ticket_len = ssl->session->tlsext_ticklen;
1176 }
1177
1178 CBB ticket;
1179 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1180 !CBB_add_u16_length_prefixed(out, &ticket) ||
1181 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1182 !CBB_flush(out)) {
1183 return 0;
1184 }
1185
1186 return 1;
1187}
1188
1189static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1190 CBS *contents) {
1191 ssl->tlsext_ticket_expected = 0;
1192
1193 if (contents == NULL) {
1194 return 1;
1195 }
1196
1197 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1198 * this function should never be called, even if the server tries to send the
1199 * extension. */
1200 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1201
1202 if (CBS_len(contents) != 0) {
1203 return 0;
1204 }
1205
1206 ssl->tlsext_ticket_expected = 1;
1207 return 1;
1208}
1209
1210static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1211 /* This function isn't used because the ticket extension from the client is
1212 * handled in ssl_sess.c. */
1213 return 1;
1214}
1215
1216static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1217 if (!ssl->tlsext_ticket_expected) {
1218 return 1;
1219 }
1220
1221 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1222 * true. */
1223 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1224
1225 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1226 !CBB_add_u16(out, 0 /* length */)) {
1227 return 0;
1228 }
1229
1230 return 1;
1231}
1232
1233
Adam Langley2e857bd2015-07-01 16:09:19 -07001234/* Signature Algorithms.
1235 *
1236 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1237
1238static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1239 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1240 return 1;
1241 }
1242
1243 const uint8_t *sigalgs_data;
1244 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1245
1246 CBB contents, sigalgs;
1247 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1248 !CBB_add_u16_length_prefixed(out, &contents) ||
1249 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1250 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1251 !CBB_flush(out)) {
1252 return 0;
1253 }
1254
1255 return 1;
1256}
1257
1258static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1259 CBS *contents) {
1260 if (contents != NULL) {
1261 /* Servers MUST NOT send this extension. */
1262 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001263 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001264 return 0;
1265 }
1266
1267 return 1;
1268}
1269
1270static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1271 CBS *contents) {
1272 OPENSSL_free(ssl->cert->peer_sigalgs);
1273 ssl->cert->peer_sigalgs = NULL;
1274 ssl->cert->peer_sigalgslen = 0;
1275
1276 OPENSSL_free(ssl->cert->shared_sigalgs);
1277 ssl->cert->shared_sigalgs = NULL;
1278 ssl->cert->shared_sigalgslen = 0;
1279
1280 if (contents == NULL) {
1281 return 1;
1282 }
1283
1284 CBS supported_signature_algorithms;
1285 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1286 CBS_len(contents) != 0) {
1287 return 0;
1288 }
1289
1290 /* Ensure the signature algorithms are non-empty. It contains a list of
1291 * SignatureAndHashAlgorithms which are two bytes each. */
1292 if (CBS_len(&supported_signature_algorithms) == 0 ||
1293 (CBS_len(&supported_signature_algorithms) % 2) != 0 ||
1294 !tls1_process_sigalgs(ssl, &supported_signature_algorithms)) {
1295 return 0;
1296 }
1297
1298 /* It's a fatal error if the signature_algorithms extension is received and
1299 * there are no shared algorithms. */
1300 if (ssl->cert->peer_sigalgs && !ssl->cert->shared_sigalgs) {
David Benjamin3570d732015-06-29 00:28:17 -04001301 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
Adam Langley2e857bd2015-07-01 16:09:19 -07001302 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1303 return 0;
1304 }
1305
1306 return 1;
1307}
1308
1309static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1310 /* Servers MUST NOT send this extension. */
1311 return 1;
1312}
1313
1314
Adam Langleybb0bd042015-07-01 16:21:03 -07001315/* OCSP Stapling.
1316 *
1317 * https://tools.ietf.org/html/rfc6066#section-8 */
1318
1319static void ext_ocsp_init(SSL *ssl) {
1320 ssl->s3->tmp.certificate_status_expected = 0;
1321}
1322
1323static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1324 if (!ssl->ocsp_stapling_enabled) {
1325 return 1;
1326 }
1327
1328 CBB contents;
1329 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1330 !CBB_add_u16_length_prefixed(out, &contents) ||
1331 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1332 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1333 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1334 !CBB_flush(out)) {
1335 return 0;
1336 }
1337
1338 return 1;
1339}
1340
1341static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1342 CBS *contents) {
1343 if (contents == NULL) {
1344 return 1;
1345 }
1346
1347 if (CBS_len(contents) != 0) {
1348 return 0;
1349 }
1350
1351 ssl->s3->tmp.certificate_status_expected = 1;
1352 return 1;
1353}
1354
1355static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1356 CBS *contents) {
1357 /* OCSP stapling as a server is not supported. */
1358 return 1;
1359}
1360
1361static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
1362 /* OCSP stapling as a server is not supported. */
1363 return 1;
1364}
1365
1366
Adam Langley97dfcbf2015-07-01 18:35:20 -07001367/* Next protocol negotiation.
1368 *
1369 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1370
1371static void ext_npn_init(SSL *ssl) {
1372 ssl->s3->next_proto_neg_seen = 0;
1373}
1374
1375static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1376 if (ssl->s3->initial_handshake_complete ||
1377 ssl->ctx->next_proto_select_cb == NULL ||
1378 SSL_IS_DTLS(ssl)) {
1379 return 1;
1380 }
1381
1382 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1383 !CBB_add_u16(out, 0 /* length */)) {
1384 return 0;
1385 }
1386
1387 return 1;
1388}
1389
1390static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1391 CBS *contents) {
1392 if (contents == NULL) {
1393 return 1;
1394 }
1395
1396 /* If any of these are false then we should never have sent the NPN
1397 * extension in the ClientHello and thus this function should never have been
1398 * called. */
1399 assert(!ssl->s3->initial_handshake_complete);
1400 assert(!SSL_IS_DTLS(ssl));
1401 assert(ssl->ctx->next_proto_select_cb != NULL);
1402
1403 const uint8_t *const orig_contents = CBS_data(contents);
1404 const size_t orig_len = CBS_len(contents);
1405
1406 while (CBS_len(contents) != 0) {
1407 CBS proto;
1408 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1409 CBS_len(&proto) == 0) {
1410 return 0;
1411 }
1412 }
1413
1414 uint8_t *selected;
1415 uint8_t selected_len;
1416 if (ssl->ctx->next_proto_select_cb(
1417 ssl, &selected, &selected_len, orig_contents, orig_len,
1418 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1419 *out_alert = SSL_AD_INTERNAL_ERROR;
1420 return 0;
1421 }
1422
1423 OPENSSL_free(ssl->next_proto_negotiated);
1424 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1425 if (ssl->next_proto_negotiated == NULL) {
1426 *out_alert = SSL_AD_INTERNAL_ERROR;
1427 return 0;
1428 }
1429
1430 ssl->next_proto_negotiated_len = selected_len;
1431 ssl->s3->next_proto_neg_seen = 1;
1432
1433 return 1;
1434}
1435
1436static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1437 CBS *contents) {
1438 if (contents != NULL && CBS_len(contents) != 0) {
1439 return 0;
1440 }
1441
1442 if (contents == NULL ||
1443 ssl->s3->initial_handshake_complete ||
1444 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1445 * afterwards, parsing the ALPN extension will clear
1446 * |next_proto_neg_seen|. */
1447 ssl->s3->alpn_selected != NULL ||
1448 ssl->ctx->next_protos_advertised_cb == NULL ||
1449 SSL_IS_DTLS(ssl)) {
1450 return 1;
1451 }
1452
1453 ssl->s3->next_proto_neg_seen = 1;
1454 return 1;
1455}
1456
1457static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1458 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1459 * parsed. */
1460 if (!ssl->s3->next_proto_neg_seen) {
1461 return 1;
1462 }
1463
1464 const uint8_t *npa;
1465 unsigned npa_len;
1466
1467 if (ssl->ctx->next_protos_advertised_cb(
1468 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1469 SSL_TLSEXT_ERR_OK) {
1470 ssl->s3->next_proto_neg_seen = 0;
1471 return 1;
1472 }
1473
1474 CBB contents;
1475 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1476 !CBB_add_u16_length_prefixed(out, &contents) ||
1477 !CBB_add_bytes(&contents, npa, npa_len) ||
1478 !CBB_flush(out)) {
1479 return 0;
1480 }
1481
1482 return 1;
1483}
1484
1485
Adam Langleyab8d87d2015-07-10 12:21:39 -07001486/* Signed certificate timestamps.
1487 *
1488 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1489
1490static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1491 if (!ssl->signed_cert_timestamps_enabled) {
1492 return 1;
1493 }
1494
1495 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1496 !CBB_add_u16(out, 0 /* length */)) {
1497 return 0;
1498 }
1499
1500 return 1;
1501}
1502
1503static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1504 CBS *contents) {
1505 if (contents == NULL) {
1506 return 1;
1507 }
1508
1509 /* If this is false then we should never have sent the SCT extension in the
1510 * ClientHello and thus this function should never have been called. */
1511 assert(ssl->signed_cert_timestamps_enabled);
1512
1513 if (CBS_len(contents) == 0) {
1514 *out_alert = SSL_AD_DECODE_ERROR;
1515 return 0;
1516 }
1517
1518 /* Session resumption uses the original session information. */
1519 if (!ssl->hit &&
1520 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1521 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1522 *out_alert = SSL_AD_INTERNAL_ERROR;
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
1529static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1530 CBS *contents) {
1531 /* The SCT extension is not supported as a server. */
1532 return 1;
1533}
1534
1535static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1536 /* The SCT extension is not supported as a server. */
1537 return 1;
1538}
1539
1540
Adam Langleyf18e4532015-07-10 13:39:53 -07001541/* Application-level Protocol Negotiation.
1542 *
1543 * https://tools.ietf.org/html/rfc7301 */
1544
1545static void ext_alpn_init(SSL *ssl) {
1546 OPENSSL_free(ssl->s3->alpn_selected);
1547 ssl->s3->alpn_selected = NULL;
1548}
1549
1550static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1551 if (ssl->alpn_client_proto_list == NULL ||
1552 ssl->s3->initial_handshake_complete) {
1553 return 1;
1554 }
1555
1556 CBB contents, proto_list;
1557 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1558 !CBB_add_u16_length_prefixed(out, &contents) ||
1559 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1560 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1561 ssl->alpn_client_proto_list_len) ||
1562 !CBB_flush(out)) {
1563 return 0;
1564 }
1565
1566 return 1;
1567}
1568
1569static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1570 CBS *contents) {
1571 if (contents == NULL) {
1572 return 1;
1573 }
1574
1575 assert(!ssl->s3->initial_handshake_complete);
1576 assert(ssl->alpn_client_proto_list != NULL);
1577
1578 /* The extension data consists of a ProtocolNameList which must have
1579 * exactly one ProtocolName. Each of these is length-prefixed. */
1580 CBS protocol_name_list, protocol_name;
1581 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1582 CBS_len(contents) != 0 ||
1583 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1584 /* Empty protocol names are forbidden. */
1585 CBS_len(&protocol_name) == 0 ||
1586 CBS_len(&protocol_name_list) != 0) {
1587 return 0;
1588 }
1589
1590 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1591 &ssl->s3->alpn_selected_len)) {
1592 *out_alert = SSL_AD_INTERNAL_ERROR;
1593 return 0;
1594 }
1595
1596 return 1;
1597}
1598
1599static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1600 CBS *contents) {
1601 if (contents == NULL) {
1602 return 1;
1603 }
1604
1605 if (ssl->ctx->alpn_select_cb == NULL ||
1606 ssl->s3->initial_handshake_complete) {
1607 return 1;
1608 }
1609
1610 /* ALPN takes precedence over NPN. */
1611 ssl->s3->next_proto_neg_seen = 0;
1612
1613 CBS protocol_name_list;
1614 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1615 CBS_len(contents) != 0 ||
1616 CBS_len(&protocol_name_list) < 2) {
1617 return 0;
1618 }
1619
1620 /* Validate the protocol list. */
1621 CBS protocol_name_list_copy = protocol_name_list;
1622 while (CBS_len(&protocol_name_list_copy) > 0) {
1623 CBS protocol_name;
1624
1625 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1626 /* Empty protocol names are forbidden. */
1627 CBS_len(&protocol_name) == 0) {
1628 return 0;
1629 }
1630 }
1631
1632 const uint8_t *selected;
1633 uint8_t selected_len;
1634 if (ssl->ctx->alpn_select_cb(
1635 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1636 CBS_len(&protocol_name_list),
1637 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1638 OPENSSL_free(ssl->s3->alpn_selected);
1639 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1640 if (ssl->s3->alpn_selected == NULL) {
1641 *out_alert = SSL_AD_INTERNAL_ERROR;
1642 return 0;
1643 }
1644 ssl->s3->alpn_selected_len = selected_len;
1645 }
1646
1647 return 1;
1648}
1649
1650static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1651 if (ssl->s3->alpn_selected == NULL) {
1652 return 1;
1653 }
1654
1655 CBB contents, proto_list, proto;
1656 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1657 !CBB_add_u16_length_prefixed(out, &contents) ||
1658 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1659 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1660 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1661 !CBB_flush(out)) {
1662 return 0;
1663 }
1664
1665 return 1;
1666}
1667
1668
Adam Langley49c7af12015-07-10 14:33:46 -07001669/* Channel ID.
1670 *
1671 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1672
1673static void ext_channel_id_init(SSL *ssl) {
1674 ssl->s3->tlsext_channel_id_valid = 0;
1675}
1676
1677static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1678 if (!ssl->tlsext_channel_id_enabled ||
1679 SSL_IS_DTLS(ssl)) {
1680 return 1;
1681 }
1682
1683 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1684 !CBB_add_u16(out, 0 /* length */)) {
1685 return 0;
1686 }
1687
1688 return 1;
1689}
1690
1691static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1692 CBS *contents) {
1693 if (contents == NULL) {
1694 return 1;
1695 }
1696
1697 assert(!SSL_IS_DTLS(ssl));
1698 assert(ssl->tlsext_channel_id_enabled);
1699
1700 if (CBS_len(contents) != 0) {
1701 return 0;
1702 }
1703
1704 ssl->s3->tlsext_channel_id_valid = 1;
1705 return 1;
1706}
1707
1708static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1709 CBS *contents) {
1710 if (contents == NULL ||
1711 !ssl->tlsext_channel_id_enabled ||
1712 SSL_IS_DTLS(ssl)) {
1713 return 1;
1714 }
1715
1716 if (CBS_len(contents) != 0) {
1717 return 0;
1718 }
1719
1720 ssl->s3->tlsext_channel_id_valid = 1;
1721 return 1;
1722}
1723
1724static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1725 if (!ssl->s3->tlsext_channel_id_valid) {
1726 return 1;
1727 }
1728
1729 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1730 !CBB_add_u16(out, 0 /* length */)) {
1731 return 0;
1732 }
1733
1734 return 1;
1735}
1736
Adam Langley391250d2015-07-15 19:06:07 -07001737
1738/* Secure Real-time Transport Protocol (SRTP) extension.
1739 *
1740 * https://tools.ietf.org/html/rfc5764 */
1741
Adam Langley391250d2015-07-15 19:06:07 -07001742
1743static void ext_srtp_init(SSL *ssl) {
1744 ssl->srtp_profile = NULL;
1745}
1746
1747static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1748 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1749 if (profiles == NULL) {
1750 return 1;
1751 }
1752 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1753 if (num_profiles == 0) {
1754 return 1;
1755 }
1756
1757 CBB contents, profile_ids;
1758 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1759 !CBB_add_u16_length_prefixed(out, &contents) ||
1760 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1761 return 0;
1762 }
1763
1764 size_t i;
1765 for (i = 0; i < num_profiles; i++) {
1766 if (!CBB_add_u16(&profile_ids,
1767 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1768 return 0;
1769 }
1770 }
1771
1772 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1773 !CBB_flush(out)) {
1774 return 0;
1775 }
1776
1777 return 1;
1778}
1779
1780static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1781 CBS *contents) {
1782 if (contents == NULL) {
1783 return 1;
1784 }
1785
1786 /* The extension consists of a u16-prefixed profile ID list containing a
1787 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1788 *
1789 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1790 CBS profile_ids, srtp_mki;
1791 uint16_t profile_id;
1792 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1793 !CBS_get_u16(&profile_ids, &profile_id) ||
1794 CBS_len(&profile_ids) != 0 ||
1795 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1796 CBS_len(contents) != 0) {
1797 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1798 return 0;
1799 }
1800
1801 if (CBS_len(&srtp_mki) != 0) {
1802 /* Must be no MKI, since we never offer one. */
1803 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1804 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1805 return 0;
1806 }
1807
1808 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1809
1810 /* Check to see if the server gave us something we support (and presumably
1811 * offered). */
1812 size_t i;
1813 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1814 const SRTP_PROTECTION_PROFILE *profile =
1815 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1816
1817 if (profile->id == profile_id) {
1818 ssl->srtp_profile = profile;
1819 return 1;
1820 }
1821 }
1822
1823 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1824 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1825 return 0;
1826}
1827
1828static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1829 CBS *contents) {
1830 if (contents == NULL) {
1831 return 1;
1832 }
1833
1834 CBS profile_ids, srtp_mki;
1835 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1836 CBS_len(&profile_ids) < 2 ||
1837 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1838 CBS_len(contents) != 0) {
1839 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1840 return 0;
1841 }
1842 /* Discard the MKI value for now. */
1843
1844 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1845 SSL_get_srtp_profiles(ssl);
1846
1847 /* Pick the server's most preferred profile. */
1848 size_t i;
1849 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1850 const SRTP_PROTECTION_PROFILE *server_profile =
1851 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1852
1853 CBS profile_ids_tmp;
1854 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1855
1856 while (CBS_len(&profile_ids_tmp) > 0) {
1857 uint16_t profile_id;
1858 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1859 return 0;
1860 }
1861
1862 if (server_profile->id == profile_id) {
1863 ssl->srtp_profile = server_profile;
1864 return 1;
1865 }
1866 }
1867 }
1868
1869 return 1;
1870}
1871
1872static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1873 if (ssl->srtp_profile == NULL) {
1874 return 1;
1875 }
1876
1877 CBB contents, profile_ids;
1878 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1879 !CBB_add_u16_length_prefixed(out, &contents) ||
1880 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1881 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1882 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1883 !CBB_flush(out)) {
1884 return 0;
1885 }
1886
1887 return 1;
1888}
1889
Adam Langleybdd5d662015-07-20 16:19:08 -07001890
1891/* EC point formats.
1892 *
1893 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1894
1895static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1896 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1897 return 0;
1898 }
1899
1900 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1901
1902 size_t i;
1903 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1904 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1905
1906 const uint32_t alg_k = cipher->algorithm_mkey;
1907 const uint32_t alg_a = cipher->algorithm_auth;
1908 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1909 return 1;
1910 }
1911 }
1912
1913 return 0;
1914}
1915
Adam Langleybdd5d662015-07-20 16:19:08 -07001916static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001917 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001918 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1919 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001920 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1921 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001922 !CBB_flush(out)) {
1923 return 0;
1924 }
1925
1926 return 1;
1927}
1928
1929static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1930 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1931 return 1;
1932 }
1933
1934 return ext_ec_point_add_extension(ssl, out);
1935}
1936
1937static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1938 CBS *contents) {
1939 if (contents == NULL) {
1940 return 1;
1941 }
1942
1943 CBS ec_point_format_list;
1944 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1945 CBS_len(contents) != 0) {
1946 return 0;
1947 }
1948
David Benjaminfc059942015-07-30 23:01:59 -04001949 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1950 * point format. */
1951 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1952 CBS_len(&ec_point_format_list)) == NULL) {
1953 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001954 return 0;
1955 }
1956
1957 return 1;
1958}
1959
1960static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1961 CBS *contents) {
1962 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1963}
1964
1965static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1966 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1967 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001968 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001969
1970 if (!using_ecc) {
1971 return 1;
1972 }
1973
1974 return ext_ec_point_add_extension(ssl, out);
1975}
1976
Adam Langley273d49c2015-07-20 16:38:52 -07001977
1978/* EC supported curves.
1979 *
1980 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1981
1982static void ext_ec_curves_init(SSL *ssl) {
1983 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1984 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1985 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1986}
1987
1988static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1989 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1990 return 1;
1991 }
1992
1993 CBB contents, curves_bytes;
1994 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1995 !CBB_add_u16_length_prefixed(out, &contents) ||
1996 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1997 return 0;
1998 }
1999
2000 const uint16_t *curves;
2001 size_t curves_len;
2002 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2003
2004 size_t i;
2005 for (i = 0; i < curves_len; i++) {
2006 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2007 return 0;
2008 }
2009 }
2010
2011 return CBB_flush(out);
2012}
2013
2014static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2015 CBS *contents) {
2016 /* This extension is not expected to be echoed by servers and is ignored. */
2017 return 1;
2018}
2019
2020static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2021 CBS *contents) {
2022 if (contents == NULL) {
2023 return 1;
2024 }
2025
2026 CBS elliptic_curve_list;
2027 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2028 CBS_len(&elliptic_curve_list) == 0 ||
2029 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2030 CBS_len(contents) != 0) {
2031 return 0;
2032 }
2033
2034 ssl->s3->tmp.peer_ellipticcurvelist =
2035 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2036
2037 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2038 *out_alert = SSL_AD_INTERNAL_ERROR;
2039 return 0;
2040 }
2041
2042 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2043 size_t i;
2044 for (i = 0; i < num_curves; i++) {
2045 if (!CBS_get_u16(&elliptic_curve_list,
2046 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2047 goto err;
2048 }
2049 }
2050
2051 assert(CBS_len(&elliptic_curve_list) == 0);
2052 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2053
2054 return 1;
2055
2056err:
2057 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2058 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2059 *out_alert = SSL_AD_INTERNAL_ERROR;
2060 return 0;
2061}
2062
2063static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2064 /* Servers don't echo this extension. */
2065 return 1;
2066}
2067
2068
Adam Langley614c66a2015-06-12 15:26:58 -07002069/* kExtensions contains all the supported extensions. */
2070static const struct tls_extension kExtensions[] = {
2071 {
Adam Langley5021b222015-06-12 18:27:58 -07002072 /* The renegotiation extension must always be at index zero because the
2073 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2074 * sent as an SCSV. */
2075 TLSEXT_TYPE_renegotiate,
2076 NULL,
2077 ext_ri_add_clienthello,
2078 ext_ri_parse_serverhello,
2079 ext_ri_parse_clienthello,
2080 ext_ri_add_serverhello,
2081 },
2082 {
Adam Langley614c66a2015-06-12 15:26:58 -07002083 TLSEXT_TYPE_server_name,
2084 ext_sni_init,
2085 ext_sni_add_clienthello,
2086 ext_sni_parse_serverhello,
2087 ext_sni_parse_clienthello,
2088 ext_sni_add_serverhello,
2089 },
Adam Langley0a056712015-07-01 15:03:33 -07002090 {
2091 TLSEXT_TYPE_extended_master_secret,
2092 ext_ems_init,
2093 ext_ems_add_clienthello,
2094 ext_ems_parse_serverhello,
2095 ext_ems_parse_clienthello,
2096 ext_ems_add_serverhello,
2097 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002098 {
2099 TLSEXT_TYPE_session_ticket,
2100 NULL,
2101 ext_ticket_add_clienthello,
2102 ext_ticket_parse_serverhello,
2103 ext_ticket_parse_clienthello,
2104 ext_ticket_add_serverhello,
2105 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002106 {
2107 TLSEXT_TYPE_signature_algorithms,
2108 NULL,
2109 ext_sigalgs_add_clienthello,
2110 ext_sigalgs_parse_serverhello,
2111 ext_sigalgs_parse_clienthello,
2112 ext_sigalgs_add_serverhello,
2113 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002114 {
2115 TLSEXT_TYPE_status_request,
2116 ext_ocsp_init,
2117 ext_ocsp_add_clienthello,
2118 ext_ocsp_parse_serverhello,
2119 ext_ocsp_parse_clienthello,
2120 ext_ocsp_add_serverhello,
2121 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002122 {
2123 TLSEXT_TYPE_next_proto_neg,
2124 ext_npn_init,
2125 ext_npn_add_clienthello,
2126 ext_npn_parse_serverhello,
2127 ext_npn_parse_clienthello,
2128 ext_npn_add_serverhello,
2129 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002130 {
2131 TLSEXT_TYPE_certificate_timestamp,
2132 NULL,
2133 ext_sct_add_clienthello,
2134 ext_sct_parse_serverhello,
2135 ext_sct_parse_clienthello,
2136 ext_sct_add_serverhello,
2137 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002138 {
2139 TLSEXT_TYPE_application_layer_protocol_negotiation,
2140 ext_alpn_init,
2141 ext_alpn_add_clienthello,
2142 ext_alpn_parse_serverhello,
2143 ext_alpn_parse_clienthello,
2144 ext_alpn_add_serverhello,
2145 },
Adam Langley49c7af12015-07-10 14:33:46 -07002146 {
2147 TLSEXT_TYPE_channel_id,
2148 ext_channel_id_init,
2149 ext_channel_id_add_clienthello,
2150 ext_channel_id_parse_serverhello,
2151 ext_channel_id_parse_clienthello,
2152 ext_channel_id_add_serverhello,
2153 },
Adam Langley391250d2015-07-15 19:06:07 -07002154 {
2155 TLSEXT_TYPE_srtp,
2156 ext_srtp_init,
2157 ext_srtp_add_clienthello,
2158 ext_srtp_parse_serverhello,
2159 ext_srtp_parse_clienthello,
2160 ext_srtp_add_serverhello,
2161 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002162 {
2163 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002164 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002165 ext_ec_point_add_clienthello,
2166 ext_ec_point_parse_serverhello,
2167 ext_ec_point_parse_clienthello,
2168 ext_ec_point_add_serverhello,
2169 },
Adam Langley273d49c2015-07-20 16:38:52 -07002170 {
2171 TLSEXT_TYPE_elliptic_curves,
2172 ext_ec_curves_init,
2173 ext_ec_curves_add_clienthello,
2174 ext_ec_curves_parse_serverhello,
2175 ext_ec_curves_parse_clienthello,
2176 ext_ec_curves_add_serverhello,
2177 },
Adam Langley614c66a2015-06-12 15:26:58 -07002178};
2179
2180#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2181
Adam Langley4cfa96b2015-07-01 11:56:55 -07002182OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2183 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002184 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002185OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2186 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2187 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002188 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002189
Adam Langley614c66a2015-06-12 15:26:58 -07002190static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2191 uint16_t value) {
2192 unsigned i;
2193 for (i = 0; i < kNumExtensions; i++) {
2194 if (kExtensions[i].value == value) {
2195 *out_index = i;
2196 return &kExtensions[i];
2197 }
2198 }
2199
2200 return NULL;
2201}
2202
Adam Langley09505632015-07-30 18:10:13 -07002203int SSL_extension_supported(unsigned extension_value) {
2204 uint32_t index;
2205 return extension_value == TLSEXT_TYPE_padding ||
2206 tls_extension_find(&index, extension_value) != NULL;
2207}
2208
Adam Langleyb0c235e2014-06-20 12:00:00 -07002209/* header_len is the length of the ClientHello header written so far, used to
2210 * compute padding. It does not include the record header. Pass 0 if no padding
2211 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002212uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2213 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2215 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002216 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langley33ad2b52015-07-20 17:43:53 -07002219 CBB cbb, extensions;
2220 CBB_zero(&cbb);
2221 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2222 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2223 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 }
Adam Langley95c29f32014-06-20 12:00:00 -07002225
Adam Langley614c66a2015-06-12 15:26:58 -07002226 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002227 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langley614c66a2015-06-12 15:26:58 -07002229 size_t i;
2230 for (i = 0; i < kNumExtensions; i++) {
2231 if (kExtensions[i].init != NULL) {
2232 kExtensions[i].init(s);
2233 }
2234 }
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langley614c66a2015-06-12 15:26:58 -07002236 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002237 const size_t len_before = CBB_len(&extensions);
2238 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2239 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2240 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2241 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langley33ad2b52015-07-20 17:43:53 -07002244 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002245 s->s3->tmp.extensions.sent |= (1u << i);
2246 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 }
Adam Langley75712922014-10-10 16:23:43 -07002248
Adam Langley09505632015-07-30 18:10:13 -07002249 if (!custom_ext_add_clienthello(s, &extensions)) {
2250 goto err;
2251 }
2252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002254 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002255 if (header_len > 0xff && header_len < 0x200) {
2256 /* Add padding to workaround bugs in F5 terminators. See
2257 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2258 *
2259 * NB: because this code works out the length of all existing extensions
2260 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002261 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 /* Extensions take at least four bytes to encode. Always include least
2263 * one byte of data if including the extension. WebSphere Application
2264 * Server 7.0 is intolerant to the last extension being zero-length. */
2265 if (padding_len >= 4 + 1) {
2266 padding_len -= 4;
2267 } else {
2268 padding_len = 1;
2269 }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langley33ad2b52015-07-20 17:43:53 -07002271 uint8_t *padding_bytes;
2272 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2273 !CBB_add_u16(&extensions, padding_len) ||
2274 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2275 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 }
Adam Langley75712922014-10-10 16:23:43 -07002277
Adam Langley33ad2b52015-07-20 17:43:53 -07002278 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 }
2280 }
Adam Langley75712922014-10-10 16:23:43 -07002281
Adam Langley33ad2b52015-07-20 17:43:53 -07002282 if (!CBB_flush(&cbb)) {
2283 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 }
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langley33ad2b52015-07-20 17:43:53 -07002286 uint8_t *ret = buf;
2287 const size_t cbb_len = CBB_len(&cbb);
2288 /* If only two bytes have been written then the extensions are actually empty
2289 * and those two bytes are the zero length. In that case, we don't bother
2290 * sending the extensions length. */
2291 if (cbb_len > 2) {
2292 ret += cbb_len;
2293 }
2294
2295 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002296 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002297
2298err:
2299 CBB_cleanup(&cbb);
2300 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2301 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002302}
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langley614c66a2015-06-12 15:26:58 -07002304uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2305 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002306 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2307 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002308 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002309 }
Adam Langley95c29f32014-06-20 12:00:00 -07002310
Adam Langley33ad2b52015-07-20 17:43:53 -07002311 CBB cbb, extensions;
2312 CBB_zero(&cbb);
2313 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2314 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2315 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002316 }
2317
2318 unsigned i;
2319 for (i = 0; i < kNumExtensions; i++) {
2320 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2321 /* Don't send extensions that were not received. */
2322 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 }
Adam Langley95c29f32014-06-20 12:00:00 -07002324
Adam Langley33ad2b52015-07-20 17:43:53 -07002325 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2326 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2327 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2328 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002329 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 }
Adam Langley95c29f32014-06-20 12:00:00 -07002331
Adam Langley09505632015-07-30 18:10:13 -07002332 if (!custom_ext_add_serverhello(s, &extensions)) {
2333 goto err;
2334 }
2335
Adam Langley33ad2b52015-07-20 17:43:53 -07002336 if (!CBB_flush(&cbb)) {
2337 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 }
2339
Adam Langley33ad2b52015-07-20 17:43:53 -07002340 uint8_t *ret = buf;
2341 const size_t cbb_len = CBB_len(&cbb);
2342 /* If only two bytes have been written then the extensions are actually empty
2343 * and those two bytes are the zero length. In that case, we don't bother
2344 * sending the extensions length. */
2345 if (cbb_len > 2) {
2346 ret += cbb_len;
2347 }
2348
2349 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002351
2352err:
2353 CBB_cleanup(&cbb);
2354 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2355 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002356}
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002359 size_t i;
2360 for (i = 0; i < kNumExtensions; i++) {
2361 if (kExtensions[i].init != NULL) {
2362 kExtensions[i].init(s);
2363 }
2364 }
2365
2366 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002367 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002368 /* The renegotiation extension must always be at index zero because the
2369 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2370 * sent as an SCSV. */
2371 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002372
Adam Langleyfcf25832014-12-18 17:42:32 -08002373 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002374 if (CBS_len(cbs) != 0) {
2375 /* Decode the extensions block and check it is valid. */
2376 CBS extensions;
2377 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2378 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002379 *out_alert = SSL_AD_DECODE_ERROR;
2380 return 0;
2381 }
Adam Langley95c29f32014-06-20 12:00:00 -07002382
Adam Langley33ad2b52015-07-20 17:43:53 -07002383 while (CBS_len(&extensions) != 0) {
2384 uint16_t type;
2385 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langley33ad2b52015-07-20 17:43:53 -07002387 /* Decode the next extension. */
2388 if (!CBS_get_u16(&extensions, &type) ||
2389 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2390 *out_alert = SSL_AD_DECODE_ERROR;
2391 return 0;
2392 }
2393
2394 unsigned ext_index;
2395 const struct tls_extension *const ext =
2396 tls_extension_find(&ext_index, type);
2397
2398 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002399 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2400 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2401 return 0;
2402 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002403 continue;
2404 }
2405
Adam Langley614c66a2015-06-12 15:26:58 -07002406 s->s3->tmp.extensions.received |= (1u << ext_index);
2407 uint8_t alert = SSL_AD_DECODE_ERROR;
2408 if (!ext->parse_clienthello(s, &alert, &extension)) {
2409 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002410 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2411 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002412 return 0;
2413 }
Adam Langley614c66a2015-06-12 15:26:58 -07002414 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002415 }
Adam Langley75712922014-10-10 16:23:43 -07002416
Adam Langley614c66a2015-06-12 15:26:58 -07002417 for (i = 0; i < kNumExtensions; i++) {
2418 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2419 /* Extension wasn't observed so call the callback with a NULL
2420 * parameter. */
2421 uint8_t alert = SSL_AD_DECODE_ERROR;
2422 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002423 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2424 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002425 *out_alert = alert;
2426 return 0;
2427 }
2428 }
2429 }
2430
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 return 1;
2432}
Adam Langley95c29f32014-06-20 12:00:00 -07002433
Adam Langleyfcf25832014-12-18 17:42:32 -08002434int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2435 int alert = -1;
2436 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2437 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2438 return 0;
2439 }
Adam Langley95c29f32014-06-20 12:00:00 -07002440
Adam Langleyfcf25832014-12-18 17:42:32 -08002441 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002442 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002443 return 0;
2444 }
Adam Langley95c29f32014-06-20 12:00:00 -07002445
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 return 1;
2447}
Adam Langley95c29f32014-06-20 12:00:00 -07002448
Adam Langleyfcf25832014-12-18 17:42:32 -08002449static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002450 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002451 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002452
Adam Langley33ad2b52015-07-20 17:43:53 -07002453 if (CBS_len(cbs) != 0) {
2454 /* Decode the extensions block and check it is valid. */
2455 CBS extensions;
2456 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2457 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002458 *out_alert = SSL_AD_DECODE_ERROR;
2459 return 0;
2460 }
Adam Langley95c29f32014-06-20 12:00:00 -07002461
Adam Langley614c66a2015-06-12 15:26:58 -07002462
Adam Langley33ad2b52015-07-20 17:43:53 -07002463 while (CBS_len(&extensions) != 0) {
2464 uint16_t type;
2465 CBS extension;
2466
2467 /* Decode the next extension. */
2468 if (!CBS_get_u16(&extensions, &type) ||
2469 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2470 *out_alert = SSL_AD_DECODE_ERROR;
2471 return 0;
2472 }
2473
2474 unsigned ext_index;
2475 const struct tls_extension *const ext =
2476 tls_extension_find(&ext_index, type);
2477
Adam Langley09505632015-07-30 18:10:13 -07002478 if (ext == NULL) {
2479 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2480 return 0;
2481 }
2482 continue;
2483 }
2484
2485 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2486 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002487 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002488 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 *out_alert = SSL_AD_DECODE_ERROR;
2490 return 0;
2491 }
David Benjamin03973092014-06-24 23:27:17 -04002492
Adam Langley614c66a2015-06-12 15:26:58 -07002493 received |= (1u << ext_index);
2494
2495 uint8_t alert = SSL_AD_DECODE_ERROR;
2496 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002497 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2498 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002499 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002500 return 0;
2501 }
Adam Langley614c66a2015-06-12 15:26:58 -07002502 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 }
Adam Langley95c29f32014-06-20 12:00:00 -07002504
Adam Langley33ad2b52015-07-20 17:43:53 -07002505 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002506 for (i = 0; i < kNumExtensions; i++) {
2507 if (!(received & (1u << i))) {
2508 /* Extension wasn't observed so call the callback with a NULL
2509 * parameter. */
2510 uint8_t alert = SSL_AD_DECODE_ERROR;
2511 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002512 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2513 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002514 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 return 0;
2516 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 }
2518 }
Adam Langley95c29f32014-06-20 12:00:00 -07002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 return 1;
2521}
Adam Langley95c29f32014-06-20 12:00:00 -07002522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523static int ssl_check_clienthello_tlsext(SSL *s) {
2524 int ret = SSL_TLSEXT_ERR_NOACK;
2525 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002526
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2528 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002529
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2531 ret = s->ctx->tlsext_servername_callback(s, &al,
2532 s->ctx->tlsext_servername_arg);
2533 } else if (s->initial_ctx != NULL &&
2534 s->initial_ctx->tlsext_servername_callback != 0) {
2535 ret = s->initial_ctx->tlsext_servername_callback(
2536 s, &al, s->initial_ctx->tlsext_servername_arg);
2537 }
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 switch (ret) {
2540 case SSL_TLSEXT_ERR_ALERT_FATAL:
2541 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2542 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544 case SSL_TLSEXT_ERR_ALERT_WARNING:
2545 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2546 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002549 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002551
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 default:
2553 return 1;
2554 }
2555}
Adam Langleyed8270a2014-09-02 13:52:56 -07002556
Adam Langleyfcf25832014-12-18 17:42:32 -08002557static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002558 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002559 int al = SSL_AD_UNRECOGNIZED_NAME;
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 &&
David Benjaminb18f0242015-03-10 18:30:08 -04002565 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002566 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;
David Benjamin03973092014-06-24 23:27:17 -04002574
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;
2578
2579 default:
2580 return 1;
2581 }
2582}
2583
2584int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2585 int alert = -1;
2586 if (s->version < SSL3_VERSION) {
2587 return 1;
2588 }
2589
2590 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2591 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2592 return 0;
2593 }
2594
2595 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002596 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002597 return 0;
2598 }
2599
2600 return 1;
2601}
Adam Langley95c29f32014-06-20 12:00:00 -07002602
David Benjamine3aa1d92015-06-16 15:34:50 -04002603int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2604 int *out_send_ticket, const uint8_t *ticket,
2605 size_t ticket_len, const uint8_t *session_id,
2606 size_t session_id_len) {
2607 int ret = 1; /* Most errors are non-fatal. */
2608 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2609 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002610
David Benjamine3aa1d92015-06-16 15:34:50 -04002611 HMAC_CTX hmac_ctx;
2612 HMAC_CTX_init(&hmac_ctx);
2613 EVP_CIPHER_CTX cipher_ctx;
2614 EVP_CIPHER_CTX_init(&cipher_ctx);
2615
2616 *out_send_ticket = 0;
2617 *out_session = NULL;
2618
2619 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2620 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 }
2622
David Benjamine3aa1d92015-06-16 15:34:50 -04002623 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002625 *out_send_ticket = 1;
2626 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 }
2628
David Benjaminadcc3952015-04-26 13:07:57 -04002629 /* Ensure there is room for the key name and the largest IV
2630 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2631 * the maximum IV length should be well under the minimum size for the
2632 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002633 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2634 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002636 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002637
David Benjamine3aa1d92015-06-16 15:34:50 -04002638 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2639 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2640 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2641 0 /* decrypt */);
2642 if (cb_ret < 0) {
2643 ret = 0;
2644 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002646 if (cb_ret == 0) {
2647 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002649 if (cb_ret == 2) {
2650 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 }
2652 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002653 /* Check the key name matches. */
2654 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2655 SSL_TICKET_KEY_NAME_LEN) != 0) {
2656 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002658 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2659 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002661 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2662 ssl_ctx->tlsext_tick_aes_key, iv)) {
2663 ret = 0;
2664 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002665 }
2666 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002667 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002668
David Benjamine3aa1d92015-06-16 15:34:50 -04002669 /* Check the MAC at the end of the ticket. */
2670 uint8_t mac[EVP_MAX_MD_SIZE];
2671 size_t mac_len = HMAC_size(&hmac_ctx);
2672 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002673 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002674 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002675 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002676 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2677 HMAC_Final(&hmac_ctx, mac, NULL);
2678 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2679 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 }
2681
David Benjamine3aa1d92015-06-16 15:34:50 -04002682 /* Decrypt the session data. */
2683 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2684 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2685 mac_len;
2686 plaintext = OPENSSL_malloc(ciphertext_len);
2687 if (plaintext == NULL) {
2688 ret = 0;
2689 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002691 if (ciphertext_len >= INT_MAX) {
2692 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002694 int len1, len2;
2695 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2696 (int)ciphertext_len) ||
2697 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2698 ERR_clear_error(); /* Don't leave an error on the queue. */
2699 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 }
2701
David Benjamine3aa1d92015-06-16 15:34:50 -04002702 /* Decode the session. */
2703 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2704 if (session == NULL) {
2705 ERR_clear_error(); /* Don't leave an error on the queue. */
2706 goto done;
2707 }
2708
2709 /* Copy the client's session ID into the new session, to denote the ticket has
2710 * been accepted. */
2711 memcpy(session->session_id, session_id, session_id_len);
2712 session->session_id_length = session_id_len;
2713
2714 *out_session = session;
2715
2716done:
2717 OPENSSL_free(plaintext);
2718 HMAC_CTX_cleanup(&hmac_ctx);
2719 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2720 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002721}
Adam Langley95c29f32014-06-20 12:00:00 -07002722
2723/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002724typedef struct {
2725 int nid;
2726 int id;
2727} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2730 {NID_sha1, TLSEXT_hash_sha1},
2731 {NID_sha224, TLSEXT_hash_sha224},
2732 {NID_sha256, TLSEXT_hash_sha256},
2733 {NID_sha384, TLSEXT_hash_sha384},
2734 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002735
Adam Langleyfcf25832014-12-18 17:42:32 -08002736static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2737 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002738
Adam Langleyfcf25832014-12-18 17:42:32 -08002739static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2740 size_t i;
2741 for (i = 0; i < tlen; i++) {
2742 if (table[i].nid == nid) {
2743 return table[i].id;
2744 }
2745 }
Adam Langley95c29f32014-06-20 12:00:00 -07002746
Adam Langleyfcf25832014-12-18 17:42:32 -08002747 return -1;
2748}
Adam Langley95c29f32014-06-20 12:00:00 -07002749
David Benjaminb4d65fd2015-05-29 17:11:21 -04002750int tls12_get_sigid(int pkey_type) {
2751 return tls12_find_id(pkey_type, tls12_sig,
2752 sizeof(tls12_sig) / sizeof(tls12_lookup));
2753}
2754
David Benjamind1d80782015-07-05 11:54:09 -04002755int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002756 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 if (!md) {
2759 return 0;
2760 }
Adam Langley95c29f32014-06-20 12:00:00 -07002761
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2763 sizeof(tls12_md) / sizeof(tls12_lookup));
2764 if (md_id == -1) {
2765 return 0;
2766 }
Adam Langley95c29f32014-06-20 12:00:00 -07002767
David Benjamind1d80782015-07-05 11:54:09 -04002768 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002769 if (sig_id == -1) {
2770 return 0;
2771 }
Adam Langley95c29f32014-06-20 12:00:00 -07002772
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 p[0] = (uint8_t)md_id;
2774 p[1] = (uint8_t)sig_id;
2775 return 1;
2776}
2777
Adam Langleyfcf25832014-12-18 17:42:32 -08002778const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2779 switch (hash_alg) {
2780 case TLSEXT_hash_md5:
2781 return EVP_md5();
2782
2783 case TLSEXT_hash_sha1:
2784 return EVP_sha1();
2785
2786 case TLSEXT_hash_sha224:
2787 return EVP_sha224();
2788
2789 case TLSEXT_hash_sha256:
2790 return EVP_sha256();
2791
2792 case TLSEXT_hash_sha384:
2793 return EVP_sha384();
2794
2795 case TLSEXT_hash_sha512:
2796 return EVP_sha512();
2797
2798 default:
2799 return NULL;
2800 }
2801}
Adam Langley95c29f32014-06-20 12:00:00 -07002802
David Benjaminec2f27d2014-11-13 19:17:25 -05002803/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2804 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002805static int tls12_get_pkey_type(uint8_t sig_alg) {
2806 switch (sig_alg) {
2807 case TLSEXT_signature_rsa:
2808 return EVP_PKEY_RSA;
2809
2810 case TLSEXT_signature_ecdsa:
2811 return EVP_PKEY_EC;
2812
2813 default:
2814 return -1;
2815 }
2816}
Adam Langley95c29f32014-06-20 12:00:00 -07002817
Adam Langley95c29f32014-06-20 12:00:00 -07002818/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002819static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2820 size_t preflen, const uint8_t *allow,
2821 size_t allowlen) {
2822 const uint8_t *ptmp, *atmp;
2823 size_t i, j, nmatch = 0;
2824
2825 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2826 /* Skip disabled hashes or signature algorithms */
2827 if (tls12_get_hash(ptmp[0]) == NULL ||
2828 tls12_get_pkey_type(ptmp[1]) == -1) {
2829 continue;
2830 }
2831
2832 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2833 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2834 nmatch++;
2835 if (shsig) {
2836 shsig->rhash = ptmp[0];
2837 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 shsig++;
2839 }
2840
2841 break;
2842 }
2843 }
2844 }
2845
2846 return nmatch;
2847}
Adam Langley95c29f32014-06-20 12:00:00 -07002848
2849/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002850static int tls1_set_shared_sigalgs(SSL *s) {
2851 const uint8_t *pref, *allow, *conf;
2852 size_t preflen, allowlen, conflen;
2853 size_t nmatch;
2854 TLS_SIGALGS *salgs = NULL;
2855 CERT *c = s->cert;
2856
David Benjamin2755a3e2015-04-22 16:17:58 -04002857 OPENSSL_free(c->shared_sigalgs);
2858 c->shared_sigalgs = NULL;
2859 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002860
2861 /* If client use client signature algorithms if not NULL */
2862 if (!s->server && c->client_sigalgs) {
2863 conf = c->client_sigalgs;
2864 conflen = c->client_sigalgslen;
2865 } else if (c->conf_sigalgs) {
2866 conf = c->conf_sigalgs;
2867 conflen = c->conf_sigalgslen;
2868 } else {
2869 conflen = tls12_get_psigalgs(s, &conf);
2870 }
2871
2872 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2873 pref = conf;
2874 preflen = conflen;
2875 allow = c->peer_sigalgs;
2876 allowlen = c->peer_sigalgslen;
2877 } else {
2878 allow = conf;
2879 allowlen = conflen;
2880 pref = c->peer_sigalgs;
2881 preflen = c->peer_sigalgslen;
2882 }
2883
2884 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2885 if (!nmatch) {
2886 return 1;
2887 }
2888
2889 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2890 if (!salgs) {
2891 return 0;
2892 }
2893
2894 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2895 c->shared_sigalgs = salgs;
2896 c->shared_sigalgslen = nmatch;
2897 return 1;
2898}
Adam Langley95c29f32014-06-20 12:00:00 -07002899
2900/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002901int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2902 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002903
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 /* Extension ignored for inappropriate versions */
2905 if (!SSL_USE_SIGALGS(s)) {
2906 return 1;
2907 }
David Benjamincd996942014-07-20 16:23:51 -04002908
David Benjamin61c0d4e2015-03-19 14:24:37 -04002909 if (CBS_len(sigalgs) % 2 != 0 ||
2910 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2911 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 return 0;
2913 }
Adam Langley95c29f32014-06-20 12:00:00 -07002914
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 return 1;
2916}
David Benjaminec2f27d2014-11-13 19:17:25 -05002917
David Benjamind1d80782015-07-05 11:54:09 -04002918const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2919 CERT *cert = ssl->cert;
2920 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002922
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002924 for (i = 0; i < cert->shared_sigalgslen; i++) {
2925 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002926 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002927 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2928 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002929 continue;
2930 }
2931 return md;
2932 }
Adam Langley95c29f32014-06-20 12:00:00 -07002933
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 /* If no suitable digest may be found, default to SHA-1. */
2935 return EVP_sha1();
2936}
Adam Langley95c29f32014-06-20 12:00:00 -07002937
David Benjamind6a4ae92015-08-06 11:10:51 -04002938int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2939 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002940 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002941
2942 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002943 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2944 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002946
David Benjamind6a4ae92015-08-06 11:10:51 -04002947 static const char kClientIDMagic[] = "TLS Channel ID signature";
2948 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2949
2950 if (ssl->hit) {
2951 static const char kResumptionMagic[] = "Resumption";
2952 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2953 if (ssl->session->original_handshake_hash_len == 0) {
2954 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2955 goto err;
2956 }
2957 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2958 ssl->session->original_handshake_hash_len);
2959 }
2960
2961 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2962 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2963 sizeof(handshake_hash));
2964 if (handshake_hash_len < 0) {
2965 goto err;
2966 }
2967 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2968 unsigned len_u;
2969 EVP_DigestFinal_ex(&ctx, out, &len_u);
2970 *out_len = len_u;
2971
2972 ret = 1;
2973
2974err:
2975 EVP_MD_CTX_cleanup(&ctx);
2976 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002977}
Adam Langley1258b6a2014-06-20 12:00:00 -07002978
2979/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2980 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002981int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2982 int digest_len;
2983 /* This function should never be called for a resumed session because the
2984 * handshake hashes that we wish to record are for the original, full
2985 * handshake. */
2986 if (s->hit) {
2987 return -1;
2988 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002989
Adam Langleyfcf25832014-12-18 17:42:32 -08002990 digest_len =
2991 tls1_handshake_digest(s, s->session->original_handshake_hash,
2992 sizeof(s->session->original_handshake_hash));
2993 if (digest_len < 0) {
2994 return -1;
2995 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002996
Adam Langleyfcf25832014-12-18 17:42:32 -08002997 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002998
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 return 1;
3000}
Adam Langley95c29f32014-06-20 12:00:00 -07003001
Adam Langleyfcf25832014-12-18 17:42:32 -08003002int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3003 int client) {
3004 uint8_t *sigalgs, *sptr;
3005 int rhash, rsign;
3006 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07003007
Adam Langleyfcf25832014-12-18 17:42:32 -08003008 if (salglen & 1) {
3009 return 0;
3010 }
Adam Langley95c29f32014-06-20 12:00:00 -07003011
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 sigalgs = OPENSSL_malloc(salglen);
3013 if (sigalgs == NULL) {
3014 return 0;
3015 }
Adam Langley95c29f32014-06-20 12:00:00 -07003016
Adam Langleyfcf25832014-12-18 17:42:32 -08003017 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3018 rhash = tls12_find_id(*psig_nids++, tls12_md,
3019 sizeof(tls12_md) / sizeof(tls12_lookup));
3020 rsign = tls12_find_id(*psig_nids++, tls12_sig,
3021 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07003022
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 if (rhash == -1 || rsign == -1) {
3024 goto err;
3025 }
3026 *sptr++ = rhash;
3027 *sptr++ = rsign;
3028 }
3029
3030 if (client) {
David Benjamin2755a3e2015-04-22 16:17:58 -04003031 OPENSSL_free(c->client_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003032 c->client_sigalgs = sigalgs;
3033 c->client_sigalgslen = salglen;
3034 } else {
David Benjamin2755a3e2015-04-22 16:17:58 -04003035 OPENSSL_free(c->conf_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003036 c->conf_sigalgs = sigalgs;
3037 c->conf_sigalgslen = salglen;
3038 }
3039
3040 return 1;
3041
3042err:
3043 OPENSSL_free(sigalgs);
3044 return 0;
3045}