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