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.
 
 
 
 
 
 

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