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