You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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