No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1747 líneas
48 KiB

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