ssl_ciph.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /* ssl/ssl_ciph.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. #include <stdio.h>
  117. #include <openssl/objects.h>
  118. #ifndef OPENSSL_NO_COMP
  119. # include <openssl/comp.h>
  120. #endif
  121. #include "ssl_locl.h"
  122. #define SSL_ENC_DES_IDX 0
  123. #define SSL_ENC_3DES_IDX 1
  124. #define SSL_ENC_RC4_IDX 2
  125. #define SSL_ENC_RC2_IDX 3
  126. #define SSL_ENC_IDEA_IDX 4
  127. #define SSL_ENC_eFZA_IDX 5
  128. #define SSL_ENC_NULL_IDX 6
  129. #define SSL_ENC_AES128_IDX 7
  130. #define SSL_ENC_AES256_IDX 8
  131. #define SSL_ENC_CAMELLIA128_IDX 9
  132. #define SSL_ENC_CAMELLIA256_IDX 10
  133. #define SSL_ENC_SEED_IDX 11
  134. #define SSL_ENC_NUM_IDX 12
  135. static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX] = {
  136. NULL, NULL, NULL, NULL, NULL, NULL,
  137. };
  138. #define SSL_COMP_NULL_IDX 0
  139. #define SSL_COMP_ZLIB_IDX 1
  140. #define SSL_COMP_NUM_IDX 2
  141. static STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
  142. #define SSL_MD_MD5_IDX 0
  143. #define SSL_MD_SHA1_IDX 1
  144. #define SSL_MD_NUM_IDX 2
  145. static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = {
  146. NULL, NULL,
  147. };
  148. #define CIPHER_ADD 1
  149. #define CIPHER_KILL 2
  150. #define CIPHER_DEL 3
  151. #define CIPHER_ORD 4
  152. #define CIPHER_SPECIAL 5
  153. typedef struct cipher_order_st {
  154. SSL_CIPHER *cipher;
  155. int active;
  156. int dead;
  157. struct cipher_order_st *next, *prev;
  158. } CIPHER_ORDER;
  159. static const SSL_CIPHER cipher_aliases[] = {
  160. /* Don't include eNULL unless specifically enabled. */
  161. /*
  162. * Don't include ECC in ALL because these ciphers are not yet official.
  163. */
  164. /* must be first */
  165. {0, SSL_TXT_ALL, 0, SSL_ALL & ~SSL_eNULL & ~SSL_kECDH & ~SSL_kECDHE,
  166. SSL_ALL, 0, 0, 0, SSL_ALL, SSL_ALL},
  167. /*
  168. * TODO: COMPLEMENT OF ALL do not have ECC cipher suites handled properly.
  169. */
  170. /* COMPLEMENT OF ALL */
  171. {0, SSL_TXT_CMPALL, 0, SSL_eNULL, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  172. {0, SSL_TXT_CMPDEF, 0, SSL_ADH, SSL_EXP_MASK, 0, 0, 0, SSL_AUTH_MASK, 0},
  173. /* VRS Kerberos5 */
  174. {0, SSL_TXT_kKRB5, 0, SSL_kKRB5, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  175. {0, SSL_TXT_kRSA, 0, SSL_kRSA, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  176. {0, SSL_TXT_kDHr, 0, SSL_kDHr, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  177. {0, SSL_TXT_kDHd, 0, SSL_kDHd, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  178. {0, SSL_TXT_kEDH, 0, SSL_kEDH, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  179. {0, SSL_TXT_kFZA, 0, SSL_kFZA, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  180. {0, SSL_TXT_DH, 0, SSL_DH, 0, 0, 0, 0, SSL_MKEY_MASK, 0},
  181. {0, SSL_TXT_ECC, 0, (SSL_kECDH | SSL_kECDHE), 0, 0, 0, 0, SSL_MKEY_MASK,
  182. 0},
  183. {0, SSL_TXT_EDH, 0, SSL_EDH, 0, 0, 0, 0, SSL_MKEY_MASK | SSL_AUTH_MASK,
  184. 0},
  185. /* VRS Kerberos5 */
  186. {0, SSL_TXT_aKRB5, 0, SSL_aKRB5, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  187. {0, SSL_TXT_aRSA, 0, SSL_aRSA, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  188. {0, SSL_TXT_aDSS, 0, SSL_aDSS, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  189. {0, SSL_TXT_aFZA, 0, SSL_aFZA, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  190. {0, SSL_TXT_aNULL, 0, SSL_aNULL, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  191. {0, SSL_TXT_aDH, 0, SSL_aDH, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  192. {0, SSL_TXT_DSS, 0, SSL_DSS, 0, 0, 0, 0, SSL_AUTH_MASK, 0},
  193. {0, SSL_TXT_DES, 0, SSL_DES, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  194. {0, SSL_TXT_3DES, 0, SSL_3DES, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  195. {0, SSL_TXT_RC4, 0, SSL_RC4, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  196. {0, SSL_TXT_RC2, 0, SSL_RC2, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  197. #ifndef OPENSSL_NO_IDEA
  198. {0, SSL_TXT_IDEA, 0, SSL_IDEA, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  199. #endif
  200. {0, SSL_TXT_SEED, 0, SSL_SEED, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  201. {0, SSL_TXT_eNULL, 0, SSL_eNULL, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  202. {0, SSL_TXT_eFZA, 0, SSL_eFZA, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  203. {0, SSL_TXT_AES, 0, SSL_AES, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  204. {0, SSL_TXT_CAMELLIA, 0, SSL_CAMELLIA, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  205. {0, SSL_TXT_MD5, 0, SSL_MD5, 0, 0, 0, 0, SSL_MAC_MASK, 0},
  206. {0, SSL_TXT_SHA1, 0, SSL_SHA1, 0, 0, 0, 0, SSL_MAC_MASK, 0},
  207. {0, SSL_TXT_SHA, 0, SSL_SHA, 0, 0, 0, 0, SSL_MAC_MASK, 0},
  208. {0, SSL_TXT_NULL, 0, SSL_NULL, 0, 0, 0, 0, SSL_ENC_MASK, 0},
  209. {0, SSL_TXT_KRB5, 0, SSL_KRB5, 0, 0, 0, 0, SSL_AUTH_MASK | SSL_MKEY_MASK,
  210. 0},
  211. {0, SSL_TXT_RSA, 0, SSL_RSA, 0, 0, 0, 0, SSL_AUTH_MASK | SSL_MKEY_MASK,
  212. 0},
  213. {0, SSL_TXT_ADH, 0, SSL_ADH, 0, 0, 0, 0, SSL_AUTH_MASK | SSL_MKEY_MASK,
  214. 0},
  215. {0, SSL_TXT_FZA, 0, SSL_FZA, 0, 0, 0, 0,
  216. SSL_AUTH_MASK | SSL_MKEY_MASK | SSL_ENC_MASK, 0},
  217. {0, SSL_TXT_SSLV2, 0, SSL_SSLV2, 0, 0, 0, 0, SSL_SSL_MASK, 0},
  218. {0, SSL_TXT_SSLV3, 0, SSL_SSLV3, 0, 0, 0, 0, SSL_SSL_MASK, 0},
  219. {0, SSL_TXT_TLSV1, 0, SSL_TLSV1, 0, 0, 0, 0, SSL_SSL_MASK, 0},
  220. {0, SSL_TXT_EXP, 0, 0, SSL_EXPORT, 0, 0, 0, 0, SSL_EXP_MASK},
  221. {0, SSL_TXT_EXPORT, 0, 0, SSL_EXPORT, 0, 0, 0, 0, SSL_EXP_MASK},
  222. {0, SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0, 0, 0, 0, SSL_STRONG_MASK},
  223. {0, SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0, 0, 0, 0, SSL_STRONG_MASK},
  224. {0, SSL_TXT_LOW, 0, 0, SSL_LOW, 0, 0, 0, 0, SSL_STRONG_MASK},
  225. {0, SSL_TXT_MEDIUM, 0, 0, SSL_MEDIUM, 0, 0, 0, 0, SSL_STRONG_MASK},
  226. {0, SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0, 0, 0, 0, SSL_STRONG_MASK},
  227. {0, SSL_TXT_FIPS, 0, 0, SSL_FIPS, 0, 0, 0, 0, SSL_FIPS | SSL_STRONG_NONE},
  228. };
  229. void ssl_load_ciphers(void)
  230. {
  231. ssl_cipher_methods[SSL_ENC_DES_IDX] = EVP_get_cipherbyname(SN_des_cbc);
  232. ssl_cipher_methods[SSL_ENC_3DES_IDX] =
  233. EVP_get_cipherbyname(SN_des_ede3_cbc);
  234. ssl_cipher_methods[SSL_ENC_RC4_IDX] = EVP_get_cipherbyname(SN_rc4);
  235. ssl_cipher_methods[SSL_ENC_RC2_IDX] = EVP_get_cipherbyname(SN_rc2_cbc);
  236. #ifndef OPENSSL_NO_IDEA
  237. ssl_cipher_methods[SSL_ENC_IDEA_IDX] = EVP_get_cipherbyname(SN_idea_cbc);
  238. #else
  239. ssl_cipher_methods[SSL_ENC_IDEA_IDX] = NULL;
  240. #endif
  241. ssl_cipher_methods[SSL_ENC_AES128_IDX] =
  242. EVP_get_cipherbyname(SN_aes_128_cbc);
  243. ssl_cipher_methods[SSL_ENC_AES256_IDX] =
  244. EVP_get_cipherbyname(SN_aes_256_cbc);
  245. ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] =
  246. EVP_get_cipherbyname(SN_camellia_128_cbc);
  247. ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] =
  248. EVP_get_cipherbyname(SN_camellia_256_cbc);
  249. ssl_cipher_methods[SSL_ENC_SEED_IDX] = EVP_get_cipherbyname(SN_seed_cbc);
  250. ssl_digest_methods[SSL_MD_MD5_IDX] = EVP_get_digestbyname(SN_md5);
  251. ssl_digest_methods[SSL_MD_SHA1_IDX] = EVP_get_digestbyname(SN_sha1);
  252. }
  253. #ifndef OPENSSL_NO_COMP
  254. static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b)
  255. {
  256. return ((*a)->id - (*b)->id);
  257. }
  258. static void load_builtin_compressions(void)
  259. {
  260. int got_write_lock = 0;
  261. CRYPTO_r_lock(CRYPTO_LOCK_SSL);
  262. if (ssl_comp_methods == NULL) {
  263. CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
  264. CRYPTO_w_lock(CRYPTO_LOCK_SSL);
  265. got_write_lock = 1;
  266. if (ssl_comp_methods == NULL) {
  267. SSL_COMP *comp = NULL;
  268. MemCheck_off();
  269. ssl_comp_methods = sk_SSL_COMP_new(sk_comp_cmp);
  270. if (ssl_comp_methods != NULL) {
  271. comp = (SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  272. if (comp != NULL) {
  273. comp->method = COMP_zlib();
  274. if (comp->method && comp->method->type == NID_undef)
  275. OPENSSL_free(comp);
  276. else {
  277. comp->id = SSL_COMP_ZLIB_IDX;
  278. comp->name = comp->method->name;
  279. sk_SSL_COMP_push(ssl_comp_methods, comp);
  280. }
  281. }
  282. sk_SSL_COMP_sort(ssl_comp_methods);
  283. }
  284. MemCheck_on();
  285. }
  286. }
  287. if (got_write_lock)
  288. CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
  289. else
  290. CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
  291. }
  292. #endif
  293. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  294. const EVP_MD **md, SSL_COMP **comp)
  295. {
  296. int i;
  297. SSL_CIPHER *c;
  298. c = s->cipher;
  299. if (c == NULL)
  300. return (0);
  301. if (comp != NULL) {
  302. SSL_COMP ctmp;
  303. #ifndef OPENSSL_NO_COMP
  304. load_builtin_compressions();
  305. #endif
  306. *comp = NULL;
  307. ctmp.id = s->compress_meth;
  308. if (ssl_comp_methods != NULL) {
  309. i = sk_SSL_COMP_find(ssl_comp_methods, &ctmp);
  310. if (i >= 0)
  311. *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
  312. else
  313. *comp = NULL;
  314. }
  315. }
  316. if ((enc == NULL) || (md == NULL))
  317. return (0);
  318. switch (c->algorithms & SSL_ENC_MASK) {
  319. case SSL_DES:
  320. i = SSL_ENC_DES_IDX;
  321. break;
  322. case SSL_3DES:
  323. i = SSL_ENC_3DES_IDX;
  324. break;
  325. case SSL_RC4:
  326. i = SSL_ENC_RC4_IDX;
  327. break;
  328. case SSL_RC2:
  329. i = SSL_ENC_RC2_IDX;
  330. break;
  331. case SSL_IDEA:
  332. i = SSL_ENC_IDEA_IDX;
  333. break;
  334. case SSL_eNULL:
  335. i = SSL_ENC_NULL_IDX;
  336. break;
  337. case SSL_AES:
  338. switch (c->alg_bits) {
  339. case 128:
  340. i = SSL_ENC_AES128_IDX;
  341. break;
  342. case 256:
  343. i = SSL_ENC_AES256_IDX;
  344. break;
  345. default:
  346. i = -1;
  347. break;
  348. }
  349. break;
  350. case SSL_CAMELLIA:
  351. switch (c->alg_bits) {
  352. case 128:
  353. i = SSL_ENC_CAMELLIA128_IDX;
  354. break;
  355. case 256:
  356. i = SSL_ENC_CAMELLIA256_IDX;
  357. break;
  358. default:
  359. i = -1;
  360. break;
  361. }
  362. break;
  363. case SSL_SEED:
  364. i = SSL_ENC_SEED_IDX;
  365. break;
  366. default:
  367. i = -1;
  368. break;
  369. }
  370. if ((i < 0) || (i >= SSL_ENC_NUM_IDX))
  371. *enc = NULL;
  372. else {
  373. if (i == SSL_ENC_NULL_IDX)
  374. *enc = EVP_enc_null();
  375. else
  376. *enc = ssl_cipher_methods[i];
  377. }
  378. switch (c->algorithms & SSL_MAC_MASK) {
  379. case SSL_MD5:
  380. i = SSL_MD_MD5_IDX;
  381. break;
  382. case SSL_SHA1:
  383. i = SSL_MD_SHA1_IDX;
  384. break;
  385. default:
  386. i = -1;
  387. break;
  388. }
  389. if ((i < 0) || (i >= SSL_MD_NUM_IDX))
  390. *md = NULL;
  391. else
  392. *md = ssl_digest_methods[i];
  393. if ((*enc != NULL) && (*md != NULL))
  394. return (1);
  395. else
  396. return (0);
  397. }
  398. #define ITEM_SEP(a) \
  399. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  400. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  401. CIPHER_ORDER **tail)
  402. {
  403. if (curr == *tail)
  404. return;
  405. if (curr == *head)
  406. *head = curr->next;
  407. if (curr->prev != NULL)
  408. curr->prev->next = curr->next;
  409. if (curr->next != NULL) /* should always be true */
  410. curr->next->prev = curr->prev;
  411. (*tail)->next = curr;
  412. curr->prev = *tail;
  413. curr->next = NULL;
  414. *tail = curr;
  415. }
  416. struct disabled_masks { /* This is a kludge no longer needed with
  417. * OpenSSL 0.9.9, where 128-bit and 256-bit
  418. * algorithms simply will get separate bits. */
  419. unsigned long mask; /* everything except m256 */
  420. unsigned long m256; /* applies to 256-bit algorithms only */
  421. };
  422. static struct disabled_masks ssl_cipher_get_disabled(void)
  423. {
  424. unsigned long mask;
  425. unsigned long m256;
  426. struct disabled_masks ret;
  427. mask = SSL_kFZA;
  428. #ifdef OPENSSL_NO_RSA
  429. mask |= SSL_aRSA | SSL_kRSA;
  430. #endif
  431. #ifdef OPENSSL_NO_DSA
  432. mask |= SSL_aDSS;
  433. #endif
  434. #ifdef OPENSSL_NO_DH
  435. mask |= SSL_kDHr | SSL_kDHd | SSL_kEDH | SSL_aDH;
  436. #endif
  437. #ifdef OPENSSL_NO_KRB5
  438. mask |= SSL_kKRB5 | SSL_aKRB5;
  439. #endif
  440. #ifdef OPENSSL_NO_ECDH
  441. mask |= SSL_kECDH | SSL_kECDHE;
  442. #endif
  443. #ifdef SSL_FORBID_ENULL
  444. mask |= SSL_eNULL;
  445. #endif
  446. mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX] == NULL) ? SSL_DES : 0;
  447. mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES : 0;
  448. mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX] == NULL) ? SSL_RC4 : 0;
  449. mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX] == NULL) ? SSL_RC2 : 0;
  450. mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA : 0;
  451. mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA : 0;
  452. mask |= (ssl_cipher_methods[SSL_ENC_SEED_IDX] == NULL) ? SSL_SEED : 0;
  453. mask |= (ssl_digest_methods[SSL_MD_MD5_IDX] == NULL) ? SSL_MD5 : 0;
  454. mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1 : 0;
  455. /* finally consider algorithms where mask and m256 differ */
  456. m256 = mask;
  457. mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES : 0;
  458. mask |=
  459. (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] ==
  460. NULL) ? SSL_CAMELLIA : 0;
  461. m256 |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES : 0;
  462. m256 |=
  463. (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] ==
  464. NULL) ? SSL_CAMELLIA : 0;
  465. ret.mask = mask;
  466. ret.m256 = m256;
  467. return ret;
  468. }
  469. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  470. int num_of_ciphers, unsigned long mask,
  471. unsigned long m256,
  472. CIPHER_ORDER *co_list,
  473. CIPHER_ORDER **head_p,
  474. CIPHER_ORDER **tail_p)
  475. {
  476. int i, co_list_num;
  477. SSL_CIPHER *c;
  478. /*
  479. * We have num_of_ciphers descriptions compiled in, depending on the
  480. * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
  481. * These will later be sorted in a linked list with at most num
  482. * entries.
  483. */
  484. /* Get the initial list of ciphers */
  485. co_list_num = 0; /* actual count of ciphers */
  486. for (i = 0; i < num_of_ciphers; i++) {
  487. c = ssl_method->get_cipher(i);
  488. #define IS_MASKED(c) ((c)->algorithms & (((c)->alg_bits == 256) ? m256 : mask))
  489. /* drop those that use any of that is not available */
  490. #ifdef OPENSSL_FIPS
  491. if ((c != NULL) && c->valid && !IS_MASKED(c)
  492. && (!FIPS_mode() || (c->algo_strength & SSL_FIPS)))
  493. #else
  494. if ((c != NULL) && c->valid && !IS_MASKED(c))
  495. #endif
  496. {
  497. co_list[co_list_num].cipher = c;
  498. co_list[co_list_num].next = NULL;
  499. co_list[co_list_num].prev = NULL;
  500. co_list[co_list_num].active = 0;
  501. co_list_num++;
  502. #ifdef KSSL_DEBUG
  503. printf("\t%d: %s %lx %lx\n", i, c->name, c->id, c->algorithms);
  504. #endif /* KSSL_DEBUG */
  505. /*
  506. * if (!sk_push(ca_list,(char *)c)) goto err;
  507. */
  508. }
  509. }
  510. /*
  511. * Prepare linked list from list entries
  512. */
  513. for (i = 1; i < co_list_num - 1; i++) {
  514. co_list[i].prev = &(co_list[i - 1]);
  515. co_list[i].next = &(co_list[i + 1]);
  516. }
  517. if (co_list_num > 0) {
  518. (*head_p) = &(co_list[0]);
  519. (*head_p)->prev = NULL;
  520. (*head_p)->next = &(co_list[1]);
  521. (*tail_p) = &(co_list[co_list_num - 1]);
  522. (*tail_p)->prev = &(co_list[co_list_num - 2]);
  523. (*tail_p)->next = NULL;
  524. }
  525. }
  526. static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
  527. int num_of_group_aliases,
  528. unsigned long mask, CIPHER_ORDER *head)
  529. {
  530. CIPHER_ORDER *ciph_curr;
  531. SSL_CIPHER **ca_curr;
  532. int i;
  533. /*
  534. * First, add the real ciphers as already collected
  535. */
  536. ciph_curr = head;
  537. ca_curr = ca_list;
  538. while (ciph_curr != NULL) {
  539. *ca_curr = ciph_curr->cipher;
  540. ca_curr++;
  541. ciph_curr = ciph_curr->next;
  542. }
  543. /*
  544. * Now we add the available ones from the cipher_aliases[] table.
  545. * They represent either an algorithm, that must be fully
  546. * supported (not match any bit in mask) or represent a cipher
  547. * strength value (will be added in any case because algorithms=0).
  548. */
  549. for (i = 0; i < num_of_group_aliases; i++) {
  550. if ((i == 0) || /* always fetch "ALL" */
  551. !(cipher_aliases[i].algorithms & mask)) {
  552. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  553. ca_curr++;
  554. }
  555. }
  556. *ca_curr = NULL; /* end of list */
  557. }
  558. static void ssl_cipher_apply_rule(unsigned long cipher_id,
  559. unsigned long ssl_version,
  560. unsigned long algorithms,
  561. unsigned long mask,
  562. unsigned long algo_strength,
  563. unsigned long mask_strength, int rule,
  564. int strength_bits, CIPHER_ORDER *co_list,
  565. CIPHER_ORDER **head_p,
  566. CIPHER_ORDER **tail_p)
  567. {
  568. CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2;
  569. SSL_CIPHER *cp;
  570. unsigned long ma, ma_s;
  571. #ifdef CIPHER_DEBUG
  572. printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n",
  573. rule, algorithms, mask, algo_strength, mask_strength,
  574. strength_bits);
  575. #endif
  576. curr = head = *head_p;
  577. curr2 = head;
  578. tail2 = tail = *tail_p;
  579. for (;;) {
  580. if ((curr == NULL) || (curr == tail2))
  581. break;
  582. curr = curr2;
  583. curr2 = curr->next;
  584. cp = curr->cipher;
  585. /* Special case: only satisfied by COMPLEMENTOFDEFAULT */
  586. if (algo_strength == SSL_EXP_MASK) {
  587. if ((SSL_C_IS_EXPORT(cp) || cp->algorithms & SSL_SSLV2
  588. || cp->algorithms & SSL_aNULL)
  589. && !(cp->algorithms & (SSL_kECDHE|SSL_kECDH)))
  590. goto ok;
  591. else
  592. continue;
  593. }
  594. /*
  595. * If explicit cipher suite, match only that one for its own protocol
  596. * version. Usual selection criteria will be used for similar
  597. * ciphersuites from other version!
  598. */
  599. if (cipher_id && (cp->algorithms & SSL_SSL_MASK) == ssl_version) {
  600. if (cp->id != cipher_id)
  601. continue;
  602. }
  603. /*
  604. * Selection criteria is either the number of strength_bits
  605. * or the algorithm used.
  606. */
  607. else if (strength_bits == -1) {
  608. ma = mask & cp->algorithms;
  609. ma_s = mask_strength & cp->algo_strength;
  610. #ifdef CIPHER_DEBUG
  611. printf
  612. ("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n",
  613. cp->name, cp->algorithms, cp->algo_strength, mask,
  614. mask_strength);
  615. printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n",
  616. ma, ma_s, ma & algorithms, ma_s & algo_strength);
  617. #endif
  618. /*
  619. * Select: if none of the mask bit was met from the
  620. * cipher or not all of the bits were met, the
  621. * selection does not apply.
  622. */
  623. if (((ma == 0) && (ma_s == 0)) ||
  624. ((ma & algorithms) != ma) || ((ma_s & algo_strength) != ma_s))
  625. continue; /* does not apply */
  626. } else if (strength_bits != cp->strength_bits)
  627. continue; /* does not apply */
  628. ok:
  629. #ifdef CIPHER_DEBUG
  630. printf("Action = %d\n", rule);
  631. #endif
  632. /* add the cipher if it has not been added yet. */
  633. if (rule == CIPHER_ADD) {
  634. if (!curr->active) {
  635. int add_this_cipher = 1;
  636. if (((cp->algorithms & (SSL_kECDHE | SSL_kECDH | SSL_aECDSA))
  637. != 0)) {
  638. /*
  639. * Make sure "ECCdraft" ciphersuites are activated only
  640. * if *explicitly* requested, but not implicitly (such as
  641. * as part of the "AES" alias).
  642. */
  643. add_this_cipher =
  644. (mask & (SSL_kECDHE | SSL_kECDH | SSL_aECDSA)) != 0
  645. || cipher_id != 0;
  646. }
  647. if (add_this_cipher) {
  648. ll_append_tail(&head, curr, &tail);
  649. curr->active = 1;
  650. }
  651. }
  652. }
  653. /* Move the added cipher to this location */
  654. else if (rule == CIPHER_ORD) {
  655. if (curr->active) {
  656. ll_append_tail(&head, curr, &tail);
  657. }
  658. } else if (rule == CIPHER_DEL)
  659. curr->active = 0;
  660. else if (rule == CIPHER_KILL) {
  661. if (head == curr)
  662. head = curr->next;
  663. else
  664. curr->prev->next = curr->next;
  665. if (tail == curr)
  666. tail = curr->prev;
  667. curr->active = 0;
  668. if (curr->next != NULL)
  669. curr->next->prev = curr->prev;
  670. if (curr->prev != NULL)
  671. curr->prev->next = curr->next;
  672. curr->next = NULL;
  673. curr->prev = NULL;
  674. }
  675. }
  676. *head_p = head;
  677. *tail_p = tail;
  678. }
  679. static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list,
  680. CIPHER_ORDER **head_p,
  681. CIPHER_ORDER **tail_p)
  682. {
  683. int max_strength_bits, i, *number_uses;
  684. CIPHER_ORDER *curr;
  685. /*
  686. * This routine sorts the ciphers with descending strength. The sorting
  687. * must keep the pre-sorted sequence, so we apply the normal sorting
  688. * routine as '+' movement to the end of the list.
  689. */
  690. max_strength_bits = 0;
  691. curr = *head_p;
  692. while (curr != NULL) {
  693. if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
  694. max_strength_bits = curr->cipher->strength_bits;
  695. curr = curr->next;
  696. }
  697. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  698. if (!number_uses) {
  699. SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
  700. return (0);
  701. }
  702. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  703. /*
  704. * Now find the strength_bits values actually used
  705. */
  706. curr = *head_p;
  707. while (curr != NULL) {
  708. if (curr->active)
  709. number_uses[curr->cipher->strength_bits]++;
  710. curr = curr->next;
  711. }
  712. /*
  713. * Go through the list of used strength_bits values in descending
  714. * order.
  715. */
  716. for (i = max_strength_bits; i >= 0; i--)
  717. if (number_uses[i] > 0)
  718. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i,
  719. co_list, head_p, tail_p);
  720. OPENSSL_free(number_uses);
  721. return (1);
  722. }
  723. static int ssl_cipher_process_rulestr(const char *rule_str,
  724. CIPHER_ORDER *co_list,
  725. CIPHER_ORDER **head_p,
  726. CIPHER_ORDER **tail_p,
  727. SSL_CIPHER **ca_list)
  728. {
  729. unsigned long algorithms, mask, algo_strength, mask_strength;
  730. const char *l, *buf;
  731. int j, multi, found, rule, retval, ok, buflen;
  732. unsigned long cipher_id = 0, ssl_version = 0;
  733. char ch;
  734. retval = 1;
  735. l = rule_str;
  736. for (;;) {
  737. ch = *l;
  738. if (ch == '\0')
  739. break; /* done */
  740. if (ch == '-') {
  741. rule = CIPHER_DEL;
  742. l++;
  743. } else if (ch == '+') {
  744. rule = CIPHER_ORD;
  745. l++;
  746. } else if (ch == '!') {
  747. rule = CIPHER_KILL;
  748. l++;
  749. } else if (ch == '@') {
  750. rule = CIPHER_SPECIAL;
  751. l++;
  752. } else {
  753. rule = CIPHER_ADD;
  754. }
  755. if (ITEM_SEP(ch)) {
  756. l++;
  757. continue;
  758. }
  759. algorithms = mask = algo_strength = mask_strength = 0;
  760. for (;;) {
  761. ch = *l;
  762. buf = l;
  763. buflen = 0;
  764. #ifndef CHARSET_EBCDIC
  765. while (((ch >= 'A') && (ch <= 'Z')) ||
  766. ((ch >= '0') && (ch <= '9')) ||
  767. ((ch >= 'a') && (ch <= 'z')) || (ch == '-'))
  768. #else
  769. while (isalnum(ch) || (ch == '-'))
  770. #endif
  771. {
  772. ch = *(++l);
  773. buflen++;
  774. }
  775. if (buflen == 0) {
  776. /*
  777. * We hit something we cannot deal with,
  778. * it is no command or separator nor
  779. * alphanumeric, so we call this an error.
  780. */
  781. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  782. SSL_R_INVALID_COMMAND);
  783. retval = found = 0;
  784. l++;
  785. break;
  786. }
  787. if (rule == CIPHER_SPECIAL) {
  788. found = 0; /* unused -- avoid compiler warning */
  789. break; /* special treatment */
  790. }
  791. /* check for multi-part specification */
  792. if (ch == '+') {
  793. multi = 1;
  794. l++;
  795. } else
  796. multi = 0;
  797. /*
  798. * Now search for the cipher alias in the ca_list. Be careful
  799. * with the strncmp, because the "buflen" limitation
  800. * will make the rule "ADH:SOME" and the cipher
  801. * "ADH-MY-CIPHER" look like a match for buflen=3.
  802. * So additionally check whether the cipher name found
  803. * has the correct length. We can save a strlen() call:
  804. * just checking for the '\0' at the right place is
  805. * sufficient, we have to strncmp() anyway. (We cannot
  806. * use strcmp(), because buf is not '\0' terminated.)
  807. */
  808. j = found = 0;
  809. cipher_id = 0;
  810. ssl_version = 0;
  811. while (ca_list[j]) {
  812. if (!strncmp(buf, ca_list[j]->name, buflen) &&
  813. (ca_list[j]->name[buflen] == '\0')) {
  814. found = 1;
  815. break;
  816. } else
  817. j++;
  818. }
  819. if (!found)
  820. break; /* ignore this entry */
  821. /*-
  822. * New algorithms:
  823. * 1 - any old restrictions apply outside new mask
  824. * 2 - any new restrictions apply outside old mask
  825. * 3 - enforce old & new where masks intersect
  826. */
  827. algorithms = (algorithms & ~ca_list[j]->mask) | /* 1 */
  828. (ca_list[j]->algorithms & ~mask) | /* 2 */
  829. (algorithms & ca_list[j]->algorithms); /* 3 */
  830. mask |= ca_list[j]->mask;
  831. algo_strength = (algo_strength & ~ca_list[j]->mask_strength) |
  832. (ca_list[j]->algo_strength & ~mask_strength) |
  833. (algo_strength & ca_list[j]->algo_strength);
  834. mask_strength |= ca_list[j]->mask_strength;
  835. /* explicit ciphersuite found */
  836. if (ca_list[j]->valid) {
  837. cipher_id = ca_list[j]->id;
  838. ssl_version = ca_list[j]->algorithms & SSL_SSL_MASK;
  839. break;
  840. }
  841. if (!multi)
  842. break;
  843. }
  844. /*
  845. * Ok, we have the rule, now apply it
  846. */
  847. if (rule == CIPHER_SPECIAL) { /* special command */
  848. ok = 0;
  849. if ((buflen == 8) && !strncmp(buf, "STRENGTH", 8))
  850. ok = ssl_cipher_strength_sort(co_list, head_p, tail_p);
  851. else
  852. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  853. SSL_R_INVALID_COMMAND);
  854. if (ok == 0)
  855. retval = 0;
  856. /*
  857. * We do not support any "multi" options
  858. * together with "@", so throw away the
  859. * rest of the command, if any left, until
  860. * end or ':' is found.
  861. */
  862. while ((*l != '\0') && !ITEM_SEP(*l))
  863. l++;
  864. } else if (found) {
  865. ssl_cipher_apply_rule(cipher_id, ssl_version, algorithms, mask,
  866. algo_strength, mask_strength, rule, -1,
  867. co_list, head_p, tail_p);
  868. } else {
  869. while ((*l != '\0') && !ITEM_SEP(*l))
  870. l++;
  871. }
  872. if (*l == '\0')
  873. break; /* done */
  874. }
  875. return (retval);
  876. }
  877. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, STACK_OF(SSL_CIPHER)
  878. **cipher_list, STACK_OF(SSL_CIPHER)
  879. **cipher_list_by_id,
  880. const char *rule_str)
  881. {
  882. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  883. unsigned long disabled_mask;
  884. unsigned long disabled_m256;
  885. STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
  886. const char *rule_p;
  887. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  888. SSL_CIPHER **ca_list = NULL;
  889. /*
  890. * Return with error if nothing to do.
  891. */
  892. if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
  893. return NULL;
  894. /*
  895. * To reduce the work to do we only want to process the compiled
  896. * in algorithms, so we first get the mask of disabled ciphers.
  897. */
  898. {
  899. struct disabled_masks d;
  900. d = ssl_cipher_get_disabled();
  901. disabled_mask = d.mask;
  902. disabled_m256 = d.m256;
  903. }
  904. /*
  905. * Now we have to collect the available ciphers from the compiled
  906. * in ciphers. We cannot get more than the number compiled in, so
  907. * it is used for allocation.
  908. */
  909. num_of_ciphers = ssl_method->num_ciphers();
  910. #ifdef KSSL_DEBUG
  911. printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
  912. #endif /* KSSL_DEBUG */
  913. co_list =
  914. (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  915. if (co_list == NULL) {
  916. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  917. return (NULL); /* Failure */
  918. }
  919. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
  920. disabled_m256, co_list, &head, &tail);
  921. /*
  922. * We also need cipher aliases for selecting based on the rule_str.
  923. * There might be two types of entries in the rule_str: 1) names
  924. * of ciphers themselves 2) aliases for groups of ciphers.
  925. * For 1) we need the available ciphers and for 2) the cipher
  926. * groups of cipher_aliases added together in one list (otherwise
  927. * we would be happy with just the cipher_aliases table).
  928. */
  929. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  930. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  931. ca_list =
  932. (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) *
  933. num_of_alias_max);
  934. if (ca_list == NULL) {
  935. OPENSSL_free(co_list);
  936. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  937. return (NULL); /* Failure */
  938. }
  939. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
  940. (disabled_mask & disabled_m256), head);
  941. /*
  942. * If the rule_string begins with DEFAULT, apply the default rule
  943. * before using the (possibly available) additional rules.
  944. */
  945. ok = 1;
  946. rule_p = rule_str;
  947. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  948. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  949. co_list, &head, &tail, ca_list);
  950. rule_p += 7;
  951. if (*rule_p == ':')
  952. rule_p++;
  953. }
  954. if (ok && (strlen(rule_p) > 0))
  955. ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail,
  956. ca_list);
  957. OPENSSL_free(ca_list); /* Not needed anymore */
  958. if (!ok) { /* Rule processing failure */
  959. OPENSSL_free(co_list);
  960. return (NULL);
  961. }
  962. /*
  963. * Allocate new "cipherstack" for the result, return with error
  964. * if we cannot get one.
  965. */
  966. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
  967. OPENSSL_free(co_list);
  968. return (NULL);
  969. }
  970. /*
  971. * The cipher selection for the list is done. The ciphers are added
  972. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  973. */
  974. for (curr = head; curr != NULL; curr = curr->next) {
  975. #ifdef OPENSSL_FIPS
  976. if (curr->active
  977. && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
  978. #else
  979. if (curr->active)
  980. #endif
  981. {
  982. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  983. #ifdef CIPHER_DEBUG
  984. printf("<%s>\n", curr->cipher->name);
  985. #endif
  986. }
  987. }
  988. OPENSSL_free(co_list); /* Not needed any longer */
  989. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  990. if (tmp_cipher_list == NULL) {
  991. sk_SSL_CIPHER_free(cipherstack);
  992. return NULL;
  993. }
  994. if (*cipher_list != NULL)
  995. sk_SSL_CIPHER_free(*cipher_list);
  996. *cipher_list = cipherstack;
  997. if (*cipher_list_by_id != NULL)
  998. sk_SSL_CIPHER_free(*cipher_list_by_id);
  999. *cipher_list_by_id = tmp_cipher_list;
  1000. (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,
  1001. ssl_cipher_ptr_id_cmp);
  1002. sk_SSL_CIPHER_sort(*cipher_list_by_id);
  1003. return (cipherstack);
  1004. }
  1005. char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
  1006. {
  1007. int is_export, pkl, kl;
  1008. const char *ver, *exp_str;
  1009. const char *kx, *au, *enc, *mac;
  1010. unsigned long alg, alg2;
  1011. #ifdef KSSL_DEBUG
  1012. static const char *format =
  1013. "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n";
  1014. #else
  1015. static const char *format =
  1016. "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
  1017. #endif /* KSSL_DEBUG */
  1018. alg = cipher->algorithms;
  1019. alg2 = cipher->algorithm2;
  1020. is_export = SSL_C_IS_EXPORT(cipher);
  1021. pkl = SSL_C_EXPORT_PKEYLENGTH(cipher);
  1022. kl = SSL_C_EXPORT_KEYLENGTH(cipher);
  1023. exp_str = is_export ? " export" : "";
  1024. if (alg & SSL_SSLV2)
  1025. ver = "SSLv2";
  1026. else if (alg & SSL_SSLV3)
  1027. ver = "SSLv3";
  1028. else
  1029. ver = "unknown";
  1030. switch (alg & SSL_MKEY_MASK) {
  1031. case SSL_kRSA:
  1032. kx = is_export ? (pkl == 512 ? "RSA(512)" : "RSA(1024)") : "RSA";
  1033. break;
  1034. case SSL_kDHr:
  1035. kx = "DH/RSA";
  1036. break;
  1037. case SSL_kDHd:
  1038. kx = "DH/DSS";
  1039. break;
  1040. case SSL_kKRB5: /* VRS */
  1041. case SSL_KRB5: /* VRS */
  1042. kx = "KRB5";
  1043. break;
  1044. case SSL_kFZA:
  1045. kx = "Fortezza";
  1046. break;
  1047. case SSL_kEDH:
  1048. kx = is_export ? (pkl == 512 ? "DH(512)" : "DH(1024)") : "DH";
  1049. break;
  1050. case SSL_kECDH:
  1051. case SSL_kECDHE:
  1052. kx = is_export ? "ECDH(<=163)" : "ECDH";
  1053. break;
  1054. default:
  1055. kx = "unknown";
  1056. }
  1057. switch (alg & SSL_AUTH_MASK) {
  1058. case SSL_aRSA:
  1059. au = "RSA";
  1060. break;
  1061. case SSL_aDSS:
  1062. au = "DSS";
  1063. break;
  1064. case SSL_aDH:
  1065. au = "DH";
  1066. break;
  1067. case SSL_aKRB5: /* VRS */
  1068. case SSL_KRB5: /* VRS */
  1069. au = "KRB5";
  1070. break;
  1071. case SSL_aFZA:
  1072. case SSL_aNULL:
  1073. au = "None";
  1074. break;
  1075. case SSL_aECDSA:
  1076. au = "ECDSA";
  1077. break;
  1078. default:
  1079. au = "unknown";
  1080. break;
  1081. }
  1082. switch (alg & SSL_ENC_MASK) {
  1083. case SSL_DES:
  1084. enc = (is_export && kl == 5) ? "DES(40)" : "DES(56)";
  1085. break;
  1086. case SSL_3DES:
  1087. enc = "3DES(168)";
  1088. break;
  1089. case SSL_RC4:
  1090. enc = is_export ? (kl == 5 ? "RC4(40)" : "RC4(56)")
  1091. : ((alg2 & SSL2_CF_8_BYTE_ENC) ? "RC4(64)" : "RC4(128)");
  1092. break;
  1093. case SSL_RC2:
  1094. enc = is_export ? (kl == 5 ? "RC2(40)" : "RC2(56)") : "RC2(128)";
  1095. break;
  1096. case SSL_IDEA:
  1097. enc = "IDEA(128)";
  1098. break;
  1099. case SSL_eFZA:
  1100. enc = "Fortezza";
  1101. break;
  1102. case SSL_eNULL:
  1103. enc = "None";
  1104. break;
  1105. case SSL_AES:
  1106. switch (cipher->strength_bits) {
  1107. case 128:
  1108. enc = "AES(128)";
  1109. break;
  1110. case 192:
  1111. enc = "AES(192)";
  1112. break;
  1113. case 256:
  1114. enc = "AES(256)";
  1115. break;
  1116. default:
  1117. enc = "AES(?" "?" "?)";
  1118. break;
  1119. }
  1120. break;
  1121. case SSL_CAMELLIA:
  1122. switch (cipher->strength_bits) {
  1123. case 128:
  1124. enc = "Camellia(128)";
  1125. break;
  1126. case 256:
  1127. enc = "Camellia(256)";
  1128. break;
  1129. default:
  1130. enc = "Camellia(?" "?" "?)";
  1131. break;
  1132. }
  1133. break;
  1134. case SSL_SEED:
  1135. enc = "SEED(128)";
  1136. break;
  1137. default:
  1138. enc = "unknown";
  1139. break;
  1140. }
  1141. switch (alg & SSL_MAC_MASK) {
  1142. case SSL_MD5:
  1143. mac = "MD5";
  1144. break;
  1145. case SSL_SHA1:
  1146. mac = "SHA1";
  1147. break;
  1148. default:
  1149. mac = "unknown";
  1150. break;
  1151. }
  1152. if (buf == NULL) {
  1153. len = 128;
  1154. buf = OPENSSL_malloc(len);
  1155. if (buf == NULL)
  1156. return ("OPENSSL_malloc Error");
  1157. } else if (len < 128)
  1158. return ("Buffer too small");
  1159. #ifdef KSSL_DEBUG
  1160. BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac,
  1161. exp_str, alg);
  1162. #else
  1163. BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac,
  1164. exp_str);
  1165. #endif /* KSSL_DEBUG */
  1166. return (buf);
  1167. }
  1168. char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1169. {
  1170. int i;
  1171. if (c == NULL)
  1172. return ("(NONE)");
  1173. i = (int)(c->id >> 24L);
  1174. if (i == 3)
  1175. return ("TLSv1/SSLv3");
  1176. else if (i == 2)
  1177. return ("SSLv2");
  1178. else
  1179. return ("unknown");
  1180. }
  1181. /* return the actual cipher being used */
  1182. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1183. {
  1184. if (c != NULL)
  1185. return (c->name);
  1186. return ("(NONE)");
  1187. }
  1188. /* number of bits for symmetric cipher */
  1189. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1190. {
  1191. int ret = 0;
  1192. if (c != NULL) {
  1193. if (alg_bits != NULL)
  1194. *alg_bits = c->alg_bits;
  1195. ret = c->strength_bits;
  1196. }
  1197. return (ret);
  1198. }
  1199. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  1200. {
  1201. SSL_COMP *ctmp;
  1202. int i, nn;
  1203. if ((n == 0) || (sk == NULL))
  1204. return (NULL);
  1205. nn = sk_SSL_COMP_num(sk);
  1206. for (i = 0; i < nn; i++) {
  1207. ctmp = sk_SSL_COMP_value(sk, i);
  1208. if (ctmp->id == n)
  1209. return (ctmp);
  1210. }
  1211. return (NULL);
  1212. }
  1213. #ifdef OPENSSL_NO_COMP
  1214. void *SSL_COMP_get_compression_methods(void)
  1215. {
  1216. return NULL;
  1217. }
  1218. int SSL_COMP_add_compression_method(int id, void *cm)
  1219. {
  1220. return 1;
  1221. }
  1222. const char *SSL_COMP_get_name(const void *comp)
  1223. {
  1224. return NULL;
  1225. }
  1226. #else
  1227. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1228. {
  1229. load_builtin_compressions();
  1230. return (ssl_comp_methods);
  1231. }
  1232. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1233. {
  1234. SSL_COMP *comp;
  1235. if (cm == NULL || cm->type == NID_undef)
  1236. return 1;
  1237. /*-
  1238. * According to draft-ietf-tls-compression-04.txt, the
  1239. * compression number ranges should be the following:
  1240. *
  1241. * 0 to 63: methods defined by the IETF
  1242. * 64 to 192: external party methods assigned by IANA
  1243. * 193 to 255: reserved for private use
  1244. */
  1245. if (id < 193 || id > 255) {
  1246. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
  1247. SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
  1248. return 0;
  1249. }
  1250. MemCheck_off();
  1251. comp = (SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  1252. comp->id = id;
  1253. comp->method = cm;
  1254. load_builtin_compressions();
  1255. if (ssl_comp_methods && sk_SSL_COMP_find(ssl_comp_methods, comp) >= 0) {
  1256. OPENSSL_free(comp);
  1257. MemCheck_on();
  1258. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
  1259. SSL_R_DUPLICATE_COMPRESSION_ID);
  1260. return (1);
  1261. } else if ((ssl_comp_methods == NULL)
  1262. || !sk_SSL_COMP_push(ssl_comp_methods, comp)) {
  1263. OPENSSL_free(comp);
  1264. MemCheck_on();
  1265. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
  1266. return (1);
  1267. } else {
  1268. MemCheck_on();
  1269. return (0);
  1270. }
  1271. }
  1272. const char *SSL_COMP_get_name(const COMP_METHOD *comp)
  1273. {
  1274. if (comp)
  1275. return comp->name;
  1276. return NULL;
  1277. }
  1278. #endif