Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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