Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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