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