blob: 7125215bd8204be51d8b6a2e25b87d78a98d7c36 [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 Benjamin39482a12014-07-20 13:30:15 -0400149#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400151#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjamin676d1e72014-07-08 14:34:10 -0400153#include <openssl/buf.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154#include <openssl/dh.h>
David Benjaminb0883312015-08-06 09:54:13 -0400155#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400156#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/md5.h>
158#include <openssl/mem.h>
159#include <openssl/obj.h>
160
David Benjamin2ee94aa2015-04-07 22:38:30 -0400161#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700162
Adam Langleybe2900a2014-12-18 12:09:04 -0800163
David Benjamin338fcaf2014-12-11 01:20:52 -0500164const SSL3_ENC_METHOD SSLv3_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500165 ssl3_prf,
David Benjamin044abb02014-12-23 10:57:17 -0500166 tls1_setup_key_block,
David Benjamin41ac9792014-12-23 10:41:06 -0500167 tls1_generate_master_secret,
David Benjamin044abb02014-12-23 10:57:17 -0500168 tls1_change_cipher_state,
Adam Langleybe2900a2014-12-18 12:09:04 -0800169 ssl3_final_finish_mac,
Adam Langleybe2900a2014-12-18 12:09:04 -0800170 ssl3_cert_verify_mac,
171 SSL3_MD_CLIENT_FINISHED_CONST, 4,
172 SSL3_MD_SERVER_FINISHED_CONST, 4,
173 ssl3_alert_code,
David Benjamind6e95ee2015-05-03 15:34:35 -0400174 tls1_export_keying_material,
Adam Langleybe2900a2014-12-18 12:09:04 -0800175 0,
Adam Langleybe2900a2014-12-18 12:09:04 -0800176};
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjamina1c90a52015-05-30 17:03:14 -0400178int ssl3_supports_cipher(const SSL_CIPHER *cipher) {
179 return 1;
Adam Langleybe2900a2014-12-18 12:09:04 -0800180}
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjaminfbdfefb2015-02-16 19:33:53 -0500182int ssl3_set_handshake_header(SSL *s, int htype, unsigned long len) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800183 uint8_t *p = (uint8_t *)s->init_buf->data;
184 *(p++) = htype;
185 l2n3(len, p);
186 s->init_num = (int)len + SSL3_HM_HEADER_LENGTH;
187 s->init_off = 0;
David Benjamine4824e82014-12-14 19:44:34 -0500188
Adam Langleybe2900a2014-12-18 12:09:04 -0800189 /* Add the message to the handshake hash. */
David Benjamin9550c3a2015-08-05 08:50:34 -0400190 return ssl3_update_handshake_hash(s, (uint8_t *)s->init_buf->data,
191 s->init_num);
Adam Langleybe2900a2014-12-18 12:09:04 -0800192}
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langleybe2900a2014-12-18 12:09:04 -0800194int ssl3_handshake_write(SSL *s) { return ssl3_do_write(s, SSL3_RT_HANDSHAKE); }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langleybe2900a2014-12-18 12:09:04 -0800196int ssl3_new(SSL *s) {
197 SSL3_STATE *s3;
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langleybe2900a2014-12-18 12:09:04 -0800199 s3 = OPENSSL_malloc(sizeof *s3);
200 if (s3 == NULL) {
201 goto err;
202 }
203 memset(s3, 0, sizeof *s3);
Adam Langleybe2900a2014-12-18 12:09:04 -0800204
David Benjaminb0883312015-08-06 09:54:13 -0400205 EVP_MD_CTX_init(&s3->handshake_hash);
206 EVP_MD_CTX_init(&s3->handshake_md5);
207
Adam Langleybe2900a2014-12-18 12:09:04 -0800208 s->s3 = s3;
Adam Langleybe2900a2014-12-18 12:09:04 -0800209
David Benjamin62fd1622015-01-11 13:30:01 -0500210 /* Set the version to the highest supported version for TLS. This controls the
211 * initial state of |s->enc_method| and what the API reports as the version
212 * prior to negotiation.
213 *
214 * TODO(davidben): This is fragile and confusing. */
215 s->version = TLS1_2_VERSION;
Adam Langleybe2900a2014-12-18 12:09:04 -0800216 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700217err:
Adam Langleybe2900a2014-12-18 12:09:04 -0800218 return 0;
219}
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langleybe2900a2014-12-18 12:09:04 -0800221void ssl3_free(SSL *s) {
David Benjamin62fd1622015-01-11 13:30:01 -0500222 if (s == NULL || s->s3 == NULL) {
Adam Langleybe2900a2014-12-18 12:09:04 -0800223 return;
224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langleybe2900a2014-12-18 12:09:04 -0800226 ssl3_cleanup_key_block(s);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400227 ssl_read_buffer_clear(s);
228 ssl_write_buffer_clear(s);
David Benjamin2755a3e2015-04-22 16:17:58 -0400229 DH_free(s->s3->tmp.dh);
230 EC_KEY_free(s->s3->tmp.ecdh);
David Benjamin4b755cb2014-12-12 03:58:07 -0500231
David Benjamin2755a3e2015-04-22 16:17:58 -0400232 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
233 OPENSSL_free(s->s3->tmp.certificate_types);
David Benjamin2755a3e2015-04-22 16:17:58 -0400234 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
235 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
David Benjamin65055672015-08-22 12:49:30 -0400236 DH_free(s->s3->tmp.peer_dh_tmp);
237 EC_KEY_free(s->s3->tmp.peer_ecdh_tmp);
David Benjamin9550c3a2015-08-05 08:50:34 -0400238 ssl3_free_handshake_buffer(s);
239 ssl3_free_handshake_hash(s);
David Benjamin2755a3e2015-04-22 16:17:58 -0400240 OPENSSL_free(s->s3->alpn_selected);
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleybe2900a2014-12-18 12:09:04 -0800242 OPENSSL_cleanse(s->s3, sizeof *s->s3);
243 OPENSSL_free(s->s3);
244 s->s3 = NULL;
245}
Adam Langley95c29f32014-06-20 12:00:00 -0700246
David Benjaminc0454692015-04-27 00:28:56 -0400247int SSL_session_reused(const SSL *ssl) {
248 return ssl->hit;
249}
250
251int SSL_total_renegotiations(const SSL *ssl) {
252 return ssl->s3->total_renegotiations;
253}
254
255int SSL_num_renegotiations(const SSL *ssl) {
256 return SSL_total_renegotiations(ssl);
257}
258
259int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) {
260 return 0;
261}
262
263int SSL_need_rsa(const SSL *ssl) {
264 return 0;
265}
266
267int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) {
268 return 1;
269}
270
271int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) {
272 return 1;
273}
274
David Benjamin15a3b002015-04-28 00:05:35 -0400275int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
276 DH_free(ctx->cert->dh_tmp);
277 ctx->cert->dh_tmp = DHparams_dup(dh);
278 if (ctx->cert->dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400279 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
David Benjamin15a3b002015-04-28 00:05:35 -0400280 return 0;
281 }
282 return 1;
283}
284
285int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
286 DH_free(ssl->cert->dh_tmp);
287 ssl->cert->dh_tmp = DHparams_dup(dh);
288 if (ssl->cert->dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400289 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
David Benjamin15a3b002015-04-28 00:05:35 -0400290 return 0;
291 }
292 return 1;
293}
294
295int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
296 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400297 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
David Benjamin15a3b002015-04-28 00:05:35 -0400298 return 0;
299 }
300 ctx->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
301 return 1;
302}
303
304int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
305 if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400306 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
David Benjamin15a3b002015-04-28 00:05:35 -0400307 return 0;
308 }
309 ssl->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
310 return 1;
311}
312
David Benjaminc2807582015-04-28 00:19:50 -0400313int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
314 ctx->tlsext_channel_id_enabled = 1;
315 return 1;
316}
317
318int SSL_enable_tls_channel_id(SSL *ssl) {
319 ssl->tlsext_channel_id_enabled = 1;
320 return 1;
321}
322
323int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
324 ctx->tlsext_channel_id_enabled = 1;
325 if (EVP_PKEY_id(private_key) != EVP_PKEY_EC ||
326 EVP_PKEY_bits(private_key) != 256) {
David Benjamin3570d732015-06-29 00:28:17 -0400327 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
David Benjaminc2807582015-04-28 00:19:50 -0400328 return 0;
329 }
330 EVP_PKEY_free(ctx->tlsext_channel_id_private);
David Benjamin9a10f8f2015-05-05 22:22:40 -0400331 ctx->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
David Benjaminc2807582015-04-28 00:19:50 -0400332 return 1;
333}
334
335int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
336 ssl->tlsext_channel_id_enabled = 1;
337 if (EVP_PKEY_id(private_key) != EVP_PKEY_EC ||
338 EVP_PKEY_bits(private_key) != 256) {
David Benjamin3570d732015-06-29 00:28:17 -0400339 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
David Benjaminc2807582015-04-28 00:19:50 -0400340 return 0;
341 }
342 EVP_PKEY_free(ssl->tlsext_channel_id_private);
David Benjamin9a10f8f2015-05-05 22:22:40 -0400343 ssl->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
David Benjaminc2807582015-04-28 00:19:50 -0400344 return 1;
345}
346
347size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
348 if (!ssl->s3->tlsext_channel_id_valid) {
349 return 0;
350 }
351 memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
352 return 64;
353}
354
David Benjamin7133d422015-04-28 00:43:39 -0400355int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
356 OPENSSL_free(ssl->tlsext_hostname);
357 ssl->tlsext_hostname = NULL;
358
359 if (name == NULL) {
360 return 1;
361 }
362 if (strlen(name) > TLSEXT_MAXLEN_host_name) {
David Benjamin3570d732015-06-29 00:28:17 -0400363 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
David Benjamin7133d422015-04-28 00:43:39 -0400364 return 0;
365 }
366 ssl->tlsext_hostname = BUF_strdup(name);
367 if (ssl->tlsext_hostname == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400368 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin7133d422015-04-28 00:43:39 -0400369 return 0;
370 }
371 return 1;
372}
373
David Benjamin75910642015-08-09 10:42:33 -0400374size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
375 if (ssl->server || !ssl->s3->tmp.cert_req) {
376 *out_types = NULL;
377 return 0;
378 }
379 *out_types = ssl->s3->tmp.certificate_types;
380 return ssl->s3->tmp.num_certificate_types;
381}
382
David Benjaminfdb4cdd2015-08-09 11:13:23 -0400383int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
384 return tls1_set_curves(&ctx->tlsext_ellipticcurvelist,
385 &ctx->tlsext_ellipticcurvelist_length, curves,
386 curves_len);
387}
388
389int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
390 return tls1_set_curves(&ssl->tlsext_ellipticcurvelist,
391 &ssl->tlsext_ellipticcurvelist_length, curves,
392 curves_len);
393}
394
David Benjamin59015c32015-04-26 13:13:55 -0400395int SSL_CTX_set_tlsext_servername_callback(
396 SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
397 ctx->tlsext_servername_callback = callback;
398 return 1;
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
David Benjamin7133d422015-04-28 00:43:39 -0400401int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
402 ctx->tlsext_servername_arg = arg;
403 return 1;
404}
405
David Benjamin6cacac02015-06-16 13:29:54 -0400406int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
407 if (out == NULL) {
408 return 48;
409 }
410 if (len != 48) {
David Benjamin3570d732015-06-29 00:28:17 -0400411 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
David Benjamin6cacac02015-06-16 13:29:54 -0400412 return 0;
413 }
414 uint8_t *out_bytes = out;
415 memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
416 memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
417 memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
418 return 1;
419}
420
421int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
422 if (in == NULL) {
423 return 48;
424 }
425 if (len != 48) {
David Benjamin3570d732015-06-29 00:28:17 -0400426 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
David Benjamin6cacac02015-06-16 13:29:54 -0400427 return 0;
428 }
429 const uint8_t *in_bytes = in;
430 memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
431 memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
432 memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
433 return 1;
434}
435
David Benjamin59015c32015-04-26 13:13:55 -0400436int SSL_CTX_set_tlsext_ticket_key_cb(
437 SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
438 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
439 int encrypt)) {
440 ctx->tlsext_ticket_key_cb = callback;
Adam Langleybe2900a2014-12-18 12:09:04 -0800441 return 1;
442}
Adam Langley95c29f32014-06-20 12:00:00 -0700443
Adam Langleybe2900a2014-12-18 12:09:04 -0800444struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(SSL *s) {
445 if (s->cipher_list != NULL) {
446 return s->cipher_list;
447 }
Adam Langley858a88d2014-06-20 12:00:00 -0700448
Adam Langleybe2900a2014-12-18 12:09:04 -0800449 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
450 s->ctx->cipher_list_tls11 != NULL) {
451 return s->ctx->cipher_list_tls11;
452 }
Adam Langley858a88d2014-06-20 12:00:00 -0700453
Adam Langleycef75832015-09-03 14:51:12 -0700454 if (s->version >= TLS1_VERSION && s->ctx != NULL &&
455 s->ctx->cipher_list_tls10 != NULL) {
456 return s->ctx->cipher_list_tls10;
457 }
458
Adam Langleybe2900a2014-12-18 12:09:04 -0800459 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
460 return s->ctx->cipher_list;
461 }
Adam Langley858a88d2014-06-20 12:00:00 -0700462
Adam Langleybe2900a2014-12-18 12:09:04 -0800463 return NULL;
464}
Adam Langley858a88d2014-06-20 12:00:00 -0700465
Adam Langleybe2900a2014-12-18 12:09:04 -0800466const SSL_CIPHER *ssl3_choose_cipher(
David Benjamin60da0cd2015-05-03 15:21:28 -0400467 SSL *s, STACK_OF(SSL_CIPHER) *clnt,
Adam Langleybe2900a2014-12-18 12:09:04 -0800468 struct ssl_cipher_preference_list_st *server_pref) {
469 const SSL_CIPHER *c, *ret = NULL;
470 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
471 size_t i;
472 int ok;
473 size_t cipher_index;
David Benjamin107db582015-04-08 00:41:59 -0400474 uint32_t alg_k, alg_a, mask_k, mask_a;
Adam Langleybe2900a2014-12-18 12:09:04 -0800475 /* in_group_flags will either be NULL, or will point to an array of bytes
476 * which indicate equal-preference groups in the |prio| stack. See the
477 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
478 * struct. */
479 const uint8_t *in_group_flags;
480 /* group_min contains the minimal index so far found in a group, or -1 if no
481 * such value exists yet. */
482 int group_min = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleybe2900a2014-12-18 12:09:04 -0800484 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
485 prio = srvr;
486 in_group_flags = server_pref->in_group_flags;
487 allow = clnt;
488 } else {
489 prio = clnt;
490 in_group_flags = NULL;
491 allow = srvr;
492 }
Adam Langley95c29f32014-06-20 12:00:00 -0700493
Adam Langleybe2900a2014-12-18 12:09:04 -0800494 ssl_get_compatible_server_ciphers(s, &mask_k, &mask_a);
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleybe2900a2014-12-18 12:09:04 -0800496 for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
497 c = sk_SSL_CIPHER_value(prio, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700498
Adam Langleybe2900a2014-12-18 12:09:04 -0800499 ok = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700500
Adam Langleybe2900a2014-12-18 12:09:04 -0800501 /* Skip TLS v1.2 only ciphersuites if not supported */
502 if ((c->algorithm_ssl & SSL_TLSV1_2) && !SSL_USE_TLS1_2_CIPHERS(s)) {
503 ok = 0;
504 }
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langleybe2900a2014-12-18 12:09:04 -0800506 alg_k = c->algorithm_mkey;
507 alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langleybe2900a2014-12-18 12:09:04 -0800509 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langleybe2900a2014-12-18 12:09:04 -0800511 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
512 if (in_group_flags != NULL && in_group_flags[i] == 1) {
513 /* This element of |prio| is in a group. Update the minimum index found
514 * so far and continue looking. */
515 if (group_min == -1 || (size_t)group_min > cipher_index) {
516 group_min = cipher_index;
517 }
518 } else {
519 if (group_min != -1 && (size_t)group_min < cipher_index) {
520 cipher_index = group_min;
521 }
522 ret = sk_SSL_CIPHER_value(allow, cipher_index);
523 break;
524 }
525 }
Adam Langley858a88d2014-06-20 12:00:00 -0700526
Adam Langleybe2900a2014-12-18 12:09:04 -0800527 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
528 /* We are about to leave a group, but we found a match in it, so that's
529 * our answer. */
530 ret = sk_SSL_CIPHER_value(allow, group_min);
531 break;
532 }
533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleybe2900a2014-12-18 12:09:04 -0800535 return ret;
536}
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleybe2900a2014-12-18 12:09:04 -0800538int ssl3_get_req_cert_type(SSL *s, uint8_t *p) {
539 int ret = 0;
540 const uint8_t *sig;
541 size_t i, siglen;
542 int have_rsa_sign = 0;
543 int have_ecdsa_sign = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langleybe2900a2014-12-18 12:09:04 -0800545 /* get configured sigalgs */
546 siglen = tls12_get_psigalgs(s, &sig);
547 for (i = 0; i < siglen; i += 2, sig += 2) {
548 switch (sig[1]) {
549 case TLSEXT_signature_rsa:
550 have_rsa_sign = 1;
551 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleybe2900a2014-12-18 12:09:04 -0800553 case TLSEXT_signature_ecdsa:
554 have_ecdsa_sign = 1;
555 break;
556 }
557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleybe2900a2014-12-18 12:09:04 -0800559 if (have_rsa_sign) {
560 p[ret++] = SSL3_CT_RSA_SIGN;
561 }
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langleybe2900a2014-12-18 12:09:04 -0800563 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
David Benjamin7061e282015-03-19 11:10:48 -0400564 * check for SSL_kECDH or SSL_kECDHE. */
Adam Langleybe2900a2014-12-18 12:09:04 -0800565 if (s->version >= TLS1_VERSION && have_ecdsa_sign) {
566 p[ret++] = TLS_CT_ECDSA_SIGN;
567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleybe2900a2014-12-18 12:09:04 -0800569 return ret;
570}
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleybe2900a2014-12-18 12:09:04 -0800572/* If we are using default SHA1+MD5 algorithms switch to new SHA256 PRF and
573 * handshake macs if required. */
David Benjamin5055c762015-08-04 09:24:14 -0400574uint32_t ssl_get_algorithm_prf(SSL *s) {
David Benjaminb0883312015-08-06 09:54:13 -0400575 uint32_t algorithm_prf = s->s3->tmp.new_cipher->algorithm_prf;
Adam Langleybe2900a2014-12-18 12:09:04 -0800576 if (s->enc_method->enc_flags & SSL_ENC_FLAG_SHA256_PRF &&
David Benjaminb0883312015-08-06 09:54:13 -0400577 algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjamin9a980ab2015-05-27 00:36:11 -0400578 return SSL_HANDSHAKE_MAC_SHA256;
Adam Langleybe2900a2014-12-18 12:09:04 -0800579 }
David Benjaminb0883312015-08-06 09:54:13 -0400580 return algorithm_prf;
Adam Langleybe2900a2014-12-18 12:09:04 -0800581}