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