1 /* $OpenBSD: ssl_ciph.c,v 1.85 2016/04/28 16:06:53 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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
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
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
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
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
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
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
145 #include <openssl/objects.h>
147 #ifndef OPENSSL_NO_ENGINE
148 #include <openssl/engine.h>
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 #define SSL_MD_STREEBOG512_IDX 7
180 /*Constant SSL_MAX_DIGEST equal to size of digests array should be
183 #define SSL_MD_NUM_IDX SSL_MAX_DIGEST
184 static const EVP_MD
*ssl_digest_methods
[SSL_MD_NUM_IDX
] = {
185 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
188 static int ssl_mac_pkey_id
[SSL_MD_NUM_IDX
] = {
189 EVP_PKEY_HMAC
, EVP_PKEY_HMAC
, EVP_PKEY_HMAC
, EVP_PKEY_GOSTIMIT
,
190 EVP_PKEY_HMAC
, EVP_PKEY_HMAC
, EVP_PKEY_HMAC
, EVP_PKEY_HMAC
,
193 static int ssl_mac_secret_size
[SSL_MD_NUM_IDX
] = {
194 0, 0, 0, 0, 0, 0, 0, 0
197 static int ssl_handshake_digest_flag
[SSL_MD_NUM_IDX
] = {
198 SSL_HANDSHAKE_MAC_MD5
, SSL_HANDSHAKE_MAC_SHA
,
199 SSL_HANDSHAKE_MAC_GOST94
, 0, SSL_HANDSHAKE_MAC_SHA256
,
200 SSL_HANDSHAKE_MAC_SHA384
, SSL_HANDSHAKE_MAC_STREEBOG256
,
201 SSL_HANDSHAKE_MAC_STREEBOG512
205 #define CIPHER_KILL 2
208 #define CIPHER_SPECIAL 5
210 typedef struct cipher_order_st
{
211 const SSL_CIPHER
*cipher
;
214 struct cipher_order_st
*next
, *prev
;
217 static const SSL_CIPHER cipher_aliases
[] = {
219 /* "ALL" doesn't include eNULL (must be specifically enabled) */
222 .algorithm_enc
= ~SSL_eNULL
,
225 /* "COMPLEMENTOFALL" */
227 .name
= SSL_TXT_CMPALL
,
228 .algorithm_enc
= SSL_eNULL
,
232 * "COMPLEMENTOFDEFAULT"
233 * (does *not* include ciphersuites not found in ALL!)
236 .name
= SSL_TXT_CMPDEF
,
237 .algorithm_mkey
= SSL_kDHE
|SSL_kECDHE
,
238 .algorithm_auth
= SSL_aNULL
,
239 .algorithm_enc
= ~SSL_eNULL
,
243 * key exchange aliases
244 * (some of those using only a single bit here combine multiple key
245 * exchange algs according to the RFCs, e.g. kEDH combines DHE_DSS
249 .name
= SSL_TXT_kRSA
,
250 .algorithm_mkey
= SSL_kRSA
,
253 .name
= SSL_TXT_kEDH
,
254 .algorithm_mkey
= SSL_kDHE
,
258 .algorithm_mkey
= SSL_kDHE
,
262 .name
= SSL_TXT_kECDHr
,
263 .algorithm_mkey
= SSL_kECDHr
,
266 .name
= SSL_TXT_kECDHe
,
267 .algorithm_mkey
= SSL_kECDHe
,
270 .name
= SSL_TXT_kECDH
,
271 .algorithm_mkey
= SSL_kECDHr
|SSL_kECDHe
,
274 .name
= SSL_TXT_kEECDH
,
275 .algorithm_mkey
= SSL_kECDHE
,
278 .name
= SSL_TXT_ECDH
,
279 .algorithm_mkey
= SSL_kECDHr
|SSL_kECDHe
|SSL_kECDHE
,
283 .name
= SSL_TXT_kGOST
,
284 .algorithm_mkey
= SSL_kGOST
,
287 /* server authentication aliases */
289 .name
= SSL_TXT_aRSA
,
290 .algorithm_auth
= SSL_aRSA
,
293 .name
= SSL_TXT_aDSS
,
294 .algorithm_auth
= SSL_aDSS
,
298 .algorithm_auth
= SSL_aDSS
,
301 .name
= SSL_TXT_aNULL
,
302 .algorithm_auth
= SSL_aNULL
,
305 .name
= SSL_TXT_aECDH
,
306 .algorithm_auth
= SSL_aECDH
,
309 .name
= SSL_TXT_aECDSA
,
310 .algorithm_auth
= SSL_aECDSA
,
313 .name
= SSL_TXT_ECDSA
,
314 .algorithm_auth
= SSL_aECDSA
,
317 .name
= SSL_TXT_aGOST01
,
318 .algorithm_auth
= SSL_aGOST01
,
321 .name
= SSL_TXT_aGOST
,
322 .algorithm_auth
= SSL_aGOST01
,
325 /* aliases combining key exchange and server authentication */
328 .algorithm_mkey
= SSL_kDHE
,
329 .algorithm_auth
= ~SSL_aNULL
,
333 .algorithm_mkey
= SSL_kDHE
,
334 .algorithm_auth
= ~SSL_aNULL
,
337 .name
= SSL_TXT_ECDHE
,
338 .algorithm_mkey
= SSL_kECDHE
,
339 .algorithm_auth
= ~SSL_aNULL
,
342 .name
= SSL_TXT_EECDH
,
343 .algorithm_mkey
= SSL_kECDHE
,
344 .algorithm_auth
= ~SSL_aNULL
,
347 .name
= SSL_TXT_NULL
,
348 .algorithm_enc
= SSL_eNULL
,
352 .algorithm_mkey
= SSL_kRSA
,
353 .algorithm_auth
= SSL_aRSA
,
357 .algorithm_mkey
= SSL_kDHE
,
358 .algorithm_auth
= SSL_aNULL
,
361 .name
= SSL_TXT_AECDH
,
362 .algorithm_mkey
= SSL_kECDHE
,
363 .algorithm_auth
= SSL_aNULL
,
366 /* symmetric encryption aliases */
369 .algorithm_enc
= SSL_DES
,
372 .name
= SSL_TXT_3DES
,
373 .algorithm_enc
= SSL_3DES
,
377 .algorithm_enc
= SSL_RC4
,
380 .name
= SSL_TXT_IDEA
,
381 .algorithm_enc
= SSL_IDEA
,
384 .name
= SSL_TXT_eNULL
,
385 .algorithm_enc
= SSL_eNULL
,
388 .name
= SSL_TXT_AES128
,
389 .algorithm_enc
= SSL_AES128
|SSL_AES128GCM
,
392 .name
= SSL_TXT_AES256
,
393 .algorithm_enc
= SSL_AES256
|SSL_AES256GCM
,
397 .algorithm_enc
= SSL_AES
,
400 .name
= SSL_TXT_AES_GCM
,
401 .algorithm_enc
= SSL_AES128GCM
|SSL_AES256GCM
,
404 .name
= SSL_TXT_CAMELLIA128
,
405 .algorithm_enc
= SSL_CAMELLIA128
,
408 .name
= SSL_TXT_CAMELLIA256
,
409 .algorithm_enc
= SSL_CAMELLIA256
,
412 .name
= SSL_TXT_CAMELLIA
,
413 .algorithm_enc
= SSL_CAMELLIA128
|SSL_CAMELLIA256
,
416 .name
= SSL_TXT_CHACHA20
,
417 .algorithm_enc
= SSL_CHACHA20POLY1305
|SSL_CHACHA20POLY1305_OLD
,
422 .name
= SSL_TXT_AEAD
,
423 .algorithm_mac
= SSL_AEAD
,
427 .algorithm_mac
= SSL_MD5
,
430 .name
= SSL_TXT_SHA1
,
431 .algorithm_mac
= SSL_SHA1
,
435 .algorithm_mac
= SSL_SHA1
,
438 .name
= SSL_TXT_GOST94
,
439 .algorithm_mac
= SSL_GOST94
,
442 .name
= SSL_TXT_GOST89MAC
,
443 .algorithm_mac
= SSL_GOST89MAC
,
446 .name
= SSL_TXT_SHA256
,
447 .algorithm_mac
= SSL_SHA256
,
450 .name
= SSL_TXT_SHA384
,
451 .algorithm_mac
= SSL_SHA384
,
454 .name
= SSL_TXT_STREEBOG256
,
455 .algorithm_mac
= SSL_STREEBOG256
,
458 .name
= SSL_TXT_STREEBOG512
,
459 .algorithm_mac
= SSL_STREEBOG512
,
462 /* protocol version aliases */
464 .name
= SSL_TXT_SSLV3
,
465 .algorithm_ssl
= SSL_SSLV3
,
468 .name
= SSL_TXT_TLSV1
,
469 .algorithm_ssl
= SSL_TLSV1
,
472 .name
= SSL_TXT_TLSV1_2
,
473 .algorithm_ssl
= SSL_TLSV1_2
,
476 /* strength classes */
479 .algo_strength
= SSL_LOW
,
482 .name
= SSL_TXT_MEDIUM
,
483 .algo_strength
= SSL_MEDIUM
,
486 .name
= SSL_TXT_HIGH
,
487 .algo_strength
= SSL_HIGH
,
492 ssl_load_ciphers(void)
494 ssl_cipher_methods
[SSL_ENC_DES_IDX
] =
495 EVP_get_cipherbyname(SN_des_cbc
);
496 ssl_cipher_methods
[SSL_ENC_3DES_IDX
] =
497 EVP_get_cipherbyname(SN_des_ede3_cbc
);
498 ssl_cipher_methods
[SSL_ENC_RC4_IDX
] =
499 EVP_get_cipherbyname(SN_rc4
);
500 #ifndef OPENSSL_NO_IDEA
501 ssl_cipher_methods
[SSL_ENC_IDEA_IDX
] =
502 EVP_get_cipherbyname(SN_idea_cbc
);
504 ssl_cipher_methods
[SSL_ENC_IDEA_IDX
] = NULL
;
506 ssl_cipher_methods
[SSL_ENC_AES128_IDX
] =
507 EVP_get_cipherbyname(SN_aes_128_cbc
);
508 ssl_cipher_methods
[SSL_ENC_AES256_IDX
] =
509 EVP_get_cipherbyname(SN_aes_256_cbc
);
510 ssl_cipher_methods
[SSL_ENC_CAMELLIA128_IDX
] =
511 EVP_get_cipherbyname(SN_camellia_128_cbc
);
512 ssl_cipher_methods
[SSL_ENC_CAMELLIA256_IDX
] =
513 EVP_get_cipherbyname(SN_camellia_256_cbc
);
514 ssl_cipher_methods
[SSL_ENC_GOST89_IDX
] =
515 EVP_get_cipherbyname(SN_gost89_cnt
);
517 ssl_cipher_methods
[SSL_ENC_AES128GCM_IDX
] =
518 EVP_get_cipherbyname(SN_aes_128_gcm
);
519 ssl_cipher_methods
[SSL_ENC_AES256GCM_IDX
] =
520 EVP_get_cipherbyname(SN_aes_256_gcm
);
522 ssl_digest_methods
[SSL_MD_MD5_IDX
] =
523 EVP_get_digestbyname(SN_md5
);
524 ssl_mac_secret_size
[SSL_MD_MD5_IDX
] =
525 EVP_MD_size(ssl_digest_methods
[SSL_MD_MD5_IDX
]);
526 OPENSSL_assert(ssl_mac_secret_size
[SSL_MD_MD5_IDX
] >= 0);
527 ssl_digest_methods
[SSL_MD_SHA1_IDX
] =
528 EVP_get_digestbyname(SN_sha1
);
529 ssl_mac_secret_size
[SSL_MD_SHA1_IDX
] =
530 EVP_MD_size(ssl_digest_methods
[SSL_MD_SHA1_IDX
]);
531 OPENSSL_assert(ssl_mac_secret_size
[SSL_MD_SHA1_IDX
] >= 0);
532 ssl_digest_methods
[SSL_MD_GOST94_IDX
] =
533 EVP_get_digestbyname(SN_id_GostR3411_94
);
534 if (ssl_digest_methods
[SSL_MD_GOST94_IDX
]) {
535 ssl_mac_secret_size
[SSL_MD_GOST94_IDX
] =
536 EVP_MD_size(ssl_digest_methods
[SSL_MD_GOST94_IDX
]);
537 OPENSSL_assert(ssl_mac_secret_size
[SSL_MD_GOST94_IDX
] >= 0);
539 ssl_digest_methods
[SSL_MD_GOST89MAC_IDX
] =
540 EVP_get_digestbyname(SN_id_Gost28147_89_MAC
);
541 if (ssl_mac_pkey_id
[SSL_MD_GOST89MAC_IDX
]) {
542 ssl_mac_secret_size
[SSL_MD_GOST89MAC_IDX
] = 32;
545 ssl_digest_methods
[SSL_MD_SHA256_IDX
] =
546 EVP_get_digestbyname(SN_sha256
);
547 ssl_mac_secret_size
[SSL_MD_SHA256_IDX
] =
548 EVP_MD_size(ssl_digest_methods
[SSL_MD_SHA256_IDX
]);
549 ssl_digest_methods
[SSL_MD_SHA384_IDX
] =
550 EVP_get_digestbyname(SN_sha384
);
551 ssl_mac_secret_size
[SSL_MD_SHA384_IDX
] =
552 EVP_MD_size(ssl_digest_methods
[SSL_MD_SHA384_IDX
]);
553 ssl_digest_methods
[SSL_MD_STREEBOG256_IDX
] =
554 EVP_get_digestbyname(SN_id_tc26_gost3411_2012_256
);
555 ssl_mac_secret_size
[SSL_MD_STREEBOG256_IDX
] =
556 EVP_MD_size(ssl_digest_methods
[SSL_MD_STREEBOG256_IDX
]);
557 ssl_digest_methods
[SSL_MD_STREEBOG512_IDX
] =
558 EVP_get_digestbyname(SN_id_tc26_gost3411_2012_512
);
559 ssl_mac_secret_size
[SSL_MD_STREEBOG512_IDX
] =
560 EVP_MD_size(ssl_digest_methods
[SSL_MD_STREEBOG512_IDX
]);
564 ssl_cipher_get_evp(const SSL_SESSION
*s
, const EVP_CIPHER
**enc
,
565 const EVP_MD
**md
, int *mac_pkey_type
, int *mac_secret_size
)
575 * This function does not handle EVP_AEAD.
576 * See ssl_cipher_get_aead_evp instead.
578 if (c
->algorithm2
& SSL_CIPHER_ALGORITHM2_AEAD
)
581 if ((enc
== NULL
) || (md
== NULL
))
584 switch (c
->algorithm_enc
) {
589 i
= SSL_ENC_3DES_IDX
;
595 i
= SSL_ENC_IDEA_IDX
;
598 i
= SSL_ENC_NULL_IDX
;
601 i
= SSL_ENC_AES128_IDX
;
604 i
= SSL_ENC_AES256_IDX
;
606 case SSL_CAMELLIA128
:
607 i
= SSL_ENC_CAMELLIA128_IDX
;
609 case SSL_CAMELLIA256
:
610 i
= SSL_ENC_CAMELLIA256_IDX
;
612 case SSL_eGOST2814789CNT
:
613 i
= SSL_ENC_GOST89_IDX
;
616 i
= SSL_ENC_AES128GCM_IDX
;
619 i
= SSL_ENC_AES256GCM_IDX
;
626 if ((i
< 0) || (i
>= SSL_ENC_NUM_IDX
))
629 if (i
== SSL_ENC_NULL_IDX
)
630 *enc
= EVP_enc_null();
632 *enc
= ssl_cipher_methods
[i
];
635 switch (c
->algorithm_mac
) {
643 i
= SSL_MD_SHA256_IDX
;
646 i
= SSL_MD_SHA384_IDX
;
649 i
= SSL_MD_GOST94_IDX
;
652 i
= SSL_MD_GOST89MAC_IDX
;
654 case SSL_STREEBOG256
:
655 i
= SSL_MD_STREEBOG256_IDX
;
657 case SSL_STREEBOG512
:
658 i
= SSL_MD_STREEBOG512_IDX
;
664 if ((i
< 0) || (i
>= SSL_MD_NUM_IDX
)) {
667 if (mac_pkey_type
!= NULL
)
668 *mac_pkey_type
= NID_undef
;
669 if (mac_secret_size
!= NULL
)
670 *mac_secret_size
= 0;
671 if (c
->algorithm_mac
== SSL_AEAD
)
672 mac_pkey_type
= NULL
;
674 *md
= ssl_digest_methods
[i
];
675 if (mac_pkey_type
!= NULL
)
676 *mac_pkey_type
= ssl_mac_pkey_id
[i
];
677 if (mac_secret_size
!= NULL
)
678 *mac_secret_size
= ssl_mac_secret_size
[i
];
681 if ((*enc
!= NULL
) &&
682 (*md
!= NULL
|| (EVP_CIPHER_flags(*enc
)&EVP_CIPH_FLAG_AEAD_CIPHER
)) &&
683 (!mac_pkey_type
|| *mac_pkey_type
!= NID_undef
)) {
684 const EVP_CIPHER
*evp
;
686 if (s
->ssl_version
>> 8 != TLS1_VERSION_MAJOR
||
687 s
->ssl_version
< TLS1_VERSION
)
690 if (c
->algorithm_enc
== SSL_RC4
&&
691 c
->algorithm_mac
== SSL_MD5
&&
692 (evp
= EVP_get_cipherbyname("RC4-HMAC-MD5")))
693 *enc
= evp
, *md
= NULL
;
694 else if (c
->algorithm_enc
== SSL_AES128
&&
695 c
->algorithm_mac
== SSL_SHA1
&&
696 (evp
= EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
697 *enc
= evp
, *md
= NULL
;
698 else if (c
->algorithm_enc
== SSL_AES256
&&
699 c
->algorithm_mac
== SSL_SHA1
&&
700 (evp
= EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
701 *enc
= evp
, *md
= NULL
;
708 * ssl_cipher_get_evp_aead sets aead to point to the correct EVP_AEAD object
709 * for s->cipher. It returns 1 on success and 0 on error.
712 ssl_cipher_get_evp_aead(const SSL_SESSION
*s
, const EVP_AEAD
**aead
)
714 const SSL_CIPHER
*c
= s
->cipher
;
720 if ((c
->algorithm2
& SSL_CIPHER_ALGORITHM2_AEAD
) == 0)
723 switch (c
->algorithm_enc
) {
724 #ifndef OPENSSL_NO_AES
726 *aead
= EVP_aead_aes_128_gcm();
729 *aead
= EVP_aead_aes_256_gcm();
732 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
733 case SSL_CHACHA20POLY1305
:
734 *aead
= EVP_aead_chacha20_poly1305();
736 case SSL_CHACHA20POLY1305_OLD
:
737 *aead
= EVP_aead_chacha20_poly1305_old();
747 ssl_get_handshake_digest(int idx
, long *mask
, const EVP_MD
**md
)
749 if (idx
< 0 || idx
>= SSL_MD_NUM_IDX
) {
752 *mask
= ssl_handshake_digest_flag
[idx
];
754 *md
= ssl_digest_methods
[idx
];
760 #define ITEM_SEP(a) \
761 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
764 ll_append_tail(CIPHER_ORDER
**head
, CIPHER_ORDER
*curr
,
771 if (curr
->prev
!= NULL
)
772 curr
->prev
->next
= curr
->next
;
773 if (curr
->next
!= NULL
)
774 curr
->next
->prev
= curr
->prev
;
775 (*tail
)->next
= curr
;
782 ll_append_head(CIPHER_ORDER
**head
, CIPHER_ORDER
*curr
,
789 if (curr
->next
!= NULL
)
790 curr
->next
->prev
= curr
->prev
;
791 if (curr
->prev
!= NULL
)
792 curr
->prev
->next
= curr
->next
;
793 (*head
)->prev
= curr
;
800 ssl_cipher_get_disabled(unsigned long *mkey
, unsigned long *auth
,
801 unsigned long *enc
, unsigned long *mac
, unsigned long *ssl
)
810 * Check for the availability of GOST 34.10 public/private key
811 * algorithms. If they are not available disable the associated
812 * authentication and key exchange algorithms.
814 if (EVP_PKEY_meth_find(NID_id_GostR3410_2001
) == NULL
) {
815 *auth
|= SSL_aGOST01
;
819 #ifdef SSL_FORBID_ENULL
823 *enc
|= (ssl_cipher_methods
[SSL_ENC_DES_IDX
] == NULL
) ? SSL_DES
: 0;
824 *enc
|= (ssl_cipher_methods
[SSL_ENC_3DES_IDX
] == NULL
) ? SSL_3DES
: 0;
825 *enc
|= (ssl_cipher_methods
[SSL_ENC_RC4_IDX
] == NULL
) ? SSL_RC4
: 0;
826 *enc
|= (ssl_cipher_methods
[SSL_ENC_IDEA_IDX
] == NULL
) ? SSL_IDEA
: 0;
827 *enc
|= (ssl_cipher_methods
[SSL_ENC_AES128_IDX
] == NULL
) ? SSL_AES128
: 0;
828 *enc
|= (ssl_cipher_methods
[SSL_ENC_AES256_IDX
] == NULL
) ? SSL_AES256
: 0;
829 *enc
|= (ssl_cipher_methods
[SSL_ENC_AES128GCM_IDX
] == NULL
) ? SSL_AES128GCM
: 0;
830 *enc
|= (ssl_cipher_methods
[SSL_ENC_AES256GCM_IDX
] == NULL
) ? SSL_AES256GCM
: 0;
831 *enc
|= (ssl_cipher_methods
[SSL_ENC_CAMELLIA128_IDX
] == NULL
) ? SSL_CAMELLIA128
: 0;
832 *enc
|= (ssl_cipher_methods
[SSL_ENC_CAMELLIA256_IDX
] == NULL
) ? SSL_CAMELLIA256
: 0;
833 *enc
|= (ssl_cipher_methods
[SSL_ENC_GOST89_IDX
] == NULL
) ? SSL_eGOST2814789CNT
: 0;
835 *mac
|= (ssl_digest_methods
[SSL_MD_MD5_IDX
] == NULL
) ? SSL_MD5
: 0;
836 *mac
|= (ssl_digest_methods
[SSL_MD_SHA1_IDX
] == NULL
) ? SSL_SHA1
: 0;
837 *mac
|= (ssl_digest_methods
[SSL_MD_SHA256_IDX
] == NULL
) ? SSL_SHA256
: 0;
838 *mac
|= (ssl_digest_methods
[SSL_MD_SHA384_IDX
] == NULL
) ? SSL_SHA384
: 0;
839 *mac
|= (ssl_digest_methods
[SSL_MD_GOST94_IDX
] == NULL
) ? SSL_GOST94
: 0;
840 *mac
|= (ssl_digest_methods
[SSL_MD_GOST89MAC_IDX
] == NULL
) ? SSL_GOST89MAC
: 0;
841 *mac
|= (ssl_digest_methods
[SSL_MD_STREEBOG256_IDX
] == NULL
) ? SSL_STREEBOG256
: 0;
842 *mac
|= (ssl_digest_methods
[SSL_MD_STREEBOG512_IDX
] == NULL
) ? SSL_STREEBOG512
: 0;
847 ssl_cipher_collect_ciphers(const SSL_METHOD
*ssl_method
, int num_of_ciphers
,
848 unsigned long disabled_mkey
, unsigned long disabled_auth
,
849 unsigned long disabled_enc
, unsigned long disabled_mac
,
850 unsigned long disabled_ssl
, CIPHER_ORDER
*co_list
,
851 CIPHER_ORDER
**head_p
, CIPHER_ORDER
**tail_p
)
857 * We have num_of_ciphers descriptions compiled in, depending on the
858 * method selected (SSLv3, TLSv1, etc). These will later be sorted in
859 * a linked list with at most num entries.
862 /* Get the initial list of ciphers */
863 co_list_num
= 0; /* actual count of ciphers */
864 for (i
= 0; i
< num_of_ciphers
; i
++) {
865 c
= ssl_method
->get_cipher(i
);
866 /* drop those that use any of that is not available */
867 if ((c
!= NULL
) && c
->valid
&&
868 !(c
->algorithm_mkey
& disabled_mkey
) &&
869 !(c
->algorithm_auth
& disabled_auth
) &&
870 !(c
->algorithm_enc
& disabled_enc
) &&
871 !(c
->algorithm_mac
& disabled_mac
) &&
872 !(c
->algorithm_ssl
& disabled_ssl
)) {
873 co_list
[co_list_num
].cipher
= c
;
874 co_list
[co_list_num
].next
= NULL
;
875 co_list
[co_list_num
].prev
= NULL
;
876 co_list
[co_list_num
].active
= 0;
879 if (!sk_push(ca_list,(char *)c)) goto err;
885 * Prepare linked list from list entries
887 if (co_list_num
> 0) {
888 co_list
[0].prev
= NULL
;
890 if (co_list_num
> 1) {
891 co_list
[0].next
= &co_list
[1];
893 for (i
= 1; i
< co_list_num
- 1; i
++) {
894 co_list
[i
].prev
= &co_list
[i
- 1];
895 co_list
[i
].next
= &co_list
[i
+ 1];
898 co_list
[co_list_num
- 1].prev
=
899 &co_list
[co_list_num
- 2];
902 co_list
[co_list_num
- 1].next
= NULL
;
904 *head_p
= &co_list
[0];
905 *tail_p
= &co_list
[co_list_num
- 1];
910 ssl_cipher_collect_aliases(const SSL_CIPHER
**ca_list
, int num_of_group_aliases
,
911 unsigned long disabled_mkey
, unsigned long disabled_auth
,
912 unsigned long disabled_enc
, unsigned long disabled_mac
,
913 unsigned long disabled_ssl
, CIPHER_ORDER
*head
)
915 CIPHER_ORDER
*ciph_curr
;
916 const SSL_CIPHER
**ca_curr
;
918 unsigned long mask_mkey
= ~disabled_mkey
;
919 unsigned long mask_auth
= ~disabled_auth
;
920 unsigned long mask_enc
= ~disabled_enc
;
921 unsigned long mask_mac
= ~disabled_mac
;
922 unsigned long mask_ssl
= ~disabled_ssl
;
925 * First, add the real ciphers as already collected
929 while (ciph_curr
!= NULL
) {
930 *ca_curr
= ciph_curr
->cipher
;
932 ciph_curr
= ciph_curr
->next
;
936 * Now we add the available ones from the cipher_aliases[] table.
937 * They represent either one or more algorithms, some of which
938 * in any affected category must be supported (set in enabled_mask),
939 * or represent a cipher strength value (will be added in any case because algorithms=0).
941 for (i
= 0; i
< num_of_group_aliases
; i
++) {
942 unsigned long algorithm_mkey
= cipher_aliases
[i
].algorithm_mkey
;
943 unsigned long algorithm_auth
= cipher_aliases
[i
].algorithm_auth
;
944 unsigned long algorithm_enc
= cipher_aliases
[i
].algorithm_enc
;
945 unsigned long algorithm_mac
= cipher_aliases
[i
].algorithm_mac
;
946 unsigned long algorithm_ssl
= cipher_aliases
[i
].algorithm_ssl
;
949 if ((algorithm_mkey
& mask_mkey
) == 0)
953 if ((algorithm_auth
& mask_auth
) == 0)
957 if ((algorithm_enc
& mask_enc
) == 0)
961 if ((algorithm_mac
& mask_mac
) == 0)
965 if ((algorithm_ssl
& mask_ssl
) == 0)
968 *ca_curr
= (SSL_CIPHER
*)(cipher_aliases
+ i
);
972 *ca_curr
= NULL
; /* end of list */
976 ssl_cipher_apply_rule(unsigned long cipher_id
, unsigned long alg_mkey
,
977 unsigned long alg_auth
, unsigned long alg_enc
, unsigned long alg_mac
,
978 unsigned long alg_ssl
, unsigned long algo_strength
,
979 int rule
, int strength_bits
, CIPHER_ORDER
**head_p
, CIPHER_ORDER
**tail_p
)
981 CIPHER_ORDER
*head
, *tail
, *curr
, *next
, *last
;
982 const SSL_CIPHER
*cp
;
986 if (rule
== CIPHER_DEL
)
987 reverse
= 1; /* needed to maintain sorting between currently deleted ciphers */
1005 next
= reverse
? curr
->prev
: curr
->next
;
1010 * Selection criteria is either the value of strength_bits
1011 * or the algorithms used.
1013 if (strength_bits
>= 0) {
1014 if (strength_bits
!= cp
->strength_bits
)
1018 if (alg_mkey
&& !(alg_mkey
& cp
->algorithm_mkey
))
1020 if (alg_auth
&& !(alg_auth
& cp
->algorithm_auth
))
1022 if (alg_enc
&& !(alg_enc
& cp
->algorithm_enc
))
1024 if (alg_mac
&& !(alg_mac
& cp
->algorithm_mac
))
1026 if (alg_ssl
&& !(alg_ssl
& cp
->algorithm_ssl
))
1028 if ((algo_strength
& SSL_STRONG_MASK
) && !(algo_strength
& SSL_STRONG_MASK
& cp
->algo_strength
))
1033 /* add the cipher if it has not been added yet. */
1034 if (rule
== CIPHER_ADD
) {
1036 if (!curr
->active
) {
1037 ll_append_tail(&head
, curr
, &tail
);
1041 /* Move the added cipher to this location */
1042 else if (rule
== CIPHER_ORD
) {
1045 ll_append_tail(&head
, curr
, &tail
);
1047 } else if (rule
== CIPHER_DEL
) {
1050 /* most recently deleted ciphersuites get best positions
1051 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
1052 * works in reverse to maintain the order) */
1053 ll_append_head(&head
, curr
, &tail
);
1056 } else if (rule
== CIPHER_KILL
) {
1061 curr
->prev
->next
= curr
->next
;
1065 if (curr
->next
!= NULL
)
1066 curr
->next
->prev
= curr
->prev
;
1067 if (curr
->prev
!= NULL
)
1068 curr
->prev
->next
= curr
->next
;
1079 ssl_cipher_strength_sort(CIPHER_ORDER
**head_p
, CIPHER_ORDER
**tail_p
)
1081 int max_strength_bits
, i
, *number_uses
;
1085 * This routine sorts the ciphers with descending strength. The sorting
1086 * must keep the pre-sorted sequence, so we apply the normal sorting
1087 * routine as '+' movement to the end of the list.
1089 max_strength_bits
= 0;
1091 while (curr
!= NULL
) {
1093 (curr
->cipher
->strength_bits
> max_strength_bits
))
1094 max_strength_bits
= curr
->cipher
->strength_bits
;
1098 number_uses
= calloc((max_strength_bits
+ 1), sizeof(int));
1100 SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT
, ERR_R_MALLOC_FAILURE
);
1105 * Now find the strength_bits values actually used
1108 while (curr
!= NULL
) {
1110 number_uses
[curr
->cipher
->strength_bits
]++;
1114 * Go through the list of used strength_bits values in descending
1117 for (i
= max_strength_bits
; i
>= 0; i
--)
1118 if (number_uses
[i
] > 0)
1119 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD
, i
, head_p
, tail_p
);
1126 ssl_cipher_process_rulestr(const char *rule_str
, CIPHER_ORDER
**head_p
,
1127 CIPHER_ORDER
**tail_p
, const SSL_CIPHER
**ca_list
)
1129 unsigned long alg_mkey
, alg_auth
, alg_enc
, alg_mac
, alg_ssl
;
1130 unsigned long algo_strength
;
1131 int j
, multi
, found
, rule
, retval
, ok
, buflen
;
1132 unsigned long cipher_id
= 0;
1133 const char *l
, *buf
;
1147 } else if (ch
== '+') {
1150 } else if (ch
== '!') {
1153 } else if (ch
== '@') {
1154 rule
= CIPHER_SPECIAL
;
1176 while (((ch
>= 'A') && (ch
<= 'Z')) ||
1177 ((ch
>= '0') && (ch
<= '9')) ||
1178 ((ch
>= 'a') && (ch
<= 'z')) ||
1179 (ch
== '-') || (ch
== '.')) {
1186 * We hit something we cannot deal with,
1187 * it is no command or separator nor
1188 * alphanumeric, so we call this an error.
1190 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR
,
1191 SSL_R_INVALID_COMMAND
);
1197 if (rule
== CIPHER_SPECIAL
) {
1198 /* unused -- avoid compiler warning */
1200 /* special treatment */
1204 /* check for multi-part specification */
1212 * Now search for the cipher alias in the ca_list.
1213 * Be careful with the strncmp, because the "buflen"
1214 * limitation will make the rule "ADH:SOME" and the
1215 * cipher "ADH-MY-CIPHER" look like a match for
1216 * buflen=3. So additionally check whether the cipher
1217 * name found has the correct length. We can save a
1218 * strlen() call: just checking for the '\0' at the
1219 * right place is sufficient, we have to strncmp()
1220 * anyway (we cannot use strcmp(), because buf is not
1225 while (ca_list
[j
]) {
1226 if (!strncmp(buf
, ca_list
[j
]->name
, buflen
) &&
1227 (ca_list
[j
]->name
[buflen
] == '\0')) {
1235 break; /* ignore this entry */
1237 if (ca_list
[j
]->algorithm_mkey
) {
1239 alg_mkey
&= ca_list
[j
]->algorithm_mkey
;
1245 alg_mkey
= ca_list
[j
]->algorithm_mkey
;
1248 if (ca_list
[j
]->algorithm_auth
) {
1250 alg_auth
&= ca_list
[j
]->algorithm_auth
;
1256 alg_auth
= ca_list
[j
]->algorithm_auth
;
1259 if (ca_list
[j
]->algorithm_enc
) {
1261 alg_enc
&= ca_list
[j
]->algorithm_enc
;
1267 alg_enc
= ca_list
[j
]->algorithm_enc
;
1270 if (ca_list
[j
]->algorithm_mac
) {
1272 alg_mac
&= ca_list
[j
]->algorithm_mac
;
1278 alg_mac
= ca_list
[j
]->algorithm_mac
;
1281 if (ca_list
[j
]->algo_strength
& SSL_STRONG_MASK
) {
1282 if (algo_strength
& SSL_STRONG_MASK
) {
1284 (ca_list
[j
]->algo_strength
&
1285 SSL_STRONG_MASK
) | ~SSL_STRONG_MASK
;
1286 if (!(algo_strength
&
1293 ca_list
[j
]->algo_strength
&
1297 if (ca_list
[j
]->valid
) {
1299 * explicit ciphersuite found; its protocol
1300 * version does not become part of the search
1303 cipher_id
= ca_list
[j
]->id
;
1306 * not an explicit ciphersuite; only in this
1307 * case, the protocol version is considered
1308 * part of the search pattern
1310 if (ca_list
[j
]->algorithm_ssl
) {
1313 ca_list
[j
]->algorithm_ssl
;
1320 ca_list
[j
]->algorithm_ssl
;
1329 * Ok, we have the rule, now apply it
1331 if (rule
== CIPHER_SPECIAL
) {
1332 /* special command */
1334 if ((buflen
== 8) && !strncmp(buf
, "STRENGTH", 8))
1335 ok
= ssl_cipher_strength_sort(head_p
, tail_p
);
1337 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR
,
1338 SSL_R_INVALID_COMMAND
);
1342 * We do not support any "multi" options
1343 * together with "@", so throw away the
1344 * rest of the command, if any left, until
1345 * end or ':' is found.
1347 while ((*l
!= '\0') && !ITEM_SEP(*l
))
1350 ssl_cipher_apply_rule(cipher_id
, alg_mkey
, alg_auth
,
1351 alg_enc
, alg_mac
, alg_ssl
, algo_strength
, rule
,
1352 -1, head_p
, tail_p
);
1354 while ((*l
!= '\0') && !ITEM_SEP(*l
))
1365 ssl_aes_is_accelerated(void)
1367 #if defined(__i386__) || defined(__x86_64__)
1368 return ((OPENSSL_cpu_caps() & (1ULL << 57)) != 0);
1374 STACK_OF(SSL_CIPHER
) *
1375 ssl_create_cipher_list(const SSL_METHOD
*ssl_method
,
1376 STACK_OF(SSL_CIPHER
) **cipher_list
,
1377 STACK_OF(SSL_CIPHER
) **cipher_list_by_id
,
1378 const char *rule_str
)
1380 int ok
, num_of_ciphers
, num_of_alias_max
, num_of_group_aliases
;
1381 unsigned long disabled_mkey
, disabled_auth
, disabled_enc
, disabled_mac
, disabled_ssl
;
1382 STACK_OF(SSL_CIPHER
) *cipherstack
, *tmp_cipher_list
;
1384 CIPHER_ORDER
*co_list
= NULL
, *head
= NULL
, *tail
= NULL
, *curr
;
1385 const SSL_CIPHER
**ca_list
= NULL
;
1388 * Return with error if nothing to do.
1390 if (rule_str
== NULL
|| cipher_list
== NULL
|| cipher_list_by_id
== NULL
)
1394 * To reduce the work to do we only want to process the compiled
1395 * in algorithms, so we first get the mask of disabled ciphers.
1397 ssl_cipher_get_disabled(&disabled_mkey
, &disabled_auth
, &disabled_enc
, &disabled_mac
, &disabled_ssl
);
1400 * Now we have to collect the available ciphers from the compiled
1401 * in ciphers. We cannot get more than the number compiled in, so
1402 * it is used for allocation.
1404 num_of_ciphers
= ssl_method
->num_ciphers();
1405 co_list
= reallocarray(NULL
, num_of_ciphers
, sizeof(CIPHER_ORDER
));
1406 if (co_list
== NULL
) {
1407 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST
, ERR_R_MALLOC_FAILURE
);
1408 return(NULL
); /* Failure */
1411 ssl_cipher_collect_ciphers(ssl_method
, num_of_ciphers
,
1412 disabled_mkey
, disabled_auth
, disabled_enc
, disabled_mac
, disabled_ssl
,
1413 co_list
, &head
, &tail
);
1416 /* Now arrange all ciphers by preference: */
1418 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
1419 ssl_cipher_apply_rule(0, SSL_kECDHE
, 0, 0, 0, 0, 0, CIPHER_ADD
, -1, &head
, &tail
);
1420 ssl_cipher_apply_rule(0, SSL_kECDHE
, 0, 0, 0, 0, 0, CIPHER_DEL
, -1, &head
, &tail
);
1422 if (ssl_aes_is_accelerated() == 1) {
1424 * We have hardware assisted AES - prefer AES as a symmetric
1425 * cipher, with CHACHA20 second.
1427 ssl_cipher_apply_rule(0, 0, 0, SSL_AES
, 0, 0, 0,
1428 CIPHER_ADD
, -1, &head
, &tail
);
1429 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305
,
1430 0, 0, 0, CIPHER_ADD
, -1, &head
, &tail
);
1431 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305_OLD
,
1432 0, 0, 0, CIPHER_ADD
, -1, &head
, &tail
);
1435 * CHACHA20 is fast and safe on all hardware and is thus our
1436 * preferred symmetric cipher, with AES second.
1438 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305
,
1439 0, 0, 0, CIPHER_ADD
, -1, &head
, &tail
);
1440 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305_OLD
,
1441 0, 0, 0, CIPHER_ADD
, -1, &head
, &tail
);
1442 ssl_cipher_apply_rule(0, 0, 0, SSL_AES
, 0, 0, 0,
1443 CIPHER_ADD
, -1, &head
, &tail
);
1446 /* Temporarily enable everything else for sorting */
1447 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD
, -1, &head
, &tail
);
1449 /* Low priority for MD5 */
1450 ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5
, 0, 0, CIPHER_ORD
, -1, &head
, &tail
);
1452 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1453 * (For applications that allow them, they aren't too bad, but we prefer
1454 * authenticated ciphers.) */
1455 ssl_cipher_apply_rule(0, 0, SSL_aNULL
, 0, 0, 0, 0, CIPHER_ORD
, -1, &head
, &tail
);
1457 /* Move ciphers without forward secrecy to the end */
1458 ssl_cipher_apply_rule(0, 0, SSL_aECDH
, 0, 0, 0, 0, CIPHER_ORD
, -1, &head
, &tail
);
1459 ssl_cipher_apply_rule(0, SSL_kRSA
, 0, 0, 0, 0, 0, CIPHER_ORD
, -1, &head
, &tail
);
1461 /* RC4 is sort of broken - move it to the end */
1462 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4
, 0, 0, 0, CIPHER_ORD
, -1, &head
, &tail
);
1464 /* Now sort by symmetric encryption strength. The above ordering remains
1465 * in force within each class */
1466 if (!ssl_cipher_strength_sort(&head
, &tail
)) {
1471 /* Now disable everything (maintaining the ordering!) */
1472 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL
, -1, &head
, &tail
);
1476 * We also need cipher aliases for selecting based on the rule_str.
1477 * There might be two types of entries in the rule_str: 1) names
1478 * of ciphers themselves 2) aliases for groups of ciphers.
1479 * For 1) we need the available ciphers and for 2) the cipher
1480 * groups of cipher_aliases added together in one list (otherwise
1481 * we would be happy with just the cipher_aliases table).
1483 num_of_group_aliases
= sizeof(cipher_aliases
) / sizeof(SSL_CIPHER
);
1484 num_of_alias_max
= num_of_ciphers
+ num_of_group_aliases
+ 1;
1485 ca_list
= reallocarray(NULL
, num_of_alias_max
, sizeof(SSL_CIPHER
*));
1486 if (ca_list
== NULL
) {
1488 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST
, ERR_R_MALLOC_FAILURE
);
1489 return(NULL
); /* Failure */
1491 ssl_cipher_collect_aliases(ca_list
, num_of_group_aliases
,
1492 disabled_mkey
, disabled_auth
, disabled_enc
,
1493 disabled_mac
, disabled_ssl
, head
);
1496 * If the rule_string begins with DEFAULT, apply the default rule
1497 * before using the (possibly available) additional rules.
1501 if (strncmp(rule_str
, "DEFAULT", 7) == 0) {
1502 ok
= ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST
,
1503 &head
, &tail
, ca_list
);
1509 if (ok
&& (strlen(rule_p
) > 0))
1510 ok
= ssl_cipher_process_rulestr(rule_p
, &head
, &tail
, ca_list
);
1512 free((void *)ca_list
); /* Not needed anymore */
1515 /* Rule processing failure */
1521 * Allocate new "cipherstack" for the result, return with error
1522 * if we cannot get one.
1524 if ((cipherstack
= sk_SSL_CIPHER_new_null()) == NULL
) {
1530 * The cipher selection for the list is done. The ciphers are added
1531 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1533 for (curr
= head
; curr
!= NULL
; curr
= curr
->next
) {
1535 sk_SSL_CIPHER_push(cipherstack
, curr
->cipher
);
1538 free(co_list
); /* Not needed any longer */
1540 tmp_cipher_list
= sk_SSL_CIPHER_dup(cipherstack
);
1541 if (tmp_cipher_list
== NULL
) {
1542 sk_SSL_CIPHER_free(cipherstack
);
1545 if (*cipher_list
!= NULL
)
1546 sk_SSL_CIPHER_free(*cipher_list
);
1547 *cipher_list
= cipherstack
;
1548 if (*cipher_list_by_id
!= NULL
)
1549 sk_SSL_CIPHER_free(*cipher_list_by_id
);
1550 *cipher_list_by_id
= tmp_cipher_list
;
1551 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id
,
1552 ssl_cipher_ptr_id_cmp
);
1554 sk_SSL_CIPHER_sort(*cipher_list_by_id
);
1555 return (cipherstack
);
1559 SSL_CIPHER_get_by_id(unsigned int id
)
1561 return ssl3_get_cipher_by_id(id
);
1565 SSL_CIPHER_get_by_value(uint16_t value
)
1567 return ssl3_get_cipher_by_value(value
);
1571 SSL_CIPHER_description(const SSL_CIPHER
*cipher
, char *buf
, int len
)
1573 unsigned long alg_mkey
, alg_auth
, alg_enc
, alg_mac
, alg_ssl
, alg2
;
1574 const char *ver
, *kx
, *au
, *enc
, *mac
;
1578 alg_mkey
= cipher
->algorithm_mkey
;
1579 alg_auth
= cipher
->algorithm_auth
;
1580 alg_enc
= cipher
->algorithm_enc
;
1581 alg_mac
= cipher
->algorithm_mac
;
1582 alg_ssl
= cipher
->algorithm_ssl
;
1584 alg2
= cipher
->algorithm2
;
1586 if (alg_ssl
& SSL_SSLV3
)
1588 else if (alg_ssl
& SSL_TLSV1_2
)
1648 enc
= alg2
& SSL2_CF_8_BYTE_ENC
? "RC4(64)" : "RC4(128)";
1663 enc
= "AESGCM(128)";
1666 enc
= "AESGCM(256)";
1668 case SSL_CAMELLIA128
:
1669 enc
= "Camellia(128)";
1671 case SSL_CAMELLIA256
:
1672 enc
= "Camellia(256)";
1674 case SSL_CHACHA20POLY1305
:
1675 enc
= "ChaCha20-Poly1305";
1677 case SSL_CHACHA20POLY1305_OLD
:
1678 enc
= "ChaCha20-Poly1305-Old";
1680 case SSL_eGOST2814789CNT
:
1681 enc
= "GOST-28178-89-CNT";
1710 case SSL_STREEBOG256
:
1711 mac
= "STREEBOG256";
1713 case SSL_STREEBOG512
:
1714 mac
= "STREEBOG512";
1721 if (asprintf(&ret
, "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1722 cipher
->name
, ver
, kx
, au
, enc
, mac
) == -1)
1723 return "OPENSSL_malloc Error";
1726 l
= strlcpy(buf
, ret
, len
);
1730 ret
= "Buffer too small";
1737 SSL_CIPHER_get_version(const SSL_CIPHER
*c
)
1741 if ((c
->id
>> 24) == 3)
1742 return("TLSv1/SSLv3");
1747 /* return the actual cipher being used */
1749 SSL_CIPHER_get_name(const SSL_CIPHER
*c
)
1756 /* number of bits for symmetric cipher */
1758 SSL_CIPHER_get_bits(const SSL_CIPHER
*c
, int *alg_bits
)
1763 if (alg_bits
!= NULL
)
1764 *alg_bits
= c
->alg_bits
;
1765 ret
= c
->strength_bits
;
1771 SSL_CIPHER_get_id(const SSL_CIPHER
*c
)
1777 SSL_CIPHER_get_value(const SSL_CIPHER
*c
)
1779 return ssl3_cipher_get_value(c
);
1783 SSL_COMP_get_compression_methods(void)
1789 SSL_COMP_add_compression_method(int id
, void *cm
)
1795 SSL_COMP_get_name(const void *comp
)