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