ssl_ciph.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. #include <stdio.h>
  59. #include <openssl/objects.h>
  60. #include <openssl/comp.h>
  61. #include "ssl_locl.h"
  62. #define SSL_ENC_DES_IDX 0
  63. #define SSL_ENC_3DES_IDX 1
  64. #define SSL_ENC_RC4_IDX 2
  65. #define SSL_ENC_RC2_IDX 3
  66. #define SSL_ENC_IDEA_IDX 4
  67. #define SSL_ENC_eFZA_IDX 5
  68. #define SSL_ENC_NULL_IDX 6
  69. #define SSL_ENC_NUM_IDX 7
  70. static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
  71. NULL,NULL,NULL,NULL,NULL,NULL,
  72. };
  73. static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL;
  74. #define SSL_MD_MD5_IDX 0
  75. #define SSL_MD_SHA1_IDX 1
  76. #define SSL_MD_NUM_IDX 2
  77. static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
  78. NULL,NULL,
  79. };
  80. #define CIPHER_ADD 1
  81. #define CIPHER_KILL 2
  82. #define CIPHER_DEL 3
  83. #define CIPHER_ORD 4
  84. #define CIPHER_SPECIAL 5
  85. typedef struct cipher_order_st
  86. {
  87. SSL_CIPHER *cipher;
  88. int active;
  89. int dead;
  90. struct cipher_order_st *next,*prev;
  91. } CIPHER_ORDER;
  92. static const SSL_CIPHER cipher_aliases[]={
  93. /* Don't include eNULL unless specifically enabled */
  94. {0,SSL_TXT_ALL, 0,SSL_ALL & ~SSL_eNULL, SSL_ALL ,0,0,0,SSL_ALL,SSL_ALL}, /* must be first */
  95. {0,SSL_TXT_kRSA,0,SSL_kRSA, 0,0,0,0,SSL_MKEY_MASK,0},
  96. {0,SSL_TXT_kDHr,0,SSL_kDHr, 0,0,0,0,SSL_MKEY_MASK,0},
  97. {0,SSL_TXT_kDHd,0,SSL_kDHd, 0,0,0,0,SSL_MKEY_MASK,0},
  98. {0,SSL_TXT_kEDH,0,SSL_kEDH, 0,0,0,0,SSL_MKEY_MASK,0},
  99. {0,SSL_TXT_kFZA,0,SSL_kFZA, 0,0,0,0,SSL_MKEY_MASK,0},
  100. {0,SSL_TXT_DH, 0,SSL_DH, 0,0,0,0,SSL_MKEY_MASK,0},
  101. {0,SSL_TXT_EDH, 0,SSL_EDH, 0,0,0,0,SSL_MKEY_MASK|SSL_AUTH_MASK,0},
  102. {0,SSL_TXT_aRSA,0,SSL_aRSA, 0,0,0,0,SSL_AUTH_MASK,0},
  103. {0,SSL_TXT_aDSS,0,SSL_aDSS, 0,0,0,0,SSL_AUTH_MASK,0},
  104. {0,SSL_TXT_aFZA,0,SSL_aFZA, 0,0,0,0,SSL_AUTH_MASK,0},
  105. {0,SSL_TXT_aNULL,0,SSL_aNULL,0,0,0,0,SSL_AUTH_MASK,0},
  106. {0,SSL_TXT_aDH, 0,SSL_aDH, 0,0,0,0,SSL_AUTH_MASK,0},
  107. {0,SSL_TXT_DSS, 0,SSL_DSS, 0,0,0,0,SSL_AUTH_MASK,0},
  108. {0,SSL_TXT_DES, 0,SSL_DES, 0,0,0,0,SSL_ENC_MASK,0},
  109. {0,SSL_TXT_3DES,0,SSL_3DES, 0,0,0,0,SSL_ENC_MASK,0},
  110. {0,SSL_TXT_RC4, 0,SSL_RC4, 0,0,0,0,SSL_ENC_MASK,0},
  111. {0,SSL_TXT_RC2, 0,SSL_RC2, 0,0,0,0,SSL_ENC_MASK,0},
  112. {0,SSL_TXT_IDEA,0,SSL_IDEA, 0,0,0,0,SSL_ENC_MASK,0},
  113. {0,SSL_TXT_eNULL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0},
  114. {0,SSL_TXT_eFZA,0,SSL_eFZA, 0,0,0,0,SSL_ENC_MASK,0},
  115. {0,SSL_TXT_MD5, 0,SSL_MD5, 0,0,0,0,SSL_MAC_MASK,0},
  116. {0,SSL_TXT_SHA1,0,SSL_SHA1, 0,0,0,0,SSL_MAC_MASK,0},
  117. {0,SSL_TXT_SHA, 0,SSL_SHA, 0,0,0,0,SSL_MAC_MASK,0},
  118. {0,SSL_TXT_NULL,0,SSL_NULL, 0,0,0,0,SSL_ENC_MASK,0},
  119. {0,SSL_TXT_RSA, 0,SSL_RSA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
  120. {0,SSL_TXT_ADH, 0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
  121. {0,SSL_TXT_FZA, 0,SSL_FZA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK,0},
  122. {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,0,0,0,SSL_SSL_MASK,0},
  123. {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,0,0,0,SSL_SSL_MASK,0},
  124. {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,0,0,0,SSL_SSL_MASK,0},
  125. {0,SSL_TXT_EXP ,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
  126. {0,SSL_TXT_EXPORT,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
  127. {0,SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0,0,0,0,SSL_STRONG_MASK},
  128. {0,SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0,0,0,0,SSL_STRONG_MASK},
  129. {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK},
  130. {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK},
  131. {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
  132. };
  133. static int init_ciphers=1;
  134. static void load_ciphers(void)
  135. {
  136. init_ciphers=0;
  137. ssl_cipher_methods[SSL_ENC_DES_IDX]=
  138. EVP_get_cipherbyname(SN_des_cbc);
  139. ssl_cipher_methods[SSL_ENC_3DES_IDX]=
  140. EVP_get_cipherbyname(SN_des_ede3_cbc);
  141. ssl_cipher_methods[SSL_ENC_RC4_IDX]=
  142. EVP_get_cipherbyname(SN_rc4);
  143. ssl_cipher_methods[SSL_ENC_RC2_IDX]=
  144. EVP_get_cipherbyname(SN_rc2_cbc);
  145. ssl_cipher_methods[SSL_ENC_IDEA_IDX]=
  146. EVP_get_cipherbyname(SN_idea_cbc);
  147. ssl_digest_methods[SSL_MD_MD5_IDX]=
  148. EVP_get_digestbyname(SN_md5);
  149. ssl_digest_methods[SSL_MD_SHA1_IDX]=
  150. EVP_get_digestbyname(SN_sha1);
  151. }
  152. int ssl_cipher_get_evp(SSL_SESSION *s, const EVP_CIPHER **enc,
  153. const EVP_MD **md, SSL_COMP **comp)
  154. {
  155. int i;
  156. SSL_CIPHER *c;
  157. c=s->cipher;
  158. if (c == NULL) return(0);
  159. if (comp != NULL)
  160. {
  161. SSL_COMP ctmp;
  162. if (s->compress_meth == 0)
  163. *comp=NULL;
  164. else if (ssl_comp_methods == NULL)
  165. {
  166. /* bad */
  167. *comp=NULL;
  168. }
  169. else
  170. {
  171. ctmp.id=s->compress_meth;
  172. i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
  173. if (i >= 0)
  174. *comp=sk_SSL_COMP_value(ssl_comp_methods,i);
  175. else
  176. *comp=NULL;
  177. }
  178. }
  179. if ((enc == NULL) || (md == NULL)) return(0);
  180. switch (c->algorithms & SSL_ENC_MASK)
  181. {
  182. case SSL_DES:
  183. i=SSL_ENC_DES_IDX;
  184. break;
  185. case SSL_3DES:
  186. i=SSL_ENC_3DES_IDX;
  187. break;
  188. case SSL_RC4:
  189. i=SSL_ENC_RC4_IDX;
  190. break;
  191. case SSL_RC2:
  192. i=SSL_ENC_RC2_IDX;
  193. break;
  194. case SSL_IDEA:
  195. i=SSL_ENC_IDEA_IDX;
  196. break;
  197. case SSL_eNULL:
  198. i=SSL_ENC_NULL_IDX;
  199. break;
  200. default:
  201. i= -1;
  202. break;
  203. }
  204. if ((i < 0) || (i > SSL_ENC_NUM_IDX))
  205. *enc=NULL;
  206. else
  207. {
  208. if (i == SSL_ENC_NULL_IDX)
  209. *enc=EVP_enc_null();
  210. else
  211. *enc=ssl_cipher_methods[i];
  212. }
  213. switch (c->algorithms & SSL_MAC_MASK)
  214. {
  215. case SSL_MD5:
  216. i=SSL_MD_MD5_IDX;
  217. break;
  218. case SSL_SHA1:
  219. i=SSL_MD_SHA1_IDX;
  220. break;
  221. default:
  222. i= -1;
  223. break;
  224. }
  225. if ((i < 0) || (i > SSL_MD_NUM_IDX))
  226. *md=NULL;
  227. else
  228. *md=ssl_digest_methods[i];
  229. if ((*enc != NULL) && (*md != NULL))
  230. return(1);
  231. else
  232. return(0);
  233. }
  234. #define ITEM_SEP(a) \
  235. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  236. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  237. CIPHER_ORDER **tail)
  238. {
  239. if (curr == *tail) return;
  240. if (curr == *head)
  241. *head=curr->next;
  242. if (curr->prev != NULL)
  243. curr->prev->next=curr->next;
  244. if (curr->next != NULL) /* should always be true */
  245. curr->next->prev=curr->prev;
  246. (*tail)->next=curr;
  247. curr->prev= *tail;
  248. curr->next=NULL;
  249. *tail=curr;
  250. }
  251. static unsigned long ssl_cipher_get_disabled(void)
  252. {
  253. unsigned long mask;
  254. mask = SSL_kFZA;
  255. #ifdef NO_RSA
  256. mask |= SSL_aRSA|SSL_kRSA;
  257. #endif
  258. #ifdef NO_DSA
  259. mask |= SSL_aDSS;
  260. #endif
  261. #ifdef NO_DH
  262. mask |= SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH;
  263. #endif
  264. #ifdef SSL_FORBID_ENULL
  265. mask |= SSL_eNULL;
  266. #endif
  267. mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
  268. mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
  269. mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
  270. mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
  271. mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
  272. mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0;
  273. mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
  274. mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
  275. return(mask);
  276. }
  277. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  278. int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list,
  279. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  280. {
  281. int i, co_list_num;
  282. SSL_CIPHER *c;
  283. /*
  284. * We have num_of_ciphers descriptions compiled in, depending on the
  285. * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
  286. * These will later be sorted in a linked list with at most num
  287. * entries.
  288. */
  289. /* Get the initial list of ciphers */
  290. co_list_num = 0; /* actual count of ciphers */
  291. for (i = 0; i < num_of_ciphers; i++)
  292. {
  293. c = ssl_method->get_cipher(i);
  294. /* drop those that use any of that is not available */
  295. if ((c != NULL) && c->valid && !(c->algorithms & mask))
  296. {
  297. co_list[co_list_num].cipher = c;
  298. co_list[co_list_num].next = NULL;
  299. co_list[co_list_num].prev = NULL;
  300. co_list[co_list_num].active = 0;
  301. co_list_num++;
  302. /*
  303. if (!sk_push(ca_list,(char *)c)) goto err;
  304. */
  305. }
  306. }
  307. /*
  308. * Prepare linked list from list entries
  309. */
  310. for (i = 1; i < co_list_num - 1; i++)
  311. {
  312. co_list[i].prev = &(co_list[i-1]);
  313. co_list[i].next = &(co_list[i+1]);
  314. }
  315. if (co_list_num > 0)
  316. {
  317. (*head_p) = &(co_list[0]);
  318. (*head_p)->prev = NULL;
  319. (*head_p)->next = &(co_list[1]);
  320. (*tail_p) = &(co_list[co_list_num - 1]);
  321. (*tail_p)->prev = &(co_list[co_list_num - 2]);
  322. (*tail_p)->next = NULL;
  323. }
  324. }
  325. static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
  326. int num_of_group_aliases, unsigned long mask,
  327. CIPHER_ORDER *head)
  328. {
  329. CIPHER_ORDER *ciph_curr;
  330. SSL_CIPHER **ca_curr;
  331. int i;
  332. /*
  333. * First, add the real ciphers as already collected
  334. */
  335. ciph_curr = head;
  336. ca_curr = ca_list;
  337. while (ciph_curr != NULL)
  338. {
  339. *ca_curr = ciph_curr->cipher;
  340. ca_curr++;
  341. ciph_curr = ciph_curr->next;
  342. }
  343. /*
  344. * Now we add the available ones from the cipher_aliases[] table.
  345. * They represent either an algorithm, that must be fully
  346. * supported (not match any bit in mask) or represent a cipher
  347. * strength value (will be added in any case because algorithms=0).
  348. */
  349. for (i = 0; i < num_of_group_aliases; i++)
  350. {
  351. if ((i == 0) || /* always fetch "ALL" */
  352. !(cipher_aliases[i].algorithms & mask))
  353. {
  354. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  355. ca_curr++;
  356. }
  357. }
  358. *ca_curr = NULL; /* end of list */
  359. }
  360. static void ssl_cipher_apply_rule(unsigned long algorithms, unsigned long mask,
  361. unsigned long algo_strength, unsigned long mask_strength,
  362. int rule, int strength_bits, CIPHER_ORDER *co_list,
  363. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  364. {
  365. CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2;
  366. SSL_CIPHER *cp;
  367. unsigned long ma, ma_s;
  368. #ifdef CIPHER_DEBUG
  369. printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n",
  370. rule, algorithms, mask, algo_strength, mask_strength,
  371. strength_bits);
  372. #endif
  373. curr = head = *head_p;
  374. curr2 = head;
  375. tail2 = tail = *tail_p;
  376. for (;;)
  377. {
  378. if ((curr == NULL) || (curr == tail2)) break;
  379. curr = curr2;
  380. curr2 = curr->next;
  381. cp = curr->cipher;
  382. /*
  383. * Selection criteria is either the number of strength_bits
  384. * or the algorithm used.
  385. */
  386. if (strength_bits == -1)
  387. {
  388. ma = mask & cp->algorithms;
  389. ma_s = mask_strength & cp->algo_strength;
  390. #ifdef CIPHER_DEBUG
  391. printf("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n", cp->name, cp->algorithms, cp->algo_strength, mask, mask_strength);
  392. printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n", ma, ma_s, ma&algorithms, ma_s&algo_strength);
  393. #endif
  394. /*
  395. * Select: if none of the mask bit was met from the
  396. * cipher or not all of the bits were met, the
  397. * selection does not apply.
  398. */
  399. if (((ma == 0) && (ma_s == 0)) ||
  400. ((ma & algorithms) != ma) ||
  401. ((ma_s & algo_strength) != ma_s))
  402. continue; /* does not apply */
  403. }
  404. else if (strength_bits != cp->strength_bits)
  405. continue; /* does not apply */
  406. #ifdef CIPHER_DEBUG
  407. printf("Action = %d\n", rule);
  408. #endif
  409. /* add the cipher if it has not been added yet. */
  410. if (rule == CIPHER_ADD)
  411. {
  412. if (!curr->active)
  413. {
  414. ll_append_tail(&head, curr, &tail);
  415. curr->active = 1;
  416. }
  417. }
  418. /* Move the added cipher to this location */
  419. else if (rule == CIPHER_ORD)
  420. {
  421. if (curr->active)
  422. {
  423. ll_append_tail(&head, curr, &tail);
  424. }
  425. }
  426. else if (rule == CIPHER_DEL)
  427. curr->active = 0;
  428. else if (rule == CIPHER_KILL)
  429. {
  430. if (head == curr)
  431. head = curr->next;
  432. else
  433. curr->prev->next = curr->next;
  434. if (tail == curr)
  435. tail = curr->prev;
  436. curr->active = 0;
  437. if (curr->next != NULL)
  438. curr->next->prev = curr->prev;
  439. if (curr->prev != NULL)
  440. curr->prev->next = curr->next;
  441. curr->next = NULL;
  442. curr->prev = NULL;
  443. }
  444. }
  445. *head_p = head;
  446. *tail_p = tail;
  447. }
  448. static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list,
  449. CIPHER_ORDER **head_p,
  450. CIPHER_ORDER **tail_p)
  451. {
  452. int max_strength_bits, i, *number_uses;
  453. CIPHER_ORDER *curr;
  454. /*
  455. * This routine sorts the ciphers with descending strength. The sorting
  456. * must keep the pre-sorted sequence, so we apply the normal sorting
  457. * routine as '+' movement to the end of the list.
  458. */
  459. max_strength_bits = 0;
  460. curr = *head_p;
  461. while (curr != NULL)
  462. {
  463. if (curr->active &&
  464. (curr->cipher->strength_bits > max_strength_bits))
  465. max_strength_bits = curr->cipher->strength_bits;
  466. curr = curr->next;
  467. }
  468. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  469. if (!number_uses)
  470. {
  471. SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE);
  472. return(0);
  473. }
  474. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  475. /*
  476. * Now find the strength_bits values actually used
  477. */
  478. curr = *head_p;
  479. while (curr != NULL)
  480. {
  481. if (curr->active)
  482. number_uses[curr->cipher->strength_bits]++;
  483. curr = curr->next;
  484. }
  485. /*
  486. * Go through the list of used strength_bits values in descending
  487. * order.
  488. */
  489. for (i = max_strength_bits; i >= 0; i--)
  490. if (number_uses[i] > 0)
  491. ssl_cipher_apply_rule(0, 0, 0, 0, CIPHER_ORD, i,
  492. co_list, head_p, tail_p);
  493. OPENSSL_free(number_uses);
  494. return(1);
  495. }
  496. static int ssl_cipher_process_rulestr(const char *rule_str,
  497. CIPHER_ORDER *co_list, CIPHER_ORDER **head_p,
  498. CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list)
  499. {
  500. unsigned long algorithms, mask, algo_strength, mask_strength;
  501. const char *l, *start, *buf;
  502. int j, multi, found, rule, retval, ok, buflen;
  503. char ch;
  504. retval = 1;
  505. l = rule_str;
  506. for (;;)
  507. {
  508. ch = *l;
  509. if (ch == '\0')
  510. break; /* done */
  511. if (ch == '-')
  512. { rule = CIPHER_DEL; l++; }
  513. else if (ch == '+')
  514. { rule = CIPHER_ORD; l++; }
  515. else if (ch == '!')
  516. { rule = CIPHER_KILL; l++; }
  517. else if (ch == '@')
  518. { rule = CIPHER_SPECIAL; l++; }
  519. else
  520. { rule = CIPHER_ADD; }
  521. if (ITEM_SEP(ch))
  522. {
  523. l++;
  524. continue;
  525. }
  526. algorithms = mask = algo_strength = mask_strength = 0;
  527. start=l;
  528. for (;;)
  529. {
  530. ch = *l;
  531. buf = l;
  532. buflen = 0;
  533. #ifndef CHARSET_EBCDIC
  534. while ( ((ch >= 'A') && (ch <= 'Z')) ||
  535. ((ch >= '0') && (ch <= '9')) ||
  536. ((ch >= 'a') && (ch <= 'z')) ||
  537. (ch == '-'))
  538. #else
  539. while ( isalnum(ch) || (ch == '-'))
  540. #endif
  541. {
  542. ch = *(++l);
  543. buflen++;
  544. }
  545. if (buflen == 0)
  546. {
  547. /*
  548. * We hit something we cannot deal with,
  549. * it is no command or separator nor
  550. * alphanumeric, so we call this an error.
  551. */
  552. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  553. SSL_R_INVALID_COMMAND);
  554. retval = found = 0;
  555. l++;
  556. break;
  557. }
  558. if (rule == CIPHER_SPECIAL)
  559. {
  560. found = 0; /* unused -- avoid compiler warning */
  561. break; /* special treatment */
  562. }
  563. /* check for multi-part specification */
  564. if (ch == '+')
  565. {
  566. multi=1;
  567. l++;
  568. }
  569. else
  570. multi=0;
  571. /*
  572. * Now search for the cipher alias in the ca_list. Be careful
  573. * with the strncmp, because the "buflen" limitation
  574. * will make the rule "ADH:SOME" and the cipher
  575. * "ADH-MY-CIPHER" look like a match for buflen=3.
  576. * So additionally check whether the cipher name found
  577. * has the correct length. We can save a strlen() call:
  578. * just checking for the '\0' at the right place is
  579. * sufficient, we have to strncmp() anyway.
  580. */
  581. j = found = 0;
  582. while (ca_list[j])
  583. {
  584. if ((ca_list[j]->name[buflen] == '\0') &&
  585. !strncmp(buf, ca_list[j]->name, buflen))
  586. {
  587. found = 1;
  588. break;
  589. }
  590. else
  591. j++;
  592. }
  593. if (!found)
  594. break; /* ignore this entry */
  595. algorithms |= ca_list[j]->algorithms;
  596. mask |= ca_list[j]->mask;
  597. algo_strength |= ca_list[j]->algo_strength;
  598. mask_strength |= ca_list[j]->mask_strength;
  599. if (!multi) break;
  600. }
  601. /*
  602. * Ok, we have the rule, now apply it
  603. */
  604. if (rule == CIPHER_SPECIAL)
  605. { /* special command */
  606. ok = 0;
  607. if ((buflen == 8) &&
  608. !strncmp(buf, "STRENGTH", 8))
  609. ok = ssl_cipher_strength_sort(co_list,
  610. head_p, tail_p);
  611. else
  612. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  613. SSL_R_INVALID_COMMAND);
  614. if (ok == 0)
  615. retval = 0;
  616. /*
  617. * We do not support any "multi" options
  618. * together with "@", so throw away the
  619. * rest of the command, if any left, until
  620. * end or ':' is found.
  621. */
  622. while ((*l != '\0') && ITEM_SEP(*l))
  623. l++;
  624. }
  625. else if (found)
  626. {
  627. ssl_cipher_apply_rule(algorithms, mask,
  628. algo_strength, mask_strength, rule, -1,
  629. co_list, head_p, tail_p);
  630. }
  631. else
  632. {
  633. while ((*l != '\0') && ITEM_SEP(*l))
  634. l++;
  635. }
  636. if (*l == '\0') break; /* done */
  637. }
  638. return(retval);
  639. }
  640. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  641. STACK_OF(SSL_CIPHER) **cipher_list,
  642. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  643. const char *rule_str)
  644. {
  645. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  646. unsigned long disabled_mask;
  647. STACK_OF(SSL_CIPHER) *cipherstack;
  648. const char *rule_p;
  649. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  650. SSL_CIPHER **ca_list = NULL;
  651. /*
  652. * Return with error if nothing to do.
  653. */
  654. if (rule_str == NULL) return(NULL);
  655. if (init_ciphers) load_ciphers();
  656. /*
  657. * To reduce the work to do we only want to process the compiled
  658. * in algorithms, so we first get the mask of disabled ciphers.
  659. */
  660. disabled_mask = ssl_cipher_get_disabled();
  661. /*
  662. * Now we have to collect the available ciphers from the compiled
  663. * in ciphers. We cannot get more than the number compiled in, so
  664. * it is used for allocation.
  665. */
  666. num_of_ciphers = ssl_method->num_ciphers();
  667. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  668. if (co_list == NULL)
  669. {
  670. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  671. return(NULL); /* Failure */
  672. }
  673. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
  674. co_list, &head, &tail);
  675. /*
  676. * We also need cipher aliases for selecting based on the rule_str.
  677. * There might be two types of entries in the rule_str: 1) names
  678. * of ciphers themselves 2) aliases for groups of ciphers.
  679. * For 1) we need the available ciphers and for 2) the cipher
  680. * groups of cipher_aliases added together in one list (otherwise
  681. * we would be happy with just the cipher_aliases table).
  682. */
  683. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  684. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  685. ca_list =
  686. (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
  687. if (ca_list == NULL)
  688. {
  689. OPENSSL_free(co_list);
  690. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  691. return(NULL); /* Failure */
  692. }
  693. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask,
  694. head);
  695. /*
  696. * If the rule_string begins with DEFAULT, apply the default rule
  697. * before using the (possibly available) additional rules.
  698. */
  699. ok = 1;
  700. rule_p = rule_str;
  701. if (strncmp(rule_str,"DEFAULT",7) == 0)
  702. {
  703. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  704. co_list, &head, &tail, ca_list);
  705. rule_p += 7;
  706. if (*rule_p == ':')
  707. rule_p++;
  708. }
  709. if (ok && (strlen(rule_p) > 0))
  710. ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail,
  711. ca_list);
  712. OPENSSL_free(ca_list); /* Not needed anymore */
  713. if (!ok)
  714. { /* Rule processing failure */
  715. OPENSSL_free(co_list);
  716. return(NULL);
  717. }
  718. /*
  719. * Allocate new "cipherstack" for the result, return with error
  720. * if we cannot get one.
  721. */
  722. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
  723. {
  724. OPENSSL_free(co_list);
  725. return(NULL);
  726. }
  727. /*
  728. * The cipher selection for the list is done. The ciphers are added
  729. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  730. */
  731. for (curr = head; curr != NULL; curr = curr->next)
  732. {
  733. if (curr->active)
  734. {
  735. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  736. #ifdef CIPHER_DEBUG
  737. printf("<%s>\n",curr->cipher->name);
  738. #endif
  739. }
  740. }
  741. OPENSSL_free(co_list); /* Not needed any longer */
  742. /*
  743. * The following passage is a little bit odd. If pointer variables
  744. * were supplied to hold STACK_OF(SSL_CIPHER) return information,
  745. * the old memory pointed to is free()ed. Then, however, the
  746. * cipher_list entry will be assigned just a copy of the returned
  747. * cipher stack. For cipher_list_by_id a copy of the cipher stack
  748. * will be created. See next comment...
  749. */
  750. if (cipher_list != NULL)
  751. {
  752. if (*cipher_list != NULL)
  753. sk_SSL_CIPHER_free(*cipher_list);
  754. *cipher_list = cipherstack;
  755. }
  756. if (cipher_list_by_id != NULL)
  757. {
  758. if (*cipher_list_by_id != NULL)
  759. sk_SSL_CIPHER_free(*cipher_list_by_id);
  760. *cipher_list_by_id = sk_SSL_CIPHER_dup(cipherstack);
  761. }
  762. /*
  763. * Now it is getting really strange. If something failed during
  764. * the previous pointer assignment or if one of the pointers was
  765. * not requested, the error condition is met. That might be
  766. * discussable. The strange thing is however that in this case
  767. * the memory "ret" pointed to is "free()ed" and hence the pointer
  768. * cipher_list becomes wild. The memory reserved for
  769. * cipher_list_by_id however is not "free()ed" and stays intact.
  770. */
  771. if ( (cipher_list_by_id == NULL) ||
  772. (*cipher_list_by_id == NULL) ||
  773. (cipher_list == NULL) ||
  774. (*cipher_list == NULL))
  775. {
  776. sk_SSL_CIPHER_free(cipherstack);
  777. return(NULL);
  778. }
  779. sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
  780. return(cipherstack);
  781. }
  782. char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
  783. {
  784. int is_export,pkl,kl;
  785. char *ver,*exp_str;
  786. char *kx,*au,*enc,*mac;
  787. unsigned long alg,alg2,alg_s;
  788. static char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
  789. alg=cipher->algorithms;
  790. alg_s=cipher->algo_strength;
  791. alg2=cipher->algorithm2;
  792. is_export=SSL_C_IS_EXPORT(cipher);
  793. pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  794. kl=SSL_C_EXPORT_KEYLENGTH(cipher);
  795. exp_str=is_export?" export":"";
  796. if (alg & SSL_SSLV2)
  797. ver="SSLv2";
  798. else if (alg & SSL_SSLV3)
  799. ver="SSLv3";
  800. else
  801. ver="unknown";
  802. switch (alg&SSL_MKEY_MASK)
  803. {
  804. case SSL_kRSA:
  805. kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
  806. break;
  807. case SSL_kDHr:
  808. kx="DH/RSA";
  809. break;
  810. case SSL_kDHd:
  811. kx="DH/DSS";
  812. break;
  813. case SSL_kFZA:
  814. kx="Fortezza";
  815. break;
  816. case SSL_kEDH:
  817. kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
  818. break;
  819. default:
  820. kx="unknown";
  821. }
  822. switch (alg&SSL_AUTH_MASK)
  823. {
  824. case SSL_aRSA:
  825. au="RSA";
  826. break;
  827. case SSL_aDSS:
  828. au="DSS";
  829. break;
  830. case SSL_aDH:
  831. au="DH";
  832. break;
  833. case SSL_aFZA:
  834. case SSL_aNULL:
  835. au="None";
  836. break;
  837. default:
  838. au="unknown";
  839. break;
  840. }
  841. switch (alg&SSL_ENC_MASK)
  842. {
  843. case SSL_DES:
  844. enc=(is_export && kl == 5)?"DES(40)":"DES(56)";
  845. break;
  846. case SSL_3DES:
  847. enc="3DES(168)";
  848. break;
  849. case SSL_RC4:
  850. enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
  851. :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
  852. break;
  853. case SSL_RC2:
  854. enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
  855. break;
  856. case SSL_IDEA:
  857. enc="IDEA(128)";
  858. break;
  859. case SSL_eFZA:
  860. enc="Fortezza";
  861. break;
  862. case SSL_eNULL:
  863. enc="None";
  864. break;
  865. default:
  866. enc="unknown";
  867. break;
  868. }
  869. switch (alg&SSL_MAC_MASK)
  870. {
  871. case SSL_MD5:
  872. mac="MD5";
  873. break;
  874. case SSL_SHA1:
  875. mac="SHA1";
  876. break;
  877. default:
  878. mac="unknown";
  879. break;
  880. }
  881. if (buf == NULL)
  882. {
  883. len=128;
  884. buf=OPENSSL_malloc(len);
  885. if (buf == NULL) return("OPENSSL_malloc Error");
  886. }
  887. else if (len < 128)
  888. return("Buffer too small");
  889. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
  890. return(buf);
  891. }
  892. char *SSL_CIPHER_get_version(SSL_CIPHER *c)
  893. {
  894. int i;
  895. if (c == NULL) return("(NONE)");
  896. i=(int)(c->id>>24L);
  897. if (i == 3)
  898. return("TLSv1/SSLv3");
  899. else if (i == 2)
  900. return("SSLv2");
  901. else
  902. return("unknown");
  903. }
  904. /* return the actual cipher being used */
  905. const char *SSL_CIPHER_get_name(SSL_CIPHER *c)
  906. {
  907. if (c != NULL)
  908. return(c->name);
  909. return("(NONE)");
  910. }
  911. /* number of bits for symmetric cipher */
  912. int SSL_CIPHER_get_bits(SSL_CIPHER *c, int *alg_bits)
  913. {
  914. int ret=0;
  915. if (c != NULL)
  916. {
  917. if (alg_bits != NULL) *alg_bits = c->alg_bits;
  918. ret = c->strength_bits;
  919. }
  920. return(ret);
  921. }
  922. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  923. {
  924. SSL_COMP *ctmp;
  925. int i,nn;
  926. if ((n == 0) || (sk == NULL)) return(NULL);
  927. nn=sk_SSL_COMP_num(sk);
  928. for (i=0; i<nn; i++)
  929. {
  930. ctmp=sk_SSL_COMP_value(sk,i);
  931. if (ctmp->id == n)
  932. return(ctmp);
  933. }
  934. return(NULL);
  935. }
  936. static int sk_comp_cmp(const SSL_COMP * const *a,
  937. const SSL_COMP * const *b)
  938. {
  939. return((*a)->id-(*b)->id);
  940. }
  941. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  942. {
  943. return(ssl_comp_methods);
  944. }
  945. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  946. {
  947. SSL_COMP *comp;
  948. STACK_OF(SSL_COMP) *sk;
  949. comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  950. comp->id=id;
  951. comp->method=cm;
  952. if (ssl_comp_methods == NULL)
  953. sk=ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
  954. else
  955. sk=ssl_comp_methods;
  956. if ((sk == NULL) || !sk_SSL_COMP_push(sk,comp))
  957. {
  958. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);
  959. return(1);
  960. }
  961. else
  962. return(0);
  963. }