import libssl (LibreSSL 2.5.4)
[unleashed.git] / lib / libssl / ssl_ciph.c
blob49af292d6c025bbae9b14e06db83e0f212a6ea36
1 /* $OpenBSD: ssl_ciph.c,v 1.96 2017/03/10 16:03:27 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * ECC cipher suite support in OpenSSL originally developed by
114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
116 /* ====================================================================
117 * Copyright 2005 Nokia. All rights reserved.
119 * The portions of the attached software ("Contribution") is developed by
120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121 * license.
123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125 * support (see RFC 4279) to OpenSSL.
127 * No patent licenses or other rights except those expressly stated in
128 * the OpenSSL open source license shall be deemed granted or received
129 * expressly, by implication, estoppel, or otherwise.
131 * No assurances are provided by Nokia that the Contribution does not
132 * infringe the patent or other intellectual property rights of any third
133 * party or that the license provides you with all the necessary rights
134 * to make use of the Contribution.
136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140 * OTHERWISE.
143 #include <stdio.h>
145 #include <openssl/objects.h>
147 #ifndef OPENSSL_NO_ENGINE
148 #include <openssl/engine.h>
149 #endif
151 #include "ssl_locl.h"
153 #define SSL_ENC_DES_IDX 0
154 #define SSL_ENC_3DES_IDX 1
155 #define SSL_ENC_RC4_IDX 2
156 #define SSL_ENC_IDEA_IDX 3
157 #define SSL_ENC_NULL_IDX 4
158 #define SSL_ENC_AES128_IDX 5
159 #define SSL_ENC_AES256_IDX 6
160 #define SSL_ENC_CAMELLIA128_IDX 7
161 #define SSL_ENC_CAMELLIA256_IDX 8
162 #define SSL_ENC_GOST89_IDX 9
163 #define SSL_ENC_AES128GCM_IDX 10
164 #define SSL_ENC_AES256GCM_IDX 11
165 #define SSL_ENC_NUM_IDX 12
168 static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX] = {
169 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
172 #define SSL_MD_MD5_IDX 0
173 #define SSL_MD_SHA1_IDX 1
174 #define SSL_MD_GOST94_IDX 2
175 #define SSL_MD_GOST89MAC_IDX 3
176 #define SSL_MD_SHA256_IDX 4
177 #define SSL_MD_SHA384_IDX 5
178 #define SSL_MD_STREEBOG256_IDX 6
179 /*Constant SSL_MAX_DIGEST equal to size of digests array should be
180 * defined in the
181 * ssl_locl.h */
182 #define SSL_MD_NUM_IDX SSL_MAX_DIGEST
183 static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = {
184 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
187 static int ssl_mac_pkey_id[SSL_MD_NUM_IDX] = {
188 EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_GOSTIMIT,
189 EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC,
192 static int ssl_mac_secret_size[SSL_MD_NUM_IDX] = {
193 0, 0, 0, 0, 0, 0, 0,
196 #define CIPHER_ADD 1
197 #define CIPHER_KILL 2
198 #define CIPHER_DEL 3
199 #define CIPHER_ORD 4
200 #define CIPHER_SPECIAL 5
202 typedef struct cipher_order_st {
203 const SSL_CIPHER *cipher;
204 int active;
205 int dead;
206 struct cipher_order_st *next, *prev;
207 } CIPHER_ORDER;
209 static const SSL_CIPHER cipher_aliases[] = {
211 /* "ALL" doesn't include eNULL (must be specifically enabled) */
213 .name = SSL_TXT_ALL,
214 .algorithm_enc = ~SSL_eNULL,
217 /* "COMPLEMENTOFALL" */
219 .name = SSL_TXT_CMPALL,
220 .algorithm_enc = SSL_eNULL,
224 * "COMPLEMENTOFDEFAULT"
225 * (does *not* include ciphersuites not found in ALL!)
228 .name = SSL_TXT_CMPDEF,
229 .algorithm_mkey = SSL_kDHE|SSL_kECDHE,
230 .algorithm_auth = SSL_aNULL,
231 .algorithm_enc = ~SSL_eNULL,
235 * key exchange aliases
236 * (some of those using only a single bit here combine multiple key
237 * exchange algs according to the RFCs, e.g. kEDH combines DHE_DSS
238 * and DHE_RSA)
241 .name = SSL_TXT_kRSA,
242 .algorithm_mkey = SSL_kRSA,
245 .name = SSL_TXT_kEDH,
246 .algorithm_mkey = SSL_kDHE,
249 .name = SSL_TXT_DH,
250 .algorithm_mkey = SSL_kDHE,
253 .name = SSL_TXT_kEECDH,
254 .algorithm_mkey = SSL_kECDHE,
257 .name = SSL_TXT_ECDH,
258 .algorithm_mkey = SSL_kECDHE,
261 .name = SSL_TXT_kGOST,
262 .algorithm_mkey = SSL_kGOST,
265 /* server authentication aliases */
267 .name = SSL_TXT_aRSA,
268 .algorithm_auth = SSL_aRSA,
271 .name = SSL_TXT_aDSS,
272 .algorithm_auth = SSL_aDSS,
275 .name = SSL_TXT_DSS,
276 .algorithm_auth = SSL_aDSS,
279 .name = SSL_TXT_aNULL,
280 .algorithm_auth = SSL_aNULL,
283 .name = SSL_TXT_aECDSA,
284 .algorithm_auth = SSL_aECDSA,
287 .name = SSL_TXT_ECDSA,
288 .algorithm_auth = SSL_aECDSA,
291 .name = SSL_TXT_aGOST01,
292 .algorithm_auth = SSL_aGOST01,
295 .name = SSL_TXT_aGOST,
296 .algorithm_auth = SSL_aGOST01,
299 /* aliases combining key exchange and server authentication */
301 .name = SSL_TXT_DHE,
302 .algorithm_mkey = SSL_kDHE,
303 .algorithm_auth = ~SSL_aNULL,
306 .name = SSL_TXT_EDH,
307 .algorithm_mkey = SSL_kDHE,
308 .algorithm_auth = ~SSL_aNULL,
311 .name = SSL_TXT_ECDHE,
312 .algorithm_mkey = SSL_kECDHE,
313 .algorithm_auth = ~SSL_aNULL,
316 .name = SSL_TXT_EECDH,
317 .algorithm_mkey = SSL_kECDHE,
318 .algorithm_auth = ~SSL_aNULL,
321 .name = SSL_TXT_NULL,
322 .algorithm_enc = SSL_eNULL,
325 .name = SSL_TXT_RSA,
326 .algorithm_mkey = SSL_kRSA,
327 .algorithm_auth = SSL_aRSA,
330 .name = SSL_TXT_ADH,
331 .algorithm_mkey = SSL_kDHE,
332 .algorithm_auth = SSL_aNULL,
335 .name = SSL_TXT_AECDH,
336 .algorithm_mkey = SSL_kECDHE,
337 .algorithm_auth = SSL_aNULL,
340 /* symmetric encryption aliases */
342 .name = SSL_TXT_DES,
343 .algorithm_enc = SSL_DES,
346 .name = SSL_TXT_3DES,
347 .algorithm_enc = SSL_3DES,
350 .name = SSL_TXT_RC4,
351 .algorithm_enc = SSL_RC4,
354 .name = SSL_TXT_IDEA,
355 .algorithm_enc = SSL_IDEA,
358 .name = SSL_TXT_eNULL,
359 .algorithm_enc = SSL_eNULL,
362 .name = SSL_TXT_AES128,
363 .algorithm_enc = SSL_AES128|SSL_AES128GCM,
366 .name = SSL_TXT_AES256,
367 .algorithm_enc = SSL_AES256|SSL_AES256GCM,
370 .name = SSL_TXT_AES,
371 .algorithm_enc = SSL_AES,
374 .name = SSL_TXT_AES_GCM,
375 .algorithm_enc = SSL_AES128GCM|SSL_AES256GCM,
378 .name = SSL_TXT_CAMELLIA128,
379 .algorithm_enc = SSL_CAMELLIA128,
382 .name = SSL_TXT_CAMELLIA256,
383 .algorithm_enc = SSL_CAMELLIA256,
386 .name = SSL_TXT_CAMELLIA,
387 .algorithm_enc = SSL_CAMELLIA128|SSL_CAMELLIA256,
390 .name = SSL_TXT_CHACHA20,
391 .algorithm_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD,
394 /* MAC aliases */
396 .name = SSL_TXT_AEAD,
397 .algorithm_mac = SSL_AEAD,
400 .name = SSL_TXT_MD5,
401 .algorithm_mac = SSL_MD5,
404 .name = SSL_TXT_SHA1,
405 .algorithm_mac = SSL_SHA1,
408 .name = SSL_TXT_SHA,
409 .algorithm_mac = SSL_SHA1,
412 .name = SSL_TXT_GOST94,
413 .algorithm_mac = SSL_GOST94,
416 .name = SSL_TXT_GOST89MAC,
417 .algorithm_mac = SSL_GOST89MAC,
420 .name = SSL_TXT_SHA256,
421 .algorithm_mac = SSL_SHA256,
424 .name = SSL_TXT_SHA384,
425 .algorithm_mac = SSL_SHA384,
428 .name = SSL_TXT_STREEBOG256,
429 .algorithm_mac = SSL_STREEBOG256,
432 /* protocol version aliases */
434 .name = SSL_TXT_SSLV3,
435 .algorithm_ssl = SSL_SSLV3,
438 .name = SSL_TXT_TLSV1,
439 .algorithm_ssl = SSL_TLSV1,
442 .name = SSL_TXT_TLSV1_2,
443 .algorithm_ssl = SSL_TLSV1_2,
446 /* strength classes */
448 .name = SSL_TXT_LOW,
449 .algo_strength = SSL_LOW,
452 .name = SSL_TXT_MEDIUM,
453 .algo_strength = SSL_MEDIUM,
456 .name = SSL_TXT_HIGH,
457 .algo_strength = SSL_HIGH,
461 void
462 ssl_load_ciphers(void)
464 ssl_cipher_methods[SSL_ENC_DES_IDX] =
465 EVP_get_cipherbyname(SN_des_cbc);
466 ssl_cipher_methods[SSL_ENC_3DES_IDX] =
467 EVP_get_cipherbyname(SN_des_ede3_cbc);
468 ssl_cipher_methods[SSL_ENC_RC4_IDX] =
469 EVP_get_cipherbyname(SN_rc4);
470 ssl_cipher_methods[SSL_ENC_IDEA_IDX] = NULL;
471 ssl_cipher_methods[SSL_ENC_AES128_IDX] =
472 EVP_get_cipherbyname(SN_aes_128_cbc);
473 ssl_cipher_methods[SSL_ENC_AES256_IDX] =
474 EVP_get_cipherbyname(SN_aes_256_cbc);
475 ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] =
476 EVP_get_cipherbyname(SN_camellia_128_cbc);
477 ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] =
478 EVP_get_cipherbyname(SN_camellia_256_cbc);
479 ssl_cipher_methods[SSL_ENC_GOST89_IDX] =
480 EVP_get_cipherbyname(SN_gost89_cnt);
482 ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] =
483 EVP_get_cipherbyname(SN_aes_128_gcm);
484 ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] =
485 EVP_get_cipherbyname(SN_aes_256_gcm);
487 ssl_digest_methods[SSL_MD_MD5_IDX] =
488 EVP_get_digestbyname(SN_md5);
489 ssl_mac_secret_size[SSL_MD_MD5_IDX] =
490 EVP_MD_size(ssl_digest_methods[SSL_MD_MD5_IDX]);
491 OPENSSL_assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
492 ssl_digest_methods[SSL_MD_SHA1_IDX] =
493 EVP_get_digestbyname(SN_sha1);
494 ssl_mac_secret_size[SSL_MD_SHA1_IDX] =
495 EVP_MD_size(ssl_digest_methods[SSL_MD_SHA1_IDX]);
496 OPENSSL_assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
497 ssl_digest_methods[SSL_MD_GOST94_IDX] =
498 EVP_get_digestbyname(SN_id_GostR3411_94);
499 if (ssl_digest_methods[SSL_MD_GOST94_IDX]) {
500 ssl_mac_secret_size[SSL_MD_GOST94_IDX] =
501 EVP_MD_size(ssl_digest_methods[SSL_MD_GOST94_IDX]);
502 OPENSSL_assert(ssl_mac_secret_size[SSL_MD_GOST94_IDX] >= 0);
504 ssl_digest_methods[SSL_MD_GOST89MAC_IDX] =
505 EVP_get_digestbyname(SN_id_Gost28147_89_MAC);
506 if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]) {
507 ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
510 ssl_digest_methods[SSL_MD_SHA256_IDX] =
511 EVP_get_digestbyname(SN_sha256);
512 ssl_mac_secret_size[SSL_MD_SHA256_IDX] =
513 EVP_MD_size(ssl_digest_methods[SSL_MD_SHA256_IDX]);
514 ssl_digest_methods[SSL_MD_SHA384_IDX] =
515 EVP_get_digestbyname(SN_sha384);
516 ssl_mac_secret_size[SSL_MD_SHA384_IDX] =
517 EVP_MD_size(ssl_digest_methods[SSL_MD_SHA384_IDX]);
518 ssl_digest_methods[SSL_MD_STREEBOG256_IDX] =
519 EVP_get_digestbyname(SN_id_tc26_gost3411_2012_256);
520 ssl_mac_secret_size[SSL_MD_STREEBOG256_IDX] =
521 EVP_MD_size(ssl_digest_methods[SSL_MD_STREEBOG256_IDX]);
525 ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
526 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
528 const SSL_CIPHER *c;
529 int i;
531 c = s->cipher;
532 if (c == NULL)
533 return (0);
536 * This function does not handle EVP_AEAD.
537 * See ssl_cipher_get_aead_evp instead.
539 if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
540 return(0);
542 if ((enc == NULL) || (md == NULL))
543 return (0);
545 switch (c->algorithm_enc) {
546 case SSL_DES:
547 i = SSL_ENC_DES_IDX;
548 break;
549 case SSL_3DES:
550 i = SSL_ENC_3DES_IDX;
551 break;
552 case SSL_RC4:
553 i = SSL_ENC_RC4_IDX;
554 break;
555 case SSL_IDEA:
556 i = SSL_ENC_IDEA_IDX;
557 break;
558 case SSL_eNULL:
559 i = SSL_ENC_NULL_IDX;
560 break;
561 case SSL_AES128:
562 i = SSL_ENC_AES128_IDX;
563 break;
564 case SSL_AES256:
565 i = SSL_ENC_AES256_IDX;
566 break;
567 case SSL_CAMELLIA128:
568 i = SSL_ENC_CAMELLIA128_IDX;
569 break;
570 case SSL_CAMELLIA256:
571 i = SSL_ENC_CAMELLIA256_IDX;
572 break;
573 case SSL_eGOST2814789CNT:
574 i = SSL_ENC_GOST89_IDX;
575 break;
576 case SSL_AES128GCM:
577 i = SSL_ENC_AES128GCM_IDX;
578 break;
579 case SSL_AES256GCM:
580 i = SSL_ENC_AES256GCM_IDX;
581 break;
582 default:
583 i = -1;
584 break;
587 if ((i < 0) || (i >= SSL_ENC_NUM_IDX))
588 *enc = NULL;
589 else {
590 if (i == SSL_ENC_NULL_IDX)
591 *enc = EVP_enc_null();
592 else
593 *enc = ssl_cipher_methods[i];
596 switch (c->algorithm_mac) {
597 case SSL_MD5:
598 i = SSL_MD_MD5_IDX;
599 break;
600 case SSL_SHA1:
601 i = SSL_MD_SHA1_IDX;
602 break;
603 case SSL_SHA256:
604 i = SSL_MD_SHA256_IDX;
605 break;
606 case SSL_SHA384:
607 i = SSL_MD_SHA384_IDX;
608 break;
609 case SSL_GOST94:
610 i = SSL_MD_GOST94_IDX;
611 break;
612 case SSL_GOST89MAC:
613 i = SSL_MD_GOST89MAC_IDX;
614 break;
615 case SSL_STREEBOG256:
616 i = SSL_MD_STREEBOG256_IDX;
617 break;
618 default:
619 i = -1;
620 break;
622 if ((i < 0) || (i >= SSL_MD_NUM_IDX)) {
623 *md = NULL;
625 if (mac_pkey_type != NULL)
626 *mac_pkey_type = NID_undef;
627 if (mac_secret_size != NULL)
628 *mac_secret_size = 0;
629 if (c->algorithm_mac == SSL_AEAD)
630 mac_pkey_type = NULL;
631 } else {
632 *md = ssl_digest_methods[i];
633 if (mac_pkey_type != NULL)
634 *mac_pkey_type = ssl_mac_pkey_id[i];
635 if (mac_secret_size != NULL)
636 *mac_secret_size = ssl_mac_secret_size[i];
639 if ((*enc != NULL) &&
640 (*md != NULL || (EVP_CIPHER_flags(*enc)&EVP_CIPH_FLAG_AEAD_CIPHER)) &&
641 (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
642 const EVP_CIPHER *evp;
644 if (s->ssl_version >> 8 != TLS1_VERSION_MAJOR ||
645 s->ssl_version < TLS1_VERSION)
646 return 1;
648 if (c->algorithm_enc == SSL_RC4 &&
649 c->algorithm_mac == SSL_MD5 &&
650 (evp = EVP_get_cipherbyname("RC4-HMAC-MD5")))
651 *enc = evp, *md = NULL;
652 else if (c->algorithm_enc == SSL_AES128 &&
653 c->algorithm_mac == SSL_SHA1 &&
654 (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
655 *enc = evp, *md = NULL;
656 else if (c->algorithm_enc == SSL_AES256 &&
657 c->algorithm_mac == SSL_SHA1 &&
658 (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
659 *enc = evp, *md = NULL;
660 return (1);
661 } else
662 return (0);
666 * ssl_cipher_get_evp_aead sets aead to point to the correct EVP_AEAD object
667 * for s->cipher. It returns 1 on success and 0 on error.
670 ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
672 const SSL_CIPHER *c = s->cipher;
674 *aead = NULL;
676 if (c == NULL)
677 return 0;
678 if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0)
679 return 0;
681 switch (c->algorithm_enc) {
682 #ifndef OPENSSL_NO_AES
683 case SSL_AES128GCM:
684 *aead = EVP_aead_aes_128_gcm();
685 return 1;
686 case SSL_AES256GCM:
687 *aead = EVP_aead_aes_256_gcm();
688 return 1;
689 #endif
690 case SSL_CHACHA20POLY1305:
691 *aead = EVP_aead_chacha20_poly1305();
692 return 1;
693 case SSL_CHACHA20POLY1305_OLD:
694 *aead = EVP_aead_chacha20_poly1305_old();
695 return 1;
696 default:
697 break;
699 return 0;
703 ssl_get_handshake_evp_md(SSL *s, const EVP_MD **md)
705 *md = NULL;
707 switch (ssl_get_algorithm2(s) & SSL_HANDSHAKE_MAC_MASK) {
708 case SSL_HANDSHAKE_MAC_DEFAULT:
709 *md = EVP_md5_sha1();
710 return 1;
711 case SSL_HANDSHAKE_MAC_GOST94:
712 *md = EVP_gostr341194();
713 return 1;
714 case SSL_HANDSHAKE_MAC_SHA256:
715 *md = EVP_sha256();
716 return 1;
717 case SSL_HANDSHAKE_MAC_SHA384:
718 *md = EVP_sha384();
719 return 1;
720 case SSL_HANDSHAKE_MAC_STREEBOG256:
721 *md = EVP_streebog256();
722 return 1;
723 default:
724 break;
727 return 0;
730 #define ITEM_SEP(a) \
731 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
733 static void
734 ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
735 CIPHER_ORDER **tail)
737 if (curr == *tail)
738 return;
739 if (curr == *head)
740 *head = curr->next;
741 if (curr->prev != NULL)
742 curr->prev->next = curr->next;
743 if (curr->next != NULL)
744 curr->next->prev = curr->prev;
745 (*tail)->next = curr;
746 curr->prev= *tail;
747 curr->next = NULL;
748 *tail = curr;
751 static void
752 ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
753 CIPHER_ORDER **tail)
755 if (curr == *head)
756 return;
757 if (curr == *tail)
758 *tail = curr->prev;
759 if (curr->next != NULL)
760 curr->next->prev = curr->prev;
761 if (curr->prev != NULL)
762 curr->prev->next = curr->next;
763 (*head)->prev = curr;
764 curr->next= *head;
765 curr->prev = NULL;
766 *head = curr;
769 static void
770 ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth,
771 unsigned long *enc, unsigned long *mac, unsigned long *ssl)
773 *mkey = 0;
774 *auth = 0;
775 *enc = 0;
776 *mac = 0;
777 *ssl = 0;
780 * Check for the availability of GOST 34.10 public/private key
781 * algorithms. If they are not available disable the associated
782 * authentication and key exchange algorithms.
784 if (EVP_PKEY_meth_find(NID_id_GostR3410_2001) == NULL) {
785 *auth |= SSL_aGOST01;
786 *mkey |= SSL_kGOST;
789 #ifdef SSL_FORBID_ENULL
790 *enc |= SSL_eNULL;
791 #endif
793 *enc |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES : 0;
794 *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES : 0;
795 *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 : 0;
796 *enc |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA : 0;
797 *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128 : 0;
798 *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256 : 0;
799 *enc |= (ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] == NULL) ? SSL_AES128GCM : 0;
800 *enc |= (ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] == NULL) ? SSL_AES256GCM : 0;
801 *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA128 : 0;
802 *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] == NULL) ? SSL_CAMELLIA256 : 0;
803 *enc |= (ssl_cipher_methods[SSL_ENC_GOST89_IDX] == NULL) ? SSL_eGOST2814789CNT : 0;
805 *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 : 0;
806 *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1 : 0;
807 *mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256 : 0;
808 *mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384 : 0;
809 *mac |= (ssl_digest_methods[SSL_MD_GOST94_IDX] == NULL) ? SSL_GOST94 : 0;
810 *mac |= (ssl_digest_methods[SSL_MD_GOST89MAC_IDX] == NULL) ? SSL_GOST89MAC : 0;
811 *mac |= (ssl_digest_methods[SSL_MD_STREEBOG256_IDX] == NULL) ? SSL_STREEBOG256 : 0;
814 static void
815 ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, int num_of_ciphers,
816 unsigned long disabled_mkey, unsigned long disabled_auth,
817 unsigned long disabled_enc, unsigned long disabled_mac,
818 unsigned long disabled_ssl, CIPHER_ORDER *co_list,
819 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
821 int i, co_list_num;
822 const SSL_CIPHER *c;
825 * We have num_of_ciphers descriptions compiled in, depending on the
826 * method selected (SSLv3, TLSv1, etc). These will later be sorted in
827 * a linked list with at most num entries.
830 /* Get the initial list of ciphers */
831 co_list_num = 0; /* actual count of ciphers */
832 for (i = 0; i < num_of_ciphers; i++) {
833 c = ssl_method->get_cipher(i);
834 /* drop those that use any of that is not available */
835 if ((c != NULL) && c->valid &&
836 !(c->algorithm_mkey & disabled_mkey) &&
837 !(c->algorithm_auth & disabled_auth) &&
838 !(c->algorithm_enc & disabled_enc) &&
839 !(c->algorithm_mac & disabled_mac) &&
840 !(c->algorithm_ssl & disabled_ssl)) {
841 co_list[co_list_num].cipher = c;
842 co_list[co_list_num].next = NULL;
843 co_list[co_list_num].prev = NULL;
844 co_list[co_list_num].active = 0;
845 co_list_num++;
847 if (!sk_push(ca_list,(char *)c)) goto err;
853 * Prepare linked list from list entries
855 if (co_list_num > 0) {
856 co_list[0].prev = NULL;
858 if (co_list_num > 1) {
859 co_list[0].next = &co_list[1];
861 for (i = 1; i < co_list_num - 1; i++) {
862 co_list[i].prev = &co_list[i - 1];
863 co_list[i].next = &co_list[i + 1];
866 co_list[co_list_num - 1].prev =
867 &co_list[co_list_num - 2];
870 co_list[co_list_num - 1].next = NULL;
872 *head_p = &co_list[0];
873 *tail_p = &co_list[co_list_num - 1];
877 static void
878 ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list, int num_of_group_aliases,
879 unsigned long disabled_mkey, unsigned long disabled_auth,
880 unsigned long disabled_enc, unsigned long disabled_mac,
881 unsigned long disabled_ssl, CIPHER_ORDER *head)
883 CIPHER_ORDER *ciph_curr;
884 const SSL_CIPHER **ca_curr;
885 int i;
886 unsigned long mask_mkey = ~disabled_mkey;
887 unsigned long mask_auth = ~disabled_auth;
888 unsigned long mask_enc = ~disabled_enc;
889 unsigned long mask_mac = ~disabled_mac;
890 unsigned long mask_ssl = ~disabled_ssl;
893 * First, add the real ciphers as already collected
895 ciph_curr = head;
896 ca_curr = ca_list;
897 while (ciph_curr != NULL) {
898 *ca_curr = ciph_curr->cipher;
899 ca_curr++;
900 ciph_curr = ciph_curr->next;
904 * Now we add the available ones from the cipher_aliases[] table.
905 * They represent either one or more algorithms, some of which
906 * in any affected category must be supported (set in enabled_mask),
907 * or represent a cipher strength value (will be added in any case because algorithms=0).
909 for (i = 0; i < num_of_group_aliases; i++) {
910 unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
911 unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
912 unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
913 unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
914 unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
916 if (algorithm_mkey)
917 if ((algorithm_mkey & mask_mkey) == 0)
918 continue;
920 if (algorithm_auth)
921 if ((algorithm_auth & mask_auth) == 0)
922 continue;
924 if (algorithm_enc)
925 if ((algorithm_enc & mask_enc) == 0)
926 continue;
928 if (algorithm_mac)
929 if ((algorithm_mac & mask_mac) == 0)
930 continue;
932 if (algorithm_ssl)
933 if ((algorithm_ssl & mask_ssl) == 0)
934 continue;
936 *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
937 ca_curr++;
940 *ca_curr = NULL; /* end of list */
943 static void
944 ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long alg_mkey,
945 unsigned long alg_auth, unsigned long alg_enc, unsigned long alg_mac,
946 unsigned long alg_ssl, unsigned long algo_strength,
947 int rule, int strength_bits, CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
949 CIPHER_ORDER *head, *tail, *curr, *next, *last;
950 const SSL_CIPHER *cp;
951 int reverse = 0;
954 if (rule == CIPHER_DEL)
955 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
957 head = *head_p;
958 tail = *tail_p;
960 if (reverse) {
961 next = tail;
962 last = head;
963 } else {
964 next = head;
965 last = tail;
968 curr = NULL;
969 for (;;) {
970 if (curr == last)
971 break;
972 curr = next;
973 next = reverse ? curr->prev : curr->next;
975 cp = curr->cipher;
978 * Selection criteria is either the value of strength_bits
979 * or the algorithms used.
981 if (strength_bits >= 0) {
982 if (strength_bits != cp->strength_bits)
983 continue;
984 } else {
986 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
987 continue;
988 if (alg_auth && !(alg_auth & cp->algorithm_auth))
989 continue;
990 if (alg_enc && !(alg_enc & cp->algorithm_enc))
991 continue;
992 if (alg_mac && !(alg_mac & cp->algorithm_mac))
993 continue;
994 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
995 continue;
996 if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
997 continue;
1001 /* add the cipher if it has not been added yet. */
1002 if (rule == CIPHER_ADD) {
1003 /* reverse == 0 */
1004 if (!curr->active) {
1005 ll_append_tail(&head, curr, &tail);
1006 curr->active = 1;
1009 /* Move the added cipher to this location */
1010 else if (rule == CIPHER_ORD) {
1011 /* reverse == 0 */
1012 if (curr->active) {
1013 ll_append_tail(&head, curr, &tail);
1015 } else if (rule == CIPHER_DEL) {
1016 /* reverse == 1 */
1017 if (curr->active) {
1018 /* most recently deleted ciphersuites get best positions
1019 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
1020 * works in reverse to maintain the order) */
1021 ll_append_head(&head, curr, &tail);
1022 curr->active = 0;
1024 } else if (rule == CIPHER_KILL) {
1025 /* reverse == 0 */
1026 if (head == curr)
1027 head = curr->next;
1028 else
1029 curr->prev->next = curr->next;
1030 if (tail == curr)
1031 tail = curr->prev;
1032 curr->active = 0;
1033 if (curr->next != NULL)
1034 curr->next->prev = curr->prev;
1035 if (curr->prev != NULL)
1036 curr->prev->next = curr->next;
1037 curr->next = NULL;
1038 curr->prev = NULL;
1042 *head_p = head;
1043 *tail_p = tail;
1046 static int
1047 ssl_cipher_strength_sort(CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
1049 int max_strength_bits, i, *number_uses;
1050 CIPHER_ORDER *curr;
1053 * This routine sorts the ciphers with descending strength. The sorting
1054 * must keep the pre-sorted sequence, so we apply the normal sorting
1055 * routine as '+' movement to the end of the list.
1057 max_strength_bits = 0;
1058 curr = *head_p;
1059 while (curr != NULL) {
1060 if (curr->active &&
1061 (curr->cipher->strength_bits > max_strength_bits))
1062 max_strength_bits = curr->cipher->strength_bits;
1063 curr = curr->next;
1066 number_uses = calloc((max_strength_bits + 1), sizeof(int));
1067 if (!number_uses) {
1068 SSLerrorx(ERR_R_MALLOC_FAILURE);
1069 return (0);
1073 * Now find the strength_bits values actually used
1075 curr = *head_p;
1076 while (curr != NULL) {
1077 if (curr->active)
1078 number_uses[curr->cipher->strength_bits]++;
1079 curr = curr->next;
1082 * Go through the list of used strength_bits values in descending
1083 * order.
1085 for (i = max_strength_bits; i >= 0; i--)
1086 if (number_uses[i] > 0)
1087 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p, tail_p);
1089 free(number_uses);
1090 return (1);
1093 static int
1094 ssl_cipher_process_rulestr(const char *rule_str, CIPHER_ORDER **head_p,
1095 CIPHER_ORDER **tail_p, const SSL_CIPHER **ca_list)
1097 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1098 unsigned long algo_strength;
1099 int j, multi, found, rule, retval, ok, buflen;
1100 unsigned long cipher_id = 0;
1101 const char *l, *buf;
1102 char ch;
1104 retval = 1;
1105 l = rule_str;
1106 for (;;) {
1107 ch = *l;
1109 if (ch == '\0')
1110 break;
1112 if (ch == '-') {
1113 rule = CIPHER_DEL;
1114 l++;
1115 } else if (ch == '+') {
1116 rule = CIPHER_ORD;
1117 l++;
1118 } else if (ch == '!') {
1119 rule = CIPHER_KILL;
1120 l++;
1121 } else if (ch == '@') {
1122 rule = CIPHER_SPECIAL;
1123 l++;
1124 } else {
1125 rule = CIPHER_ADD;
1128 if (ITEM_SEP(ch)) {
1129 l++;
1130 continue;
1133 alg_mkey = 0;
1134 alg_auth = 0;
1135 alg_enc = 0;
1136 alg_mac = 0;
1137 alg_ssl = 0;
1138 algo_strength = 0;
1140 for (;;) {
1141 ch = *l;
1142 buf = l;
1143 buflen = 0;
1144 while (((ch >= 'A') && (ch <= 'Z')) ||
1145 ((ch >= '0') && (ch <= '9')) ||
1146 ((ch >= 'a') && (ch <= 'z')) ||
1147 (ch == '-') || (ch == '.')) {
1148 ch = *(++l);
1149 buflen++;
1152 if (buflen == 0) {
1154 * We hit something we cannot deal with,
1155 * it is no command or separator nor
1156 * alphanumeric, so we call this an error.
1158 SSLerrorx(SSL_R_INVALID_COMMAND);
1159 retval = found = 0;
1160 l++;
1161 break;
1164 if (rule == CIPHER_SPECIAL) {
1165 /* unused -- avoid compiler warning */
1166 found = 0;
1167 /* special treatment */
1168 break;
1171 /* check for multi-part specification */
1172 if (ch == '+') {
1173 multi = 1;
1174 l++;
1175 } else
1176 multi = 0;
1179 * Now search for the cipher alias in the ca_list.
1180 * Be careful with the strncmp, because the "buflen"
1181 * limitation will make the rule "ADH:SOME" and the
1182 * cipher "ADH-MY-CIPHER" look like a match for
1183 * buflen=3. So additionally check whether the cipher
1184 * name found has the correct length. We can save a
1185 * strlen() call: just checking for the '\0' at the
1186 * right place is sufficient, we have to strncmp()
1187 * anyway (we cannot use strcmp(), because buf is not
1188 * '\0' terminated.)
1190 j = found = 0;
1191 cipher_id = 0;
1192 while (ca_list[j]) {
1193 if (!strncmp(buf, ca_list[j]->name, buflen) &&
1194 (ca_list[j]->name[buflen] == '\0')) {
1195 found = 1;
1196 break;
1197 } else
1198 j++;
1201 if (!found)
1202 break; /* ignore this entry */
1204 if (ca_list[j]->algorithm_mkey) {
1205 if (alg_mkey) {
1206 alg_mkey &= ca_list[j]->algorithm_mkey;
1207 if (!alg_mkey) {
1208 found = 0;
1209 break;
1211 } else
1212 alg_mkey = ca_list[j]->algorithm_mkey;
1215 if (ca_list[j]->algorithm_auth) {
1216 if (alg_auth) {
1217 alg_auth &= ca_list[j]->algorithm_auth;
1218 if (!alg_auth) {
1219 found = 0;
1220 break;
1222 } else
1223 alg_auth = ca_list[j]->algorithm_auth;
1226 if (ca_list[j]->algorithm_enc) {
1227 if (alg_enc) {
1228 alg_enc &= ca_list[j]->algorithm_enc;
1229 if (!alg_enc) {
1230 found = 0;
1231 break;
1233 } else
1234 alg_enc = ca_list[j]->algorithm_enc;
1237 if (ca_list[j]->algorithm_mac) {
1238 if (alg_mac) {
1239 alg_mac &= ca_list[j]->algorithm_mac;
1240 if (!alg_mac) {
1241 found = 0;
1242 break;
1244 } else
1245 alg_mac = ca_list[j]->algorithm_mac;
1248 if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1249 if (algo_strength & SSL_STRONG_MASK) {
1250 algo_strength &=
1251 (ca_list[j]->algo_strength &
1252 SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1253 if (!(algo_strength &
1254 SSL_STRONG_MASK)) {
1255 found = 0;
1256 break;
1258 } else
1259 algo_strength |=
1260 ca_list[j]->algo_strength &
1261 SSL_STRONG_MASK;
1264 if (ca_list[j]->valid) {
1266 * explicit ciphersuite found; its protocol
1267 * version does not become part of the search
1268 * pattern!
1270 cipher_id = ca_list[j]->id;
1271 } else {
1273 * not an explicit ciphersuite; only in this
1274 * case, the protocol version is considered
1275 * part of the search pattern
1277 if (ca_list[j]->algorithm_ssl) {
1278 if (alg_ssl) {
1279 alg_ssl &=
1280 ca_list[j]->algorithm_ssl;
1281 if (!alg_ssl) {
1282 found = 0;
1283 break;
1285 } else
1286 alg_ssl =
1287 ca_list[j]->algorithm_ssl;
1291 if (!multi)
1292 break;
1296 * Ok, we have the rule, now apply it
1298 if (rule == CIPHER_SPECIAL) {
1299 /* special command */
1300 ok = 0;
1301 if ((buflen == 8) && !strncmp(buf, "STRENGTH", 8))
1302 ok = ssl_cipher_strength_sort(head_p, tail_p);
1303 else
1304 SSLerrorx(SSL_R_INVALID_COMMAND);
1305 if (ok == 0)
1306 retval = 0;
1308 * We do not support any "multi" options
1309 * together with "@", so throw away the
1310 * rest of the command, if any left, until
1311 * end or ':' is found.
1313 while ((*l != '\0') && !ITEM_SEP(*l))
1314 l++;
1315 } else if (found) {
1316 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth,
1317 alg_enc, alg_mac, alg_ssl, algo_strength, rule,
1318 -1, head_p, tail_p);
1319 } else {
1320 while ((*l != '\0') && !ITEM_SEP(*l))
1321 l++;
1323 if (*l == '\0')
1324 break; /* done */
1327 return (retval);
1330 static inline int
1331 ssl_aes_is_accelerated(void)
1333 #if defined(__i386__) || defined(__x86_64__)
1334 return ((OPENSSL_cpu_caps() & (1ULL << 57)) != 0);
1335 #else
1336 return (0);
1337 #endif
1340 STACK_OF(SSL_CIPHER) *
1341 ssl_create_cipher_list(const SSL_METHOD *ssl_method,
1342 STACK_OF(SSL_CIPHER) **cipher_list,
1343 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1344 const char *rule_str)
1346 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1347 unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
1348 STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
1349 const char *rule_p;
1350 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1351 const SSL_CIPHER **ca_list = NULL;
1354 * Return with error if nothing to do.
1356 if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
1357 return NULL;
1360 * To reduce the work to do we only want to process the compiled
1361 * in algorithms, so we first get the mask of disabled ciphers.
1363 ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1366 * Now we have to collect the available ciphers from the compiled
1367 * in ciphers. We cannot get more than the number compiled in, so
1368 * it is used for allocation.
1370 num_of_ciphers = ssl_method->num_ciphers();
1371 co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER));
1372 if (co_list == NULL) {
1373 SSLerrorx(ERR_R_MALLOC_FAILURE);
1374 return(NULL); /* Failure */
1377 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1378 disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1379 co_list, &head, &tail);
1382 /* Now arrange all ciphers by preference: */
1384 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
1385 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1386 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1388 if (ssl_aes_is_accelerated() == 1) {
1390 * We have hardware assisted AES - prefer AES as a symmetric
1391 * cipher, with CHACHA20 second.
1393 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1394 CIPHER_ADD, -1, &head, &tail);
1395 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1396 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1397 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305_OLD,
1398 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1399 } else {
1401 * CHACHA20 is fast and safe on all hardware and is thus our
1402 * preferred symmetric cipher, with AES second.
1404 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1405 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1406 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305_OLD,
1407 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1408 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1409 CIPHER_ADD, -1, &head, &tail);
1412 /* Temporarily enable everything else for sorting */
1413 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1415 /* Low priority for MD5 */
1416 ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
1418 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1419 * (For applications that allow them, they aren't too bad, but we prefer
1420 * authenticated ciphers.) */
1421 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1423 /* Move ciphers without forward secrecy to the end */
1424 ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1426 /* RC4 is sort of broken - move it to the end */
1427 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1429 /* Now sort by symmetric encryption strength. The above ordering remains
1430 * in force within each class */
1431 if (!ssl_cipher_strength_sort(&head, &tail)) {
1432 free(co_list);
1433 return NULL;
1436 /* Now disable everything (maintaining the ordering!) */
1437 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1441 * We also need cipher aliases for selecting based on the rule_str.
1442 * There might be two types of entries in the rule_str: 1) names
1443 * of ciphers themselves 2) aliases for groups of ciphers.
1444 * For 1) we need the available ciphers and for 2) the cipher
1445 * groups of cipher_aliases added together in one list (otherwise
1446 * we would be happy with just the cipher_aliases table).
1448 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1449 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1450 ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *));
1451 if (ca_list == NULL) {
1452 free(co_list);
1453 SSLerrorx(ERR_R_MALLOC_FAILURE);
1454 return(NULL); /* Failure */
1456 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1457 disabled_mkey, disabled_auth, disabled_enc,
1458 disabled_mac, disabled_ssl, head);
1461 * If the rule_string begins with DEFAULT, apply the default rule
1462 * before using the (possibly available) additional rules.
1464 ok = 1;
1465 rule_p = rule_str;
1466 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1467 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1468 &head, &tail, ca_list);
1469 rule_p += 7;
1470 if (*rule_p == ':')
1471 rule_p++;
1474 if (ok && (strlen(rule_p) > 0))
1475 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1477 free((void *)ca_list); /* Not needed anymore */
1479 if (!ok) {
1480 /* Rule processing failure */
1481 free(co_list);
1482 return (NULL);
1486 * Allocate new "cipherstack" for the result, return with error
1487 * if we cannot get one.
1489 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1490 free(co_list);
1491 return (NULL);
1495 * The cipher selection for the list is done. The ciphers are added
1496 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1498 for (curr = head; curr != NULL; curr = curr->next) {
1499 if (curr->active) {
1500 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1503 free(co_list); /* Not needed any longer */
1505 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1506 if (tmp_cipher_list == NULL) {
1507 sk_SSL_CIPHER_free(cipherstack);
1508 return NULL;
1510 sk_SSL_CIPHER_free(*cipher_list);
1511 *cipher_list = cipherstack;
1512 sk_SSL_CIPHER_free(*cipher_list_by_id);
1513 *cipher_list_by_id = tmp_cipher_list;
1514 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,
1515 ssl_cipher_ptr_id_cmp);
1517 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1518 return (cipherstack);
1521 const SSL_CIPHER *
1522 SSL_CIPHER_get_by_id(unsigned int id)
1524 return ssl3_get_cipher_by_id(id);
1527 const SSL_CIPHER *
1528 SSL_CIPHER_get_by_value(uint16_t value)
1530 return ssl3_get_cipher_by_value(value);
1533 char *
1534 SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1536 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, alg2;
1537 const char *ver, *kx, *au, *enc, *mac;
1538 char *ret;
1539 int l;
1541 alg_mkey = cipher->algorithm_mkey;
1542 alg_auth = cipher->algorithm_auth;
1543 alg_enc = cipher->algorithm_enc;
1544 alg_mac = cipher->algorithm_mac;
1545 alg_ssl = cipher->algorithm_ssl;
1547 alg2 = cipher->algorithm2;
1549 if (alg_ssl & SSL_SSLV3)
1550 ver = "SSLv3";
1551 else if (alg_ssl & SSL_TLSV1_2)
1552 ver = "TLSv1.2";
1553 else
1554 ver = "unknown";
1556 switch (alg_mkey) {
1557 case SSL_kRSA:
1558 kx = "RSA";
1559 break;
1560 case SSL_kDHE:
1561 kx = "DH";
1562 break;
1563 case SSL_kECDHE:
1564 kx = "ECDH";
1565 break;
1566 case SSL_kGOST:
1567 kx = "GOST";
1568 break;
1569 default:
1570 kx = "unknown";
1573 switch (alg_auth) {
1574 case SSL_aRSA:
1575 au = "RSA";
1576 break;
1577 case SSL_aDSS:
1578 au = "DSS";
1579 break;
1580 case SSL_aNULL:
1581 au = "None";
1582 break;
1583 case SSL_aECDSA:
1584 au = "ECDSA";
1585 break;
1586 case SSL_aGOST01:
1587 au = "GOST01";
1588 break;
1589 default:
1590 au = "unknown";
1591 break;
1594 switch (alg_enc) {
1595 case SSL_DES:
1596 enc = "DES(56)";
1597 break;
1598 case SSL_3DES:
1599 enc = "3DES(168)";
1600 break;
1601 case SSL_RC4:
1602 enc = alg2 & SSL2_CF_8_BYTE_ENC ? "RC4(64)" : "RC4(128)";
1603 break;
1604 case SSL_IDEA:
1605 enc = "IDEA(128)";
1606 break;
1607 case SSL_eNULL:
1608 enc = "None";
1609 break;
1610 case SSL_AES128:
1611 enc = "AES(128)";
1612 break;
1613 case SSL_AES256:
1614 enc = "AES(256)";
1615 break;
1616 case SSL_AES128GCM:
1617 enc = "AESGCM(128)";
1618 break;
1619 case SSL_AES256GCM:
1620 enc = "AESGCM(256)";
1621 break;
1622 case SSL_CAMELLIA128:
1623 enc = "Camellia(128)";
1624 break;
1625 case SSL_CAMELLIA256:
1626 enc = "Camellia(256)";
1627 break;
1628 case SSL_CHACHA20POLY1305:
1629 enc = "ChaCha20-Poly1305";
1630 break;
1631 case SSL_CHACHA20POLY1305_OLD:
1632 enc = "ChaCha20-Poly1305-Old";
1633 break;
1634 case SSL_eGOST2814789CNT:
1635 enc = "GOST-28178-89-CNT";
1636 break;
1637 default:
1638 enc = "unknown";
1639 break;
1642 switch (alg_mac) {
1643 case SSL_MD5:
1644 mac = "MD5";
1645 break;
1646 case SSL_SHA1:
1647 mac = "SHA1";
1648 break;
1649 case SSL_SHA256:
1650 mac = "SHA256";
1651 break;
1652 case SSL_SHA384:
1653 mac = "SHA384";
1654 break;
1655 case SSL_AEAD:
1656 mac = "AEAD";
1657 break;
1658 case SSL_GOST94:
1659 mac = "GOST94";
1660 break;
1661 case SSL_GOST89MAC:
1662 mac = "GOST89IMIT";
1663 break;
1664 case SSL_STREEBOG256:
1665 mac = "STREEBOG256";
1666 break;
1667 default:
1668 mac = "unknown";
1669 break;
1672 if (asprintf(&ret, "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1673 cipher->name, ver, kx, au, enc, mac) == -1)
1674 return "OPENSSL_malloc Error";
1676 if (buf != NULL) {
1677 l = strlcpy(buf, ret, len);
1678 free(ret);
1679 ret = buf;
1680 if (l >= len)
1681 ret = "Buffer too small";
1684 return (ret);
1687 char *
1688 SSL_CIPHER_get_version(const SSL_CIPHER *c)
1690 if (c == NULL)
1691 return("(NONE)");
1692 if ((c->id >> 24) == 3)
1693 return("TLSv1/SSLv3");
1694 else
1695 return("unknown");
1698 /* return the actual cipher being used */
1699 const char *
1700 SSL_CIPHER_get_name(const SSL_CIPHER *c)
1702 if (c != NULL)
1703 return (c->name);
1704 return("(NONE)");
1707 /* number of bits for symmetric cipher */
1709 SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1711 int ret = 0;
1713 if (c != NULL) {
1714 if (alg_bits != NULL)
1715 *alg_bits = c->alg_bits;
1716 ret = c->strength_bits;
1718 return (ret);
1721 unsigned long
1722 SSL_CIPHER_get_id(const SSL_CIPHER *c)
1724 return c->id;
1727 uint16_t
1728 SSL_CIPHER_get_value(const SSL_CIPHER *c)
1730 return ssl3_cipher_get_value(c);
1733 void *
1734 SSL_COMP_get_compression_methods(void)
1736 return NULL;
1740 SSL_COMP_add_compression_method(int id, void *cm)
1742 return 1;
1745 const char *
1746 SSL_COMP_get_name(const void *comp)
1748 return NULL;