1 /* $OpenBSD: ssl_ciph.c,v 1.142 2024/05/09 07:55:48 tb 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/evp.h>
146 #include <openssl/objects.h>
147 #include <openssl/opensslconf.h>
148
149 #include "ssl_local.h"
150
151 #define CIPHER_ADD 1
152 #define CIPHER_KILL 2
153 #define CIPHER_DEL 3
154 #define CIPHER_ORD 4
155 #define CIPHER_SPECIAL 5
156
157 typedef struct cipher_order_st {
158 const SSL_CIPHER *cipher;
159 int active;
160 int dead;
161 struct cipher_order_st *next, *prev;
162 } CIPHER_ORDER;
163
164 static const SSL_CIPHER cipher_aliases[] = {
165
166 /* "ALL" doesn't include eNULL (must be specifically enabled) */
167 {
168 .name = SSL_TXT_ALL,
169 .algorithm_enc = ~SSL_eNULL,
170 },
171
172 /* "COMPLEMENTOFALL" */
173 {
174 .name = SSL_TXT_CMPALL,
175 .algorithm_enc = SSL_eNULL,
176 },
177
178 /*
179 * "COMPLEMENTOFDEFAULT"
180 * (does *not* include ciphersuites not found in ALL!)
181 */
182 {
183 .name = SSL_TXT_CMPDEF,
184 .algorithm_mkey = SSL_kDHE|SSL_kECDHE,
185 .algorithm_auth = SSL_aNULL,
186 .algorithm_enc = ~SSL_eNULL,
187 },
188
189 /*
190 * key exchange aliases
191 * (some of those using only a single bit here combine multiple key
192 * exchange algs according to the RFCs, e.g. kEDH combines DHE_DSS
193 * and DHE_RSA)
194 */
195 {
196 .name = SSL_TXT_kRSA,
197 .algorithm_mkey = SSL_kRSA,
198 },
199 {
200 .name = SSL_TXT_kEDH,
201 .algorithm_mkey = SSL_kDHE,
202 },
203 {
204 .name = SSL_TXT_DH,
205 .algorithm_mkey = SSL_kDHE,
206 },
207 {
208 .name = SSL_TXT_kEECDH,
209 .algorithm_mkey = SSL_kECDHE,
210 },
211 {
212 .name = SSL_TXT_ECDH,
213 .algorithm_mkey = SSL_kECDHE,
214 },
215
216 /* server authentication aliases */
217 {
218 .name = SSL_TXT_aRSA,
219 .algorithm_auth = SSL_aRSA,
220 },
221 {
222 .name = SSL_TXT_aDSS,
223 .algorithm_auth = SSL_aDSS,
224 },
225 {
226 .name = SSL_TXT_DSS,
227 .algorithm_auth = SSL_aDSS,
228 },
229 {
230 .name = SSL_TXT_aNULL,
231 .algorithm_auth = SSL_aNULL,
232 },
233 {
234 .name = SSL_TXT_aECDSA,
235 .algorithm_auth = SSL_aECDSA,
236 },
237 {
238 .name = SSL_TXT_ECDSA,
239 .algorithm_auth = SSL_aECDSA,
240 },
241
242 /* aliases combining key exchange and server authentication */
243 {
244 .name = SSL_TXT_DHE,
245 .algorithm_mkey = SSL_kDHE,
246 .algorithm_auth = ~SSL_aNULL,
247 },
248 {
249 .name = SSL_TXT_EDH,
250 .algorithm_mkey = SSL_kDHE,
251 .algorithm_auth = ~SSL_aNULL,
252 },
253 {
254 .name = SSL_TXT_ECDHE,
255 .algorithm_mkey = SSL_kECDHE,
256 .algorithm_auth = ~SSL_aNULL,
257 },
258 {
259 .name = SSL_TXT_EECDH,
260 .algorithm_mkey = SSL_kECDHE,
261 .algorithm_auth = ~SSL_aNULL,
262 },
263 {
264 .name = SSL_TXT_NULL,
265 .algorithm_enc = SSL_eNULL,
266 },
267 {
268 .name = SSL_TXT_RSA,
269 .algorithm_mkey = SSL_kRSA,
270 .algorithm_auth = SSL_aRSA,
271 },
272 {
273 .name = SSL_TXT_ADH,
274 .algorithm_mkey = SSL_kDHE,
275 .algorithm_auth = SSL_aNULL,
276 },
277 {
278 .name = SSL_TXT_AECDH,
279 .algorithm_mkey = SSL_kECDHE,
280 .algorithm_auth = SSL_aNULL,
281 },
282
283 /* symmetric encryption aliases */
284 {
285 .name = SSL_TXT_3DES,
286 .algorithm_enc = SSL_3DES,
287 },
288 {
289 .name = SSL_TXT_RC4,
290 .algorithm_enc = SSL_RC4,
291 },
292 {
293 .name = SSL_TXT_eNULL,
294 .algorithm_enc = SSL_eNULL,
295 },
296 {
297 .name = SSL_TXT_AES128,
298 .algorithm_enc = SSL_AES128|SSL_AES128GCM,
299 },
300 {
301 .name = SSL_TXT_AES256,
302 .algorithm_enc = SSL_AES256|SSL_AES256GCM,
303 },
304 {
305 .name = SSL_TXT_AES,
306 .algorithm_enc = SSL_AES,
307 },
308 {
309 .name = SSL_TXT_AES_GCM,
310 .algorithm_enc = SSL_AES128GCM|SSL_AES256GCM,
311 },
312 {
313 .name = SSL_TXT_CAMELLIA128,
314 .algorithm_enc = SSL_CAMELLIA128,
315 },
316 {
317 .name = SSL_TXT_CAMELLIA256,
318 .algorithm_enc = SSL_CAMELLIA256,
319 },
320 {
321 .name = SSL_TXT_CAMELLIA,
322 .algorithm_enc = SSL_CAMELLIA128|SSL_CAMELLIA256,
323 },
324 {
325 .name = SSL_TXT_CHACHA20,
326 .algorithm_enc = SSL_CHACHA20POLY1305,
327 },
328
329 /* MAC aliases */
330 {
331 .name = SSL_TXT_AEAD,
332 .algorithm_mac = SSL_AEAD,
333 },
334 {
335 .name = SSL_TXT_MD5,
336 .algorithm_mac = SSL_MD5,
337 },
338 {
339 .name = SSL_TXT_SHA1,
340 .algorithm_mac = SSL_SHA1,
341 },
342 {
343 .name = SSL_TXT_SHA,
344 .algorithm_mac = SSL_SHA1,
345 },
346 {
347 .name = SSL_TXT_SHA256,
348 .algorithm_mac = SSL_SHA256,
349 },
350 {
351 .name = SSL_TXT_SHA384,
352 .algorithm_mac = SSL_SHA384,
353 },
354
355 /* protocol version aliases */
356 {
357 .name = SSL_TXT_SSLV3,
358 .algorithm_ssl = SSL_SSLV3,
359 },
360 {
361 .name = SSL_TXT_TLSV1,
362 .algorithm_ssl = SSL_TLSV1,
363 },
364 {
365 .name = SSL_TXT_TLSV1_2,
366 .algorithm_ssl = SSL_TLSV1_2,
367 },
368 {
369 .name = SSL_TXT_TLSV1_3,
370 .algorithm_ssl = SSL_TLSV1_3,
371 },
372
373 /* cipher suite aliases */
374 #ifdef LIBRESSL_HAS_TLS1_3
375 {
376 .valid = 1,
377 .name = "TLS_AES_128_GCM_SHA256",
378 .id = TLS1_3_CK_AES_128_GCM_SHA256,
379 .algorithm_ssl = SSL_TLSV1_3,
380 },
381 {
382 .valid = 1,
383 .name = "TLS_AES_256_GCM_SHA384",
384 .id = TLS1_3_CK_AES_256_GCM_SHA384,
385 .algorithm_ssl = SSL_TLSV1_3,
386 },
387 {
388 .valid = 1,
389 .name = "TLS_CHACHA20_POLY1305_SHA256",
390 .id = TLS1_3_CK_CHACHA20_POLY1305_SHA256,
391 .algorithm_ssl = SSL_TLSV1_3,
392 },
393 #endif
394
395 /* strength classes */
396 {
397 .name = SSL_TXT_LOW,
398 .algo_strength = SSL_LOW,
399 },
400 {
401 .name = SSL_TXT_MEDIUM,
402 .algo_strength = SSL_MEDIUM,
403 },
404 {
405 .name = SSL_TXT_HIGH,
406 .algo_strength = SSL_HIGH,
407 },
408 };
409
410 int
ssl_cipher_get_evp(const SSL_SESSION * ss,const EVP_CIPHER ** enc,const EVP_MD ** md,int * mac_pkey_type,int * mac_secret_size)411 ssl_cipher_get_evp(const SSL_SESSION *ss, const EVP_CIPHER **enc,
412 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
413 {
414 *enc = NULL;
415 *md = NULL;
416 *mac_pkey_type = NID_undef;
417 *mac_secret_size = 0;
418
419 if (ss->cipher == NULL)
420 return 0;
421
422 /*
423 * This function does not handle EVP_AEAD.
424 * See ssl_cipher_get_evp_aead instead.
425 */
426 if (ss->cipher->algorithm_mac & SSL_AEAD)
427 return 0;
428
429 switch (ss->cipher->algorithm_enc) {
430 case SSL_3DES:
431 *enc = EVP_des_ede3_cbc();
432 break;
433 case SSL_RC4:
434 *enc = EVP_rc4();
435 break;
436 case SSL_eNULL:
437 *enc = EVP_enc_null();
438 break;
439 case SSL_AES128:
440 *enc = EVP_aes_128_cbc();
441 break;
442 case SSL_AES256:
443 *enc = EVP_aes_256_cbc();
444 break;
445 case SSL_CAMELLIA128:
446 *enc = EVP_camellia_128_cbc();
447 break;
448 case SSL_CAMELLIA256:
449 *enc = EVP_camellia_256_cbc();
450 break;
451 }
452
453 switch (ss->cipher->algorithm_mac) {
454 case SSL_MD5:
455 *md = EVP_md5();
456 break;
457 case SSL_SHA1:
458 *md = EVP_sha1();
459 break;
460 case SSL_SHA256:
461 *md = EVP_sha256();
462 break;
463 case SSL_SHA384:
464 *md = EVP_sha384();
465 break;
466 }
467 if (*enc == NULL || *md == NULL)
468 return 0;
469
470 /* XXX remove these from ssl_cipher_get_evp? */
471 /*
472 * EVP_CIPH_FLAG_AEAD_CIPHER and EVP_CIPH_GCM_MODE ciphers are not
473 * supported via EVP_CIPHER (they should be using EVP_AEAD instead).
474 */
475 if (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER)
476 return 0;
477 if (EVP_CIPHER_mode(*enc) == EVP_CIPH_GCM_MODE)
478 return 0;
479
480 *mac_pkey_type = EVP_PKEY_HMAC;
481 *mac_secret_size = EVP_MD_size(*md);
482 return 1;
483 }
484
485 /*
486 * ssl_cipher_get_evp_aead sets aead to point to the correct EVP_AEAD object
487 * for s->cipher. It returns 1 on success and 0 on error.
488 */
489 int
ssl_cipher_get_evp_aead(const SSL_SESSION * ss,const EVP_AEAD ** aead)490 ssl_cipher_get_evp_aead(const SSL_SESSION *ss, const EVP_AEAD **aead)
491 {
492 *aead = NULL;
493
494 if (ss->cipher == NULL)
495 return 0;
496 if ((ss->cipher->algorithm_mac & SSL_AEAD) == 0)
497 return 0;
498
499 switch (ss->cipher->algorithm_enc) {
500 case SSL_AES128GCM:
501 *aead = EVP_aead_aes_128_gcm();
502 return 1;
503 case SSL_AES256GCM:
504 *aead = EVP_aead_aes_256_gcm();
505 return 1;
506 case SSL_CHACHA20POLY1305:
507 *aead = EVP_aead_chacha20_poly1305();
508 return 1;
509 default:
510 break;
511 }
512 return 0;
513 }
514
515 int
ssl_get_handshake_evp_md(SSL * s,const EVP_MD ** md)516 ssl_get_handshake_evp_md(SSL *s, const EVP_MD **md)
517 {
518 unsigned long handshake_mac;
519
520 *md = NULL;
521
522 if (s->s3->hs.cipher == NULL)
523 return 0;
524
525 handshake_mac = s->s3->hs.cipher->algorithm2 & SSL_HANDSHAKE_MAC_MASK;
526
527 /* For TLSv1.2 we upgrade the default MD5+SHA1 MAC to SHA256. */
528 if (SSL_USE_SHA256_PRF(s) && handshake_mac == SSL_HANDSHAKE_MAC_DEFAULT)
529 handshake_mac = SSL_HANDSHAKE_MAC_SHA256;
530
531 switch (handshake_mac) {
532 case SSL_HANDSHAKE_MAC_DEFAULT:
533 *md = EVP_md5_sha1();
534 return 1;
535 case SSL_HANDSHAKE_MAC_SHA256:
536 *md = EVP_sha256();
537 return 1;
538 case SSL_HANDSHAKE_MAC_SHA384:
539 *md = EVP_sha384();
540 return 1;
541 default:
542 break;
543 }
544
545 return 0;
546 }
547
548 #define ITEM_SEP(a) \
549 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
550
551 static void
ll_append_tail(CIPHER_ORDER ** head,CIPHER_ORDER * curr,CIPHER_ORDER ** tail)552 ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
553 CIPHER_ORDER **tail)
554 {
555 if (curr == *tail)
556 return;
557 if (curr == *head)
558 *head = curr->next;
559 if (curr->prev != NULL)
560 curr->prev->next = curr->next;
561 if (curr->next != NULL)
562 curr->next->prev = curr->prev;
563 (*tail)->next = curr;
564 curr->prev= *tail;
565 curr->next = NULL;
566 *tail = curr;
567 }
568
569 static void
ll_append_head(CIPHER_ORDER ** head,CIPHER_ORDER * curr,CIPHER_ORDER ** tail)570 ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
571 CIPHER_ORDER **tail)
572 {
573 if (curr == *head)
574 return;
575 if (curr == *tail)
576 *tail = curr->prev;
577 if (curr->next != NULL)
578 curr->next->prev = curr->prev;
579 if (curr->prev != NULL)
580 curr->prev->next = curr->next;
581 (*head)->prev = curr;
582 curr->next= *head;
583 curr->prev = NULL;
584 *head = curr;
585 }
586
587 /* XXX beck: remove this in a followon to removing GOST */
588 static void
ssl_cipher_get_disabled(unsigned long * mkey,unsigned long * auth,unsigned long * enc,unsigned long * mac,unsigned long * ssl)589 ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth,
590 unsigned long *enc, unsigned long *mac, unsigned long *ssl)
591 {
592 *mkey = 0;
593 *auth = 0;
594 *enc = 0;
595 *mac = 0;
596 *ssl = 0;
597
598 #ifdef SSL_FORBID_ENULL
599 *enc |= SSL_eNULL;
600 #endif
601 }
602
603 static void
ssl_cipher_collect_ciphers(const SSL_METHOD * ssl_method,int num_of_ciphers,unsigned long disabled_mkey,unsigned long disabled_auth,unsigned long disabled_enc,unsigned long disabled_mac,unsigned long disabled_ssl,CIPHER_ORDER * co_list,CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p)604 ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, int num_of_ciphers,
605 unsigned long disabled_mkey, unsigned long disabled_auth,
606 unsigned long disabled_enc, unsigned long disabled_mac,
607 unsigned long disabled_ssl, CIPHER_ORDER *co_list,
608 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
609 {
610 int i, co_list_num;
611 const SSL_CIPHER *c;
612
613 /*
614 * We have num_of_ciphers descriptions compiled in, depending on the
615 * method selected (SSLv3, TLSv1, etc). These will later be sorted in
616 * a linked list with at most num entries.
617 */
618
619 /* Get the initial list of ciphers */
620 co_list_num = 0; /* actual count of ciphers */
621 for (i = 0; i < num_of_ciphers; i++) {
622 c = ssl_method->get_cipher(i);
623 /*
624 * Drop any invalid ciphers and any which use unavailable
625 * algorithms.
626 */
627 if ((c != NULL) && c->valid &&
628 !(c->algorithm_mkey & disabled_mkey) &&
629 !(c->algorithm_auth & disabled_auth) &&
630 !(c->algorithm_enc & disabled_enc) &&
631 !(c->algorithm_mac & disabled_mac) &&
632 !(c->algorithm_ssl & disabled_ssl)) {
633 co_list[co_list_num].cipher = c;
634 co_list[co_list_num].next = NULL;
635 co_list[co_list_num].prev = NULL;
636 co_list[co_list_num].active = 0;
637 co_list_num++;
638 }
639 }
640
641 /*
642 * Prepare linked list from list entries
643 */
644 if (co_list_num > 0) {
645 co_list[0].prev = NULL;
646
647 if (co_list_num > 1) {
648 co_list[0].next = &co_list[1];
649
650 for (i = 1; i < co_list_num - 1; i++) {
651 co_list[i].prev = &co_list[i - 1];
652 co_list[i].next = &co_list[i + 1];
653 }
654
655 co_list[co_list_num - 1].prev =
656 &co_list[co_list_num - 2];
657 }
658
659 co_list[co_list_num - 1].next = NULL;
660
661 *head_p = &co_list[0];
662 *tail_p = &co_list[co_list_num - 1];
663 }
664 }
665
666 static void
ssl_cipher_collect_aliases(const SSL_CIPHER ** ca_list,int num_of_group_aliases,unsigned long disabled_mkey,unsigned long disabled_auth,unsigned long disabled_enc,unsigned long disabled_mac,unsigned long disabled_ssl,CIPHER_ORDER * head)667 ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list, int num_of_group_aliases,
668 unsigned long disabled_mkey, unsigned long disabled_auth,
669 unsigned long disabled_enc, unsigned long disabled_mac,
670 unsigned long disabled_ssl, CIPHER_ORDER *head)
671 {
672 CIPHER_ORDER *ciph_curr;
673 const SSL_CIPHER **ca_curr;
674 int i;
675 unsigned long mask_mkey = ~disabled_mkey;
676 unsigned long mask_auth = ~disabled_auth;
677 unsigned long mask_enc = ~disabled_enc;
678 unsigned long mask_mac = ~disabled_mac;
679 unsigned long mask_ssl = ~disabled_ssl;
680
681 /*
682 * First, add the real ciphers as already collected
683 */
684 ciph_curr = head;
685 ca_curr = ca_list;
686 while (ciph_curr != NULL) {
687 *ca_curr = ciph_curr->cipher;
688 ca_curr++;
689 ciph_curr = ciph_curr->next;
690 }
691
692 /*
693 * Now we add the available ones from the cipher_aliases[] table.
694 * They represent either one or more algorithms, some of which
695 * in any affected category must be supported (set in enabled_mask),
696 * or represent a cipher strength value (will be added in any case because algorithms=0).
697 */
698 for (i = 0; i < num_of_group_aliases; i++) {
699 unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
700 unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
701 unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
702 unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
703 unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
704
705 if (algorithm_mkey)
706 if ((algorithm_mkey & mask_mkey) == 0)
707 continue;
708
709 if (algorithm_auth)
710 if ((algorithm_auth & mask_auth) == 0)
711 continue;
712
713 if (algorithm_enc)
714 if ((algorithm_enc & mask_enc) == 0)
715 continue;
716
717 if (algorithm_mac)
718 if ((algorithm_mac & mask_mac) == 0)
719 continue;
720
721 if (algorithm_ssl)
722 if ((algorithm_ssl & mask_ssl) == 0)
723 continue;
724
725 *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
726 ca_curr++;
727 }
728
729 *ca_curr = NULL; /* end of list */
730 }
731
732 static void
ssl_cipher_apply_rule(unsigned long cipher_id,unsigned long alg_mkey,unsigned long alg_auth,unsigned long alg_enc,unsigned long alg_mac,unsigned long alg_ssl,unsigned long algo_strength,int rule,int strength_bits,CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p)733 ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long alg_mkey,
734 unsigned long alg_auth, unsigned long alg_enc, unsigned long alg_mac,
735 unsigned long alg_ssl, unsigned long algo_strength, int rule,
736 int strength_bits, CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
737 {
738 CIPHER_ORDER *head, *tail, *curr, *next, *last;
739 const SSL_CIPHER *cp;
740 int reverse = 0;
741
742 if (rule == CIPHER_DEL)
743 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
744
745 head = *head_p;
746 tail = *tail_p;
747
748 if (reverse) {
749 next = tail;
750 last = head;
751 } else {
752 next = head;
753 last = tail;
754 }
755
756 curr = NULL;
757 for (;;) {
758 if (curr == last)
759 break;
760 curr = next;
761 next = reverse ? curr->prev : curr->next;
762
763 cp = curr->cipher;
764
765 if (cipher_id && cp->id != cipher_id)
766 continue;
767
768 /*
769 * Selection criteria is either the value of strength_bits
770 * or the algorithms used.
771 */
772 if (strength_bits >= 0) {
773 if (strength_bits != cp->strength_bits)
774 continue;
775 } else {
776 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
777 continue;
778 if (alg_auth && !(alg_auth & cp->algorithm_auth))
779 continue;
780 if (alg_enc && !(alg_enc & cp->algorithm_enc))
781 continue;
782 if (alg_mac && !(alg_mac & cp->algorithm_mac))
783 continue;
784 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
785 continue;
786 if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
787 continue;
788 }
789
790 /* add the cipher if it has not been added yet. */
791 if (rule == CIPHER_ADD) {
792 /* reverse == 0 */
793 if (!curr->active) {
794 ll_append_tail(&head, curr, &tail);
795 curr->active = 1;
796 }
797 }
798 /* Move the added cipher to this location */
799 else if (rule == CIPHER_ORD) {
800 /* reverse == 0 */
801 if (curr->active) {
802 ll_append_tail(&head, curr, &tail);
803 }
804 } else if (rule == CIPHER_DEL) {
805 /* reverse == 1 */
806 if (curr->active) {
807 /* most recently deleted ciphersuites get best positions
808 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
809 * works in reverse to maintain the order) */
810 ll_append_head(&head, curr, &tail);
811 curr->active = 0;
812 }
813 } else if (rule == CIPHER_KILL) {
814 /* reverse == 0 */
815 if (head == curr)
816 head = curr->next;
817 else
818 curr->prev->next = curr->next;
819 if (tail == curr)
820 tail = curr->prev;
821 curr->active = 0;
822 if (curr->next != NULL)
823 curr->next->prev = curr->prev;
824 if (curr->prev != NULL)
825 curr->prev->next = curr->next;
826 curr->next = NULL;
827 curr->prev = NULL;
828 }
829 }
830
831 *head_p = head;
832 *tail_p = tail;
833 }
834
835 static int
ssl_cipher_strength_sort(CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p)836 ssl_cipher_strength_sort(CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
837 {
838 int max_strength_bits, i, *number_uses;
839 CIPHER_ORDER *curr;
840
841 /*
842 * This routine sorts the ciphers with descending strength. The sorting
843 * must keep the pre-sorted sequence, so we apply the normal sorting
844 * routine as '+' movement to the end of the list.
845 */
846 max_strength_bits = 0;
847 curr = *head_p;
848 while (curr != NULL) {
849 if (curr->active &&
850 (curr->cipher->strength_bits > max_strength_bits))
851 max_strength_bits = curr->cipher->strength_bits;
852 curr = curr->next;
853 }
854
855 number_uses = calloc((max_strength_bits + 1), sizeof(int));
856 if (!number_uses) {
857 SSLerrorx(ERR_R_MALLOC_FAILURE);
858 return (0);
859 }
860
861 /*
862 * Now find the strength_bits values actually used
863 */
864 curr = *head_p;
865 while (curr != NULL) {
866 if (curr->active)
867 number_uses[curr->cipher->strength_bits]++;
868 curr = curr->next;
869 }
870 /*
871 * Go through the list of used strength_bits values in descending
872 * order.
873 */
874 for (i = max_strength_bits; i >= 0; i--)
875 if (number_uses[i] > 0)
876 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p, tail_p);
877
878 free(number_uses);
879 return (1);
880 }
881
882 static int
ssl_cipher_process_rulestr(const char * rule_str,CIPHER_ORDER ** head_p,CIPHER_ORDER ** tail_p,const SSL_CIPHER ** ca_list,SSL_CERT * cert,int * tls13_seen)883 ssl_cipher_process_rulestr(const char *rule_str, CIPHER_ORDER **head_p,
884 CIPHER_ORDER **tail_p, const SSL_CIPHER **ca_list, SSL_CERT *cert,
885 int *tls13_seen)
886 {
887 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
888 unsigned long algo_strength;
889 int j, multi, found, rule, retval, ok, buflen;
890 unsigned long cipher_id = 0;
891 const char *l, *buf;
892 char ch;
893
894 *tls13_seen = 0;
895
896 retval = 1;
897 l = rule_str;
898 for (;;) {
899 ch = *l;
900
901 if (ch == '\0')
902 break;
903
904 if (ch == '-') {
905 rule = CIPHER_DEL;
906 l++;
907 } else if (ch == '+') {
908 rule = CIPHER_ORD;
909 l++;
910 } else if (ch == '!') {
911 rule = CIPHER_KILL;
912 l++;
913 } else if (ch == '@') {
914 rule = CIPHER_SPECIAL;
915 l++;
916 } else {
917 rule = CIPHER_ADD;
918 }
919
920 if (ITEM_SEP(ch)) {
921 l++;
922 continue;
923 }
924
925 alg_mkey = 0;
926 alg_auth = 0;
927 alg_enc = 0;
928 alg_mac = 0;
929 alg_ssl = 0;
930 algo_strength = 0;
931
932 for (;;) {
933 ch = *l;
934 buf = l;
935 buflen = 0;
936 while (((ch >= 'A') && (ch <= 'Z')) ||
937 ((ch >= '0') && (ch <= '9')) ||
938 ((ch >= 'a') && (ch <= 'z')) ||
939 (ch == '-') || (ch == '.') ||
940 (ch == '_') || (ch == '=')) {
941 ch = *(++l);
942 buflen++;
943 }
944
945 if (buflen == 0) {
946 /*
947 * We hit something we cannot deal with,
948 * it is no command or separator nor
949 * alphanumeric, so we call this an error.
950 */
951 SSLerrorx(SSL_R_INVALID_COMMAND);
952 return 0;
953 }
954
955 if (rule == CIPHER_SPECIAL) {
956 /* unused -- avoid compiler warning */
957 found = 0;
958 /* special treatment */
959 break;
960 }
961
962 /* check for multi-part specification */
963 if (ch == '+') {
964 multi = 1;
965 l++;
966 } else
967 multi = 0;
968
969 /*
970 * Now search for the cipher alias in the ca_list.
971 * Be careful with the strncmp, because the "buflen"
972 * limitation will make the rule "ADH:SOME" and the
973 * cipher "ADH-MY-CIPHER" look like a match for
974 * buflen=3. So additionally check whether the cipher
975 * name found has the correct length. We can save a
976 * strlen() call: just checking for the '\0' at the
977 * right place is sufficient, we have to strncmp()
978 * anyway (we cannot use strcmp(), because buf is not
979 * '\0' terminated.)
980 */
981 j = found = 0;
982 cipher_id = 0;
983 while (ca_list[j]) {
984 if (!strncmp(buf, ca_list[j]->name, buflen) &&
985 (ca_list[j]->name[buflen] == '\0')) {
986 found = 1;
987 break;
988 } else
989 j++;
990 }
991
992 if (!found)
993 break; /* ignore this entry */
994
995 if (ca_list[j]->algorithm_mkey) {
996 if (alg_mkey) {
997 alg_mkey &= ca_list[j]->algorithm_mkey;
998 if (!alg_mkey) {
999 found = 0;
1000 break;
1001 }
1002 } else
1003 alg_mkey = ca_list[j]->algorithm_mkey;
1004 }
1005
1006 if (ca_list[j]->algorithm_auth) {
1007 if (alg_auth) {
1008 alg_auth &= ca_list[j]->algorithm_auth;
1009 if (!alg_auth) {
1010 found = 0;
1011 break;
1012 }
1013 } else
1014 alg_auth = ca_list[j]->algorithm_auth;
1015 }
1016
1017 if (ca_list[j]->algorithm_enc) {
1018 if (alg_enc) {
1019 alg_enc &= ca_list[j]->algorithm_enc;
1020 if (!alg_enc) {
1021 found = 0;
1022 break;
1023 }
1024 } else
1025 alg_enc = ca_list[j]->algorithm_enc;
1026 }
1027
1028 if (ca_list[j]->algorithm_mac) {
1029 if (alg_mac) {
1030 alg_mac &= ca_list[j]->algorithm_mac;
1031 if (!alg_mac) {
1032 found = 0;
1033 break;
1034 }
1035 } else
1036 alg_mac = ca_list[j]->algorithm_mac;
1037 }
1038
1039 if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1040 if (algo_strength & SSL_STRONG_MASK) {
1041 algo_strength &=
1042 (ca_list[j]->algo_strength &
1043 SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1044 if (!(algo_strength &
1045 SSL_STRONG_MASK)) {
1046 found = 0;
1047 break;
1048 }
1049 } else
1050 algo_strength |=
1051 ca_list[j]->algo_strength &
1052 SSL_STRONG_MASK;
1053 }
1054
1055 if (ca_list[j]->valid) {
1056 /*
1057 * explicit ciphersuite found; its protocol
1058 * version does not become part of the search
1059 * pattern!
1060 */
1061 cipher_id = ca_list[j]->id;
1062 if (ca_list[j]->algorithm_ssl == SSL_TLSV1_3)
1063 *tls13_seen = 1;
1064 } else {
1065 /*
1066 * not an explicit ciphersuite; only in this
1067 * case, the protocol version is considered
1068 * part of the search pattern
1069 */
1070 if (ca_list[j]->algorithm_ssl) {
1071 if (alg_ssl) {
1072 alg_ssl &=
1073 ca_list[j]->algorithm_ssl;
1074 if (!alg_ssl) {
1075 found = 0;
1076 break;
1077 }
1078 } else
1079 alg_ssl =
1080 ca_list[j]->algorithm_ssl;
1081 }
1082 }
1083
1084 if (!multi)
1085 break;
1086 }
1087
1088 /*
1089 * Ok, we have the rule, now apply it
1090 */
1091 if (rule == CIPHER_SPECIAL) {
1092 /* special command */
1093 ok = 0;
1094 if (buflen == 8 && strncmp(buf, "STRENGTH", 8) == 0) {
1095 ok = ssl_cipher_strength_sort(head_p, tail_p);
1096 } else if (buflen == 10 &&
1097 strncmp(buf, "SECLEVEL=", 9) == 0) {
1098 int level = buf[9] - '0';
1099
1100 if (level >= 0 && level <= 5) {
1101 cert->security_level = level;
1102 ok = 1;
1103 } else {
1104 SSLerrorx(SSL_R_INVALID_COMMAND);
1105 }
1106 } else {
1107 SSLerrorx(SSL_R_INVALID_COMMAND);
1108 }
1109 if (ok == 0)
1110 retval = 0;
1111
1112 while ((*l != '\0') && !ITEM_SEP(*l))
1113 l++;
1114 } else if (found) {
1115 if (alg_ssl == SSL_TLSV1_3)
1116 *tls13_seen = 1;
1117 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth,
1118 alg_enc, alg_mac, alg_ssl, algo_strength, rule,
1119 -1, head_p, tail_p);
1120 } else {
1121 while ((*l != '\0') && !ITEM_SEP(*l))
1122 l++;
1123 }
1124 if (*l == '\0')
1125 break; /* done */
1126 }
1127
1128 return (retval);
1129 }
1130
1131 static inline int
ssl_aes_is_accelerated(void)1132 ssl_aes_is_accelerated(void)
1133 {
1134 #if defined(__i386__) || defined(__x86_64__)
1135 return ((OPENSSL_cpu_caps() & (1ULL << 57)) != 0);
1136 #else
1137 return (0);
1138 #endif
1139 }
1140
STACK_OF(SSL_CIPHER)1141 STACK_OF(SSL_CIPHER) *
1142 ssl_create_cipher_list(const SSL_METHOD *ssl_method,
1143 STACK_OF(SSL_CIPHER) **cipher_list,
1144 STACK_OF(SSL_CIPHER) *cipher_list_tls13,
1145 const char *rule_str, SSL_CERT *cert)
1146 {
1147 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1148 unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
1149 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *ret = NULL;
1150 const char *rule_p;
1151 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1152 const SSL_CIPHER **ca_list = NULL;
1153 const SSL_CIPHER *cipher;
1154 int tls13_seen = 0;
1155 int any_active;
1156 int i;
1157
1158 /*
1159 * Return with error if nothing to do.
1160 */
1161 if (rule_str == NULL || cipher_list == NULL)
1162 goto err;
1163
1164 /*
1165 * To reduce the work to do we only want to process the compiled
1166 * in algorithms, so we first get the mask of disabled ciphers.
1167 */
1168 ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1169
1170 /*
1171 * Now we have to collect the available ciphers from the compiled
1172 * in ciphers. We cannot get more than the number compiled in, so
1173 * it is used for allocation.
1174 */
1175 num_of_ciphers = ssl3_num_ciphers();
1176 co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER));
1177 if (co_list == NULL) {
1178 SSLerrorx(ERR_R_MALLOC_FAILURE);
1179 goto err;
1180 }
1181
1182 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1183 disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1184 co_list, &head, &tail);
1185
1186
1187 /* Now arrange all ciphers by preference: */
1188
1189 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
1190 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1191 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1192
1193 if (ssl_aes_is_accelerated()) {
1194 /*
1195 * We have hardware assisted AES - prefer AES as a symmetric
1196 * cipher, with CHACHA20 second.
1197 */
1198 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1199 CIPHER_ADD, -1, &head, &tail);
1200 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1201 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1202 } else {
1203 /*
1204 * CHACHA20 is fast and safe on all hardware and is thus our
1205 * preferred symmetric cipher, with AES second.
1206 */
1207 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1208 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1209 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1210 CIPHER_ADD, -1, &head, &tail);
1211 }
1212
1213 /* Temporarily enable everything else for sorting */
1214 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1215
1216 /* Low priority for MD5 */
1217 ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
1218
1219 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1220 * (For applications that allow them, they aren't too bad, but we prefer
1221 * authenticated ciphers.) */
1222 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1223
1224 /* Move ciphers without forward secrecy to the end */
1225 ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1226
1227 /* RC4 is sort of broken - move it to the end */
1228 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1229
1230 /* Now sort by symmetric encryption strength. The above ordering remains
1231 * in force within each class */
1232 if (!ssl_cipher_strength_sort(&head, &tail))
1233 goto err;
1234
1235 /* Now disable everything (maintaining the ordering!) */
1236 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1237
1238 /* TLSv1.3 first. */
1239 ssl_cipher_apply_rule(0, 0, 0, 0, 0, SSL_TLSV1_3, 0, CIPHER_ADD, -1, &head, &tail);
1240 ssl_cipher_apply_rule(0, 0, 0, 0, 0, SSL_TLSV1_3, 0, CIPHER_DEL, -1, &head, &tail);
1241
1242 /*
1243 * We also need cipher aliases for selecting based on the rule_str.
1244 * There might be two types of entries in the rule_str: 1) names
1245 * of ciphers themselves 2) aliases for groups of ciphers.
1246 * For 1) we need the available ciphers and for 2) the cipher
1247 * groups of cipher_aliases added together in one list (otherwise
1248 * we would be happy with just the cipher_aliases table).
1249 */
1250 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1251 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1252 ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *));
1253 if (ca_list == NULL) {
1254 SSLerrorx(ERR_R_MALLOC_FAILURE);
1255 goto err;
1256 }
1257 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mkey,
1258 disabled_auth, disabled_enc, disabled_mac, disabled_ssl, head);
1259
1260 /*
1261 * If the rule_string begins with DEFAULT, apply the default rule
1262 * before using the (possibly available) additional rules.
1263 */
1264 ok = 1;
1265 rule_p = rule_str;
1266 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1267 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1268 &head, &tail, ca_list, cert, &tls13_seen);
1269 rule_p += 7;
1270 if (*rule_p == ':')
1271 rule_p++;
1272 }
1273
1274 if (ok && (strlen(rule_p) > 0))
1275 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list,
1276 cert, &tls13_seen);
1277
1278 if (!ok) {
1279 /* Rule processing failure */
1280 goto err;
1281 }
1282
1283 /*
1284 * Allocate new "cipherstack" for the result, return with error
1285 * if we cannot get one.
1286 */
1287 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1288 SSLerrorx(ERR_R_MALLOC_FAILURE);
1289 goto err;
1290 }
1291
1292 /* Prefer TLSv1.3 cipher suites. */
1293 if (cipher_list_tls13 != NULL) {
1294 for (i = 0; i < sk_SSL_CIPHER_num(cipher_list_tls13); i++) {
1295 cipher = sk_SSL_CIPHER_value(cipher_list_tls13, i);
1296 if (!sk_SSL_CIPHER_push(cipherstack, cipher)) {
1297 SSLerrorx(ERR_R_MALLOC_FAILURE);
1298 goto err;
1299 }
1300 }
1301 tls13_seen = 1;
1302 }
1303
1304 /*
1305 * The cipher selection for the list is done. The ciphers are added
1306 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1307 *
1308 * If the rule string did not contain any references to TLSv1.3 and
1309 * TLSv1.3 cipher suites have not been configured separately,
1310 * include inactive TLSv1.3 cipher suites. This avoids attempts to
1311 * use TLSv1.3 with an older rule string that does not include
1312 * TLSv1.3 cipher suites. If the rule string resulted in no active
1313 * cipher suites then we return an empty stack.
1314 */
1315 any_active = 0;
1316 for (curr = head; curr != NULL; curr = curr->next) {
1317 if (curr->active ||
1318 (!tls13_seen && curr->cipher->algorithm_ssl == SSL_TLSV1_3)) {
1319 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1320 SSLerrorx(ERR_R_MALLOC_FAILURE);
1321 goto err;
1322 }
1323 }
1324 any_active |= curr->active;
1325 }
1326 if (!any_active)
1327 sk_SSL_CIPHER_zero(cipherstack);
1328
1329 sk_SSL_CIPHER_free(*cipher_list);
1330 *cipher_list = cipherstack;
1331 cipherstack = NULL;
1332
1333 ret = *cipher_list;
1334
1335 err:
1336 sk_SSL_CIPHER_free(cipherstack);
1337 free((void *)ca_list);
1338 free(co_list);
1339
1340 return ret;
1341 }
1342
1343 char *
SSL_CIPHER_description(const SSL_CIPHER * cipher,char * buf,int len)1344 SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1345 {
1346 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1347 const char *ver, *kx, *au, *enc, *mac;
1348 char *ret;
1349 int l;
1350
1351 alg_mkey = cipher->algorithm_mkey;
1352 alg_auth = cipher->algorithm_auth;
1353 alg_enc = cipher->algorithm_enc;
1354 alg_mac = cipher->algorithm_mac;
1355 alg_ssl = cipher->algorithm_ssl;
1356
1357 if (alg_ssl & SSL_SSLV3)
1358 ver = "SSLv3";
1359 else if (alg_ssl & SSL_TLSV1_2)
1360 ver = "TLSv1.2";
1361 else if (alg_ssl & SSL_TLSV1_3)
1362 ver = "TLSv1.3";
1363 else
1364 ver = "unknown";
1365
1366 switch (alg_mkey) {
1367 case SSL_kRSA:
1368 kx = "RSA";
1369 break;
1370 case SSL_kDHE:
1371 kx = "DH";
1372 break;
1373 case SSL_kECDHE:
1374 kx = "ECDH";
1375 break;
1376 case SSL_kTLS1_3:
1377 kx = "TLSv1.3";
1378 break;
1379 default:
1380 kx = "unknown";
1381 }
1382
1383 switch (alg_auth) {
1384 case SSL_aRSA:
1385 au = "RSA";
1386 break;
1387 case SSL_aDSS:
1388 au = "DSS";
1389 break;
1390 case SSL_aNULL:
1391 au = "None";
1392 break;
1393 case SSL_aECDSA:
1394 au = "ECDSA";
1395 break;
1396 case SSL_aTLS1_3:
1397 au = "TLSv1.3";
1398 break;
1399 default:
1400 au = "unknown";
1401 break;
1402 }
1403
1404 switch (alg_enc) {
1405 case SSL_3DES:
1406 enc = "3DES(168)";
1407 break;
1408 case SSL_RC4:
1409 enc = "RC4(128)";
1410 break;
1411 case SSL_eNULL:
1412 enc = "None";
1413 break;
1414 case SSL_AES128:
1415 enc = "AES(128)";
1416 break;
1417 case SSL_AES256:
1418 enc = "AES(256)";
1419 break;
1420 case SSL_AES128GCM:
1421 enc = "AESGCM(128)";
1422 break;
1423 case SSL_AES256GCM:
1424 enc = "AESGCM(256)";
1425 break;
1426 case SSL_CAMELLIA128:
1427 enc = "Camellia(128)";
1428 break;
1429 case SSL_CAMELLIA256:
1430 enc = "Camellia(256)";
1431 break;
1432 case SSL_CHACHA20POLY1305:
1433 enc = "ChaCha20-Poly1305";
1434 break;
1435 default:
1436 enc = "unknown";
1437 break;
1438 }
1439
1440 switch (alg_mac) {
1441 case SSL_MD5:
1442 mac = "MD5";
1443 break;
1444 case SSL_SHA1:
1445 mac = "SHA1";
1446 break;
1447 case SSL_SHA256:
1448 mac = "SHA256";
1449 break;
1450 case SSL_SHA384:
1451 mac = "SHA384";
1452 break;
1453 case SSL_AEAD:
1454 mac = "AEAD";
1455 break;
1456 default:
1457 mac = "unknown";
1458 break;
1459 }
1460
1461 if (asprintf(&ret, "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1462 cipher->name, ver, kx, au, enc, mac) == -1)
1463 return "OPENSSL_malloc Error";
1464
1465 if (buf != NULL) {
1466 l = strlcpy(buf, ret, len);
1467 free(ret);
1468 ret = buf;
1469 if (l >= len)
1470 ret = "Buffer too small";
1471 }
1472
1473 return (ret);
1474 }
1475 LSSL_ALIAS(SSL_CIPHER_description);
1476
1477 const char *
SSL_CIPHER_get_version(const SSL_CIPHER * c)1478 SSL_CIPHER_get_version(const SSL_CIPHER *c)
1479 {
1480 if (c == NULL)
1481 return("(NONE)");
1482 if ((c->id >> 24) == 3)
1483 return("TLSv1/SSLv3");
1484 else
1485 return("unknown");
1486 }
1487 LSSL_ALIAS(SSL_CIPHER_get_version);
1488
1489 /* return the actual cipher being used */
1490 const char *
SSL_CIPHER_get_name(const SSL_CIPHER * c)1491 SSL_CIPHER_get_name(const SSL_CIPHER *c)
1492 {
1493 if (c != NULL)
1494 return (c->name);
1495 return("(NONE)");
1496 }
1497 LSSL_ALIAS(SSL_CIPHER_get_name);
1498
1499 /* number of bits for symmetric cipher */
1500 int
SSL_CIPHER_get_bits(const SSL_CIPHER * c,int * alg_bits)1501 SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1502 {
1503 int ret = 0;
1504
1505 if (c != NULL) {
1506 if (alg_bits != NULL)
1507 *alg_bits = c->alg_bits;
1508 ret = c->strength_bits;
1509 }
1510 return (ret);
1511 }
1512 LSSL_ALIAS(SSL_CIPHER_get_bits);
1513
1514 unsigned long
SSL_CIPHER_get_id(const SSL_CIPHER * c)1515 SSL_CIPHER_get_id(const SSL_CIPHER *c)
1516 {
1517 return c->id;
1518 }
1519 LSSL_ALIAS(SSL_CIPHER_get_id);
1520
1521 uint16_t
SSL_CIPHER_get_value(const SSL_CIPHER * c)1522 SSL_CIPHER_get_value(const SSL_CIPHER *c)
1523 {
1524 return ssl3_cipher_get_value(c);
1525 }
1526 LSSL_ALIAS(SSL_CIPHER_get_value);
1527
1528 const SSL_CIPHER *
SSL_CIPHER_find(SSL * ssl,const unsigned char * ptr)1529 SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
1530 {
1531 uint16_t cipher_value;
1532 CBS cbs;
1533
1534 /* This API is documented with ptr being an array of length two. */
1535 CBS_init(&cbs, ptr, 2);
1536 if (!CBS_get_u16(&cbs, &cipher_value))
1537 return NULL;
1538
1539 return ssl3_get_cipher_by_value(cipher_value);
1540 }
1541 LSSL_ALIAS(SSL_CIPHER_find);
1542
1543 int
SSL_CIPHER_get_cipher_nid(const SSL_CIPHER * c)1544 SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
1545 {
1546 switch (c->algorithm_enc) {
1547 case SSL_eNULL:
1548 return NID_undef;
1549 case SSL_3DES:
1550 return NID_des_ede3_cbc;
1551 case SSL_AES128:
1552 return NID_aes_128_cbc;
1553 case SSL_AES128GCM:
1554 return NID_aes_128_gcm;
1555 case SSL_AES256:
1556 return NID_aes_256_cbc;
1557 case SSL_AES256GCM:
1558 return NID_aes_256_gcm;
1559 case SSL_CAMELLIA128:
1560 return NID_camellia_128_cbc;
1561 case SSL_CAMELLIA256:
1562 return NID_camellia_256_cbc;
1563 case SSL_CHACHA20POLY1305:
1564 return NID_chacha20_poly1305;
1565 case SSL_DES:
1566 return NID_des_cbc;
1567 case SSL_RC4:
1568 return NID_rc4;
1569 default:
1570 return NID_undef;
1571 }
1572 }
1573 LSSL_ALIAS(SSL_CIPHER_get_cipher_nid);
1574
1575 int
SSL_CIPHER_get_digest_nid(const SSL_CIPHER * c)1576 SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
1577 {
1578 switch (c->algorithm_mac) {
1579 case SSL_AEAD:
1580 return NID_undef;
1581 case SSL_MD5:
1582 return NID_md5;
1583 case SSL_SHA1:
1584 return NID_sha1;
1585 case SSL_SHA256:
1586 return NID_sha256;
1587 case SSL_SHA384:
1588 return NID_sha384;
1589 default:
1590 return NID_undef;
1591 }
1592 }
1593 LSSL_ALIAS(SSL_CIPHER_get_digest_nid);
1594
1595 int
SSL_CIPHER_get_kx_nid(const SSL_CIPHER * c)1596 SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
1597 {
1598 switch (c->algorithm_mkey) {
1599 case SSL_kDHE:
1600 return NID_kx_dhe;
1601 case SSL_kECDHE:
1602 return NID_kx_ecdhe;
1603 case SSL_kRSA:
1604 return NID_kx_rsa;
1605 default:
1606 return NID_undef;
1607 }
1608 }
1609 LSSL_ALIAS(SSL_CIPHER_get_kx_nid);
1610
1611 int
SSL_CIPHER_get_auth_nid(const SSL_CIPHER * c)1612 SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
1613 {
1614 switch (c->algorithm_auth) {
1615 case SSL_aNULL:
1616 return NID_auth_null;
1617 case SSL_aECDSA:
1618 return NID_auth_ecdsa;
1619 case SSL_aRSA:
1620 return NID_auth_rsa;
1621 default:
1622 return NID_undef;
1623 }
1624 }
1625 LSSL_ALIAS(SSL_CIPHER_get_auth_nid);
1626
1627 int
SSL_CIPHER_is_aead(const SSL_CIPHER * c)1628 SSL_CIPHER_is_aead(const SSL_CIPHER *c)
1629 {
1630 return (c->algorithm_mac & SSL_AEAD) == SSL_AEAD;
1631 }
1632 LSSL_ALIAS(SSL_CIPHER_is_aead);
1633
1634 void *
SSL_COMP_get_compression_methods(void)1635 SSL_COMP_get_compression_methods(void)
1636 {
1637 return NULL;
1638 }
1639 LSSL_ALIAS(SSL_COMP_get_compression_methods);
1640
1641 int
SSL_COMP_add_compression_method(int id,void * cm)1642 SSL_COMP_add_compression_method(int id, void *cm)
1643 {
1644 return 1;
1645 }
1646 LSSL_ALIAS(SSL_COMP_add_compression_method);
1647
1648 const char *
SSL_COMP_get_name(const void * comp)1649 SSL_COMP_get_name(const void *comp)
1650 {
1651 return NULL;
1652 }
1653 LSSL_ALIAS(SSL_COMP_get_name);
1654