ssl_ciph.c 37 KB

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