25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

1704 satır
51 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 <assert.h>
  141. #include <stdio.h>
  142. #include <string.h>
  143. #include <openssl/buf.h>
  144. #include <openssl/err.h>
  145. #include <openssl/md5.h>
  146. #include <openssl/mem.h>
  147. #include <openssl/sha.h>
  148. #include <openssl/stack.h>
  149. #include "internal.h"
  150. /* kCiphers is an array of all supported ciphers, sorted by id. */
  151. const SSL_CIPHER kCiphers[] = {
  152. /* The RSA ciphers */
  153. /* Cipher 04 */
  154. {
  155. SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
  156. SSL_RC4, SSL_MD5, SSL_SSLV3, SSL_MEDIUM,
  157. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  158. },
  159. /* Cipher 05 */
  160. {
  161. SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
  162. SSL_RC4, SSL_SHA1, SSL_SSLV3, SSL_MEDIUM,
  163. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  164. },
  165. /* Cipher 0A */
  166. {
  167. SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
  168. SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_SSLV3, SSL_HIGH | SSL_FIPS,
  169. SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
  170. },
  171. /* New AES ciphersuites */
  172. /* Cipher 2F */
  173. {
  174. TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
  175. SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  176. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  177. },
  178. /* Cipher 33 */
  179. {
  180. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
  181. SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  182. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  183. },
  184. /* Cipher 35 */
  185. {
  186. TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
  187. SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  188. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  189. },
  190. /* Cipher 39 */
  191. {
  192. TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
  193. SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  194. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  195. },
  196. /* TLS v1.2 ciphersuites */
  197. /* Cipher 3C */
  198. {
  199. TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
  200. SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_TLSV1_2,
  201. SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  202. },
  203. /* Cipher 3D */
  204. {
  205. TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
  206. SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, SSL_TLSV1_2,
  207. SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
  208. },
  209. /* Cipher 67 */
  210. {
  211. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
  212. TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
  213. SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  214. SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  215. },
  216. /* Cipher 6B */
  217. {
  218. TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
  219. TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
  220. SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  221. SSL_HANDSHAKE_MAC_SHA256, 256, 256,
  222. },
  223. /* Cipher 8A */
  224. {
  225. TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
  226. SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
  227. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  228. },
  229. /* Cipher 8C */
  230. {
  231. TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
  232. SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  233. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  234. },
  235. /* Cipher 8D */
  236. {
  237. TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
  238. SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  239. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  240. },
  241. /* GCM ciphersuites from RFC5288 */
  242. /* Cipher 9C */
  243. {
  244. TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
  245. TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
  246. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  247. SSL_HANDSHAKE_MAC_SHA256 |
  248. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  249. 128, 128,
  250. },
  251. /* Cipher 9D */
  252. {
  253. TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
  254. TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
  255. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  256. SSL_HANDSHAKE_MAC_SHA384 |
  257. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  258. 256, 256,
  259. },
  260. /* Cipher 9E */
  261. {
  262. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
  263. TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
  264. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  265. SSL_HANDSHAKE_MAC_SHA256 |
  266. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  267. 128, 128,
  268. },
  269. /* Cipher 9F */
  270. {
  271. TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
  272. TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
  273. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  274. SSL_HANDSHAKE_MAC_SHA384 |
  275. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  276. 256, 256,
  277. },
  278. /* Cipher C007 */
  279. {
  280. TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
  281. TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
  282. SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
  283. 128,
  284. },
  285. /* Cipher C009 */
  286. {
  287. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  288. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
  289. SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  290. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  291. },
  292. /* Cipher C00A */
  293. {
  294. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  295. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
  296. SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  297. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  298. },
  299. /* Cipher C011 */
  300. {
  301. TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
  302. SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
  303. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  304. },
  305. /* Cipher C013 */
  306. {
  307. TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  308. TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
  309. SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  310. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  311. },
  312. /* Cipher C014 */
  313. {
  314. TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  315. TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
  316. SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  317. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  318. },
  319. /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
  320. /* Cipher C023 */
  321. {
  322. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
  323. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
  324. SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  325. SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  326. },
  327. /* Cipher C024 */
  328. {
  329. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
  330. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
  331. SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  332. SSL_HANDSHAKE_MAC_SHA384, 256, 256,
  333. },
  334. /* Cipher C027 */
  335. {
  336. TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
  337. TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
  338. SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  339. SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  340. },
  341. /* Cipher C028 */
  342. {
  343. TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
  344. TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
  345. SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  346. SSL_HANDSHAKE_MAC_SHA384, 256, 256,
  347. },
  348. /* GCM based TLS v1.2 ciphersuites from RFC5289 */
  349. /* Cipher C02B */
  350. {
  351. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  352. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
  353. SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  354. SSL_HANDSHAKE_MAC_SHA256 |
  355. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  356. 128, 128,
  357. },
  358. /* Cipher C02C */
  359. {
  360. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  361. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
  362. SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  363. SSL_HANDSHAKE_MAC_SHA384 |
  364. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  365. 256, 256,
  366. },
  367. /* Cipher C02F */
  368. {
  369. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  370. TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
  371. SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  372. SSL_HANDSHAKE_MAC_SHA256 |
  373. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  374. 128, 128,
  375. },
  376. /* Cipher C030 */
  377. {
  378. TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  379. TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
  380. SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  381. SSL_HANDSHAKE_MAC_SHA384 |
  382. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  383. 256, 256,
  384. },
  385. /* ECDH PSK ciphersuites */
  386. /* Cipher CAFE */
  387. {
  388. TLS1_TXT_ECDHE_PSK_WITH_AES_128_GCM_SHA256,
  389. TLS1_CK_ECDHE_PSK_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aPSK,
  390. SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
  391. SSL_HANDSHAKE_MAC_SHA256 |
  392. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
  393. 128, 128,
  394. },
  395. {
  396. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  397. TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA,
  398. SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
  399. SSL_HANDSHAKE_MAC_SHA256,
  400. 256, 0,
  401. },
  402. {
  403. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  404. TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA,
  405. SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
  406. SSL_HANDSHAKE_MAC_SHA256,
  407. 256, 0,
  408. },
  409. {
  410. TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
  411. TLS1_CK_DHE_RSA_CHACHA20_POLY1305, SSL_kDHE, SSL_aRSA,
  412. SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
  413. SSL_HANDSHAKE_MAC_SHA256,
  414. 256, 0,
  415. },
  416. };
  417. static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
  418. struct handshake_digest {
  419. uint32_t mask;
  420. const EVP_MD *(*md_func)(void);
  421. };
  422. static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
  423. {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
  424. {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
  425. {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
  426. {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
  427. };
  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. int active;
  436. int 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. uint32_t algorithm_ssl;
  451. uint32_t algo_strength;
  452. } CIPHER_ALIAS;
  453. static const CIPHER_ALIAS kCipherAliases[] = {
  454. {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
  455. /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
  456. /* key exchange aliases
  457. * (some of those using only a single bit here combine
  458. * multiple key exchange algs according to the RFCs,
  459. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  460. {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
  461. {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  462. {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  463. {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  464. {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  465. {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  466. {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  467. {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
  468. /* server authentication aliases */
  469. {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
  470. {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
  471. {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
  472. {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
  473. /* aliases combining key exchange and server authentication */
  474. {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  475. {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  476. {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  477. {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  478. {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
  479. {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
  480. /* symmetric encryption aliases */
  481. {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
  482. {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
  483. {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
  484. {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
  485. {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
  486. {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
  487. {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
  488. /* MAC aliases */
  489. {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
  490. {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
  491. {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
  492. {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
  493. {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
  494. /* protocol version aliases */
  495. {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
  496. {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
  497. {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
  498. /* strength classes */
  499. {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
  500. {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
  501. /* FIPS 140-2 approved ciphersuite */
  502. {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
  503. };
  504. static const size_t kCipherAliasesLen =
  505. sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
  506. static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  507. const SSL_CIPHER *a = in_a;
  508. const SSL_CIPHER *b = in_b;
  509. if (a->id > b->id) {
  510. return 1;
  511. } else if (a->id < b->id) {
  512. return -1;
  513. } else {
  514. return 0;
  515. }
  516. }
  517. static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
  518. return ssl_cipher_id_cmp(*a, *b);
  519. }
  520. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  521. SSL_CIPHER c;
  522. c.id = 0x03000000L | value;
  523. return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
  524. ssl_cipher_id_cmp);
  525. }
  526. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  527. size_t *out_mac_secret_len,
  528. size_t *out_fixed_iv_len,
  529. const SSL_CIPHER *cipher, uint16_t version) {
  530. *out_aead = NULL;
  531. *out_mac_secret_len = 0;
  532. *out_fixed_iv_len = 0;
  533. switch (cipher->algorithm_enc) {
  534. case SSL_AES128GCM:
  535. *out_aead = EVP_aead_aes_128_gcm();
  536. *out_fixed_iv_len = 4;
  537. return 1;
  538. case SSL_AES256GCM:
  539. *out_aead = EVP_aead_aes_256_gcm();
  540. *out_fixed_iv_len = 4;
  541. return 1;
  542. case SSL_CHACHA20POLY1305:
  543. *out_aead = EVP_aead_chacha20_poly1305();
  544. *out_fixed_iv_len = 0;
  545. return 1;
  546. case SSL_RC4:
  547. switch (cipher->algorithm_mac) {
  548. case SSL_MD5:
  549. if (version == SSL3_VERSION) {
  550. *out_aead = EVP_aead_rc4_md5_ssl3();
  551. } else {
  552. *out_aead = EVP_aead_rc4_md5_tls();
  553. }
  554. *out_mac_secret_len = MD5_DIGEST_LENGTH;
  555. return 1;
  556. case SSL_SHA1:
  557. if (version == SSL3_VERSION) {
  558. *out_aead = EVP_aead_rc4_sha1_ssl3();
  559. } else {
  560. *out_aead = EVP_aead_rc4_sha1_tls();
  561. }
  562. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  563. return 1;
  564. default:
  565. return 0;
  566. }
  567. case SSL_AES128:
  568. switch (cipher->algorithm_mac) {
  569. case SSL_SHA1:
  570. if (version == SSL3_VERSION) {
  571. *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
  572. *out_fixed_iv_len = 16;
  573. } else if (version == TLS1_VERSION) {
  574. *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
  575. *out_fixed_iv_len = 16;
  576. } else {
  577. *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
  578. }
  579. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  580. return 1;
  581. case SSL_SHA256:
  582. *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
  583. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  584. return 1;
  585. default:
  586. return 0;
  587. }
  588. case SSL_AES256:
  589. switch (cipher->algorithm_mac) {
  590. case SSL_SHA1:
  591. if (version == SSL3_VERSION) {
  592. *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
  593. *out_fixed_iv_len = 16;
  594. } else if (version == TLS1_VERSION) {
  595. *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
  596. *out_fixed_iv_len = 16;
  597. } else {
  598. *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
  599. }
  600. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  601. return 1;
  602. case SSL_SHA256:
  603. *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
  604. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  605. return 1;
  606. case SSL_SHA384:
  607. *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
  608. *out_mac_secret_len = SHA384_DIGEST_LENGTH;
  609. return 1;
  610. default:
  611. return 0;
  612. }
  613. case SSL_3DES:
  614. switch (cipher->algorithm_mac) {
  615. case SSL_SHA1:
  616. if (version == SSL3_VERSION) {
  617. *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
  618. *out_fixed_iv_len = 8;
  619. } else if (version == TLS1_VERSION) {
  620. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
  621. *out_fixed_iv_len = 8;
  622. } else {
  623. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
  624. }
  625. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  626. return 1;
  627. default:
  628. return 0;
  629. }
  630. default:
  631. return 0;
  632. }
  633. }
  634. int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md,
  635. size_t idx) {
  636. if (idx >= SSL_MAX_DIGEST) {
  637. return 0;
  638. }
  639. *out_mask = ssl_handshake_digests[idx].mask;
  640. *out_md = ssl_handshake_digests[idx].md_func();
  641. return 1;
  642. }
  643. #define ITEM_SEP(a) \
  644. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  645. /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
  646. * |buf_len| bytes at |buf|. */
  647. static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
  648. /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
  649. return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
  650. }
  651. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  652. CIPHER_ORDER **tail) {
  653. if (curr == *tail) {
  654. return;
  655. }
  656. if (curr == *head) {
  657. *head = curr->next;
  658. }
  659. if (curr->prev != NULL) {
  660. curr->prev->next = curr->next;
  661. }
  662. if (curr->next != NULL) {
  663. curr->next->prev = curr->prev;
  664. }
  665. (*tail)->next = curr;
  666. curr->prev = *tail;
  667. curr->next = NULL;
  668. *tail = curr;
  669. }
  670. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  671. CIPHER_ORDER **tail) {
  672. if (curr == *head) {
  673. return;
  674. }
  675. if (curr == *tail) {
  676. *tail = curr->prev;
  677. }
  678. if (curr->next != NULL) {
  679. curr->next->prev = curr->prev;
  680. }
  681. if (curr->prev != NULL) {
  682. curr->prev->next = curr->next;
  683. }
  684. (*head)->prev = curr;
  685. curr->next = *head;
  686. curr->prev = NULL;
  687. *head = curr;
  688. }
  689. static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
  690. CIPHER_ORDER *co_list,
  691. CIPHER_ORDER **head_p,
  692. CIPHER_ORDER **tail_p) {
  693. /* The set of ciphers is static, but some subset may be unsupported by
  694. * |ssl_method|, so the list may be smaller. */
  695. size_t co_list_num = 0;
  696. size_t i;
  697. for (i = 0; i < kCiphersLen; i++) {
  698. const SSL_CIPHER *cipher = &kCiphers[i];
  699. if (ssl_method->supports_cipher(cipher)) {
  700. co_list[co_list_num].cipher = cipher;
  701. co_list[co_list_num].next = NULL;
  702. co_list[co_list_num].prev = NULL;
  703. co_list[co_list_num].active = 0;
  704. co_list[co_list_num].in_group = 0;
  705. co_list_num++;
  706. }
  707. }
  708. /* Prepare linked list from list entries. */
  709. if (co_list_num > 0) {
  710. co_list[0].prev = NULL;
  711. if (co_list_num > 1) {
  712. co_list[0].next = &co_list[1];
  713. for (i = 1; i < co_list_num - 1; i++) {
  714. co_list[i].prev = &co_list[i - 1];
  715. co_list[i].next = &co_list[i + 1];
  716. }
  717. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  718. }
  719. co_list[co_list_num - 1].next = NULL;
  720. *head_p = &co_list[0];
  721. *tail_p = &co_list[co_list_num - 1];
  722. }
  723. }
  724. /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
  725. * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
  726. * head and tail of the list to |*head_p| and |*tail_p|, respectively.
  727. *
  728. * - If |cipher_id| is non-zero, only that cipher is selected.
  729. * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
  730. * of that strength.
  731. * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
  732. * |algo_strength|. */
  733. static void ssl_cipher_apply_rule(
  734. uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
  735. uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
  736. uint32_t algo_strength, int rule, int strength_bits, int in_group,
  737. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
  738. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  739. const SSL_CIPHER *cp;
  740. int reverse = 0;
  741. if (cipher_id == 0 && strength_bits == -1 &&
  742. (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
  743. alg_ssl == 0 || algo_strength == 0)) {
  744. /* The rule matches nothing, so bail early. */
  745. return;
  746. }
  747. if (rule == CIPHER_DEL) {
  748. /* needed to maintain sorting between currently deleted ciphers */
  749. reverse = 1;
  750. }
  751. head = *head_p;
  752. tail = *tail_p;
  753. if (reverse) {
  754. next = tail;
  755. last = head;
  756. } else {
  757. next = head;
  758. last = tail;
  759. }
  760. curr = NULL;
  761. for (;;) {
  762. if (curr == last) {
  763. break;
  764. }
  765. curr = next;
  766. if (curr == NULL) {
  767. break;
  768. }
  769. next = reverse ? curr->prev : curr->next;
  770. cp = curr->cipher;
  771. /* Selection criteria is either a specific cipher, the value of
  772. * |strength_bits|, or the algorithms used. */
  773. if (cipher_id != 0) {
  774. if (cipher_id != cp->id) {
  775. continue;
  776. }
  777. } else if (strength_bits >= 0) {
  778. if (strength_bits != cp->strength_bits) {
  779. continue;
  780. }
  781. } else if (!(alg_mkey & cp->algorithm_mkey) ||
  782. !(alg_auth & cp->algorithm_auth) ||
  783. !(alg_enc & cp->algorithm_enc) ||
  784. !(alg_mac & cp->algorithm_mac) ||
  785. !(alg_ssl & cp->algorithm_ssl) ||
  786. !(algo_strength & cp->algo_strength)) {
  787. continue;
  788. }
  789. /* add the cipher if it has not been added yet. */
  790. if (rule == CIPHER_ADD) {
  791. /* reverse == 0 */
  792. if (!curr->active) {
  793. ll_append_tail(&head, curr, &tail);
  794. curr->active = 1;
  795. curr->in_group = in_group;
  796. }
  797. }
  798. /* Move the added cipher to this location */
  799. else if (rule == CIPHER_ORD) {
  800. /* reverse == 0 */
  801. if (curr->active) {
  802. ll_append_tail(&head, curr, &tail);
  803. curr->in_group = 0;
  804. }
  805. } else if (rule == CIPHER_DEL) {
  806. /* reverse == 1 */
  807. if (curr->active) {
  808. /* most recently deleted ciphersuites get best positions
  809. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  810. * works in reverse to maintain the order) */
  811. ll_append_head(&head, curr, &tail);
  812. curr->active = 0;
  813. curr->in_group = 0;
  814. }
  815. } else if (rule == CIPHER_KILL) {
  816. /* reverse == 0 */
  817. if (head == curr) {
  818. head = curr->next;
  819. } else {
  820. curr->prev->next = curr->next;
  821. }
  822. if (tail == curr) {
  823. tail = curr->prev;
  824. }
  825. curr->active = 0;
  826. if (curr->next != NULL) {
  827. curr->next->prev = curr->prev;
  828. }
  829. if (curr->prev != NULL) {
  830. curr->prev->next = curr->next;
  831. }
  832. curr->next = NULL;
  833. curr->prev = NULL;
  834. }
  835. }
  836. *head_p = head;
  837. *tail_p = tail;
  838. }
  839. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  840. CIPHER_ORDER **tail_p) {
  841. int max_strength_bits, i, *number_uses;
  842. CIPHER_ORDER *curr;
  843. /* This routine sorts the ciphers with descending strength. The sorting must
  844. * keep the pre-sorted sequence, so we apply the normal sorting routine as
  845. * '+' movement to the end of the list. */
  846. max_strength_bits = 0;
  847. curr = *head_p;
  848. while (curr != NULL) {
  849. if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
  850. max_strength_bits = curr->cipher->strength_bits;
  851. }
  852. curr = curr->next;
  853. }
  854. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  855. if (!number_uses) {
  856. OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
  857. return 0;
  858. }
  859. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  860. /* Now find the strength_bits values actually used. */
  861. curr = *head_p;
  862. while (curr != NULL) {
  863. if (curr->active) {
  864. number_uses[curr->cipher->strength_bits]++;
  865. }
  866. curr = curr->next;
  867. }
  868. /* Go through the list of used strength_bits values in descending order. */
  869. for (i = max_strength_bits; i >= 0; i--) {
  870. if (number_uses[i] > 0) {
  871. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
  872. tail_p);
  873. }
  874. }
  875. OPENSSL_free(number_uses);
  876. return 1;
  877. }
  878. static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
  879. const char *rule_str,
  880. CIPHER_ORDER **head_p,
  881. CIPHER_ORDER **tail_p) {
  882. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
  883. const char *l, *buf;
  884. int multi, rule, retval, ok, in_group = 0, has_group = 0;
  885. size_t j, buf_len;
  886. uint32_t cipher_id;
  887. char ch;
  888. retval = 1;
  889. l = rule_str;
  890. for (;;) {
  891. ch = *l;
  892. if (ch == '\0') {
  893. break; /* done */
  894. }
  895. if (in_group) {
  896. if (ch == ']') {
  897. if (*tail_p) {
  898. (*tail_p)->in_group = 0;
  899. }
  900. in_group = 0;
  901. l++;
  902. continue;
  903. }
  904. if (ch == '|') {
  905. rule = CIPHER_ADD;
  906. l++;
  907. continue;
  908. } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
  909. !(ch >= '0' && ch <= '9')) {
  910. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
  911. SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  912. retval = in_group = 0;
  913. break;
  914. } else {
  915. rule = CIPHER_ADD;
  916. }
  917. } else if (ch == '-') {
  918. rule = CIPHER_DEL;
  919. l++;
  920. } else if (ch == '+') {
  921. rule = CIPHER_ORD;
  922. l++;
  923. } else if (ch == '!') {
  924. rule = CIPHER_KILL;
  925. l++;
  926. } else if (ch == '@') {
  927. rule = CIPHER_SPECIAL;
  928. l++;
  929. } else if (ch == '[') {
  930. if (in_group) {
  931. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
  932. retval = in_group = 0;
  933. break;
  934. }
  935. in_group = 1;
  936. has_group = 1;
  937. l++;
  938. continue;
  939. } else {
  940. rule = CIPHER_ADD;
  941. }
  942. /* If preference groups are enabled, the only legal operator is +.
  943. * Otherwise the in_group bits will get mixed up. */
  944. if (has_group && rule != CIPHER_ADD) {
  945. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
  946. SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  947. retval = in_group = 0;
  948. break;
  949. }
  950. if (ITEM_SEP(ch)) {
  951. l++;
  952. continue;
  953. }
  954. multi = 0;
  955. cipher_id = 0;
  956. alg_mkey = ~0u;
  957. alg_auth = ~0u;
  958. alg_enc = ~0u;
  959. alg_mac = ~0u;
  960. alg_ssl = ~0u;
  961. algo_strength = ~0u;
  962. for (;;) {
  963. ch = *l;
  964. buf = l;
  965. buf_len = 0;
  966. while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
  967. ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
  968. ch = *(++l);
  969. buf_len++;
  970. }
  971. if (buf_len == 0) {
  972. /* We hit something we cannot deal with, it is no command or separator
  973. * nor alphanumeric, so we call this an error. */
  974. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
  975. SSL_R_INVALID_COMMAND);
  976. retval = in_group = 0;
  977. l++;
  978. break;
  979. }
  980. if (rule == CIPHER_SPECIAL) {
  981. break;
  982. }
  983. /* Look for a matching exact cipher. These aren't allowed in multipart
  984. * rules. */
  985. if (!multi && ch != '+') {
  986. for (j = 0; j < kCiphersLen; j++) {
  987. const SSL_CIPHER *cipher = &kCiphers[j];
  988. if (rule_equals(cipher->name, buf, buf_len)) {
  989. cipher_id = cipher->id;
  990. break;
  991. }
  992. }
  993. }
  994. if (cipher_id == 0) {
  995. /* If not an exact cipher, look for a matching cipher alias. */
  996. for (j = 0; j < kCipherAliasesLen; j++) {
  997. if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
  998. alg_mkey &= kCipherAliases[j].algorithm_mkey;
  999. alg_auth &= kCipherAliases[j].algorithm_auth;
  1000. alg_enc &= kCipherAliases[j].algorithm_enc;
  1001. alg_mac &= kCipherAliases[j].algorithm_mac;
  1002. alg_ssl &= kCipherAliases[j].algorithm_ssl;
  1003. algo_strength &= kCipherAliases[j].algo_strength;
  1004. break;
  1005. }
  1006. }
  1007. if (j == kCipherAliasesLen) {
  1008. alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
  1009. }
  1010. }
  1011. /* Check for a multipart rule. */
  1012. if (ch != '+') {
  1013. break;
  1014. }
  1015. l++;
  1016. multi = 1;
  1017. }
  1018. /* Ok, we have the rule, now apply it. */
  1019. if (rule == CIPHER_SPECIAL) {
  1020. /* special command */
  1021. ok = 0;
  1022. if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
  1023. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1024. } else {
  1025. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
  1026. SSL_R_INVALID_COMMAND);
  1027. }
  1028. if (ok == 0) {
  1029. retval = 0;
  1030. }
  1031. /* We do not support any "multi" options together with "@", so throw away
  1032. * the rest of the command, if any left, until end or ':' is found. */
  1033. while (*l != '\0' && !ITEM_SEP(*l)) {
  1034. l++;
  1035. }
  1036. } else {
  1037. ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
  1038. alg_ssl, algo_strength, rule, -1, in_group, head_p,
  1039. tail_p);
  1040. }
  1041. }
  1042. if (in_group) {
  1043. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  1044. retval = 0;
  1045. }
  1046. return retval;
  1047. }
  1048. STACK_OF(SSL_CIPHER) *
  1049. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
  1050. struct ssl_cipher_preference_list_st **out_cipher_list,
  1051. STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
  1052. const char *rule_str) {
  1053. int ok;
  1054. STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
  1055. const char *rule_p;
  1056. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1057. uint8_t *in_group_flags = NULL;
  1058. unsigned int num_in_group_flags = 0;
  1059. struct ssl_cipher_preference_list_st *pref_list = NULL;
  1060. /* Return with error if nothing to do. */
  1061. if (rule_str == NULL || out_cipher_list == NULL) {
  1062. return NULL;
  1063. }
  1064. /* Now we have to collect the available ciphers from the compiled in ciphers.
  1065. * We cannot get more than the number compiled in, so it is used for
  1066. * allocation. */
  1067. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
  1068. if (co_list == NULL) {
  1069. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  1070. return NULL;
  1071. }
  1072. ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
  1073. /* Now arrange all ciphers by preference:
  1074. * TODO(davidben): Compute this order once and copy it. */
  1075. /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
  1076. * key exchange mechanisms */
  1077. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
  1078. CIPHER_ADD, -1, 0, &head, &tail);
  1079. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
  1080. 0, &head, &tail);
  1081. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
  1082. 0, &head, &tail);
  1083. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  1084. * CHACHA20 unless there is hardware support for fast and constant-time
  1085. * AES_GCM. */
  1086. if (EVP_has_aes_hardware()) {
  1087. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1088. -1, 0, &head, &tail);
  1089. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1090. -1, 0, &head, &tail);
  1091. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
  1092. CIPHER_ADD, -1, 0, &head, &tail);
  1093. } else {
  1094. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
  1095. CIPHER_ADD, -1, 0, &head, &tail);
  1096. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1097. -1, 0, &head, &tail);
  1098. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1099. -1, 0, &head, &tail);
  1100. }
  1101. /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
  1102. * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
  1103. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
  1104. 0, &head, &tail);
  1105. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
  1106. 0, &head, &tail);
  1107. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
  1108. -1, 0, &head, &tail);
  1109. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
  1110. 0, &head, &tail);
  1111. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
  1112. &head, &tail);
  1113. /* Temporarily enable everything else for sorting */
  1114. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
  1115. &head, &tail);
  1116. /* Move ciphers without forward secrecy to the end. */
  1117. ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
  1118. CIPHER_ORD, -1, 0, &head, &tail);
  1119. /* Now disable everything (maintaining the ordering!) */
  1120. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
  1121. &head, &tail);
  1122. /* If the rule_string begins with DEFAULT, apply the default rule before
  1123. * using the (possibly available) additional rules. */
  1124. ok = 1;
  1125. rule_p = rule_str;
  1126. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1127. ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
  1128. &tail);
  1129. rule_p += 7;
  1130. if (*rule_p == ':') {
  1131. rule_p++;
  1132. }
  1133. }
  1134. if (ok && strlen(rule_p) > 0) {
  1135. ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
  1136. }
  1137. if (!ok) {
  1138. goto err;
  1139. }
  1140. /* Allocate new "cipherstack" for the result, return with error
  1141. * if we cannot get one. */
  1142. cipherstack = sk_SSL_CIPHER_new_null();
  1143. if (cipherstack == NULL) {
  1144. goto err;
  1145. }
  1146. in_group_flags = OPENSSL_malloc(kCiphersLen);
  1147. if (!in_group_flags) {
  1148. goto err;
  1149. }
  1150. /* The cipher selection for the list is done. The ciphers are added
  1151. * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
  1152. for (curr = head; curr != NULL; curr = curr->next) {
  1153. if (curr->active) {
  1154. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1155. goto err;
  1156. }
  1157. in_group_flags[num_in_group_flags++] = curr->in_group;
  1158. }
  1159. }
  1160. OPENSSL_free(co_list); /* Not needed any longer */
  1161. co_list = NULL;
  1162. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1163. if (tmp_cipher_list == NULL) {
  1164. goto err;
  1165. }
  1166. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1167. if (!pref_list) {
  1168. goto err;
  1169. }
  1170. pref_list->ciphers = cipherstack;
  1171. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1172. if (!pref_list->in_group_flags) {
  1173. goto err;
  1174. }
  1175. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1176. OPENSSL_free(in_group_flags);
  1177. in_group_flags = NULL;
  1178. if (*out_cipher_list != NULL) {
  1179. ssl_cipher_preference_list_free(*out_cipher_list);
  1180. }
  1181. *out_cipher_list = pref_list;
  1182. pref_list = NULL;
  1183. if (out_cipher_list_by_id != NULL) {
  1184. sk_SSL_CIPHER_free(*out_cipher_list_by_id);
  1185. *out_cipher_list_by_id = tmp_cipher_list;
  1186. tmp_cipher_list = NULL;
  1187. (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
  1188. ssl_cipher_ptr_id_cmp);
  1189. sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
  1190. } else {
  1191. sk_SSL_CIPHER_free(tmp_cipher_list);
  1192. tmp_cipher_list = NULL;
  1193. }
  1194. return cipherstack;
  1195. err:
  1196. OPENSSL_free(co_list);
  1197. OPENSSL_free(in_group_flags);
  1198. sk_SSL_CIPHER_free(cipherstack);
  1199. sk_SSL_CIPHER_free(tmp_cipher_list);
  1200. if (pref_list) {
  1201. OPENSSL_free(pref_list->in_group_flags);
  1202. }
  1203. OPENSSL_free(pref_list);
  1204. return NULL;
  1205. }
  1206. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
  1207. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
  1208. uint32_t id = cipher->id;
  1209. /* All ciphers are SSLv3. */
  1210. assert((id & 0xff000000) == 0x03000000);
  1211. return id & 0xffff;
  1212. }
  1213. int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
  1214. return (cipher->algorithm_enc & SSL_AES) != 0;
  1215. }
  1216. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
  1217. return (cipher->algorithm_mac & SSL_MD5) != 0;
  1218. }
  1219. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
  1220. return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
  1221. }
  1222. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
  1223. return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
  1224. }
  1225. /* return the actual cipher being used */
  1226. const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
  1227. if (cipher != NULL) {
  1228. return cipher->name;
  1229. }
  1230. return "(NONE)";
  1231. }
  1232. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1233. if (cipher == NULL) {
  1234. return "";
  1235. }
  1236. switch (cipher->algorithm_mkey) {
  1237. case SSL_kRSA:
  1238. return "RSA";
  1239. case SSL_kDHE:
  1240. switch (cipher->algorithm_auth) {
  1241. case SSL_aRSA:
  1242. return "DHE_RSA";
  1243. default:
  1244. assert(0);
  1245. return "UNKNOWN";
  1246. }
  1247. case SSL_kECDHE:
  1248. switch (cipher->algorithm_auth) {
  1249. case SSL_aECDSA:
  1250. return "ECDHE_ECDSA";
  1251. case SSL_aRSA:
  1252. return "ECDHE_RSA";
  1253. case SSL_aPSK:
  1254. return "ECDHE_PSK";
  1255. default:
  1256. assert(0);
  1257. return "UNKNOWN";
  1258. }
  1259. case SSL_kPSK:
  1260. assert(cipher->algorithm_auth == SSL_aPSK);
  1261. return "PSK";
  1262. default:
  1263. assert(0);
  1264. return "UNKNOWN";
  1265. }
  1266. }
  1267. static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
  1268. switch (cipher->algorithm_enc) {
  1269. case SSL_3DES:
  1270. return "3DES_EDE_CBC";
  1271. case SSL_RC4:
  1272. return "RC4";
  1273. case SSL_AES128:
  1274. return "AES_128_CBC";
  1275. case SSL_AES256:
  1276. return "AES_256_CBC";
  1277. case SSL_AES128GCM:
  1278. return "AES_128_GCM";
  1279. case SSL_AES256GCM:
  1280. return "AES_256_GCM";
  1281. case SSL_CHACHA20POLY1305:
  1282. return "CHACHA20_POLY1305";
  1283. break;
  1284. default:
  1285. assert(0);
  1286. return "UNKNOWN";
  1287. }
  1288. }
  1289. static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
  1290. if ((cipher->algorithm2 & SSL_HANDSHAKE_MAC_DEFAULT) ==
  1291. SSL_HANDSHAKE_MAC_DEFAULT) {
  1292. /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
  1293. * only ever MD5 or SHA-1. */
  1294. switch (cipher->algorithm_mac) {
  1295. case SSL_MD5:
  1296. return "MD5";
  1297. case SSL_SHA1:
  1298. return "SHA";
  1299. default:
  1300. assert(0);
  1301. return "UNKNOWN";
  1302. }
  1303. } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA256) {
  1304. return "SHA256";
  1305. } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA384) {
  1306. return "SHA384";
  1307. } else {
  1308. assert(0);
  1309. return "UNKNOWN";
  1310. }
  1311. }
  1312. char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
  1313. if (cipher == NULL) {
  1314. return NULL;
  1315. }
  1316. const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
  1317. const char *enc_name = ssl_cipher_get_enc_name(cipher);
  1318. const char *prf_name = ssl_cipher_get_prf_name(cipher);
  1319. /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
  1320. size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
  1321. strlen(prf_name) + 1;
  1322. char *ret = OPENSSL_malloc(len);
  1323. if (ret == NULL) {
  1324. return NULL;
  1325. }
  1326. if (BUF_strlcpy(ret, "TLS_", len) >= len ||
  1327. BUF_strlcat(ret, kx_name, len) >= len ||
  1328. BUF_strlcat(ret, "_WITH_", len) >= len ||
  1329. BUF_strlcat(ret, enc_name, len) >= len ||
  1330. BUF_strlcat(ret, "_", len) >= len ||
  1331. BUF_strlcat(ret, prf_name, len) >= len) {
  1332. assert(0);
  1333. OPENSSL_free(ret);
  1334. return NULL;
  1335. }
  1336. assert(strlen(ret) + 1 == len);
  1337. return ret;
  1338. }
  1339. int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
  1340. if (cipher == NULL) {
  1341. return 0;
  1342. }
  1343. if (out_alg_bits != NULL) {
  1344. *out_alg_bits = cipher->alg_bits;
  1345. }
  1346. return cipher->strength_bits;
  1347. }
  1348. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
  1349. int len) {
  1350. const char *ver;
  1351. const char *kx, *au, *enc, *mac;
  1352. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
  1353. static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
  1354. alg_mkey = cipher->algorithm_mkey;
  1355. alg_auth = cipher->algorithm_auth;
  1356. alg_enc = cipher->algorithm_enc;
  1357. alg_mac = cipher->algorithm_mac;
  1358. alg_ssl = cipher->algorithm_ssl;
  1359. if (alg_ssl & SSL_SSLV3) {
  1360. ver = "SSLv3";
  1361. } else if (alg_ssl & SSL_TLSV1_2) {
  1362. ver = "TLSv1.2";
  1363. } else {
  1364. ver = "unknown";
  1365. }
  1366. switch (alg_mkey) {
  1367. case SSL_kRSA:
  1368. kx = "RSA";
  1369. break;
  1370. case SSL_kDHE:
  1371. kx = "DH";
  1372. break;
  1373. case SSL_kECDHE:
  1374. kx = "ECDH";
  1375. break;
  1376. case SSL_kPSK:
  1377. kx = "PSK";
  1378. break;
  1379. default:
  1380. kx = "unknown";
  1381. }
  1382. switch (alg_auth) {
  1383. case SSL_aRSA:
  1384. au = "RSA";
  1385. break;
  1386. case SSL_aECDSA:
  1387. au = "ECDSA";
  1388. break;
  1389. case SSL_aPSK:
  1390. au = "PSK";
  1391. break;
  1392. default:
  1393. au = "unknown";
  1394. break;
  1395. }
  1396. switch (alg_enc) {
  1397. case SSL_3DES:
  1398. enc = "3DES(168)";
  1399. break;
  1400. case SSL_RC4:
  1401. enc = "RC4(128)";
  1402. break;
  1403. case SSL_AES128:
  1404. enc = "AES(128)";
  1405. break;
  1406. case SSL_AES256:
  1407. enc = "AES(256)";
  1408. break;
  1409. case SSL_AES128GCM:
  1410. enc = "AESGCM(128)";
  1411. break;
  1412. case SSL_AES256GCM:
  1413. enc = "AESGCM(256)";
  1414. break;
  1415. case SSL_CHACHA20POLY1305:
  1416. enc = "ChaCha20-Poly1305";
  1417. break;
  1418. default:
  1419. enc = "unknown";
  1420. break;
  1421. }
  1422. switch (alg_mac) {
  1423. case SSL_MD5:
  1424. mac = "MD5";
  1425. break;
  1426. case SSL_SHA1:
  1427. mac = "SHA1";
  1428. break;
  1429. case SSL_SHA256:
  1430. mac = "SHA256";
  1431. break;
  1432. case SSL_SHA384:
  1433. mac = "SHA384";
  1434. break;
  1435. case SSL_AEAD:
  1436. mac = "AEAD";
  1437. break;
  1438. default:
  1439. mac = "unknown";
  1440. break;
  1441. }
  1442. if (buf == NULL) {
  1443. len = 128;
  1444. buf = OPENSSL_malloc(len);
  1445. if (buf == NULL) {
  1446. return NULL;
  1447. }
  1448. } else if (len < 128) {
  1449. return "Buffer too small";
  1450. }
  1451. BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
  1452. return buf;
  1453. }
  1454. const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
  1455. return "TLSv1/SSLv3";
  1456. }
  1457. void *SSL_COMP_get_compression_methods(void) { return NULL; }
  1458. int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
  1459. const char *SSL_COMP_get_name(const void *comp) { return NULL; }
  1460. int ssl_cipher_get_cert_index(const SSL_CIPHER *cipher) {
  1461. uint32_t alg_a = cipher->algorithm_auth;
  1462. if (alg_a & SSL_aECDSA) {
  1463. return SSL_PKEY_ECC;
  1464. } else if (alg_a & SSL_aRSA) {
  1465. return SSL_PKEY_RSA_ENC;
  1466. }
  1467. return -1;
  1468. }
  1469. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
  1470. /* PSK-authenticated ciphers do not use a public key, except for
  1471. * RSA_PSK. */
  1472. if ((cipher->algorithm_auth & SSL_aPSK) &&
  1473. !(cipher->algorithm_mkey & SSL_kRSA)) {
  1474. return 0;
  1475. }
  1476. /* All other ciphers include it. */
  1477. return 1;
  1478. }
  1479. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
  1480. /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
  1481. if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
  1482. return 1;
  1483. }
  1484. /* It is optional in all others. */
  1485. return 0;
  1486. }