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.
 
 
 
 
 
 

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