Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1495 rader
40 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@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@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@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@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@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@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@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. #include <stdio.h>
  141. #include <assert.h>
  142. #include <openssl/engine.h>
  143. #include <openssl/mem.h>
  144. #include <openssl/obj.h>
  145. #include "ssl_locl.h"
  146. struct handshake_digest
  147. {
  148. long mask;
  149. const EVP_MD *(*md_func)(void);
  150. };
  151. static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
  152. { SSL_HANDSHAKE_MAC_MD5, EVP_md5 },
  153. { SSL_HANDSHAKE_MAC_SHA, EVP_sha1 },
  154. { SSL_HANDSHAKE_MAC_SHA256, EVP_sha256 },
  155. { SSL_HANDSHAKE_MAC_SHA384, EVP_sha384 },
  156. };
  157. #define CIPHER_ADD 1
  158. #define CIPHER_KILL 2
  159. #define CIPHER_DEL 3
  160. #define CIPHER_ORD 4
  161. #define CIPHER_SPECIAL 5
  162. typedef struct cipher_order_st
  163. {
  164. const SSL_CIPHER *cipher;
  165. int active;
  166. int dead;
  167. int in_group;
  168. struct cipher_order_st *next,*prev;
  169. } CIPHER_ORDER;
  170. static const SSL_CIPHER cipher_aliases[]={
  171. {0,SSL_TXT_ALL,0, 0,0,0,0,0,0,0,0,0},
  172. /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
  173. {0,SSL_TXT_CMPDEF,0, SSL_kEDH|SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
  174. /* key exchange aliases
  175. * (some of those using only a single bit here combine
  176. * multiple key exchange algs according to the RFCs,
  177. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  178. {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
  179. {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
  180. {0,SSL_TXT_DH,0, SSL_kEDH,0,0,0,0,0,0,0,0},
  181. {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
  182. {0,SSL_TXT_ECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
  183. {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
  184. /* server authentication aliases */
  185. {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
  186. {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
  187. {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
  188. {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
  189. {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
  190. /* aliases combining key exchange and server authentication */
  191. {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
  192. {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
  193. {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
  194. {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
  195. {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
  196. {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
  197. /* symmetric encryption aliases */
  198. {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
  199. {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
  200. {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
  201. {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
  202. {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
  203. {0,SSL_TXT_AES_GCM,0, 0,0,SSL_AES128GCM|SSL_AES256GCM,0,0,0,0,0,0},
  204. {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
  205. /* MAC aliases */
  206. {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
  207. {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
  208. {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
  209. {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
  210. {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
  211. /* protocol version aliases */
  212. {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
  213. {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
  214. {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
  215. /* strength classes */
  216. {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
  217. {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
  218. /* FIPS 140-2 approved ciphersuite */
  219. {0,SSL_TXT_FIPS,0, 0,0,0,0,0,SSL_FIPS, 0,0,0},
  220. };
  221. /* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
  222. * for |s->cipher|. It returns 1 on success and 0 on error. */
  223. int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
  224. {
  225. const SSL_CIPHER *c = s->cipher;
  226. *aead = NULL;
  227. if (c == NULL)
  228. return 0;
  229. if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0 &&
  230. (c->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) == 0)
  231. return 0;
  232. switch (c->algorithm_enc)
  233. {
  234. case SSL_AES128GCM:
  235. *aead = EVP_aead_aes_128_gcm();
  236. return 1;
  237. case SSL_AES256GCM:
  238. *aead = EVP_aead_aes_256_gcm();
  239. return 1;
  240. case SSL_CHACHA20POLY1305:
  241. *aead = EVP_aead_chacha20_poly1305();
  242. return 1;
  243. case SSL_RC4:
  244. if (c->algorithm_mac == SSL_MD5)
  245. *aead = EVP_aead_rc4_md5_tls();
  246. else
  247. return 0;
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  253. const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
  254. {
  255. const SSL_CIPHER *c;
  256. c=s->cipher;
  257. if (c == NULL) return(0);
  258. /* This function doesn't deal with EVP_AEAD. See
  259. * |ssl_cipher_get_aead_evp|. */
  260. if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
  261. return(0);
  262. if ((enc == NULL) || (md == NULL)) return(0);
  263. switch (c->algorithm_enc)
  264. {
  265. case SSL_3DES:
  266. *enc = EVP_des_ede3_cbc();
  267. break;
  268. case SSL_RC4:
  269. *enc = EVP_rc4();
  270. break;
  271. case SSL_AES128:
  272. *enc = EVP_aes_128_cbc();
  273. break;
  274. case SSL_AES256:
  275. *enc = EVP_aes_256_cbc();
  276. break;
  277. default:
  278. return 0;
  279. }
  280. if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size))
  281. return 0;
  282. assert(*enc != NULL && *md != NULL);
  283. /* TODO(fork): enable the stitched cipher modes. */
  284. #if 0
  285. if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
  286. s->ssl_version < TLS1_VERSION)
  287. return 1;
  288. if (c->algorithm_enc == SSL_RC4 &&
  289. c->algorithm_mac == SSL_MD5 &&
  290. (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
  291. *enc = evp, *md = NULL;
  292. else if (c->algorithm_enc == SSL_AES128 &&
  293. c->algorithm_mac == SSL_SHA1 &&
  294. (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
  295. *enc = evp, *md = NULL;
  296. else if (c->algorithm_enc == SSL_AES256 &&
  297. c->algorithm_mac == SSL_SHA1 &&
  298. (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
  299. *enc = evp, *md = NULL;
  300. #endif
  301. return 1;
  302. }
  303. int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
  304. {
  305. const SSL_CIPHER *c;
  306. c=s->cipher;
  307. if (c == NULL) return(0);
  308. switch (c->algorithm_mac)
  309. {
  310. case SSL_MD5:
  311. *md = EVP_md5();
  312. break;
  313. case SSL_SHA1:
  314. *md = EVP_sha1();
  315. break;
  316. case SSL_SHA256:
  317. *md = EVP_sha256();
  318. break;
  319. case SSL_SHA384:
  320. *md = EVP_sha384();
  321. break;
  322. default:
  323. return 0;
  324. }
  325. if (mac_pkey_type != NULL)
  326. {
  327. *mac_pkey_type = EVP_PKEY_HMAC;
  328. }
  329. if (mac_secret_size!=NULL)
  330. {
  331. *mac_secret_size = EVP_MD_size(*md);
  332. }
  333. return 1;
  334. }
  335. int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
  336. {
  337. if (idx < 0 || idx >= SSL_MAX_DIGEST)
  338. {
  339. return 0;
  340. }
  341. *mask = ssl_handshake_digests[idx].mask;
  342. *md = ssl_handshake_digests[idx].md_func();
  343. return 1;
  344. }
  345. #define ITEM_SEP(a) \
  346. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  347. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  348. CIPHER_ORDER **tail)
  349. {
  350. if (curr == *tail) return;
  351. if (curr == *head)
  352. *head=curr->next;
  353. if (curr->prev != NULL)
  354. curr->prev->next=curr->next;
  355. if (curr->next != NULL)
  356. curr->next->prev=curr->prev;
  357. (*tail)->next=curr;
  358. curr->prev= *tail;
  359. curr->next=NULL;
  360. *tail=curr;
  361. }
  362. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  363. CIPHER_ORDER **tail)
  364. {
  365. if (curr == *head) return;
  366. if (curr == *tail)
  367. *tail=curr->prev;
  368. if (curr->next != NULL)
  369. curr->next->prev=curr->prev;
  370. if (curr->prev != NULL)
  371. curr->prev->next=curr->next;
  372. (*head)->prev=curr;
  373. curr->next= *head;
  374. curr->prev=NULL;
  375. *head=curr;
  376. }
  377. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  378. int num_of_ciphers,
  379. CIPHER_ORDER *co_list,
  380. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  381. {
  382. int i, co_list_num;
  383. const SSL_CIPHER *c;
  384. /*
  385. * We have num_of_ciphers descriptions compiled in, depending on the
  386. * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
  387. * These will later be sorted in a linked list with at most num
  388. * entries.
  389. */
  390. /* Get the initial list of ciphers */
  391. co_list_num = 0; /* actual count of ciphers */
  392. for (i = 0; i < num_of_ciphers; i++)
  393. {
  394. c = ssl_method->get_cipher(i);
  395. /* drop those that use any of that is not available */
  396. if ((c != NULL) && c->valid)
  397. {
  398. co_list[co_list_num].cipher = c;
  399. co_list[co_list_num].next = NULL;
  400. co_list[co_list_num].prev = NULL;
  401. co_list[co_list_num].active = 0;
  402. co_list[co_list_num].in_group = 0;
  403. co_list_num++;
  404. #ifdef KSSL_DEBUG
  405. printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
  406. #endif /* KSSL_DEBUG */
  407. /*
  408. if (!sk_push(ca_list,(char *)c)) goto err;
  409. */
  410. }
  411. }
  412. /*
  413. * Prepare linked list from list entries
  414. */
  415. if (co_list_num > 0)
  416. {
  417. co_list[0].prev = NULL;
  418. if (co_list_num > 1)
  419. {
  420. co_list[0].next = &co_list[1];
  421. for (i = 1; i < co_list_num - 1; i++)
  422. {
  423. co_list[i].prev = &co_list[i - 1];
  424. co_list[i].next = &co_list[i + 1];
  425. }
  426. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  427. }
  428. co_list[co_list_num - 1].next = NULL;
  429. *head_p = &co_list[0];
  430. *tail_p = &co_list[co_list_num - 1];
  431. }
  432. }
  433. static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
  434. int num_of_group_aliases,
  435. CIPHER_ORDER *head)
  436. {
  437. CIPHER_ORDER *ciph_curr;
  438. const SSL_CIPHER **ca_curr;
  439. int i;
  440. /*
  441. * First, add the real ciphers as already collected
  442. */
  443. ciph_curr = head;
  444. ca_curr = ca_list;
  445. while (ciph_curr != NULL)
  446. {
  447. *ca_curr = ciph_curr->cipher;
  448. ca_curr++;
  449. ciph_curr = ciph_curr->next;
  450. }
  451. /*
  452. * Now we add the available ones from the cipher_aliases[] table.
  453. * They represent either one or more algorithms, some of which
  454. * in any affected category must be supported (set in enabled_mask),
  455. * or represent a cipher strength value (will be added in any case because algorithms=0).
  456. */
  457. for (i = 0; i < num_of_group_aliases; i++)
  458. {
  459. *ca_curr = cipher_aliases + i;
  460. ca_curr++;
  461. }
  462. *ca_curr = NULL; /* end of list */
  463. }
  464. static void ssl_cipher_apply_rule(unsigned long cipher_id,
  465. unsigned long alg_mkey, unsigned long alg_auth,
  466. unsigned long alg_enc, unsigned long alg_mac,
  467. unsigned long alg_ssl,
  468. unsigned long algo_strength,
  469. int rule, int strength_bits, int in_group,
  470. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  471. {
  472. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  473. const SSL_CIPHER *cp;
  474. int reverse = 0;
  475. #ifdef CIPHER_DEBUG
  476. printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
  477. rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
  478. #endif
  479. if (rule == CIPHER_DEL)
  480. reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
  481. head = *head_p;
  482. tail = *tail_p;
  483. if (reverse)
  484. {
  485. next = tail;
  486. last = head;
  487. }
  488. else
  489. {
  490. next = head;
  491. last = tail;
  492. }
  493. curr = NULL;
  494. for (;;)
  495. {
  496. if (curr == last) break;
  497. curr = next;
  498. if (curr == NULL) break;
  499. next = reverse ? curr->prev : curr->next;
  500. cp = curr->cipher;
  501. /*
  502. * Selection criteria is either the value of strength_bits
  503. * or the algorithms used.
  504. */
  505. if (strength_bits >= 0)
  506. {
  507. if (strength_bits != cp->strength_bits)
  508. continue;
  509. }
  510. else
  511. {
  512. #ifdef CIPHER_DEBUG
  513. 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);
  514. #endif
  515. if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
  516. continue;
  517. if (alg_auth && !(alg_auth & cp->algorithm_auth))
  518. continue;
  519. if (alg_enc && !(alg_enc & cp->algorithm_enc))
  520. continue;
  521. if (alg_mac && !(alg_mac & cp->algorithm_mac))
  522. continue;
  523. if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
  524. continue;
  525. if (algo_strength && !(algo_strength & cp->algo_strength))
  526. continue;
  527. }
  528. #ifdef CIPHER_DEBUG
  529. printf("Action = %d\n", rule);
  530. #endif
  531. /* add the cipher if it has not been added yet. */
  532. if (rule == CIPHER_ADD)
  533. {
  534. /* reverse == 0 */
  535. if (!curr->active)
  536. {
  537. ll_append_tail(&head, curr, &tail);
  538. curr->active = 1;
  539. curr->in_group = in_group;
  540. }
  541. }
  542. /* Move the added cipher to this location */
  543. else if (rule == CIPHER_ORD)
  544. {
  545. /* reverse == 0 */
  546. if (curr->active)
  547. {
  548. ll_append_tail(&head, curr, &tail);
  549. curr->in_group = 0;
  550. }
  551. }
  552. else if (rule == CIPHER_DEL)
  553. {
  554. /* reverse == 1 */
  555. if (curr->active)
  556. {
  557. /* most recently deleted ciphersuites get best positions
  558. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  559. * works in reverse to maintain the order) */
  560. ll_append_head(&head, curr, &tail);
  561. curr->active = 0;
  562. curr->in_group = 0;
  563. }
  564. }
  565. else if (rule == CIPHER_KILL)
  566. {
  567. /* reverse == 0 */
  568. if (head == curr)
  569. head = curr->next;
  570. else
  571. curr->prev->next = curr->next;
  572. if (tail == curr)
  573. tail = curr->prev;
  574. curr->active = 0;
  575. if (curr->next != NULL)
  576. curr->next->prev = curr->prev;
  577. if (curr->prev != NULL)
  578. curr->prev->next = curr->next;
  579. curr->next = NULL;
  580. curr->prev = NULL;
  581. }
  582. }
  583. *head_p = head;
  584. *tail_p = tail;
  585. }
  586. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  587. CIPHER_ORDER **tail_p)
  588. {
  589. int max_strength_bits, i, *number_uses;
  590. CIPHER_ORDER *curr;
  591. /*
  592. * This routine sorts the ciphers with descending strength. The sorting
  593. * must keep the pre-sorted sequence, so we apply the normal sorting
  594. * routine as '+' movement to the end of the list.
  595. */
  596. max_strength_bits = 0;
  597. curr = *head_p;
  598. while (curr != NULL)
  599. {
  600. if (curr->active &&
  601. (curr->cipher->strength_bits > max_strength_bits))
  602. max_strength_bits = curr->cipher->strength_bits;
  603. curr = curr->next;
  604. }
  605. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  606. if (!number_uses)
  607. {
  608. OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
  609. return(0);
  610. }
  611. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  612. /*
  613. * Now find the strength_bits values actually used
  614. */
  615. curr = *head_p;
  616. while (curr != NULL)
  617. {
  618. if (curr->active)
  619. number_uses[curr->cipher->strength_bits]++;
  620. curr = curr->next;
  621. }
  622. /*
  623. * Go through the list of used strength_bits values in descending
  624. * order.
  625. */
  626. for (i = max_strength_bits; i >= 0; i--)
  627. if (number_uses[i] > 0)
  628. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
  629. OPENSSL_free(number_uses);
  630. return(1);
  631. }
  632. static int ssl_cipher_process_rulestr(const char *rule_str,
  633. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
  634. const SSL_CIPHER **ca_list)
  635. {
  636. unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
  637. const char *l, *buf;
  638. int j, multi, found, rule, retval, ok, buflen, in_group = 0,
  639. has_group = 0;
  640. unsigned long cipher_id = 0;
  641. char ch;
  642. retval = 1;
  643. l = rule_str;
  644. for (;;)
  645. {
  646. ch = *l;
  647. if (ch == '\0')
  648. break; /* done */
  649. if (in_group)
  650. {
  651. if (ch == ']')
  652. {
  653. if (!in_group)
  654. {
  655. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
  656. retval = found = in_group = 0;
  657. break;
  658. }
  659. if (*tail_p)
  660. (*tail_p)->in_group = 0;
  661. in_group = 0;
  662. l++;
  663. continue;
  664. }
  665. if (ch == '|')
  666. { rule = CIPHER_ADD; l++; continue; }
  667. else if (!(ch >= 'a' && ch <= 'z') &&
  668. !(ch >= 'A' && ch <= 'Z') &&
  669. !(ch >= '0' && ch <= '9'))
  670. {
  671. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  672. retval = found = in_group = 0;
  673. break;
  674. }
  675. else
  676. rule = CIPHER_ADD;
  677. }
  678. else if (ch == '-')
  679. { rule = CIPHER_DEL; l++; }
  680. else if (ch == '+')
  681. { rule = CIPHER_ORD; l++; }
  682. else if (ch == '!')
  683. { rule = CIPHER_KILL; l++; }
  684. else if (ch == '@')
  685. { rule = CIPHER_SPECIAL; l++; }
  686. else if (ch == '[')
  687. {
  688. if (in_group)
  689. {
  690. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
  691. retval = found = in_group = 0;
  692. break;
  693. }
  694. in_group = 1;
  695. has_group = 1;
  696. l++;
  697. continue;
  698. }
  699. else
  700. { rule = CIPHER_ADD; }
  701. /* If preference groups are enabled, the only legal
  702. * operator is +. Otherwise the in_group bits will get
  703. * mixed up. */
  704. if (has_group && rule != CIPHER_ADD)
  705. {
  706. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  707. retval = found = in_group = 0;
  708. break;
  709. }
  710. if (ITEM_SEP(ch))
  711. {
  712. l++;
  713. continue;
  714. }
  715. alg_mkey = 0;
  716. alg_auth = 0;
  717. alg_enc = 0;
  718. alg_mac = 0;
  719. alg_ssl = 0;
  720. algo_strength = 0;
  721. for (;;)
  722. {
  723. ch = *l;
  724. buf = l;
  725. buflen = 0;
  726. while ( ((ch >= 'A') && (ch <= 'Z')) ||
  727. ((ch >= '0') && (ch <= '9')) ||
  728. ((ch >= 'a') && (ch <= 'z')) ||
  729. (ch == '-') || (ch == '.'))
  730. {
  731. ch = *(++l);
  732. buflen++;
  733. }
  734. if (buflen == 0)
  735. {
  736. /*
  737. * We hit something we cannot deal with,
  738. * it is no command or separator nor
  739. * alphanumeric, so we call this an error.
  740. */
  741. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  742. retval = found = in_group = 0;
  743. l++;
  744. break;
  745. }
  746. if (rule == CIPHER_SPECIAL)
  747. {
  748. found = 0; /* unused -- avoid compiler warning */
  749. break; /* special treatment */
  750. }
  751. /* check for multi-part specification */
  752. if (ch == '+')
  753. {
  754. multi=1;
  755. l++;
  756. }
  757. else
  758. multi=0;
  759. /*
  760. * Now search for the cipher alias in the ca_list. Be careful
  761. * with the strncmp, because the "buflen" limitation
  762. * will make the rule "ADH:SOME" and the cipher
  763. * "ADH-MY-CIPHER" look like a match for buflen=3.
  764. * So additionally check whether the cipher name found
  765. * has the correct length. We can save a strlen() call:
  766. * just checking for the '\0' at the right place is
  767. * sufficient, we have to strncmp() anyway. (We cannot
  768. * use strcmp(), because buf is not '\0' terminated.)
  769. */
  770. j = found = 0;
  771. cipher_id = 0;
  772. while (ca_list[j])
  773. {
  774. if (!strncmp(buf, ca_list[j]->name, buflen) &&
  775. (ca_list[j]->name[buflen] == '\0'))
  776. {
  777. found = 1;
  778. break;
  779. }
  780. else
  781. j++;
  782. }
  783. if (!found)
  784. break; /* ignore this entry */
  785. if (ca_list[j]->algorithm_mkey)
  786. {
  787. if (alg_mkey)
  788. {
  789. alg_mkey &= ca_list[j]->algorithm_mkey;
  790. if (!alg_mkey) { found = 0; break; }
  791. }
  792. else
  793. alg_mkey = ca_list[j]->algorithm_mkey;
  794. }
  795. if (ca_list[j]->algorithm_auth)
  796. {
  797. if (alg_auth)
  798. {
  799. alg_auth &= ca_list[j]->algorithm_auth;
  800. if (!alg_auth) { found = 0; break; }
  801. }
  802. else
  803. alg_auth = ca_list[j]->algorithm_auth;
  804. }
  805. if (ca_list[j]->algorithm_enc)
  806. {
  807. if (alg_enc)
  808. {
  809. alg_enc &= ca_list[j]->algorithm_enc;
  810. if (!alg_enc) { found = 0; break; }
  811. }
  812. else
  813. alg_enc = ca_list[j]->algorithm_enc;
  814. }
  815. if (ca_list[j]->algorithm_mac)
  816. {
  817. if (alg_mac)
  818. {
  819. alg_mac &= ca_list[j]->algorithm_mac;
  820. if (!alg_mac) { found = 0; break; }
  821. }
  822. else
  823. alg_mac = ca_list[j]->algorithm_mac;
  824. }
  825. if (ca_list[j]->algo_strength)
  826. {
  827. if (algo_strength)
  828. {
  829. algo_strength &= ca_list[j]->algo_strength;
  830. if (!algo_strength) { found = 0; break; }
  831. }
  832. else
  833. algo_strength |= ca_list[j]->algo_strength;
  834. }
  835. if (ca_list[j]->valid)
  836. {
  837. /* explicit ciphersuite found; its protocol version
  838. * does not become part of the search pattern!*/
  839. cipher_id = ca_list[j]->id;
  840. }
  841. else
  842. {
  843. /* not an explicit ciphersuite; only in this case, the
  844. * protocol version is considered part of the search pattern */
  845. if (ca_list[j]->algorithm_ssl)
  846. {
  847. if (alg_ssl)
  848. {
  849. alg_ssl &= ca_list[j]->algorithm_ssl;
  850. if (!alg_ssl) { found = 0; break; }
  851. }
  852. else
  853. alg_ssl = ca_list[j]->algorithm_ssl;
  854. }
  855. }
  856. if (!multi) break;
  857. }
  858. /*
  859. * Ok, we have the rule, now apply it
  860. */
  861. if (rule == CIPHER_SPECIAL)
  862. { /* special command */
  863. ok = 0;
  864. if ((buflen == 8) &&
  865. !strncmp(buf, "STRENGTH", 8))
  866. ok = ssl_cipher_strength_sort(head_p, tail_p);
  867. else
  868. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  869. if (ok == 0)
  870. retval = 0;
  871. /*
  872. * We do not support any "multi" options
  873. * together with "@", so throw away the
  874. * rest of the command, if any left, until
  875. * end or ':' is found.
  876. */
  877. while ((*l != '\0') && !ITEM_SEP(*l))
  878. l++;
  879. }
  880. else if (found)
  881. {
  882. ssl_cipher_apply_rule(cipher_id,
  883. alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
  884. rule, -1, in_group, head_p, tail_p);
  885. }
  886. else
  887. {
  888. while ((*l != '\0') && !ITEM_SEP(*l))
  889. l++;
  890. }
  891. }
  892. if (in_group)
  893. {
  894. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  895. retval = 0;
  896. }
  897. return(retval);
  898. }
  899. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  900. struct ssl_cipher_preference_list_st **cipher_list,
  901. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  902. const char *rule_str, CERT *c)
  903. {
  904. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  905. STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
  906. const char *rule_p;
  907. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  908. const SSL_CIPHER **ca_list = NULL;
  909. unsigned char *in_group_flags = NULL;
  910. unsigned int num_in_group_flags = 0;
  911. struct ssl_cipher_preference_list_st *pref_list = NULL;
  912. /*
  913. * Return with error if nothing to do.
  914. */
  915. if (rule_str == NULL || cipher_list == NULL)
  916. return NULL;
  917. /*
  918. * Now we have to collect the available ciphers from the compiled
  919. * in ciphers. We cannot get more than the number compiled in, so
  920. * it is used for allocation.
  921. */
  922. num_of_ciphers = ssl_method->num_ciphers();
  923. #ifdef KSSL_DEBUG
  924. printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
  925. #endif /* KSSL_DEBUG */
  926. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  927. if (co_list == NULL)
  928. {
  929. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  930. return(NULL); /* Failure */
  931. }
  932. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
  933. co_list, &head, &tail);
  934. /* Now arrange all ciphers by preference:
  935. * TODO(davidben): Compute this order once and copy it. */
  936. /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
  937. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  938. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
  939. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  940. * CHACHA20 unless there is hardware support for fast and constant-time
  941. * AES_GCM. */
  942. if (EVP_has_aes_hardware())
  943. {
  944. ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  945. ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  946. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  947. }
  948. else
  949. {
  950. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  951. ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  952. ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  953. }
  954. /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC,
  955. * RC4_128_SHA, RC4_128_MD5, 3DES_EDE_CBC_SHA. */
  956. ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  957. ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  958. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  959. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  960. ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  961. /* Temporarily enable everything else for sorting */
  962. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  963. /* Move ciphers without forward secrecy to the end. */
  964. ssl_cipher_apply_rule(0, ~(SSL_kEDH|SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  965. /* Move anonymous ciphers to the end. Usually, these will remain disabled.
  966. * (For applications that allow them, they aren't too bad, but we prefer
  967. * authenticated ciphers.)
  968. * TODO(davidben): Remove them altogether? */
  969. ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  970. /* Now disable everything (maintaining the ordering!) */
  971. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
  972. /*
  973. * We also need cipher aliases for selecting based on the rule_str.
  974. * There might be two types of entries in the rule_str: 1) names
  975. * of ciphers themselves 2) aliases for groups of ciphers.
  976. * For 1) we need the available ciphers and for 2) the cipher
  977. * groups of cipher_aliases added together in one list (otherwise
  978. * we would be happy with just the cipher_aliases table).
  979. */
  980. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  981. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  982. ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
  983. if (ca_list == NULL)
  984. {
  985. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  986. goto err;
  987. }
  988. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
  989. /*
  990. * If the rule_string begins with DEFAULT, apply the default rule
  991. * before using the (possibly available) additional rules.
  992. */
  993. ok = 1;
  994. rule_p = rule_str;
  995. if (strncmp(rule_str,"DEFAULT",7) == 0)
  996. {
  997. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  998. &head, &tail, ca_list);
  999. rule_p += 7;
  1000. if (*rule_p == ':')
  1001. rule_p++;
  1002. }
  1003. if (ok && (strlen(rule_p) > 0))
  1004. ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
  1005. OPENSSL_free((void *)ca_list); /* Not needed anymore */
  1006. if (!ok)
  1007. goto err;
  1008. /*
  1009. * Allocate new "cipherstack" for the result, return with error
  1010. * if we cannot get one.
  1011. */
  1012. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
  1013. goto err;
  1014. in_group_flags = OPENSSL_malloc(num_of_ciphers);
  1015. if (!in_group_flags)
  1016. goto err;
  1017. /*
  1018. * The cipher selection for the list is done. The ciphers are added
  1019. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1020. */
  1021. for (curr = head; curr != NULL; curr = curr->next)
  1022. {
  1023. if (curr->active)
  1024. {
  1025. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  1026. in_group_flags[num_in_group_flags++] = curr->in_group;
  1027. #ifdef CIPHER_DEBUG
  1028. printf("<%s>\n",curr->cipher->name);
  1029. #endif
  1030. }
  1031. }
  1032. OPENSSL_free(co_list); /* Not needed any longer */
  1033. co_list = NULL;
  1034. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1035. if (tmp_cipher_list == NULL)
  1036. goto err;
  1037. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1038. if (!pref_list)
  1039. goto err;
  1040. pref_list->ciphers = cipherstack;
  1041. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1042. if (!pref_list->in_group_flags)
  1043. goto err;
  1044. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1045. OPENSSL_free(in_group_flags);
  1046. in_group_flags = NULL;
  1047. if (*cipher_list != NULL)
  1048. ssl_cipher_preference_list_free(*cipher_list);
  1049. *cipher_list = pref_list;
  1050. pref_list = NULL;
  1051. if (cipher_list_by_id != NULL)
  1052. {
  1053. if (*cipher_list_by_id != NULL)
  1054. sk_SSL_CIPHER_free(*cipher_list_by_id);
  1055. *cipher_list_by_id = tmp_cipher_list;
  1056. tmp_cipher_list = NULL;
  1057. (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
  1058. sk_SSL_CIPHER_sort(*cipher_list_by_id);
  1059. }
  1060. else
  1061. {
  1062. sk_SSL_CIPHER_free(tmp_cipher_list);
  1063. tmp_cipher_list = NULL;
  1064. }
  1065. return(cipherstack);
  1066. err:
  1067. if (co_list)
  1068. OPENSSL_free(co_list);
  1069. if (in_group_flags)
  1070. OPENSSL_free(in_group_flags);
  1071. if (cipherstack)
  1072. sk_SSL_CIPHER_free(cipherstack);
  1073. if (tmp_cipher_list)
  1074. sk_SSL_CIPHER_free(tmp_cipher_list);
  1075. if (pref_list && pref_list->in_group_flags)
  1076. OPENSSL_free(pref_list->in_group_flags);
  1077. if (pref_list)
  1078. OPENSSL_free(pref_list);
  1079. return NULL;
  1080. }
  1081. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
  1082. {
  1083. const char *ver;
  1084. const char *kx,*au,*enc,*mac;
  1085. unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
  1086. #ifdef KSSL_DEBUG
  1087. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s AL=%lx/%lx/%lx/%lx/%lx\n";
  1088. #else
  1089. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
  1090. #endif /* KSSL_DEBUG */
  1091. alg_mkey = cipher->algorithm_mkey;
  1092. alg_auth = cipher->algorithm_auth;
  1093. alg_enc = cipher->algorithm_enc;
  1094. alg_mac = cipher->algorithm_mac;
  1095. alg_ssl = cipher->algorithm_ssl;
  1096. if (alg_ssl & SSL_SSLV3)
  1097. ver="SSLv3";
  1098. else if (alg_ssl & SSL_TLSV1_2)
  1099. ver="TLSv1.2";
  1100. else
  1101. ver="unknown";
  1102. switch (alg_mkey)
  1103. {
  1104. case SSL_kRSA:
  1105. kx="RSA";
  1106. break;
  1107. case SSL_kEDH:
  1108. kx="DH";
  1109. break;
  1110. case SSL_kEECDH:
  1111. kx="ECDH";
  1112. break;
  1113. case SSL_kPSK:
  1114. kx="PSK";
  1115. break;
  1116. default:
  1117. kx="unknown";
  1118. }
  1119. switch (alg_auth)
  1120. {
  1121. case SSL_aRSA:
  1122. au="RSA";
  1123. break;
  1124. case SSL_aNULL:
  1125. au="None";
  1126. break;
  1127. case SSL_aECDSA:
  1128. au="ECDSA";
  1129. break;
  1130. case SSL_aPSK:
  1131. au="PSK";
  1132. break;
  1133. default:
  1134. au="unknown";
  1135. break;
  1136. }
  1137. switch (alg_enc)
  1138. {
  1139. case SSL_3DES:
  1140. enc="3DES(168)";
  1141. break;
  1142. case SSL_RC4:
  1143. enc="RC4(128)";
  1144. break;
  1145. case SSL_AES128:
  1146. enc="AES(128)";
  1147. break;
  1148. case SSL_AES256:
  1149. enc="AES(256)";
  1150. break;
  1151. case SSL_AES128GCM:
  1152. enc="AESGCM(128)";
  1153. break;
  1154. case SSL_AES256GCM:
  1155. enc="AESGCM(256)";
  1156. break;
  1157. case SSL_CHACHA20POLY1305:
  1158. enc="ChaCha20-Poly1305";
  1159. break;
  1160. default:
  1161. enc="unknown";
  1162. break;
  1163. }
  1164. switch (alg_mac)
  1165. {
  1166. case SSL_MD5:
  1167. mac="MD5";
  1168. break;
  1169. case SSL_SHA1:
  1170. mac="SHA1";
  1171. break;
  1172. case SSL_SHA256:
  1173. mac="SHA256";
  1174. break;
  1175. case SSL_SHA384:
  1176. mac="SHA384";
  1177. break;
  1178. case SSL_AEAD:
  1179. mac="AEAD";
  1180. break;
  1181. default:
  1182. mac="unknown";
  1183. break;
  1184. }
  1185. if (buf == NULL)
  1186. {
  1187. len=128;
  1188. buf=OPENSSL_malloc(len);
  1189. if (buf == NULL) return("OPENSSL_malloc Error");
  1190. }
  1191. else if (len < 128)
  1192. return("Buffer too small");
  1193. #ifdef KSSL_DEBUG
  1194. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl);
  1195. #else
  1196. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
  1197. #endif /* KSSL_DEBUG */
  1198. return(buf);
  1199. }
  1200. /* Next three functions require non-null cipher */
  1201. int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
  1202. {
  1203. return (c->algorithm_enc & SSL_AES) != 0;
  1204. }
  1205. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
  1206. {
  1207. return (c->algorithm_mac & SSL_MD5) != 0;
  1208. }
  1209. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
  1210. {
  1211. return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
  1212. }
  1213. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
  1214. {
  1215. return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
  1216. }
  1217. const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1218. {
  1219. int i;
  1220. if (c == NULL) return("(NONE)");
  1221. i=(int)(c->id>>24L);
  1222. if (i == 3)
  1223. return("TLSv1/SSLv3");
  1224. else if (i == 2)
  1225. return("SSLv2");
  1226. else
  1227. return("unknown");
  1228. }
  1229. /* return the actual cipher being used */
  1230. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1231. {
  1232. if (c != NULL)
  1233. return(c->name);
  1234. return("(NONE)");
  1235. }
  1236. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1237. if (cipher == NULL) {
  1238. return "";
  1239. }
  1240. switch (cipher->algorithm_mkey) {
  1241. case SSL_kRSA:
  1242. return SSL_TXT_RSA;
  1243. case SSL_kEDH:
  1244. switch (cipher->algorithm_auth) {
  1245. case SSL_aRSA:
  1246. return "DHE_" SSL_TXT_RSA;
  1247. case SSL_aNULL:
  1248. return SSL_TXT_DH "_anon";
  1249. default:
  1250. return "UNKNOWN";
  1251. }
  1252. case SSL_kEECDH:
  1253. switch (cipher->algorithm_auth) {
  1254. case SSL_aECDSA:
  1255. return "ECDHE_" SSL_TXT_ECDSA;
  1256. case SSL_aRSA:
  1257. return "ECDHE_" SSL_TXT_RSA;
  1258. case SSL_aNULL:
  1259. return SSL_TXT_ECDH "_anon";
  1260. default:
  1261. return "UNKNOWN";
  1262. }
  1263. default:
  1264. return "UNKNOWN";
  1265. }
  1266. }
  1267. /* number of bits for symmetric cipher */
  1268. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1269. {
  1270. int ret=0;
  1271. if (c != NULL)
  1272. {
  1273. if (alg_bits != NULL) *alg_bits = c->alg_bits;
  1274. ret = c->strength_bits;
  1275. }
  1276. return(ret);
  1277. }
  1278. unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
  1279. {
  1280. return c->id;
  1281. }
  1282. void *SSL_COMP_get_compression_methods(void)
  1283. {
  1284. return NULL;
  1285. }
  1286. int SSL_COMP_add_compression_method(int id, void *cm)
  1287. {
  1288. return 1;
  1289. }
  1290. const char *SSL_COMP_get_name(const void *comp)
  1291. {
  1292. return NULL;
  1293. }
  1294. /* For a cipher return the index corresponding to the certificate type */
  1295. int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
  1296. {
  1297. unsigned long alg_a = c->algorithm_auth;
  1298. if (alg_a & SSL_aECDSA)
  1299. return SSL_PKEY_ECC;
  1300. else if (alg_a & SSL_aRSA)
  1301. return SSL_PKEY_RSA_ENC;
  1302. return -1;
  1303. }
  1304. /* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
  1305. * server public key in the key exchange, sent in a server Certificate
  1306. * message. Otherwise it returns 0. */
  1307. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
  1308. {
  1309. /* Anonymous ciphers do not include a server certificate. */
  1310. if (cipher->algorithm_auth & SSL_aNULL)
  1311. return 0;
  1312. /* Neither do PSK ciphers, except for RSA_PSK. */
  1313. if ((cipher->algorithm_auth & SSL_aPSK) &&
  1314. !(cipher->algorithm_mkey & SSL_kRSA))
  1315. return 0;
  1316. /* All other ciphers include it. */
  1317. return 1;
  1318. }
  1319. /* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
  1320. * requires a ServerKeyExchange message. Otherwise it returns 0.
  1321. *
  1322. * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
  1323. * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
  1324. * to communicate a psk_identity_hint, so it is optional.
  1325. *
  1326. * Also, as implemented, the RSA key exchange takes an optional
  1327. * ServerKeyExchange containing a signed ephemeral RSA encryption key.
  1328. *
  1329. * TODO(davidben): Can we remove the RSA one? This is a remnant of
  1330. * RSA_EXPORT ciphers which required this (it was used to generate an
  1331. * ephemeral 512-bit RSA encryption key), but it's allowed for all RSA
  1332. * ciphers. */
  1333. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
  1334. {
  1335. /* Ephemeral Diffie-Hellman key exchanges require a
  1336. * ServerKeyExchange. */
  1337. if (cipher->algorithm_mkey & SSL_kEDH ||
  1338. cipher->algorithm_mkey & SSL_kEECDH)
  1339. return 1;
  1340. /* It is optional in all others. */
  1341. return 0;
  1342. }