blob: 2d0a4fb1ff448b0793a6fba2f6fab60490c0d9d3 [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 Benjamin0d56f882015-12-19 17:05:56 -0500130static int ssl_check_clienthello_tlsext(SSL *ssl);
131static int ssl_check_serverhello_tlsext(SSL *ssl);
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_generate_master_secret,
Adam Langleyfcf25832014-12-18 17:42:32 -0800136 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800137 tls1_cert_verify_mac,
138 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
140 tls1_alert_code,
141 tls1_export_keying_material,
142 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800143};
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin338fcaf2014-12-11 01:20:52 -0500145const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500146 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 tls1_generate_master_secret,
Adam Langleyfcf25832014-12-18 17:42:32 -0800148 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_cert_verify_mac,
150 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
151 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
152 tls1_alert_code,
153 tls1_export_keying_material,
154 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800155};
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamin338fcaf2014-12-11 01:20:52 -0500157const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500158 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 tls1_generate_master_secret,
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800161 tls1_cert_verify_mac,
162 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
163 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
164 tls1_alert_code,
165 tls1_export_keying_material,
David Benjaminc7817d82015-11-05 18:28:33 -0500166 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF,
Adam Langleyfcf25832014-12-18 17:42:32 -0800167};
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langleyfcf25832014-12-18 17:42:32 -0800169static int compare_uint16_t(const void *p1, const void *p2) {
170 uint16_t u1 = *((const uint16_t *)p1);
171 uint16_t u2 = *((const uint16_t *)p2);
172 if (u1 < u2) {
173 return -1;
174 } else if (u1 > u2) {
175 return 1;
176 } else {
177 return 0;
178 }
179}
David Benjamin35a7a442014-07-05 00:23:20 -0400180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
182 * more than one extension of the same type in a ClientHello or ServerHello.
183 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400184 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800185static int tls1_check_duplicate_extensions(const CBS *cbs) {
186 CBS extensions = *cbs;
187 size_t num_extensions = 0, i = 0;
188 uint16_t *extension_types = NULL;
189 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* First pass: count the extensions. */
192 while (CBS_len(&extensions) > 0) {
193 uint16_t type;
194 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 if (!CBS_get_u16(&extensions, &type) ||
197 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
198 goto done;
199 }
David Benjamin35a7a442014-07-05 00:23:20 -0400200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 num_extensions++;
202 }
David Benjamin35a7a442014-07-05 00:23:20 -0400203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 if (num_extensions == 0) {
205 return 1;
206 }
David Benjamin9a373592014-07-25 04:27:53 -0400207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 extension_types =
209 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
210 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400211 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 goto done;
213 }
David Benjamin35a7a442014-07-05 00:23:20 -0400214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 /* Second pass: gather the extension types. */
216 extensions = *cbs;
217 for (i = 0; i < num_extensions; i++) {
218 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400219
Adam Langleyfcf25832014-12-18 17:42:32 -0800220 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
221 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
222 /* This should not happen. */
223 goto done;
224 }
225 }
226 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400227
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 /* Sort the extensions and make sure there are no duplicates. */
229 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
230 for (i = 1; i < num_extensions; i++) {
231 if (extension_types[i - 1] == extension_types[i]) {
232 goto done;
233 }
234 }
David Benjamin35a7a442014-07-05 00:23:20 -0400235
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 ret = 1;
237
David Benjamin35a7a442014-07-05 00:23:20 -0400238done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400239 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return ret;
241}
David Benjamin35a7a442014-07-05 00:23:20 -0400242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
244 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (/* Skip client version. */
249 !CBS_skip(&client_hello, 2) ||
250 /* Skip client nonce. */
251 !CBS_skip(&client_hello, 32) ||
252 /* Extract session_id. */
253 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
254 return 0;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 ctx->session_id = CBS_data(&session_id);
258 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 /* Skip past DTLS cookie */
261 if (SSL_IS_DTLS(ctx->ssl)) {
262 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
265 return 0;
266 }
267 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 /* Extract cipher_suites. */
270 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
271 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
272 return 0;
273 }
274 ctx->cipher_suites = CBS_data(&cipher_suites);
275 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 /* Extract compression_methods. */
278 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
279 CBS_len(&compression_methods) < 1) {
280 return 0;
281 }
282 ctx->compression_methods = CBS_data(&compression_methods);
283 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 /* If the ClientHello ends here then it's valid, but doesn't have any
286 * extensions. (E.g. SSLv3.) */
287 if (CBS_len(&client_hello) == 0) {
288 ctx->extensions = NULL;
289 ctx->extensions_len = 0;
290 return 1;
291 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 /* Extract extensions and check it is valid. */
294 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
295 !tls1_check_duplicate_extensions(&extensions) ||
296 CBS_len(&client_hello) != 0) {
297 return 0;
298 }
299 ctx->extensions = CBS_data(&extensions);
300 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700301
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 return 1;
303}
Adam Langleydc9b1412014-06-20 12:00:00 -0700304
David Benjamind4c2bce2015-10-17 12:28:18 -0400305int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
307 const uint8_t **out_data, size_t *out_len) {
308 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700311
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 while (CBS_len(&extensions) != 0) {
313 uint16_t type;
314 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 /* Decode the next extension. */
317 if (!CBS_get_u16(&extensions, &type) ||
318 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
319 return 0;
320 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 if (type == extension_type) {
323 *out_data = CBS_data(&extension);
324 *out_len = CBS_len(&extension);
325 return 1;
326 }
327 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 return 0;
330}
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332static const uint16_t eccurves_default[] = {
David Benjamin4298d772015-12-19 00:18:25 -0500333 SSL_CURVE_SECP256R1,
334 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700335#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin4298d772015-12-19 00:18:25 -0500336 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700337#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800338};
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
341 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
342 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin0d56f882015-12-19 17:05:56 -0500343static void tls1_get_curvelist(SSL *ssl, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 const uint16_t **out_curve_ids,
345 size_t *out_curve_ids_len) {
346 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400347 /* Only clients send a curve list, so this function is only called
348 * on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500349 assert(ssl->server);
350 *out_curve_ids = ssl->s3->tmp.peer_ellipticcurvelist;
351 *out_curve_ids_len = ssl->s3->tmp.peer_ellipticcurvelist_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 return;
353 }
Adam Langley95c29f32014-06-20 12:00:00 -0700354
David Benjamin0d56f882015-12-19 17:05:56 -0500355 *out_curve_ids = ssl->tlsext_ellipticcurvelist;
356 *out_curve_ids_len = ssl->tlsext_ellipticcurvelist_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 if (!*out_curve_ids) {
358 *out_curve_ids = eccurves_default;
359 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
360 }
361}
David Benjamined439582014-07-14 19:13:02 -0400362
David Benjamin4298d772015-12-19 00:18:25 -0500363int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
David Benjamin55a43642015-04-20 14:45:55 -0400364 const uint16_t *curves, *peer_curves, *pref, *supp;
365 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400366
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500368 if (ssl->server == 0) {
369 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 }
371
David Benjamin4298d772015-12-19 00:18:25 -0500372 tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len);
373 tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800374
David Benjamin55a43642015-04-20 14:45:55 -0400375 if (peer_curves_len == 0) {
376 /* Clients are not required to send a supported_curves extension. In this
377 * case, the server is free to pick any curve it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500378 * section 4, paragraph 3.
379 *
380 * However, in the interests of compatibility, we will skip ECDH if the
381 * client didn't send an extension because we can't be sure that they'll
382 * support our favoured curve. */
383 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400384 }
385
David Benjamin4298d772015-12-19 00:18:25 -0500386 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamin55a43642015-04-20 14:45:55 -0400387 pref = curves;
388 pref_len = curves_len;
389 supp = peer_curves;
390 supp_len = peer_curves_len;
391 } else {
392 pref = peer_curves;
393 pref_len = peer_curves_len;
394 supp = curves;
395 supp_len = curves_len;
396 }
397
398 for (i = 0; i < pref_len; i++) {
399 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 if (pref[i] == supp[j]) {
David Benjamin4298d772015-12-19 00:18:25 -0500401 *out_curve_id = pref[i];
402 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 }
404 }
405 }
406
David Benjamin4298d772015-12-19 00:18:25 -0500407 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800408}
Adam Langley95c29f32014-06-20 12:00:00 -0700409
David Benjamin072334d2014-07-13 16:24:27 -0400410int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 const int *curves, size_t ncurves) {
412 uint16_t *curve_ids;
413 size_t i;
414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
416 if (curve_ids == NULL) {
417 return 0;
418 }
419
420 for (i = 0; i < ncurves; i++) {
David Benjamin4298d772015-12-19 00:18:25 -0500421 if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 OPENSSL_free(curve_ids);
423 return 0;
424 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 }
426
David Benjamin2755a3e2015-04-22 16:17:58 -0400427 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 *out_curve_ids = curve_ids;
429 *out_curve_ids_len = ncurves;
430
431 return 1;
432}
Adam Langley95c29f32014-06-20 12:00:00 -0700433
David Benjamin072334d2014-07-13 16:24:27 -0400434/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
435 * TLS curve ID and point format, respectively, for |ec|. It returns one on
436 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800437static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
438 uint8_t *out_comp_id, EC_KEY *ec) {
439 int nid;
440 uint16_t id;
441 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 if (ec == NULL) {
444 return 0;
445 }
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 grp = EC_KEY_get0_group(ec);
448 if (grp == NULL) {
449 return 0;
450 }
David Benjamin072334d2014-07-13 16:24:27 -0400451
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 /* Determine curve ID */
453 nid = EC_GROUP_get_curve_name(grp);
David Benjamin4298d772015-12-19 00:18:25 -0500454 if (!ssl_nid_to_curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 return 0;
456 }
David Benjamin072334d2014-07-13 16:24:27 -0400457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
459 *out_curve_id = id;
460
461 if (out_comp_id) {
462 if (EC_KEY_get0_public_key(ec) == NULL) {
463 return 0;
464 }
465 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
466 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
467 } else {
468 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
469 }
470 }
471
472 return 1;
473}
David Benjamin072334d2014-07-13 16:24:27 -0400474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
476 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400477 * preferences are checked; the peer (the server) does not send preferences. */
David Benjamin4298d772015-12-19 00:18:25 -0500478int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400480 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400483 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
David Benjamin4298d772015-12-19 00:18:25 -0500484 if (get_peer_curves && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400485 /* Servers do not present a preference list so, if we are a client, only
486 * check our list. */
487 continue;
488 }
489
David Benjamin4298d772015-12-19 00:18:25 -0500490 tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len);
David Benjamin55a43642015-04-20 14:45:55 -0400491 if (get_peer_curves && curves_len == 0) {
492 /* Clients are not required to send a supported_curves extension. In this
493 * case, the server is free to pick any curve it likes. See RFC 4492,
494 * section 4, paragraph 3. */
495 continue;
496 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 for (i = 0; i < curves_len; i++) {
498 if (curves[i] == curve_id) {
499 break;
500 }
501 }
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 if (i == curves_len) {
504 return 0;
505 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 }
David Benjamin033e5f42014-11-13 18:47:41 -0500507
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 return 1;
509}
David Benjamin033e5f42014-11-13 18:47:41 -0500510
David Benjamin0d56f882015-12-19 17:05:56 -0500511int tls1_check_ec_cert(SSL *ssl, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 int ret = 0;
513 EVP_PKEY *pkey = X509_get_pubkey(x);
514 uint16_t curve_id;
515 uint8_t comp_id;
516
David Benjamin758d1272015-11-20 17:47:25 -0500517 if (!pkey) {
518 goto done;
519 }
520 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
521 if (ec_key == NULL ||
522 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500523 !tls1_check_curve_id(ssl, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400524 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 goto done;
526 }
527
528 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500529
530done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400531 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 return ret;
533}
David Benjamin42e9a772014-09-02 23:18:44 -0400534
Adam Langley95c29f32014-06-20 12:00:00 -0700535/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langley95c29f32014-06-20 12:00:00 -0700538#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langley95c29f32014-06-20 12:00:00 -0700540#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamincff64722014-08-19 19:54:46 -0400544static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 tlsext_sigalg(TLSEXT_hash_sha512)
546 tlsext_sigalg(TLSEXT_hash_sha384)
547 tlsext_sigalg(TLSEXT_hash_sha256)
548 tlsext_sigalg(TLSEXT_hash_sha224)
549 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700550};
David Benjamin05da6e12014-07-12 20:42:55 -0400551
David Benjamin0d56f882015-12-19 17:05:56 -0500552size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400553 *psigs = tls12_sigalgs;
554 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800555}
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamin6e807652015-11-02 12:02:20 -0500557int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
558 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 const uint8_t *sent_sigs;
560 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400561 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800562
563 /* Should never happen */
564 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400565 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 *out_alert = SSL_AD_INTERNAL_ERROR;
567 return 0;
568 }
569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 /* Check key type is consistent with signature */
571 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400572 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
574 return 0;
575 }
576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500578 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
580 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
581 break;
582 }
583 }
584
David Benjamin788be4a2015-10-30 17:50:57 -0400585 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400586 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
588 return 0;
589 }
590
591 *out_md = tls12_get_hash(hash);
592 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400593 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
595 return 0;
596 }
597
598 return 1;
599}
600
601/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
602 * supported or doesn't appear in supported signature algorithms. Unlike
603 * ssl_cipher_get_disabled this applies to a specific session and not global
604 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500605void ssl_set_client_disabled(SSL *ssl) {
606 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 const uint8_t *sigalgs;
608 size_t i, sigalgslen;
609 int have_rsa = 0, have_ecdsa = 0;
610 c->mask_a = 0;
611 c->mask_k = 0;
612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 /* Now go through all signature algorithms seeing if we support any for RSA,
614 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500615 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
617 switch (sigalgs[1]) {
618 case TLSEXT_signature_rsa:
619 have_rsa = 1;
620 break;
621
622 case TLSEXT_signature_ecdsa:
623 have_ecdsa = 1;
624 break;
625 }
626 }
627
628 /* Disable auth if we don't include any appropriate signature algorithms. */
629 if (!have_rsa) {
630 c->mask_a |= SSL_aRSA;
631 }
632 if (!have_ecdsa) {
633 c->mask_a |= SSL_aECDSA;
634 }
635
636 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500637 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 c->mask_a |= SSL_aPSK;
639 c->mask_k |= SSL_kPSK;
640 }
641}
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langley614c66a2015-06-12 15:26:58 -0700643/* tls_extension represents a TLS extension that is handled internally. The
644 * |init| function is called for each handshake, before any other functions of
645 * the extension. Then the add and parse callbacks are called as needed.
646 *
647 * The parse callbacks receive a |CBS| that contains the contents of the
648 * extension (i.e. not including the type and length bytes). If an extension is
649 * not received then the parse callbacks will be called with a NULL CBS so that
650 * they can do any processing needed to handle the absence of an extension.
651 *
652 * The add callbacks receive a |CBB| to which the extension can be appended but
653 * the function is responsible for appending the type and length bytes too.
654 *
655 * All callbacks return one for success and zero for error. If a parse function
656 * returns zero then a fatal alert with value |*out_alert| will be sent. If
657 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
658struct tls_extension {
659 uint16_t value;
660 void (*init)(SSL *ssl);
661
662 int (*add_clienthello)(SSL *ssl, CBB *out);
663 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
664
665 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
666 int (*add_serverhello)(SSL *ssl, CBB *out);
667};
668
669
670/* Server name indication (SNI).
671 *
672 * https://tools.ietf.org/html/rfc6066#section-3. */
673
674static void ext_sni_init(SSL *ssl) {
675 ssl->s3->tmp.should_ack_sni = 0;
676}
677
678static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
679 if (ssl->tlsext_hostname == NULL) {
680 return 1;
681 }
682
683 CBB contents, server_name_list, name;
684 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
685 !CBB_add_u16_length_prefixed(out, &contents) ||
686 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
687 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
688 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
689 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
690 strlen(ssl->tlsext_hostname)) ||
691 !CBB_flush(out)) {
692 return 0;
693 }
694
695 return 1;
696}
697
David Benjamin0d56f882015-12-19 17:05:56 -0500698static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
699 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700700 if (contents == NULL) {
701 return 1;
702 }
703
704 if (CBS_len(contents) != 0) {
705 return 0;
706 }
707
708 assert(ssl->tlsext_hostname != NULL);
709
710 if (!ssl->hit) {
711 assert(ssl->session->tlsext_hostname == NULL);
712 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
713 if (!ssl->session->tlsext_hostname) {
714 *out_alert = SSL_AD_INTERNAL_ERROR;
715 return 0;
716 }
717 }
718
719 return 1;
720}
721
David Benjamin0d56f882015-12-19 17:05:56 -0500722static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
723 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700724 if (contents == NULL) {
725 return 1;
726 }
727
728 /* The servername extension is treated as follows:
729 *
730 * - Only the hostname type is supported with a maximum length of 255.
731 * - The servername is rejected if too long or if it contains zeros, in
732 * which case an fatal alert is generated.
733 * - The servername field is maintained together with the session cache.
734 * - When a session is resumed, the servername callback is invoked in order
735 * to allow the application to position itself to the right context.
736 * - The servername is acknowledged if it is new for a session or when
737 * it is identical to a previously used for the same session.
738 * Applications can control the behaviour. They can at any time
739 * set a 'desirable' servername for a new SSL object. This can be the
740 * case for example with HTTPS when a Host: header field is received and
741 * a renegotiation is requested. In this case, a possible servername
742 * presented in the new client hello is only acknowledged if it matches
743 * the value of the Host: field.
744 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
745 * if they provide for changing an explicit servername context for the
746 * session,
747 * i.e. when the session has been established with a servername extension.
748 */
749
750 CBS server_name_list;
751 char have_seen_host_name = 0;
752
753 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
754 CBS_len(&server_name_list) == 0 ||
755 CBS_len(contents) != 0) {
756 return 0;
757 }
758
759 /* Decode each ServerName in the extension. */
760 while (CBS_len(&server_name_list) > 0) {
761 uint8_t name_type;
762 CBS host_name;
763
764 if (!CBS_get_u8(&server_name_list, &name_type) ||
765 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
766 return 0;
767 }
768
769 /* Only host_name is supported. */
770 if (name_type != TLSEXT_NAMETYPE_host_name) {
771 continue;
772 }
773
774 if (have_seen_host_name) {
775 /* The ServerNameList MUST NOT contain more than one name of the same
776 * name_type. */
777 return 0;
778 }
779
780 have_seen_host_name = 1;
781
782 if (CBS_len(&host_name) == 0 ||
783 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
784 CBS_contains_zero_byte(&host_name)) {
785 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
786 return 0;
787 }
788
789 if (!ssl->hit) {
790 assert(ssl->session->tlsext_hostname == NULL);
791 if (ssl->session->tlsext_hostname) {
792 /* This should be impossible. */
793 return 0;
794 }
795
796 /* Copy the hostname as a string. */
797 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
798 *out_alert = SSL_AD_INTERNAL_ERROR;
799 return 0;
800 }
801
802 ssl->s3->tmp.should_ack_sni = 1;
803 }
804 }
805
806 return 1;
807}
808
809static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
810 if (ssl->hit ||
811 !ssl->s3->tmp.should_ack_sni ||
812 ssl->session->tlsext_hostname == NULL) {
813 return 1;
814 }
815
816 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
817 !CBB_add_u16(out, 0 /* length */)) {
818 return 0;
819 }
820
821 return 1;
822}
823
824
Adam Langley5021b222015-06-12 18:27:58 -0700825/* Renegotiation indication.
826 *
827 * https://tools.ietf.org/html/rfc5746 */
828
829static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
830 CBB contents, prev_finished;
831 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
832 !CBB_add_u16_length_prefixed(out, &contents) ||
833 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
834 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
835 ssl->s3->previous_client_finished_len) ||
836 !CBB_flush(out)) {
837 return 0;
838 }
839
840 return 1;
841}
842
843static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
844 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500845 /* Servers may not switch between omitting the extension and supporting it.
846 * See RFC 5746, sections 3.5 and 4.2. */
847 if (ssl->s3->initial_handshake_complete &&
848 (contents != NULL) != ssl->s3->send_connection_binding) {
849 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
850 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
851 return 0;
852 }
853
Adam Langley5021b222015-06-12 18:27:58 -0700854 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500855 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700856 * RI even on initial ServerHello because the client doesn't see any
857 * renegotiation during an attack. However this would mean we could not
858 * connect to any server which doesn't support RI.
859 *
David Benjamine9cddb82015-11-23 14:36:40 -0500860 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
861 * practical terms every client sets it so it's just assumed here. */
862 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700863 }
864
865 const size_t expected_len = ssl->s3->previous_client_finished_len +
866 ssl->s3->previous_server_finished_len;
867
868 /* Check for logic errors */
869 assert(!expected_len || ssl->s3->previous_client_finished_len);
870 assert(!expected_len || ssl->s3->previous_server_finished_len);
871
872 /* Parse out the extension contents. */
873 CBS renegotiated_connection;
874 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
875 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400876 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700877 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
878 return 0;
879 }
880
881 /* Check that the extension matches. */
882 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400883 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700884 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
885 return 0;
886 }
887
888 const uint8_t *d = CBS_data(&renegotiated_connection);
889 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
890 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400891 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700892 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
893 return 0;
894 }
895 d += ssl->s3->previous_client_finished_len;
896
897 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
898 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400899 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700900 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
901 return 0;
902 }
903 ssl->s3->send_connection_binding = 1;
904
905 return 1;
906}
907
908static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
909 CBS *contents) {
910 /* Renegotiation isn't supported as a server so this function should never be
911 * called after the initial handshake. */
912 assert(!ssl->s3->initial_handshake_complete);
913
914 CBS fake_contents;
915 static const uint8_t kFakeExtension[] = {0};
916
917 if (contents == NULL) {
918 if (ssl->s3->send_connection_binding) {
919 /* The renegotiation SCSV was received so pretend that we received a
920 * renegotiation extension. */
921 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
922 contents = &fake_contents;
923 /* We require that the renegotiation extension is at index zero of
924 * kExtensions. */
925 ssl->s3->tmp.extensions.received |= (1u << 0);
926 } else {
927 return 1;
928 }
929 }
930
931 CBS renegotiated_connection;
932
933 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
934 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400935 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700936 return 0;
937 }
938
939 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500940 if (!CBS_mem_equal(&renegotiated_connection,
941 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700942 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400943 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700944 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
945 return 0;
946 }
947
948 ssl->s3->send_connection_binding = 1;
949
950 return 1;
951}
952
953static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
954 CBB contents, prev_finished;
955 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
956 !CBB_add_u16_length_prefixed(out, &contents) ||
957 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
958 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
959 ssl->s3->previous_client_finished_len) ||
960 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
961 ssl->s3->previous_server_finished_len) ||
962 !CBB_flush(out)) {
963 return 0;
964 }
965
966 return 1;
967}
968
Adam Langley0a056712015-07-01 15:03:33 -0700969
970/* Extended Master Secret.
971 *
972 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
973
974static void ext_ems_init(SSL *ssl) {
975 ssl->s3->tmp.extended_master_secret = 0;
976}
977
978static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
979 if (ssl->version == SSL3_VERSION) {
980 return 1;
981 }
982
983 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
984 !CBB_add_u16(out, 0 /* length */)) {
985 return 0;
986 }
987
988 return 1;
989}
990
991static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
992 CBS *contents) {
993 if (contents == NULL) {
994 return 1;
995 }
996
997 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
998 return 0;
999 }
1000
1001 ssl->s3->tmp.extended_master_secret = 1;
1002 return 1;
1003}
1004
David Benjamin0d56f882015-12-19 17:05:56 -05001005static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1006 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -07001007 if (ssl->version == SSL3_VERSION || contents == NULL) {
1008 return 1;
1009 }
1010
1011 if (CBS_len(contents) != 0) {
1012 return 0;
1013 }
1014
1015 ssl->s3->tmp.extended_master_secret = 1;
1016 return 1;
1017}
1018
1019static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1020 if (!ssl->s3->tmp.extended_master_secret) {
1021 return 1;
1022 }
1023
1024 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1025 !CBB_add_u16(out, 0 /* length */)) {
1026 return 0;
1027 }
1028
1029 return 1;
1030}
1031
Adam Langley9b05bc52015-07-01 15:25:33 -07001032
1033/* Session tickets.
1034 *
1035 * https://tools.ietf.org/html/rfc5077 */
1036
1037static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1038 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1039 return 1;
1040 }
1041
1042 const uint8_t *ticket_data = NULL;
1043 int ticket_len = 0;
1044
1045 /* Renegotiation does not participate in session resumption. However, still
1046 * advertise the extension to avoid potentially breaking servers which carry
1047 * over the state from the previous handshake, such as OpenSSL servers
1048 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1049 if (!ssl->s3->initial_handshake_complete &&
1050 ssl->session != NULL &&
1051 ssl->session->tlsext_tick != NULL) {
1052 ticket_data = ssl->session->tlsext_tick;
1053 ticket_len = ssl->session->tlsext_ticklen;
1054 }
1055
1056 CBB ticket;
1057 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1058 !CBB_add_u16_length_prefixed(out, &ticket) ||
1059 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1060 !CBB_flush(out)) {
1061 return 0;
1062 }
1063
1064 return 1;
1065}
1066
1067static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1068 CBS *contents) {
1069 ssl->tlsext_ticket_expected = 0;
1070
1071 if (contents == NULL) {
1072 return 1;
1073 }
1074
1075 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1076 * this function should never be called, even if the server tries to send the
1077 * extension. */
1078 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1079
1080 if (CBS_len(contents) != 0) {
1081 return 0;
1082 }
1083
1084 ssl->tlsext_ticket_expected = 1;
1085 return 1;
1086}
1087
David Benjamin0d56f882015-12-19 17:05:56 -05001088static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1089 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001090 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001091 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001092 return 1;
1093}
1094
1095static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1096 if (!ssl->tlsext_ticket_expected) {
1097 return 1;
1098 }
1099
1100 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1101 * true. */
1102 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1103
1104 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1105 !CBB_add_u16(out, 0 /* length */)) {
1106 return 0;
1107 }
1108
1109 return 1;
1110}
1111
1112
Adam Langley2e857bd2015-07-01 16:09:19 -07001113/* Signature Algorithms.
1114 *
1115 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1116
1117static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1118 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1119 return 1;
1120 }
1121
1122 const uint8_t *sigalgs_data;
1123 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1124
1125 CBB contents, sigalgs;
1126 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1127 !CBB_add_u16_length_prefixed(out, &contents) ||
1128 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1129 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1130 !CBB_flush(out)) {
1131 return 0;
1132 }
1133
1134 return 1;
1135}
1136
1137static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1138 CBS *contents) {
1139 if (contents != NULL) {
1140 /* Servers MUST NOT send this extension. */
1141 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001142 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001143 return 0;
1144 }
1145
1146 return 1;
1147}
1148
1149static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1150 CBS *contents) {
1151 OPENSSL_free(ssl->cert->peer_sigalgs);
1152 ssl->cert->peer_sigalgs = NULL;
1153 ssl->cert->peer_sigalgslen = 0;
1154
Adam Langley2e857bd2015-07-01 16:09:19 -07001155 if (contents == NULL) {
1156 return 1;
1157 }
1158
1159 CBS supported_signature_algorithms;
1160 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001161 CBS_len(contents) != 0 ||
1162 CBS_len(&supported_signature_algorithms) == 0 ||
1163 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001164 return 0;
1165 }
1166
1167 return 1;
1168}
1169
1170static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1171 /* Servers MUST NOT send this extension. */
1172 return 1;
1173}
1174
1175
Adam Langleybb0bd042015-07-01 16:21:03 -07001176/* OCSP Stapling.
1177 *
1178 * https://tools.ietf.org/html/rfc6066#section-8 */
1179
1180static void ext_ocsp_init(SSL *ssl) {
1181 ssl->s3->tmp.certificate_status_expected = 0;
1182}
1183
1184static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1185 if (!ssl->ocsp_stapling_enabled) {
1186 return 1;
1187 }
1188
1189 CBB contents;
1190 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1191 !CBB_add_u16_length_prefixed(out, &contents) ||
1192 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1193 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1194 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1195 !CBB_flush(out)) {
1196 return 0;
1197 }
1198
1199 return 1;
1200}
1201
1202static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001203 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001204 if (contents == NULL) {
1205 return 1;
1206 }
1207
1208 if (CBS_len(contents) != 0) {
1209 return 0;
1210 }
1211
1212 ssl->s3->tmp.certificate_status_expected = 1;
1213 return 1;
1214}
1215
1216static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1217 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001218 if (contents == NULL) {
1219 return 1;
1220 }
1221
1222 uint8_t status_type;
1223 if (!CBS_get_u8(contents, &status_type)) {
1224 return 0;
1225 }
1226
1227 /* We cannot decide whether OCSP stapling will occur yet because the correct
1228 * SSL_CTX might not have been selected. */
1229 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1230
Adam Langleybb0bd042015-07-01 16:21:03 -07001231 return 1;
1232}
1233
1234static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001235 /* The extension shouldn't be sent when resuming sessions. */
1236 if (ssl->hit ||
1237 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001238 ssl->ctx->ocsp_response_length == 0) {
1239 return 1;
1240 }
1241
1242 ssl->s3->tmp.certificate_status_expected = 1;
1243
1244 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1245 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001246}
1247
1248
Adam Langley97dfcbf2015-07-01 18:35:20 -07001249/* Next protocol negotiation.
1250 *
1251 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1252
1253static void ext_npn_init(SSL *ssl) {
1254 ssl->s3->next_proto_neg_seen = 0;
1255}
1256
1257static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1258 if (ssl->s3->initial_handshake_complete ||
1259 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001260 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001261 SSL_IS_DTLS(ssl)) {
1262 return 1;
1263 }
1264
1265 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1266 !CBB_add_u16(out, 0 /* length */)) {
1267 return 0;
1268 }
1269
1270 return 1;
1271}
1272
1273static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1274 CBS *contents) {
1275 if (contents == NULL) {
1276 return 1;
1277 }
1278
1279 /* If any of these are false then we should never have sent the NPN
1280 * extension in the ClientHello and thus this function should never have been
1281 * called. */
1282 assert(!ssl->s3->initial_handshake_complete);
1283 assert(!SSL_IS_DTLS(ssl));
1284 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001285 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001286
David Benjamin76c2efc2015-08-31 14:24:29 -04001287 if (ssl->s3->alpn_selected != NULL) {
1288 /* NPN and ALPN may not be negotiated in the same connection. */
1289 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1290 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1291 return 0;
1292 }
1293
Adam Langley97dfcbf2015-07-01 18:35:20 -07001294 const uint8_t *const orig_contents = CBS_data(contents);
1295 const size_t orig_len = CBS_len(contents);
1296
1297 while (CBS_len(contents) != 0) {
1298 CBS proto;
1299 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1300 CBS_len(&proto) == 0) {
1301 return 0;
1302 }
1303 }
1304
1305 uint8_t *selected;
1306 uint8_t selected_len;
1307 if (ssl->ctx->next_proto_select_cb(
1308 ssl, &selected, &selected_len, orig_contents, orig_len,
1309 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1310 *out_alert = SSL_AD_INTERNAL_ERROR;
1311 return 0;
1312 }
1313
David Benjamin79978df2015-12-25 15:56:49 -05001314 OPENSSL_free(ssl->s3->next_proto_negotiated);
1315 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1316 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001317 *out_alert = SSL_AD_INTERNAL_ERROR;
1318 return 0;
1319 }
1320
David Benjamin79978df2015-12-25 15:56:49 -05001321 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001322 ssl->s3->next_proto_neg_seen = 1;
1323
1324 return 1;
1325}
1326
1327static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1328 CBS *contents) {
1329 if (contents != NULL && CBS_len(contents) != 0) {
1330 return 0;
1331 }
1332
1333 if (contents == NULL ||
1334 ssl->s3->initial_handshake_complete ||
1335 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1336 * afterwards, parsing the ALPN extension will clear
1337 * |next_proto_neg_seen|. */
1338 ssl->s3->alpn_selected != NULL ||
1339 ssl->ctx->next_protos_advertised_cb == NULL ||
1340 SSL_IS_DTLS(ssl)) {
1341 return 1;
1342 }
1343
1344 ssl->s3->next_proto_neg_seen = 1;
1345 return 1;
1346}
1347
1348static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1349 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1350 * parsed. */
1351 if (!ssl->s3->next_proto_neg_seen) {
1352 return 1;
1353 }
1354
1355 const uint8_t *npa;
1356 unsigned npa_len;
1357
1358 if (ssl->ctx->next_protos_advertised_cb(
1359 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1360 SSL_TLSEXT_ERR_OK) {
1361 ssl->s3->next_proto_neg_seen = 0;
1362 return 1;
1363 }
1364
1365 CBB contents;
1366 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1367 !CBB_add_u16_length_prefixed(out, &contents) ||
1368 !CBB_add_bytes(&contents, npa, npa_len) ||
1369 !CBB_flush(out)) {
1370 return 0;
1371 }
1372
1373 return 1;
1374}
1375
1376
Adam Langleyab8d87d2015-07-10 12:21:39 -07001377/* Signed certificate timestamps.
1378 *
1379 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1380
1381static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1382 if (!ssl->signed_cert_timestamps_enabled) {
1383 return 1;
1384 }
1385
1386 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1387 !CBB_add_u16(out, 0 /* length */)) {
1388 return 0;
1389 }
1390
1391 return 1;
1392}
1393
1394static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1395 CBS *contents) {
1396 if (contents == NULL) {
1397 return 1;
1398 }
1399
1400 /* If this is false then we should never have sent the SCT extension in the
1401 * ClientHello and thus this function should never have been called. */
1402 assert(ssl->signed_cert_timestamps_enabled);
1403
1404 if (CBS_len(contents) == 0) {
1405 *out_alert = SSL_AD_DECODE_ERROR;
1406 return 0;
1407 }
1408
1409 /* Session resumption uses the original session information. */
1410 if (!ssl->hit &&
1411 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1412 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1413 *out_alert = SSL_AD_INTERNAL_ERROR;
1414 return 0;
1415 }
1416
1417 return 1;
1418}
1419
1420static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1421 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001422 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001423}
1424
1425static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001426 /* The extension shouldn't be sent when resuming sessions. */
1427 if (ssl->hit ||
1428 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001429 return 1;
1430 }
1431
1432 CBB contents;
1433 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1434 CBB_add_u16_length_prefixed(out, &contents) &&
1435 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1436 ssl->ctx->signed_cert_timestamp_list_length) &&
1437 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001438}
1439
1440
Adam Langleyf18e4532015-07-10 13:39:53 -07001441/* Application-level Protocol Negotiation.
1442 *
1443 * https://tools.ietf.org/html/rfc7301 */
1444
1445static void ext_alpn_init(SSL *ssl) {
1446 OPENSSL_free(ssl->s3->alpn_selected);
1447 ssl->s3->alpn_selected = NULL;
1448}
1449
1450static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1451 if (ssl->alpn_client_proto_list == NULL ||
1452 ssl->s3->initial_handshake_complete) {
1453 return 1;
1454 }
1455
1456 CBB contents, proto_list;
1457 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1458 !CBB_add_u16_length_prefixed(out, &contents) ||
1459 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1460 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1461 ssl->alpn_client_proto_list_len) ||
1462 !CBB_flush(out)) {
1463 return 0;
1464 }
1465
1466 return 1;
1467}
1468
1469static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1470 CBS *contents) {
1471 if (contents == NULL) {
1472 return 1;
1473 }
1474
1475 assert(!ssl->s3->initial_handshake_complete);
1476 assert(ssl->alpn_client_proto_list != NULL);
1477
David Benjamin76c2efc2015-08-31 14:24:29 -04001478 if (ssl->s3->next_proto_neg_seen) {
1479 /* NPN and ALPN may not be negotiated in the same connection. */
1480 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1481 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1482 return 0;
1483 }
1484
Adam Langleyf18e4532015-07-10 13:39:53 -07001485 /* The extension data consists of a ProtocolNameList which must have
1486 * exactly one ProtocolName. Each of these is length-prefixed. */
1487 CBS protocol_name_list, protocol_name;
1488 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1489 CBS_len(contents) != 0 ||
1490 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1491 /* Empty protocol names are forbidden. */
1492 CBS_len(&protocol_name) == 0 ||
1493 CBS_len(&protocol_name_list) != 0) {
1494 return 0;
1495 }
1496
1497 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1498 &ssl->s3->alpn_selected_len)) {
1499 *out_alert = SSL_AD_INTERNAL_ERROR;
1500 return 0;
1501 }
1502
1503 return 1;
1504}
1505
1506static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1507 CBS *contents) {
1508 if (contents == NULL) {
1509 return 1;
1510 }
1511
1512 if (ssl->ctx->alpn_select_cb == NULL ||
1513 ssl->s3->initial_handshake_complete) {
1514 return 1;
1515 }
1516
1517 /* ALPN takes precedence over NPN. */
1518 ssl->s3->next_proto_neg_seen = 0;
1519
1520 CBS protocol_name_list;
1521 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1522 CBS_len(contents) != 0 ||
1523 CBS_len(&protocol_name_list) < 2) {
1524 return 0;
1525 }
1526
1527 /* Validate the protocol list. */
1528 CBS protocol_name_list_copy = protocol_name_list;
1529 while (CBS_len(&protocol_name_list_copy) > 0) {
1530 CBS protocol_name;
1531
1532 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1533 /* Empty protocol names are forbidden. */
1534 CBS_len(&protocol_name) == 0) {
1535 return 0;
1536 }
1537 }
1538
1539 const uint8_t *selected;
1540 uint8_t selected_len;
1541 if (ssl->ctx->alpn_select_cb(
1542 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1543 CBS_len(&protocol_name_list),
1544 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1545 OPENSSL_free(ssl->s3->alpn_selected);
1546 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1547 if (ssl->s3->alpn_selected == NULL) {
1548 *out_alert = SSL_AD_INTERNAL_ERROR;
1549 return 0;
1550 }
1551 ssl->s3->alpn_selected_len = selected_len;
1552 }
1553
1554 return 1;
1555}
1556
1557static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1558 if (ssl->s3->alpn_selected == NULL) {
1559 return 1;
1560 }
1561
1562 CBB contents, proto_list, proto;
1563 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1564 !CBB_add_u16_length_prefixed(out, &contents) ||
1565 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1566 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001567 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1568 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001569 !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
David Benjamin0d56f882015-12-19 17:05:56 -05002232static int ssl_scan_clienthello_tlsext(SSL *ssl, 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) {
David Benjamin0d56f882015-12-19 17:05:56 -05002236 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002237 }
2238 }
2239
David Benjamin0d56f882015-12-19 17:05:56 -05002240 ssl->s3->tmp.extensions.received = 0;
2241 ssl->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. */
David Benjamin0d56f882015-12-19 17:05:56 -05002270 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002271 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) {
David Benjamin0d56f882015-12-19 17:05:56 -05002279 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002280 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2281 return 0;
2282 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002283 continue;
2284 }
2285
David Benjamin0d56f882015-12-19 17:05:56 -05002286 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002287 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002288 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002289 *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++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002298 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002299 /* Extension wasn't observed so call the callback with a NULL
2300 * parameter. */
2301 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002302 if (!kExtensions[i].parse_clienthello(ssl, &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
David Benjamin0d56f882015-12-19 17:05:56 -05002314int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002316 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2317 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 return 0;
2319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320
David Benjamin0d56f882015-12-19 17:05:56 -05002321 if (ssl_check_clienthello_tlsext(ssl) <= 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
David Benjamin0d56f882015-12-19 17:05:56 -05002331static int ssl_scan_serverhello_tlsext(SSL *ssl, 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) {
David Benjamin0d56f882015-12-19 17:05:56 -05002360 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002361 return 0;
2362 }
2363 continue;
2364 }
2365
David Benjamin0d56f882015-12-19 17:05:56 -05002366 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002367 /* 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;
David Benjamin0d56f882015-12-19 17:05:56 -05002377 if (!ext->parse_serverhello(ssl, &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;
David Benjamin0d56f882015-12-19 17:05:56 -05002392 if (!kExtensions[i].parse_serverhello(ssl, &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
David Benjamin0d56f882015-12-19 17:05:56 -05002404static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002405 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
David Benjamin0d56f882015-12-19 17:05:56 -05002411 if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2412 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2413 ssl->ctx->tlsext_servername_arg);
2414 } else if (ssl->initial_ctx != NULL &&
2415 ssl->initial_ctx->tlsext_servername_callback != 0) {
2416 ret = ssl->initial_ctx->tlsext_servername_callback(
2417 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002418 }
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:
David Benjamin0d56f882015-12-19 17:05:56 -05002422 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002424
Adam Langleyfcf25832014-12-18 17:42:32 -08002425 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002426 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002430 ssl->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
David Benjamin0d56f882015-12-19 17:05:56 -05002438static int ssl_check_serverhello_tlsext(SSL *ssl) {
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
David Benjamin0d56f882015-12-19 17:05:56 -05002442 if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2443 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2444 ssl->ctx->tlsext_servername_arg);
2445 } else if (ssl->initial_ctx != NULL &&
2446 ssl->initial_ctx->tlsext_servername_callback != 0) {
2447 ret = ssl->initial_ctx->tlsext_servername_callback(
2448 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002449 }
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:
David Benjamin0d56f882015-12-19 17:05:56 -05002453 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002454 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002455
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002457 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002458 return 1;
2459
2460 default:
2461 return 1;
2462 }
2463}
2464
David Benjamin0d56f882015-12-19 17:05:56 -05002465int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002466 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002467 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2468 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 return 0;
2470 }
2471
David Benjamin0d56f882015-12-19 17:05:56 -05002472 if (ssl_check_serverhello_tlsext(ssl) <= 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,
David Benjaminef1b0092015-11-21 14:05:44 -05002481 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002482 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
David Benjaminef1b0092015-11-21 14:05:44 -05002493 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002494 *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 Benjaminadcc3952015-04-26 13:07:57 -04002500 /* Ensure there is room for the key name and the largest IV
2501 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2502 * the maximum IV length should be well under the minimum size for the
2503 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002504 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2505 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002507 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002508
David Benjamine3aa1d92015-06-16 15:34:50 -04002509 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002510 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2511 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2512 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002513 if (cb_ret < 0) {
2514 ret = 0;
2515 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002517 if (cb_ret == 0) {
2518 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002519 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002520 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002521 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 }
2523 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002524 /* Check the key name matches. */
2525 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2526 SSL_TICKET_KEY_NAME_LEN) != 0) {
2527 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002529 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2530 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002532 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2533 ssl_ctx->tlsext_tick_aes_key, iv)) {
2534 ret = 0;
2535 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 }
2537 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002538 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002539
David Benjamine3aa1d92015-06-16 15:34:50 -04002540 /* Check the MAC at the end of the ticket. */
2541 uint8_t mac[EVP_MAX_MD_SIZE];
2542 size_t mac_len = HMAC_size(&hmac_ctx);
2543 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002544 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002545 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002547 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2548 HMAC_Final(&hmac_ctx, mac, NULL);
2549 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2550 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 }
2552
David Benjamine3aa1d92015-06-16 15:34:50 -04002553 /* Decrypt the session data. */
2554 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2555 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2556 mac_len;
2557 plaintext = OPENSSL_malloc(ciphertext_len);
2558 if (plaintext == NULL) {
2559 ret = 0;
2560 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002562 if (ciphertext_len >= INT_MAX) {
2563 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002565 int len1, len2;
2566 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2567 (int)ciphertext_len) ||
2568 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2569 ERR_clear_error(); /* Don't leave an error on the queue. */
2570 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 }
2572
David Benjamine3aa1d92015-06-16 15:34:50 -04002573 /* Decode the session. */
2574 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2575 if (session == NULL) {
2576 ERR_clear_error(); /* Don't leave an error on the queue. */
2577 goto done;
2578 }
2579
2580 /* Copy the client's session ID into the new session, to denote the ticket has
2581 * been accepted. */
2582 memcpy(session->session_id, session_id, session_id_len);
2583 session->session_id_length = session_id_len;
2584
2585 *out_session = session;
2586
2587done:
2588 OPENSSL_free(plaintext);
2589 HMAC_CTX_cleanup(&hmac_ctx);
2590 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2591 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002592}
Adam Langley95c29f32014-06-20 12:00:00 -07002593
2594/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002595typedef struct {
2596 int nid;
2597 int id;
2598} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002599
Adam Langleyfcf25832014-12-18 17:42:32 -08002600static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2601 {NID_sha1, TLSEXT_hash_sha1},
2602 {NID_sha224, TLSEXT_hash_sha224},
2603 {NID_sha256, TLSEXT_hash_sha256},
2604 {NID_sha384, TLSEXT_hash_sha384},
2605 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2608 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2611 size_t i;
2612 for (i = 0; i < tlen; i++) {
2613 if (table[i].nid == nid) {
2614 return table[i].id;
2615 }
2616 }
Adam Langley95c29f32014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 return -1;
2619}
Adam Langley95c29f32014-06-20 12:00:00 -07002620
David Benjaminb4d65fd2015-05-29 17:11:21 -04002621int tls12_get_sigid(int pkey_type) {
2622 return tls12_find_id(pkey_type, tls12_sig,
2623 sizeof(tls12_sig) / sizeof(tls12_lookup));
2624}
2625
David Benjaminfc825122015-12-18 01:57:43 -05002626int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2627 int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2628 sizeof(tls12_md) / sizeof(tls12_lookup));
2629 int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -07002630
David Benjaminfc825122015-12-18 01:57:43 -05002631 return md_id != -1 &&
2632 sig_id != -1 &&
2633 CBB_add_u8(out, (uint8_t)md_id) &&
2634 CBB_add_u8(out, (uint8_t)sig_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002635}
2636
Adam Langleyfcf25832014-12-18 17:42:32 -08002637const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2638 switch (hash_alg) {
2639 case TLSEXT_hash_md5:
2640 return EVP_md5();
2641
2642 case TLSEXT_hash_sha1:
2643 return EVP_sha1();
2644
2645 case TLSEXT_hash_sha224:
2646 return EVP_sha224();
2647
2648 case TLSEXT_hash_sha256:
2649 return EVP_sha256();
2650
2651 case TLSEXT_hash_sha384:
2652 return EVP_sha384();
2653
2654 case TLSEXT_hash_sha512:
2655 return EVP_sha512();
2656
2657 default:
2658 return NULL;
2659 }
2660}
Adam Langley95c29f32014-06-20 12:00:00 -07002661
David Benjaminec2f27d2014-11-13 19:17:25 -05002662/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2663 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002664static int tls12_get_pkey_type(uint8_t sig_alg) {
2665 switch (sig_alg) {
2666 case TLSEXT_signature_rsa:
2667 return EVP_PKEY_RSA;
2668
2669 case TLSEXT_signature_ecdsa:
2670 return EVP_PKEY_EC;
2671
2672 default:
2673 return -1;
2674 }
2675}
Adam Langley95c29f32014-06-20 12:00:00 -07002676
Steven Valdez0d62f262015-09-04 12:41:04 -04002677OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2678 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002679
Steven Valdez0d62f262015-09-04 12:41:04 -04002680int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002682 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002683 return 1;
2684 }
David Benjamincd996942014-07-20 16:23:51 -04002685
Steven Valdez0d62f262015-09-04 12:41:04 -04002686 CERT *const cert = ssl->cert;
2687 OPENSSL_free(cert->peer_sigalgs);
2688 cert->peer_sigalgs = NULL;
2689 cert->peer_sigalgslen = 0;
2690
2691 size_t num_sigalgs = CBS_len(in_sigalgs);
2692
2693 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 return 0;
2695 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002696 num_sigalgs /= 2;
2697
2698 /* supported_signature_algorithms in the certificate request is
2699 * allowed to be empty. */
2700 if (num_sigalgs == 0) {
2701 return 1;
2702 }
2703
2704 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2705 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2706 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2707 if (cert->peer_sigalgs == NULL) {
2708 return 0;
2709 }
2710 cert->peer_sigalgslen = num_sigalgs;
2711
2712 CBS sigalgs;
2713 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2714
2715 size_t i;
2716 for (i = 0; i < num_sigalgs; i++) {
2717 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2718 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2719 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2720 return 0;
2721 }
2722 }
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 return 1;
2725}
David Benjaminec2f27d2014-11-13 19:17:25 -05002726
David Benjamind1d80782015-07-05 11:54:09 -04002727const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2728 CERT *cert = ssl->cert;
2729 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002730 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002731
Steven Valdez0d62f262015-09-04 12:41:04 -04002732 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2733 NID_sha224, NID_sha1};
2734
2735 const int *digest_nids = kDefaultDigestList;
2736 size_t num_digest_nids =
2737 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2738 if (cert->digest_nids != NULL) {
2739 digest_nids = cert->digest_nids;
2740 num_digest_nids = cert->num_digest_nids;
2741 }
2742
2743 for (i = 0; i < num_digest_nids; i++) {
2744 const int digest_nid = digest_nids[i];
2745 for (j = 0; j < cert->peer_sigalgslen; j++) {
2746 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2747 if (md == NULL ||
2748 digest_nid != EVP_MD_type(md) ||
2749 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2750 continue;
2751 }
2752
2753 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002755 }
Adam Langley95c29f32014-06-20 12:00:00 -07002756
Adam Langleyfcf25832014-12-18 17:42:32 -08002757 /* If no suitable digest may be found, default to SHA-1. */
2758 return EVP_sha1();
2759}
Adam Langley95c29f32014-06-20 12:00:00 -07002760
David Benjamind6a4ae92015-08-06 11:10:51 -04002761int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2762 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002764
2765 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002766 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2767 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002769
David Benjamind6a4ae92015-08-06 11:10:51 -04002770 static const char kClientIDMagic[] = "TLS Channel ID signature";
2771 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2772
2773 if (ssl->hit) {
2774 static const char kResumptionMagic[] = "Resumption";
2775 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2776 if (ssl->session->original_handshake_hash_len == 0) {
2777 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2778 goto err;
2779 }
2780 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2781 ssl->session->original_handshake_hash_len);
2782 }
2783
2784 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2785 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2786 sizeof(handshake_hash));
2787 if (handshake_hash_len < 0) {
2788 goto err;
2789 }
2790 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2791 unsigned len_u;
2792 EVP_DigestFinal_ex(&ctx, out, &len_u);
2793 *out_len = len_u;
2794
2795 ret = 1;
2796
2797err:
2798 EVP_MD_CTX_cleanup(&ctx);
2799 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002800}
Adam Langley1258b6a2014-06-20 12:00:00 -07002801
2802/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002803 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2804 * data. */
2805int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 int digest_len;
2807 /* This function should never be called for a resumed session because the
2808 * handshake hashes that we wish to record are for the original, full
2809 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002810 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 return -1;
2812 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002813
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002815 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2816 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 if (digest_len < 0) {
2818 return -1;
2819 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002820
David Benjamin0d56f882015-12-19 17:05:56 -05002821 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002822
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 return 1;
2824}