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