ssl_ciph.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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-2007 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. /* ====================================================================
  117. * Copyright 2005 Nokia. All rights reserved.
  118. *
  119. * The portions of the attached software ("Contribution") is developed by
  120. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  121. * license.
  122. *
  123. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  124. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  125. * support (see RFC 4279) to OpenSSL.
  126. *
  127. * No patent licenses or other rights except those expressly stated in
  128. * the OpenSSL open source license shall be deemed granted or received
  129. * expressly, by implication, estoppel, or otherwise.
  130. *
  131. * No assurances are provided by Nokia that the Contribution does not
  132. * infringe the patent or other intellectual property rights of any third
  133. * party or that the license provides you with all the necessary rights
  134. * to make use of the Contribution.
  135. *
  136. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  137. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  138. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  139. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  140. * OTHERWISE.
  141. */
  142. #include <stdio.h>
  143. #include <openssl/objects.h>
  144. #include <openssl/comp.h>
  145. #include <openssl/engine.h>
  146. #include "ssl_locl.h"
  147. #define SSL_ENC_DES_IDX 0
  148. #define SSL_ENC_3DES_IDX 1
  149. #define SSL_ENC_RC4_IDX 2
  150. #define SSL_ENC_RC2_IDX 3
  151. #define SSL_ENC_IDEA_IDX 4
  152. #define SSL_ENC_NULL_IDX 5
  153. #define SSL_ENC_AES128_IDX 6
  154. #define SSL_ENC_AES256_IDX 7
  155. #define SSL_ENC_CAMELLIA128_IDX 8
  156. #define SSL_ENC_CAMELLIA256_IDX 9
  157. #define SSL_ENC_GOST89_IDX 10
  158. #define SSL_ENC_SEED_IDX 11
  159. #define SSL_ENC_NUM_IDX 12
  160. static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
  161. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  162. };
  163. #define SSL_COMP_NULL_IDX 0
  164. #define SSL_COMP_ZLIB_IDX 1
  165. #define SSL_COMP_NUM_IDX 2
  166. static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL;
  167. #define SSL_MD_MD5_IDX 0
  168. #define SSL_MD_SHA1_IDX 1
  169. #define SSL_MD_GOST94_IDX 2
  170. #define SSL_MD_GOST89MAC_IDX 3
  171. /*Constant SSL_MAX_DIGEST equal to size of digests array should be
  172. * defined in the
  173. * ssl_locl.h */
  174. #define SSL_MD_NUM_IDX SSL_MAX_DIGEST
  175. static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
  176. NULL,NULL,NULL,NULL
  177. };
  178. /* PKEY_TYPE for GOST89MAC is known in advance, but, because
  179. * implementation is engine-provided, we'll fill it only if
  180. * corresponding EVP_PKEY_METHOD is found
  181. */
  182. static int ssl_mac_pkey_id[SSL_MD_NUM_IDX]={
  183. EVP_PKEY_HMAC,EVP_PKEY_HMAC,EVP_PKEY_HMAC,NID_undef
  184. };
  185. static int ssl_mac_secret_size[SSL_MD_NUM_IDX]={
  186. 0,0,0,0
  187. };
  188. static int ssl_handshake_digest_flag[SSL_MD_NUM_IDX]={
  189. SSL_HANDSHAKE_MAC_MD5,SSL_HANDSHAKE_MAC_SHA,
  190. SSL_HANDSHAKE_MAC_GOST94,0
  191. };
  192. #define CIPHER_ADD 1
  193. #define CIPHER_KILL 2
  194. #define CIPHER_DEL 3
  195. #define CIPHER_ORD 4
  196. #define CIPHER_SPECIAL 5
  197. typedef struct cipher_order_st
  198. {
  199. SSL_CIPHER *cipher;
  200. int active;
  201. int dead;
  202. struct cipher_order_st *next,*prev;
  203. } CIPHER_ORDER;
  204. static const SSL_CIPHER cipher_aliases[]={
  205. /* "ALL" doesn't include eNULL (must be specifically enabled) */
  206. {0,SSL_TXT_ALL,0, 0,0,~SSL_eNULL,0,0,0,0,0,0},
  207. /* "COMPLEMENTOFALL" */
  208. {0,SSL_TXT_CMPALL,0, 0,0,SSL_eNULL,0,0,0,0,0,0},
  209. /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
  210. {0,SSL_TXT_CMPDEF,0, SSL_kEDH|SSL_kEECDH,SSL_aNULL,~SSL_eNULL,0,0,0,0,0,0},
  211. /* key exchange aliases
  212. * (some of those using only a single bit here combine
  213. * multiple key exchange algs according to the RFCs,
  214. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  215. {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
  216. {0,SSL_TXT_kDHr,0, SSL_kDHr, 0,0,0,0,0,0,0,0}, /* no such ciphersuites supported! */
  217. {0,SSL_TXT_kDHd,0, SSL_kDHd, 0,0,0,0,0,0,0,0}, /* no such ciphersuites supported! */
  218. {0,SSL_TXT_kDH,0, SSL_kDHr|SSL_kDHd,0,0,0,0,0,0,0,0}, /* no such ciphersuites supported! */
  219. {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
  220. {0,SSL_TXT_DH,0, SSL_kDHr|SSL_kDHd|SSL_kEDH,0,0,0,0,0,0,0,0},
  221. {0,SSL_TXT_kKRB5,0, SSL_kKRB5, 0,0,0,0,0,0,0,0},
  222. {0,SSL_TXT_kECDHr,0, SSL_kECDHr,0,0,0,0,0,0,0,0},
  223. {0,SSL_TXT_kECDHe,0, SSL_kECDHe,0,0,0,0,0,0,0,0},
  224. {0,SSL_TXT_kECDH,0, SSL_kECDHr|SSL_kECDHe,0,0,0,0,0,0,0,0},
  225. {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
  226. {0,SSL_TXT_ECDH,0, SSL_kECDHr|SSL_kECDHe|SSL_kEECDH,0,0,0,0,0,0,0,0},
  227. {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
  228. {0,SSL_TXT_kGOST,0, SSL_kGOST,0,0,0,0,0,0,0,0},
  229. /* server authentication aliases */
  230. {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
  231. {0,SSL_TXT_aDSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
  232. {0,SSL_TXT_DSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
  233. {0,SSL_TXT_aKRB5,0, 0,SSL_aKRB5, 0,0,0,0,0,0,0},
  234. {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
  235. {0,SSL_TXT_aDH,0, 0,SSL_aDH, 0,0,0,0,0,0,0}, /* no such ciphersuites supported! */
  236. {0,SSL_TXT_aECDH,0, 0,SSL_aECDH, 0,0,0,0,0,0,0},
  237. {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
  238. {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
  239. {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
  240. {0,SSL_TXT_aGOST94,0,0,SSL_aGOST94,0,0,0,0,0,0,0},
  241. {0,SSL_TXT_aGOST01,0,0,SSL_aGOST01,0,0,0,0,0,0,0},
  242. {0,SSL_TXT_aGOST,0,0,SSL_aGOST94|SSL_aGOST01,0,0,0,0,0,0,0},
  243. /* aliases combining key exchange and server authentication */
  244. {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
  245. {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
  246. {0,SSL_TXT_NULL,0, 0,0,SSL_eNULL, 0,0,0,0,0,0},
  247. {0,SSL_TXT_KRB5,0, SSL_kKRB5,SSL_aKRB5,0,0,0,0,0,0,0},
  248. {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
  249. {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
  250. {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
  251. {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
  252. /* symmetric encryption aliases */
  253. {0,SSL_TXT_DES,0, 0,0,SSL_DES, 0,0,0,0,0,0},
  254. {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
  255. {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
  256. {0,SSL_TXT_RC2,0, 0,0,SSL_RC2, 0,0,0,0,0,0},
  257. {0,SSL_TXT_IDEA,0, 0,0,SSL_IDEA, 0,0,0,0,0,0},
  258. {0,SSL_TXT_SEED,0, 0,0,SSL_SEED, 0,0,0,0,0,0},
  259. {0,SSL_TXT_eNULL,0, 0,0,SSL_eNULL, 0,0,0,0,0,0},
  260. {0,SSL_TXT_AES128,0, 0,0,SSL_AES128,0,0,0,0,0,0},
  261. {0,SSL_TXT_AES256,0, 0,0,SSL_AES256,0,0,0,0,0,0},
  262. {0,SSL_TXT_AES,0, 0,0,SSL_AES128|SSL_AES256,0,0,0,0,0,0},
  263. {0,SSL_TXT_CAMELLIA128,0,0,0,SSL_CAMELLIA128,0,0,0,0,0,0},
  264. {0,SSL_TXT_CAMELLIA256,0,0,0,SSL_CAMELLIA256,0,0,0,0,0,0},
  265. {0,SSL_TXT_CAMELLIA ,0,0,0,SSL_CAMELLIA128|SSL_CAMELLIA256,0,0,0,0,0,0},
  266. /* MAC aliases */
  267. {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
  268. {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
  269. {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
  270. {0,SSL_TXT_GOST94,0, 0,0,0,SSL_GOST94, 0,0,0,0,0},
  271. {0,SSL_TXT_GOST89MAC,0, 0,0,0,SSL_GOST89MAC, 0,0,0,0,0},
  272. /* protocol version aliases */
  273. {0,SSL_TXT_SSLV2,0, 0,0,0,0,SSL_SSLV2, 0,0,0,0},
  274. {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
  275. {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
  276. /* export flag */
  277. {0,SSL_TXT_EXP,0, 0,0,0,0,0,SSL_EXPORT,0,0,0},
  278. {0,SSL_TXT_EXPORT,0, 0,0,0,0,0,SSL_EXPORT,0,0,0},
  279. /* strength classes */
  280. {0,SSL_TXT_EXP40,0, 0,0,0,0,0,SSL_EXP40, 0,0,0},
  281. {0,SSL_TXT_EXP56,0, 0,0,0,0,0,SSL_EXP56, 0,0,0},
  282. {0,SSL_TXT_LOW,0, 0,0,0,0,0,SSL_LOW, 0,0,0},
  283. {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
  284. {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
  285. };
  286. /* Search for public key algorithm with given name and
  287. * return its pkey_id if it is available. Otherwise return 0
  288. */
  289. static int get_optional_pkey_id(const char *pkey_name)
  290. {
  291. const EVP_PKEY_ASN1_METHOD *ameth;
  292. ENGINE *tmpeng = NULL;
  293. int pkey_id=0;
  294. ameth = EVP_PKEY_asn1_find_str(&tmpeng,pkey_name,-1);
  295. if (ameth)
  296. {
  297. EVP_PKEY_asn1_get0_info(&pkey_id, NULL,NULL,NULL,NULL,ameth);
  298. }
  299. if (tmpeng) ENGINE_finish(tmpeng);
  300. return pkey_id;
  301. }
  302. void ssl_load_ciphers(void)
  303. {
  304. ssl_cipher_methods[SSL_ENC_DES_IDX]=
  305. EVP_get_cipherbyname(SN_des_cbc);
  306. ssl_cipher_methods[SSL_ENC_3DES_IDX]=
  307. EVP_get_cipherbyname(SN_des_ede3_cbc);
  308. ssl_cipher_methods[SSL_ENC_RC4_IDX]=
  309. EVP_get_cipherbyname(SN_rc4);
  310. ssl_cipher_methods[SSL_ENC_RC2_IDX]=
  311. EVP_get_cipherbyname(SN_rc2_cbc);
  312. #ifndef OPENSSL_NO_IDEA
  313. ssl_cipher_methods[SSL_ENC_IDEA_IDX]=
  314. EVP_get_cipherbyname(SN_idea_cbc);
  315. #else
  316. ssl_cipher_methods[SSL_ENC_IDEA_IDX]= NULL;
  317. #endif
  318. ssl_cipher_methods[SSL_ENC_AES128_IDX]=
  319. EVP_get_cipherbyname(SN_aes_128_cbc);
  320. ssl_cipher_methods[SSL_ENC_AES256_IDX]=
  321. EVP_get_cipherbyname(SN_aes_256_cbc);
  322. ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX]=
  323. EVP_get_cipherbyname(SN_camellia_128_cbc);
  324. ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX]=
  325. EVP_get_cipherbyname(SN_camellia_256_cbc);
  326. ssl_cipher_methods[SSL_ENC_GOST89_IDX]=
  327. EVP_get_cipherbyname(SN_gost89_cnt);
  328. ssl_cipher_methods[SSL_ENC_SEED_IDX]=
  329. EVP_get_cipherbyname(SN_seed_cbc);
  330. ssl_digest_methods[SSL_MD_MD5_IDX]=
  331. EVP_get_digestbyname(SN_md5);
  332. ssl_mac_secret_size[SSL_MD_MD5_IDX]=
  333. EVP_MD_size(ssl_digest_methods[SSL_MD_MD5_IDX]);
  334. ssl_digest_methods[SSL_MD_SHA1_IDX]=
  335. EVP_get_digestbyname(SN_sha1);
  336. ssl_mac_secret_size[SSL_MD_SHA1_IDX]=
  337. EVP_MD_size(ssl_digest_methods[SSL_MD_SHA1_IDX]);
  338. ssl_digest_methods[SSL_MD_GOST94_IDX]=
  339. EVP_get_digestbyname(SN_id_GostR3411_94);
  340. if (ssl_digest_methods[SSL_MD_GOST94_IDX])
  341. {
  342. ssl_mac_secret_size[SSL_MD_GOST94_IDX]=
  343. EVP_MD_size(ssl_digest_methods[SSL_MD_GOST94_IDX]);
  344. }
  345. ssl_digest_methods[SSL_MD_GOST89MAC_IDX]=
  346. EVP_get_digestbyname(SN_id_Gost28147_89_MAC);
  347. ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id("gost-mac");
  348. if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]) {
  349. ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX]=32;
  350. }
  351. }
  352. #ifndef OPENSSL_NO_COMP
  353. static int sk_comp_cmp(const SSL_COMP * const *a,
  354. const SSL_COMP * const *b)
  355. {
  356. return((*a)->id-(*b)->id);
  357. }
  358. static void load_builtin_compressions(void)
  359. {
  360. int got_write_lock = 0;
  361. CRYPTO_r_lock(CRYPTO_LOCK_SSL);
  362. if (ssl_comp_methods == NULL)
  363. {
  364. CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
  365. CRYPTO_w_lock(CRYPTO_LOCK_SSL);
  366. got_write_lock = 1;
  367. if (ssl_comp_methods == NULL)
  368. {
  369. SSL_COMP *comp = NULL;
  370. MemCheck_off();
  371. ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
  372. if (ssl_comp_methods != NULL)
  373. {
  374. comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  375. if (comp != NULL)
  376. {
  377. comp->method=COMP_zlib();
  378. if (comp->method
  379. && comp->method->type == NID_undef)
  380. OPENSSL_free(comp);
  381. else
  382. {
  383. comp->id=SSL_COMP_ZLIB_IDX;
  384. comp->name=comp->method->name;
  385. sk_SSL_COMP_push(ssl_comp_methods,comp);
  386. }
  387. }
  388. }
  389. MemCheck_on();
  390. }
  391. }
  392. if (got_write_lock)
  393. CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
  394. else
  395. CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
  396. }
  397. #endif
  398. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  399. const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size,SSL_COMP **comp)
  400. {
  401. int i;
  402. SSL_CIPHER *c;
  403. c=s->cipher;
  404. if (c == NULL) return(0);
  405. if (comp != NULL)
  406. {
  407. SSL_COMP ctmp;
  408. #ifndef OPENSSL_NO_COMP
  409. load_builtin_compressions();
  410. #endif
  411. *comp=NULL;
  412. ctmp.id=s->compress_meth;
  413. if (ssl_comp_methods != NULL)
  414. {
  415. i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
  416. if (i >= 0)
  417. *comp=sk_SSL_COMP_value(ssl_comp_methods,i);
  418. else
  419. *comp=NULL;
  420. }
  421. }
  422. if ((enc == NULL) || (md == NULL)) return(0);
  423. switch (c->algorithm_enc)
  424. {
  425. case SSL_DES:
  426. i=SSL_ENC_DES_IDX;
  427. break;
  428. case SSL_3DES:
  429. i=SSL_ENC_3DES_IDX;
  430. break;
  431. case SSL_RC4:
  432. i=SSL_ENC_RC4_IDX;
  433. break;
  434. case SSL_RC2:
  435. i=SSL_ENC_RC2_IDX;
  436. break;
  437. case SSL_IDEA:
  438. i=SSL_ENC_IDEA_IDX;
  439. break;
  440. case SSL_eNULL:
  441. i=SSL_ENC_NULL_IDX;
  442. break;
  443. case SSL_AES128:
  444. i=SSL_ENC_AES128_IDX;
  445. break;
  446. case SSL_AES256:
  447. i=SSL_ENC_AES256_IDX;
  448. break;
  449. case SSL_CAMELLIA128:
  450. i=SSL_ENC_CAMELLIA128_IDX;
  451. break;
  452. case SSL_CAMELLIA256:
  453. i=SSL_ENC_CAMELLIA256_IDX;
  454. break;
  455. case SSL_eGOST2814789CNT:
  456. i=SSL_ENC_GOST89_IDX;
  457. break;
  458. case SSL_SEED:
  459. i=SSL_ENC_SEED_IDX;
  460. break;
  461. default:
  462. i= -1;
  463. break;
  464. }
  465. if ((i < 0) || (i > SSL_ENC_NUM_IDX))
  466. *enc=NULL;
  467. else
  468. {
  469. if (i == SSL_ENC_NULL_IDX)
  470. *enc=EVP_enc_null();
  471. else
  472. *enc=ssl_cipher_methods[i];
  473. }
  474. switch (c->algorithm_mac)
  475. {
  476. case SSL_MD5:
  477. i=SSL_MD_MD5_IDX;
  478. break;
  479. case SSL_SHA1:
  480. i=SSL_MD_SHA1_IDX;
  481. break;
  482. case SSL_GOST94:
  483. i = SSL_MD_GOST94_IDX;
  484. break;
  485. case SSL_GOST89MAC:
  486. i = SSL_MD_GOST89MAC_IDX;
  487. break;
  488. default:
  489. i= -1;
  490. break;
  491. }
  492. if ((i < 0) || (i > SSL_MD_NUM_IDX))
  493. {
  494. *md=NULL;
  495. if (mac_pkey_type!=NULL) *mac_pkey_type = NID_undef;
  496. if (mac_secret_size!=NULL) *mac_secret_size = 0;
  497. }
  498. else
  499. {
  500. *md=ssl_digest_methods[i];
  501. if (mac_pkey_type!=NULL) *mac_pkey_type = ssl_mac_pkey_id[i];
  502. if (mac_secret_size!=NULL) *mac_secret_size = ssl_mac_secret_size[i];
  503. }
  504. if ((*enc != NULL) && (*md != NULL) && (!mac_pkey_type||*mac_pkey_type != NID_undef))
  505. return(1);
  506. else
  507. return(0);
  508. }
  509. int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
  510. {
  511. if (idx <0||idx>=SSL_MD_NUM_IDX)
  512. {
  513. return 0;
  514. }
  515. if (ssl_handshake_digest_flag[idx]==0) return 0;
  516. *mask = ssl_handshake_digest_flag[idx];
  517. *md = ssl_digest_methods[idx];
  518. return 1;
  519. }
  520. #define ITEM_SEP(a) \
  521. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  522. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  523. CIPHER_ORDER **tail)
  524. {
  525. if (curr == *tail) return;
  526. if (curr == *head)
  527. *head=curr->next;
  528. if (curr->prev != NULL)
  529. curr->prev->next=curr->next;
  530. if (curr->next != NULL)
  531. curr->next->prev=curr->prev;
  532. (*tail)->next=curr;
  533. curr->prev= *tail;
  534. curr->next=NULL;
  535. *tail=curr;
  536. }
  537. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  538. CIPHER_ORDER **tail)
  539. {
  540. if (curr == *head) return;
  541. if (curr == *tail)
  542. *tail=curr->prev;
  543. if (curr->next != NULL)
  544. curr->next->prev=curr->prev;
  545. if (curr->prev != NULL)
  546. curr->prev->next=curr->next;
  547. (*head)->prev=curr;
  548. curr->next= *head;
  549. curr->prev=NULL;
  550. *head=curr;
  551. }
  552. static void ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth, unsigned long *enc, unsigned long *mac, unsigned long *ssl)
  553. {
  554. *mkey = 0;
  555. *auth = 0;
  556. *enc = 0;
  557. *mac = 0;
  558. *ssl = 0;
  559. #ifdef OPENSSL_NO_RSA
  560. *mkey |= SSL_kRSA;
  561. *auth |= SSL_aRSA;
  562. #endif
  563. #ifdef OPENSSL_NO_DSA
  564. *auth |= SSL_aDSS;
  565. #endif
  566. *mkey |= SSL_kDHr|SSL_kDHd; /* no such ciphersuites supported! */
  567. *auth |= SSL_aDH;
  568. #ifdef OPENSSL_NO_DH
  569. *mkey |= SSL_kDHr|SSL_kDHd|SSL_kEDH;
  570. *auth |= SSL_aDH;
  571. #endif
  572. #ifdef OPENSSL_NO_KRB5
  573. *mkey |= SSL_kKRB5;
  574. *auth |= SSL_aKRB5;
  575. #endif
  576. #ifdef OPENSSL_NO_ECDSA
  577. *auth |= SSL_aECDSA;
  578. #endif
  579. #ifdef OPENSSL_NO_ECDH
  580. *mkey |= SSL_kECDHe|SSL_kECDHr;
  581. *auth |= SSL_aECDH;
  582. #endif
  583. #ifdef OPENSSL_NO_PSK
  584. *mkey |= SSL_kPSK;
  585. *auth |= SSL_aPSK;
  586. #endif
  587. /* Check for presence of GOST 34.10 algorithms, and if they
  588. * do not present, disable appropriate auth and key exchange */
  589. if (!get_optional_pkey_id("gost94")) {
  590. *auth |= SSL_aGOST94;
  591. }
  592. if (!get_optional_pkey_id("gost2001")) {
  593. *auth |= SSL_aGOST01;
  594. }
  595. /* Disable GOST key exchange if no GOST signature algs are available * */
  596. if ((*auth & (SSL_aGOST94|SSL_aGOST01)) == (SSL_aGOST94|SSL_aGOST01)) {
  597. *mkey |= SSL_kGOST;
  598. }
  599. #ifdef SSL_FORBID_ENULL
  600. *enc |= SSL_eNULL;
  601. #endif
  602. *enc |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
  603. *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
  604. *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
  605. *enc |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
  606. *enc |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
  607. *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128:0;
  608. *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256:0;
  609. *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA128:0;
  610. *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] == NULL) ? SSL_CAMELLIA256:0;
  611. *enc |= (ssl_cipher_methods[SSL_ENC_GOST89_IDX] == NULL) ? SSL_eGOST2814789CNT:0;
  612. *enc |= (ssl_cipher_methods[SSL_ENC_SEED_IDX] == NULL) ? SSL_SEED:0;
  613. *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
  614. *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
  615. *mac |= (ssl_digest_methods[SSL_MD_GOST94_IDX] == NULL) ? SSL_GOST94:0;
  616. *mac |= (ssl_digest_methods[SSL_MD_GOST89MAC_IDX] == NULL || ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]==NID_undef)? SSL_GOST89MAC:0;
  617. }
  618. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  619. int num_of_ciphers,
  620. unsigned long disabled_mkey, unsigned long disabled_auth,
  621. unsigned long disabled_enc, unsigned long disabled_mac,
  622. unsigned long disabled_ssl,
  623. CIPHER_ORDER *co_list,
  624. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  625. {
  626. int i, co_list_num;
  627. SSL_CIPHER *c;
  628. /*
  629. * We have num_of_ciphers descriptions compiled in, depending on the
  630. * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
  631. * These will later be sorted in a linked list with at most num
  632. * entries.
  633. */
  634. /* Get the initial list of ciphers */
  635. co_list_num = 0; /* actual count of ciphers */
  636. for (i = 0; i < num_of_ciphers; i++)
  637. {
  638. c = ssl_method->get_cipher(i);
  639. /* drop those that use any of that is not available */
  640. if ((c != NULL) && c->valid &&
  641. !(c->algorithm_mkey & disabled_mkey) &&
  642. !(c->algorithm_auth & disabled_auth) &&
  643. !(c->algorithm_enc & disabled_enc) &&
  644. !(c->algorithm_mac & disabled_mac) &&
  645. !(c->algorithm_ssl & disabled_ssl))
  646. {
  647. co_list[co_list_num].cipher = c;
  648. co_list[co_list_num].next = NULL;
  649. co_list[co_list_num].prev = NULL;
  650. co_list[co_list_num].active = 0;
  651. co_list_num++;
  652. #ifdef KSSL_DEBUG
  653. printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
  654. #endif /* KSSL_DEBUG */
  655. /*
  656. if (!sk_push(ca_list,(char *)c)) goto err;
  657. */
  658. }
  659. }
  660. /*
  661. * Prepare linked list from list entries
  662. */
  663. if (co_list_num > 0)
  664. {
  665. co_list[0].prev = NULL;
  666. if (co_list_num > 1)
  667. {
  668. co_list[0].next = &co_list[1];
  669. for (i = 1; i < co_list_num - 1; i++)
  670. {
  671. co_list[i].prev = &co_list[i - 1];
  672. co_list[i].next = &co_list[i + 1];
  673. }
  674. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  675. }
  676. co_list[co_list_num - 1].next = NULL;
  677. *head_p = &co_list[0];
  678. *tail_p = &co_list[co_list_num - 1];
  679. }
  680. }
  681. static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
  682. int num_of_group_aliases,
  683. unsigned long disabled_mkey, unsigned long disabled_auth,
  684. unsigned long disabled_enc, unsigned long disabled_mac,
  685. unsigned long disabled_ssl,
  686. CIPHER_ORDER *head)
  687. {
  688. CIPHER_ORDER *ciph_curr;
  689. SSL_CIPHER **ca_curr;
  690. int i;
  691. unsigned long mask_mkey = ~disabled_mkey;
  692. unsigned long mask_auth = ~disabled_auth;
  693. unsigned long mask_enc = ~disabled_enc;
  694. unsigned long mask_mac = ~disabled_mac;
  695. unsigned long mask_ssl = ~disabled_ssl;
  696. /*
  697. * First, add the real ciphers as already collected
  698. */
  699. ciph_curr = head;
  700. ca_curr = ca_list;
  701. while (ciph_curr != NULL)
  702. {
  703. *ca_curr = ciph_curr->cipher;
  704. ca_curr++;
  705. ciph_curr = ciph_curr->next;
  706. }
  707. /*
  708. * Now we add the available ones from the cipher_aliases[] table.
  709. * They represent either one or more algorithms, some of which
  710. * in any affected category must be supported (set in enabled_mask),
  711. * or represent a cipher strength value (will be added in any case because algorithms=0).
  712. */
  713. for (i = 0; i < num_of_group_aliases; i++)
  714. {
  715. unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
  716. unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
  717. unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
  718. unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
  719. unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
  720. if (algorithm_mkey)
  721. if ((algorithm_mkey & mask_mkey) == 0)
  722. continue;
  723. if (algorithm_auth)
  724. if ((algorithm_auth & mask_auth) == 0)
  725. continue;
  726. if (algorithm_enc)
  727. if ((algorithm_enc & mask_enc) == 0)
  728. continue;
  729. if (algorithm_mac)
  730. if ((algorithm_mac & mask_mac) == 0)
  731. continue;
  732. if (algorithm_ssl)
  733. if ((algorithm_ssl & mask_ssl) == 0)
  734. continue;
  735. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  736. ca_curr++;
  737. }
  738. *ca_curr = NULL; /* end of list */
  739. }
  740. static void ssl_cipher_apply_rule(unsigned long cipher_id,
  741. unsigned long alg_mkey, unsigned long alg_auth,
  742. unsigned long alg_enc, unsigned long alg_mac,
  743. unsigned long alg_ssl,
  744. unsigned long algo_strength,
  745. int rule, int strength_bits,
  746. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
  747. {
  748. CIPHER_ORDER *head, *tail, *curr, *curr2, *last;
  749. SSL_CIPHER *cp;
  750. int reverse = 0;
  751. #ifdef CIPHER_DEBUG
  752. printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d)\n",
  753. rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits);
  754. #endif
  755. if (rule == CIPHER_DEL)
  756. reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
  757. head = *head_p;
  758. tail = *tail_p;
  759. if (reverse)
  760. {
  761. curr = tail;
  762. last = head;
  763. }
  764. else
  765. {
  766. curr = head;
  767. last = tail;
  768. }
  769. curr2 = curr;
  770. for (;;)
  771. {
  772. if ((curr == NULL) || (curr == last)) break;
  773. curr = curr2;
  774. curr2 = reverse ? curr->prev : curr->next;
  775. cp = curr->cipher;
  776. /*
  777. * Selection criteria is either the value of strength_bits
  778. * or the algorithms used.
  779. */
  780. if (strength_bits >= 0)
  781. {
  782. if (strength_bits != cp->strength_bits)
  783. continue;
  784. }
  785. else
  786. {
  787. #ifdef CIPHER_DEBUG
  788. printf("\nName: %s:\nAlgo = %08lx/%08lx/%08lx/%08lx/%08lx Algo_strength = %08lx\n", cp->name, cp->algorithm_mkey, cp->algorithm_auth, cp->algorithm_enc, cp->algorithm_mac, cp->algorithm_ssl, cp->algo_strength);
  789. #endif
  790. if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
  791. continue;
  792. if (alg_auth && !(alg_auth & cp->algorithm_auth))
  793. continue;
  794. if (alg_enc && !(alg_enc & cp->algorithm_enc))
  795. continue;
  796. if (alg_mac && !(alg_mac & cp->algorithm_mac))
  797. continue;
  798. if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
  799. continue;
  800. if ((algo_strength & SSL_EXP_MASK) && !(algo_strength & SSL_EXP_MASK & cp->algo_strength))
  801. continue;
  802. if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
  803. continue;
  804. }
  805. #ifdef CIPHER_DEBUG
  806. printf("Action = %d\n", rule);
  807. #endif
  808. /* add the cipher if it has not been added yet. */
  809. if (rule == CIPHER_ADD)
  810. {
  811. /* reverse == 0 */
  812. if (!curr->active)
  813. {
  814. ll_append_tail(&head, curr, &tail);
  815. curr->active = 1;
  816. }
  817. }
  818. /* Move the added cipher to this location */
  819. else if (rule == CIPHER_ORD)
  820. {
  821. /* reverse == 0 */
  822. if (curr->active)
  823. {
  824. ll_append_tail(&head, curr, &tail);
  825. }
  826. }
  827. else if (rule == CIPHER_DEL)
  828. {
  829. /* reverse == 1 */
  830. if (curr->active)
  831. {
  832. /* most recently deleted ciphersuites get best positions
  833. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  834. * works in reverse to maintain the order) */
  835. ll_append_head(&head, curr, &tail);
  836. curr->active = 0;
  837. }
  838. }
  839. else if (rule == CIPHER_KILL)
  840. {
  841. /* reverse == 0 */
  842. if (head == curr)
  843. head = curr->next;
  844. else
  845. curr->prev->next = curr->next;
  846. if (tail == curr)
  847. tail = curr->prev;
  848. curr->active = 0;
  849. if (curr->next != NULL)
  850. curr->next->prev = curr->prev;
  851. if (curr->prev != NULL)
  852. curr->prev->next = curr->next;
  853. curr->next = NULL;
  854. curr->prev = NULL;
  855. }
  856. }
  857. *head_p = head;
  858. *tail_p = tail;
  859. }
  860. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  861. CIPHER_ORDER **tail_p)
  862. {
  863. int max_strength_bits, i, *number_uses;
  864. CIPHER_ORDER *curr;
  865. /*
  866. * This routine sorts the ciphers with descending strength. The sorting
  867. * must keep the pre-sorted sequence, so we apply the normal sorting
  868. * routine as '+' movement to the end of the list.
  869. */
  870. max_strength_bits = 0;
  871. curr = *head_p;
  872. while (curr != NULL)
  873. {
  874. if (curr->active &&
  875. (curr->cipher->strength_bits > max_strength_bits))
  876. max_strength_bits = curr->cipher->strength_bits;
  877. curr = curr->next;
  878. }
  879. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  880. if (!number_uses)
  881. {
  882. SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE);
  883. return(0);
  884. }
  885. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  886. /*
  887. * Now find the strength_bits values actually used
  888. */
  889. curr = *head_p;
  890. while (curr != NULL)
  891. {
  892. if (curr->active)
  893. number_uses[curr->cipher->strength_bits]++;
  894. curr = curr->next;
  895. }
  896. /*
  897. * Go through the list of used strength_bits values in descending
  898. * order.
  899. */
  900. for (i = max_strength_bits; i >= 0; i--)
  901. if (number_uses[i] > 0)
  902. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p, tail_p);
  903. OPENSSL_free(number_uses);
  904. return(1);
  905. }
  906. static int ssl_cipher_process_rulestr(const char *rule_str,
  907. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
  908. SSL_CIPHER **ca_list)
  909. {
  910. unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
  911. const char *l, *start, *buf;
  912. int j, multi, found, rule, retval, ok, buflen;
  913. unsigned long cipher_id = 0;
  914. char ch;
  915. retval = 1;
  916. l = rule_str;
  917. for (;;)
  918. {
  919. ch = *l;
  920. if (ch == '\0')
  921. break; /* done */
  922. if (ch == '-')
  923. { rule = CIPHER_DEL; l++; }
  924. else if (ch == '+')
  925. { rule = CIPHER_ORD; l++; }
  926. else if (ch == '!')
  927. { rule = CIPHER_KILL; l++; }
  928. else if (ch == '@')
  929. { rule = CIPHER_SPECIAL; l++; }
  930. else
  931. { rule = CIPHER_ADD; }
  932. if (ITEM_SEP(ch))
  933. {
  934. l++;
  935. continue;
  936. }
  937. alg_mkey = 0;
  938. alg_auth = 0;
  939. alg_enc = 0;
  940. alg_mac = 0;
  941. alg_ssl = 0;
  942. algo_strength = 0;
  943. start=l;
  944. for (;;)
  945. {
  946. ch = *l;
  947. buf = l;
  948. buflen = 0;
  949. #ifndef CHARSET_EBCDIC
  950. while ( ((ch >= 'A') && (ch <= 'Z')) ||
  951. ((ch >= '0') && (ch <= '9')) ||
  952. ((ch >= 'a') && (ch <= 'z')) ||
  953. (ch == '-'))
  954. #else
  955. while ( isalnum(ch) || (ch == '-'))
  956. #endif
  957. {
  958. ch = *(++l);
  959. buflen++;
  960. }
  961. if (buflen == 0)
  962. {
  963. /*
  964. * We hit something we cannot deal with,
  965. * it is no command or separator nor
  966. * alphanumeric, so we call this an error.
  967. */
  968. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  969. SSL_R_INVALID_COMMAND);
  970. retval = found = 0;
  971. l++;
  972. break;
  973. }
  974. if (rule == CIPHER_SPECIAL)
  975. {
  976. found = 0; /* unused -- avoid compiler warning */
  977. break; /* special treatment */
  978. }
  979. /* check for multi-part specification */
  980. if (ch == '+')
  981. {
  982. multi=1;
  983. l++;
  984. }
  985. else
  986. multi=0;
  987. /*
  988. * Now search for the cipher alias in the ca_list. Be careful
  989. * with the strncmp, because the "buflen" limitation
  990. * will make the rule "ADH:SOME" and the cipher
  991. * "ADH-MY-CIPHER" look like a match for buflen=3.
  992. * So additionally check whether the cipher name found
  993. * has the correct length. We can save a strlen() call:
  994. * just checking for the '\0' at the right place is
  995. * sufficient, we have to strncmp() anyway. (We cannot
  996. * use strcmp(), because buf is not '\0' terminated.)
  997. */
  998. j = found = 0;
  999. cipher_id = 0;
  1000. while (ca_list[j])
  1001. {
  1002. if (!strncmp(buf, ca_list[j]->name, buflen) &&
  1003. (ca_list[j]->name[buflen] == '\0'))
  1004. {
  1005. found = 1;
  1006. break;
  1007. }
  1008. else
  1009. j++;
  1010. }
  1011. if (!found)
  1012. break; /* ignore this entry */
  1013. if (ca_list[j]->algorithm_mkey)
  1014. {
  1015. if (alg_mkey)
  1016. {
  1017. alg_mkey &= ca_list[j]->algorithm_mkey;
  1018. if (!alg_mkey) { found = 0; break; }
  1019. }
  1020. else
  1021. alg_mkey = ca_list[j]->algorithm_mkey;
  1022. }
  1023. if (ca_list[j]->algorithm_auth)
  1024. {
  1025. if (alg_auth)
  1026. {
  1027. alg_auth &= ca_list[j]->algorithm_auth;
  1028. if (!alg_auth) { found = 0; break; }
  1029. }
  1030. else
  1031. alg_auth = ca_list[j]->algorithm_auth;
  1032. }
  1033. if (ca_list[j]->algorithm_enc)
  1034. {
  1035. if (alg_enc)
  1036. {
  1037. alg_enc &= ca_list[j]->algorithm_enc;
  1038. if (!alg_enc) { found = 0; break; }
  1039. }
  1040. else
  1041. alg_enc = ca_list[j]->algorithm_enc;
  1042. }
  1043. if (ca_list[j]->algorithm_mac)
  1044. {
  1045. if (alg_mac)
  1046. {
  1047. alg_mac &= ca_list[j]->algorithm_mac;
  1048. if (!alg_mac) { found = 0; break; }
  1049. }
  1050. else
  1051. alg_mac = ca_list[j]->algorithm_mac;
  1052. }
  1053. if (ca_list[j]->algo_strength & SSL_EXP_MASK)
  1054. {
  1055. if (algo_strength & SSL_EXP_MASK)
  1056. {
  1057. algo_strength &= (ca_list[j]->algo_strength & SSL_EXP_MASK) | ~SSL_EXP_MASK;
  1058. if (!(algo_strength & SSL_EXP_MASK)) { found = 0; break; }
  1059. }
  1060. else
  1061. algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
  1062. }
  1063. if (ca_list[j]->algo_strength & SSL_STRONG_MASK)
  1064. {
  1065. if (algo_strength & SSL_STRONG_MASK)
  1066. {
  1067. algo_strength &= (ca_list[j]->algo_strength & SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
  1068. if (!(algo_strength & SSL_STRONG_MASK)) { found = 0; break; }
  1069. }
  1070. else
  1071. algo_strength |= ca_list[j]->algo_strength & SSL_STRONG_MASK;
  1072. }
  1073. if (ca_list[j]->valid)
  1074. {
  1075. /* explicit ciphersuite found; its protocol version
  1076. * does not become part of the search pattern!*/
  1077. cipher_id = ca_list[j]->id;
  1078. }
  1079. else
  1080. {
  1081. /* not an explicit ciphersuite; only in this case, the
  1082. * protocol version is considered part of the search pattern */
  1083. if (ca_list[j]->algorithm_ssl)
  1084. {
  1085. if (alg_ssl)
  1086. {
  1087. alg_ssl &= ca_list[j]->algorithm_ssl;
  1088. if (!alg_ssl) { found = 0; break; }
  1089. }
  1090. else
  1091. alg_ssl = ca_list[j]->algorithm_ssl;
  1092. }
  1093. }
  1094. if (!multi) break;
  1095. }
  1096. /*
  1097. * Ok, we have the rule, now apply it
  1098. */
  1099. if (rule == CIPHER_SPECIAL)
  1100. { /* special command */
  1101. ok = 0;
  1102. if ((buflen == 8) &&
  1103. !strncmp(buf, "STRENGTH", 8))
  1104. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1105. else
  1106. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  1107. SSL_R_INVALID_COMMAND);
  1108. if (ok == 0)
  1109. retval = 0;
  1110. /*
  1111. * We do not support any "multi" options
  1112. * together with "@", so throw away the
  1113. * rest of the command, if any left, until
  1114. * end or ':' is found.
  1115. */
  1116. while ((*l != '\0') && !ITEM_SEP(*l))
  1117. l++;
  1118. }
  1119. else if (found)
  1120. {
  1121. ssl_cipher_apply_rule(cipher_id,
  1122. alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
  1123. rule, -1, head_p, tail_p);
  1124. }
  1125. else
  1126. {
  1127. while ((*l != '\0') && !ITEM_SEP(*l))
  1128. l++;
  1129. }
  1130. if (*l == '\0') break; /* done */
  1131. }
  1132. return(retval);
  1133. }
  1134. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  1135. STACK_OF(SSL_CIPHER) **cipher_list,
  1136. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  1137. const char *rule_str)
  1138. {
  1139. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
  1140. unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
  1141. STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
  1142. const char *rule_p;
  1143. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1144. SSL_CIPHER **ca_list = NULL;
  1145. /*
  1146. * Return with error if nothing to do.
  1147. */
  1148. if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
  1149. return NULL;
  1150. /*
  1151. * To reduce the work to do we only want to process the compiled
  1152. * in algorithms, so we first get the mask of disabled ciphers.
  1153. */
  1154. ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
  1155. /*
  1156. * Now we have to collect the available ciphers from the compiled
  1157. * in ciphers. We cannot get more than the number compiled in, so
  1158. * it is used for allocation.
  1159. */
  1160. num_of_ciphers = ssl_method->num_ciphers();
  1161. #ifdef KSSL_DEBUG
  1162. printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
  1163. #endif /* KSSL_DEBUG */
  1164. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
  1165. if (co_list == NULL)
  1166. {
  1167. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  1168. return(NULL); /* Failure */
  1169. }
  1170. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
  1171. disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
  1172. co_list, &head, &tail);
  1173. /* Now arrange all ciphers by preference: */
  1174. /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
  1175. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
  1176. ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
  1177. /* AES is our preferred symmetric cipher */
  1178. ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
  1179. /* Temporarily enable everything else for sorting */
  1180. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
  1181. /* Low priority for MD5 */
  1182. ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1183. /* Move anonymous ciphers to the end. Usually, these will remain disabled.
  1184. * (For applications that allow them, they aren't too bad, but we prefer
  1185. * authenticated ciphers.) */
  1186. ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1187. /* Move ciphers without forward secrecy to the end */
  1188. ssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1189. /* ssl_cipher_apply_rule(0, 0, SSL_aDH, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail); */
  1190. ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1191. ssl_cipher_apply_rule(0, SSL_kPSK, 0,0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1192. ssl_cipher_apply_rule(0, SSL_kKRB5, 0,0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1193. /* RC4 is sort-of broken -- move the the end */
  1194. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
  1195. /* Now sort by symmetric encryption strength. The above ordering remains
  1196. * in force within each class */
  1197. if (!ssl_cipher_strength_sort(&head, &tail))
  1198. {
  1199. OPENSSL_free(co_list);
  1200. return NULL;
  1201. }
  1202. /* Now disable everything (maintaining the ordering!) */
  1203. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
  1204. /*
  1205. * We also need cipher aliases for selecting based on the rule_str.
  1206. * There might be two types of entries in the rule_str: 1) names
  1207. * of ciphers themselves 2) aliases for groups of ciphers.
  1208. * For 1) we need the available ciphers and for 2) the cipher
  1209. * groups of cipher_aliases added together in one list (otherwise
  1210. * we would be happy with just the cipher_aliases table).
  1211. */
  1212. num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
  1213. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  1214. ca_list =
  1215. (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
  1216. if (ca_list == NULL)
  1217. {
  1218. OPENSSL_free(co_list);
  1219. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  1220. return(NULL); /* Failure */
  1221. }
  1222. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
  1223. disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
  1224. head);
  1225. /*
  1226. * If the rule_string begins with DEFAULT, apply the default rule
  1227. * before using the (possibly available) additional rules.
  1228. */
  1229. ok = 1;
  1230. rule_p = rule_str;
  1231. if (strncmp(rule_str,"DEFAULT",7) == 0)
  1232. {
  1233. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  1234. &head, &tail, ca_list);
  1235. rule_p += 7;
  1236. if (*rule_p == ':')
  1237. rule_p++;
  1238. }
  1239. if (ok && (strlen(rule_p) > 0))
  1240. ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
  1241. OPENSSL_free(ca_list); /* Not needed anymore */
  1242. if (!ok)
  1243. { /* Rule processing failure */
  1244. OPENSSL_free(co_list);
  1245. return(NULL);
  1246. }
  1247. /*
  1248. * Allocate new "cipherstack" for the result, return with error
  1249. * if we cannot get one.
  1250. */
  1251. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
  1252. {
  1253. OPENSSL_free(co_list);
  1254. return(NULL);
  1255. }
  1256. /*
  1257. * The cipher selection for the list is done. The ciphers are added
  1258. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1259. */
  1260. for (curr = head; curr != NULL; curr = curr->next)
  1261. {
  1262. if (curr->active)
  1263. {
  1264. sk_SSL_CIPHER_push(cipherstack, curr->cipher);
  1265. #ifdef CIPHER_DEBUG
  1266. printf("<%s>\n",curr->cipher->name);
  1267. #endif
  1268. }
  1269. }
  1270. OPENSSL_free(co_list); /* Not needed any longer */
  1271. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1272. if (tmp_cipher_list == NULL)
  1273. {
  1274. sk_SSL_CIPHER_free(cipherstack);
  1275. return NULL;
  1276. }
  1277. if (*cipher_list != NULL)
  1278. sk_SSL_CIPHER_free(*cipher_list);
  1279. *cipher_list = cipherstack;
  1280. if (*cipher_list_by_id != NULL)
  1281. sk_SSL_CIPHER_free(*cipher_list_by_id);
  1282. *cipher_list_by_id = tmp_cipher_list;
  1283. (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
  1284. return(cipherstack);
  1285. }
  1286. char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
  1287. {
  1288. int is_export,pkl,kl;
  1289. const char *ver,*exp_str;
  1290. const char *kx,*au,*enc,*mac;
  1291. unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl,alg2,alg_s;
  1292. #ifdef KSSL_DEBUG
  1293. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx/%lx/%lx/%lx/%lx\n";
  1294. #else
  1295. static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
  1296. #endif /* KSSL_DEBUG */
  1297. alg_mkey = cipher->algorithm_mkey;
  1298. alg_auth = cipher->algorithm_auth;
  1299. alg_enc = cipher->algorithm_enc;
  1300. alg_mac = cipher->algorithm_mac;
  1301. alg_ssl = cipher->algorithm_ssl;
  1302. alg_s=cipher->algo_strength;
  1303. alg2=cipher->algorithm2;
  1304. is_export=SSL_C_IS_EXPORT(cipher);
  1305. pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  1306. kl=SSL_C_EXPORT_KEYLENGTH(cipher);
  1307. exp_str=is_export?" export":"";
  1308. if (alg_ssl & SSL_SSLV2)
  1309. ver="SSLv2";
  1310. else if (alg_ssl & SSL_SSLV3)
  1311. ver="SSLv3";
  1312. else
  1313. ver="unknown";
  1314. switch (alg_mkey)
  1315. {
  1316. case SSL_kRSA:
  1317. kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
  1318. break;
  1319. case SSL_kDHr:
  1320. kx="DH/RSA";
  1321. break;
  1322. case SSL_kDHd:
  1323. kx="DH/DSS";
  1324. break;
  1325. case SSL_kKRB5:
  1326. kx="KRB5";
  1327. break;
  1328. case SSL_kEDH:
  1329. kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
  1330. break;
  1331. case SSL_kECDHr:
  1332. kx="ECDH/RSA";
  1333. break;
  1334. case SSL_kECDHe:
  1335. kx="ECDH/ECDSA";
  1336. break;
  1337. case SSL_kEECDH:
  1338. kx="ECDH";
  1339. break;
  1340. case SSL_kPSK:
  1341. kx="PSK";
  1342. break;
  1343. default:
  1344. kx="unknown";
  1345. }
  1346. switch (alg_auth)
  1347. {
  1348. case SSL_aRSA:
  1349. au="RSA";
  1350. break;
  1351. case SSL_aDSS:
  1352. au="DSS";
  1353. break;
  1354. case SSL_aDH:
  1355. au="DH";
  1356. break;
  1357. case SSL_aKRB5:
  1358. au="KRB5";
  1359. break;
  1360. case SSL_aECDH:
  1361. au="ECDH";
  1362. break;
  1363. case SSL_aNULL:
  1364. au="None";
  1365. break;
  1366. case SSL_aECDSA:
  1367. au="ECDSA";
  1368. break;
  1369. case SSL_aPSK:
  1370. au="PSK";
  1371. break;
  1372. default:
  1373. au="unknown";
  1374. break;
  1375. }
  1376. switch (alg_enc)
  1377. {
  1378. case SSL_DES:
  1379. enc=(is_export && kl == 5)?"DES(40)":"DES(56)";
  1380. break;
  1381. case SSL_3DES:
  1382. enc="3DES(168)";
  1383. break;
  1384. case SSL_RC4:
  1385. enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
  1386. :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
  1387. break;
  1388. case SSL_RC2:
  1389. enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
  1390. break;
  1391. case SSL_IDEA:
  1392. enc="IDEA(128)";
  1393. break;
  1394. case SSL_eNULL:
  1395. enc="None";
  1396. break;
  1397. case SSL_AES128:
  1398. enc="AES(128)";
  1399. break;
  1400. case SSL_AES256:
  1401. enc="AES(256)";
  1402. break;
  1403. case SSL_CAMELLIA128:
  1404. enc="Camellia(128)";
  1405. break;
  1406. case SSL_CAMELLIA256:
  1407. enc="Camellia(256)";
  1408. break;
  1409. case SSL_SEED:
  1410. enc="SEED(128)";
  1411. break;
  1412. default:
  1413. enc="unknown";
  1414. break;
  1415. }
  1416. switch (alg_mac)
  1417. {
  1418. case SSL_MD5:
  1419. mac="MD5";
  1420. break;
  1421. case SSL_SHA1:
  1422. mac="SHA1";
  1423. break;
  1424. default:
  1425. mac="unknown";
  1426. break;
  1427. }
  1428. if (buf == NULL)
  1429. {
  1430. len=128;
  1431. buf=OPENSSL_malloc(len);
  1432. if (buf == NULL) return("OPENSSL_malloc Error");
  1433. }
  1434. else if (len < 128)
  1435. return("Buffer too small");
  1436. #ifdef KSSL_DEBUG
  1437. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl);
  1438. #else
  1439. BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
  1440. #endif /* KSSL_DEBUG */
  1441. return(buf);
  1442. }
  1443. char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1444. {
  1445. int i;
  1446. if (c == NULL) return("(NONE)");
  1447. i=(int)(c->id>>24L);
  1448. if (i == 3)
  1449. return("TLSv1/SSLv3");
  1450. else if (i == 2)
  1451. return("SSLv2");
  1452. else
  1453. return("unknown");
  1454. }
  1455. /* return the actual cipher being used */
  1456. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1457. {
  1458. if (c != NULL)
  1459. return(c->name);
  1460. return("(NONE)");
  1461. }
  1462. /* number of bits for symmetric cipher */
  1463. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1464. {
  1465. int ret=0;
  1466. if (c != NULL)
  1467. {
  1468. if (alg_bits != NULL) *alg_bits = c->alg_bits;
  1469. ret = c->strength_bits;
  1470. }
  1471. return(ret);
  1472. }
  1473. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  1474. {
  1475. SSL_COMP *ctmp;
  1476. int i,nn;
  1477. if ((n == 0) || (sk == NULL)) return(NULL);
  1478. nn=sk_SSL_COMP_num(sk);
  1479. for (i=0; i<nn; i++)
  1480. {
  1481. ctmp=sk_SSL_COMP_value(sk,i);
  1482. if (ctmp->id == n)
  1483. return(ctmp);
  1484. }
  1485. return(NULL);
  1486. }
  1487. #ifdef OPENSSL_NO_COMP
  1488. void *SSL_COMP_get_compression_methods(void)
  1489. {
  1490. return NULL;
  1491. }
  1492. int SSL_COMP_add_compression_method(int id, void *cm)
  1493. {
  1494. return 1;
  1495. }
  1496. const char *SSL_COMP_get_name(const void *comp)
  1497. {
  1498. return NULL;
  1499. }
  1500. #else
  1501. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1502. {
  1503. load_builtin_compressions();
  1504. return(ssl_comp_methods);
  1505. }
  1506. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1507. {
  1508. SSL_COMP *comp;
  1509. if (cm == NULL || cm->type == NID_undef)
  1510. return 1;
  1511. /* According to draft-ietf-tls-compression-04.txt, the
  1512. compression number ranges should be the following:
  1513. 0 to 63: methods defined by the IETF
  1514. 64 to 192: external party methods assigned by IANA
  1515. 193 to 255: reserved for private use */
  1516. if (id < 193 || id > 255)
  1517. {
  1518. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
  1519. return 0;
  1520. }
  1521. MemCheck_off();
  1522. comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
  1523. comp->id=id;
  1524. comp->method=cm;
  1525. load_builtin_compressions();
  1526. if (ssl_comp_methods
  1527. && !sk_SSL_COMP_find(ssl_comp_methods,comp))
  1528. {
  1529. OPENSSL_free(comp);
  1530. MemCheck_on();
  1531. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID);
  1532. return(1);
  1533. }
  1534. else if ((ssl_comp_methods == NULL)
  1535. || !sk_SSL_COMP_push(ssl_comp_methods,comp))
  1536. {
  1537. OPENSSL_free(comp);
  1538. MemCheck_on();
  1539. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);
  1540. return(1);
  1541. }
  1542. else
  1543. {
  1544. MemCheck_on();
  1545. return(0);
  1546. }
  1547. }
  1548. const char *SSL_COMP_get_name(const COMP_METHOD *comp)
  1549. {
  1550. if (comp)
  1551. return comp->name;
  1552. return NULL;
  1553. }
  1554. #endif