xref: /minix3/crypto/external/bsd/openssl/dist/ssl/ssl_ciph.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/ssl_ciph.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc  * are met:
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc  *    distribution.
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
75ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
82ebfedea0SLionel Sambuc  *
83ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc  *
87ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc  *    acknowledgment:
89ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc  *
92ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc  * ====================================================================
105ebfedea0SLionel Sambuc  *
106ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
108ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc /* ====================================================================
112ebfedea0SLionel Sambuc  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113ebfedea0SLionel Sambuc  * ECC cipher suite support in OpenSSL originally developed by
114ebfedea0SLionel Sambuc  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115ebfedea0SLionel Sambuc  */
116ebfedea0SLionel Sambuc /* ====================================================================
117ebfedea0SLionel Sambuc  * Copyright 2005 Nokia. All rights reserved.
118ebfedea0SLionel Sambuc  *
119ebfedea0SLionel Sambuc  * The portions of the attached software ("Contribution") is developed by
120ebfedea0SLionel Sambuc  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121ebfedea0SLionel Sambuc  * license.
122ebfedea0SLionel Sambuc  *
123ebfedea0SLionel Sambuc  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124ebfedea0SLionel Sambuc  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125ebfedea0SLionel Sambuc  * support (see RFC 4279) to OpenSSL.
126ebfedea0SLionel Sambuc  *
127ebfedea0SLionel Sambuc  * No patent licenses or other rights except those expressly stated in
128ebfedea0SLionel Sambuc  * the OpenSSL open source license shall be deemed granted or received
129ebfedea0SLionel Sambuc  * expressly, by implication, estoppel, or otherwise.
130ebfedea0SLionel Sambuc  *
131ebfedea0SLionel Sambuc  * No assurances are provided by Nokia that the Contribution does not
132ebfedea0SLionel Sambuc  * infringe the patent or other intellectual property rights of any third
133ebfedea0SLionel Sambuc  * party or that the license provides you with all the necessary rights
134ebfedea0SLionel Sambuc  * to make use of the Contribution.
135ebfedea0SLionel Sambuc  *
136ebfedea0SLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137ebfedea0SLionel Sambuc  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138ebfedea0SLionel Sambuc  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139ebfedea0SLionel Sambuc  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140ebfedea0SLionel Sambuc  * OTHERWISE.
141ebfedea0SLionel Sambuc  */
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc #include <stdio.h>
144ebfedea0SLionel Sambuc #include <openssl/objects.h>
145ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
146ebfedea0SLionel Sambuc # include <openssl/comp.h>
147ebfedea0SLionel Sambuc #endif
148ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
149ebfedea0SLionel Sambuc # include <openssl/engine.h>
150ebfedea0SLionel Sambuc #endif
151ebfedea0SLionel Sambuc #include "ssl_locl.h"
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc #define SSL_ENC_DES_IDX         0
154ebfedea0SLionel Sambuc #define SSL_ENC_3DES_IDX        1
155ebfedea0SLionel Sambuc #define SSL_ENC_RC4_IDX         2
156ebfedea0SLionel Sambuc #define SSL_ENC_RC2_IDX         3
157ebfedea0SLionel Sambuc #define SSL_ENC_IDEA_IDX        4
158ebfedea0SLionel Sambuc #define SSL_ENC_NULL_IDX        5
159ebfedea0SLionel Sambuc #define SSL_ENC_AES128_IDX      6
160ebfedea0SLionel Sambuc #define SSL_ENC_AES256_IDX      7
161ebfedea0SLionel Sambuc #define SSL_ENC_CAMELLIA128_IDX 8
162ebfedea0SLionel Sambuc #define SSL_ENC_CAMELLIA256_IDX 9
163ebfedea0SLionel Sambuc #define SSL_ENC_GOST89_IDX      10
164ebfedea0SLionel Sambuc #define SSL_ENC_SEED_IDX        11
165ebfedea0SLionel Sambuc #define SSL_ENC_AES128GCM_IDX   12
166ebfedea0SLionel Sambuc #define SSL_ENC_AES256GCM_IDX   13
167ebfedea0SLionel Sambuc #define SSL_ENC_NUM_IDX         14
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX] = {
170*0a6a1f1dSLionel Sambuc     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
171*0a6a1f1dSLionel Sambuc     NULL, NULL
172ebfedea0SLionel Sambuc };
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc #define SSL_COMP_NULL_IDX       0
175ebfedea0SLionel Sambuc #define SSL_COMP_ZLIB_IDX       1
176ebfedea0SLionel Sambuc #define SSL_COMP_NUM_IDX        2
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc static STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc #define SSL_MD_MD5_IDX  0
181ebfedea0SLionel Sambuc #define SSL_MD_SHA1_IDX 1
182ebfedea0SLionel Sambuc #define SSL_MD_GOST94_IDX 2
183ebfedea0SLionel Sambuc #define SSL_MD_GOST89MAC_IDX 3
184ebfedea0SLionel Sambuc #define SSL_MD_SHA256_IDX 4
185ebfedea0SLionel Sambuc #define SSL_MD_SHA384_IDX 5
186*0a6a1f1dSLionel Sambuc /*
187*0a6a1f1dSLionel Sambuc  * Constant SSL_MAX_DIGEST equal to size of digests array should be defined
188*0a6a1f1dSLionel Sambuc  * in the ssl_locl.h
189*0a6a1f1dSLionel Sambuc  */
190ebfedea0SLionel Sambuc #define SSL_MD_NUM_IDX  SSL_MAX_DIGEST
191ebfedea0SLionel Sambuc static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = {
192ebfedea0SLionel Sambuc     NULL, NULL, NULL, NULL, NULL, NULL
193ebfedea0SLionel Sambuc };
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc /*
196*0a6a1f1dSLionel Sambuc  * PKEY_TYPE for GOST89MAC is known in advance, but, because implementation
197*0a6a1f1dSLionel Sambuc  * is engine-provided, we'll fill it only if corresponding EVP_PKEY_METHOD is
198*0a6a1f1dSLionel Sambuc  * found
199ebfedea0SLionel Sambuc  */
200ebfedea0SLionel Sambuc static int ssl_mac_pkey_id[SSL_MD_NUM_IDX] = {
201ebfedea0SLionel Sambuc     EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
202ebfedea0SLionel Sambuc     EVP_PKEY_HMAC, EVP_PKEY_HMAC
203ebfedea0SLionel Sambuc };
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc static int ssl_mac_secret_size[SSL_MD_NUM_IDX] = {
206ebfedea0SLionel Sambuc     0, 0, 0, 0, 0, 0
207ebfedea0SLionel Sambuc };
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc static int ssl_handshake_digest_flag[SSL_MD_NUM_IDX] = {
210ebfedea0SLionel Sambuc     SSL_HANDSHAKE_MAC_MD5, SSL_HANDSHAKE_MAC_SHA,
211ebfedea0SLionel Sambuc     SSL_HANDSHAKE_MAC_GOST94, 0, SSL_HANDSHAKE_MAC_SHA256,
212ebfedea0SLionel Sambuc     SSL_HANDSHAKE_MAC_SHA384
213ebfedea0SLionel Sambuc };
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc #define CIPHER_ADD      1
216ebfedea0SLionel Sambuc #define CIPHER_KILL     2
217ebfedea0SLionel Sambuc #define CIPHER_DEL      3
218ebfedea0SLionel Sambuc #define CIPHER_ORD      4
219ebfedea0SLionel Sambuc #define CIPHER_SPECIAL  5
220ebfedea0SLionel Sambuc 
221*0a6a1f1dSLionel Sambuc typedef struct cipher_order_st {
222ebfedea0SLionel Sambuc     const SSL_CIPHER *cipher;
223ebfedea0SLionel Sambuc     int active;
224ebfedea0SLionel Sambuc     int dead;
225ebfedea0SLionel Sambuc     struct cipher_order_st *next, *prev;
226ebfedea0SLionel Sambuc } CIPHER_ORDER;
227ebfedea0SLionel Sambuc 
228ebfedea0SLionel Sambuc static const SSL_CIPHER cipher_aliases[] = {
229ebfedea0SLionel Sambuc     /* "ALL" doesn't include eNULL (must be specifically enabled) */
230ebfedea0SLionel Sambuc     {0, SSL_TXT_ALL, 0, 0, 0, ~SSL_eNULL, 0, 0, 0, 0, 0, 0},
231ebfedea0SLionel Sambuc     /* "COMPLEMENTOFALL" */
232ebfedea0SLionel Sambuc     {0, SSL_TXT_CMPALL, 0, 0, 0, SSL_eNULL, 0, 0, 0, 0, 0, 0},
233ebfedea0SLionel Sambuc 
234*0a6a1f1dSLionel Sambuc     /*
235*0a6a1f1dSLionel Sambuc      * "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in
236*0a6a1f1dSLionel Sambuc      * ALL!)
237*0a6a1f1dSLionel Sambuc      */
238*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_CMPDEF, 0, 0, SSL_aNULL, ~SSL_eNULL, 0, ~SSL_SSLV2,
239*0a6a1f1dSLionel Sambuc      SSL_EXP_MASK, 0, 0, 0},
240ebfedea0SLionel Sambuc 
241*0a6a1f1dSLionel Sambuc     /*
242*0a6a1f1dSLionel Sambuc      * key exchange aliases (some of those using only a single bit here
243*0a6a1f1dSLionel Sambuc      * combine multiple key exchange algs according to the RFCs, e.g. kEDH
244*0a6a1f1dSLionel Sambuc      * combines DHE_DSS and DHE_RSA)
245*0a6a1f1dSLionel Sambuc      */
246ebfedea0SLionel Sambuc     {0, SSL_TXT_kRSA, 0, SSL_kRSA, 0, 0, 0, 0, 0, 0, 0, 0},
247ebfedea0SLionel Sambuc 
248*0a6a1f1dSLionel Sambuc     /* no such ciphersuites supported! */
249*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_kDHr, 0, SSL_kDHr, 0, 0, 0, 0, 0, 0, 0, 0},
250*0a6a1f1dSLionel Sambuc     /* no such ciphersuites supported! */
251*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_kDHd, 0, SSL_kDHd, 0, 0, 0, 0, 0, 0, 0, 0},
252*0a6a1f1dSLionel Sambuc     /* no such ciphersuites supported! */
253*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_kDH, 0, SSL_kDHr | SSL_kDHd, 0, 0, 0, 0, 0, 0, 0, 0},
254ebfedea0SLionel Sambuc         {0, SSL_TXT_kEDH, 0, SSL_kEDH, 0, 0, 0, 0, 0, 0, 0, 0},
255*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_DH, 0, SSL_kDHr | SSL_kDHd | SSL_kEDH, 0, 0, 0, 0, 0, 0, 0,
256*0a6a1f1dSLionel Sambuc      0},
257ebfedea0SLionel Sambuc 
258ebfedea0SLionel Sambuc     {0, SSL_TXT_kKRB5, 0, SSL_kKRB5, 0, 0, 0, 0, 0, 0, 0, 0},
259ebfedea0SLionel Sambuc 
260ebfedea0SLionel Sambuc     {0, SSL_TXT_kECDHr, 0, SSL_kECDHr, 0, 0, 0, 0, 0, 0, 0, 0},
261ebfedea0SLionel Sambuc     {0, SSL_TXT_kECDHe, 0, SSL_kECDHe, 0, 0, 0, 0, 0, 0, 0, 0},
262ebfedea0SLionel Sambuc     {0, SSL_TXT_kECDH, 0, SSL_kECDHr | SSL_kECDHe, 0, 0, 0, 0, 0, 0, 0, 0},
263ebfedea0SLionel Sambuc     {0, SSL_TXT_kEECDH, 0, SSL_kEECDH, 0, 0, 0, 0, 0, 0, 0, 0},
264*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_ECDH, 0, SSL_kECDHr | SSL_kECDHe | SSL_kEECDH, 0, 0, 0, 0, 0,
265*0a6a1f1dSLionel Sambuc      0, 0, 0},
266ebfedea0SLionel Sambuc 
267ebfedea0SLionel Sambuc     {0, SSL_TXT_kPSK, 0, SSL_kPSK, 0, 0, 0, 0, 0, 0, 0, 0},
268ebfedea0SLionel Sambuc     {0, SSL_TXT_kSRP, 0, SSL_kSRP, 0, 0, 0, 0, 0, 0, 0, 0},
269ebfedea0SLionel Sambuc     {0, SSL_TXT_kGOST, 0, SSL_kGOST, 0, 0, 0, 0, 0, 0, 0, 0},
270ebfedea0SLionel Sambuc 
271ebfedea0SLionel Sambuc     /* server authentication aliases */
272ebfedea0SLionel Sambuc     {0, SSL_TXT_aRSA, 0, 0, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
273ebfedea0SLionel Sambuc     {0, SSL_TXT_aDSS, 0, 0, SSL_aDSS, 0, 0, 0, 0, 0, 0, 0},
274ebfedea0SLionel Sambuc     {0, SSL_TXT_DSS, 0, 0, SSL_aDSS, 0, 0, 0, 0, 0, 0, 0},
275ebfedea0SLionel Sambuc     {0, SSL_TXT_aKRB5, 0, 0, SSL_aKRB5, 0, 0, 0, 0, 0, 0, 0},
276ebfedea0SLionel Sambuc     {0, SSL_TXT_aNULL, 0, 0, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
277*0a6a1f1dSLionel Sambuc     /* no such ciphersuites supported! */
278*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_aDH, 0, 0, SSL_aDH, 0, 0, 0, 0, 0, 0, 0},
279ebfedea0SLionel Sambuc     {0, SSL_TXT_aECDH, 0, 0, SSL_aECDH, 0, 0, 0, 0, 0, 0, 0},
280ebfedea0SLionel Sambuc     {0, SSL_TXT_aECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
281ebfedea0SLionel Sambuc     {0, SSL_TXT_ECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
282ebfedea0SLionel Sambuc     {0, SSL_TXT_aPSK, 0, 0, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
283ebfedea0SLionel Sambuc     {0, SSL_TXT_aGOST94, 0, 0, SSL_aGOST94, 0, 0, 0, 0, 0, 0, 0},
284ebfedea0SLionel Sambuc     {0, SSL_TXT_aGOST01, 0, 0, SSL_aGOST01, 0, 0, 0, 0, 0, 0, 0},
285ebfedea0SLionel Sambuc     {0, SSL_TXT_aGOST, 0, 0, SSL_aGOST94 | SSL_aGOST01, 0, 0, 0, 0, 0, 0, 0},
286*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_aSRP, 0, 0, SSL_aSRP, 0, 0, 0, 0, 0, 0, 0},
287ebfedea0SLionel Sambuc 
288ebfedea0SLionel Sambuc     /* aliases combining key exchange and server authentication */
289ebfedea0SLionel Sambuc     {0, SSL_TXT_EDH, 0, SSL_kEDH, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
290ebfedea0SLionel Sambuc     {0, SSL_TXT_EECDH, 0, SSL_kEECDH, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
291ebfedea0SLionel Sambuc     {0, SSL_TXT_NULL, 0, 0, 0, SSL_eNULL, 0, 0, 0, 0, 0, 0},
292ebfedea0SLionel Sambuc     {0, SSL_TXT_KRB5, 0, SSL_kKRB5, SSL_aKRB5, 0, 0, 0, 0, 0, 0, 0},
293ebfedea0SLionel Sambuc     {0, SSL_TXT_RSA, 0, SSL_kRSA, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
294ebfedea0SLionel Sambuc     {0, SSL_TXT_ADH, 0, SSL_kEDH, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
295ebfedea0SLionel Sambuc     {0, SSL_TXT_AECDH, 0, SSL_kEECDH, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
296ebfedea0SLionel Sambuc     {0, SSL_TXT_PSK, 0, SSL_kPSK, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
297ebfedea0SLionel Sambuc     {0, SSL_TXT_SRP, 0, SSL_kSRP, 0, 0, 0, 0, 0, 0, 0, 0},
298ebfedea0SLionel Sambuc 
299ebfedea0SLionel Sambuc     /* symmetric encryption aliases */
300ebfedea0SLionel Sambuc     {0, SSL_TXT_DES, 0, 0, 0, SSL_DES, 0, 0, 0, 0, 0, 0},
301ebfedea0SLionel Sambuc     {0, SSL_TXT_3DES, 0, 0, 0, SSL_3DES, 0, 0, 0, 0, 0, 0},
302ebfedea0SLionel Sambuc     {0, SSL_TXT_RC4, 0, 0, 0, SSL_RC4, 0, 0, 0, 0, 0, 0},
303ebfedea0SLionel Sambuc     {0, SSL_TXT_RC2, 0, 0, 0, SSL_RC2, 0, 0, 0, 0, 0, 0},
304ebfedea0SLionel Sambuc     {0, SSL_TXT_IDEA, 0, 0, 0, SSL_IDEA, 0, 0, 0, 0, 0, 0},
305ebfedea0SLionel Sambuc     {0, SSL_TXT_SEED, 0, 0, 0, SSL_SEED, 0, 0, 0, 0, 0, 0},
306ebfedea0SLionel Sambuc     {0, SSL_TXT_eNULL, 0, 0, 0, SSL_eNULL, 0, 0, 0, 0, 0, 0},
307*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_AES128, 0, 0, 0, SSL_AES128 | SSL_AES128GCM, 0, 0, 0, 0, 0,
308*0a6a1f1dSLionel Sambuc      0},
309*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_AES256, 0, 0, 0, SSL_AES256 | SSL_AES256GCM, 0, 0, 0, 0, 0,
310*0a6a1f1dSLionel Sambuc      0},
311ebfedea0SLionel Sambuc     {0, SSL_TXT_AES, 0, 0, 0, SSL_AES, 0, 0, 0, 0, 0, 0},
312*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_AES_GCM, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM, 0, 0, 0, 0,
313*0a6a1f1dSLionel Sambuc      0, 0},
314ebfedea0SLionel Sambuc     {0, SSL_TXT_CAMELLIA128, 0, 0, 0, SSL_CAMELLIA128, 0, 0, 0, 0, 0, 0},
315ebfedea0SLionel Sambuc     {0, SSL_TXT_CAMELLIA256, 0, 0, 0, SSL_CAMELLIA256, 0, 0, 0, 0, 0, 0},
316*0a6a1f1dSLionel Sambuc     {0, SSL_TXT_CAMELLIA, 0, 0, 0, SSL_CAMELLIA128 | SSL_CAMELLIA256, 0, 0, 0,
317*0a6a1f1dSLionel Sambuc      0, 0, 0},
318ebfedea0SLionel Sambuc 
319ebfedea0SLionel Sambuc     /* MAC aliases */
320ebfedea0SLionel Sambuc     {0, SSL_TXT_MD5, 0, 0, 0, 0, SSL_MD5, 0, 0, 0, 0, 0},
321ebfedea0SLionel Sambuc     {0, SSL_TXT_SHA1, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
322ebfedea0SLionel Sambuc     {0, SSL_TXT_SHA, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
323ebfedea0SLionel Sambuc     {0, SSL_TXT_GOST94, 0, 0, 0, 0, SSL_GOST94, 0, 0, 0, 0, 0},
324ebfedea0SLionel Sambuc     {0, SSL_TXT_GOST89MAC, 0, 0, 0, 0, SSL_GOST89MAC, 0, 0, 0, 0, 0},
325ebfedea0SLionel Sambuc     {0, SSL_TXT_SHA256, 0, 0, 0, 0, SSL_SHA256, 0, 0, 0, 0, 0},
326ebfedea0SLionel Sambuc     {0, SSL_TXT_SHA384, 0, 0, 0, 0, SSL_SHA384, 0, 0, 0, 0, 0},
327ebfedea0SLionel Sambuc 
328ebfedea0SLionel Sambuc     /* protocol version aliases */
329ebfedea0SLionel Sambuc     {0, SSL_TXT_SSLV2, 0, 0, 0, 0, 0, SSL_SSLV2, 0, 0, 0, 0},
330ebfedea0SLionel Sambuc     {0, SSL_TXT_SSLV3, 0, 0, 0, 0, 0, SSL_SSLV3, 0, 0, 0, 0},
331ebfedea0SLionel Sambuc     {0, SSL_TXT_TLSV1, 0, 0, 0, 0, 0, SSL_TLSV1, 0, 0, 0, 0},
332ebfedea0SLionel Sambuc     {0, SSL_TXT_TLSV1_2, 0, 0, 0, 0, 0, SSL_TLSV1_2, 0, 0, 0, 0},
333ebfedea0SLionel Sambuc 
334ebfedea0SLionel Sambuc     /* export flag */
335ebfedea0SLionel Sambuc     {0, SSL_TXT_EXP, 0, 0, 0, 0, 0, 0, SSL_EXPORT, 0, 0, 0},
336ebfedea0SLionel Sambuc     {0, SSL_TXT_EXPORT, 0, 0, 0, 0, 0, 0, SSL_EXPORT, 0, 0, 0},
337ebfedea0SLionel Sambuc 
338ebfedea0SLionel Sambuc     /* strength classes */
339ebfedea0SLionel Sambuc     {0, SSL_TXT_EXP40, 0, 0, 0, 0, 0, 0, SSL_EXP40, 0, 0, 0},
340ebfedea0SLionel Sambuc     {0, SSL_TXT_EXP56, 0, 0, 0, 0, 0, 0, SSL_EXP56, 0, 0, 0},
341ebfedea0SLionel Sambuc     {0, SSL_TXT_LOW, 0, 0, 0, 0, 0, 0, SSL_LOW, 0, 0, 0},
342ebfedea0SLionel Sambuc     {0, SSL_TXT_MEDIUM, 0, 0, 0, 0, 0, 0, SSL_MEDIUM, 0, 0, 0},
343ebfedea0SLionel Sambuc     {0, SSL_TXT_HIGH, 0, 0, 0, 0, 0, 0, SSL_HIGH, 0, 0, 0},
344ebfedea0SLionel Sambuc     /* FIPS 140-2 approved ciphersuite */
345ebfedea0SLionel Sambuc     {0, SSL_TXT_FIPS, 0, 0, 0, ~SSL_eNULL, 0, 0, SSL_FIPS, 0, 0, 0},
346ebfedea0SLionel Sambuc };
347*0a6a1f1dSLionel Sambuc 
348*0a6a1f1dSLionel Sambuc /*
349*0a6a1f1dSLionel Sambuc  * Search for public key algorithm with given name and return its pkey_id if
350*0a6a1f1dSLionel Sambuc  * it is available. Otherwise return 0
351ebfedea0SLionel Sambuc  */
352ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_ENGINE
353ebfedea0SLionel Sambuc 
get_optional_pkey_id(const char * pkey_name)354ebfedea0SLionel Sambuc static int get_optional_pkey_id(const char *pkey_name)
355ebfedea0SLionel Sambuc {
356ebfedea0SLionel Sambuc     const EVP_PKEY_ASN1_METHOD *ameth;
357ebfedea0SLionel Sambuc     int pkey_id = 0;
358ebfedea0SLionel Sambuc     ameth = EVP_PKEY_asn1_find_str(NULL, pkey_name, -1);
359*0a6a1f1dSLionel Sambuc     if (ameth) {
360ebfedea0SLionel Sambuc         EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
361ebfedea0SLionel Sambuc     }
362ebfedea0SLionel Sambuc     return pkey_id;
363ebfedea0SLionel Sambuc }
364ebfedea0SLionel Sambuc 
365ebfedea0SLionel Sambuc #else
366ebfedea0SLionel Sambuc 
get_optional_pkey_id(const char * pkey_name)367ebfedea0SLionel Sambuc static int get_optional_pkey_id(const char *pkey_name)
368ebfedea0SLionel Sambuc {
369ebfedea0SLionel Sambuc     const EVP_PKEY_ASN1_METHOD *ameth;
370ebfedea0SLionel Sambuc     ENGINE *tmpeng = NULL;
371ebfedea0SLionel Sambuc     int pkey_id = 0;
372ebfedea0SLionel Sambuc     ameth = EVP_PKEY_asn1_find_str(&tmpeng, pkey_name, -1);
373*0a6a1f1dSLionel Sambuc     if (ameth) {
374ebfedea0SLionel Sambuc         EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
375ebfedea0SLionel Sambuc     }
376*0a6a1f1dSLionel Sambuc     if (tmpeng)
377*0a6a1f1dSLionel Sambuc         ENGINE_finish(tmpeng);
378ebfedea0SLionel Sambuc     return pkey_id;
379ebfedea0SLionel Sambuc }
380ebfedea0SLionel Sambuc 
381ebfedea0SLionel Sambuc #endif
382ebfedea0SLionel Sambuc 
ssl_load_ciphers(void)383ebfedea0SLionel Sambuc void ssl_load_ciphers(void)
384ebfedea0SLionel Sambuc {
385*0a6a1f1dSLionel Sambuc     ssl_cipher_methods[SSL_ENC_DES_IDX] = EVP_get_cipherbyname(SN_des_cbc);
386ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_3DES_IDX] =
387ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_des_ede3_cbc);
388*0a6a1f1dSLionel Sambuc     ssl_cipher_methods[SSL_ENC_RC4_IDX] = EVP_get_cipherbyname(SN_rc4);
389*0a6a1f1dSLionel Sambuc     ssl_cipher_methods[SSL_ENC_RC2_IDX] = EVP_get_cipherbyname(SN_rc2_cbc);
390ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_IDEA
391*0a6a1f1dSLionel Sambuc     ssl_cipher_methods[SSL_ENC_IDEA_IDX] = EVP_get_cipherbyname(SN_idea_cbc);
392ebfedea0SLionel Sambuc #else
393ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_IDEA_IDX] = NULL;
394ebfedea0SLionel Sambuc #endif
395ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_AES128_IDX] =
396ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_aes_128_cbc);
397ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_AES256_IDX] =
398ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_aes_256_cbc);
399ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] =
400ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_camellia_128_cbc);
401ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] =
402ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_camellia_256_cbc);
403ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_GOST89_IDX] =
404ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_gost89_cnt);
405*0a6a1f1dSLionel Sambuc     ssl_cipher_methods[SSL_ENC_SEED_IDX] = EVP_get_cipherbyname(SN_seed_cbc);
406ebfedea0SLionel Sambuc 
407ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] =
408ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_aes_128_gcm);
409ebfedea0SLionel Sambuc     ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] =
410ebfedea0SLionel Sambuc         EVP_get_cipherbyname(SN_aes_256_gcm);
411ebfedea0SLionel Sambuc 
412*0a6a1f1dSLionel Sambuc     ssl_digest_methods[SSL_MD_MD5_IDX] = EVP_get_digestbyname(SN_md5);
413ebfedea0SLionel Sambuc     ssl_mac_secret_size[SSL_MD_MD5_IDX] =
414ebfedea0SLionel Sambuc         EVP_MD_size(ssl_digest_methods[SSL_MD_MD5_IDX]);
415ebfedea0SLionel Sambuc     OPENSSL_assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
416*0a6a1f1dSLionel Sambuc     ssl_digest_methods[SSL_MD_SHA1_IDX] = EVP_get_digestbyname(SN_sha1);
417ebfedea0SLionel Sambuc     ssl_mac_secret_size[SSL_MD_SHA1_IDX] =
418ebfedea0SLionel Sambuc         EVP_MD_size(ssl_digest_methods[SSL_MD_SHA1_IDX]);
419ebfedea0SLionel Sambuc     OPENSSL_assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
420ebfedea0SLionel Sambuc     ssl_digest_methods[SSL_MD_GOST94_IDX] =
421ebfedea0SLionel Sambuc         EVP_get_digestbyname(SN_id_GostR3411_94);
422*0a6a1f1dSLionel Sambuc     if (ssl_digest_methods[SSL_MD_GOST94_IDX]) {
423ebfedea0SLionel Sambuc         ssl_mac_secret_size[SSL_MD_GOST94_IDX] =
424ebfedea0SLionel Sambuc             EVP_MD_size(ssl_digest_methods[SSL_MD_GOST94_IDX]);
425ebfedea0SLionel Sambuc         OPENSSL_assert(ssl_mac_secret_size[SSL_MD_GOST94_IDX] >= 0);
426ebfedea0SLionel Sambuc     }
427ebfedea0SLionel Sambuc     ssl_digest_methods[SSL_MD_GOST89MAC_IDX] =
428ebfedea0SLionel Sambuc         EVP_get_digestbyname(SN_id_Gost28147_89_MAC);
429ebfedea0SLionel Sambuc     ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id("gost-mac");
430ebfedea0SLionel Sambuc     if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]) {
431ebfedea0SLionel Sambuc         ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
432ebfedea0SLionel Sambuc     }
433ebfedea0SLionel Sambuc 
434*0a6a1f1dSLionel Sambuc     ssl_digest_methods[SSL_MD_SHA256_IDX] = EVP_get_digestbyname(SN_sha256);
435ebfedea0SLionel Sambuc     ssl_mac_secret_size[SSL_MD_SHA256_IDX] =
436ebfedea0SLionel Sambuc         EVP_MD_size(ssl_digest_methods[SSL_MD_SHA256_IDX]);
437*0a6a1f1dSLionel Sambuc     ssl_digest_methods[SSL_MD_SHA384_IDX] = EVP_get_digestbyname(SN_sha384);
438ebfedea0SLionel Sambuc     ssl_mac_secret_size[SSL_MD_SHA384_IDX] =
439ebfedea0SLionel Sambuc         EVP_MD_size(ssl_digest_methods[SSL_MD_SHA384_IDX]);
440ebfedea0SLionel Sambuc }
441*0a6a1f1dSLionel Sambuc 
442ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
443ebfedea0SLionel Sambuc 
sk_comp_cmp(const SSL_COMP * const * a,const SSL_COMP * const * b)444*0a6a1f1dSLionel Sambuc static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b)
445ebfedea0SLionel Sambuc {
446ebfedea0SLionel Sambuc     return ((*a)->id - (*b)->id);
447ebfedea0SLionel Sambuc }
448ebfedea0SLionel Sambuc 
load_builtin_compressions(void)449ebfedea0SLionel Sambuc static void load_builtin_compressions(void)
450ebfedea0SLionel Sambuc {
451ebfedea0SLionel Sambuc     int got_write_lock = 0;
452ebfedea0SLionel Sambuc 
453ebfedea0SLionel Sambuc     CRYPTO_r_lock(CRYPTO_LOCK_SSL);
454*0a6a1f1dSLionel Sambuc     if (ssl_comp_methods == NULL) {
455ebfedea0SLionel Sambuc         CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
456ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_SSL);
457ebfedea0SLionel Sambuc         got_write_lock = 1;
458ebfedea0SLionel Sambuc 
459*0a6a1f1dSLionel Sambuc         if (ssl_comp_methods == NULL) {
460ebfedea0SLionel Sambuc             SSL_COMP *comp = NULL;
461ebfedea0SLionel Sambuc 
462ebfedea0SLionel Sambuc             MemCheck_off();
463ebfedea0SLionel Sambuc             ssl_comp_methods = sk_SSL_COMP_new(sk_comp_cmp);
464*0a6a1f1dSLionel Sambuc             if (ssl_comp_methods != NULL) {
465ebfedea0SLionel Sambuc                 comp = (SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
466*0a6a1f1dSLionel Sambuc                 if (comp != NULL) {
467ebfedea0SLionel Sambuc                     comp->method = COMP_zlib();
468*0a6a1f1dSLionel Sambuc                     if (comp->method && comp->method->type == NID_undef)
469ebfedea0SLionel Sambuc                         OPENSSL_free(comp);
470*0a6a1f1dSLionel Sambuc                     else {
471ebfedea0SLionel Sambuc                         comp->id = SSL_COMP_ZLIB_IDX;
472ebfedea0SLionel Sambuc                         comp->name = comp->method->name;
473ebfedea0SLionel Sambuc                         sk_SSL_COMP_push(ssl_comp_methods, comp);
474ebfedea0SLionel Sambuc                     }
475ebfedea0SLionel Sambuc                 }
476ebfedea0SLionel Sambuc                 sk_SSL_COMP_sort(ssl_comp_methods);
477ebfedea0SLionel Sambuc             }
478ebfedea0SLionel Sambuc             MemCheck_on();
479ebfedea0SLionel Sambuc         }
480ebfedea0SLionel Sambuc     }
481ebfedea0SLionel Sambuc 
482ebfedea0SLionel Sambuc     if (got_write_lock)
483ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
484ebfedea0SLionel Sambuc     else
485ebfedea0SLionel Sambuc         CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
486ebfedea0SLionel Sambuc }
487ebfedea0SLionel Sambuc #endif
488ebfedea0SLionel Sambuc 
ssl_cipher_get_evp(const SSL_SESSION * s,const EVP_CIPHER ** enc,const EVP_MD ** md,int * mac_pkey_type,int * mac_secret_size,SSL_COMP ** comp)489ebfedea0SLionel Sambuc int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
490*0a6a1f1dSLionel Sambuc                        const EVP_MD **md, int *mac_pkey_type,
491*0a6a1f1dSLionel Sambuc                        int *mac_secret_size, SSL_COMP **comp)
492ebfedea0SLionel Sambuc {
493ebfedea0SLionel Sambuc     int i;
494ebfedea0SLionel Sambuc     const SSL_CIPHER *c;
495ebfedea0SLionel Sambuc 
496ebfedea0SLionel Sambuc     c = s->cipher;
497*0a6a1f1dSLionel Sambuc     if (c == NULL)
498*0a6a1f1dSLionel Sambuc         return (0);
499*0a6a1f1dSLionel Sambuc     if (comp != NULL) {
500ebfedea0SLionel Sambuc         SSL_COMP ctmp;
501ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
502ebfedea0SLionel Sambuc         load_builtin_compressions();
503ebfedea0SLionel Sambuc #endif
504ebfedea0SLionel Sambuc 
505ebfedea0SLionel Sambuc         *comp = NULL;
506ebfedea0SLionel Sambuc         ctmp.id = s->compress_meth;
507*0a6a1f1dSLionel Sambuc         if (ssl_comp_methods != NULL) {
508ebfedea0SLionel Sambuc             i = sk_SSL_COMP_find(ssl_comp_methods, &ctmp);
509ebfedea0SLionel Sambuc             if (i >= 0)
510ebfedea0SLionel Sambuc                 *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
511ebfedea0SLionel Sambuc             else
512ebfedea0SLionel Sambuc                 *comp = NULL;
513ebfedea0SLionel Sambuc         }
514ebfedea0SLionel Sambuc     }
515ebfedea0SLionel Sambuc 
516*0a6a1f1dSLionel Sambuc     if ((enc == NULL) || (md == NULL))
517*0a6a1f1dSLionel Sambuc         return (0);
518ebfedea0SLionel Sambuc 
519*0a6a1f1dSLionel Sambuc     switch (c->algorithm_enc) {
520ebfedea0SLionel Sambuc     case SSL_DES:
521ebfedea0SLionel Sambuc         i = SSL_ENC_DES_IDX;
522ebfedea0SLionel Sambuc         break;
523ebfedea0SLionel Sambuc     case SSL_3DES:
524ebfedea0SLionel Sambuc         i = SSL_ENC_3DES_IDX;
525ebfedea0SLionel Sambuc         break;
526ebfedea0SLionel Sambuc     case SSL_RC4:
527ebfedea0SLionel Sambuc         i = SSL_ENC_RC4_IDX;
528ebfedea0SLionel Sambuc         break;
529ebfedea0SLionel Sambuc     case SSL_RC2:
530ebfedea0SLionel Sambuc         i = SSL_ENC_RC2_IDX;
531ebfedea0SLionel Sambuc         break;
532ebfedea0SLionel Sambuc     case SSL_IDEA:
533ebfedea0SLionel Sambuc         i = SSL_ENC_IDEA_IDX;
534ebfedea0SLionel Sambuc         break;
535ebfedea0SLionel Sambuc     case SSL_eNULL:
536ebfedea0SLionel Sambuc         i = SSL_ENC_NULL_IDX;
537ebfedea0SLionel Sambuc         break;
538ebfedea0SLionel Sambuc     case SSL_AES128:
539ebfedea0SLionel Sambuc         i = SSL_ENC_AES128_IDX;
540ebfedea0SLionel Sambuc         break;
541ebfedea0SLionel Sambuc     case SSL_AES256:
542ebfedea0SLionel Sambuc         i = SSL_ENC_AES256_IDX;
543ebfedea0SLionel Sambuc         break;
544ebfedea0SLionel Sambuc     case SSL_CAMELLIA128:
545ebfedea0SLionel Sambuc         i = SSL_ENC_CAMELLIA128_IDX;
546ebfedea0SLionel Sambuc         break;
547ebfedea0SLionel Sambuc     case SSL_CAMELLIA256:
548ebfedea0SLionel Sambuc         i = SSL_ENC_CAMELLIA256_IDX;
549ebfedea0SLionel Sambuc         break;
550ebfedea0SLionel Sambuc     case SSL_eGOST2814789CNT:
551ebfedea0SLionel Sambuc         i = SSL_ENC_GOST89_IDX;
552ebfedea0SLionel Sambuc         break;
553ebfedea0SLionel Sambuc     case SSL_SEED:
554ebfedea0SLionel Sambuc         i = SSL_ENC_SEED_IDX;
555ebfedea0SLionel Sambuc         break;
556ebfedea0SLionel Sambuc     case SSL_AES128GCM:
557ebfedea0SLionel Sambuc         i = SSL_ENC_AES128GCM_IDX;
558ebfedea0SLionel Sambuc         break;
559ebfedea0SLionel Sambuc     case SSL_AES256GCM:
560ebfedea0SLionel Sambuc         i = SSL_ENC_AES256GCM_IDX;
561ebfedea0SLionel Sambuc         break;
562ebfedea0SLionel Sambuc     default:
563ebfedea0SLionel Sambuc         i = -1;
564ebfedea0SLionel Sambuc         break;
565ebfedea0SLionel Sambuc     }
566ebfedea0SLionel Sambuc 
567ebfedea0SLionel Sambuc     if ((i < 0) || (i >= SSL_ENC_NUM_IDX))
568ebfedea0SLionel Sambuc         *enc = NULL;
569*0a6a1f1dSLionel Sambuc     else {
570ebfedea0SLionel Sambuc         if (i == SSL_ENC_NULL_IDX)
571ebfedea0SLionel Sambuc             *enc = EVP_enc_null();
572ebfedea0SLionel Sambuc         else
573ebfedea0SLionel Sambuc             *enc = ssl_cipher_methods[i];
574ebfedea0SLionel Sambuc     }
575ebfedea0SLionel Sambuc 
576*0a6a1f1dSLionel Sambuc     switch (c->algorithm_mac) {
577ebfedea0SLionel Sambuc     case SSL_MD5:
578ebfedea0SLionel Sambuc         i = SSL_MD_MD5_IDX;
579ebfedea0SLionel Sambuc         break;
580ebfedea0SLionel Sambuc     case SSL_SHA1:
581ebfedea0SLionel Sambuc         i = SSL_MD_SHA1_IDX;
582ebfedea0SLionel Sambuc         break;
583ebfedea0SLionel Sambuc     case SSL_SHA256:
584ebfedea0SLionel Sambuc         i = SSL_MD_SHA256_IDX;
585ebfedea0SLionel Sambuc         break;
586ebfedea0SLionel Sambuc     case SSL_SHA384:
587ebfedea0SLionel Sambuc         i = SSL_MD_SHA384_IDX;
588ebfedea0SLionel Sambuc         break;
589ebfedea0SLionel Sambuc     case SSL_GOST94:
590ebfedea0SLionel Sambuc         i = SSL_MD_GOST94_IDX;
591ebfedea0SLionel Sambuc         break;
592ebfedea0SLionel Sambuc     case SSL_GOST89MAC:
593ebfedea0SLionel Sambuc         i = SSL_MD_GOST89MAC_IDX;
594ebfedea0SLionel Sambuc         break;
595ebfedea0SLionel Sambuc     default:
596ebfedea0SLionel Sambuc         i = -1;
597ebfedea0SLionel Sambuc         break;
598ebfedea0SLionel Sambuc     }
599*0a6a1f1dSLionel Sambuc     if ((i < 0) || (i >= SSL_MD_NUM_IDX)) {
600ebfedea0SLionel Sambuc         *md = NULL;
601*0a6a1f1dSLionel Sambuc         if (mac_pkey_type != NULL)
602*0a6a1f1dSLionel Sambuc             *mac_pkey_type = NID_undef;
603*0a6a1f1dSLionel Sambuc         if (mac_secret_size != NULL)
604*0a6a1f1dSLionel Sambuc             *mac_secret_size = 0;
605ebfedea0SLionel Sambuc         if (c->algorithm_mac == SSL_AEAD)
606ebfedea0SLionel Sambuc             mac_pkey_type = NULL;
607*0a6a1f1dSLionel Sambuc     } else {
608ebfedea0SLionel Sambuc         *md = ssl_digest_methods[i];
609*0a6a1f1dSLionel Sambuc         if (mac_pkey_type != NULL)
610*0a6a1f1dSLionel Sambuc             *mac_pkey_type = ssl_mac_pkey_id[i];
611*0a6a1f1dSLionel Sambuc         if (mac_secret_size != NULL)
612*0a6a1f1dSLionel Sambuc             *mac_secret_size = ssl_mac_secret_size[i];
613ebfedea0SLionel Sambuc     }
614ebfedea0SLionel Sambuc 
615ebfedea0SLionel Sambuc     if ((*enc != NULL) &&
616*0a6a1f1dSLionel Sambuc         (*md != NULL || (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
617*0a6a1f1dSLionel Sambuc         && (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
618ebfedea0SLionel Sambuc         const EVP_CIPHER *evp;
619ebfedea0SLionel Sambuc 
620ebfedea0SLionel Sambuc         if (s->ssl_version >> 8 != TLS1_VERSION_MAJOR ||
621ebfedea0SLionel Sambuc             s->ssl_version < TLS1_VERSION)
622ebfedea0SLionel Sambuc             return 1;
623ebfedea0SLionel Sambuc 
624ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
625ebfedea0SLionel Sambuc         if (FIPS_mode())
626ebfedea0SLionel Sambuc             return 1;
627ebfedea0SLionel Sambuc #endif
628ebfedea0SLionel Sambuc 
629ebfedea0SLionel Sambuc         if (c->algorithm_enc == SSL_RC4 &&
630ebfedea0SLionel Sambuc             c->algorithm_mac == SSL_MD5 &&
631ebfedea0SLionel Sambuc             (evp = EVP_get_cipherbyname("RC4-HMAC-MD5")))
632ebfedea0SLionel Sambuc             *enc = evp, *md = NULL;
633ebfedea0SLionel Sambuc         else if (c->algorithm_enc == SSL_AES128 &&
634ebfedea0SLionel Sambuc                  c->algorithm_mac == SSL_SHA1 &&
635ebfedea0SLionel Sambuc                  (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
636ebfedea0SLionel Sambuc             *enc = evp, *md = NULL;
637ebfedea0SLionel Sambuc         else if (c->algorithm_enc == SSL_AES256 &&
638ebfedea0SLionel Sambuc                  c->algorithm_mac == SSL_SHA1 &&
639ebfedea0SLionel Sambuc                  (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
640ebfedea0SLionel Sambuc             *enc = evp, *md = NULL;
641ebfedea0SLionel Sambuc         return (1);
642*0a6a1f1dSLionel Sambuc     } else
643ebfedea0SLionel Sambuc         return (0);
644ebfedea0SLionel Sambuc }
645ebfedea0SLionel Sambuc 
ssl_get_handshake_digest(int idx,long * mask,const EVP_MD ** md)646ebfedea0SLionel Sambuc int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
647ebfedea0SLionel Sambuc {
648*0a6a1f1dSLionel Sambuc     if (idx < 0 || idx >= SSL_MD_NUM_IDX) {
649ebfedea0SLionel Sambuc         return 0;
650ebfedea0SLionel Sambuc     }
651ebfedea0SLionel Sambuc     *mask = ssl_handshake_digest_flag[idx];
652ebfedea0SLionel Sambuc     if (*mask)
653ebfedea0SLionel Sambuc         *md = ssl_digest_methods[idx];
654ebfedea0SLionel Sambuc     else
655ebfedea0SLionel Sambuc         *md = NULL;
656ebfedea0SLionel Sambuc     return 1;
657ebfedea0SLionel Sambuc }
658ebfedea0SLionel Sambuc 
659ebfedea0SLionel Sambuc #define ITEM_SEP(a) \
660ebfedea0SLionel Sambuc         (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
661ebfedea0SLionel Sambuc 
ll_append_tail(CIPHER_ORDER ** head,CIPHER_ORDER * curr,CIPHER_ORDER ** tail)662ebfedea0SLionel Sambuc static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
663ebfedea0SLionel Sambuc                            CIPHER_ORDER **tail)
664ebfedea0SLionel Sambuc {
665*0a6a1f1dSLionel Sambuc     if (curr == *tail)
666*0a6a1f1dSLionel Sambuc         return;
667ebfedea0SLionel Sambuc     if (curr == *head)
668ebfedea0SLionel Sambuc         *head = curr->next;
669ebfedea0SLionel Sambuc     if (curr->prev != NULL)
670ebfedea0SLionel Sambuc         curr->prev->next = curr->next;
671ebfedea0SLionel Sambuc     if (curr->next != NULL)
672ebfedea0SLionel Sambuc         curr->next->prev = curr->prev;
673ebfedea0SLionel Sambuc     (*tail)->next = curr;
674ebfedea0SLionel Sambuc     curr->prev = *tail;
675ebfedea0SLionel Sambuc     curr->next = NULL;
676ebfedea0SLionel Sambuc     *tail = curr;
677ebfedea0SLionel Sambuc }
678ebfedea0SLionel Sambuc 
ll_append_head(CIPHER_ORDER ** head,CIPHER_ORDER * curr,CIPHER_ORDER ** tail)679ebfedea0SLionel Sambuc static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
680ebfedea0SLionel Sambuc                            CIPHER_ORDER **tail)
681ebfedea0SLionel Sambuc {
682*0a6a1f1dSLionel Sambuc     if (curr == *head)
683*0a6a1f1dSLionel Sambuc         return;
684ebfedea0SLionel Sambuc     if (curr == *tail)
685ebfedea0SLionel Sambuc         *tail = curr->prev;
686ebfedea0SLionel Sambuc     if (curr->next != NULL)
687ebfedea0SLionel Sambuc         curr->next->prev = curr->prev;
688ebfedea0SLionel Sambuc     if (curr->prev != NULL)
689ebfedea0SLionel Sambuc         curr->prev->next = curr->next;
690ebfedea0SLionel Sambuc     (*head)->prev = curr;
691ebfedea0SLionel Sambuc     curr->next = *head;
692ebfedea0SLionel Sambuc     curr->prev = NULL;
693ebfedea0SLionel Sambuc     *head = curr;
694ebfedea0SLionel Sambuc }
695ebfedea0SLionel Sambuc 
ssl_cipher_get_disabled(unsigned long * mkey,unsigned long * auth,unsigned long * enc,unsigned long * mac,unsigned long * ssl)696*0a6a1f1dSLionel Sambuc static void ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth,
697*0a6a1f1dSLionel Sambuc                                     unsigned long *enc, unsigned long *mac,
698*0a6a1f1dSLionel Sambuc                                     unsigned long *ssl)
699ebfedea0SLionel Sambuc {
700ebfedea0SLionel Sambuc     *mkey = 0;
701ebfedea0SLionel Sambuc     *auth = 0;
702ebfedea0SLionel Sambuc     *enc = 0;
703ebfedea0SLionel Sambuc     *mac = 0;
704ebfedea0SLionel Sambuc     *ssl = 0;
705ebfedea0SLionel Sambuc 
706ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_RSA
707ebfedea0SLionel Sambuc     *mkey |= SSL_kRSA;
708ebfedea0SLionel Sambuc     *auth |= SSL_aRSA;
709ebfedea0SLionel Sambuc #endif
710ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DSA
711ebfedea0SLionel Sambuc     *auth |= SSL_aDSS;
712ebfedea0SLionel Sambuc #endif
713ebfedea0SLionel Sambuc     *mkey |= SSL_kDHr | SSL_kDHd; /* no such ciphersuites supported! */
714ebfedea0SLionel Sambuc     *auth |= SSL_aDH;
715ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DH
716ebfedea0SLionel Sambuc     *mkey |= SSL_kDHr | SSL_kDHd | SSL_kEDH;
717ebfedea0SLionel Sambuc     *auth |= SSL_aDH;
718ebfedea0SLionel Sambuc #endif
719ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_KRB5
720ebfedea0SLionel Sambuc     *mkey |= SSL_kKRB5;
721ebfedea0SLionel Sambuc     *auth |= SSL_aKRB5;
722ebfedea0SLionel Sambuc #endif
723ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_ECDSA
724ebfedea0SLionel Sambuc     *auth |= SSL_aECDSA;
725ebfedea0SLionel Sambuc #endif
726ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_ECDH
727ebfedea0SLionel Sambuc     *mkey |= SSL_kECDHe | SSL_kECDHr;
728ebfedea0SLionel Sambuc     *auth |= SSL_aECDH;
729ebfedea0SLionel Sambuc #endif
730ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_PSK
731ebfedea0SLionel Sambuc     *mkey |= SSL_kPSK;
732ebfedea0SLionel Sambuc     *auth |= SSL_aPSK;
733ebfedea0SLionel Sambuc #endif
734ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_SRP
735ebfedea0SLionel Sambuc     *mkey |= SSL_kSRP;
736ebfedea0SLionel Sambuc #endif
737*0a6a1f1dSLionel Sambuc     /*
738*0a6a1f1dSLionel Sambuc      * Check for presence of GOST 34.10 algorithms, and if they do not
739*0a6a1f1dSLionel Sambuc      * present, disable appropriate auth and key exchange
740*0a6a1f1dSLionel Sambuc      */
741ebfedea0SLionel Sambuc     if (!get_optional_pkey_id("gost94")) {
742ebfedea0SLionel Sambuc         *auth |= SSL_aGOST94;
743ebfedea0SLionel Sambuc     }
744ebfedea0SLionel Sambuc     if (!get_optional_pkey_id("gost2001")) {
745ebfedea0SLionel Sambuc         *auth |= SSL_aGOST01;
746ebfedea0SLionel Sambuc     }
747*0a6a1f1dSLionel Sambuc     /*
748*0a6a1f1dSLionel Sambuc      * Disable GOST key exchange if no GOST signature algs are available *
749*0a6a1f1dSLionel Sambuc      */
750ebfedea0SLionel Sambuc     if ((*auth & (SSL_aGOST94 | SSL_aGOST01)) == (SSL_aGOST94 | SSL_aGOST01)) {
751ebfedea0SLionel Sambuc         *mkey |= SSL_kGOST;
752ebfedea0SLionel Sambuc     }
753ebfedea0SLionel Sambuc #ifdef SSL_FORBID_ENULL
754ebfedea0SLionel Sambuc     *enc |= SSL_eNULL;
755ebfedea0SLionel Sambuc #endif
756ebfedea0SLionel Sambuc 
757ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_DES_IDX] == NULL) ? SSL_DES : 0;
758ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES : 0;
759ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX] == NULL) ? SSL_RC4 : 0;
760ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_RC2_IDX] == NULL) ? SSL_RC2 : 0;
761ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA : 0;
762ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128 : 0;
763ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256 : 0;
764*0a6a1f1dSLionel Sambuc     *enc |=
765*0a6a1f1dSLionel Sambuc         (ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] ==
766*0a6a1f1dSLionel Sambuc          NULL) ? SSL_AES128GCM : 0;
767*0a6a1f1dSLionel Sambuc     *enc |=
768*0a6a1f1dSLionel Sambuc         (ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] ==
769*0a6a1f1dSLionel Sambuc          NULL) ? SSL_AES256GCM : 0;
770*0a6a1f1dSLionel Sambuc     *enc |=
771*0a6a1f1dSLionel Sambuc         (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] ==
772*0a6a1f1dSLionel Sambuc          NULL) ? SSL_CAMELLIA128 : 0;
773*0a6a1f1dSLionel Sambuc     *enc |=
774*0a6a1f1dSLionel Sambuc         (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] ==
775*0a6a1f1dSLionel Sambuc          NULL) ? SSL_CAMELLIA256 : 0;
776*0a6a1f1dSLionel Sambuc     *enc |=
777*0a6a1f1dSLionel Sambuc         (ssl_cipher_methods[SSL_ENC_GOST89_IDX] ==
778*0a6a1f1dSLionel Sambuc          NULL) ? SSL_eGOST2814789CNT : 0;
779ebfedea0SLionel Sambuc     *enc |= (ssl_cipher_methods[SSL_ENC_SEED_IDX] == NULL) ? SSL_SEED : 0;
780ebfedea0SLionel Sambuc 
781ebfedea0SLionel Sambuc     *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX] == NULL) ? SSL_MD5 : 0;
782ebfedea0SLionel Sambuc     *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1 : 0;
783ebfedea0SLionel Sambuc     *mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256 : 0;
784ebfedea0SLionel Sambuc     *mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384 : 0;
785ebfedea0SLionel Sambuc     *mac |= (ssl_digest_methods[SSL_MD_GOST94_IDX] == NULL) ? SSL_GOST94 : 0;
786*0a6a1f1dSLionel Sambuc     *mac |= (ssl_digest_methods[SSL_MD_GOST89MAC_IDX] == NULL
787*0a6a1f1dSLionel Sambuc              || ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] ==
788*0a6a1f1dSLionel Sambuc              NID_undef) ? SSL_GOST89MAC : 0;
789ebfedea0SLionel Sambuc 
790ebfedea0SLionel Sambuc }
791ebfedea0SLionel Sambuc 
ssl_cipher_collect_ciphers(const SSL_METHOD * ssl_method,int num_of_ciphers,unsigned long disabled_mkey,unsigned long disabled_auth,unsigned long disabled_enc,unsigned long disabled_mac,unsigned long disabled_ssl,CIPHER_ORDER * co_list,CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p)792ebfedea0SLionel Sambuc static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
793ebfedea0SLionel Sambuc                                        int num_of_ciphers,
794*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_mkey,
795*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_auth,
796*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_enc,
797*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_mac,
798ebfedea0SLionel Sambuc                                        unsigned long disabled_ssl,
799ebfedea0SLionel Sambuc                                        CIPHER_ORDER *co_list,
800*0a6a1f1dSLionel Sambuc                                        CIPHER_ORDER **head_p,
801*0a6a1f1dSLionel Sambuc                                        CIPHER_ORDER **tail_p)
802ebfedea0SLionel Sambuc {
803ebfedea0SLionel Sambuc     int i, co_list_num;
804ebfedea0SLionel Sambuc     const SSL_CIPHER *c;
805ebfedea0SLionel Sambuc 
806ebfedea0SLionel Sambuc     /*
807ebfedea0SLionel Sambuc      * We have num_of_ciphers descriptions compiled in, depending on the
808ebfedea0SLionel Sambuc      * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
809ebfedea0SLionel Sambuc      * These will later be sorted in a linked list with at most num
810ebfedea0SLionel Sambuc      * entries.
811ebfedea0SLionel Sambuc      */
812ebfedea0SLionel Sambuc 
813ebfedea0SLionel Sambuc     /* Get the initial list of ciphers */
814ebfedea0SLionel Sambuc     co_list_num = 0;            /* actual count of ciphers */
815*0a6a1f1dSLionel Sambuc     for (i = 0; i < num_of_ciphers; i++) {
816ebfedea0SLionel Sambuc         c = ssl_method->get_cipher(i);
817ebfedea0SLionel Sambuc         /* drop those that use any of that is not available */
818ebfedea0SLionel Sambuc         if ((c != NULL) && c->valid &&
819ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
820ebfedea0SLionel Sambuc             (!FIPS_mode() || (c->algo_strength & SSL_FIPS)) &&
821ebfedea0SLionel Sambuc #endif
822ebfedea0SLionel Sambuc             !(c->algorithm_mkey & disabled_mkey) &&
823ebfedea0SLionel Sambuc             !(c->algorithm_auth & disabled_auth) &&
824ebfedea0SLionel Sambuc             !(c->algorithm_enc & disabled_enc) &&
825ebfedea0SLionel Sambuc             !(c->algorithm_mac & disabled_mac) &&
826*0a6a1f1dSLionel Sambuc             !(c->algorithm_ssl & disabled_ssl)) {
827ebfedea0SLionel Sambuc             co_list[co_list_num].cipher = c;
828ebfedea0SLionel Sambuc             co_list[co_list_num].next = NULL;
829ebfedea0SLionel Sambuc             co_list[co_list_num].prev = NULL;
830ebfedea0SLionel Sambuc             co_list[co_list_num].active = 0;
831ebfedea0SLionel Sambuc             co_list_num++;
832ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
833*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\t%d: %s %lx %lx %lx\n", i, c->name, c->id,
834*0a6a1f1dSLionel Sambuc                     c->algorithm_mkey, c->algorithm_auth);
835ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
836ebfedea0SLionel Sambuc             /*
837*0a6a1f1dSLionel Sambuc              * if (!sk_push(ca_list,(char *)c)) goto err;
838ebfedea0SLionel Sambuc              */
839ebfedea0SLionel Sambuc         }
840ebfedea0SLionel Sambuc     }
841ebfedea0SLionel Sambuc 
842ebfedea0SLionel Sambuc     /*
843ebfedea0SLionel Sambuc      * Prepare linked list from list entries
844ebfedea0SLionel Sambuc      */
845*0a6a1f1dSLionel Sambuc     if (co_list_num > 0) {
846ebfedea0SLionel Sambuc         co_list[0].prev = NULL;
847ebfedea0SLionel Sambuc 
848*0a6a1f1dSLionel Sambuc         if (co_list_num > 1) {
849ebfedea0SLionel Sambuc             co_list[0].next = &co_list[1];
850ebfedea0SLionel Sambuc 
851*0a6a1f1dSLionel Sambuc             for (i = 1; i < co_list_num - 1; i++) {
852ebfedea0SLionel Sambuc                 co_list[i].prev = &co_list[i - 1];
853ebfedea0SLionel Sambuc                 co_list[i].next = &co_list[i + 1];
854ebfedea0SLionel Sambuc             }
855ebfedea0SLionel Sambuc 
856ebfedea0SLionel Sambuc             co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
857ebfedea0SLionel Sambuc         }
858ebfedea0SLionel Sambuc 
859ebfedea0SLionel Sambuc         co_list[co_list_num - 1].next = NULL;
860ebfedea0SLionel Sambuc 
861ebfedea0SLionel Sambuc         *head_p = &co_list[0];
862ebfedea0SLionel Sambuc         *tail_p = &co_list[co_list_num - 1];
863ebfedea0SLionel Sambuc     }
864ebfedea0SLionel Sambuc }
865ebfedea0SLionel Sambuc 
ssl_cipher_collect_aliases(const SSL_CIPHER ** ca_list,int num_of_group_aliases,unsigned long disabled_mkey,unsigned long disabled_auth,unsigned long disabled_enc,unsigned long disabled_mac,unsigned long disabled_ssl,CIPHER_ORDER * head)866ebfedea0SLionel Sambuc static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
867ebfedea0SLionel Sambuc                                        int num_of_group_aliases,
868*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_mkey,
869*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_auth,
870*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_enc,
871*0a6a1f1dSLionel Sambuc                                        unsigned long disabled_mac,
872ebfedea0SLionel Sambuc                                        unsigned long disabled_ssl,
873ebfedea0SLionel Sambuc                                        CIPHER_ORDER *head)
874ebfedea0SLionel Sambuc {
875ebfedea0SLionel Sambuc     CIPHER_ORDER *ciph_curr;
876ebfedea0SLionel Sambuc     const SSL_CIPHER **ca_curr;
877ebfedea0SLionel Sambuc     int i;
878ebfedea0SLionel Sambuc     unsigned long mask_mkey = ~disabled_mkey;
879ebfedea0SLionel Sambuc     unsigned long mask_auth = ~disabled_auth;
880ebfedea0SLionel Sambuc     unsigned long mask_enc = ~disabled_enc;
881ebfedea0SLionel Sambuc     unsigned long mask_mac = ~disabled_mac;
882ebfedea0SLionel Sambuc     unsigned long mask_ssl = ~disabled_ssl;
883ebfedea0SLionel Sambuc 
884ebfedea0SLionel Sambuc     /*
885ebfedea0SLionel Sambuc      * First, add the real ciphers as already collected
886ebfedea0SLionel Sambuc      */
887ebfedea0SLionel Sambuc     ciph_curr = head;
888ebfedea0SLionel Sambuc     ca_curr = ca_list;
889*0a6a1f1dSLionel Sambuc     while (ciph_curr != NULL) {
890ebfedea0SLionel Sambuc         *ca_curr = ciph_curr->cipher;
891ebfedea0SLionel Sambuc         ca_curr++;
892ebfedea0SLionel Sambuc         ciph_curr = ciph_curr->next;
893ebfedea0SLionel Sambuc     }
894ebfedea0SLionel Sambuc 
895ebfedea0SLionel Sambuc     /*
896ebfedea0SLionel Sambuc      * Now we add the available ones from the cipher_aliases[] table.
897ebfedea0SLionel Sambuc      * They represent either one or more algorithms, some of which
898ebfedea0SLionel Sambuc      * in any affected category must be supported (set in enabled_mask),
899ebfedea0SLionel Sambuc      * or represent a cipher strength value (will be added in any case because algorithms=0).
900ebfedea0SLionel Sambuc      */
901*0a6a1f1dSLionel Sambuc     for (i = 0; i < num_of_group_aliases; i++) {
902ebfedea0SLionel Sambuc         unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
903ebfedea0SLionel Sambuc         unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
904ebfedea0SLionel Sambuc         unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
905ebfedea0SLionel Sambuc         unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
906ebfedea0SLionel Sambuc         unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
907ebfedea0SLionel Sambuc 
908ebfedea0SLionel Sambuc         if (algorithm_mkey)
909ebfedea0SLionel Sambuc             if ((algorithm_mkey & mask_mkey) == 0)
910ebfedea0SLionel Sambuc                 continue;
911ebfedea0SLionel Sambuc 
912ebfedea0SLionel Sambuc         if (algorithm_auth)
913ebfedea0SLionel Sambuc             if ((algorithm_auth & mask_auth) == 0)
914ebfedea0SLionel Sambuc                 continue;
915ebfedea0SLionel Sambuc 
916ebfedea0SLionel Sambuc         if (algorithm_enc)
917ebfedea0SLionel Sambuc             if ((algorithm_enc & mask_enc) == 0)
918ebfedea0SLionel Sambuc                 continue;
919ebfedea0SLionel Sambuc 
920ebfedea0SLionel Sambuc         if (algorithm_mac)
921ebfedea0SLionel Sambuc             if ((algorithm_mac & mask_mac) == 0)
922ebfedea0SLionel Sambuc                 continue;
923ebfedea0SLionel Sambuc 
924ebfedea0SLionel Sambuc         if (algorithm_ssl)
925ebfedea0SLionel Sambuc             if ((algorithm_ssl & mask_ssl) == 0)
926ebfedea0SLionel Sambuc                 continue;
927ebfedea0SLionel Sambuc 
928ebfedea0SLionel Sambuc         *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
929ebfedea0SLionel Sambuc         ca_curr++;
930ebfedea0SLionel Sambuc     }
931ebfedea0SLionel Sambuc 
932ebfedea0SLionel Sambuc     *ca_curr = NULL;            /* end of list */
933ebfedea0SLionel Sambuc }
934ebfedea0SLionel Sambuc 
ssl_cipher_apply_rule(unsigned long cipher_id,unsigned long alg_mkey,unsigned long alg_auth,unsigned long alg_enc,unsigned long alg_mac,unsigned long alg_ssl,unsigned long algo_strength,int rule,int strength_bits,CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p)935ebfedea0SLionel Sambuc static void ssl_cipher_apply_rule(unsigned long cipher_id,
936*0a6a1f1dSLionel Sambuc                                   unsigned long alg_mkey,
937*0a6a1f1dSLionel Sambuc                                   unsigned long alg_auth,
938*0a6a1f1dSLionel Sambuc                                   unsigned long alg_enc,
939*0a6a1f1dSLionel Sambuc                                   unsigned long alg_mac,
940ebfedea0SLionel Sambuc                                   unsigned long alg_ssl,
941*0a6a1f1dSLionel Sambuc                                   unsigned long algo_strength, int rule,
942*0a6a1f1dSLionel Sambuc                                   int strength_bits, CIPHER_ORDER **head_p,
943*0a6a1f1dSLionel Sambuc                                   CIPHER_ORDER **tail_p)
944ebfedea0SLionel Sambuc {
945*0a6a1f1dSLionel Sambuc     CIPHER_ORDER *head, *tail, *curr, *next, *last;
946ebfedea0SLionel Sambuc     const SSL_CIPHER *cp;
947ebfedea0SLionel Sambuc     int reverse = 0;
948ebfedea0SLionel Sambuc 
949ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
950*0a6a1f1dSLionel Sambuc     fprintf(stderr,
951*0a6a1f1dSLionel Sambuc             "Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d)\n",
952*0a6a1f1dSLionel Sambuc             rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl,
953*0a6a1f1dSLionel Sambuc             algo_strength, strength_bits);
954ebfedea0SLionel Sambuc #endif
955ebfedea0SLionel Sambuc 
956ebfedea0SLionel Sambuc     if (rule == CIPHER_DEL)
957*0a6a1f1dSLionel Sambuc         reverse = 1;            /* needed to maintain sorting between
958*0a6a1f1dSLionel Sambuc                                  * currently deleted ciphers */
959ebfedea0SLionel Sambuc 
960ebfedea0SLionel Sambuc     head = *head_p;
961ebfedea0SLionel Sambuc     tail = *tail_p;
962ebfedea0SLionel Sambuc 
963*0a6a1f1dSLionel Sambuc     if (reverse) {
964*0a6a1f1dSLionel Sambuc         next = tail;
965ebfedea0SLionel Sambuc         last = head;
966*0a6a1f1dSLionel Sambuc     } else {
967*0a6a1f1dSLionel Sambuc         next = head;
968ebfedea0SLionel Sambuc         last = tail;
969ebfedea0SLionel Sambuc     }
970ebfedea0SLionel Sambuc 
971*0a6a1f1dSLionel Sambuc     curr = NULL;
972*0a6a1f1dSLionel Sambuc     for (;;) {
973*0a6a1f1dSLionel Sambuc         if (curr == last)
974*0a6a1f1dSLionel Sambuc             break;
975*0a6a1f1dSLionel Sambuc 
976*0a6a1f1dSLionel Sambuc         curr = next;
977*0a6a1f1dSLionel Sambuc 
978*0a6a1f1dSLionel Sambuc         if (curr == NULL)
979*0a6a1f1dSLionel Sambuc             break;
980*0a6a1f1dSLionel Sambuc 
981*0a6a1f1dSLionel Sambuc         next = reverse ? curr->prev : curr->next;
982ebfedea0SLionel Sambuc 
983ebfedea0SLionel Sambuc         cp = curr->cipher;
984ebfedea0SLionel Sambuc 
985ebfedea0SLionel Sambuc         /*
986ebfedea0SLionel Sambuc          * Selection criteria is either the value of strength_bits
987ebfedea0SLionel Sambuc          * or the algorithms used.
988ebfedea0SLionel Sambuc          */
989*0a6a1f1dSLionel Sambuc         if (strength_bits >= 0) {
990ebfedea0SLionel Sambuc             if (strength_bits != cp->strength_bits)
991ebfedea0SLionel Sambuc                 continue;
992*0a6a1f1dSLionel Sambuc         } else {
993ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
994*0a6a1f1dSLionel Sambuc             fprintf(stderr,
995*0a6a1f1dSLionel Sambuc                     "\nName: %s:\nAlgo = %08lx/%08lx/%08lx/%08lx/%08lx Algo_strength = %08lx\n",
996*0a6a1f1dSLionel Sambuc                     cp->name, cp->algorithm_mkey, cp->algorithm_auth,
997*0a6a1f1dSLionel Sambuc                     cp->algorithm_enc, cp->algorithm_mac, cp->algorithm_ssl,
998*0a6a1f1dSLionel Sambuc                     cp->algo_strength);
999ebfedea0SLionel Sambuc #endif
1000*0a6a1f1dSLionel Sambuc             if (algo_strength == SSL_EXP_MASK && SSL_C_IS_EXPORT(cp))
1001*0a6a1f1dSLionel Sambuc                 goto ok;
1002*0a6a1f1dSLionel Sambuc             if (alg_ssl == ~SSL_SSLV2 && cp->algorithm_ssl == SSL_SSLV2)
1003*0a6a1f1dSLionel Sambuc                 goto ok;
1004ebfedea0SLionel Sambuc             if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
1005ebfedea0SLionel Sambuc                 continue;
1006ebfedea0SLionel Sambuc             if (alg_auth && !(alg_auth & cp->algorithm_auth))
1007ebfedea0SLionel Sambuc                 continue;
1008ebfedea0SLionel Sambuc             if (alg_enc && !(alg_enc & cp->algorithm_enc))
1009ebfedea0SLionel Sambuc                 continue;
1010ebfedea0SLionel Sambuc             if (alg_mac && !(alg_mac & cp->algorithm_mac))
1011ebfedea0SLionel Sambuc                 continue;
1012ebfedea0SLionel Sambuc             if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
1013ebfedea0SLionel Sambuc                 continue;
1014*0a6a1f1dSLionel Sambuc             if ((algo_strength & SSL_EXP_MASK)
1015*0a6a1f1dSLionel Sambuc                 && !(algo_strength & SSL_EXP_MASK & cp->algo_strength))
1016ebfedea0SLionel Sambuc                 continue;
1017*0a6a1f1dSLionel Sambuc             if ((algo_strength & SSL_STRONG_MASK)
1018*0a6a1f1dSLionel Sambuc                 && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
1019ebfedea0SLionel Sambuc                 continue;
1020ebfedea0SLionel Sambuc         }
1021ebfedea0SLionel Sambuc 
1022*0a6a1f1dSLionel Sambuc     ok:
1023*0a6a1f1dSLionel Sambuc 
1024ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
1025*0a6a1f1dSLionel Sambuc         fprintf(stderr, "Action = %d\n", rule);
1026ebfedea0SLionel Sambuc #endif
1027ebfedea0SLionel Sambuc 
1028ebfedea0SLionel Sambuc         /* add the cipher if it has not been added yet. */
1029*0a6a1f1dSLionel Sambuc         if (rule == CIPHER_ADD) {
1030ebfedea0SLionel Sambuc             /* reverse == 0 */
1031*0a6a1f1dSLionel Sambuc             if (!curr->active) {
1032ebfedea0SLionel Sambuc                 ll_append_tail(&head, curr, &tail);
1033ebfedea0SLionel Sambuc                 curr->active = 1;
1034ebfedea0SLionel Sambuc             }
1035ebfedea0SLionel Sambuc         }
1036ebfedea0SLionel Sambuc         /* Move the added cipher to this location */
1037*0a6a1f1dSLionel Sambuc         else if (rule == CIPHER_ORD) {
1038ebfedea0SLionel Sambuc             /* reverse == 0 */
1039*0a6a1f1dSLionel Sambuc             if (curr->active) {
1040ebfedea0SLionel Sambuc                 ll_append_tail(&head, curr, &tail);
1041ebfedea0SLionel Sambuc             }
1042*0a6a1f1dSLionel Sambuc         } else if (rule == CIPHER_DEL) {
1043ebfedea0SLionel Sambuc             /* reverse == 1 */
1044*0a6a1f1dSLionel Sambuc             if (curr->active) {
1045*0a6a1f1dSLionel Sambuc                 /*
1046*0a6a1f1dSLionel Sambuc                  * most recently deleted ciphersuites get best positions for
1047*0a6a1f1dSLionel Sambuc                  * any future CIPHER_ADD (note that the CIPHER_DEL loop works
1048*0a6a1f1dSLionel Sambuc                  * in reverse to maintain the order)
1049*0a6a1f1dSLionel Sambuc                  */
1050ebfedea0SLionel Sambuc                 ll_append_head(&head, curr, &tail);
1051ebfedea0SLionel Sambuc                 curr->active = 0;
1052ebfedea0SLionel Sambuc             }
1053*0a6a1f1dSLionel Sambuc         } else if (rule == CIPHER_KILL) {
1054ebfedea0SLionel Sambuc             /* reverse == 0 */
1055ebfedea0SLionel Sambuc             if (head == curr)
1056ebfedea0SLionel Sambuc                 head = curr->next;
1057ebfedea0SLionel Sambuc             else
1058ebfedea0SLionel Sambuc                 curr->prev->next = curr->next;
1059ebfedea0SLionel Sambuc             if (tail == curr)
1060ebfedea0SLionel Sambuc                 tail = curr->prev;
1061ebfedea0SLionel Sambuc             curr->active = 0;
1062ebfedea0SLionel Sambuc             if (curr->next != NULL)
1063ebfedea0SLionel Sambuc                 curr->next->prev = curr->prev;
1064ebfedea0SLionel Sambuc             if (curr->prev != NULL)
1065ebfedea0SLionel Sambuc                 curr->prev->next = curr->next;
1066ebfedea0SLionel Sambuc             curr->next = NULL;
1067ebfedea0SLionel Sambuc             curr->prev = NULL;
1068ebfedea0SLionel Sambuc         }
1069ebfedea0SLionel Sambuc     }
1070ebfedea0SLionel Sambuc 
1071ebfedea0SLionel Sambuc     *head_p = head;
1072ebfedea0SLionel Sambuc     *tail_p = tail;
1073ebfedea0SLionel Sambuc }
1074ebfedea0SLionel Sambuc 
ssl_cipher_strength_sort(CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p)1075ebfedea0SLionel Sambuc static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
1076ebfedea0SLionel Sambuc                                     CIPHER_ORDER **tail_p)
1077ebfedea0SLionel Sambuc {
1078ebfedea0SLionel Sambuc     int max_strength_bits, i, *number_uses;
1079ebfedea0SLionel Sambuc     CIPHER_ORDER *curr;
1080ebfedea0SLionel Sambuc 
1081ebfedea0SLionel Sambuc     /*
1082ebfedea0SLionel Sambuc      * This routine sorts the ciphers with descending strength. The sorting
1083ebfedea0SLionel Sambuc      * must keep the pre-sorted sequence, so we apply the normal sorting
1084ebfedea0SLionel Sambuc      * routine as '+' movement to the end of the list.
1085ebfedea0SLionel Sambuc      */
1086ebfedea0SLionel Sambuc     max_strength_bits = 0;
1087ebfedea0SLionel Sambuc     curr = *head_p;
1088*0a6a1f1dSLionel Sambuc     while (curr != NULL) {
1089*0a6a1f1dSLionel Sambuc         if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
1090ebfedea0SLionel Sambuc             max_strength_bits = curr->cipher->strength_bits;
1091ebfedea0SLionel Sambuc         curr = curr->next;
1092ebfedea0SLionel Sambuc     }
1093ebfedea0SLionel Sambuc 
1094ebfedea0SLionel Sambuc     number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
1095*0a6a1f1dSLionel Sambuc     if (!number_uses) {
1096ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
1097ebfedea0SLionel Sambuc         return (0);
1098ebfedea0SLionel Sambuc     }
1099ebfedea0SLionel Sambuc     memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
1100ebfedea0SLionel Sambuc 
1101ebfedea0SLionel Sambuc     /*
1102ebfedea0SLionel Sambuc      * Now find the strength_bits values actually used
1103ebfedea0SLionel Sambuc      */
1104ebfedea0SLionel Sambuc     curr = *head_p;
1105*0a6a1f1dSLionel Sambuc     while (curr != NULL) {
1106ebfedea0SLionel Sambuc         if (curr->active)
1107ebfedea0SLionel Sambuc             number_uses[curr->cipher->strength_bits]++;
1108ebfedea0SLionel Sambuc         curr = curr->next;
1109ebfedea0SLionel Sambuc     }
1110ebfedea0SLionel Sambuc     /*
1111ebfedea0SLionel Sambuc      * Go through the list of used strength_bits values in descending
1112ebfedea0SLionel Sambuc      * order.
1113ebfedea0SLionel Sambuc      */
1114ebfedea0SLionel Sambuc     for (i = max_strength_bits; i >= 0; i--)
1115ebfedea0SLionel Sambuc         if (number_uses[i] > 0)
1116*0a6a1f1dSLionel Sambuc             ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p,
1117*0a6a1f1dSLionel Sambuc                                   tail_p);
1118ebfedea0SLionel Sambuc 
1119ebfedea0SLionel Sambuc     OPENSSL_free(number_uses);
1120ebfedea0SLionel Sambuc     return (1);
1121ebfedea0SLionel Sambuc }
1122ebfedea0SLionel Sambuc 
ssl_cipher_process_rulestr(const char * rule_str,CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p,const SSL_CIPHER ** ca_list)1123ebfedea0SLionel Sambuc static int ssl_cipher_process_rulestr(const char *rule_str,
1124*0a6a1f1dSLionel Sambuc                                       CIPHER_ORDER **head_p,
1125*0a6a1f1dSLionel Sambuc                                       CIPHER_ORDER **tail_p,
1126ebfedea0SLionel Sambuc                                       const SSL_CIPHER **ca_list)
1127ebfedea0SLionel Sambuc {
1128*0a6a1f1dSLionel Sambuc     unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl,
1129*0a6a1f1dSLionel Sambuc         algo_strength;
1130ebfedea0SLionel Sambuc     const char *l, *buf;
1131ebfedea0SLionel Sambuc     int j, multi, found, rule, retval, ok, buflen;
1132ebfedea0SLionel Sambuc     unsigned long cipher_id = 0;
1133ebfedea0SLionel Sambuc     char ch;
1134ebfedea0SLionel Sambuc 
1135ebfedea0SLionel Sambuc     retval = 1;
1136ebfedea0SLionel Sambuc     l = rule_str;
1137*0a6a1f1dSLionel Sambuc     for (;;) {
1138ebfedea0SLionel Sambuc         ch = *l;
1139ebfedea0SLionel Sambuc 
1140ebfedea0SLionel Sambuc         if (ch == '\0')
1141ebfedea0SLionel Sambuc             break;              /* done */
1142*0a6a1f1dSLionel Sambuc         if (ch == '-') {
1143*0a6a1f1dSLionel Sambuc             rule = CIPHER_DEL;
1144*0a6a1f1dSLionel Sambuc             l++;
1145*0a6a1f1dSLionel Sambuc         } else if (ch == '+') {
1146*0a6a1f1dSLionel Sambuc             rule = CIPHER_ORD;
1147*0a6a1f1dSLionel Sambuc             l++;
1148*0a6a1f1dSLionel Sambuc         } else if (ch == '!') {
1149*0a6a1f1dSLionel Sambuc             rule = CIPHER_KILL;
1150*0a6a1f1dSLionel Sambuc             l++;
1151*0a6a1f1dSLionel Sambuc         } else if (ch == '@') {
1152*0a6a1f1dSLionel Sambuc             rule = CIPHER_SPECIAL;
1153*0a6a1f1dSLionel Sambuc             l++;
1154*0a6a1f1dSLionel Sambuc         } else {
1155*0a6a1f1dSLionel Sambuc             rule = CIPHER_ADD;
1156*0a6a1f1dSLionel Sambuc         }
1157ebfedea0SLionel Sambuc 
1158*0a6a1f1dSLionel Sambuc         if (ITEM_SEP(ch)) {
1159ebfedea0SLionel Sambuc             l++;
1160ebfedea0SLionel Sambuc             continue;
1161ebfedea0SLionel Sambuc         }
1162ebfedea0SLionel Sambuc 
1163ebfedea0SLionel Sambuc         alg_mkey = 0;
1164ebfedea0SLionel Sambuc         alg_auth = 0;
1165ebfedea0SLionel Sambuc         alg_enc = 0;
1166ebfedea0SLionel Sambuc         alg_mac = 0;
1167ebfedea0SLionel Sambuc         alg_ssl = 0;
1168ebfedea0SLionel Sambuc         algo_strength = 0;
1169ebfedea0SLionel Sambuc 
1170*0a6a1f1dSLionel Sambuc         for (;;) {
1171ebfedea0SLionel Sambuc             ch = *l;
1172ebfedea0SLionel Sambuc             buf = l;
1173ebfedea0SLionel Sambuc             buflen = 0;
1174ebfedea0SLionel Sambuc #ifndef CHARSET_EBCDIC
1175ebfedea0SLionel Sambuc             while (((ch >= 'A') && (ch <= 'Z')) ||
1176ebfedea0SLionel Sambuc                    ((ch >= '0') && (ch <= '9')) ||
1177*0a6a1f1dSLionel Sambuc                    ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.'))
1178ebfedea0SLionel Sambuc #else
1179ebfedea0SLionel Sambuc             while (isalnum(ch) || (ch == '-') || (ch == '.'))
1180ebfedea0SLionel Sambuc #endif
1181ebfedea0SLionel Sambuc             {
1182ebfedea0SLionel Sambuc                 ch = *(++l);
1183ebfedea0SLionel Sambuc                 buflen++;
1184ebfedea0SLionel Sambuc             }
1185ebfedea0SLionel Sambuc 
1186*0a6a1f1dSLionel Sambuc             if (buflen == 0) {
1187ebfedea0SLionel Sambuc                 /*
1188ebfedea0SLionel Sambuc                  * We hit something we cannot deal with,
1189ebfedea0SLionel Sambuc                  * it is no command or separator nor
1190ebfedea0SLionel Sambuc                  * alphanumeric, so we call this an error.
1191ebfedea0SLionel Sambuc                  */
1192ebfedea0SLionel Sambuc                 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
1193ebfedea0SLionel Sambuc                        SSL_R_INVALID_COMMAND);
1194ebfedea0SLionel Sambuc                 retval = found = 0;
1195ebfedea0SLionel Sambuc                 l++;
1196ebfedea0SLionel Sambuc                 break;
1197ebfedea0SLionel Sambuc             }
1198ebfedea0SLionel Sambuc 
1199*0a6a1f1dSLionel Sambuc             if (rule == CIPHER_SPECIAL) {
1200ebfedea0SLionel Sambuc                 found = 0;      /* unused -- avoid compiler warning */
1201ebfedea0SLionel Sambuc                 break;          /* special treatment */
1202ebfedea0SLionel Sambuc             }
1203ebfedea0SLionel Sambuc 
1204ebfedea0SLionel Sambuc             /* check for multi-part specification */
1205*0a6a1f1dSLionel Sambuc             if (ch == '+') {
1206ebfedea0SLionel Sambuc                 multi = 1;
1207ebfedea0SLionel Sambuc                 l++;
1208*0a6a1f1dSLionel Sambuc             } else
1209ebfedea0SLionel Sambuc                 multi = 0;
1210ebfedea0SLionel Sambuc 
1211ebfedea0SLionel Sambuc             /*
1212ebfedea0SLionel Sambuc              * Now search for the cipher alias in the ca_list. Be careful
1213ebfedea0SLionel Sambuc              * with the strncmp, because the "buflen" limitation
1214ebfedea0SLionel Sambuc              * will make the rule "ADH:SOME" and the cipher
1215ebfedea0SLionel Sambuc              * "ADH-MY-CIPHER" look like a match for buflen=3.
1216ebfedea0SLionel Sambuc              * So additionally check whether the cipher name found
1217ebfedea0SLionel Sambuc              * has the correct length. We can save a strlen() call:
1218ebfedea0SLionel Sambuc              * just checking for the '\0' at the right place is
1219ebfedea0SLionel Sambuc              * sufficient, we have to strncmp() anyway. (We cannot
1220ebfedea0SLionel Sambuc              * use strcmp(), because buf is not '\0' terminated.)
1221ebfedea0SLionel Sambuc              */
1222ebfedea0SLionel Sambuc             j = found = 0;
1223ebfedea0SLionel Sambuc             cipher_id = 0;
1224*0a6a1f1dSLionel Sambuc             while (ca_list[j]) {
1225ebfedea0SLionel Sambuc                 if (!strncmp(buf, ca_list[j]->name, buflen) &&
1226*0a6a1f1dSLionel Sambuc                     (ca_list[j]->name[buflen] == '\0')) {
1227ebfedea0SLionel Sambuc                     found = 1;
1228ebfedea0SLionel Sambuc                     break;
1229*0a6a1f1dSLionel Sambuc                 } else
1230ebfedea0SLionel Sambuc                     j++;
1231ebfedea0SLionel Sambuc             }
1232ebfedea0SLionel Sambuc 
1233ebfedea0SLionel Sambuc             if (!found)
1234ebfedea0SLionel Sambuc                 break;          /* ignore this entry */
1235ebfedea0SLionel Sambuc 
1236*0a6a1f1dSLionel Sambuc             if (ca_list[j]->algorithm_mkey) {
1237*0a6a1f1dSLionel Sambuc                 if (alg_mkey) {
1238ebfedea0SLionel Sambuc                     alg_mkey &= ca_list[j]->algorithm_mkey;
1239*0a6a1f1dSLionel Sambuc                     if (!alg_mkey) {
1240*0a6a1f1dSLionel Sambuc                         found = 0;
1241*0a6a1f1dSLionel Sambuc                         break;
1242ebfedea0SLionel Sambuc                     }
1243*0a6a1f1dSLionel Sambuc                 } else
1244ebfedea0SLionel Sambuc                     alg_mkey = ca_list[j]->algorithm_mkey;
1245ebfedea0SLionel Sambuc             }
1246ebfedea0SLionel Sambuc 
1247*0a6a1f1dSLionel Sambuc             if (ca_list[j]->algorithm_auth) {
1248*0a6a1f1dSLionel Sambuc                 if (alg_auth) {
1249ebfedea0SLionel Sambuc                     alg_auth &= ca_list[j]->algorithm_auth;
1250*0a6a1f1dSLionel Sambuc                     if (!alg_auth) {
1251*0a6a1f1dSLionel Sambuc                         found = 0;
1252*0a6a1f1dSLionel Sambuc                         break;
1253ebfedea0SLionel Sambuc                     }
1254*0a6a1f1dSLionel Sambuc                 } else
1255ebfedea0SLionel Sambuc                     alg_auth = ca_list[j]->algorithm_auth;
1256ebfedea0SLionel Sambuc             }
1257ebfedea0SLionel Sambuc 
1258*0a6a1f1dSLionel Sambuc             if (ca_list[j]->algorithm_enc) {
1259*0a6a1f1dSLionel Sambuc                 if (alg_enc) {
1260ebfedea0SLionel Sambuc                     alg_enc &= ca_list[j]->algorithm_enc;
1261*0a6a1f1dSLionel Sambuc                     if (!alg_enc) {
1262*0a6a1f1dSLionel Sambuc                         found = 0;
1263*0a6a1f1dSLionel Sambuc                         break;
1264ebfedea0SLionel Sambuc                     }
1265*0a6a1f1dSLionel Sambuc                 } else
1266ebfedea0SLionel Sambuc                     alg_enc = ca_list[j]->algorithm_enc;
1267ebfedea0SLionel Sambuc             }
1268ebfedea0SLionel Sambuc 
1269*0a6a1f1dSLionel Sambuc             if (ca_list[j]->algorithm_mac) {
1270*0a6a1f1dSLionel Sambuc                 if (alg_mac) {
1271ebfedea0SLionel Sambuc                     alg_mac &= ca_list[j]->algorithm_mac;
1272*0a6a1f1dSLionel Sambuc                     if (!alg_mac) {
1273*0a6a1f1dSLionel Sambuc                         found = 0;
1274*0a6a1f1dSLionel Sambuc                         break;
1275ebfedea0SLionel Sambuc                     }
1276*0a6a1f1dSLionel Sambuc                 } else
1277ebfedea0SLionel Sambuc                     alg_mac = ca_list[j]->algorithm_mac;
1278ebfedea0SLionel Sambuc             }
1279ebfedea0SLionel Sambuc 
1280*0a6a1f1dSLionel Sambuc             if (ca_list[j]->algo_strength & SSL_EXP_MASK) {
1281*0a6a1f1dSLionel Sambuc                 if (algo_strength & SSL_EXP_MASK) {
1282*0a6a1f1dSLionel Sambuc                     algo_strength &=
1283*0a6a1f1dSLionel Sambuc                         (ca_list[j]->algo_strength & SSL_EXP_MASK) |
1284*0a6a1f1dSLionel Sambuc                         ~SSL_EXP_MASK;
1285*0a6a1f1dSLionel Sambuc                     if (!(algo_strength & SSL_EXP_MASK)) {
1286*0a6a1f1dSLionel Sambuc                         found = 0;
1287*0a6a1f1dSLionel Sambuc                         break;
1288ebfedea0SLionel Sambuc                     }
1289*0a6a1f1dSLionel Sambuc                 } else
1290ebfedea0SLionel Sambuc                     algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
1291ebfedea0SLionel Sambuc             }
1292ebfedea0SLionel Sambuc 
1293*0a6a1f1dSLionel Sambuc             if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1294*0a6a1f1dSLionel Sambuc                 if (algo_strength & SSL_STRONG_MASK) {
1295*0a6a1f1dSLionel Sambuc                     algo_strength &=
1296*0a6a1f1dSLionel Sambuc                         (ca_list[j]->algo_strength & SSL_STRONG_MASK) |
1297*0a6a1f1dSLionel Sambuc                         ~SSL_STRONG_MASK;
1298*0a6a1f1dSLionel Sambuc                     if (!(algo_strength & SSL_STRONG_MASK)) {
1299*0a6a1f1dSLionel Sambuc                         found = 0;
1300*0a6a1f1dSLionel Sambuc                         break;
1301ebfedea0SLionel Sambuc                     }
1302*0a6a1f1dSLionel Sambuc                 } else
1303*0a6a1f1dSLionel Sambuc                     algo_strength |=
1304*0a6a1f1dSLionel Sambuc                         ca_list[j]->algo_strength & SSL_STRONG_MASK;
1305ebfedea0SLionel Sambuc             }
1306ebfedea0SLionel Sambuc 
1307*0a6a1f1dSLionel Sambuc             if (ca_list[j]->valid) {
1308*0a6a1f1dSLionel Sambuc                 /*
1309*0a6a1f1dSLionel Sambuc                  * explicit ciphersuite found; its protocol version does not
1310*0a6a1f1dSLionel Sambuc                  * become part of the search pattern!
1311*0a6a1f1dSLionel Sambuc                  */
1312ebfedea0SLionel Sambuc 
1313ebfedea0SLionel Sambuc                 cipher_id = ca_list[j]->id;
1314*0a6a1f1dSLionel Sambuc             } else {
1315*0a6a1f1dSLionel Sambuc                 /*
1316*0a6a1f1dSLionel Sambuc                  * not an explicit ciphersuite; only in this case, the
1317*0a6a1f1dSLionel Sambuc                  * protocol version is considered part of the search pattern
1318*0a6a1f1dSLionel Sambuc                  */
1319ebfedea0SLionel Sambuc 
1320*0a6a1f1dSLionel Sambuc                 if (ca_list[j]->algorithm_ssl) {
1321*0a6a1f1dSLionel Sambuc                     if (alg_ssl) {
1322ebfedea0SLionel Sambuc                         alg_ssl &= ca_list[j]->algorithm_ssl;
1323*0a6a1f1dSLionel Sambuc                         if (!alg_ssl) {
1324*0a6a1f1dSLionel Sambuc                             found = 0;
1325*0a6a1f1dSLionel Sambuc                             break;
1326ebfedea0SLionel Sambuc                         }
1327*0a6a1f1dSLionel Sambuc                     } else
1328ebfedea0SLionel Sambuc                         alg_ssl = ca_list[j]->algorithm_ssl;
1329ebfedea0SLionel Sambuc                 }
1330ebfedea0SLionel Sambuc             }
1331ebfedea0SLionel Sambuc 
1332*0a6a1f1dSLionel Sambuc             if (!multi)
1333*0a6a1f1dSLionel Sambuc                 break;
1334ebfedea0SLionel Sambuc         }
1335ebfedea0SLionel Sambuc 
1336ebfedea0SLionel Sambuc         /*
1337ebfedea0SLionel Sambuc          * Ok, we have the rule, now apply it
1338ebfedea0SLionel Sambuc          */
1339*0a6a1f1dSLionel Sambuc         if (rule == CIPHER_SPECIAL) { /* special command */
1340ebfedea0SLionel Sambuc             ok = 0;
1341*0a6a1f1dSLionel Sambuc             if ((buflen == 8) && !strncmp(buf, "STRENGTH", 8))
1342ebfedea0SLionel Sambuc                 ok = ssl_cipher_strength_sort(head_p, tail_p);
1343ebfedea0SLionel Sambuc             else
1344ebfedea0SLionel Sambuc                 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
1345ebfedea0SLionel Sambuc                        SSL_R_INVALID_COMMAND);
1346ebfedea0SLionel Sambuc             if (ok == 0)
1347ebfedea0SLionel Sambuc                 retval = 0;
1348ebfedea0SLionel Sambuc             /*
1349ebfedea0SLionel Sambuc              * We do not support any "multi" options
1350ebfedea0SLionel Sambuc              * together with "@", so throw away the
1351ebfedea0SLionel Sambuc              * rest of the command, if any left, until
1352ebfedea0SLionel Sambuc              * end or ':' is found.
1353ebfedea0SLionel Sambuc              */
1354ebfedea0SLionel Sambuc             while ((*l != '\0') && !ITEM_SEP(*l))
1355ebfedea0SLionel Sambuc                 l++;
1356*0a6a1f1dSLionel Sambuc         } else if (found) {
1357ebfedea0SLionel Sambuc             ssl_cipher_apply_rule(cipher_id,
1358*0a6a1f1dSLionel Sambuc                                   alg_mkey, alg_auth, alg_enc, alg_mac,
1359*0a6a1f1dSLionel Sambuc                                   alg_ssl, algo_strength, rule, -1, head_p,
1360*0a6a1f1dSLionel Sambuc                                   tail_p);
1361*0a6a1f1dSLionel Sambuc         } else {
1362ebfedea0SLionel Sambuc             while ((*l != '\0') && !ITEM_SEP(*l))
1363ebfedea0SLionel Sambuc                 l++;
1364ebfedea0SLionel Sambuc         }
1365*0a6a1f1dSLionel Sambuc         if (*l == '\0')
1366*0a6a1f1dSLionel Sambuc             break;              /* done */
1367ebfedea0SLionel Sambuc     }
1368ebfedea0SLionel Sambuc 
1369ebfedea0SLionel Sambuc     return (retval);
1370ebfedea0SLionel Sambuc }
1371ebfedea0SLionel Sambuc 
STACK_OF(SSL_CIPHER)1372*0a6a1f1dSLionel Sambuc STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, STACK_OF(SSL_CIPHER)
1373*0a6a1f1dSLionel Sambuc                                              **cipher_list, STACK_OF(SSL_CIPHER)
1374*0a6a1f1dSLionel Sambuc                                              **cipher_list_by_id,
1375ebfedea0SLionel Sambuc                                              const char *rule_str)
1376ebfedea0SLionel Sambuc {
1377ebfedea0SLionel Sambuc     int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1378*0a6a1f1dSLionel Sambuc     unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac,
1379*0a6a1f1dSLionel Sambuc         disabled_ssl;
1380ebfedea0SLionel Sambuc     STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
1381ebfedea0SLionel Sambuc     const char *rule_p;
1382ebfedea0SLionel Sambuc     CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1383ebfedea0SLionel Sambuc     const SSL_CIPHER **ca_list = NULL;
1384ebfedea0SLionel Sambuc 
1385ebfedea0SLionel Sambuc     /*
1386ebfedea0SLionel Sambuc      * Return with error if nothing to do.
1387ebfedea0SLionel Sambuc      */
1388ebfedea0SLionel Sambuc     if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
1389ebfedea0SLionel Sambuc         return NULL;
1390ebfedea0SLionel Sambuc 
1391ebfedea0SLionel Sambuc     /*
1392ebfedea0SLionel Sambuc      * To reduce the work to do we only want to process the compiled
1393ebfedea0SLionel Sambuc      * in algorithms, so we first get the mask of disabled ciphers.
1394ebfedea0SLionel Sambuc      */
1395*0a6a1f1dSLionel Sambuc     ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc,
1396*0a6a1f1dSLionel Sambuc                             &disabled_mac, &disabled_ssl);
1397ebfedea0SLionel Sambuc 
1398ebfedea0SLionel Sambuc     /*
1399ebfedea0SLionel Sambuc      * Now we have to collect the available ciphers from the compiled
1400ebfedea0SLionel Sambuc      * in ciphers. We cannot get more than the number compiled in, so
1401ebfedea0SLionel Sambuc      * it is used for allocation.
1402ebfedea0SLionel Sambuc      */
1403ebfedea0SLionel Sambuc     num_of_ciphers = ssl_method->num_ciphers();
1404ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1405*0a6a1f1dSLionel Sambuc     fprintf(stderr, "ssl_create_cipher_list() for %d ciphers\n",
1406*0a6a1f1dSLionel Sambuc             num_of_ciphers);
1407ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1408*0a6a1f1dSLionel Sambuc     co_list =
1409*0a6a1f1dSLionel Sambuc         (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1410*0a6a1f1dSLionel Sambuc     if (co_list == NULL) {
1411ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1412ebfedea0SLionel Sambuc         return (NULL);          /* Failure */
1413ebfedea0SLionel Sambuc     }
1414ebfedea0SLionel Sambuc 
1415ebfedea0SLionel Sambuc     ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1416*0a6a1f1dSLionel Sambuc                                disabled_mkey, disabled_auth, disabled_enc,
1417*0a6a1f1dSLionel Sambuc                                disabled_mac, disabled_ssl, co_list, &head,
1418*0a6a1f1dSLionel Sambuc                                &tail);
1419ebfedea0SLionel Sambuc 
1420ebfedea0SLionel Sambuc     /* Now arrange all ciphers by preference: */
1421ebfedea0SLionel Sambuc 
1422*0a6a1f1dSLionel Sambuc     /*
1423*0a6a1f1dSLionel Sambuc      * Everything else being equal, prefer ephemeral ECDH over other key
1424*0a6a1f1dSLionel Sambuc      * exchange mechanisms
1425*0a6a1f1dSLionel Sambuc      */
1426*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head,
1427*0a6a1f1dSLionel Sambuc                           &tail);
1428*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
1429*0a6a1f1dSLionel Sambuc                           &tail);
1430ebfedea0SLionel Sambuc 
1431ebfedea0SLionel Sambuc     /* AES is our preferred symmetric cipher */
1432*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, &head,
1433*0a6a1f1dSLionel Sambuc                           &tail);
1434ebfedea0SLionel Sambuc 
1435ebfedea0SLionel Sambuc     /* Temporarily enable everything else for sorting */
1436ebfedea0SLionel Sambuc     ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1437ebfedea0SLionel Sambuc 
1438ebfedea0SLionel Sambuc     /* Low priority for MD5 */
1439*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head,
1440*0a6a1f1dSLionel Sambuc                           &tail);
1441ebfedea0SLionel Sambuc 
1442*0a6a1f1dSLionel Sambuc     /*
1443*0a6a1f1dSLionel Sambuc      * Move anonymous ciphers to the end.  Usually, these will remain
1444*0a6a1f1dSLionel Sambuc      * disabled. (For applications that allow them, they aren't too bad, but
1445*0a6a1f1dSLionel Sambuc      * we prefer authenticated ciphers.)
1446*0a6a1f1dSLionel Sambuc      */
1447*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1448*0a6a1f1dSLionel Sambuc                           &tail);
1449ebfedea0SLionel Sambuc 
1450ebfedea0SLionel Sambuc     /* Move ciphers without forward secrecy to the end */
1451*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1452*0a6a1f1dSLionel Sambuc                           &tail);
1453*0a6a1f1dSLionel Sambuc     /*
1454*0a6a1f1dSLionel Sambuc      * ssl_cipher_apply_rule(0, 0, SSL_aDH, 0, 0, 0, 0, CIPHER_ORD, -1,
1455*0a6a1f1dSLionel Sambuc      * &head, &tail);
1456*0a6a1f1dSLionel Sambuc      */
1457*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1458*0a6a1f1dSLionel Sambuc                           &tail);
1459*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, SSL_kPSK, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1460*0a6a1f1dSLionel Sambuc                           &tail);
1461*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, SSL_kKRB5, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1462*0a6a1f1dSLionel Sambuc                           &tail);
1463ebfedea0SLionel Sambuc 
1464ebfedea0SLionel Sambuc     /* RC4 is sort-of broken -- move the the end */
1465*0a6a1f1dSLionel Sambuc     ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head,
1466*0a6a1f1dSLionel Sambuc                           &tail);
1467ebfedea0SLionel Sambuc 
1468*0a6a1f1dSLionel Sambuc     /*
1469*0a6a1f1dSLionel Sambuc      * Now sort by symmetric encryption strength.  The above ordering remains
1470*0a6a1f1dSLionel Sambuc      * in force within each class
1471*0a6a1f1dSLionel Sambuc      */
1472*0a6a1f1dSLionel Sambuc     if (!ssl_cipher_strength_sort(&head, &tail)) {
1473ebfedea0SLionel Sambuc         OPENSSL_free(co_list);
1474ebfedea0SLionel Sambuc         return NULL;
1475ebfedea0SLionel Sambuc     }
1476ebfedea0SLionel Sambuc 
1477ebfedea0SLionel Sambuc     /* Now disable everything (maintaining the ordering!) */
1478ebfedea0SLionel Sambuc     ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1479ebfedea0SLionel Sambuc 
1480ebfedea0SLionel Sambuc     /*
1481ebfedea0SLionel Sambuc      * We also need cipher aliases for selecting based on the rule_str.
1482ebfedea0SLionel Sambuc      * There might be two types of entries in the rule_str: 1) names
1483ebfedea0SLionel Sambuc      * of ciphers themselves 2) aliases for groups of ciphers.
1484ebfedea0SLionel Sambuc      * For 1) we need the available ciphers and for 2) the cipher
1485ebfedea0SLionel Sambuc      * groups of cipher_aliases added together in one list (otherwise
1486ebfedea0SLionel Sambuc      * we would be happy with just the cipher_aliases table).
1487ebfedea0SLionel Sambuc      */
1488ebfedea0SLionel Sambuc     num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1489ebfedea0SLionel Sambuc     num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1490ebfedea0SLionel Sambuc     ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1491*0a6a1f1dSLionel Sambuc     if (ca_list == NULL) {
1492ebfedea0SLionel Sambuc         OPENSSL_free(co_list);
1493ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1494ebfedea0SLionel Sambuc         return (NULL);          /* Failure */
1495ebfedea0SLionel Sambuc     }
1496ebfedea0SLionel Sambuc     ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1497ebfedea0SLionel Sambuc                                disabled_mkey, disabled_auth, disabled_enc,
1498ebfedea0SLionel Sambuc                                disabled_mac, disabled_ssl, head);
1499ebfedea0SLionel Sambuc 
1500ebfedea0SLionel Sambuc     /*
1501ebfedea0SLionel Sambuc      * If the rule_string begins with DEFAULT, apply the default rule
1502ebfedea0SLionel Sambuc      * before using the (possibly available) additional rules.
1503ebfedea0SLionel Sambuc      */
1504ebfedea0SLionel Sambuc     ok = 1;
1505ebfedea0SLionel Sambuc     rule_p = rule_str;
1506*0a6a1f1dSLionel Sambuc     if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1507ebfedea0SLionel Sambuc         ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1508ebfedea0SLionel Sambuc                                         &head, &tail, ca_list);
1509ebfedea0SLionel Sambuc         rule_p += 7;
1510ebfedea0SLionel Sambuc         if (*rule_p == ':')
1511ebfedea0SLionel Sambuc             rule_p++;
1512ebfedea0SLionel Sambuc     }
1513ebfedea0SLionel Sambuc 
1514ebfedea0SLionel Sambuc     if (ok && (strlen(rule_p) > 0))
1515ebfedea0SLionel Sambuc         ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1516ebfedea0SLionel Sambuc 
1517ebfedea0SLionel Sambuc     OPENSSL_free((void *)ca_list); /* Not needed anymore */
1518ebfedea0SLionel Sambuc 
1519*0a6a1f1dSLionel Sambuc     if (!ok) {                  /* Rule processing failure */
1520ebfedea0SLionel Sambuc         OPENSSL_free(co_list);
1521ebfedea0SLionel Sambuc         return (NULL);
1522ebfedea0SLionel Sambuc     }
1523ebfedea0SLionel Sambuc 
1524ebfedea0SLionel Sambuc     /*
1525ebfedea0SLionel Sambuc      * Allocate new "cipherstack" for the result, return with error
1526ebfedea0SLionel Sambuc      * if we cannot get one.
1527ebfedea0SLionel Sambuc      */
1528*0a6a1f1dSLionel Sambuc     if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1529ebfedea0SLionel Sambuc         OPENSSL_free(co_list);
1530ebfedea0SLionel Sambuc         return (NULL);
1531ebfedea0SLionel Sambuc     }
1532ebfedea0SLionel Sambuc 
1533ebfedea0SLionel Sambuc     /*
1534ebfedea0SLionel Sambuc      * The cipher selection for the list is done. The ciphers are added
1535ebfedea0SLionel Sambuc      * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1536ebfedea0SLionel Sambuc      */
1537*0a6a1f1dSLionel Sambuc     for (curr = head; curr != NULL; curr = curr->next) {
1538ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
1539*0a6a1f1dSLionel Sambuc         if (curr->active
1540*0a6a1f1dSLionel Sambuc             && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
1541ebfedea0SLionel Sambuc #else
1542ebfedea0SLionel Sambuc         if (curr->active)
1543ebfedea0SLionel Sambuc #endif
1544ebfedea0SLionel Sambuc         {
1545ebfedea0SLionel Sambuc             sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1546ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
1547*0a6a1f1dSLionel Sambuc             fprintf(stderr, "<%s>\n", curr->cipher->name);
1548ebfedea0SLionel Sambuc #endif
1549ebfedea0SLionel Sambuc         }
1550ebfedea0SLionel Sambuc     }
1551ebfedea0SLionel Sambuc     OPENSSL_free(co_list);      /* Not needed any longer */
1552ebfedea0SLionel Sambuc 
1553ebfedea0SLionel Sambuc     tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1554*0a6a1f1dSLionel Sambuc     if (tmp_cipher_list == NULL) {
1555ebfedea0SLionel Sambuc         sk_SSL_CIPHER_free(cipherstack);
1556ebfedea0SLionel Sambuc         return NULL;
1557ebfedea0SLionel Sambuc     }
1558ebfedea0SLionel Sambuc     if (*cipher_list != NULL)
1559ebfedea0SLionel Sambuc         sk_SSL_CIPHER_free(*cipher_list);
1560ebfedea0SLionel Sambuc     *cipher_list = cipherstack;
1561ebfedea0SLionel Sambuc     if (*cipher_list_by_id != NULL)
1562ebfedea0SLionel Sambuc         sk_SSL_CIPHER_free(*cipher_list_by_id);
1563ebfedea0SLionel Sambuc     *cipher_list_by_id = tmp_cipher_list;
1564*0a6a1f1dSLionel Sambuc     (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,
1565*0a6a1f1dSLionel Sambuc                                      ssl_cipher_ptr_id_cmp);
1566ebfedea0SLionel Sambuc 
1567ebfedea0SLionel Sambuc     sk_SSL_CIPHER_sort(*cipher_list_by_id);
1568ebfedea0SLionel Sambuc     return (cipherstack);
1569ebfedea0SLionel Sambuc }
1570ebfedea0SLionel Sambuc 
SSL_CIPHER_description(const SSL_CIPHER * cipher,char * buf,int len)1571ebfedea0SLionel Sambuc char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1572ebfedea0SLionel Sambuc {
1573ebfedea0SLionel Sambuc     int is_export, pkl, kl;
1574ebfedea0SLionel Sambuc     const char *ver, *exp_str;
1575ebfedea0SLionel Sambuc     const char *kx, *au, *enc, *mac;
1576ebfedea0SLionel Sambuc     unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, alg2;
1577ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1578*0a6a1f1dSLionel Sambuc     static const char *format =
1579*0a6a1f1dSLionel Sambuc         "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx/%lx/%lx/%lx/%lx\n";
1580ebfedea0SLionel Sambuc #else
1581*0a6a1f1dSLionel Sambuc     static const char *format =
1582*0a6a1f1dSLionel Sambuc         "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
1583ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1584ebfedea0SLionel Sambuc 
1585ebfedea0SLionel Sambuc     alg_mkey = cipher->algorithm_mkey;
1586ebfedea0SLionel Sambuc     alg_auth = cipher->algorithm_auth;
1587ebfedea0SLionel Sambuc     alg_enc = cipher->algorithm_enc;
1588ebfedea0SLionel Sambuc     alg_mac = cipher->algorithm_mac;
1589ebfedea0SLionel Sambuc     alg_ssl = cipher->algorithm_ssl;
1590ebfedea0SLionel Sambuc 
1591ebfedea0SLionel Sambuc     alg2 = cipher->algorithm2;
1592ebfedea0SLionel Sambuc 
1593ebfedea0SLionel Sambuc     is_export = SSL_C_IS_EXPORT(cipher);
1594ebfedea0SLionel Sambuc     pkl = SSL_C_EXPORT_PKEYLENGTH(cipher);
1595ebfedea0SLionel Sambuc     kl = SSL_C_EXPORT_KEYLENGTH(cipher);
1596ebfedea0SLionel Sambuc     exp_str = is_export ? " export" : "";
1597ebfedea0SLionel Sambuc 
1598ebfedea0SLionel Sambuc     if (alg_ssl & SSL_SSLV2)
1599ebfedea0SLionel Sambuc         ver = "SSLv2";
1600ebfedea0SLionel Sambuc     else if (alg_ssl & SSL_SSLV3)
1601ebfedea0SLionel Sambuc         ver = "SSLv3";
1602ebfedea0SLionel Sambuc     else if (alg_ssl & SSL_TLSV1_2)
1603ebfedea0SLionel Sambuc         ver = "TLSv1.2";
1604ebfedea0SLionel Sambuc     else
1605ebfedea0SLionel Sambuc         ver = "unknown";
1606ebfedea0SLionel Sambuc 
1607*0a6a1f1dSLionel Sambuc     switch (alg_mkey) {
1608ebfedea0SLionel Sambuc     case SSL_kRSA:
1609ebfedea0SLionel Sambuc         kx = is_export ? (pkl == 512 ? "RSA(512)" : "RSA(1024)") : "RSA";
1610ebfedea0SLionel Sambuc         break;
1611ebfedea0SLionel Sambuc     case SSL_kDHr:
1612ebfedea0SLionel Sambuc         kx = "DH/RSA";
1613ebfedea0SLionel Sambuc         break;
1614ebfedea0SLionel Sambuc     case SSL_kDHd:
1615ebfedea0SLionel Sambuc         kx = "DH/DSS";
1616ebfedea0SLionel Sambuc         break;
1617ebfedea0SLionel Sambuc     case SSL_kKRB5:
1618ebfedea0SLionel Sambuc         kx = "KRB5";
1619ebfedea0SLionel Sambuc         break;
1620ebfedea0SLionel Sambuc     case SSL_kEDH:
1621ebfedea0SLionel Sambuc         kx = is_export ? (pkl == 512 ? "DH(512)" : "DH(1024)") : "DH";
1622ebfedea0SLionel Sambuc         break;
1623ebfedea0SLionel Sambuc     case SSL_kECDHr:
1624ebfedea0SLionel Sambuc         kx = "ECDH/RSA";
1625ebfedea0SLionel Sambuc         break;
1626ebfedea0SLionel Sambuc     case SSL_kECDHe:
1627ebfedea0SLionel Sambuc         kx = "ECDH/ECDSA";
1628ebfedea0SLionel Sambuc         break;
1629ebfedea0SLionel Sambuc     case SSL_kEECDH:
1630ebfedea0SLionel Sambuc         kx = "ECDH";
1631ebfedea0SLionel Sambuc         break;
1632ebfedea0SLionel Sambuc     case SSL_kPSK:
1633ebfedea0SLionel Sambuc         kx = "PSK";
1634ebfedea0SLionel Sambuc         break;
1635ebfedea0SLionel Sambuc     case SSL_kSRP:
1636ebfedea0SLionel Sambuc         kx = "SRP";
1637ebfedea0SLionel Sambuc         break;
1638*0a6a1f1dSLionel Sambuc     case SSL_kGOST:
1639*0a6a1f1dSLionel Sambuc         kx = "GOST";
1640*0a6a1f1dSLionel Sambuc         break;
1641ebfedea0SLionel Sambuc     default:
1642ebfedea0SLionel Sambuc         kx = "unknown";
1643ebfedea0SLionel Sambuc     }
1644ebfedea0SLionel Sambuc 
1645*0a6a1f1dSLionel Sambuc     switch (alg_auth) {
1646ebfedea0SLionel Sambuc     case SSL_aRSA:
1647ebfedea0SLionel Sambuc         au = "RSA";
1648ebfedea0SLionel Sambuc         break;
1649ebfedea0SLionel Sambuc     case SSL_aDSS:
1650ebfedea0SLionel Sambuc         au = "DSS";
1651ebfedea0SLionel Sambuc         break;
1652ebfedea0SLionel Sambuc     case SSL_aDH:
1653ebfedea0SLionel Sambuc         au = "DH";
1654ebfedea0SLionel Sambuc         break;
1655ebfedea0SLionel Sambuc     case SSL_aKRB5:
1656ebfedea0SLionel Sambuc         au = "KRB5";
1657ebfedea0SLionel Sambuc         break;
1658ebfedea0SLionel Sambuc     case SSL_aECDH:
1659ebfedea0SLionel Sambuc         au = "ECDH";
1660ebfedea0SLionel Sambuc         break;
1661ebfedea0SLionel Sambuc     case SSL_aNULL:
1662ebfedea0SLionel Sambuc         au = "None";
1663ebfedea0SLionel Sambuc         break;
1664ebfedea0SLionel Sambuc     case SSL_aECDSA:
1665ebfedea0SLionel Sambuc         au = "ECDSA";
1666ebfedea0SLionel Sambuc         break;
1667ebfedea0SLionel Sambuc     case SSL_aPSK:
1668ebfedea0SLionel Sambuc         au = "PSK";
1669ebfedea0SLionel Sambuc         break;
1670*0a6a1f1dSLionel Sambuc     case SSL_aSRP:
1671*0a6a1f1dSLionel Sambuc         au = "SRP";
1672*0a6a1f1dSLionel Sambuc         break;
1673*0a6a1f1dSLionel Sambuc     case SSL_aGOST94:
1674*0a6a1f1dSLionel Sambuc         au = "GOST94";
1675*0a6a1f1dSLionel Sambuc         break;
1676*0a6a1f1dSLionel Sambuc     case SSL_aGOST01:
1677*0a6a1f1dSLionel Sambuc         au = "GOST01";
1678*0a6a1f1dSLionel Sambuc         break;
1679ebfedea0SLionel Sambuc     default:
1680ebfedea0SLionel Sambuc         au = "unknown";
1681ebfedea0SLionel Sambuc         break;
1682ebfedea0SLionel Sambuc     }
1683ebfedea0SLionel Sambuc 
1684*0a6a1f1dSLionel Sambuc     switch (alg_enc) {
1685ebfedea0SLionel Sambuc     case SSL_DES:
1686ebfedea0SLionel Sambuc         enc = (is_export && kl == 5) ? "DES(40)" : "DES(56)";
1687ebfedea0SLionel Sambuc         break;
1688ebfedea0SLionel Sambuc     case SSL_3DES:
1689ebfedea0SLionel Sambuc         enc = "3DES(168)";
1690ebfedea0SLionel Sambuc         break;
1691ebfedea0SLionel Sambuc     case SSL_RC4:
1692ebfedea0SLionel Sambuc         enc = is_export ? (kl == 5 ? "RC4(40)" : "RC4(56)")
1693ebfedea0SLionel Sambuc             : ((alg2 & SSL2_CF_8_BYTE_ENC) ? "RC4(64)" : "RC4(128)");
1694ebfedea0SLionel Sambuc         break;
1695ebfedea0SLionel Sambuc     case SSL_RC2:
1696ebfedea0SLionel Sambuc         enc = is_export ? (kl == 5 ? "RC2(40)" : "RC2(56)") : "RC2(128)";
1697ebfedea0SLionel Sambuc         break;
1698ebfedea0SLionel Sambuc     case SSL_IDEA:
1699ebfedea0SLionel Sambuc         enc = "IDEA(128)";
1700ebfedea0SLionel Sambuc         break;
1701ebfedea0SLionel Sambuc     case SSL_eNULL:
1702ebfedea0SLionel Sambuc         enc = "None";
1703ebfedea0SLionel Sambuc         break;
1704ebfedea0SLionel Sambuc     case SSL_AES128:
1705ebfedea0SLionel Sambuc         enc = "AES(128)";
1706ebfedea0SLionel Sambuc         break;
1707ebfedea0SLionel Sambuc     case SSL_AES256:
1708ebfedea0SLionel Sambuc         enc = "AES(256)";
1709ebfedea0SLionel Sambuc         break;
1710ebfedea0SLionel Sambuc     case SSL_AES128GCM:
1711ebfedea0SLionel Sambuc         enc = "AESGCM(128)";
1712ebfedea0SLionel Sambuc         break;
1713ebfedea0SLionel Sambuc     case SSL_AES256GCM:
1714ebfedea0SLionel Sambuc         enc = "AESGCM(256)";
1715ebfedea0SLionel Sambuc         break;
1716ebfedea0SLionel Sambuc     case SSL_CAMELLIA128:
1717ebfedea0SLionel Sambuc         enc = "Camellia(128)";
1718ebfedea0SLionel Sambuc         break;
1719ebfedea0SLionel Sambuc     case SSL_CAMELLIA256:
1720ebfedea0SLionel Sambuc         enc = "Camellia(256)";
1721ebfedea0SLionel Sambuc         break;
1722ebfedea0SLionel Sambuc     case SSL_SEED:
1723ebfedea0SLionel Sambuc         enc = "SEED(128)";
1724ebfedea0SLionel Sambuc         break;
1725*0a6a1f1dSLionel Sambuc     case SSL_eGOST2814789CNT:
1726*0a6a1f1dSLionel Sambuc         enc = "GOST89(256)";
1727*0a6a1f1dSLionel Sambuc         break;
1728ebfedea0SLionel Sambuc     default:
1729ebfedea0SLionel Sambuc         enc = "unknown";
1730ebfedea0SLionel Sambuc         break;
1731ebfedea0SLionel Sambuc     }
1732ebfedea0SLionel Sambuc 
1733*0a6a1f1dSLionel Sambuc     switch (alg_mac) {
1734ebfedea0SLionel Sambuc     case SSL_MD5:
1735ebfedea0SLionel Sambuc         mac = "MD5";
1736ebfedea0SLionel Sambuc         break;
1737ebfedea0SLionel Sambuc     case SSL_SHA1:
1738ebfedea0SLionel Sambuc         mac = "SHA1";
1739ebfedea0SLionel Sambuc         break;
1740ebfedea0SLionel Sambuc     case SSL_SHA256:
1741ebfedea0SLionel Sambuc         mac = "SHA256";
1742ebfedea0SLionel Sambuc         break;
1743ebfedea0SLionel Sambuc     case SSL_SHA384:
1744ebfedea0SLionel Sambuc         mac = "SHA384";
1745ebfedea0SLionel Sambuc         break;
1746ebfedea0SLionel Sambuc     case SSL_AEAD:
1747ebfedea0SLionel Sambuc         mac = "AEAD";
1748ebfedea0SLionel Sambuc         break;
1749*0a6a1f1dSLionel Sambuc     case SSL_GOST89MAC:
1750*0a6a1f1dSLionel Sambuc         mac = "GOST89";
1751*0a6a1f1dSLionel Sambuc         break;
1752*0a6a1f1dSLionel Sambuc     case SSL_GOST94:
1753*0a6a1f1dSLionel Sambuc         mac = "GOST94";
1754*0a6a1f1dSLionel Sambuc         break;
1755ebfedea0SLionel Sambuc     default:
1756ebfedea0SLionel Sambuc         mac = "unknown";
1757ebfedea0SLionel Sambuc         break;
1758ebfedea0SLionel Sambuc     }
1759ebfedea0SLionel Sambuc 
1760*0a6a1f1dSLionel Sambuc     if (buf == NULL) {
1761ebfedea0SLionel Sambuc         len = 128;
1762ebfedea0SLionel Sambuc         buf = OPENSSL_malloc(len);
1763*0a6a1f1dSLionel Sambuc         if (buf == NULL)
1764*0a6a1f1dSLionel Sambuc             return ("OPENSSL_malloc Error");
1765*0a6a1f1dSLionel Sambuc     } else if (len < 128)
1766ebfedea0SLionel Sambuc         return ("Buffer too small");
1767ebfedea0SLionel Sambuc 
1768ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1769*0a6a1f1dSLionel Sambuc     BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac,
1770*0a6a1f1dSLionel Sambuc                  exp_str, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl);
1771ebfedea0SLionel Sambuc #else
1772*0a6a1f1dSLionel Sambuc     BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac,
1773*0a6a1f1dSLionel Sambuc                  exp_str);
1774ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1775ebfedea0SLionel Sambuc     return (buf);
1776ebfedea0SLionel Sambuc }
1777ebfedea0SLionel Sambuc 
SSL_CIPHER_get_version(const SSL_CIPHER * c)1778ebfedea0SLionel Sambuc char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
1779ebfedea0SLionel Sambuc {
1780ebfedea0SLionel Sambuc     int i;
1781ebfedea0SLionel Sambuc 
1782*0a6a1f1dSLionel Sambuc     if (c == NULL)
1783*0a6a1f1dSLionel Sambuc         return ("(NONE)");
1784ebfedea0SLionel Sambuc     i = (int)(c->id >> 24L);
1785ebfedea0SLionel Sambuc     if (i == 3)
1786ebfedea0SLionel Sambuc         return ("TLSv1/SSLv3");
1787ebfedea0SLionel Sambuc     else if (i == 2)
1788ebfedea0SLionel Sambuc         return ("SSLv2");
1789ebfedea0SLionel Sambuc     else
1790ebfedea0SLionel Sambuc         return ("unknown");
1791ebfedea0SLionel Sambuc }
1792ebfedea0SLionel Sambuc 
1793ebfedea0SLionel Sambuc /* return the actual cipher being used */
SSL_CIPHER_get_name(const SSL_CIPHER * c)1794ebfedea0SLionel Sambuc const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1795ebfedea0SLionel Sambuc {
1796ebfedea0SLionel Sambuc     if (c != NULL)
1797ebfedea0SLionel Sambuc         return (c->name);
1798ebfedea0SLionel Sambuc     return ("(NONE)");
1799ebfedea0SLionel Sambuc }
1800ebfedea0SLionel Sambuc 
1801ebfedea0SLionel Sambuc /* number of bits for symmetric cipher */
SSL_CIPHER_get_bits(const SSL_CIPHER * c,int * alg_bits)1802ebfedea0SLionel Sambuc int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1803ebfedea0SLionel Sambuc {
1804ebfedea0SLionel Sambuc     int ret = 0;
1805ebfedea0SLionel Sambuc 
1806*0a6a1f1dSLionel Sambuc     if (c != NULL) {
1807*0a6a1f1dSLionel Sambuc         if (alg_bits != NULL)
1808*0a6a1f1dSLionel Sambuc             *alg_bits = c->alg_bits;
1809ebfedea0SLionel Sambuc         ret = c->strength_bits;
1810ebfedea0SLionel Sambuc     }
1811ebfedea0SLionel Sambuc     return (ret);
1812ebfedea0SLionel Sambuc }
1813ebfedea0SLionel Sambuc 
SSL_CIPHER_get_id(const SSL_CIPHER * c)1814ebfedea0SLionel Sambuc unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1815ebfedea0SLionel Sambuc {
1816ebfedea0SLionel Sambuc     return c->id;
1817ebfedea0SLionel Sambuc }
1818ebfedea0SLionel Sambuc 
ssl3_comp_find(STACK_OF (SSL_COMP)* sk,int n)1819ebfedea0SLionel Sambuc SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
1820ebfedea0SLionel Sambuc {
1821ebfedea0SLionel Sambuc     SSL_COMP *ctmp;
1822ebfedea0SLionel Sambuc     int i, nn;
1823ebfedea0SLionel Sambuc 
1824*0a6a1f1dSLionel Sambuc     if ((n == 0) || (sk == NULL))
1825*0a6a1f1dSLionel Sambuc         return (NULL);
1826ebfedea0SLionel Sambuc     nn = sk_SSL_COMP_num(sk);
1827*0a6a1f1dSLionel Sambuc     for (i = 0; i < nn; i++) {
1828ebfedea0SLionel Sambuc         ctmp = sk_SSL_COMP_value(sk, i);
1829ebfedea0SLionel Sambuc         if (ctmp->id == n)
1830ebfedea0SLionel Sambuc             return (ctmp);
1831ebfedea0SLionel Sambuc     }
1832ebfedea0SLionel Sambuc     return (NULL);
1833ebfedea0SLionel Sambuc }
1834ebfedea0SLionel Sambuc 
1835ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
SSL_COMP_get_compression_methods(void)1836ebfedea0SLionel Sambuc void *SSL_COMP_get_compression_methods(void)
1837ebfedea0SLionel Sambuc {
1838ebfedea0SLionel Sambuc     return NULL;
1839ebfedea0SLionel Sambuc }
1840*0a6a1f1dSLionel Sambuc 
SSL_COMP_add_compression_method(int id,void * cm)1841ebfedea0SLionel Sambuc int SSL_COMP_add_compression_method(int id, void *cm)
1842ebfedea0SLionel Sambuc {
1843ebfedea0SLionel Sambuc     return 1;
1844ebfedea0SLionel Sambuc }
1845ebfedea0SLionel Sambuc 
SSL_COMP_get_name(const void * comp)1846ebfedea0SLionel Sambuc const char *SSL_COMP_get_name(const void *comp)
1847ebfedea0SLionel Sambuc {
1848ebfedea0SLionel Sambuc     return NULL;
1849ebfedea0SLionel Sambuc }
1850ebfedea0SLionel Sambuc #else
STACK_OF(SSL_COMP)1851ebfedea0SLionel Sambuc STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1852ebfedea0SLionel Sambuc {
1853ebfedea0SLionel Sambuc     load_builtin_compressions();
1854ebfedea0SLionel Sambuc     return (ssl_comp_methods);
1855ebfedea0SLionel Sambuc }
1856ebfedea0SLionel Sambuc 
SSL_COMP_add_compression_method(int id,COMP_METHOD * cm)1857ebfedea0SLionel Sambuc int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
1858ebfedea0SLionel Sambuc {
1859ebfedea0SLionel Sambuc     SSL_COMP *comp;
1860ebfedea0SLionel Sambuc 
1861ebfedea0SLionel Sambuc     if (cm == NULL || cm->type == NID_undef)
1862ebfedea0SLionel Sambuc         return 1;
1863ebfedea0SLionel Sambuc 
1864*0a6a1f1dSLionel Sambuc     /*-
1865*0a6a1f1dSLionel Sambuc      * According to draft-ietf-tls-compression-04.txt, the
1866*0a6a1f1dSLionel Sambuc      * compression number ranges should be the following:
1867*0a6a1f1dSLionel Sambuc      *
1868*0a6a1f1dSLionel Sambuc      *   0 to  63:  methods defined by the IETF
1869*0a6a1f1dSLionel Sambuc      *  64 to 192:  external party methods assigned by IANA
1870*0a6a1f1dSLionel Sambuc      * 193 to 255:  reserved for private use
1871*0a6a1f1dSLionel Sambuc      */
1872*0a6a1f1dSLionel Sambuc     if (id < 193 || id > 255) {
1873*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
1874*0a6a1f1dSLionel Sambuc                SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
1875ebfedea0SLionel Sambuc         return 0;
1876ebfedea0SLionel Sambuc     }
1877ebfedea0SLionel Sambuc 
1878ebfedea0SLionel Sambuc     MemCheck_off();
1879ebfedea0SLionel Sambuc     comp = (SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
1880ebfedea0SLionel Sambuc     comp->id = id;
1881ebfedea0SLionel Sambuc     comp->method = cm;
1882ebfedea0SLionel Sambuc     load_builtin_compressions();
1883*0a6a1f1dSLionel Sambuc     if (ssl_comp_methods && sk_SSL_COMP_find(ssl_comp_methods, comp) >= 0) {
1884ebfedea0SLionel Sambuc         OPENSSL_free(comp);
1885ebfedea0SLionel Sambuc         MemCheck_on();
1886*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
1887*0a6a1f1dSLionel Sambuc                SSL_R_DUPLICATE_COMPRESSION_ID);
1888ebfedea0SLionel Sambuc         return (1);
1889*0a6a1f1dSLionel Sambuc     } else if ((ssl_comp_methods == NULL)
1890*0a6a1f1dSLionel Sambuc                || !sk_SSL_COMP_push(ssl_comp_methods, comp)) {
1891ebfedea0SLionel Sambuc         OPENSSL_free(comp);
1892ebfedea0SLionel Sambuc         MemCheck_on();
1893ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
1894ebfedea0SLionel Sambuc         return (1);
1895*0a6a1f1dSLionel Sambuc     } else {
1896ebfedea0SLionel Sambuc         MemCheck_on();
1897ebfedea0SLionel Sambuc         return (0);
1898ebfedea0SLionel Sambuc     }
1899ebfedea0SLionel Sambuc }
1900ebfedea0SLionel Sambuc 
SSL_COMP_get_name(const COMP_METHOD * comp)1901ebfedea0SLionel Sambuc const char *SSL_COMP_get_name(const COMP_METHOD *comp)
1902ebfedea0SLionel Sambuc {
1903ebfedea0SLionel Sambuc     if (comp)
1904ebfedea0SLionel Sambuc         return comp->name;
1905ebfedea0SLionel Sambuc     return NULL;
1906ebfedea0SLionel Sambuc }
1907ebfedea0SLionel Sambuc 
1908ebfedea0SLionel Sambuc #endif
1909