blob: eb47e198b4b769ad03571e2cc7c77aa512a5d432 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjaminf0ae1702015-04-07 23:05:04 -0400109#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400110#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400112#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114
David Benjamin03973092014-06-24 23:27:17 -0400115#include <openssl/bytestring.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400116#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700117#include <openssl/evp.h>
118#include <openssl/hmac.h>
119#include <openssl/mem.h>
120#include <openssl/obj.h>
121#include <openssl/rand.h>
122
David Benjamin2ee94aa2015-04-07 22:38:30 -0400123#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800124
125
David Benjamin6c7aed02014-08-27 16:42:38 -0400126static int ssl_check_clienthello_tlsext(SSL *s);
127static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjamin338fcaf2014-12-11 01:20:52 -0500129const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500130 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800131 tls1_setup_key_block,
132 tls1_generate_master_secret,
133 tls1_change_cipher_state,
134 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800141};
Adam Langley95c29f32014-06-20 12:00:00 -0700142
David Benjamin338fcaf2014-12-11 01:20:52 -0500143const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500144 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800145 tls1_setup_key_block,
146 tls1_generate_master_secret,
147 tls1_change_cipher_state,
148 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_setup_key_block,
160 tls1_generate_master_secret,
161 tls1_change_cipher_state,
162 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 tls1_cert_verify_mac,
164 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
165 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
166 tls1_alert_code,
167 tls1_export_keying_material,
168 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
169 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800170};
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172static int compare_uint16_t(const void *p1, const void *p2) {
173 uint16_t u1 = *((const uint16_t *)p1);
174 uint16_t u2 = *((const uint16_t *)p2);
175 if (u1 < u2) {
176 return -1;
177 } else if (u1 > u2) {
178 return 1;
179 } else {
180 return 0;
181 }
182}
David Benjamin35a7a442014-07-05 00:23:20 -0400183
Adam Langleyfcf25832014-12-18 17:42:32 -0800184/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
185 * more than one extension of the same type in a ClientHello or ServerHello.
186 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400187 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800188static int tls1_check_duplicate_extensions(const CBS *cbs) {
189 CBS extensions = *cbs;
190 size_t num_extensions = 0, i = 0;
191 uint16_t *extension_types = NULL;
192 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400193
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 /* First pass: count the extensions. */
195 while (CBS_len(&extensions) > 0) {
196 uint16_t type;
197 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 if (!CBS_get_u16(&extensions, &type) ||
200 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
201 goto done;
202 }
David Benjamin35a7a442014-07-05 00:23:20 -0400203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 num_extensions++;
205 }
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 if (num_extensions == 0) {
208 return 1;
209 }
David Benjamin9a373592014-07-25 04:27:53 -0400210
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 extension_types =
212 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
213 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400214 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 goto done;
216 }
David Benjamin35a7a442014-07-05 00:23:20 -0400217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 /* Second pass: gather the extension types. */
219 extensions = *cbs;
220 for (i = 0; i < num_extensions; i++) {
221 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400222
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
224 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
225 /* This should not happen. */
226 goto done;
227 }
228 }
229 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* Sort the extensions and make sure there are no duplicates. */
232 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
233 for (i = 1; i < num_extensions; i++) {
234 if (extension_types[i - 1] == extension_types[i]) {
235 goto done;
236 }
237 }
David Benjamin35a7a442014-07-05 00:23:20 -0400238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 ret = 1;
240
David Benjamin35a7a442014-07-05 00:23:20 -0400241done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400242 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 return ret;
244}
David Benjamin35a7a442014-07-05 00:23:20 -0400245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
247 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700250
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (/* Skip client version. */
252 !CBS_skip(&client_hello, 2) ||
253 /* Skip client nonce. */
254 !CBS_skip(&client_hello, 32) ||
255 /* Extract session_id. */
256 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
257 return 0;
258 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 ctx->session_id = CBS_data(&session_id);
261 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 /* Skip past DTLS cookie */
264 if (SSL_IS_DTLS(ctx->ssl)) {
265 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
268 return 0;
269 }
270 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 /* Extract cipher_suites. */
273 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
274 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
275 return 0;
276 }
277 ctx->cipher_suites = CBS_data(&cipher_suites);
278 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 /* Extract compression_methods. */
281 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
282 CBS_len(&compression_methods) < 1) {
283 return 0;
284 }
285 ctx->compression_methods = CBS_data(&compression_methods);
286 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 /* If the ClientHello ends here then it's valid, but doesn't have any
289 * extensions. (E.g. SSLv3.) */
290 if (CBS_len(&client_hello) == 0) {
291 ctx->extensions = NULL;
292 ctx->extensions_len = 0;
293 return 1;
294 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 /* Extract extensions and check it is valid. */
297 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
298 !tls1_check_duplicate_extensions(&extensions) ||
299 CBS_len(&client_hello) != 0) {
300 return 0;
301 }
302 ctx->extensions = CBS_data(&extensions);
303 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 return 1;
306}
Adam Langleydc9b1412014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308char SSL_early_callback_ctx_extension_get(
309 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
310 const uint8_t **out_data, size_t *out_len) {
311 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 while (CBS_len(&extensions) != 0) {
316 uint16_t type;
317 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 /* Decode the next extension. */
320 if (!CBS_get_u16(&extensions, &type) ||
321 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
322 return 0;
323 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 if (type == extension_type) {
326 *out_data = CBS_data(&extension);
327 *out_len = CBS_len(&extension);
328 return 1;
329 }
330 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 return 0;
333}
Adam Langley95c29f32014-06-20 12:00:00 -0700334
David Benjamin52e5bac2014-12-27 02:27:35 -0500335struct tls_curve {
336 uint16_t curve_id;
337 int nid;
338};
339
David Benjamin70bd80a2014-12-27 03:06:46 -0500340/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500341static const struct tls_curve tls_curves[] = {
342 {21, NID_secp224r1},
343 {23, NID_X9_62_prime256v1},
344 {24, NID_secp384r1},
345 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800346};
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langleyfcf25832014-12-18 17:42:32 -0800348static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400349 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500350 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800351};
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500354 size_t i;
355 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
356 if (curve_id == tls_curves[i].curve_id) {
357 return tls_curves[i].nid;
358 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500360 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
David Benjamin70bd80a2014-12-27 03:06:46 -0500363int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500365 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
366 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500367 *out_curve_id = tls_curves[i].curve_id;
368 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 }
370 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 return 0;
372}
373
374/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
375 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
376 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400377static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 const uint16_t **out_curve_ids,
379 size_t *out_curve_ids_len) {
380 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400381 /* Only clients send a curve list, so this function is only called
382 * on the server. */
383 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
385 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
386 return;
387 }
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 *out_curve_ids = s->tlsext_ellipticcurvelist;
390 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
391 if (!*out_curve_ids) {
392 *out_curve_ids = eccurves_default;
393 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
394 }
395}
David Benjamined439582014-07-14 19:13:02 -0400396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
398 uint8_t curve_type;
399 uint16_t curve_id;
400 const uint16_t *curves;
401 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 /* Only support named curves. */
404 if (!CBS_get_u8(cbs, &curve_type) ||
405 curve_type != NAMED_CURVE_TYPE ||
406 !CBS_get_u16(cbs, &curve_id)) {
407 return 0;
408 }
David Benjamined439582014-07-14 19:13:02 -0400409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 tls1_get_curvelist(s, 0, &curves, &curves_len);
411 for (i = 0; i < curves_len; i++) {
412 if (curve_id == curves[i]) {
413 *out_curve_id = curve_id;
414 return 1;
415 }
416 }
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419}
David Benjamin072334d2014-07-13 16:24:27 -0400420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400422 const uint16_t *curves, *peer_curves, *pref, *supp;
423 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 /* Can't do anything on client side */
426 if (s->server == 0) {
427 return NID_undef;
428 }
429
David Benjamin55a43642015-04-20 14:45:55 -0400430 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
431 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800432
David Benjamin55a43642015-04-20 14:45:55 -0400433 if (peer_curves_len == 0) {
434 /* Clients are not required to send a supported_curves extension. In this
435 * case, the server is free to pick any curve it likes. See RFC 4492,
436 * section 4, paragraph 3. */
437 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
438 }
439
440 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
441 pref = curves;
442 pref_len = curves_len;
443 supp = peer_curves;
444 supp_len = peer_curves_len;
445 } else {
446 pref = peer_curves;
447 pref_len = peer_curves_len;
448 supp = curves;
449 supp_len = curves_len;
450 }
451
452 for (i = 0; i < pref_len; i++) {
453 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 if (pref[i] == supp[j]) {
455 return tls1_ec_curve_id2nid(pref[i]);
456 }
457 }
458 }
459
460 return NID_undef;
461}
Adam Langley95c29f32014-06-20 12:00:00 -0700462
David Benjamin072334d2014-07-13 16:24:27 -0400463int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 const int *curves, size_t ncurves) {
465 uint16_t *curve_ids;
466 size_t i;
467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
469 if (curve_ids == NULL) {
470 return 0;
471 }
472
473 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500474 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 OPENSSL_free(curve_ids);
476 return 0;
477 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 }
479
David Benjamin2755a3e2015-04-22 16:17:58 -0400480 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 *out_curve_ids = curve_ids;
482 *out_curve_ids_len = ncurves;
483
484 return 1;
485}
Adam Langley95c29f32014-06-20 12:00:00 -0700486
David Benjamin072334d2014-07-13 16:24:27 -0400487/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
488 * TLS curve ID and point format, respectively, for |ec|. It returns one on
489 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800490static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
491 uint8_t *out_comp_id, EC_KEY *ec) {
492 int nid;
493 uint16_t id;
494 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 if (ec == NULL) {
497 return 0;
498 }
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 grp = EC_KEY_get0_group(ec);
501 if (grp == NULL) {
502 return 0;
503 }
David Benjamin072334d2014-07-13 16:24:27 -0400504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 /* Determine curve ID */
506 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500507 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 return 0;
509 }
David Benjamin072334d2014-07-13 16:24:27 -0400510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
512 *out_curve_id = id;
513
514 if (out_comp_id) {
515 if (EC_KEY_get0_public_key(ec) == NULL) {
516 return 0;
517 }
518 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
519 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
520 } else {
521 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
522 }
523 }
524
525 return 1;
526}
David Benjamin072334d2014-07-13 16:24:27 -0400527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
529 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400530 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800531static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
532 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400533 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400536 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
537 if (get_peer_curves && !s->server) {
538 /* Servers do not present a preference list so, if we are a client, only
539 * check our list. */
540 continue;
541 }
542
543 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
544 if (get_peer_curves && curves_len == 0) {
545 /* Clients are not required to send a supported_curves extension. In this
546 * case, the server is free to pick any curve it likes. See RFC 4492,
547 * section 4, paragraph 3. */
548 continue;
549 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 for (i = 0; i < curves_len; i++) {
551 if (curves[i] == curve_id) {
552 break;
553 }
554 }
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 if (i == curves_len) {
557 return 0;
558 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 }
David Benjamin033e5f42014-11-13 18:47:41 -0500560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 return 1;
562}
David Benjamin033e5f42014-11-13 18:47:41 -0500563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564int tls1_check_ec_cert(SSL *s, X509 *x) {
565 int ret = 0;
566 EVP_PKEY *pkey = X509_get_pubkey(x);
567 uint16_t curve_id;
568 uint8_t comp_id;
569
570 if (!pkey ||
571 pkey->type != EVP_PKEY_EC ||
572 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
573 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400574 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 goto done;
576 }
577
578 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500579
580done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400581 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 return ret;
583}
David Benjamin42e9a772014-09-02 23:18:44 -0400584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400586 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400587 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400588 * the curve. */
589 uint16_t curve_id;
590 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
591 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 }
593
David Benjamindd978782015-04-24 15:20:13 -0400594 if (s->cert->ecdh_tmp_cb != NULL) {
595 /* Assume the callback will provide an acceptable curve. */
596 return 1;
597 }
598
599 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
600 * there is a shared curve. */
601 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800602}
Adam Langley95c29f32014-06-20 12:00:00 -0700603
604/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langley95c29f32014-06-20 12:00:00 -0700607#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langley95c29f32014-06-20 12:00:00 -0700609#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700612
David Benjamincff64722014-08-19 19:54:46 -0400613static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 tlsext_sigalg(TLSEXT_hash_sha512)
615 tlsext_sigalg(TLSEXT_hash_sha384)
616 tlsext_sigalg(TLSEXT_hash_sha256)
617 tlsext_sigalg(TLSEXT_hash_sha224)
618 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700619};
David Benjamin05da6e12014-07-12 20:42:55 -0400620
Adam Langleyfcf25832014-12-18 17:42:32 -0800621size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
622 /* If server use client authentication sigalgs if not NULL */
623 if (s->server && s->cert->client_sigalgs) {
624 *psigs = s->cert->client_sigalgs;
625 return s->cert->client_sigalgslen;
626 } else if (s->cert->conf_sigalgs) {
627 *psigs = s->cert->conf_sigalgs;
628 return s->cert->conf_sigalgslen;
629 } else {
630 *psigs = tls12_sigalgs;
631 return sizeof(tls12_sigalgs);
632 }
633}
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
636 * checks it is consistent with |s|'s sent supported signature algorithms and,
637 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
638 * returns 0 and writes an alert into |*out_alert|. */
639int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
640 CBS *cbs, EVP_PKEY *pkey) {
641 const uint8_t *sent_sigs;
642 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400643 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 uint8_t hash, signature;
645
646 /* Should never happen */
647 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400648 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 *out_alert = SSL_AD_INTERNAL_ERROR;
650 return 0;
651 }
652
653 if (!CBS_get_u8(cbs, &hash) ||
654 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400655 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 *out_alert = SSL_AD_DECODE_ERROR;
657 return 0;
658 }
659
660 /* Check key type is consistent with signature */
661 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400662 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
664 return 0;
665 }
666
667 if (pkey->type == EVP_PKEY_EC) {
668 uint16_t curve_id;
669 uint8_t comp_id;
670 /* Check compression and curve matches extensions */
671 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
672 *out_alert = SSL_AD_INTERNAL_ERROR;
673 return 0;
674 }
675
676 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400677 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400678 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
680 return 0;
681 }
682 }
683
684 /* Check signature matches a type we sent */
685 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
686 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
687 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
688 break;
689 }
690 }
691
692 /* Allow fallback to SHA-1. */
693 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400694 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
696 return 0;
697 }
698
699 *out_md = tls12_get_hash(hash);
700 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400701 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
703 return 0;
704 }
705
706 return 1;
707}
708
709/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
710 * supported or doesn't appear in supported signature algorithms. Unlike
711 * ssl_cipher_get_disabled this applies to a specific session and not global
712 * settings. */
713void ssl_set_client_disabled(SSL *s) {
714 CERT *c = s->cert;
715 const uint8_t *sigalgs;
716 size_t i, sigalgslen;
717 int have_rsa = 0, have_ecdsa = 0;
718 c->mask_a = 0;
719 c->mask_k = 0;
720
721 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
722 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
723 c->mask_ssl = SSL_TLSV1_2;
724 } else {
725 c->mask_ssl = 0;
726 }
727
728 /* Now go through all signature algorithms seeing if we support any for RSA,
729 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
730 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
731 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
732 switch (sigalgs[1]) {
733 case TLSEXT_signature_rsa:
734 have_rsa = 1;
735 break;
736
737 case TLSEXT_signature_ecdsa:
738 have_ecdsa = 1;
739 break;
740 }
741 }
742
743 /* Disable auth if we don't include any appropriate signature algorithms. */
744 if (!have_rsa) {
745 c->mask_a |= SSL_aRSA;
746 }
747 if (!have_ecdsa) {
748 c->mask_a |= SSL_aECDSA;
749 }
750
751 /* with PSK there must be client callback set */
752 if (!s->psk_client_callback) {
753 c->mask_a |= SSL_aPSK;
754 c->mask_k |= SSL_kPSK;
755 }
756}
Adam Langley95c29f32014-06-20 12:00:00 -0700757
Adam Langley614c66a2015-06-12 15:26:58 -0700758/* tls_extension represents a TLS extension that is handled internally. The
759 * |init| function is called for each handshake, before any other functions of
760 * the extension. Then the add and parse callbacks are called as needed.
761 *
762 * The parse callbacks receive a |CBS| that contains the contents of the
763 * extension (i.e. not including the type and length bytes). If an extension is
764 * not received then the parse callbacks will be called with a NULL CBS so that
765 * they can do any processing needed to handle the absence of an extension.
766 *
767 * The add callbacks receive a |CBB| to which the extension can be appended but
768 * the function is responsible for appending the type and length bytes too.
769 *
770 * All callbacks return one for success and zero for error. If a parse function
771 * returns zero then a fatal alert with value |*out_alert| will be sent. If
772 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
773struct tls_extension {
774 uint16_t value;
775 void (*init)(SSL *ssl);
776
777 int (*add_clienthello)(SSL *ssl, CBB *out);
778 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
779
780 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
781 int (*add_serverhello)(SSL *ssl, CBB *out);
782};
783
784
785/* Server name indication (SNI).
786 *
787 * https://tools.ietf.org/html/rfc6066#section-3. */
788
789static void ext_sni_init(SSL *ssl) {
790 ssl->s3->tmp.should_ack_sni = 0;
791}
792
793static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
794 if (ssl->tlsext_hostname == NULL) {
795 return 1;
796 }
797
798 CBB contents, server_name_list, name;
799 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
800 !CBB_add_u16_length_prefixed(out, &contents) ||
801 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
802 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
803 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
804 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
805 strlen(ssl->tlsext_hostname)) ||
806 !CBB_flush(out)) {
807 return 0;
808 }
809
810 return 1;
811}
812
813static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
814 if (contents == NULL) {
815 return 1;
816 }
817
818 if (CBS_len(contents) != 0) {
819 return 0;
820 }
821
822 assert(ssl->tlsext_hostname != NULL);
823
824 if (!ssl->hit) {
825 assert(ssl->session->tlsext_hostname == NULL);
826 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
827 if (!ssl->session->tlsext_hostname) {
828 *out_alert = SSL_AD_INTERNAL_ERROR;
829 return 0;
830 }
831 }
832
833 return 1;
834}
835
836static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
837 if (contents == NULL) {
838 return 1;
839 }
840
841 /* The servername extension is treated as follows:
842 *
843 * - Only the hostname type is supported with a maximum length of 255.
844 * - The servername is rejected if too long or if it contains zeros, in
845 * which case an fatal alert is generated.
846 * - The servername field is maintained together with the session cache.
847 * - When a session is resumed, the servername callback is invoked in order
848 * to allow the application to position itself to the right context.
849 * - The servername is acknowledged if it is new for a session or when
850 * it is identical to a previously used for the same session.
851 * Applications can control the behaviour. They can at any time
852 * set a 'desirable' servername for a new SSL object. This can be the
853 * case for example with HTTPS when a Host: header field is received and
854 * a renegotiation is requested. In this case, a possible servername
855 * presented in the new client hello is only acknowledged if it matches
856 * the value of the Host: field.
857 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
858 * if they provide for changing an explicit servername context for the
859 * session,
860 * i.e. when the session has been established with a servername extension.
861 */
862
863 CBS server_name_list;
864 char have_seen_host_name = 0;
865
866 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
867 CBS_len(&server_name_list) == 0 ||
868 CBS_len(contents) != 0) {
869 return 0;
870 }
871
872 /* Decode each ServerName in the extension. */
873 while (CBS_len(&server_name_list) > 0) {
874 uint8_t name_type;
875 CBS host_name;
876
877 if (!CBS_get_u8(&server_name_list, &name_type) ||
878 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
879 return 0;
880 }
881
882 /* Only host_name is supported. */
883 if (name_type != TLSEXT_NAMETYPE_host_name) {
884 continue;
885 }
886
887 if (have_seen_host_name) {
888 /* The ServerNameList MUST NOT contain more than one name of the same
889 * name_type. */
890 return 0;
891 }
892
893 have_seen_host_name = 1;
894
895 if (CBS_len(&host_name) == 0 ||
896 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
897 CBS_contains_zero_byte(&host_name)) {
898 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
899 return 0;
900 }
901
902 if (!ssl->hit) {
903 assert(ssl->session->tlsext_hostname == NULL);
904 if (ssl->session->tlsext_hostname) {
905 /* This should be impossible. */
906 return 0;
907 }
908
909 /* Copy the hostname as a string. */
910 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
911 *out_alert = SSL_AD_INTERNAL_ERROR;
912 return 0;
913 }
914
915 ssl->s3->tmp.should_ack_sni = 1;
916 }
917 }
918
919 return 1;
920}
921
922static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
923 if (ssl->hit ||
924 !ssl->s3->tmp.should_ack_sni ||
925 ssl->session->tlsext_hostname == NULL) {
926 return 1;
927 }
928
929 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
930 !CBB_add_u16(out, 0 /* length */)) {
931 return 0;
932 }
933
934 return 1;
935}
936
937
Adam Langley5021b222015-06-12 18:27:58 -0700938/* Renegotiation indication.
939 *
940 * https://tools.ietf.org/html/rfc5746 */
941
942static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
943 CBB contents, prev_finished;
944 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
945 !CBB_add_u16_length_prefixed(out, &contents) ||
946 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
947 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
948 ssl->s3->previous_client_finished_len) ||
949 !CBB_flush(out)) {
950 return 0;
951 }
952
953 return 1;
954}
955
956static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
957 CBS *contents) {
958 if (contents == NULL) {
959 /* No renegotiation extension received.
960 *
961 * Strictly speaking if we want to avoid an attack we should *always* see
962 * RI even on initial ServerHello because the client doesn't see any
963 * renegotiation during an attack. However this would mean we could not
964 * connect to any server which doesn't support RI.
965 *
966 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
967 * defined. */
968 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
969 return 1;
970 }
971
972 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400973 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700974 return 0;
975 }
976
977 const size_t expected_len = ssl->s3->previous_client_finished_len +
978 ssl->s3->previous_server_finished_len;
979
980 /* Check for logic errors */
981 assert(!expected_len || ssl->s3->previous_client_finished_len);
982 assert(!expected_len || ssl->s3->previous_server_finished_len);
983
984 /* Parse out the extension contents. */
985 CBS renegotiated_connection;
986 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
987 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400988 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700989 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
990 return 0;
991 }
992
993 /* Check that the extension matches. */
994 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400995 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700996 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
997 return 0;
998 }
999
1000 const uint8_t *d = CBS_data(&renegotiated_connection);
1001 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1002 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001004 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1005 return 0;
1006 }
1007 d += ssl->s3->previous_client_finished_len;
1008
1009 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1010 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001012 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1013 return 0;
1014 }
1015 ssl->s3->send_connection_binding = 1;
1016
1017 return 1;
1018}
1019
1020static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1021 CBS *contents) {
1022 /* Renegotiation isn't supported as a server so this function should never be
1023 * called after the initial handshake. */
1024 assert(!ssl->s3->initial_handshake_complete);
1025
1026 CBS fake_contents;
1027 static const uint8_t kFakeExtension[] = {0};
1028
1029 if (contents == NULL) {
1030 if (ssl->s3->send_connection_binding) {
1031 /* The renegotiation SCSV was received so pretend that we received a
1032 * renegotiation extension. */
1033 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1034 contents = &fake_contents;
1035 /* We require that the renegotiation extension is at index zero of
1036 * kExtensions. */
1037 ssl->s3->tmp.extensions.received |= (1u << 0);
1038 } else {
1039 return 1;
1040 }
1041 }
1042
1043 CBS renegotiated_connection;
1044
1045 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1046 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001047 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001048 return 0;
1049 }
1050
1051 /* Check that the extension matches */
1052 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1053 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001054 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001055 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1056 return 0;
1057 }
1058
1059 ssl->s3->send_connection_binding = 1;
1060
1061 return 1;
1062}
1063
1064static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1065 CBB contents, prev_finished;
1066 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1067 !CBB_add_u16_length_prefixed(out, &contents) ||
1068 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1069 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1070 ssl->s3->previous_client_finished_len) ||
1071 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1072 ssl->s3->previous_server_finished_len) ||
1073 !CBB_flush(out)) {
1074 return 0;
1075 }
1076
1077 return 1;
1078}
1079
Adam Langley0a056712015-07-01 15:03:33 -07001080
1081/* Extended Master Secret.
1082 *
1083 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1084
1085static void ext_ems_init(SSL *ssl) {
1086 ssl->s3->tmp.extended_master_secret = 0;
1087}
1088
1089static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1090 if (ssl->version == SSL3_VERSION) {
1091 return 1;
1092 }
1093
1094 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1095 !CBB_add_u16(out, 0 /* length */)) {
1096 return 0;
1097 }
1098
1099 return 1;
1100}
1101
1102static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1103 CBS *contents) {
1104 if (contents == NULL) {
1105 return 1;
1106 }
1107
1108 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1109 return 0;
1110 }
1111
1112 ssl->s3->tmp.extended_master_secret = 1;
1113 return 1;
1114}
1115
1116static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1117 if (ssl->version == SSL3_VERSION || contents == NULL) {
1118 return 1;
1119 }
1120
1121 if (CBS_len(contents) != 0) {
1122 return 0;
1123 }
1124
1125 ssl->s3->tmp.extended_master_secret = 1;
1126 return 1;
1127}
1128
1129static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1130 if (!ssl->s3->tmp.extended_master_secret) {
1131 return 1;
1132 }
1133
1134 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1135 !CBB_add_u16(out, 0 /* length */)) {
1136 return 0;
1137 }
1138
1139 return 1;
1140}
1141
Adam Langley9b05bc52015-07-01 15:25:33 -07001142
1143/* Session tickets.
1144 *
1145 * https://tools.ietf.org/html/rfc5077 */
1146
1147static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1148 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1149 return 1;
1150 }
1151
1152 const uint8_t *ticket_data = NULL;
1153 int ticket_len = 0;
1154
1155 /* Renegotiation does not participate in session resumption. However, still
1156 * advertise the extension to avoid potentially breaking servers which carry
1157 * over the state from the previous handshake, such as OpenSSL servers
1158 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1159 if (!ssl->s3->initial_handshake_complete &&
1160 ssl->session != NULL &&
1161 ssl->session->tlsext_tick != NULL) {
1162 ticket_data = ssl->session->tlsext_tick;
1163 ticket_len = ssl->session->tlsext_ticklen;
1164 }
1165
1166 CBB ticket;
1167 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1168 !CBB_add_u16_length_prefixed(out, &ticket) ||
1169 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1170 !CBB_flush(out)) {
1171 return 0;
1172 }
1173
1174 return 1;
1175}
1176
1177static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1178 CBS *contents) {
1179 ssl->tlsext_ticket_expected = 0;
1180
1181 if (contents == NULL) {
1182 return 1;
1183 }
1184
1185 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1186 * this function should never be called, even if the server tries to send the
1187 * extension. */
1188 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1189
1190 if (CBS_len(contents) != 0) {
1191 return 0;
1192 }
1193
1194 ssl->tlsext_ticket_expected = 1;
1195 return 1;
1196}
1197
1198static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1199 /* This function isn't used because the ticket extension from the client is
1200 * handled in ssl_sess.c. */
1201 return 1;
1202}
1203
1204static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1205 if (!ssl->tlsext_ticket_expected) {
1206 return 1;
1207 }
1208
1209 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1210 * true. */
1211 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1212
1213 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1214 !CBB_add_u16(out, 0 /* length */)) {
1215 return 0;
1216 }
1217
1218 return 1;
1219}
1220
1221
Adam Langley2e857bd2015-07-01 16:09:19 -07001222/* Signature Algorithms.
1223 *
1224 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1225
1226static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1227 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1228 return 1;
1229 }
1230
1231 const uint8_t *sigalgs_data;
1232 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1233
1234 CBB contents, sigalgs;
1235 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1236 !CBB_add_u16_length_prefixed(out, &contents) ||
1237 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1238 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1239 !CBB_flush(out)) {
1240 return 0;
1241 }
1242
1243 return 1;
1244}
1245
1246static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1247 CBS *contents) {
1248 if (contents != NULL) {
1249 /* Servers MUST NOT send this extension. */
1250 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001251 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001252 return 0;
1253 }
1254
1255 return 1;
1256}
1257
1258static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1259 CBS *contents) {
1260 OPENSSL_free(ssl->cert->peer_sigalgs);
1261 ssl->cert->peer_sigalgs = NULL;
1262 ssl->cert->peer_sigalgslen = 0;
1263
1264 OPENSSL_free(ssl->cert->shared_sigalgs);
1265 ssl->cert->shared_sigalgs = NULL;
1266 ssl->cert->shared_sigalgslen = 0;
1267
1268 if (contents == NULL) {
1269 return 1;
1270 }
1271
1272 CBS supported_signature_algorithms;
1273 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1274 CBS_len(contents) != 0) {
1275 return 0;
1276 }
1277
1278 /* Ensure the signature algorithms are non-empty. It contains a list of
1279 * SignatureAndHashAlgorithms which are two bytes each. */
1280 if (CBS_len(&supported_signature_algorithms) == 0 ||
1281 (CBS_len(&supported_signature_algorithms) % 2) != 0 ||
1282 !tls1_process_sigalgs(ssl, &supported_signature_algorithms)) {
1283 return 0;
1284 }
1285
1286 /* It's a fatal error if the signature_algorithms extension is received and
1287 * there are no shared algorithms. */
1288 if (ssl->cert->peer_sigalgs && !ssl->cert->shared_sigalgs) {
David Benjamin3570d732015-06-29 00:28:17 -04001289 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
Adam Langley2e857bd2015-07-01 16:09:19 -07001290 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1291 return 0;
1292 }
1293
1294 return 1;
1295}
1296
1297static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1298 /* Servers MUST NOT send this extension. */
1299 return 1;
1300}
1301
1302
Adam Langleybb0bd042015-07-01 16:21:03 -07001303/* OCSP Stapling.
1304 *
1305 * https://tools.ietf.org/html/rfc6066#section-8 */
1306
1307static void ext_ocsp_init(SSL *ssl) {
1308 ssl->s3->tmp.certificate_status_expected = 0;
1309}
1310
1311static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1312 if (!ssl->ocsp_stapling_enabled) {
1313 return 1;
1314 }
1315
1316 CBB contents;
1317 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1318 !CBB_add_u16_length_prefixed(out, &contents) ||
1319 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1320 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1321 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1322 !CBB_flush(out)) {
1323 return 0;
1324 }
1325
1326 return 1;
1327}
1328
1329static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1330 CBS *contents) {
1331 if (contents == NULL) {
1332 return 1;
1333 }
1334
1335 if (CBS_len(contents) != 0) {
1336 return 0;
1337 }
1338
1339 ssl->s3->tmp.certificate_status_expected = 1;
1340 return 1;
1341}
1342
1343static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1344 CBS *contents) {
1345 /* OCSP stapling as a server is not supported. */
1346 return 1;
1347}
1348
1349static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
1350 /* OCSP stapling as a server is not supported. */
1351 return 1;
1352}
1353
1354
Adam Langley97dfcbf2015-07-01 18:35:20 -07001355/* Next protocol negotiation.
1356 *
1357 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1358
1359static void ext_npn_init(SSL *ssl) {
1360 ssl->s3->next_proto_neg_seen = 0;
1361}
1362
1363static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1364 if (ssl->s3->initial_handshake_complete ||
1365 ssl->ctx->next_proto_select_cb == NULL ||
1366 SSL_IS_DTLS(ssl)) {
1367 return 1;
1368 }
1369
1370 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1371 !CBB_add_u16(out, 0 /* length */)) {
1372 return 0;
1373 }
1374
1375 return 1;
1376}
1377
1378static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1379 CBS *contents) {
1380 if (contents == NULL) {
1381 return 1;
1382 }
1383
1384 /* If any of these are false then we should never have sent the NPN
1385 * extension in the ClientHello and thus this function should never have been
1386 * called. */
1387 assert(!ssl->s3->initial_handshake_complete);
1388 assert(!SSL_IS_DTLS(ssl));
1389 assert(ssl->ctx->next_proto_select_cb != NULL);
1390
1391 const uint8_t *const orig_contents = CBS_data(contents);
1392 const size_t orig_len = CBS_len(contents);
1393
1394 while (CBS_len(contents) != 0) {
1395 CBS proto;
1396 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1397 CBS_len(&proto) == 0) {
1398 return 0;
1399 }
1400 }
1401
1402 uint8_t *selected;
1403 uint8_t selected_len;
1404 if (ssl->ctx->next_proto_select_cb(
1405 ssl, &selected, &selected_len, orig_contents, orig_len,
1406 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1407 *out_alert = SSL_AD_INTERNAL_ERROR;
1408 return 0;
1409 }
1410
1411 OPENSSL_free(ssl->next_proto_negotiated);
1412 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1413 if (ssl->next_proto_negotiated == NULL) {
1414 *out_alert = SSL_AD_INTERNAL_ERROR;
1415 return 0;
1416 }
1417
1418 ssl->next_proto_negotiated_len = selected_len;
1419 ssl->s3->next_proto_neg_seen = 1;
1420
1421 return 1;
1422}
1423
1424static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1425 CBS *contents) {
1426 if (contents != NULL && CBS_len(contents) != 0) {
1427 return 0;
1428 }
1429
1430 if (contents == NULL ||
1431 ssl->s3->initial_handshake_complete ||
1432 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1433 * afterwards, parsing the ALPN extension will clear
1434 * |next_proto_neg_seen|. */
1435 ssl->s3->alpn_selected != NULL ||
1436 ssl->ctx->next_protos_advertised_cb == NULL ||
1437 SSL_IS_DTLS(ssl)) {
1438 return 1;
1439 }
1440
1441 ssl->s3->next_proto_neg_seen = 1;
1442 return 1;
1443}
1444
1445static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1446 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1447 * parsed. */
1448 if (!ssl->s3->next_proto_neg_seen) {
1449 return 1;
1450 }
1451
1452 const uint8_t *npa;
1453 unsigned npa_len;
1454
1455 if (ssl->ctx->next_protos_advertised_cb(
1456 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1457 SSL_TLSEXT_ERR_OK) {
1458 ssl->s3->next_proto_neg_seen = 0;
1459 return 1;
1460 }
1461
1462 CBB contents;
1463 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1464 !CBB_add_u16_length_prefixed(out, &contents) ||
1465 !CBB_add_bytes(&contents, npa, npa_len) ||
1466 !CBB_flush(out)) {
1467 return 0;
1468 }
1469
1470 return 1;
1471}
1472
1473
Adam Langleyab8d87d2015-07-10 12:21:39 -07001474/* Signed certificate timestamps.
1475 *
1476 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1477
1478static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1479 if (!ssl->signed_cert_timestamps_enabled) {
1480 return 1;
1481 }
1482
1483 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1484 !CBB_add_u16(out, 0 /* length */)) {
1485 return 0;
1486 }
1487
1488 return 1;
1489}
1490
1491static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1492 CBS *contents) {
1493 if (contents == NULL) {
1494 return 1;
1495 }
1496
1497 /* If this is false then we should never have sent the SCT extension in the
1498 * ClientHello and thus this function should never have been called. */
1499 assert(ssl->signed_cert_timestamps_enabled);
1500
1501 if (CBS_len(contents) == 0) {
1502 *out_alert = SSL_AD_DECODE_ERROR;
1503 return 0;
1504 }
1505
1506 /* Session resumption uses the original session information. */
1507 if (!ssl->hit &&
1508 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1509 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1510 *out_alert = SSL_AD_INTERNAL_ERROR;
1511 return 0;
1512 }
1513
1514 return 1;
1515}
1516
1517static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1518 CBS *contents) {
1519 /* The SCT extension is not supported as a server. */
1520 return 1;
1521}
1522
1523static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1524 /* The SCT extension is not supported as a server. */
1525 return 1;
1526}
1527
1528
Adam Langleyf18e4532015-07-10 13:39:53 -07001529/* Application-level Protocol Negotiation.
1530 *
1531 * https://tools.ietf.org/html/rfc7301 */
1532
1533static void ext_alpn_init(SSL *ssl) {
1534 OPENSSL_free(ssl->s3->alpn_selected);
1535 ssl->s3->alpn_selected = NULL;
1536}
1537
1538static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1539 if (ssl->alpn_client_proto_list == NULL ||
1540 ssl->s3->initial_handshake_complete) {
1541 return 1;
1542 }
1543
1544 CBB contents, proto_list;
1545 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1546 !CBB_add_u16_length_prefixed(out, &contents) ||
1547 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1548 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1549 ssl->alpn_client_proto_list_len) ||
1550 !CBB_flush(out)) {
1551 return 0;
1552 }
1553
1554 return 1;
1555}
1556
1557static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1558 CBS *contents) {
1559 if (contents == NULL) {
1560 return 1;
1561 }
1562
1563 assert(!ssl->s3->initial_handshake_complete);
1564 assert(ssl->alpn_client_proto_list != NULL);
1565
1566 /* The extension data consists of a ProtocolNameList which must have
1567 * exactly one ProtocolName. Each of these is length-prefixed. */
1568 CBS protocol_name_list, protocol_name;
1569 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1570 CBS_len(contents) != 0 ||
1571 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1572 /* Empty protocol names are forbidden. */
1573 CBS_len(&protocol_name) == 0 ||
1574 CBS_len(&protocol_name_list) != 0) {
1575 return 0;
1576 }
1577
1578 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1579 &ssl->s3->alpn_selected_len)) {
1580 *out_alert = SSL_AD_INTERNAL_ERROR;
1581 return 0;
1582 }
1583
1584 return 1;
1585}
1586
1587static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1588 CBS *contents) {
1589 if (contents == NULL) {
1590 return 1;
1591 }
1592
1593 if (ssl->ctx->alpn_select_cb == NULL ||
1594 ssl->s3->initial_handshake_complete) {
1595 return 1;
1596 }
1597
1598 /* ALPN takes precedence over NPN. */
1599 ssl->s3->next_proto_neg_seen = 0;
1600
1601 CBS protocol_name_list;
1602 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1603 CBS_len(contents) != 0 ||
1604 CBS_len(&protocol_name_list) < 2) {
1605 return 0;
1606 }
1607
1608 /* Validate the protocol list. */
1609 CBS protocol_name_list_copy = protocol_name_list;
1610 while (CBS_len(&protocol_name_list_copy) > 0) {
1611 CBS protocol_name;
1612
1613 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1614 /* Empty protocol names are forbidden. */
1615 CBS_len(&protocol_name) == 0) {
1616 return 0;
1617 }
1618 }
1619
1620 const uint8_t *selected;
1621 uint8_t selected_len;
1622 if (ssl->ctx->alpn_select_cb(
1623 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1624 CBS_len(&protocol_name_list),
1625 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1626 OPENSSL_free(ssl->s3->alpn_selected);
1627 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1628 if (ssl->s3->alpn_selected == NULL) {
1629 *out_alert = SSL_AD_INTERNAL_ERROR;
1630 return 0;
1631 }
1632 ssl->s3->alpn_selected_len = selected_len;
1633 }
1634
1635 return 1;
1636}
1637
1638static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1639 if (ssl->s3->alpn_selected == NULL) {
1640 return 1;
1641 }
1642
1643 CBB contents, proto_list, proto;
1644 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1645 !CBB_add_u16_length_prefixed(out, &contents) ||
1646 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1647 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1648 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1649 !CBB_flush(out)) {
1650 return 0;
1651 }
1652
1653 return 1;
1654}
1655
1656
Adam Langley49c7af12015-07-10 14:33:46 -07001657/* Channel ID.
1658 *
1659 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1660
1661static void ext_channel_id_init(SSL *ssl) {
1662 ssl->s3->tlsext_channel_id_valid = 0;
1663}
1664
1665static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1666 if (!ssl->tlsext_channel_id_enabled ||
1667 SSL_IS_DTLS(ssl)) {
1668 return 1;
1669 }
1670
1671 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1672 !CBB_add_u16(out, 0 /* length */)) {
1673 return 0;
1674 }
1675
1676 return 1;
1677}
1678
1679static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1680 CBS *contents) {
1681 if (contents == NULL) {
1682 return 1;
1683 }
1684
1685 assert(!SSL_IS_DTLS(ssl));
1686 assert(ssl->tlsext_channel_id_enabled);
1687
1688 if (CBS_len(contents) != 0) {
1689 return 0;
1690 }
1691
1692 ssl->s3->tlsext_channel_id_valid = 1;
1693 return 1;
1694}
1695
1696static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1697 CBS *contents) {
1698 if (contents == NULL ||
1699 !ssl->tlsext_channel_id_enabled ||
1700 SSL_IS_DTLS(ssl)) {
1701 return 1;
1702 }
1703
1704 if (CBS_len(contents) != 0) {
1705 return 0;
1706 }
1707
1708 ssl->s3->tlsext_channel_id_valid = 1;
1709 return 1;
1710}
1711
1712static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1713 if (!ssl->s3->tlsext_channel_id_valid) {
1714 return 1;
1715 }
1716
1717 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1718 !CBB_add_u16(out, 0 /* length */)) {
1719 return 0;
1720 }
1721
1722 return 1;
1723}
1724
Adam Langley391250d2015-07-15 19:06:07 -07001725
1726/* Secure Real-time Transport Protocol (SRTP) extension.
1727 *
1728 * https://tools.ietf.org/html/rfc5764 */
1729
Adam Langley391250d2015-07-15 19:06:07 -07001730
1731static void ext_srtp_init(SSL *ssl) {
1732 ssl->srtp_profile = NULL;
1733}
1734
1735static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1736 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1737 if (profiles == NULL) {
1738 return 1;
1739 }
1740 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1741 if (num_profiles == 0) {
1742 return 1;
1743 }
1744
1745 CBB contents, profile_ids;
1746 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1747 !CBB_add_u16_length_prefixed(out, &contents) ||
1748 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1749 return 0;
1750 }
1751
1752 size_t i;
1753 for (i = 0; i < num_profiles; i++) {
1754 if (!CBB_add_u16(&profile_ids,
1755 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1756 return 0;
1757 }
1758 }
1759
1760 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1761 !CBB_flush(out)) {
1762 return 0;
1763 }
1764
1765 return 1;
1766}
1767
1768static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1769 CBS *contents) {
1770 if (contents == NULL) {
1771 return 1;
1772 }
1773
1774 /* The extension consists of a u16-prefixed profile ID list containing a
1775 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1776 *
1777 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1778 CBS profile_ids, srtp_mki;
1779 uint16_t profile_id;
1780 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1781 !CBS_get_u16(&profile_ids, &profile_id) ||
1782 CBS_len(&profile_ids) != 0 ||
1783 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1784 CBS_len(contents) != 0) {
1785 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1786 return 0;
1787 }
1788
1789 if (CBS_len(&srtp_mki) != 0) {
1790 /* Must be no MKI, since we never offer one. */
1791 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1792 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1793 return 0;
1794 }
1795
1796 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1797
1798 /* Check to see if the server gave us something we support (and presumably
1799 * offered). */
1800 size_t i;
1801 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1802 const SRTP_PROTECTION_PROFILE *profile =
1803 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1804
1805 if (profile->id == profile_id) {
1806 ssl->srtp_profile = profile;
1807 return 1;
1808 }
1809 }
1810
1811 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1812 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1813 return 0;
1814}
1815
1816static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1817 CBS *contents) {
1818 if (contents == NULL) {
1819 return 1;
1820 }
1821
1822 CBS profile_ids, srtp_mki;
1823 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1824 CBS_len(&profile_ids) < 2 ||
1825 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1826 CBS_len(contents) != 0) {
1827 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1828 return 0;
1829 }
1830 /* Discard the MKI value for now. */
1831
1832 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1833 SSL_get_srtp_profiles(ssl);
1834
1835 /* Pick the server's most preferred profile. */
1836 size_t i;
1837 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1838 const SRTP_PROTECTION_PROFILE *server_profile =
1839 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1840
1841 CBS profile_ids_tmp;
1842 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1843
1844 while (CBS_len(&profile_ids_tmp) > 0) {
1845 uint16_t profile_id;
1846 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1847 return 0;
1848 }
1849
1850 if (server_profile->id == profile_id) {
1851 ssl->srtp_profile = server_profile;
1852 return 1;
1853 }
1854 }
1855 }
1856
1857 return 1;
1858}
1859
1860static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1861 if (ssl->srtp_profile == NULL) {
1862 return 1;
1863 }
1864
1865 CBB contents, profile_ids;
1866 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1867 !CBB_add_u16_length_prefixed(out, &contents) ||
1868 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1869 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1870 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1871 !CBB_flush(out)) {
1872 return 0;
1873 }
1874
1875 return 1;
1876}
1877
Adam Langleybdd5d662015-07-20 16:19:08 -07001878
1879/* EC point formats.
1880 *
1881 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1882
1883static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1884 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1885 return 0;
1886 }
1887
1888 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1889
1890 size_t i;
1891 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1892 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1893
1894 const uint32_t alg_k = cipher->algorithm_mkey;
1895 const uint32_t alg_a = cipher->algorithm_auth;
1896 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1897 return 1;
1898 }
1899 }
1900
1901 return 0;
1902}
1903
Adam Langleybdd5d662015-07-20 16:19:08 -07001904static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001905 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001906 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1907 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001908 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1909 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001910 !CBB_flush(out)) {
1911 return 0;
1912 }
1913
1914 return 1;
1915}
1916
1917static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1918 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1919 return 1;
1920 }
1921
1922 return ext_ec_point_add_extension(ssl, out);
1923}
1924
1925static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1926 CBS *contents) {
1927 if (contents == NULL) {
1928 return 1;
1929 }
1930
1931 CBS ec_point_format_list;
1932 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1933 CBS_len(contents) != 0) {
1934 return 0;
1935 }
1936
David Benjaminfc059942015-07-30 23:01:59 -04001937 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1938 * point format. */
1939 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1940 CBS_len(&ec_point_format_list)) == NULL) {
1941 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001942 return 0;
1943 }
1944
1945 return 1;
1946}
1947
1948static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1949 CBS *contents) {
1950 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1951}
1952
1953static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1954 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1955 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001956 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001957
1958 if (!using_ecc) {
1959 return 1;
1960 }
1961
1962 return ext_ec_point_add_extension(ssl, out);
1963}
1964
Adam Langley273d49c2015-07-20 16:38:52 -07001965
1966/* EC supported curves.
1967 *
1968 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1969
1970static void ext_ec_curves_init(SSL *ssl) {
1971 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1972 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1973 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1974}
1975
1976static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1977 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1978 return 1;
1979 }
1980
1981 CBB contents, curves_bytes;
1982 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1983 !CBB_add_u16_length_prefixed(out, &contents) ||
1984 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1985 return 0;
1986 }
1987
1988 const uint16_t *curves;
1989 size_t curves_len;
1990 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1991
1992 size_t i;
1993 for (i = 0; i < curves_len; i++) {
1994 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1995 return 0;
1996 }
1997 }
1998
1999 return CBB_flush(out);
2000}
2001
2002static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2003 CBS *contents) {
2004 /* This extension is not expected to be echoed by servers and is ignored. */
2005 return 1;
2006}
2007
2008static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2009 CBS *contents) {
2010 if (contents == NULL) {
2011 return 1;
2012 }
2013
2014 CBS elliptic_curve_list;
2015 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2016 CBS_len(&elliptic_curve_list) == 0 ||
2017 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2018 CBS_len(contents) != 0) {
2019 return 0;
2020 }
2021
2022 ssl->s3->tmp.peer_ellipticcurvelist =
2023 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2024
2025 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2026 *out_alert = SSL_AD_INTERNAL_ERROR;
2027 return 0;
2028 }
2029
2030 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2031 size_t i;
2032 for (i = 0; i < num_curves; i++) {
2033 if (!CBS_get_u16(&elliptic_curve_list,
2034 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2035 goto err;
2036 }
2037 }
2038
2039 assert(CBS_len(&elliptic_curve_list) == 0);
2040 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2041
2042 return 1;
2043
2044err:
2045 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2046 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2047 *out_alert = SSL_AD_INTERNAL_ERROR;
2048 return 0;
2049}
2050
2051static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2052 /* Servers don't echo this extension. */
2053 return 1;
2054}
2055
2056
Adam Langley614c66a2015-06-12 15:26:58 -07002057/* kExtensions contains all the supported extensions. */
2058static const struct tls_extension kExtensions[] = {
2059 {
Adam Langley5021b222015-06-12 18:27:58 -07002060 /* The renegotiation extension must always be at index zero because the
2061 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2062 * sent as an SCSV. */
2063 TLSEXT_TYPE_renegotiate,
2064 NULL,
2065 ext_ri_add_clienthello,
2066 ext_ri_parse_serverhello,
2067 ext_ri_parse_clienthello,
2068 ext_ri_add_serverhello,
2069 },
2070 {
Adam Langley614c66a2015-06-12 15:26:58 -07002071 TLSEXT_TYPE_server_name,
2072 ext_sni_init,
2073 ext_sni_add_clienthello,
2074 ext_sni_parse_serverhello,
2075 ext_sni_parse_clienthello,
2076 ext_sni_add_serverhello,
2077 },
Adam Langley0a056712015-07-01 15:03:33 -07002078 {
2079 TLSEXT_TYPE_extended_master_secret,
2080 ext_ems_init,
2081 ext_ems_add_clienthello,
2082 ext_ems_parse_serverhello,
2083 ext_ems_parse_clienthello,
2084 ext_ems_add_serverhello,
2085 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002086 {
2087 TLSEXT_TYPE_session_ticket,
2088 NULL,
2089 ext_ticket_add_clienthello,
2090 ext_ticket_parse_serverhello,
2091 ext_ticket_parse_clienthello,
2092 ext_ticket_add_serverhello,
2093 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002094 {
2095 TLSEXT_TYPE_signature_algorithms,
2096 NULL,
2097 ext_sigalgs_add_clienthello,
2098 ext_sigalgs_parse_serverhello,
2099 ext_sigalgs_parse_clienthello,
2100 ext_sigalgs_add_serverhello,
2101 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002102 {
2103 TLSEXT_TYPE_status_request,
2104 ext_ocsp_init,
2105 ext_ocsp_add_clienthello,
2106 ext_ocsp_parse_serverhello,
2107 ext_ocsp_parse_clienthello,
2108 ext_ocsp_add_serverhello,
2109 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002110 {
2111 TLSEXT_TYPE_next_proto_neg,
2112 ext_npn_init,
2113 ext_npn_add_clienthello,
2114 ext_npn_parse_serverhello,
2115 ext_npn_parse_clienthello,
2116 ext_npn_add_serverhello,
2117 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002118 {
2119 TLSEXT_TYPE_certificate_timestamp,
2120 NULL,
2121 ext_sct_add_clienthello,
2122 ext_sct_parse_serverhello,
2123 ext_sct_parse_clienthello,
2124 ext_sct_add_serverhello,
2125 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002126 {
2127 TLSEXT_TYPE_application_layer_protocol_negotiation,
2128 ext_alpn_init,
2129 ext_alpn_add_clienthello,
2130 ext_alpn_parse_serverhello,
2131 ext_alpn_parse_clienthello,
2132 ext_alpn_add_serverhello,
2133 },
Adam Langley49c7af12015-07-10 14:33:46 -07002134 {
2135 TLSEXT_TYPE_channel_id,
2136 ext_channel_id_init,
2137 ext_channel_id_add_clienthello,
2138 ext_channel_id_parse_serverhello,
2139 ext_channel_id_parse_clienthello,
2140 ext_channel_id_add_serverhello,
2141 },
Adam Langley391250d2015-07-15 19:06:07 -07002142 {
2143 TLSEXT_TYPE_srtp,
2144 ext_srtp_init,
2145 ext_srtp_add_clienthello,
2146 ext_srtp_parse_serverhello,
2147 ext_srtp_parse_clienthello,
2148 ext_srtp_add_serverhello,
2149 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002150 {
2151 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002152 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002153 ext_ec_point_add_clienthello,
2154 ext_ec_point_parse_serverhello,
2155 ext_ec_point_parse_clienthello,
2156 ext_ec_point_add_serverhello,
2157 },
Adam Langley273d49c2015-07-20 16:38:52 -07002158 {
2159 TLSEXT_TYPE_elliptic_curves,
2160 ext_ec_curves_init,
2161 ext_ec_curves_add_clienthello,
2162 ext_ec_curves_parse_serverhello,
2163 ext_ec_curves_parse_clienthello,
2164 ext_ec_curves_add_serverhello,
2165 },
Adam Langley614c66a2015-06-12 15:26:58 -07002166};
2167
2168#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2169
Adam Langley4cfa96b2015-07-01 11:56:55 -07002170OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2171 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002172 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002173OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2174 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2175 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002176 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002177
Adam Langley614c66a2015-06-12 15:26:58 -07002178static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2179 uint16_t value) {
2180 unsigned i;
2181 for (i = 0; i < kNumExtensions; i++) {
2182 if (kExtensions[i].value == value) {
2183 *out_index = i;
2184 return &kExtensions[i];
2185 }
2186 }
2187
2188 return NULL;
2189}
2190
Adam Langley09505632015-07-30 18:10:13 -07002191int SSL_extension_supported(unsigned extension_value) {
2192 uint32_t index;
2193 return extension_value == TLSEXT_TYPE_padding ||
2194 tls_extension_find(&index, extension_value) != NULL;
2195}
2196
Adam Langleyb0c235e2014-06-20 12:00:00 -07002197/* header_len is the length of the ClientHello header written so far, used to
2198 * compute padding. It does not include the record header. Pass 0 if no padding
2199 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002200uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2201 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002202 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2203 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002204 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 }
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langley33ad2b52015-07-20 17:43:53 -07002207 CBB cbb, extensions;
2208 CBB_zero(&cbb);
2209 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2210 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2211 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002212 }
Adam Langley95c29f32014-06-20 12:00:00 -07002213
Adam Langley614c66a2015-06-12 15:26:58 -07002214 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002215 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002216
Adam Langley614c66a2015-06-12 15:26:58 -07002217 size_t i;
2218 for (i = 0; i < kNumExtensions; i++) {
2219 if (kExtensions[i].init != NULL) {
2220 kExtensions[i].init(s);
2221 }
2222 }
Adam Langley95c29f32014-06-20 12:00:00 -07002223
Adam Langley614c66a2015-06-12 15:26:58 -07002224 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002225 const size_t len_before = CBB_len(&extensions);
2226 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2227 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2228 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2229 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 }
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langley33ad2b52015-07-20 17:43:53 -07002232 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002233 s->s3->tmp.extensions.sent |= (1u << i);
2234 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002235 }
Adam Langley75712922014-10-10 16:23:43 -07002236
Adam Langley09505632015-07-30 18:10:13 -07002237 if (!custom_ext_add_clienthello(s, &extensions)) {
2238 goto err;
2239 }
2240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002242 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 if (header_len > 0xff && header_len < 0x200) {
2244 /* Add padding to workaround bugs in F5 terminators. See
2245 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2246 *
2247 * NB: because this code works out the length of all existing extensions
2248 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002249 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 /* Extensions take at least four bytes to encode. Always include least
2251 * one byte of data if including the extension. WebSphere Application
2252 * Server 7.0 is intolerant to the last extension being zero-length. */
2253 if (padding_len >= 4 + 1) {
2254 padding_len -= 4;
2255 } else {
2256 padding_len = 1;
2257 }
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langley33ad2b52015-07-20 17:43:53 -07002259 uint8_t *padding_bytes;
2260 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2261 !CBB_add_u16(&extensions, padding_len) ||
2262 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2263 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002264 }
Adam Langley75712922014-10-10 16:23:43 -07002265
Adam Langley33ad2b52015-07-20 17:43:53 -07002266 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 }
2268 }
Adam Langley75712922014-10-10 16:23:43 -07002269
Adam Langley33ad2b52015-07-20 17:43:53 -07002270 if (!CBB_flush(&cbb)) {
2271 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 }
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langley33ad2b52015-07-20 17:43:53 -07002274 uint8_t *ret = buf;
2275 const size_t cbb_len = CBB_len(&cbb);
2276 /* If only two bytes have been written then the extensions are actually empty
2277 * and those two bytes are the zero length. In that case, we don't bother
2278 * sending the extensions length. */
2279 if (cbb_len > 2) {
2280 ret += cbb_len;
2281 }
2282
2283 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002285
2286err:
2287 CBB_cleanup(&cbb);
2288 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2289 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002290}
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langley614c66a2015-06-12 15:26:58 -07002292uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2293 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2295 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002296 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 }
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langley33ad2b52015-07-20 17:43:53 -07002299 CBB cbb, extensions;
2300 CBB_zero(&cbb);
2301 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2302 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2303 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002304 }
2305
2306 unsigned i;
2307 for (i = 0; i < kNumExtensions; i++) {
2308 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2309 /* Don't send extensions that were not received. */
2310 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 }
Adam Langley95c29f32014-06-20 12:00:00 -07002312
Adam Langley33ad2b52015-07-20 17:43:53 -07002313 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2314 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2315 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2316 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002317 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 }
Adam Langley95c29f32014-06-20 12:00:00 -07002319
Adam Langley09505632015-07-30 18:10:13 -07002320 if (!custom_ext_add_serverhello(s, &extensions)) {
2321 goto err;
2322 }
2323
Adam Langley33ad2b52015-07-20 17:43:53 -07002324 if (!CBB_flush(&cbb)) {
2325 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 }
2327
Adam Langley33ad2b52015-07-20 17:43:53 -07002328 uint8_t *ret = buf;
2329 const size_t cbb_len = CBB_len(&cbb);
2330 /* If only two bytes have been written then the extensions are actually empty
2331 * and those two bytes are the zero length. In that case, we don't bother
2332 * sending the extensions length. */
2333 if (cbb_len > 2) {
2334 ret += cbb_len;
2335 }
2336
2337 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002339
2340err:
2341 CBB_cleanup(&cbb);
2342 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2343 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002344}
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002347 size_t i;
2348 for (i = 0; i < kNumExtensions; i++) {
2349 if (kExtensions[i].init != NULL) {
2350 kExtensions[i].init(s);
2351 }
2352 }
2353
2354 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002355 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002356 /* The renegotiation extension must always be at index zero because the
2357 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2358 * sent as an SCSV. */
2359 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002362 if (CBS_len(cbs) != 0) {
2363 /* Decode the extensions block and check it is valid. */
2364 CBS extensions;
2365 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2366 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 *out_alert = SSL_AD_DECODE_ERROR;
2368 return 0;
2369 }
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langley33ad2b52015-07-20 17:43:53 -07002371 while (CBS_len(&extensions) != 0) {
2372 uint16_t type;
2373 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002374
Adam Langley33ad2b52015-07-20 17:43:53 -07002375 /* Decode the next extension. */
2376 if (!CBS_get_u16(&extensions, &type) ||
2377 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2378 *out_alert = SSL_AD_DECODE_ERROR;
2379 return 0;
2380 }
2381
2382 unsigned ext_index;
2383 const struct tls_extension *const ext =
2384 tls_extension_find(&ext_index, type);
2385
2386 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002387 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2388 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2389 return 0;
2390 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002391 continue;
2392 }
2393
Adam Langley614c66a2015-06-12 15:26:58 -07002394 s->s3->tmp.extensions.received |= (1u << ext_index);
2395 uint8_t alert = SSL_AD_DECODE_ERROR;
2396 if (!ext->parse_clienthello(s, &alert, &extension)) {
2397 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002398 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2399 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 return 0;
2401 }
Adam Langley614c66a2015-06-12 15:26:58 -07002402 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 }
Adam Langley75712922014-10-10 16:23:43 -07002404
Adam Langley614c66a2015-06-12 15:26:58 -07002405 for (i = 0; i < kNumExtensions; i++) {
2406 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2407 /* Extension wasn't observed so call the callback with a NULL
2408 * parameter. */
2409 uint8_t alert = SSL_AD_DECODE_ERROR;
2410 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002411 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2412 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002413 *out_alert = alert;
2414 return 0;
2415 }
2416 }
2417 }
2418
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 return 1;
2420}
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2423 int alert = -1;
2424 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2425 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2426 return 0;
2427 }
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002430 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 return 0;
2432 }
Adam Langley95c29f32014-06-20 12:00:00 -07002433
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 return 1;
2435}
Adam Langley95c29f32014-06-20 12:00:00 -07002436
Adam Langleyfcf25832014-12-18 17:42:32 -08002437static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002438 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002439 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002440
Adam Langley33ad2b52015-07-20 17:43:53 -07002441 if (CBS_len(cbs) != 0) {
2442 /* Decode the extensions block and check it is valid. */
2443 CBS extensions;
2444 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2445 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 *out_alert = SSL_AD_DECODE_ERROR;
2447 return 0;
2448 }
Adam Langley95c29f32014-06-20 12:00:00 -07002449
Adam Langley614c66a2015-06-12 15:26:58 -07002450
Adam Langley33ad2b52015-07-20 17:43:53 -07002451 while (CBS_len(&extensions) != 0) {
2452 uint16_t type;
2453 CBS extension;
2454
2455 /* Decode the next extension. */
2456 if (!CBS_get_u16(&extensions, &type) ||
2457 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2458 *out_alert = SSL_AD_DECODE_ERROR;
2459 return 0;
2460 }
2461
2462 unsigned ext_index;
2463 const struct tls_extension *const ext =
2464 tls_extension_find(&ext_index, type);
2465
Adam Langley09505632015-07-30 18:10:13 -07002466 if (ext == NULL) {
2467 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2468 return 0;
2469 }
2470 continue;
2471 }
2472
2473 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2474 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002475 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002476 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 *out_alert = SSL_AD_DECODE_ERROR;
2478 return 0;
2479 }
David Benjamin03973092014-06-24 23:27:17 -04002480
Adam Langley614c66a2015-06-12 15:26:58 -07002481 received |= (1u << ext_index);
2482
2483 uint8_t alert = SSL_AD_DECODE_ERROR;
2484 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002485 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2486 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002487 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 return 0;
2489 }
Adam Langley614c66a2015-06-12 15:26:58 -07002490 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002491 }
Adam Langley95c29f32014-06-20 12:00:00 -07002492
Adam Langley33ad2b52015-07-20 17:43:53 -07002493 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002494 for (i = 0; i < kNumExtensions; i++) {
2495 if (!(received & (1u << i))) {
2496 /* Extension wasn't observed so call the callback with a NULL
2497 * parameter. */
2498 uint8_t alert = SSL_AD_DECODE_ERROR;
2499 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002500 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2501 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002502 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 return 0;
2504 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002505 }
2506 }
Adam Langley95c29f32014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 return 1;
2509}
Adam Langley95c29f32014-06-20 12:00:00 -07002510
Adam Langleyfcf25832014-12-18 17:42:32 -08002511static int ssl_check_clienthello_tlsext(SSL *s) {
2512 int ret = SSL_TLSEXT_ERR_NOACK;
2513 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2516 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2519 ret = s->ctx->tlsext_servername_callback(s, &al,
2520 s->ctx->tlsext_servername_arg);
2521 } else if (s->initial_ctx != NULL &&
2522 s->initial_ctx->tlsext_servername_callback != 0) {
2523 ret = s->initial_ctx->tlsext_servername_callback(
2524 s, &al, s->initial_ctx->tlsext_servername_arg);
2525 }
Adam Langley95c29f32014-06-20 12:00:00 -07002526
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 switch (ret) {
2528 case SSL_TLSEXT_ERR_ALERT_FATAL:
2529 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2530 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 case SSL_TLSEXT_ERR_ALERT_WARNING:
2533 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2534 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002537 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540 default:
2541 return 1;
2542 }
2543}
Adam Langleyed8270a2014-09-02 13:52:56 -07002544
Adam Langleyfcf25832014-12-18 17:42:32 -08002545static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002546 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002547 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2550 ret = s->ctx->tlsext_servername_callback(s, &al,
2551 s->ctx->tlsext_servername_arg);
2552 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002553 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002554 ret = s->initial_ctx->tlsext_servername_callback(
2555 s, &al, s->initial_ctx->tlsext_servername_arg);
2556 }
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 switch (ret) {
2559 case SSL_TLSEXT_ERR_ALERT_FATAL:
2560 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2561 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 case SSL_TLSEXT_ERR_ALERT_WARNING:
2564 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2565 return 1;
2566
2567 default:
2568 return 1;
2569 }
2570}
2571
2572int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2573 int alert = -1;
2574 if (s->version < SSL3_VERSION) {
2575 return 1;
2576 }
2577
2578 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2579 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2580 return 0;
2581 }
2582
2583 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002584 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 return 0;
2586 }
2587
2588 return 1;
2589}
Adam Langley95c29f32014-06-20 12:00:00 -07002590
David Benjamine3aa1d92015-06-16 15:34:50 -04002591int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2592 int *out_send_ticket, const uint8_t *ticket,
2593 size_t ticket_len, const uint8_t *session_id,
2594 size_t session_id_len) {
2595 int ret = 1; /* Most errors are non-fatal. */
2596 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2597 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002598
David Benjamine3aa1d92015-06-16 15:34:50 -04002599 HMAC_CTX hmac_ctx;
2600 HMAC_CTX_init(&hmac_ctx);
2601 EVP_CIPHER_CTX cipher_ctx;
2602 EVP_CIPHER_CTX_init(&cipher_ctx);
2603
2604 *out_send_ticket = 0;
2605 *out_session = NULL;
2606
2607 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2608 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002609 }
2610
David Benjamine3aa1d92015-06-16 15:34:50 -04002611 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002613 *out_send_ticket = 1;
2614 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 }
2616
David Benjaminadcc3952015-04-26 13:07:57 -04002617 /* Ensure there is room for the key name and the largest IV
2618 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2619 * the maximum IV length should be well under the minimum size for the
2620 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002621 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2622 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002624 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002625
David Benjamine3aa1d92015-06-16 15:34:50 -04002626 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2627 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2628 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2629 0 /* decrypt */);
2630 if (cb_ret < 0) {
2631 ret = 0;
2632 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002634 if (cb_ret == 0) {
2635 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002637 if (cb_ret == 2) {
2638 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 }
2640 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002641 /* Check the key name matches. */
2642 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2643 SSL_TICKET_KEY_NAME_LEN) != 0) {
2644 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002646 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2647 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002649 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2650 ssl_ctx->tlsext_tick_aes_key, iv)) {
2651 ret = 0;
2652 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002653 }
2654 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002655 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002656
David Benjamine3aa1d92015-06-16 15:34:50 -04002657 /* Check the MAC at the end of the ticket. */
2658 uint8_t mac[EVP_MAX_MD_SIZE];
2659 size_t mac_len = HMAC_size(&hmac_ctx);
2660 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002661 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002662 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002664 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2665 HMAC_Final(&hmac_ctx, mac, NULL);
2666 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2667 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 }
2669
David Benjamine3aa1d92015-06-16 15:34:50 -04002670 /* Decrypt the session data. */
2671 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2672 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2673 mac_len;
2674 plaintext = OPENSSL_malloc(ciphertext_len);
2675 if (plaintext == NULL) {
2676 ret = 0;
2677 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002679 if (ciphertext_len >= INT_MAX) {
2680 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002682 int len1, len2;
2683 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2684 (int)ciphertext_len) ||
2685 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2686 ERR_clear_error(); /* Don't leave an error on the queue. */
2687 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 }
2689
David Benjamine3aa1d92015-06-16 15:34:50 -04002690 /* Decode the session. */
2691 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2692 if (session == NULL) {
2693 ERR_clear_error(); /* Don't leave an error on the queue. */
2694 goto done;
2695 }
2696
2697 /* Copy the client's session ID into the new session, to denote the ticket has
2698 * been accepted. */
2699 memcpy(session->session_id, session_id, session_id_len);
2700 session->session_id_length = session_id_len;
2701
2702 *out_session = session;
2703
2704done:
2705 OPENSSL_free(plaintext);
2706 HMAC_CTX_cleanup(&hmac_ctx);
2707 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2708 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002709}
Adam Langley95c29f32014-06-20 12:00:00 -07002710
2711/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002712typedef struct {
2713 int nid;
2714 int id;
2715} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2718 {NID_sha1, TLSEXT_hash_sha1},
2719 {NID_sha224, TLSEXT_hash_sha224},
2720 {NID_sha256, TLSEXT_hash_sha256},
2721 {NID_sha384, TLSEXT_hash_sha384},
2722 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2725 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002726
Adam Langleyfcf25832014-12-18 17:42:32 -08002727static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2728 size_t i;
2729 for (i = 0; i < tlen; i++) {
2730 if (table[i].nid == nid) {
2731 return table[i].id;
2732 }
2733 }
Adam Langley95c29f32014-06-20 12:00:00 -07002734
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 return -1;
2736}
Adam Langley95c29f32014-06-20 12:00:00 -07002737
David Benjaminb4d65fd2015-05-29 17:11:21 -04002738int tls12_get_sigid(int pkey_type) {
2739 return tls12_find_id(pkey_type, tls12_sig,
2740 sizeof(tls12_sig) / sizeof(tls12_lookup));
2741}
2742
David Benjamind1d80782015-07-05 11:54:09 -04002743int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002744 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746 if (!md) {
2747 return 0;
2748 }
Adam Langley95c29f32014-06-20 12:00:00 -07002749
Adam Langleyfcf25832014-12-18 17:42:32 -08002750 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2751 sizeof(tls12_md) / sizeof(tls12_lookup));
2752 if (md_id == -1) {
2753 return 0;
2754 }
Adam Langley95c29f32014-06-20 12:00:00 -07002755
David Benjamind1d80782015-07-05 11:54:09 -04002756 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002757 if (sig_id == -1) {
2758 return 0;
2759 }
Adam Langley95c29f32014-06-20 12:00:00 -07002760
Adam Langleyfcf25832014-12-18 17:42:32 -08002761 p[0] = (uint8_t)md_id;
2762 p[1] = (uint8_t)sig_id;
2763 return 1;
2764}
2765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2767 switch (hash_alg) {
2768 case TLSEXT_hash_md5:
2769 return EVP_md5();
2770
2771 case TLSEXT_hash_sha1:
2772 return EVP_sha1();
2773
2774 case TLSEXT_hash_sha224:
2775 return EVP_sha224();
2776
2777 case TLSEXT_hash_sha256:
2778 return EVP_sha256();
2779
2780 case TLSEXT_hash_sha384:
2781 return EVP_sha384();
2782
2783 case TLSEXT_hash_sha512:
2784 return EVP_sha512();
2785
2786 default:
2787 return NULL;
2788 }
2789}
Adam Langley95c29f32014-06-20 12:00:00 -07002790
David Benjaminec2f27d2014-11-13 19:17:25 -05002791/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2792 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002793static int tls12_get_pkey_type(uint8_t sig_alg) {
2794 switch (sig_alg) {
2795 case TLSEXT_signature_rsa:
2796 return EVP_PKEY_RSA;
2797
2798 case TLSEXT_signature_ecdsa:
2799 return EVP_PKEY_EC;
2800
2801 default:
2802 return -1;
2803 }
2804}
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langley95c29f32014-06-20 12:00:00 -07002806/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002807static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2808 size_t preflen, const uint8_t *allow,
2809 size_t allowlen) {
2810 const uint8_t *ptmp, *atmp;
2811 size_t i, j, nmatch = 0;
2812
2813 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2814 /* Skip disabled hashes or signature algorithms */
2815 if (tls12_get_hash(ptmp[0]) == NULL ||
2816 tls12_get_pkey_type(ptmp[1]) == -1) {
2817 continue;
2818 }
2819
2820 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2821 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2822 nmatch++;
2823 if (shsig) {
2824 shsig->rhash = ptmp[0];
2825 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 shsig++;
2827 }
2828
2829 break;
2830 }
2831 }
2832 }
2833
2834 return nmatch;
2835}
Adam Langley95c29f32014-06-20 12:00:00 -07002836
2837/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002838static int tls1_set_shared_sigalgs(SSL *s) {
2839 const uint8_t *pref, *allow, *conf;
2840 size_t preflen, allowlen, conflen;
2841 size_t nmatch;
2842 TLS_SIGALGS *salgs = NULL;
2843 CERT *c = s->cert;
2844
David Benjamin2755a3e2015-04-22 16:17:58 -04002845 OPENSSL_free(c->shared_sigalgs);
2846 c->shared_sigalgs = NULL;
2847 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002848
2849 /* If client use client signature algorithms if not NULL */
2850 if (!s->server && c->client_sigalgs) {
2851 conf = c->client_sigalgs;
2852 conflen = c->client_sigalgslen;
2853 } else if (c->conf_sigalgs) {
2854 conf = c->conf_sigalgs;
2855 conflen = c->conf_sigalgslen;
2856 } else {
2857 conflen = tls12_get_psigalgs(s, &conf);
2858 }
2859
2860 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2861 pref = conf;
2862 preflen = conflen;
2863 allow = c->peer_sigalgs;
2864 allowlen = c->peer_sigalgslen;
2865 } else {
2866 allow = conf;
2867 allowlen = conflen;
2868 pref = c->peer_sigalgs;
2869 preflen = c->peer_sigalgslen;
2870 }
2871
2872 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2873 if (!nmatch) {
2874 return 1;
2875 }
2876
2877 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2878 if (!salgs) {
2879 return 0;
2880 }
2881
2882 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2883 c->shared_sigalgs = salgs;
2884 c->shared_sigalgslen = nmatch;
2885 return 1;
2886}
Adam Langley95c29f32014-06-20 12:00:00 -07002887
2888/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002889int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2890 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002891
Adam Langleyfcf25832014-12-18 17:42:32 -08002892 /* Extension ignored for inappropriate versions */
2893 if (!SSL_USE_SIGALGS(s)) {
2894 return 1;
2895 }
David Benjamincd996942014-07-20 16:23:51 -04002896
David Benjamin61c0d4e2015-03-19 14:24:37 -04002897 if (CBS_len(sigalgs) % 2 != 0 ||
2898 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2899 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 return 0;
2901 }
Adam Langley95c29f32014-06-20 12:00:00 -07002902
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 return 1;
2904}
David Benjaminec2f27d2014-11-13 19:17:25 -05002905
David Benjamind1d80782015-07-05 11:54:09 -04002906const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2907 CERT *cert = ssl->cert;
2908 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002909 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002910
Adam Langleyfcf25832014-12-18 17:42:32 -08002911 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002912 for (i = 0; i < cert->shared_sigalgslen; i++) {
2913 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002915 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2916 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002917 continue;
2918 }
2919 return md;
2920 }
Adam Langley95c29f32014-06-20 12:00:00 -07002921
Adam Langleyfcf25832014-12-18 17:42:32 -08002922 /* If no suitable digest may be found, default to SHA-1. */
2923 return EVP_sha1();
2924}
Adam Langley95c29f32014-06-20 12:00:00 -07002925
Adam Langleyfcf25832014-12-18 17:42:32 -08002926/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2927 * given SSL connection and writes it to |md|. */
2928int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2929 EVP_MD_CTX ctx;
2930 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2931 unsigned temp_digest_len;
2932 int i;
2933 static const char kClientIDMagic[] = "TLS Channel ID signature";
2934
2935 if (s->s3->handshake_buffer &&
2936 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2937 return 0;
2938 }
2939
2940 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2941
Adam Langley49c7af12015-07-10 14:33:46 -07002942 if (s->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 static const char kResumptionMagic[] = "Resumption";
2944 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2945 if (s->session->original_handshake_hash_len == 0) {
2946 return 0;
2947 }
2948 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2949 s->session->original_handshake_hash_len);
2950 }
2951
2952 EVP_MD_CTX_init(&ctx);
2953 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2954 if (s->s3->handshake_dgst[i] == NULL) {
2955 continue;
2956 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002957 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2958 EVP_MD_CTX_cleanup(&ctx);
2959 return 0;
2960 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2962 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2963 }
2964 EVP_MD_CTX_cleanup(&ctx);
2965
2966 return 1;
2967}
Adam Langley1258b6a2014-06-20 12:00:00 -07002968
2969/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2970 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002971int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2972 int digest_len;
2973 /* This function should never be called for a resumed session because the
2974 * handshake hashes that we wish to record are for the original, full
2975 * handshake. */
2976 if (s->hit) {
2977 return -1;
2978 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002979
Adam Langleyfcf25832014-12-18 17:42:32 -08002980 digest_len =
2981 tls1_handshake_digest(s, s->session->original_handshake_hash,
2982 sizeof(s->session->original_handshake_hash));
2983 if (digest_len < 0) {
2984 return -1;
2985 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002986
Adam Langleyfcf25832014-12-18 17:42:32 -08002987 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002988
Adam Langleyfcf25832014-12-18 17:42:32 -08002989 return 1;
2990}
Adam Langley95c29f32014-06-20 12:00:00 -07002991
Adam Langleyfcf25832014-12-18 17:42:32 -08002992int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2993 int client) {
2994 uint8_t *sigalgs, *sptr;
2995 int rhash, rsign;
2996 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002997
Adam Langleyfcf25832014-12-18 17:42:32 -08002998 if (salglen & 1) {
2999 return 0;
3000 }
Adam Langley95c29f32014-06-20 12:00:00 -07003001
Adam Langleyfcf25832014-12-18 17:42:32 -08003002 sigalgs = OPENSSL_malloc(salglen);
3003 if (sigalgs == NULL) {
3004 return 0;
3005 }
Adam Langley95c29f32014-06-20 12:00:00 -07003006
Adam Langleyfcf25832014-12-18 17:42:32 -08003007 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3008 rhash = tls12_find_id(*psig_nids++, tls12_md,
3009 sizeof(tls12_md) / sizeof(tls12_lookup));
3010 rsign = tls12_find_id(*psig_nids++, tls12_sig,
3011 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07003012
Adam Langleyfcf25832014-12-18 17:42:32 -08003013 if (rhash == -1 || rsign == -1) {
3014 goto err;
3015 }
3016 *sptr++ = rhash;
3017 *sptr++ = rsign;
3018 }
3019
3020 if (client) {
David Benjamin2755a3e2015-04-22 16:17:58 -04003021 OPENSSL_free(c->client_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003022 c->client_sigalgs = sigalgs;
3023 c->client_sigalgslen = salglen;
3024 } else {
David Benjamin2755a3e2015-04-22 16:17:58 -04003025 OPENSSL_free(c->conf_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003026 c->conf_sigalgs = sigalgs;
3027 c->conf_sigalgslen = salglen;
3028 }
3029
3030 return 1;
3031
3032err:
3033 OPENSSL_free(sigalgs);
3034 return 0;
3035}