Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

1470 wiersze
39 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. }
  405. }
  406. /*
  407. * Prepare linked list from list entries
  408. */
  409. if (co_list_num > 0)
  410. {
  411. co_list[0].prev = NULL;
  412. if (co_list_num > 1)
  413. {
  414. co_list[0].next = &co_list[1];
  415. for (i = 1; i < co_list_num - 1; i++)
  416. {
  417. co_list[i].prev = &co_list[i - 1];
  418. co_list[i].next = &co_list[i + 1];
  419. }
  420. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  421. }
  422. co_list[co_list_num - 1].next = NULL;
  423. *head_p = &co_list[0];
  424. *tail_p = &co_list[co_list_num - 1];
  425. }
  426. }
  427. static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
  428. int num_of_group_aliases,
  429. CIPHER_ORDER *head)
  430. {
  431. CIPHER_ORDER *ciph_curr;
  432. const SSL_CIPHER **ca_curr;
  433. int i;
  434. /*
  435. * First, add the real ciphers as already collected
  436. */
  437. ciph_curr = head;
  438. ca_curr = ca_list;
  439. while (ciph_curr != NULL)
  440. {
  441. *ca_curr = ciph_curr->cipher;
  442. ca_curr++;
  443. ciph_curr = ciph_curr->next;
  444. }
  445. /*
  446. * Now we add the available ones from the cipher_aliases[] table.
  447. * They represent either one or more algorithms, some of which
  448. * in any affected category must be supported (set in enabled_mask),
  449. * or represent a cipher strength value (will be added in any case because algorithms=0).
  450. */
  451. for (i = 0; i < num_of_group_aliases; i++)
  452. {
  453. *ca_curr = cipher_aliases + i;
  454. ca_curr++;
  455. }
  456. *ca_curr = NULL; /* end of list */
  457. }
  458. static void ssl_cipher_apply_rule(unsigned long cipher_id,
  459. unsigned long alg_mkey, unsigned long alg_auth,
  460. unsigned long alg_enc, unsigned long alg_mac,
  461. unsigned long alg_ssl,
  462. unsigned long algo_strength,
  463. int rule, int strength_bits, int in_group,
  464. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  465. {
  466. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  467. const SSL_CIPHER *cp;
  468. int reverse = 0;
  469. #ifdef CIPHER_DEBUG
  470. printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
  471. rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
  472. #endif
  473. if (rule == CIPHER_DEL)
  474. reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
  475. head = *head_p;
  476. tail = *tail_p;
  477. if (reverse)
  478. {
  479. next = tail;
  480. last = head;
  481. }
  482. else
  483. {
  484. next = head;
  485. last = tail;
  486. }
  487. curr = NULL;
  488. for (;;)
  489. {
  490. if (curr == last) break;
  491. curr = next;
  492. if (curr == NULL) break;
  493. next = reverse ? curr->prev : curr->next;
  494. cp = curr->cipher;
  495. /*
  496. * Selection criteria is either the value of strength_bits
  497. * or the algorithms used.
  498. */
  499. if (strength_bits >= 0)
  500. {
  501. if (strength_bits != cp->strength_bits)
  502. continue;
  503. }
  504. else
  505. {
  506. #ifdef CIPHER_DEBUG
  507. 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);
  508. #endif
  509. if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
  510. continue;
  511. if (alg_auth && !(alg_auth & cp->algorithm_auth))
  512. continue;
  513. if (alg_enc && !(alg_enc & cp->algorithm_enc))
  514. continue;
  515. if (alg_mac && !(alg_mac & cp->algorithm_mac))
  516. continue;
  517. if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
  518. continue;
  519. if (algo_strength && !(algo_strength & cp->algo_strength))
  520. continue;
  521. }
  522. #ifdef CIPHER_DEBUG
  523. printf("Action = %d\n", rule);
  524. #endif
  525. /* add the cipher if it has not been added yet. */
  526. if (rule == CIPHER_ADD)
  527. {
  528. /* reverse == 0 */
  529. if (!curr->active)
  530. {
  531. ll_append_tail(&head, curr, &tail);
  532. curr->active = 1;
  533. curr->in_group = in_group;
  534. }
  535. }
  536. /* Move the added cipher to this location */
  537. else if (rule == CIPHER_ORD)
  538. {
  539. /* reverse == 0 */
  540. if (curr->active)
  541. {
  542. ll_append_tail(&head, curr, &tail);
  543. curr->in_group = 0;
  544. }
  545. }
  546. else if (rule == CIPHER_DEL)
  547. {
  548. /* reverse == 1 */
  549. if (curr->active)
  550. {
  551. /* most recently deleted ciphersuites get best positions
  552. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  553. * works in reverse to maintain the order) */
  554. ll_append_head(&head, curr, &tail);
  555. curr->active = 0;
  556. curr->in_group = 0;
  557. }
  558. }
  559. else if (rule == CIPHER_KILL)
  560. {
  561. /* reverse == 0 */
  562. if (head == curr)
  563. head = curr->next;
  564. else
  565. curr->prev->next = curr->next;
  566. if (tail == curr)
  567. tail = curr->prev;
  568. curr->active = 0;
  569. if (curr->next != NULL)
  570. curr->next->prev = curr->prev;
  571. if (curr->prev != NULL)
  572. curr->prev->next = curr->next;
  573. curr->next = NULL;
  574. curr->prev = NULL;
  575. }
  576. }
  577. *head_p = head;
  578. *tail_p = tail;
  579. }
  580. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  581. CIPHER_ORDER **tail_p)
  582. {
  583. int max_strength_bits, i, *number_uses;
  584. CIPHER_ORDER *curr;
  585. /*
  586. * This routine sorts the ciphers with descending strength. The sorting
  587. * must keep the pre-sorted sequence, so we apply the normal sorting
  588. * routine as '+' movement to the end of the list.
  589. */
  590. max_strength_bits = 0;
  591. curr = *head_p;
  592. while (curr != NULL)
  593. {
  594. if (curr->active &&
  595. (curr->cipher->strength_bits > max_strength_bits))
  596. max_strength_bits = curr->cipher->strength_bits;
  597. curr = curr->next;
  598. }
  599. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  600. if (!number_uses)
  601. {
  602. OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
  603. return(0);
  604. }
  605. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  606. /*
  607. * Now find the strength_bits values actually used
  608. */
  609. curr = *head_p;
  610. while (curr != NULL)
  611. {
  612. if (curr->active)
  613. number_uses[curr->cipher->strength_bits]++;
  614. curr = curr->next;
  615. }
  616. /*
  617. * Go through the list of used strength_bits values in descending
  618. * order.
  619. */
  620. for (i = max_strength_bits; i >= 0; i--)
  621. if (number_uses[i] > 0)
  622. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
  623. OPENSSL_free(number_uses);
  624. return(1);
  625. }
  626. static int ssl_cipher_process_rulestr(const char *rule_str,
  627. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
  628. const SSL_CIPHER **ca_list)
  629. {
  630. unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
  631. const char *l, *buf;
  632. int j, multi, found, rule, retval, ok, buflen, in_group = 0,
  633. has_group = 0;
  634. unsigned long cipher_id = 0;
  635. char ch;
  636. retval = 1;
  637. l = rule_str;
  638. for (;;)
  639. {
  640. ch = *l;
  641. if (ch == '\0')
  642. break; /* done */
  643. if (in_group)
  644. {
  645. if (ch == ']')
  646. {
  647. if (!in_group)
  648. {
  649. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
  650. retval = found = in_group = 0;
  651. break;
  652. }
  653. if (*tail_p)
  654. (*tail_p)->in_group = 0;
  655. in_group = 0;
  656. l++;
  657. continue;
  658. }
  659. if (ch == '|')
  660. { rule = CIPHER_ADD; l++; continue; }
  661. else if (!(ch >= 'a' && ch <= 'z') &&
  662. !(ch >= 'A' && ch <= 'Z') &&
  663. !(ch >= '0' && ch <= '9'))
  664. {
  665. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  666. retval = found = in_group = 0;
  667. break;
  668. }
  669. else
  670. rule = CIPHER_ADD;
  671. }
  672. else if (ch == '-')
  673. { rule = CIPHER_DEL; l++; }
  674. else if (ch == '+')
  675. { rule = CIPHER_ORD; l++; }
  676. else if (ch == '!')
  677. { rule = CIPHER_KILL; l++; }
  678. else if (ch == '@')
  679. { rule = CIPHER_SPECIAL; l++; }
  680. else if (ch == '[')
  681. {
  682. if (in_group)
  683. {
  684. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
  685. retval = found = in_group = 0;
  686. break;
  687. }
  688. in_group = 1;
  689. has_group = 1;
  690. l++;
  691. continue;
  692. }
  693. else
  694. { rule = CIPHER_ADD; }
  695. /* If preference groups are enabled, the only legal
  696. * operator is +. Otherwise the in_group bits will get
  697. * mixed up. */
  698. if (has_group && rule != CIPHER_ADD)
  699. {
  700. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  701. retval = found = in_group = 0;
  702. break;
  703. }
  704. if (ITEM_SEP(ch))
  705. {
  706. l++;
  707. continue;
  708. }
  709. alg_mkey = 0;
  710. alg_auth = 0;
  711. alg_enc = 0;
  712. alg_mac = 0;
  713. alg_ssl = 0;
  714. algo_strength = 0;
  715. for (;;)
  716. {
  717. ch = *l;
  718. buf = l;
  719. buflen = 0;
  720. while ( ((ch >= 'A') && (ch <= 'Z')) ||
  721. ((ch >= '0') && (ch <= '9')) ||
  722. ((ch >= 'a') && (ch <= 'z')) ||
  723. (ch == '-') || (ch == '.'))
  724. {
  725. ch = *(++l);
  726. buflen++;
  727. }
  728. if (buflen == 0)
  729. {
  730. /*
  731. * We hit something we cannot deal with,
  732. * it is no command or separator nor
  733. * alphanumeric, so we call this an error.
  734. */
  735. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  736. retval = found = in_group = 0;
  737. l++;
  738. break;
  739. }
  740. if (rule == CIPHER_SPECIAL)
  741. {
  742. found = 0; /* unused -- avoid compiler warning */
  743. break; /* special treatment */
  744. }
  745. /* check for multi-part specification */
  746. if (ch == '+')
  747. {
  748. multi=1;
  749. l++;
  750. }
  751. else
  752. multi=0;
  753. /*
  754. * Now search for the cipher alias in the ca_list. Be careful
  755. * with the strncmp, because the "buflen" limitation
  756. * will make the rule "ADH:SOME" and the cipher
  757. * "ADH-MY-CIPHER" look like a match for buflen=3.
  758. * So additionally check whether the cipher name found
  759. * has the correct length. We can save a strlen() call:
  760. * just checking for the '\0' at the right place is
  761. * sufficient, we have to strncmp() anyway. (We cannot
  762. * use strcmp(), because buf is not '\0' terminated.)
  763. */
  764. j = found = 0;
  765. cipher_id = 0;
  766. while (ca_list[j])
  767. {
  768. if (!strncmp(buf, ca_list[j]->name, buflen) &&
  769. (ca_list[j]->name[buflen] == '\0'))
  770. {
  771. found = 1;
  772. break;
  773. }
  774. else
  775. j++;
  776. }
  777. if (!found)
  778. break; /* ignore this entry */
  779. if (ca_list[j]->algorithm_mkey)
  780. {
  781. if (alg_mkey)
  782. {
  783. alg_mkey &= ca_list[j]->algorithm_mkey;
  784. if (!alg_mkey) { found = 0; break; }
  785. }
  786. else
  787. alg_mkey = ca_list[j]->algorithm_mkey;
  788. }
  789. if (ca_list[j]->algorithm_auth)
  790. {
  791. if (alg_auth)
  792. {
  793. alg_auth &= ca_list[j]->algorithm_auth;
  794. if (!alg_auth) { found = 0; break; }
  795. }
  796. else
  797. alg_auth = ca_list[j]->algorithm_auth;
  798. }
  799. if (ca_list[j]->algorithm_enc)
  800. {
  801. if (alg_enc)
  802. {
  803. alg_enc &= ca_list[j]->algorithm_enc;
  804. if (!alg_enc) { found = 0; break; }
  805. }
  806. else
  807. alg_enc = ca_list[j]->algorithm_enc;
  808. }
  809. if (ca_list[j]->algorithm_mac)
  810. {
  811. if (alg_mac)
  812. {
  813. alg_mac &= ca_list[j]->algorithm_mac;
  814. if (!alg_mac) { found = 0; break; }
  815. }
  816. else
  817. alg_mac = ca_list[j]->algorithm_mac;
  818. }
  819. if (ca_list[j]->algo_strength)
  820. {
  821. if (algo_strength)
  822. {
  823. algo_strength &= ca_list[j]->algo_strength;
  824. if (!algo_strength) { found = 0; break; }
  825. }
  826. else
  827. algo_strength |= ca_list[j]->algo_strength;
  828. }
  829. if (ca_list[j]->valid)
  830. {
  831. /* explicit ciphersuite found; its protocol version
  832. * does not become part of the search pattern!*/
  833. cipher_id = ca_list[j]->id;
  834. }
  835. else
  836. {
  837. /* not an explicit ciphersuite; only in this case, the
  838. * protocol version is considered part of the search pattern */
  839. if (ca_list[j]->algorithm_ssl)
  840. {
  841. if (alg_ssl)
  842. {
  843. alg_ssl &= ca_list[j]->algorithm_ssl;
  844. if (!alg_ssl) { found = 0; break; }
  845. }
  846. else
  847. alg_ssl = ca_list[j]->algorithm_ssl;
  848. }
  849. }
  850. if (!multi) break;
  851. }
  852. /*
  853. * Ok, we have the rule, now apply it
  854. */
  855. if (rule == CIPHER_SPECIAL)
  856. { /* special command */
  857. ok = 0;
  858. if ((buflen == 8) &&
  859. !strncmp(buf, "STRENGTH", 8))
  860. ok = ssl_cipher_strength_sort(head_p, tail_p);
  861. else
  862. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  863. if (ok == 0)
  864. retval = 0;
  865. /*
  866. * We do not support any "multi" options
  867. * together with "@", so throw away the
  868. * rest of the command, if any left, until
  869. * end or ':' is found.
  870. */
  871. while ((*l != '\0') && !ITEM_SEP(*l))
  872. l++;
  873. }
  874. else if (found)
  875. {
  876. ssl_cipher_apply_rule(cipher_id,
  877. alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
  878. rule, -1, in_group, head_p, tail_p);
  879. }
  880. else
  881. {
  882. while ((*l != '\0') && !ITEM_SEP(*l))
  883. l++;
  884. }
  885. }
  886. if (in_group)
  887. {
  888. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  889. retval = 0;
  890. }
  891. return(retval);
  892. }
  893. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  894. struct ssl_cipher_preference_list_st **cipher_list,
  895. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  896. const char *rule_str, CERT *c)
  897. {
  898. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  899. STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
  900. const char *rule_p;
  901. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  902. const SSL_CIPHER **ca_list = NULL;
  903. unsigned char *in_group_flags = NULL;
  904. unsigned int num_in_group_flags = 0;
  905. struct ssl_cipher_preference_list_st *pref_list = NULL;
  906. /*
  907. * Return with error if nothing to do.
  908. */
  909. if (rule_str == NULL || cipher_list == NULL)
  910. return NULL;
  911. /*
  912. * Now we have to collect the available ciphers from the compiled
  913. * in ciphers. We cannot get more than the number compiled in, so
  914. * it is used for allocation.
  915. */
  916. num_of_ciphers = ssl_method->num_ciphers();
  917. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  918. if (co_list == NULL)
  919. {
  920. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  921. return(NULL); /* Failure */
  922. }
  923. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
  924. co_list, &head, &tail);
  925. /* Now arrange all ciphers by preference:
  926. * TODO(davidben): Compute this order once and copy it. */
  927. /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
  928. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  929. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
  930. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  931. * CHACHA20 unless there is hardware support for fast and constant-time
  932. * AES_GCM. */
  933. if (EVP_has_aes_hardware())
  934. {
  935. ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  936. ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  937. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  938. }
  939. else
  940. {
  941. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  942. ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  943. ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  944. }
  945. /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC,
  946. * RC4_128_SHA, RC4_128_MD5, 3DES_EDE_CBC_SHA. */
  947. ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  948. ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  949. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  950. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  951. ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  952. /* Temporarily enable everything else for sorting */
  953. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  954. /* Move ciphers without forward secrecy to the end. */
  955. ssl_cipher_apply_rule(0, ~(SSL_kEDH|SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  956. /* Move anonymous ciphers to the end. Usually, these will remain disabled.
  957. * (For applications that allow them, they aren't too bad, but we prefer
  958. * authenticated ciphers.)
  959. * TODO(davidben): Remove them altogether? */
  960. ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  961. /* Now disable everything (maintaining the ordering!) */
  962. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
  963. /*
  964. * We also need cipher aliases for selecting based on the rule_str.
  965. * There might be two types of entries in the rule_str: 1) names
  966. * of ciphers themselves 2) aliases for groups of ciphers.
  967. * For 1) we need the available ciphers and for 2) the cipher
  968. * groups of cipher_aliases added together in one list (otherwise
  969. * we would be happy with just the cipher_aliases table).
  970. */
  971. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  972. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  973. ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
  974. if (ca_list == NULL)
  975. {
  976. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  977. goto err;
  978. }
  979. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
  980. /*
  981. * If the rule_string begins with DEFAULT, apply the default rule
  982. * before using the (possibly available) additional rules.
  983. */
  984. ok = 1;
  985. rule_p = rule_str;
  986. if (strncmp(rule_str,"DEFAULT",7) == 0)
  987. {
  988. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  989. &head, &tail, ca_list);
  990. rule_p += 7;
  991. if (*rule_p == ':')
  992. rule_p++;
  993. }
  994. if (ok && (strlen(rule_p) > 0))
  995. ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
  996. OPENSSL_free((void *)ca_list); /* Not needed anymore */
  997. if (!ok)
  998. goto err;
  999. /*
  1000. * Allocate new "cipherstack" for the result, return with error
  1001. * if we cannot get one.
  1002. */
  1003. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
  1004. goto err;
  1005. in_group_flags = OPENSSL_malloc(num_of_ciphers);
  1006. if (!in_group_flags)
  1007. goto err;
  1008. /*
  1009. * The cipher selection for the list is done. The ciphers are added
  1010. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1011. */
  1012. for (curr = head; curr != NULL; curr = curr->next)
  1013. {
  1014. if (curr->active)
  1015. {
  1016. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  1017. in_group_flags[num_in_group_flags++] = curr->in_group;
  1018. #ifdef CIPHER_DEBUG
  1019. printf("<%s>\n",curr->cipher->name);
  1020. #endif
  1021. }
  1022. }
  1023. OPENSSL_free(co_list); /* Not needed any longer */
  1024. co_list = NULL;
  1025. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1026. if (tmp_cipher_list == NULL)
  1027. goto err;
  1028. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1029. if (!pref_list)
  1030. goto err;
  1031. pref_list->ciphers = cipherstack;
  1032. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1033. if (!pref_list->in_group_flags)
  1034. goto err;
  1035. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1036. OPENSSL_free(in_group_flags);
  1037. in_group_flags = NULL;
  1038. if (*cipher_list != NULL)
  1039. ssl_cipher_preference_list_free(*cipher_list);
  1040. *cipher_list = pref_list;
  1041. pref_list = NULL;
  1042. if (cipher_list_by_id != NULL)
  1043. {
  1044. if (*cipher_list_by_id != NULL)
  1045. sk_SSL_CIPHER_free(*cipher_list_by_id);
  1046. *cipher_list_by_id = tmp_cipher_list;
  1047. tmp_cipher_list = NULL;
  1048. (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
  1049. sk_SSL_CIPHER_sort(*cipher_list_by_id);
  1050. }
  1051. else
  1052. {
  1053. sk_SSL_CIPHER_free(tmp_cipher_list);
  1054. tmp_cipher_list = NULL;
  1055. }
  1056. return(cipherstack);
  1057. err:
  1058. if (co_list)
  1059. OPENSSL_free(co_list);
  1060. if (in_group_flags)
  1061. OPENSSL_free(in_group_flags);
  1062. if (cipherstack)
  1063. sk_SSL_CIPHER_free(cipherstack);
  1064. if (tmp_cipher_list)
  1065. sk_SSL_CIPHER_free(tmp_cipher_list);
  1066. if (pref_list && pref_list->in_group_flags)
  1067. OPENSSL_free(pref_list->in_group_flags);
  1068. if (pref_list)
  1069. OPENSSL_free(pref_list);
  1070. return NULL;
  1071. }
  1072. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
  1073. {
  1074. const char *ver;
  1075. const char *kx,*au,*enc,*mac;
  1076. unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
  1077. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
  1078. alg_mkey = cipher->algorithm_mkey;
  1079. alg_auth = cipher->algorithm_auth;
  1080. alg_enc = cipher->algorithm_enc;
  1081. alg_mac = cipher->algorithm_mac;
  1082. alg_ssl = cipher->algorithm_ssl;
  1083. if (alg_ssl & SSL_SSLV3)
  1084. ver="SSLv3";
  1085. else if (alg_ssl & SSL_TLSV1_2)
  1086. ver="TLSv1.2";
  1087. else
  1088. ver="unknown";
  1089. switch (alg_mkey)
  1090. {
  1091. case SSL_kRSA:
  1092. kx="RSA";
  1093. break;
  1094. case SSL_kEDH:
  1095. kx="DH";
  1096. break;
  1097. case SSL_kEECDH:
  1098. kx="ECDH";
  1099. break;
  1100. case SSL_kPSK:
  1101. kx="PSK";
  1102. break;
  1103. default:
  1104. kx="unknown";
  1105. }
  1106. switch (alg_auth)
  1107. {
  1108. case SSL_aRSA:
  1109. au="RSA";
  1110. break;
  1111. case SSL_aNULL:
  1112. au="None";
  1113. break;
  1114. case SSL_aECDSA:
  1115. au="ECDSA";
  1116. break;
  1117. case SSL_aPSK:
  1118. au="PSK";
  1119. break;
  1120. default:
  1121. au="unknown";
  1122. break;
  1123. }
  1124. switch (alg_enc)
  1125. {
  1126. case SSL_3DES:
  1127. enc="3DES(168)";
  1128. break;
  1129. case SSL_RC4:
  1130. enc="RC4(128)";
  1131. break;
  1132. case SSL_AES128:
  1133. enc="AES(128)";
  1134. break;
  1135. case SSL_AES256:
  1136. enc="AES(256)";
  1137. break;
  1138. case SSL_AES128GCM:
  1139. enc="AESGCM(128)";
  1140. break;
  1141. case SSL_AES256GCM:
  1142. enc="AESGCM(256)";
  1143. break;
  1144. case SSL_CHACHA20POLY1305:
  1145. enc="ChaCha20-Poly1305";
  1146. break;
  1147. default:
  1148. enc="unknown";
  1149. break;
  1150. }
  1151. switch (alg_mac)
  1152. {
  1153. case SSL_MD5:
  1154. mac="MD5";
  1155. break;
  1156. case SSL_SHA1:
  1157. mac="SHA1";
  1158. break;
  1159. case SSL_SHA256:
  1160. mac="SHA256";
  1161. break;
  1162. case SSL_SHA384:
  1163. mac="SHA384";
  1164. break;
  1165. case SSL_AEAD:
  1166. mac="AEAD";
  1167. break;
  1168. default:
  1169. mac="unknown";
  1170. break;
  1171. }
  1172. if (buf == NULL)
  1173. {
  1174. len=128;
  1175. buf=OPENSSL_malloc(len);
  1176. if (buf == NULL) return("OPENSSL_malloc Error");
  1177. }
  1178. else if (len < 128)
  1179. return("Buffer too small");
  1180. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
  1181. return(buf);
  1182. }
  1183. /* Next three functions require non-null cipher */
  1184. int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
  1185. {
  1186. return (c->algorithm_enc & SSL_AES) != 0;
  1187. }
  1188. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
  1189. {
  1190. return (c->algorithm_mac & SSL_MD5) != 0;
  1191. }
  1192. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
  1193. {
  1194. return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
  1195. }
  1196. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
  1197. {
  1198. return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
  1199. }
  1200. const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1201. {
  1202. int i;
  1203. if (c == NULL) return("(NONE)");
  1204. i=(int)(c->id>>24L);
  1205. if (i == 3)
  1206. return("TLSv1/SSLv3");
  1207. else if (i == 2)
  1208. return("SSLv2");
  1209. else
  1210. return("unknown");
  1211. }
  1212. /* return the actual cipher being used */
  1213. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1214. {
  1215. if (c != NULL)
  1216. return(c->name);
  1217. return("(NONE)");
  1218. }
  1219. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1220. if (cipher == NULL) {
  1221. return "";
  1222. }
  1223. switch (cipher->algorithm_mkey) {
  1224. case SSL_kRSA:
  1225. return SSL_TXT_RSA;
  1226. case SSL_kEDH:
  1227. switch (cipher->algorithm_auth) {
  1228. case SSL_aRSA:
  1229. return "DHE_" SSL_TXT_RSA;
  1230. case SSL_aNULL:
  1231. return SSL_TXT_DH "_anon";
  1232. default:
  1233. return "UNKNOWN";
  1234. }
  1235. case SSL_kEECDH:
  1236. switch (cipher->algorithm_auth) {
  1237. case SSL_aECDSA:
  1238. return "ECDHE_" SSL_TXT_ECDSA;
  1239. case SSL_aRSA:
  1240. return "ECDHE_" SSL_TXT_RSA;
  1241. case SSL_aNULL:
  1242. return SSL_TXT_ECDH "_anon";
  1243. default:
  1244. return "UNKNOWN";
  1245. }
  1246. default:
  1247. return "UNKNOWN";
  1248. }
  1249. }
  1250. /* number of bits for symmetric cipher */
  1251. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1252. {
  1253. int ret=0;
  1254. if (c != NULL)
  1255. {
  1256. if (alg_bits != NULL) *alg_bits = c->alg_bits;
  1257. ret = c->strength_bits;
  1258. }
  1259. return(ret);
  1260. }
  1261. unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
  1262. {
  1263. return c->id;
  1264. }
  1265. void *SSL_COMP_get_compression_methods(void)
  1266. {
  1267. return NULL;
  1268. }
  1269. int SSL_COMP_add_compression_method(int id, void *cm)
  1270. {
  1271. return 1;
  1272. }
  1273. const char *SSL_COMP_get_name(const void *comp)
  1274. {
  1275. return NULL;
  1276. }
  1277. /* For a cipher return the index corresponding to the certificate type */
  1278. int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
  1279. {
  1280. unsigned long alg_a = c->algorithm_auth;
  1281. if (alg_a & SSL_aECDSA)
  1282. return SSL_PKEY_ECC;
  1283. else if (alg_a & SSL_aRSA)
  1284. return SSL_PKEY_RSA_ENC;
  1285. return -1;
  1286. }
  1287. /* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
  1288. * server public key in the key exchange, sent in a server Certificate
  1289. * message. Otherwise it returns 0. */
  1290. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
  1291. {
  1292. /* Anonymous ciphers do not include a server certificate. */
  1293. if (cipher->algorithm_auth & SSL_aNULL)
  1294. return 0;
  1295. /* Neither do PSK ciphers, except for RSA_PSK. */
  1296. if ((cipher->algorithm_auth & SSL_aPSK) &&
  1297. !(cipher->algorithm_mkey & SSL_kRSA))
  1298. return 0;
  1299. /* All other ciphers include it. */
  1300. return 1;
  1301. }
  1302. /* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
  1303. * requires a ServerKeyExchange message. Otherwise it returns 0.
  1304. *
  1305. * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
  1306. * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
  1307. * to communicate a psk_identity_hint, so it is optional. */
  1308. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
  1309. {
  1310. /* Ephemeral Diffie-Hellman key exchanges require a
  1311. * ServerKeyExchange. */
  1312. if (cipher->algorithm_mkey & SSL_kEDH ||
  1313. cipher->algorithm_mkey & SSL_kEECDH)
  1314. return 1;
  1315. /* It is optional in all others. */
  1316. return 0;
  1317. }