summaryrefslogtreecommitdiff
path: root/src/lib/libssl/ssl_ciph.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/ssl_ciph.c')
-rw-r--r--src/lib/libssl/ssl_ciph.c1731
1 files changed, 0 insertions, 1731 deletions
diff --git a/src/lib/libssl/ssl_ciph.c b/src/lib/libssl/ssl_ciph.c
deleted file mode 100644
index 0e9941bc0b..0000000000
--- a/src/lib/libssl/ssl_ciph.c
+++ /dev/null
@@ -1,1731 +0,0 @@
1/* $OpenBSD: ssl_ciph.c,v 1.124 2021/07/03 16:06:44 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * ECC cipher suite support in OpenSSL originally developed by
114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115 */
116/* ====================================================================
117 * Copyright 2005 Nokia. All rights reserved.
118 *
119 * The portions of the attached software ("Contribution") is developed by
120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121 * license.
122 *
123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125 * support (see RFC 4279) to OpenSSL.
126 *
127 * No patent licenses or other rights except those expressly stated in
128 * the OpenSSL open source license shall be deemed granted or received
129 * expressly, by implication, estoppel, or otherwise.
130 *
131 * No assurances are provided by Nokia that the Contribution does not
132 * infringe the patent or other intellectual property rights of any third
133 * party or that the license provides you with all the necessary rights
134 * to make use of the Contribution.
135 *
136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140 * OTHERWISE.
141 */
142
143#include <stdio.h>
144
145#include <openssl/objects.h>
146#include <openssl/opensslconf.h>
147
148#ifndef OPENSSL_NO_ENGINE
149#include <openssl/engine.h>
150#endif
151
152#include "ssl_locl.h"
153
154#define CIPHER_ADD 1
155#define CIPHER_KILL 2
156#define CIPHER_DEL 3
157#define CIPHER_ORD 4
158#define CIPHER_SPECIAL 5
159
160typedef struct cipher_order_st {
161 const SSL_CIPHER *cipher;
162 int active;
163 int dead;
164 struct cipher_order_st *next, *prev;
165} CIPHER_ORDER;
166
167static const SSL_CIPHER cipher_aliases[] = {
168
169 /* "ALL" doesn't include eNULL (must be specifically enabled) */
170 {
171 .name = SSL_TXT_ALL,
172 .algorithm_enc = ~SSL_eNULL,
173 },
174
175 /* "COMPLEMENTOFALL" */
176 {
177 .name = SSL_TXT_CMPALL,
178 .algorithm_enc = SSL_eNULL,
179 },
180
181 /*
182 * "COMPLEMENTOFDEFAULT"
183 * (does *not* include ciphersuites not found in ALL!)
184 */
185 {
186 .name = SSL_TXT_CMPDEF,
187 .algorithm_mkey = SSL_kDHE|SSL_kECDHE,
188 .algorithm_auth = SSL_aNULL,
189 .algorithm_enc = ~SSL_eNULL,
190 },
191
192 /*
193 * key exchange aliases
194 * (some of those using only a single bit here combine multiple key
195 * exchange algs according to the RFCs, e.g. kEDH combines DHE_DSS
196 * and DHE_RSA)
197 */
198 {
199 .name = SSL_TXT_kRSA,
200 .algorithm_mkey = SSL_kRSA,
201 },
202 {
203 .name = SSL_TXT_kEDH,
204 .algorithm_mkey = SSL_kDHE,
205 },
206 {
207 .name = SSL_TXT_DH,
208 .algorithm_mkey = SSL_kDHE,
209 },
210 {
211 .name = SSL_TXT_kEECDH,
212 .algorithm_mkey = SSL_kECDHE,
213 },
214 {
215 .name = SSL_TXT_ECDH,
216 .algorithm_mkey = SSL_kECDHE,
217 },
218 {
219 .name = SSL_TXT_kGOST,
220 .algorithm_mkey = SSL_kGOST,
221 },
222
223 /* server authentication aliases */
224 {
225 .name = SSL_TXT_aRSA,
226 .algorithm_auth = SSL_aRSA,
227 },
228 {
229 .name = SSL_TXT_aDSS,
230 .algorithm_auth = SSL_aDSS,
231 },
232 {
233 .name = SSL_TXT_DSS,
234 .algorithm_auth = SSL_aDSS,
235 },
236 {
237 .name = SSL_TXT_aNULL,
238 .algorithm_auth = SSL_aNULL,
239 },
240 {
241 .name = SSL_TXT_aECDSA,
242 .algorithm_auth = SSL_aECDSA,
243 },
244 {
245 .name = SSL_TXT_ECDSA,
246 .algorithm_auth = SSL_aECDSA,
247 },
248 {
249 .name = SSL_TXT_aGOST01,
250 .algorithm_auth = SSL_aGOST01,
251 },
252 {
253 .name = SSL_TXT_aGOST,
254 .algorithm_auth = SSL_aGOST01,
255 },
256
257 /* aliases combining key exchange and server authentication */
258 {
259 .name = SSL_TXT_DHE,
260 .algorithm_mkey = SSL_kDHE,
261 .algorithm_auth = ~SSL_aNULL,
262 },
263 {
264 .name = SSL_TXT_EDH,
265 .algorithm_mkey = SSL_kDHE,
266 .algorithm_auth = ~SSL_aNULL,
267 },
268 {
269 .name = SSL_TXT_ECDHE,
270 .algorithm_mkey = SSL_kECDHE,
271 .algorithm_auth = ~SSL_aNULL,
272 },
273 {
274 .name = SSL_TXT_EECDH,
275 .algorithm_mkey = SSL_kECDHE,
276 .algorithm_auth = ~SSL_aNULL,
277 },
278 {
279 .name = SSL_TXT_NULL,
280 .algorithm_enc = SSL_eNULL,
281 },
282 {
283 .name = SSL_TXT_RSA,
284 .algorithm_mkey = SSL_kRSA,
285 .algorithm_auth = SSL_aRSA,
286 },
287 {
288 .name = SSL_TXT_ADH,
289 .algorithm_mkey = SSL_kDHE,
290 .algorithm_auth = SSL_aNULL,
291 },
292 {
293 .name = SSL_TXT_AECDH,
294 .algorithm_mkey = SSL_kECDHE,
295 .algorithm_auth = SSL_aNULL,
296 },
297
298 /* symmetric encryption aliases */
299 {
300 .name = SSL_TXT_3DES,
301 .algorithm_enc = SSL_3DES,
302 },
303 {
304 .name = SSL_TXT_RC4,
305 .algorithm_enc = SSL_RC4,
306 },
307 {
308 .name = SSL_TXT_eNULL,
309 .algorithm_enc = SSL_eNULL,
310 },
311 {
312 .name = SSL_TXT_AES128,
313 .algorithm_enc = SSL_AES128|SSL_AES128GCM,
314 },
315 {
316 .name = SSL_TXT_AES256,
317 .algorithm_enc = SSL_AES256|SSL_AES256GCM,
318 },
319 {
320 .name = SSL_TXT_AES,
321 .algorithm_enc = SSL_AES,
322 },
323 {
324 .name = SSL_TXT_AES_GCM,
325 .algorithm_enc = SSL_AES128GCM|SSL_AES256GCM,
326 },
327 {
328 .name = SSL_TXT_CAMELLIA128,
329 .algorithm_enc = SSL_CAMELLIA128,
330 },
331 {
332 .name = SSL_TXT_CAMELLIA256,
333 .algorithm_enc = SSL_CAMELLIA256,
334 },
335 {
336 .name = SSL_TXT_CAMELLIA,
337 .algorithm_enc = SSL_CAMELLIA128|SSL_CAMELLIA256,
338 },
339 {
340 .name = SSL_TXT_CHACHA20,
341 .algorithm_enc = SSL_CHACHA20POLY1305,
342 },
343
344 /* MAC aliases */
345 {
346 .name = SSL_TXT_AEAD,
347 .algorithm_mac = SSL_AEAD,
348 },
349 {
350 .name = SSL_TXT_MD5,
351 .algorithm_mac = SSL_MD5,
352 },
353 {
354 .name = SSL_TXT_SHA1,
355 .algorithm_mac = SSL_SHA1,
356 },
357 {
358 .name = SSL_TXT_SHA,
359 .algorithm_mac = SSL_SHA1,
360 },
361 {
362 .name = SSL_TXT_GOST94,
363 .algorithm_mac = SSL_GOST94,
364 },
365 {
366 .name = SSL_TXT_GOST89MAC,
367 .algorithm_mac = SSL_GOST89MAC,
368 },
369 {
370 .name = SSL_TXT_SHA256,
371 .algorithm_mac = SSL_SHA256,
372 },
373 {
374 .name = SSL_TXT_SHA384,
375 .algorithm_mac = SSL_SHA384,
376 },
377 {
378 .name = SSL_TXT_STREEBOG256,
379 .algorithm_mac = SSL_STREEBOG256,
380 },
381
382 /* protocol version aliases */
383 {
384 .name = SSL_TXT_SSLV3,
385 .algorithm_ssl = SSL_SSLV3,
386 },
387 {
388 .name = SSL_TXT_TLSV1,
389 .algorithm_ssl = SSL_TLSV1,
390 },
391 {
392 .name = SSL_TXT_TLSV1_2,
393 .algorithm_ssl = SSL_TLSV1_2,
394 },
395 {
396 .name = SSL_TXT_TLSV1_3,
397 .algorithm_ssl = SSL_TLSV1_3,
398 },
399
400 /* cipher suite aliases */
401#ifdef LIBRESSL_HAS_TLS1_3
402 {
403 .valid = 1,
404 .name = "TLS_AES_128_GCM_SHA256",
405 .id = TLS1_3_CK_AES_128_GCM_SHA256,
406 .algorithm_ssl = SSL_TLSV1_3,
407 },
408 {
409 .valid = 1,
410 .name = "TLS_AES_256_GCM_SHA384",
411 .id = TLS1_3_CK_AES_256_GCM_SHA384,
412 .algorithm_ssl = SSL_TLSV1_3,
413 },
414 {
415 .valid = 1,
416 .name = "TLS_CHACHA20_POLY1305_SHA256",
417 .id = TLS1_3_CK_CHACHA20_POLY1305_SHA256,
418 .algorithm_ssl = SSL_TLSV1_3,
419 },
420#endif
421
422 /* strength classes */
423 {
424 .name = SSL_TXT_LOW,
425 .algo_strength = SSL_LOW,
426 },
427 {
428 .name = SSL_TXT_MEDIUM,
429 .algo_strength = SSL_MEDIUM,
430 },
431 {
432 .name = SSL_TXT_HIGH,
433 .algo_strength = SSL_HIGH,
434 },
435};
436
437int
438ssl_cipher_get_evp(const SSL_SESSION *ss, const EVP_CIPHER **enc,
439 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
440{
441 *enc = NULL;
442 *md = NULL;
443 *mac_pkey_type = NID_undef;
444 *mac_secret_size = 0;
445
446 if (ss->cipher == NULL)
447 return 0;
448
449 /*
450 * This function does not handle EVP_AEAD.
451 * See ssl_cipher_get_aead_evp instead.
452 */
453 if (ss->cipher->algorithm_mac & SSL_AEAD)
454 return 0;
455
456 switch (ss->cipher->algorithm_enc) {
457 case SSL_3DES:
458 *enc = EVP_des_ede3_cbc();
459 break;
460 case SSL_RC4:
461 *enc = EVP_rc4();
462 break;
463 case SSL_eNULL:
464 *enc = EVP_enc_null();
465 break;
466 case SSL_AES128:
467 *enc = EVP_aes_128_cbc();
468 break;
469 case SSL_AES256:
470 *enc = EVP_aes_256_cbc();
471 break;
472 case SSL_CAMELLIA128:
473 *enc = EVP_camellia_128_cbc();
474 break;
475 case SSL_CAMELLIA256:
476 *enc = EVP_camellia_256_cbc();
477 break;
478 case SSL_eGOST2814789CNT:
479 *enc = EVP_gost2814789_cnt();
480 break;
481 }
482
483 switch (ss->cipher->algorithm_mac) {
484 case SSL_MD5:
485 *md = EVP_md5();
486 break;
487 case SSL_SHA1:
488 *md = EVP_sha1();
489 break;
490 case SSL_SHA256:
491 *md = EVP_sha256();
492 break;
493 case SSL_SHA384:
494 *md = EVP_sha384();
495 break;
496 case SSL_GOST89MAC:
497 *md = EVP_gost2814789imit();
498 break;
499 case SSL_GOST94:
500 *md = EVP_gostr341194();
501 break;
502 case SSL_STREEBOG256:
503 *md = EVP_streebog256();
504 break;
505 }
506
507 if (*enc == NULL || *md == NULL)
508 return 0;
509
510 /*
511 * EVP_CIPH_FLAG_AEAD_CIPHER and EVP_CIPH_GCM_MODE ciphers are not
512 * supported via EVP_CIPHER (they should be using EVP_AEAD instead).
513 */
514 if (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER)
515 return 0;
516 if (EVP_CIPHER_mode(*enc) == EVP_CIPH_GCM_MODE)
517 return 0;
518
519 if (ss->cipher->algorithm_mac == SSL_GOST89MAC) {
520 *mac_pkey_type = EVP_PKEY_GOSTIMIT;
521 *mac_secret_size = 32; /* XXX */
522 } else {
523 *mac_pkey_type = EVP_PKEY_HMAC;
524 *mac_secret_size = EVP_MD_size(*md);
525 }
526
527 return 1;
528}
529
530/*
531 * ssl_cipher_get_evp_aead sets aead to point to the correct EVP_AEAD object
532 * for s->cipher. It returns 1 on success and 0 on error.
533 */
534int
535ssl_cipher_get_evp_aead(const SSL_SESSION *ss, const EVP_AEAD **aead)
536{
537 *aead = NULL;
538
539 if (ss->cipher == NULL)
540 return 0;
541 if ((ss->cipher->algorithm_mac & SSL_AEAD) == 0)
542 return 0;
543
544 switch (ss->cipher->algorithm_enc) {
545 case SSL_AES128GCM:
546 *aead = EVP_aead_aes_128_gcm();
547 return 1;
548 case SSL_AES256GCM:
549 *aead = EVP_aead_aes_256_gcm();
550 return 1;
551 case SSL_CHACHA20POLY1305:
552 *aead = EVP_aead_chacha20_poly1305();
553 return 1;
554 default:
555 break;
556 }
557 return 0;
558}
559
560int
561ssl_get_handshake_evp_md(SSL *s, const EVP_MD **md)
562{
563 unsigned long handshake_mac;
564
565 *md = NULL;
566
567 if (S3I(s)->hs.cipher == NULL)
568 return 0;
569
570 handshake_mac = S3I(s)->hs.cipher->algorithm2 &
571 SSL_HANDSHAKE_MAC_MASK;
572
573 /* For TLSv1.2 we upgrade the default MD5+SHA1 MAC to SHA256. */
574 if (SSL_USE_SHA256_PRF(s) && handshake_mac == SSL_HANDSHAKE_MAC_DEFAULT)
575 handshake_mac = SSL_HANDSHAKE_MAC_SHA256;
576
577 switch (handshake_mac) {
578 case SSL_HANDSHAKE_MAC_DEFAULT:
579 *md = EVP_md5_sha1();
580 return 1;
581 case SSL_HANDSHAKE_MAC_GOST94:
582 *md = EVP_gostr341194();
583 return 1;
584 case SSL_HANDSHAKE_MAC_SHA256:
585 *md = EVP_sha256();
586 return 1;
587 case SSL_HANDSHAKE_MAC_SHA384:
588 *md = EVP_sha384();
589 return 1;
590 case SSL_HANDSHAKE_MAC_STREEBOG256:
591 *md = EVP_streebog256();
592 return 1;
593 default:
594 break;
595 }
596
597 return 0;
598}
599
600#define ITEM_SEP(a) \
601 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
602
603static void
604ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
605 CIPHER_ORDER **tail)
606{
607 if (curr == *tail)
608 return;
609 if (curr == *head)
610 *head = curr->next;
611 if (curr->prev != NULL)
612 curr->prev->next = curr->next;
613 if (curr->next != NULL)
614 curr->next->prev = curr->prev;
615 (*tail)->next = curr;
616 curr->prev= *tail;
617 curr->next = NULL;
618 *tail = curr;
619}
620
621static void
622ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
623 CIPHER_ORDER **tail)
624{
625 if (curr == *head)
626 return;
627 if (curr == *tail)
628 *tail = curr->prev;
629 if (curr->next != NULL)
630 curr->next->prev = curr->prev;
631 if (curr->prev != NULL)
632 curr->prev->next = curr->next;
633 (*head)->prev = curr;
634 curr->next= *head;
635 curr->prev = NULL;
636 *head = curr;
637}
638
639static void
640ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth,
641 unsigned long *enc, unsigned long *mac, unsigned long *ssl)
642{
643 *mkey = 0;
644 *auth = 0;
645 *enc = 0;
646 *mac = 0;
647 *ssl = 0;
648
649 /*
650 * Check for the availability of GOST 34.10 public/private key
651 * algorithms. If they are not available disable the associated
652 * authentication and key exchange algorithms.
653 */
654 if (EVP_PKEY_meth_find(NID_id_GostR3410_2001) == NULL) {
655 *auth |= SSL_aGOST01;
656 *mkey |= SSL_kGOST;
657 }
658
659#ifdef SSL_FORBID_ENULL
660 *enc |= SSL_eNULL;
661#endif
662}
663
664static void
665ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, int num_of_ciphers,
666 unsigned long disabled_mkey, unsigned long disabled_auth,
667 unsigned long disabled_enc, unsigned long disabled_mac,
668 unsigned long disabled_ssl, CIPHER_ORDER *co_list,
669 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
670{
671 int i, co_list_num;
672 const SSL_CIPHER *c;
673
674 /*
675 * We have num_of_ciphers descriptions compiled in, depending on the
676 * method selected (SSLv3, TLSv1, etc). These will later be sorted in
677 * a linked list with at most num entries.
678 */
679
680 /* Get the initial list of ciphers */
681 co_list_num = 0; /* actual count of ciphers */
682 for (i = 0; i < num_of_ciphers; i++) {
683 c = ssl_method->get_cipher(i);
684 /* drop those that use any of that is not available */
685 if ((c != NULL) && c->valid &&
686 !(c->algorithm_mkey & disabled_mkey) &&
687 !(c->algorithm_auth & disabled_auth) &&
688 !(c->algorithm_enc & disabled_enc) &&
689 !(c->algorithm_mac & disabled_mac) &&
690 !(c->algorithm_ssl & disabled_ssl)) {
691 co_list[co_list_num].cipher = c;
692 co_list[co_list_num].next = NULL;
693 co_list[co_list_num].prev = NULL;
694 co_list[co_list_num].active = 0;
695 co_list_num++;
696 /*
697 if (!sk_push(ca_list,(char *)c)) goto err;
698 */
699 }
700 }
701
702 /*
703 * Prepare linked list from list entries
704 */
705 if (co_list_num > 0) {
706 co_list[0].prev = NULL;
707
708 if (co_list_num > 1) {
709 co_list[0].next = &co_list[1];
710
711 for (i = 1; i < co_list_num - 1; i++) {
712 co_list[i].prev = &co_list[i - 1];
713 co_list[i].next = &co_list[i + 1];
714 }
715
716 co_list[co_list_num - 1].prev =
717 &co_list[co_list_num - 2];
718 }
719
720 co_list[co_list_num - 1].next = NULL;
721
722 *head_p = &co_list[0];
723 *tail_p = &co_list[co_list_num - 1];
724 }
725}
726
727static void
728ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list, int num_of_group_aliases,
729 unsigned long disabled_mkey, unsigned long disabled_auth,
730 unsigned long disabled_enc, unsigned long disabled_mac,
731 unsigned long disabled_ssl, CIPHER_ORDER *head)
732{
733 CIPHER_ORDER *ciph_curr;
734 const SSL_CIPHER **ca_curr;
735 int i;
736 unsigned long mask_mkey = ~disabled_mkey;
737 unsigned long mask_auth = ~disabled_auth;
738 unsigned long mask_enc = ~disabled_enc;
739 unsigned long mask_mac = ~disabled_mac;
740 unsigned long mask_ssl = ~disabled_ssl;
741
742 /*
743 * First, add the real ciphers as already collected
744 */
745 ciph_curr = head;
746 ca_curr = ca_list;
747 while (ciph_curr != NULL) {
748 *ca_curr = ciph_curr->cipher;
749 ca_curr++;
750 ciph_curr = ciph_curr->next;
751 }
752
753 /*
754 * Now we add the available ones from the cipher_aliases[] table.
755 * They represent either one or more algorithms, some of which
756 * in any affected category must be supported (set in enabled_mask),
757 * or represent a cipher strength value (will be added in any case because algorithms=0).
758 */
759 for (i = 0; i < num_of_group_aliases; i++) {
760 unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
761 unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
762 unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
763 unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
764 unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
765
766 if (algorithm_mkey)
767 if ((algorithm_mkey & mask_mkey) == 0)
768 continue;
769
770 if (algorithm_auth)
771 if ((algorithm_auth & mask_auth) == 0)
772 continue;
773
774 if (algorithm_enc)
775 if ((algorithm_enc & mask_enc) == 0)
776 continue;
777
778 if (algorithm_mac)
779 if ((algorithm_mac & mask_mac) == 0)
780 continue;
781
782 if (algorithm_ssl)
783 if ((algorithm_ssl & mask_ssl) == 0)
784 continue;
785
786 *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
787 ca_curr++;
788 }
789
790 *ca_curr = NULL; /* end of list */
791}
792
793static void
794ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long alg_mkey,
795 unsigned long alg_auth, unsigned long alg_enc, unsigned long alg_mac,
796 unsigned long alg_ssl, unsigned long algo_strength, int rule,
797 int strength_bits, CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
798{
799 CIPHER_ORDER *head, *tail, *curr, *next, *last;
800 const SSL_CIPHER *cp;
801 int reverse = 0;
802
803 if (rule == CIPHER_DEL)
804 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
805
806 head = *head_p;
807 tail = *tail_p;
808
809 if (reverse) {
810 next = tail;
811 last = head;
812 } else {
813 next = head;
814 last = tail;
815 }
816
817 curr = NULL;
818 for (;;) {
819 if (curr == last)
820 break;
821 curr = next;
822 next = reverse ? curr->prev : curr->next;
823
824 cp = curr->cipher;
825
826 if (cipher_id && cp->id != cipher_id)
827 continue;
828
829 /*
830 * Selection criteria is either the value of strength_bits
831 * or the algorithms used.
832 */
833 if (strength_bits >= 0) {
834 if (strength_bits != cp->strength_bits)
835 continue;
836 } else {
837 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
838 continue;
839 if (alg_auth && !(alg_auth & cp->algorithm_auth))
840 continue;
841 if (alg_enc && !(alg_enc & cp->algorithm_enc))
842 continue;
843 if (alg_mac && !(alg_mac & cp->algorithm_mac))
844 continue;
845 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
846 continue;
847 if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
848 continue;
849 }
850
851 /* add the cipher if it has not been added yet. */
852 if (rule == CIPHER_ADD) {
853 /* reverse == 0 */
854 if (!curr->active) {
855 ll_append_tail(&head, curr, &tail);
856 curr->active = 1;
857 }
858 }
859 /* Move the added cipher to this location */
860 else if (rule == CIPHER_ORD) {
861 /* reverse == 0 */
862 if (curr->active) {
863 ll_append_tail(&head, curr, &tail);
864 }
865 } else if (rule == CIPHER_DEL) {
866 /* reverse == 1 */
867 if (curr->active) {
868 /* most recently deleted ciphersuites get best positions
869 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
870 * works in reverse to maintain the order) */
871 ll_append_head(&head, curr, &tail);
872 curr->active = 0;
873 }
874 } else if (rule == CIPHER_KILL) {
875 /* reverse == 0 */
876 if (head == curr)
877 head = curr->next;
878 else
879 curr->prev->next = curr->next;
880 if (tail == curr)
881 tail = curr->prev;
882 curr->active = 0;
883 if (curr->next != NULL)
884 curr->next->prev = curr->prev;
885 if (curr->prev != NULL)
886 curr->prev->next = curr->next;
887 curr->next = NULL;
888 curr->prev = NULL;
889 }
890 }
891
892 *head_p = head;
893 *tail_p = tail;
894}
895
896static int
897ssl_cipher_strength_sort(CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
898{
899 int max_strength_bits, i, *number_uses;
900 CIPHER_ORDER *curr;
901
902 /*
903 * This routine sorts the ciphers with descending strength. The sorting
904 * must keep the pre-sorted sequence, so we apply the normal sorting
905 * routine as '+' movement to the end of the list.
906 */
907 max_strength_bits = 0;
908 curr = *head_p;
909 while (curr != NULL) {
910 if (curr->active &&
911 (curr->cipher->strength_bits > max_strength_bits))
912 max_strength_bits = curr->cipher->strength_bits;
913 curr = curr->next;
914 }
915
916 number_uses = calloc((max_strength_bits + 1), sizeof(int));
917 if (!number_uses) {
918 SSLerrorx(ERR_R_MALLOC_FAILURE);
919 return (0);
920 }
921
922 /*
923 * Now find the strength_bits values actually used
924 */
925 curr = *head_p;
926 while (curr != NULL) {
927 if (curr->active)
928 number_uses[curr->cipher->strength_bits]++;
929 curr = curr->next;
930 }
931 /*
932 * Go through the list of used strength_bits values in descending
933 * order.
934 */
935 for (i = max_strength_bits; i >= 0; i--)
936 if (number_uses[i] > 0)
937 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p, tail_p);
938
939 free(number_uses);
940 return (1);
941}
942
943static int
944ssl_cipher_process_rulestr(const char *rule_str, CIPHER_ORDER **head_p,
945 CIPHER_ORDER **tail_p, const SSL_CIPHER **ca_list, int *tls13_seen)
946{
947 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
948 unsigned long algo_strength;
949 int j, multi, found, rule, retval, ok, buflen;
950 unsigned long cipher_id = 0;
951 const char *l, *buf;
952 char ch;
953
954 *tls13_seen = 0;
955
956 retval = 1;
957 l = rule_str;
958 for (;;) {
959 ch = *l;
960
961 if (ch == '\0')
962 break;
963
964 if (ch == '-') {
965 rule = CIPHER_DEL;
966 l++;
967 } else if (ch == '+') {
968 rule = CIPHER_ORD;
969 l++;
970 } else if (ch == '!') {
971 rule = CIPHER_KILL;
972 l++;
973 } else if (ch == '@') {
974 rule = CIPHER_SPECIAL;
975 l++;
976 } else {
977 rule = CIPHER_ADD;
978 }
979
980 if (ITEM_SEP(ch)) {
981 l++;
982 continue;
983 }
984
985 alg_mkey = 0;
986 alg_auth = 0;
987 alg_enc = 0;
988 alg_mac = 0;
989 alg_ssl = 0;
990 algo_strength = 0;
991
992 for (;;) {
993 ch = *l;
994 buf = l;
995 buflen = 0;
996 while (((ch >= 'A') && (ch <= 'Z')) ||
997 ((ch >= '0') && (ch <= '9')) ||
998 ((ch >= 'a') && (ch <= 'z')) ||
999 (ch == '-') || (ch == '.') ||
1000 (ch == '_')) {
1001 ch = *(++l);
1002 buflen++;
1003 }
1004
1005 if (buflen == 0) {
1006 /*
1007 * We hit something we cannot deal with,
1008 * it is no command or separator nor
1009 * alphanumeric, so we call this an error.
1010 */
1011 SSLerrorx(SSL_R_INVALID_COMMAND);
1012 retval = found = 0;
1013 l++;
1014 break;
1015 }
1016
1017 if (rule == CIPHER_SPECIAL) {
1018 /* unused -- avoid compiler warning */
1019 found = 0;
1020 /* special treatment */
1021 break;
1022 }
1023
1024 /* check for multi-part specification */
1025 if (ch == '+') {
1026 multi = 1;
1027 l++;
1028 } else
1029 multi = 0;
1030
1031 /*
1032 * Now search for the cipher alias in the ca_list.
1033 * Be careful with the strncmp, because the "buflen"
1034 * limitation will make the rule "ADH:SOME" and the
1035 * cipher "ADH-MY-CIPHER" look like a match for
1036 * buflen=3. So additionally check whether the cipher
1037 * name found has the correct length. We can save a
1038 * strlen() call: just checking for the '\0' at the
1039 * right place is sufficient, we have to strncmp()
1040 * anyway (we cannot use strcmp(), because buf is not
1041 * '\0' terminated.)
1042 */
1043 j = found = 0;
1044 cipher_id = 0;
1045 while (ca_list[j]) {
1046 if (!strncmp(buf, ca_list[j]->name, buflen) &&
1047 (ca_list[j]->name[buflen] == '\0')) {
1048 found = 1;
1049 break;
1050 } else
1051 j++;
1052 }
1053
1054 if (!found)
1055 break; /* ignore this entry */
1056
1057 if (ca_list[j]->algorithm_mkey) {
1058 if (alg_mkey) {
1059 alg_mkey &= ca_list[j]->algorithm_mkey;
1060 if (!alg_mkey) {
1061 found = 0;
1062 break;
1063 }
1064 } else
1065 alg_mkey = ca_list[j]->algorithm_mkey;
1066 }
1067
1068 if (ca_list[j]->algorithm_auth) {
1069 if (alg_auth) {
1070 alg_auth &= ca_list[j]->algorithm_auth;
1071 if (!alg_auth) {
1072 found = 0;
1073 break;
1074 }
1075 } else
1076 alg_auth = ca_list[j]->algorithm_auth;
1077 }
1078
1079 if (ca_list[j]->algorithm_enc) {
1080 if (alg_enc) {
1081 alg_enc &= ca_list[j]->algorithm_enc;
1082 if (!alg_enc) {
1083 found = 0;
1084 break;
1085 }
1086 } else
1087 alg_enc = ca_list[j]->algorithm_enc;
1088 }
1089
1090 if (ca_list[j]->algorithm_mac) {
1091 if (alg_mac) {
1092 alg_mac &= ca_list[j]->algorithm_mac;
1093 if (!alg_mac) {
1094 found = 0;
1095 break;
1096 }
1097 } else
1098 alg_mac = ca_list[j]->algorithm_mac;
1099 }
1100
1101 if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1102 if (algo_strength & SSL_STRONG_MASK) {
1103 algo_strength &=
1104 (ca_list[j]->algo_strength &
1105 SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1106 if (!(algo_strength &
1107 SSL_STRONG_MASK)) {
1108 found = 0;
1109 break;
1110 }
1111 } else
1112 algo_strength |=
1113 ca_list[j]->algo_strength &
1114 SSL_STRONG_MASK;
1115 }
1116
1117 if (ca_list[j]->valid) {
1118 /*
1119 * explicit ciphersuite found; its protocol
1120 * version does not become part of the search
1121 * pattern!
1122 */
1123 cipher_id = ca_list[j]->id;
1124 if (ca_list[j]->algorithm_ssl == SSL_TLSV1_3)
1125 *tls13_seen = 1;
1126 } else {
1127 /*
1128 * not an explicit ciphersuite; only in this
1129 * case, the protocol version is considered
1130 * part of the search pattern
1131 */
1132 if (ca_list[j]->algorithm_ssl) {
1133 if (alg_ssl) {
1134 alg_ssl &=
1135 ca_list[j]->algorithm_ssl;
1136 if (!alg_ssl) {
1137 found = 0;
1138 break;
1139 }
1140 } else
1141 alg_ssl =
1142 ca_list[j]->algorithm_ssl;
1143 }
1144 }
1145
1146 if (!multi)
1147 break;
1148 }
1149
1150 /*
1151 * Ok, we have the rule, now apply it
1152 */
1153 if (rule == CIPHER_SPECIAL) {
1154 /* special command */
1155 ok = 0;
1156 if ((buflen == 8) && !strncmp(buf, "STRENGTH", 8))
1157 ok = ssl_cipher_strength_sort(head_p, tail_p);
1158 else
1159 SSLerrorx(SSL_R_INVALID_COMMAND);
1160 if (ok == 0)
1161 retval = 0;
1162 /*
1163 * We do not support any "multi" options
1164 * together with "@", so throw away the
1165 * rest of the command, if any left, until
1166 * end or ':' is found.
1167 */
1168 while ((*l != '\0') && !ITEM_SEP(*l))
1169 l++;
1170 } else if (found) {
1171 if (alg_ssl == SSL_TLSV1_3)
1172 *tls13_seen = 1;
1173 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth,
1174 alg_enc, alg_mac, alg_ssl, algo_strength, rule,
1175 -1, head_p, tail_p);
1176 } else {
1177 while ((*l != '\0') && !ITEM_SEP(*l))
1178 l++;
1179 }
1180 if (*l == '\0')
1181 break; /* done */
1182 }
1183
1184 return (retval);
1185}
1186
1187static inline int
1188ssl_aes_is_accelerated(void)
1189{
1190#if defined(__i386__) || defined(__x86_64__)
1191 return ((OPENSSL_cpu_caps() & (1ULL << 57)) != 0);
1192#else
1193 return (0);
1194#endif
1195}
1196
1197STACK_OF(SSL_CIPHER) *
1198ssl_create_cipher_list(const SSL_METHOD *ssl_method,
1199 STACK_OF(SSL_CIPHER) **cipher_list,
1200 STACK_OF(SSL_CIPHER) *cipher_list_tls13,
1201 const char *rule_str)
1202{
1203 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1204 unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
1205 STACK_OF(SSL_CIPHER) *cipherstack;
1206 const char *rule_p;
1207 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1208 const SSL_CIPHER **ca_list = NULL;
1209 const SSL_CIPHER *cipher;
1210 int tls13_seen = 0;
1211 int any_active;
1212 int i;
1213
1214 /*
1215 * Return with error if nothing to do.
1216 */
1217 if (rule_str == NULL || cipher_list == NULL)
1218 return NULL;
1219
1220 /*
1221 * To reduce the work to do we only want to process the compiled
1222 * in algorithms, so we first get the mask of disabled ciphers.
1223 */
1224 ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1225
1226 /*
1227 * Now we have to collect the available ciphers from the compiled
1228 * in ciphers. We cannot get more than the number compiled in, so
1229 * it is used for allocation.
1230 */
1231 num_of_ciphers = ssl3_num_ciphers();
1232 co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER));
1233 if (co_list == NULL) {
1234 SSLerrorx(ERR_R_MALLOC_FAILURE);
1235 return(NULL); /* Failure */
1236 }
1237
1238 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1239 disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1240 co_list, &head, &tail);
1241
1242
1243 /* Now arrange all ciphers by preference: */
1244
1245 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
1246 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1247 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1248
1249 if (ssl_aes_is_accelerated()) {
1250 /*
1251 * We have hardware assisted AES - prefer AES as a symmetric
1252 * cipher, with CHACHA20 second.
1253 */
1254 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1255 CIPHER_ADD, -1, &head, &tail);
1256 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1257 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1258 } else {
1259 /*
1260 * CHACHA20 is fast and safe on all hardware and is thus our
1261 * preferred symmetric cipher, with AES second.
1262 */
1263 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1264 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1265 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1266 CIPHER_ADD, -1, &head, &tail);
1267 }
1268
1269 /* Temporarily enable everything else for sorting */
1270 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1271
1272 /* Low priority for MD5 */
1273 ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
1274
1275 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1276 * (For applications that allow them, they aren't too bad, but we prefer
1277 * authenticated ciphers.) */
1278 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1279
1280 /* Move ciphers without forward secrecy to the end */
1281 ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1282
1283 /* RC4 is sort of broken - move it to the end */
1284 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1285
1286 /* Now sort by symmetric encryption strength. The above ordering remains
1287 * in force within each class */
1288 if (!ssl_cipher_strength_sort(&head, &tail)) {
1289 free(co_list);
1290 return NULL;
1291 }
1292
1293 /* Now disable everything (maintaining the ordering!) */
1294 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1295
1296 /* TLSv1.3 first. */
1297 ssl_cipher_apply_rule(0, 0, 0, 0, 0, SSL_TLSV1_3, 0, CIPHER_ADD, -1, &head, &tail);
1298 ssl_cipher_apply_rule(0, 0, 0, 0, 0, SSL_TLSV1_3, 0, CIPHER_DEL, -1, &head, &tail);
1299
1300 /*
1301 * We also need cipher aliases for selecting based on the rule_str.
1302 * There might be two types of entries in the rule_str: 1) names
1303 * of ciphers themselves 2) aliases for groups of ciphers.
1304 * For 1) we need the available ciphers and for 2) the cipher
1305 * groups of cipher_aliases added together in one list (otherwise
1306 * we would be happy with just the cipher_aliases table).
1307 */
1308 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1309 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1310 ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *));
1311 if (ca_list == NULL) {
1312 free(co_list);
1313 SSLerrorx(ERR_R_MALLOC_FAILURE);
1314 return(NULL); /* Failure */
1315 }
1316 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mkey,
1317 disabled_auth, disabled_enc, disabled_mac, disabled_ssl, head);
1318
1319 /*
1320 * If the rule_string begins with DEFAULT, apply the default rule
1321 * before using the (possibly available) additional rules.
1322 */
1323 ok = 1;
1324 rule_p = rule_str;
1325 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1326 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1327 &head, &tail, ca_list, &tls13_seen);
1328 rule_p += 7;
1329 if (*rule_p == ':')
1330 rule_p++;
1331 }
1332
1333 if (ok && (strlen(rule_p) > 0))
1334 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list,
1335 &tls13_seen);
1336
1337 free((void *)ca_list); /* Not needed anymore */
1338
1339 if (!ok) {
1340 /* Rule processing failure */
1341 free(co_list);
1342 return (NULL);
1343 }
1344
1345 /*
1346 * Allocate new "cipherstack" for the result, return with error
1347 * if we cannot get one.
1348 */
1349 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1350 free(co_list);
1351 return (NULL);
1352 }
1353
1354 /* Prefer TLSv1.3 cipher suites. */
1355 if (cipher_list_tls13 != NULL) {
1356 for (i = 0; i < sk_SSL_CIPHER_num(cipher_list_tls13); i++) {
1357 cipher = sk_SSL_CIPHER_value(cipher_list_tls13, i);
1358 sk_SSL_CIPHER_push(cipherstack, cipher);
1359 }
1360 tls13_seen = 1;
1361 }
1362
1363 /*
1364 * The cipher selection for the list is done. The ciphers are added
1365 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1366 *
1367 * If the rule string did not contain any references to TLSv1.3 and
1368 * TLSv1.3 cipher suites have not been configured separately,
1369 * include inactive TLSv1.3 cipher suites. This avoids attempts to
1370 * use TLSv1.3 with an older rule string that does not include
1371 * TLSv1.3 cipher suites. If the rule string resulted in no active
1372 * cipher suites then we return an empty stack.
1373 */
1374 any_active = 0;
1375 for (curr = head; curr != NULL; curr = curr->next) {
1376 if (curr->active ||
1377 (!tls13_seen && curr->cipher->algorithm_ssl == SSL_TLSV1_3))
1378 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1379 any_active |= curr->active;
1380 }
1381 if (!any_active)
1382 sk_SSL_CIPHER_zero(cipherstack);
1383
1384 free(co_list); /* Not needed any longer */
1385
1386 sk_SSL_CIPHER_free(*cipher_list);
1387 *cipher_list = cipherstack;
1388
1389 return (cipherstack);
1390}
1391
1392const SSL_CIPHER *
1393SSL_CIPHER_get_by_id(unsigned int id)
1394{
1395 return ssl3_get_cipher_by_id(id);
1396}
1397
1398const SSL_CIPHER *
1399SSL_CIPHER_get_by_value(uint16_t value)
1400{
1401 return ssl3_get_cipher_by_value(value);
1402}
1403
1404char *
1405SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1406{
1407 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, alg2;
1408 const char *ver, *kx, *au, *enc, *mac;
1409 char *ret;
1410 int l;
1411
1412 alg_mkey = cipher->algorithm_mkey;
1413 alg_auth = cipher->algorithm_auth;
1414 alg_enc = cipher->algorithm_enc;
1415 alg_mac = cipher->algorithm_mac;
1416 alg_ssl = cipher->algorithm_ssl;
1417
1418 alg2 = cipher->algorithm2;
1419
1420 if (alg_ssl & SSL_SSLV3)
1421 ver = "SSLv3";
1422 else if (alg_ssl & SSL_TLSV1_2)
1423 ver = "TLSv1.2";
1424 else if (alg_ssl & SSL_TLSV1_3)
1425 ver = "TLSv1.3";
1426 else
1427 ver = "unknown";
1428
1429 switch (alg_mkey) {
1430 case SSL_kRSA:
1431 kx = "RSA";
1432 break;
1433 case SSL_kDHE:
1434 kx = "DH";
1435 break;
1436 case SSL_kECDHE:
1437 kx = "ECDH";
1438 break;
1439 case SSL_kGOST:
1440 kx = "GOST";
1441 break;
1442 case SSL_kTLS1_3:
1443 kx = "TLSv1.3";
1444 break;
1445 default:
1446 kx = "unknown";
1447 }
1448
1449 switch (alg_auth) {
1450 case SSL_aRSA:
1451 au = "RSA";
1452 break;
1453 case SSL_aDSS:
1454 au = "DSS";
1455 break;
1456 case SSL_aNULL:
1457 au = "None";
1458 break;
1459 case SSL_aECDSA:
1460 au = "ECDSA";
1461 break;
1462 case SSL_aGOST01:
1463 au = "GOST01";
1464 break;
1465 case SSL_aTLS1_3:
1466 au = "TLSv1.3";
1467 break;
1468 default:
1469 au = "unknown";
1470 break;
1471 }
1472
1473 switch (alg_enc) {
1474 case SSL_3DES:
1475 enc = "3DES(168)";
1476 break;
1477 case SSL_RC4:
1478 enc = alg2 & SSL2_CF_8_BYTE_ENC ? "RC4(64)" : "RC4(128)";
1479 break;
1480 case SSL_eNULL:
1481 enc = "None";
1482 break;
1483 case SSL_AES128:
1484 enc = "AES(128)";
1485 break;
1486 case SSL_AES256:
1487 enc = "AES(256)";
1488 break;
1489 case SSL_AES128GCM:
1490 enc = "AESGCM(128)";
1491 break;
1492 case SSL_AES256GCM:
1493 enc = "AESGCM(256)";
1494 break;
1495 case SSL_CAMELLIA128:
1496 enc = "Camellia(128)";
1497 break;
1498 case SSL_CAMELLIA256:
1499 enc = "Camellia(256)";
1500 break;
1501 case SSL_CHACHA20POLY1305:
1502 enc = "ChaCha20-Poly1305";
1503 break;
1504 case SSL_eGOST2814789CNT:
1505 enc = "GOST-28178-89-CNT";
1506 break;
1507 default:
1508 enc = "unknown";
1509 break;
1510 }
1511
1512 switch (alg_mac) {
1513 case SSL_MD5:
1514 mac = "MD5";
1515 break;
1516 case SSL_SHA1:
1517 mac = "SHA1";
1518 break;
1519 case SSL_SHA256:
1520 mac = "SHA256";
1521 break;
1522 case SSL_SHA384:
1523 mac = "SHA384";
1524 break;
1525 case SSL_AEAD:
1526 mac = "AEAD";
1527 break;
1528 case SSL_GOST94:
1529 mac = "GOST94";
1530 break;
1531 case SSL_GOST89MAC:
1532 mac = "GOST89IMIT";
1533 break;
1534 case SSL_STREEBOG256:
1535 mac = "STREEBOG256";
1536 break;
1537 default:
1538 mac = "unknown";
1539 break;
1540 }
1541
1542 if (asprintf(&ret, "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1543 cipher->name, ver, kx, au, enc, mac) == -1)
1544 return "OPENSSL_malloc Error";
1545
1546 if (buf != NULL) {
1547 l = strlcpy(buf, ret, len);
1548 free(ret);
1549 ret = buf;
1550 if (l >= len)
1551 ret = "Buffer too small";
1552 }
1553
1554 return (ret);
1555}
1556
1557const char *
1558SSL_CIPHER_get_version(const SSL_CIPHER *c)
1559{
1560 if (c == NULL)
1561 return("(NONE)");
1562 if ((c->id >> 24) == 3)
1563 return("TLSv1/SSLv3");
1564 else
1565 return("unknown");
1566}
1567
1568/* return the actual cipher being used */
1569const char *
1570SSL_CIPHER_get_name(const SSL_CIPHER *c)
1571{
1572 if (c != NULL)
1573 return (c->name);
1574 return("(NONE)");
1575}
1576
1577/* number of bits for symmetric cipher */
1578int
1579SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1580{
1581 int ret = 0;
1582
1583 if (c != NULL) {
1584 if (alg_bits != NULL)
1585 *alg_bits = c->alg_bits;
1586 ret = c->strength_bits;
1587 }
1588 return (ret);
1589}
1590
1591unsigned long
1592SSL_CIPHER_get_id(const SSL_CIPHER *c)
1593{
1594 return c->id;
1595}
1596
1597uint16_t
1598SSL_CIPHER_get_value(const SSL_CIPHER *c)
1599{
1600 return ssl3_cipher_get_value(c);
1601}
1602
1603const SSL_CIPHER *
1604SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
1605{
1606 uint16_t cipher_value;
1607 CBS cbs;
1608
1609 /* This API is documented with ptr being an array of length two. */
1610 CBS_init(&cbs, ptr, 2);
1611 if (!CBS_get_u16(&cbs, &cipher_value))
1612 return NULL;
1613
1614 return ssl3_get_cipher_by_value(cipher_value);
1615}
1616
1617int
1618SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
1619{
1620 switch (c->algorithm_enc) {
1621 case SSL_eNULL:
1622 return NID_undef;
1623 case SSL_3DES:
1624 return NID_des_ede3_cbc;
1625 case SSL_AES128:
1626 return NID_aes_128_cbc;
1627 case SSL_AES128GCM:
1628 return NID_aes_128_gcm;
1629 case SSL_AES256:
1630 return NID_aes_256_cbc;
1631 case SSL_AES256GCM:
1632 return NID_aes_256_gcm;
1633 case SSL_CAMELLIA128:
1634 return NID_camellia_128_cbc;
1635 case SSL_CAMELLIA256:
1636 return NID_camellia_256_cbc;
1637 case SSL_CHACHA20POLY1305:
1638 return NID_chacha20_poly1305;
1639 case SSL_DES:
1640 return NID_des_cbc;
1641 case SSL_RC4:
1642 return NID_rc4;
1643 case SSL_eGOST2814789CNT:
1644 return NID_gost89_cnt;
1645 default:
1646 return NID_undef;
1647 }
1648}
1649
1650int
1651SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
1652{
1653 switch (c->algorithm_mac) {
1654 case SSL_AEAD:
1655 return NID_undef;
1656 case SSL_GOST89MAC:
1657 return NID_id_Gost28147_89_MAC;
1658 case SSL_GOST94:
1659 return NID_id_GostR3411_94;
1660 case SSL_MD5:
1661 return NID_md5;
1662 case SSL_SHA1:
1663 return NID_sha1;
1664 case SSL_SHA256:
1665 return NID_sha256;
1666 case SSL_SHA384:
1667 return NID_sha384;
1668 case SSL_STREEBOG256:
1669 return NID_id_tc26_gost3411_2012_256;
1670 default:
1671 return NID_undef;
1672 }
1673}
1674
1675int
1676SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
1677{
1678 switch (c->algorithm_mkey) {
1679 case SSL_kDHE:
1680 return NID_kx_dhe;
1681 case SSL_kECDHE:
1682 return NID_kx_ecdhe;
1683 case SSL_kGOST:
1684 return NID_kx_gost;
1685 case SSL_kRSA:
1686 return NID_kx_rsa;
1687 default:
1688 return NID_undef;
1689 }
1690}
1691
1692int
1693SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
1694{
1695 switch (c->algorithm_auth) {
1696 case SSL_aNULL:
1697 return NID_auth_null;
1698 case SSL_aECDSA:
1699 return NID_auth_ecdsa;
1700 case SSL_aGOST01:
1701 return NID_auth_gost01;
1702 case SSL_aRSA:
1703 return NID_auth_rsa;
1704 default:
1705 return NID_undef;
1706 }
1707}
1708
1709int
1710SSL_CIPHER_is_aead(const SSL_CIPHER *c)
1711{
1712 return (c->algorithm_mac & SSL_AEAD) == SSL_AEAD;
1713}
1714
1715void *
1716SSL_COMP_get_compression_methods(void)
1717{
1718 return NULL;
1719}
1720
1721int
1722SSL_COMP_add_compression_method(int id, void *cm)
1723{
1724 return 1;
1725}
1726
1727const char *
1728SSL_COMP_get_name(const void *comp)
1729{
1730 return NULL;
1731}