blob: 617ea6e99463818aca892127cc84d37adae43914 [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 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin39482a12014-07-20 13:30:15 -0400151#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin676d1e72014-07-08 14:34:10 -0400155#include <openssl/buf.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/dh.h>
David Benjaminb0883312015-08-06 09:54:13 -0400157#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400158#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700159#include <openssl/md5.h>
160#include <openssl/mem.h>
161#include <openssl/obj.h>
162
David Benjamin2ee94aa2015-04-07 22:38:30 -0400163#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700164
Adam Langleybe2900a2014-12-18 12:09:04 -0800165
David Benjamin338fcaf2014-12-11 01:20:52 -0500166const SSL3_ENC_METHOD SSLv3_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500167 ssl3_prf,
David Benjamin044abb02014-12-23 10:57:17 -0500168 tls1_setup_key_block,
David Benjamin41ac9792014-12-23 10:41:06 -0500169 tls1_generate_master_secret,
David Benjamin044abb02014-12-23 10:57:17 -0500170 tls1_change_cipher_state,
Adam Langleybe2900a2014-12-18 12:09:04 -0800171 ssl3_final_finish_mac,
Adam Langleybe2900a2014-12-18 12:09:04 -0800172 ssl3_cert_verify_mac,
173 SSL3_MD_CLIENT_FINISHED_CONST, 4,
174 SSL3_MD_SERVER_FINISHED_CONST, 4,
175 ssl3_alert_code,
David Benjamind6e95ee2015-05-03 15:34:35 -0400176 tls1_export_keying_material,
Adam Langleybe2900a2014-12-18 12:09:04 -0800177 0,
Adam Langleybe2900a2014-12-18 12:09:04 -0800178};
Adam Langley95c29f32014-06-20 12:00:00 -0700179
David Benjamina1c90a52015-05-30 17:03:14 -0400180int ssl3_supports_cipher(const SSL_CIPHER *cipher) {
181 return 1;
Adam Langleybe2900a2014-12-18 12:09:04 -0800182}
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjaminfbdfefb2015-02-16 19:33:53 -0500184int ssl3_set_handshake_header(SSL *s, int htype, unsigned long len) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800185 uint8_t *p = (uint8_t *)s->init_buf->data;
186 *(p++) = htype;
187 l2n3(len, p);
188 s->init_num = (int)len + SSL3_HM_HEADER_LENGTH;
189 s->init_off = 0;
David Benjamine4824e82014-12-14 19:44:34 -0500190
Adam Langleybe2900a2014-12-18 12:09:04 -0800191 /* Add the message to the handshake hash. */
David Benjamin9550c3a2015-08-05 08:50:34 -0400192 return ssl3_update_handshake_hash(s, (uint8_t *)s->init_buf->data,
193 s->init_num);
Adam Langleybe2900a2014-12-18 12:09:04 -0800194}
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langleybe2900a2014-12-18 12:09:04 -0800196int ssl3_handshake_write(SSL *s) { return ssl3_do_write(s, SSL3_RT_HANDSHAKE); }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langleybe2900a2014-12-18 12:09:04 -0800198int ssl3_new(SSL *s) {
199 SSL3_STATE *s3;
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langleybe2900a2014-12-18 12:09:04 -0800201 s3 = OPENSSL_malloc(sizeof *s3);
202 if (s3 == NULL) {
203 goto err;
204 }
205 memset(s3, 0, sizeof *s3);
Adam Langleybe2900a2014-12-18 12:09:04 -0800206
David Benjaminb0883312015-08-06 09:54:13 -0400207 EVP_MD_CTX_init(&s3->handshake_hash);
208 EVP_MD_CTX_init(&s3->handshake_md5);
209
Adam Langleybe2900a2014-12-18 12:09:04 -0800210 s->s3 = s3;
Adam Langleybe2900a2014-12-18 12:09:04 -0800211
David Benjamin62fd1622015-01-11 13:30:01 -0500212 /* Set the version to the highest supported version for TLS. This controls the
213 * initial state of |s->enc_method| and what the API reports as the version
214 * prior to negotiation.
215 *
216 * TODO(davidben): This is fragile and confusing. */
217 s->version = TLS1_2_VERSION;
Adam Langleybe2900a2014-12-18 12:09:04 -0800218 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700219err:
Adam Langleybe2900a2014-12-18 12:09:04 -0800220 return 0;
221}
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langleybe2900a2014-12-18 12:09:04 -0800223void ssl3_free(SSL *s) {
David Benjamin62fd1622015-01-11 13:30:01 -0500224 if (s == NULL || s->s3 == NULL) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800225 return;
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
Adam Langleybe2900a2014-12-18 12:09:04 -0800228 ssl3_cleanup_key_block(s);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400229 ssl_read_buffer_clear(s);
230 ssl_write_buffer_clear(s);
David Benjamin2755a3e2015-04-22 16:17:58 -0400231 DH_free(s->s3->tmp.dh);
232 EC_KEY_free(s->s3->tmp.ecdh);
David Benjamin4b755cb2014-12-12 03:58:07 -0500233
David Benjamin2755a3e2015-04-22 16:17:58 -0400234 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
235 OPENSSL_free(s->s3->tmp.certificate_types);
David Benjamin2755a3e2015-04-22 16:17:58 -0400236 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
237 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
David Benjamin65055672015-08-22 12:49:30 -0400238 DH_free(s->s3->tmp.peer_dh_tmp);
239 EC_KEY_free(s->s3->tmp.peer_ecdh_tmp);
David Benjamin9550c3a2015-08-05 08:50:34 -0400240 ssl3_free_handshake_buffer(s);
241 ssl3_free_handshake_hash(s);
David Benjamin2755a3e2015-04-22 16:17:58 -0400242 OPENSSL_free(s->s3->alpn_selected);
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langleybe2900a2014-12-18 12:09:04 -0800244 OPENSSL_cleanse(s->s3, sizeof *s->s3);
245 OPENSSL_free(s->s3);
246 s->s3 = NULL;
247}
Adam Langley95c29f32014-06-20 12:00:00 -0700248
David Benjaminc0454692015-04-27 00:28:56 -0400249int SSL_session_reused(const SSL *ssl) {
250 return ssl->hit;
251}
252
253int SSL_total_renegotiations(const SSL *ssl) {
254 return ssl->s3->total_renegotiations;
255}
256
257int SSL_num_renegotiations(const SSL *ssl) {
258 return SSL_total_renegotiations(ssl);
259}
260
261int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) {
262 return 0;
263}
264
265int SSL_need_rsa(const SSL *ssl) {
266 return 0;
267}
268
269int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) {
270 return 1;
271}
272
273int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) {
274 return 1;
275}
276
David Benjamin15a3b002015-04-28 00:05:35 -0400277int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
278 DH_free(ctx->cert->dh_tmp);
279 ctx->cert->dh_tmp = DHparams_dup(dh);
280 if (ctx->cert->dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400281 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
David Benjamin15a3b002015-04-28 00:05:35 -0400282 return 0;
283 }
284 return 1;
285}
286
287int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
288 DH_free(ssl->cert->dh_tmp);
289 ssl->cert->dh_tmp = DHparams_dup(dh);
290 if (ssl->cert->dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400291 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
David Benjamin15a3b002015-04-28 00:05:35 -0400292 return 0;
293 }
294 return 1;
295}
296
297int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
298 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400299 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
David Benjamin15a3b002015-04-28 00:05:35 -0400300 return 0;
301 }
302 ctx->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
303 return 1;
304}
305
306int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
307 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400308 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
David Benjamin15a3b002015-04-28 00:05:35 -0400309 return 0;
310 }
311 ssl->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
312 return 1;
313}
314
David Benjaminc2807582015-04-28 00:19:50 -0400315int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
316 ctx->tlsext_channel_id_enabled = 1;
317 return 1;
318}
319
320int SSL_enable_tls_channel_id(SSL *ssl) {
321 ssl->tlsext_channel_id_enabled = 1;
322 return 1;
323}
324
325int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
326 ctx->tlsext_channel_id_enabled = 1;
327 if (EVP_PKEY_id(private_key) != EVP_PKEY_EC ||
328 EVP_PKEY_bits(private_key) != 256) {
David Benjamin3570d732015-06-29 00:28:17 -0400329 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
David Benjaminc2807582015-04-28 00:19:50 -0400330 return 0;
331 }
332 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin9a10f8f2015-05-05 22:22:40 -0400333 ctx->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
David Benjaminc2807582015-04-28 00:19:50 -0400334 return 1;
335}
336
337int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
338 ssl->tlsext_channel_id_enabled = 1;
339 if (EVP_PKEY_id(private_key) != EVP_PKEY_EC ||
340 EVP_PKEY_bits(private_key) != 256) {
David Benjamin3570d732015-06-29 00:28:17 -0400341 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
David Benjaminc2807582015-04-28 00:19:50 -0400342 return 0;
343 }
344 EVP_PKEY_free(ssl->tlsext_channel_id_private);
David Benjamin9a10f8f2015-05-05 22:22:40 -0400345 ssl->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
David Benjaminc2807582015-04-28 00:19:50 -0400346 return 1;
347}
348
349size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
350 if (!ssl->s3->tlsext_channel_id_valid) {
351 return 0;
352 }
353 memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
354 return 64;
355}
356
David Benjamin7133d422015-04-28 00:43:39 -0400357int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
358 OPENSSL_free(ssl->tlsext_hostname);
359 ssl->tlsext_hostname = NULL;
360
361 if (name == NULL) {
362 return 1;
363 }
364 if (strlen(name) > TLSEXT_MAXLEN_host_name) {
David Benjamin3570d732015-06-29 00:28:17 -0400365 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
David Benjamin7133d422015-04-28 00:43:39 -0400366 return 0;
367 }
368 ssl->tlsext_hostname = BUF_strdup(name);
369 if (ssl->tlsext_hostname == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400370 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin7133d422015-04-28 00:43:39 -0400371 return 0;
372 }
373 return 1;
374}
375
David Benjamin75910642015-08-09 10:42:33 -0400376size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
377 if (ssl->server || !ssl->s3->tmp.cert_req) {
378 *out_types = NULL;
379 return 0;
380 }
381 *out_types = ssl->s3->tmp.certificate_types;
382 return ssl->s3->tmp.num_certificate_types;
383}
384
David Benjaminfdb4cdd2015-08-09 11:13:23 -0400385int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
386 return tls1_set_curves(&ctx->tlsext_ellipticcurvelist,
387 &ctx->tlsext_ellipticcurvelist_length, curves,
388 curves_len);
389}
390
391int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
392 return tls1_set_curves(&ssl->tlsext_ellipticcurvelist,
393 &ssl->tlsext_ellipticcurvelist_length, curves,
394 curves_len);
395}
396
David Benjamin59015c32015-04-26 13:13:55 -0400397int SSL_CTX_set_tlsext_servername_callback(
398 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
399 ctx->tlsext_servername_callback = callback;
400 return 1;
401}
Adam Langley95c29f32014-06-20 12:00:00 -0700402
David Benjamin7133d422015-04-28 00:43:39 -0400403int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
404 ctx->tlsext_servername_arg = arg;
405 return 1;
406}
407
David Benjamin6cacac02015-06-16 13:29:54 -0400408int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
409 if (out == NULL) {
410 return 48;
411 }
412 if (len != 48) {
David Benjamin3570d732015-06-29 00:28:17 -0400413 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
David Benjamin6cacac02015-06-16 13:29:54 -0400414 return 0;
415 }
416 uint8_t *out_bytes = out;
417 memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
418 memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
419 memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
420 return 1;
421}
422
423int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
424 if (in == NULL) {
425 return 48;
426 }
427 if (len != 48) {
David Benjamin3570d732015-06-29 00:28:17 -0400428 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
David Benjamin6cacac02015-06-16 13:29:54 -0400429 return 0;
430 }
431 const uint8_t *in_bytes = in;
432 memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
433 memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
434 memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
435 return 1;
436}
437
David Benjamin59015c32015-04-26 13:13:55 -0400438int SSL_CTX_set_tlsext_ticket_key_cb(
439 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
440 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
441 int encrypt)) {
442 ctx->tlsext_ticket_key_cb = callback;
Adam Langleybe2900a2014-12-18 12:09:04 -0800443 return 1;
444}
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleybe2900a2014-12-18 12:09:04 -0800446struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(SSL *s) {
447 if (s->cipher_list != NULL) {
448 return s->cipher_list;
449 }
Adam Langley858a88d2014-06-20 12:00:00 -0700450
Adam Langleybe2900a2014-12-18 12:09:04 -0800451 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
452 s->ctx->cipher_list_tls11 != NULL) {
453 return s->ctx->cipher_list_tls11;
454 }
Adam Langley858a88d2014-06-20 12:00:00 -0700455
Adam Langleycef75832015-09-03 14:51:12 -0700456 if (s->version >= TLS1_VERSION && s->ctx != NULL &&
457 s->ctx->cipher_list_tls10 != NULL) {
458 return s->ctx->cipher_list_tls10;
459 }
460
Adam Langleybe2900a2014-12-18 12:09:04 -0800461 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
462 return s->ctx->cipher_list;
463 }
Adam Langley858a88d2014-06-20 12:00:00 -0700464
Adam Langleybe2900a2014-12-18 12:09:04 -0800465 return NULL;
466}
Adam Langley858a88d2014-06-20 12:00:00 -0700467
Adam Langleybe2900a2014-12-18 12:09:04 -0800468const SSL_CIPHER *ssl3_choose_cipher(
David Benjamin60da0cd2015-05-03 15:21:28 -0400469 SSL *s, STACK_OF(SSL_CIPHER) *clnt,
Adam Langleybe2900a2014-12-18 12:09:04 -0800470 struct ssl_cipher_preference_list_st *server_pref) {
471 const SSL_CIPHER *c, *ret = NULL;
472 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
473 size_t i;
474 int ok;
475 size_t cipher_index;
David Benjamin107db582015-04-08 00:41:59 -0400476 uint32_t alg_k, alg_a, mask_k, mask_a;
Adam Langleybe2900a2014-12-18 12:09:04 -0800477 /* in_group_flags will either be NULL, or will point to an array of bytes
478 * which indicate equal-preference groups in the |prio| stack. See the
479 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
480 * struct. */
481 const uint8_t *in_group_flags;
482 /* group_min contains the minimal index so far found in a group, or -1 if no
483 * such value exists yet. */
484 int group_min = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langleybe2900a2014-12-18 12:09:04 -0800486 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
487 prio = srvr;
488 in_group_flags = server_pref->in_group_flags;
489 allow = clnt;
490 } else {
491 prio = clnt;
492 in_group_flags = NULL;
493 allow = srvr;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleybe2900a2014-12-18 12:09:04 -0800496 ssl_get_compatible_server_ciphers(s, &mask_k, &mask_a);
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleybe2900a2014-12-18 12:09:04 -0800498 for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
499 c = sk_SSL_CIPHER_value(prio, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleybe2900a2014-12-18 12:09:04 -0800501 ok = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700502
Adam Langleybe2900a2014-12-18 12:09:04 -0800503 /* Skip TLS v1.2 only ciphersuites if not supported */
504 if ((c->algorithm_ssl & SSL_TLSV1_2) && !SSL_USE_TLS1_2_CIPHERS(s)) {
505 ok = 0;
506 }
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langleybe2900a2014-12-18 12:09:04 -0800508 alg_k = c->algorithm_mkey;
509 alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langleybe2900a2014-12-18 12:09:04 -0800511 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleybe2900a2014-12-18 12:09:04 -0800513 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
514 if (in_group_flags != NULL && in_group_flags[i] == 1) {
515 /* This element of |prio| is in a group. Update the minimum index found
516 * so far and continue looking. */
517 if (group_min == -1 || (size_t)group_min > cipher_index) {
518 group_min = cipher_index;
519 }
520 } else {
521 if (group_min != -1 && (size_t)group_min < cipher_index) {
522 cipher_index = group_min;
523 }
524 ret = sk_SSL_CIPHER_value(allow, cipher_index);
525 break;
526 }
527 }
Adam Langley858a88d2014-06-20 12:00:00 -0700528
Adam Langleybe2900a2014-12-18 12:09:04 -0800529 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
530 /* We are about to leave a group, but we found a match in it, so that's
531 * our answer. */
532 ret = sk_SSL_CIPHER_value(allow, group_min);
533 break;
534 }
535 }
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleybe2900a2014-12-18 12:09:04 -0800537 return ret;
538}
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleybe2900a2014-12-18 12:09:04 -0800540int ssl3_get_req_cert_type(SSL *s, uint8_t *p) {
541 int ret = 0;
542 const uint8_t *sig;
543 size_t i, siglen;
544 int have_rsa_sign = 0;
545 int have_ecdsa_sign = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleybe2900a2014-12-18 12:09:04 -0800547 /* get configured sigalgs */
548 siglen = tls12_get_psigalgs(s, &sig);
549 for (i = 0; i < siglen; i += 2, sig += 2) {
550 switch (sig[1]) {
551 case TLSEXT_signature_rsa:
552 have_rsa_sign = 1;
553 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700554
Adam Langleybe2900a2014-12-18 12:09:04 -0800555 case TLSEXT_signature_ecdsa:
556 have_ecdsa_sign = 1;
557 break;
558 }
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langleybe2900a2014-12-18 12:09:04 -0800561 if (have_rsa_sign) {
562 p[ret++] = SSL3_CT_RSA_SIGN;
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleybe2900a2014-12-18 12:09:04 -0800565 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
David Benjamin7061e282015-03-19 11:10:48 -0400566 * check for SSL_kECDH or SSL_kECDHE. */
Adam Langleybe2900a2014-12-18 12:09:04 -0800567 if (s->version >= TLS1_VERSION && have_ecdsa_sign) {
568 p[ret++] = TLS_CT_ECDSA_SIGN;
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleybe2900a2014-12-18 12:09:04 -0800571 return ret;
572}
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleybe2900a2014-12-18 12:09:04 -0800574/* If we are using default SHA1+MD5 algorithms switch to new SHA256 PRF and
575 * handshake macs if required. */
David Benjamin5055c762015-08-04 09:24:14 -0400576uint32_t ssl_get_algorithm_prf(SSL *s) {
David Benjaminb0883312015-08-06 09:54:13 -0400577 uint32_t algorithm_prf = s->s3->tmp.new_cipher->algorithm_prf;
Adam Langleybe2900a2014-12-18 12:09:04 -0800578 if (s->enc_method->enc_flags & SSL_ENC_FLAG_SHA256_PRF &&
David Benjaminb0883312015-08-06 09:54:13 -0400579 algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjamin9a980ab2015-05-27 00:36:11 -0400580 return SSL_HANDSHAKE_MAC_SHA256;
Adam Langleybe2900a2014-12-18 12:09:04 -0800581 }
David Benjaminb0883312015-08-06 09:54:13 -0400582 return algorithm_prf;
Adam Langleybe2900a2014-12-18 12:09:04 -0800583}