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