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.
 
 
 
 
 
 

1778 rivejä
49 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 <openssl/ssl.h>
  141. #include <assert.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. #include "../crypto/internal.h"
  151. /* kCiphers is an array of all supported ciphers, sorted by id. */
  152. static const SSL_CIPHER kCiphers[] = {
  153. /* The RSA ciphers */
  154. /* Cipher 02 */
  155. {
  156. SSL3_TXT_RSA_NULL_SHA,
  157. SSL3_CK_RSA_NULL_SHA,
  158. SSL_kRSA,
  159. SSL_aRSA,
  160. SSL_eNULL,
  161. SSL_SHA1,
  162. SSL_HANDSHAKE_MAC_DEFAULT,
  163. },
  164. /* Cipher 0A */
  165. {
  166. SSL3_TXT_RSA_DES_192_CBC3_SHA,
  167. SSL3_CK_RSA_DES_192_CBC3_SHA,
  168. SSL_kRSA,
  169. SSL_aRSA,
  170. SSL_3DES,
  171. SSL_SHA1,
  172. SSL_HANDSHAKE_MAC_DEFAULT,
  173. },
  174. /* New AES ciphersuites */
  175. /* Cipher 2F */
  176. {
  177. TLS1_TXT_RSA_WITH_AES_128_SHA,
  178. TLS1_CK_RSA_WITH_AES_128_SHA,
  179. SSL_kRSA,
  180. SSL_aRSA,
  181. SSL_AES128,
  182. SSL_SHA1,
  183. SSL_HANDSHAKE_MAC_DEFAULT,
  184. },
  185. /* Cipher 35 */
  186. {
  187. TLS1_TXT_RSA_WITH_AES_256_SHA,
  188. TLS1_CK_RSA_WITH_AES_256_SHA,
  189. SSL_kRSA,
  190. SSL_aRSA,
  191. SSL_AES256,
  192. SSL_SHA1,
  193. SSL_HANDSHAKE_MAC_DEFAULT,
  194. },
  195. /* TLS v1.2 ciphersuites */
  196. /* Cipher 3C */
  197. {
  198. TLS1_TXT_RSA_WITH_AES_128_SHA256,
  199. TLS1_CK_RSA_WITH_AES_128_SHA256,
  200. SSL_kRSA,
  201. SSL_aRSA,
  202. SSL_AES128,
  203. SSL_SHA256,
  204. SSL_HANDSHAKE_MAC_SHA256,
  205. },
  206. /* Cipher 3D */
  207. {
  208. TLS1_TXT_RSA_WITH_AES_256_SHA256,
  209. TLS1_CK_RSA_WITH_AES_256_SHA256,
  210. SSL_kRSA,
  211. SSL_aRSA,
  212. SSL_AES256,
  213. SSL_SHA256,
  214. SSL_HANDSHAKE_MAC_SHA256,
  215. },
  216. /* PSK cipher suites. */
  217. /* Cipher 8C */
  218. {
  219. TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
  220. TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
  221. SSL_kPSK,
  222. SSL_aPSK,
  223. SSL_AES128,
  224. SSL_SHA1,
  225. SSL_HANDSHAKE_MAC_DEFAULT,
  226. },
  227. /* Cipher 8D */
  228. {
  229. TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
  230. TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
  231. SSL_kPSK,
  232. SSL_aPSK,
  233. SSL_AES256,
  234. SSL_SHA1,
  235. SSL_HANDSHAKE_MAC_DEFAULT,
  236. },
  237. /* GCM ciphersuites from RFC5288 */
  238. /* Cipher 9C */
  239. {
  240. TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
  241. TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  242. SSL_kRSA,
  243. SSL_aRSA,
  244. SSL_AES128GCM,
  245. SSL_AEAD,
  246. SSL_HANDSHAKE_MAC_SHA256,
  247. },
  248. /* Cipher 9D */
  249. {
  250. TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
  251. TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  252. SSL_kRSA,
  253. SSL_aRSA,
  254. SSL_AES256GCM,
  255. SSL_AEAD,
  256. SSL_HANDSHAKE_MAC_SHA384,
  257. },
  258. /* TLS 1.3 suites. */
  259. /* Cipher 1301 */
  260. {
  261. TLS1_TXT_AES_128_GCM_SHA256,
  262. TLS1_CK_AES_128_GCM_SHA256,
  263. SSL_kGENERIC,
  264. SSL_aGENERIC,
  265. SSL_AES128GCM,
  266. SSL_AEAD,
  267. SSL_HANDSHAKE_MAC_SHA256,
  268. },
  269. /* Cipher 1302 */
  270. {
  271. TLS1_TXT_AES_256_GCM_SHA384,
  272. TLS1_CK_AES_256_GCM_SHA384,
  273. SSL_kGENERIC,
  274. SSL_aGENERIC,
  275. SSL_AES256GCM,
  276. SSL_AEAD,
  277. SSL_HANDSHAKE_MAC_SHA384,
  278. },
  279. /* Cipher 1303 */
  280. {
  281. TLS1_TXT_CHACHA20_POLY1305_SHA256,
  282. TLS1_CK_CHACHA20_POLY1305_SHA256,
  283. SSL_kGENERIC,
  284. SSL_aGENERIC,
  285. SSL_CHACHA20POLY1305,
  286. SSL_AEAD,
  287. SSL_HANDSHAKE_MAC_SHA256,
  288. },
  289. /* Cipher C009 */
  290. {
  291. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  292. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  293. SSL_kECDHE,
  294. SSL_aECDSA,
  295. SSL_AES128,
  296. SSL_SHA1,
  297. SSL_HANDSHAKE_MAC_DEFAULT,
  298. },
  299. /* Cipher C00A */
  300. {
  301. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  302. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  303. SSL_kECDHE,
  304. SSL_aECDSA,
  305. SSL_AES256,
  306. SSL_SHA1,
  307. SSL_HANDSHAKE_MAC_DEFAULT,
  308. },
  309. /* Cipher C013 */
  310. {
  311. TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  312. TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  313. SSL_kECDHE,
  314. SSL_aRSA,
  315. SSL_AES128,
  316. SSL_SHA1,
  317. SSL_HANDSHAKE_MAC_DEFAULT,
  318. },
  319. /* Cipher C014 */
  320. {
  321. TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  322. TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  323. SSL_kECDHE,
  324. SSL_aRSA,
  325. SSL_AES256,
  326. SSL_SHA1,
  327. SSL_HANDSHAKE_MAC_DEFAULT,
  328. },
  329. /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
  330. /* Cipher C023 */
  331. {
  332. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
  333. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
  334. SSL_kECDHE,
  335. SSL_aECDSA,
  336. SSL_AES128,
  337. SSL_SHA256,
  338. SSL_HANDSHAKE_MAC_SHA256,
  339. },
  340. /* Cipher C024 */
  341. {
  342. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
  343. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
  344. SSL_kECDHE,
  345. SSL_aECDSA,
  346. SSL_AES256,
  347. SSL_SHA384,
  348. SSL_HANDSHAKE_MAC_SHA384,
  349. },
  350. /* Cipher C027 */
  351. {
  352. TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
  353. TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
  354. SSL_kECDHE,
  355. SSL_aRSA,
  356. SSL_AES128,
  357. SSL_SHA256,
  358. SSL_HANDSHAKE_MAC_SHA256,
  359. },
  360. /* Cipher C028 */
  361. {
  362. TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
  363. TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
  364. SSL_kECDHE,
  365. SSL_aRSA,
  366. SSL_AES256,
  367. SSL_SHA384,
  368. SSL_HANDSHAKE_MAC_SHA384,
  369. },
  370. /* GCM based TLS v1.2 ciphersuites from RFC5289 */
  371. /* Cipher C02B */
  372. {
  373. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  374. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  375. SSL_kECDHE,
  376. SSL_aECDSA,
  377. SSL_AES128GCM,
  378. SSL_AEAD,
  379. SSL_HANDSHAKE_MAC_SHA256,
  380. },
  381. /* Cipher C02C */
  382. {
  383. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  384. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  385. SSL_kECDHE,
  386. SSL_aECDSA,
  387. SSL_AES256GCM,
  388. SSL_AEAD,
  389. SSL_HANDSHAKE_MAC_SHA384,
  390. },
  391. /* Cipher C02F */
  392. {
  393. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  394. TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  395. SSL_kECDHE,
  396. SSL_aRSA,
  397. SSL_AES128GCM,
  398. SSL_AEAD,
  399. SSL_HANDSHAKE_MAC_SHA256,
  400. },
  401. /* Cipher C030 */
  402. {
  403. TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  404. TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  405. SSL_kECDHE,
  406. SSL_aRSA,
  407. SSL_AES256GCM,
  408. SSL_AEAD,
  409. SSL_HANDSHAKE_MAC_SHA384,
  410. },
  411. /* ECDHE-PSK cipher suites. */
  412. /* Cipher C035 */
  413. {
  414. TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  415. TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  416. SSL_kECDHE,
  417. SSL_aPSK,
  418. SSL_AES128,
  419. SSL_SHA1,
  420. SSL_HANDSHAKE_MAC_DEFAULT,
  421. },
  422. /* Cipher C036 */
  423. {
  424. TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  425. TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  426. SSL_kECDHE,
  427. SSL_aPSK,
  428. SSL_AES256,
  429. SSL_SHA1,
  430. SSL_HANDSHAKE_MAC_DEFAULT,
  431. },
  432. /* ChaCha20-Poly1305 cipher suites. */
  433. /* Cipher CCA8 */
  434. {
  435. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  436. TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  437. SSL_kECDHE,
  438. SSL_aRSA,
  439. SSL_CHACHA20POLY1305,
  440. SSL_AEAD,
  441. SSL_HANDSHAKE_MAC_SHA256,
  442. },
  443. /* Cipher CCA9 */
  444. {
  445. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  446. TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  447. SSL_kECDHE,
  448. SSL_aECDSA,
  449. SSL_CHACHA20POLY1305,
  450. SSL_AEAD,
  451. SSL_HANDSHAKE_MAC_SHA256,
  452. },
  453. /* Cipher CCAB */
  454. {
  455. TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  456. TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  457. SSL_kECDHE,
  458. SSL_aPSK,
  459. SSL_CHACHA20POLY1305,
  460. SSL_AEAD,
  461. SSL_HANDSHAKE_MAC_SHA256,
  462. },
  463. };
  464. static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
  465. #define CIPHER_ADD 1
  466. #define CIPHER_KILL 2
  467. #define CIPHER_DEL 3
  468. #define CIPHER_ORD 4
  469. #define CIPHER_SPECIAL 5
  470. typedef struct cipher_order_st {
  471. const SSL_CIPHER *cipher;
  472. int active;
  473. int in_group;
  474. struct cipher_order_st *next, *prev;
  475. } CIPHER_ORDER;
  476. typedef struct cipher_alias_st {
  477. /* name is the name of the cipher alias. */
  478. const char *name;
  479. /* The following fields are bitmasks for the corresponding fields on
  480. * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
  481. * bit corresponding to the cipher's value is set to 1. If any bitmask is
  482. * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
  483. uint32_t algorithm_mkey;
  484. uint32_t algorithm_auth;
  485. uint32_t algorithm_enc;
  486. uint32_t algorithm_mac;
  487. /* min_version, if non-zero, matches all ciphers which were added in that
  488. * particular protocol version. */
  489. uint16_t min_version;
  490. } CIPHER_ALIAS;
  491. static const CIPHER_ALIAS kCipherAliases[] = {
  492. /* "ALL" doesn't include eNULL. It must be explicitly enabled. */
  493. {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  494. /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
  495. /* key exchange aliases
  496. * (some of those using only a single bit here combine
  497. * multiple key exchange algs according to the RFCs. */
  498. {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
  499. {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  500. {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  501. {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  502. {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
  503. /* server authentication aliases */
  504. {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
  505. {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  506. {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  507. {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
  508. /* aliases combining key exchange and server authentication */
  509. {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  510. {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  511. {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
  512. {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
  513. /* symmetric encryption aliases */
  514. {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
  515. {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
  516. {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
  517. {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
  518. {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
  519. {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0},
  520. /* MAC aliases */
  521. {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
  522. {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
  523. {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
  524. {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
  525. /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
  526. * same as "SSLv3". */
  527. {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
  528. {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
  529. {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
  530. /* Legacy strength classes. */
  531. {"HIGH", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  532. {"FIPS", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  533. };
  534. static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
  535. static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  536. const SSL_CIPHER *a = in_a;
  537. const SSL_CIPHER *b = in_b;
  538. if (a->id > b->id) {
  539. return 1;
  540. } else if (a->id < b->id) {
  541. return -1;
  542. } else {
  543. return 0;
  544. }
  545. }
  546. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  547. SSL_CIPHER c;
  548. c.id = 0x03000000L | value;
  549. return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
  550. ssl_cipher_id_cmp);
  551. }
  552. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  553. size_t *out_mac_secret_len,
  554. size_t *out_fixed_iv_len,
  555. const SSL_CIPHER *cipher, uint16_t version) {
  556. *out_aead = NULL;
  557. *out_mac_secret_len = 0;
  558. *out_fixed_iv_len = 0;
  559. if (cipher->algorithm_mac == SSL_AEAD) {
  560. if (cipher->algorithm_enc == SSL_AES128GCM) {
  561. *out_aead = EVP_aead_aes_128_gcm();
  562. *out_fixed_iv_len = 4;
  563. } else if (cipher->algorithm_enc == SSL_AES256GCM) {
  564. *out_aead = EVP_aead_aes_256_gcm();
  565. *out_fixed_iv_len = 4;
  566. } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
  567. *out_aead = EVP_aead_chacha20_poly1305();
  568. *out_fixed_iv_len = 12;
  569. } else {
  570. return 0;
  571. }
  572. /* In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
  573. * above computes the TLS 1.2 construction. */
  574. if (version >= TLS1_3_VERSION) {
  575. *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
  576. }
  577. } else if (cipher->algorithm_mac == SSL_SHA1) {
  578. if (cipher->algorithm_enc == SSL_eNULL) {
  579. if (version == SSL3_VERSION) {
  580. *out_aead = EVP_aead_null_sha1_ssl3();
  581. } else {
  582. *out_aead = EVP_aead_null_sha1_tls();
  583. }
  584. } else if (cipher->algorithm_enc == SSL_3DES) {
  585. if (version == SSL3_VERSION) {
  586. *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
  587. *out_fixed_iv_len = 8;
  588. } else if (version == TLS1_VERSION) {
  589. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
  590. *out_fixed_iv_len = 8;
  591. } else {
  592. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
  593. }
  594. } else if (cipher->algorithm_enc == SSL_AES128) {
  595. if (version == SSL3_VERSION) {
  596. *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
  597. *out_fixed_iv_len = 16;
  598. } else if (version == TLS1_VERSION) {
  599. *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
  600. *out_fixed_iv_len = 16;
  601. } else {
  602. *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
  603. }
  604. } else if (cipher->algorithm_enc == SSL_AES256) {
  605. if (version == SSL3_VERSION) {
  606. *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
  607. *out_fixed_iv_len = 16;
  608. } else if (version == TLS1_VERSION) {
  609. *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
  610. *out_fixed_iv_len = 16;
  611. } else {
  612. *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
  613. }
  614. } else {
  615. return 0;
  616. }
  617. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  618. } else if (cipher->algorithm_mac == SSL_SHA256) {
  619. if (cipher->algorithm_enc == SSL_AES128) {
  620. *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
  621. } else if (cipher->algorithm_enc == SSL_AES256) {
  622. *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
  623. } else {
  624. return 0;
  625. }
  626. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  627. } else if (cipher->algorithm_mac == SSL_SHA384) {
  628. if (cipher->algorithm_enc != SSL_AES256) {
  629. return 0;
  630. }
  631. *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
  632. *out_mac_secret_len = SHA384_DIGEST_LENGTH;
  633. } else {
  634. return 0;
  635. }
  636. return 1;
  637. }
  638. const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf,
  639. uint16_t version) {
  640. switch (algorithm_prf) {
  641. case SSL_HANDSHAKE_MAC_DEFAULT:
  642. return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
  643. case SSL_HANDSHAKE_MAC_SHA256:
  644. return EVP_sha256();
  645. case SSL_HANDSHAKE_MAC_SHA384:
  646. return EVP_sha384();
  647. default:
  648. return NULL;
  649. }
  650. }
  651. #define ITEM_SEP(a) \
  652. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  653. /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
  654. * |buf_len| bytes at |buf|. */
  655. static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
  656. /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
  657. return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
  658. }
  659. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  660. CIPHER_ORDER **tail) {
  661. if (curr == *tail) {
  662. return;
  663. }
  664. if (curr == *head) {
  665. *head = curr->next;
  666. }
  667. if (curr->prev != NULL) {
  668. curr->prev->next = curr->next;
  669. }
  670. if (curr->next != NULL) {
  671. curr->next->prev = curr->prev;
  672. }
  673. (*tail)->next = curr;
  674. curr->prev = *tail;
  675. curr->next = NULL;
  676. *tail = curr;
  677. }
  678. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  679. CIPHER_ORDER **tail) {
  680. if (curr == *head) {
  681. return;
  682. }
  683. if (curr == *tail) {
  684. *tail = curr->prev;
  685. }
  686. if (curr->next != NULL) {
  687. curr->next->prev = curr->prev;
  688. }
  689. if (curr->prev != NULL) {
  690. curr->prev->next = curr->next;
  691. }
  692. (*head)->prev = curr;
  693. curr->next = *head;
  694. curr->prev = NULL;
  695. *head = curr;
  696. }
  697. static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
  698. CIPHER_ORDER *co_list,
  699. CIPHER_ORDER **head_p,
  700. CIPHER_ORDER **tail_p) {
  701. /* The set of ciphers is static, but some subset may be unsupported by
  702. * |ssl_method|, so the list may be smaller. */
  703. size_t co_list_num = 0;
  704. for (size_t i = 0; i < kCiphersLen; i++) {
  705. const SSL_CIPHER *cipher = &kCiphers[i];
  706. if (ssl_method->supports_cipher(cipher) &&
  707. /* TLS 1.3 ciphers do not participate in this mechanism. */
  708. cipher->algorithm_mkey != SSL_kGENERIC) {
  709. co_list[co_list_num].cipher = cipher;
  710. co_list[co_list_num].next = NULL;
  711. co_list[co_list_num].prev = NULL;
  712. co_list[co_list_num].active = 0;
  713. co_list[co_list_num].in_group = 0;
  714. co_list_num++;
  715. }
  716. }
  717. /* Prepare linked list from list entries. */
  718. if (co_list_num > 0) {
  719. co_list[0].prev = NULL;
  720. if (co_list_num > 1) {
  721. co_list[0].next = &co_list[1];
  722. for (size_t i = 1; i < co_list_num - 1; i++) {
  723. co_list[i].prev = &co_list[i - 1];
  724. co_list[i].next = &co_list[i + 1];
  725. }
  726. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  727. }
  728. co_list[co_list_num - 1].next = NULL;
  729. *head_p = &co_list[0];
  730. *tail_p = &co_list[co_list_num - 1];
  731. }
  732. }
  733. /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
  734. * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
  735. * head and tail of the list to |*head_p| and |*tail_p|, respectively.
  736. *
  737. * - If |cipher_id| is non-zero, only that cipher is selected.
  738. * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
  739. * of that strength.
  740. * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
  741. * |min_version|. */
  742. static void ssl_cipher_apply_rule(
  743. uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
  744. uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
  745. int strength_bits, int in_group, CIPHER_ORDER **head_p,
  746. CIPHER_ORDER **tail_p) {
  747. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  748. const SSL_CIPHER *cp;
  749. int reverse = 0;
  750. if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
  751. (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
  752. /* The rule matches nothing, so bail early. */
  753. return;
  754. }
  755. if (rule == CIPHER_DEL) {
  756. /* needed to maintain sorting between currently deleted ciphers */
  757. reverse = 1;
  758. }
  759. head = *head_p;
  760. tail = *tail_p;
  761. if (reverse) {
  762. next = tail;
  763. last = head;
  764. } else {
  765. next = head;
  766. last = tail;
  767. }
  768. curr = NULL;
  769. for (;;) {
  770. if (curr == last) {
  771. break;
  772. }
  773. curr = next;
  774. if (curr == NULL) {
  775. break;
  776. }
  777. next = reverse ? curr->prev : curr->next;
  778. cp = curr->cipher;
  779. /* Selection criteria is either a specific cipher, the value of
  780. * |strength_bits|, or the algorithms used. */
  781. if (cipher_id != 0) {
  782. if (cipher_id != cp->id) {
  783. continue;
  784. }
  785. } else if (strength_bits >= 0) {
  786. if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
  787. continue;
  788. }
  789. } else {
  790. if (!(alg_mkey & cp->algorithm_mkey) ||
  791. !(alg_auth & cp->algorithm_auth) ||
  792. !(alg_enc & cp->algorithm_enc) ||
  793. !(alg_mac & cp->algorithm_mac) ||
  794. (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version)) {
  795. continue;
  796. }
  797. }
  798. /* add the cipher if it has not been added yet. */
  799. if (rule == CIPHER_ADD) {
  800. /* reverse == 0 */
  801. if (!curr->active) {
  802. ll_append_tail(&head, curr, &tail);
  803. curr->active = 1;
  804. curr->in_group = in_group;
  805. }
  806. }
  807. /* Move the added cipher to this location */
  808. else if (rule == CIPHER_ORD) {
  809. /* reverse == 0 */
  810. if (curr->active) {
  811. ll_append_tail(&head, curr, &tail);
  812. curr->in_group = 0;
  813. }
  814. } else if (rule == CIPHER_DEL) {
  815. /* reverse == 1 */
  816. if (curr->active) {
  817. /* most recently deleted ciphersuites get best positions
  818. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  819. * works in reverse to maintain the order) */
  820. ll_append_head(&head, curr, &tail);
  821. curr->active = 0;
  822. curr->in_group = 0;
  823. }
  824. } else if (rule == CIPHER_KILL) {
  825. /* reverse == 0 */
  826. if (head == curr) {
  827. head = curr->next;
  828. } else {
  829. curr->prev->next = curr->next;
  830. }
  831. if (tail == curr) {
  832. tail = curr->prev;
  833. }
  834. curr->active = 0;
  835. if (curr->next != NULL) {
  836. curr->next->prev = curr->prev;
  837. }
  838. if (curr->prev != NULL) {
  839. curr->prev->next = curr->next;
  840. }
  841. curr->next = NULL;
  842. curr->prev = NULL;
  843. }
  844. }
  845. *head_p = head;
  846. *tail_p = tail;
  847. }
  848. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  849. CIPHER_ORDER **tail_p) {
  850. int max_strength_bits, i, *number_uses;
  851. CIPHER_ORDER *curr;
  852. /* This routine sorts the ciphers with descending strength. The sorting must
  853. * keep the pre-sorted sequence, so we apply the normal sorting routine as
  854. * '+' movement to the end of the list. */
  855. max_strength_bits = 0;
  856. curr = *head_p;
  857. while (curr != NULL) {
  858. if (curr->active &&
  859. SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
  860. max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
  861. }
  862. curr = curr->next;
  863. }
  864. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  865. if (!number_uses) {
  866. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  867. return 0;
  868. }
  869. OPENSSL_memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  870. /* Now find the strength_bits values actually used. */
  871. curr = *head_p;
  872. while (curr != NULL) {
  873. if (curr->active) {
  874. number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
  875. }
  876. curr = curr->next;
  877. }
  878. /* Go through the list of used strength_bits values in descending order. */
  879. for (i = max_strength_bits; i >= 0; i--) {
  880. if (number_uses[i] > 0) {
  881. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
  882. }
  883. }
  884. OPENSSL_free(number_uses);
  885. return 1;
  886. }
  887. static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
  888. const char *rule_str,
  889. CIPHER_ORDER **head_p,
  890. CIPHER_ORDER **tail_p, int strict) {
  891. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  892. uint16_t min_version;
  893. const char *l, *buf;
  894. int multi, skip_rule, rule, ok, in_group = 0, has_group = 0;
  895. size_t j, buf_len;
  896. uint32_t cipher_id;
  897. char ch;
  898. l = rule_str;
  899. for (;;) {
  900. ch = *l;
  901. if (ch == '\0') {
  902. break; /* done */
  903. }
  904. if (in_group) {
  905. if (ch == ']') {
  906. if (*tail_p) {
  907. (*tail_p)->in_group = 0;
  908. }
  909. in_group = 0;
  910. l++;
  911. continue;
  912. }
  913. if (ch == '|') {
  914. rule = CIPHER_ADD;
  915. l++;
  916. continue;
  917. } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
  918. !(ch >= '0' && ch <= '9')) {
  919. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  920. return 0;
  921. } else {
  922. rule = CIPHER_ADD;
  923. }
  924. } else if (ch == '-') {
  925. rule = CIPHER_DEL;
  926. l++;
  927. } else if (ch == '+') {
  928. rule = CIPHER_ORD;
  929. l++;
  930. } else if (ch == '!') {
  931. rule = CIPHER_KILL;
  932. l++;
  933. } else if (ch == '@') {
  934. rule = CIPHER_SPECIAL;
  935. l++;
  936. } else if (ch == '[') {
  937. if (in_group) {
  938. OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
  939. return 0;
  940. }
  941. in_group = 1;
  942. has_group = 1;
  943. l++;
  944. continue;
  945. } else {
  946. rule = CIPHER_ADD;
  947. }
  948. /* If preference groups are enabled, the only legal operator is +.
  949. * Otherwise the in_group bits will get mixed up. */
  950. if (has_group && rule != CIPHER_ADD) {
  951. OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  952. return 0;
  953. }
  954. if (ITEM_SEP(ch)) {
  955. l++;
  956. continue;
  957. }
  958. multi = 0;
  959. cipher_id = 0;
  960. alg_mkey = ~0u;
  961. alg_auth = ~0u;
  962. alg_enc = ~0u;
  963. alg_mac = ~0u;
  964. min_version = 0;
  965. skip_rule = 0;
  966. for (;;) {
  967. ch = *l;
  968. buf = l;
  969. buf_len = 0;
  970. while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
  971. ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
  972. ch = *(++l);
  973. buf_len++;
  974. }
  975. if (buf_len == 0) {
  976. /* We hit something we cannot deal with, it is no command or separator
  977. * nor alphanumeric, so we call this an error. */
  978. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  979. return 0;
  980. }
  981. if (rule == CIPHER_SPECIAL) {
  982. break;
  983. }
  984. /* Look for a matching exact cipher. These aren't allowed in multipart
  985. * rules. */
  986. if (!multi && ch != '+') {
  987. for (j = 0; j < kCiphersLen; j++) {
  988. const SSL_CIPHER *cipher = &kCiphers[j];
  989. if (rule_equals(cipher->name, buf, buf_len)) {
  990. cipher_id = cipher->id;
  991. break;
  992. }
  993. }
  994. }
  995. if (cipher_id == 0) {
  996. /* If not an exact cipher, look for a matching cipher alias. */
  997. for (j = 0; j < kCipherAliasesLen; j++) {
  998. if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
  999. alg_mkey &= kCipherAliases[j].algorithm_mkey;
  1000. alg_auth &= kCipherAliases[j].algorithm_auth;
  1001. alg_enc &= kCipherAliases[j].algorithm_enc;
  1002. alg_mac &= kCipherAliases[j].algorithm_mac;
  1003. if (min_version != 0 &&
  1004. min_version != kCipherAliases[j].min_version) {
  1005. skip_rule = 1;
  1006. } else {
  1007. min_version = kCipherAliases[j].min_version;
  1008. }
  1009. break;
  1010. }
  1011. }
  1012. if (j == kCipherAliasesLen) {
  1013. skip_rule = 1;
  1014. if (strict) {
  1015. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1016. return 0;
  1017. }
  1018. }
  1019. }
  1020. /* Check for a multipart rule. */
  1021. if (ch != '+') {
  1022. break;
  1023. }
  1024. l++;
  1025. multi = 1;
  1026. }
  1027. /* Ok, we have the rule, now apply it. */
  1028. if (rule == CIPHER_SPECIAL) {
  1029. /* special command */
  1030. ok = 0;
  1031. if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
  1032. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1033. } else {
  1034. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1035. }
  1036. if (ok == 0) {
  1037. return 0;
  1038. }
  1039. /* We do not support any "multi" options together with "@", so throw away
  1040. * the rest of the command, if any left, until end or ':' is found. */
  1041. while (*l != '\0' && !ITEM_SEP(*l)) {
  1042. l++;
  1043. }
  1044. } else if (!skip_rule) {
  1045. ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
  1046. min_version, rule, -1, in_group, head_p, tail_p);
  1047. }
  1048. }
  1049. if (in_group) {
  1050. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1051. return 0;
  1052. }
  1053. return 1;
  1054. }
  1055. int ssl_create_cipher_list(
  1056. const SSL_PROTOCOL_METHOD *ssl_method,
  1057. struct ssl_cipher_preference_list_st **out_cipher_list,
  1058. const char *rule_str, int strict) {
  1059. STACK_OF(SSL_CIPHER) *cipherstack = NULL;
  1060. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1061. uint8_t *in_group_flags = NULL;
  1062. unsigned int num_in_group_flags = 0;
  1063. struct ssl_cipher_preference_list_st *pref_list = NULL;
  1064. /* Return with error if nothing to do. */
  1065. if (rule_str == NULL || out_cipher_list == NULL) {
  1066. return 0;
  1067. }
  1068. /* Now we have to collect the available ciphers from the compiled in ciphers.
  1069. * We cannot get more than the number compiled in, so it is used for
  1070. * allocation. */
  1071. co_list = OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
  1072. if (co_list == NULL) {
  1073. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1074. return 0;
  1075. }
  1076. ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
  1077. /* Now arrange all ciphers by preference:
  1078. * TODO(davidben): Compute this order once and copy it. */
  1079. /* Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
  1080. * key exchange mechanisms */
  1081. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
  1082. 0, &head, &tail);
  1083. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
  1084. &head, &tail);
  1085. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
  1086. &tail);
  1087. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  1088. * CHACHA20 unless there is hardware support for fast and constant-time
  1089. * AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
  1090. * old one. */
  1091. if (EVP_has_aes_hardware()) {
  1092. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1093. &head, &tail);
  1094. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1095. &head, &tail);
  1096. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1097. -1, 0, &head, &tail);
  1098. } else {
  1099. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1100. -1, 0, &head, &tail);
  1101. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1102. &head, &tail);
  1103. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1104. &head, &tail);
  1105. }
  1106. /* Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
  1107. * 3DES_EDE_CBC_SHA. */
  1108. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
  1109. &head, &tail);
  1110. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
  1111. &head, &tail);
  1112. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
  1113. &tail);
  1114. /* Temporarily enable everything else for sorting */
  1115. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
  1116. &tail);
  1117. /* Move ciphers without forward secrecy to the end. */
  1118. ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0,
  1119. CIPHER_ORD, -1, 0, &head, &tail);
  1120. /* Now disable everything (maintaining the ordering!) */
  1121. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
  1122. &tail);
  1123. /* If the rule_string begins with DEFAULT, apply the default rule before
  1124. * using the (possibly available) additional rules. */
  1125. const char *rule_p = rule_str;
  1126. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1127. if (!ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
  1128. &tail, strict)) {
  1129. goto err;
  1130. }
  1131. rule_p += 7;
  1132. if (*rule_p == ':') {
  1133. rule_p++;
  1134. }
  1135. }
  1136. if (*rule_p != '\0' &&
  1137. !ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail, strict)) {
  1138. goto err;
  1139. }
  1140. /* Allocate new "cipherstack" for the result, return with error
  1141. * if we cannot get one. */
  1142. cipherstack = sk_SSL_CIPHER_new_null();
  1143. if (cipherstack == NULL) {
  1144. goto err;
  1145. }
  1146. in_group_flags = OPENSSL_malloc(kCiphersLen);
  1147. if (!in_group_flags) {
  1148. goto err;
  1149. }
  1150. /* The cipher selection for the list is done. The ciphers are added
  1151. * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
  1152. for (curr = head; curr != NULL; curr = curr->next) {
  1153. if (curr->active) {
  1154. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1155. goto err;
  1156. }
  1157. in_group_flags[num_in_group_flags++] = curr->in_group;
  1158. }
  1159. }
  1160. OPENSSL_free(co_list); /* Not needed any longer */
  1161. co_list = NULL;
  1162. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1163. if (!pref_list) {
  1164. goto err;
  1165. }
  1166. pref_list->ciphers = cipherstack;
  1167. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1168. if (!pref_list->in_group_flags) {
  1169. goto err;
  1170. }
  1171. OPENSSL_memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1172. OPENSSL_free(in_group_flags);
  1173. in_group_flags = NULL;
  1174. if (*out_cipher_list != NULL) {
  1175. ssl_cipher_preference_list_free(*out_cipher_list);
  1176. }
  1177. *out_cipher_list = pref_list;
  1178. pref_list = NULL;
  1179. /* Configuring an empty cipher list is an error but still updates the
  1180. * output. */
  1181. if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers) == 0) {
  1182. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1183. return 0;
  1184. }
  1185. return 1;
  1186. err:
  1187. OPENSSL_free(co_list);
  1188. OPENSSL_free(in_group_flags);
  1189. sk_SSL_CIPHER_free(cipherstack);
  1190. if (pref_list) {
  1191. OPENSSL_free(pref_list->in_group_flags);
  1192. }
  1193. OPENSSL_free(pref_list);
  1194. return 0;
  1195. }
  1196. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
  1197. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
  1198. uint32_t id = cipher->id;
  1199. /* All ciphers are SSLv3. */
  1200. assert((id & 0xff000000) == 0x03000000);
  1201. return id & 0xffff;
  1202. }
  1203. int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
  1204. return (cipher->algorithm_enc & SSL_AES) != 0;
  1205. }
  1206. int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
  1207. return (cipher->algorithm_mac & SSL_SHA1) != 0;
  1208. }
  1209. int SSL_CIPHER_has_SHA256_HMAC(const SSL_CIPHER *cipher) {
  1210. return (cipher->algorithm_mac & SSL_SHA256) != 0;
  1211. }
  1212. int SSL_CIPHER_is_AEAD(const SSL_CIPHER *cipher) {
  1213. return (cipher->algorithm_mac & SSL_AEAD) != 0;
  1214. }
  1215. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
  1216. return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
  1217. }
  1218. int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
  1219. return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
  1220. }
  1221. int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
  1222. return (cipher->algorithm_enc & SSL_AES128) != 0;
  1223. }
  1224. int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
  1225. return (cipher->algorithm_enc & SSL_AES256) != 0;
  1226. }
  1227. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
  1228. return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
  1229. }
  1230. int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
  1231. return (cipher->algorithm_enc & SSL_eNULL) != 0;
  1232. }
  1233. int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
  1234. return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
  1235. cipher->algorithm_mac != SSL_AEAD;
  1236. }
  1237. int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
  1238. return (cipher->algorithm_auth & SSL_aECDSA) != 0;
  1239. }
  1240. int SSL_CIPHER_is_ECDHE(const SSL_CIPHER *cipher) {
  1241. return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
  1242. }
  1243. int SSL_CIPHER_is_static_RSA(const SSL_CIPHER *cipher) {
  1244. return (cipher->algorithm_mkey & SSL_kRSA) != 0;
  1245. }
  1246. uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
  1247. if (cipher->algorithm_mkey == SSL_kGENERIC ||
  1248. cipher->algorithm_auth == SSL_aGENERIC) {
  1249. return TLS1_3_VERSION;
  1250. }
  1251. if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
  1252. /* Cipher suites before TLS 1.2 use the default PRF, while all those added
  1253. * afterwards specify a particular hash. */
  1254. return TLS1_2_VERSION;
  1255. }
  1256. return SSL3_VERSION;
  1257. }
  1258. uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
  1259. if (cipher->algorithm_mkey == SSL_kGENERIC ||
  1260. cipher->algorithm_auth == SSL_aGENERIC) {
  1261. return TLS1_3_VERSION;
  1262. }
  1263. return TLS1_2_VERSION;
  1264. }
  1265. /* return the actual cipher being used */
  1266. const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
  1267. if (cipher != NULL) {
  1268. return cipher->name;
  1269. }
  1270. return "(NONE)";
  1271. }
  1272. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1273. if (cipher == NULL) {
  1274. return "";
  1275. }
  1276. switch (cipher->algorithm_mkey) {
  1277. case SSL_kRSA:
  1278. return "RSA";
  1279. case SSL_kECDHE:
  1280. switch (cipher->algorithm_auth) {
  1281. case SSL_aECDSA:
  1282. return "ECDHE_ECDSA";
  1283. case SSL_aRSA:
  1284. return "ECDHE_RSA";
  1285. case SSL_aPSK:
  1286. return "ECDHE_PSK";
  1287. default:
  1288. assert(0);
  1289. return "UNKNOWN";
  1290. }
  1291. case SSL_kPSK:
  1292. assert(cipher->algorithm_auth == SSL_aPSK);
  1293. return "PSK";
  1294. case SSL_kGENERIC:
  1295. assert(cipher->algorithm_auth == SSL_aGENERIC);
  1296. return "GENERIC";
  1297. default:
  1298. assert(0);
  1299. return "UNKNOWN";
  1300. }
  1301. }
  1302. static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
  1303. switch (cipher->algorithm_enc) {
  1304. case SSL_3DES:
  1305. return "3DES_EDE_CBC";
  1306. case SSL_AES128:
  1307. return "AES_128_CBC";
  1308. case SSL_AES256:
  1309. return "AES_256_CBC";
  1310. case SSL_AES128GCM:
  1311. return "AES_128_GCM";
  1312. case SSL_AES256GCM:
  1313. return "AES_256_GCM";
  1314. case SSL_CHACHA20POLY1305:
  1315. return "CHACHA20_POLY1305";
  1316. break;
  1317. default:
  1318. assert(0);
  1319. return "UNKNOWN";
  1320. }
  1321. }
  1322. static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
  1323. switch (cipher->algorithm_prf) {
  1324. case SSL_HANDSHAKE_MAC_DEFAULT:
  1325. /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which
  1326. * is SHA-1 for all supported ciphers. */
  1327. assert(cipher->algorithm_mac == SSL_SHA1);
  1328. return "SHA";
  1329. case SSL_HANDSHAKE_MAC_SHA256:
  1330. return "SHA256";
  1331. case SSL_HANDSHAKE_MAC_SHA384:
  1332. return "SHA384";
  1333. }
  1334. assert(0);
  1335. return "UNKNOWN";
  1336. }
  1337. char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
  1338. if (cipher == NULL) {
  1339. return NULL;
  1340. }
  1341. const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
  1342. const char *enc_name = ssl_cipher_get_enc_name(cipher);
  1343. const char *prf_name = ssl_cipher_get_prf_name(cipher);
  1344. /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name} or
  1345. * TLS_{enc_name}_{prf_name} depending on whether the cipher is AEAD-only. */
  1346. size_t len = 4 + strlen(enc_name) + 1 + strlen(prf_name) + 1;
  1347. if (cipher->algorithm_mkey != SSL_kGENERIC) {
  1348. len += strlen(kx_name) + 6;
  1349. }
  1350. char *ret = OPENSSL_malloc(len);
  1351. if (ret == NULL) {
  1352. return NULL;
  1353. }
  1354. if (BUF_strlcpy(ret, "TLS_", len) >= len ||
  1355. (cipher->algorithm_mkey != SSL_kGENERIC &&
  1356. (BUF_strlcat(ret, kx_name, len) >= len ||
  1357. BUF_strlcat(ret, "_WITH_", len) >= len)) ||
  1358. BUF_strlcat(ret, enc_name, len) >= len ||
  1359. BUF_strlcat(ret, "_", len) >= len ||
  1360. BUF_strlcat(ret, prf_name, len) >= len) {
  1361. assert(0);
  1362. OPENSSL_free(ret);
  1363. return NULL;
  1364. }
  1365. assert(strlen(ret) + 1 == len);
  1366. return ret;
  1367. }
  1368. int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
  1369. if (cipher == NULL) {
  1370. return 0;
  1371. }
  1372. int alg_bits, strength_bits;
  1373. switch (cipher->algorithm_enc) {
  1374. case SSL_AES128:
  1375. case SSL_AES128GCM:
  1376. alg_bits = 128;
  1377. strength_bits = 128;
  1378. break;
  1379. case SSL_AES256:
  1380. case SSL_AES256GCM:
  1381. case SSL_CHACHA20POLY1305:
  1382. alg_bits = 256;
  1383. strength_bits = 256;
  1384. break;
  1385. case SSL_3DES:
  1386. alg_bits = 168;
  1387. strength_bits = 112;
  1388. break;
  1389. case SSL_eNULL:
  1390. alg_bits = 0;
  1391. strength_bits = 0;
  1392. break;
  1393. default:
  1394. assert(0);
  1395. alg_bits = 0;
  1396. strength_bits = 0;
  1397. }
  1398. if (out_alg_bits != NULL) {
  1399. *out_alg_bits = alg_bits;
  1400. }
  1401. return strength_bits;
  1402. }
  1403. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
  1404. int len) {
  1405. const char *kx, *au, *enc, *mac;
  1406. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1407. alg_mkey = cipher->algorithm_mkey;
  1408. alg_auth = cipher->algorithm_auth;
  1409. alg_enc = cipher->algorithm_enc;
  1410. alg_mac = cipher->algorithm_mac;
  1411. switch (alg_mkey) {
  1412. case SSL_kRSA:
  1413. kx = "RSA";
  1414. break;
  1415. case SSL_kECDHE:
  1416. kx = "ECDH";
  1417. break;
  1418. case SSL_kPSK:
  1419. kx = "PSK";
  1420. break;
  1421. case SSL_kGENERIC:
  1422. kx = "GENERIC";
  1423. break;
  1424. default:
  1425. kx = "unknown";
  1426. }
  1427. switch (alg_auth) {
  1428. case SSL_aRSA:
  1429. au = "RSA";
  1430. break;
  1431. case SSL_aECDSA:
  1432. au = "ECDSA";
  1433. break;
  1434. case SSL_aPSK:
  1435. au = "PSK";
  1436. break;
  1437. case SSL_aGENERIC:
  1438. au = "GENERIC";
  1439. break;
  1440. default:
  1441. au = "unknown";
  1442. break;
  1443. }
  1444. switch (alg_enc) {
  1445. case SSL_3DES:
  1446. enc = "3DES(168)";
  1447. break;
  1448. case SSL_AES128:
  1449. enc = "AES(128)";
  1450. break;
  1451. case SSL_AES256:
  1452. enc = "AES(256)";
  1453. break;
  1454. case SSL_AES128GCM:
  1455. enc = "AESGCM(128)";
  1456. break;
  1457. case SSL_AES256GCM:
  1458. enc = "AESGCM(256)";
  1459. break;
  1460. case SSL_CHACHA20POLY1305:
  1461. enc = "ChaCha20-Poly1305";
  1462. break;
  1463. case SSL_eNULL:
  1464. enc="None";
  1465. break;
  1466. default:
  1467. enc = "unknown";
  1468. break;
  1469. }
  1470. switch (alg_mac) {
  1471. case SSL_SHA1:
  1472. mac = "SHA1";
  1473. break;
  1474. case SSL_SHA256:
  1475. mac = "SHA256";
  1476. break;
  1477. case SSL_SHA384:
  1478. mac = "SHA384";
  1479. break;
  1480. case SSL_AEAD:
  1481. mac = "AEAD";
  1482. break;
  1483. default:
  1484. mac = "unknown";
  1485. break;
  1486. }
  1487. if (buf == NULL) {
  1488. len = 128;
  1489. buf = OPENSSL_malloc(len);
  1490. if (buf == NULL) {
  1491. return NULL;
  1492. }
  1493. } else if (len < 128) {
  1494. return "Buffer too small";
  1495. }
  1496. BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
  1497. cipher->name, kx, au, enc, mac);
  1498. return buf;
  1499. }
  1500. const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
  1501. return "TLSv1/SSLv3";
  1502. }
  1503. COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
  1504. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
  1505. const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
  1506. void SSL_COMP_free_compression_methods(void) {}
  1507. uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) {
  1508. switch (EVP_PKEY_id(key)) {
  1509. case EVP_PKEY_RSA:
  1510. return SSL_aRSA;
  1511. case EVP_PKEY_EC:
  1512. case EVP_PKEY_ED25519:
  1513. /* Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers. */
  1514. return SSL_aECDSA;
  1515. default:
  1516. return 0;
  1517. }
  1518. }
  1519. int ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
  1520. return (cipher->algorithm_auth & SSL_aCERT) != 0;
  1521. }
  1522. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
  1523. /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
  1524. if (cipher->algorithm_mkey & SSL_kECDHE) {
  1525. return 1;
  1526. }
  1527. /* It is optional in all others. */
  1528. return 0;
  1529. }
  1530. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
  1531. size_t block_size;
  1532. switch (cipher->algorithm_enc) {
  1533. case SSL_3DES:
  1534. block_size = 8;
  1535. break;
  1536. case SSL_AES128:
  1537. case SSL_AES256:
  1538. block_size = 16;
  1539. break;
  1540. default:
  1541. return 0;
  1542. }
  1543. /* All supported TLS 1.0 ciphers use SHA-1. */
  1544. assert(cipher->algorithm_mac == SSL_SHA1);
  1545. size_t ret = 1 + SHA_DIGEST_LENGTH;
  1546. ret += block_size - (ret % block_size);
  1547. return ret;
  1548. }