Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/ssl/ssl_ciph.c

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