25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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