選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

2011 行
54 KiB

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