blob: 296ab80025976407412045049f007cd1ad931811 [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 uint8_t ecformats_default[] = {
349 TLSEXT_ECPOINTFORMAT_uncompressed,
350};
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400353 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500354 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800355};
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500358 size_t i;
359 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
360 if (curve_id == tls_curves[i].curve_id) {
361 return tls_curves[i].nid;
362 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500364 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
Adam Langley95c29f32014-06-20 12:00:00 -0700366
David Benjamin70bd80a2014-12-27 03:06:46 -0500367int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500369 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
370 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500371 *out_curve_id = tls_curves[i].curve_id;
372 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 }
374 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376}
377
378/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
379 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
380 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400381static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 const uint16_t **out_curve_ids,
383 size_t *out_curve_ids_len) {
384 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400385 /* Only clients send a curve list, so this function is only called
386 * on the server. */
387 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
389 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
390 return;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 *out_curve_ids = s->tlsext_ellipticcurvelist;
394 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
395 if (!*out_curve_ids) {
396 *out_curve_ids = eccurves_default;
397 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
398 }
399}
David Benjamined439582014-07-14 19:13:02 -0400400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
402 uint8_t curve_type;
403 uint16_t curve_id;
404 const uint16_t *curves;
405 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 /* Only support named curves. */
408 if (!CBS_get_u8(cbs, &curve_type) ||
409 curve_type != NAMED_CURVE_TYPE ||
410 !CBS_get_u16(cbs, &curve_id)) {
411 return 0;
412 }
David Benjamined439582014-07-14 19:13:02 -0400413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 tls1_get_curvelist(s, 0, &curves, &curves_len);
415 for (i = 0; i < curves_len; i++) {
416 if (curve_id == curves[i]) {
417 *out_curve_id = curve_id;
418 return 1;
419 }
420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 return 0;
423}
David Benjamin072334d2014-07-13 16:24:27 -0400424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400426 const uint16_t *curves, *peer_curves, *pref, *supp;
427 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 /* Can't do anything on client side */
430 if (s->server == 0) {
431 return NID_undef;
432 }
433
David Benjamin55a43642015-04-20 14:45:55 -0400434 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
435 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800436
David Benjamin55a43642015-04-20 14:45:55 -0400437 if (peer_curves_len == 0) {
438 /* Clients are not required to send a supported_curves extension. In this
439 * case, the server is free to pick any curve it likes. See RFC 4492,
440 * section 4, paragraph 3. */
441 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
442 }
443
444 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
445 pref = curves;
446 pref_len = curves_len;
447 supp = peer_curves;
448 supp_len = peer_curves_len;
449 } else {
450 pref = peer_curves;
451 pref_len = peer_curves_len;
452 supp = curves;
453 supp_len = curves_len;
454 }
455
456 for (i = 0; i < pref_len; i++) {
457 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 if (pref[i] == supp[j]) {
459 return tls1_ec_curve_id2nid(pref[i]);
460 }
461 }
462 }
463
464 return NID_undef;
465}
Adam Langley95c29f32014-06-20 12:00:00 -0700466
David Benjamin072334d2014-07-13 16:24:27 -0400467int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 const int *curves, size_t ncurves) {
469 uint16_t *curve_ids;
470 size_t i;
471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
473 if (curve_ids == NULL) {
474 return 0;
475 }
476
477 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500478 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 OPENSSL_free(curve_ids);
480 return 0;
481 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 }
483
David Benjamin2755a3e2015-04-22 16:17:58 -0400484 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 *out_curve_ids = curve_ids;
486 *out_curve_ids_len = ncurves;
487
488 return 1;
489}
Adam Langley95c29f32014-06-20 12:00:00 -0700490
David Benjamin072334d2014-07-13 16:24:27 -0400491/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
492 * TLS curve ID and point format, respectively, for |ec|. It returns one on
493 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800494static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
495 uint8_t *out_comp_id, EC_KEY *ec) {
496 int nid;
497 uint16_t id;
498 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 if (ec == NULL) {
501 return 0;
502 }
Adam Langley95c29f32014-06-20 12:00:00 -0700503
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 grp = EC_KEY_get0_group(ec);
505 if (grp == NULL) {
506 return 0;
507 }
David Benjamin072334d2014-07-13 16:24:27 -0400508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 /* Determine curve ID */
510 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500511 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 return 0;
513 }
David Benjamin072334d2014-07-13 16:24:27 -0400514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
516 *out_curve_id = id;
517
518 if (out_comp_id) {
519 if (EC_KEY_get0_public_key(ec) == NULL) {
520 return 0;
521 }
522 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
523 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
524 } else {
525 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
526 }
527 }
528
529 return 1;
530}
David Benjamin072334d2014-07-13 16:24:27 -0400531
David Benjamin42e9a772014-09-02 23:18:44 -0400532/* tls1_check_point_format returns one if |comp_id| is consistent with the
533 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800534static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
535 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
536 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
537 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 /* If point formats extension present check it, otherwise everything is
540 * supported (see RFC4492). */
541 if (p == NULL) {
542 return 1;
543 }
David Benjamin42e9a772014-09-02 23:18:44 -0400544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 for (i = 0; i < plen; i++) {
546 if (comp_id == p[i]) {
547 return 1;
548 }
549 }
David Benjamin42e9a772014-09-02 23:18:44 -0400550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 return 0;
552}
553
554/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
555 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400556 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800557static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
558 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400559 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400562 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
563 if (get_peer_curves && !s->server) {
564 /* Servers do not present a preference list so, if we are a client, only
565 * check our list. */
566 continue;
567 }
568
569 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
570 if (get_peer_curves && curves_len == 0) {
571 /* Clients are not required to send a supported_curves extension. In this
572 * case, the server is free to pick any curve it likes. See RFC 4492,
573 * section 4, paragraph 3. */
574 continue;
575 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 for (i = 0; i < curves_len; i++) {
577 if (curves[i] == curve_id) {
578 break;
579 }
580 }
Adam Langley95c29f32014-06-20 12:00:00 -0700581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 if (i == curves_len) {
583 return 0;
584 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 }
David Benjamin033e5f42014-11-13 18:47:41 -0500586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 return 1;
588}
David Benjamin033e5f42014-11-13 18:47:41 -0500589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
591 size_t *pformatslen) {
592 /* If we have a custom point format list use it otherwise use default */
593 if (s->tlsext_ecpointformatlist) {
594 *pformats = s->tlsext_ecpointformatlist;
595 *pformatslen = s->tlsext_ecpointformatlist_length;
596 } else {
597 *pformats = ecformats_default;
598 *pformatslen = sizeof(ecformats_default);
599 }
600}
601
602int tls1_check_ec_cert(SSL *s, X509 *x) {
603 int ret = 0;
604 EVP_PKEY *pkey = X509_get_pubkey(x);
605 uint16_t curve_id;
606 uint8_t comp_id;
607
608 if (!pkey ||
609 pkey->type != EVP_PKEY_EC ||
610 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
611 !tls1_check_curve_id(s, curve_id) ||
612 !tls1_check_point_format(s, comp_id)) {
613 goto done;
614 }
615
616 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500617
618done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400619 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 return ret;
621}
David Benjamin42e9a772014-09-02 23:18:44 -0400622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400624 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400625 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400626 * the curve. */
627 uint16_t curve_id;
628 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
629 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 }
631
David Benjamindd978782015-04-24 15:20:13 -0400632 if (s->cert->ecdh_tmp_cb != NULL) {
633 /* Assume the callback will provide an acceptable curve. */
634 return 1;
635 }
636
637 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
638 * there is a shared curve. */
639 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800640}
Adam Langley95c29f32014-06-20 12:00:00 -0700641
642/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langley95c29f32014-06-20 12:00:00 -0700645#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langley95c29f32014-06-20 12:00:00 -0700647#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langleyfcf25832014-12-18 17:42:32 -0800649#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700650
David Benjamincff64722014-08-19 19:54:46 -0400651static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 tlsext_sigalg(TLSEXT_hash_sha512)
653 tlsext_sigalg(TLSEXT_hash_sha384)
654 tlsext_sigalg(TLSEXT_hash_sha256)
655 tlsext_sigalg(TLSEXT_hash_sha224)
656 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700657};
David Benjamin05da6e12014-07-12 20:42:55 -0400658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
660 /* If server use client authentication sigalgs if not NULL */
661 if (s->server && s->cert->client_sigalgs) {
662 *psigs = s->cert->client_sigalgs;
663 return s->cert->client_sigalgslen;
664 } else if (s->cert->conf_sigalgs) {
665 *psigs = s->cert->conf_sigalgs;
666 return s->cert->conf_sigalgslen;
667 } else {
668 *psigs = tls12_sigalgs;
669 return sizeof(tls12_sigalgs);
670 }
671}
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
674 * checks it is consistent with |s|'s sent supported signature algorithms and,
675 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
676 * returns 0 and writes an alert into |*out_alert|. */
677int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
678 CBS *cbs, EVP_PKEY *pkey) {
679 const uint8_t *sent_sigs;
680 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400681 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 uint8_t hash, signature;
683
684 /* Should never happen */
685 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400686 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800687 *out_alert = SSL_AD_INTERNAL_ERROR;
688 return 0;
689 }
690
691 if (!CBS_get_u8(cbs, &hash) ||
692 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400693 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800694 *out_alert = SSL_AD_DECODE_ERROR;
695 return 0;
696 }
697
698 /* Check key type is consistent with signature */
699 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400700 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800701 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
702 return 0;
703 }
704
705 if (pkey->type == EVP_PKEY_EC) {
706 uint16_t curve_id;
707 uint8_t comp_id;
708 /* Check compression and curve matches extensions */
709 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
710 *out_alert = SSL_AD_INTERNAL_ERROR;
711 return 0;
712 }
713
714 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
715 !tls1_check_point_format(s, comp_id))) {
David Benjamin3570d732015-06-29 00:28:17 -0400716 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800717 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
718 return 0;
719 }
720 }
721
722 /* Check signature matches a type we sent */
723 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
724 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
725 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
726 break;
727 }
728 }
729
730 /* Allow fallback to SHA-1. */
731 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400732 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800733 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
734 return 0;
735 }
736
737 *out_md = tls12_get_hash(hash);
738 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400739 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
741 return 0;
742 }
743
744 return 1;
745}
746
747/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
748 * supported or doesn't appear in supported signature algorithms. Unlike
749 * ssl_cipher_get_disabled this applies to a specific session and not global
750 * settings. */
751void ssl_set_client_disabled(SSL *s) {
752 CERT *c = s->cert;
753 const uint8_t *sigalgs;
754 size_t i, sigalgslen;
755 int have_rsa = 0, have_ecdsa = 0;
756 c->mask_a = 0;
757 c->mask_k = 0;
758
759 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
760 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
761 c->mask_ssl = SSL_TLSV1_2;
762 } else {
763 c->mask_ssl = 0;
764 }
765
766 /* Now go through all signature algorithms seeing if we support any for RSA,
767 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
768 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
769 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
770 switch (sigalgs[1]) {
771 case TLSEXT_signature_rsa:
772 have_rsa = 1;
773 break;
774
775 case TLSEXT_signature_ecdsa:
776 have_ecdsa = 1;
777 break;
778 }
779 }
780
781 /* Disable auth if we don't include any appropriate signature algorithms. */
782 if (!have_rsa) {
783 c->mask_a |= SSL_aRSA;
784 }
785 if (!have_ecdsa) {
786 c->mask_a |= SSL_aECDSA;
787 }
788
789 /* with PSK there must be client callback set */
790 if (!s->psk_client_callback) {
791 c->mask_a |= SSL_aPSK;
792 c->mask_k |= SSL_kPSK;
793 }
794}
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langley614c66a2015-06-12 15:26:58 -0700796/* tls_extension represents a TLS extension that is handled internally. The
797 * |init| function is called for each handshake, before any other functions of
798 * the extension. Then the add and parse callbacks are called as needed.
799 *
800 * The parse callbacks receive a |CBS| that contains the contents of the
801 * extension (i.e. not including the type and length bytes). If an extension is
802 * not received then the parse callbacks will be called with a NULL CBS so that
803 * they can do any processing needed to handle the absence of an extension.
804 *
805 * The add callbacks receive a |CBB| to which the extension can be appended but
806 * the function is responsible for appending the type and length bytes too.
807 *
808 * All callbacks return one for success and zero for error. If a parse function
809 * returns zero then a fatal alert with value |*out_alert| will be sent. If
810 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
811struct tls_extension {
812 uint16_t value;
813 void (*init)(SSL *ssl);
814
815 int (*add_clienthello)(SSL *ssl, CBB *out);
816 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
817
818 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
819 int (*add_serverhello)(SSL *ssl, CBB *out);
820};
821
822
823/* Server name indication (SNI).
824 *
825 * https://tools.ietf.org/html/rfc6066#section-3. */
826
827static void ext_sni_init(SSL *ssl) {
828 ssl->s3->tmp.should_ack_sni = 0;
829}
830
831static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
832 if (ssl->tlsext_hostname == NULL) {
833 return 1;
834 }
835
836 CBB contents, server_name_list, name;
837 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
838 !CBB_add_u16_length_prefixed(out, &contents) ||
839 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
840 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
841 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
842 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
843 strlen(ssl->tlsext_hostname)) ||
844 !CBB_flush(out)) {
845 return 0;
846 }
847
848 return 1;
849}
850
851static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
852 if (contents == NULL) {
853 return 1;
854 }
855
856 if (CBS_len(contents) != 0) {
857 return 0;
858 }
859
860 assert(ssl->tlsext_hostname != NULL);
861
862 if (!ssl->hit) {
863 assert(ssl->session->tlsext_hostname == NULL);
864 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
865 if (!ssl->session->tlsext_hostname) {
866 *out_alert = SSL_AD_INTERNAL_ERROR;
867 return 0;
868 }
869 }
870
871 return 1;
872}
873
874static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
875 if (contents == NULL) {
876 return 1;
877 }
878
879 /* The servername extension is treated as follows:
880 *
881 * - Only the hostname type is supported with a maximum length of 255.
882 * - The servername is rejected if too long or if it contains zeros, in
883 * which case an fatal alert is generated.
884 * - The servername field is maintained together with the session cache.
885 * - When a session is resumed, the servername callback is invoked in order
886 * to allow the application to position itself to the right context.
887 * - The servername is acknowledged if it is new for a session or when
888 * it is identical to a previously used for the same session.
889 * Applications can control the behaviour. They can at any time
890 * set a 'desirable' servername for a new SSL object. This can be the
891 * case for example with HTTPS when a Host: header field is received and
892 * a renegotiation is requested. In this case, a possible servername
893 * presented in the new client hello is only acknowledged if it matches
894 * the value of the Host: field.
895 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
896 * if they provide for changing an explicit servername context for the
897 * session,
898 * i.e. when the session has been established with a servername extension.
899 */
900
901 CBS server_name_list;
902 char have_seen_host_name = 0;
903
904 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
905 CBS_len(&server_name_list) == 0 ||
906 CBS_len(contents) != 0) {
907 return 0;
908 }
909
910 /* Decode each ServerName in the extension. */
911 while (CBS_len(&server_name_list) > 0) {
912 uint8_t name_type;
913 CBS host_name;
914
915 if (!CBS_get_u8(&server_name_list, &name_type) ||
916 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
917 return 0;
918 }
919
920 /* Only host_name is supported. */
921 if (name_type != TLSEXT_NAMETYPE_host_name) {
922 continue;
923 }
924
925 if (have_seen_host_name) {
926 /* The ServerNameList MUST NOT contain more than one name of the same
927 * name_type. */
928 return 0;
929 }
930
931 have_seen_host_name = 1;
932
933 if (CBS_len(&host_name) == 0 ||
934 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
935 CBS_contains_zero_byte(&host_name)) {
936 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
937 return 0;
938 }
939
940 if (!ssl->hit) {
941 assert(ssl->session->tlsext_hostname == NULL);
942 if (ssl->session->tlsext_hostname) {
943 /* This should be impossible. */
944 return 0;
945 }
946
947 /* Copy the hostname as a string. */
948 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
949 *out_alert = SSL_AD_INTERNAL_ERROR;
950 return 0;
951 }
952
953 ssl->s3->tmp.should_ack_sni = 1;
954 }
955 }
956
957 return 1;
958}
959
960static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
961 if (ssl->hit ||
962 !ssl->s3->tmp.should_ack_sni ||
963 ssl->session->tlsext_hostname == NULL) {
964 return 1;
965 }
966
967 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
968 !CBB_add_u16(out, 0 /* length */)) {
969 return 0;
970 }
971
972 return 1;
973}
974
975
Adam Langley5021b222015-06-12 18:27:58 -0700976/* Renegotiation indication.
977 *
978 * https://tools.ietf.org/html/rfc5746 */
979
980static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
981 CBB contents, prev_finished;
982 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
983 !CBB_add_u16_length_prefixed(out, &contents) ||
984 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
985 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
986 ssl->s3->previous_client_finished_len) ||
987 !CBB_flush(out)) {
988 return 0;
989 }
990
991 return 1;
992}
993
994static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
995 CBS *contents) {
996 if (contents == NULL) {
997 /* No renegotiation extension received.
998 *
999 * Strictly speaking if we want to avoid an attack we should *always* see
1000 * RI even on initial ServerHello because the client doesn't see any
1001 * renegotiation during an attack. However this would mean we could not
1002 * connect to any server which doesn't support RI.
1003 *
1004 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
1005 * defined. */
1006 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
1007 return 1;
1008 }
1009
1010 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -07001012 return 0;
1013 }
1014
1015 const size_t expected_len = ssl->s3->previous_client_finished_len +
1016 ssl->s3->previous_server_finished_len;
1017
1018 /* Check for logic errors */
1019 assert(!expected_len || ssl->s3->previous_client_finished_len);
1020 assert(!expected_len || ssl->s3->previous_server_finished_len);
1021
1022 /* Parse out the extension contents. */
1023 CBS renegotiated_connection;
1024 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1025 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001026 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001027 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1028 return 0;
1029 }
1030
1031 /* Check that the extension matches. */
1032 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -04001033 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001034 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1035 return 0;
1036 }
1037
1038 const uint8_t *d = CBS_data(&renegotiated_connection);
1039 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1040 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001041 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001042 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1043 return 0;
1044 }
1045 d += ssl->s3->previous_client_finished_len;
1046
1047 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1048 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001049 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001050 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1051 return 0;
1052 }
1053 ssl->s3->send_connection_binding = 1;
1054
1055 return 1;
1056}
1057
1058static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1059 CBS *contents) {
1060 /* Renegotiation isn't supported as a server so this function should never be
1061 * called after the initial handshake. */
1062 assert(!ssl->s3->initial_handshake_complete);
1063
1064 CBS fake_contents;
1065 static const uint8_t kFakeExtension[] = {0};
1066
1067 if (contents == NULL) {
1068 if (ssl->s3->send_connection_binding) {
1069 /* The renegotiation SCSV was received so pretend that we received a
1070 * renegotiation extension. */
1071 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1072 contents = &fake_contents;
1073 /* We require that the renegotiation extension is at index zero of
1074 * kExtensions. */
1075 ssl->s3->tmp.extensions.received |= (1u << 0);
1076 } else {
1077 return 1;
1078 }
1079 }
1080
1081 CBS renegotiated_connection;
1082
1083 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1084 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001085 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001086 return 0;
1087 }
1088
1089 /* Check that the extension matches */
1090 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1091 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001092 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001093 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1094 return 0;
1095 }
1096
1097 ssl->s3->send_connection_binding = 1;
1098
1099 return 1;
1100}
1101
1102static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1103 CBB contents, prev_finished;
1104 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1105 !CBB_add_u16_length_prefixed(out, &contents) ||
1106 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1107 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1108 ssl->s3->previous_client_finished_len) ||
1109 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1110 ssl->s3->previous_server_finished_len) ||
1111 !CBB_flush(out)) {
1112 return 0;
1113 }
1114
1115 return 1;
1116}
1117
Adam Langley0a056712015-07-01 15:03:33 -07001118
1119/* Extended Master Secret.
1120 *
1121 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1122
1123static void ext_ems_init(SSL *ssl) {
1124 ssl->s3->tmp.extended_master_secret = 0;
1125}
1126
1127static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1128 if (ssl->version == SSL3_VERSION) {
1129 return 1;
1130 }
1131
1132 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1133 !CBB_add_u16(out, 0 /* length */)) {
1134 return 0;
1135 }
1136
1137 return 1;
1138}
1139
1140static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1141 CBS *contents) {
1142 if (contents == NULL) {
1143 return 1;
1144 }
1145
1146 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1147 return 0;
1148 }
1149
1150 ssl->s3->tmp.extended_master_secret = 1;
1151 return 1;
1152}
1153
1154static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1155 if (ssl->version == SSL3_VERSION || contents == NULL) {
1156 return 1;
1157 }
1158
1159 if (CBS_len(contents) != 0) {
1160 return 0;
1161 }
1162
1163 ssl->s3->tmp.extended_master_secret = 1;
1164 return 1;
1165}
1166
1167static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1168 if (!ssl->s3->tmp.extended_master_secret) {
1169 return 1;
1170 }
1171
1172 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1173 !CBB_add_u16(out, 0 /* length */)) {
1174 return 0;
1175 }
1176
1177 return 1;
1178}
1179
Adam Langley9b05bc52015-07-01 15:25:33 -07001180
1181/* Session tickets.
1182 *
1183 * https://tools.ietf.org/html/rfc5077 */
1184
1185static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1186 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1187 return 1;
1188 }
1189
1190 const uint8_t *ticket_data = NULL;
1191 int ticket_len = 0;
1192
1193 /* Renegotiation does not participate in session resumption. However, still
1194 * advertise the extension to avoid potentially breaking servers which carry
1195 * over the state from the previous handshake, such as OpenSSL servers
1196 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1197 if (!ssl->s3->initial_handshake_complete &&
1198 ssl->session != NULL &&
1199 ssl->session->tlsext_tick != NULL) {
1200 ticket_data = ssl->session->tlsext_tick;
1201 ticket_len = ssl->session->tlsext_ticklen;
1202 }
1203
1204 CBB ticket;
1205 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1206 !CBB_add_u16_length_prefixed(out, &ticket) ||
1207 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1208 !CBB_flush(out)) {
1209 return 0;
1210 }
1211
1212 return 1;
1213}
1214
1215static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1216 CBS *contents) {
1217 ssl->tlsext_ticket_expected = 0;
1218
1219 if (contents == NULL) {
1220 return 1;
1221 }
1222
1223 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1224 * this function should never be called, even if the server tries to send the
1225 * extension. */
1226 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1227
1228 if (CBS_len(contents) != 0) {
1229 return 0;
1230 }
1231
1232 ssl->tlsext_ticket_expected = 1;
1233 return 1;
1234}
1235
1236static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1237 /* This function isn't used because the ticket extension from the client is
1238 * handled in ssl_sess.c. */
1239 return 1;
1240}
1241
1242static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1243 if (!ssl->tlsext_ticket_expected) {
1244 return 1;
1245 }
1246
1247 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1248 * true. */
1249 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1250
1251 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1252 !CBB_add_u16(out, 0 /* length */)) {
1253 return 0;
1254 }
1255
1256 return 1;
1257}
1258
1259
Adam Langley2e857bd2015-07-01 16:09:19 -07001260/* Signature Algorithms.
1261 *
1262 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1263
1264static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1265 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1266 return 1;
1267 }
1268
1269 const uint8_t *sigalgs_data;
1270 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1271
1272 CBB contents, sigalgs;
1273 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1274 !CBB_add_u16_length_prefixed(out, &contents) ||
1275 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1276 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1277 !CBB_flush(out)) {
1278 return 0;
1279 }
1280
1281 return 1;
1282}
1283
1284static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1285 CBS *contents) {
1286 if (contents != NULL) {
1287 /* Servers MUST NOT send this extension. */
1288 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001289 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001290 return 0;
1291 }
1292
1293 return 1;
1294}
1295
1296static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1297 CBS *contents) {
1298 OPENSSL_free(ssl->cert->peer_sigalgs);
1299 ssl->cert->peer_sigalgs = NULL;
1300 ssl->cert->peer_sigalgslen = 0;
1301
1302 OPENSSL_free(ssl->cert->shared_sigalgs);
1303 ssl->cert->shared_sigalgs = NULL;
1304 ssl->cert->shared_sigalgslen = 0;
1305
1306 if (contents == NULL) {
1307 return 1;
1308 }
1309
1310 CBS supported_signature_algorithms;
1311 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1312 CBS_len(contents) != 0) {
1313 return 0;
1314 }
1315
1316 /* Ensure the signature algorithms are non-empty. It contains a list of
1317 * SignatureAndHashAlgorithms which are two bytes each. */
1318 if (CBS_len(&supported_signature_algorithms) == 0 ||
1319 (CBS_len(&supported_signature_algorithms) % 2) != 0 ||
1320 !tls1_process_sigalgs(ssl, &supported_signature_algorithms)) {
1321 return 0;
1322 }
1323
1324 /* It's a fatal error if the signature_algorithms extension is received and
1325 * there are no shared algorithms. */
1326 if (ssl->cert->peer_sigalgs && !ssl->cert->shared_sigalgs) {
David Benjamin3570d732015-06-29 00:28:17 -04001327 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
Adam Langley2e857bd2015-07-01 16:09:19 -07001328 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1329 return 0;
1330 }
1331
1332 return 1;
1333}
1334
1335static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1336 /* Servers MUST NOT send this extension. */
1337 return 1;
1338}
1339
1340
Adam Langleybb0bd042015-07-01 16:21:03 -07001341/* OCSP Stapling.
1342 *
1343 * https://tools.ietf.org/html/rfc6066#section-8 */
1344
1345static void ext_ocsp_init(SSL *ssl) {
1346 ssl->s3->tmp.certificate_status_expected = 0;
1347}
1348
1349static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1350 if (!ssl->ocsp_stapling_enabled) {
1351 return 1;
1352 }
1353
1354 CBB contents;
1355 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1356 !CBB_add_u16_length_prefixed(out, &contents) ||
1357 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1358 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1359 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1360 !CBB_flush(out)) {
1361 return 0;
1362 }
1363
1364 return 1;
1365}
1366
1367static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1368 CBS *contents) {
1369 if (contents == NULL) {
1370 return 1;
1371 }
1372
1373 if (CBS_len(contents) != 0) {
1374 return 0;
1375 }
1376
1377 ssl->s3->tmp.certificate_status_expected = 1;
1378 return 1;
1379}
1380
1381static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1382 CBS *contents) {
1383 /* OCSP stapling as a server is not supported. */
1384 return 1;
1385}
1386
1387static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
1388 /* OCSP stapling as a server is not supported. */
1389 return 1;
1390}
1391
1392
Adam Langley97dfcbf2015-07-01 18:35:20 -07001393/* Next protocol negotiation.
1394 *
1395 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1396
1397static void ext_npn_init(SSL *ssl) {
1398 ssl->s3->next_proto_neg_seen = 0;
1399}
1400
1401static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1402 if (ssl->s3->initial_handshake_complete ||
1403 ssl->ctx->next_proto_select_cb == NULL ||
1404 SSL_IS_DTLS(ssl)) {
1405 return 1;
1406 }
1407
1408 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1409 !CBB_add_u16(out, 0 /* length */)) {
1410 return 0;
1411 }
1412
1413 return 1;
1414}
1415
1416static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1417 CBS *contents) {
1418 if (contents == NULL) {
1419 return 1;
1420 }
1421
1422 /* If any of these are false then we should never have sent the NPN
1423 * extension in the ClientHello and thus this function should never have been
1424 * called. */
1425 assert(!ssl->s3->initial_handshake_complete);
1426 assert(!SSL_IS_DTLS(ssl));
1427 assert(ssl->ctx->next_proto_select_cb != NULL);
1428
1429 const uint8_t *const orig_contents = CBS_data(contents);
1430 const size_t orig_len = CBS_len(contents);
1431
1432 while (CBS_len(contents) != 0) {
1433 CBS proto;
1434 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1435 CBS_len(&proto) == 0) {
1436 return 0;
1437 }
1438 }
1439
1440 uint8_t *selected;
1441 uint8_t selected_len;
1442 if (ssl->ctx->next_proto_select_cb(
1443 ssl, &selected, &selected_len, orig_contents, orig_len,
1444 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1445 *out_alert = SSL_AD_INTERNAL_ERROR;
1446 return 0;
1447 }
1448
1449 OPENSSL_free(ssl->next_proto_negotiated);
1450 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1451 if (ssl->next_proto_negotiated == NULL) {
1452 *out_alert = SSL_AD_INTERNAL_ERROR;
1453 return 0;
1454 }
1455
1456 ssl->next_proto_negotiated_len = selected_len;
1457 ssl->s3->next_proto_neg_seen = 1;
1458
1459 return 1;
1460}
1461
1462static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1463 CBS *contents) {
1464 if (contents != NULL && CBS_len(contents) != 0) {
1465 return 0;
1466 }
1467
1468 if (contents == NULL ||
1469 ssl->s3->initial_handshake_complete ||
1470 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1471 * afterwards, parsing the ALPN extension will clear
1472 * |next_proto_neg_seen|. */
1473 ssl->s3->alpn_selected != NULL ||
1474 ssl->ctx->next_protos_advertised_cb == NULL ||
1475 SSL_IS_DTLS(ssl)) {
1476 return 1;
1477 }
1478
1479 ssl->s3->next_proto_neg_seen = 1;
1480 return 1;
1481}
1482
1483static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1484 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1485 * parsed. */
1486 if (!ssl->s3->next_proto_neg_seen) {
1487 return 1;
1488 }
1489
1490 const uint8_t *npa;
1491 unsigned npa_len;
1492
1493 if (ssl->ctx->next_protos_advertised_cb(
1494 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1495 SSL_TLSEXT_ERR_OK) {
1496 ssl->s3->next_proto_neg_seen = 0;
1497 return 1;
1498 }
1499
1500 CBB contents;
1501 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1502 !CBB_add_u16_length_prefixed(out, &contents) ||
1503 !CBB_add_bytes(&contents, npa, npa_len) ||
1504 !CBB_flush(out)) {
1505 return 0;
1506 }
1507
1508 return 1;
1509}
1510
1511
Adam Langleyab8d87d2015-07-10 12:21:39 -07001512/* Signed certificate timestamps.
1513 *
1514 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1515
1516static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1517 if (!ssl->signed_cert_timestamps_enabled) {
1518 return 1;
1519 }
1520
1521 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1522 !CBB_add_u16(out, 0 /* length */)) {
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
1529static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1530 CBS *contents) {
1531 if (contents == NULL) {
1532 return 1;
1533 }
1534
1535 /* If this is false then we should never have sent the SCT extension in the
1536 * ClientHello and thus this function should never have been called. */
1537 assert(ssl->signed_cert_timestamps_enabled);
1538
1539 if (CBS_len(contents) == 0) {
1540 *out_alert = SSL_AD_DECODE_ERROR;
1541 return 0;
1542 }
1543
1544 /* Session resumption uses the original session information. */
1545 if (!ssl->hit &&
1546 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1547 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1548 *out_alert = SSL_AD_INTERNAL_ERROR;
1549 return 0;
1550 }
1551
1552 return 1;
1553}
1554
1555static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1556 CBS *contents) {
1557 /* The SCT extension is not supported as a server. */
1558 return 1;
1559}
1560
1561static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1562 /* The SCT extension is not supported as a server. */
1563 return 1;
1564}
1565
1566
Adam Langleyf18e4532015-07-10 13:39:53 -07001567/* Application-level Protocol Negotiation.
1568 *
1569 * https://tools.ietf.org/html/rfc7301 */
1570
1571static void ext_alpn_init(SSL *ssl) {
1572 OPENSSL_free(ssl->s3->alpn_selected);
1573 ssl->s3->alpn_selected = NULL;
1574}
1575
1576static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1577 if (ssl->alpn_client_proto_list == NULL ||
1578 ssl->s3->initial_handshake_complete) {
1579 return 1;
1580 }
1581
1582 CBB contents, proto_list;
1583 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1584 !CBB_add_u16_length_prefixed(out, &contents) ||
1585 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1586 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1587 ssl->alpn_client_proto_list_len) ||
1588 !CBB_flush(out)) {
1589 return 0;
1590 }
1591
1592 return 1;
1593}
1594
1595static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1596 CBS *contents) {
1597 if (contents == NULL) {
1598 return 1;
1599 }
1600
1601 assert(!ssl->s3->initial_handshake_complete);
1602 assert(ssl->alpn_client_proto_list != NULL);
1603
1604 /* The extension data consists of a ProtocolNameList which must have
1605 * exactly one ProtocolName. Each of these is length-prefixed. */
1606 CBS protocol_name_list, protocol_name;
1607 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1608 CBS_len(contents) != 0 ||
1609 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1610 /* Empty protocol names are forbidden. */
1611 CBS_len(&protocol_name) == 0 ||
1612 CBS_len(&protocol_name_list) != 0) {
1613 return 0;
1614 }
1615
1616 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1617 &ssl->s3->alpn_selected_len)) {
1618 *out_alert = SSL_AD_INTERNAL_ERROR;
1619 return 0;
1620 }
1621
1622 return 1;
1623}
1624
1625static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1626 CBS *contents) {
1627 if (contents == NULL) {
1628 return 1;
1629 }
1630
1631 if (ssl->ctx->alpn_select_cb == NULL ||
1632 ssl->s3->initial_handshake_complete) {
1633 return 1;
1634 }
1635
1636 /* ALPN takes precedence over NPN. */
1637 ssl->s3->next_proto_neg_seen = 0;
1638
1639 CBS protocol_name_list;
1640 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1641 CBS_len(contents) != 0 ||
1642 CBS_len(&protocol_name_list) < 2) {
1643 return 0;
1644 }
1645
1646 /* Validate the protocol list. */
1647 CBS protocol_name_list_copy = protocol_name_list;
1648 while (CBS_len(&protocol_name_list_copy) > 0) {
1649 CBS protocol_name;
1650
1651 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1652 /* Empty protocol names are forbidden. */
1653 CBS_len(&protocol_name) == 0) {
1654 return 0;
1655 }
1656 }
1657
1658 const uint8_t *selected;
1659 uint8_t selected_len;
1660 if (ssl->ctx->alpn_select_cb(
1661 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1662 CBS_len(&protocol_name_list),
1663 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1664 OPENSSL_free(ssl->s3->alpn_selected);
1665 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1666 if (ssl->s3->alpn_selected == NULL) {
1667 *out_alert = SSL_AD_INTERNAL_ERROR;
1668 return 0;
1669 }
1670 ssl->s3->alpn_selected_len = selected_len;
1671 }
1672
1673 return 1;
1674}
1675
1676static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1677 if (ssl->s3->alpn_selected == NULL) {
1678 return 1;
1679 }
1680
1681 CBB contents, proto_list, proto;
1682 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1683 !CBB_add_u16_length_prefixed(out, &contents) ||
1684 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1685 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1686 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1687 !CBB_flush(out)) {
1688 return 0;
1689 }
1690
1691 return 1;
1692}
1693
1694
Adam Langley49c7af12015-07-10 14:33:46 -07001695/* Channel ID.
1696 *
1697 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1698
1699static void ext_channel_id_init(SSL *ssl) {
1700 ssl->s3->tlsext_channel_id_valid = 0;
1701}
1702
1703static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1704 if (!ssl->tlsext_channel_id_enabled ||
1705 SSL_IS_DTLS(ssl)) {
1706 return 1;
1707 }
1708
1709 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1710 !CBB_add_u16(out, 0 /* length */)) {
1711 return 0;
1712 }
1713
1714 return 1;
1715}
1716
1717static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1718 CBS *contents) {
1719 if (contents == NULL) {
1720 return 1;
1721 }
1722
1723 assert(!SSL_IS_DTLS(ssl));
1724 assert(ssl->tlsext_channel_id_enabled);
1725
1726 if (CBS_len(contents) != 0) {
1727 return 0;
1728 }
1729
1730 ssl->s3->tlsext_channel_id_valid = 1;
1731 return 1;
1732}
1733
1734static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1735 CBS *contents) {
1736 if (contents == NULL ||
1737 !ssl->tlsext_channel_id_enabled ||
1738 SSL_IS_DTLS(ssl)) {
1739 return 1;
1740 }
1741
1742 if (CBS_len(contents) != 0) {
1743 return 0;
1744 }
1745
1746 ssl->s3->tlsext_channel_id_valid = 1;
1747 return 1;
1748}
1749
1750static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1751 if (!ssl->s3->tlsext_channel_id_valid) {
1752 return 1;
1753 }
1754
1755 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1756 !CBB_add_u16(out, 0 /* length */)) {
1757 return 0;
1758 }
1759
1760 return 1;
1761}
1762
Adam Langley391250d2015-07-15 19:06:07 -07001763
1764/* Secure Real-time Transport Protocol (SRTP) extension.
1765 *
1766 * https://tools.ietf.org/html/rfc5764 */
1767
Adam Langley391250d2015-07-15 19:06:07 -07001768
1769static void ext_srtp_init(SSL *ssl) {
1770 ssl->srtp_profile = NULL;
1771}
1772
1773static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1774 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1775 if (profiles == NULL) {
1776 return 1;
1777 }
1778 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1779 if (num_profiles == 0) {
1780 return 1;
1781 }
1782
1783 CBB contents, profile_ids;
1784 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1785 !CBB_add_u16_length_prefixed(out, &contents) ||
1786 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1787 return 0;
1788 }
1789
1790 size_t i;
1791 for (i = 0; i < num_profiles; i++) {
1792 if (!CBB_add_u16(&profile_ids,
1793 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1794 return 0;
1795 }
1796 }
1797
1798 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1799 !CBB_flush(out)) {
1800 return 0;
1801 }
1802
1803 return 1;
1804}
1805
1806static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1807 CBS *contents) {
1808 if (contents == NULL) {
1809 return 1;
1810 }
1811
1812 /* The extension consists of a u16-prefixed profile ID list containing a
1813 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1814 *
1815 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1816 CBS profile_ids, srtp_mki;
1817 uint16_t profile_id;
1818 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1819 !CBS_get_u16(&profile_ids, &profile_id) ||
1820 CBS_len(&profile_ids) != 0 ||
1821 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1822 CBS_len(contents) != 0) {
1823 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1824 return 0;
1825 }
1826
1827 if (CBS_len(&srtp_mki) != 0) {
1828 /* Must be no MKI, since we never offer one. */
1829 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1830 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1831 return 0;
1832 }
1833
1834 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1835
1836 /* Check to see if the server gave us something we support (and presumably
1837 * offered). */
1838 size_t i;
1839 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1840 const SRTP_PROTECTION_PROFILE *profile =
1841 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1842
1843 if (profile->id == profile_id) {
1844 ssl->srtp_profile = profile;
1845 return 1;
1846 }
1847 }
1848
1849 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1850 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1851 return 0;
1852}
1853
1854static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1855 CBS *contents) {
1856 if (contents == NULL) {
1857 return 1;
1858 }
1859
1860 CBS profile_ids, srtp_mki;
1861 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1862 CBS_len(&profile_ids) < 2 ||
1863 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1864 CBS_len(contents) != 0) {
1865 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1866 return 0;
1867 }
1868 /* Discard the MKI value for now. */
1869
1870 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1871 SSL_get_srtp_profiles(ssl);
1872
1873 /* Pick the server's most preferred profile. */
1874 size_t i;
1875 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1876 const SRTP_PROTECTION_PROFILE *server_profile =
1877 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1878
1879 CBS profile_ids_tmp;
1880 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1881
1882 while (CBS_len(&profile_ids_tmp) > 0) {
1883 uint16_t profile_id;
1884 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1885 return 0;
1886 }
1887
1888 if (server_profile->id == profile_id) {
1889 ssl->srtp_profile = server_profile;
1890 return 1;
1891 }
1892 }
1893 }
1894
1895 return 1;
1896}
1897
1898static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1899 if (ssl->srtp_profile == NULL) {
1900 return 1;
1901 }
1902
1903 CBB contents, profile_ids;
1904 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1905 !CBB_add_u16_length_prefixed(out, &contents) ||
1906 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1907 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1908 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1909 !CBB_flush(out)) {
1910 return 0;
1911 }
1912
1913 return 1;
1914}
1915
Adam Langleybdd5d662015-07-20 16:19:08 -07001916
1917/* EC point formats.
1918 *
1919 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1920
1921static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1922 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1923 return 0;
1924 }
1925
1926 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1927
1928 size_t i;
1929 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1930 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1931
1932 const uint32_t alg_k = cipher->algorithm_mkey;
1933 const uint32_t alg_a = cipher->algorithm_auth;
1934 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1935 return 1;
1936 }
1937 }
1938
1939 return 0;
1940}
1941
1942static void ext_ec_point_init(SSL *ssl) {
1943 OPENSSL_free(ssl->s3->tmp.peer_ecpointformatlist);
1944 ssl->s3->tmp.peer_ecpointformatlist = NULL;
1945 ssl->s3->tmp.peer_ecpointformatlist_length = 0;
1946}
1947
1948static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
1949 const uint8_t *formats;
1950 size_t formats_len;
1951 tls1_get_formatlist(ssl, &formats, &formats_len);
1952
1953 CBB contents, format_bytes;
1954 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1955 !CBB_add_u16_length_prefixed(out, &contents) ||
1956 !CBB_add_u8_length_prefixed(&contents, &format_bytes) ||
1957 !CBB_add_bytes(&format_bytes, formats, formats_len) ||
1958 !CBB_flush(out)) {
1959 return 0;
1960 }
1961
1962 return 1;
1963}
1964
1965static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1966 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1967 return 1;
1968 }
1969
1970 return ext_ec_point_add_extension(ssl, out);
1971}
1972
1973static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1974 CBS *contents) {
1975 if (contents == NULL) {
1976 return 1;
1977 }
1978
1979 CBS ec_point_format_list;
1980 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1981 CBS_len(contents) != 0) {
1982 return 0;
1983 }
1984
1985 if (!CBS_stow(&ec_point_format_list, &ssl->s3->tmp.peer_ecpointformatlist,
1986 &ssl->s3->tmp.peer_ecpointformatlist_length)) {
1987 *out_alert = SSL_AD_INTERNAL_ERROR;
1988 return 0;
1989 }
1990
1991 return 1;
1992}
1993
1994static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1995 CBS *contents) {
1996 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1997}
1998
1999static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
2000 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
2001 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
2002 const int using_ecc = ssl->s3->tmp.peer_ecpointformatlist_length != 0 &&
2003 ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2004
2005 if (!using_ecc) {
2006 return 1;
2007 }
2008
2009 return ext_ec_point_add_extension(ssl, out);
2010}
2011
Adam Langley273d49c2015-07-20 16:38:52 -07002012
2013/* EC supported curves.
2014 *
2015 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2016
2017static void ext_ec_curves_init(SSL *ssl) {
2018 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2019 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2020 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2021}
2022
2023static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2024 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2025 return 1;
2026 }
2027
2028 CBB contents, curves_bytes;
2029 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2030 !CBB_add_u16_length_prefixed(out, &contents) ||
2031 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2032 return 0;
2033 }
2034
2035 const uint16_t *curves;
2036 size_t curves_len;
2037 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2038
2039 size_t i;
2040 for (i = 0; i < curves_len; i++) {
2041 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2042 return 0;
2043 }
2044 }
2045
2046 return CBB_flush(out);
2047}
2048
2049static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2050 CBS *contents) {
2051 /* This extension is not expected to be echoed by servers and is ignored. */
2052 return 1;
2053}
2054
2055static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2056 CBS *contents) {
2057 if (contents == NULL) {
2058 return 1;
2059 }
2060
2061 CBS elliptic_curve_list;
2062 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2063 CBS_len(&elliptic_curve_list) == 0 ||
2064 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2065 CBS_len(contents) != 0) {
2066 return 0;
2067 }
2068
2069 ssl->s3->tmp.peer_ellipticcurvelist =
2070 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2071
2072 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2073 *out_alert = SSL_AD_INTERNAL_ERROR;
2074 return 0;
2075 }
2076
2077 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2078 size_t i;
2079 for (i = 0; i < num_curves; i++) {
2080 if (!CBS_get_u16(&elliptic_curve_list,
2081 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2082 goto err;
2083 }
2084 }
2085
2086 assert(CBS_len(&elliptic_curve_list) == 0);
2087 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2088
2089 return 1;
2090
2091err:
2092 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2093 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2094 *out_alert = SSL_AD_INTERNAL_ERROR;
2095 return 0;
2096}
2097
2098static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2099 /* Servers don't echo this extension. */
2100 return 1;
2101}
2102
2103
Adam Langley614c66a2015-06-12 15:26:58 -07002104/* kExtensions contains all the supported extensions. */
2105static const struct tls_extension kExtensions[] = {
2106 {
Adam Langley5021b222015-06-12 18:27:58 -07002107 /* The renegotiation extension must always be at index zero because the
2108 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2109 * sent as an SCSV. */
2110 TLSEXT_TYPE_renegotiate,
2111 NULL,
2112 ext_ri_add_clienthello,
2113 ext_ri_parse_serverhello,
2114 ext_ri_parse_clienthello,
2115 ext_ri_add_serverhello,
2116 },
2117 {
Adam Langley614c66a2015-06-12 15:26:58 -07002118 TLSEXT_TYPE_server_name,
2119 ext_sni_init,
2120 ext_sni_add_clienthello,
2121 ext_sni_parse_serverhello,
2122 ext_sni_parse_clienthello,
2123 ext_sni_add_serverhello,
2124 },
Adam Langley0a056712015-07-01 15:03:33 -07002125 {
2126 TLSEXT_TYPE_extended_master_secret,
2127 ext_ems_init,
2128 ext_ems_add_clienthello,
2129 ext_ems_parse_serverhello,
2130 ext_ems_parse_clienthello,
2131 ext_ems_add_serverhello,
2132 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002133 {
2134 TLSEXT_TYPE_session_ticket,
2135 NULL,
2136 ext_ticket_add_clienthello,
2137 ext_ticket_parse_serverhello,
2138 ext_ticket_parse_clienthello,
2139 ext_ticket_add_serverhello,
2140 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002141 {
2142 TLSEXT_TYPE_signature_algorithms,
2143 NULL,
2144 ext_sigalgs_add_clienthello,
2145 ext_sigalgs_parse_serverhello,
2146 ext_sigalgs_parse_clienthello,
2147 ext_sigalgs_add_serverhello,
2148 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002149 {
2150 TLSEXT_TYPE_status_request,
2151 ext_ocsp_init,
2152 ext_ocsp_add_clienthello,
2153 ext_ocsp_parse_serverhello,
2154 ext_ocsp_parse_clienthello,
2155 ext_ocsp_add_serverhello,
2156 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002157 {
2158 TLSEXT_TYPE_next_proto_neg,
2159 ext_npn_init,
2160 ext_npn_add_clienthello,
2161 ext_npn_parse_serverhello,
2162 ext_npn_parse_clienthello,
2163 ext_npn_add_serverhello,
2164 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002165 {
2166 TLSEXT_TYPE_certificate_timestamp,
2167 NULL,
2168 ext_sct_add_clienthello,
2169 ext_sct_parse_serverhello,
2170 ext_sct_parse_clienthello,
2171 ext_sct_add_serverhello,
2172 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002173 {
2174 TLSEXT_TYPE_application_layer_protocol_negotiation,
2175 ext_alpn_init,
2176 ext_alpn_add_clienthello,
2177 ext_alpn_parse_serverhello,
2178 ext_alpn_parse_clienthello,
2179 ext_alpn_add_serverhello,
2180 },
Adam Langley49c7af12015-07-10 14:33:46 -07002181 {
2182 TLSEXT_TYPE_channel_id,
2183 ext_channel_id_init,
2184 ext_channel_id_add_clienthello,
2185 ext_channel_id_parse_serverhello,
2186 ext_channel_id_parse_clienthello,
2187 ext_channel_id_add_serverhello,
2188 },
Adam Langley391250d2015-07-15 19:06:07 -07002189 {
2190 TLSEXT_TYPE_srtp,
2191 ext_srtp_init,
2192 ext_srtp_add_clienthello,
2193 ext_srtp_parse_serverhello,
2194 ext_srtp_parse_clienthello,
2195 ext_srtp_add_serverhello,
2196 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002197 {
2198 TLSEXT_TYPE_ec_point_formats,
2199 ext_ec_point_init,
2200 ext_ec_point_add_clienthello,
2201 ext_ec_point_parse_serverhello,
2202 ext_ec_point_parse_clienthello,
2203 ext_ec_point_add_serverhello,
2204 },
Adam Langley273d49c2015-07-20 16:38:52 -07002205 {
2206 TLSEXT_TYPE_elliptic_curves,
2207 ext_ec_curves_init,
2208 ext_ec_curves_add_clienthello,
2209 ext_ec_curves_parse_serverhello,
2210 ext_ec_curves_parse_clienthello,
2211 ext_ec_curves_add_serverhello,
2212 },
Adam Langley614c66a2015-06-12 15:26:58 -07002213};
2214
2215#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2216
Adam Langley4cfa96b2015-07-01 11:56:55 -07002217OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2218 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002219 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002220OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2221 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2222 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002223 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002224
Adam Langley614c66a2015-06-12 15:26:58 -07002225static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2226 uint16_t value) {
2227 unsigned i;
2228 for (i = 0; i < kNumExtensions; i++) {
2229 if (kExtensions[i].value == value) {
2230 *out_index = i;
2231 return &kExtensions[i];
2232 }
2233 }
2234
2235 return NULL;
2236}
2237
Adam Langley09505632015-07-30 18:10:13 -07002238int SSL_extension_supported(unsigned extension_value) {
2239 uint32_t index;
2240 return extension_value == TLSEXT_TYPE_padding ||
2241 tls_extension_find(&index, extension_value) != NULL;
2242}
2243
Adam Langleyb0c235e2014-06-20 12:00:00 -07002244/* header_len is the length of the ClientHello header written so far, used to
2245 * compute padding. It does not include the record header. Pass 0 if no padding
2246 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002247uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2248 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002249 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2250 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002251 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 }
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langley33ad2b52015-07-20 17:43:53 -07002254 CBB cbb, extensions;
2255 CBB_zero(&cbb);
2256 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2257 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2258 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 }
Adam Langley95c29f32014-06-20 12:00:00 -07002260
Adam Langley614c66a2015-06-12 15:26:58 -07002261 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002262 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002263
Adam Langley614c66a2015-06-12 15:26:58 -07002264 size_t i;
2265 for (i = 0; i < kNumExtensions; i++) {
2266 if (kExtensions[i].init != NULL) {
2267 kExtensions[i].init(s);
2268 }
2269 }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langley614c66a2015-06-12 15:26:58 -07002271 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002272 const size_t len_before = CBB_len(&extensions);
2273 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2274 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2275 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2276 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 }
Adam Langley95c29f32014-06-20 12:00:00 -07002278
Adam Langley33ad2b52015-07-20 17:43:53 -07002279 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002280 s->s3->tmp.extensions.sent |= (1u << i);
2281 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 }
Adam Langley75712922014-10-10 16:23:43 -07002283
Adam Langley09505632015-07-30 18:10:13 -07002284 if (!custom_ext_add_clienthello(s, &extensions)) {
2285 goto err;
2286 }
2287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002289 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 if (header_len > 0xff && header_len < 0x200) {
2291 /* Add padding to workaround bugs in F5 terminators. See
2292 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2293 *
2294 * NB: because this code works out the length of all existing extensions
2295 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002296 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 /* Extensions take at least four bytes to encode. Always include least
2298 * one byte of data if including the extension. WebSphere Application
2299 * Server 7.0 is intolerant to the last extension being zero-length. */
2300 if (padding_len >= 4 + 1) {
2301 padding_len -= 4;
2302 } else {
2303 padding_len = 1;
2304 }
Adam Langley95c29f32014-06-20 12:00:00 -07002305
Adam Langley33ad2b52015-07-20 17:43:53 -07002306 uint8_t *padding_bytes;
2307 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2308 !CBB_add_u16(&extensions, padding_len) ||
2309 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2310 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 }
Adam Langley75712922014-10-10 16:23:43 -07002312
Adam Langley33ad2b52015-07-20 17:43:53 -07002313 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 }
2315 }
Adam Langley75712922014-10-10 16:23:43 -07002316
Adam Langley33ad2b52015-07-20 17:43:53 -07002317 if (!CBB_flush(&cbb)) {
2318 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langley33ad2b52015-07-20 17:43:53 -07002321 uint8_t *ret = buf;
2322 const size_t cbb_len = CBB_len(&cbb);
2323 /* If only two bytes have been written then the extensions are actually empty
2324 * and those two bytes are the zero length. In that case, we don't bother
2325 * sending the extensions length. */
2326 if (cbb_len > 2) {
2327 ret += cbb_len;
2328 }
2329
2330 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002331 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002332
2333err:
2334 CBB_cleanup(&cbb);
2335 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2336 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002337}
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langley614c66a2015-06-12 15:26:58 -07002339uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2340 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002341 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2342 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002343 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 }
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langley33ad2b52015-07-20 17:43:53 -07002346 CBB cbb, extensions;
2347 CBB_zero(&cbb);
2348 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2349 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2350 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002351 }
2352
2353 unsigned i;
2354 for (i = 0; i < kNumExtensions; i++) {
2355 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2356 /* Don't send extensions that were not received. */
2357 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 }
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langley33ad2b52015-07-20 17:43:53 -07002360 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2361 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2362 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2363 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002364 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002365 }
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langley09505632015-07-30 18:10:13 -07002367 if (!custom_ext_add_serverhello(s, &extensions)) {
2368 goto err;
2369 }
2370
Adam Langley33ad2b52015-07-20 17:43:53 -07002371 if (!CBB_flush(&cbb)) {
2372 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002373 }
2374
Adam Langley33ad2b52015-07-20 17:43:53 -07002375 uint8_t *ret = buf;
2376 const size_t cbb_len = CBB_len(&cbb);
2377 /* If only two bytes have been written then the extensions are actually empty
2378 * and those two bytes are the zero length. In that case, we don't bother
2379 * sending the extensions length. */
2380 if (cbb_len > 2) {
2381 ret += cbb_len;
2382 }
2383
2384 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002386
2387err:
2388 CBB_cleanup(&cbb);
2389 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2390 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002394 size_t i;
2395 for (i = 0; i < kNumExtensions; i++) {
2396 if (kExtensions[i].init != NULL) {
2397 kExtensions[i].init(s);
2398 }
2399 }
2400
2401 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002402 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002403 /* The renegotiation extension must always be at index zero because the
2404 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2405 * sent as an SCSV. */
2406 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002409 if (CBS_len(cbs) != 0) {
2410 /* Decode the extensions block and check it is valid. */
2411 CBS extensions;
2412 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2413 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 *out_alert = SSL_AD_DECODE_ERROR;
2415 return 0;
2416 }
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langley33ad2b52015-07-20 17:43:53 -07002418 while (CBS_len(&extensions) != 0) {
2419 uint16_t type;
2420 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langley33ad2b52015-07-20 17:43:53 -07002422 /* Decode the next extension. */
2423 if (!CBS_get_u16(&extensions, &type) ||
2424 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2425 *out_alert = SSL_AD_DECODE_ERROR;
2426 return 0;
2427 }
2428
2429 unsigned ext_index;
2430 const struct tls_extension *const ext =
2431 tls_extension_find(&ext_index, type);
2432
2433 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002434 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2435 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2436 return 0;
2437 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002438 continue;
2439 }
2440
Adam Langley614c66a2015-06-12 15:26:58 -07002441 s->s3->tmp.extensions.received |= (1u << ext_index);
2442 uint8_t alert = SSL_AD_DECODE_ERROR;
2443 if (!ext->parse_clienthello(s, &alert, &extension)) {
2444 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002445 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2446 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 return 0;
2448 }
Adam Langley614c66a2015-06-12 15:26:58 -07002449 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 }
Adam Langley75712922014-10-10 16:23:43 -07002451
Adam Langley614c66a2015-06-12 15:26:58 -07002452 for (i = 0; i < kNumExtensions; i++) {
2453 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2454 /* Extension wasn't observed so call the callback with a NULL
2455 * parameter. */
2456 uint8_t alert = SSL_AD_DECODE_ERROR;
2457 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002458 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2459 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002460 *out_alert = alert;
2461 return 0;
2462 }
2463 }
2464 }
2465
Adam Langleyfcf25832014-12-18 17:42:32 -08002466 return 1;
2467}
Adam Langley95c29f32014-06-20 12:00:00 -07002468
Adam Langleyfcf25832014-12-18 17:42:32 -08002469int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2470 int alert = -1;
2471 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2472 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2473 return 0;
2474 }
Adam Langley95c29f32014-06-20 12:00:00 -07002475
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002477 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002478 return 0;
2479 }
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 return 1;
2482}
Adam Langley95c29f32014-06-20 12:00:00 -07002483
Adam Langleyfcf25832014-12-18 17:42:32 -08002484static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002485 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002486 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002487
Adam Langley33ad2b52015-07-20 17:43:53 -07002488 if (CBS_len(cbs) != 0) {
2489 /* Decode the extensions block and check it is valid. */
2490 CBS extensions;
2491 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2492 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 *out_alert = SSL_AD_DECODE_ERROR;
2494 return 0;
2495 }
Adam Langley95c29f32014-06-20 12:00:00 -07002496
Adam Langley614c66a2015-06-12 15:26:58 -07002497
Adam Langley33ad2b52015-07-20 17:43:53 -07002498 while (CBS_len(&extensions) != 0) {
2499 uint16_t type;
2500 CBS extension;
2501
2502 /* Decode the next extension. */
2503 if (!CBS_get_u16(&extensions, &type) ||
2504 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2505 *out_alert = SSL_AD_DECODE_ERROR;
2506 return 0;
2507 }
2508
2509 unsigned ext_index;
2510 const struct tls_extension *const ext =
2511 tls_extension_find(&ext_index, type);
2512
Adam Langley09505632015-07-30 18:10:13 -07002513 if (ext == NULL) {
2514 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2515 return 0;
2516 }
2517 continue;
2518 }
2519
2520 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2521 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002522 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002523 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 *out_alert = SSL_AD_DECODE_ERROR;
2525 return 0;
2526 }
David Benjamin03973092014-06-24 23:27:17 -04002527
Adam Langley614c66a2015-06-12 15:26:58 -07002528 received |= (1u << ext_index);
2529
2530 uint8_t alert = SSL_AD_DECODE_ERROR;
2531 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002532 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2533 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002534 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 return 0;
2536 }
Adam Langley614c66a2015-06-12 15:26:58 -07002537 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 }
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langley33ad2b52015-07-20 17:43:53 -07002540 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002541 for (i = 0; i < kNumExtensions; i++) {
2542 if (!(received & (1u << i))) {
2543 /* Extension wasn't observed so call the callback with a NULL
2544 * parameter. */
2545 uint8_t alert = SSL_AD_DECODE_ERROR;
2546 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002547 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2548 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002549 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 return 0;
2551 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 }
2553 }
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 return 1;
2556}
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558static int ssl_check_clienthello_tlsext(SSL *s) {
2559 int ret = SSL_TLSEXT_ERR_NOACK;
2560 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002561
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2563 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002564
Adam Langleyfcf25832014-12-18 17:42:32 -08002565 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2566 ret = s->ctx->tlsext_servername_callback(s, &al,
2567 s->ctx->tlsext_servername_arg);
2568 } else if (s->initial_ctx != NULL &&
2569 s->initial_ctx->tlsext_servername_callback != 0) {
2570 ret = s->initial_ctx->tlsext_servername_callback(
2571 s, &al, s->initial_ctx->tlsext_servername_arg);
2572 }
Adam Langley95c29f32014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 switch (ret) {
2575 case SSL_TLSEXT_ERR_ALERT_FATAL:
2576 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2577 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 case SSL_TLSEXT_ERR_ALERT_WARNING:
2580 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2581 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002584 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002586
Adam Langleyfcf25832014-12-18 17:42:32 -08002587 default:
2588 return 1;
2589 }
2590}
Adam Langleyed8270a2014-09-02 13:52:56 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592static int ssl_check_serverhello_tlsext(SSL *s) {
2593 int ret = SSL_TLSEXT_ERR_NOACK;
2594 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596 /* If we are client and using an elliptic curve cryptography cipher suite,
2597 * then if server returns an EC point formats lists extension it must contain
2598 * uncompressed. */
David Benjamin107db582015-04-08 00:41:59 -04002599 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2600 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04002601 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08002602 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -04002603 OPENSSL_PUT_ERROR(SSL, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
Adam Langleyfcf25832014-12-18 17:42:32 -08002604 return -1;
2605 }
2606 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002607
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2609 ret = s->ctx->tlsext_servername_callback(s, &al,
2610 s->ctx->tlsext_servername_arg);
2611 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002612 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002613 ret = s->initial_ctx->tlsext_servername_callback(
2614 s, &al, s->initial_ctx->tlsext_servername_arg);
2615 }
Adam Langley95c29f32014-06-20 12:00:00 -07002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 switch (ret) {
2618 case SSL_TLSEXT_ERR_ALERT_FATAL:
2619 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2620 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002621
Adam Langleyfcf25832014-12-18 17:42:32 -08002622 case SSL_TLSEXT_ERR_ALERT_WARNING:
2623 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2624 return 1;
2625
2626 default:
2627 return 1;
2628 }
2629}
2630
2631int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2632 int alert = -1;
2633 if (s->version < SSL3_VERSION) {
2634 return 1;
2635 }
2636
2637 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2638 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2639 return 0;
2640 }
2641
2642 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002643 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002644 return 0;
2645 }
2646
2647 return 1;
2648}
Adam Langley95c29f32014-06-20 12:00:00 -07002649
David Benjamine3aa1d92015-06-16 15:34:50 -04002650int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2651 int *out_send_ticket, const uint8_t *ticket,
2652 size_t ticket_len, const uint8_t *session_id,
2653 size_t session_id_len) {
2654 int ret = 1; /* Most errors are non-fatal. */
2655 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2656 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002657
David Benjamine3aa1d92015-06-16 15:34:50 -04002658 HMAC_CTX hmac_ctx;
2659 HMAC_CTX_init(&hmac_ctx);
2660 EVP_CIPHER_CTX cipher_ctx;
2661 EVP_CIPHER_CTX_init(&cipher_ctx);
2662
2663 *out_send_ticket = 0;
2664 *out_session = NULL;
2665
2666 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2667 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 }
2669
David Benjamine3aa1d92015-06-16 15:34:50 -04002670 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002672 *out_send_ticket = 1;
2673 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
2675
David Benjaminadcc3952015-04-26 13:07:57 -04002676 /* Ensure there is room for the key name and the largest IV
2677 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2678 * the maximum IV length should be well under the minimum size for the
2679 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002680 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2681 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002683 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002684
David Benjamine3aa1d92015-06-16 15:34:50 -04002685 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2686 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2687 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2688 0 /* decrypt */);
2689 if (cb_ret < 0) {
2690 ret = 0;
2691 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002693 if (cb_ret == 0) {
2694 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002696 if (cb_ret == 2) {
2697 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 }
2699 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002700 /* Check the key name matches. */
2701 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2702 SSL_TICKET_KEY_NAME_LEN) != 0) {
2703 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002705 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2706 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002708 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2709 ssl_ctx->tlsext_tick_aes_key, iv)) {
2710 ret = 0;
2711 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002712 }
2713 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002714 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002715
David Benjamine3aa1d92015-06-16 15:34:50 -04002716 /* Check the MAC at the end of the ticket. */
2717 uint8_t mac[EVP_MAX_MD_SIZE];
2718 size_t mac_len = HMAC_size(&hmac_ctx);
2719 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002720 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002721 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002722 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002723 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2724 HMAC_Final(&hmac_ctx, mac, NULL);
2725 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2726 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002727 }
2728
David Benjamine3aa1d92015-06-16 15:34:50 -04002729 /* Decrypt the session data. */
2730 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2731 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2732 mac_len;
2733 plaintext = OPENSSL_malloc(ciphertext_len);
2734 if (plaintext == NULL) {
2735 ret = 0;
2736 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002737 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002738 if (ciphertext_len >= INT_MAX) {
2739 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002740 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002741 int len1, len2;
2742 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2743 (int)ciphertext_len) ||
2744 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2745 ERR_clear_error(); /* Don't leave an error on the queue. */
2746 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002747 }
2748
David Benjamine3aa1d92015-06-16 15:34:50 -04002749 /* Decode the session. */
2750 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2751 if (session == NULL) {
2752 ERR_clear_error(); /* Don't leave an error on the queue. */
2753 goto done;
2754 }
2755
2756 /* Copy the client's session ID into the new session, to denote the ticket has
2757 * been accepted. */
2758 memcpy(session->session_id, session_id, session_id_len);
2759 session->session_id_length = session_id_len;
2760
2761 *out_session = session;
2762
2763done:
2764 OPENSSL_free(plaintext);
2765 HMAC_CTX_cleanup(&hmac_ctx);
2766 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2767 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002768}
Adam Langley95c29f32014-06-20 12:00:00 -07002769
2770/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002771typedef struct {
2772 int nid;
2773 int id;
2774} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002775
Adam Langleyfcf25832014-12-18 17:42:32 -08002776static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2777 {NID_sha1, TLSEXT_hash_sha1},
2778 {NID_sha224, TLSEXT_hash_sha224},
2779 {NID_sha256, TLSEXT_hash_sha256},
2780 {NID_sha384, TLSEXT_hash_sha384},
2781 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002782
Adam Langleyfcf25832014-12-18 17:42:32 -08002783static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2784 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2787 size_t i;
2788 for (i = 0; i < tlen; i++) {
2789 if (table[i].nid == nid) {
2790 return table[i].id;
2791 }
2792 }
Adam Langley95c29f32014-06-20 12:00:00 -07002793
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 return -1;
2795}
Adam Langley95c29f32014-06-20 12:00:00 -07002796
David Benjaminb4d65fd2015-05-29 17:11:21 -04002797int tls12_get_sigid(int pkey_type) {
2798 return tls12_find_id(pkey_type, tls12_sig,
2799 sizeof(tls12_sig) / sizeof(tls12_lookup));
2800}
2801
David Benjamind1d80782015-07-05 11:54:09 -04002802int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002804
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 if (!md) {
2806 return 0;
2807 }
Adam Langley95c29f32014-06-20 12:00:00 -07002808
Adam Langleyfcf25832014-12-18 17:42:32 -08002809 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2810 sizeof(tls12_md) / sizeof(tls12_lookup));
2811 if (md_id == -1) {
2812 return 0;
2813 }
Adam Langley95c29f32014-06-20 12:00:00 -07002814
David Benjamind1d80782015-07-05 11:54:09 -04002815 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 if (sig_id == -1) {
2817 return 0;
2818 }
Adam Langley95c29f32014-06-20 12:00:00 -07002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 p[0] = (uint8_t)md_id;
2821 p[1] = (uint8_t)sig_id;
2822 return 1;
2823}
2824
Adam Langleyfcf25832014-12-18 17:42:32 -08002825const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2826 switch (hash_alg) {
2827 case TLSEXT_hash_md5:
2828 return EVP_md5();
2829
2830 case TLSEXT_hash_sha1:
2831 return EVP_sha1();
2832
2833 case TLSEXT_hash_sha224:
2834 return EVP_sha224();
2835
2836 case TLSEXT_hash_sha256:
2837 return EVP_sha256();
2838
2839 case TLSEXT_hash_sha384:
2840 return EVP_sha384();
2841
2842 case TLSEXT_hash_sha512:
2843 return EVP_sha512();
2844
2845 default:
2846 return NULL;
2847 }
2848}
Adam Langley95c29f32014-06-20 12:00:00 -07002849
David Benjaminec2f27d2014-11-13 19:17:25 -05002850/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2851 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002852static int tls12_get_pkey_type(uint8_t sig_alg) {
2853 switch (sig_alg) {
2854 case TLSEXT_signature_rsa:
2855 return EVP_PKEY_RSA;
2856
2857 case TLSEXT_signature_ecdsa:
2858 return EVP_PKEY_EC;
2859
2860 default:
2861 return -1;
2862 }
2863}
Adam Langley95c29f32014-06-20 12:00:00 -07002864
Adam Langley95c29f32014-06-20 12:00:00 -07002865/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002866static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2867 size_t preflen, const uint8_t *allow,
2868 size_t allowlen) {
2869 const uint8_t *ptmp, *atmp;
2870 size_t i, j, nmatch = 0;
2871
2872 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2873 /* Skip disabled hashes or signature algorithms */
2874 if (tls12_get_hash(ptmp[0]) == NULL ||
2875 tls12_get_pkey_type(ptmp[1]) == -1) {
2876 continue;
2877 }
2878
2879 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2880 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2881 nmatch++;
2882 if (shsig) {
2883 shsig->rhash = ptmp[0];
2884 shsig->rsign = ptmp[1];
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 shsig++;
2886 }
2887
2888 break;
2889 }
2890 }
2891 }
2892
2893 return nmatch;
2894}
Adam Langley95c29f32014-06-20 12:00:00 -07002895
2896/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002897static int tls1_set_shared_sigalgs(SSL *s) {
2898 const uint8_t *pref, *allow, *conf;
2899 size_t preflen, allowlen, conflen;
2900 size_t nmatch;
2901 TLS_SIGALGS *salgs = NULL;
2902 CERT *c = s->cert;
2903
David Benjamin2755a3e2015-04-22 16:17:58 -04002904 OPENSSL_free(c->shared_sigalgs);
2905 c->shared_sigalgs = NULL;
2906 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002907
2908 /* If client use client signature algorithms if not NULL */
2909 if (!s->server && c->client_sigalgs) {
2910 conf = c->client_sigalgs;
2911 conflen = c->client_sigalgslen;
2912 } else if (c->conf_sigalgs) {
2913 conf = c->conf_sigalgs;
2914 conflen = c->conf_sigalgslen;
2915 } else {
2916 conflen = tls12_get_psigalgs(s, &conf);
2917 }
2918
2919 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2920 pref = conf;
2921 preflen = conflen;
2922 allow = c->peer_sigalgs;
2923 allowlen = c->peer_sigalgslen;
2924 } else {
2925 allow = conf;
2926 allowlen = conflen;
2927 pref = c->peer_sigalgs;
2928 preflen = c->peer_sigalgslen;
2929 }
2930
2931 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2932 if (!nmatch) {
2933 return 1;
2934 }
2935
2936 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2937 if (!salgs) {
2938 return 0;
2939 }
2940
2941 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2942 c->shared_sigalgs = salgs;
2943 c->shared_sigalgslen = nmatch;
2944 return 1;
2945}
Adam Langley95c29f32014-06-20 12:00:00 -07002946
2947/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002948int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2949 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002950
Adam Langleyfcf25832014-12-18 17:42:32 -08002951 /* Extension ignored for inappropriate versions */
2952 if (!SSL_USE_SIGALGS(s)) {
2953 return 1;
2954 }
David Benjamincd996942014-07-20 16:23:51 -04002955
David Benjamin61c0d4e2015-03-19 14:24:37 -04002956 if (CBS_len(sigalgs) % 2 != 0 ||
2957 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2958 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002959 return 0;
2960 }
Adam Langley95c29f32014-06-20 12:00:00 -07002961
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 return 1;
2963}
David Benjaminec2f27d2014-11-13 19:17:25 -05002964
David Benjamind1d80782015-07-05 11:54:09 -04002965const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2966 CERT *cert = ssl->cert;
2967 int type = ssl_private_key_type(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002969
Adam Langleyfcf25832014-12-18 17:42:32 -08002970 /* Select the first shared digest supported by our key. */
David Benjamind1d80782015-07-05 11:54:09 -04002971 for (i = 0; i < cert->shared_sigalgslen; i++) {
2972 const EVP_MD *md = tls12_get_hash(cert->shared_sigalgs[i].rhash);
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 if (md == NULL ||
David Benjamind1d80782015-07-05 11:54:09 -04002974 tls12_get_pkey_type(cert->shared_sigalgs[i].rsign) != type ||
2975 !ssl_private_key_supports_digest(ssl, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002976 continue;
2977 }
2978 return md;
2979 }
Adam Langley95c29f32014-06-20 12:00:00 -07002980
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 /* If no suitable digest may be found, default to SHA-1. */
2982 return EVP_sha1();
2983}
Adam Langley95c29f32014-06-20 12:00:00 -07002984
Adam Langleyfcf25832014-12-18 17:42:32 -08002985/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2986 * given SSL connection and writes it to |md|. */
2987int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2988 EVP_MD_CTX ctx;
2989 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2990 unsigned temp_digest_len;
2991 int i;
2992 static const char kClientIDMagic[] = "TLS Channel ID signature";
2993
2994 if (s->s3->handshake_buffer &&
2995 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2996 return 0;
2997 }
2998
2999 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
3000
Adam Langley49c7af12015-07-10 14:33:46 -07003001 if (s->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003002 static const char kResumptionMagic[] = "Resumption";
3003 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
3004 if (s->session->original_handshake_hash_len == 0) {
3005 return 0;
3006 }
3007 EVP_DigestUpdate(md, s->session->original_handshake_hash,
3008 s->session->original_handshake_hash_len);
3009 }
3010
3011 EVP_MD_CTX_init(&ctx);
3012 for (i = 0; i < SSL_MAX_DIGEST; i++) {
3013 if (s->s3->handshake_dgst[i] == NULL) {
3014 continue;
3015 }
David Benjamin9d0847a2015-02-16 03:57:55 -05003016 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
3017 EVP_MD_CTX_cleanup(&ctx);
3018 return 0;
3019 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003020 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
3021 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
3022 }
3023 EVP_MD_CTX_cleanup(&ctx);
3024
3025 return 1;
3026}
Adam Langley1258b6a2014-06-20 12:00:00 -07003027
3028/* tls1_record_handshake_hashes_for_channel_id records the current handshake
3029 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08003030int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
3031 int digest_len;
3032 /* This function should never be called for a resumed session because the
3033 * handshake hashes that we wish to record are for the original, full
3034 * handshake. */
3035 if (s->hit) {
3036 return -1;
3037 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003038
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 digest_len =
3040 tls1_handshake_digest(s, s->session->original_handshake_hash,
3041 sizeof(s->session->original_handshake_hash));
3042 if (digest_len < 0) {
3043 return -1;
3044 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003045
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003047
Adam Langleyfcf25832014-12-18 17:42:32 -08003048 return 1;
3049}
Adam Langley95c29f32014-06-20 12:00:00 -07003050
Adam Langleyfcf25832014-12-18 17:42:32 -08003051int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3052 int client) {
3053 uint8_t *sigalgs, *sptr;
3054 int rhash, rsign;
3055 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07003056
Adam Langleyfcf25832014-12-18 17:42:32 -08003057 if (salglen & 1) {
3058 return 0;
3059 }
Adam Langley95c29f32014-06-20 12:00:00 -07003060
Adam Langleyfcf25832014-12-18 17:42:32 -08003061 sigalgs = OPENSSL_malloc(salglen);
3062 if (sigalgs == NULL) {
3063 return 0;
3064 }
Adam Langley95c29f32014-06-20 12:00:00 -07003065
Adam Langleyfcf25832014-12-18 17:42:32 -08003066 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3067 rhash = tls12_find_id(*psig_nids++, tls12_md,
3068 sizeof(tls12_md) / sizeof(tls12_lookup));
3069 rsign = tls12_find_id(*psig_nids++, tls12_sig,
3070 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07003071
Adam Langleyfcf25832014-12-18 17:42:32 -08003072 if (rhash == -1 || rsign == -1) {
3073 goto err;
3074 }
3075 *sptr++ = rhash;
3076 *sptr++ = rsign;
3077 }
3078
3079 if (client) {
David Benjamin2755a3e2015-04-22 16:17:58 -04003080 OPENSSL_free(c->client_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 c->client_sigalgs = sigalgs;
3082 c->client_sigalgslen = salglen;
3083 } else {
David Benjamin2755a3e2015-04-22 16:17:58 -04003084 OPENSSL_free(c->conf_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003085 c->conf_sigalgs = sigalgs;
3086 c->conf_sigalgslen = salglen;
3087 }
3088
3089 return 1;
3090
3091err:
3092 OPENSSL_free(sigalgs);
3093 return 0;
3094}