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