You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1755 line
52 KiB

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