ssl_ciph.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * ECC cipher suite support in OpenSSL originally developed by
  61. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  62. */
  63. /* ====================================================================
  64. * Copyright 2005 Nokia. All rights reserved.
  65. *
  66. * The portions of the attached software ("Contribution") is developed by
  67. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  68. * license.
  69. *
  70. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  71. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  72. * support (see RFC 4279) to OpenSSL.
  73. *
  74. * No patent licenses or other rights except those expressly stated in
  75. * the OpenSSL open source license shall be deemed granted or received
  76. * expressly, by implication, estoppel, or otherwise.
  77. *
  78. * No assurances are provided by Nokia that the Contribution does not
  79. * infringe the patent or other intellectual property rights of any third
  80. * party or that the license provides you with all the necessary rights
  81. * to make use of the Contribution.
  82. *
  83. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  84. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  85. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  86. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  87. * OTHERWISE.
  88. */
  89. #include <stdio.h>
  90. #include <openssl/objects.h>
  91. #include <openssl/comp.h>
  92. #include "ssl_locl.h"
  93. #define SSL_ENC_DES_IDX 0
  94. #define SSL_ENC_3DES_IDX 1
  95. #define SSL_ENC_RC4_IDX 2
  96. #define SSL_ENC_RC2_IDX 3
  97. #define SSL_ENC_IDEA_IDX 4
  98. #define SSL_ENC_eFZA_IDX 5
  99. #define SSL_ENC_NULL_IDX 6
  100. #define SSL_ENC_AES128_IDX 7
  101. #define SSL_ENC_AES256_IDX 8
  102. #define SSL_ENC_NUM_IDX 9
  103. static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
  104. NULL,NULL,NULL,NULL,NULL,NULL,
  105. };
  106. #define SSL_COMP_NULL_IDX 0
  107. #define SSL_COMP_ZLIB_IDX 1
  108. #define SSL_COMP_NUM_IDX 2
  109. static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL;
  110. #define SSL_MD_MD5_IDX 0
  111. #define SSL_MD_SHA1_IDX 1
  112. #define SSL_MD_NUM_IDX 2
  113. static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
  114. NULL,NULL,
  115. };
  116. #define CIPHER_ADD 1
  117. #define CIPHER_KILL 2
  118. #define CIPHER_DEL 3
  119. #define CIPHER_ORD 4
  120. #define CIPHER_SPECIAL 5
  121. typedef struct cipher_order_st
  122. {
  123. SSL_CIPHER *cipher;
  124. int active;
  125. int dead;
  126. struct cipher_order_st *next,*prev;
  127. } CIPHER_ORDER;
  128. static const SSL_CIPHER cipher_aliases[]={
  129. /* Don't include eNULL unless specifically enabled. */
  130. /* Don't include ECC in ALL because these ciphers are not yet official. */
  131. {0,SSL_TXT_ALL, 0,SSL_ALL & ~SSL_eNULL & ~SSL_kECDH & ~SSL_kECDHE, SSL_ALL ,0,0,0,SSL_ALL,SSL_ALL}, /* must be first */
  132. /* TODO: COMPLEMENT OF ALL and COMPLEMENT OF DEFAULT do not have ECC cipher suites handled properly. */
  133. {0,SSL_TXT_CMPALL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0}, /* COMPLEMENT OF ALL */
  134. {0,SSL_TXT_CMPDEF,0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK,0},
  135. {0,SSL_TXT_kKRB5,0,SSL_kKRB5,0,0,0,0,SSL_MKEY_MASK,0}, /* VRS Kerberos5 */
  136. {0,SSL_TXT_kRSA,0,SSL_kRSA, 0,0,0,0,SSL_MKEY_MASK,0},
  137. {0,SSL_TXT_kDHr,0,SSL_kDHr, 0,0,0,0,SSL_MKEY_MASK,0},
  138. {0,SSL_TXT_kDHd,0,SSL_kDHd, 0,0,0,0,SSL_MKEY_MASK,0},
  139. {0,SSL_TXT_kEDH,0,SSL_kEDH, 0,0,0,0,SSL_MKEY_MASK,0},
  140. {0,SSL_TXT_kFZA,0,SSL_kFZA, 0,0,0,0,SSL_MKEY_MASK,0},
  141. {0,SSL_TXT_kPSK,0,SSL_kPSK, 0,0,0,0,SSL_MKEY_MASK,0},
  142. {0,SSL_TXT_DH, 0,SSL_DH, 0,0,0,0,SSL_MKEY_MASK,0},
  143. {0,SSL_TXT_ECC, 0,(SSL_kECDH|SSL_kECDHE), 0,0,0,0,SSL_MKEY_MASK,0},
  144. {0,SSL_TXT_EDH, 0,SSL_EDH, 0,0,0,0,SSL_MKEY_MASK|SSL_AUTH_MASK,0},
  145. {0,SSL_TXT_aKRB5,0,SSL_aKRB5,0,0,0,0,SSL_AUTH_MASK,0}, /* VRS Kerberos5 */
  146. {0,SSL_TXT_aRSA,0,SSL_aRSA, 0,0,0,0,SSL_AUTH_MASK,0},
  147. {0,SSL_TXT_aDSS,0,SSL_aDSS, 0,0,0,0,SSL_AUTH_MASK,0},
  148. {0,SSL_TXT_aFZA,0,SSL_aFZA, 0,0,0,0,SSL_AUTH_MASK,0},
  149. {0,SSL_TXT_aPSK,0,SSL_aPSK, 0,0,0,0,SSL_AUTH_MASK,0},
  150. {0,SSL_TXT_aNULL,0,SSL_aNULL,0,0,0,0,SSL_AUTH_MASK,0},
  151. {0,SSL_TXT_aDH, 0,SSL_aDH, 0,0,0,0,SSL_AUTH_MASK,0},
  152. {0,SSL_TXT_DSS, 0,SSL_DSS, 0,0,0,0,SSL_AUTH_MASK,0},
  153. {0,SSL_TXT_DES, 0,SSL_DES, 0,0,0,0,SSL_ENC_MASK,0},
  154. {0,SSL_TXT_3DES,0,SSL_3DES, 0,0,0,0,SSL_ENC_MASK,0},
  155. {0,SSL_TXT_RC4, 0,SSL_RC4, 0,0,0,0,SSL_ENC_MASK,0},
  156. {0,SSL_TXT_RC2, 0,SSL_RC2, 0,0,0,0,SSL_ENC_MASK,0},
  157. #ifndef OPENSSL_NO_IDEA
  158. {0,SSL_TXT_IDEA,0,SSL_IDEA, 0,0,0,0,SSL_ENC_MASK,0},
  159. #endif
  160. {0,SSL_TXT_eNULL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0},
  161. {0,SSL_TXT_eFZA,0,SSL_eFZA, 0,0,0,0,SSL_ENC_MASK,0},
  162. {0,SSL_TXT_AES, 0,SSL_AES, 0,0,0,0,SSL_ENC_MASK,0},
  163. {0,SSL_TXT_MD5, 0,SSL_MD5, 0,0,0,0,SSL_MAC_MASK,0},
  164. {0,SSL_TXT_SHA1,0,SSL_SHA1, 0,0,0,0,SSL_MAC_MASK,0},
  165. {0,SSL_TXT_SHA, 0,SSL_SHA, 0,0,0,0,SSL_MAC_MASK,0},
  166. {0,SSL_TXT_NULL,0,SSL_NULL, 0,0,0,0,SSL_ENC_MASK,0},
  167. {0,SSL_TXT_KRB5,0,SSL_KRB5, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
  168. {0,SSL_TXT_RSA, 0,SSL_RSA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
  169. {0,SSL_TXT_ADH, 0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
  170. {0,SSL_TXT_FZA, 0,SSL_FZA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK,0},
  171. {0,SSL_TXT_PSK, 0,SSL_PSK, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
  172. {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,0,0,0,SSL_SSL_MASK,0},
  173. {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,0,0,0,SSL_SSL_MASK,0},
  174. {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,0,0,0,SSL_SSL_MASK,0},
  175. {0,SSL_TXT_EXP ,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
  176. {0,SSL_TXT_EXPORT,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
  177. {0,SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0,0,0,0,SSL_STRONG_MASK},
  178. {0,SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0,0,0,0,SSL_STRONG_MASK},
  179. {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK},
  180. {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK},
  181. {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
  182. };
  183. void ssl_load_ciphers(void)
  184. {
  185. ssl_cipher_methods[SSL_ENC_DES_IDX]=
  186. EVP_get_cipherbyname(SN_des_cbc);
  187. ssl_cipher_methods[SSL_ENC_3DES_IDX]=
  188. EVP_get_cipherbyname(SN_des_ede3_cbc);
  189. ssl_cipher_methods[SSL_ENC_RC4_IDX]=
  190. EVP_get_cipherbyname(SN_rc4);
  191. ssl_cipher_methods[SSL_ENC_RC2_IDX]=
  192. EVP_get_cipherbyname(SN_rc2_cbc);
  193. #ifndef OPENSSL_NO_IDEA
  194. ssl_cipher_methods[SSL_ENC_IDEA_IDX]=
  195. EVP_get_cipherbyname(SN_idea_cbc);
  196. #else
  197. ssl_cipher_methods[SSL_ENC_IDEA_IDX]= NULL;
  198. #endif
  199. ssl_cipher_methods[SSL_ENC_AES128_IDX]=
  200. EVP_get_cipherbyname(SN_aes_128_cbc);
  201. ssl_cipher_methods[SSL_ENC_AES256_IDX]=
  202. EVP_get_cipherbyname(SN_aes_256_cbc);
  203. ssl_digest_methods[SSL_MD_MD5_IDX]=
  204. EVP_get_digestbyname(SN_md5);
  205. ssl_digest_methods[SSL_MD_SHA1_IDX]=
  206. EVP_get_digestbyname(SN_sha1);
  207. }
  208. #ifndef OPENSSL_NO_COMP
  209. static int sk_comp_cmp(const SSL_COMP * const *a,
  210. const SSL_COMP * const *b)
  211. {
  212. return((*a)->id-(*b)->id);
  213. }
  214. static void load_builtin_compressions(void)
  215. {
  216. if (ssl_comp_methods != NULL)
  217. return;
  218. CRYPTO_w_lock(CRYPTO_LOCK_SSL);
  219. if (ssl_comp_methods == NULL)
  220. {
  221. SSL_COMP *comp = NULL;
  222. MemCheck_off();
  223. ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
  224. if (ssl_comp_methods != NULL)
  225. {
  226. comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  227. if (comp != NULL)
  228. {
  229. comp->method=COMP_zlib();
  230. if (comp->method
  231. && comp->method->type == NID_undef)
  232. OPENSSL_free(comp);
  233. else
  234. {
  235. comp->id=SSL_COMP_ZLIB_IDX;
  236. comp->name=comp->method->name;
  237. sk_SSL_COMP_push(ssl_comp_methods,comp);
  238. }
  239. }
  240. }
  241. MemCheck_on();
  242. }
  243. CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
  244. }
  245. #endif
  246. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  247. const EVP_MD **md, SSL_COMP **comp)
  248. {
  249. int i;
  250. SSL_CIPHER *c;
  251. c=s->cipher;
  252. if (c == NULL) return(0);
  253. if (comp != NULL)
  254. {
  255. SSL_COMP ctmp;
  256. #ifndef OPENSSL_NO_COMP
  257. load_builtin_compressions();
  258. #endif
  259. *comp=NULL;
  260. ctmp.id=s->compress_meth;
  261. if (ssl_comp_methods != NULL)
  262. {
  263. i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
  264. if (i >= 0)
  265. *comp=sk_SSL_COMP_value(ssl_comp_methods,i);
  266. else
  267. *comp=NULL;
  268. }
  269. }
  270. if ((enc == NULL) || (md == NULL)) return(0);
  271. switch (c->algorithms & SSL_ENC_MASK)
  272. {
  273. case SSL_DES:
  274. i=SSL_ENC_DES_IDX;
  275. break;
  276. case SSL_3DES:
  277. i=SSL_ENC_3DES_IDX;
  278. break;
  279. case SSL_RC4:
  280. i=SSL_ENC_RC4_IDX;
  281. break;
  282. case SSL_RC2:
  283. i=SSL_ENC_RC2_IDX;
  284. break;
  285. case SSL_IDEA:
  286. i=SSL_ENC_IDEA_IDX;
  287. break;
  288. case SSL_eNULL:
  289. i=SSL_ENC_NULL_IDX;
  290. break;
  291. case SSL_AES:
  292. switch(c->alg_bits)
  293. {
  294. case 128: i=SSL_ENC_AES128_IDX; break;
  295. case 256: i=SSL_ENC_AES256_IDX; break;
  296. default: i=-1; break;
  297. }
  298. break;
  299. default:
  300. i= -1;
  301. break;
  302. }
  303. if ((i < 0) || (i > SSL_ENC_NUM_IDX))
  304. *enc=NULL;
  305. else
  306. {
  307. if (i == SSL_ENC_NULL_IDX)
  308. *enc=EVP_enc_null();
  309. else
  310. *enc=ssl_cipher_methods[i];
  311. }
  312. switch (c->algorithms & SSL_MAC_MASK)
  313. {
  314. case SSL_MD5:
  315. i=SSL_MD_MD5_IDX;
  316. break;
  317. case SSL_SHA1:
  318. i=SSL_MD_SHA1_IDX;
  319. break;
  320. default:
  321. i= -1;
  322. break;
  323. }
  324. if ((i < 0) || (i > SSL_MD_NUM_IDX))
  325. *md=NULL;
  326. else
  327. *md=ssl_digest_methods[i];
  328. if ((*enc != NULL) && (*md != NULL))
  329. return(1);
  330. else
  331. return(0);
  332. }
  333. #define ITEM_SEP(a) \
  334. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  335. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  336. CIPHER_ORDER **tail)
  337. {
  338. if (curr == *tail) return;
  339. if (curr == *head)
  340. *head=curr->next;
  341. if (curr->prev != NULL)
  342. curr->prev->next=curr->next;
  343. if (curr->next != NULL) /* should always be true */
  344. curr->next->prev=curr->prev;
  345. (*tail)->next=curr;
  346. curr->prev= *tail;
  347. curr->next=NULL;
  348. *tail=curr;
  349. }
  350. static unsigned long ssl_cipher_get_disabled(void)
  351. {
  352. unsigned long mask;
  353. mask = SSL_kFZA;
  354. #ifdef OPENSSL_NO_RSA
  355. mask |= SSL_aRSA|SSL_kRSA;
  356. #endif
  357. #ifdef OPENSSL_NO_DSA
  358. mask |= SSL_aDSS;
  359. #endif
  360. #ifdef OPENSSL_NO_DH
  361. mask |= SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH;
  362. #endif
  363. #ifdef OPENSSL_NO_KRB5
  364. mask |= SSL_kKRB5|SSL_aKRB5;
  365. #endif
  366. #ifdef OPENSSL_NO_ECDH
  367. mask |= SSL_kECDH|SSL_kECDHE;
  368. #endif
  369. #ifdef OPENSSL_NO_PSK
  370. mask |= SSL_kPSK;
  371. #endif
  372. #ifdef SSL_FORBID_ENULL
  373. mask |= SSL_eNULL;
  374. #endif
  375. mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
  376. mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
  377. mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
  378. mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
  379. mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
  380. mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0;
  381. mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES:0;
  382. mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
  383. mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
  384. return(mask);
  385. }
  386. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  387. int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list,
  388. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  389. {
  390. int i, co_list_num;
  391. SSL_CIPHER *c;
  392. /*
  393. * We have num_of_ciphers descriptions compiled in, depending on the
  394. * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
  395. * These will later be sorted in a linked list with at most num
  396. * entries.
  397. */
  398. /* Get the initial list of ciphers */
  399. co_list_num = 0; /* actual count of ciphers */
  400. for (i = 0; i < num_of_ciphers; i++)
  401. {
  402. c = ssl_method->get_cipher(i);
  403. /* drop those that use any of that is not available */
  404. if ((c != NULL) && c->valid && !(c->algorithms & mask))
  405. {
  406. co_list[co_list_num].cipher = c;
  407. co_list[co_list_num].next = NULL;
  408. co_list[co_list_num].prev = NULL;
  409. co_list[co_list_num].active = 0;
  410. co_list_num++;
  411. #ifdef KSSL_DEBUG
  412. printf("\t%d: %s %lx %lx\n",i,c->name,c->id,c->algorithms);
  413. #endif /* KSSL_DEBUG */
  414. /*
  415. if (!sk_push(ca_list,(char *)c)) goto err;
  416. */
  417. }
  418. }
  419. /*
  420. * Prepare linked list from list entries
  421. */
  422. for (i = 1; i < co_list_num - 1; i++)
  423. {
  424. co_list[i].prev = &(co_list[i-1]);
  425. co_list[i].next = &(co_list[i+1]);
  426. }
  427. if (co_list_num > 0)
  428. {
  429. (*head_p) = &(co_list[0]);
  430. (*head_p)->prev = NULL;
  431. (*head_p)->next = &(co_list[1]);
  432. (*tail_p) = &(co_list[co_list_num - 1]);
  433. (*tail_p)->prev = &(co_list[co_list_num - 2]);
  434. (*tail_p)->next = NULL;
  435. }
  436. }
  437. static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
  438. int num_of_group_aliases, unsigned long mask,
  439. CIPHER_ORDER *head)
  440. {
  441. CIPHER_ORDER *ciph_curr;
  442. SSL_CIPHER **ca_curr;
  443. int i;
  444. /*
  445. * First, add the real ciphers as already collected
  446. */
  447. ciph_curr = head;
  448. ca_curr = ca_list;
  449. while (ciph_curr != NULL)
  450. {
  451. *ca_curr = ciph_curr->cipher;
  452. ca_curr++;
  453. ciph_curr = ciph_curr->next;
  454. }
  455. /*
  456. * Now we add the available ones from the cipher_aliases[] table.
  457. * They represent either an algorithm, that must be fully
  458. * supported (not match any bit in mask) or represent a cipher
  459. * strength value (will be added in any case because algorithms=0).
  460. */
  461. for (i = 0; i < num_of_group_aliases; i++)
  462. {
  463. if ((i == 0) || /* always fetch "ALL" */
  464. !(cipher_aliases[i].algorithms & mask))
  465. {
  466. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  467. ca_curr++;
  468. }
  469. }
  470. *ca_curr = NULL; /* end of list */
  471. }
  472. static void ssl_cipher_apply_rule(unsigned long cipher_id,
  473. unsigned long algorithms, unsigned long mask,
  474. unsigned long algo_strength, unsigned long mask_strength,
  475. int rule, int strength_bits, CIPHER_ORDER *co_list,
  476. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  477. {
  478. CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2;
  479. SSL_CIPHER *cp;
  480. unsigned long ma, ma_s;
  481. #ifdef CIPHER_DEBUG
  482. printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n",
  483. rule, algorithms, mask, algo_strength, mask_strength,
  484. strength_bits);
  485. #endif
  486. curr = head = *head_p;
  487. curr2 = head;
  488. tail2 = tail = *tail_p;
  489. for (;;)
  490. {
  491. if ((curr == NULL) || (curr == tail2)) break;
  492. curr = curr2;
  493. curr2 = curr->next;
  494. cp = curr->cipher;
  495. /* If explicit cipher suite match that one only */
  496. if (cipher_id)
  497. {
  498. if (cp->id != cipher_id)
  499. continue;
  500. }
  501. /*
  502. * Selection criteria is either the number of strength_bits
  503. * or the algorithm used.
  504. */
  505. else if (strength_bits == -1)
  506. {
  507. ma = mask & cp->algorithms;
  508. ma_s = mask_strength & cp->algo_strength;
  509. #ifdef CIPHER_DEBUG
  510. printf("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n", cp->name, cp->algorithms, cp->algo_strength, mask, mask_strength);
  511. printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n", ma, ma_s, ma&algorithms, ma_s&algo_strength);
  512. #endif
  513. /*
  514. * Select: if none of the mask bit was met from the
  515. * cipher or not all of the bits were met, the
  516. * selection does not apply.
  517. */
  518. if (((ma == 0) && (ma_s == 0)) ||
  519. ((ma & algorithms) != ma) ||
  520. ((ma_s & algo_strength) != ma_s))
  521. continue; /* does not apply */
  522. }
  523. else if (strength_bits != cp->strength_bits)
  524. continue; /* does not apply */
  525. #ifdef CIPHER_DEBUG
  526. printf("Action = %d\n", rule);
  527. #endif
  528. /* add the cipher if it has not been added yet. */
  529. if (rule == CIPHER_ADD)
  530. {
  531. if (!curr->active)
  532. {
  533. ll_append_tail(&head, curr, &tail);
  534. curr->active = 1;
  535. }
  536. }
  537. /* Move the added cipher to this location */
  538. else if (rule == CIPHER_ORD)
  539. {
  540. if (curr->active)
  541. {
  542. ll_append_tail(&head, curr, &tail);
  543. }
  544. }
  545. else if (rule == CIPHER_DEL)
  546. curr->active = 0;
  547. else if (rule == CIPHER_KILL)
  548. {
  549. if (head == curr)
  550. head = curr->next;
  551. else
  552. curr->prev->next = curr->next;
  553. if (tail == curr)
  554. tail = curr->prev;
  555. curr->active = 0;
  556. if (curr->next != NULL)
  557. curr->next->prev = curr->prev;
  558. if (curr->prev != NULL)
  559. curr->prev->next = curr->next;
  560. curr->next = NULL;
  561. curr->prev = NULL;
  562. }
  563. }
  564. *head_p = head;
  565. *tail_p = tail;
  566. }
  567. static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list,
  568. CIPHER_ORDER **head_p,
  569. CIPHER_ORDER **tail_p)
  570. {
  571. int max_strength_bits, i, *number_uses;
  572. CIPHER_ORDER *curr;
  573. /*
  574. * This routine sorts the ciphers with descending strength. The sorting
  575. * must keep the pre-sorted sequence, so we apply the normal sorting
  576. * routine as '+' movement to the end of the list.
  577. */
  578. max_strength_bits = 0;
  579. curr = *head_p;
  580. while (curr != NULL)
  581. {
  582. if (curr->active &&
  583. (curr->cipher->strength_bits > max_strength_bits))
  584. max_strength_bits = curr->cipher->strength_bits;
  585. curr = curr->next;
  586. }
  587. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  588. if (!number_uses)
  589. {
  590. SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE);
  591. return(0);
  592. }
  593. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  594. /*
  595. * Now find the strength_bits values actually used
  596. */
  597. curr = *head_p;
  598. while (curr != NULL)
  599. {
  600. if (curr->active)
  601. number_uses[curr->cipher->strength_bits]++;
  602. curr = curr->next;
  603. }
  604. /*
  605. * Go through the list of used strength_bits values in descending
  606. * order.
  607. */
  608. for (i = max_strength_bits; i >= 0; i--)
  609. if (number_uses[i] > 0)
  610. ssl_cipher_apply_rule(0, 0, 0, 0, 0, CIPHER_ORD, i,
  611. co_list, head_p, tail_p);
  612. OPENSSL_free(number_uses);
  613. return(1);
  614. }
  615. static int ssl_cipher_process_rulestr(const char *rule_str,
  616. CIPHER_ORDER *co_list, CIPHER_ORDER **head_p,
  617. CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list)
  618. {
  619. unsigned long algorithms, mask, algo_strength, mask_strength;
  620. const char *l, *start, *buf;
  621. int j, multi, found, rule, retval, ok, buflen;
  622. unsigned long cipher_id;
  623. char ch;
  624. retval = 1;
  625. l = rule_str;
  626. for (;;)
  627. {
  628. ch = *l;
  629. if (ch == '\0')
  630. break; /* done */
  631. if (ch == '-')
  632. { rule = CIPHER_DEL; l++; }
  633. else if (ch == '+')
  634. { rule = CIPHER_ORD; l++; }
  635. else if (ch == '!')
  636. { rule = CIPHER_KILL; l++; }
  637. else if (ch == '@')
  638. { rule = CIPHER_SPECIAL; l++; }
  639. else
  640. { rule = CIPHER_ADD; }
  641. if (ITEM_SEP(ch))
  642. {
  643. l++;
  644. continue;
  645. }
  646. algorithms = mask = algo_strength = mask_strength = 0;
  647. start=l;
  648. for (;;)
  649. {
  650. ch = *l;
  651. buf = l;
  652. buflen = 0;
  653. #ifndef CHARSET_EBCDIC
  654. while ( ((ch >= 'A') && (ch <= 'Z')) ||
  655. ((ch >= '0') && (ch <= '9')) ||
  656. ((ch >= 'a') && (ch <= 'z')) ||
  657. (ch == '-'))
  658. #else
  659. while ( isalnum(ch) || (ch == '-'))
  660. #endif
  661. {
  662. ch = *(++l);
  663. buflen++;
  664. }
  665. if (buflen == 0)
  666. {
  667. /*
  668. * We hit something we cannot deal with,
  669. * it is no command or separator nor
  670. * alphanumeric, so we call this an error.
  671. */
  672. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  673. SSL_R_INVALID_COMMAND);
  674. retval = found = 0;
  675. l++;
  676. break;
  677. }
  678. if (rule == CIPHER_SPECIAL)
  679. {
  680. found = 0; /* unused -- avoid compiler warning */
  681. break; /* special treatment */
  682. }
  683. /* check for multi-part specification */
  684. if (ch == '+')
  685. {
  686. multi=1;
  687. l++;
  688. }
  689. else
  690. multi=0;
  691. /*
  692. * Now search for the cipher alias in the ca_list. Be careful
  693. * with the strncmp, because the "buflen" limitation
  694. * will make the rule "ADH:SOME" and the cipher
  695. * "ADH-MY-CIPHER" look like a match for buflen=3.
  696. * So additionally check whether the cipher name found
  697. * has the correct length. We can save a strlen() call:
  698. * just checking for the '\0' at the right place is
  699. * sufficient, we have to strncmp() anyway. (We cannot
  700. * use strcmp(), because buf is not '\0' terminated.)
  701. */
  702. j = found = 0;
  703. cipher_id = 0;
  704. while (ca_list[j])
  705. {
  706. if (!strncmp(buf, ca_list[j]->name, buflen) &&
  707. (ca_list[j]->name[buflen] == '\0'))
  708. {
  709. found = 1;
  710. break;
  711. }
  712. else
  713. j++;
  714. }
  715. if (!found)
  716. break; /* ignore this entry */
  717. if (ca_list[j]->valid)
  718. {
  719. cipher_id = ca_list[j]->id;
  720. break;
  721. }
  722. /* New algorithms:
  723. * 1 - any old restrictions apply outside new mask
  724. * 2 - any new restrictions apply outside old mask
  725. * 3 - enforce old & new where masks intersect
  726. */
  727. algorithms = (algorithms & ~ca_list[j]->mask) | /* 1 */
  728. (ca_list[j]->algorithms & ~mask) | /* 2 */
  729. (algorithms & ca_list[j]->algorithms); /* 3 */
  730. mask |= ca_list[j]->mask;
  731. algo_strength = (algo_strength & ~ca_list[j]->mask_strength) |
  732. (ca_list[j]->algo_strength & ~mask_strength) |
  733. (algo_strength & ca_list[j]->algo_strength);
  734. mask_strength |= ca_list[j]->mask_strength;
  735. if (!multi) break;
  736. }
  737. /*
  738. * Ok, we have the rule, now apply it
  739. */
  740. if (rule == CIPHER_SPECIAL)
  741. { /* special command */
  742. ok = 0;
  743. if ((buflen == 8) &&
  744. !strncmp(buf, "STRENGTH", 8))
  745. ok = ssl_cipher_strength_sort(co_list,
  746. head_p, tail_p);
  747. else
  748. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  749. SSL_R_INVALID_COMMAND);
  750. if (ok == 0)
  751. retval = 0;
  752. /*
  753. * We do not support any "multi" options
  754. * together with "@", so throw away the
  755. * rest of the command, if any left, until
  756. * end or ':' is found.
  757. */
  758. while ((*l != '\0') && ITEM_SEP(*l))
  759. l++;
  760. }
  761. else if (found)
  762. {
  763. ssl_cipher_apply_rule(cipher_id, algorithms, mask,
  764. algo_strength, mask_strength, rule, -1,
  765. co_list, head_p, tail_p);
  766. }
  767. else
  768. {
  769. while ((*l != '\0') && ITEM_SEP(*l))
  770. l++;
  771. }
  772. if (*l == '\0') break; /* done */
  773. }
  774. return(retval);
  775. }
  776. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  777. STACK_OF(SSL_CIPHER) **cipher_list,
  778. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  779. const char *rule_str)
  780. {
  781. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  782. unsigned long disabled_mask;
  783. STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
  784. const char *rule_p;
  785. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  786. SSL_CIPHER **ca_list = NULL;
  787. /*
  788. * Return with error if nothing to do.
  789. */
  790. if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
  791. return NULL;
  792. /*
  793. * To reduce the work to do we only want to process the compiled
  794. * in algorithms, so we first get the mask of disabled ciphers.
  795. */
  796. disabled_mask = ssl_cipher_get_disabled();
  797. /*
  798. * Now we have to collect the available ciphers from the compiled
  799. * in ciphers. We cannot get more than the number compiled in, so
  800. * it is used for allocation.
  801. */
  802. num_of_ciphers = ssl_method->num_ciphers();
  803. #ifdef KSSL_DEBUG
  804. printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
  805. #endif /* KSSL_DEBUG */
  806. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  807. if (co_list == NULL)
  808. {
  809. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  810. return(NULL); /* Failure */
  811. }
  812. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
  813. co_list, &head, &tail);
  814. /*
  815. * We also need cipher aliases for selecting based on the rule_str.
  816. * There might be two types of entries in the rule_str: 1) names
  817. * of ciphers themselves 2) aliases for groups of ciphers.
  818. * For 1) we need the available ciphers and for 2) the cipher
  819. * groups of cipher_aliases added together in one list (otherwise
  820. * we would be happy with just the cipher_aliases table).
  821. */
  822. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  823. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  824. ca_list =
  825. (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
  826. if (ca_list == NULL)
  827. {
  828. OPENSSL_free(co_list);
  829. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  830. return(NULL); /* Failure */
  831. }
  832. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask,
  833. head);
  834. /*
  835. * If the rule_string begins with DEFAULT, apply the default rule
  836. * before using the (possibly available) additional rules.
  837. */
  838. ok = 1;
  839. rule_p = rule_str;
  840. if (strncmp(rule_str,"DEFAULT",7) == 0)
  841. {
  842. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  843. co_list, &head, &tail, ca_list);
  844. rule_p += 7;
  845. if (*rule_p == ':')
  846. rule_p++;
  847. }
  848. if (ok && (strlen(rule_p) > 0))
  849. ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail,
  850. ca_list);
  851. OPENSSL_free(ca_list); /* Not needed anymore */
  852. if (!ok)
  853. { /* Rule processing failure */
  854. OPENSSL_free(co_list);
  855. return(NULL);
  856. }
  857. /*
  858. * Allocate new "cipherstack" for the result, return with error
  859. * if we cannot get one.
  860. */
  861. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
  862. {
  863. OPENSSL_free(co_list);
  864. return(NULL);
  865. }
  866. /*
  867. * The cipher selection for the list is done. The ciphers are added
  868. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  869. */
  870. for (curr = head; curr != NULL; curr = curr->next)
  871. {
  872. if (curr->active)
  873. {
  874. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  875. #ifdef CIPHER_DEBUG
  876. printf("<%s>\n",curr->cipher->name);
  877. #endif
  878. }
  879. }
  880. OPENSSL_free(co_list); /* Not needed any longer */
  881. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  882. if (tmp_cipher_list == NULL)
  883. {
  884. sk_SSL_CIPHER_free(cipherstack);
  885. return NULL;
  886. }
  887. if (*cipher_list != NULL)
  888. sk_SSL_CIPHER_free(*cipher_list);
  889. *cipher_list = cipherstack;
  890. if (*cipher_list_by_id != NULL)
  891. sk_SSL_CIPHER_free(*cipher_list_by_id);
  892. *cipher_list_by_id = tmp_cipher_list;
  893. sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
  894. return(cipherstack);
  895. }
  896. char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
  897. {
  898. int is_export,pkl,kl;
  899. const char *ver,*exp_str;
  900. const char *kx,*au,*enc,*mac;
  901. unsigned long alg,alg2,alg_s;
  902. #ifdef KSSL_DEBUG
  903. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n";
  904. #else
  905. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
  906. #endif /* KSSL_DEBUG */
  907. alg=cipher->algorithms;
  908. alg_s=cipher->algo_strength;
  909. alg2=cipher->algorithm2;
  910. is_export=SSL_C_IS_EXPORT(cipher);
  911. pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  912. kl=SSL_C_EXPORT_KEYLENGTH(cipher);
  913. exp_str=is_export?" export":"";
  914. if (alg & SSL_SSLV2)
  915. ver="SSLv2";
  916. else if (alg & SSL_SSLV3)
  917. ver="SSLv3";
  918. else
  919. ver="unknown";
  920. switch (alg&SSL_MKEY_MASK)
  921. {
  922. case SSL_kRSA:
  923. kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
  924. break;
  925. case SSL_kDHr:
  926. kx="DH/RSA";
  927. break;
  928. case SSL_kDHd:
  929. kx="DH/DSS";
  930. break;
  931. case SSL_kKRB5: /* VRS */
  932. case SSL_KRB5: /* VRS */
  933. kx="KRB5";
  934. break;
  935. case SSL_kFZA:
  936. kx="Fortezza";
  937. break;
  938. case SSL_kEDH:
  939. kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
  940. break;
  941. case SSL_kECDH:
  942. case SSL_kECDHE:
  943. kx=is_export?"ECDH(<=163)":"ECDH";
  944. break;
  945. case SSL_kPSK:
  946. kx="PSK";
  947. break;
  948. default:
  949. kx="unknown";
  950. }
  951. switch (alg&SSL_AUTH_MASK)
  952. {
  953. case SSL_aRSA:
  954. au="RSA";
  955. break;
  956. case SSL_aDSS:
  957. au="DSS";
  958. break;
  959. case SSL_aDH:
  960. au="DH";
  961. break;
  962. case SSL_aKRB5: /* VRS */
  963. case SSL_KRB5: /* VRS */
  964. au="KRB5";
  965. break;
  966. case SSL_aFZA:
  967. case SSL_aNULL:
  968. au="None";
  969. break;
  970. case SSL_aECDSA:
  971. au="ECDSA";
  972. break;
  973. case SSL_aPSK:
  974. au="PSK";
  975. break;
  976. default:
  977. au="unknown";
  978. break;
  979. }
  980. switch (alg&SSL_ENC_MASK)
  981. {
  982. case SSL_DES:
  983. enc=(is_export && kl == 5)?"DES(40)":"DES(56)";
  984. break;
  985. case SSL_3DES:
  986. enc="3DES(168)";
  987. break;
  988. case SSL_RC4:
  989. enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
  990. :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
  991. break;
  992. case SSL_RC2:
  993. enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
  994. break;
  995. case SSL_IDEA:
  996. enc="IDEA(128)";
  997. break;
  998. case SSL_eFZA:
  999. enc="Fortezza";
  1000. break;
  1001. case SSL_eNULL:
  1002. enc="None";
  1003. break;
  1004. case SSL_AES:
  1005. switch(cipher->strength_bits)
  1006. {
  1007. case 128: enc="AES(128)"; break;
  1008. case 192: enc="AES(192)"; break;
  1009. case 256: enc="AES(256)"; break;
  1010. default: enc="AES(?""?""?)"; break;
  1011. }
  1012. break;
  1013. default:
  1014. enc="unknown";
  1015. break;
  1016. }
  1017. switch (alg&SSL_MAC_MASK)
  1018. {
  1019. case SSL_MD5:
  1020. mac="MD5";
  1021. break;
  1022. case SSL_SHA1:
  1023. mac="SHA1";
  1024. break;
  1025. default:
  1026. mac="unknown";
  1027. break;
  1028. }
  1029. if (buf == NULL)
  1030. {
  1031. len=128;
  1032. buf=OPENSSL_malloc(len);
  1033. if (buf == NULL) return("OPENSSL_malloc Error");
  1034. }
  1035. else if (len < 128)
  1036. return("Buffer too small");
  1037. #ifdef KSSL_DEBUG
  1038. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg);
  1039. #else
  1040. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
  1041. #endif /* KSSL_DEBUG */
  1042. return(buf);
  1043. }
  1044. char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1045. {
  1046. int i;
  1047. if (c == NULL) return("(NONE)");
  1048. i=(int)(c->id>>24L);
  1049. if (i == 3)
  1050. return("TLSv1/SSLv3");
  1051. else if (i == 2)
  1052. return("SSLv2");
  1053. else
  1054. return("unknown");
  1055. }
  1056. /* return the actual cipher being used */
  1057. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1058. {
  1059. if (c != NULL)
  1060. return(c->name);
  1061. return("(NONE)");
  1062. }
  1063. /* number of bits for symmetric cipher */
  1064. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1065. {
  1066. int ret=0;
  1067. if (c != NULL)
  1068. {
  1069. if (alg_bits != NULL) *alg_bits = c->alg_bits;
  1070. ret = c->strength_bits;
  1071. }
  1072. return(ret);
  1073. }
  1074. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  1075. {
  1076. SSL_COMP *ctmp;
  1077. int i,nn;
  1078. if ((n == 0) || (sk == NULL)) return(NULL);
  1079. nn=sk_SSL_COMP_num(sk);
  1080. for (i=0; i<nn; i++)
  1081. {
  1082. ctmp=sk_SSL_COMP_value(sk,i);
  1083. if (ctmp->id == n)
  1084. return(ctmp);
  1085. }
  1086. return(NULL);
  1087. }
  1088. #ifdef OPENSSL_NO_COMP
  1089. void *SSL_COMP_get_compression_methods(void)
  1090. {
  1091. return NULL;
  1092. }
  1093. int SSL_COMP_add_compression_method(int id, void *cm)
  1094. {
  1095. return 1;
  1096. }
  1097. const char *SSL_COMP_get_name(const void *comp)
  1098. {
  1099. return NULL;
  1100. }
  1101. #else
  1102. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1103. {
  1104. load_builtin_compressions();
  1105. return(ssl_comp_methods);
  1106. }
  1107. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1108. {
  1109. SSL_COMP *comp;
  1110. if (cm == NULL || cm->type == NID_undef)
  1111. return 1;
  1112. /* According to draft-ietf-tls-compression-04.txt, the
  1113. compression number ranges should be the following:
  1114. 0 to 63: methods defined by the IETF
  1115. 64 to 192: external party methods assigned by IANA
  1116. 193 to 255: reserved for private use */
  1117. if (id < 193 || id > 255)
  1118. {
  1119. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
  1120. return 0;
  1121. }
  1122. MemCheck_off();
  1123. comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  1124. comp->id=id;
  1125. comp->method=cm;
  1126. load_builtin_compressions();
  1127. if (ssl_comp_methods
  1128. && !sk_SSL_COMP_find(ssl_comp_methods,comp))
  1129. {
  1130. OPENSSL_free(comp);
  1131. MemCheck_on();
  1132. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID);
  1133. return(1);
  1134. }
  1135. else if ((ssl_comp_methods == NULL)
  1136. || !sk_SSL_COMP_push(ssl_comp_methods,comp))
  1137. {
  1138. OPENSSL_free(comp);
  1139. MemCheck_on();
  1140. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);
  1141. return(1);
  1142. }
  1143. else
  1144. {
  1145. MemCheck_on();
  1146. return(0);
  1147. }
  1148. }
  1149. const char *SSL_COMP_get_name(const COMP_METHOD *comp)
  1150. {
  1151. if (comp)
  1152. return comp->name;
  1153. return NULL;
  1154. }
  1155. #endif