Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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