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