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