blob: eb1e569156ffd2716970da463b3310d6124c005c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400114#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116
David Benjamin03973092014-06-24 23:27:17 -0400117#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400118#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/evp.h>
121#include <openssl/hmac.h>
122#include <openssl/mem.h>
123#include <openssl/obj.h>
124#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700125#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin6c7aed02014-08-27 16:42:38 -0400130static int ssl_check_clienthello_tlsext(SSL *s);
131static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin338fcaf2014-12-11 01:20:52 -0500133const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500134 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 tls1_setup_key_block,
136 tls1_generate_master_secret,
137 tls1_change_cipher_state,
138 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800139 tls1_cert_verify_mac,
140 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
141 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
142 tls1_alert_code,
143 tls1_export_keying_material,
144 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800145};
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin338fcaf2014-12-11 01:20:52 -0500147const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500148 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 tls1_cert_verify_mac,
154 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
155 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
156 tls1_alert_code,
157 tls1_export_keying_material,
158 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159};
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin338fcaf2014-12-11 01:20:52 -0500161const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500162 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 tls1_setup_key_block,
164 tls1_generate_master_secret,
165 tls1_change_cipher_state,
166 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 tls1_cert_verify_mac,
168 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
169 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
170 tls1_alert_code,
171 tls1_export_keying_material,
David Benjaminc7817d82015-11-05 18:28:33 -0500172 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF,
Adam Langleyfcf25832014-12-18 17:42:32 -0800173};
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175static int compare_uint16_t(const void *p1, const void *p2) {
176 uint16_t u1 = *((const uint16_t *)p1);
177 uint16_t u2 = *((const uint16_t *)p2);
178 if (u1 < u2) {
179 return -1;
180 } else if (u1 > u2) {
181 return 1;
182 } else {
183 return 0;
184 }
185}
David Benjamin35a7a442014-07-05 00:23:20 -0400186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
188 * more than one extension of the same type in a ClientHello or ServerHello.
189 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400190 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800191static int tls1_check_duplicate_extensions(const CBS *cbs) {
192 CBS extensions = *cbs;
193 size_t num_extensions = 0, i = 0;
194 uint16_t *extension_types = NULL;
195 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 /* First pass: count the extensions. */
198 while (CBS_len(&extensions) > 0) {
199 uint16_t type;
200 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 if (!CBS_get_u16(&extensions, &type) ||
203 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
204 goto done;
205 }
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 num_extensions++;
208 }
David Benjamin35a7a442014-07-05 00:23:20 -0400209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (num_extensions == 0) {
211 return 1;
212 }
David Benjamin9a373592014-07-25 04:27:53 -0400213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 extension_types =
215 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
216 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400217 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 goto done;
219 }
David Benjamin35a7a442014-07-05 00:23:20 -0400220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 /* Second pass: gather the extension types. */
222 extensions = *cbs;
223 for (i = 0; i < num_extensions; i++) {
224 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
227 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
228 /* This should not happen. */
229 goto done;
230 }
231 }
232 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 /* Sort the extensions and make sure there are no duplicates. */
235 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
236 for (i = 1; i < num_extensions; i++) {
237 if (extension_types[i - 1] == extension_types[i]) {
238 goto done;
239 }
240 }
David Benjamin35a7a442014-07-05 00:23:20 -0400241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 ret = 1;
243
David Benjamin35a7a442014-07-05 00:23:20 -0400244done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400245 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 return ret;
247}
David Benjamin35a7a442014-07-05 00:23:20 -0400248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
250 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (/* Skip client version. */
255 !CBS_skip(&client_hello, 2) ||
256 /* Skip client nonce. */
257 !CBS_skip(&client_hello, 32) ||
258 /* Extract session_id. */
259 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
260 return 0;
261 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 ctx->session_id = CBS_data(&session_id);
264 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 /* Skip past DTLS cookie */
267 if (SSL_IS_DTLS(ctx->ssl)) {
268 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
271 return 0;
272 }
273 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 /* Extract cipher_suites. */
276 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
277 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
278 return 0;
279 }
280 ctx->cipher_suites = CBS_data(&cipher_suites);
281 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 /* Extract compression_methods. */
284 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
285 CBS_len(&compression_methods) < 1) {
286 return 0;
287 }
288 ctx->compression_methods = CBS_data(&compression_methods);
289 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 /* If the ClientHello ends here then it's valid, but doesn't have any
292 * extensions. (E.g. SSLv3.) */
293 if (CBS_len(&client_hello) == 0) {
294 ctx->extensions = NULL;
295 ctx->extensions_len = 0;
296 return 1;
297 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 /* Extract extensions and check it is valid. */
300 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
301 !tls1_check_duplicate_extensions(&extensions) ||
302 CBS_len(&client_hello) != 0) {
303 return 0;
304 }
305 ctx->extensions = CBS_data(&extensions);
306 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 return 1;
309}
Adam Langleydc9b1412014-06-20 12:00:00 -0700310
David Benjamind4c2bce2015-10-17 12:28:18 -0400311int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
313 const uint8_t **out_data, size_t *out_len) {
314 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 while (CBS_len(&extensions) != 0) {
319 uint16_t type;
320 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 /* Decode the next extension. */
323 if (!CBS_get_u16(&extensions, &type) ||
324 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
325 return 0;
326 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 if (type == extension_type) {
329 *out_data = CBS_data(&extension);
330 *out_len = CBS_len(&extension);
331 return 1;
332 }
333 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700334
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 return 0;
336}
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338static const uint16_t eccurves_default[] = {
David Benjamin4298d772015-12-19 00:18:25 -0500339 SSL_CURVE_SECP256R1,
340 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700341#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin4298d772015-12-19 00:18:25 -0500342 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700343#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800344};
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langleyfcf25832014-12-18 17:42:32 -0800346/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
347 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
348 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400349static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 const uint16_t **out_curve_ids,
351 size_t *out_curve_ids_len) {
352 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400353 /* Only clients send a curve list, so this function is only called
354 * on the server. */
355 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
357 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
358 return;
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 *out_curve_ids = s->tlsext_ellipticcurvelist;
362 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
363 if (!*out_curve_ids) {
364 *out_curve_ids = eccurves_default;
365 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
366 }
367}
David Benjamined439582014-07-14 19:13:02 -0400368
David Benjamin4298d772015-12-19 00:18:25 -0500369int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
David Benjamin55a43642015-04-20 14:45:55 -0400370 const uint16_t *curves, *peer_curves, *pref, *supp;
371 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500374 if (ssl->server == 0) {
375 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377
David Benjamin4298d772015-12-19 00:18:25 -0500378 tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len);
379 tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800380
David Benjamin55a43642015-04-20 14:45:55 -0400381 if (peer_curves_len == 0) {
382 /* Clients are not required to send a supported_curves extension. In this
383 * case, the server is free to pick any curve it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500384 * section 4, paragraph 3.
385 *
386 * However, in the interests of compatibility, we will skip ECDH if the
387 * client didn't send an extension because we can't be sure that they'll
388 * support our favoured curve. */
389 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400390 }
391
David Benjamin4298d772015-12-19 00:18:25 -0500392 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamin55a43642015-04-20 14:45:55 -0400393 pref = curves;
394 pref_len = curves_len;
395 supp = peer_curves;
396 supp_len = peer_curves_len;
397 } else {
398 pref = peer_curves;
399 pref_len = peer_curves_len;
400 supp = curves;
401 supp_len = curves_len;
402 }
403
404 for (i = 0; i < pref_len; i++) {
405 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (pref[i] == supp[j]) {
David Benjamin4298d772015-12-19 00:18:25 -0500407 *out_curve_id = pref[i];
408 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 }
410 }
411 }
412
David Benjamin4298d772015-12-19 00:18:25 -0500413 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800414}
Adam Langley95c29f32014-06-20 12:00:00 -0700415
David Benjamin072334d2014-07-13 16:24:27 -0400416int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 const int *curves, size_t ncurves) {
418 uint16_t *curve_ids;
419 size_t i;
420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
422 if (curve_ids == NULL) {
423 return 0;
424 }
425
426 for (i = 0; i < ncurves; i++) {
David Benjamin4298d772015-12-19 00:18:25 -0500427 if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 OPENSSL_free(curve_ids);
429 return 0;
430 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 }
432
David Benjamin2755a3e2015-04-22 16:17:58 -0400433 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 *out_curve_ids = curve_ids;
435 *out_curve_ids_len = ncurves;
436
437 return 1;
438}
Adam Langley95c29f32014-06-20 12:00:00 -0700439
David Benjamin072334d2014-07-13 16:24:27 -0400440/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
441 * TLS curve ID and point format, respectively, for |ec|. It returns one on
442 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800443static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
444 uint8_t *out_comp_id, EC_KEY *ec) {
445 int nid;
446 uint16_t id;
447 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 if (ec == NULL) {
450 return 0;
451 }
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 grp = EC_KEY_get0_group(ec);
454 if (grp == NULL) {
455 return 0;
456 }
David Benjamin072334d2014-07-13 16:24:27 -0400457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 /* Determine curve ID */
459 nid = EC_GROUP_get_curve_name(grp);
David Benjamin4298d772015-12-19 00:18:25 -0500460 if (!ssl_nid_to_curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 return 0;
462 }
David Benjamin072334d2014-07-13 16:24:27 -0400463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
465 *out_curve_id = id;
466
467 if (out_comp_id) {
468 if (EC_KEY_get0_public_key(ec) == NULL) {
469 return 0;
470 }
471 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
472 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
473 } else {
474 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
475 }
476 }
477
478 return 1;
479}
David Benjamin072334d2014-07-13 16:24:27 -0400480
Adam Langleyfcf25832014-12-18 17:42:32 -0800481/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
482 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400483 * preferences are checked; the peer (the server) does not send preferences. */
David Benjamin4298d772015-12-19 00:18:25 -0500484int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400486 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400489 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
David Benjamin4298d772015-12-19 00:18:25 -0500490 if (get_peer_curves && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400491 /* Servers do not present a preference list so, if we are a client, only
492 * check our list. */
493 continue;
494 }
495
David Benjamin4298d772015-12-19 00:18:25 -0500496 tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len);
David Benjamin55a43642015-04-20 14:45:55 -0400497 if (get_peer_curves && curves_len == 0) {
498 /* Clients are not required to send a supported_curves extension. In this
499 * case, the server is free to pick any curve it likes. See RFC 4492,
500 * section 4, paragraph 3. */
501 continue;
502 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 for (i = 0; i < curves_len; i++) {
504 if (curves[i] == curve_id) {
505 break;
506 }
507 }
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 if (i == curves_len) {
510 return 0;
511 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 }
David Benjamin033e5f42014-11-13 18:47:41 -0500513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 return 1;
515}
David Benjamin033e5f42014-11-13 18:47:41 -0500516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517int tls1_check_ec_cert(SSL *s, X509 *x) {
518 int ret = 0;
519 EVP_PKEY *pkey = X509_get_pubkey(x);
520 uint16_t curve_id;
521 uint8_t comp_id;
522
David Benjamin758d1272015-11-20 17:47:25 -0500523 if (!pkey) {
524 goto done;
525 }
526 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
527 if (ec_key == NULL ||
528 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400530 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 goto done;
532 }
533
534 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500535
536done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400537 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 return ret;
539}
David Benjamin42e9a772014-09-02 23:18:44 -0400540
Adam Langley95c29f32014-06-20 12:00:00 -0700541/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langley95c29f32014-06-20 12:00:00 -0700544#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langley95c29f32014-06-20 12:00:00 -0700546#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langleyfcf25832014-12-18 17:42:32 -0800548#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamincff64722014-08-19 19:54:46 -0400550static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 tlsext_sigalg(TLSEXT_hash_sha512)
552 tlsext_sigalg(TLSEXT_hash_sha384)
553 tlsext_sigalg(TLSEXT_hash_sha256)
554 tlsext_sigalg(TLSEXT_hash_sha224)
555 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700556};
David Benjamin05da6e12014-07-12 20:42:55 -0400557
Adam Langleyfcf25832014-12-18 17:42:32 -0800558size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400559 *psigs = tls12_sigalgs;
560 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800561}
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamin6e807652015-11-02 12:02:20 -0500563int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
564 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 const uint8_t *sent_sigs;
566 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400567 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800568
569 /* Should never happen */
570 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400571 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 *out_alert = SSL_AD_INTERNAL_ERROR;
573 return 0;
574 }
575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 /* Check key type is consistent with signature */
577 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400578 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
580 return 0;
581 }
582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500584 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
586 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
587 break;
588 }
589 }
590
David Benjamin788be4a2015-10-30 17:50:57 -0400591 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400592 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
594 return 0;
595 }
596
597 *out_md = tls12_get_hash(hash);
598 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400599 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
601 return 0;
602 }
603
604 return 1;
605}
606
607/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
608 * supported or doesn't appear in supported signature algorithms. Unlike
609 * ssl_cipher_get_disabled this applies to a specific session and not global
610 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500611void ssl_set_client_disabled(SSL *ssl) {
612 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 const uint8_t *sigalgs;
614 size_t i, sigalgslen;
615 int have_rsa = 0, have_ecdsa = 0;
616 c->mask_a = 0;
617 c->mask_k = 0;
618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 /* Now go through all signature algorithms seeing if we support any for RSA,
620 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500621 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
623 switch (sigalgs[1]) {
624 case TLSEXT_signature_rsa:
625 have_rsa = 1;
626 break;
627
628 case TLSEXT_signature_ecdsa:
629 have_ecdsa = 1;
630 break;
631 }
632 }
633
634 /* Disable auth if we don't include any appropriate signature algorithms. */
635 if (!have_rsa) {
636 c->mask_a |= SSL_aRSA;
637 }
638 if (!have_ecdsa) {
639 c->mask_a |= SSL_aECDSA;
640 }
641
642 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500643 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 c->mask_a |= SSL_aPSK;
645 c->mask_k |= SSL_kPSK;
646 }
647}
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langley614c66a2015-06-12 15:26:58 -0700649/* tls_extension represents a TLS extension that is handled internally. The
650 * |init| function is called for each handshake, before any other functions of
651 * the extension. Then the add and parse callbacks are called as needed.
652 *
653 * The parse callbacks receive a |CBS| that contains the contents of the
654 * extension (i.e. not including the type and length bytes). If an extension is
655 * not received then the parse callbacks will be called with a NULL CBS so that
656 * they can do any processing needed to handle the absence of an extension.
657 *
658 * The add callbacks receive a |CBB| to which the extension can be appended but
659 * the function is responsible for appending the type and length bytes too.
660 *
661 * All callbacks return one for success and zero for error. If a parse function
662 * returns zero then a fatal alert with value |*out_alert| will be sent. If
663 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
664struct tls_extension {
665 uint16_t value;
666 void (*init)(SSL *ssl);
667
668 int (*add_clienthello)(SSL *ssl, CBB *out);
669 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
670
671 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
672 int (*add_serverhello)(SSL *ssl, CBB *out);
673};
674
675
676/* Server name indication (SNI).
677 *
678 * https://tools.ietf.org/html/rfc6066#section-3. */
679
680static void ext_sni_init(SSL *ssl) {
681 ssl->s3->tmp.should_ack_sni = 0;
682}
683
684static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
685 if (ssl->tlsext_hostname == NULL) {
686 return 1;
687 }
688
689 CBB contents, server_name_list, name;
690 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
691 !CBB_add_u16_length_prefixed(out, &contents) ||
692 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
693 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
694 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
695 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
696 strlen(ssl->tlsext_hostname)) ||
697 !CBB_flush(out)) {
698 return 0;
699 }
700
701 return 1;
702}
703
704static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
705 if (contents == NULL) {
706 return 1;
707 }
708
709 if (CBS_len(contents) != 0) {
710 return 0;
711 }
712
713 assert(ssl->tlsext_hostname != NULL);
714
715 if (!ssl->hit) {
716 assert(ssl->session->tlsext_hostname == NULL);
717 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
718 if (!ssl->session->tlsext_hostname) {
719 *out_alert = SSL_AD_INTERNAL_ERROR;
720 return 0;
721 }
722 }
723
724 return 1;
725}
726
727static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
728 if (contents == NULL) {
729 return 1;
730 }
731
732 /* The servername extension is treated as follows:
733 *
734 * - Only the hostname type is supported with a maximum length of 255.
735 * - The servername is rejected if too long or if it contains zeros, in
736 * which case an fatal alert is generated.
737 * - The servername field is maintained together with the session cache.
738 * - When a session is resumed, the servername callback is invoked in order
739 * to allow the application to position itself to the right context.
740 * - The servername is acknowledged if it is new for a session or when
741 * it is identical to a previously used for the same session.
742 * Applications can control the behaviour. They can at any time
743 * set a 'desirable' servername for a new SSL object. This can be the
744 * case for example with HTTPS when a Host: header field is received and
745 * a renegotiation is requested. In this case, a possible servername
746 * presented in the new client hello is only acknowledged if it matches
747 * the value of the Host: field.
748 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
749 * if they provide for changing an explicit servername context for the
750 * session,
751 * i.e. when the session has been established with a servername extension.
752 */
753
754 CBS server_name_list;
755 char have_seen_host_name = 0;
756
757 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
758 CBS_len(&server_name_list) == 0 ||
759 CBS_len(contents) != 0) {
760 return 0;
761 }
762
763 /* Decode each ServerName in the extension. */
764 while (CBS_len(&server_name_list) > 0) {
765 uint8_t name_type;
766 CBS host_name;
767
768 if (!CBS_get_u8(&server_name_list, &name_type) ||
769 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
770 return 0;
771 }
772
773 /* Only host_name is supported. */
774 if (name_type != TLSEXT_NAMETYPE_host_name) {
775 continue;
776 }
777
778 if (have_seen_host_name) {
779 /* The ServerNameList MUST NOT contain more than one name of the same
780 * name_type. */
781 return 0;
782 }
783
784 have_seen_host_name = 1;
785
786 if (CBS_len(&host_name) == 0 ||
787 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
788 CBS_contains_zero_byte(&host_name)) {
789 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
790 return 0;
791 }
792
793 if (!ssl->hit) {
794 assert(ssl->session->tlsext_hostname == NULL);
795 if (ssl->session->tlsext_hostname) {
796 /* This should be impossible. */
797 return 0;
798 }
799
800 /* Copy the hostname as a string. */
801 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
802 *out_alert = SSL_AD_INTERNAL_ERROR;
803 return 0;
804 }
805
806 ssl->s3->tmp.should_ack_sni = 1;
807 }
808 }
809
810 return 1;
811}
812
813static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
814 if (ssl->hit ||
815 !ssl->s3->tmp.should_ack_sni ||
816 ssl->session->tlsext_hostname == NULL) {
817 return 1;
818 }
819
820 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
821 !CBB_add_u16(out, 0 /* length */)) {
822 return 0;
823 }
824
825 return 1;
826}
827
828
Adam Langley5021b222015-06-12 18:27:58 -0700829/* Renegotiation indication.
830 *
831 * https://tools.ietf.org/html/rfc5746 */
832
833static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
834 CBB contents, prev_finished;
835 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
836 !CBB_add_u16_length_prefixed(out, &contents) ||
837 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
838 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
839 ssl->s3->previous_client_finished_len) ||
840 !CBB_flush(out)) {
841 return 0;
842 }
843
844 return 1;
845}
846
847static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
848 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500849 /* Servers may not switch between omitting the extension and supporting it.
850 * See RFC 5746, sections 3.5 and 4.2. */
851 if (ssl->s3->initial_handshake_complete &&
852 (contents != NULL) != ssl->s3->send_connection_binding) {
853 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
854 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
855 return 0;
856 }
857
Adam Langley5021b222015-06-12 18:27:58 -0700858 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500859 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700860 * RI even on initial ServerHello because the client doesn't see any
861 * renegotiation during an attack. However this would mean we could not
862 * connect to any server which doesn't support RI.
863 *
David Benjamine9cddb82015-11-23 14:36:40 -0500864 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
865 * practical terms every client sets it so it's just assumed here. */
866 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700867 }
868
869 const size_t expected_len = ssl->s3->previous_client_finished_len +
870 ssl->s3->previous_server_finished_len;
871
872 /* Check for logic errors */
873 assert(!expected_len || ssl->s3->previous_client_finished_len);
874 assert(!expected_len || ssl->s3->previous_server_finished_len);
875
876 /* Parse out the extension contents. */
877 CBS renegotiated_connection;
878 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
879 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400880 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700881 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
882 return 0;
883 }
884
885 /* Check that the extension matches. */
886 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400887 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700888 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
889 return 0;
890 }
891
892 const uint8_t *d = CBS_data(&renegotiated_connection);
893 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
894 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400895 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700896 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
897 return 0;
898 }
899 d += ssl->s3->previous_client_finished_len;
900
901 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
902 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400903 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700904 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
905 return 0;
906 }
907 ssl->s3->send_connection_binding = 1;
908
909 return 1;
910}
911
912static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
913 CBS *contents) {
914 /* Renegotiation isn't supported as a server so this function should never be
915 * called after the initial handshake. */
916 assert(!ssl->s3->initial_handshake_complete);
917
918 CBS fake_contents;
919 static const uint8_t kFakeExtension[] = {0};
920
921 if (contents == NULL) {
922 if (ssl->s3->send_connection_binding) {
923 /* The renegotiation SCSV was received so pretend that we received a
924 * renegotiation extension. */
925 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
926 contents = &fake_contents;
927 /* We require that the renegotiation extension is at index zero of
928 * kExtensions. */
929 ssl->s3->tmp.extensions.received |= (1u << 0);
930 } else {
931 return 1;
932 }
933 }
934
935 CBS renegotiated_connection;
936
937 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
938 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400939 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700940 return 0;
941 }
942
943 /* Check that the extension matches */
944 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
945 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400946 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700947 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
948 return 0;
949 }
950
951 ssl->s3->send_connection_binding = 1;
952
953 return 1;
954}
955
956static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
957 CBB contents, prev_finished;
958 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
959 !CBB_add_u16_length_prefixed(out, &contents) ||
960 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
961 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
962 ssl->s3->previous_client_finished_len) ||
963 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
964 ssl->s3->previous_server_finished_len) ||
965 !CBB_flush(out)) {
966 return 0;
967 }
968
969 return 1;
970}
971
Adam Langley0a056712015-07-01 15:03:33 -0700972
973/* Extended Master Secret.
974 *
975 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
976
977static void ext_ems_init(SSL *ssl) {
978 ssl->s3->tmp.extended_master_secret = 0;
979}
980
981static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
982 if (ssl->version == SSL3_VERSION) {
983 return 1;
984 }
985
986 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
987 !CBB_add_u16(out, 0 /* length */)) {
988 return 0;
989 }
990
991 return 1;
992}
993
994static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
995 CBS *contents) {
996 if (contents == NULL) {
997 return 1;
998 }
999
1000 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1001 return 0;
1002 }
1003
1004 ssl->s3->tmp.extended_master_secret = 1;
1005 return 1;
1006}
1007
1008static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1009 if (ssl->version == SSL3_VERSION || contents == NULL) {
1010 return 1;
1011 }
1012
1013 if (CBS_len(contents) != 0) {
1014 return 0;
1015 }
1016
1017 ssl->s3->tmp.extended_master_secret = 1;
1018 return 1;
1019}
1020
1021static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1022 if (!ssl->s3->tmp.extended_master_secret) {
1023 return 1;
1024 }
1025
1026 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1027 !CBB_add_u16(out, 0 /* length */)) {
1028 return 0;
1029 }
1030
1031 return 1;
1032}
1033
Adam Langley9b05bc52015-07-01 15:25:33 -07001034
1035/* Session tickets.
1036 *
1037 * https://tools.ietf.org/html/rfc5077 */
1038
1039static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1040 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1041 return 1;
1042 }
1043
1044 const uint8_t *ticket_data = NULL;
1045 int ticket_len = 0;
1046
1047 /* Renegotiation does not participate in session resumption. However, still
1048 * advertise the extension to avoid potentially breaking servers which carry
1049 * over the state from the previous handshake, such as OpenSSL servers
1050 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1051 if (!ssl->s3->initial_handshake_complete &&
1052 ssl->session != NULL &&
1053 ssl->session->tlsext_tick != NULL) {
1054 ticket_data = ssl->session->tlsext_tick;
1055 ticket_len = ssl->session->tlsext_ticklen;
1056 }
1057
1058 CBB ticket;
1059 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1060 !CBB_add_u16_length_prefixed(out, &ticket) ||
1061 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1062 !CBB_flush(out)) {
1063 return 0;
1064 }
1065
1066 return 1;
1067}
1068
1069static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1070 CBS *contents) {
1071 ssl->tlsext_ticket_expected = 0;
1072
1073 if (contents == NULL) {
1074 return 1;
1075 }
1076
1077 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1078 * this function should never be called, even if the server tries to send the
1079 * extension. */
1080 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1081
1082 if (CBS_len(contents) != 0) {
1083 return 0;
1084 }
1085
1086 ssl->tlsext_ticket_expected = 1;
1087 return 1;
1088}
1089
1090static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1091 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001092 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001093 return 1;
1094}
1095
1096static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1097 if (!ssl->tlsext_ticket_expected) {
1098 return 1;
1099 }
1100
1101 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1102 * true. */
1103 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1104
1105 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1106 !CBB_add_u16(out, 0 /* length */)) {
1107 return 0;
1108 }
1109
1110 return 1;
1111}
1112
1113
Adam Langley2e857bd2015-07-01 16:09:19 -07001114/* Signature Algorithms.
1115 *
1116 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1117
1118static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1119 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1120 return 1;
1121 }
1122
1123 const uint8_t *sigalgs_data;
1124 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1125
1126 CBB contents, sigalgs;
1127 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1128 !CBB_add_u16_length_prefixed(out, &contents) ||
1129 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1130 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1131 !CBB_flush(out)) {
1132 return 0;
1133 }
1134
1135 return 1;
1136}
1137
1138static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1139 CBS *contents) {
1140 if (contents != NULL) {
1141 /* Servers MUST NOT send this extension. */
1142 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001143 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001144 return 0;
1145 }
1146
1147 return 1;
1148}
1149
1150static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1151 CBS *contents) {
1152 OPENSSL_free(ssl->cert->peer_sigalgs);
1153 ssl->cert->peer_sigalgs = NULL;
1154 ssl->cert->peer_sigalgslen = 0;
1155
Adam Langley2e857bd2015-07-01 16:09:19 -07001156 if (contents == NULL) {
1157 return 1;
1158 }
1159
1160 CBS supported_signature_algorithms;
1161 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001162 CBS_len(contents) != 0 ||
1163 CBS_len(&supported_signature_algorithms) == 0 ||
1164 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001165 return 0;
1166 }
1167
1168 return 1;
1169}
1170
1171static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1172 /* Servers MUST NOT send this extension. */
1173 return 1;
1174}
1175
1176
Adam Langleybb0bd042015-07-01 16:21:03 -07001177/* OCSP Stapling.
1178 *
1179 * https://tools.ietf.org/html/rfc6066#section-8 */
1180
1181static void ext_ocsp_init(SSL *ssl) {
1182 ssl->s3->tmp.certificate_status_expected = 0;
1183}
1184
1185static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1186 if (!ssl->ocsp_stapling_enabled) {
1187 return 1;
1188 }
1189
1190 CBB contents;
1191 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1192 !CBB_add_u16_length_prefixed(out, &contents) ||
1193 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1194 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1195 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1196 !CBB_flush(out)) {
1197 return 0;
1198 }
1199
1200 return 1;
1201}
1202
1203static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001204 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001205 if (contents == NULL) {
1206 return 1;
1207 }
1208
1209 if (CBS_len(contents) != 0) {
1210 return 0;
1211 }
1212
1213 ssl->s3->tmp.certificate_status_expected = 1;
1214 return 1;
1215}
1216
1217static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1218 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001219 if (contents == NULL) {
1220 return 1;
1221 }
1222
1223 uint8_t status_type;
1224 if (!CBS_get_u8(contents, &status_type)) {
1225 return 0;
1226 }
1227
1228 /* We cannot decide whether OCSP stapling will occur yet because the correct
1229 * SSL_CTX might not have been selected. */
1230 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1231
Adam Langleybb0bd042015-07-01 16:21:03 -07001232 return 1;
1233}
1234
1235static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001236 /* The extension shouldn't be sent when resuming sessions. */
1237 if (ssl->hit ||
1238 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001239 ssl->ctx->ocsp_response_length == 0) {
1240 return 1;
1241 }
1242
1243 ssl->s3->tmp.certificate_status_expected = 1;
1244
1245 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1246 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001247}
1248
1249
Adam Langley97dfcbf2015-07-01 18:35:20 -07001250/* Next protocol negotiation.
1251 *
1252 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1253
1254static void ext_npn_init(SSL *ssl) {
1255 ssl->s3->next_proto_neg_seen = 0;
1256}
1257
1258static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1259 if (ssl->s3->initial_handshake_complete ||
1260 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001261 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001262 SSL_IS_DTLS(ssl)) {
1263 return 1;
1264 }
1265
1266 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1267 !CBB_add_u16(out, 0 /* length */)) {
1268 return 0;
1269 }
1270
1271 return 1;
1272}
1273
1274static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1275 CBS *contents) {
1276 if (contents == NULL) {
1277 return 1;
1278 }
1279
1280 /* If any of these are false then we should never have sent the NPN
1281 * extension in the ClientHello and thus this function should never have been
1282 * called. */
1283 assert(!ssl->s3->initial_handshake_complete);
1284 assert(!SSL_IS_DTLS(ssl));
1285 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001286 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001287
David Benjamin76c2efc2015-08-31 14:24:29 -04001288 if (ssl->s3->alpn_selected != NULL) {
1289 /* NPN and ALPN may not be negotiated in the same connection. */
1290 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1291 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1292 return 0;
1293 }
1294
Adam Langley97dfcbf2015-07-01 18:35:20 -07001295 const uint8_t *const orig_contents = CBS_data(contents);
1296 const size_t orig_len = CBS_len(contents);
1297
1298 while (CBS_len(contents) != 0) {
1299 CBS proto;
1300 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1301 CBS_len(&proto) == 0) {
1302 return 0;
1303 }
1304 }
1305
1306 uint8_t *selected;
1307 uint8_t selected_len;
1308 if (ssl->ctx->next_proto_select_cb(
1309 ssl, &selected, &selected_len, orig_contents, orig_len,
1310 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1311 *out_alert = SSL_AD_INTERNAL_ERROR;
1312 return 0;
1313 }
1314
1315 OPENSSL_free(ssl->next_proto_negotiated);
1316 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1317 if (ssl->next_proto_negotiated == NULL) {
1318 *out_alert = SSL_AD_INTERNAL_ERROR;
1319 return 0;
1320 }
1321
1322 ssl->next_proto_negotiated_len = selected_len;
1323 ssl->s3->next_proto_neg_seen = 1;
1324
1325 return 1;
1326}
1327
1328static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1329 CBS *contents) {
1330 if (contents != NULL && CBS_len(contents) != 0) {
1331 return 0;
1332 }
1333
1334 if (contents == NULL ||
1335 ssl->s3->initial_handshake_complete ||
1336 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1337 * afterwards, parsing the ALPN extension will clear
1338 * |next_proto_neg_seen|. */
1339 ssl->s3->alpn_selected != NULL ||
1340 ssl->ctx->next_protos_advertised_cb == NULL ||
1341 SSL_IS_DTLS(ssl)) {
1342 return 1;
1343 }
1344
1345 ssl->s3->next_proto_neg_seen = 1;
1346 return 1;
1347}
1348
1349static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1350 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1351 * parsed. */
1352 if (!ssl->s3->next_proto_neg_seen) {
1353 return 1;
1354 }
1355
1356 const uint8_t *npa;
1357 unsigned npa_len;
1358
1359 if (ssl->ctx->next_protos_advertised_cb(
1360 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1361 SSL_TLSEXT_ERR_OK) {
1362 ssl->s3->next_proto_neg_seen = 0;
1363 return 1;
1364 }
1365
1366 CBB contents;
1367 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1368 !CBB_add_u16_length_prefixed(out, &contents) ||
1369 !CBB_add_bytes(&contents, npa, npa_len) ||
1370 !CBB_flush(out)) {
1371 return 0;
1372 }
1373
1374 return 1;
1375}
1376
1377
Adam Langleyab8d87d2015-07-10 12:21:39 -07001378/* Signed certificate timestamps.
1379 *
1380 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1381
1382static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1383 if (!ssl->signed_cert_timestamps_enabled) {
1384 return 1;
1385 }
1386
1387 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1388 !CBB_add_u16(out, 0 /* length */)) {
1389 return 0;
1390 }
1391
1392 return 1;
1393}
1394
1395static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1396 CBS *contents) {
1397 if (contents == NULL) {
1398 return 1;
1399 }
1400
1401 /* If this is false then we should never have sent the SCT extension in the
1402 * ClientHello and thus this function should never have been called. */
1403 assert(ssl->signed_cert_timestamps_enabled);
1404
1405 if (CBS_len(contents) == 0) {
1406 *out_alert = SSL_AD_DECODE_ERROR;
1407 return 0;
1408 }
1409
1410 /* Session resumption uses the original session information. */
1411 if (!ssl->hit &&
1412 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1413 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1414 *out_alert = SSL_AD_INTERNAL_ERROR;
1415 return 0;
1416 }
1417
1418 return 1;
1419}
1420
1421static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1422 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001423 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001424}
1425
1426static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001427 /* The extension shouldn't be sent when resuming sessions. */
1428 if (ssl->hit ||
1429 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001430 return 1;
1431 }
1432
1433 CBB contents;
1434 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1435 CBB_add_u16_length_prefixed(out, &contents) &&
1436 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1437 ssl->ctx->signed_cert_timestamp_list_length) &&
1438 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001439}
1440
1441
Adam Langleyf18e4532015-07-10 13:39:53 -07001442/* Application-level Protocol Negotiation.
1443 *
1444 * https://tools.ietf.org/html/rfc7301 */
1445
1446static void ext_alpn_init(SSL *ssl) {
1447 OPENSSL_free(ssl->s3->alpn_selected);
1448 ssl->s3->alpn_selected = NULL;
1449}
1450
1451static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1452 if (ssl->alpn_client_proto_list == NULL ||
1453 ssl->s3->initial_handshake_complete) {
1454 return 1;
1455 }
1456
1457 CBB contents, proto_list;
1458 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1459 !CBB_add_u16_length_prefixed(out, &contents) ||
1460 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1461 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1462 ssl->alpn_client_proto_list_len) ||
1463 !CBB_flush(out)) {
1464 return 0;
1465 }
1466
1467 return 1;
1468}
1469
1470static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1471 CBS *contents) {
1472 if (contents == NULL) {
1473 return 1;
1474 }
1475
1476 assert(!ssl->s3->initial_handshake_complete);
1477 assert(ssl->alpn_client_proto_list != NULL);
1478
David Benjamin76c2efc2015-08-31 14:24:29 -04001479 if (ssl->s3->next_proto_neg_seen) {
1480 /* NPN and ALPN may not be negotiated in the same connection. */
1481 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1482 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1483 return 0;
1484 }
1485
Adam Langleyf18e4532015-07-10 13:39:53 -07001486 /* The extension data consists of a ProtocolNameList which must have
1487 * exactly one ProtocolName. Each of these is length-prefixed. */
1488 CBS protocol_name_list, protocol_name;
1489 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1490 CBS_len(contents) != 0 ||
1491 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1492 /* Empty protocol names are forbidden. */
1493 CBS_len(&protocol_name) == 0 ||
1494 CBS_len(&protocol_name_list) != 0) {
1495 return 0;
1496 }
1497
1498 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1499 &ssl->s3->alpn_selected_len)) {
1500 *out_alert = SSL_AD_INTERNAL_ERROR;
1501 return 0;
1502 }
1503
1504 return 1;
1505}
1506
1507static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1508 CBS *contents) {
1509 if (contents == NULL) {
1510 return 1;
1511 }
1512
1513 if (ssl->ctx->alpn_select_cb == NULL ||
1514 ssl->s3->initial_handshake_complete) {
1515 return 1;
1516 }
1517
1518 /* ALPN takes precedence over NPN. */
1519 ssl->s3->next_proto_neg_seen = 0;
1520
1521 CBS protocol_name_list;
1522 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1523 CBS_len(contents) != 0 ||
1524 CBS_len(&protocol_name_list) < 2) {
1525 return 0;
1526 }
1527
1528 /* Validate the protocol list. */
1529 CBS protocol_name_list_copy = protocol_name_list;
1530 while (CBS_len(&protocol_name_list_copy) > 0) {
1531 CBS protocol_name;
1532
1533 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1534 /* Empty protocol names are forbidden. */
1535 CBS_len(&protocol_name) == 0) {
1536 return 0;
1537 }
1538 }
1539
1540 const uint8_t *selected;
1541 uint8_t selected_len;
1542 if (ssl->ctx->alpn_select_cb(
1543 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1544 CBS_len(&protocol_name_list),
1545 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1546 OPENSSL_free(ssl->s3->alpn_selected);
1547 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1548 if (ssl->s3->alpn_selected == NULL) {
1549 *out_alert = SSL_AD_INTERNAL_ERROR;
1550 return 0;
1551 }
1552 ssl->s3->alpn_selected_len = selected_len;
1553 }
1554
1555 return 1;
1556}
1557
1558static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1559 if (ssl->s3->alpn_selected == NULL) {
1560 return 1;
1561 }
1562
1563 CBB contents, proto_list, proto;
1564 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1565 !CBB_add_u16_length_prefixed(out, &contents) ||
1566 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1567 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1568 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1569 !CBB_flush(out)) {
1570 return 0;
1571 }
1572
1573 return 1;
1574}
1575
1576
Adam Langley49c7af12015-07-10 14:33:46 -07001577/* Channel ID.
1578 *
1579 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1580
1581static void ext_channel_id_init(SSL *ssl) {
1582 ssl->s3->tlsext_channel_id_valid = 0;
1583}
1584
1585static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1586 if (!ssl->tlsext_channel_id_enabled ||
1587 SSL_IS_DTLS(ssl)) {
1588 return 1;
1589 }
1590
1591 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1592 !CBB_add_u16(out, 0 /* length */)) {
1593 return 0;
1594 }
1595
1596 return 1;
1597}
1598
1599static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1600 CBS *contents) {
1601 if (contents == NULL) {
1602 return 1;
1603 }
1604
1605 assert(!SSL_IS_DTLS(ssl));
1606 assert(ssl->tlsext_channel_id_enabled);
1607
1608 if (CBS_len(contents) != 0) {
1609 return 0;
1610 }
1611
1612 ssl->s3->tlsext_channel_id_valid = 1;
1613 return 1;
1614}
1615
1616static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1617 CBS *contents) {
1618 if (contents == NULL ||
1619 !ssl->tlsext_channel_id_enabled ||
1620 SSL_IS_DTLS(ssl)) {
1621 return 1;
1622 }
1623
1624 if (CBS_len(contents) != 0) {
1625 return 0;
1626 }
1627
1628 ssl->s3->tlsext_channel_id_valid = 1;
1629 return 1;
1630}
1631
1632static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1633 if (!ssl->s3->tlsext_channel_id_valid) {
1634 return 1;
1635 }
1636
1637 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1638 !CBB_add_u16(out, 0 /* length */)) {
1639 return 0;
1640 }
1641
1642 return 1;
1643}
1644
Adam Langley391250d2015-07-15 19:06:07 -07001645
1646/* Secure Real-time Transport Protocol (SRTP) extension.
1647 *
1648 * https://tools.ietf.org/html/rfc5764 */
1649
Adam Langley391250d2015-07-15 19:06:07 -07001650
1651static void ext_srtp_init(SSL *ssl) {
1652 ssl->srtp_profile = NULL;
1653}
1654
1655static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1656 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1657 if (profiles == NULL) {
1658 return 1;
1659 }
1660 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1661 if (num_profiles == 0) {
1662 return 1;
1663 }
1664
1665 CBB contents, profile_ids;
1666 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1667 !CBB_add_u16_length_prefixed(out, &contents) ||
1668 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1669 return 0;
1670 }
1671
1672 size_t i;
1673 for (i = 0; i < num_profiles; i++) {
1674 if (!CBB_add_u16(&profile_ids,
1675 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1676 return 0;
1677 }
1678 }
1679
1680 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1681 !CBB_flush(out)) {
1682 return 0;
1683 }
1684
1685 return 1;
1686}
1687
1688static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1689 CBS *contents) {
1690 if (contents == NULL) {
1691 return 1;
1692 }
1693
1694 /* The extension consists of a u16-prefixed profile ID list containing a
1695 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1696 *
1697 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1698 CBS profile_ids, srtp_mki;
1699 uint16_t profile_id;
1700 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1701 !CBS_get_u16(&profile_ids, &profile_id) ||
1702 CBS_len(&profile_ids) != 0 ||
1703 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1704 CBS_len(contents) != 0) {
1705 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1706 return 0;
1707 }
1708
1709 if (CBS_len(&srtp_mki) != 0) {
1710 /* Must be no MKI, since we never offer one. */
1711 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1712 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1713 return 0;
1714 }
1715
1716 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1717
1718 /* Check to see if the server gave us something we support (and presumably
1719 * offered). */
1720 size_t i;
1721 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1722 const SRTP_PROTECTION_PROFILE *profile =
1723 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1724
1725 if (profile->id == profile_id) {
1726 ssl->srtp_profile = profile;
1727 return 1;
1728 }
1729 }
1730
1731 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1732 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1733 return 0;
1734}
1735
1736static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1737 CBS *contents) {
1738 if (contents == NULL) {
1739 return 1;
1740 }
1741
1742 CBS profile_ids, srtp_mki;
1743 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1744 CBS_len(&profile_ids) < 2 ||
1745 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1746 CBS_len(contents) != 0) {
1747 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1748 return 0;
1749 }
1750 /* Discard the MKI value for now. */
1751
1752 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1753 SSL_get_srtp_profiles(ssl);
1754
1755 /* Pick the server's most preferred profile. */
1756 size_t i;
1757 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1758 const SRTP_PROTECTION_PROFILE *server_profile =
1759 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1760
1761 CBS profile_ids_tmp;
1762 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1763
1764 while (CBS_len(&profile_ids_tmp) > 0) {
1765 uint16_t profile_id;
1766 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1767 return 0;
1768 }
1769
1770 if (server_profile->id == profile_id) {
1771 ssl->srtp_profile = server_profile;
1772 return 1;
1773 }
1774 }
1775 }
1776
1777 return 1;
1778}
1779
1780static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1781 if (ssl->srtp_profile == NULL) {
1782 return 1;
1783 }
1784
1785 CBB contents, profile_ids;
1786 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1787 !CBB_add_u16_length_prefixed(out, &contents) ||
1788 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1789 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1790 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1791 !CBB_flush(out)) {
1792 return 0;
1793 }
1794
1795 return 1;
1796}
1797
Adam Langleybdd5d662015-07-20 16:19:08 -07001798
1799/* EC point formats.
1800 *
1801 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1802
1803static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1804 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1805 return 0;
1806 }
1807
1808 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1809
1810 size_t i;
1811 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1812 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1813
1814 const uint32_t alg_k = cipher->algorithm_mkey;
1815 const uint32_t alg_a = cipher->algorithm_auth;
1816 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1817 return 1;
1818 }
1819 }
1820
1821 return 0;
1822}
1823
Adam Langleybdd5d662015-07-20 16:19:08 -07001824static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001825 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001826 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1827 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001828 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1829 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001830 !CBB_flush(out)) {
1831 return 0;
1832 }
1833
1834 return 1;
1835}
1836
1837static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1838 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1839 return 1;
1840 }
1841
1842 return ext_ec_point_add_extension(ssl, out);
1843}
1844
1845static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1846 CBS *contents) {
1847 if (contents == NULL) {
1848 return 1;
1849 }
1850
1851 CBS ec_point_format_list;
1852 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1853 CBS_len(contents) != 0) {
1854 return 0;
1855 }
1856
David Benjaminfc059942015-07-30 23:01:59 -04001857 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1858 * point format. */
1859 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1860 CBS_len(&ec_point_format_list)) == NULL) {
1861 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001862 return 0;
1863 }
1864
1865 return 1;
1866}
1867
1868static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1869 CBS *contents) {
1870 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1871}
1872
1873static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1874 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1875 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001876 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001877
1878 if (!using_ecc) {
1879 return 1;
1880 }
1881
1882 return ext_ec_point_add_extension(ssl, out);
1883}
1884
Adam Langley273d49c2015-07-20 16:38:52 -07001885
1886/* EC supported curves.
1887 *
1888 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1889
1890static void ext_ec_curves_init(SSL *ssl) {
1891 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1892 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1893 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1894}
1895
1896static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1897 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1898 return 1;
1899 }
1900
1901 CBB contents, curves_bytes;
1902 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1903 !CBB_add_u16_length_prefixed(out, &contents) ||
1904 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1905 return 0;
1906 }
1907
1908 const uint16_t *curves;
1909 size_t curves_len;
1910 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1911
1912 size_t i;
1913 for (i = 0; i < curves_len; i++) {
1914 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1915 return 0;
1916 }
1917 }
1918
1919 return CBB_flush(out);
1920}
1921
1922static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1923 CBS *contents) {
1924 /* This extension is not expected to be echoed by servers and is ignored. */
1925 return 1;
1926}
1927
1928static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1929 CBS *contents) {
1930 if (contents == NULL) {
1931 return 1;
1932 }
1933
1934 CBS elliptic_curve_list;
1935 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
1936 CBS_len(&elliptic_curve_list) == 0 ||
1937 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1938 CBS_len(contents) != 0) {
1939 return 0;
1940 }
1941
1942 ssl->s3->tmp.peer_ellipticcurvelist =
1943 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1944
1945 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
1946 *out_alert = SSL_AD_INTERNAL_ERROR;
1947 return 0;
1948 }
1949
1950 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
1951 size_t i;
1952 for (i = 0; i < num_curves; i++) {
1953 if (!CBS_get_u16(&elliptic_curve_list,
1954 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
1955 goto err;
1956 }
1957 }
1958
1959 assert(CBS_len(&elliptic_curve_list) == 0);
1960 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1961
1962 return 1;
1963
1964err:
1965 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1966 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1967 *out_alert = SSL_AD_INTERNAL_ERROR;
1968 return 0;
1969}
1970
1971static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
1972 /* Servers don't echo this extension. */
1973 return 1;
1974}
1975
1976
Adam Langley614c66a2015-06-12 15:26:58 -07001977/* kExtensions contains all the supported extensions. */
1978static const struct tls_extension kExtensions[] = {
1979 {
Adam Langley5021b222015-06-12 18:27:58 -07001980 /* The renegotiation extension must always be at index zero because the
1981 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1982 * sent as an SCSV. */
1983 TLSEXT_TYPE_renegotiate,
1984 NULL,
1985 ext_ri_add_clienthello,
1986 ext_ri_parse_serverhello,
1987 ext_ri_parse_clienthello,
1988 ext_ri_add_serverhello,
1989 },
1990 {
Adam Langley614c66a2015-06-12 15:26:58 -07001991 TLSEXT_TYPE_server_name,
1992 ext_sni_init,
1993 ext_sni_add_clienthello,
1994 ext_sni_parse_serverhello,
1995 ext_sni_parse_clienthello,
1996 ext_sni_add_serverhello,
1997 },
Adam Langley0a056712015-07-01 15:03:33 -07001998 {
1999 TLSEXT_TYPE_extended_master_secret,
2000 ext_ems_init,
2001 ext_ems_add_clienthello,
2002 ext_ems_parse_serverhello,
2003 ext_ems_parse_clienthello,
2004 ext_ems_add_serverhello,
2005 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002006 {
2007 TLSEXT_TYPE_session_ticket,
2008 NULL,
2009 ext_ticket_add_clienthello,
2010 ext_ticket_parse_serverhello,
2011 ext_ticket_parse_clienthello,
2012 ext_ticket_add_serverhello,
2013 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002014 {
2015 TLSEXT_TYPE_signature_algorithms,
2016 NULL,
2017 ext_sigalgs_add_clienthello,
2018 ext_sigalgs_parse_serverhello,
2019 ext_sigalgs_parse_clienthello,
2020 ext_sigalgs_add_serverhello,
2021 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002022 {
2023 TLSEXT_TYPE_status_request,
2024 ext_ocsp_init,
2025 ext_ocsp_add_clienthello,
2026 ext_ocsp_parse_serverhello,
2027 ext_ocsp_parse_clienthello,
2028 ext_ocsp_add_serverhello,
2029 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002030 {
2031 TLSEXT_TYPE_next_proto_neg,
2032 ext_npn_init,
2033 ext_npn_add_clienthello,
2034 ext_npn_parse_serverhello,
2035 ext_npn_parse_clienthello,
2036 ext_npn_add_serverhello,
2037 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002038 {
2039 TLSEXT_TYPE_certificate_timestamp,
2040 NULL,
2041 ext_sct_add_clienthello,
2042 ext_sct_parse_serverhello,
2043 ext_sct_parse_clienthello,
2044 ext_sct_add_serverhello,
2045 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002046 {
2047 TLSEXT_TYPE_application_layer_protocol_negotiation,
2048 ext_alpn_init,
2049 ext_alpn_add_clienthello,
2050 ext_alpn_parse_serverhello,
2051 ext_alpn_parse_clienthello,
2052 ext_alpn_add_serverhello,
2053 },
Adam Langley49c7af12015-07-10 14:33:46 -07002054 {
2055 TLSEXT_TYPE_channel_id,
2056 ext_channel_id_init,
2057 ext_channel_id_add_clienthello,
2058 ext_channel_id_parse_serverhello,
2059 ext_channel_id_parse_clienthello,
2060 ext_channel_id_add_serverhello,
2061 },
Adam Langley391250d2015-07-15 19:06:07 -07002062 {
2063 TLSEXT_TYPE_srtp,
2064 ext_srtp_init,
2065 ext_srtp_add_clienthello,
2066 ext_srtp_parse_serverhello,
2067 ext_srtp_parse_clienthello,
2068 ext_srtp_add_serverhello,
2069 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002070 {
2071 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002072 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002073 ext_ec_point_add_clienthello,
2074 ext_ec_point_parse_serverhello,
2075 ext_ec_point_parse_clienthello,
2076 ext_ec_point_add_serverhello,
2077 },
Adam Langley273d49c2015-07-20 16:38:52 -07002078 {
2079 TLSEXT_TYPE_elliptic_curves,
2080 ext_ec_curves_init,
2081 ext_ec_curves_add_clienthello,
2082 ext_ec_curves_parse_serverhello,
2083 ext_ec_curves_parse_clienthello,
2084 ext_ec_curves_add_serverhello,
2085 },
Adam Langley614c66a2015-06-12 15:26:58 -07002086};
2087
2088#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2089
Adam Langley4cfa96b2015-07-01 11:56:55 -07002090OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2091 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002092 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002093OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2094 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2095 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002096 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002097
Adam Langley614c66a2015-06-12 15:26:58 -07002098static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2099 uint16_t value) {
2100 unsigned i;
2101 for (i = 0; i < kNumExtensions; i++) {
2102 if (kExtensions[i].value == value) {
2103 *out_index = i;
2104 return &kExtensions[i];
2105 }
2106 }
2107
2108 return NULL;
2109}
2110
Adam Langley09505632015-07-30 18:10:13 -07002111int SSL_extension_supported(unsigned extension_value) {
2112 uint32_t index;
2113 return extension_value == TLSEXT_TYPE_padding ||
2114 tls_extension_find(&index, extension_value) != NULL;
2115}
2116
David Benjamine8d53502015-10-10 14:13:23 -04002117int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002118 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002119 if (ssl->client_version == SSL3_VERSION &&
2120 !ssl->s3->send_connection_binding) {
2121 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 }
Adam Langley95c29f32014-06-20 12:00:00 -07002123
David Benjamine8d53502015-10-10 14:13:23 -04002124 CBB extensions;
2125 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002126 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 }
Adam Langley95c29f32014-06-20 12:00:00 -07002128
David Benjamine8d53502015-10-10 14:13:23 -04002129 ssl->s3->tmp.extensions.sent = 0;
2130 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002131
Adam Langley614c66a2015-06-12 15:26:58 -07002132 size_t i;
2133 for (i = 0; i < kNumExtensions; i++) {
2134 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002135 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002136 }
2137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langley614c66a2015-06-12 15:26:58 -07002139 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002140 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002141 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002142 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2143 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2144 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002145 }
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langley33ad2b52015-07-20 17:43:53 -07002147 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002148 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002149 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 }
Adam Langley75712922014-10-10 16:23:43 -07002151
David Benjamine8d53502015-10-10 14:13:23 -04002152 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002153 goto err;
2154 }
2155
David Benjamine8d53502015-10-10 14:13:23 -04002156 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002157 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002158 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002159 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 *
2161 * NB: because this code works out the length of all existing extensions
2162 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002163 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002164 /* Extensions take at least four bytes to encode. Always include least
2165 * one byte of data if including the extension. WebSphere Application
2166 * Server 7.0 is intolerant to the last extension being zero-length. */
2167 if (padding_len >= 4 + 1) {
2168 padding_len -= 4;
2169 } else {
2170 padding_len = 1;
2171 }
Adam Langley95c29f32014-06-20 12:00:00 -07002172
Adam Langley33ad2b52015-07-20 17:43:53 -07002173 uint8_t *padding_bytes;
2174 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2175 !CBB_add_u16(&extensions, padding_len) ||
2176 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2177 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 }
Adam Langley75712922014-10-10 16:23:43 -07002179
Adam Langley33ad2b52015-07-20 17:43:53 -07002180 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 }
2182 }
Adam Langley75712922014-10-10 16:23:43 -07002183
David Benjamina01deee2015-12-08 18:56:31 -05002184 /* Discard empty extensions blocks. */
2185 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002186 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002187 }
2188
David Benjamine8d53502015-10-10 14:13:23 -04002189 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002190
2191err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002192 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002193 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002194}
Adam Langley95c29f32014-06-20 12:00:00 -07002195
David Benjamin56380462015-10-10 14:59:09 -04002196int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002197 CBB extensions;
2198 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002199 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002200 }
2201
2202 unsigned i;
2203 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002204 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002205 /* Don't send extensions that were not received. */
2206 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002207 }
Adam Langley95c29f32014-06-20 12:00:00 -07002208
David Benjamin56380462015-10-10 14:59:09 -04002209 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002210 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2211 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2212 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002213 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 }
Adam Langley95c29f32014-06-20 12:00:00 -07002215
David Benjamin56380462015-10-10 14:59:09 -04002216 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002217 goto err;
2218 }
2219
David Benjamina01deee2015-12-08 18:56:31 -05002220 /* Discard empty extensions blocks. */
2221 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002222 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002223 }
2224
David Benjamin56380462015-10-10 14:59:09 -04002225 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002226
2227err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002228 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002229 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002230}
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002233 size_t i;
2234 for (i = 0; i < kNumExtensions; i++) {
2235 if (kExtensions[i].init != NULL) {
2236 kExtensions[i].init(s);
2237 }
2238 }
2239
2240 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002241 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002242 /* The renegotiation extension must always be at index zero because the
2243 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2244 * sent as an SCSV. */
2245 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002248 if (CBS_len(cbs) != 0) {
2249 /* Decode the extensions block and check it is valid. */
2250 CBS extensions;
2251 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2252 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 *out_alert = SSL_AD_DECODE_ERROR;
2254 return 0;
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langley33ad2b52015-07-20 17:43:53 -07002257 while (CBS_len(&extensions) != 0) {
2258 uint16_t type;
2259 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002260
Adam Langley33ad2b52015-07-20 17:43:53 -07002261 /* Decode the next extension. */
2262 if (!CBS_get_u16(&extensions, &type) ||
2263 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2264 *out_alert = SSL_AD_DECODE_ERROR;
2265 return 0;
2266 }
2267
David Benjaminc7ce9772015-10-09 19:32:41 -04002268 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2269 * ambiguous. Ignore all but the renegotiation_info extension. */
2270 if (s->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2271 continue;
2272 }
2273
Adam Langley33ad2b52015-07-20 17:43:53 -07002274 unsigned ext_index;
2275 const struct tls_extension *const ext =
2276 tls_extension_find(&ext_index, type);
2277
2278 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002279 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2280 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2281 return 0;
2282 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002283 continue;
2284 }
2285
Adam Langley614c66a2015-06-12 15:26:58 -07002286 s->s3->tmp.extensions.received |= (1u << ext_index);
2287 uint8_t alert = SSL_AD_DECODE_ERROR;
2288 if (!ext->parse_clienthello(s, &alert, &extension)) {
2289 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002290 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2291 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 return 0;
2293 }
Adam Langley614c66a2015-06-12 15:26:58 -07002294 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 }
Adam Langley75712922014-10-10 16:23:43 -07002296
Adam Langley614c66a2015-06-12 15:26:58 -07002297 for (i = 0; i < kNumExtensions; i++) {
2298 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2299 /* Extension wasn't observed so call the callback with a NULL
2300 * parameter. */
2301 uint8_t alert = SSL_AD_DECODE_ERROR;
2302 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002303 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2304 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002305 *out_alert = alert;
2306 return 0;
2307 }
2308 }
2309 }
2310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 return 1;
2312}
Adam Langley95c29f32014-06-20 12:00:00 -07002313
Adam Langleyfcf25832014-12-18 17:42:32 -08002314int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2315 int alert = -1;
2316 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2317 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2318 return 0;
2319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002322 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 return 0;
2324 }
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 return 1;
2327}
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002329OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2330
Adam Langleyfcf25832014-12-18 17:42:32 -08002331static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002332 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002333
Adam Langley33ad2b52015-07-20 17:43:53 -07002334 if (CBS_len(cbs) != 0) {
2335 /* Decode the extensions block and check it is valid. */
2336 CBS extensions;
2337 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2338 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 *out_alert = SSL_AD_DECODE_ERROR;
2340 return 0;
2341 }
Adam Langley95c29f32014-06-20 12:00:00 -07002342
Adam Langley614c66a2015-06-12 15:26:58 -07002343
Adam Langley33ad2b52015-07-20 17:43:53 -07002344 while (CBS_len(&extensions) != 0) {
2345 uint16_t type;
2346 CBS extension;
2347
2348 /* Decode the next extension. */
2349 if (!CBS_get_u16(&extensions, &type) ||
2350 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2351 *out_alert = SSL_AD_DECODE_ERROR;
2352 return 0;
2353 }
2354
2355 unsigned ext_index;
2356 const struct tls_extension *const ext =
2357 tls_extension_find(&ext_index, type);
2358
Adam Langley09505632015-07-30 18:10:13 -07002359 if (ext == NULL) {
2360 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2361 return 0;
2362 }
2363 continue;
2364 }
2365
2366 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2367 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002368 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002369 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 *out_alert = SSL_AD_DECODE_ERROR;
2371 return 0;
2372 }
David Benjamin03973092014-06-24 23:27:17 -04002373
Adam Langley614c66a2015-06-12 15:26:58 -07002374 received |= (1u << ext_index);
2375
2376 uint8_t alert = SSL_AD_DECODE_ERROR;
2377 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002378 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2379 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002380 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002381 return 0;
2382 }
Adam Langley614c66a2015-06-12 15:26:58 -07002383 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002384 }
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langley33ad2b52015-07-20 17:43:53 -07002386 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002387 for (i = 0; i < kNumExtensions; i++) {
2388 if (!(received & (1u << i))) {
2389 /* Extension wasn't observed so call the callback with a NULL
2390 * parameter. */
2391 uint8_t alert = SSL_AD_DECODE_ERROR;
2392 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002393 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2394 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002395 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 return 0;
2397 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 }
2399 }
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401 return 1;
2402}
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404static int ssl_check_clienthello_tlsext(SSL *s) {
2405 int ret = SSL_TLSEXT_ERR_NOACK;
2406 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2409 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2412 ret = s->ctx->tlsext_servername_callback(s, &al,
2413 s->ctx->tlsext_servername_arg);
2414 } else if (s->initial_ctx != NULL &&
2415 s->initial_ctx->tlsext_servername_callback != 0) {
2416 ret = s->initial_ctx->tlsext_servername_callback(
2417 s, &al, s->initial_ctx->tlsext_servername_arg);
2418 }
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langleyfcf25832014-12-18 17:42:32 -08002420 switch (ret) {
2421 case SSL_TLSEXT_ERR_ALERT_FATAL:
2422 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2423 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002424
Adam Langleyfcf25832014-12-18 17:42:32 -08002425 case SSL_TLSEXT_ERR_ALERT_WARNING:
2426 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2427 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002430 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002432
Adam Langleyfcf25832014-12-18 17:42:32 -08002433 default:
2434 return 1;
2435 }
2436}
Adam Langleyed8270a2014-09-02 13:52:56 -07002437
Adam Langleyfcf25832014-12-18 17:42:32 -08002438static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002439 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002440 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002441
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2443 ret = s->ctx->tlsext_servername_callback(s, &al,
2444 s->ctx->tlsext_servername_arg);
2445 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002446 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 ret = s->initial_ctx->tlsext_servername_callback(
2448 s, &al, s->initial_ctx->tlsext_servername_arg);
2449 }
Adam Langley95c29f32014-06-20 12:00:00 -07002450
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 switch (ret) {
2452 case SSL_TLSEXT_ERR_ALERT_FATAL:
2453 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2454 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002455
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 case SSL_TLSEXT_ERR_ALERT_WARNING:
2457 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2458 return 1;
2459
2460 default:
2461 return 1;
2462 }
2463}
2464
2465int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2466 int alert = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002467 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2468 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2469 return 0;
2470 }
2471
2472 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002473 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 return 0;
2475 }
2476
2477 return 1;
2478}
Adam Langley95c29f32014-06-20 12:00:00 -07002479
David Benjamine3aa1d92015-06-16 15:34:50 -04002480int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2481 int *out_send_ticket, const uint8_t *ticket,
2482 size_t ticket_len, const uint8_t *session_id,
2483 size_t session_id_len) {
2484 int ret = 1; /* Most errors are non-fatal. */
2485 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2486 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002487
David Benjamine3aa1d92015-06-16 15:34:50 -04002488 HMAC_CTX hmac_ctx;
2489 HMAC_CTX_init(&hmac_ctx);
2490 EVP_CIPHER_CTX cipher_ctx;
2491 EVP_CIPHER_CTX_init(&cipher_ctx);
2492
2493 *out_send_ticket = 0;
2494 *out_session = NULL;
2495
2496 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2497 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002498 }
2499
David Benjamine3aa1d92015-06-16 15:34:50 -04002500 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002502 *out_send_ticket = 1;
2503 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 }
2505
David Benjaminadcc3952015-04-26 13:07:57 -04002506 /* Ensure there is room for the key name and the largest IV
2507 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2508 * the maximum IV length should be well under the minimum size for the
2509 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002510 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2511 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002513 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002514
David Benjamine3aa1d92015-06-16 15:34:50 -04002515 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2516 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2517 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2518 0 /* decrypt */);
2519 if (cb_ret < 0) {
2520 ret = 0;
2521 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002523 if (cb_ret == 0) {
2524 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002526 if (cb_ret == 2) {
2527 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 }
2529 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002530 /* Check the key name matches. */
2531 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2532 SSL_TICKET_KEY_NAME_LEN) != 0) {
2533 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002535 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2536 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002538 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2539 ssl_ctx->tlsext_tick_aes_key, iv)) {
2540 ret = 0;
2541 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002542 }
2543 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002544 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002545
David Benjamine3aa1d92015-06-16 15:34:50 -04002546 /* Check the MAC at the end of the ticket. */
2547 uint8_t mac[EVP_MAX_MD_SIZE];
2548 size_t mac_len = HMAC_size(&hmac_ctx);
2549 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002550 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002551 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002553 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2554 HMAC_Final(&hmac_ctx, mac, NULL);
2555 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2556 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 }
2558
David Benjamine3aa1d92015-06-16 15:34:50 -04002559 /* Decrypt the session data. */
2560 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2561 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2562 mac_len;
2563 plaintext = OPENSSL_malloc(ciphertext_len);
2564 if (plaintext == NULL) {
2565 ret = 0;
2566 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002568 if (ciphertext_len >= INT_MAX) {
2569 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002571 int len1, len2;
2572 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2573 (int)ciphertext_len) ||
2574 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2575 ERR_clear_error(); /* Don't leave an error on the queue. */
2576 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 }
2578
David Benjamine3aa1d92015-06-16 15:34:50 -04002579 /* Decode the session. */
2580 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2581 if (session == NULL) {
2582 ERR_clear_error(); /* Don't leave an error on the queue. */
2583 goto done;
2584 }
2585
2586 /* Copy the client's session ID into the new session, to denote the ticket has
2587 * been accepted. */
2588 memcpy(session->session_id, session_id, session_id_len);
2589 session->session_id_length = session_id_len;
2590
2591 *out_session = session;
2592
2593done:
2594 OPENSSL_free(plaintext);
2595 HMAC_CTX_cleanup(&hmac_ctx);
2596 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2597 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002598}
Adam Langley95c29f32014-06-20 12:00:00 -07002599
2600/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002601typedef struct {
2602 int nid;
2603 int id;
2604} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002605
Adam Langleyfcf25832014-12-18 17:42:32 -08002606static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2607 {NID_sha1, TLSEXT_hash_sha1},
2608 {NID_sha224, TLSEXT_hash_sha224},
2609 {NID_sha256, TLSEXT_hash_sha256},
2610 {NID_sha384, TLSEXT_hash_sha384},
2611 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002612
Adam Langleyfcf25832014-12-18 17:42:32 -08002613static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2614 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2617 size_t i;
2618 for (i = 0; i < tlen; i++) {
2619 if (table[i].nid == nid) {
2620 return table[i].id;
2621 }
2622 }
Adam Langley95c29f32014-06-20 12:00:00 -07002623
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 return -1;
2625}
Adam Langley95c29f32014-06-20 12:00:00 -07002626
David Benjaminb4d65fd2015-05-29 17:11:21 -04002627int tls12_get_sigid(int pkey_type) {
2628 return tls12_find_id(pkey_type, tls12_sig,
2629 sizeof(tls12_sig) / sizeof(tls12_lookup));
2630}
2631
David Benjaminfc825122015-12-18 01:57:43 -05002632int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2633 int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2634 sizeof(tls12_md) / sizeof(tls12_lookup));
2635 int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -07002636
David Benjaminfc825122015-12-18 01:57:43 -05002637 return md_id != -1 &&
2638 sig_id != -1 &&
2639 CBB_add_u8(out, (uint8_t)md_id) &&
2640 CBB_add_u8(out, (uint8_t)sig_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002641}
2642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2644 switch (hash_alg) {
2645 case TLSEXT_hash_md5:
2646 return EVP_md5();
2647
2648 case TLSEXT_hash_sha1:
2649 return EVP_sha1();
2650
2651 case TLSEXT_hash_sha224:
2652 return EVP_sha224();
2653
2654 case TLSEXT_hash_sha256:
2655 return EVP_sha256();
2656
2657 case TLSEXT_hash_sha384:
2658 return EVP_sha384();
2659
2660 case TLSEXT_hash_sha512:
2661 return EVP_sha512();
2662
2663 default:
2664 return NULL;
2665 }
2666}
Adam Langley95c29f32014-06-20 12:00:00 -07002667
David Benjaminec2f27d2014-11-13 19:17:25 -05002668/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2669 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002670static int tls12_get_pkey_type(uint8_t sig_alg) {
2671 switch (sig_alg) {
2672 case TLSEXT_signature_rsa:
2673 return EVP_PKEY_RSA;
2674
2675 case TLSEXT_signature_ecdsa:
2676 return EVP_PKEY_EC;
2677
2678 default:
2679 return -1;
2680 }
2681}
Adam Langley95c29f32014-06-20 12:00:00 -07002682
Steven Valdez0d62f262015-09-04 12:41:04 -04002683OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2684 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002685
Steven Valdez0d62f262015-09-04 12:41:04 -04002686int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002688 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 return 1;
2690 }
David Benjamincd996942014-07-20 16:23:51 -04002691
Steven Valdez0d62f262015-09-04 12:41:04 -04002692 CERT *const cert = ssl->cert;
2693 OPENSSL_free(cert->peer_sigalgs);
2694 cert->peer_sigalgs = NULL;
2695 cert->peer_sigalgslen = 0;
2696
2697 size_t num_sigalgs = CBS_len(in_sigalgs);
2698
2699 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 return 0;
2701 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002702 num_sigalgs /= 2;
2703
2704 /* supported_signature_algorithms in the certificate request is
2705 * allowed to be empty. */
2706 if (num_sigalgs == 0) {
2707 return 1;
2708 }
2709
2710 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2711 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2712 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2713 if (cert->peer_sigalgs == NULL) {
2714 return 0;
2715 }
2716 cert->peer_sigalgslen = num_sigalgs;
2717
2718 CBS sigalgs;
2719 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2720
2721 size_t i;
2722 for (i = 0; i < num_sigalgs; i++) {
2723 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2724 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2725 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2726 return 0;
2727 }
2728 }
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 return 1;
2731}
David Benjaminec2f27d2014-11-13 19:17:25 -05002732
David Benjamind1d80782015-07-05 11:54:09 -04002733const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2734 CERT *cert = ssl->cert;
2735 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002736 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002737
Steven Valdez0d62f262015-09-04 12:41:04 -04002738 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2739 NID_sha224, NID_sha1};
2740
2741 const int *digest_nids = kDefaultDigestList;
2742 size_t num_digest_nids =
2743 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2744 if (cert->digest_nids != NULL) {
2745 digest_nids = cert->digest_nids;
2746 num_digest_nids = cert->num_digest_nids;
2747 }
2748
2749 for (i = 0; i < num_digest_nids; i++) {
2750 const int digest_nid = digest_nids[i];
2751 for (j = 0; j < cert->peer_sigalgslen; j++) {
2752 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2753 if (md == NULL ||
2754 digest_nid != EVP_MD_type(md) ||
2755 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2756 continue;
2757 }
2758
2759 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002761 }
Adam Langley95c29f32014-06-20 12:00:00 -07002762
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 /* If no suitable digest may be found, default to SHA-1. */
2764 return EVP_sha1();
2765}
Adam Langley95c29f32014-06-20 12:00:00 -07002766
David Benjamind6a4ae92015-08-06 11:10:51 -04002767int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2768 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002769 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002770
2771 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002772 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2773 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002775
David Benjamind6a4ae92015-08-06 11:10:51 -04002776 static const char kClientIDMagic[] = "TLS Channel ID signature";
2777 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2778
2779 if (ssl->hit) {
2780 static const char kResumptionMagic[] = "Resumption";
2781 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2782 if (ssl->session->original_handshake_hash_len == 0) {
2783 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2784 goto err;
2785 }
2786 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2787 ssl->session->original_handshake_hash_len);
2788 }
2789
2790 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2791 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2792 sizeof(handshake_hash));
2793 if (handshake_hash_len < 0) {
2794 goto err;
2795 }
2796 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2797 unsigned len_u;
2798 EVP_DigestFinal_ex(&ctx, out, &len_u);
2799 *out_len = len_u;
2800
2801 ret = 1;
2802
2803err:
2804 EVP_MD_CTX_cleanup(&ctx);
2805 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002806}
Adam Langley1258b6a2014-06-20 12:00:00 -07002807
2808/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2809 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002810int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2811 int digest_len;
2812 /* This function should never be called for a resumed session because the
2813 * handshake hashes that we wish to record are for the original, full
2814 * handshake. */
2815 if (s->hit) {
2816 return -1;
2817 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002818
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 digest_len =
2820 tls1_handshake_digest(s, s->session->original_handshake_hash,
2821 sizeof(s->session->original_handshake_hash));
2822 if (digest_len < 0) {
2823 return -1;
2824 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002825
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002827
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 return 1;
2829}