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

1916 行
52 KiB

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