Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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