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