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

1924 líneas
53 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 <stdio.h>
  141. #include <assert.h>
  142. #include <openssl/comp.h>
  143. #include <openssl/engine.h>
  144. #include <openssl/mem.h>
  145. #include <openssl/obj.h>
  146. #include "ssl_locl.h"
  147. #define SSL_ENC_DES_IDX 0
  148. #define SSL_ENC_3DES_IDX 1
  149. #define SSL_ENC_RC4_IDX 2
  150. #define SSL_ENC_RC2_IDX 3
  151. #define SSL_ENC_IDEA_IDX 4
  152. #define SSL_ENC_NULL_IDX 5
  153. #define SSL_ENC_AES128_IDX 6
  154. #define SSL_ENC_AES256_IDX 7
  155. #define SSL_ENC_CAMELLIA128_IDX 8
  156. #define SSL_ENC_CAMELLIA256_IDX 9
  157. #define SSL_ENC_GOST89_IDX 10
  158. #define SSL_ENC_SEED_IDX 11
  159. #define SSL_ENC_AES128GCM_IDX 12
  160. #define SSL_ENC_AES256GCM_IDX 13
  161. #define SSL_ENC_NUM_IDX 14
  162. static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
  163. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
  164. };
  165. #define SSL_COMP_NULL_IDX 0
  166. #define SSL_COMP_ZLIB_IDX 1
  167. #define SSL_COMP_NUM_IDX 2
  168. static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL;
  169. #define SSL_MD_MD5_IDX 0
  170. #define SSL_MD_SHA1_IDX 1
  171. #define SSL_MD_GOST94_IDX 2
  172. #define SSL_MD_GOST89MAC_IDX 3
  173. #define SSL_MD_SHA256_IDX 4
  174. #define SSL_MD_SHA384_IDX 5
  175. /*Constant SSL_MAX_DIGEST equal to size of digests array should be
  176. * defined in the
  177. * ssl_locl.h */
  178. #define SSL_MD_NUM_IDX SSL_MAX_DIGEST
  179. static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
  180. NULL,NULL,NULL,NULL,NULL,NULL
  181. };
  182. /* PKEY_TYPE for GOST89MAC is known in advance, but, because
  183. * implementation is engine-provided, we'll fill it only if
  184. * corresponding EVP_PKEY_METHOD is found
  185. */
  186. static int ssl_mac_pkey_id[SSL_MD_NUM_IDX]={
  187. EVP_PKEY_HMAC,EVP_PKEY_HMAC,EVP_PKEY_HMAC,NID_undef,
  188. EVP_PKEY_HMAC,EVP_PKEY_HMAC
  189. };
  190. static int ssl_mac_secret_size[SSL_MD_NUM_IDX]={
  191. 0,0,0,0,0,0
  192. };
  193. static int ssl_handshake_digest_flag[SSL_MD_NUM_IDX]={
  194. SSL_HANDSHAKE_MAC_MD5,SSL_HANDSHAKE_MAC_SHA,
  195. SSL_HANDSHAKE_MAC_GOST94, 0, SSL_HANDSHAKE_MAC_SHA256,
  196. SSL_HANDSHAKE_MAC_SHA384
  197. };
  198. #define CIPHER_ADD 1
  199. #define CIPHER_KILL 2
  200. #define CIPHER_DEL 3
  201. #define CIPHER_ORD 4
  202. #define CIPHER_SPECIAL 5
  203. typedef struct cipher_order_st
  204. {
  205. const SSL_CIPHER *cipher;
  206. int active;
  207. int dead;
  208. int in_group;
  209. struct cipher_order_st *next,*prev;
  210. } CIPHER_ORDER;
  211. static const SSL_CIPHER cipher_aliases[]={
  212. /* "ALL" doesn't include eNULL (must be specifically enabled) */
  213. {0,SSL_TXT_ALL,0, 0,0,~SSL_eNULL,0,0,0,0,0,0},
  214. /* "COMPLEMENTOFALL" */
  215. {0,SSL_TXT_CMPALL,0, 0,0,SSL_eNULL,0,0,0,0,0,0},
  216. /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
  217. {0,SSL_TXT_CMPDEF,0, SSL_kEDH|SSL_kEECDH,SSL_aNULL,~SSL_eNULL,0,0,0,0,0,0},
  218. /* key exchange aliases
  219. * (some of those using only a single bit here combine
  220. * multiple key exchange algs according to the RFCs,
  221. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  222. {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
  223. {0,SSL_TXT_kDHr,0, SSL_kDHr, 0,0,0,0,0,0,0,0},
  224. {0,SSL_TXT_kDHd,0, SSL_kDHd, 0,0,0,0,0,0,0,0},
  225. {0,SSL_TXT_kDH,0, SSL_kDHr|SSL_kDHd,0,0,0,0,0,0,0,0},
  226. {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
  227. {0,SSL_TXT_DH,0, SSL_kDHr|SSL_kDHd|SSL_kEDH,0,0,0,0,0,0,0,0},
  228. {0,SSL_TXT_kKRB5,0, SSL_kKRB5, 0,0,0,0,0,0,0,0},
  229. {0,SSL_TXT_kECDHr,0, SSL_kECDHr,0,0,0,0,0,0,0,0},
  230. {0,SSL_TXT_kECDHe,0, SSL_kECDHe,0,0,0,0,0,0,0,0},
  231. {0,SSL_TXT_kECDH,0, SSL_kECDHr|SSL_kECDHe,0,0,0,0,0,0,0,0},
  232. {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
  233. {0,SSL_TXT_ECDH,0, SSL_kECDHr|SSL_kECDHe|SSL_kEECDH,0,0,0,0,0,0,0,0},
  234. {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
  235. {0,SSL_TXT_kSRP,0, SSL_kSRP, 0,0,0,0,0,0,0,0},
  236. {0,SSL_TXT_kGOST,0, SSL_kGOST,0,0,0,0,0,0,0,0},
  237. /* server authentication aliases */
  238. {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
  239. {0,SSL_TXT_aDSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
  240. {0,SSL_TXT_DSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
  241. {0,SSL_TXT_aKRB5,0, 0,SSL_aKRB5, 0,0,0,0,0,0,0},
  242. {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
  243. {0,SSL_TXT_aDH,0, 0,SSL_aDH, 0,0,0,0,0,0,0}, /* no such ciphersuites supported! */
  244. {0,SSL_TXT_aECDH,0, 0,SSL_aECDH, 0,0,0,0,0,0,0},
  245. {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
  246. {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
  247. {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
  248. {0,SSL_TXT_aGOST94,0,0,SSL_aGOST94,0,0,0,0,0,0,0},
  249. {0,SSL_TXT_aGOST01,0,0,SSL_aGOST01,0,0,0,0,0,0,0},
  250. {0,SSL_TXT_aGOST,0,0,SSL_aGOST94|SSL_aGOST01,0,0,0,0,0,0,0},
  251. /* aliases combining key exchange and server authentication */
  252. {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
  253. {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
  254. {0,SSL_TXT_NULL,0, 0,0,SSL_eNULL, 0,0,0,0,0,0},
  255. {0,SSL_TXT_KRB5,0, SSL_kKRB5,SSL_aKRB5,0,0,0,0,0,0,0},
  256. {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
  257. {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
  258. {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
  259. {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
  260. {0,SSL_TXT_SRP,0, SSL_kSRP,0,0,0,0,0,0,0,0},
  261. /* symmetric encryption aliases */
  262. {0,SSL_TXT_DES,0, 0,0,SSL_DES, 0,0,0,0,0,0},
  263. {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
  264. {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
  265. {0,SSL_TXT_RC2,0, 0,0,SSL_RC2, 0,0,0,0,0,0},
  266. {0,SSL_TXT_IDEA,0, 0,0,SSL_IDEA, 0,0,0,0,0,0},
  267. {0,SSL_TXT_SEED,0, 0,0,SSL_SEED, 0,0,0,0,0,0},
  268. {0,SSL_TXT_eNULL,0, 0,0,SSL_eNULL, 0,0,0,0,0,0},
  269. {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
  270. {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
  271. {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
  272. {0,SSL_TXT_AES_GCM,0, 0,0,SSL_AES128GCM|SSL_AES256GCM,0,0,0,0,0,0},
  273. {0,SSL_TXT_CAMELLIA128,0,0,0,SSL_CAMELLIA128,0,0,0,0,0,0},
  274. {0,SSL_TXT_CAMELLIA256,0,0,0,SSL_CAMELLIA256,0,0,0,0,0,0},
  275. {0,SSL_TXT_CAMELLIA ,0,0,0,SSL_CAMELLIA128|SSL_CAMELLIA256,0,0,0,0,0,0},
  276. {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
  277. /* MAC aliases */
  278. {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
  279. {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
  280. {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
  281. {0,SSL_TXT_GOST94,0, 0,0,0,SSL_GOST94, 0,0,0,0,0},
  282. {0,SSL_TXT_GOST89MAC,0, 0,0,0,SSL_GOST89MAC, 0,0,0,0,0},
  283. {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
  284. {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
  285. /* protocol version aliases */
  286. {0,SSL_TXT_SSLV2,0, 0,0,0,0,SSL_SSLV2, 0,0,0,0},
  287. {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
  288. {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
  289. {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
  290. /* export flag */
  291. {0,SSL_TXT_EXP,0, 0,0,0,0,0,SSL_EXPORT,0,0,0},
  292. {0,SSL_TXT_EXPORT,0, 0,0,0,0,0,SSL_EXPORT,0,0,0},
  293. /* strength classes */
  294. {0,SSL_TXT_EXP40,0, 0,0,0,0,0,SSL_EXP40, 0,0,0},
  295. {0,SSL_TXT_EXP56,0, 0,0,0,0,0,SSL_EXP56, 0,0,0},
  296. {0,SSL_TXT_LOW,0, 0,0,0,0,0,SSL_LOW, 0,0,0},
  297. {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
  298. {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
  299. /* FIPS 140-2 approved ciphersuite */
  300. {0,SSL_TXT_FIPS,0, 0,0,~SSL_eNULL,0,0,SSL_FIPS, 0,0,0},
  301. };
  302. void ssl_load_ciphers(void)
  303. {
  304. ssl_cipher_methods[SSL_ENC_DES_IDX]= EVP_des_cbc();
  305. ssl_cipher_methods[SSL_ENC_3DES_IDX]= EVP_des_ede3_cbc();
  306. ssl_cipher_methods[SSL_ENC_RC4_IDX]= EVP_rc4();
  307. ssl_cipher_methods[SSL_ENC_AES128_IDX]= EVP_aes_128_cbc();
  308. ssl_cipher_methods[SSL_ENC_AES256_IDX]= EVP_aes_256_cbc();
  309. ssl_cipher_methods[SSL_ENC_AES128GCM_IDX]= EVP_aes_128_gcm();
  310. ssl_cipher_methods[SSL_ENC_AES256GCM_IDX]= EVP_aes_256_gcm();
  311. ssl_digest_methods[SSL_MD_MD5_IDX]= EVP_md5();
  312. ssl_mac_secret_size[SSL_MD_MD5_IDX]= EVP_MD_size(EVP_md5());
  313. assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
  314. ssl_digest_methods[SSL_MD_SHA1_IDX]=EVP_sha1();
  315. ssl_mac_secret_size[SSL_MD_SHA1_IDX]= EVP_MD_size(EVP_sha1());
  316. assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
  317. ssl_digest_methods[SSL_MD_SHA256_IDX]= EVP_sha256();
  318. ssl_mac_secret_size[SSL_MD_SHA256_IDX]= EVP_MD_size(EVP_sha256());
  319. ssl_digest_methods[SSL_MD_SHA384_IDX]= EVP_sha384();
  320. ssl_mac_secret_size[SSL_MD_SHA384_IDX]= EVP_MD_size(EVP_sha384());
  321. }
  322. /* ssl_cipher_get_comp sets |comp| to the correct SSL_COMP for the given
  323. * session and returns 1. On error it returns 0. */
  324. int ssl_cipher_get_comp(const SSL_SESSION *s, SSL_COMP **comp)
  325. {
  326. size_t index;
  327. SSL_COMP ctmp;
  328. *comp=NULL;
  329. ctmp.id=s->compress_meth;
  330. if (ssl_comp_methods != NULL)
  331. {
  332. if (sk_SSL_COMP_find(ssl_comp_methods, &index, &ctmp))
  333. *comp=sk_SSL_COMP_value(ssl_comp_methods,index);
  334. else
  335. *comp=NULL;
  336. }
  337. return 1;
  338. }
  339. /* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
  340. * for |s->cipher|. It returns 1 on success and 0 on error. */
  341. int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
  342. {
  343. const SSL_CIPHER *c = s->cipher;
  344. *aead = NULL;
  345. if (c == NULL)
  346. return 0;
  347. if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0)
  348. return 0;
  349. #ifndef OPENSSL_NO_AES
  350. switch (c->algorithm_enc)
  351. {
  352. case SSL_AES128GCM:
  353. *aead = EVP_aead_aes_128_gcm();
  354. return 1;
  355. case SSL_AES256GCM:
  356. *aead = EVP_aead_aes_256_gcm();
  357. return 1;
  358. case SSL_CHACHA20POLY1305:
  359. *aead = EVP_aead_chacha20_poly1305();
  360. return 1;
  361. }
  362. #endif
  363. return 0;
  364. }
  365. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  366. const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
  367. {
  368. int i;
  369. const SSL_CIPHER *c;
  370. c=s->cipher;
  371. if (c == NULL) return(0);
  372. /* This function doesn't deal with EVP_AEAD. See
  373. * |ssl_cipher_get_aead_evp|. */
  374. if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
  375. return(0);
  376. if ((enc == NULL) || (md == NULL)) return(0);
  377. switch (c->algorithm_enc)
  378. {
  379. case SSL_DES:
  380. i=SSL_ENC_DES_IDX;
  381. break;
  382. case SSL_3DES:
  383. i=SSL_ENC_3DES_IDX;
  384. break;
  385. case SSL_RC4:
  386. i=SSL_ENC_RC4_IDX;
  387. break;
  388. case SSL_RC2:
  389. i=SSL_ENC_RC2_IDX;
  390. break;
  391. case SSL_IDEA:
  392. i=SSL_ENC_IDEA_IDX;
  393. break;
  394. case SSL_eNULL:
  395. i=SSL_ENC_NULL_IDX;
  396. break;
  397. case SSL_AES128:
  398. i=SSL_ENC_AES128_IDX;
  399. break;
  400. case SSL_AES256:
  401. i=SSL_ENC_AES256_IDX;
  402. break;
  403. case SSL_CAMELLIA128:
  404. i=SSL_ENC_CAMELLIA128_IDX;
  405. break;
  406. case SSL_CAMELLIA256:
  407. i=SSL_ENC_CAMELLIA256_IDX;
  408. break;
  409. case SSL_eGOST2814789CNT:
  410. i=SSL_ENC_GOST89_IDX;
  411. break;
  412. case SSL_SEED:
  413. i=SSL_ENC_SEED_IDX;
  414. break;
  415. case SSL_AES128GCM:
  416. i=SSL_ENC_AES128GCM_IDX;
  417. break;
  418. case SSL_AES256GCM:
  419. i=SSL_ENC_AES256GCM_IDX;
  420. break;
  421. default:
  422. i= -1;
  423. break;
  424. }
  425. if ((i < 0) || (i > SSL_ENC_NUM_IDX))
  426. *enc=NULL;
  427. else
  428. {
  429. if (i == SSL_ENC_NULL_IDX)
  430. *enc = EVP_enc_null();
  431. *enc=ssl_cipher_methods[i];
  432. }
  433. switch (c->algorithm_mac)
  434. {
  435. case SSL_MD5:
  436. i=SSL_MD_MD5_IDX;
  437. break;
  438. case SSL_SHA1:
  439. i=SSL_MD_SHA1_IDX;
  440. break;
  441. case SSL_SHA256:
  442. i=SSL_MD_SHA256_IDX;
  443. break;
  444. case SSL_SHA384:
  445. i=SSL_MD_SHA384_IDX;
  446. break;
  447. case SSL_GOST94:
  448. i = SSL_MD_GOST94_IDX;
  449. break;
  450. case SSL_GOST89MAC:
  451. i = SSL_MD_GOST89MAC_IDX;
  452. break;
  453. default:
  454. i= -1;
  455. break;
  456. }
  457. if ((i < 0) || (i > SSL_MD_NUM_IDX))
  458. {
  459. *md=NULL;
  460. if (mac_pkey_type!=NULL) *mac_pkey_type = NID_undef;
  461. if (mac_secret_size!=NULL) *mac_secret_size = 0;
  462. if (c->algorithm_mac == SSL_AEAD)
  463. mac_pkey_type = NULL;
  464. }
  465. else
  466. {
  467. *md=ssl_digest_methods[i];
  468. if (mac_pkey_type!=NULL) *mac_pkey_type = ssl_mac_pkey_id[i];
  469. if (mac_secret_size!=NULL) *mac_secret_size = ssl_mac_secret_size[i];
  470. }
  471. if ((*enc != NULL) &&
  472. (*md != NULL || (EVP_CIPHER_flags(*enc)&EVP_CIPH_FLAG_AEAD_CIPHER)) &&
  473. (!mac_pkey_type||*mac_pkey_type != NID_undef))
  474. {
  475. if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
  476. s->ssl_version < TLS1_VERSION)
  477. return 1;
  478. #ifdef OPENSSL_FIPS
  479. if (FIPS_mode())
  480. return 1;
  481. #endif
  482. /* TODO(fork): enable the stitched cipher modes. */
  483. #if 0
  484. if (c->algorithm_enc == SSL_RC4 &&
  485. c->algorithm_mac == SSL_MD5 &&
  486. (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
  487. *enc = evp, *md = NULL;
  488. else if (c->algorithm_enc == SSL_AES128 &&
  489. c->algorithm_mac == SSL_SHA1 &&
  490. (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
  491. *enc = evp, *md = NULL;
  492. else if (c->algorithm_enc == SSL_AES256 &&
  493. c->algorithm_mac == SSL_SHA1 &&
  494. (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
  495. *enc = evp, *md = NULL;
  496. #endif
  497. return(1);
  498. }
  499. else
  500. return(0);
  501. }
  502. int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
  503. {
  504. if (idx <0||idx>=SSL_MD_NUM_IDX)
  505. {
  506. return 0;
  507. }
  508. *mask = ssl_handshake_digest_flag[idx];
  509. if (*mask)
  510. *md = ssl_digest_methods[idx];
  511. else
  512. *md = NULL;
  513. return 1;
  514. }
  515. #define ITEM_SEP(a) \
  516. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  517. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  518. CIPHER_ORDER **tail)
  519. {
  520. if (curr == *tail) return;
  521. if (curr == *head)
  522. *head=curr->next;
  523. if (curr->prev != NULL)
  524. curr->prev->next=curr->next;
  525. if (curr->next != NULL)
  526. curr->next->prev=curr->prev;
  527. (*tail)->next=curr;
  528. curr->prev= *tail;
  529. curr->next=NULL;
  530. *tail=curr;
  531. }
  532. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  533. CIPHER_ORDER **tail)
  534. {
  535. if (curr == *head) return;
  536. if (curr == *tail)
  537. *tail=curr->prev;
  538. if (curr->next != NULL)
  539. curr->next->prev=curr->prev;
  540. if (curr->prev != NULL)
  541. curr->prev->next=curr->next;
  542. (*head)->prev=curr;
  543. curr->next= *head;
  544. curr->prev=NULL;
  545. *head=curr;
  546. }
  547. static void ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth, unsigned long *enc, unsigned long *mac, unsigned long *ssl)
  548. {
  549. *mkey = 0;
  550. *auth = 0;
  551. *enc = 0;
  552. *mac = 0;
  553. *ssl = 0;
  554. #ifdef OPENSSL_NO_RSA
  555. *mkey |= SSL_kRSA;
  556. *auth |= SSL_aRSA;
  557. #endif
  558. #ifdef OPENSSL_NO_DSA
  559. *auth |= SSL_aDSS;
  560. #endif
  561. #ifdef OPENSSL_NO_DH
  562. *mkey |= SSL_kDHr|SSL_kDHd|SSL_kEDH;
  563. *auth |= SSL_aDH;
  564. #endif
  565. #ifdef OPENSSL_NO_ECDSA
  566. *auth |= SSL_aECDSA;
  567. #endif
  568. #ifdef OPENSSL_NO_ECDH
  569. *mkey |= SSL_kECDHe|SSL_kECDHr;
  570. *auth |= SSL_aECDH;
  571. #endif
  572. #ifdef OPENSSL_NO_PSK
  573. *mkey |= SSL_kPSK;
  574. *auth |= SSL_aPSK;
  575. #endif
  576. #ifdef SSL_FORBID_ENULL
  577. *enc |= SSL_eNULL;
  578. #endif
  579. *enc |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
  580. *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
  581. *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
  582. *enc |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
  583. *enc |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
  584. *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128:0;
  585. *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256:0;
  586. *enc |= (ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] == NULL) ? SSL_AES128GCM:0;
  587. *enc |= (ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] == NULL) ? SSL_AES256GCM:0;
  588. *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA128:0;
  589. *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] == NULL) ? SSL_CAMELLIA256:0;
  590. *enc |= (ssl_cipher_methods[SSL_ENC_GOST89_IDX] == NULL) ? SSL_eGOST2814789CNT:0;
  591. *enc |= (ssl_cipher_methods[SSL_ENC_SEED_IDX] == NULL) ? SSL_SEED:0;
  592. *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
  593. *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
  594. *mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256:0;
  595. *mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384:0;
  596. *mac |= (ssl_digest_methods[SSL_MD_GOST94_IDX] == NULL) ? SSL_GOST94:0;
  597. *mac |= (ssl_digest_methods[SSL_MD_GOST89MAC_IDX] == NULL || ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]==NID_undef)? SSL_GOST89MAC:0;
  598. }
  599. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  600. int num_of_ciphers,
  601. unsigned long disabled_mkey, unsigned long disabled_auth,
  602. unsigned long disabled_enc, unsigned long disabled_mac,
  603. unsigned long disabled_ssl,
  604. CIPHER_ORDER *co_list,
  605. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  606. {
  607. int i, co_list_num;
  608. const SSL_CIPHER *c;
  609. /*
  610. * We have num_of_ciphers descriptions compiled in, depending on the
  611. * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
  612. * These will later be sorted in a linked list with at most num
  613. * entries.
  614. */
  615. /* Get the initial list of ciphers */
  616. co_list_num = 0; /* actual count of ciphers */
  617. for (i = 0; i < num_of_ciphers; i++)
  618. {
  619. c = ssl_method->get_cipher(i);
  620. /* drop those that use any of that is not available */
  621. if ((c != NULL) && c->valid &&
  622. #ifdef OPENSSL_FIPS
  623. (!FIPS_mode() || (c->algo_strength & SSL_FIPS)) &&
  624. #endif
  625. !(c->algorithm_mkey & disabled_mkey) &&
  626. !(c->algorithm_auth & disabled_auth) &&
  627. !(c->algorithm_enc & disabled_enc) &&
  628. !(c->algorithm_mac & disabled_mac) &&
  629. !(c->algorithm_ssl & disabled_ssl))
  630. {
  631. co_list[co_list_num].cipher = c;
  632. co_list[co_list_num].next = NULL;
  633. co_list[co_list_num].prev = NULL;
  634. co_list[co_list_num].active = 0;
  635. co_list[co_list_num].in_group = 0;
  636. co_list_num++;
  637. #ifdef KSSL_DEBUG
  638. printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
  639. #endif /* KSSL_DEBUG */
  640. /*
  641. if (!sk_push(ca_list,(char *)c)) goto err;
  642. */
  643. }
  644. }
  645. /*
  646. * Prepare linked list from list entries
  647. */
  648. if (co_list_num > 0)
  649. {
  650. co_list[0].prev = NULL;
  651. if (co_list_num > 1)
  652. {
  653. co_list[0].next = &co_list[1];
  654. for (i = 1; i < co_list_num - 1; i++)
  655. {
  656. co_list[i].prev = &co_list[i - 1];
  657. co_list[i].next = &co_list[i + 1];
  658. }
  659. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  660. }
  661. co_list[co_list_num - 1].next = NULL;
  662. *head_p = &co_list[0];
  663. *tail_p = &co_list[co_list_num - 1];
  664. }
  665. }
  666. static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
  667. int num_of_group_aliases,
  668. unsigned long disabled_mkey, unsigned long disabled_auth,
  669. unsigned long disabled_enc, unsigned long disabled_mac,
  670. unsigned long disabled_ssl,
  671. CIPHER_ORDER *head)
  672. {
  673. CIPHER_ORDER *ciph_curr;
  674. const SSL_CIPHER **ca_curr;
  675. int i;
  676. unsigned long mask_mkey = ~disabled_mkey;
  677. unsigned long mask_auth = ~disabled_auth;
  678. unsigned long mask_enc = ~disabled_enc;
  679. unsigned long mask_mac = ~disabled_mac;
  680. unsigned long mask_ssl = ~disabled_ssl;
  681. /*
  682. * First, add the real ciphers as already collected
  683. */
  684. ciph_curr = head;
  685. ca_curr = ca_list;
  686. while (ciph_curr != NULL)
  687. {
  688. *ca_curr = ciph_curr->cipher;
  689. ca_curr++;
  690. ciph_curr = ciph_curr->next;
  691. }
  692. /*
  693. * Now we add the available ones from the cipher_aliases[] table.
  694. * They represent either one or more algorithms, some of which
  695. * in any affected category must be supported (set in enabled_mask),
  696. * or represent a cipher strength value (will be added in any case because algorithms=0).
  697. */
  698. for (i = 0; i < num_of_group_aliases; i++)
  699. {
  700. unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
  701. unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
  702. unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
  703. unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
  704. unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
  705. if (algorithm_mkey)
  706. if ((algorithm_mkey & mask_mkey) == 0)
  707. continue;
  708. if (algorithm_auth)
  709. if ((algorithm_auth & mask_auth) == 0)
  710. continue;
  711. if (algorithm_enc)
  712. if ((algorithm_enc & mask_enc) == 0)
  713. continue;
  714. if (algorithm_mac)
  715. if ((algorithm_mac & mask_mac) == 0)
  716. continue;
  717. if (algorithm_ssl)
  718. if ((algorithm_ssl & mask_ssl) == 0)
  719. continue;
  720. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  721. ca_curr++;
  722. }
  723. *ca_curr = NULL; /* end of list */
  724. }
  725. static void ssl_cipher_apply_rule(unsigned long cipher_id,
  726. unsigned long alg_mkey, unsigned long alg_auth,
  727. unsigned long alg_enc, unsigned long alg_mac,
  728. unsigned long alg_ssl,
  729. unsigned long algo_strength,
  730. int rule, int strength_bits, int in_group,
  731. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  732. {
  733. CIPHER_ORDER *head, *tail, *curr, *curr2, *last;
  734. const SSL_CIPHER *cp;
  735. int reverse = 0;
  736. #ifdef CIPHER_DEBUG
  737. printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
  738. rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
  739. #endif
  740. if (rule == CIPHER_DEL)
  741. reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
  742. head = *head_p;
  743. tail = *tail_p;
  744. if (reverse)
  745. {
  746. curr = tail;
  747. last = head;
  748. }
  749. else
  750. {
  751. curr = head;
  752. last = tail;
  753. }
  754. curr2 = curr;
  755. for (;;)
  756. {
  757. if ((curr == NULL) || (curr == last)) break;
  758. curr = curr2;
  759. curr2 = reverse ? curr->prev : curr->next;
  760. cp = curr->cipher;
  761. /*
  762. * Selection criteria is either the value of strength_bits
  763. * or the algorithms used.
  764. */
  765. if (strength_bits >= 0)
  766. {
  767. if (strength_bits != cp->strength_bits)
  768. continue;
  769. }
  770. else
  771. {
  772. #ifdef CIPHER_DEBUG
  773. printf("\nName: %s:\nAlgo = %08lx/%08lx/%08lx/%08lx/%08lx Algo_strength = %08lx\n", cp->name, cp->algorithm_mkey, cp->algorithm_auth, cp->algorithm_enc, cp->algorithm_mac, cp->algorithm_ssl, cp->algo_strength);
  774. #endif
  775. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  776. if (cipher_id && cipher_id != cp->id)
  777. continue;
  778. #endif
  779. if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
  780. continue;
  781. if (alg_auth && !(alg_auth & cp->algorithm_auth))
  782. continue;
  783. if (alg_enc && !(alg_enc & cp->algorithm_enc))
  784. continue;
  785. if (alg_mac && !(alg_mac & cp->algorithm_mac))
  786. continue;
  787. if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
  788. continue;
  789. if ((algo_strength & SSL_EXP_MASK) && !(algo_strength & SSL_EXP_MASK & cp->algo_strength))
  790. continue;
  791. if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
  792. continue;
  793. }
  794. #ifdef CIPHER_DEBUG
  795. printf("Action = %d\n", rule);
  796. #endif
  797. /* add the cipher if it has not been added yet. */
  798. if (rule == CIPHER_ADD)
  799. {
  800. /* reverse == 0 */
  801. if (!curr->active)
  802. {
  803. ll_append_tail(&head, curr, &tail);
  804. curr->active = 1;
  805. curr->in_group = in_group;
  806. }
  807. }
  808. /* Move the added cipher to this location */
  809. else if (rule == CIPHER_ORD)
  810. {
  811. /* reverse == 0 */
  812. if (curr->active)
  813. {
  814. ll_append_tail(&head, curr, &tail);
  815. curr->in_group = 0;
  816. }
  817. }
  818. else if (rule == CIPHER_DEL)
  819. {
  820. /* reverse == 1 */
  821. if (curr->active)
  822. {
  823. /* most recently deleted ciphersuites get best positions
  824. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  825. * works in reverse to maintain the order) */
  826. ll_append_head(&head, curr, &tail);
  827. curr->active = 0;
  828. curr->in_group = 0;
  829. }
  830. }
  831. else if (rule == CIPHER_KILL)
  832. {
  833. /* reverse == 0 */
  834. if (head == curr)
  835. head = curr->next;
  836. else
  837. curr->prev->next = curr->next;
  838. if (tail == curr)
  839. tail = curr->prev;
  840. curr->active = 0;
  841. if (curr->next != NULL)
  842. curr->next->prev = curr->prev;
  843. if (curr->prev != NULL)
  844. curr->prev->next = curr->next;
  845. curr->next = NULL;
  846. curr->prev = NULL;
  847. }
  848. }
  849. *head_p = head;
  850. *tail_p = tail;
  851. }
  852. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  853. CIPHER_ORDER **tail_p)
  854. {
  855. int max_strength_bits, i, *number_uses;
  856. CIPHER_ORDER *curr;
  857. /*
  858. * This routine sorts the ciphers with descending strength. The sorting
  859. * must keep the pre-sorted sequence, so we apply the normal sorting
  860. * routine as '+' movement to the end of the list.
  861. */
  862. max_strength_bits = 0;
  863. curr = *head_p;
  864. while (curr != NULL)
  865. {
  866. if (curr->active &&
  867. (curr->cipher->strength_bits > max_strength_bits))
  868. max_strength_bits = curr->cipher->strength_bits;
  869. curr = curr->next;
  870. }
  871. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  872. if (!number_uses)
  873. {
  874. OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
  875. return(0);
  876. }
  877. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  878. /*
  879. * Now find the strength_bits values actually used
  880. */
  881. curr = *head_p;
  882. while (curr != NULL)
  883. {
  884. if (curr->active)
  885. number_uses[curr->cipher->strength_bits]++;
  886. curr = curr->next;
  887. }
  888. /*
  889. * Go through the list of used strength_bits values in descending
  890. * order.
  891. */
  892. for (i = max_strength_bits; i >= 0; i--)
  893. if (number_uses[i] > 0)
  894. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
  895. OPENSSL_free(number_uses);
  896. return(1);
  897. }
  898. static int ssl_cipher_process_rulestr(const char *rule_str,
  899. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
  900. const SSL_CIPHER **ca_list)
  901. {
  902. unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
  903. const char *l, *buf;
  904. int j, multi, found, rule, retval, ok, buflen, in_group = 0,
  905. has_group = 0;
  906. unsigned long cipher_id = 0;
  907. char ch;
  908. retval = 1;
  909. l = rule_str;
  910. for (;;)
  911. {
  912. ch = *l;
  913. if (ch == '\0')
  914. break; /* done */
  915. if (in_group)
  916. {
  917. if (ch == ']')
  918. {
  919. if (!in_group)
  920. {
  921. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
  922. retval = found = in_group = 0;
  923. break;
  924. }
  925. if (*tail_p)
  926. (*tail_p)->in_group = 0;
  927. in_group = 0;
  928. l++;
  929. continue;
  930. }
  931. if (ch == '|')
  932. { rule = CIPHER_ADD; l++; continue; }
  933. else if (!(ch >= 'a' && ch <= 'z') &&
  934. !(ch >= 'A' && ch <= 'Z') &&
  935. !(ch >= '0' && ch <= '9'))
  936. {
  937. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  938. retval = found = in_group = 0;
  939. break;
  940. }
  941. else
  942. rule = CIPHER_ADD;
  943. }
  944. else if (ch == '-')
  945. { rule = CIPHER_DEL; l++; }
  946. else if (ch == '+')
  947. { rule = CIPHER_ORD; l++; }
  948. else if (ch == '!' && has_group)
  949. {
  950. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  951. retval = found = in_group = 0;
  952. break;
  953. }
  954. else if (ch == '!')
  955. { rule = CIPHER_KILL; l++; }
  956. else if (ch == '@' && has_group)
  957. {
  958. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  959. retval = found = in_group = 0;
  960. break;
  961. }
  962. else if (ch == '@')
  963. { rule = CIPHER_SPECIAL; l++; }
  964. else if (ch == '[')
  965. {
  966. if (in_group)
  967. {
  968. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
  969. retval = found = in_group = 0;
  970. break;
  971. }
  972. in_group = 1;
  973. has_group = 1;
  974. l++;
  975. continue;
  976. }
  977. else
  978. { rule = CIPHER_ADD; }
  979. if (ITEM_SEP(ch))
  980. {
  981. l++;
  982. continue;
  983. }
  984. alg_mkey = 0;
  985. alg_auth = 0;
  986. alg_enc = 0;
  987. alg_mac = 0;
  988. alg_ssl = 0;
  989. algo_strength = 0;
  990. for (;;)
  991. {
  992. ch = *l;
  993. buf = l;
  994. buflen = 0;
  995. #ifndef CHARSET_EBCDIC
  996. while ( ((ch >= 'A') && (ch <= 'Z')) ||
  997. ((ch >= '0') && (ch <= '9')) ||
  998. ((ch >= 'a') && (ch <= 'z')) ||
  999. (ch == '-') || (ch == '.'))
  1000. #else
  1001. while ( isalnum(ch) || (ch == '-') || (ch == '.'))
  1002. #endif
  1003. {
  1004. ch = *(++l);
  1005. buflen++;
  1006. }
  1007. if (buflen == 0)
  1008. {
  1009. /*
  1010. * We hit something we cannot deal with,
  1011. * it is no command or separator nor
  1012. * alphanumeric, so we call this an error.
  1013. */
  1014. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  1015. retval = found = in_group = 0;
  1016. l++;
  1017. break;
  1018. }
  1019. if (rule == CIPHER_SPECIAL)
  1020. {
  1021. found = 0; /* unused -- avoid compiler warning */
  1022. break; /* special treatment */
  1023. }
  1024. /* check for multi-part specification */
  1025. if (ch == '+')
  1026. {
  1027. multi=1;
  1028. l++;
  1029. }
  1030. else
  1031. multi=0;
  1032. /*
  1033. * Now search for the cipher alias in the ca_list. Be careful
  1034. * with the strncmp, because the "buflen" limitation
  1035. * will make the rule "ADH:SOME" and the cipher
  1036. * "ADH-MY-CIPHER" look like a match for buflen=3.
  1037. * So additionally check whether the cipher name found
  1038. * has the correct length. We can save a strlen() call:
  1039. * just checking for the '\0' at the right place is
  1040. * sufficient, we have to strncmp() anyway. (We cannot
  1041. * use strcmp(), because buf is not '\0' terminated.)
  1042. */
  1043. j = found = 0;
  1044. cipher_id = 0;
  1045. while (ca_list[j])
  1046. {
  1047. if (!strncmp(buf, ca_list[j]->name, buflen) &&
  1048. (ca_list[j]->name[buflen] == '\0'))
  1049. {
  1050. found = 1;
  1051. break;
  1052. }
  1053. else
  1054. j++;
  1055. }
  1056. if (!found)
  1057. break; /* ignore this entry */
  1058. if (ca_list[j]->algorithm_mkey)
  1059. {
  1060. if (alg_mkey)
  1061. {
  1062. alg_mkey &= ca_list[j]->algorithm_mkey;
  1063. if (!alg_mkey) { found = 0; break; }
  1064. }
  1065. else
  1066. alg_mkey = ca_list[j]->algorithm_mkey;
  1067. }
  1068. if (ca_list[j]->algorithm_auth)
  1069. {
  1070. if (alg_auth)
  1071. {
  1072. alg_auth &= ca_list[j]->algorithm_auth;
  1073. if (!alg_auth) { found = 0; break; }
  1074. }
  1075. else
  1076. alg_auth = ca_list[j]->algorithm_auth;
  1077. }
  1078. if (ca_list[j]->algorithm_enc)
  1079. {
  1080. if (alg_enc)
  1081. {
  1082. alg_enc &= ca_list[j]->algorithm_enc;
  1083. if (!alg_enc) { found = 0; break; }
  1084. }
  1085. else
  1086. alg_enc = ca_list[j]->algorithm_enc;
  1087. }
  1088. if (ca_list[j]->algorithm_mac)
  1089. {
  1090. if (alg_mac)
  1091. {
  1092. alg_mac &= ca_list[j]->algorithm_mac;
  1093. if (!alg_mac) { found = 0; break; }
  1094. }
  1095. else
  1096. alg_mac = ca_list[j]->algorithm_mac;
  1097. }
  1098. if (ca_list[j]->algo_strength & SSL_EXP_MASK)
  1099. {
  1100. if (algo_strength & SSL_EXP_MASK)
  1101. {
  1102. algo_strength &= (ca_list[j]->algo_strength & SSL_EXP_MASK) | ~SSL_EXP_MASK;
  1103. if (!(algo_strength & SSL_EXP_MASK)) { found = 0; break; }
  1104. }
  1105. else
  1106. algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
  1107. }
  1108. if (ca_list[j]->algo_strength & SSL_STRONG_MASK)
  1109. {
  1110. if (algo_strength & SSL_STRONG_MASK)
  1111. {
  1112. algo_strength &= (ca_list[j]->algo_strength & SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
  1113. if (!(algo_strength & SSL_STRONG_MASK)) { found = 0; break; }
  1114. }
  1115. else
  1116. algo_strength |= ca_list[j]->algo_strength & SSL_STRONG_MASK;
  1117. }
  1118. if (ca_list[j]->valid)
  1119. {
  1120. /* explicit ciphersuite found; its protocol version
  1121. * does not become part of the search pattern!*/
  1122. cipher_id = ca_list[j]->id;
  1123. }
  1124. else
  1125. {
  1126. /* not an explicit ciphersuite; only in this case, the
  1127. * protocol version is considered part of the search pattern */
  1128. if (ca_list[j]->algorithm_ssl)
  1129. {
  1130. if (alg_ssl)
  1131. {
  1132. alg_ssl &= ca_list[j]->algorithm_ssl;
  1133. if (!alg_ssl) { found = 0; break; }
  1134. }
  1135. else
  1136. alg_ssl = ca_list[j]->algorithm_ssl;
  1137. }
  1138. }
  1139. if (!multi) break;
  1140. }
  1141. /*
  1142. * Ok, we have the rule, now apply it
  1143. */
  1144. if (rule == CIPHER_SPECIAL)
  1145. { /* special command */
  1146. ok = 0;
  1147. if ((buflen == 8) &&
  1148. !strncmp(buf, "STRENGTH", 8))
  1149. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1150. else
  1151. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  1152. if (ok == 0)
  1153. retval = 0;
  1154. /*
  1155. * We do not support any "multi" options
  1156. * together with "@", so throw away the
  1157. * rest of the command, if any left, until
  1158. * end or ':' is found.
  1159. */
  1160. while ((*l != '\0') && !ITEM_SEP(*l))
  1161. l++;
  1162. }
  1163. else if (found)
  1164. {
  1165. ssl_cipher_apply_rule(cipher_id,
  1166. alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
  1167. rule, -1, in_group, head_p, tail_p);
  1168. }
  1169. else
  1170. {
  1171. while ((*l != '\0') && !ITEM_SEP(*l))
  1172. l++;
  1173. }
  1174. if (*l == '\0') break; /* done */
  1175. }
  1176. if (in_group)
  1177. {
  1178. OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
  1179. retval = 0;
  1180. }
  1181. return(retval);
  1182. }
  1183. #ifndef OPENSSL_NO_EC
  1184. static int check_suiteb_cipher_list(const SSL_METHOD *meth, CERT *c,
  1185. const char **prule_str)
  1186. {
  1187. unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
  1188. if (!strcmp(*prule_str, "SUITEB128"))
  1189. suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
  1190. else if (!strcmp(*prule_str, "SUITEB128ONLY"))
  1191. suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
  1192. else if (!strcmp(*prule_str, "SUITEB128C2"))
  1193. {
  1194. suiteb_comb2 = 1;
  1195. suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
  1196. }
  1197. else if (!strcmp(*prule_str, "SUITEB192"))
  1198. suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
  1199. if (suiteb_flags)
  1200. {
  1201. c->cert_flags &= ~SSL_CERT_FLAG_SUITEB_128_LOS;
  1202. c->cert_flags |= suiteb_flags;
  1203. }
  1204. else
  1205. suiteb_flags = c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS;
  1206. if (!suiteb_flags)
  1207. return 1;
  1208. /* Check version: if TLS 1.2 ciphers allowed we can use Suite B */
  1209. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS))
  1210. {
  1211. if (meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)
  1212. OPENSSL_PUT_ERROR(SSL, check_suiteb_cipher_list, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
  1213. else
  1214. OPENSSL_PUT_ERROR(SSL, check_suiteb_cipher_list, SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
  1215. return 0;
  1216. }
  1217. switch(suiteb_flags)
  1218. {
  1219. case SSL_CERT_FLAG_SUITEB_128_LOS:
  1220. if (suiteb_comb2)
  1221. *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
  1222. else
  1223. *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384";
  1224. break;
  1225. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  1226. *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256";
  1227. break;
  1228. case SSL_CERT_FLAG_SUITEB_192_LOS:
  1229. *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
  1230. break;
  1231. }
  1232. /* Set auto ECDH parameter determination */
  1233. c->ecdh_tmp_auto = 1;
  1234. return 1;
  1235. }
  1236. #endif
  1237. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  1238. struct ssl_cipher_preference_list_st **cipher_list,
  1239. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  1240. const char *rule_str, CERT *c)
  1241. {
  1242. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  1243. unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
  1244. STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
  1245. const char *rule_p;
  1246. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1247. const SSL_CIPHER **ca_list = NULL;
  1248. unsigned char *in_group_flags = NULL;
  1249. unsigned int num_in_group_flags = 0;
  1250. struct ssl_cipher_preference_list_st *pref_list = NULL;
  1251. /*
  1252. * Return with error if nothing to do.
  1253. */
  1254. if (rule_str == NULL || cipher_list == NULL)
  1255. return NULL;
  1256. #ifndef OPENSSL_NO_EC
  1257. if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
  1258. return NULL;
  1259. #endif
  1260. /*
  1261. * To reduce the work to do we only want to process the compiled
  1262. * in algorithms, so we first get the mask of disabled ciphers.
  1263. */
  1264. ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
  1265. /*
  1266. * Now we have to collect the available ciphers from the compiled
  1267. * in ciphers. We cannot get more than the number compiled in, so
  1268. * it is used for allocation.
  1269. */
  1270. num_of_ciphers = ssl_method->num_ciphers();
  1271. #ifdef KSSL_DEBUG
  1272. printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
  1273. #endif /* KSSL_DEBUG */
  1274. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  1275. if (co_list == NULL)
  1276. {
  1277. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  1278. return(NULL); /* Failure */
  1279. }
  1280. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
  1281. disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
  1282. co_list, &head, &tail);
  1283. /* Now arrange all ciphers by preference: */
  1284. /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
  1285. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  1286. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
  1287. /* AES is our preferred symmetric cipher */
  1288. ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  1289. /* Temporarily enable everything else for sorting */
  1290. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
  1291. /* Low priority for MD5 */
  1292. ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1293. /* Move anonymous ciphers to the end. Usually, these will remain disabled.
  1294. * (For applications that allow them, they aren't too bad, but we prefer
  1295. * authenticated ciphers.) */
  1296. ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1297. /* Move ciphers without forward secrecy to the end */
  1298. ssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1299. /* ssl_cipher_apply_rule(0, 0, SSL_aDH, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail); */
  1300. ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1301. ssl_cipher_apply_rule(0, SSL_kPSK, 0,0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1302. ssl_cipher_apply_rule(0, SSL_kKRB5, 0,0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1303. /* RC4 is sort-of broken -- move the the end */
  1304. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
  1305. /* Now sort by symmetric encryption strength. The above ordering remains
  1306. * in force within each class */
  1307. if (!ssl_cipher_strength_sort(&head, &tail))
  1308. {
  1309. OPENSSL_free(co_list);
  1310. return NULL;
  1311. }
  1312. /* Now disable everything (maintaining the ordering!) */
  1313. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
  1314. /*
  1315. * We also need cipher aliases for selecting based on the rule_str.
  1316. * There might be two types of entries in the rule_str: 1) names
  1317. * of ciphers themselves 2) aliases for groups of ciphers.
  1318. * For 1) we need the available ciphers and for 2) the cipher
  1319. * groups of cipher_aliases added together in one list (otherwise
  1320. * we would be happy with just the cipher_aliases table).
  1321. */
  1322. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  1323. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  1324. ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
  1325. if (ca_list == NULL)
  1326. {
  1327. OPENSSL_free(co_list);
  1328. OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
  1329. return(NULL); /* Failure */
  1330. }
  1331. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
  1332. disabled_mkey, disabled_auth, disabled_enc,
  1333. disabled_mac, disabled_ssl, head);
  1334. /*
  1335. * If the rule_string begins with DEFAULT, apply the default rule
  1336. * before using the (possibly available) additional rules.
  1337. */
  1338. ok = 1;
  1339. rule_p = rule_str;
  1340. if (strncmp(rule_str,"DEFAULT",7) == 0)
  1341. {
  1342. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  1343. &head, &tail, ca_list);
  1344. rule_p += 7;
  1345. if (*rule_p == ':')
  1346. rule_p++;
  1347. }
  1348. if (ok && (strlen(rule_p) > 0))
  1349. ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
  1350. OPENSSL_free((void *)ca_list); /* Not needed anymore */
  1351. if (!ok)
  1352. goto err;
  1353. /*
  1354. * Allocate new "cipherstack" for the result, return with error
  1355. * if we cannot get one.
  1356. */
  1357. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
  1358. goto err;
  1359. in_group_flags = OPENSSL_malloc(num_of_ciphers);
  1360. if (!in_group_flags)
  1361. goto err;
  1362. /*
  1363. * The cipher selection for the list is done. The ciphers are added
  1364. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1365. */
  1366. for (curr = head; curr != NULL; curr = curr->next)
  1367. {
  1368. #ifdef OPENSSL_FIPS
  1369. if (curr->active && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
  1370. #else
  1371. if (curr->active)
  1372. #endif
  1373. {
  1374. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  1375. in_group_flags[num_in_group_flags++] = curr->in_group;
  1376. #ifdef CIPHER_DEBUG
  1377. printf("<%s>\n",curr->cipher->name);
  1378. #endif
  1379. }
  1380. }
  1381. OPENSSL_free(co_list); /* Not needed any longer */
  1382. co_list = NULL;
  1383. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1384. if (tmp_cipher_list == NULL)
  1385. goto err;
  1386. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1387. if (!pref_list)
  1388. goto err;
  1389. pref_list->ciphers = cipherstack;
  1390. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1391. if (!pref_list->in_group_flags)
  1392. goto err;
  1393. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1394. OPENSSL_free(in_group_flags);
  1395. in_group_flags = NULL;
  1396. if (*cipher_list != NULL)
  1397. ssl_cipher_preference_list_free(*cipher_list);
  1398. *cipher_list = pref_list;
  1399. pref_list = NULL;
  1400. if (cipher_list_by_id != NULL)
  1401. {
  1402. if (*cipher_list_by_id != NULL)
  1403. sk_SSL_CIPHER_free(*cipher_list_by_id);
  1404. *cipher_list_by_id = tmp_cipher_list;
  1405. tmp_cipher_list = NULL;
  1406. (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
  1407. sk_SSL_CIPHER_sort(*cipher_list_by_id);
  1408. }
  1409. else
  1410. {
  1411. sk_SSL_CIPHER_free(tmp_cipher_list);
  1412. tmp_cipher_list = NULL;
  1413. }
  1414. return(cipherstack);
  1415. err:
  1416. if (co_list)
  1417. OPENSSL_free(co_list);
  1418. if (in_group_flags)
  1419. OPENSSL_free(in_group_flags);
  1420. if (cipherstack)
  1421. sk_SSL_CIPHER_free(cipherstack);
  1422. if (tmp_cipher_list)
  1423. sk_SSL_CIPHER_free(tmp_cipher_list);
  1424. if (pref_list && pref_list->in_group_flags)
  1425. OPENSSL_free(pref_list->in_group_flags);
  1426. if (pref_list)
  1427. OPENSSL_free(pref_list);
  1428. return NULL;
  1429. }
  1430. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
  1431. {
  1432. int is_export,pkl,kl;
  1433. const char *ver,*exp_str;
  1434. const char *kx,*au,*enc,*mac;
  1435. unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl,alg2;
  1436. #ifdef KSSL_DEBUG
  1437. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx/%lx/%lx/%lx/%lx\n";
  1438. #else
  1439. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
  1440. #endif /* KSSL_DEBUG */
  1441. alg_mkey = cipher->algorithm_mkey;
  1442. alg_auth = cipher->algorithm_auth;
  1443. alg_enc = cipher->algorithm_enc;
  1444. alg_mac = cipher->algorithm_mac;
  1445. alg_ssl = cipher->algorithm_ssl;
  1446. alg2=cipher->algorithm2;
  1447. is_export=SSL_C_IS_EXPORT(cipher);
  1448. pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  1449. kl=SSL_C_EXPORT_KEYLENGTH(cipher);
  1450. exp_str=is_export?" export":"";
  1451. if (alg_ssl & SSL_SSLV2)
  1452. ver="SSLv2";
  1453. else if (alg_ssl & SSL_SSLV3)
  1454. ver="SSLv3";
  1455. else if (alg_ssl & SSL_TLSV1_2)
  1456. ver="TLSv1.2";
  1457. else
  1458. ver="unknown";
  1459. switch (alg_mkey)
  1460. {
  1461. case SSL_kRSA:
  1462. kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
  1463. break;
  1464. case SSL_kDHr:
  1465. kx="DH/RSA";
  1466. break;
  1467. case SSL_kDHd:
  1468. kx="DH/DSS";
  1469. break;
  1470. case SSL_kKRB5:
  1471. kx="KRB5";
  1472. break;
  1473. case SSL_kEDH:
  1474. kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
  1475. break;
  1476. case SSL_kECDHr:
  1477. kx="ECDH/RSA";
  1478. break;
  1479. case SSL_kECDHe:
  1480. kx="ECDH/ECDSA";
  1481. break;
  1482. case SSL_kEECDH:
  1483. kx="ECDH";
  1484. break;
  1485. case SSL_kPSK:
  1486. kx="PSK";
  1487. break;
  1488. case SSL_kSRP:
  1489. kx="SRP";
  1490. break;
  1491. default:
  1492. kx="unknown";
  1493. }
  1494. switch (alg_auth)
  1495. {
  1496. case SSL_aRSA:
  1497. au="RSA";
  1498. break;
  1499. case SSL_aDSS:
  1500. au="DSS";
  1501. break;
  1502. case SSL_aDH:
  1503. au="DH";
  1504. break;
  1505. case SSL_aKRB5:
  1506. au="KRB5";
  1507. break;
  1508. case SSL_aECDH:
  1509. au="ECDH";
  1510. break;
  1511. case SSL_aNULL:
  1512. au="None";
  1513. break;
  1514. case SSL_aECDSA:
  1515. au="ECDSA";
  1516. break;
  1517. case SSL_aPSK:
  1518. au="PSK";
  1519. break;
  1520. default:
  1521. au="unknown";
  1522. break;
  1523. }
  1524. switch (alg_enc)
  1525. {
  1526. case SSL_DES:
  1527. enc=(is_export && kl == 5)?"DES(40)":"DES(56)";
  1528. break;
  1529. case SSL_3DES:
  1530. enc="3DES(168)";
  1531. break;
  1532. case SSL_RC4:
  1533. enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
  1534. :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
  1535. break;
  1536. case SSL_RC2:
  1537. enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
  1538. break;
  1539. case SSL_IDEA:
  1540. enc="IDEA(128)";
  1541. break;
  1542. case SSL_eNULL:
  1543. enc="None";
  1544. break;
  1545. case SSL_AES128:
  1546. enc="AES(128)";
  1547. break;
  1548. case SSL_AES256:
  1549. enc="AES(256)";
  1550. break;
  1551. case SSL_AES128GCM:
  1552. enc="AESGCM(128)";
  1553. break;
  1554. case SSL_AES256GCM:
  1555. enc="AESGCM(256)";
  1556. break;
  1557. case SSL_CAMELLIA128:
  1558. enc="Camellia(128)";
  1559. break;
  1560. case SSL_CAMELLIA256:
  1561. enc="Camellia(256)";
  1562. break;
  1563. case SSL_SEED:
  1564. enc="SEED(128)";
  1565. break;
  1566. case SSL_CHACHA20POLY1305:
  1567. enc="ChaCha20-Poly1305";
  1568. break;
  1569. default:
  1570. enc="unknown";
  1571. break;
  1572. }
  1573. switch (alg_mac)
  1574. {
  1575. case SSL_MD5:
  1576. mac="MD5";
  1577. break;
  1578. case SSL_SHA1:
  1579. mac="SHA1";
  1580. break;
  1581. case SSL_SHA256:
  1582. mac="SHA256";
  1583. break;
  1584. case SSL_SHA384:
  1585. mac="SHA384";
  1586. break;
  1587. case SSL_AEAD:
  1588. mac="AEAD";
  1589. break;
  1590. default:
  1591. mac="unknown";
  1592. break;
  1593. }
  1594. if (buf == NULL)
  1595. {
  1596. len=128;
  1597. buf=OPENSSL_malloc(len);
  1598. if (buf == NULL) return("OPENSSL_malloc Error");
  1599. }
  1600. else if (len < 128)
  1601. return("Buffer too small");
  1602. #ifdef KSSL_DEBUG
  1603. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl);
  1604. #else
  1605. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
  1606. #endif /* KSSL_DEBUG */
  1607. return(buf);
  1608. }
  1609. /* Next three functions require non-null cipher */
  1610. int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
  1611. {
  1612. return (c->algorithm_enc & SSL_AES) != 0;
  1613. }
  1614. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
  1615. {
  1616. return (c->algorithm_mac & SSL_MD5) != 0;
  1617. }
  1618. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
  1619. {
  1620. return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
  1621. }
  1622. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
  1623. {
  1624. return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
  1625. }
  1626. const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1627. {
  1628. int i;
  1629. if (c == NULL) return("(NONE)");
  1630. i=(int)(c->id>>24L);
  1631. if (i == 3)
  1632. return("TLSv1/SSLv3");
  1633. else if (i == 2)
  1634. return("SSLv2");
  1635. else
  1636. return("unknown");
  1637. }
  1638. /* return the actual cipher being used */
  1639. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1640. {
  1641. if (c != NULL)
  1642. return(c->name);
  1643. return("(NONE)");
  1644. }
  1645. /* number of bits for symmetric cipher */
  1646. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1647. {
  1648. int ret=0;
  1649. if (c != NULL)
  1650. {
  1651. if (alg_bits != NULL) *alg_bits = c->alg_bits;
  1652. ret = c->strength_bits;
  1653. }
  1654. return(ret);
  1655. }
  1656. unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
  1657. {
  1658. return c->id;
  1659. }
  1660. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  1661. {
  1662. SSL_COMP *ctmp;
  1663. int i,nn;
  1664. if ((n == 0) || (sk == NULL)) return(NULL);
  1665. nn=sk_SSL_COMP_num(sk);
  1666. for (i=0; i<nn; i++)
  1667. {
  1668. ctmp=sk_SSL_COMP_value(sk,i);
  1669. if (ctmp->id == n)
  1670. return(ctmp);
  1671. }
  1672. return(NULL);
  1673. }
  1674. void *SSL_COMP_get_compression_methods(void)
  1675. {
  1676. return NULL;
  1677. }
  1678. int SSL_COMP_add_compression_method(int id, void *cm)
  1679. {
  1680. return 1;
  1681. }
  1682. const char *SSL_COMP_get_name(const void *comp)
  1683. {
  1684. return NULL;
  1685. }
  1686. /* For a cipher return the index corresponding to the certificate type */
  1687. int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
  1688. {
  1689. unsigned long alg_k, alg_a;
  1690. alg_k = c->algorithm_mkey;
  1691. alg_a = c->algorithm_auth;
  1692. if (alg_k & (SSL_kECDHr|SSL_kECDHe))
  1693. {
  1694. /* we don't need to look at SSL_kEECDH
  1695. * since no certificate is needed for
  1696. * anon ECDH and for authenticated
  1697. * EECDH, the check for the auth
  1698. * algorithm will set i correctly
  1699. * NOTE: For ECDH-RSA, we need an ECC
  1700. * not an RSA cert but for EECDH-RSA
  1701. * we need an RSA cert. Placing the
  1702. * checks for SSL_kECDH before RSA
  1703. * checks ensures the correct cert is chosen.
  1704. */
  1705. return SSL_PKEY_ECC;
  1706. }
  1707. else if (alg_a & SSL_aECDSA)
  1708. return SSL_PKEY_ECC;
  1709. else if (alg_k & SSL_kDHr)
  1710. return SSL_PKEY_DH_RSA;
  1711. else if (alg_k & SSL_kDHd)
  1712. return SSL_PKEY_DH_DSA;
  1713. else if (alg_a & SSL_aDSS)
  1714. return SSL_PKEY_DSA_SIGN;
  1715. else if (alg_a & SSL_aRSA)
  1716. return SSL_PKEY_RSA_ENC;
  1717. else if (alg_a & SSL_aKRB5)
  1718. /* VRS something else here? */
  1719. return -1;
  1720. else if (alg_a & SSL_aGOST94)
  1721. return SSL_PKEY_GOST94;
  1722. else if (alg_a & SSL_aGOST01)
  1723. return SSL_PKEY_GOST01;
  1724. return -1;
  1725. }
  1726. const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr)
  1727. {
  1728. const SSL_CIPHER *c;
  1729. c = ssl->method->get_cipher_by_char(ptr);
  1730. if (c == NULL || c->valid == 0)
  1731. return NULL;
  1732. return c;
  1733. }
  1734. const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
  1735. {
  1736. return ssl->method->get_cipher_by_char(ptr);
  1737. }