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.
 
 
 
 
 
 

2008 lines
54 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <stdio.h>
  143. #include <string.h>
  144. #include <openssl/buf.h>
  145. #include <openssl/err.h>
  146. #include <openssl/md5.h>
  147. #include <openssl/mem.h>
  148. #include <openssl/sha.h>
  149. #include <openssl/stack.h>
  150. #include "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 04 */
  165. {
  166. SSL3_TXT_RSA_RC4_128_MD5,
  167. SSL3_CK_RSA_RC4_128_MD5,
  168. SSL_kRSA,
  169. SSL_aRSA,
  170. SSL_RC4,
  171. SSL_MD5,
  172. SSL_HANDSHAKE_MAC_DEFAULT,
  173. },
  174. /* Cipher 05 */
  175. {
  176. SSL3_TXT_RSA_RC4_128_SHA,
  177. SSL3_CK_RSA_RC4_128_SHA,
  178. SSL_kRSA,
  179. SSL_aRSA,
  180. SSL_RC4,
  181. SSL_SHA1,
  182. SSL_HANDSHAKE_MAC_DEFAULT,
  183. },
  184. /* Cipher 0A */
  185. {
  186. SSL3_TXT_RSA_DES_192_CBC3_SHA,
  187. SSL3_CK_RSA_DES_192_CBC3_SHA,
  188. SSL_kRSA,
  189. SSL_aRSA,
  190. SSL_3DES,
  191. SSL_SHA1,
  192. SSL_HANDSHAKE_MAC_DEFAULT,
  193. },
  194. /* New AES ciphersuites */
  195. /* Cipher 2F */
  196. {
  197. TLS1_TXT_RSA_WITH_AES_128_SHA,
  198. TLS1_CK_RSA_WITH_AES_128_SHA,
  199. SSL_kRSA,
  200. SSL_aRSA,
  201. SSL_AES128,
  202. SSL_SHA1,
  203. SSL_HANDSHAKE_MAC_DEFAULT,
  204. },
  205. /* Cipher 33 */
  206. {
  207. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
  208. TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
  209. SSL_kDHE,
  210. SSL_aRSA,
  211. SSL_AES128,
  212. SSL_SHA1,
  213. SSL_HANDSHAKE_MAC_DEFAULT,
  214. },
  215. /* Cipher 35 */
  216. {
  217. TLS1_TXT_RSA_WITH_AES_256_SHA,
  218. TLS1_CK_RSA_WITH_AES_256_SHA,
  219. SSL_kRSA,
  220. SSL_aRSA,
  221. SSL_AES256,
  222. SSL_SHA1,
  223. SSL_HANDSHAKE_MAC_DEFAULT,
  224. },
  225. /* Cipher 39 */
  226. {
  227. TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
  228. TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
  229. SSL_kDHE,
  230. SSL_aRSA,
  231. SSL_AES256,
  232. SSL_SHA1,
  233. SSL_HANDSHAKE_MAC_DEFAULT,
  234. },
  235. /* TLS v1.2 ciphersuites */
  236. /* Cipher 3C */
  237. {
  238. TLS1_TXT_RSA_WITH_AES_128_SHA256,
  239. TLS1_CK_RSA_WITH_AES_128_SHA256,
  240. SSL_kRSA,
  241. SSL_aRSA,
  242. SSL_AES128,
  243. SSL_SHA256,
  244. SSL_HANDSHAKE_MAC_SHA256,
  245. },
  246. /* Cipher 3D */
  247. {
  248. TLS1_TXT_RSA_WITH_AES_256_SHA256,
  249. TLS1_CK_RSA_WITH_AES_256_SHA256,
  250. SSL_kRSA,
  251. SSL_aRSA,
  252. SSL_AES256,
  253. SSL_SHA256,
  254. SSL_HANDSHAKE_MAC_SHA256,
  255. },
  256. /* Cipher 67 */
  257. {
  258. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
  259. TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
  260. SSL_kDHE,
  261. SSL_aRSA,
  262. SSL_AES128,
  263. SSL_SHA256,
  264. SSL_HANDSHAKE_MAC_SHA256,
  265. },
  266. /* Cipher 6B */
  267. {
  268. TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
  269. TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
  270. SSL_kDHE,
  271. SSL_aRSA,
  272. SSL_AES256,
  273. SSL_SHA256,
  274. SSL_HANDSHAKE_MAC_SHA256,
  275. },
  276. /* PSK cipher suites. */
  277. /* Cipher 8A */
  278. {
  279. TLS1_TXT_PSK_WITH_RC4_128_SHA,
  280. TLS1_CK_PSK_WITH_RC4_128_SHA,
  281. SSL_kPSK,
  282. SSL_aPSK,
  283. SSL_RC4,
  284. SSL_SHA1,
  285. SSL_HANDSHAKE_MAC_DEFAULT,
  286. },
  287. /* Cipher 8C */
  288. {
  289. TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
  290. TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
  291. SSL_kPSK,
  292. SSL_aPSK,
  293. SSL_AES128,
  294. SSL_SHA1,
  295. SSL_HANDSHAKE_MAC_DEFAULT,
  296. },
  297. /* Cipher 8D */
  298. {
  299. TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
  300. TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
  301. SSL_kPSK,
  302. SSL_aPSK,
  303. SSL_AES256,
  304. SSL_SHA1,
  305. SSL_HANDSHAKE_MAC_DEFAULT,
  306. },
  307. /* GCM ciphersuites from RFC5288 */
  308. /* Cipher 9C */
  309. {
  310. TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
  311. TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  312. SSL_kRSA,
  313. SSL_aRSA,
  314. SSL_AES128GCM,
  315. SSL_AEAD,
  316. SSL_HANDSHAKE_MAC_SHA256,
  317. },
  318. /* Cipher 9D */
  319. {
  320. TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
  321. TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  322. SSL_kRSA,
  323. SSL_aRSA,
  324. SSL_AES256GCM,
  325. SSL_AEAD,
  326. SSL_HANDSHAKE_MAC_SHA384,
  327. },
  328. /* Cipher 9E */
  329. {
  330. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
  331. TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
  332. SSL_kDHE,
  333. SSL_aRSA,
  334. SSL_AES128GCM,
  335. SSL_AEAD,
  336. SSL_HANDSHAKE_MAC_SHA256,
  337. },
  338. /* Cipher 9F */
  339. {
  340. TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
  341. TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
  342. SSL_kDHE,
  343. SSL_aRSA,
  344. SSL_AES256GCM,
  345. SSL_AEAD,
  346. SSL_HANDSHAKE_MAC_SHA384,
  347. },
  348. /* Cipher C007 */
  349. {
  350. TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
  351. TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA,
  352. SSL_kECDHE,
  353. SSL_aECDSA,
  354. SSL_RC4,
  355. SSL_SHA1,
  356. SSL_HANDSHAKE_MAC_DEFAULT,
  357. },
  358. /* Cipher C009 */
  359. {
  360. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  361. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  362. SSL_kECDHE,
  363. SSL_aECDSA,
  364. SSL_AES128,
  365. SSL_SHA1,
  366. SSL_HANDSHAKE_MAC_DEFAULT,
  367. },
  368. /* Cipher C00A */
  369. {
  370. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  371. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  372. SSL_kECDHE,
  373. SSL_aECDSA,
  374. SSL_AES256,
  375. SSL_SHA1,
  376. SSL_HANDSHAKE_MAC_DEFAULT,
  377. },
  378. /* Cipher C011 */
  379. {
  380. TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA,
  381. TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
  382. SSL_kECDHE,
  383. SSL_aRSA,
  384. SSL_RC4,
  385. SSL_SHA1,
  386. SSL_HANDSHAKE_MAC_DEFAULT,
  387. },
  388. /* Cipher C013 */
  389. {
  390. TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  391. TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  392. SSL_kECDHE,
  393. SSL_aRSA,
  394. SSL_AES128,
  395. SSL_SHA1,
  396. SSL_HANDSHAKE_MAC_DEFAULT,
  397. },
  398. /* Cipher C014 */
  399. {
  400. TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  401. TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  402. SSL_kECDHE,
  403. SSL_aRSA,
  404. SSL_AES256,
  405. SSL_SHA1,
  406. SSL_HANDSHAKE_MAC_DEFAULT,
  407. },
  408. /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
  409. /* Cipher C023 */
  410. {
  411. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
  412. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
  413. SSL_kECDHE,
  414. SSL_aECDSA,
  415. SSL_AES128,
  416. SSL_SHA256,
  417. SSL_HANDSHAKE_MAC_SHA256,
  418. },
  419. /* Cipher C024 */
  420. {
  421. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
  422. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
  423. SSL_kECDHE,
  424. SSL_aECDSA,
  425. SSL_AES256,
  426. SSL_SHA384,
  427. SSL_HANDSHAKE_MAC_SHA384,
  428. },
  429. /* Cipher C027 */
  430. {
  431. TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
  432. TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
  433. SSL_kECDHE,
  434. SSL_aRSA,
  435. SSL_AES128,
  436. SSL_SHA256,
  437. SSL_HANDSHAKE_MAC_SHA256,
  438. },
  439. /* Cipher C028 */
  440. {
  441. TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
  442. TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
  443. SSL_kECDHE,
  444. SSL_aRSA,
  445. SSL_AES256,
  446. SSL_SHA384,
  447. SSL_HANDSHAKE_MAC_SHA384,
  448. },
  449. /* GCM based TLS v1.2 ciphersuites from RFC5289 */
  450. /* Cipher C02B */
  451. {
  452. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  453. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  454. SSL_kECDHE,
  455. SSL_aECDSA,
  456. SSL_AES128GCM,
  457. SSL_AEAD,
  458. SSL_HANDSHAKE_MAC_SHA256,
  459. },
  460. /* Cipher C02C */
  461. {
  462. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  463. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  464. SSL_kECDHE,
  465. SSL_aECDSA,
  466. SSL_AES256GCM,
  467. SSL_AEAD,
  468. SSL_HANDSHAKE_MAC_SHA384,
  469. },
  470. /* Cipher C02F */
  471. {
  472. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  473. TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  474. SSL_kECDHE,
  475. SSL_aRSA,
  476. SSL_AES128GCM,
  477. SSL_AEAD,
  478. SSL_HANDSHAKE_MAC_SHA256,
  479. },
  480. /* Cipher C030 */
  481. {
  482. TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  483. TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  484. SSL_kECDHE,
  485. SSL_aRSA,
  486. SSL_AES256GCM,
  487. SSL_AEAD,
  488. SSL_HANDSHAKE_MAC_SHA384,
  489. },
  490. /* ECDHE-PSK cipher suites. */
  491. /* Cipher C035 */
  492. {
  493. TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  494. TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  495. SSL_kECDHE,
  496. SSL_aPSK,
  497. SSL_AES128,
  498. SSL_SHA1,
  499. SSL_HANDSHAKE_MAC_DEFAULT,
  500. },
  501. /* Cipher C036 */
  502. {
  503. TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  504. TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  505. SSL_kECDHE,
  506. SSL_aPSK,
  507. SSL_AES256,
  508. SSL_SHA1,
  509. SSL_HANDSHAKE_MAC_DEFAULT,
  510. },
  511. /* ChaCha20-Poly1305 cipher suites. */
  512. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  513. {
  514. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
  515. TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD,
  516. SSL_kECDHE,
  517. SSL_aRSA,
  518. SSL_CHACHA20POLY1305_OLD,
  519. SSL_AEAD,
  520. SSL_HANDSHAKE_MAC_SHA256,
  521. },
  522. {
  523. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
  524. TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD,
  525. SSL_kECDHE,
  526. SSL_aECDSA,
  527. SSL_CHACHA20POLY1305_OLD,
  528. SSL_AEAD,
  529. SSL_HANDSHAKE_MAC_SHA256,
  530. },
  531. #endif
  532. /* Cipher CCA8 */
  533. {
  534. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  535. TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  536. SSL_kECDHE,
  537. SSL_aRSA,
  538. SSL_CHACHA20POLY1305,
  539. SSL_AEAD,
  540. SSL_HANDSHAKE_MAC_SHA256,
  541. },
  542. /* Cipher CCA9 */
  543. {
  544. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  545. TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  546. SSL_kECDHE,
  547. SSL_aECDSA,
  548. SSL_CHACHA20POLY1305,
  549. SSL_AEAD,
  550. SSL_HANDSHAKE_MAC_SHA256,
  551. },
  552. /* Cipher CCAB */
  553. {
  554. TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  555. TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  556. SSL_kECDHE,
  557. SSL_aPSK,
  558. SSL_CHACHA20POLY1305,
  559. SSL_AEAD,
  560. SSL_HANDSHAKE_MAC_SHA256,
  561. },
  562. };
  563. static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
  564. #define CIPHER_ADD 1
  565. #define CIPHER_KILL 2
  566. #define CIPHER_DEL 3
  567. #define CIPHER_ORD 4
  568. #define CIPHER_SPECIAL 5
  569. typedef struct cipher_order_st {
  570. const SSL_CIPHER *cipher;
  571. int active;
  572. int in_group;
  573. struct cipher_order_st *next, *prev;
  574. } CIPHER_ORDER;
  575. typedef struct cipher_alias_st {
  576. /* name is the name of the cipher alias. */
  577. const char *name;
  578. /* The following fields are bitmasks for the corresponding fields on
  579. * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
  580. * bit corresponding to the cipher's value is set to 1. If any bitmask is
  581. * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
  582. uint32_t algorithm_mkey;
  583. uint32_t algorithm_auth;
  584. uint32_t algorithm_enc;
  585. uint32_t algorithm_mac;
  586. /* min_version, if non-zero, matches all ciphers which were added in that
  587. * particular protocol version. */
  588. uint16_t min_version;
  589. } CIPHER_ALIAS;
  590. static const CIPHER_ALIAS kCipherAliases[] = {
  591. /* "ALL" doesn't include eNULL (must be specifically enabled) */
  592. {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
  593. /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
  594. /* key exchange aliases
  595. * (some of those using only a single bit here combine
  596. * multiple key exchange algs according to the RFCs,
  597. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  598. {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
  599. {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  600. {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  601. {"DH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  602. {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  603. {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  604. {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  605. {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
  606. /* server authentication aliases */
  607. {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
  608. {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  609. {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  610. {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
  611. /* aliases combining key exchange and server authentication */
  612. {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  613. {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
  614. {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  615. {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  616. {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
  617. {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
  618. /* symmetric encryption aliases */
  619. {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
  620. {"RC4", ~0u, ~0u, SSL_RC4, ~0u, 0},
  621. {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
  622. {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
  623. {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
  624. {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
  625. {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD, ~0u,
  626. 0},
  627. /* MAC aliases */
  628. {"MD5", ~0u, ~0u, ~0u, SSL_MD5, 0},
  629. {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
  630. {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
  631. {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
  632. {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
  633. /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
  634. * same as "SSLv3". */
  635. {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
  636. {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
  637. {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
  638. /* Legacy strength classes. */
  639. {"MEDIUM", ~0u, ~0u, SSL_RC4, ~0u, 0},
  640. {"HIGH", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
  641. {"FIPS", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
  642. };
  643. static const size_t kCipherAliasesLen =
  644. sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
  645. static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  646. const SSL_CIPHER *a = in_a;
  647. const SSL_CIPHER *b = in_b;
  648. if (a->id > b->id) {
  649. return 1;
  650. } else if (a->id < b->id) {
  651. return -1;
  652. } else {
  653. return 0;
  654. }
  655. }
  656. static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
  657. return ssl_cipher_id_cmp(*a, *b);
  658. }
  659. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  660. SSL_CIPHER c;
  661. c.id = 0x03000000L | value;
  662. return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
  663. ssl_cipher_id_cmp);
  664. }
  665. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  666. size_t *out_mac_secret_len,
  667. size_t *out_fixed_iv_len,
  668. const SSL_CIPHER *cipher, uint16_t version) {
  669. *out_aead = NULL;
  670. *out_mac_secret_len = 0;
  671. *out_fixed_iv_len = 0;
  672. switch (cipher->algorithm_enc) {
  673. case SSL_AES128GCM:
  674. *out_aead = EVP_aead_aes_128_gcm();
  675. *out_fixed_iv_len = 4;
  676. return 1;
  677. case SSL_AES256GCM:
  678. *out_aead = EVP_aead_aes_256_gcm();
  679. *out_fixed_iv_len = 4;
  680. return 1;
  681. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  682. case SSL_CHACHA20POLY1305_OLD:
  683. *out_aead = EVP_aead_chacha20_poly1305_old();
  684. *out_fixed_iv_len = 0;
  685. return 1;
  686. #endif
  687. case SSL_CHACHA20POLY1305:
  688. *out_aead = EVP_aead_chacha20_poly1305();
  689. *out_fixed_iv_len = 12;
  690. return 1;
  691. case SSL_RC4:
  692. switch (cipher->algorithm_mac) {
  693. case SSL_MD5:
  694. if (version == SSL3_VERSION) {
  695. *out_aead = EVP_aead_rc4_md5_ssl3();
  696. } else {
  697. *out_aead = EVP_aead_rc4_md5_tls();
  698. }
  699. *out_mac_secret_len = MD5_DIGEST_LENGTH;
  700. return 1;
  701. case SSL_SHA1:
  702. if (version == SSL3_VERSION) {
  703. *out_aead = EVP_aead_rc4_sha1_ssl3();
  704. } else {
  705. *out_aead = EVP_aead_rc4_sha1_tls();
  706. }
  707. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  708. return 1;
  709. default:
  710. return 0;
  711. }
  712. case SSL_AES128:
  713. switch (cipher->algorithm_mac) {
  714. case SSL_SHA1:
  715. if (version == SSL3_VERSION) {
  716. *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
  717. *out_fixed_iv_len = 16;
  718. } else if (version == TLS1_VERSION) {
  719. *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
  720. *out_fixed_iv_len = 16;
  721. } else {
  722. *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
  723. }
  724. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  725. return 1;
  726. case SSL_SHA256:
  727. *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
  728. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  729. return 1;
  730. default:
  731. return 0;
  732. }
  733. case SSL_AES256:
  734. switch (cipher->algorithm_mac) {
  735. case SSL_SHA1:
  736. if (version == SSL3_VERSION) {
  737. *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
  738. *out_fixed_iv_len = 16;
  739. } else if (version == TLS1_VERSION) {
  740. *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
  741. *out_fixed_iv_len = 16;
  742. } else {
  743. *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
  744. }
  745. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  746. return 1;
  747. case SSL_SHA256:
  748. *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
  749. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  750. return 1;
  751. case SSL_SHA384:
  752. *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
  753. *out_mac_secret_len = SHA384_DIGEST_LENGTH;
  754. return 1;
  755. default:
  756. return 0;
  757. }
  758. case SSL_3DES:
  759. switch (cipher->algorithm_mac) {
  760. case SSL_SHA1:
  761. if (version == SSL3_VERSION) {
  762. *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
  763. *out_fixed_iv_len = 8;
  764. } else if (version == TLS1_VERSION) {
  765. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
  766. *out_fixed_iv_len = 8;
  767. } else {
  768. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
  769. }
  770. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  771. return 1;
  772. default:
  773. return 0;
  774. }
  775. case SSL_eNULL:
  776. switch (cipher->algorithm_mac) {
  777. case SSL_SHA1:
  778. if (version == SSL3_VERSION) {
  779. *out_aead = EVP_aead_null_sha1_ssl3();
  780. } else {
  781. *out_aead = EVP_aead_null_sha1_tls();
  782. }
  783. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  784. return 1;
  785. default:
  786. return 0;
  787. }
  788. default:
  789. return 0;
  790. }
  791. }
  792. const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
  793. switch (algorithm_prf) {
  794. case SSL_HANDSHAKE_MAC_DEFAULT:
  795. return EVP_sha1();
  796. case SSL_HANDSHAKE_MAC_SHA256:
  797. return EVP_sha256();
  798. case SSL_HANDSHAKE_MAC_SHA384:
  799. return EVP_sha384();
  800. default:
  801. return NULL;
  802. }
  803. }
  804. #define ITEM_SEP(a) \
  805. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  806. /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
  807. * |buf_len| bytes at |buf|. */
  808. static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
  809. /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
  810. return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
  811. }
  812. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  813. CIPHER_ORDER **tail) {
  814. if (curr == *tail) {
  815. return;
  816. }
  817. if (curr == *head) {
  818. *head = curr->next;
  819. }
  820. if (curr->prev != NULL) {
  821. curr->prev->next = curr->next;
  822. }
  823. if (curr->next != NULL) {
  824. curr->next->prev = curr->prev;
  825. }
  826. (*tail)->next = curr;
  827. curr->prev = *tail;
  828. curr->next = NULL;
  829. *tail = curr;
  830. }
  831. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  832. CIPHER_ORDER **tail) {
  833. if (curr == *head) {
  834. return;
  835. }
  836. if (curr == *tail) {
  837. *tail = curr->prev;
  838. }
  839. if (curr->next != NULL) {
  840. curr->next->prev = curr->prev;
  841. }
  842. if (curr->prev != NULL) {
  843. curr->prev->next = curr->next;
  844. }
  845. (*head)->prev = curr;
  846. curr->next = *head;
  847. curr->prev = NULL;
  848. *head = curr;
  849. }
  850. static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
  851. CIPHER_ORDER *co_list,
  852. CIPHER_ORDER **head_p,
  853. CIPHER_ORDER **tail_p) {
  854. /* The set of ciphers is static, but some subset may be unsupported by
  855. * |ssl_method|, so the list may be smaller. */
  856. size_t co_list_num = 0;
  857. size_t i;
  858. for (i = 0; i < kCiphersLen; i++) {
  859. const SSL_CIPHER *cipher = &kCiphers[i];
  860. if (ssl_method->supports_cipher(cipher)) {
  861. co_list[co_list_num].cipher = cipher;
  862. co_list[co_list_num].next = NULL;
  863. co_list[co_list_num].prev = NULL;
  864. co_list[co_list_num].active = 0;
  865. co_list[co_list_num].in_group = 0;
  866. co_list_num++;
  867. }
  868. }
  869. /* Prepare linked list from list entries. */
  870. if (co_list_num > 0) {
  871. co_list[0].prev = NULL;
  872. if (co_list_num > 1) {
  873. co_list[0].next = &co_list[1];
  874. for (i = 1; i < co_list_num - 1; i++) {
  875. co_list[i].prev = &co_list[i - 1];
  876. co_list[i].next = &co_list[i + 1];
  877. }
  878. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  879. }
  880. co_list[co_list_num - 1].next = NULL;
  881. *head_p = &co_list[0];
  882. *tail_p = &co_list[co_list_num - 1];
  883. }
  884. }
  885. /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
  886. * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
  887. * head and tail of the list to |*head_p| and |*tail_p|, respectively.
  888. *
  889. * - If |cipher_id| is non-zero, only that cipher is selected.
  890. * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
  891. * of that strength.
  892. * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
  893. * |min_version|. */
  894. static void ssl_cipher_apply_rule(
  895. uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
  896. uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
  897. int strength_bits, int in_group, CIPHER_ORDER **head_p,
  898. CIPHER_ORDER **tail_p) {
  899. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  900. const SSL_CIPHER *cp;
  901. int reverse = 0;
  902. if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
  903. (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
  904. /* The rule matches nothing, so bail early. */
  905. return;
  906. }
  907. if (rule == CIPHER_DEL) {
  908. /* needed to maintain sorting between currently deleted ciphers */
  909. reverse = 1;
  910. }
  911. head = *head_p;
  912. tail = *tail_p;
  913. if (reverse) {
  914. next = tail;
  915. last = head;
  916. } else {
  917. next = head;
  918. last = tail;
  919. }
  920. curr = NULL;
  921. for (;;) {
  922. if (curr == last) {
  923. break;
  924. }
  925. curr = next;
  926. if (curr == NULL) {
  927. break;
  928. }
  929. next = reverse ? curr->prev : curr->next;
  930. cp = curr->cipher;
  931. /* Selection criteria is either a specific cipher, the value of
  932. * |strength_bits|, or the algorithms used. */
  933. if (cipher_id != 0) {
  934. if (cipher_id != cp->id) {
  935. continue;
  936. }
  937. } else if (strength_bits >= 0) {
  938. if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
  939. continue;
  940. }
  941. } else if (!(alg_mkey & cp->algorithm_mkey) ||
  942. !(alg_auth & cp->algorithm_auth) ||
  943. !(alg_enc & cp->algorithm_enc) ||
  944. !(alg_mac & cp->algorithm_mac) ||
  945. (min_version != 0 &&
  946. SSL_CIPHER_get_min_version(cp) != min_version)) {
  947. continue;
  948. }
  949. /* add the cipher if it has not been added yet. */
  950. if (rule == CIPHER_ADD) {
  951. /* reverse == 0 */
  952. if (!curr->active) {
  953. ll_append_tail(&head, curr, &tail);
  954. curr->active = 1;
  955. curr->in_group = in_group;
  956. }
  957. }
  958. /* Move the added cipher to this location */
  959. else if (rule == CIPHER_ORD) {
  960. /* reverse == 0 */
  961. if (curr->active) {
  962. ll_append_tail(&head, curr, &tail);
  963. curr->in_group = 0;
  964. }
  965. } else if (rule == CIPHER_DEL) {
  966. /* reverse == 1 */
  967. if (curr->active) {
  968. /* most recently deleted ciphersuites get best positions
  969. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  970. * works in reverse to maintain the order) */
  971. ll_append_head(&head, curr, &tail);
  972. curr->active = 0;
  973. curr->in_group = 0;
  974. }
  975. } else if (rule == CIPHER_KILL) {
  976. /* reverse == 0 */
  977. if (head == curr) {
  978. head = curr->next;
  979. } else {
  980. curr->prev->next = curr->next;
  981. }
  982. if (tail == curr) {
  983. tail = curr->prev;
  984. }
  985. curr->active = 0;
  986. if (curr->next != NULL) {
  987. curr->next->prev = curr->prev;
  988. }
  989. if (curr->prev != NULL) {
  990. curr->prev->next = curr->next;
  991. }
  992. curr->next = NULL;
  993. curr->prev = NULL;
  994. }
  995. }
  996. *head_p = head;
  997. *tail_p = tail;
  998. }
  999. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  1000. CIPHER_ORDER **tail_p) {
  1001. int max_strength_bits, i, *number_uses;
  1002. CIPHER_ORDER *curr;
  1003. /* This routine sorts the ciphers with descending strength. The sorting must
  1004. * keep the pre-sorted sequence, so we apply the normal sorting routine as
  1005. * '+' movement to the end of the list. */
  1006. max_strength_bits = 0;
  1007. curr = *head_p;
  1008. while (curr != NULL) {
  1009. if (curr->active &&
  1010. SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
  1011. max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
  1012. }
  1013. curr = curr->next;
  1014. }
  1015. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  1016. if (!number_uses) {
  1017. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1018. return 0;
  1019. }
  1020. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  1021. /* Now find the strength_bits values actually used. */
  1022. curr = *head_p;
  1023. while (curr != NULL) {
  1024. if (curr->active) {
  1025. number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
  1026. }
  1027. curr = curr->next;
  1028. }
  1029. /* Go through the list of used strength_bits values in descending order. */
  1030. for (i = max_strength_bits; i >= 0; i--) {
  1031. if (number_uses[i] > 0) {
  1032. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
  1033. }
  1034. }
  1035. OPENSSL_free(number_uses);
  1036. return 1;
  1037. }
  1038. static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
  1039. const char *rule_str,
  1040. CIPHER_ORDER **head_p,
  1041. CIPHER_ORDER **tail_p) {
  1042. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1043. uint16_t min_version;
  1044. const char *l, *buf;
  1045. int multi, skip_rule, rule, retval, ok, in_group = 0, has_group = 0;
  1046. size_t j, buf_len;
  1047. uint32_t cipher_id;
  1048. char ch;
  1049. retval = 1;
  1050. l = rule_str;
  1051. for (;;) {
  1052. ch = *l;
  1053. if (ch == '\0') {
  1054. break; /* done */
  1055. }
  1056. if (in_group) {
  1057. if (ch == ']') {
  1058. if (*tail_p) {
  1059. (*tail_p)->in_group = 0;
  1060. }
  1061. in_group = 0;
  1062. l++;
  1063. continue;
  1064. }
  1065. if (ch == '|') {
  1066. rule = CIPHER_ADD;
  1067. l++;
  1068. continue;
  1069. } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
  1070. !(ch >= '0' && ch <= '9')) {
  1071. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  1072. retval = in_group = 0;
  1073. break;
  1074. } else {
  1075. rule = CIPHER_ADD;
  1076. }
  1077. } else if (ch == '-') {
  1078. rule = CIPHER_DEL;
  1079. l++;
  1080. } else if (ch == '+') {
  1081. rule = CIPHER_ORD;
  1082. l++;
  1083. } else if (ch == '!') {
  1084. rule = CIPHER_KILL;
  1085. l++;
  1086. } else if (ch == '@') {
  1087. rule = CIPHER_SPECIAL;
  1088. l++;
  1089. } else if (ch == '[') {
  1090. if (in_group) {
  1091. OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
  1092. retval = in_group = 0;
  1093. break;
  1094. }
  1095. in_group = 1;
  1096. has_group = 1;
  1097. l++;
  1098. continue;
  1099. } else {
  1100. rule = CIPHER_ADD;
  1101. }
  1102. /* If preference groups are enabled, the only legal operator is +.
  1103. * Otherwise the in_group bits will get mixed up. */
  1104. if (has_group && rule != CIPHER_ADD) {
  1105. OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  1106. retval = in_group = 0;
  1107. break;
  1108. }
  1109. if (ITEM_SEP(ch)) {
  1110. l++;
  1111. continue;
  1112. }
  1113. multi = 0;
  1114. cipher_id = 0;
  1115. alg_mkey = ~0u;
  1116. alg_auth = ~0u;
  1117. alg_enc = ~0u;
  1118. alg_mac = ~0u;
  1119. min_version = 0;
  1120. skip_rule = 0;
  1121. for (;;) {
  1122. ch = *l;
  1123. buf = l;
  1124. buf_len = 0;
  1125. while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
  1126. ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
  1127. ch = *(++l);
  1128. buf_len++;
  1129. }
  1130. if (buf_len == 0) {
  1131. /* We hit something we cannot deal with, it is no command or separator
  1132. * nor alphanumeric, so we call this an error. */
  1133. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1134. retval = in_group = 0;
  1135. l++;
  1136. break;
  1137. }
  1138. if (rule == CIPHER_SPECIAL) {
  1139. break;
  1140. }
  1141. /* Look for a matching exact cipher. These aren't allowed in multipart
  1142. * rules. */
  1143. if (!multi && ch != '+') {
  1144. for (j = 0; j < kCiphersLen; j++) {
  1145. const SSL_CIPHER *cipher = &kCiphers[j];
  1146. if (rule_equals(cipher->name, buf, buf_len)) {
  1147. cipher_id = cipher->id;
  1148. break;
  1149. }
  1150. }
  1151. }
  1152. if (cipher_id == 0) {
  1153. /* If not an exact cipher, look for a matching cipher alias. */
  1154. for (j = 0; j < kCipherAliasesLen; j++) {
  1155. if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
  1156. alg_mkey &= kCipherAliases[j].algorithm_mkey;
  1157. alg_auth &= kCipherAliases[j].algorithm_auth;
  1158. alg_enc &= kCipherAliases[j].algorithm_enc;
  1159. alg_mac &= kCipherAliases[j].algorithm_mac;
  1160. if (min_version != 0 &&
  1161. min_version != kCipherAliases[j].min_version) {
  1162. skip_rule = 1;
  1163. } else {
  1164. min_version = kCipherAliases[j].min_version;
  1165. }
  1166. break;
  1167. }
  1168. }
  1169. if (j == kCipherAliasesLen) {
  1170. skip_rule = 1;
  1171. }
  1172. }
  1173. /* Check for a multipart rule. */
  1174. if (ch != '+') {
  1175. break;
  1176. }
  1177. l++;
  1178. multi = 1;
  1179. }
  1180. /* If one of the CHACHA20_POLY1305 variants is selected, include the other
  1181. * as well. They have the same name to avoid requiring changes in
  1182. * configuration. Apply this transformation late so that the cipher name
  1183. * still behaves as an exact name and not an alias in multipart rules.
  1184. *
  1185. * This is temporary and will be removed when the pre-standard construction
  1186. * is removed. */
  1187. if (cipher_id == TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD ||
  1188. cipher_id == TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) {
  1189. cipher_id = 0;
  1190. alg_mkey = SSL_kECDHE;
  1191. alg_auth = SSL_aRSA;
  1192. alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
  1193. alg_mac = SSL_AEAD;
  1194. } else if (cipher_id == TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD ||
  1195. cipher_id == TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) {
  1196. cipher_id = 0;
  1197. alg_mkey = SSL_kECDHE;
  1198. alg_auth = SSL_aECDSA;
  1199. alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
  1200. alg_mac = SSL_AEAD;
  1201. }
  1202. /* Ok, we have the rule, now apply it. */
  1203. if (rule == CIPHER_SPECIAL) {
  1204. /* special command */
  1205. ok = 0;
  1206. if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
  1207. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1208. } else {
  1209. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1210. }
  1211. if (ok == 0) {
  1212. retval = 0;
  1213. }
  1214. /* We do not support any "multi" options together with "@", so throw away
  1215. * the rest of the command, if any left, until end or ':' is found. */
  1216. while (*l != '\0' && !ITEM_SEP(*l)) {
  1217. l++;
  1218. }
  1219. } else if (!skip_rule) {
  1220. ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
  1221. min_version, rule, -1, in_group, head_p, tail_p);
  1222. }
  1223. }
  1224. if (in_group) {
  1225. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1226. retval = 0;
  1227. }
  1228. return retval;
  1229. }
  1230. STACK_OF(SSL_CIPHER) *
  1231. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
  1232. struct ssl_cipher_preference_list_st **out_cipher_list,
  1233. STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
  1234. const char *rule_str) {
  1235. int ok;
  1236. STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
  1237. const char *rule_p;
  1238. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1239. uint8_t *in_group_flags = NULL;
  1240. unsigned int num_in_group_flags = 0;
  1241. struct ssl_cipher_preference_list_st *pref_list = NULL;
  1242. /* Return with error if nothing to do. */
  1243. if (rule_str == NULL || out_cipher_list == NULL) {
  1244. return NULL;
  1245. }
  1246. /* Now we have to collect the available ciphers from the compiled in ciphers.
  1247. * We cannot get more than the number compiled in, so it is used for
  1248. * allocation. */
  1249. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
  1250. if (co_list == NULL) {
  1251. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1252. return NULL;
  1253. }
  1254. ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
  1255. /* Now arrange all ciphers by preference:
  1256. * TODO(davidben): Compute this order once and copy it. */
  1257. /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
  1258. * key exchange mechanisms */
  1259. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
  1260. 0, &head, &tail);
  1261. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
  1262. &head, &tail);
  1263. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0,
  1264. &head, &tail);
  1265. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  1266. * CHACHA20 unless there is hardware support for fast and constant-time
  1267. * AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
  1268. * old one. */
  1269. if (EVP_has_aes_hardware()) {
  1270. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1271. &head, &tail);
  1272. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1273. &head, &tail);
  1274. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1275. -1, 0, &head, &tail);
  1276. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
  1277. CIPHER_ADD, -1, 0, &head, &tail);
  1278. } else {
  1279. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1280. -1, 0, &head, &tail);
  1281. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
  1282. CIPHER_ADD, -1, 0, &head, &tail);
  1283. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1284. &head, &tail);
  1285. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
  1286. &head, &tail);
  1287. }
  1288. /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
  1289. * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
  1290. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
  1291. &head, &tail);
  1292. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
  1293. &head, &tail);
  1294. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, 0, CIPHER_ADD, -1, 0,
  1295. &head, &tail);
  1296. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, 0, CIPHER_ADD, -1, 0,
  1297. &head, &tail);
  1298. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
  1299. &tail);
  1300. /* Temporarily enable everything else for sorting */
  1301. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
  1302. &tail);
  1303. /* Move ciphers without forward secrecy to the end. */
  1304. ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, 0,
  1305. CIPHER_ORD, -1, 0, &head, &tail);
  1306. /* Now disable everything (maintaining the ordering!) */
  1307. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
  1308. &tail);
  1309. /* If the rule_string begins with DEFAULT, apply the default rule before
  1310. * using the (possibly available) additional rules. */
  1311. ok = 1;
  1312. rule_p = rule_str;
  1313. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1314. ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
  1315. &tail);
  1316. rule_p += 7;
  1317. if (*rule_p == ':') {
  1318. rule_p++;
  1319. }
  1320. }
  1321. if (ok && strlen(rule_p) > 0) {
  1322. ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
  1323. }
  1324. if (!ok) {
  1325. goto err;
  1326. }
  1327. /* Allocate new "cipherstack" for the result, return with error
  1328. * if we cannot get one. */
  1329. cipherstack = sk_SSL_CIPHER_new_null();
  1330. if (cipherstack == NULL) {
  1331. goto err;
  1332. }
  1333. in_group_flags = OPENSSL_malloc(kCiphersLen);
  1334. if (!in_group_flags) {
  1335. goto err;
  1336. }
  1337. /* The cipher selection for the list is done. The ciphers are added
  1338. * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
  1339. for (curr = head; curr != NULL; curr = curr->next) {
  1340. if (curr->active) {
  1341. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1342. goto err;
  1343. }
  1344. in_group_flags[num_in_group_flags++] = curr->in_group;
  1345. }
  1346. }
  1347. OPENSSL_free(co_list); /* Not needed any longer */
  1348. co_list = NULL;
  1349. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1350. if (tmp_cipher_list == NULL) {
  1351. goto err;
  1352. }
  1353. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1354. if (!pref_list) {
  1355. goto err;
  1356. }
  1357. pref_list->ciphers = cipherstack;
  1358. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1359. if (!pref_list->in_group_flags) {
  1360. goto err;
  1361. }
  1362. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1363. OPENSSL_free(in_group_flags);
  1364. in_group_flags = NULL;
  1365. if (*out_cipher_list != NULL) {
  1366. ssl_cipher_preference_list_free(*out_cipher_list);
  1367. }
  1368. *out_cipher_list = pref_list;
  1369. pref_list = NULL;
  1370. if (out_cipher_list_by_id != NULL) {
  1371. sk_SSL_CIPHER_free(*out_cipher_list_by_id);
  1372. *out_cipher_list_by_id = tmp_cipher_list;
  1373. tmp_cipher_list = NULL;
  1374. (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
  1375. ssl_cipher_ptr_id_cmp);
  1376. sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
  1377. } else {
  1378. sk_SSL_CIPHER_free(tmp_cipher_list);
  1379. tmp_cipher_list = NULL;
  1380. }
  1381. return cipherstack;
  1382. err:
  1383. OPENSSL_free(co_list);
  1384. OPENSSL_free(in_group_flags);
  1385. sk_SSL_CIPHER_free(cipherstack);
  1386. sk_SSL_CIPHER_free(tmp_cipher_list);
  1387. if (pref_list) {
  1388. OPENSSL_free(pref_list->in_group_flags);
  1389. }
  1390. OPENSSL_free(pref_list);
  1391. return NULL;
  1392. }
  1393. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
  1394. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
  1395. uint32_t id = cipher->id;
  1396. /* All ciphers are SSLv3. */
  1397. assert((id & 0xff000000) == 0x03000000);
  1398. return id & 0xffff;
  1399. }
  1400. int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
  1401. return (cipher->algorithm_enc & SSL_AES) != 0;
  1402. }
  1403. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
  1404. return (cipher->algorithm_mac & SSL_MD5) != 0;
  1405. }
  1406. int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
  1407. return (cipher->algorithm_mac & SSL_SHA1) != 0;
  1408. }
  1409. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
  1410. return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
  1411. }
  1412. int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
  1413. return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
  1414. }
  1415. int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
  1416. return (cipher->algorithm_enc & SSL_AES128) != 0;
  1417. }
  1418. int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
  1419. return (cipher->algorithm_enc & SSL_AES256) != 0;
  1420. }
  1421. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
  1422. return (cipher->algorithm_enc &
  1423. (SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD)) != 0;
  1424. }
  1425. int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
  1426. return (cipher->algorithm_enc & SSL_eNULL) != 0;
  1427. }
  1428. int SSL_CIPHER_is_RC4(const SSL_CIPHER *cipher) {
  1429. return (cipher->algorithm_enc & SSL_RC4) != 0;
  1430. }
  1431. int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
  1432. /* Neither stream cipher nor AEAD. */
  1433. return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 &&
  1434. cipher->algorithm_mac != SSL_AEAD;
  1435. }
  1436. int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
  1437. return (cipher->algorithm_auth & SSL_aECDSA) != 0;
  1438. }
  1439. int SSL_CIPHER_is_ECDHE(const SSL_CIPHER *cipher) {
  1440. return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
  1441. }
  1442. uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
  1443. if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
  1444. /* Cipher suites before TLS 1.2 use the default PRF, while all those added
  1445. * afterwards specify a particular hash. */
  1446. return TLS1_2_VERSION;
  1447. }
  1448. return SSL3_VERSION;
  1449. }
  1450. /* return the actual cipher being used */
  1451. const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
  1452. if (cipher != NULL) {
  1453. return cipher->name;
  1454. }
  1455. return "(NONE)";
  1456. }
  1457. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1458. if (cipher == NULL) {
  1459. return "";
  1460. }
  1461. switch (cipher->algorithm_mkey) {
  1462. case SSL_kRSA:
  1463. return "RSA";
  1464. case SSL_kDHE:
  1465. switch (cipher->algorithm_auth) {
  1466. case SSL_aRSA:
  1467. return "DHE_RSA";
  1468. default:
  1469. assert(0);
  1470. return "UNKNOWN";
  1471. }
  1472. case SSL_kECDHE:
  1473. switch (cipher->algorithm_auth) {
  1474. case SSL_aECDSA:
  1475. return "ECDHE_ECDSA";
  1476. case SSL_aRSA:
  1477. return "ECDHE_RSA";
  1478. case SSL_aPSK:
  1479. return "ECDHE_PSK";
  1480. default:
  1481. assert(0);
  1482. return "UNKNOWN";
  1483. }
  1484. case SSL_kPSK:
  1485. assert(cipher->algorithm_auth == SSL_aPSK);
  1486. return "PSK";
  1487. default:
  1488. assert(0);
  1489. return "UNKNOWN";
  1490. }
  1491. }
  1492. static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
  1493. switch (cipher->algorithm_enc) {
  1494. case SSL_3DES:
  1495. return "3DES_EDE_CBC";
  1496. case SSL_RC4:
  1497. return "RC4";
  1498. case SSL_AES128:
  1499. return "AES_128_CBC";
  1500. case SSL_AES256:
  1501. return "AES_256_CBC";
  1502. case SSL_AES128GCM:
  1503. return "AES_128_GCM";
  1504. case SSL_AES256GCM:
  1505. return "AES_256_GCM";
  1506. case SSL_CHACHA20POLY1305:
  1507. case SSL_CHACHA20POLY1305_OLD:
  1508. return "CHACHA20_POLY1305";
  1509. break;
  1510. default:
  1511. assert(0);
  1512. return "UNKNOWN";
  1513. }
  1514. }
  1515. static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
  1516. switch (cipher->algorithm_prf) {
  1517. case SSL_HANDSHAKE_MAC_DEFAULT:
  1518. /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
  1519. * only ever MD5 or SHA-1. */
  1520. switch (cipher->algorithm_mac) {
  1521. case SSL_MD5:
  1522. return "MD5";
  1523. case SSL_SHA1:
  1524. return "SHA";
  1525. }
  1526. break;
  1527. case SSL_HANDSHAKE_MAC_SHA256:
  1528. return "SHA256";
  1529. case SSL_HANDSHAKE_MAC_SHA384:
  1530. return "SHA384";
  1531. }
  1532. assert(0);
  1533. return "UNKNOWN";
  1534. }
  1535. char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
  1536. if (cipher == NULL) {
  1537. return NULL;
  1538. }
  1539. const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
  1540. const char *enc_name = ssl_cipher_get_enc_name(cipher);
  1541. const char *prf_name = ssl_cipher_get_prf_name(cipher);
  1542. /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
  1543. size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
  1544. strlen(prf_name) + 1;
  1545. char *ret = OPENSSL_malloc(len);
  1546. if (ret == NULL) {
  1547. return NULL;
  1548. }
  1549. if (BUF_strlcpy(ret, "TLS_", len) >= len ||
  1550. BUF_strlcat(ret, kx_name, len) >= len ||
  1551. BUF_strlcat(ret, "_WITH_", len) >= len ||
  1552. BUF_strlcat(ret, enc_name, len) >= len ||
  1553. BUF_strlcat(ret, "_", len) >= len ||
  1554. BUF_strlcat(ret, prf_name, len) >= len) {
  1555. assert(0);
  1556. OPENSSL_free(ret);
  1557. return NULL;
  1558. }
  1559. assert(strlen(ret) + 1 == len);
  1560. return ret;
  1561. }
  1562. int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
  1563. if (cipher == NULL) {
  1564. return 0;
  1565. }
  1566. int alg_bits, strength_bits;
  1567. switch (cipher->algorithm_enc) {
  1568. case SSL_AES128:
  1569. case SSL_AES128GCM:
  1570. case SSL_RC4:
  1571. alg_bits = 128;
  1572. strength_bits = 128;
  1573. break;
  1574. case SSL_AES256:
  1575. case SSL_AES256GCM:
  1576. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  1577. case SSL_CHACHA20POLY1305_OLD:
  1578. #endif
  1579. case SSL_CHACHA20POLY1305:
  1580. alg_bits = 256;
  1581. strength_bits = 256;
  1582. break;
  1583. case SSL_3DES:
  1584. alg_bits = 168;
  1585. strength_bits = 112;
  1586. break;
  1587. case SSL_eNULL:
  1588. alg_bits = 0;
  1589. strength_bits = 0;
  1590. break;
  1591. default:
  1592. assert(0);
  1593. alg_bits = 0;
  1594. strength_bits = 0;
  1595. }
  1596. if (out_alg_bits != NULL) {
  1597. *out_alg_bits = alg_bits;
  1598. }
  1599. return strength_bits;
  1600. }
  1601. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
  1602. int len) {
  1603. const char *kx, *au, *enc, *mac;
  1604. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1605. alg_mkey = cipher->algorithm_mkey;
  1606. alg_auth = cipher->algorithm_auth;
  1607. alg_enc = cipher->algorithm_enc;
  1608. alg_mac = cipher->algorithm_mac;
  1609. switch (alg_mkey) {
  1610. case SSL_kRSA:
  1611. kx = "RSA";
  1612. break;
  1613. case SSL_kDHE:
  1614. kx = "DH";
  1615. break;
  1616. case SSL_kECDHE:
  1617. kx = "ECDH";
  1618. break;
  1619. case SSL_kPSK:
  1620. kx = "PSK";
  1621. break;
  1622. default:
  1623. kx = "unknown";
  1624. }
  1625. switch (alg_auth) {
  1626. case SSL_aRSA:
  1627. au = "RSA";
  1628. break;
  1629. case SSL_aECDSA:
  1630. au = "ECDSA";
  1631. break;
  1632. case SSL_aPSK:
  1633. au = "PSK";
  1634. break;
  1635. default:
  1636. au = "unknown";
  1637. break;
  1638. }
  1639. switch (alg_enc) {
  1640. case SSL_3DES:
  1641. enc = "3DES(168)";
  1642. break;
  1643. case SSL_RC4:
  1644. enc = "RC4(128)";
  1645. break;
  1646. case SSL_AES128:
  1647. enc = "AES(128)";
  1648. break;
  1649. case SSL_AES256:
  1650. enc = "AES(256)";
  1651. break;
  1652. case SSL_AES128GCM:
  1653. enc = "AESGCM(128)";
  1654. break;
  1655. case SSL_AES256GCM:
  1656. enc = "AESGCM(256)";
  1657. break;
  1658. case SSL_CHACHA20POLY1305_OLD:
  1659. enc = "ChaCha20-Poly1305-Old";
  1660. break;
  1661. case SSL_CHACHA20POLY1305:
  1662. enc = "ChaCha20-Poly1305";
  1663. break;
  1664. case SSL_eNULL:
  1665. enc="None";
  1666. break;
  1667. default:
  1668. enc = "unknown";
  1669. break;
  1670. }
  1671. switch (alg_mac) {
  1672. case SSL_MD5:
  1673. mac = "MD5";
  1674. break;
  1675. case SSL_SHA1:
  1676. mac = "SHA1";
  1677. break;
  1678. case SSL_SHA256:
  1679. mac = "SHA256";
  1680. break;
  1681. case SSL_SHA384:
  1682. mac = "SHA384";
  1683. break;
  1684. case SSL_AEAD:
  1685. mac = "AEAD";
  1686. break;
  1687. default:
  1688. mac = "unknown";
  1689. break;
  1690. }
  1691. if (buf == NULL) {
  1692. len = 128;
  1693. buf = OPENSSL_malloc(len);
  1694. if (buf == NULL) {
  1695. return NULL;
  1696. }
  1697. } else if (len < 128) {
  1698. return "Buffer too small";
  1699. }
  1700. BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
  1701. cipher->name, kx, au, enc, mac);
  1702. return buf;
  1703. }
  1704. const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
  1705. return "TLSv1/SSLv3";
  1706. }
  1707. COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
  1708. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
  1709. const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
  1710. int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
  1711. uint32_t alg_a = cipher->algorithm_auth;
  1712. if (alg_a & SSL_aECDSA) {
  1713. return EVP_PKEY_EC;
  1714. } else if (alg_a & SSL_aRSA) {
  1715. return EVP_PKEY_RSA;
  1716. }
  1717. return EVP_PKEY_NONE;
  1718. }
  1719. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
  1720. /* PSK-authenticated ciphers do not use a certificate. (RSA_PSK is not
  1721. * supported.) */
  1722. if (cipher->algorithm_auth & SSL_aPSK) {
  1723. return 0;
  1724. }
  1725. /* All other ciphers include it. */
  1726. return 1;
  1727. }
  1728. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
  1729. /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
  1730. if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
  1731. return 1;
  1732. }
  1733. /* It is optional in all others. */
  1734. return 0;
  1735. }
  1736. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
  1737. size_t block_size;
  1738. switch (cipher->algorithm_enc) {
  1739. case SSL_3DES:
  1740. block_size = 8;
  1741. break;
  1742. case SSL_AES128:
  1743. case SSL_AES256:
  1744. block_size = 16;
  1745. break;
  1746. default:
  1747. return 0;
  1748. }
  1749. size_t mac_len;
  1750. switch (cipher->algorithm_mac) {
  1751. case SSL_MD5:
  1752. mac_len = MD5_DIGEST_LENGTH;
  1753. break;
  1754. case SSL_SHA1:
  1755. mac_len = SHA_DIGEST_LENGTH;
  1756. break;
  1757. default:
  1758. return 0;
  1759. }
  1760. size_t ret = 1 + mac_len;
  1761. ret += block_size - (ret % block_size);
  1762. return ret;
  1763. }