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.
 
 
 
 
 
 

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