/src/boringssl/ssl/ssl_cipher.cc
Line | Count | Source |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
3 | | // Copyright 2005 Nokia. All rights reserved. |
4 | | // |
5 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | // you may not use this file except in compliance with the License. |
7 | | // You may obtain a copy of the License at |
8 | | // |
9 | | // https://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software |
12 | | // distributed under the License is distributed on an "AS IS" BASIS, |
13 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | // See the License for the specific language governing permissions and |
15 | | // limitations under the License. |
16 | | |
17 | | #include <openssl/ssl.h> |
18 | | |
19 | | #include <assert.h> |
20 | | #include <string.h> |
21 | | |
22 | | #include <iterator> |
23 | | #include <optional> |
24 | | |
25 | | #include <openssl/aead.h> |
26 | | #include <openssl/bytestring.h> |
27 | | #include <openssl/err.h> |
28 | | #include <openssl/md5.h> |
29 | | #include <openssl/mem.h> |
30 | | #include <openssl/sha.h> |
31 | | #include <openssl/stack.h> |
32 | | |
33 | | #include "internal.h" |
34 | | |
35 | | |
36 | | BSSL_NAMESPACE_BEGIN |
37 | | |
38 | | static constexpr SSL_CIPHER kCiphers[] = { |
39 | | // The RSA ciphers |
40 | | |
41 | | // Cipher 0A |
42 | | { |
43 | | SSL3_TXT_RSA_DES_192_CBC3_SHA, |
44 | | "TLS_RSA_WITH_3DES_EDE_CBC_SHA", |
45 | | SSL_CIPHER_RSA_WITH_3DES_EDE_CBC_SHA, |
46 | | SSL_kRSA, |
47 | | SSL_aRSA_DECRYPT, |
48 | | SSL_3DES, |
49 | | SSL_SHA1, |
50 | | SSL_HANDSHAKE_MAC_DEFAULT, |
51 | | }, |
52 | | |
53 | | |
54 | | // New AES ciphersuites |
55 | | |
56 | | // Cipher 2F |
57 | | { |
58 | | TLS1_TXT_RSA_WITH_AES_128_SHA, |
59 | | "TLS_RSA_WITH_AES_128_CBC_SHA", |
60 | | SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA, |
61 | | SSL_kRSA, |
62 | | SSL_aRSA_DECRYPT, |
63 | | SSL_AES128, |
64 | | SSL_SHA1, |
65 | | SSL_HANDSHAKE_MAC_DEFAULT, |
66 | | }, |
67 | | |
68 | | // Cipher 35 |
69 | | { |
70 | | TLS1_TXT_RSA_WITH_AES_256_SHA, |
71 | | "TLS_RSA_WITH_AES_256_CBC_SHA", |
72 | | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA, |
73 | | SSL_kRSA, |
74 | | SSL_aRSA_DECRYPT, |
75 | | SSL_AES256, |
76 | | SSL_SHA1, |
77 | | SSL_HANDSHAKE_MAC_DEFAULT, |
78 | | }, |
79 | | |
80 | | // PSK cipher suites. |
81 | | |
82 | | // Cipher 8C |
83 | | { |
84 | | TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, |
85 | | "TLS_PSK_WITH_AES_128_CBC_SHA", |
86 | | SSL_CIPHER_PSK_WITH_AES_128_CBC_SHA, |
87 | | SSL_kPSK, |
88 | | SSL_aPSK, |
89 | | SSL_AES128, |
90 | | SSL_SHA1, |
91 | | SSL_HANDSHAKE_MAC_DEFAULT, |
92 | | }, |
93 | | |
94 | | // Cipher 8D |
95 | | { |
96 | | TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, |
97 | | "TLS_PSK_WITH_AES_256_CBC_SHA", |
98 | | SSL_CIPHER_PSK_WITH_AES_256_CBC_SHA, |
99 | | SSL_kPSK, |
100 | | SSL_aPSK, |
101 | | SSL_AES256, |
102 | | SSL_SHA1, |
103 | | SSL_HANDSHAKE_MAC_DEFAULT, |
104 | | }, |
105 | | |
106 | | // GCM ciphersuites from RFC 5288 |
107 | | |
108 | | // Cipher 9C |
109 | | { |
110 | | TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256, |
111 | | "TLS_RSA_WITH_AES_128_GCM_SHA256", |
112 | | SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256, |
113 | | SSL_kRSA, |
114 | | SSL_aRSA_DECRYPT, |
115 | | SSL_AES128GCM, |
116 | | SSL_AEAD, |
117 | | SSL_HANDSHAKE_MAC_SHA256, |
118 | | }, |
119 | | |
120 | | // Cipher 9D |
121 | | { |
122 | | TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384, |
123 | | "TLS_RSA_WITH_AES_256_GCM_SHA384", |
124 | | SSL_CIPHER_RSA_WITH_AES_256_GCM_SHA384, |
125 | | SSL_kRSA, |
126 | | SSL_aRSA_DECRYPT, |
127 | | SSL_AES256GCM, |
128 | | SSL_AEAD, |
129 | | SSL_HANDSHAKE_MAC_SHA384, |
130 | | }, |
131 | | |
132 | | // TLS 1.3 suites. |
133 | | |
134 | | // Cipher 1301 |
135 | | { |
136 | | TLS1_3_RFC_AES_128_GCM_SHA256, |
137 | | "TLS_AES_128_GCM_SHA256", |
138 | | SSL_CIPHER_AES_128_GCM_SHA256, |
139 | | SSL_kGENERIC, |
140 | | SSL_aGENERIC, |
141 | | SSL_AES128GCM, |
142 | | SSL_AEAD, |
143 | | SSL_HANDSHAKE_MAC_SHA256, |
144 | | }, |
145 | | |
146 | | // Cipher 1302 |
147 | | { |
148 | | TLS1_3_RFC_AES_256_GCM_SHA384, |
149 | | "TLS_AES_256_GCM_SHA384", |
150 | | SSL_CIPHER_AES_256_GCM_SHA384, |
151 | | SSL_kGENERIC, |
152 | | SSL_aGENERIC, |
153 | | SSL_AES256GCM, |
154 | | SSL_AEAD, |
155 | | SSL_HANDSHAKE_MAC_SHA384, |
156 | | }, |
157 | | |
158 | | // Cipher 1303 |
159 | | { |
160 | | TLS1_3_RFC_CHACHA20_POLY1305_SHA256, |
161 | | "TLS_CHACHA20_POLY1305_SHA256", |
162 | | SSL_CIPHER_CHACHA20_POLY1305_SHA256, |
163 | | SSL_kGENERIC, |
164 | | SSL_aGENERIC, |
165 | | SSL_CHACHA20POLY1305, |
166 | | SSL_AEAD, |
167 | | SSL_HANDSHAKE_MAC_SHA256, |
168 | | }, |
169 | | |
170 | | // Cipher C009 |
171 | | { |
172 | | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, |
173 | | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", |
174 | | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, |
175 | | SSL_kECDHE, |
176 | | SSL_aECDSA, |
177 | | SSL_AES128, |
178 | | SSL_SHA1, |
179 | | SSL_HANDSHAKE_MAC_DEFAULT, |
180 | | }, |
181 | | |
182 | | // Cipher C00A |
183 | | { |
184 | | TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, |
185 | | "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", |
186 | | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, |
187 | | SSL_kECDHE, |
188 | | SSL_aECDSA, |
189 | | SSL_AES256, |
190 | | SSL_SHA1, |
191 | | SSL_HANDSHAKE_MAC_DEFAULT, |
192 | | }, |
193 | | |
194 | | // Cipher C013 |
195 | | { |
196 | | TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
197 | | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", |
198 | | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
199 | | SSL_kECDHE, |
200 | | SSL_aRSA_SIGN, |
201 | | SSL_AES128, |
202 | | SSL_SHA1, |
203 | | SSL_HANDSHAKE_MAC_DEFAULT, |
204 | | }, |
205 | | |
206 | | // Cipher C014 |
207 | | { |
208 | | TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
209 | | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", |
210 | | SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
211 | | SSL_kECDHE, |
212 | | SSL_aRSA_SIGN, |
213 | | SSL_AES256, |
214 | | SSL_SHA1, |
215 | | SSL_HANDSHAKE_MAC_DEFAULT, |
216 | | }, |
217 | | |
218 | | // HMAC based TLS v1.2 ciphersuites from RFC5289 |
219 | | |
220 | | // Cipher C023 (deprecated) |
221 | | { |
222 | | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, |
223 | | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", |
224 | | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, |
225 | | SSL_kECDHE, |
226 | | SSL_aECDSA, |
227 | | SSL_AES128, |
228 | | SSL_SHA256, |
229 | | SSL_HANDSHAKE_MAC_SHA256, |
230 | | }, |
231 | | |
232 | | // Cipher C027 (deprecated) |
233 | | { |
234 | | TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA256, |
235 | | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", |
236 | | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256, |
237 | | SSL_kECDHE, |
238 | | SSL_aRSA_SIGN, |
239 | | SSL_AES128, |
240 | | SSL_SHA256, |
241 | | SSL_HANDSHAKE_MAC_SHA256, |
242 | | }, |
243 | | |
244 | | // GCM based TLS v1.2 ciphersuites from RFC 5289 |
245 | | |
246 | | // Cipher C02B |
247 | | { |
248 | | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
249 | | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", |
250 | | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
251 | | SSL_kECDHE, |
252 | | SSL_aECDSA, |
253 | | SSL_AES128GCM, |
254 | | SSL_AEAD, |
255 | | SSL_HANDSHAKE_MAC_SHA256, |
256 | | }, |
257 | | |
258 | | // Cipher C02C |
259 | | { |
260 | | TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
261 | | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", |
262 | | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
263 | | SSL_kECDHE, |
264 | | SSL_aECDSA, |
265 | | SSL_AES256GCM, |
266 | | SSL_AEAD, |
267 | | SSL_HANDSHAKE_MAC_SHA384, |
268 | | }, |
269 | | |
270 | | // Cipher C02F |
271 | | { |
272 | | TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
273 | | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", |
274 | | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
275 | | SSL_kECDHE, |
276 | | SSL_aRSA_SIGN, |
277 | | SSL_AES128GCM, |
278 | | SSL_AEAD, |
279 | | SSL_HANDSHAKE_MAC_SHA256, |
280 | | }, |
281 | | |
282 | | // Cipher C030 |
283 | | { |
284 | | TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
285 | | "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", |
286 | | SSL_CIPHER_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
287 | | SSL_kECDHE, |
288 | | SSL_aRSA_SIGN, |
289 | | SSL_AES256GCM, |
290 | | SSL_AEAD, |
291 | | SSL_HANDSHAKE_MAC_SHA384, |
292 | | }, |
293 | | |
294 | | // ECDHE-PSK cipher suites. |
295 | | |
296 | | // Cipher C035 |
297 | | { |
298 | | TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
299 | | "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA", |
300 | | SSL_CIPHER_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
301 | | SSL_kECDHE, |
302 | | SSL_aPSK, |
303 | | SSL_AES128, |
304 | | SSL_SHA1, |
305 | | SSL_HANDSHAKE_MAC_DEFAULT, |
306 | | }, |
307 | | |
308 | | // Cipher C036 |
309 | | { |
310 | | TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
311 | | "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA", |
312 | | SSL_CIPHER_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
313 | | SSL_kECDHE, |
314 | | SSL_aPSK, |
315 | | SSL_AES256, |
316 | | SSL_SHA1, |
317 | | SSL_HANDSHAKE_MAC_DEFAULT, |
318 | | }, |
319 | | |
320 | | // ChaCha20-Poly1305 cipher suites. |
321 | | |
322 | | // Cipher CCA8 |
323 | | { |
324 | | TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
325 | | "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", |
326 | | SSL_CIPHER_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
327 | | SSL_kECDHE, |
328 | | SSL_aRSA_SIGN, |
329 | | SSL_CHACHA20POLY1305, |
330 | | SSL_AEAD, |
331 | | SSL_HANDSHAKE_MAC_SHA256, |
332 | | }, |
333 | | |
334 | | // Cipher CCA9 |
335 | | { |
336 | | TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
337 | | "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", |
338 | | SSL_CIPHER_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
339 | | SSL_kECDHE, |
340 | | SSL_aECDSA, |
341 | | SSL_CHACHA20POLY1305, |
342 | | SSL_AEAD, |
343 | | SSL_HANDSHAKE_MAC_SHA256, |
344 | | }, |
345 | | |
346 | | // Cipher CCAB |
347 | | { |
348 | | TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, |
349 | | "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256", |
350 | | SSL_CIPHER_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, |
351 | | SSL_kECDHE, |
352 | | SSL_aPSK, |
353 | | SSL_CHACHA20POLY1305, |
354 | | SSL_AEAD, |
355 | | SSL_HANDSHAKE_MAC_SHA256, |
356 | | }, |
357 | | |
358 | | }; |
359 | | |
360 | 0 | Span<const SSL_CIPHER> AllCiphers() { return kCiphers; } |
361 | | |
362 | | namespace { |
363 | 0 | constexpr size_t NumTLS13Ciphers() { |
364 | 0 | size_t num = 0; |
365 | 0 | for (const auto &cipher : kCiphers) { |
366 | 0 | if (cipher.algorithm_mkey == SSL_kGENERIC) { |
367 | 0 | num++; |
368 | 0 | } |
369 | 0 | } |
370 | 0 | return num; |
371 | 0 | } |
372 | | |
373 | 411k | #define CIPHER_ADD 1 |
374 | 41.1k | #define CIPHER_KILL 2 |
375 | 72.6k | #define CIPHER_DEL 3 |
376 | 111k | #define CIPHER_ORD 4 |
377 | 82.0k | #define CIPHER_SPECIAL 5 |
378 | | |
379 | | typedef struct cipher_order_st { |
380 | | const SSL_CIPHER *cipher; |
381 | | bool active; |
382 | | bool in_group; |
383 | | struct cipher_order_st *next, *prev; |
384 | | } CIPHER_ORDER; |
385 | | |
386 | | typedef struct cipher_alias_st { |
387 | | // name is the name of the cipher alias. |
388 | | const char *name = nullptr; |
389 | | |
390 | | // The following fields are bitmasks for the corresponding fields on |
391 | | // `SSL_CIPHER`. A cipher matches a cipher alias iff, for each bitmask, the |
392 | | // bit corresponding to the cipher's value is set to 1. If any bitmask is |
393 | | // all zeroes, the alias matches nothing. Use `~0u` for the default value. |
394 | | uint32_t algorithm_mkey = ~0u; |
395 | | uint32_t algorithm_auth = ~0u; |
396 | | uint32_t algorithm_enc = ~0u; |
397 | | uint32_t algorithm_mac = ~0u; |
398 | | |
399 | | // min_version, if non-zero, matches all ciphers which were added in that |
400 | | // particular protocol version. |
401 | | uint16_t min_version = 0; |
402 | | |
403 | | // include_deprecated, if true, means this alias includes deprecated ciphers. |
404 | | bool include_deprecated = false; |
405 | | } CIPHER_ALIAS; |
406 | | const CIPHER_ALIAS kCipherAliases[] = { |
407 | | {"ALL", ~0u, ~0u, ~0u, ~0u, 0}, |
408 | | |
409 | | // The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. |
410 | | |
411 | | // key exchange aliases |
412 | | // (some of those using only a single bit here combine |
413 | | // multiple key exchange algs according to the RFCs. |
414 | | {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0}, |
415 | | |
416 | | {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
417 | | {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
418 | | {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
419 | | |
420 | | {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0}, |
421 | | |
422 | | // server authentication aliases |
423 | | {"aRSA", ~0u, SSL_aRSA_SIGN | SSL_aRSA_DECRYPT, ~0u, ~0u, 0}, |
424 | | {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0}, |
425 | | {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0}, |
426 | | {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0}, |
427 | | |
428 | | // aliases combining key exchange and server authentication |
429 | | {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
430 | | {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
431 | | {"RSA", SSL_kRSA, SSL_aRSA_SIGN | SSL_aRSA_DECRYPT, ~0u, ~0u, 0}, |
432 | | {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0}, |
433 | | |
434 | | // symmetric encryption aliases |
435 | | {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0, /*include_deprecated=*/true}, |
436 | | {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0, |
437 | | /*include_deprecated=*/false}, |
438 | | {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0, |
439 | | /*include_deprecated=*/false}, |
440 | | {"AES", ~0u, ~0u, SSL_AES, ~0u, 0}, |
441 | | {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0, |
442 | | /*include_deprecated=*/false}, |
443 | | {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, |
444 | | /*include_deprecated=*/false}, |
445 | | |
446 | | // MAC aliases |
447 | | {"SHA1", ~0u, ~0u, ~0u, SSL_SHA1, 0}, |
448 | | {"SHA", ~0u, ~0u, ~0u, SSL_SHA1, 0}, |
449 | | |
450 | | // Legacy protocol minimum version aliases. "TLSv1" is intentionally the |
451 | | // same as "SSLv3". |
452 | | {"SSLv3", ~0u, ~0u, ~0u, ~0u, TLS1_VERSION}, |
453 | | {"TLSv1", ~0u, ~0u, ~0u, ~0u, TLS1_VERSION}, |
454 | | {"TLSv1.2", ~0u, ~0u, ~0u, ~0u, TLS1_2_VERSION}, |
455 | | |
456 | | // Legacy strength classes. |
457 | | {"HIGH", ~0u, ~0u, ~0u, ~0u, 0}, |
458 | | {"FIPS", ~0u, ~0u, ~0u, ~0u, 0}, |
459 | | }; |
460 | | } // namespace |
461 | | |
462 | | bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
463 | | size_t *out_mac_secret_len, |
464 | | size_t *out_fixed_iv_len, const SSL_CIPHER *cipher, |
465 | 279k | uint16_t version) { |
466 | 279k | *out_aead = nullptr; |
467 | 279k | *out_mac_secret_len = 0; |
468 | 279k | *out_fixed_iv_len = 0; |
469 | | |
470 | 279k | if (cipher->algorithm_mac == SSL_AEAD) { |
471 | 226k | if (cipher->algorithm_enc == SSL_AES128GCM) { |
472 | 114k | if (version < TLS1_3_VERSION) { |
473 | 73.4k | *out_aead = EVP_aead_aes_128_gcm_tls12(); |
474 | 73.4k | } else { |
475 | 41.1k | *out_aead = EVP_aead_aes_128_gcm_tls13(); |
476 | 41.1k | } |
477 | 114k | *out_fixed_iv_len = 4; |
478 | 114k | } else if (cipher->algorithm_enc == SSL_AES256GCM) { |
479 | 71.7k | if (version < TLS1_3_VERSION) { |
480 | 41.4k | *out_aead = EVP_aead_aes_256_gcm_tls12(); |
481 | 41.4k | } else { |
482 | 30.2k | *out_aead = EVP_aead_aes_256_gcm_tls13(); |
483 | 30.2k | } |
484 | 71.7k | *out_fixed_iv_len = 4; |
485 | 71.7k | } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) { |
486 | 40.1k | *out_aead = EVP_aead_chacha20_poly1305(); |
487 | 40.1k | *out_fixed_iv_len = 12; |
488 | 40.1k | } else { |
489 | 0 | return false; |
490 | 0 | } |
491 | | |
492 | | // In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code |
493 | | // above computes the TLS 1.2 construction. |
494 | 226k | if (version >= TLS1_3_VERSION) { |
495 | 111k | *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead); |
496 | 111k | } |
497 | 226k | } else if (cipher->algorithm_mac == SSL_SHA1) { |
498 | 53.4k | if (cipher->algorithm_enc == SSL_3DES) { |
499 | 8.07k | if (version == TLS1_VERSION) { |
500 | 282 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(); |
501 | 282 | *out_fixed_iv_len = 8; |
502 | 7.79k | } else { |
503 | 7.79k | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls(); |
504 | 7.79k | } |
505 | 45.3k | } else if (cipher->algorithm_enc == SSL_AES128) { |
506 | 7.67k | if (version == TLS1_VERSION) { |
507 | 1.86k | *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(); |
508 | 1.86k | *out_fixed_iv_len = 16; |
509 | 5.81k | } else { |
510 | 5.81k | *out_aead = EVP_aead_aes_128_cbc_sha1_tls(); |
511 | 5.81k | } |
512 | 37.6k | } else if (cipher->algorithm_enc == SSL_AES256) { |
513 | 37.6k | if (version == TLS1_VERSION) { |
514 | 4.39k | *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(); |
515 | 4.39k | *out_fixed_iv_len = 16; |
516 | 33.2k | } else { |
517 | 33.2k | *out_aead = EVP_aead_aes_256_cbc_sha1_tls(); |
518 | 33.2k | } |
519 | 37.6k | } else { |
520 | 0 | return false; |
521 | 0 | } |
522 | | |
523 | 53.4k | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
524 | 53.4k | } else if (cipher->algorithm_mac == SSL_SHA256) { |
525 | 0 | if (cipher->algorithm_enc == SSL_AES128) { |
526 | 0 | *out_aead = EVP_aead_aes_128_cbc_sha256_tls(); |
527 | 0 | } else { |
528 | 0 | return false; |
529 | 0 | } |
530 | | |
531 | 0 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
532 | 0 | } else { |
533 | 0 | return false; |
534 | 0 | } |
535 | | |
536 | 279k | return true; |
537 | 279k | } |
538 | | |
539 | | const EVP_MD *ssl_get_handshake_digest(uint16_t version, |
540 | 267k | const SSL_CIPHER *cipher) { |
541 | 267k | switch (cipher->algorithm_prf) { |
542 | 34.8k | case SSL_HANDSHAKE_MAC_DEFAULT: |
543 | 34.8k | return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1(); |
544 | 168k | case SSL_HANDSHAKE_MAC_SHA256: |
545 | 168k | return EVP_sha256(); |
546 | 64.0k | case SSL_HANDSHAKE_MAC_SHA384: |
547 | 64.0k | return EVP_sha384(); |
548 | 0 | default: |
549 | 0 | assert(0); |
550 | 0 | return nullptr; |
551 | 267k | } |
552 | 267k | } |
553 | | |
554 | 63.3k | static bool is_cipher_list_separator(char c, bool is_strict) { |
555 | 63.3k | if (c == ':') { |
556 | 11.1k | return true; |
557 | 11.1k | } |
558 | 52.1k | return !is_strict && (c == ' ' || c == ';' || c == ','); |
559 | 63.3k | } |
560 | | |
561 | | // rule_equals returns whether the NUL-terminated string `rule` is equal to the |
562 | | // `buf_len` bytes at `buf`. |
563 | 1.95M | static bool rule_equals(const char *rule, const char *buf, size_t buf_len) { |
564 | | // `strncmp` alone only checks that `buf` is a prefix of `rule`. |
565 | 1.95M | return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0'; |
566 | 1.95M | } |
567 | | |
568 | | static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
569 | 228k | CIPHER_ORDER **tail) { |
570 | 228k | if (curr == *tail) { |
571 | 3.69k | return; |
572 | 3.69k | } |
573 | 224k | if (curr == *head) { |
574 | 122k | *head = curr->next; |
575 | 122k | } |
576 | 224k | if (curr->prev != nullptr) { |
577 | 101k | curr->prev->next = curr->next; |
578 | 101k | } |
579 | 224k | if (curr->next != nullptr) { |
580 | 224k | curr->next->prev = curr->prev; |
581 | 224k | } |
582 | 224k | (*tail)->next = curr; |
583 | 224k | curr->prev = *tail; |
584 | 224k | curr->next = nullptr; |
585 | 224k | *tail = curr; |
586 | 224k | } |
587 | | |
588 | | static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
589 | 1.90k | CIPHER_ORDER **tail) { |
590 | 1.90k | if (curr == *head) { |
591 | 957 | return; |
592 | 957 | } |
593 | 943 | if (curr == *tail) { |
594 | 500 | *tail = curr->prev; |
595 | 500 | } |
596 | 943 | if (curr->next != nullptr) { |
597 | 443 | curr->next->prev = curr->prev; |
598 | 443 | } |
599 | 943 | if (curr->prev != nullptr) { |
600 | 943 | curr->prev->next = curr->next; |
601 | 943 | } |
602 | 943 | (*head)->prev = curr; |
603 | 943 | curr->next = *head; |
604 | 943 | curr->prev = nullptr; |
605 | 943 | *head = curr; |
606 | 943 | } |
607 | | |
608 | | // Helper to iterate over a client cipher list and find a given cipher protocol |
609 | | // ID. Returns the index of the cipher, if found in `cipher_list`, or returns |
610 | | // std::nullopt if not found. |
611 | | static std::optional<size_t> FindProtocolID(CBS *cipher_list, |
612 | 74.5k | uint16_t cipher_id) { |
613 | 74.5k | assert(CBS_len(cipher_list) % 2 == 0); |
614 | 74.5k | if (CBS_len(cipher_list) == 0) { |
615 | 0 | return std::nullopt; |
616 | 0 | } |
617 | 74.5k | size_t cur_index = 0; |
618 | 795k | while (CBS_len(cipher_list) > 0) { |
619 | 748k | uint16_t cipher_suite; |
620 | 748k | if (!CBS_get_u16(cipher_list, &cipher_suite)) { |
621 | 0 | return std::nullopt; |
622 | 0 | } |
623 | 748k | if (cipher_suite == cipher_id) { |
624 | 28.1k | return cur_index; |
625 | 28.1k | } |
626 | 720k | ++cur_index; |
627 | 720k | } |
628 | 46.3k | return std::nullopt; |
629 | 74.5k | } |
630 | | |
631 | | bool SSLCipherPreferenceList::Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers, |
632 | 85.5k | Array<bool> in_group_flags) { |
633 | 85.5k | if (sk_SSL_CIPHER_num(ciphers.get()) != in_group_flags.size()) { |
634 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
635 | 0 | return false; |
636 | 0 | } |
637 | | // The last element has no next element to be in a group with. |
638 | 85.5k | if (!in_group_flags.empty() && in_group_flags.back()) { |
639 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
640 | 0 | return false; |
641 | 0 | } |
642 | | |
643 | 85.5k | ciphers_ = std::move(ciphers); |
644 | 85.5k | in_group_flags_ = std::move(in_group_flags); |
645 | 85.5k | return true; |
646 | 85.5k | } |
647 | | |
648 | | bool SSLCipherPreferenceList::Init(Span<const uint16_t> cipher_ids, |
649 | 5.15k | Span<const bool> in_group_flags) { |
650 | 5.15k | if (cipher_ids.size() != in_group_flags.size()) { |
651 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
652 | 0 | return false; |
653 | 0 | } |
654 | | |
655 | 5.15k | UniquePtr<STACK_OF(SSL_CIPHER)> ciphers(sk_SSL_CIPHER_new_null()); |
656 | 5.15k | if (!ciphers) { |
657 | 0 | return false; |
658 | 0 | } |
659 | 15.4k | for (uint16_t cipher_id : cipher_ids) { |
660 | 15.4k | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_id); |
661 | 15.4k | if (cipher == nullptr) { |
662 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
663 | 0 | return false; |
664 | 0 | } |
665 | 15.4k | if (!sk_SSL_CIPHER_push(ciphers.get(), cipher)) { |
666 | 0 | return false; |
667 | 0 | } |
668 | 15.4k | } |
669 | | |
670 | 5.15k | Array<bool> flags; |
671 | 5.15k | if (!flags.CopyFrom(in_group_flags)) { |
672 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
673 | 0 | return false; |
674 | 0 | } |
675 | 5.15k | return Init(std::move(ciphers), std::move(flags)); |
676 | 5.15k | } |
677 | | |
678 | 5.15k | void SSLCipherPreferenceList::Reset() { |
679 | 5.15k | sk_SSL_CIPHER_zero(ciphers_.get()); |
680 | 5.15k | in_group_flags_.Reset(); |
681 | 5.15k | } |
682 | | |
683 | 59.9k | bool SSLCipherPreferenceList::CopyFrom(const SSLCipherPreferenceList &other) { |
684 | 59.9k | UniquePtr<STACK_OF(SSL_CIPHER)> other_ciphers( |
685 | 59.9k | sk_SSL_CIPHER_dup(other.ciphers())); |
686 | 59.9k | if (!other_ciphers) { |
687 | 0 | return false; |
688 | 0 | } |
689 | 59.9k | Array<bool> other_flags; |
690 | 59.9k | if (!other_flags.CopyFrom(other.in_group_flags())) { |
691 | 0 | return false; |
692 | 0 | } |
693 | 59.9k | return Init(std::move(other_ciphers), std::move(other_flags)); |
694 | 59.9k | } |
695 | | |
696 | 0 | void SSLCipherPreferenceList::Remove(const SSL_CIPHER *cipher) { |
697 | 0 | size_t index; |
698 | 0 | if (!sk_SSL_CIPHER_find(ciphers_.get(), &index, cipher)) { |
699 | 0 | return; |
700 | 0 | } |
701 | 0 | if (!in_group_flags_[index] /* last element of group */ && index > 0) { |
702 | 0 | in_group_flags_[index - 1] = false; |
703 | 0 | } |
704 | 0 | for (size_t i = index; i < size() - 1; ++i) { |
705 | 0 | in_group_flags_[i] = in_group_flags_[i + 1]; |
706 | 0 | } |
707 | 0 | sk_SSL_CIPHER_delete(ciphers_.get(), index); |
708 | 0 | in_group_flags_.Shrink(size()); |
709 | 0 | } |
710 | | |
711 | 2.75k | bool SSLCipherPreferenceList::Contains(uint16_t cipher_id) const { |
712 | 3.35k | for (const SSL_CIPHER *cipher : ciphers_.get()) { |
713 | 3.35k | if (cipher->protocol_id == cipher_id) { |
714 | 2.75k | return true; |
715 | 2.75k | } |
716 | 3.35k | } |
717 | 0 | return false; |
718 | 2.75k | } |
719 | | |
720 | | const SSL_CIPHER *SSLCipherPreferenceList::ChooseCipher( |
721 | | const CBS *client_cipher_list, bool prioritize_client_pref, |
722 | 12.8k | uint16_t version, uint32_t mask_k, uint32_t mask_a) const { |
723 | 12.8k | if (CBS_len(client_cipher_list) % 2 != 0) { |
724 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
725 | 0 | return nullptr; |
726 | 0 | } |
727 | | |
728 | | // Index of the best matching cipher suite found so far, indexed into |
729 | | // `client_cipher_list`. |
730 | 12.8k | std::optional<size_t> best_index = std::nullopt; |
731 | 12.8k | const SSL_CIPHER *best_cipher = nullptr; |
732 | | |
733 | | // Iterate over our list (the server preference list) and check for each |
734 | | // cipher in the client's list. |
735 | 173k | for (size_t i = 0; i < size(); ++i) { |
736 | 172k | const SSL_CIPHER *const c = sk_SSL_CIPHER_value(ciphers_.get(), i); |
737 | 172k | bool in_group = in_group_flags_[i]; |
738 | | // If prioritizing the client preference list, treat all of the server's |
739 | | // allowed ciphers as a single equipreference group so that the client's |
740 | | // preferences dictate the choice. |
741 | 172k | if (prioritize_client_pref) { |
742 | 157k | in_group = (i < size() - 1); |
743 | 157k | } |
744 | | |
745 | 172k | if (version >= SSL_CIPHER_get_min_version(c) && |
746 | 150k | version <= SSL_CIPHER_get_max_version(c) && |
747 | 150k | (c->algorithm_mkey & mask_k) != 0 && |
748 | 116k | (c->algorithm_auth & mask_a) != 0) { |
749 | 74.5k | CBS copy = *client_cipher_list; |
750 | 74.5k | std::optional<size_t> client_list_index = |
751 | 74.5k | FindProtocolID(©, c->protocol_id); |
752 | | // Within a group, the client's preference order applies. |
753 | 74.5k | if (client_list_index.has_value() && |
754 | 28.1k | (!best_index.has_value() || *best_index > *client_list_index)) { |
755 | 15.9k | best_index = *client_list_index; |
756 | 15.9k | best_cipher = c; |
757 | 15.9k | } |
758 | 74.5k | } |
759 | | |
760 | | // Always evaluate a whole equipreference group. |
761 | 172k | if (in_group) { |
762 | 159k | continue; |
763 | 159k | } |
764 | | // We are about to leave a (possibly singleton) group. If we have a match, |
765 | | // return it because we will only see less-preferred ciphers if we keep |
766 | | // going. |
767 | 12.8k | if (best_index.has_value()) { |
768 | 12.5k | assert(best_cipher != nullptr); |
769 | 12.5k | return best_cipher; |
770 | 12.5k | } |
771 | 12.8k | } |
772 | | |
773 | | // The final cipher suite must end a group, so, if we found a match, we must |
774 | | // have returned early above. |
775 | 12.8k | assert(!best_index.has_value()); |
776 | 330 | assert(best_cipher == nullptr); |
777 | 330 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
778 | 330 | return nullptr; |
779 | 330 | } |
780 | | |
781 | 246k | bool ssl_cipher_is_deprecated(const SSL_CIPHER *cipher) { |
782 | 246k | return cipher->protocol_id == |
783 | 246k | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 || |
784 | 236k | cipher->protocol_id == SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 || |
785 | 226k | cipher->algorithm_enc == SSL_3DES; |
786 | 246k | } |
787 | | |
788 | | // ssl_cipher_apply_rule applies the rule type `rule` to ciphers matching its |
789 | | // parameters in the linked list from `*head_p` to `*tail_p`. It writes the new |
790 | | // head and tail of the list to `*head_p` and `*tail_p`, respectively. |
791 | | // |
792 | | // - If `cipher_id` is non-zero, only that cipher is selected. |
793 | | // - Otherwise, if `strength_bits` is non-negative, it selects ciphers |
794 | | // of that strength. |
795 | | // - Otherwise, `alias` must be non-null. It selects ciphers that matches |
796 | | // `*alias`. |
797 | | static void ssl_cipher_apply_rule(uint16_t cipher_id, const CIPHER_ALIAS *alias, |
798 | | int rule, int strength_bits, bool in_group, |
799 | | CIPHER_ORDER **head_p, |
800 | 28.0k | CIPHER_ORDER **tail_p) { |
801 | 28.0k | CIPHER_ORDER *head, *tail, *curr, *next, *last; |
802 | 28.0k | const SSL_CIPHER *cp; |
803 | 28.0k | bool reverse = false; |
804 | | |
805 | 28.0k | if (cipher_id == 0 && strength_bits == -1 && alias->min_version == 0 && |
806 | 18.8k | (alias->algorithm_mkey == 0 || alias->algorithm_auth == 0 || |
807 | 18.4k | alias->algorithm_enc == 0 || alias->algorithm_mac == 0)) { |
808 | | // The rule matches nothing, so bail early. |
809 | 623 | return; |
810 | 623 | } |
811 | | |
812 | 27.4k | if (rule == CIPHER_DEL) { |
813 | | // needed to maintain sorting between currently deleted ciphers |
814 | 1.24k | reverse = true; |
815 | 1.24k | } |
816 | | |
817 | 27.4k | head = *head_p; |
818 | 27.4k | tail = *tail_p; |
819 | | |
820 | 27.4k | if (reverse) { |
821 | 1.24k | next = tail; |
822 | 1.24k | last = head; |
823 | 26.1k | } else { |
824 | 26.1k | next = head; |
825 | 26.1k | last = tail; |
826 | 26.1k | } |
827 | | |
828 | 27.4k | curr = nullptr; |
829 | 563k | for (;;) { |
830 | 563k | if (curr == last) { |
831 | 27.4k | break; |
832 | 27.4k | } |
833 | | |
834 | 535k | curr = next; |
835 | 535k | if (curr == nullptr) { |
836 | 0 | break; |
837 | 0 | } |
838 | | |
839 | 535k | next = reverse ? curr->prev : curr->next; |
840 | 535k | cp = curr->cipher; |
841 | | |
842 | | // Selection criteria is either a specific cipher, the value of |
843 | | // `strength_bits`, or the algorithms used. |
844 | 535k | if (cipher_id != 0) { |
845 | 10.5k | if (cipher_id != cp->protocol_id) { |
846 | 10.0k | continue; |
847 | 10.0k | } |
848 | 525k | } else if (strength_bits >= 0) { |
849 | 119k | if (strength_bits != SSL_CIPHER_get_bits(cp, nullptr)) { |
850 | 68.1k | continue; |
851 | 68.1k | } |
852 | 406k | } else { |
853 | 406k | if (!(alias->algorithm_mkey & cp->algorithm_mkey) || |
854 | 384k | !(alias->algorithm_auth & cp->algorithm_auth) || |
855 | 381k | !(alias->algorithm_enc & cp->algorithm_enc) || |
856 | 338k | !(alias->algorithm_mac & cp->algorithm_mac) || |
857 | 326k | (alias->min_version != 0 && |
858 | 45.2k | SSL_CIPHER_get_min_version(cp) != alias->min_version) || |
859 | 300k | (!alias->include_deprecated && ssl_cipher_is_deprecated(cp))) { |
860 | 138k | continue; |
861 | 138k | } |
862 | 406k | } |
863 | | |
864 | | // add the cipher if it has not been added yet. |
865 | 319k | if (rule == CIPHER_ADD) { |
866 | | // reverse == false |
867 | 215k | if (!curr->active) { |
868 | 192k | ll_append_tail(&head, curr, &tail); |
869 | 192k | curr->active = true; |
870 | 192k | curr->in_group = in_group; |
871 | 192k | } |
872 | 215k | } |
873 | | |
874 | | // Move the added cipher to this location |
875 | 103k | else if (rule == CIPHER_ORD) { |
876 | | // reverse == false |
877 | 60.2k | if (curr->active) { |
878 | 35.9k | ll_append_tail(&head, curr, &tail); |
879 | 35.9k | curr->in_group = false; |
880 | 35.9k | } |
881 | 60.2k | } else if (rule == CIPHER_DEL) { |
882 | | // reverse == true |
883 | 6.38k | if (curr->active) { |
884 | | // most recently deleted ciphersuites get best positions |
885 | | // for any future CIPHER_ADD (note that the CIPHER_DEL loop |
886 | | // works in reverse to maintain the order) |
887 | 1.90k | ll_append_head(&head, curr, &tail); |
888 | 1.90k | curr->active = false; |
889 | 1.90k | curr->in_group = false; |
890 | 1.90k | } |
891 | 37.3k | } else if (rule == CIPHER_KILL) { |
892 | | // reverse == false |
893 | 37.3k | if (head == curr) { |
894 | 13.7k | head = curr->next; |
895 | 23.6k | } else { |
896 | 23.6k | curr->prev->next = curr->next; |
897 | 23.6k | } |
898 | | |
899 | 37.3k | if (tail == curr) { |
900 | 1.29k | tail = curr->prev; |
901 | 1.29k | } |
902 | 37.3k | curr->active = false; |
903 | 37.3k | if (curr->next != nullptr) { |
904 | 36.0k | curr->next->prev = curr->prev; |
905 | 36.0k | } |
906 | 37.3k | if (curr->prev != nullptr) { |
907 | 23.6k | curr->prev->next = curr->next; |
908 | 23.6k | } |
909 | 37.3k | curr->next = nullptr; |
910 | 37.3k | curr->prev = nullptr; |
911 | 37.3k | } |
912 | 319k | } |
913 | | |
914 | 27.4k | *head_p = head; |
915 | 27.4k | *tail_p = tail; |
916 | 27.4k | } |
917 | | |
918 | | static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p, |
919 | 5.02k | CIPHER_ORDER **tail_p) { |
920 | | // This routine sorts the ciphers with descending strength. The sorting must |
921 | | // keep the pre-sorted sequence, so we apply the normal sorting routine as |
922 | | // '+' movement to the end of the list. |
923 | 5.02k | int max_strength_bits = 0; |
924 | 5.02k | CIPHER_ORDER *curr = *head_p; |
925 | 92.3k | while (curr != nullptr) { |
926 | 87.3k | if (curr->active && |
927 | 31.5k | SSL_CIPHER_get_bits(curr->cipher, nullptr) > max_strength_bits) { |
928 | 5.13k | max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, nullptr); |
929 | 5.13k | } |
930 | 87.3k | curr = curr->next; |
931 | 87.3k | } |
932 | | |
933 | 5.02k | Array<int> number_uses; |
934 | 5.02k | if (!number_uses.Init(max_strength_bits + 1)) { |
935 | 0 | return false; |
936 | 0 | } |
937 | | |
938 | | // Now find the strength_bits values actually used. |
939 | 5.02k | curr = *head_p; |
940 | 92.3k | while (curr != nullptr) { |
941 | 87.3k | if (curr->active) { |
942 | 31.5k | number_uses[SSL_CIPHER_get_bits(curr->cipher, nullptr)]++; |
943 | 31.5k | } |
944 | 87.3k | curr = curr->next; |
945 | 87.3k | } |
946 | | |
947 | | // Go through the list of used strength_bits values in descending order. |
948 | 787k | for (int i = max_strength_bits; i >= 0; i--) { |
949 | 782k | if (number_uses[i] > 0) { |
950 | 6.31k | ssl_cipher_apply_rule(/*cipher_id=*/0, /*alias=*/nullptr, CIPHER_ORD, i, |
951 | 6.31k | false, head_p, tail_p); |
952 | 6.31k | } |
953 | 782k | } |
954 | | |
955 | 5.02k | return true; |
956 | 5.02k | } |
957 | | |
958 | | static bool ssl_cipher_process_rulestr(const char *rule_str, |
959 | | CIPHER_ORDER **head_p, |
960 | 25.4k | CIPHER_ORDER **tail_p, bool strict) { |
961 | 25.4k | const char *l, *buf; |
962 | 25.4k | bool in_group = false, has_group = false; |
963 | 25.4k | size_t buf_len; |
964 | 25.4k | char ch; |
965 | | |
966 | 25.4k | l = rule_str; |
967 | 76.5k | for (;;) { |
968 | 76.5k | ch = *l; |
969 | | |
970 | 76.5k | if (ch == '\0') { |
971 | 14.3k | break; // done |
972 | 14.3k | } |
973 | | |
974 | 62.1k | int rule; |
975 | 62.1k | if (in_group) { |
976 | 3.29k | if (ch == ']') { |
977 | 1.18k | if (*tail_p) { |
978 | 994 | (*tail_p)->in_group = false; |
979 | 994 | } |
980 | 1.18k | in_group = false; |
981 | 1.18k | l++; |
982 | 1.18k | continue; |
983 | 1.18k | } |
984 | | |
985 | 2.10k | if (ch == '|') { |
986 | 776 | rule = CIPHER_ADD; |
987 | 776 | l++; |
988 | 776 | continue; |
989 | 1.32k | } else if (!OPENSSL_isalnum(ch)) { |
990 | 224 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP); |
991 | 224 | return false; |
992 | 1.10k | } else { |
993 | 1.10k | rule = CIPHER_ADD; |
994 | 1.10k | } |
995 | 58.8k | } else if (ch == '-') { |
996 | 1.52k | rule = CIPHER_DEL; |
997 | 1.52k | l++; |
998 | 57.3k | } else if (ch == '+') { |
999 | 912 | rule = CIPHER_ORD; |
1000 | 912 | l++; |
1001 | 56.4k | } else if (ch == '!') { |
1002 | 3.80k | rule = CIPHER_KILL; |
1003 | 3.80k | l++; |
1004 | 52.6k | } else if (ch == '@') { |
1005 | 6.80k | rule = CIPHER_SPECIAL; |
1006 | 6.80k | l++; |
1007 | 45.8k | } else if (ch == '[') { |
1008 | 2.20k | assert(!in_group); |
1009 | 2.20k | in_group = true; |
1010 | 2.20k | has_group = true; |
1011 | 2.20k | l++; |
1012 | 2.20k | continue; |
1013 | 43.5k | } else { |
1014 | 43.5k | rule = CIPHER_ADD; |
1015 | 43.5k | } |
1016 | | |
1017 | | // If preference groups are enabled, the only legal operator is +. |
1018 | | // Otherwise the in_group bits will get mixed up. |
1019 | 57.7k | if (has_group && rule != CIPHER_ADD) { |
1020 | 195 | OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS); |
1021 | 195 | return false; |
1022 | 195 | } |
1023 | | |
1024 | 57.5k | if (is_cipher_list_separator(ch, strict)) { |
1025 | 12.2k | l++; |
1026 | 12.2k | continue; |
1027 | 12.2k | } |
1028 | | |
1029 | 45.3k | bool multi = false; |
1030 | 45.3k | uint16_t cipher_id = 0; |
1031 | 45.3k | CIPHER_ALIAS alias; |
1032 | 45.3k | bool skip_rule = false; |
1033 | | |
1034 | | // When adding, exclude deprecated ciphers by default. |
1035 | 45.3k | alias.include_deprecated = rule != CIPHER_ADD; |
1036 | | |
1037 | 47.7k | for (;;) { |
1038 | 47.7k | ch = *l; |
1039 | 47.7k | buf = l; |
1040 | 47.7k | buf_len = 0; |
1041 | 213k | while (OPENSSL_isalnum(ch) || ch == '-' || ch == '.' || ch == '_') { |
1042 | 165k | ch = *(++l); |
1043 | 165k | buf_len++; |
1044 | 165k | } |
1045 | | |
1046 | 47.7k | if (buf_len == 0) { |
1047 | | // We hit something we cannot deal with, it is no command or separator |
1048 | | // nor alphanumeric, so we call this an error. |
1049 | 8.25k | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
1050 | 8.25k | return false; |
1051 | 8.25k | } |
1052 | | |
1053 | 39.4k | if (rule == CIPHER_SPECIAL) { |
1054 | 6.03k | break; |
1055 | 6.03k | } |
1056 | | |
1057 | | // Look for a matching exact cipher. These aren't allowed in multipart |
1058 | | // rules. |
1059 | 33.4k | if (!multi && ch != '+') { |
1060 | 725k | for (const SSL_CIPHER &cipher : kCiphers) { |
1061 | 725k | if (rule_equals(cipher.name, buf, buf_len) || |
1062 | 725k | rule_equals(cipher.standard_name, buf, buf_len)) { |
1063 | 482 | cipher_id = cipher.protocol_id; |
1064 | 482 | break; |
1065 | 482 | } |
1066 | 725k | } |
1067 | 29.4k | } |
1068 | 33.4k | if (cipher_id == 0) { |
1069 | | // If not an exact cipher, look for a matching cipher alias. |
1070 | 32.9k | const auto found = |
1071 | 32.9k | std::find_if(std::begin(kCipherAliases), std::end(kCipherAliases), |
1072 | 500k | [buf, buf_len](const CIPHER_ALIAS &cipher_alias) { |
1073 | 500k | return rule_equals(cipher_alias.name, buf, buf_len); |
1074 | 500k | }); |
1075 | 32.9k | if (found == std::end(kCipherAliases)) { |
1076 | 10.3k | skip_rule = true; |
1077 | 10.3k | if (strict) { |
1078 | 1.35k | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
1079 | 1.35k | return false; |
1080 | 1.35k | } |
1081 | 22.6k | } else { |
1082 | 22.6k | alias.algorithm_mkey &= found->algorithm_mkey; |
1083 | 22.6k | alias.algorithm_auth &= found->algorithm_auth; |
1084 | 22.6k | alias.algorithm_enc &= found->algorithm_enc; |
1085 | 22.6k | alias.algorithm_mac &= found->algorithm_mac; |
1086 | | |
1087 | | // When specifying a combination of aliases, if any aliases |
1088 | | // enables deprecated ciphers, deprecated ciphers are included. This |
1089 | | // is slightly different from the bitmasks in that adding aliases |
1090 | | // can increase the set of matched ciphers. This is so that an alias |
1091 | | // like "RSA" will only specify AES-based RSA ciphers, but |
1092 | | // "RSA+3DES" will still specify 3DES. |
1093 | 22.6k | alias.include_deprecated |= found->include_deprecated; |
1094 | | |
1095 | 22.6k | if (found->min_version != 0) { |
1096 | 2.93k | if (alias.min_version != 0 && |
1097 | 388 | alias.min_version != found->min_version) { |
1098 | 194 | skip_rule = true; |
1099 | 2.74k | } else { |
1100 | 2.74k | alias.min_version = found->min_version; |
1101 | 2.74k | } |
1102 | 2.93k | } |
1103 | 22.6k | } |
1104 | 32.9k | } |
1105 | | |
1106 | | // Check for a multipart rule. |
1107 | 32.0k | if (ch != '+') { |
1108 | 29.6k | break; |
1109 | 29.6k | } |
1110 | 2.39k | l++; |
1111 | 2.39k | multi = true; |
1112 | 2.39k | } |
1113 | | |
1114 | | // Ok, we have the rule, now apply it. |
1115 | 35.7k | if (rule == CIPHER_SPECIAL) { |
1116 | 6.03k | if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) { |
1117 | 1.01k | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
1118 | 1.01k | return false; |
1119 | 1.01k | } |
1120 | 5.02k | if (!ssl_cipher_strength_sort(head_p, tail_p)) { |
1121 | 0 | return false; |
1122 | 0 | } |
1123 | | |
1124 | | // We do not support any "multi" options together with "@", so throw away |
1125 | | // the rest of the command, if any left, until end or ':' is found. |
1126 | 7.22k | while (*l != '\0' && !is_cipher_list_separator(*l, strict)) { |
1127 | 2.20k | l++; |
1128 | 2.20k | } |
1129 | 29.6k | } else if (!skip_rule) { |
1130 | 21.7k | ssl_cipher_apply_rule(cipher_id, &alias, rule, -1, in_group, head_p, |
1131 | 21.7k | tail_p); |
1132 | 21.7k | } |
1133 | 35.7k | } |
1134 | | |
1135 | 14.3k | if (in_group) { |
1136 | 784 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
1137 | 784 | return false; |
1138 | 784 | } |
1139 | | |
1140 | 13.5k | return true; |
1141 | 14.3k | } |
1142 | | |
1143 | | bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list, |
1144 | 32.3k | const char *rule_str, bool strict) { |
1145 | | // Return with error if nothing to do. |
1146 | 32.3k | if (rule_str == nullptr || out_cipher_list == nullptr) { |
1147 | 0 | return false; |
1148 | 0 | } |
1149 | | |
1150 | | // We prefer ECDHE ciphers over non-PFS ciphers. Then we prefer AEAD over |
1151 | | // non-AEAD. |
1152 | 32.3k | static const uint16_t kAESCiphers[] = { |
1153 | 32.3k | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
1154 | 32.3k | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
1155 | 32.3k | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
1156 | 32.3k | SSL_CIPHER_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
1157 | 32.3k | }; |
1158 | 32.3k | static const uint16_t kChaChaCiphers[] = { |
1159 | 32.3k | SSL_CIPHER_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
1160 | 32.3k | SSL_CIPHER_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
1161 | 32.3k | SSL_CIPHER_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, |
1162 | 32.3k | }; |
1163 | 32.3k | static const uint16_t kLegacyCiphers[] = { |
1164 | 32.3k | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, |
1165 | 32.3k | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
1166 | 32.3k | SSL_CIPHER_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
1167 | 32.3k | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, |
1168 | 32.3k | SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
1169 | 32.3k | SSL_CIPHER_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
1170 | 32.3k | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, |
1171 | 32.3k | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256, |
1172 | 32.3k | SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256, |
1173 | 32.3k | SSL_CIPHER_RSA_WITH_AES_256_GCM_SHA384, |
1174 | 32.3k | SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA, |
1175 | 32.3k | SSL_CIPHER_PSK_WITH_AES_128_CBC_SHA, |
1176 | 32.3k | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA, |
1177 | 32.3k | SSL_CIPHER_PSK_WITH_AES_256_CBC_SHA, |
1178 | 32.3k | SSL_CIPHER_RSA_WITH_3DES_EDE_CBC_SHA, |
1179 | 32.3k | }; |
1180 | | |
1181 | | // Set up a linked list of ciphers. |
1182 | 32.3k | CIPHER_ORDER co_list[std::size(kAESCiphers) + std::size(kChaChaCiphers) + |
1183 | 32.3k | std::size(kLegacyCiphers)]; |
1184 | 743k | for (size_t i = 0; i < std::size(co_list); i++) { |
1185 | 711k | co_list[i].next = i + 1 < std::size(co_list) ? &co_list[i + 1] : nullptr; |
1186 | 711k | co_list[i].prev = i == 0 ? nullptr : &co_list[i - 1]; |
1187 | 711k | co_list[i].active = false; |
1188 | 711k | co_list[i].in_group = false; |
1189 | 711k | } |
1190 | 32.3k | CIPHER_ORDER *head = &co_list[0]; |
1191 | 32.3k | CIPHER_ORDER *tail = &co_list[std::size(co_list) - 1]; |
1192 | | |
1193 | | // Order AES ciphers vs ChaCha ciphers based on whether we have AES hardware. |
1194 | | // |
1195 | | // TODO(crbug.com/boringssl/29): We should also set up equipreference groups |
1196 | | // as a server. |
1197 | 32.3k | size_t num = 0; |
1198 | 32.3k | const bool has_aes_hw = EVP_has_aes_hardware(); |
1199 | 32.3k | if (has_aes_hw) { |
1200 | 129k | for (uint16_t id : kAESCiphers) { |
1201 | 129k | co_list[num++].cipher = SSL_get_cipher_by_value(id); |
1202 | 129k | assert(co_list[num - 1].cipher != nullptr); |
1203 | 129k | } |
1204 | 32.3k | } |
1205 | 96.9k | for (uint16_t id : kChaChaCiphers) { |
1206 | 96.9k | co_list[num++].cipher = SSL_get_cipher_by_value(id); |
1207 | 96.9k | assert(co_list[num - 1].cipher != nullptr); |
1208 | 96.9k | } |
1209 | 32.3k | if (!has_aes_hw) { |
1210 | 0 | for (uint16_t id : kAESCiphers) { |
1211 | 0 | co_list[num++].cipher = SSL_get_cipher_by_value(id); |
1212 | 0 | assert(co_list[num - 1].cipher != nullptr); |
1213 | 0 | } |
1214 | 0 | } |
1215 | 484k | for (uint16_t id : kLegacyCiphers) { |
1216 | 484k | co_list[num++].cipher = SSL_get_cipher_by_value(id); |
1217 | 484k | assert(co_list[num - 1].cipher != nullptr); |
1218 | 484k | } |
1219 | 32.3k | assert(num == std::size(co_list)); |
1220 | 32.3k | static_assert(std::size(co_list) + NumTLS13Ciphers() == std::size(kCiphers), |
1221 | 32.3k | "Not all ciphers are included in the cipher order"); |
1222 | | |
1223 | | // If the rule_string begins with DEFAULT, apply the default rule before |
1224 | | // using the (possibly available) additional rules. |
1225 | 32.3k | const char *rule_p = rule_str; |
1226 | 32.3k | if (strncmp(rule_str, "DEFAULT", 7) == 0) { |
1227 | 977 | if (!ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail, |
1228 | 977 | strict)) { |
1229 | 0 | return false; |
1230 | 0 | } |
1231 | 977 | rule_p += 7; |
1232 | 977 | if (*rule_p == ':') { |
1233 | 271 | rule_p++; |
1234 | 271 | } |
1235 | 977 | } |
1236 | | |
1237 | 32.3k | if (*rule_p != '\0' && |
1238 | 24.4k | !ssl_cipher_process_rulestr(rule_p, &head, &tail, strict)) { |
1239 | 11.8k | return false; |
1240 | 11.8k | } |
1241 | | |
1242 | | // Allocate new "cipherstack" for the result, return with error |
1243 | | // if we cannot get one. |
1244 | 20.4k | UniquePtr<STACK_OF(SSL_CIPHER)> cipherstack(sk_SSL_CIPHER_new_null()); |
1245 | 20.4k | Array<bool> in_group_flags; |
1246 | 20.4k | if (cipherstack == nullptr || |
1247 | 20.4k | !in_group_flags.InitForOverwrite(std::size(kCiphers))) { |
1248 | 0 | return false; |
1249 | 0 | } |
1250 | | |
1251 | | // The cipher selection for the list is done. The ciphers are added |
1252 | | // to the resulting precedence to the STACK_OF(SSL_CIPHER). |
1253 | 20.4k | size_t num_in_group_flags = 0; |
1254 | 452k | for (CIPHER_ORDER *curr = head; curr != nullptr; curr = curr->next) { |
1255 | 432k | if (curr->active) { |
1256 | 154k | if (!sk_SSL_CIPHER_push(cipherstack.get(), curr->cipher)) { |
1257 | 0 | return false; |
1258 | 0 | } |
1259 | 154k | in_group_flags[num_in_group_flags++] = curr->in_group; |
1260 | 154k | } |
1261 | 432k | } |
1262 | 20.4k | in_group_flags.Shrink(num_in_group_flags); |
1263 | | |
1264 | 20.4k | UniquePtr<SSLCipherPreferenceList> pref_list = |
1265 | 20.4k | MakeUnique<SSLCipherPreferenceList>(); |
1266 | 20.4k | if (!pref_list || |
1267 | 20.4k | !pref_list->Init(std::move(cipherstack), std::move(in_group_flags))) { |
1268 | 0 | return false; |
1269 | 0 | } |
1270 | | |
1271 | 20.4k | *out_cipher_list = std::move(pref_list); |
1272 | | |
1273 | | // Configuring an empty cipher list is an error but still updates the |
1274 | | // output. |
1275 | 20.4k | if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers()) == 0) { |
1276 | 10.6k | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); |
1277 | 10.6k | return false; |
1278 | 10.6k | } |
1279 | | |
1280 | 9.86k | return true; |
1281 | 20.4k | } |
1282 | | |
1283 | | bool ssl_create_default_tls13_cipher_list( |
1284 | 5.15k | SSLCipherPreferenceList *out_cipher_list) { |
1285 | | // If we have AES hardware: |
1286 | | // For a client: AES-128 > AES-256 > ChaCha20. |
1287 | | // For a server: (AES-128 | AES-256 | ChaCha20), i.e. defer to client |
1288 | | // preference. |
1289 | 5.15k | static const uint16_t kCiphersAESHardware[] = { |
1290 | 5.15k | SSL_CIPHER_AES_128_GCM_SHA256, |
1291 | 5.15k | SSL_CIPHER_AES_256_GCM_SHA384, |
1292 | 5.15k | SSL_CIPHER_CHACHA20_POLY1305_SHA256, |
1293 | 5.15k | }; |
1294 | 5.15k | static const bool kInGroupFlagsAESHardware[] = { |
1295 | 5.15k | true, |
1296 | 5.15k | true, |
1297 | 5.15k | false, |
1298 | 5.15k | }; |
1299 | | // If we do not have AES hardware: |
1300 | | // For a client: ChaCha20 > AES-128 > AES-256. |
1301 | | // For a server: ChaCha20 > (AES-128 | AES-256). |
1302 | 5.15k | static const uint16_t kCiphersNoAESHardware[] = { |
1303 | 5.15k | SSL_CIPHER_CHACHA20_POLY1305_SHA256, |
1304 | 5.15k | SSL_CIPHER_AES_128_GCM_SHA256, |
1305 | 5.15k | SSL_CIPHER_AES_256_GCM_SHA384, |
1306 | 5.15k | }; |
1307 | 5.15k | static const bool kInGroupFlagsNoAESHardware[] = { |
1308 | 5.15k | false, |
1309 | 5.15k | true, |
1310 | 5.15k | false, |
1311 | 5.15k | }; |
1312 | | |
1313 | 5.15k | Span<const uint16_t> ciphers = EVP_has_aes_hardware() |
1314 | 5.15k | ? Span(kCiphersAESHardware) |
1315 | 5.15k | : Span(kCiphersNoAESHardware); |
1316 | 5.15k | Span<const bool> in_group_flags = EVP_has_aes_hardware() |
1317 | 5.15k | ? Span(kInGroupFlagsAESHardware) |
1318 | 5.15k | : Span(kInGroupFlagsNoAESHardware); |
1319 | | |
1320 | 5.15k | out_cipher_list->Reset(); |
1321 | 5.15k | return out_cipher_list->Init(ciphers, in_group_flags); |
1322 | 5.15k | } |
1323 | | |
1324 | 52.3k | uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key, bool sign_ok) { |
1325 | 52.3k | switch (EVP_PKEY_id(key)) { |
1326 | 33.1k | case EVP_PKEY_RSA: |
1327 | 33.1k | return sign_ok ? (SSL_aRSA_SIGN | SSL_aRSA_DECRYPT) : SSL_aRSA_DECRYPT; |
1328 | 19.2k | case EVP_PKEY_EC: |
1329 | 19.2k | case EVP_PKEY_ED25519: |
1330 | | // Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers. |
1331 | 19.2k | return sign_ok ? SSL_aECDSA : 0; |
1332 | 0 | default: |
1333 | 0 | return 0; |
1334 | 52.3k | } |
1335 | 52.3k | } |
1336 | | |
1337 | 416k | bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) { |
1338 | 416k | return (cipher->algorithm_auth & SSL_aCERT) != 0; |
1339 | 416k | } |
1340 | | |
1341 | 17.8k | bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) { |
1342 | | // Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is |
1343 | | // optional or omitted in all others. |
1344 | 17.8k | return (cipher->algorithm_mkey & SSL_kECDHE) != 0; |
1345 | 17.8k | } |
1346 | | |
1347 | 0 | size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) { |
1348 | 0 | size_t block_size; |
1349 | 0 | switch (cipher->algorithm_enc) { |
1350 | 0 | case SSL_3DES: |
1351 | 0 | block_size = 8; |
1352 | 0 | break; |
1353 | 0 | case SSL_AES128: |
1354 | 0 | case SSL_AES256: |
1355 | 0 | block_size = 16; |
1356 | 0 | break; |
1357 | 0 | default: |
1358 | 0 | return 0; |
1359 | 0 | } |
1360 | | |
1361 | | // All supported TLS 1.0 ciphers use SHA-1. |
1362 | 0 | assert(cipher->algorithm_mac == SSL_SHA1); |
1363 | 0 | size_t ret = 1 + SHA_DIGEST_LENGTH; |
1364 | 0 | ret += block_size - (ret % block_size); |
1365 | 0 | return ret; |
1366 | 0 | } |
1367 | | |
1368 | | BSSL_NAMESPACE_END |
1369 | | |
1370 | | using namespace bssl; |
1371 | | |
1372 | | static constexpr int ssl_cipher_id_cmp(const SSL_CIPHER *a, |
1373 | 3.65M | const SSL_CIPHER *b) { |
1374 | 3.65M | if (a->protocol_id > b->protocol_id) { |
1375 | 1.17M | return 1; |
1376 | 1.17M | } |
1377 | 2.47M | if (a->protocol_id < b->protocol_id) { |
1378 | 1.56M | return -1; |
1379 | 1.56M | } |
1380 | 917k | return 0; |
1381 | 2.47M | } |
1382 | | |
1383 | 3.65M | static int ssl_cipher_id_cmp_void(const void *in_a, const void *in_b) { |
1384 | 3.65M | return ssl_cipher_id_cmp(reinterpret_cast<const SSL_CIPHER *>(in_a), |
1385 | 3.65M | reinterpret_cast<const SSL_CIPHER *>(in_b)); |
1386 | 3.65M | } |
1387 | | |
1388 | | template <size_t N> |
1389 | 0 | static constexpr bool ssl_ciphers_sorted(const SSL_CIPHER (&ciphers)[N]) { |
1390 | 0 | for (size_t i = 1; i < N; i++) { |
1391 | 0 | if (ssl_cipher_id_cmp(&ciphers[i - 1], &ciphers[i]) >= 0) { |
1392 | 0 | return false; |
1393 | 0 | } |
1394 | 0 | } |
1395 | 0 | return true; |
1396 | 0 | } |
1397 | | |
1398 | | static_assert(ssl_ciphers_sorted(kCiphers), |
1399 | | "Ciphers are not sorted, bsearch won't work"); |
1400 | | |
1401 | 917k | const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) { |
1402 | 917k | SSL_CIPHER c; |
1403 | 917k | c.protocol_id = value; |
1404 | 917k | return reinterpret_cast<const SSL_CIPHER *>( |
1405 | 917k | bsearch(&c, kCiphers, std::size(kCiphers), sizeof(SSL_CIPHER), |
1406 | 917k | ssl_cipher_id_cmp_void)); |
1407 | 917k | } |
1408 | | |
1409 | 0 | uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { |
1410 | | // Historically, OpenSSL added a leading 0x03 byte to cipher IDs, to |
1411 | | // distinguish between SSL 2.0 and SSL 3.0. |
1412 | 0 | return cipher->protocol_id | 0x03000000; |
1413 | 0 | } |
1414 | | |
1415 | 1.01M | uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *cipher) { |
1416 | 1.01M | return cipher->protocol_id; |
1417 | 1.01M | } |
1418 | | |
1419 | 0 | int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) { |
1420 | 0 | return (cipher->algorithm_mac & SSL_AEAD) != 0; |
1421 | 0 | } |
1422 | | |
1423 | 0 | int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) { |
1424 | 0 | switch (cipher->algorithm_enc) { |
1425 | 0 | case SSL_3DES: |
1426 | 0 | return NID_des_ede3_cbc; |
1427 | 0 | case SSL_AES128: |
1428 | 0 | return NID_aes_128_cbc; |
1429 | 0 | case SSL_AES256: |
1430 | 0 | return NID_aes_256_cbc; |
1431 | 0 | case SSL_AES128GCM: |
1432 | 0 | return NID_aes_128_gcm; |
1433 | 0 | case SSL_AES256GCM: |
1434 | 0 | return NID_aes_256_gcm; |
1435 | 0 | case SSL_CHACHA20POLY1305: |
1436 | 0 | return NID_chacha20_poly1305; |
1437 | 0 | } |
1438 | 0 | assert(0); |
1439 | 0 | return NID_undef; |
1440 | 0 | } |
1441 | | |
1442 | 0 | int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) { |
1443 | 0 | switch (cipher->algorithm_mac) { |
1444 | 0 | case SSL_AEAD: |
1445 | 0 | return NID_undef; |
1446 | 0 | case SSL_SHA1: |
1447 | 0 | return NID_sha1; |
1448 | 0 | case SSL_SHA256: |
1449 | 0 | return NID_sha256; |
1450 | 0 | } |
1451 | 0 | assert(0); |
1452 | 0 | return NID_undef; |
1453 | 0 | } |
1454 | | |
1455 | 0 | int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) { |
1456 | 0 | switch (cipher->algorithm_mkey) { |
1457 | 0 | case SSL_kRSA: |
1458 | 0 | return NID_kx_rsa; |
1459 | 0 | case SSL_kECDHE: |
1460 | 0 | return NID_kx_ecdhe; |
1461 | 0 | case SSL_kPSK: |
1462 | 0 | return NID_kx_psk; |
1463 | 0 | case SSL_kGENERIC: |
1464 | 0 | return NID_kx_any; |
1465 | 0 | } |
1466 | 0 | assert(0); |
1467 | 0 | return NID_undef; |
1468 | 0 | } |
1469 | | |
1470 | 0 | int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) { |
1471 | 0 | switch (cipher->algorithm_auth) { |
1472 | 0 | case SSL_aRSA_DECRYPT: |
1473 | 0 | case SSL_aRSA_SIGN: |
1474 | 0 | return NID_auth_rsa; |
1475 | 0 | case SSL_aECDSA: |
1476 | 0 | return NID_auth_ecdsa; |
1477 | 0 | case SSL_aPSK: |
1478 | 0 | return NID_auth_psk; |
1479 | 0 | case SSL_aGENERIC: |
1480 | 0 | return NID_auth_any; |
1481 | 0 | } |
1482 | 0 | assert(0); |
1483 | 0 | return NID_undef; |
1484 | 0 | } |
1485 | | |
1486 | 653 | const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *cipher) { |
1487 | 653 | switch (cipher->algorithm_prf) { |
1488 | 0 | case SSL_HANDSHAKE_MAC_DEFAULT: |
1489 | 0 | return EVP_md5_sha1(); |
1490 | 628 | case SSL_HANDSHAKE_MAC_SHA256: |
1491 | 628 | return EVP_sha256(); |
1492 | 25 | case SSL_HANDSHAKE_MAC_SHA384: |
1493 | 25 | return EVP_sha384(); |
1494 | 653 | } |
1495 | 653 | assert(0); |
1496 | 0 | return nullptr; |
1497 | 0 | } |
1498 | | |
1499 | 0 | int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) { |
1500 | 0 | const EVP_MD *md = SSL_CIPHER_get_handshake_digest(cipher); |
1501 | 0 | if (md == nullptr) { |
1502 | 0 | return NID_undef; |
1503 | 0 | } |
1504 | 0 | return EVP_MD_nid(md); |
1505 | 0 | } |
1506 | | |
1507 | 0 | int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) { |
1508 | 0 | return cipher->algorithm_mac != SSL_AEAD; |
1509 | 0 | } |
1510 | | |
1511 | 1.09M | uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) { |
1512 | 1.09M | if (cipher->algorithm_mkey == SSL_kGENERIC || |
1513 | 1.08M | cipher->algorithm_auth == SSL_aGENERIC) { |
1514 | 17.5k | return TLS1_3_VERSION; |
1515 | 17.5k | } |
1516 | | |
1517 | 1.08M | if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) { |
1518 | | // Cipher suites before TLS 1.2 use the default PRF, while all those added |
1519 | | // afterwards specify a particular hash. |
1520 | 572k | return TLS1_2_VERSION; |
1521 | 572k | } |
1522 | 508k | return TLS1_VERSION; |
1523 | 1.08M | } |
1524 | | |
1525 | 1.03M | uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) { |
1526 | 1.03M | if (cipher->algorithm_mkey == SSL_kGENERIC || |
1527 | 1.01M | cipher->algorithm_auth == SSL_aGENERIC) { |
1528 | 17.5k | return TLS1_3_VERSION; |
1529 | 17.5k | } |
1530 | 1.01M | return TLS1_2_VERSION; |
1531 | 1.03M | } |
1532 | | |
1533 | | static const char *const kUnknownCipher = "(NONE)"; |
1534 | | |
1535 | | // return the actual cipher being used |
1536 | 0 | const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) { |
1537 | 0 | if (cipher != nullptr) { |
1538 | 0 | return cipher->name; |
1539 | 0 | } |
1540 | | |
1541 | 0 | return kUnknownCipher; |
1542 | 0 | } |
1543 | | |
1544 | 0 | const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) { |
1545 | 0 | return cipher->standard_name; |
1546 | 0 | } |
1547 | | |
1548 | 0 | const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) { |
1549 | 0 | if (cipher == nullptr) { |
1550 | 0 | return ""; |
1551 | 0 | } |
1552 | | |
1553 | 0 | switch (cipher->algorithm_mkey) { |
1554 | 0 | case SSL_kRSA: |
1555 | 0 | return "RSA"; |
1556 | | |
1557 | 0 | case SSL_kECDHE: |
1558 | 0 | switch (cipher->algorithm_auth) { |
1559 | 0 | case SSL_aECDSA: |
1560 | 0 | return "ECDHE_ECDSA"; |
1561 | 0 | case SSL_aRSA_SIGN: |
1562 | 0 | return "ECDHE_RSA"; |
1563 | 0 | case SSL_aPSK: |
1564 | 0 | return "ECDHE_PSK"; |
1565 | 0 | default: |
1566 | 0 | assert(0); |
1567 | 0 | return "UNKNOWN"; |
1568 | 0 | } |
1569 | | |
1570 | 0 | case SSL_kPSK: |
1571 | 0 | assert(cipher->algorithm_auth == SSL_aPSK); |
1572 | 0 | return "PSK"; |
1573 | | |
1574 | 0 | case SSL_kGENERIC: |
1575 | 0 | assert(cipher->algorithm_auth == SSL_aGENERIC); |
1576 | 0 | return "GENERIC"; |
1577 | | |
1578 | 0 | default: |
1579 | 0 | assert(0); |
1580 | 0 | return "UNKNOWN"; |
1581 | 0 | } |
1582 | 0 | } |
1583 | | |
1584 | 187k | int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) { |
1585 | 187k | if (cipher == nullptr) { |
1586 | 0 | return 0; |
1587 | 0 | } |
1588 | | |
1589 | 187k | int alg_bits, strength_bits; |
1590 | 187k | switch (cipher->algorithm_enc) { |
1591 | 53.8k | case SSL_AES128: |
1592 | 79.4k | case SSL_AES128GCM: |
1593 | 79.4k | alg_bits = 128; |
1594 | 79.4k | strength_bits = 128; |
1595 | 79.4k | break; |
1596 | | |
1597 | 42.8k | case SSL_AES256: |
1598 | 68.6k | case SSL_AES256GCM: |
1599 | 96.9k | case SSL_CHACHA20POLY1305: |
1600 | 96.9k | alg_bits = 256; |
1601 | 96.9k | strength_bits = 256; |
1602 | 96.9k | break; |
1603 | | |
1604 | 10.9k | case SSL_3DES: |
1605 | 10.9k | alg_bits = 168; |
1606 | 10.9k | strength_bits = 112; |
1607 | 10.9k | break; |
1608 | | |
1609 | 0 | default: |
1610 | 0 | assert(0); |
1611 | 0 | alg_bits = 0; |
1612 | 0 | strength_bits = 0; |
1613 | 187k | } |
1614 | | |
1615 | 187k | if (out_alg_bits != nullptr) { |
1616 | 0 | *out_alg_bits = alg_bits; |
1617 | 0 | } |
1618 | 187k | return strength_bits; |
1619 | 187k | } |
1620 | | |
1621 | | const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, |
1622 | 0 | int len) { |
1623 | 0 | const char *kx, *au, *enc, *mac; |
1624 | 0 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac; |
1625 | |
|
1626 | 0 | alg_mkey = cipher->algorithm_mkey; |
1627 | 0 | alg_auth = cipher->algorithm_auth; |
1628 | 0 | alg_enc = cipher->algorithm_enc; |
1629 | 0 | alg_mac = cipher->algorithm_mac; |
1630 | |
|
1631 | 0 | switch (alg_mkey) { |
1632 | 0 | case SSL_kRSA: |
1633 | 0 | kx = "RSA"; |
1634 | 0 | break; |
1635 | | |
1636 | 0 | case SSL_kECDHE: |
1637 | 0 | kx = "ECDH"; |
1638 | 0 | break; |
1639 | | |
1640 | 0 | case SSL_kPSK: |
1641 | 0 | kx = "PSK"; |
1642 | 0 | break; |
1643 | | |
1644 | 0 | case SSL_kGENERIC: |
1645 | 0 | kx = "GENERIC"; |
1646 | 0 | break; |
1647 | | |
1648 | 0 | default: |
1649 | 0 | kx = "unknown"; |
1650 | 0 | } |
1651 | | |
1652 | 0 | switch (alg_auth) { |
1653 | 0 | case SSL_aRSA_DECRYPT: |
1654 | 0 | case SSL_aRSA_SIGN: |
1655 | 0 | au = "RSA"; |
1656 | 0 | break; |
1657 | | |
1658 | 0 | case SSL_aECDSA: |
1659 | 0 | au = "ECDSA"; |
1660 | 0 | break; |
1661 | | |
1662 | 0 | case SSL_aPSK: |
1663 | 0 | au = "PSK"; |
1664 | 0 | break; |
1665 | | |
1666 | 0 | case SSL_aGENERIC: |
1667 | 0 | au = "GENERIC"; |
1668 | 0 | break; |
1669 | | |
1670 | 0 | default: |
1671 | 0 | au = "unknown"; |
1672 | 0 | break; |
1673 | 0 | } |
1674 | | |
1675 | 0 | switch (alg_enc) { |
1676 | 0 | case SSL_3DES: |
1677 | 0 | enc = "3DES(168)"; |
1678 | 0 | break; |
1679 | | |
1680 | 0 | case SSL_AES128: |
1681 | 0 | enc = "AES(128)"; |
1682 | 0 | break; |
1683 | | |
1684 | 0 | case SSL_AES256: |
1685 | 0 | enc = "AES(256)"; |
1686 | 0 | break; |
1687 | | |
1688 | 0 | case SSL_AES128GCM: |
1689 | 0 | enc = "AESGCM(128)"; |
1690 | 0 | break; |
1691 | | |
1692 | 0 | case SSL_AES256GCM: |
1693 | 0 | enc = "AESGCM(256)"; |
1694 | 0 | break; |
1695 | | |
1696 | 0 | case SSL_CHACHA20POLY1305: |
1697 | 0 | enc = "ChaCha20-Poly1305"; |
1698 | 0 | break; |
1699 | | |
1700 | 0 | default: |
1701 | 0 | enc = "unknown"; |
1702 | 0 | break; |
1703 | 0 | } |
1704 | | |
1705 | 0 | switch (alg_mac) { |
1706 | 0 | case SSL_SHA1: |
1707 | 0 | mac = "SHA1"; |
1708 | 0 | break; |
1709 | | |
1710 | 0 | case SSL_SHA256: |
1711 | 0 | mac = "SHA256"; |
1712 | 0 | break; |
1713 | | |
1714 | 0 | case SSL_AEAD: |
1715 | 0 | mac = "AEAD"; |
1716 | 0 | break; |
1717 | | |
1718 | 0 | default: |
1719 | 0 | mac = "unknown"; |
1720 | 0 | break; |
1721 | 0 | } |
1722 | | |
1723 | 0 | if (buf == nullptr) { |
1724 | 0 | len = 128; |
1725 | 0 | buf = (char *)OPENSSL_malloc(len); |
1726 | 0 | if (buf == nullptr) { |
1727 | 0 | return nullptr; |
1728 | 0 | } |
1729 | 0 | } else if (len < 128) { |
1730 | 0 | return "Buffer too small"; |
1731 | 0 | } |
1732 | | |
1733 | 0 | snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n", cipher->name, |
1734 | 0 | kx, au, enc, mac); |
1735 | 0 | return buf; |
1736 | 0 | } |
1737 | | |
1738 | 0 | const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) { |
1739 | 0 | return "TLSv1/SSLv3"; |
1740 | 0 | } |
1741 | | |
1742 | 0 | STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods() { return nullptr; } |
1743 | | |
1744 | 0 | int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; } |
1745 | | |
1746 | 0 | const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return nullptr; } |
1747 | | |
1748 | 0 | const char *SSL_COMP_get0_name(const SSL_COMP *comp) { return comp->name; } |
1749 | | |
1750 | 0 | int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; } |
1751 | | |
1752 | 0 | void SSL_COMP_free_compression_methods() {} |
1753 | | |
1754 | 0 | size_t SSL_get_all_cipher_names(const char **out, size_t max_out) { |
1755 | 0 | return GetAllNames(out, max_out, Span(&kUnknownCipher, 1), &SSL_CIPHER::name, |
1756 | 0 | Span(kCiphers)); |
1757 | 0 | } |
1758 | | |
1759 | 0 | size_t SSL_get_all_standard_cipher_names(const char **out, size_t max_out) { |
1760 | 0 | return GetAllNames(out, max_out, Span<const char *const>(), |
1761 | 0 | &SSL_CIPHER::standard_name, Span(kCiphers)); |
1762 | 0 | } |