ssl_ciph.c 51 KB

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