Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

1936 рядки
53 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. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  494. {
  495. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
  496. TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD,
  497. SSL_kECDHE,
  498. SSL_aRSA,
  499. SSL_CHACHA20POLY1305_OLD,
  500. SSL_AEAD,
  501. SSL_HANDSHAKE_MAC_SHA256,
  502. },
  503. {
  504. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
  505. TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD,
  506. SSL_kECDHE,
  507. SSL_aECDSA,
  508. SSL_CHACHA20POLY1305_OLD,
  509. SSL_AEAD,
  510. SSL_HANDSHAKE_MAC_SHA256,
  511. },
  512. #endif
  513. /* Cipher CCA8 */
  514. {
  515. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  516. TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  517. SSL_kECDHE,
  518. SSL_aRSA,
  519. SSL_CHACHA20POLY1305,
  520. SSL_AEAD,
  521. SSL_HANDSHAKE_MAC_SHA256,
  522. },
  523. /* Cipher CCA9 */
  524. {
  525. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  526. TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  527. SSL_kECDHE,
  528. SSL_aECDSA,
  529. SSL_CHACHA20POLY1305,
  530. SSL_AEAD,
  531. SSL_HANDSHAKE_MAC_SHA256,
  532. },
  533. /* Cipher CCAB */
  534. {
  535. TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  536. TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  537. SSL_kECDHE,
  538. SSL_aPSK,
  539. SSL_CHACHA20POLY1305,
  540. SSL_AEAD,
  541. SSL_HANDSHAKE_MAC_SHA256,
  542. },
  543. };
  544. static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
  545. #define CIPHER_ADD 1
  546. #define CIPHER_KILL 2
  547. #define CIPHER_DEL 3
  548. #define CIPHER_ORD 4
  549. #define CIPHER_SPECIAL 5
  550. typedef struct cipher_order_st {
  551. const SSL_CIPHER *cipher;
  552. int active;
  553. int in_group;
  554. struct cipher_order_st *next, *prev;
  555. } CIPHER_ORDER;
  556. typedef struct cipher_alias_st {
  557. /* name is the name of the cipher alias. */
  558. const char *name;
  559. /* The following fields are bitmasks for the corresponding fields on
  560. * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
  561. * bit corresponding to the cipher's value is set to 1. If any bitmask is
  562. * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
  563. uint32_t algorithm_mkey;
  564. uint32_t algorithm_auth;
  565. uint32_t algorithm_enc;
  566. uint32_t algorithm_mac;
  567. /* min_version, if non-zero, matches all ciphers which were added in that
  568. * particular protocol version. */
  569. uint16_t min_version;
  570. } CIPHER_ALIAS;
  571. static const CIPHER_ALIAS kCipherAliases[] = {
  572. /* "ALL" doesn't include eNULL. It must be explicitly enabled. */
  573. {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  574. /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
  575. /* key exchange aliases
  576. * (some of those using only a single bit here combine
  577. * multiple key exchange algs according to the RFCs,
  578. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  579. {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
  580. {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  581. {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  582. {"DH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  583. {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  584. {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  585. {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  586. {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
  587. /* server authentication aliases */
  588. {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
  589. {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  590. {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  591. {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
  592. /* aliases combining key exchange and server authentication */
  593. {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  594. {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  595. {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  596. {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  597. {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
  598. {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
  599. /* symmetric encryption aliases */
  600. {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
  601. {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
  602. {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
  603. {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
  604. {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
  605. {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD, ~0u,
  606. 0},
  607. /* MAC aliases */
  608. {"MD5", ~0u, ~0u, ~0u, SSL_MD5, 0},
  609. {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
  610. {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
  611. {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
  612. {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
  613. /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
  614. * same as "SSLv3". */
  615. {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
  616. {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
  617. {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
  618. /* Legacy strength classes. */
  619. {"HIGH", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  620. {"FIPS", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  621. };
  622. static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
  623. static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  624. const SSL_CIPHER *a = in_a;
  625. const SSL_CIPHER *b = in_b;
  626. if (a->id > b->id) {
  627. return 1;
  628. } else if (a->id < b->id) {
  629. return -1;
  630. } else {
  631. return 0;
  632. }
  633. }
  634. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  635. SSL_CIPHER c;
  636. c.id = 0x03000000L | value;
  637. return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
  638. ssl_cipher_id_cmp);
  639. }
  640. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  641. size_t *out_mac_secret_len,
  642. size_t *out_fixed_iv_len,
  643. const SSL_CIPHER *cipher, uint16_t version) {
  644. *out_aead = NULL;
  645. *out_mac_secret_len = 0;
  646. *out_fixed_iv_len = 0;
  647. if (cipher->algorithm_mac == SSL_AEAD) {
  648. if (cipher->algorithm_enc == SSL_AES128GCM) {
  649. *out_aead = EVP_aead_aes_128_gcm();
  650. *out_fixed_iv_len = 4;
  651. } else if (cipher->algorithm_enc == SSL_AES256GCM) {
  652. *out_aead = EVP_aead_aes_256_gcm();
  653. *out_fixed_iv_len = 4;
  654. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  655. } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305_OLD) {
  656. *out_aead = EVP_aead_chacha20_poly1305_old();
  657. *out_fixed_iv_len = 0;
  658. #endif
  659. } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
  660. *out_aead = EVP_aead_chacha20_poly1305();
  661. *out_fixed_iv_len = 12;
  662. } else {
  663. return 0;
  664. }
  665. /* In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
  666. * above computes the TLS 1.2 construction. */
  667. if (version >= TLS1_3_VERSION) {
  668. *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
  669. }
  670. } else if (cipher->algorithm_mac == SSL_SHA1) {
  671. if (cipher->algorithm_enc == SSL_eNULL) {
  672. if (version == SSL3_VERSION) {
  673. *out_aead = EVP_aead_null_sha1_ssl3();
  674. } else {
  675. *out_aead = EVP_aead_null_sha1_tls();
  676. }
  677. } else if (cipher->algorithm_enc == SSL_3DES) {
  678. if (version == SSL3_VERSION) {
  679. *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
  680. *out_fixed_iv_len = 8;
  681. } else if (version == TLS1_VERSION) {
  682. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
  683. *out_fixed_iv_len = 8;
  684. } else {
  685. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
  686. }
  687. } else if (cipher->algorithm_enc == SSL_AES128) {
  688. if (version == SSL3_VERSION) {
  689. *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
  690. *out_fixed_iv_len = 16;
  691. } else if (version == TLS1_VERSION) {
  692. *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
  693. *out_fixed_iv_len = 16;
  694. } else {
  695. *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
  696. }
  697. } else if (cipher->algorithm_enc == SSL_AES256) {
  698. if (version == SSL3_VERSION) {
  699. *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
  700. *out_fixed_iv_len = 16;
  701. } else if (version == TLS1_VERSION) {
  702. *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
  703. *out_fixed_iv_len = 16;
  704. } else {
  705. *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
  706. }
  707. } else {
  708. return 0;
  709. }
  710. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  711. } else if (cipher->algorithm_mac == SSL_SHA256) {
  712. if (cipher->algorithm_enc == SSL_AES128) {
  713. *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
  714. } else if (cipher->algorithm_enc == SSL_AES256) {
  715. *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
  716. } else {
  717. return 0;
  718. }
  719. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  720. } else if (cipher->algorithm_mac == SSL_SHA384) {
  721. if (cipher->algorithm_enc != SSL_AES256) {
  722. return 0;
  723. }
  724. *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
  725. *out_mac_secret_len = SHA384_DIGEST_LENGTH;
  726. } else {
  727. return 0;
  728. }
  729. return 1;
  730. }
  731. const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
  732. switch (algorithm_prf) {
  733. case SSL_HANDSHAKE_MAC_DEFAULT:
  734. return EVP_sha1();
  735. case SSL_HANDSHAKE_MAC_SHA256:
  736. return EVP_sha256();
  737. case SSL_HANDSHAKE_MAC_SHA384:
  738. return EVP_sha384();
  739. default:
  740. return NULL;
  741. }
  742. }
  743. #define ITEM_SEP(a) \
  744. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  745. /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
  746. * |buf_len| bytes at |buf|. */
  747. static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
  748. /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
  749. return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
  750. }
  751. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  752. CIPHER_ORDER **tail) {
  753. if (curr == *tail) {
  754. return;
  755. }
  756. if (curr == *head) {
  757. *head = curr->next;
  758. }
  759. if (curr->prev != NULL) {
  760. curr->prev->next = curr->next;
  761. }
  762. if (curr->next != NULL) {
  763. curr->next->prev = curr->prev;
  764. }
  765. (*tail)->next = curr;
  766. curr->prev = *tail;
  767. curr->next = NULL;
  768. *tail = curr;
  769. }
  770. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  771. CIPHER_ORDER **tail) {
  772. if (curr == *head) {
  773. return;
  774. }
  775. if (curr == *tail) {
  776. *tail = curr->prev;
  777. }
  778. if (curr->next != NULL) {
  779. curr->next->prev = curr->prev;
  780. }
  781. if (curr->prev != NULL) {
  782. curr->prev->next = curr->next;
  783. }
  784. (*head)->prev = curr;
  785. curr->next = *head;
  786. curr->prev = NULL;
  787. *head = curr;
  788. }
  789. static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
  790. CIPHER_ORDER *co_list,
  791. CIPHER_ORDER **head_p,
  792. CIPHER_ORDER **tail_p) {
  793. /* The set of ciphers is static, but some subset may be unsupported by
  794. * |ssl_method|, so the list may be smaller. */
  795. size_t co_list_num = 0;
  796. for (size_t i = 0; i < kCiphersLen; i++) {
  797. const SSL_CIPHER *cipher = &kCiphers[i];
  798. if (ssl_method->supports_cipher(cipher) &&
  799. /* TLS 1.3 ciphers do not participate in this mechanism. */
  800. cipher->algorithm_mkey != SSL_kGENERIC) {
  801. co_list[co_list_num].cipher = cipher;
  802. co_list[co_list_num].next = NULL;
  803. co_list[co_list_num].prev = NULL;
  804. co_list[co_list_num].active = 0;
  805. co_list[co_list_num].in_group = 0;
  806. co_list_num++;
  807. }
  808. }
  809. /* Prepare linked list from list entries. */
  810. if (co_list_num > 0) {
  811. co_list[0].prev = NULL;
  812. if (co_list_num > 1) {
  813. co_list[0].next = &co_list[1];
  814. for (size_t i = 1; i < co_list_num - 1; i++) {
  815. co_list[i].prev = &co_list[i - 1];
  816. co_list[i].next = &co_list[i + 1];
  817. }
  818. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  819. }
  820. co_list[co_list_num - 1].next = NULL;
  821. *head_p = &co_list[0];
  822. *tail_p = &co_list[co_list_num - 1];
  823. }
  824. }
  825. /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
  826. * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
  827. * head and tail of the list to |*head_p| and |*tail_p|, respectively.
  828. *
  829. * - If |cipher_id| is non-zero, only that cipher is selected.
  830. * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
  831. * of that strength.
  832. * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
  833. * |min_version|. */
  834. static void ssl_cipher_apply_rule(
  835. uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
  836. uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
  837. int strength_bits, int in_group, CIPHER_ORDER **head_p,
  838. CIPHER_ORDER **tail_p) {
  839. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  840. const SSL_CIPHER *cp;
  841. int reverse = 0;
  842. if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
  843. (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
  844. /* The rule matches nothing, so bail early. */
  845. return;
  846. }
  847. if (rule == CIPHER_DEL) {
  848. /* needed to maintain sorting between currently deleted ciphers */
  849. reverse = 1;
  850. }
  851. head = *head_p;
  852. tail = *tail_p;
  853. if (reverse) {
  854. next = tail;
  855. last = head;
  856. } else {
  857. next = head;
  858. last = tail;
  859. }
  860. curr = NULL;
  861. for (;;) {
  862. if (curr == last) {
  863. break;
  864. }
  865. curr = next;
  866. if (curr == NULL) {
  867. break;
  868. }
  869. next = reverse ? curr->prev : curr->next;
  870. cp = curr->cipher;
  871. /* Selection criteria is either a specific cipher, the value of
  872. * |strength_bits|, or the algorithms used. */
  873. if (cipher_id != 0) {
  874. if (cipher_id != cp->id) {
  875. continue;
  876. }
  877. } else if (strength_bits >= 0) {
  878. if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
  879. continue;
  880. }
  881. } else {
  882. if (!(alg_mkey & cp->algorithm_mkey) ||
  883. !(alg_auth & cp->algorithm_auth) ||
  884. !(alg_enc & cp->algorithm_enc) ||
  885. !(alg_mac & cp->algorithm_mac) ||
  886. (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version)) {
  887. continue;
  888. }
  889. }
  890. /* add the cipher if it has not been added yet. */
  891. if (rule == CIPHER_ADD) {
  892. /* reverse == 0 */
  893. if (!curr->active) {
  894. ll_append_tail(&head, curr, &tail);
  895. curr->active = 1;
  896. curr->in_group = in_group;
  897. }
  898. }
  899. /* Move the added cipher to this location */
  900. else if (rule == CIPHER_ORD) {
  901. /* reverse == 0 */
  902. if (curr->active) {
  903. ll_append_tail(&head, curr, &tail);
  904. curr->in_group = 0;
  905. }
  906. } else if (rule == CIPHER_DEL) {
  907. /* reverse == 1 */
  908. if (curr->active) {
  909. /* most recently deleted ciphersuites get best positions
  910. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  911. * works in reverse to maintain the order) */
  912. ll_append_head(&head, curr, &tail);
  913. curr->active = 0;
  914. curr->in_group = 0;
  915. }
  916. } else if (rule == CIPHER_KILL) {
  917. /* reverse == 0 */
  918. if (head == curr) {
  919. head = curr->next;
  920. } else {
  921. curr->prev->next = curr->next;
  922. }
  923. if (tail == curr) {
  924. tail = curr->prev;
  925. }
  926. curr->active = 0;
  927. if (curr->next != NULL) {
  928. curr->next->prev = curr->prev;
  929. }
  930. if (curr->prev != NULL) {
  931. curr->prev->next = curr->next;
  932. }
  933. curr->next = NULL;
  934. curr->prev = NULL;
  935. }
  936. }
  937. *head_p = head;
  938. *tail_p = tail;
  939. }
  940. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  941. CIPHER_ORDER **tail_p) {
  942. int max_strength_bits, i, *number_uses;
  943. CIPHER_ORDER *curr;
  944. /* This routine sorts the ciphers with descending strength. The sorting must
  945. * keep the pre-sorted sequence, so we apply the normal sorting routine as
  946. * '+' movement to the end of the list. */
  947. max_strength_bits = 0;
  948. curr = *head_p;
  949. while (curr != NULL) {
  950. if (curr->active &&
  951. SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
  952. max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
  953. }
  954. curr = curr->next;
  955. }
  956. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  957. if (!number_uses) {
  958. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  959. return 0;
  960. }
  961. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  962. /* Now find the strength_bits values actually used. */
  963. curr = *head_p;
  964. while (curr != NULL) {
  965. if (curr->active) {
  966. number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
  967. }
  968. curr = curr->next;
  969. }
  970. /* Go through the list of used strength_bits values in descending order. */
  971. for (i = max_strength_bits; i >= 0; i--) {
  972. if (number_uses[i] > 0) {
  973. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
  974. }
  975. }
  976. OPENSSL_free(number_uses);
  977. return 1;
  978. }
  979. static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
  980. const char *rule_str,
  981. CIPHER_ORDER **head_p,
  982. CIPHER_ORDER **tail_p) {
  983. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  984. uint16_t min_version;
  985. const char *l, *buf;
  986. int multi, skip_rule, rule, ok, in_group = 0, has_group = 0;
  987. size_t j, buf_len;
  988. uint32_t cipher_id;
  989. char ch;
  990. l = rule_str;
  991. for (;;) {
  992. ch = *l;
  993. if (ch == '\0') {
  994. break; /* done */
  995. }
  996. if (in_group) {
  997. if (ch == ']') {
  998. if (*tail_p) {
  999. (*tail_p)->in_group = 0;
  1000. }
  1001. in_group = 0;
  1002. l++;
  1003. continue;
  1004. }
  1005. if (ch == '|') {
  1006. rule = CIPHER_ADD;
  1007. l++;
  1008. continue;
  1009. } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
  1010. !(ch >= '0' && ch <= '9')) {
  1011. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  1012. return 0;
  1013. } else {
  1014. rule = CIPHER_ADD;
  1015. }
  1016. } else if (ch == '-') {
  1017. rule = CIPHER_DEL;
  1018. l++;
  1019. } else if (ch == '+') {
  1020. rule = CIPHER_ORD;
  1021. l++;
  1022. } else if (ch == '!') {
  1023. rule = CIPHER_KILL;
  1024. l++;
  1025. } else if (ch == '@') {
  1026. rule = CIPHER_SPECIAL;
  1027. l++;
  1028. } else if (ch == '[') {
  1029. if (in_group) {
  1030. OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
  1031. return 0;
  1032. }
  1033. in_group = 1;
  1034. has_group = 1;
  1035. l++;
  1036. continue;
  1037. } else {
  1038. rule = CIPHER_ADD;
  1039. }
  1040. /* If preference groups are enabled, the only legal operator is +.
  1041. * Otherwise the in_group bits will get mixed up. */
  1042. if (has_group && rule != CIPHER_ADD) {
  1043. OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  1044. return 0;
  1045. }
  1046. if (ITEM_SEP(ch)) {
  1047. l++;
  1048. continue;
  1049. }
  1050. multi = 0;
  1051. cipher_id = 0;
  1052. alg_mkey = ~0u;
  1053. alg_auth = ~0u;
  1054. alg_enc = ~0u;
  1055. alg_mac = ~0u;
  1056. min_version = 0;
  1057. skip_rule = 0;
  1058. for (;;) {
  1059. ch = *l;
  1060. buf = l;
  1061. buf_len = 0;
  1062. while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
  1063. ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
  1064. ch = *(++l);
  1065. buf_len++;
  1066. }
  1067. if (buf_len == 0) {
  1068. /* We hit something we cannot deal with, it is no command or separator
  1069. * nor alphanumeric, so we call this an error. */
  1070. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1071. return 0;
  1072. }
  1073. if (rule == CIPHER_SPECIAL) {
  1074. break;
  1075. }
  1076. /* Look for a matching exact cipher. These aren't allowed in multipart
  1077. * rules. */
  1078. if (!multi && ch != '+') {
  1079. for (j = 0; j < kCiphersLen; j++) {
  1080. const SSL_CIPHER *cipher = &kCiphers[j];
  1081. if (rule_equals(cipher->name, buf, buf_len)) {
  1082. cipher_id = cipher->id;
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. if (cipher_id == 0) {
  1088. /* If not an exact cipher, look for a matching cipher alias. */
  1089. for (j = 0; j < kCipherAliasesLen; j++) {
  1090. if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
  1091. alg_mkey &= kCipherAliases[j].algorithm_mkey;
  1092. alg_auth &= kCipherAliases[j].algorithm_auth;
  1093. alg_enc &= kCipherAliases[j].algorithm_enc;
  1094. alg_mac &= kCipherAliases[j].algorithm_mac;
  1095. if (min_version != 0 &&
  1096. min_version != kCipherAliases[j].min_version) {
  1097. skip_rule = 1;
  1098. } else {
  1099. min_version = kCipherAliases[j].min_version;
  1100. }
  1101. break;
  1102. }
  1103. }
  1104. if (j == kCipherAliasesLen) {
  1105. skip_rule = 1;
  1106. }
  1107. }
  1108. /* Check for a multipart rule. */
  1109. if (ch != '+') {
  1110. break;
  1111. }
  1112. l++;
  1113. multi = 1;
  1114. }
  1115. /* If one of the CHACHA20_POLY1305 variants is selected, include the other
  1116. * as well. They have the same name to avoid requiring changes in
  1117. * configuration. Apply this transformation late so that the cipher name
  1118. * still behaves as an exact name and not an alias in multipart rules.
  1119. *
  1120. * This is temporary and will be removed when the pre-standard construction
  1121. * is removed. */
  1122. if (cipher_id == TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD ||
  1123. cipher_id == TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) {
  1124. cipher_id = 0;
  1125. alg_mkey = SSL_kECDHE;
  1126. alg_auth = SSL_aRSA;
  1127. alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
  1128. alg_mac = SSL_AEAD;
  1129. } else if (cipher_id == TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD ||
  1130. cipher_id == TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) {
  1131. cipher_id = 0;
  1132. alg_mkey = SSL_kECDHE;
  1133. alg_auth = SSL_aECDSA;
  1134. alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
  1135. alg_mac = SSL_AEAD;
  1136. }
  1137. /* Ok, we have the rule, now apply it. */
  1138. if (rule == CIPHER_SPECIAL) {
  1139. /* special command */
  1140. ok = 0;
  1141. if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
  1142. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1143. } else {
  1144. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1145. }
  1146. if (ok == 0) {
  1147. return 0;
  1148. }
  1149. /* We do not support any "multi" options together with "@", so throw away
  1150. * the rest of the command, if any left, until end or ':' is found. */
  1151. while (*l != '\0' && !ITEM_SEP(*l)) {
  1152. l++;
  1153. }
  1154. } else if (!skip_rule) {
  1155. ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
  1156. min_version, rule, -1, in_group, head_p, tail_p);
  1157. }
  1158. }
  1159. if (in_group) {
  1160. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1161. return 0;
  1162. }
  1163. return 1;
  1164. }
  1165. STACK_OF(SSL_CIPHER) *
  1166. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
  1167. struct ssl_cipher_preference_list_st **out_cipher_list,
  1168. const char *rule_str) {
  1169. STACK_OF(SSL_CIPHER) *cipherstack = NULL;
  1170. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1171. uint8_t *in_group_flags = NULL;
  1172. unsigned int num_in_group_flags = 0;
  1173. struct ssl_cipher_preference_list_st *pref_list = NULL;
  1174. /* Return with error if nothing to do. */
  1175. if (rule_str == NULL || out_cipher_list == NULL) {
  1176. return NULL;
  1177. }
  1178. /* Now we have to collect the available ciphers from the compiled in ciphers.
  1179. * We cannot get more than the number compiled in, so it is used for
  1180. * allocation. */
  1181. co_list = OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
  1182. if (co_list == NULL) {
  1183. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1184. return NULL;
  1185. }
  1186. ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
  1187. /* Now arrange all ciphers by preference:
  1188. * TODO(davidben): Compute this order once and copy it. */
  1189. /* Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
  1190. * key exchange mechanisms */
  1191. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
  1192. 0, &head, &tail);
  1193. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
  1194. &head, &tail);
  1195. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
  1196. &tail);
  1197. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  1198. * CHACHA20 unless there is hardware support for fast and constant-time
  1199. * AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
  1200. * old one. */
  1201. if (EVP_has_aes_hardware()) {
  1202. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1203. &head, &tail);
  1204. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1205. &head, &tail);
  1206. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1207. -1, 0, &head, &tail);
  1208. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
  1209. CIPHER_ADD, -1, 0, &head, &tail);
  1210. } else {
  1211. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1212. -1, 0, &head, &tail);
  1213. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
  1214. CIPHER_ADD, -1, 0, &head, &tail);
  1215. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1216. &head, &tail);
  1217. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1218. &head, &tail);
  1219. }
  1220. /* Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
  1221. * 3DES_EDE_CBC_SHA. */
  1222. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
  1223. &head, &tail);
  1224. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
  1225. &head, &tail);
  1226. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
  1227. &tail);
  1228. /* Temporarily enable everything else for sorting */
  1229. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
  1230. &tail);
  1231. /* Move ciphers without forward secrecy to the end. */
  1232. ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0,
  1233. CIPHER_ORD, -1, 0, &head, &tail);
  1234. /* Now disable everything (maintaining the ordering!) */
  1235. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
  1236. &tail);
  1237. /* If the rule_string begins with DEFAULT, apply the default rule before
  1238. * using the (possibly available) additional rules. */
  1239. const char *rule_p = rule_str;
  1240. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1241. if (!ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
  1242. &tail)) {
  1243. goto err;
  1244. }
  1245. rule_p += 7;
  1246. if (*rule_p == ':') {
  1247. rule_p++;
  1248. }
  1249. }
  1250. if (*rule_p != '\0' &&
  1251. !ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail)) {
  1252. goto err;
  1253. }
  1254. /* Allocate new "cipherstack" for the result, return with error
  1255. * if we cannot get one. */
  1256. cipherstack = sk_SSL_CIPHER_new_null();
  1257. if (cipherstack == NULL) {
  1258. goto err;
  1259. }
  1260. in_group_flags = OPENSSL_malloc(kCiphersLen);
  1261. if (!in_group_flags) {
  1262. goto err;
  1263. }
  1264. /* The cipher selection for the list is done. The ciphers are added
  1265. * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
  1266. for (curr = head; curr != NULL; curr = curr->next) {
  1267. if (curr->active) {
  1268. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1269. goto err;
  1270. }
  1271. in_group_flags[num_in_group_flags++] = curr->in_group;
  1272. }
  1273. }
  1274. OPENSSL_free(co_list); /* Not needed any longer */
  1275. co_list = NULL;
  1276. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1277. if (!pref_list) {
  1278. goto err;
  1279. }
  1280. pref_list->ciphers = cipherstack;
  1281. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1282. if (!pref_list->in_group_flags) {
  1283. goto err;
  1284. }
  1285. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1286. OPENSSL_free(in_group_flags);
  1287. in_group_flags = NULL;
  1288. if (*out_cipher_list != NULL) {
  1289. ssl_cipher_preference_list_free(*out_cipher_list);
  1290. }
  1291. *out_cipher_list = pref_list;
  1292. pref_list = NULL;
  1293. return cipherstack;
  1294. err:
  1295. OPENSSL_free(co_list);
  1296. OPENSSL_free(in_group_flags);
  1297. sk_SSL_CIPHER_free(cipherstack);
  1298. if (pref_list) {
  1299. OPENSSL_free(pref_list->in_group_flags);
  1300. }
  1301. OPENSSL_free(pref_list);
  1302. return NULL;
  1303. }
  1304. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
  1305. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
  1306. uint32_t id = cipher->id;
  1307. /* All ciphers are SSLv3. */
  1308. assert((id & 0xff000000) == 0x03000000);
  1309. return id & 0xffff;
  1310. }
  1311. int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
  1312. return (cipher->algorithm_enc & SSL_AES) != 0;
  1313. }
  1314. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
  1315. return (cipher->algorithm_mac & SSL_MD5) != 0;
  1316. }
  1317. int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
  1318. return (cipher->algorithm_mac & SSL_SHA1) != 0;
  1319. }
  1320. int SSL_CIPHER_has_SHA256_HMAC(const SSL_CIPHER *cipher) {
  1321. return (cipher->algorithm_mac & SSL_SHA256) != 0;
  1322. }
  1323. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
  1324. return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
  1325. }
  1326. int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
  1327. return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
  1328. }
  1329. int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
  1330. return (cipher->algorithm_enc & SSL_AES128) != 0;
  1331. }
  1332. int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
  1333. return (cipher->algorithm_enc & SSL_AES256) != 0;
  1334. }
  1335. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
  1336. return (cipher->algorithm_enc &
  1337. (SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD)) != 0;
  1338. }
  1339. int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
  1340. return (cipher->algorithm_enc & SSL_eNULL) != 0;
  1341. }
  1342. int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
  1343. return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
  1344. cipher->algorithm_mac != SSL_AEAD;
  1345. }
  1346. int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
  1347. return (cipher->algorithm_auth & SSL_aECDSA) != 0;
  1348. }
  1349. int SSL_CIPHER_is_DHE(const SSL_CIPHER *cipher) {
  1350. return (cipher->algorithm_mkey & SSL_kDHE) != 0;
  1351. }
  1352. int SSL_CIPHER_is_ECDHE(const SSL_CIPHER *cipher) {
  1353. return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
  1354. }
  1355. uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
  1356. if (cipher->algorithm_mkey == SSL_kGENERIC ||
  1357. cipher->algorithm_auth == SSL_aGENERIC) {
  1358. return TLS1_3_VERSION;
  1359. }
  1360. if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
  1361. /* Cipher suites before TLS 1.2 use the default PRF, while all those added
  1362. * afterwards specify a particular hash. */
  1363. return TLS1_2_VERSION;
  1364. }
  1365. return SSL3_VERSION;
  1366. }
  1367. uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
  1368. if (cipher->algorithm_mkey == SSL_kGENERIC ||
  1369. cipher->algorithm_auth == SSL_aGENERIC) {
  1370. return TLS1_3_VERSION;
  1371. }
  1372. return TLS1_2_VERSION;
  1373. }
  1374. /* return the actual cipher being used */
  1375. const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
  1376. if (cipher != NULL) {
  1377. return cipher->name;
  1378. }
  1379. return "(NONE)";
  1380. }
  1381. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1382. if (cipher == NULL) {
  1383. return "";
  1384. }
  1385. switch (cipher->algorithm_mkey) {
  1386. case SSL_kRSA:
  1387. return "RSA";
  1388. case SSL_kDHE:
  1389. switch (cipher->algorithm_auth) {
  1390. case SSL_aRSA:
  1391. return "DHE_RSA";
  1392. default:
  1393. assert(0);
  1394. return "UNKNOWN";
  1395. }
  1396. case SSL_kECDHE:
  1397. switch (cipher->algorithm_auth) {
  1398. case SSL_aECDSA:
  1399. return "ECDHE_ECDSA";
  1400. case SSL_aRSA:
  1401. return "ECDHE_RSA";
  1402. case SSL_aPSK:
  1403. return "ECDHE_PSK";
  1404. default:
  1405. assert(0);
  1406. return "UNKNOWN";
  1407. }
  1408. case SSL_kPSK:
  1409. assert(cipher->algorithm_auth == SSL_aPSK);
  1410. return "PSK";
  1411. case SSL_kGENERIC:
  1412. assert(cipher->algorithm_auth == SSL_aGENERIC);
  1413. return "GENERIC";
  1414. default:
  1415. assert(0);
  1416. return "UNKNOWN";
  1417. }
  1418. }
  1419. static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
  1420. switch (cipher->algorithm_enc) {
  1421. case SSL_3DES:
  1422. return "3DES_EDE_CBC";
  1423. case SSL_AES128:
  1424. return "AES_128_CBC";
  1425. case SSL_AES256:
  1426. return "AES_256_CBC";
  1427. case SSL_AES128GCM:
  1428. return "AES_128_GCM";
  1429. case SSL_AES256GCM:
  1430. return "AES_256_GCM";
  1431. case SSL_CHACHA20POLY1305:
  1432. case SSL_CHACHA20POLY1305_OLD:
  1433. return "CHACHA20_POLY1305";
  1434. break;
  1435. default:
  1436. assert(0);
  1437. return "UNKNOWN";
  1438. }
  1439. }
  1440. static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
  1441. switch (cipher->algorithm_prf) {
  1442. case SSL_HANDSHAKE_MAC_DEFAULT:
  1443. /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
  1444. * only ever MD5 or SHA-1. */
  1445. switch (cipher->algorithm_mac) {
  1446. case SSL_MD5:
  1447. return "MD5";
  1448. case SSL_SHA1:
  1449. return "SHA";
  1450. }
  1451. break;
  1452. case SSL_HANDSHAKE_MAC_SHA256:
  1453. return "SHA256";
  1454. case SSL_HANDSHAKE_MAC_SHA384:
  1455. return "SHA384";
  1456. }
  1457. assert(0);
  1458. return "UNKNOWN";
  1459. }
  1460. char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
  1461. if (cipher == NULL) {
  1462. return NULL;
  1463. }
  1464. const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
  1465. const char *enc_name = ssl_cipher_get_enc_name(cipher);
  1466. const char *prf_name = ssl_cipher_get_prf_name(cipher);
  1467. /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name} or
  1468. * TLS_{enc_name}_{prf_name} depending on whether the cipher is AEAD-only. */
  1469. size_t len = 4 + strlen(enc_name) + 1 + strlen(prf_name) + 1;
  1470. if (cipher->algorithm_mkey != SSL_kGENERIC) {
  1471. len += strlen(kx_name) + 6;
  1472. }
  1473. char *ret = OPENSSL_malloc(len);
  1474. if (ret == NULL) {
  1475. return NULL;
  1476. }
  1477. if (BUF_strlcpy(ret, "TLS_", len) >= len ||
  1478. (cipher->algorithm_mkey != SSL_kGENERIC &&
  1479. (BUF_strlcat(ret, kx_name, len) >= len ||
  1480. BUF_strlcat(ret, "_WITH_", len) >= len)) ||
  1481. BUF_strlcat(ret, enc_name, len) >= len ||
  1482. BUF_strlcat(ret, "_", len) >= len ||
  1483. BUF_strlcat(ret, prf_name, len) >= len) {
  1484. assert(0);
  1485. OPENSSL_free(ret);
  1486. return NULL;
  1487. }
  1488. assert(strlen(ret) + 1 == len);
  1489. return ret;
  1490. }
  1491. int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
  1492. if (cipher == NULL) {
  1493. return 0;
  1494. }
  1495. int alg_bits, strength_bits;
  1496. switch (cipher->algorithm_enc) {
  1497. case SSL_AES128:
  1498. case SSL_AES128GCM:
  1499. alg_bits = 128;
  1500. strength_bits = 128;
  1501. break;
  1502. case SSL_AES256:
  1503. case SSL_AES256GCM:
  1504. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  1505. case SSL_CHACHA20POLY1305_OLD:
  1506. #endif
  1507. case SSL_CHACHA20POLY1305:
  1508. alg_bits = 256;
  1509. strength_bits = 256;
  1510. break;
  1511. case SSL_3DES:
  1512. alg_bits = 168;
  1513. strength_bits = 112;
  1514. break;
  1515. case SSL_eNULL:
  1516. alg_bits = 0;
  1517. strength_bits = 0;
  1518. break;
  1519. default:
  1520. assert(0);
  1521. alg_bits = 0;
  1522. strength_bits = 0;
  1523. }
  1524. if (out_alg_bits != NULL) {
  1525. *out_alg_bits = alg_bits;
  1526. }
  1527. return strength_bits;
  1528. }
  1529. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
  1530. int len) {
  1531. const char *kx, *au, *enc, *mac;
  1532. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1533. alg_mkey = cipher->algorithm_mkey;
  1534. alg_auth = cipher->algorithm_auth;
  1535. alg_enc = cipher->algorithm_enc;
  1536. alg_mac = cipher->algorithm_mac;
  1537. switch (alg_mkey) {
  1538. case SSL_kRSA:
  1539. kx = "RSA";
  1540. break;
  1541. case SSL_kDHE:
  1542. kx = "DH";
  1543. break;
  1544. case SSL_kECDHE:
  1545. kx = "ECDH";
  1546. break;
  1547. case SSL_kPSK:
  1548. kx = "PSK";
  1549. break;
  1550. case SSL_kGENERIC:
  1551. kx = "GENERIC";
  1552. break;
  1553. default:
  1554. kx = "unknown";
  1555. }
  1556. switch (alg_auth) {
  1557. case SSL_aRSA:
  1558. au = "RSA";
  1559. break;
  1560. case SSL_aECDSA:
  1561. au = "ECDSA";
  1562. break;
  1563. case SSL_aPSK:
  1564. au = "PSK";
  1565. break;
  1566. case SSL_aGENERIC:
  1567. au = "GENERIC";
  1568. break;
  1569. default:
  1570. au = "unknown";
  1571. break;
  1572. }
  1573. switch (alg_enc) {
  1574. case SSL_3DES:
  1575. enc = "3DES(168)";
  1576. break;
  1577. case SSL_AES128:
  1578. enc = "AES(128)";
  1579. break;
  1580. case SSL_AES256:
  1581. enc = "AES(256)";
  1582. break;
  1583. case SSL_AES128GCM:
  1584. enc = "AESGCM(128)";
  1585. break;
  1586. case SSL_AES256GCM:
  1587. enc = "AESGCM(256)";
  1588. break;
  1589. case SSL_CHACHA20POLY1305_OLD:
  1590. enc = "ChaCha20-Poly1305-Old";
  1591. break;
  1592. case SSL_CHACHA20POLY1305:
  1593. enc = "ChaCha20-Poly1305";
  1594. break;
  1595. case SSL_eNULL:
  1596. enc="None";
  1597. break;
  1598. default:
  1599. enc = "unknown";
  1600. break;
  1601. }
  1602. switch (alg_mac) {
  1603. case SSL_MD5:
  1604. mac = "MD5";
  1605. break;
  1606. case SSL_SHA1:
  1607. mac = "SHA1";
  1608. break;
  1609. case SSL_SHA256:
  1610. mac = "SHA256";
  1611. break;
  1612. case SSL_SHA384:
  1613. mac = "SHA384";
  1614. break;
  1615. case SSL_AEAD:
  1616. mac = "AEAD";
  1617. break;
  1618. default:
  1619. mac = "unknown";
  1620. break;
  1621. }
  1622. if (buf == NULL) {
  1623. len = 128;
  1624. buf = OPENSSL_malloc(len);
  1625. if (buf == NULL) {
  1626. return NULL;
  1627. }
  1628. } else if (len < 128) {
  1629. return "Buffer too small";
  1630. }
  1631. BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
  1632. cipher->name, kx, au, enc, mac);
  1633. return buf;
  1634. }
  1635. const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
  1636. return "TLSv1/SSLv3";
  1637. }
  1638. COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
  1639. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
  1640. const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
  1641. void SSL_COMP_free_compression_methods(void) {}
  1642. int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
  1643. uint32_t alg_a = cipher->algorithm_auth;
  1644. if (alg_a & SSL_aECDSA) {
  1645. return EVP_PKEY_EC;
  1646. } else if (alg_a & SSL_aRSA) {
  1647. return EVP_PKEY_RSA;
  1648. }
  1649. return EVP_PKEY_NONE;
  1650. }
  1651. int ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
  1652. return (cipher->algorithm_auth & SSL_aCERT) != 0;
  1653. }
  1654. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
  1655. /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
  1656. if (cipher->algorithm_mkey & SSL_kDHE ||
  1657. cipher->algorithm_mkey & SSL_kECDHE) {
  1658. return 1;
  1659. }
  1660. /* It is optional in all others. */
  1661. return 0;
  1662. }
  1663. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
  1664. size_t block_size;
  1665. switch (cipher->algorithm_enc) {
  1666. case SSL_3DES:
  1667. block_size = 8;
  1668. break;
  1669. case SSL_AES128:
  1670. case SSL_AES256:
  1671. block_size = 16;
  1672. break;
  1673. default:
  1674. return 0;
  1675. }
  1676. size_t mac_len;
  1677. switch (cipher->algorithm_mac) {
  1678. case SSL_MD5:
  1679. mac_len = MD5_DIGEST_LENGTH;
  1680. break;
  1681. case SSL_SHA1:
  1682. mac_len = SHA_DIGEST_LENGTH;
  1683. break;
  1684. default:
  1685. return 0;
  1686. }
  1687. size_t ret = 1 + mac_len;
  1688. ret += block_size - (ret % block_size);
  1689. return ret;
  1690. }