您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

1781 行
49 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. namespace bssl {
  152. // kCiphers is an array of all supported ciphers, sorted by id.
  153. static const SSL_CIPHER kCiphers[] = {
  154. // The RSA ciphers
  155. // Cipher 02
  156. {
  157. SSL3_TXT_RSA_NULL_SHA,
  158. "TLS_RSA_WITH_NULL_SHA",
  159. SSL3_CK_RSA_NULL_SHA,
  160. SSL_kRSA,
  161. SSL_aRSA,
  162. SSL_eNULL,
  163. SSL_SHA1,
  164. SSL_HANDSHAKE_MAC_DEFAULT,
  165. },
  166. // Cipher 0A
  167. {
  168. SSL3_TXT_RSA_DES_192_CBC3_SHA,
  169. "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
  170. SSL3_CK_RSA_DES_192_CBC3_SHA,
  171. SSL_kRSA,
  172. SSL_aRSA,
  173. SSL_3DES,
  174. SSL_SHA1,
  175. SSL_HANDSHAKE_MAC_DEFAULT,
  176. },
  177. // New AES ciphersuites
  178. // Cipher 2F
  179. {
  180. TLS1_TXT_RSA_WITH_AES_128_SHA,
  181. "TLS_RSA_WITH_AES_128_CBC_SHA",
  182. TLS1_CK_RSA_WITH_AES_128_SHA,
  183. SSL_kRSA,
  184. SSL_aRSA,
  185. SSL_AES128,
  186. SSL_SHA1,
  187. SSL_HANDSHAKE_MAC_DEFAULT,
  188. },
  189. // Cipher 35
  190. {
  191. TLS1_TXT_RSA_WITH_AES_256_SHA,
  192. "TLS_RSA_WITH_AES_256_CBC_SHA",
  193. TLS1_CK_RSA_WITH_AES_256_SHA,
  194. SSL_kRSA,
  195. SSL_aRSA,
  196. SSL_AES256,
  197. SSL_SHA1,
  198. SSL_HANDSHAKE_MAC_DEFAULT,
  199. },
  200. // TLS v1.2 ciphersuites
  201. // Cipher 3C
  202. {
  203. TLS1_TXT_RSA_WITH_AES_128_SHA256,
  204. "TLS_RSA_WITH_AES_128_CBC_SHA256",
  205. TLS1_CK_RSA_WITH_AES_128_SHA256,
  206. SSL_kRSA,
  207. SSL_aRSA,
  208. SSL_AES128,
  209. SSL_SHA256,
  210. SSL_HANDSHAKE_MAC_SHA256,
  211. },
  212. // Cipher 3D
  213. {
  214. TLS1_TXT_RSA_WITH_AES_256_SHA256,
  215. "TLS_RSA_WITH_AES_256_CBC_SHA256",
  216. TLS1_CK_RSA_WITH_AES_256_SHA256,
  217. SSL_kRSA,
  218. SSL_aRSA,
  219. SSL_AES256,
  220. SSL_SHA256,
  221. SSL_HANDSHAKE_MAC_SHA256,
  222. },
  223. // PSK cipher suites.
  224. // Cipher 8C
  225. {
  226. TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
  227. "TLS_PSK_WITH_AES_128_CBC_SHA",
  228. TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
  229. SSL_kPSK,
  230. SSL_aPSK,
  231. SSL_AES128,
  232. SSL_SHA1,
  233. SSL_HANDSHAKE_MAC_DEFAULT,
  234. },
  235. // Cipher 8D
  236. {
  237. TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
  238. "TLS_PSK_WITH_AES_256_CBC_SHA",
  239. TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
  240. SSL_kPSK,
  241. SSL_aPSK,
  242. SSL_AES256,
  243. SSL_SHA1,
  244. SSL_HANDSHAKE_MAC_DEFAULT,
  245. },
  246. // GCM ciphersuites from RFC5288
  247. // Cipher 9C
  248. {
  249. TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
  250. "TLS_RSA_WITH_AES_128_GCM_SHA256",
  251. TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  252. SSL_kRSA,
  253. SSL_aRSA,
  254. SSL_AES128GCM,
  255. SSL_AEAD,
  256. SSL_HANDSHAKE_MAC_SHA256,
  257. },
  258. // Cipher 9D
  259. {
  260. TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
  261. "TLS_RSA_WITH_AES_256_GCM_SHA384",
  262. TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  263. SSL_kRSA,
  264. SSL_aRSA,
  265. SSL_AES256GCM,
  266. SSL_AEAD,
  267. SSL_HANDSHAKE_MAC_SHA384,
  268. },
  269. // TLS 1.3 suites.
  270. // Cipher 1301
  271. {
  272. TLS1_TXT_AES_128_GCM_SHA256,
  273. "TLS_AES_128_GCM_SHA256",
  274. TLS1_CK_AES_128_GCM_SHA256,
  275. SSL_kGENERIC,
  276. SSL_aGENERIC,
  277. SSL_AES128GCM,
  278. SSL_AEAD,
  279. SSL_HANDSHAKE_MAC_SHA256,
  280. },
  281. // Cipher 1302
  282. {
  283. TLS1_TXT_AES_256_GCM_SHA384,
  284. "TLS_AES_256_GCM_SHA384",
  285. TLS1_CK_AES_256_GCM_SHA384,
  286. SSL_kGENERIC,
  287. SSL_aGENERIC,
  288. SSL_AES256GCM,
  289. SSL_AEAD,
  290. SSL_HANDSHAKE_MAC_SHA384,
  291. },
  292. // Cipher 1303
  293. {
  294. TLS1_TXT_CHACHA20_POLY1305_SHA256,
  295. "TLS_CHACHA20_POLY1305_SHA256",
  296. TLS1_CK_CHACHA20_POLY1305_SHA256,
  297. SSL_kGENERIC,
  298. SSL_aGENERIC,
  299. SSL_CHACHA20POLY1305,
  300. SSL_AEAD,
  301. SSL_HANDSHAKE_MAC_SHA256,
  302. },
  303. // Cipher C009
  304. {
  305. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  306. "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  307. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  308. SSL_kECDHE,
  309. SSL_aECDSA,
  310. SSL_AES128,
  311. SSL_SHA1,
  312. SSL_HANDSHAKE_MAC_DEFAULT,
  313. },
  314. // Cipher C00A
  315. {
  316. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  317. "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  318. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  319. SSL_kECDHE,
  320. SSL_aECDSA,
  321. SSL_AES256,
  322. SSL_SHA1,
  323. SSL_HANDSHAKE_MAC_DEFAULT,
  324. },
  325. // Cipher C013
  326. {
  327. TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  328. "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  329. TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  330. SSL_kECDHE,
  331. SSL_aRSA,
  332. SSL_AES128,
  333. SSL_SHA1,
  334. SSL_HANDSHAKE_MAC_DEFAULT,
  335. },
  336. // Cipher C014
  337. {
  338. TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  339. "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  340. TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  341. SSL_kECDHE,
  342. SSL_aRSA,
  343. SSL_AES256,
  344. SSL_SHA1,
  345. SSL_HANDSHAKE_MAC_DEFAULT,
  346. },
  347. // HMAC based TLS v1.2 ciphersuites from RFC5289
  348. // Cipher C023
  349. {
  350. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
  351. "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
  352. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
  353. SSL_kECDHE,
  354. SSL_aECDSA,
  355. SSL_AES128,
  356. SSL_SHA256,
  357. SSL_HANDSHAKE_MAC_SHA256,
  358. },
  359. // Cipher C024
  360. {
  361. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
  362. "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
  363. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
  364. SSL_kECDHE,
  365. SSL_aECDSA,
  366. SSL_AES256,
  367. SSL_SHA384,
  368. SSL_HANDSHAKE_MAC_SHA384,
  369. },
  370. // Cipher C027
  371. {
  372. TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
  373. "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
  374. TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
  375. SSL_kECDHE,
  376. SSL_aRSA,
  377. SSL_AES128,
  378. SSL_SHA256,
  379. SSL_HANDSHAKE_MAC_SHA256,
  380. },
  381. // Cipher C028
  382. {
  383. TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
  384. "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
  385. TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
  386. SSL_kECDHE,
  387. SSL_aRSA,
  388. SSL_AES256,
  389. SSL_SHA384,
  390. SSL_HANDSHAKE_MAC_SHA384,
  391. },
  392. // GCM based TLS v1.2 ciphersuites from RFC5289
  393. // Cipher C02B
  394. {
  395. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  396. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  397. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  398. SSL_kECDHE,
  399. SSL_aECDSA,
  400. SSL_AES128GCM,
  401. SSL_AEAD,
  402. SSL_HANDSHAKE_MAC_SHA256,
  403. },
  404. // Cipher C02C
  405. {
  406. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  407. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  408. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  409. SSL_kECDHE,
  410. SSL_aECDSA,
  411. SSL_AES256GCM,
  412. SSL_AEAD,
  413. SSL_HANDSHAKE_MAC_SHA384,
  414. },
  415. // Cipher C02F
  416. {
  417. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  418. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  419. TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  420. SSL_kECDHE,
  421. SSL_aRSA,
  422. SSL_AES128GCM,
  423. SSL_AEAD,
  424. SSL_HANDSHAKE_MAC_SHA256,
  425. },
  426. // Cipher C030
  427. {
  428. TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  429. "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  430. TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  431. SSL_kECDHE,
  432. SSL_aRSA,
  433. SSL_AES256GCM,
  434. SSL_AEAD,
  435. SSL_HANDSHAKE_MAC_SHA384,
  436. },
  437. // ECDHE-PSK cipher suites.
  438. // Cipher C035
  439. {
  440. TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  441. "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
  442. TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  443. SSL_kECDHE,
  444. SSL_aPSK,
  445. SSL_AES128,
  446. SSL_SHA1,
  447. SSL_HANDSHAKE_MAC_DEFAULT,
  448. },
  449. // Cipher C036
  450. {
  451. TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  452. "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
  453. TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  454. SSL_kECDHE,
  455. SSL_aPSK,
  456. SSL_AES256,
  457. SSL_SHA1,
  458. SSL_HANDSHAKE_MAC_DEFAULT,
  459. },
  460. // ChaCha20-Poly1305 cipher suites.
  461. // Cipher CCA8
  462. {
  463. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  464. "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  465. TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  466. SSL_kECDHE,
  467. SSL_aRSA,
  468. SSL_CHACHA20POLY1305,
  469. SSL_AEAD,
  470. SSL_HANDSHAKE_MAC_SHA256,
  471. },
  472. // Cipher CCA9
  473. {
  474. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  475. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  476. TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
  477. SSL_kECDHE,
  478. SSL_aECDSA,
  479. SSL_CHACHA20POLY1305,
  480. SSL_AEAD,
  481. SSL_HANDSHAKE_MAC_SHA256,
  482. },
  483. // Cipher CCAB
  484. {
  485. TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  486. "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
  487. TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
  488. SSL_kECDHE,
  489. SSL_aPSK,
  490. SSL_CHACHA20POLY1305,
  491. SSL_AEAD,
  492. SSL_HANDSHAKE_MAC_SHA256,
  493. },
  494. };
  495. static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
  496. #define CIPHER_ADD 1
  497. #define CIPHER_KILL 2
  498. #define CIPHER_DEL 3
  499. #define CIPHER_ORD 4
  500. #define CIPHER_SPECIAL 5
  501. typedef struct cipher_order_st {
  502. const SSL_CIPHER *cipher;
  503. bool active;
  504. bool in_group;
  505. struct cipher_order_st *next, *prev;
  506. } CIPHER_ORDER;
  507. typedef struct cipher_alias_st {
  508. // name is the name of the cipher alias.
  509. const char *name;
  510. // The following fields are bitmasks for the corresponding fields on
  511. // |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
  512. // bit corresponding to the cipher's value is set to 1. If any bitmask is
  513. // all zeroes, the alias matches nothing. Use |~0u| for the default value.
  514. uint32_t algorithm_mkey;
  515. uint32_t algorithm_auth;
  516. uint32_t algorithm_enc;
  517. uint32_t algorithm_mac;
  518. // min_version, if non-zero, matches all ciphers which were added in that
  519. // particular protocol version.
  520. uint16_t min_version;
  521. } CIPHER_ALIAS;
  522. static const CIPHER_ALIAS kCipherAliases[] = {
  523. // "ALL" doesn't include eNULL. It must be explicitly enabled.
  524. {"ALL", ~0u, ~0u, ~0u, ~0u, 0},
  525. // The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing.
  526. // key exchange aliases
  527. // (some of those using only a single bit here combine
  528. // multiple key exchange algs according to the RFCs.
  529. {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
  530. {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  531. {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  532. {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  533. {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
  534. // server authentication aliases
  535. {"aRSA", ~0u, SSL_aRSA, ~0u, ~0u, 0},
  536. {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  537. {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
  538. {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
  539. // aliases combining key exchange and server authentication
  540. {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  541. {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
  542. {"RSA", SSL_kRSA, SSL_aRSA, ~0u, ~0u, 0},
  543. {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
  544. // symmetric encryption aliases
  545. {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
  546. {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
  547. {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
  548. {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
  549. {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
  550. {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0},
  551. // MAC aliases
  552. {"SHA1", ~0u, ~0u, ~0u, SSL_SHA1, 0},
  553. {"SHA", ~0u, ~0u, ~0u, SSL_SHA1, 0},
  554. {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
  555. {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
  556. // Legacy protocol minimum version aliases. "TLSv1" is intentionally the
  557. // same as "SSLv3".
  558. {"SSLv3", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
  559. {"TLSv1", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
  560. {"TLSv1.2", ~0u, ~0u, ~0u, ~0u, TLS1_2_VERSION},
  561. // Legacy strength classes.
  562. {"HIGH", ~0u, ~0u, ~0u, ~0u, 0},
  563. {"FIPS", ~0u, ~0u, ~0u, ~0u, 0},
  564. };
  565. static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
  566. static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  567. const SSL_CIPHER *a = reinterpret_cast<const SSL_CIPHER *>(in_a);
  568. const SSL_CIPHER *b = reinterpret_cast<const SSL_CIPHER *>(in_b);
  569. if (a->id > b->id) {
  570. return 1;
  571. } else if (a->id < b->id) {
  572. return -1;
  573. } else {
  574. return 0;
  575. }
  576. }
  577. bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  578. size_t *out_mac_secret_len,
  579. size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
  580. uint16_t version, int is_dtls) {
  581. *out_aead = NULL;
  582. *out_mac_secret_len = 0;
  583. *out_fixed_iv_len = 0;
  584. const int is_tls12 = version == TLS1_2_VERSION && !is_dtls;
  585. if (cipher->algorithm_mac == SSL_AEAD) {
  586. if (cipher->algorithm_enc == SSL_AES128GCM) {
  587. *out_aead =
  588. is_tls12 ? EVP_aead_aes_128_gcm_tls12() : EVP_aead_aes_128_gcm();
  589. *out_fixed_iv_len = 4;
  590. } else if (cipher->algorithm_enc == SSL_AES256GCM) {
  591. *out_aead =
  592. is_tls12 ? EVP_aead_aes_256_gcm_tls12() : EVP_aead_aes_256_gcm();
  593. *out_fixed_iv_len = 4;
  594. } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
  595. *out_aead = EVP_aead_chacha20_poly1305();
  596. *out_fixed_iv_len = 12;
  597. } else {
  598. return false;
  599. }
  600. // In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
  601. // above computes the TLS 1.2 construction.
  602. if (version >= TLS1_3_VERSION) {
  603. *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
  604. }
  605. } else if (cipher->algorithm_mac == SSL_SHA1) {
  606. if (cipher->algorithm_enc == SSL_eNULL) {
  607. if (version == SSL3_VERSION) {
  608. *out_aead = EVP_aead_null_sha1_ssl3();
  609. } else {
  610. *out_aead = EVP_aead_null_sha1_tls();
  611. }
  612. } else if (cipher->algorithm_enc == SSL_3DES) {
  613. if (version == SSL3_VERSION) {
  614. *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
  615. *out_fixed_iv_len = 8;
  616. } else if (version == TLS1_VERSION) {
  617. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
  618. *out_fixed_iv_len = 8;
  619. } else {
  620. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
  621. }
  622. } else if (cipher->algorithm_enc == SSL_AES128) {
  623. if (version == SSL3_VERSION) {
  624. *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
  625. *out_fixed_iv_len = 16;
  626. } else if (version == TLS1_VERSION) {
  627. *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
  628. *out_fixed_iv_len = 16;
  629. } else {
  630. *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
  631. }
  632. } else if (cipher->algorithm_enc == SSL_AES256) {
  633. if (version == SSL3_VERSION) {
  634. *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
  635. *out_fixed_iv_len = 16;
  636. } else if (version == TLS1_VERSION) {
  637. *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
  638. *out_fixed_iv_len = 16;
  639. } else {
  640. *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
  641. }
  642. } else {
  643. return false;
  644. }
  645. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  646. } else if (cipher->algorithm_mac == SSL_SHA256) {
  647. if (cipher->algorithm_enc == SSL_AES128) {
  648. *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
  649. } else if (cipher->algorithm_enc == SSL_AES256) {
  650. *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
  651. } else {
  652. return false;
  653. }
  654. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  655. } else if (cipher->algorithm_mac == SSL_SHA384) {
  656. if (cipher->algorithm_enc != SSL_AES256) {
  657. return false;
  658. }
  659. *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
  660. *out_mac_secret_len = SHA384_DIGEST_LENGTH;
  661. } else {
  662. return false;
  663. }
  664. return true;
  665. }
  666. const EVP_MD *ssl_get_handshake_digest(uint16_t version,
  667. const SSL_CIPHER *cipher) {
  668. switch (cipher->algorithm_prf) {
  669. case SSL_HANDSHAKE_MAC_DEFAULT:
  670. return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
  671. case SSL_HANDSHAKE_MAC_SHA256:
  672. return EVP_sha256();
  673. case SSL_HANDSHAKE_MAC_SHA384:
  674. return EVP_sha384();
  675. default:
  676. assert(0);
  677. return NULL;
  678. }
  679. }
  680. static bool is_cipher_list_separator(char c, int is_strict) {
  681. if (c == ':') {
  682. return true;
  683. }
  684. return !is_strict && (c == ' ' || c == ';' || c == ',');
  685. }
  686. // rule_equals returns whether the NUL-terminated string |rule| is equal to the
  687. // |buf_len| bytes at |buf|.
  688. static bool rule_equals(const char *rule, const char *buf, size_t buf_len) {
  689. // |strncmp| alone only checks that |buf| is a prefix of |rule|.
  690. return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
  691. }
  692. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  693. CIPHER_ORDER **tail) {
  694. if (curr == *tail) {
  695. return;
  696. }
  697. if (curr == *head) {
  698. *head = curr->next;
  699. }
  700. if (curr->prev != NULL) {
  701. curr->prev->next = curr->next;
  702. }
  703. if (curr->next != NULL) {
  704. curr->next->prev = curr->prev;
  705. }
  706. (*tail)->next = curr;
  707. curr->prev = *tail;
  708. curr->next = NULL;
  709. *tail = curr;
  710. }
  711. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  712. CIPHER_ORDER **tail) {
  713. if (curr == *head) {
  714. return;
  715. }
  716. if (curr == *tail) {
  717. *tail = curr->prev;
  718. }
  719. if (curr->next != NULL) {
  720. curr->next->prev = curr->prev;
  721. }
  722. if (curr->prev != NULL) {
  723. curr->prev->next = curr->next;
  724. }
  725. (*head)->prev = curr;
  726. curr->next = *head;
  727. curr->prev = NULL;
  728. *head = curr;
  729. }
  730. static bool ssl_cipher_collect_ciphers(Array<CIPHER_ORDER> *out_co_list,
  731. CIPHER_ORDER **out_head,
  732. CIPHER_ORDER **out_tail) {
  733. Array<CIPHER_ORDER> co_list;
  734. if (!co_list.Init(kCiphersLen)) {
  735. return false;
  736. }
  737. size_t co_list_num = 0;
  738. for (const SSL_CIPHER &cipher : kCiphers) {
  739. // TLS 1.3 ciphers do not participate in this mechanism.
  740. if (cipher.algorithm_mkey != SSL_kGENERIC) {
  741. co_list[co_list_num].cipher = &cipher;
  742. co_list[co_list_num].next = NULL;
  743. co_list[co_list_num].prev = NULL;
  744. co_list[co_list_num].active = false;
  745. co_list[co_list_num].in_group = false;
  746. co_list_num++;
  747. }
  748. }
  749. // Prepare linked list from list entries.
  750. if (co_list_num > 0) {
  751. co_list[0].prev = NULL;
  752. if (co_list_num > 1) {
  753. co_list[0].next = &co_list[1];
  754. for (size_t i = 1; i < co_list_num - 1; i++) {
  755. co_list[i].prev = &co_list[i - 1];
  756. co_list[i].next = &co_list[i + 1];
  757. }
  758. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  759. }
  760. co_list[co_list_num - 1].next = NULL;
  761. *out_head = &co_list[0];
  762. *out_tail = &co_list[co_list_num - 1];
  763. } else {
  764. *out_head = nullptr;
  765. *out_tail = nullptr;
  766. }
  767. *out_co_list = std::move(co_list);
  768. return true;
  769. }
  770. SSLCipherPreferenceList::~SSLCipherPreferenceList() {
  771. OPENSSL_free(in_group_flags);
  772. }
  773. bool SSLCipherPreferenceList::Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers_arg,
  774. Span<const bool> in_group_flags_arg) {
  775. if (sk_SSL_CIPHER_num(ciphers_arg.get()) != in_group_flags_arg.size()) {
  776. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  777. return false;
  778. }
  779. Array<bool> copy;
  780. if (!copy.CopyFrom(in_group_flags_arg)) {
  781. return false;
  782. }
  783. ciphers = std::move(ciphers_arg);
  784. size_t unused_len;
  785. copy.Release(&in_group_flags, &unused_len);
  786. return true;
  787. }
  788. // ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
  789. // parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
  790. // head and tail of the list to |*head_p| and |*tail_p|, respectively.
  791. //
  792. // - If |cipher_id| is non-zero, only that cipher is selected.
  793. // - Otherwise, if |strength_bits| is non-negative, it selects ciphers
  794. // of that strength.
  795. // - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
  796. // |min_version|.
  797. static void ssl_cipher_apply_rule(
  798. uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
  799. uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
  800. int strength_bits, bool in_group, CIPHER_ORDER **head_p,
  801. CIPHER_ORDER **tail_p) {
  802. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  803. const SSL_CIPHER *cp;
  804. bool reverse = false;
  805. if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
  806. (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
  807. // The rule matches nothing, so bail early.
  808. return;
  809. }
  810. if (rule == CIPHER_DEL) {
  811. // needed to maintain sorting between currently deleted ciphers
  812. reverse = true;
  813. }
  814. head = *head_p;
  815. tail = *tail_p;
  816. if (reverse) {
  817. next = tail;
  818. last = head;
  819. } else {
  820. next = head;
  821. last = tail;
  822. }
  823. curr = NULL;
  824. for (;;) {
  825. if (curr == last) {
  826. break;
  827. }
  828. curr = next;
  829. if (curr == NULL) {
  830. break;
  831. }
  832. next = reverse ? curr->prev : curr->next;
  833. cp = curr->cipher;
  834. // Selection criteria is either a specific cipher, the value of
  835. // |strength_bits|, or the algorithms used.
  836. if (cipher_id != 0) {
  837. if (cipher_id != cp->id) {
  838. continue;
  839. }
  840. } else if (strength_bits >= 0) {
  841. if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
  842. continue;
  843. }
  844. } else {
  845. if (!(alg_mkey & cp->algorithm_mkey) ||
  846. !(alg_auth & cp->algorithm_auth) ||
  847. !(alg_enc & cp->algorithm_enc) ||
  848. !(alg_mac & cp->algorithm_mac) ||
  849. (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version) ||
  850. // The NULL cipher must be selected explicitly.
  851. cp->algorithm_enc == SSL_eNULL) {
  852. continue;
  853. }
  854. }
  855. // add the cipher if it has not been added yet.
  856. if (rule == CIPHER_ADD) {
  857. // reverse == false
  858. if (!curr->active) {
  859. ll_append_tail(&head, curr, &tail);
  860. curr->active = true;
  861. curr->in_group = in_group;
  862. }
  863. }
  864. // Move the added cipher to this location
  865. else if (rule == CIPHER_ORD) {
  866. // reverse == false
  867. if (curr->active) {
  868. ll_append_tail(&head, curr, &tail);
  869. curr->in_group = false;
  870. }
  871. } else if (rule == CIPHER_DEL) {
  872. // reverse == true
  873. if (curr->active) {
  874. // most recently deleted ciphersuites get best positions
  875. // for any future CIPHER_ADD (note that the CIPHER_DEL loop
  876. // works in reverse to maintain the order)
  877. ll_append_head(&head, curr, &tail);
  878. curr->active = false;
  879. curr->in_group = false;
  880. }
  881. } else if (rule == CIPHER_KILL) {
  882. // reverse == false
  883. if (head == curr) {
  884. head = curr->next;
  885. } else {
  886. curr->prev->next = curr->next;
  887. }
  888. if (tail == curr) {
  889. tail = curr->prev;
  890. }
  891. curr->active = false;
  892. if (curr->next != NULL) {
  893. curr->next->prev = curr->prev;
  894. }
  895. if (curr->prev != NULL) {
  896. curr->prev->next = curr->next;
  897. }
  898. curr->next = NULL;
  899. curr->prev = NULL;
  900. }
  901. }
  902. *head_p = head;
  903. *tail_p = tail;
  904. }
  905. static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  906. CIPHER_ORDER **tail_p) {
  907. // This routine sorts the ciphers with descending strength. The sorting must
  908. // keep the pre-sorted sequence, so we apply the normal sorting routine as
  909. // '+' movement to the end of the list.
  910. int max_strength_bits = 0;
  911. CIPHER_ORDER *curr = *head_p;
  912. while (curr != NULL) {
  913. if (curr->active &&
  914. SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
  915. max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
  916. }
  917. curr = curr->next;
  918. }
  919. Array<int> number_uses;
  920. if (!number_uses.Init(max_strength_bits + 1)) {
  921. return false;
  922. }
  923. OPENSSL_memset(number_uses.data(), 0, (max_strength_bits + 1) * sizeof(int));
  924. // Now find the strength_bits values actually used.
  925. curr = *head_p;
  926. while (curr != NULL) {
  927. if (curr->active) {
  928. number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
  929. }
  930. curr = curr->next;
  931. }
  932. // Go through the list of used strength_bits values in descending order.
  933. for (int i = max_strength_bits; i >= 0; i--) {
  934. if (number_uses[i] > 0) {
  935. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, false, head_p,
  936. tail_p);
  937. }
  938. }
  939. return true;
  940. }
  941. static bool ssl_cipher_process_rulestr(const char *rule_str,
  942. CIPHER_ORDER **head_p,
  943. CIPHER_ORDER **tail_p, bool strict) {
  944. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  945. uint16_t min_version;
  946. const char *l, *buf;
  947. int rule;
  948. bool multi, skip_rule, in_group = false, has_group = false;
  949. size_t j, buf_len;
  950. uint32_t cipher_id;
  951. char ch;
  952. l = rule_str;
  953. for (;;) {
  954. ch = *l;
  955. if (ch == '\0') {
  956. break; // done
  957. }
  958. if (in_group) {
  959. if (ch == ']') {
  960. if (*tail_p) {
  961. (*tail_p)->in_group = false;
  962. }
  963. in_group = false;
  964. l++;
  965. continue;
  966. }
  967. if (ch == '|') {
  968. rule = CIPHER_ADD;
  969. l++;
  970. continue;
  971. } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
  972. !(ch >= '0' && ch <= '9')) {
  973. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  974. return false;
  975. } else {
  976. rule = CIPHER_ADD;
  977. }
  978. } else if (ch == '-') {
  979. rule = CIPHER_DEL;
  980. l++;
  981. } else if (ch == '+') {
  982. rule = CIPHER_ORD;
  983. l++;
  984. } else if (ch == '!') {
  985. rule = CIPHER_KILL;
  986. l++;
  987. } else if (ch == '@') {
  988. rule = CIPHER_SPECIAL;
  989. l++;
  990. } else if (ch == '[') {
  991. assert(!in_group);
  992. in_group = true;
  993. has_group = true;
  994. l++;
  995. continue;
  996. } else {
  997. rule = CIPHER_ADD;
  998. }
  999. // If preference groups are enabled, the only legal operator is +.
  1000. // Otherwise the in_group bits will get mixed up.
  1001. if (has_group && rule != CIPHER_ADD) {
  1002. OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  1003. return false;
  1004. }
  1005. if (is_cipher_list_separator(ch, strict)) {
  1006. l++;
  1007. continue;
  1008. }
  1009. multi = false;
  1010. cipher_id = 0;
  1011. alg_mkey = ~0u;
  1012. alg_auth = ~0u;
  1013. alg_enc = ~0u;
  1014. alg_mac = ~0u;
  1015. min_version = 0;
  1016. skip_rule = false;
  1017. for (;;) {
  1018. ch = *l;
  1019. buf = l;
  1020. buf_len = 0;
  1021. while ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') ||
  1022. (ch >= 'a' && ch <= 'z') || ch == '-' || ch == '.' || ch == '_') {
  1023. ch = *(++l);
  1024. buf_len++;
  1025. }
  1026. if (buf_len == 0) {
  1027. // We hit something we cannot deal with, it is no command or separator
  1028. // nor alphanumeric, so we call this an error.
  1029. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1030. return false;
  1031. }
  1032. if (rule == CIPHER_SPECIAL) {
  1033. break;
  1034. }
  1035. // Look for a matching exact cipher. These aren't allowed in multipart
  1036. // rules.
  1037. if (!multi && ch != '+') {
  1038. for (j = 0; j < kCiphersLen; j++) {
  1039. const SSL_CIPHER *cipher = &kCiphers[j];
  1040. if (rule_equals(cipher->name, buf, buf_len) ||
  1041. rule_equals(cipher->standard_name, buf, buf_len)) {
  1042. cipher_id = cipher->id;
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. if (cipher_id == 0) {
  1048. // If not an exact cipher, look for a matching cipher alias.
  1049. for (j = 0; j < kCipherAliasesLen; j++) {
  1050. if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
  1051. alg_mkey &= kCipherAliases[j].algorithm_mkey;
  1052. alg_auth &= kCipherAliases[j].algorithm_auth;
  1053. alg_enc &= kCipherAliases[j].algorithm_enc;
  1054. alg_mac &= kCipherAliases[j].algorithm_mac;
  1055. if (min_version != 0 &&
  1056. min_version != kCipherAliases[j].min_version) {
  1057. skip_rule = true;
  1058. } else {
  1059. min_version = kCipherAliases[j].min_version;
  1060. }
  1061. break;
  1062. }
  1063. }
  1064. if (j == kCipherAliasesLen) {
  1065. skip_rule = true;
  1066. if (strict) {
  1067. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1068. return false;
  1069. }
  1070. }
  1071. }
  1072. // Check for a multipart rule.
  1073. if (ch != '+') {
  1074. break;
  1075. }
  1076. l++;
  1077. multi = true;
  1078. }
  1079. // Ok, we have the rule, now apply it.
  1080. if (rule == CIPHER_SPECIAL) {
  1081. if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) {
  1082. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1083. return false;
  1084. }
  1085. if (!ssl_cipher_strength_sort(head_p, tail_p)) {
  1086. return false;
  1087. }
  1088. // We do not support any "multi" options together with "@", so throw away
  1089. // the rest of the command, if any left, until end or ':' is found.
  1090. while (*l != '\0' && !is_cipher_list_separator(*l, strict)) {
  1091. l++;
  1092. }
  1093. } else if (!skip_rule) {
  1094. ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
  1095. min_version, rule, -1, in_group, head_p, tail_p);
  1096. }
  1097. }
  1098. if (in_group) {
  1099. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1100. return false;
  1101. }
  1102. return true;
  1103. }
  1104. bool ssl_create_cipher_list(SSLCipherPreferenceList **out_cipher_list,
  1105. const char *rule_str, bool strict) {
  1106. // Return with error if nothing to do.
  1107. if (rule_str == NULL || out_cipher_list == NULL) {
  1108. return false;
  1109. }
  1110. // Now we have to collect the available ciphers from the compiled in ciphers.
  1111. // We cannot get more than the number compiled in, so it is used for
  1112. // allocation.
  1113. Array<CIPHER_ORDER> co_list;
  1114. CIPHER_ORDER *head = nullptr, *tail = nullptr;
  1115. if (!ssl_cipher_collect_ciphers(&co_list, &head, &tail)) {
  1116. return false;
  1117. }
  1118. // Now arrange all ciphers by preference:
  1119. // TODO(davidben): Compute this order once and copy it.
  1120. // Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
  1121. // key exchange mechanisms
  1122. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
  1123. false, &head, &tail);
  1124. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false,
  1125. &head, &tail);
  1126. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head,
  1127. &tail);
  1128. // Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  1129. // CHACHA20 unless there is hardware support for fast and constant-time
  1130. // AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
  1131. // old one.
  1132. if (EVP_has_aes_hardware()) {
  1133. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1,
  1134. false, &head, &tail);
  1135. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1,
  1136. false, &head, &tail);
  1137. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1138. -1, false, &head, &tail);
  1139. } else {
  1140. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
  1141. -1, false, &head, &tail);
  1142. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1,
  1143. false, &head, &tail);
  1144. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1,
  1145. false, &head, &tail);
  1146. }
  1147. // Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
  1148. // 3DES_EDE_CBC_SHA.
  1149. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, false,
  1150. &head, &tail);
  1151. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, false,
  1152. &head, &tail);
  1153. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, false,
  1154. &head, &tail);
  1155. // Temporarily enable everything else for sorting
  1156. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false, &head,
  1157. &tail);
  1158. // Move ciphers without forward secrecy to the end.
  1159. ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0, CIPHER_ORD,
  1160. -1, false, &head, &tail);
  1161. // Now disable everything (maintaining the ordering!)
  1162. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head,
  1163. &tail);
  1164. // If the rule_string begins with DEFAULT, apply the default rule before
  1165. // using the (possibly available) additional rules.
  1166. const char *rule_p = rule_str;
  1167. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1168. if (!ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
  1169. strict)) {
  1170. return false;
  1171. }
  1172. rule_p += 7;
  1173. if (*rule_p == ':') {
  1174. rule_p++;
  1175. }
  1176. }
  1177. if (*rule_p != '\0' &&
  1178. !ssl_cipher_process_rulestr(rule_p, &head, &tail, strict)) {
  1179. return false;
  1180. }
  1181. // Allocate new "cipherstack" for the result, return with error
  1182. // if we cannot get one.
  1183. UniquePtr<STACK_OF(SSL_CIPHER)> cipherstack(sk_SSL_CIPHER_new_null());
  1184. Array<bool> in_group_flags;
  1185. if (cipherstack == nullptr ||
  1186. !in_group_flags.Init(kCiphersLen)) {
  1187. return false;
  1188. }
  1189. // The cipher selection for the list is done. The ciphers are added
  1190. // to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1191. size_t num_in_group_flags = 0;
  1192. for (CIPHER_ORDER *curr = head; curr != NULL; curr = curr->next) {
  1193. if (curr->active) {
  1194. if (!sk_SSL_CIPHER_push(cipherstack.get(), curr->cipher)) {
  1195. return false;
  1196. }
  1197. in_group_flags[num_in_group_flags++] = curr->in_group;
  1198. }
  1199. }
  1200. UniquePtr<SSLCipherPreferenceList> pref_list =
  1201. MakeUnique<SSLCipherPreferenceList>();
  1202. if (!pref_list ||
  1203. !pref_list->Init(
  1204. std::move(cipherstack),
  1205. MakeConstSpan(in_group_flags).subspan(0, num_in_group_flags))) {
  1206. return false;
  1207. }
  1208. if (*out_cipher_list) {
  1209. Delete(*out_cipher_list);
  1210. }
  1211. *out_cipher_list = pref_list.release();
  1212. // Configuring an empty cipher list is an error but still updates the
  1213. // output.
  1214. if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers.get()) == 0) {
  1215. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1216. return false;
  1217. }
  1218. return true;
  1219. }
  1220. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
  1221. uint32_t id = cipher->id;
  1222. // All ciphers are SSLv3.
  1223. assert((id & 0xff000000) == 0x03000000);
  1224. return id & 0xffff;
  1225. }
  1226. uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) {
  1227. switch (EVP_PKEY_id(key)) {
  1228. case EVP_PKEY_RSA:
  1229. return SSL_aRSA;
  1230. case EVP_PKEY_EC:
  1231. case EVP_PKEY_ED25519:
  1232. // Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers.
  1233. return SSL_aECDSA;
  1234. default:
  1235. return 0;
  1236. }
  1237. }
  1238. bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
  1239. return (cipher->algorithm_auth & SSL_aCERT) != 0;
  1240. }
  1241. bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
  1242. // Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is
  1243. // optional or omitted in all others.
  1244. return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
  1245. }
  1246. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
  1247. size_t block_size;
  1248. switch (cipher->algorithm_enc) {
  1249. case SSL_3DES:
  1250. block_size = 8;
  1251. break;
  1252. case SSL_AES128:
  1253. case SSL_AES256:
  1254. block_size = 16;
  1255. break;
  1256. default:
  1257. return 0;
  1258. }
  1259. // All supported TLS 1.0 ciphers use SHA-1.
  1260. assert(cipher->algorithm_mac == SSL_SHA1);
  1261. size_t ret = 1 + SHA_DIGEST_LENGTH;
  1262. ret += block_size - (ret % block_size);
  1263. return ret;
  1264. }
  1265. } // namespace bssl
  1266. using namespace bssl;
  1267. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  1268. SSL_CIPHER c;
  1269. c.id = 0x03000000L | value;
  1270. return reinterpret_cast<const SSL_CIPHER *>(bsearch(
  1271. &c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER), ssl_cipher_id_cmp));
  1272. }
  1273. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
  1274. int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) {
  1275. return (cipher->algorithm_mac & SSL_AEAD) != 0;
  1276. }
  1277. int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) {
  1278. switch (cipher->algorithm_enc) {
  1279. case SSL_eNULL:
  1280. return NID_undef;
  1281. case SSL_3DES:
  1282. return NID_des_ede3_cbc;
  1283. case SSL_AES128:
  1284. return NID_aes_128_cbc;
  1285. case SSL_AES256:
  1286. return NID_aes_256_cbc;
  1287. case SSL_AES128GCM:
  1288. return NID_aes_128_gcm;
  1289. case SSL_AES256GCM:
  1290. return NID_aes_256_gcm;
  1291. case SSL_CHACHA20POLY1305:
  1292. return NID_chacha20_poly1305;
  1293. }
  1294. assert(0);
  1295. return NID_undef;
  1296. }
  1297. int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) {
  1298. switch (cipher->algorithm_mac) {
  1299. case SSL_AEAD:
  1300. return NID_undef;
  1301. case SSL_SHA1:
  1302. return NID_sha1;
  1303. case SSL_SHA256:
  1304. return NID_sha256;
  1305. case SSL_SHA384:
  1306. return NID_sha384;
  1307. }
  1308. assert(0);
  1309. return NID_undef;
  1310. }
  1311. int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) {
  1312. switch (cipher->algorithm_mkey) {
  1313. case SSL_kRSA:
  1314. return NID_kx_rsa;
  1315. case SSL_kECDHE:
  1316. return NID_kx_ecdhe;
  1317. case SSL_kPSK:
  1318. return NID_kx_psk;
  1319. case SSL_kGENERIC:
  1320. return NID_kx_any;
  1321. }
  1322. assert(0);
  1323. return NID_undef;
  1324. }
  1325. int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) {
  1326. switch (cipher->algorithm_auth) {
  1327. case SSL_aRSA:
  1328. return NID_auth_rsa;
  1329. case SSL_aECDSA:
  1330. return NID_auth_ecdsa;
  1331. case SSL_aPSK:
  1332. return NID_auth_psk;
  1333. case SSL_aGENERIC:
  1334. return NID_auth_any;
  1335. }
  1336. assert(0);
  1337. return NID_undef;
  1338. }
  1339. int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) {
  1340. switch (cipher->algorithm_prf) {
  1341. case SSL_HANDSHAKE_MAC_DEFAULT:
  1342. return NID_md5_sha1;
  1343. case SSL_HANDSHAKE_MAC_SHA256:
  1344. return NID_sha256;
  1345. case SSL_HANDSHAKE_MAC_SHA384:
  1346. return NID_sha384;
  1347. }
  1348. assert(0);
  1349. return NID_undef;
  1350. }
  1351. int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
  1352. return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
  1353. cipher->algorithm_mac != SSL_AEAD;
  1354. }
  1355. uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
  1356. if (cipher->algorithm_mkey == SSL_kGENERIC ||
  1357. cipher->algorithm_auth == SSL_aGENERIC) {
  1358. return TLS1_3_VERSION;
  1359. }
  1360. if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
  1361. // Cipher suites before TLS 1.2 use the default PRF, while all those added
  1362. // afterwards specify a particular hash.
  1363. return TLS1_2_VERSION;
  1364. }
  1365. return SSL3_VERSION;
  1366. }
  1367. uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
  1368. if (cipher->algorithm_mkey == SSL_kGENERIC ||
  1369. cipher->algorithm_auth == SSL_aGENERIC) {
  1370. return TLS1_3_VERSION;
  1371. }
  1372. return TLS1_2_VERSION;
  1373. }
  1374. // return the actual cipher being used
  1375. const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
  1376. if (cipher != NULL) {
  1377. return cipher->name;
  1378. }
  1379. return "(NONE)";
  1380. }
  1381. const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) {
  1382. return cipher->standard_name;
  1383. }
  1384. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1385. if (cipher == NULL) {
  1386. return "";
  1387. }
  1388. switch (cipher->algorithm_mkey) {
  1389. case SSL_kRSA:
  1390. return "RSA";
  1391. case SSL_kECDHE:
  1392. switch (cipher->algorithm_auth) {
  1393. case SSL_aECDSA:
  1394. return "ECDHE_ECDSA";
  1395. case SSL_aRSA:
  1396. return "ECDHE_RSA";
  1397. case SSL_aPSK:
  1398. return "ECDHE_PSK";
  1399. default:
  1400. assert(0);
  1401. return "UNKNOWN";
  1402. }
  1403. case SSL_kPSK:
  1404. assert(cipher->algorithm_auth == SSL_aPSK);
  1405. return "PSK";
  1406. case SSL_kGENERIC:
  1407. assert(cipher->algorithm_auth == SSL_aGENERIC);
  1408. return "GENERIC";
  1409. default:
  1410. assert(0);
  1411. return "UNKNOWN";
  1412. }
  1413. }
  1414. char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
  1415. if (cipher == NULL) {
  1416. return NULL;
  1417. }
  1418. return OPENSSL_strdup(SSL_CIPHER_standard_name(cipher));
  1419. }
  1420. int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
  1421. if (cipher == NULL) {
  1422. return 0;
  1423. }
  1424. int alg_bits, strength_bits;
  1425. switch (cipher->algorithm_enc) {
  1426. case SSL_AES128:
  1427. case SSL_AES128GCM:
  1428. alg_bits = 128;
  1429. strength_bits = 128;
  1430. break;
  1431. case SSL_AES256:
  1432. case SSL_AES256GCM:
  1433. case SSL_CHACHA20POLY1305:
  1434. alg_bits = 256;
  1435. strength_bits = 256;
  1436. break;
  1437. case SSL_3DES:
  1438. alg_bits = 168;
  1439. strength_bits = 112;
  1440. break;
  1441. case SSL_eNULL:
  1442. alg_bits = 0;
  1443. strength_bits = 0;
  1444. break;
  1445. default:
  1446. assert(0);
  1447. alg_bits = 0;
  1448. strength_bits = 0;
  1449. }
  1450. if (out_alg_bits != NULL) {
  1451. *out_alg_bits = alg_bits;
  1452. }
  1453. return strength_bits;
  1454. }
  1455. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
  1456. int len) {
  1457. const char *kx, *au, *enc, *mac;
  1458. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1459. alg_mkey = cipher->algorithm_mkey;
  1460. alg_auth = cipher->algorithm_auth;
  1461. alg_enc = cipher->algorithm_enc;
  1462. alg_mac = cipher->algorithm_mac;
  1463. switch (alg_mkey) {
  1464. case SSL_kRSA:
  1465. kx = "RSA";
  1466. break;
  1467. case SSL_kECDHE:
  1468. kx = "ECDH";
  1469. break;
  1470. case SSL_kPSK:
  1471. kx = "PSK";
  1472. break;
  1473. case SSL_kGENERIC:
  1474. kx = "GENERIC";
  1475. break;
  1476. default:
  1477. kx = "unknown";
  1478. }
  1479. switch (alg_auth) {
  1480. case SSL_aRSA:
  1481. au = "RSA";
  1482. break;
  1483. case SSL_aECDSA:
  1484. au = "ECDSA";
  1485. break;
  1486. case SSL_aPSK:
  1487. au = "PSK";
  1488. break;
  1489. case SSL_aGENERIC:
  1490. au = "GENERIC";
  1491. break;
  1492. default:
  1493. au = "unknown";
  1494. break;
  1495. }
  1496. switch (alg_enc) {
  1497. case SSL_3DES:
  1498. enc = "3DES(168)";
  1499. break;
  1500. case SSL_AES128:
  1501. enc = "AES(128)";
  1502. break;
  1503. case SSL_AES256:
  1504. enc = "AES(256)";
  1505. break;
  1506. case SSL_AES128GCM:
  1507. enc = "AESGCM(128)";
  1508. break;
  1509. case SSL_AES256GCM:
  1510. enc = "AESGCM(256)";
  1511. break;
  1512. case SSL_CHACHA20POLY1305:
  1513. enc = "ChaCha20-Poly1305";
  1514. break;
  1515. case SSL_eNULL:
  1516. enc="None";
  1517. break;
  1518. default:
  1519. enc = "unknown";
  1520. break;
  1521. }
  1522. switch (alg_mac) {
  1523. case SSL_SHA1:
  1524. mac = "SHA1";
  1525. break;
  1526. case SSL_SHA256:
  1527. mac = "SHA256";
  1528. break;
  1529. case SSL_SHA384:
  1530. mac = "SHA384";
  1531. break;
  1532. case SSL_AEAD:
  1533. mac = "AEAD";
  1534. break;
  1535. default:
  1536. mac = "unknown";
  1537. break;
  1538. }
  1539. if (buf == NULL) {
  1540. len = 128;
  1541. buf = (char *)OPENSSL_malloc(len);
  1542. if (buf == NULL) {
  1543. return NULL;
  1544. }
  1545. } else if (len < 128) {
  1546. return "Buffer too small";
  1547. }
  1548. BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
  1549. cipher->name, kx, au, enc, mac);
  1550. return buf;
  1551. }
  1552. const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
  1553. return "TLSv1/SSLv3";
  1554. }
  1555. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { return NULL; }
  1556. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
  1557. const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
  1558. const char *SSL_COMP_get0_name(const SSL_COMP *comp) { return comp->name; }
  1559. int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; }
  1560. void SSL_COMP_free_compression_methods(void) {}