Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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