914d0d8f4aa6e1dab25626a4096a962fe96a51df
[openssl.git] / ssl / ssl_ciph.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  * Copyright 2005 Nokia. All rights reserved.
5  *
6  * Licensed under the OpenSSL license (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <openssl/objects.h>
15 #include <openssl/comp.h>
16 #include <openssl/engine.h>
17 #include <openssl/crypto.h>
18 #include "ssl_locl.h"
19 #include "internal/thread_once.h"
20 #include "internal/cryptlib.h"
21
22 #define SSL_ENC_DES_IDX         0
23 #define SSL_ENC_3DES_IDX        1
24 #define SSL_ENC_RC4_IDX         2
25 #define SSL_ENC_RC2_IDX         3
26 #define SSL_ENC_IDEA_IDX        4
27 #define SSL_ENC_NULL_IDX        5
28 #define SSL_ENC_AES128_IDX      6
29 #define SSL_ENC_AES256_IDX      7
30 #define SSL_ENC_CAMELLIA128_IDX 8
31 #define SSL_ENC_CAMELLIA256_IDX 9
32 #define SSL_ENC_GOST89_IDX      10
33 #define SSL_ENC_SEED_IDX        11
34 #define SSL_ENC_AES128GCM_IDX   12
35 #define SSL_ENC_AES256GCM_IDX   13
36 #define SSL_ENC_AES128CCM_IDX   14
37 #define SSL_ENC_AES256CCM_IDX   15
38 #define SSL_ENC_AES128CCM8_IDX  16
39 #define SSL_ENC_AES256CCM8_IDX  17
40 #define SSL_ENC_GOST8912_IDX    18
41 #define SSL_ENC_CHACHA_IDX      19
42 #define SSL_ENC_NUM_IDX         20
43
44 /* NB: make sure indices in these tables match values above */
45
46 typedef struct {
47     uint32_t mask;
48     int nid;
49 } ssl_cipher_table;
50
51 /* Table of NIDs for each cipher */
52 static const ssl_cipher_table ssl_cipher_table_cipher[SSL_ENC_NUM_IDX] = {
53     {SSL_DES, NID_des_cbc},     /* SSL_ENC_DES_IDX 0 */
54     {SSL_3DES, NID_des_ede3_cbc}, /* SSL_ENC_3DES_IDX 1 */
55     {SSL_RC4, NID_rc4},         /* SSL_ENC_RC4_IDX 2 */
56     {SSL_RC2, NID_rc2_cbc},     /* SSL_ENC_RC2_IDX 3 */
57     {SSL_IDEA, NID_idea_cbc},   /* SSL_ENC_IDEA_IDX 4 */
58     {SSL_eNULL, NID_undef},     /* SSL_ENC_NULL_IDX 5 */
59     {SSL_AES128, NID_aes_128_cbc}, /* SSL_ENC_AES128_IDX 6 */
60     {SSL_AES256, NID_aes_256_cbc}, /* SSL_ENC_AES256_IDX 7 */
61     {SSL_CAMELLIA128, NID_camellia_128_cbc}, /* SSL_ENC_CAMELLIA128_IDX 8 */
62     {SSL_CAMELLIA256, NID_camellia_256_cbc}, /* SSL_ENC_CAMELLIA256_IDX 9 */
63     {SSL_eGOST2814789CNT, NID_gost89_cnt}, /* SSL_ENC_GOST89_IDX 10 */
64     {SSL_SEED, NID_seed_cbc},   /* SSL_ENC_SEED_IDX 11 */
65     {SSL_AES128GCM, NID_aes_128_gcm}, /* SSL_ENC_AES128GCM_IDX 12 */
66     {SSL_AES256GCM, NID_aes_256_gcm}, /* SSL_ENC_AES256GCM_IDX 13 */
67     {SSL_AES128CCM, NID_aes_128_ccm}, /* SSL_ENC_AES128CCM_IDX 14 */
68     {SSL_AES256CCM, NID_aes_256_ccm}, /* SSL_ENC_AES256CCM_IDX 15 */
69     {SSL_AES128CCM8, NID_aes_128_ccm}, /* SSL_ENC_AES128CCM8_IDX 16 */
70     {SSL_AES256CCM8, NID_aes_256_ccm}, /* SSL_ENC_AES256CCM8_IDX 17 */
71     {SSL_eGOST2814789CNT12, NID_gost89_cnt_12}, /* SSL_ENC_GOST8912_IDX */
72     {SSL_CHACHA20POLY1305, NID_chacha20_poly1305},
73 };
74
75 static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX];
76
77 #define SSL_COMP_NULL_IDX       0
78 #define SSL_COMP_ZLIB_IDX       1
79 #define SSL_COMP_NUM_IDX        2
80
81 static STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
82
83 #ifndef OPENSSL_NO_COMP
84 static CRYPTO_ONCE ssl_load_builtin_comp_once = CRYPTO_ONCE_STATIC_INIT;
85 #endif
86
87 /*
88  * Constant SSL_MAX_DIGEST equal to size of digests array should be defined
89  * in the ssl_locl.h
90  */
91
92 #define SSL_MD_NUM_IDX  SSL_MAX_DIGEST
93
94 /* NB: make sure indices in this table matches values above */
95 static const ssl_cipher_table ssl_cipher_table_mac[SSL_MD_NUM_IDX] = {
96     {SSL_MD5, NID_md5},         /* SSL_MD_MD5_IDX 0 */
97     {SSL_SHA1, NID_sha1},       /* SSL_MD_SHA1_IDX 1 */
98     {SSL_GOST94, NID_id_GostR3411_94}, /* SSL_MD_GOST94_IDX 2 */
99     {SSL_GOST89MAC, NID_id_Gost28147_89_MAC}, /* SSL_MD_GOST89MAC_IDX 3 */
100     {SSL_SHA256, NID_sha256},   /* SSL_MD_SHA256_IDX 4 */
101     {SSL_SHA384, NID_sha384},   /* SSL_MD_SHA384_IDX 5 */
102     {SSL_GOST12_256, NID_id_GostR3411_2012_256}, /* SSL_MD_GOST12_256_IDX 6 */
103     {SSL_GOST89MAC12, NID_gost_mac_12}, /* SSL_MD_GOST89MAC12_IDX 7 */
104     {SSL_GOST12_512, NID_id_GostR3411_2012_512}, /* SSL_MD_GOST12_512_IDX 8 */
105     {0, NID_md5_sha1},          /* SSL_MD_MD5_SHA1_IDX 9 */
106     {0, NID_sha224},            /* SSL_MD_SHA224_IDX 10 */
107     {0, NID_sha512}             /* SSL_MD_SHA512_IDX 11 */
108 };
109
110 static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = {
111     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
112 };
113
114 /* *INDENT-OFF* */
115 static const ssl_cipher_table ssl_cipher_table_kx[] = {
116     {SSL_kRSA,      NID_kx_rsa},
117     {SSL_kECDHE,    NID_kx_ecdhe},
118     {SSL_kDHE,      NID_kx_dhe},
119     {SSL_kECDHEPSK, NID_kx_ecdhe_psk},
120     {SSL_kDHEPSK,   NID_kx_dhe_psk},
121     {SSL_kRSAPSK,   NID_kx_rsa_psk},
122     {SSL_kPSK,      NID_kx_psk},
123     {SSL_kSRP,      NID_kx_srp},
124     {SSL_kGOST,     NID_kx_gost},
125     {SSL_kANY,      NID_kx_any}
126 };
127
128 static const ssl_cipher_table ssl_cipher_table_auth[] = {
129     {SSL_aRSA,    NID_auth_rsa},
130     {SSL_aECDSA,  NID_auth_ecdsa},
131     {SSL_aPSK,    NID_auth_psk},
132     {SSL_aDSS,    NID_auth_dss},
133     {SSL_aGOST01, NID_auth_gost01},
134     {SSL_aGOST12, NID_auth_gost12},
135     {SSL_aSRP,    NID_auth_srp},
136     {SSL_aNULL,   NID_auth_null},
137     {SSL_aANY,    NID_auth_any}
138 };
139 /* *INDENT-ON* */
140
141 /* Utility function for table lookup */
142 static int ssl_cipher_info_find(const ssl_cipher_table * table,
143                                 size_t table_cnt, uint32_t mask)
144 {
145     size_t i;
146     for (i = 0; i < table_cnt; i++, table++) {
147         if (table->mask == mask)
148             return (int)i;
149     }
150     return -1;
151 }
152
153 #define ssl_cipher_info_lookup(table, x) \
154     ssl_cipher_info_find(table, OSSL_NELEM(table), x)
155
156 /*
157  * PKEY_TYPE for GOST89MAC is known in advance, but, because implementation
158  * is engine-provided, we'll fill it only if corresponding EVP_PKEY_METHOD is
159  * found
160  */
161 static int ssl_mac_pkey_id[SSL_MD_NUM_IDX] = {
162     /* MD5, SHA, GOST94, MAC89 */
163     EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
164     /* SHA256, SHA384, GOST2012_256, MAC89-12 */
165     EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
166     /* GOST2012_512 */
167     EVP_PKEY_HMAC,
168 };
169
170 static size_t ssl_mac_secret_size[SSL_MD_NUM_IDX];
171
172 #define CIPHER_ADD      1
173 #define CIPHER_KILL     2
174 #define CIPHER_DEL      3
175 #define CIPHER_ORD      4
176 #define CIPHER_SPECIAL  5
177 /*
178  * Bump the ciphers to the top of the list.
179  * This rule isn't currently supported by the public cipherstring API.
180  */
181 #define CIPHER_BUMP     6
182
183 typedef struct cipher_order_st {
184     const SSL_CIPHER *cipher;
185     int active;
186     int dead;
187     struct cipher_order_st *next, *prev;
188 } CIPHER_ORDER;
189
190 static const SSL_CIPHER cipher_aliases[] = {
191     /* "ALL" doesn't include eNULL (must be specifically enabled) */
192     {0, SSL_TXT_ALL, NULL, 0, 0, 0, ~SSL_eNULL},
193     /* "COMPLEMENTOFALL" */
194     {0, SSL_TXT_CMPALL, NULL, 0, 0, 0, SSL_eNULL},
195
196     /*
197      * "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in
198      * ALL!)
199      */
200     {0, SSL_TXT_CMPDEF, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_NOT_DEFAULT},
201
202     /*
203      * key exchange aliases (some of those using only a single bit here
204      * combine multiple key exchange algs according to the RFCs, e.g. kDHE
205      * combines DHE_DSS and DHE_RSA)
206      */
207     {0, SSL_TXT_kRSA, NULL, 0, SSL_kRSA},
208
209     {0, SSL_TXT_kEDH, NULL, 0, SSL_kDHE},
210     {0, SSL_TXT_kDHE, NULL, 0, SSL_kDHE},
211     {0, SSL_TXT_DH, NULL, 0, SSL_kDHE},
212
213     {0, SSL_TXT_kEECDH, NULL, 0, SSL_kECDHE},
214     {0, SSL_TXT_kECDHE, NULL, 0, SSL_kECDHE},
215     {0, SSL_TXT_ECDH, NULL, 0, SSL_kECDHE},
216
217     {0, SSL_TXT_kPSK, NULL, 0, SSL_kPSK},
218     {0, SSL_TXT_kRSAPSK, NULL, 0, SSL_kRSAPSK},
219     {0, SSL_TXT_kECDHEPSK, NULL, 0, SSL_kECDHEPSK},
220     {0, SSL_TXT_kDHEPSK, NULL, 0, SSL_kDHEPSK},
221     {0, SSL_TXT_kSRP, NULL, 0, SSL_kSRP},
222     {0, SSL_TXT_kGOST, NULL, 0, SSL_kGOST},
223
224     /* server authentication aliases */
225     {0, SSL_TXT_aRSA, NULL, 0, 0, SSL_aRSA},
226     {0, SSL_TXT_aDSS, NULL, 0, 0, SSL_aDSS},
227     {0, SSL_TXT_DSS, NULL, 0, 0, SSL_aDSS},
228     {0, SSL_TXT_aNULL, NULL, 0, 0, SSL_aNULL},
229     {0, SSL_TXT_aECDSA, NULL, 0, 0, SSL_aECDSA},
230     {0, SSL_TXT_ECDSA, NULL, 0, 0, SSL_aECDSA},
231     {0, SSL_TXT_aPSK, NULL, 0, 0, SSL_aPSK},
232     {0, SSL_TXT_aGOST01, NULL, 0, 0, SSL_aGOST01},
233     {0, SSL_TXT_aGOST12, NULL, 0, 0, SSL_aGOST12},
234     {0, SSL_TXT_aGOST, NULL, 0, 0, SSL_aGOST01 | SSL_aGOST12},
235     {0, SSL_TXT_aSRP, NULL, 0, 0, SSL_aSRP},
236
237     /* aliases combining key exchange and server authentication */
238     {0, SSL_TXT_EDH, NULL, 0, SSL_kDHE, ~SSL_aNULL},
239     {0, SSL_TXT_DHE, NULL, 0, SSL_kDHE, ~SSL_aNULL},
240     {0, SSL_TXT_EECDH, NULL, 0, SSL_kECDHE, ~SSL_aNULL},
241     {0, SSL_TXT_ECDHE, NULL, 0, SSL_kECDHE, ~SSL_aNULL},
242     {0, SSL_TXT_NULL, NULL, 0, 0, 0, SSL_eNULL},
243     {0, SSL_TXT_RSA, NULL, 0, SSL_kRSA, SSL_aRSA},
244     {0, SSL_TXT_ADH, NULL, 0, SSL_kDHE, SSL_aNULL},
245     {0, SSL_TXT_AECDH, NULL, 0, SSL_kECDHE, SSL_aNULL},
246     {0, SSL_TXT_PSK, NULL, 0, SSL_PSK},
247     {0, SSL_TXT_SRP, NULL, 0, SSL_kSRP},
248
249     /* symmetric encryption aliases */
250     {0, SSL_TXT_3DES, NULL, 0, 0, 0, SSL_3DES},
251     {0, SSL_TXT_RC4, NULL, 0, 0, 0, SSL_RC4},
252     {0, SSL_TXT_RC2, NULL, 0, 0, 0, SSL_RC2},
253     {0, SSL_TXT_IDEA, NULL, 0, 0, 0, SSL_IDEA},
254     {0, SSL_TXT_SEED, NULL, 0, 0, 0, SSL_SEED},
255     {0, SSL_TXT_eNULL, NULL, 0, 0, 0, SSL_eNULL},
256     {0, SSL_TXT_GOST, NULL, 0, 0, 0, SSL_eGOST2814789CNT | SSL_eGOST2814789CNT12},
257     {0, SSL_TXT_AES128, NULL, 0, 0, 0,
258      SSL_AES128 | SSL_AES128GCM | SSL_AES128CCM | SSL_AES128CCM8},
259     {0, SSL_TXT_AES256, NULL, 0, 0, 0,
260      SSL_AES256 | SSL_AES256GCM | SSL_AES256CCM | SSL_AES256CCM8},
261     {0, SSL_TXT_AES, NULL, 0, 0, 0, SSL_AES},
262     {0, SSL_TXT_AES_GCM, NULL, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM},
263     {0, SSL_TXT_AES_CCM, NULL, 0, 0, 0,
264      SSL_AES128CCM | SSL_AES256CCM | SSL_AES128CCM8 | SSL_AES256CCM8},
265     {0, SSL_TXT_AES_CCM_8, NULL, 0, 0, 0, SSL_AES128CCM8 | SSL_AES256CCM8},
266     {0, SSL_TXT_CAMELLIA128, NULL, 0, 0, 0, SSL_CAMELLIA128},
267     {0, SSL_TXT_CAMELLIA256, NULL, 0, 0, 0, SSL_CAMELLIA256},
268     {0, SSL_TXT_CAMELLIA, NULL, 0, 0, 0, SSL_CAMELLIA},
269     {0, SSL_TXT_CHACHA20, NULL, 0, 0, 0, SSL_CHACHA20},
270
271     /* MAC aliases */
272     {0, SSL_TXT_MD5, NULL, 0, 0, 0, 0, SSL_MD5},
273     {0, SSL_TXT_SHA1, NULL, 0, 0, 0, 0, SSL_SHA1},
274     {0, SSL_TXT_SHA, NULL, 0, 0, 0, 0, SSL_SHA1},
275     {0, SSL_TXT_GOST94, NULL, 0, 0, 0, 0, SSL_GOST94},
276     {0, SSL_TXT_GOST89MAC, NULL, 0, 0, 0, 0, SSL_GOST89MAC | SSL_GOST89MAC12},
277     {0, SSL_TXT_SHA256, NULL, 0, 0, 0, 0, SSL_SHA256},
278     {0, SSL_TXT_SHA384, NULL, 0, 0, 0, 0, SSL_SHA384},
279     {0, SSL_TXT_GOST12, NULL, 0, 0, 0, 0, SSL_GOST12_256},
280
281     /* protocol version aliases */
282     {0, SSL_TXT_SSLV3, NULL, 0, 0, 0, 0, 0, SSL3_VERSION},
283     {0, SSL_TXT_TLSV1, NULL, 0, 0, 0, 0, 0, TLS1_VERSION},
284     {0, "TLSv1.0", NULL, 0, 0, 0, 0, 0, TLS1_VERSION},
285     {0, SSL_TXT_TLSV1_2, NULL, 0, 0, 0, 0, 0, TLS1_2_VERSION},
286
287     /* strength classes */
288     {0, SSL_TXT_LOW, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_LOW},
289     {0, SSL_TXT_MEDIUM, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_MEDIUM},
290     {0, SSL_TXT_HIGH, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_HIGH},
291     /* FIPS 140-2 approved ciphersuite */
292     {0, SSL_TXT_FIPS, NULL, 0, 0, 0, ~SSL_eNULL, 0, 0, 0, 0, 0, SSL_FIPS},
293
294     /* "EDH-" aliases to "DHE-" labels (for backward compatibility) */
295     {0, SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA, NULL, 0,
296      SSL_kDHE, SSL_aDSS, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS},
297     {0, SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA, NULL, 0,
298      SSL_kDHE, SSL_aRSA, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS},
299
300 };
301
302 /*
303  * Search for public key algorithm with given name and return its pkey_id if
304  * it is available. Otherwise return 0
305  */
306 #ifdef OPENSSL_NO_ENGINE
307
308 static int get_optional_pkey_id(const char *pkey_name)
309 {
310     const EVP_PKEY_ASN1_METHOD *ameth;
311     int pkey_id = 0;
312     ameth = EVP_PKEY_asn1_find_str(NULL, pkey_name, -1);
313     if (ameth && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
314                                          ameth) > 0)
315         return pkey_id;
316     return 0;
317 }
318
319 #else
320
321 static int get_optional_pkey_id(const char *pkey_name)
322 {
323     const EVP_PKEY_ASN1_METHOD *ameth;
324     ENGINE *tmpeng = NULL;
325     int pkey_id = 0;
326     ameth = EVP_PKEY_asn1_find_str(&tmpeng, pkey_name, -1);
327     if (ameth) {
328         if (EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
329                                     ameth) <= 0)
330             pkey_id = 0;
331     }
332     ENGINE_finish(tmpeng);
333     return pkey_id;
334 }
335
336 #endif
337
338 /* masks of disabled algorithms */
339 static uint32_t disabled_enc_mask;
340 static uint32_t disabled_mac_mask;
341 static uint32_t disabled_mkey_mask;
342 static uint32_t disabled_auth_mask;
343
344 int ssl_load_ciphers(void)
345 {
346     size_t i;
347     const ssl_cipher_table *t;
348
349     disabled_enc_mask = 0;
350     ssl_sort_cipher_list();
351     for (i = 0, t = ssl_cipher_table_cipher; i < SSL_ENC_NUM_IDX; i++, t++) {
352         if (t->nid == NID_undef) {
353             ssl_cipher_methods[i] = NULL;
354         } else {
355             const EVP_CIPHER *cipher = EVP_get_cipherbynid(t->nid);
356             ssl_cipher_methods[i] = cipher;
357             if (cipher == NULL)
358                 disabled_enc_mask |= t->mask;
359         }
360     }
361     disabled_mac_mask = 0;
362     for (i = 0, t = ssl_cipher_table_mac; i < SSL_MD_NUM_IDX; i++, t++) {
363         const EVP_MD *md = EVP_get_digestbynid(t->nid);
364         ssl_digest_methods[i] = md;
365         if (md == NULL) {
366             disabled_mac_mask |= t->mask;
367         } else {
368             int tmpsize = EVP_MD_size(md);
369             if (!ossl_assert(tmpsize >= 0))
370                 return 0;
371             ssl_mac_secret_size[i] = tmpsize;
372         }
373     }
374     /* Make sure we can access MD5 and SHA1 */
375     if (!ossl_assert(ssl_digest_methods[SSL_MD_MD5_IDX] != NULL))
376         return 0;
377     if (!ossl_assert(ssl_digest_methods[SSL_MD_SHA1_IDX] != NULL))
378         return 0;
379
380     disabled_mkey_mask = 0;
381     disabled_auth_mask = 0;
382
383 #ifdef OPENSSL_NO_RSA
384     disabled_mkey_mask |= SSL_kRSA | SSL_kRSAPSK;
385     disabled_auth_mask |= SSL_aRSA;
386 #endif
387 #ifdef OPENSSL_NO_DSA
388     disabled_auth_mask |= SSL_aDSS;
389 #endif
390 #ifdef OPENSSL_NO_DH
391     disabled_mkey_mask |= SSL_kDHE | SSL_kDHEPSK;
392 #endif
393 #ifdef OPENSSL_NO_EC
394     disabled_mkey_mask |= SSL_kECDHEPSK;
395     disabled_auth_mask |= SSL_aECDSA;
396 #endif
397 #ifdef OPENSSL_NO_PSK
398     disabled_mkey_mask |= SSL_PSK;
399     disabled_auth_mask |= SSL_aPSK;
400 #endif
401 #ifdef OPENSSL_NO_SRP
402     disabled_mkey_mask |= SSL_kSRP;
403 #endif
404
405     /*
406      * Check for presence of GOST 34.10 algorithms, and if they are not
407      * present, disable appropriate auth and key exchange
408      */
409     ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id("gost-mac");
410     if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
411         ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
412     else
413         disabled_mac_mask |= SSL_GOST89MAC;
414
415     ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] =
416         get_optional_pkey_id("gost-mac-12");
417     if (ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
418         ssl_mac_secret_size[SSL_MD_GOST89MAC12_IDX] = 32;
419     else
420         disabled_mac_mask |= SSL_GOST89MAC12;
421
422     if (!get_optional_pkey_id("gost2001"))
423         disabled_auth_mask |= SSL_aGOST01 | SSL_aGOST12;
424     if (!get_optional_pkey_id("gost2012_256"))
425         disabled_auth_mask |= SSL_aGOST12;
426     if (!get_optional_pkey_id("gost2012_512"))
427         disabled_auth_mask |= SSL_aGOST12;
428     /*
429      * Disable GOST key exchange if no GOST signature algs are available *
430      */
431     if ((disabled_auth_mask & (SSL_aGOST01 | SSL_aGOST12)) ==
432         (SSL_aGOST01 | SSL_aGOST12))
433         disabled_mkey_mask |= SSL_kGOST;
434
435     return 1;
436 }
437
438 #ifndef OPENSSL_NO_COMP
439
440 static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b)
441 {
442     return ((*a)->id - (*b)->id);
443 }
444
445 DEFINE_RUN_ONCE_STATIC(do_load_builtin_compressions)
446 {
447     SSL_COMP *comp = NULL;
448     COMP_METHOD *method = COMP_zlib();
449
450     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
451     ssl_comp_methods = sk_SSL_COMP_new(sk_comp_cmp);
452
453     if (COMP_get_type(method) != NID_undef && ssl_comp_methods != NULL) {
454         comp = OPENSSL_malloc(sizeof(*comp));
455         if (comp != NULL) {
456             comp->method = method;
457             comp->id = SSL_COMP_ZLIB_IDX;
458             comp->name = COMP_get_name(method);
459             sk_SSL_COMP_push(ssl_comp_methods, comp);
460             sk_SSL_COMP_sort(ssl_comp_methods);
461         }
462     }
463     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
464     return 1;
465 }
466
467 static int load_builtin_compressions(void)
468 {
469     return RUN_ONCE(&ssl_load_builtin_comp_once, do_load_builtin_compressions);
470 }
471 #endif
472
473 int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
474                        const EVP_MD **md, int *mac_pkey_type,
475                        size_t *mac_secret_size, SSL_COMP **comp, int use_etm)
476 {
477     int i;
478     const SSL_CIPHER *c;
479
480     c = s->cipher;
481     if (c == NULL)
482         return 0;
483     if (comp != NULL) {
484         SSL_COMP ctmp;
485 #ifndef OPENSSL_NO_COMP
486         if (!load_builtin_compressions()) {
487             /*
488              * Currently don't care, since a failure only means that
489              * ssl_comp_methods is NULL, which is perfectly OK
490              */
491         }
492 #endif
493         *comp = NULL;
494         ctmp.id = s->compress_meth;
495         if (ssl_comp_methods != NULL) {
496             i = sk_SSL_COMP_find(ssl_comp_methods, &ctmp);
497             if (i >= 0)
498                 *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
499             else
500                 *comp = NULL;
501         }
502         /* If were only interested in comp then return success */
503         if ((enc == NULL) && (md == NULL))
504             return 1;
505     }
506
507     if ((enc == NULL) || (md == NULL))
508         return 0;
509
510     i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
511
512     if (i == -1) {
513         *enc = NULL;
514     } else {
515         if (i == SSL_ENC_NULL_IDX)
516             *enc = EVP_enc_null();
517         else
518             *enc = ssl_cipher_methods[i];
519     }
520
521     i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
522     if (i == -1) {
523         *md = NULL;
524         if (mac_pkey_type != NULL)
525             *mac_pkey_type = NID_undef;
526         if (mac_secret_size != NULL)
527             *mac_secret_size = 0;
528         if (c->algorithm_mac == SSL_AEAD)
529             mac_pkey_type = NULL;
530     } else {
531         *md = ssl_digest_methods[i];
532         if (mac_pkey_type != NULL)
533             *mac_pkey_type = ssl_mac_pkey_id[i];
534         if (mac_secret_size != NULL)
535             *mac_secret_size = ssl_mac_secret_size[i];
536     }
537
538     if ((*enc != NULL) &&
539         (*md != NULL || (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
540         && (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
541         const EVP_CIPHER *evp;
542
543         if (use_etm)
544             return 1;
545
546         if (s->ssl_version >> 8 != TLS1_VERSION_MAJOR ||
547             s->ssl_version < TLS1_VERSION)
548             return 1;
549
550         if (c->algorithm_enc == SSL_RC4 &&
551             c->algorithm_mac == SSL_MD5 &&
552             (evp = EVP_get_cipherbyname("RC4-HMAC-MD5")))
553             *enc = evp, *md = NULL;
554         else if (c->algorithm_enc == SSL_AES128 &&
555                  c->algorithm_mac == SSL_SHA1 &&
556                  (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
557             *enc = evp, *md = NULL;
558         else if (c->algorithm_enc == SSL_AES256 &&
559                  c->algorithm_mac == SSL_SHA1 &&
560                  (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
561             *enc = evp, *md = NULL;
562         else if (c->algorithm_enc == SSL_AES128 &&
563                  c->algorithm_mac == SSL_SHA256 &&
564                  (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA256")))
565             *enc = evp, *md = NULL;
566         else if (c->algorithm_enc == SSL_AES256 &&
567                  c->algorithm_mac == SSL_SHA256 &&
568                  (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA256")))
569             *enc = evp, *md = NULL;
570         return 1;
571     } else {
572         return 0;
573     }
574 }
575
576 const EVP_MD *ssl_md(int idx)
577 {
578     idx &= SSL_HANDSHAKE_MAC_MASK;
579     if (idx < 0 || idx >= SSL_MD_NUM_IDX)
580         return NULL;
581     return ssl_digest_methods[idx];
582 }
583
584 const EVP_MD *ssl_handshake_md(SSL *s)
585 {
586     return ssl_md(ssl_get_algorithm2(s));
587 }
588
589 const EVP_MD *ssl_prf_md(SSL *s)
590 {
591     return ssl_md(ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
592 }
593
594 #define ITEM_SEP(a) \
595         (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
596
597 static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
598                            CIPHER_ORDER **tail)
599 {
600     if (curr == *tail)
601         return;
602     if (curr == *head)
603         *head = curr->next;
604     if (curr->prev != NULL)
605         curr->prev->next = curr->next;
606     if (curr->next != NULL)
607         curr->next->prev = curr->prev;
608     (*tail)->next = curr;
609     curr->prev = *tail;
610     curr->next = NULL;
611     *tail = curr;
612 }
613
614 static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
615                            CIPHER_ORDER **tail)
616 {
617     if (curr == *head)
618         return;
619     if (curr == *tail)
620         *tail = curr->prev;
621     if (curr->next != NULL)
622         curr->next->prev = curr->prev;
623     if (curr->prev != NULL)
624         curr->prev->next = curr->next;
625     (*head)->prev = curr;
626     curr->next = *head;
627     curr->prev = NULL;
628     *head = curr;
629 }
630
631 static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
632                                        int num_of_ciphers,
633                                        uint32_t disabled_mkey,
634                                        uint32_t disabled_auth,
635                                        uint32_t disabled_enc,
636                                        uint32_t disabled_mac,
637                                        CIPHER_ORDER *co_list,
638                                        CIPHER_ORDER **head_p,
639                                        CIPHER_ORDER **tail_p)
640 {
641     int i, co_list_num;
642     const SSL_CIPHER *c;
643
644     /*
645      * We have num_of_ciphers descriptions compiled in, depending on the
646      * method selected (SSLv3, TLSv1 etc).
647      * These will later be sorted in a linked list with at most num
648      * entries.
649      */
650
651     /* Get the initial list of ciphers */
652     co_list_num = 0;            /* actual count of ciphers */
653     for (i = 0; i < num_of_ciphers; i++) {
654         c = ssl_method->get_cipher(i);
655         /* drop those that use any of that is not available */
656         if (c == NULL || !c->valid)
657             continue;
658         if ((c->algorithm_mkey & disabled_mkey) ||
659             (c->algorithm_auth & disabled_auth) ||
660             (c->algorithm_enc & disabled_enc) ||
661             (c->algorithm_mac & disabled_mac))
662             continue;
663         if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) == 0) &&
664             c->min_tls == 0)
665             continue;
666         if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) != 0) &&
667             c->min_dtls == 0)
668             continue;
669
670         co_list[co_list_num].cipher = c;
671         co_list[co_list_num].next = NULL;
672         co_list[co_list_num].prev = NULL;
673         co_list[co_list_num].active = 0;
674         co_list_num++;
675     }
676
677     /*
678      * Prepare linked list from list entries
679      */
680     if (co_list_num > 0) {
681         co_list[0].prev = NULL;
682
683         if (co_list_num > 1) {
684             co_list[0].next = &co_list[1];
685
686             for (i = 1; i < co_list_num - 1; i++) {
687                 co_list[i].prev = &co_list[i - 1];
688                 co_list[i].next = &co_list[i + 1];
689             }
690
691             co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
692         }
693
694         co_list[co_list_num - 1].next = NULL;
695
696         *head_p = &co_list[0];
697         *tail_p = &co_list[co_list_num - 1];
698     }
699 }
700
701 static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
702                                        int num_of_group_aliases,
703                                        uint32_t disabled_mkey,
704                                        uint32_t disabled_auth,
705                                        uint32_t disabled_enc,
706                                        uint32_t disabled_mac,
707                                        CIPHER_ORDER *head)
708 {
709     CIPHER_ORDER *ciph_curr;
710     const SSL_CIPHER **ca_curr;
711     int i;
712     uint32_t mask_mkey = ~disabled_mkey;
713     uint32_t mask_auth = ~disabled_auth;
714     uint32_t mask_enc = ~disabled_enc;
715     uint32_t mask_mac = ~disabled_mac;
716
717     /*
718      * First, add the real ciphers as already collected
719      */
720     ciph_curr = head;
721     ca_curr = ca_list;
722     while (ciph_curr != NULL) {
723         *ca_curr = ciph_curr->cipher;
724         ca_curr++;
725         ciph_curr = ciph_curr->next;
726     }
727
728     /*
729      * Now we add the available ones from the cipher_aliases[] table.
730      * They represent either one or more algorithms, some of which
731      * in any affected category must be supported (set in enabled_mask),
732      * or represent a cipher strength value (will be added in any case because algorithms=0).
733      */
734     for (i = 0; i < num_of_group_aliases; i++) {
735         uint32_t algorithm_mkey = cipher_aliases[i].algorithm_mkey;
736         uint32_t algorithm_auth = cipher_aliases[i].algorithm_auth;
737         uint32_t algorithm_enc = cipher_aliases[i].algorithm_enc;
738         uint32_t algorithm_mac = cipher_aliases[i].algorithm_mac;
739
740         if (algorithm_mkey)
741             if ((algorithm_mkey & mask_mkey) == 0)
742                 continue;
743
744         if (algorithm_auth)
745             if ((algorithm_auth & mask_auth) == 0)
746                 continue;
747
748         if (algorithm_enc)
749             if ((algorithm_enc & mask_enc) == 0)
750                 continue;
751
752         if (algorithm_mac)
753             if ((algorithm_mac & mask_mac) == 0)
754                 continue;
755
756         *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
757         ca_curr++;
758     }
759
760     *ca_curr = NULL;            /* end of list */
761 }
762
763 static void ssl_cipher_apply_rule(uint32_t cipher_id, uint32_t alg_mkey,
764                                   uint32_t alg_auth, uint32_t alg_enc,
765                                   uint32_t alg_mac, int min_tls,
766                                   uint32_t algo_strength, int rule,
767                                   int32_t strength_bits, CIPHER_ORDER **head_p,
768                                   CIPHER_ORDER **tail_p)
769 {
770     CIPHER_ORDER *head, *tail, *curr, *next, *last;
771     const SSL_CIPHER *cp;
772     int reverse = 0;
773
774 #ifdef CIPHER_DEBUG
775     fprintf(stderr,
776             "Applying rule %d with %08x/%08x/%08x/%08x/%08x %08x (%d)\n",
777             rule, alg_mkey, alg_auth, alg_enc, alg_mac, min_tls,
778             algo_strength, strength_bits);
779 #endif
780
781     if (rule == CIPHER_DEL || rule == CIPHER_BUMP)
782         reverse = 1;            /* needed to maintain sorting between currently
783                                  * deleted ciphers */
784
785     head = *head_p;
786     tail = *tail_p;
787
788     if (reverse) {
789         next = tail;
790         last = head;
791     } else {
792         next = head;
793         last = tail;
794     }
795
796     curr = NULL;
797     for (;;) {
798         if (curr == last)
799             break;
800
801         curr = next;
802
803         if (curr == NULL)
804             break;
805
806         next = reverse ? curr->prev : curr->next;
807
808         cp = curr->cipher;
809
810         /*
811          * Selection criteria is either the value of strength_bits
812          * or the algorithms used.
813          */
814         if (strength_bits >= 0) {
815             if (strength_bits != cp->strength_bits)
816                 continue;
817         } else {
818 #ifdef CIPHER_DEBUG
819             fprintf(stderr,
820                     "\nName: %s:\nAlgo = %08x/%08x/%08x/%08x/%08x Algo_strength = %08x\n",
821                     cp->name, cp->algorithm_mkey, cp->algorithm_auth,
822                     cp->algorithm_enc, cp->algorithm_mac, cp->min_tls,
823                     cp->algo_strength);
824 #endif
825             if (cipher_id != 0 && (cipher_id != cp->id))
826                 continue;
827             if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
828                 continue;
829             if (alg_auth && !(alg_auth & cp->algorithm_auth))
830                 continue;
831             if (alg_enc && !(alg_enc & cp->algorithm_enc))
832                 continue;
833             if (alg_mac && !(alg_mac & cp->algorithm_mac))
834                 continue;
835             if (min_tls && (min_tls != cp->min_tls))
836                 continue;
837             if ((algo_strength & SSL_STRONG_MASK)
838                 && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
839                 continue;
840             if ((algo_strength & SSL_DEFAULT_MASK)
841                 && !(algo_strength & SSL_DEFAULT_MASK & cp->algo_strength))
842                 continue;
843         }
844
845 #ifdef CIPHER_DEBUG
846         fprintf(stderr, "Action = %d\n", rule);
847 #endif
848
849         /* add the cipher if it has not been added yet. */
850         if (rule == CIPHER_ADD) {
851             /* reverse == 0 */
852             if (!curr->active) {
853                 ll_append_tail(&head, curr, &tail);
854                 curr->active = 1;
855             }
856         }
857         /* Move the added cipher to this location */
858         else if (rule == CIPHER_ORD) {
859             /* reverse == 0 */
860             if (curr->active) {
861                 ll_append_tail(&head, curr, &tail);
862             }
863         } else if (rule == CIPHER_DEL) {
864             /* reverse == 1 */
865             if (curr->active) {
866                 /*
867                  * most recently deleted ciphersuites get best positions for
868                  * any future CIPHER_ADD (note that the CIPHER_DEL loop works
869                  * in reverse to maintain the order)
870                  */
871                 ll_append_head(&head, curr, &tail);
872                 curr->active = 0;
873             }
874         } else if (rule == CIPHER_BUMP) {
875             if (curr->active)
876                 ll_append_head(&head, curr, &tail);
877         } else if (rule == CIPHER_KILL) {
878             /* reverse == 0 */
879             if (head == curr)
880                 head = curr->next;
881             else
882                 curr->prev->next = curr->next;
883             if (tail == curr)
884                 tail = curr->prev;
885             curr->active = 0;
886             if (curr->next != NULL)
887                 curr->next->prev = curr->prev;
888             if (curr->prev != NULL)
889                 curr->prev->next = curr->next;
890             curr->next = NULL;
891             curr->prev = NULL;
892         }
893     }
894
895     *head_p = head;
896     *tail_p = tail;
897 }
898
899 static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
900                                     CIPHER_ORDER **tail_p)
901 {
902     int32_t max_strength_bits;
903     int i, *number_uses;
904     CIPHER_ORDER *curr;
905
906     /*
907      * This routine sorts the ciphers with descending strength. The sorting
908      * must keep the pre-sorted sequence, so we apply the normal sorting
909      * routine as '+' movement to the end of the list.
910      */
911     max_strength_bits = 0;
912     curr = *head_p;
913     while (curr != NULL) {
914         if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
915             max_strength_bits = curr->cipher->strength_bits;
916         curr = curr->next;
917     }
918
919     number_uses = OPENSSL_zalloc(sizeof(int) * (max_strength_bits + 1));
920     if (number_uses == NULL) {
921         SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
922         return 0;
923     }
924
925     /*
926      * Now find the strength_bits values actually used
927      */
928     curr = *head_p;
929     while (curr != NULL) {
930         if (curr->active)
931             number_uses[curr->cipher->strength_bits]++;
932         curr = curr->next;
933     }
934     /*
935      * Go through the list of used strength_bits values in descending
936      * order.
937      */
938     for (i = max_strength_bits; i >= 0; i--)
939         if (number_uses[i] > 0)
940             ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p,
941                                   tail_p);
942
943     OPENSSL_free(number_uses);
944     return 1;
945 }
946
947 static int ssl_cipher_process_rulestr(const char *rule_str,
948                                       CIPHER_ORDER **head_p,
949                                       CIPHER_ORDER **tail_p,
950                                       const SSL_CIPHER **ca_list, CERT *c)
951 {
952     uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, algo_strength;
953     int min_tls;
954     const char *l, *buf;
955     int j, multi, found, rule, retval, ok, buflen;
956     uint32_t cipher_id = 0;
957     char ch;
958
959     retval = 1;
960     l = rule_str;
961     for ( ; ; ) {
962         ch = *l;
963
964         if (ch == '\0')
965             break;              /* done */
966         if (ch == '-') {
967             rule = CIPHER_DEL;
968             l++;
969         } else if (ch == '+') {
970             rule = CIPHER_ORD;
971             l++;
972         } else if (ch == '!') {
973             rule = CIPHER_KILL;
974             l++;
975         } else if (ch == '@') {
976             rule = CIPHER_SPECIAL;
977             l++;
978         } else {
979             rule = CIPHER_ADD;
980         }
981
982         if (ITEM_SEP(ch)) {
983             l++;
984             continue;
985         }
986
987         alg_mkey = 0;
988         alg_auth = 0;
989         alg_enc = 0;
990         alg_mac = 0;
991         min_tls = 0;
992         algo_strength = 0;
993
994         for (;;) {
995             ch = *l;
996             buf = l;
997             buflen = 0;
998 #ifndef CHARSET_EBCDIC
999             while (((ch >= 'A') && (ch <= 'Z')) ||
1000                    ((ch >= '0') && (ch <= '9')) ||
1001                    ((ch >= 'a') && (ch <= 'z')) ||
1002                    (ch == '-') || (ch == '.') || (ch == '='))
1003 #else
1004             while (isalnum(ch) || (ch == '-') || (ch == '.') || (ch == '='))
1005 #endif
1006             {
1007                 ch = *(++l);
1008                 buflen++;
1009             }
1010
1011             if (buflen == 0) {
1012                 /*
1013                  * We hit something we cannot deal with,
1014                  * it is no command or separator nor
1015                  * alphanumeric, so we call this an error.
1016                  */
1017                 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, SSL_R_INVALID_COMMAND);
1018                 retval = found = 0;
1019                 l++;
1020                 break;
1021             }
1022
1023             if (rule == CIPHER_SPECIAL) {
1024                 found = 0;      /* unused -- avoid compiler warning */
1025                 break;          /* special treatment */
1026             }
1027
1028             /* check for multi-part specification */
1029             if (ch == '+') {
1030                 multi = 1;
1031                 l++;
1032             } else {
1033                 multi = 0;
1034             }
1035
1036             /*
1037              * Now search for the cipher alias in the ca_list. Be careful
1038              * with the strncmp, because the "buflen" limitation
1039              * will make the rule "ADH:SOME" and the cipher
1040              * "ADH-MY-CIPHER" look like a match for buflen=3.
1041              * So additionally check whether the cipher name found
1042              * has the correct length. We can save a strlen() call:
1043              * just checking for the '\0' at the right place is
1044              * sufficient, we have to strncmp() anyway. (We cannot
1045              * use strcmp(), because buf is not '\0' terminated.)
1046              */
1047             j = found = 0;
1048             cipher_id = 0;
1049             while (ca_list[j]) {
1050                 if (strncmp(buf, ca_list[j]->name, buflen) == 0
1051                     && (ca_list[j]->name[buflen] == '\0')) {
1052                     found = 1;
1053                     break;
1054                 } else
1055                     j++;
1056             }
1057
1058             if (!found)
1059                 break;          /* ignore this entry */
1060
1061             if (ca_list[j]->algorithm_mkey) {
1062                 if (alg_mkey) {
1063                     alg_mkey &= ca_list[j]->algorithm_mkey;
1064                     if (!alg_mkey) {
1065                         found = 0;
1066                         break;
1067                     }
1068                 } else {
1069                     alg_mkey = ca_list[j]->algorithm_mkey;
1070                 }
1071             }
1072
1073             if (ca_list[j]->algorithm_auth) {
1074                 if (alg_auth) {
1075                     alg_auth &= ca_list[j]->algorithm_auth;
1076                     if (!alg_auth) {
1077                         found = 0;
1078                         break;
1079                     }
1080                 } else {
1081                     alg_auth = ca_list[j]->algorithm_auth;
1082                 }
1083             }
1084
1085             if (ca_list[j]->algorithm_enc) {
1086                 if (alg_enc) {
1087                     alg_enc &= ca_list[j]->algorithm_enc;
1088                     if (!alg_enc) {
1089                         found = 0;
1090                         break;
1091                     }
1092                 } else {
1093                     alg_enc = ca_list[j]->algorithm_enc;
1094                 }
1095             }
1096
1097             if (ca_list[j]->algorithm_mac) {
1098                 if (alg_mac) {
1099                     alg_mac &= ca_list[j]->algorithm_mac;
1100                     if (!alg_mac) {
1101                         found = 0;
1102                         break;
1103                     }
1104                 } else {
1105                     alg_mac = ca_list[j]->algorithm_mac;
1106                 }
1107             }
1108
1109             if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1110                 if (algo_strength & SSL_STRONG_MASK) {
1111                     algo_strength &=
1112                         (ca_list[j]->algo_strength & SSL_STRONG_MASK) |
1113                         ~SSL_STRONG_MASK;
1114                     if (!(algo_strength & SSL_STRONG_MASK)) {
1115                         found = 0;
1116                         break;
1117                     }
1118                 } else {
1119                     algo_strength = ca_list[j]->algo_strength & SSL_STRONG_MASK;
1120                 }
1121             }
1122
1123             if (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) {
1124                 if (algo_strength & SSL_DEFAULT_MASK) {
1125                     algo_strength &=
1126                         (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) |
1127                         ~SSL_DEFAULT_MASK;
1128                     if (!(algo_strength & SSL_DEFAULT_MASK)) {
1129                         found = 0;
1130                         break;
1131                     }
1132                 } else {
1133                     algo_strength |=
1134                         ca_list[j]->algo_strength & SSL_DEFAULT_MASK;
1135                 }
1136             }
1137
1138             if (ca_list[j]->valid) {
1139                 /*
1140                  * explicit ciphersuite found; its protocol version does not
1141                  * become part of the search pattern!
1142                  */
1143
1144                 cipher_id = ca_list[j]->id;
1145             } else {
1146                 /*
1147                  * not an explicit ciphersuite; only in this case, the
1148                  * protocol version is considered part of the search pattern
1149                  */
1150
1151                 if (ca_list[j]->min_tls) {
1152                     if (min_tls != 0 && min_tls != ca_list[j]->min_tls) {
1153                         found = 0;
1154                         break;
1155                     } else {
1156                         min_tls = ca_list[j]->min_tls;
1157                     }
1158                 }
1159             }
1160
1161             if (!multi)
1162                 break;
1163         }
1164
1165         /*
1166          * Ok, we have the rule, now apply it
1167          */
1168         if (rule == CIPHER_SPECIAL) { /* special command */
1169             ok = 0;
1170             if ((buflen == 8) && strncmp(buf, "STRENGTH", 8) == 0) {
1171                 ok = ssl_cipher_strength_sort(head_p, tail_p);
1172             } else if (buflen == 10 && strncmp(buf, "SECLEVEL=", 9) == 0) {
1173                 int level = buf[9] - '0';
1174                 if (level < 0 || level > 5) {
1175                     SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
1176                            SSL_R_INVALID_COMMAND);
1177                 } else {
1178                     c->sec_level = level;
1179                     ok = 1;
1180                 }
1181             } else {
1182                 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, SSL_R_INVALID_COMMAND);
1183             }
1184             if (ok == 0)
1185                 retval = 0;
1186             /*
1187              * We do not support any "multi" options
1188              * together with "@", so throw away the
1189              * rest of the command, if any left, until
1190              * end or ':' is found.
1191              */
1192             while ((*l != '\0') && !ITEM_SEP(*l))
1193                 l++;
1194         } else if (found) {
1195             ssl_cipher_apply_rule(cipher_id,
1196                                   alg_mkey, alg_auth, alg_enc, alg_mac,
1197                                   min_tls, algo_strength, rule, -1, head_p,
1198                                   tail_p);
1199         } else {
1200             while ((*l != '\0') && !ITEM_SEP(*l))
1201                 l++;
1202         }
1203         if (*l == '\0')
1204             break;              /* done */
1205     }
1206
1207     return retval;
1208 }
1209
1210 #ifndef OPENSSL_NO_EC
1211 static int check_suiteb_cipher_list(const SSL_METHOD *meth, CERT *c,
1212                                     const char **prule_str)
1213 {
1214     unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
1215     if (strncmp(*prule_str, "SUITEB128ONLY", 13) == 0) {
1216         suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
1217     } else if (strncmp(*prule_str, "SUITEB128C2", 11) == 0) {
1218         suiteb_comb2 = 1;
1219         suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1220     } else if (strncmp(*prule_str, "SUITEB128", 9) == 0) {
1221         suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1222     } else if (strncmp(*prule_str, "SUITEB192", 9) == 0) {
1223         suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
1224     }
1225
1226     if (suiteb_flags) {
1227         c->cert_flags &= ~SSL_CERT_FLAG_SUITEB_128_LOS;
1228         c->cert_flags |= suiteb_flags;
1229     } else {
1230         suiteb_flags = c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS;
1231     }
1232
1233     if (!suiteb_flags)
1234         return 1;
1235     /* Check version: if TLS 1.2 ciphers allowed we can use Suite B */
1236
1237     if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)) {
1238         SSLerr(SSL_F_CHECK_SUITEB_CIPHER_LIST,
1239                SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE);
1240         return 0;
1241     }
1242 # ifndef OPENSSL_NO_EC
1243     switch (suiteb_flags) {
1244     case SSL_CERT_FLAG_SUITEB_128_LOS:
1245         if (suiteb_comb2)
1246             *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1247         else
1248             *prule_str =
1249                 "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384";
1250         break;
1251     case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1252         *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256";
1253         break;
1254     case SSL_CERT_FLAG_SUITEB_192_LOS:
1255         *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1256         break;
1257     }
1258     return 1;
1259 # else
1260     SSLerr(SSL_F_CHECK_SUITEB_CIPHER_LIST, SSL_R_ECDH_REQUIRED_FOR_SUITEB_MODE);
1261     return 0;
1262 # endif
1263 }
1264 #endif
1265
1266 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, STACK_OF(SSL_CIPHER)
1267                                              **cipher_list, STACK_OF(SSL_CIPHER)
1268                                              **cipher_list_by_id,
1269                                              const char *rule_str, CERT *c)
1270 {
1271     int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1272     uint32_t disabled_mkey, disabled_auth, disabled_enc, disabled_mac;
1273     STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
1274     const char *rule_p;
1275     CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1276     const SSL_CIPHER **ca_list = NULL;
1277
1278     /*
1279      * Return with error if nothing to do.
1280      */
1281     if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
1282         return NULL;
1283 #ifndef OPENSSL_NO_EC
1284     if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
1285         return NULL;
1286 #endif
1287
1288     /*
1289      * To reduce the work to do we only want to process the compiled
1290      * in algorithms, so we first get the mask of disabled ciphers.
1291      */
1292
1293     disabled_mkey = disabled_mkey_mask;
1294     disabled_auth = disabled_auth_mask;
1295     disabled_enc = disabled_enc_mask;
1296     disabled_mac = disabled_mac_mask;
1297
1298     /*
1299      * Now we have to collect the available ciphers from the compiled
1300      * in ciphers. We cannot get more than the number compiled in, so
1301      * it is used for allocation.
1302      */
1303     num_of_ciphers = ssl_method->num_ciphers();
1304
1305     co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
1306     if (co_list == NULL) {
1307         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1308         return NULL;          /* Failure */
1309     }
1310
1311     ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1312                                disabled_mkey, disabled_auth, disabled_enc,
1313                                disabled_mac, co_list, &head, &tail);
1314
1315     /* Now arrange all ciphers by preference. */
1316
1317     /*
1318      * Everything else being equal, prefer ephemeral ECDH over other key
1319      * exchange mechanisms.
1320      * For consistency, prefer ECDSA over RSA (though this only matters if the
1321      * server has both certificates, and is using the DEFAULT, or a client
1322      * preference).
1323      */
1324     ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD,
1325                           -1, &head, &tail);
1326     ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head,
1327                           &tail);
1328     ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
1329                           &tail);
1330
1331     /* Within each strength group, we prefer GCM over CHACHA... */
1332     ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
1333                           &head, &tail);
1334     ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
1335                           &head, &tail);
1336
1337     /*
1338      * ...and generally, our preferred cipher is AES.
1339      * Note that AEADs will be bumped to take preference after sorting by
1340      * strength.
1341      */
1342     ssl_cipher_apply_rule(0, 0, 0, SSL_AES ^ SSL_AESGCM, 0, 0, 0, CIPHER_ADD,
1343                           -1, &head, &tail);
1344
1345     /* Temporarily enable everything else for sorting */
1346     ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1347
1348     /* Low priority for MD5 */
1349     ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head,
1350                           &tail);
1351
1352     /*
1353      * Move anonymous ciphers to the end.  Usually, these will remain
1354      * disabled. (For applications that allow them, they aren't too bad, but
1355      * we prefer authenticated ciphers.)
1356      */
1357     ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1358                           &tail);
1359
1360     /*
1361      * ssl_cipher_apply_rule(0, 0, SSL_aDH, 0, 0, 0, 0, CIPHER_ORD, -1,
1362      * &head, &tail);
1363      */
1364     ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1365                           &tail);
1366     ssl_cipher_apply_rule(0, SSL_kPSK, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1367                           &tail);
1368
1369     /* RC4 is sort-of broken -- move to the end */
1370     ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head,
1371                           &tail);
1372
1373     /*
1374      * Now sort by symmetric encryption strength.  The above ordering remains
1375      * in force within each class
1376      */
1377     if (!ssl_cipher_strength_sort(&head, &tail)) {
1378         OPENSSL_free(co_list);
1379         return NULL;
1380     }
1381
1382     /*
1383      * Partially overrule strength sort to prefer TLS 1.2 ciphers/PRFs.
1384      * TODO(openssl-team): is there an easier way to accomplish all this?
1385      */
1386     ssl_cipher_apply_rule(0, 0, 0, 0, 0, TLS1_2_VERSION, 0, CIPHER_BUMP, -1,
1387                           &head, &tail);
1388
1389     /*
1390      * Irrespective of strength, enforce the following order:
1391      * (EC)DHE + AEAD > (EC)DHE > rest of AEAD > rest.
1392      * Within each group, ciphers remain sorted by strength and previous
1393      * preference, i.e.,
1394      * 1) ECDHE > DHE
1395      * 2) GCM > CHACHA
1396      * 3) AES > rest
1397      * 4) TLS 1.2 > legacy
1398      *
1399      * Because we now bump ciphers to the top of the list, we proceed in
1400      * reverse order of preference.
1401      */
1402     ssl_cipher_apply_rule(0, 0, 0, 0, SSL_AEAD, 0, 0, CIPHER_BUMP, -1,
1403                           &head, &tail);
1404     ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, 0, 0, 0,
1405                           CIPHER_BUMP, -1, &head, &tail);
1406     ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, SSL_AEAD, 0, 0,
1407                           CIPHER_BUMP, -1, &head, &tail);
1408
1409     /* Now disable everything (maintaining the ordering!) */
1410     ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1411
1412     /*
1413      * We also need cipher aliases for selecting based on the rule_str.
1414      * There might be two types of entries in the rule_str: 1) names
1415      * of ciphers themselves 2) aliases for groups of ciphers.
1416      * For 1) we need the available ciphers and for 2) the cipher
1417      * groups of cipher_aliases added together in one list (otherwise
1418      * we would be happy with just the cipher_aliases table).
1419      */
1420     num_of_group_aliases = OSSL_NELEM(cipher_aliases);
1421     num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1422     ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
1423     if (ca_list == NULL) {
1424         OPENSSL_free(co_list);
1425         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1426         return NULL;          /* Failure */
1427     }
1428     ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1429                                disabled_mkey, disabled_auth, disabled_enc,
1430                                disabled_mac, head);
1431
1432     /*
1433      * If the rule_string begins with DEFAULT, apply the default rule
1434      * before using the (possibly available) additional rules.
1435      */
1436     ok = 1;
1437     rule_p = rule_str;
1438     if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1439         ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1440                                         &head, &tail, ca_list, c);
1441         rule_p += 7;
1442         if (*rule_p == ':')
1443             rule_p++;
1444     }
1445
1446     if (ok && (strlen(rule_p) > 0))
1447         ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, c);
1448
1449     OPENSSL_free(ca_list);      /* Not needed anymore */
1450
1451     if (!ok) {                  /* Rule processing failure */
1452         OPENSSL_free(co_list);
1453         return NULL;
1454     }
1455
1456     /*
1457      * Allocate new "cipherstack" for the result, return with error
1458      * if we cannot get one.
1459      */
1460     if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1461         OPENSSL_free(co_list);
1462         return NULL;
1463     }
1464
1465     /*
1466      * The cipher selection for the list is done. The ciphers are added
1467      * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1468      */
1469     for (curr = head; curr != NULL; curr = curr->next) {
1470         if (curr->active) {
1471             if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1472                 OPENSSL_free(co_list);
1473                 sk_SSL_CIPHER_free(cipherstack);
1474                 return NULL;
1475             }
1476 #ifdef CIPHER_DEBUG
1477             fprintf(stderr, "<%s>\n", curr->cipher->name);
1478 #endif
1479         }
1480     }
1481     OPENSSL_free(co_list);      /* Not needed any longer */
1482
1483     tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1484     if (tmp_cipher_list == NULL) {
1485         sk_SSL_CIPHER_free(cipherstack);
1486         return NULL;
1487     }
1488     sk_SSL_CIPHER_free(*cipher_list);
1489     *cipher_list = cipherstack;
1490     if (*cipher_list_by_id != NULL)
1491         sk_SSL_CIPHER_free(*cipher_list_by_id);
1492     *cipher_list_by_id = tmp_cipher_list;
1493     (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
1494
1495     sk_SSL_CIPHER_sort(*cipher_list_by_id);
1496     return cipherstack;
1497 }
1498
1499 char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1500 {
1501     const char *ver;
1502     const char *kx, *au, *enc, *mac;
1503     uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1504     static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
1505
1506     if (buf == NULL) {
1507         len = 128;
1508         buf = OPENSSL_malloc(len);
1509         if (buf == NULL)
1510             return NULL;
1511     } else if (len < 128) {
1512         return NULL;
1513     }
1514
1515     alg_mkey = cipher->algorithm_mkey;
1516     alg_auth = cipher->algorithm_auth;
1517     alg_enc = cipher->algorithm_enc;
1518     alg_mac = cipher->algorithm_mac;
1519
1520     ver = ssl_protocol_to_string(cipher->min_tls);
1521
1522     switch (alg_mkey) {
1523     case SSL_kRSA:
1524         kx = "RSA";
1525         break;
1526     case SSL_kDHE:
1527         kx = "DH";
1528         break;
1529     case SSL_kECDHE:
1530         kx = "ECDH";
1531         break;
1532     case SSL_kPSK:
1533         kx = "PSK";
1534         break;
1535     case SSL_kRSAPSK:
1536         kx = "RSAPSK";
1537         break;
1538     case SSL_kECDHEPSK:
1539         kx = "ECDHEPSK";
1540         break;
1541     case SSL_kDHEPSK:
1542         kx = "DHEPSK";
1543         break;
1544     case SSL_kSRP:
1545         kx = "SRP";
1546         break;
1547     case SSL_kGOST:
1548         kx = "GOST";
1549         break;
1550     case SSL_kANY:
1551         kx = "any";
1552         break;
1553     default:
1554         kx = "unknown";
1555     }
1556
1557     switch (alg_auth) {
1558     case SSL_aRSA:
1559         au = "RSA";
1560         break;
1561     case SSL_aDSS:
1562         au = "DSS";
1563         break;
1564     case SSL_aNULL:
1565         au = "None";
1566         break;
1567     case SSL_aECDSA:
1568         au = "ECDSA";
1569         break;
1570     case SSL_aPSK:
1571         au = "PSK";
1572         break;
1573     case SSL_aSRP:
1574         au = "SRP";
1575         break;
1576     case SSL_aGOST01:
1577         au = "GOST01";
1578         break;
1579     /* New GOST ciphersuites have both SSL_aGOST12 and SSL_aGOST01 bits */
1580     case (SSL_aGOST12 | SSL_aGOST01):
1581         au = "GOST12";
1582         break;
1583     case SSL_aANY:
1584         au = "any";
1585         break;
1586     default:
1587         au = "unknown";
1588         break;
1589     }
1590
1591     switch (alg_enc) {
1592     case SSL_DES:
1593         enc = "DES(56)";
1594         break;
1595     case SSL_3DES:
1596         enc = "3DES(168)";
1597         break;
1598     case SSL_RC4:
1599         enc = "RC4(128)";
1600         break;
1601     case SSL_RC2:
1602         enc = "RC2(128)";
1603         break;
1604     case SSL_IDEA:
1605         enc = "IDEA(128)";
1606         break;
1607     case SSL_eNULL:
1608         enc = "None";
1609         break;
1610     case SSL_AES128:
1611         enc = "AES(128)";
1612         break;
1613     case SSL_AES256:
1614         enc = "AES(256)";
1615         break;
1616     case SSL_AES128GCM:
1617         enc = "AESGCM(128)";
1618         break;
1619     case SSL_AES256GCM:
1620         enc = "AESGCM(256)";
1621         break;
1622     case SSL_AES128CCM:
1623         enc = "AESCCM(128)";
1624         break;
1625     case SSL_AES256CCM:
1626         enc = "AESCCM(256)";
1627         break;
1628     case SSL_AES128CCM8:
1629         enc = "AESCCM8(128)";
1630         break;
1631     case SSL_AES256CCM8:
1632         enc = "AESCCM8(256)";
1633         break;
1634     case SSL_CAMELLIA128:
1635         enc = "Camellia(128)";
1636         break;
1637     case SSL_CAMELLIA256:
1638         enc = "Camellia(256)";
1639         break;
1640     case SSL_SEED:
1641         enc = "SEED(128)";
1642         break;
1643     case SSL_eGOST2814789CNT:
1644     case SSL_eGOST2814789CNT12:
1645         enc = "GOST89(256)";
1646         break;
1647     case SSL_CHACHA20POLY1305:
1648         enc = "CHACHA20/POLY1305(256)";
1649         break;
1650     default:
1651         enc = "unknown";
1652         break;
1653     }
1654
1655     switch (alg_mac) {
1656     case SSL_MD5:
1657         mac = "MD5";
1658         break;
1659     case SSL_SHA1:
1660         mac = "SHA1";
1661         break;
1662     case SSL_SHA256:
1663         mac = "SHA256";
1664         break;
1665     case SSL_SHA384:
1666         mac = "SHA384";
1667         break;
1668     case SSL_AEAD:
1669         mac = "AEAD";
1670         break;
1671     case SSL_GOST89MAC:
1672     case SSL_GOST89MAC12:
1673         mac = "GOST89";
1674         break;
1675     case SSL_GOST94:
1676         mac = "GOST94";
1677         break;
1678     case SSL_GOST12_256:
1679     case SSL_GOST12_512:
1680         mac = "GOST2012";
1681         break;
1682     default:
1683         mac = "unknown";
1684         break;
1685     }
1686
1687     BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1688
1689     return buf;
1690 }
1691
1692 const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
1693 {
1694     if (c == NULL)
1695         return "(NONE)";
1696
1697     /*
1698      * Backwards-compatibility crutch.  In almost all contexts we report TLS
1699      * 1.0 as "TLSv1", but for ciphers we report "TLSv1.0".
1700      */
1701     if (c->min_tls == TLS1_VERSION)
1702         return "TLSv1.0";
1703     return ssl_protocol_to_string(c->min_tls);
1704 }
1705
1706 /* return the actual cipher being used */
1707 const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1708 {
1709     if (c != NULL)
1710         return c->name;
1711     return "(NONE)";
1712 }
1713
1714 /* return the actual cipher being used in RFC standard name */
1715 const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1716 {
1717     if (c != NULL)
1718         return c->stdname;
1719     return "(NONE)";
1720 }
1721
1722 /* return the OpenSSL name based on given RFC standard name */
1723 const char *OPENSSL_cipher_name(const char *stdname)
1724 {
1725     const SSL_CIPHER *c;
1726
1727     if (stdname == NULL)
1728         return "(NONE)";
1729     c = ssl3_get_cipher_by_std_name(stdname);
1730     return SSL_CIPHER_get_name(c);
1731 }
1732
1733 /* number of bits for symmetric cipher */
1734 int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1735 {
1736     int ret = 0;
1737
1738     if (c != NULL) {
1739         if (alg_bits != NULL)
1740             *alg_bits = (int)c->alg_bits;
1741         ret = (int)c->strength_bits;
1742     }
1743     return ret;
1744 }
1745
1746 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c)
1747 {
1748     return c->id;
1749 }
1750
1751 SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
1752 {
1753     SSL_COMP *ctmp;
1754     int i, nn;
1755
1756     if ((n == 0) || (sk == NULL))
1757         return (NULL);
1758     nn = sk_SSL_COMP_num(sk);
1759     for (i = 0; i < nn; i++) {
1760         ctmp = sk_SSL_COMP_value(sk, i);
1761         if (ctmp->id == n)
1762             return ctmp;
1763     }
1764     return NULL;
1765 }
1766
1767 #ifdef OPENSSL_NO_COMP
1768 STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1769 {
1770     return NULL;
1771 }
1772
1773 STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
1774                                                       *meths)
1775 {
1776     return meths;
1777 }
1778
1779 int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
1780 {
1781     return 1;
1782 }
1783
1784 #else
1785 STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1786 {
1787     load_builtin_compressions();
1788     return ssl_comp_methods;
1789 }
1790
1791 STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
1792                                                       *meths)
1793 {
1794     STACK_OF(SSL_COMP) *old_meths = ssl_comp_methods;
1795     ssl_comp_methods = meths;
1796     return old_meths;
1797 }
1798
1799 static void cmeth_free(SSL_COMP *cm)
1800 {
1801     OPENSSL_free(cm);
1802 }
1803
1804 void ssl_comp_free_compression_methods_int(void)
1805 {
1806     STACK_OF(SSL_COMP) *old_meths = ssl_comp_methods;
1807     ssl_comp_methods = NULL;
1808     sk_SSL_COMP_pop_free(old_meths, cmeth_free);
1809 }
1810
1811 int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
1812 {
1813     SSL_COMP *comp;
1814
1815     if (cm == NULL || COMP_get_type(cm) == NID_undef)
1816         return 1;
1817
1818     /*-
1819      * According to draft-ietf-tls-compression-04.txt, the
1820      * compression number ranges should be the following:
1821      *
1822      *   0 to  63:  methods defined by the IETF
1823      *  64 to 192:  external party methods assigned by IANA
1824      * 193 to 255:  reserved for private use
1825      */
1826     if (id < 193 || id > 255) {
1827         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
1828                SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
1829         return 1;
1830     }
1831
1832     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
1833     comp = OPENSSL_malloc(sizeof(*comp));
1834     if (comp == NULL) {
1835         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
1836         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
1837         return 1;
1838     }
1839
1840     comp->id = id;
1841     comp->method = cm;
1842     load_builtin_compressions();
1843     if (ssl_comp_methods && sk_SSL_COMP_find(ssl_comp_methods, comp) >= 0) {
1844         OPENSSL_free(comp);
1845         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
1846         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
1847                SSL_R_DUPLICATE_COMPRESSION_ID);
1848         return 1;
1849     }
1850     if (ssl_comp_methods == NULL || !sk_SSL_COMP_push(ssl_comp_methods, comp)) {
1851         OPENSSL_free(comp);
1852         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
1853         SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
1854         return 1;
1855     }
1856     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
1857     return 0;
1858 }
1859 #endif
1860
1861 const char *SSL_COMP_get_name(const COMP_METHOD *comp)
1862 {
1863 #ifndef OPENSSL_NO_COMP
1864     return comp ? COMP_get_name(comp) : NULL;
1865 #else
1866     return NULL;
1867 #endif
1868 }
1869
1870 const char *SSL_COMP_get0_name(const SSL_COMP *comp)
1871 {
1872 #ifndef OPENSSL_NO_COMP
1873     return comp->name;
1874 #else
1875     return NULL;
1876 #endif
1877 }
1878
1879 int SSL_COMP_get_id(const SSL_COMP *comp)
1880 {
1881 #ifndef OPENSSL_NO_COMP
1882     return comp->id;
1883 #else
1884     return -1;
1885 #endif
1886 }
1887
1888 const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr,
1889                                          int all)
1890 {
1891     const SSL_CIPHER *c = ssl->method->get_cipher_by_char(ptr);
1892
1893     if (c == NULL || (!all && c->valid == 0))
1894         return NULL;
1895     return c;
1896 }
1897
1898 const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
1899 {
1900     return ssl->method->get_cipher_by_char(ptr);
1901 }
1902
1903 int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
1904 {
1905     int i;
1906     if (c == NULL)
1907         return NID_undef;
1908     i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
1909     if (i == -1)
1910         return NID_undef;
1911     return ssl_cipher_table_cipher[i].nid;
1912 }
1913
1914 int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
1915 {
1916     int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
1917
1918     if (i == -1)
1919         return NID_undef;
1920     return ssl_cipher_table_mac[i].nid;
1921 }
1922
1923 int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
1924 {
1925     int i = ssl_cipher_info_lookup(ssl_cipher_table_kx, c->algorithm_mkey);
1926
1927     if (i == -1)
1928         return NID_undef;
1929     return ssl_cipher_table_kx[i].nid;
1930 }
1931
1932 int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
1933 {
1934     int i = ssl_cipher_info_lookup(ssl_cipher_table_auth, c->algorithm_auth);
1935
1936     if (i == -1)
1937         return NID_undef;
1938     return ssl_cipher_table_auth[i].nid;
1939 }
1940
1941 const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c)
1942 {
1943     int idx = c->algorithm2 & SSL_HANDSHAKE_MAC_MASK;
1944
1945     if (idx < 0 || idx >= SSL_MD_NUM_IDX)
1946         return NULL;
1947     return ssl_digest_methods[idx];
1948 }
1949
1950 int SSL_CIPHER_is_aead(const SSL_CIPHER *c)
1951 {
1952     return (c->algorithm_mac & SSL_AEAD) ? 1 : 0;
1953 }
1954
1955 int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead,
1956                             size_t *int_overhead, size_t *blocksize,
1957                             size_t *ext_overhead)
1958 {
1959     size_t mac = 0, in = 0, blk = 0, out = 0;
1960
1961     /* Some hard-coded numbers for the CCM/Poly1305 MAC overhead
1962      * because there are no handy #defines for those. */
1963     if (c->algorithm_enc & SSL_AESGCM) {
1964         out = EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1965     } else if (c->algorithm_enc & (SSL_AES128CCM | SSL_AES256CCM)) {
1966         out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 16;
1967     } else if (c->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8)) {
1968         out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 8;
1969     } else if (c->algorithm_enc & SSL_CHACHA20POLY1305) {
1970         out = 16;
1971     } else if (c->algorithm_mac & SSL_AEAD) {
1972         /* We're supposed to have handled all the AEAD modes above */
1973         return 0;
1974     } else {
1975         /* Non-AEAD modes. Calculate MAC/cipher overhead separately */
1976         int digest_nid = SSL_CIPHER_get_digest_nid(c);
1977         const EVP_MD *e_md = EVP_get_digestbynid(digest_nid);
1978
1979         if (e_md == NULL)
1980             return 0;
1981
1982         mac = EVP_MD_size(e_md);
1983         if (c->algorithm_enc != SSL_eNULL) {
1984             int cipher_nid = SSL_CIPHER_get_cipher_nid(c);
1985             const EVP_CIPHER *e_ciph = EVP_get_cipherbynid(cipher_nid);
1986
1987             /* If it wasn't AEAD or SSL_eNULL, we expect it to be a
1988                known CBC cipher. */
1989             if (e_ciph == NULL ||
1990                 EVP_CIPHER_mode(e_ciph) != EVP_CIPH_CBC_MODE)
1991                 return 0;
1992
1993             in = 1; /* padding length byte */
1994             out = EVP_CIPHER_iv_length(e_ciph);
1995             blk = EVP_CIPHER_block_size(e_ciph);
1996         }
1997     }
1998
1999     *mac_overhead = mac;
2000     *int_overhead = in;
2001     *blocksize = blk;
2002     *ext_overhead = out;
2003
2004     return 1;
2005 }
2006
2007 int ssl_cert_is_disabled(size_t idx)
2008 {
2009     const SSL_CERT_LOOKUP *cl = ssl_cert_lookup_by_idx(idx);
2010
2011     if (cl == NULL || (cl->amask & disabled_auth_mask) != 0)
2012         return 1;
2013     return 0;
2014 }