ssl_ciph.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/comp.h>
  15. #include <openssl/engine.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/conf.h>
  18. #include <openssl/trace.h>
  19. #include "internal/nelem.h"
  20. #include "ssl_local.h"
  21. #include "internal/thread_once.h"
  22. #include "internal/cryptlib.h"
  23. #define SSL_ENC_DES_IDX 0
  24. #define SSL_ENC_3DES_IDX 1
  25. #define SSL_ENC_RC4_IDX 2
  26. #define SSL_ENC_RC2_IDX 3
  27. #define SSL_ENC_IDEA_IDX 4
  28. #define SSL_ENC_NULL_IDX 5
  29. #define SSL_ENC_AES128_IDX 6
  30. #define SSL_ENC_AES256_IDX 7
  31. #define SSL_ENC_CAMELLIA128_IDX 8
  32. #define SSL_ENC_CAMELLIA256_IDX 9
  33. #define SSL_ENC_GOST89_IDX 10
  34. #define SSL_ENC_SEED_IDX 11
  35. #define SSL_ENC_AES128GCM_IDX 12
  36. #define SSL_ENC_AES256GCM_IDX 13
  37. #define SSL_ENC_AES128CCM_IDX 14
  38. #define SSL_ENC_AES256CCM_IDX 15
  39. #define SSL_ENC_AES128CCM8_IDX 16
  40. #define SSL_ENC_AES256CCM8_IDX 17
  41. #define SSL_ENC_GOST8912_IDX 18
  42. #define SSL_ENC_CHACHA_IDX 19
  43. #define SSL_ENC_ARIA128GCM_IDX 20
  44. #define SSL_ENC_ARIA256GCM_IDX 21
  45. #define SSL_ENC_NUM_IDX 22
  46. /* NB: make sure indices in these tables match values above */
  47. typedef struct {
  48. uint32_t mask;
  49. int nid;
  50. } ssl_cipher_table;
  51. /* Table of NIDs for each cipher */
  52. static const ssl_cipher_table ssl_cipher_table_cipher[SSL_ENC_NUM_IDX] = {
  53. {SSL_DES, NID_des_cbc}, /* SSL_ENC_DES_IDX 0 */
  54. {SSL_3DES, NID_des_ede3_cbc}, /* SSL_ENC_3DES_IDX 1 */
  55. {SSL_RC4, NID_rc4}, /* SSL_ENC_RC4_IDX 2 */
  56. {SSL_RC2, NID_rc2_cbc}, /* SSL_ENC_RC2_IDX 3 */
  57. {SSL_IDEA, NID_idea_cbc}, /* SSL_ENC_IDEA_IDX 4 */
  58. {SSL_eNULL, NID_undef}, /* SSL_ENC_NULL_IDX 5 */
  59. {SSL_AES128, NID_aes_128_cbc}, /* SSL_ENC_AES128_IDX 6 */
  60. {SSL_AES256, NID_aes_256_cbc}, /* SSL_ENC_AES256_IDX 7 */
  61. {SSL_CAMELLIA128, NID_camellia_128_cbc}, /* SSL_ENC_CAMELLIA128_IDX 8 */
  62. {SSL_CAMELLIA256, NID_camellia_256_cbc}, /* SSL_ENC_CAMELLIA256_IDX 9 */
  63. {SSL_eGOST2814789CNT, NID_gost89_cnt}, /* SSL_ENC_GOST89_IDX 10 */
  64. {SSL_SEED, NID_seed_cbc}, /* SSL_ENC_SEED_IDX 11 */
  65. {SSL_AES128GCM, NID_aes_128_gcm}, /* SSL_ENC_AES128GCM_IDX 12 */
  66. {SSL_AES256GCM, NID_aes_256_gcm}, /* SSL_ENC_AES256GCM_IDX 13 */
  67. {SSL_AES128CCM, NID_aes_128_ccm}, /* SSL_ENC_AES128CCM_IDX 14 */
  68. {SSL_AES256CCM, NID_aes_256_ccm}, /* SSL_ENC_AES256CCM_IDX 15 */
  69. {SSL_AES128CCM8, NID_aes_128_ccm}, /* SSL_ENC_AES128CCM8_IDX 16 */
  70. {SSL_AES256CCM8, NID_aes_256_ccm}, /* SSL_ENC_AES256CCM8_IDX 17 */
  71. {SSL_eGOST2814789CNT12, NID_gost89_cnt_12}, /* SSL_ENC_GOST8912_IDX 18 */
  72. {SSL_CHACHA20POLY1305, NID_chacha20_poly1305}, /* SSL_ENC_CHACHA_IDX 19 */
  73. {SSL_ARIA128GCM, NID_aria_128_gcm}, /* SSL_ENC_ARIA128GCM_IDX 20 */
  74. {SSL_ARIA256GCM, NID_aria_256_gcm}, /* SSL_ENC_ARIA256GCM_IDX 21 */
  75. };
  76. static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX];
  77. #define SSL_COMP_NULL_IDX 0
  78. #define SSL_COMP_ZLIB_IDX 1
  79. #define SSL_COMP_NUM_IDX 2
  80. static STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
  81. #ifndef OPENSSL_NO_COMP
  82. static CRYPTO_ONCE ssl_load_builtin_comp_once = CRYPTO_ONCE_STATIC_INIT;
  83. #endif
  84. /*
  85. * Constant SSL_MAX_DIGEST equal to size of digests array should be defined
  86. * in the ssl_local.h
  87. */
  88. #define SSL_MD_NUM_IDX SSL_MAX_DIGEST
  89. /* NB: make sure indices in this table matches values above */
  90. static const ssl_cipher_table ssl_cipher_table_mac[SSL_MD_NUM_IDX] = {
  91. {SSL_MD5, NID_md5}, /* SSL_MD_MD5_IDX 0 */
  92. {SSL_SHA1, NID_sha1}, /* SSL_MD_SHA1_IDX 1 */
  93. {SSL_GOST94, NID_id_GostR3411_94}, /* SSL_MD_GOST94_IDX 2 */
  94. {SSL_GOST89MAC, NID_id_Gost28147_89_MAC}, /* SSL_MD_GOST89MAC_IDX 3 */
  95. {SSL_SHA256, NID_sha256}, /* SSL_MD_SHA256_IDX 4 */
  96. {SSL_SHA384, NID_sha384}, /* SSL_MD_SHA384_IDX 5 */
  97. {SSL_GOST12_256, NID_id_GostR3411_2012_256}, /* SSL_MD_GOST12_256_IDX 6 */
  98. {SSL_GOST89MAC12, NID_gost_mac_12}, /* SSL_MD_GOST89MAC12_IDX 7 */
  99. {SSL_GOST12_512, NID_id_GostR3411_2012_512}, /* SSL_MD_GOST12_512_IDX 8 */
  100. {0, NID_md5_sha1}, /* SSL_MD_MD5_SHA1_IDX 9 */
  101. {0, NID_sha224}, /* SSL_MD_SHA224_IDX 10 */
  102. {0, NID_sha512} /* SSL_MD_SHA512_IDX 11 */
  103. };
  104. static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = {
  105. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  106. };
  107. /* *INDENT-OFF* */
  108. static const ssl_cipher_table ssl_cipher_table_kx[] = {
  109. {SSL_kRSA, NID_kx_rsa},
  110. {SSL_kECDHE, NID_kx_ecdhe},
  111. {SSL_kDHE, NID_kx_dhe},
  112. {SSL_kECDHEPSK, NID_kx_ecdhe_psk},
  113. {SSL_kDHEPSK, NID_kx_dhe_psk},
  114. {SSL_kRSAPSK, NID_kx_rsa_psk},
  115. {SSL_kPSK, NID_kx_psk},
  116. {SSL_kSRP, NID_kx_srp},
  117. {SSL_kGOST, NID_kx_gost},
  118. {SSL_kANY, NID_kx_any}
  119. };
  120. static const ssl_cipher_table ssl_cipher_table_auth[] = {
  121. {SSL_aRSA, NID_auth_rsa},
  122. {SSL_aECDSA, NID_auth_ecdsa},
  123. {SSL_aPSK, NID_auth_psk},
  124. {SSL_aDSS, NID_auth_dss},
  125. {SSL_aGOST01, NID_auth_gost01},
  126. {SSL_aGOST12, NID_auth_gost12},
  127. {SSL_aSRP, NID_auth_srp},
  128. {SSL_aNULL, NID_auth_null},
  129. {SSL_aANY, NID_auth_any}
  130. };
  131. /* *INDENT-ON* */
  132. /* Utility function for table lookup */
  133. static int ssl_cipher_info_find(const ssl_cipher_table * table,
  134. size_t table_cnt, uint32_t mask)
  135. {
  136. size_t i;
  137. for (i = 0; i < table_cnt; i++, table++) {
  138. if (table->mask == mask)
  139. return (int)i;
  140. }
  141. return -1;
  142. }
  143. #define ssl_cipher_info_lookup(table, x) \
  144. ssl_cipher_info_find(table, OSSL_NELEM(table), x)
  145. /*
  146. * PKEY_TYPE for GOST89MAC is known in advance, but, because implementation
  147. * is engine-provided, we'll fill it only if corresponding EVP_PKEY_METHOD is
  148. * found
  149. */
  150. static int ssl_mac_pkey_id[SSL_MD_NUM_IDX] = {
  151. /* MD5, SHA, GOST94, MAC89 */
  152. EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
  153. /* SHA256, SHA384, GOST2012_256, MAC89-12 */
  154. EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
  155. /* GOST2012_512 */
  156. EVP_PKEY_HMAC,
  157. /* MD5/SHA1, SHA224, SHA512 */
  158. NID_undef, NID_undef, NID_undef
  159. };
  160. static size_t ssl_mac_secret_size[SSL_MD_NUM_IDX];
  161. #define CIPHER_ADD 1
  162. #define CIPHER_KILL 2
  163. #define CIPHER_DEL 3
  164. #define CIPHER_ORD 4
  165. #define CIPHER_SPECIAL 5
  166. /*
  167. * Bump the ciphers to the top of the list.
  168. * This rule isn't currently supported by the public cipherstring API.
  169. */
  170. #define CIPHER_BUMP 6
  171. typedef struct cipher_order_st {
  172. const SSL_CIPHER *cipher;
  173. int active;
  174. int dead;
  175. struct cipher_order_st *next, *prev;
  176. } CIPHER_ORDER;
  177. static const SSL_CIPHER cipher_aliases[] = {
  178. /* "ALL" doesn't include eNULL (must be specifically enabled) */
  179. {0, SSL_TXT_ALL, NULL, 0, 0, 0, ~SSL_eNULL},
  180. /* "COMPLEMENTOFALL" */
  181. {0, SSL_TXT_CMPALL, NULL, 0, 0, 0, SSL_eNULL},
  182. /*
  183. * "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in
  184. * ALL!)
  185. */
  186. {0, SSL_TXT_CMPDEF, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_NOT_DEFAULT},
  187. /*
  188. * key exchange aliases (some of those using only a single bit here
  189. * combine multiple key exchange algs according to the RFCs, e.g. kDHE
  190. * combines DHE_DSS and DHE_RSA)
  191. */
  192. {0, SSL_TXT_kRSA, NULL, 0, SSL_kRSA},
  193. {0, SSL_TXT_kEDH, NULL, 0, SSL_kDHE},
  194. {0, SSL_TXT_kDHE, NULL, 0, SSL_kDHE},
  195. {0, SSL_TXT_DH, NULL, 0, SSL_kDHE},
  196. {0, SSL_TXT_kEECDH, NULL, 0, SSL_kECDHE},
  197. {0, SSL_TXT_kECDHE, NULL, 0, SSL_kECDHE},
  198. {0, SSL_TXT_ECDH, NULL, 0, SSL_kECDHE},
  199. {0, SSL_TXT_kPSK, NULL, 0, SSL_kPSK},
  200. {0, SSL_TXT_kRSAPSK, NULL, 0, SSL_kRSAPSK},
  201. {0, SSL_TXT_kECDHEPSK, NULL, 0, SSL_kECDHEPSK},
  202. {0, SSL_TXT_kDHEPSK, NULL, 0, SSL_kDHEPSK},
  203. {0, SSL_TXT_kSRP, NULL, 0, SSL_kSRP},
  204. {0, SSL_TXT_kGOST, NULL, 0, SSL_kGOST},
  205. /* server authentication aliases */
  206. {0, SSL_TXT_aRSA, NULL, 0, 0, SSL_aRSA},
  207. {0, SSL_TXT_aDSS, NULL, 0, 0, SSL_aDSS},
  208. {0, SSL_TXT_DSS, NULL, 0, 0, SSL_aDSS},
  209. {0, SSL_TXT_aNULL, NULL, 0, 0, SSL_aNULL},
  210. {0, SSL_TXT_aECDSA, NULL, 0, 0, SSL_aECDSA},
  211. {0, SSL_TXT_ECDSA, NULL, 0, 0, SSL_aECDSA},
  212. {0, SSL_TXT_aPSK, NULL, 0, 0, SSL_aPSK},
  213. {0, SSL_TXT_aGOST01, NULL, 0, 0, SSL_aGOST01},
  214. {0, SSL_TXT_aGOST12, NULL, 0, 0, SSL_aGOST12},
  215. {0, SSL_TXT_aGOST, NULL, 0, 0, SSL_aGOST01 | SSL_aGOST12},
  216. {0, SSL_TXT_aSRP, NULL, 0, 0, SSL_aSRP},
  217. /* aliases combining key exchange and server authentication */
  218. {0, SSL_TXT_EDH, NULL, 0, SSL_kDHE, ~SSL_aNULL},
  219. {0, SSL_TXT_DHE, NULL, 0, SSL_kDHE, ~SSL_aNULL},
  220. {0, SSL_TXT_EECDH, NULL, 0, SSL_kECDHE, ~SSL_aNULL},
  221. {0, SSL_TXT_ECDHE, NULL, 0, SSL_kECDHE, ~SSL_aNULL},
  222. {0, SSL_TXT_NULL, NULL, 0, 0, 0, SSL_eNULL},
  223. {0, SSL_TXT_RSA, NULL, 0, SSL_kRSA, SSL_aRSA},
  224. {0, SSL_TXT_ADH, NULL, 0, SSL_kDHE, SSL_aNULL},
  225. {0, SSL_TXT_AECDH, NULL, 0, SSL_kECDHE, SSL_aNULL},
  226. {0, SSL_TXT_PSK, NULL, 0, SSL_PSK},
  227. {0, SSL_TXT_SRP, NULL, 0, SSL_kSRP},
  228. /* symmetric encryption aliases */
  229. {0, SSL_TXT_3DES, NULL, 0, 0, 0, SSL_3DES},
  230. {0, SSL_TXT_RC4, NULL, 0, 0, 0, SSL_RC4},
  231. {0, SSL_TXT_RC2, NULL, 0, 0, 0, SSL_RC2},
  232. {0, SSL_TXT_IDEA, NULL, 0, 0, 0, SSL_IDEA},
  233. {0, SSL_TXT_SEED, NULL, 0, 0, 0, SSL_SEED},
  234. {0, SSL_TXT_eNULL, NULL, 0, 0, 0, SSL_eNULL},
  235. {0, SSL_TXT_GOST, NULL, 0, 0, 0, SSL_eGOST2814789CNT | SSL_eGOST2814789CNT12},
  236. {0, SSL_TXT_AES128, NULL, 0, 0, 0,
  237. SSL_AES128 | SSL_AES128GCM | SSL_AES128CCM | SSL_AES128CCM8},
  238. {0, SSL_TXT_AES256, NULL, 0, 0, 0,
  239. SSL_AES256 | SSL_AES256GCM | SSL_AES256CCM | SSL_AES256CCM8},
  240. {0, SSL_TXT_AES, NULL, 0, 0, 0, SSL_AES},
  241. {0, SSL_TXT_AES_GCM, NULL, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM},
  242. {0, SSL_TXT_AES_CCM, NULL, 0, 0, 0,
  243. SSL_AES128CCM | SSL_AES256CCM | SSL_AES128CCM8 | SSL_AES256CCM8},
  244. {0, SSL_TXT_AES_CCM_8, NULL, 0, 0, 0, SSL_AES128CCM8 | SSL_AES256CCM8},
  245. {0, SSL_TXT_CAMELLIA128, NULL, 0, 0, 0, SSL_CAMELLIA128},
  246. {0, SSL_TXT_CAMELLIA256, NULL, 0, 0, 0, SSL_CAMELLIA256},
  247. {0, SSL_TXT_CAMELLIA, NULL, 0, 0, 0, SSL_CAMELLIA},
  248. {0, SSL_TXT_CHACHA20, NULL, 0, 0, 0, SSL_CHACHA20},
  249. {0, SSL_TXT_ARIA, NULL, 0, 0, 0, SSL_ARIA},
  250. {0, SSL_TXT_ARIA_GCM, NULL, 0, 0, 0, SSL_ARIA128GCM | SSL_ARIA256GCM},
  251. {0, SSL_TXT_ARIA128, NULL, 0, 0, 0, SSL_ARIA128GCM},
  252. {0, SSL_TXT_ARIA256, NULL, 0, 0, 0, SSL_ARIA256GCM},
  253. /* MAC aliases */
  254. {0, SSL_TXT_MD5, NULL, 0, 0, 0, 0, SSL_MD5},
  255. {0, SSL_TXT_SHA1, NULL, 0, 0, 0, 0, SSL_SHA1},
  256. {0, SSL_TXT_SHA, NULL, 0, 0, 0, 0, SSL_SHA1},
  257. {0, SSL_TXT_GOST94, NULL, 0, 0, 0, 0, SSL_GOST94},
  258. {0, SSL_TXT_GOST89MAC, NULL, 0, 0, 0, 0, SSL_GOST89MAC | SSL_GOST89MAC12},
  259. {0, SSL_TXT_SHA256, NULL, 0, 0, 0, 0, SSL_SHA256},
  260. {0, SSL_TXT_SHA384, NULL, 0, 0, 0, 0, SSL_SHA384},
  261. {0, SSL_TXT_GOST12, NULL, 0, 0, 0, 0, SSL_GOST12_256},
  262. /* protocol version aliases */
  263. {0, SSL_TXT_SSLV3, NULL, 0, 0, 0, 0, 0, SSL3_VERSION},
  264. {0, SSL_TXT_TLSV1, NULL, 0, 0, 0, 0, 0, TLS1_VERSION},
  265. {0, "TLSv1.0", NULL, 0, 0, 0, 0, 0, TLS1_VERSION},
  266. {0, SSL_TXT_TLSV1_2, NULL, 0, 0, 0, 0, 0, TLS1_2_VERSION},
  267. /* strength classes */
  268. {0, SSL_TXT_LOW, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_LOW},
  269. {0, SSL_TXT_MEDIUM, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_MEDIUM},
  270. {0, SSL_TXT_HIGH, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_HIGH},
  271. /* FIPS 140-2 approved ciphersuite */
  272. {0, SSL_TXT_FIPS, NULL, 0, 0, 0, ~SSL_eNULL, 0, 0, 0, 0, 0, SSL_FIPS},
  273. /* "EDH-" aliases to "DHE-" labels (for backward compatibility) */
  274. {0, SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA, NULL, 0,
  275. SSL_kDHE, SSL_aDSS, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS},
  276. {0, SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA, NULL, 0,
  277. SSL_kDHE, SSL_aRSA, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS},
  278. };
  279. /*
  280. * Search for public key algorithm with given name and return its pkey_id if
  281. * it is available. Otherwise return 0
  282. */
  283. #ifdef OPENSSL_NO_ENGINE
  284. static int get_optional_pkey_id(const char *pkey_name)
  285. {
  286. const EVP_PKEY_ASN1_METHOD *ameth;
  287. int pkey_id = 0;
  288. ameth = EVP_PKEY_asn1_find_str(NULL, pkey_name, -1);
  289. if (ameth && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
  290. ameth) > 0)
  291. return pkey_id;
  292. return 0;
  293. }
  294. #else
  295. static int get_optional_pkey_id(const char *pkey_name)
  296. {
  297. const EVP_PKEY_ASN1_METHOD *ameth;
  298. ENGINE *tmpeng = NULL;
  299. int pkey_id = 0;
  300. ameth = EVP_PKEY_asn1_find_str(&tmpeng, pkey_name, -1);
  301. if (ameth) {
  302. if (EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
  303. ameth) <= 0)
  304. pkey_id = 0;
  305. }
  306. ENGINE_finish(tmpeng);
  307. return pkey_id;
  308. }
  309. #endif
  310. /* masks of disabled algorithms */
  311. static uint32_t disabled_enc_mask;
  312. static uint32_t disabled_mac_mask;
  313. static uint32_t disabled_mkey_mask;
  314. static uint32_t disabled_auth_mask;
  315. int ssl_load_ciphers(void)
  316. {
  317. size_t i;
  318. const ssl_cipher_table *t;
  319. disabled_enc_mask = 0;
  320. ssl_sort_cipher_list();
  321. for (i = 0, t = ssl_cipher_table_cipher; i < SSL_ENC_NUM_IDX; i++, t++) {
  322. if (t->nid == NID_undef) {
  323. ssl_cipher_methods[i] = NULL;
  324. } else {
  325. const EVP_CIPHER *cipher = EVP_get_cipherbynid(t->nid);
  326. ssl_cipher_methods[i] = cipher;
  327. if (cipher == NULL)
  328. disabled_enc_mask |= t->mask;
  329. }
  330. }
  331. disabled_mac_mask = 0;
  332. for (i = 0, t = ssl_cipher_table_mac; i < SSL_MD_NUM_IDX; i++, t++) {
  333. const EVP_MD *md = EVP_get_digestbynid(t->nid);
  334. ssl_digest_methods[i] = md;
  335. if (md == NULL) {
  336. disabled_mac_mask |= t->mask;
  337. } else {
  338. int tmpsize = EVP_MD_size(md);
  339. if (!ossl_assert(tmpsize >= 0))
  340. return 0;
  341. ssl_mac_secret_size[i] = tmpsize;
  342. }
  343. }
  344. /* Make sure we can access MD5 and SHA1 */
  345. if (!ossl_assert(ssl_digest_methods[SSL_MD_MD5_IDX] != NULL))
  346. return 0;
  347. if (!ossl_assert(ssl_digest_methods[SSL_MD_SHA1_IDX] != NULL))
  348. return 0;
  349. disabled_mkey_mask = 0;
  350. disabled_auth_mask = 0;
  351. #ifdef OPENSSL_NO_RSA
  352. disabled_mkey_mask |= SSL_kRSA | SSL_kRSAPSK;
  353. disabled_auth_mask |= SSL_aRSA;
  354. #endif
  355. #ifdef OPENSSL_NO_DSA
  356. disabled_auth_mask |= SSL_aDSS;
  357. #endif
  358. #ifdef OPENSSL_NO_DH
  359. disabled_mkey_mask |= SSL_kDHE | SSL_kDHEPSK;
  360. #endif
  361. #ifdef OPENSSL_NO_EC
  362. disabled_mkey_mask |= SSL_kECDHE | SSL_kECDHEPSK;
  363. disabled_auth_mask |= SSL_aECDSA;
  364. #endif
  365. #ifdef OPENSSL_NO_PSK
  366. disabled_mkey_mask |= SSL_PSK;
  367. disabled_auth_mask |= SSL_aPSK;
  368. #endif
  369. #ifdef OPENSSL_NO_SRP
  370. disabled_mkey_mask |= SSL_kSRP;
  371. #endif
  372. /*
  373. * Check for presence of GOST 34.10 algorithms, and if they are not
  374. * present, disable appropriate auth and key exchange
  375. */
  376. ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id("gost-mac");
  377. if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
  378. ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
  379. else
  380. disabled_mac_mask |= SSL_GOST89MAC;
  381. ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] =
  382. get_optional_pkey_id("gost-mac-12");
  383. if (ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
  384. ssl_mac_secret_size[SSL_MD_GOST89MAC12_IDX] = 32;
  385. else
  386. disabled_mac_mask |= SSL_GOST89MAC12;
  387. if (!get_optional_pkey_id("gost2001"))
  388. disabled_auth_mask |= SSL_aGOST01 | SSL_aGOST12;
  389. if (!get_optional_pkey_id("gost2012_256"))
  390. disabled_auth_mask |= SSL_aGOST12;
  391. if (!get_optional_pkey_id("gost2012_512"))
  392. disabled_auth_mask |= SSL_aGOST12;
  393. /*
  394. * Disable GOST key exchange if no GOST signature algs are available *
  395. */
  396. if ((disabled_auth_mask & (SSL_aGOST01 | SSL_aGOST12)) ==
  397. (SSL_aGOST01 | SSL_aGOST12))
  398. disabled_mkey_mask |= SSL_kGOST;
  399. return 1;
  400. }
  401. #ifndef OPENSSL_NO_COMP
  402. static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b)
  403. {
  404. return ((*a)->id - (*b)->id);
  405. }
  406. DEFINE_RUN_ONCE_STATIC(do_load_builtin_compressions)
  407. {
  408. SSL_COMP *comp = NULL;
  409. COMP_METHOD *method = COMP_zlib();
  410. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  411. ssl_comp_methods = sk_SSL_COMP_new(sk_comp_cmp);
  412. if (COMP_get_type(method) != NID_undef && ssl_comp_methods != NULL) {
  413. comp = OPENSSL_malloc(sizeof(*comp));
  414. if (comp != NULL) {
  415. comp->method = method;
  416. comp->id = SSL_COMP_ZLIB_IDX;
  417. comp->name = COMP_get_name(method);
  418. sk_SSL_COMP_push(ssl_comp_methods, comp);
  419. sk_SSL_COMP_sort(ssl_comp_methods);
  420. }
  421. }
  422. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  423. return 1;
  424. }
  425. static int load_builtin_compressions(void)
  426. {
  427. return RUN_ONCE(&ssl_load_builtin_comp_once, do_load_builtin_compressions);
  428. }
  429. #endif
  430. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  431. const EVP_MD **md, int *mac_pkey_type,
  432. size_t *mac_secret_size, SSL_COMP **comp, int use_etm)
  433. {
  434. int i;
  435. const SSL_CIPHER *c;
  436. c = s->cipher;
  437. if (c == NULL)
  438. return 0;
  439. if (comp != NULL) {
  440. SSL_COMP ctmp;
  441. #ifndef OPENSSL_NO_COMP
  442. if (!load_builtin_compressions()) {
  443. /*
  444. * Currently don't care, since a failure only means that
  445. * ssl_comp_methods is NULL, which is perfectly OK
  446. */
  447. }
  448. #endif
  449. *comp = NULL;
  450. ctmp.id = s->compress_meth;
  451. if (ssl_comp_methods != NULL) {
  452. i = sk_SSL_COMP_find(ssl_comp_methods, &ctmp);
  453. *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
  454. }
  455. /* If were only interested in comp then return success */
  456. if ((enc == NULL) && (md == NULL))
  457. return 1;
  458. }
  459. if ((enc == NULL) || (md == NULL))
  460. return 0;
  461. i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
  462. if (i == -1) {
  463. *enc = NULL;
  464. } else {
  465. if (i == SSL_ENC_NULL_IDX)
  466. *enc = EVP_enc_null();
  467. else
  468. *enc = ssl_cipher_methods[i];
  469. }
  470. i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
  471. if (i == -1) {
  472. *md = NULL;
  473. if (mac_pkey_type != NULL)
  474. *mac_pkey_type = NID_undef;
  475. if (mac_secret_size != NULL)
  476. *mac_secret_size = 0;
  477. if (c->algorithm_mac == SSL_AEAD)
  478. mac_pkey_type = NULL;
  479. } else {
  480. *md = ssl_digest_methods[i];
  481. if (mac_pkey_type != NULL)
  482. *mac_pkey_type = ssl_mac_pkey_id[i];
  483. if (mac_secret_size != NULL)
  484. *mac_secret_size = ssl_mac_secret_size[i];
  485. }
  486. if ((*enc != NULL) &&
  487. (*md != NULL || (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
  488. && (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
  489. const EVP_CIPHER *evp;
  490. if (use_etm)
  491. return 1;
  492. if (s->ssl_version >> 8 != TLS1_VERSION_MAJOR ||
  493. s->ssl_version < TLS1_VERSION)
  494. return 1;
  495. if (c->algorithm_enc == SSL_RC4 &&
  496. c->algorithm_mac == SSL_MD5 &&
  497. (evp = EVP_get_cipherbyname("RC4-HMAC-MD5")))
  498. *enc = evp, *md = NULL;
  499. else if (c->algorithm_enc == SSL_AES128 &&
  500. c->algorithm_mac == SSL_SHA1 &&
  501. (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
  502. *enc = evp, *md = NULL;
  503. else if (c->algorithm_enc == SSL_AES256 &&
  504. c->algorithm_mac == SSL_SHA1 &&
  505. (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
  506. *enc = evp, *md = NULL;
  507. else if (c->algorithm_enc == SSL_AES128 &&
  508. c->algorithm_mac == SSL_SHA256 &&
  509. (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA256")))
  510. *enc = evp, *md = NULL;
  511. else if (c->algorithm_enc == SSL_AES256 &&
  512. c->algorithm_mac == SSL_SHA256 &&
  513. (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA256")))
  514. *enc = evp, *md = NULL;
  515. return 1;
  516. } else {
  517. return 0;
  518. }
  519. }
  520. const EVP_MD *ssl_md(int idx)
  521. {
  522. idx &= SSL_HANDSHAKE_MAC_MASK;
  523. if (idx < 0 || idx >= SSL_MD_NUM_IDX)
  524. return NULL;
  525. return ssl_digest_methods[idx];
  526. }
  527. const EVP_MD *ssl_handshake_md(SSL *s)
  528. {
  529. return ssl_md(ssl_get_algorithm2(s));
  530. }
  531. const EVP_MD *ssl_prf_md(SSL *s)
  532. {
  533. return ssl_md(ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
  534. }
  535. #define ITEM_SEP(a) \
  536. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  537. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  538. CIPHER_ORDER **tail)
  539. {
  540. if (curr == *tail)
  541. return;
  542. if (curr == *head)
  543. *head = curr->next;
  544. if (curr->prev != NULL)
  545. curr->prev->next = curr->next;
  546. if (curr->next != NULL)
  547. curr->next->prev = curr->prev;
  548. (*tail)->next = curr;
  549. curr->prev = *tail;
  550. curr->next = NULL;
  551. *tail = curr;
  552. }
  553. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  554. CIPHER_ORDER **tail)
  555. {
  556. if (curr == *head)
  557. return;
  558. if (curr == *tail)
  559. *tail = curr->prev;
  560. if (curr->next != NULL)
  561. curr->next->prev = curr->prev;
  562. if (curr->prev != NULL)
  563. curr->prev->next = curr->next;
  564. (*head)->prev = curr;
  565. curr->next = *head;
  566. curr->prev = NULL;
  567. *head = curr;
  568. }
  569. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  570. int num_of_ciphers,
  571. uint32_t disabled_mkey,
  572. uint32_t disabled_auth,
  573. uint32_t disabled_enc,
  574. uint32_t disabled_mac,
  575. CIPHER_ORDER *co_list,
  576. CIPHER_ORDER **head_p,
  577. CIPHER_ORDER **tail_p)
  578. {
  579. int i, co_list_num;
  580. const SSL_CIPHER *c;
  581. /*
  582. * We have num_of_ciphers descriptions compiled in, depending on the
  583. * method selected (SSLv3, TLSv1 etc).
  584. * These will later be sorted in a linked list with at most num
  585. * entries.
  586. */
  587. /* Get the initial list of ciphers */
  588. co_list_num = 0; /* actual count of ciphers */
  589. for (i = 0; i < num_of_ciphers; i++) {
  590. c = ssl_method->get_cipher(i);
  591. /* drop those that use any of that is not available */
  592. if (c == NULL || !c->valid)
  593. continue;
  594. if ((c->algorithm_mkey & disabled_mkey) ||
  595. (c->algorithm_auth & disabled_auth) ||
  596. (c->algorithm_enc & disabled_enc) ||
  597. (c->algorithm_mac & disabled_mac))
  598. continue;
  599. if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) == 0) &&
  600. c->min_tls == 0)
  601. continue;
  602. if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) != 0) &&
  603. c->min_dtls == 0)
  604. continue;
  605. co_list[co_list_num].cipher = c;
  606. co_list[co_list_num].next = NULL;
  607. co_list[co_list_num].prev = NULL;
  608. co_list[co_list_num].active = 0;
  609. co_list_num++;
  610. }
  611. /*
  612. * Prepare linked list from list entries
  613. */
  614. if (co_list_num > 0) {
  615. co_list[0].prev = NULL;
  616. if (co_list_num > 1) {
  617. co_list[0].next = &co_list[1];
  618. for (i = 1; i < co_list_num - 1; i++) {
  619. co_list[i].prev = &co_list[i - 1];
  620. co_list[i].next = &co_list[i + 1];
  621. }
  622. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  623. }
  624. co_list[co_list_num - 1].next = NULL;
  625. *head_p = &co_list[0];
  626. *tail_p = &co_list[co_list_num - 1];
  627. }
  628. }
  629. static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
  630. int num_of_group_aliases,
  631. uint32_t disabled_mkey,
  632. uint32_t disabled_auth,
  633. uint32_t disabled_enc,
  634. uint32_t disabled_mac,
  635. CIPHER_ORDER *head)
  636. {
  637. CIPHER_ORDER *ciph_curr;
  638. const SSL_CIPHER **ca_curr;
  639. int i;
  640. uint32_t mask_mkey = ~disabled_mkey;
  641. uint32_t mask_auth = ~disabled_auth;
  642. uint32_t mask_enc = ~disabled_enc;
  643. uint32_t mask_mac = ~disabled_mac;
  644. /*
  645. * First, add the real ciphers as already collected
  646. */
  647. ciph_curr = head;
  648. ca_curr = ca_list;
  649. while (ciph_curr != NULL) {
  650. *ca_curr = ciph_curr->cipher;
  651. ca_curr++;
  652. ciph_curr = ciph_curr->next;
  653. }
  654. /*
  655. * Now we add the available ones from the cipher_aliases[] table.
  656. * They represent either one or more algorithms, some of which
  657. * in any affected category must be supported (set in enabled_mask),
  658. * or represent a cipher strength value (will be added in any case because algorithms=0).
  659. */
  660. for (i = 0; i < num_of_group_aliases; i++) {
  661. uint32_t algorithm_mkey = cipher_aliases[i].algorithm_mkey;
  662. uint32_t algorithm_auth = cipher_aliases[i].algorithm_auth;
  663. uint32_t algorithm_enc = cipher_aliases[i].algorithm_enc;
  664. uint32_t algorithm_mac = cipher_aliases[i].algorithm_mac;
  665. if (algorithm_mkey)
  666. if ((algorithm_mkey & mask_mkey) == 0)
  667. continue;
  668. if (algorithm_auth)
  669. if ((algorithm_auth & mask_auth) == 0)
  670. continue;
  671. if (algorithm_enc)
  672. if ((algorithm_enc & mask_enc) == 0)
  673. continue;
  674. if (algorithm_mac)
  675. if ((algorithm_mac & mask_mac) == 0)
  676. continue;
  677. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  678. ca_curr++;
  679. }
  680. *ca_curr = NULL; /* end of list */
  681. }
  682. static void ssl_cipher_apply_rule(uint32_t cipher_id, uint32_t alg_mkey,
  683. uint32_t alg_auth, uint32_t alg_enc,
  684. uint32_t alg_mac, int min_tls,
  685. uint32_t algo_strength, int rule,
  686. int32_t strength_bits, CIPHER_ORDER **head_p,
  687. CIPHER_ORDER **tail_p)
  688. {
  689. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  690. const SSL_CIPHER *cp;
  691. int reverse = 0;
  692. OSSL_TRACE_BEGIN(TLS_CIPHER){
  693. BIO_printf(trc_out,
  694. "Applying rule %d with %08x/%08x/%08x/%08x/%08x %08x (%d)\n",
  695. rule, alg_mkey, alg_auth, alg_enc, alg_mac, min_tls,
  696. algo_strength, strength_bits);
  697. }
  698. if (rule == CIPHER_DEL || rule == CIPHER_BUMP)
  699. reverse = 1; /* needed to maintain sorting between currently
  700. * deleted ciphers */
  701. head = *head_p;
  702. tail = *tail_p;
  703. if (reverse) {
  704. next = tail;
  705. last = head;
  706. } else {
  707. next = head;
  708. last = tail;
  709. }
  710. curr = NULL;
  711. for (;;) {
  712. if (curr == last)
  713. break;
  714. curr = next;
  715. if (curr == NULL)
  716. break;
  717. next = reverse ? curr->prev : curr->next;
  718. cp = curr->cipher;
  719. /*
  720. * Selection criteria is either the value of strength_bits
  721. * or the algorithms used.
  722. */
  723. if (strength_bits >= 0) {
  724. if (strength_bits != cp->strength_bits)
  725. continue;
  726. } else {
  727. if (trc_out != NULL) {
  728. BIO_printf(trc_out,
  729. "\nName: %s:"
  730. "\nAlgo = %08x/%08x/%08x/%08x/%08x Algo_strength = %08x\n",
  731. cp->name, cp->algorithm_mkey, cp->algorithm_auth,
  732. cp->algorithm_enc, cp->algorithm_mac, cp->min_tls,
  733. cp->algo_strength);
  734. }
  735. if (cipher_id != 0 && (cipher_id != cp->id))
  736. continue;
  737. if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
  738. continue;
  739. if (alg_auth && !(alg_auth & cp->algorithm_auth))
  740. continue;
  741. if (alg_enc && !(alg_enc & cp->algorithm_enc))
  742. continue;
  743. if (alg_mac && !(alg_mac & cp->algorithm_mac))
  744. continue;
  745. if (min_tls && (min_tls != cp->min_tls))
  746. continue;
  747. if ((algo_strength & SSL_STRONG_MASK)
  748. && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
  749. continue;
  750. if ((algo_strength & SSL_DEFAULT_MASK)
  751. && !(algo_strength & SSL_DEFAULT_MASK & cp->algo_strength))
  752. continue;
  753. }
  754. if (trc_out != NULL)
  755. BIO_printf(trc_out, "Action = %d\n", rule);
  756. /* add the cipher if it has not been added yet. */
  757. if (rule == CIPHER_ADD) {
  758. /* reverse == 0 */
  759. if (!curr->active) {
  760. ll_append_tail(&head, curr, &tail);
  761. curr->active = 1;
  762. }
  763. }
  764. /* Move the added cipher to this location */
  765. else if (rule == CIPHER_ORD) {
  766. /* reverse == 0 */
  767. if (curr->active) {
  768. ll_append_tail(&head, curr, &tail);
  769. }
  770. } else if (rule == CIPHER_DEL) {
  771. /* reverse == 1 */
  772. if (curr->active) {
  773. /*
  774. * most recently deleted ciphersuites get best positions for
  775. * any future CIPHER_ADD (note that the CIPHER_DEL loop works
  776. * in reverse to maintain the order)
  777. */
  778. ll_append_head(&head, curr, &tail);
  779. curr->active = 0;
  780. }
  781. } else if (rule == CIPHER_BUMP) {
  782. if (curr->active)
  783. ll_append_head(&head, curr, &tail);
  784. } else if (rule == CIPHER_KILL) {
  785. /* reverse == 0 */
  786. if (head == curr)
  787. head = curr->next;
  788. else
  789. curr->prev->next = curr->next;
  790. if (tail == curr)
  791. tail = curr->prev;
  792. curr->active = 0;
  793. if (curr->next != NULL)
  794. curr->next->prev = curr->prev;
  795. if (curr->prev != NULL)
  796. curr->prev->next = curr->next;
  797. curr->next = NULL;
  798. curr->prev = NULL;
  799. }
  800. }
  801. *head_p = head;
  802. *tail_p = tail;
  803. OSSL_TRACE_END(TLS_CIPHER);
  804. }
  805. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  806. CIPHER_ORDER **tail_p)
  807. {
  808. int32_t max_strength_bits;
  809. int i, *number_uses;
  810. CIPHER_ORDER *curr;
  811. /*
  812. * This routine sorts the ciphers with descending strength. The sorting
  813. * must keep the pre-sorted sequence, so we apply the normal sorting
  814. * routine as '+' movement to the end of the list.
  815. */
  816. max_strength_bits = 0;
  817. curr = *head_p;
  818. while (curr != NULL) {
  819. if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
  820. max_strength_bits = curr->cipher->strength_bits;
  821. curr = curr->next;
  822. }
  823. number_uses = OPENSSL_zalloc(sizeof(int) * (max_strength_bits + 1));
  824. if (number_uses == NULL) {
  825. SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
  826. return 0;
  827. }
  828. /*
  829. * Now find the strength_bits values actually used
  830. */
  831. curr = *head_p;
  832. while (curr != NULL) {
  833. if (curr->active)
  834. number_uses[curr->cipher->strength_bits]++;
  835. curr = curr->next;
  836. }
  837. /*
  838. * Go through the list of used strength_bits values in descending
  839. * order.
  840. */
  841. for (i = max_strength_bits; i >= 0; i--)
  842. if (number_uses[i] > 0)
  843. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p,
  844. tail_p);
  845. OPENSSL_free(number_uses);
  846. return 1;
  847. }
  848. static int ssl_cipher_process_rulestr(const char *rule_str,
  849. CIPHER_ORDER **head_p,
  850. CIPHER_ORDER **tail_p,
  851. const SSL_CIPHER **ca_list, CERT *c)
  852. {
  853. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, algo_strength;
  854. int min_tls;
  855. const char *l, *buf;
  856. int j, multi, found, rule, retval, ok, buflen;
  857. uint32_t cipher_id = 0;
  858. char ch;
  859. retval = 1;
  860. l = rule_str;
  861. for ( ; ; ) {
  862. ch = *l;
  863. if (ch == '\0')
  864. break; /* done */
  865. if (ch == '-') {
  866. rule = CIPHER_DEL;
  867. l++;
  868. } else if (ch == '+') {
  869. rule = CIPHER_ORD;
  870. l++;
  871. } else if (ch == '!') {
  872. rule = CIPHER_KILL;
  873. l++;
  874. } else if (ch == '@') {
  875. rule = CIPHER_SPECIAL;
  876. l++;
  877. } else {
  878. rule = CIPHER_ADD;
  879. }
  880. if (ITEM_SEP(ch)) {
  881. l++;
  882. continue;
  883. }
  884. alg_mkey = 0;
  885. alg_auth = 0;
  886. alg_enc = 0;
  887. alg_mac = 0;
  888. min_tls = 0;
  889. algo_strength = 0;
  890. for (;;) {
  891. ch = *l;
  892. buf = l;
  893. buflen = 0;
  894. #ifndef CHARSET_EBCDIC
  895. while (((ch >= 'A') && (ch <= 'Z')) ||
  896. ((ch >= '0') && (ch <= '9')) ||
  897. ((ch >= 'a') && (ch <= 'z')) ||
  898. (ch == '-') || (ch == '.') || (ch == '='))
  899. #else
  900. while (isalnum((unsigned char)ch) || (ch == '-') || (ch == '.')
  901. || (ch == '='))
  902. #endif
  903. {
  904. ch = *(++l);
  905. buflen++;
  906. }
  907. if (buflen == 0) {
  908. /*
  909. * We hit something we cannot deal with,
  910. * it is no command or separator nor
  911. * alphanumeric, so we call this an error.
  912. */
  913. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, SSL_R_INVALID_COMMAND);
  914. retval = found = 0;
  915. l++;
  916. break;
  917. }
  918. if (rule == CIPHER_SPECIAL) {
  919. found = 0; /* unused -- avoid compiler warning */
  920. break; /* special treatment */
  921. }
  922. /* check for multi-part specification */
  923. if (ch == '+') {
  924. multi = 1;
  925. l++;
  926. } else {
  927. multi = 0;
  928. }
  929. /*
  930. * Now search for the cipher alias in the ca_list. Be careful
  931. * with the strncmp, because the "buflen" limitation
  932. * will make the rule "ADH:SOME" and the cipher
  933. * "ADH-MY-CIPHER" look like a match for buflen=3.
  934. * So additionally check whether the cipher name found
  935. * has the correct length. We can save a strlen() call:
  936. * just checking for the '\0' at the right place is
  937. * sufficient, we have to strncmp() anyway. (We cannot
  938. * use strcmp(), because buf is not '\0' terminated.)
  939. */
  940. j = found = 0;
  941. cipher_id = 0;
  942. while (ca_list[j]) {
  943. if (strncmp(buf, ca_list[j]->name, buflen) == 0
  944. && (ca_list[j]->name[buflen] == '\0')) {
  945. found = 1;
  946. break;
  947. } else
  948. j++;
  949. }
  950. if (!found)
  951. break; /* ignore this entry */
  952. if (ca_list[j]->algorithm_mkey) {
  953. if (alg_mkey) {
  954. alg_mkey &= ca_list[j]->algorithm_mkey;
  955. if (!alg_mkey) {
  956. found = 0;
  957. break;
  958. }
  959. } else {
  960. alg_mkey = ca_list[j]->algorithm_mkey;
  961. }
  962. }
  963. if (ca_list[j]->algorithm_auth) {
  964. if (alg_auth) {
  965. alg_auth &= ca_list[j]->algorithm_auth;
  966. if (!alg_auth) {
  967. found = 0;
  968. break;
  969. }
  970. } else {
  971. alg_auth = ca_list[j]->algorithm_auth;
  972. }
  973. }
  974. if (ca_list[j]->algorithm_enc) {
  975. if (alg_enc) {
  976. alg_enc &= ca_list[j]->algorithm_enc;
  977. if (!alg_enc) {
  978. found = 0;
  979. break;
  980. }
  981. } else {
  982. alg_enc = ca_list[j]->algorithm_enc;
  983. }
  984. }
  985. if (ca_list[j]->algorithm_mac) {
  986. if (alg_mac) {
  987. alg_mac &= ca_list[j]->algorithm_mac;
  988. if (!alg_mac) {
  989. found = 0;
  990. break;
  991. }
  992. } else {
  993. alg_mac = ca_list[j]->algorithm_mac;
  994. }
  995. }
  996. if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
  997. if (algo_strength & SSL_STRONG_MASK) {
  998. algo_strength &=
  999. (ca_list[j]->algo_strength & SSL_STRONG_MASK) |
  1000. ~SSL_STRONG_MASK;
  1001. if (!(algo_strength & SSL_STRONG_MASK)) {
  1002. found = 0;
  1003. break;
  1004. }
  1005. } else {
  1006. algo_strength = ca_list[j]->algo_strength & SSL_STRONG_MASK;
  1007. }
  1008. }
  1009. if (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) {
  1010. if (algo_strength & SSL_DEFAULT_MASK) {
  1011. algo_strength &=
  1012. (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) |
  1013. ~SSL_DEFAULT_MASK;
  1014. if (!(algo_strength & SSL_DEFAULT_MASK)) {
  1015. found = 0;
  1016. break;
  1017. }
  1018. } else {
  1019. algo_strength |=
  1020. ca_list[j]->algo_strength & SSL_DEFAULT_MASK;
  1021. }
  1022. }
  1023. if (ca_list[j]->valid) {
  1024. /*
  1025. * explicit ciphersuite found; its protocol version does not
  1026. * become part of the search pattern!
  1027. */
  1028. cipher_id = ca_list[j]->id;
  1029. } else {
  1030. /*
  1031. * not an explicit ciphersuite; only in this case, the
  1032. * protocol version is considered part of the search pattern
  1033. */
  1034. if (ca_list[j]->min_tls) {
  1035. if (min_tls != 0 && min_tls != ca_list[j]->min_tls) {
  1036. found = 0;
  1037. break;
  1038. } else {
  1039. min_tls = ca_list[j]->min_tls;
  1040. }
  1041. }
  1042. }
  1043. if (!multi)
  1044. break;
  1045. }
  1046. /*
  1047. * Ok, we have the rule, now apply it
  1048. */
  1049. if (rule == CIPHER_SPECIAL) { /* special command */
  1050. ok = 0;
  1051. if ((buflen == 8) && strncmp(buf, "STRENGTH", 8) == 0) {
  1052. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1053. } else if (buflen == 10 && strncmp(buf, "SECLEVEL=", 9) == 0) {
  1054. int level = buf[9] - '0';
  1055. if (level < 0 || level > 5) {
  1056. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
  1057. SSL_R_INVALID_COMMAND);
  1058. } else {
  1059. c->sec_level = level;
  1060. ok = 1;
  1061. }
  1062. } else {
  1063. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, SSL_R_INVALID_COMMAND);
  1064. }
  1065. if (ok == 0)
  1066. retval = 0;
  1067. /*
  1068. * We do not support any "multi" options
  1069. * together with "@", so throw away the
  1070. * rest of the command, if any left, until
  1071. * end or ':' is found.
  1072. */
  1073. while ((*l != '\0') && !ITEM_SEP(*l))
  1074. l++;
  1075. } else if (found) {
  1076. ssl_cipher_apply_rule(cipher_id,
  1077. alg_mkey, alg_auth, alg_enc, alg_mac,
  1078. min_tls, algo_strength, rule, -1, head_p,
  1079. tail_p);
  1080. } else {
  1081. while ((*l != '\0') && !ITEM_SEP(*l))
  1082. l++;
  1083. }
  1084. if (*l == '\0')
  1085. break; /* done */
  1086. }
  1087. return retval;
  1088. }
  1089. #ifndef OPENSSL_NO_EC
  1090. static int check_suiteb_cipher_list(const SSL_METHOD *meth, CERT *c,
  1091. const char **prule_str)
  1092. {
  1093. unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
  1094. if (strncmp(*prule_str, "SUITEB128ONLY", 13) == 0) {
  1095. suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
  1096. } else if (strncmp(*prule_str, "SUITEB128C2", 11) == 0) {
  1097. suiteb_comb2 = 1;
  1098. suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
  1099. } else if (strncmp(*prule_str, "SUITEB128", 9) == 0) {
  1100. suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
  1101. } else if (strncmp(*prule_str, "SUITEB192", 9) == 0) {
  1102. suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
  1103. }
  1104. if (suiteb_flags) {
  1105. c->cert_flags &= ~SSL_CERT_FLAG_SUITEB_128_LOS;
  1106. c->cert_flags |= suiteb_flags;
  1107. } else {
  1108. suiteb_flags = c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS;
  1109. }
  1110. if (!suiteb_flags)
  1111. return 1;
  1112. /* Check version: if TLS 1.2 ciphers allowed we can use Suite B */
  1113. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)) {
  1114. SSLerr(SSL_F_CHECK_SUITEB_CIPHER_LIST,
  1115. SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE);
  1116. return 0;
  1117. }
  1118. # ifndef OPENSSL_NO_EC
  1119. switch (suiteb_flags) {
  1120. case SSL_CERT_FLAG_SUITEB_128_LOS:
  1121. if (suiteb_comb2)
  1122. *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
  1123. else
  1124. *prule_str =
  1125. "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384";
  1126. break;
  1127. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  1128. *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256";
  1129. break;
  1130. case SSL_CERT_FLAG_SUITEB_192_LOS:
  1131. *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
  1132. break;
  1133. }
  1134. return 1;
  1135. # else
  1136. SSLerr(SSL_F_CHECK_SUITEB_CIPHER_LIST, SSL_R_ECDH_REQUIRED_FOR_SUITEB_MODE);
  1137. return 0;
  1138. # endif
  1139. }
  1140. #endif
  1141. static int ciphersuite_cb(const char *elem, int len, void *arg)
  1142. {
  1143. STACK_OF(SSL_CIPHER) *ciphersuites = (STACK_OF(SSL_CIPHER) *)arg;
  1144. const SSL_CIPHER *cipher;
  1145. /* Arbitrary sized temp buffer for the cipher name. Should be big enough */
  1146. char name[80];
  1147. if (len > (int)(sizeof(name) - 1)) {
  1148. SSLerr(SSL_F_CIPHERSUITE_CB, SSL_R_NO_CIPHER_MATCH);
  1149. return 0;
  1150. }
  1151. memcpy(name, elem, len);
  1152. name[len] = '\0';
  1153. cipher = ssl3_get_cipher_by_std_name(name);
  1154. if (cipher == NULL) {
  1155. SSLerr(SSL_F_CIPHERSUITE_CB, SSL_R_NO_CIPHER_MATCH);
  1156. return 0;
  1157. }
  1158. if (!sk_SSL_CIPHER_push(ciphersuites, cipher)) {
  1159. SSLerr(SSL_F_CIPHERSUITE_CB, ERR_R_INTERNAL_ERROR);
  1160. return 0;
  1161. }
  1162. return 1;
  1163. }
  1164. static __owur int set_ciphersuites(STACK_OF(SSL_CIPHER) **currciphers, const char *str)
  1165. {
  1166. STACK_OF(SSL_CIPHER) *newciphers = sk_SSL_CIPHER_new_null();
  1167. if (newciphers == NULL)
  1168. return 0;
  1169. /* Parse the list. We explicitly allow an empty list */
  1170. if (*str != '\0'
  1171. && !CONF_parse_list(str, ':', 1, ciphersuite_cb, newciphers)) {
  1172. sk_SSL_CIPHER_free(newciphers);
  1173. return 0;
  1174. }
  1175. sk_SSL_CIPHER_free(*currciphers);
  1176. *currciphers = newciphers;
  1177. return 1;
  1178. }
  1179. static int update_cipher_list_by_id(STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  1180. STACK_OF(SSL_CIPHER) *cipherstack)
  1181. {
  1182. STACK_OF(SSL_CIPHER) *tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1183. if (tmp_cipher_list == NULL) {
  1184. return 0;
  1185. }
  1186. sk_SSL_CIPHER_free(*cipher_list_by_id);
  1187. *cipher_list_by_id = tmp_cipher_list;
  1188. (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
  1189. sk_SSL_CIPHER_sort(*cipher_list_by_id);
  1190. return 1;
  1191. }
  1192. static int update_cipher_list(STACK_OF(SSL_CIPHER) **cipher_list,
  1193. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  1194. STACK_OF(SSL_CIPHER) *tls13_ciphersuites)
  1195. {
  1196. int i;
  1197. STACK_OF(SSL_CIPHER) *tmp_cipher_list = sk_SSL_CIPHER_dup(*cipher_list);
  1198. if (tmp_cipher_list == NULL)
  1199. return 0;
  1200. /*
  1201. * Delete any existing TLSv1.3 ciphersuites. These are always first in the
  1202. * list.
  1203. */
  1204. while (sk_SSL_CIPHER_num(tmp_cipher_list) > 0
  1205. && sk_SSL_CIPHER_value(tmp_cipher_list, 0)->min_tls
  1206. == TLS1_3_VERSION)
  1207. sk_SSL_CIPHER_delete(tmp_cipher_list, 0);
  1208. /* Insert the new TLSv1.3 ciphersuites */
  1209. for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++)
  1210. sk_SSL_CIPHER_insert(tmp_cipher_list,
  1211. sk_SSL_CIPHER_value(tls13_ciphersuites, i), i);
  1212. if (!update_cipher_list_by_id(cipher_list_by_id, tmp_cipher_list))
  1213. return 0;
  1214. sk_SSL_CIPHER_free(*cipher_list);
  1215. *cipher_list = tmp_cipher_list;
  1216. return 1;
  1217. }
  1218. int SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
  1219. {
  1220. int ret = set_ciphersuites(&(ctx->tls13_ciphersuites), str);
  1221. if (ret && ctx->cipher_list != NULL)
  1222. return update_cipher_list(&ctx->cipher_list, &ctx->cipher_list_by_id,
  1223. ctx->tls13_ciphersuites);
  1224. return ret;
  1225. }
  1226. int SSL_set_ciphersuites(SSL *s, const char *str)
  1227. {
  1228. STACK_OF(SSL_CIPHER) *cipher_list;
  1229. int ret = set_ciphersuites(&(s->tls13_ciphersuites), str);
  1230. if (s->cipher_list == NULL) {
  1231. if ((cipher_list = SSL_get_ciphers(s)) != NULL)
  1232. s->cipher_list = sk_SSL_CIPHER_dup(cipher_list);
  1233. }
  1234. if (ret && s->cipher_list != NULL)
  1235. return update_cipher_list(&s->cipher_list, &s->cipher_list_by_id,
  1236. s->tls13_ciphersuites);
  1237. return ret;
  1238. }
  1239. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  1240. STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
  1241. STACK_OF(SSL_CIPHER) **cipher_list,
  1242. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  1243. const char *rule_str,
  1244. CERT *c)
  1245. {
  1246. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases, i;
  1247. uint32_t disabled_mkey, disabled_auth, disabled_enc, disabled_mac;
  1248. STACK_OF(SSL_CIPHER) *cipherstack;
  1249. const char *rule_p;
  1250. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1251. const SSL_CIPHER **ca_list = NULL;
  1252. /*
  1253. * Return with error if nothing to do.
  1254. */
  1255. if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
  1256. return NULL;
  1257. #ifndef OPENSSL_NO_EC
  1258. if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
  1259. return NULL;
  1260. #endif
  1261. /*
  1262. * To reduce the work to do we only want to process the compiled
  1263. * in algorithms, so we first get the mask of disabled ciphers.
  1264. */
  1265. disabled_mkey = disabled_mkey_mask;
  1266. disabled_auth = disabled_auth_mask;
  1267. disabled_enc = disabled_enc_mask;
  1268. disabled_mac = disabled_mac_mask;
  1269. /*
  1270. * Now we have to collect the available ciphers from the compiled
  1271. * in ciphers. We cannot get more than the number compiled in, so
  1272. * it is used for allocation.
  1273. */
  1274. num_of_ciphers = ssl_method->num_ciphers();
  1275. co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
  1276. if (co_list == NULL) {
  1277. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  1278. return NULL; /* Failure */
  1279. }
  1280. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
  1281. disabled_mkey, disabled_auth, disabled_enc,
  1282. disabled_mac, co_list, &head, &tail);
  1283. /* Now arrange all ciphers by preference. */
  1284. /*
  1285. * Everything else being equal, prefer ephemeral ECDH over other key
  1286. * exchange mechanisms.
  1287. * For consistency, prefer ECDSA over RSA (though this only matters if the
  1288. * server has both certificates, and is using the DEFAULT, or a client
  1289. * preference).
  1290. */
  1291. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD,
  1292. -1, &head, &tail);
  1293. ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head,
  1294. &tail);
  1295. ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
  1296. &tail);
  1297. /* Within each strength group, we prefer GCM over CHACHA... */
  1298. ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
  1299. &head, &tail);
  1300. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
  1301. &head, &tail);
  1302. /*
  1303. * ...and generally, our preferred cipher is AES.
  1304. * Note that AEADs will be bumped to take preference after sorting by
  1305. * strength.
  1306. */
  1307. ssl_cipher_apply_rule(0, 0, 0, SSL_AES ^ SSL_AESGCM, 0, 0, 0, CIPHER_ADD,
  1308. -1, &head, &tail);
  1309. /* Temporarily enable everything else for sorting */
  1310. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
  1311. /* Low priority for MD5 */
  1312. ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head,
  1313. &tail);
  1314. /*
  1315. * Move anonymous ciphers to the end. Usually, these will remain
  1316. * disabled. (For applications that allow them, they aren't too bad, but
  1317. * we prefer authenticated ciphers.)
  1318. */
  1319. ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
  1320. &tail);
  1321. ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
  1322. &tail);
  1323. ssl_cipher_apply_rule(0, SSL_kPSK, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
  1324. &tail);
  1325. /* RC4 is sort-of broken -- move to the end */
  1326. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head,
  1327. &tail);
  1328. /*
  1329. * Now sort by symmetric encryption strength. The above ordering remains
  1330. * in force within each class
  1331. */
  1332. if (!ssl_cipher_strength_sort(&head, &tail)) {
  1333. OPENSSL_free(co_list);
  1334. return NULL;
  1335. }
  1336. /*
  1337. * Partially overrule strength sort to prefer TLS 1.2 ciphers/PRFs.
  1338. * TODO(openssl-team): is there an easier way to accomplish all this?
  1339. */
  1340. ssl_cipher_apply_rule(0, 0, 0, 0, 0, TLS1_2_VERSION, 0, CIPHER_BUMP, -1,
  1341. &head, &tail);
  1342. /*
  1343. * Irrespective of strength, enforce the following order:
  1344. * (EC)DHE + AEAD > (EC)DHE > rest of AEAD > rest.
  1345. * Within each group, ciphers remain sorted by strength and previous
  1346. * preference, i.e.,
  1347. * 1) ECDHE > DHE
  1348. * 2) GCM > CHACHA
  1349. * 3) AES > rest
  1350. * 4) TLS 1.2 > legacy
  1351. *
  1352. * Because we now bump ciphers to the top of the list, we proceed in
  1353. * reverse order of preference.
  1354. */
  1355. ssl_cipher_apply_rule(0, 0, 0, 0, SSL_AEAD, 0, 0, CIPHER_BUMP, -1,
  1356. &head, &tail);
  1357. ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, 0, 0, 0,
  1358. CIPHER_BUMP, -1, &head, &tail);
  1359. ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, SSL_AEAD, 0, 0,
  1360. CIPHER_BUMP, -1, &head, &tail);
  1361. /* Now disable everything (maintaining the ordering!) */
  1362. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
  1363. /*
  1364. * We also need cipher aliases for selecting based on the rule_str.
  1365. * There might be two types of entries in the rule_str: 1) names
  1366. * of ciphers themselves 2) aliases for groups of ciphers.
  1367. * For 1) we need the available ciphers and for 2) the cipher
  1368. * groups of cipher_aliases added together in one list (otherwise
  1369. * we would be happy with just the cipher_aliases table).
  1370. */
  1371. num_of_group_aliases = OSSL_NELEM(cipher_aliases);
  1372. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  1373. ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
  1374. if (ca_list == NULL) {
  1375. OPENSSL_free(co_list);
  1376. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  1377. return NULL; /* Failure */
  1378. }
  1379. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
  1380. disabled_mkey, disabled_auth, disabled_enc,
  1381. disabled_mac, head);
  1382. /*
  1383. * If the rule_string begins with DEFAULT, apply the default rule
  1384. * before using the (possibly available) additional rules.
  1385. */
  1386. ok = 1;
  1387. rule_p = rule_str;
  1388. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1389. ok = ssl_cipher_process_rulestr(OSSL_default_cipher_list(),
  1390. &head, &tail, ca_list, c);
  1391. rule_p += 7;
  1392. if (*rule_p == ':')
  1393. rule_p++;
  1394. }
  1395. if (ok && (strlen(rule_p) > 0))
  1396. ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, c);
  1397. OPENSSL_free(ca_list); /* Not needed anymore */
  1398. if (!ok) { /* Rule processing failure */
  1399. OPENSSL_free(co_list);
  1400. return NULL;
  1401. }
  1402. /*
  1403. * Allocate new "cipherstack" for the result, return with error
  1404. * if we cannot get one.
  1405. */
  1406. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
  1407. OPENSSL_free(co_list);
  1408. return NULL;
  1409. }
  1410. /* Add TLSv1.3 ciphers first - we always prefer those if possible */
  1411. for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++) {
  1412. if (!sk_SSL_CIPHER_push(cipherstack,
  1413. sk_SSL_CIPHER_value(tls13_ciphersuites, i))) {
  1414. sk_SSL_CIPHER_free(cipherstack);
  1415. return NULL;
  1416. }
  1417. }
  1418. OSSL_TRACE_BEGIN(TLS_CIPHER) {
  1419. BIO_printf(trc_out, "cipher selection:\n");
  1420. }
  1421. /*
  1422. * The cipher selection for the list is done. The ciphers are added
  1423. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1424. */
  1425. for (curr = head; curr != NULL; curr = curr->next) {
  1426. if (curr->active) {
  1427. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1428. OPENSSL_free(co_list);
  1429. sk_SSL_CIPHER_free(cipherstack);
  1430. OSSL_TRACE_CANCEL(TLS_CIPHER);
  1431. return NULL;
  1432. }
  1433. if (trc_out != NULL)
  1434. BIO_printf(trc_out, "<%s>\n", curr->cipher->name);
  1435. }
  1436. }
  1437. OPENSSL_free(co_list); /* Not needed any longer */
  1438. OSSL_TRACE_END(TLS_CIPHER);
  1439. if (!update_cipher_list_by_id(cipher_list_by_id, cipherstack)) {
  1440. sk_SSL_CIPHER_free(cipherstack);
  1441. return NULL;
  1442. }
  1443. sk_SSL_CIPHER_free(*cipher_list);
  1444. *cipher_list = cipherstack;
  1445. return cipherstack;
  1446. }
  1447. char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
  1448. {
  1449. const char *ver;
  1450. const char *kx, *au, *enc, *mac;
  1451. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1452. static const char *format = "%-30s %-7s Kx=%-8s Au=%-5s Enc=%-9s Mac=%-4s\n";
  1453. if (buf == NULL) {
  1454. len = 128;
  1455. if ((buf = OPENSSL_malloc(len)) == NULL) {
  1456. SSLerr(SSL_F_SSL_CIPHER_DESCRIPTION, ERR_R_MALLOC_FAILURE);
  1457. return NULL;
  1458. }
  1459. } else if (len < 128) {
  1460. return NULL;
  1461. }
  1462. alg_mkey = cipher->algorithm_mkey;
  1463. alg_auth = cipher->algorithm_auth;
  1464. alg_enc = cipher->algorithm_enc;
  1465. alg_mac = cipher->algorithm_mac;
  1466. ver = ssl_protocol_to_string(cipher->min_tls);
  1467. switch (alg_mkey) {
  1468. case SSL_kRSA:
  1469. kx = "RSA";
  1470. break;
  1471. case SSL_kDHE:
  1472. kx = "DH";
  1473. break;
  1474. case SSL_kECDHE:
  1475. kx = "ECDH";
  1476. break;
  1477. case SSL_kPSK:
  1478. kx = "PSK";
  1479. break;
  1480. case SSL_kRSAPSK:
  1481. kx = "RSAPSK";
  1482. break;
  1483. case SSL_kECDHEPSK:
  1484. kx = "ECDHEPSK";
  1485. break;
  1486. case SSL_kDHEPSK:
  1487. kx = "DHEPSK";
  1488. break;
  1489. case SSL_kSRP:
  1490. kx = "SRP";
  1491. break;
  1492. case SSL_kGOST:
  1493. kx = "GOST";
  1494. break;
  1495. case SSL_kANY:
  1496. kx = "any";
  1497. break;
  1498. default:
  1499. kx = "unknown";
  1500. }
  1501. switch (alg_auth) {
  1502. case SSL_aRSA:
  1503. au = "RSA";
  1504. break;
  1505. case SSL_aDSS:
  1506. au = "DSS";
  1507. break;
  1508. case SSL_aNULL:
  1509. au = "None";
  1510. break;
  1511. case SSL_aECDSA:
  1512. au = "ECDSA";
  1513. break;
  1514. case SSL_aPSK:
  1515. au = "PSK";
  1516. break;
  1517. case SSL_aSRP:
  1518. au = "SRP";
  1519. break;
  1520. case SSL_aGOST01:
  1521. au = "GOST01";
  1522. break;
  1523. /* New GOST ciphersuites have both SSL_aGOST12 and SSL_aGOST01 bits */
  1524. case (SSL_aGOST12 | SSL_aGOST01):
  1525. au = "GOST12";
  1526. break;
  1527. case SSL_aANY:
  1528. au = "any";
  1529. break;
  1530. default:
  1531. au = "unknown";
  1532. break;
  1533. }
  1534. switch (alg_enc) {
  1535. case SSL_DES:
  1536. enc = "DES(56)";
  1537. break;
  1538. case SSL_3DES:
  1539. enc = "3DES(168)";
  1540. break;
  1541. case SSL_RC4:
  1542. enc = "RC4(128)";
  1543. break;
  1544. case SSL_RC2:
  1545. enc = "RC2(128)";
  1546. break;
  1547. case SSL_IDEA:
  1548. enc = "IDEA(128)";
  1549. break;
  1550. case SSL_eNULL:
  1551. enc = "None";
  1552. break;
  1553. case SSL_AES128:
  1554. enc = "AES(128)";
  1555. break;
  1556. case SSL_AES256:
  1557. enc = "AES(256)";
  1558. break;
  1559. case SSL_AES128GCM:
  1560. enc = "AESGCM(128)";
  1561. break;
  1562. case SSL_AES256GCM:
  1563. enc = "AESGCM(256)";
  1564. break;
  1565. case SSL_AES128CCM:
  1566. enc = "AESCCM(128)";
  1567. break;
  1568. case SSL_AES256CCM:
  1569. enc = "AESCCM(256)";
  1570. break;
  1571. case SSL_AES128CCM8:
  1572. enc = "AESCCM8(128)";
  1573. break;
  1574. case SSL_AES256CCM8:
  1575. enc = "AESCCM8(256)";
  1576. break;
  1577. case SSL_CAMELLIA128:
  1578. enc = "Camellia(128)";
  1579. break;
  1580. case SSL_CAMELLIA256:
  1581. enc = "Camellia(256)";
  1582. break;
  1583. case SSL_ARIA128GCM:
  1584. enc = "ARIAGCM(128)";
  1585. break;
  1586. case SSL_ARIA256GCM:
  1587. enc = "ARIAGCM(256)";
  1588. break;
  1589. case SSL_SEED:
  1590. enc = "SEED(128)";
  1591. break;
  1592. case SSL_eGOST2814789CNT:
  1593. case SSL_eGOST2814789CNT12:
  1594. enc = "GOST89(256)";
  1595. break;
  1596. case SSL_CHACHA20POLY1305:
  1597. enc = "CHACHA20/POLY1305(256)";
  1598. break;
  1599. default:
  1600. enc = "unknown";
  1601. break;
  1602. }
  1603. switch (alg_mac) {
  1604. case SSL_MD5:
  1605. mac = "MD5";
  1606. break;
  1607. case SSL_SHA1:
  1608. mac = "SHA1";
  1609. break;
  1610. case SSL_SHA256:
  1611. mac = "SHA256";
  1612. break;
  1613. case SSL_SHA384:
  1614. mac = "SHA384";
  1615. break;
  1616. case SSL_AEAD:
  1617. mac = "AEAD";
  1618. break;
  1619. case SSL_GOST89MAC:
  1620. case SSL_GOST89MAC12:
  1621. mac = "GOST89";
  1622. break;
  1623. case SSL_GOST94:
  1624. mac = "GOST94";
  1625. break;
  1626. case SSL_GOST12_256:
  1627. case SSL_GOST12_512:
  1628. mac = "GOST2012";
  1629. break;
  1630. default:
  1631. mac = "unknown";
  1632. break;
  1633. }
  1634. BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
  1635. return buf;
  1636. }
  1637. const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1638. {
  1639. if (c == NULL)
  1640. return "(NONE)";
  1641. /*
  1642. * Backwards-compatibility crutch. In almost all contexts we report TLS
  1643. * 1.0 as "TLSv1", but for ciphers we report "TLSv1.0".
  1644. */
  1645. if (c->min_tls == TLS1_VERSION)
  1646. return "TLSv1.0";
  1647. return ssl_protocol_to_string(c->min_tls);
  1648. }
  1649. /* return the actual cipher being used */
  1650. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1651. {
  1652. if (c != NULL)
  1653. return c->name;
  1654. return "(NONE)";
  1655. }
  1656. /* return the actual cipher being used in RFC standard name */
  1657. const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
  1658. {
  1659. if (c != NULL)
  1660. return c->stdname;
  1661. return "(NONE)";
  1662. }
  1663. /* return the OpenSSL name based on given RFC standard name */
  1664. const char *OPENSSL_cipher_name(const char *stdname)
  1665. {
  1666. const SSL_CIPHER *c;
  1667. if (stdname == NULL)
  1668. return "(NONE)";
  1669. c = ssl3_get_cipher_by_std_name(stdname);
  1670. return SSL_CIPHER_get_name(c);
  1671. }
  1672. /* number of bits for symmetric cipher */
  1673. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1674. {
  1675. int ret = 0;
  1676. if (c != NULL) {
  1677. if (alg_bits != NULL)
  1678. *alg_bits = (int)c->alg_bits;
  1679. ret = (int)c->strength_bits;
  1680. }
  1681. return ret;
  1682. }
  1683. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c)
  1684. {
  1685. return c->id;
  1686. }
  1687. uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *c)
  1688. {
  1689. return c->id & 0xFFFF;
  1690. }
  1691. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  1692. {
  1693. SSL_COMP *ctmp;
  1694. int i, nn;
  1695. if ((n == 0) || (sk == NULL))
  1696. return NULL;
  1697. nn = sk_SSL_COMP_num(sk);
  1698. for (i = 0; i < nn; i++) {
  1699. ctmp = sk_SSL_COMP_value(sk, i);
  1700. if (ctmp->id == n)
  1701. return ctmp;
  1702. }
  1703. return NULL;
  1704. }
  1705. #ifdef OPENSSL_NO_COMP
  1706. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1707. {
  1708. return NULL;
  1709. }
  1710. STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
  1711. *meths)
  1712. {
  1713. return meths;
  1714. }
  1715. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1716. {
  1717. return 1;
  1718. }
  1719. #else
  1720. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1721. {
  1722. load_builtin_compressions();
  1723. return ssl_comp_methods;
  1724. }
  1725. STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
  1726. *meths)
  1727. {
  1728. STACK_OF(SSL_COMP) *old_meths = ssl_comp_methods;
  1729. ssl_comp_methods = meths;
  1730. return old_meths;
  1731. }
  1732. static void cmeth_free(SSL_COMP *cm)
  1733. {
  1734. OPENSSL_free(cm);
  1735. }
  1736. void ssl_comp_free_compression_methods_int(void)
  1737. {
  1738. STACK_OF(SSL_COMP) *old_meths = ssl_comp_methods;
  1739. ssl_comp_methods = NULL;
  1740. sk_SSL_COMP_pop_free(old_meths, cmeth_free);
  1741. }
  1742. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1743. {
  1744. SSL_COMP *comp;
  1745. if (cm == NULL || COMP_get_type(cm) == NID_undef)
  1746. return 1;
  1747. /*-
  1748. * According to draft-ietf-tls-compression-04.txt, the
  1749. * compression number ranges should be the following:
  1750. *
  1751. * 0 to 63: methods defined by the IETF
  1752. * 64 to 192: external party methods assigned by IANA
  1753. * 193 to 255: reserved for private use
  1754. */
  1755. if (id < 193 || id > 255) {
  1756. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
  1757. SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
  1758. return 1;
  1759. }
  1760. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  1761. comp = OPENSSL_malloc(sizeof(*comp));
  1762. if (comp == NULL) {
  1763. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1764. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
  1765. return 1;
  1766. }
  1767. comp->id = id;
  1768. comp->method = cm;
  1769. load_builtin_compressions();
  1770. if (ssl_comp_methods && sk_SSL_COMP_find(ssl_comp_methods, comp) >= 0) {
  1771. OPENSSL_free(comp);
  1772. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1773. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
  1774. SSL_R_DUPLICATE_COMPRESSION_ID);
  1775. return 1;
  1776. }
  1777. if (ssl_comp_methods == NULL || !sk_SSL_COMP_push(ssl_comp_methods, comp)) {
  1778. OPENSSL_free(comp);
  1779. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1780. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
  1781. return 1;
  1782. }
  1783. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1784. return 0;
  1785. }
  1786. #endif
  1787. const char *SSL_COMP_get_name(const COMP_METHOD *comp)
  1788. {
  1789. #ifndef OPENSSL_NO_COMP
  1790. return comp ? COMP_get_name(comp) : NULL;
  1791. #else
  1792. return NULL;
  1793. #endif
  1794. }
  1795. const char *SSL_COMP_get0_name(const SSL_COMP *comp)
  1796. {
  1797. #ifndef OPENSSL_NO_COMP
  1798. return comp->name;
  1799. #else
  1800. return NULL;
  1801. #endif
  1802. }
  1803. int SSL_COMP_get_id(const SSL_COMP *comp)
  1804. {
  1805. #ifndef OPENSSL_NO_COMP
  1806. return comp->id;
  1807. #else
  1808. return -1;
  1809. #endif
  1810. }
  1811. const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr,
  1812. int all)
  1813. {
  1814. const SSL_CIPHER *c = ssl->method->get_cipher_by_char(ptr);
  1815. if (c == NULL || (!all && c->valid == 0))
  1816. return NULL;
  1817. return c;
  1818. }
  1819. const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
  1820. {
  1821. return ssl->method->get_cipher_by_char(ptr);
  1822. }
  1823. int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
  1824. {
  1825. int i;
  1826. if (c == NULL)
  1827. return NID_undef;
  1828. i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
  1829. if (i == -1)
  1830. return NID_undef;
  1831. return ssl_cipher_table_cipher[i].nid;
  1832. }
  1833. int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
  1834. {
  1835. int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
  1836. if (i == -1)
  1837. return NID_undef;
  1838. return ssl_cipher_table_mac[i].nid;
  1839. }
  1840. int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
  1841. {
  1842. int i = ssl_cipher_info_lookup(ssl_cipher_table_kx, c->algorithm_mkey);
  1843. if (i == -1)
  1844. return NID_undef;
  1845. return ssl_cipher_table_kx[i].nid;
  1846. }
  1847. int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
  1848. {
  1849. int i = ssl_cipher_info_lookup(ssl_cipher_table_auth, c->algorithm_auth);
  1850. if (i == -1)
  1851. return NID_undef;
  1852. return ssl_cipher_table_auth[i].nid;
  1853. }
  1854. const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c)
  1855. {
  1856. int idx = c->algorithm2 & SSL_HANDSHAKE_MAC_MASK;
  1857. if (idx < 0 || idx >= SSL_MD_NUM_IDX)
  1858. return NULL;
  1859. return ssl_digest_methods[idx];
  1860. }
  1861. int SSL_CIPHER_is_aead(const SSL_CIPHER *c)
  1862. {
  1863. return (c->algorithm_mac & SSL_AEAD) ? 1 : 0;
  1864. }
  1865. int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead,
  1866. size_t *int_overhead, size_t *blocksize,
  1867. size_t *ext_overhead)
  1868. {
  1869. size_t mac = 0, in = 0, blk = 0, out = 0;
  1870. /* Some hard-coded numbers for the CCM/Poly1305 MAC overhead
  1871. * because there are no handy #defines for those. */
  1872. if (c->algorithm_enc & (SSL_AESGCM | SSL_ARIAGCM)) {
  1873. out = EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  1874. } else if (c->algorithm_enc & (SSL_AES128CCM | SSL_AES256CCM)) {
  1875. out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 16;
  1876. } else if (c->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8)) {
  1877. out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 8;
  1878. } else if (c->algorithm_enc & SSL_CHACHA20POLY1305) {
  1879. out = 16;
  1880. } else if (c->algorithm_mac & SSL_AEAD) {
  1881. /* We're supposed to have handled all the AEAD modes above */
  1882. return 0;
  1883. } else {
  1884. /* Non-AEAD modes. Calculate MAC/cipher overhead separately */
  1885. int digest_nid = SSL_CIPHER_get_digest_nid(c);
  1886. const EVP_MD *e_md = EVP_get_digestbynid(digest_nid);
  1887. if (e_md == NULL)
  1888. return 0;
  1889. mac = EVP_MD_size(e_md);
  1890. if (c->algorithm_enc != SSL_eNULL) {
  1891. int cipher_nid = SSL_CIPHER_get_cipher_nid(c);
  1892. const EVP_CIPHER *e_ciph = EVP_get_cipherbynid(cipher_nid);
  1893. /* If it wasn't AEAD or SSL_eNULL, we expect it to be a
  1894. known CBC cipher. */
  1895. if (e_ciph == NULL ||
  1896. EVP_CIPHER_mode(e_ciph) != EVP_CIPH_CBC_MODE)
  1897. return 0;
  1898. in = 1; /* padding length byte */
  1899. out = EVP_CIPHER_iv_length(e_ciph);
  1900. blk = EVP_CIPHER_block_size(e_ciph);
  1901. }
  1902. }
  1903. *mac_overhead = mac;
  1904. *int_overhead = in;
  1905. *blocksize = blk;
  1906. *ext_overhead = out;
  1907. return 1;
  1908. }
  1909. int ssl_cert_is_disabled(size_t idx)
  1910. {
  1911. const SSL_CERT_LOOKUP *cl = ssl_cert_lookup_by_idx(idx);
  1912. if (cl == NULL || (cl->amask & disabled_auth_mask) != 0)
  1913. return 1;
  1914. return 0;
  1915. }
  1916. /*
  1917. * Default list of TLSv1.2 (and earlier) ciphers
  1918. * SSL_DEFAULT_CIPHER_LIST deprecated in 3.0.0
  1919. * Update both macro and function simultaneously
  1920. */
  1921. const char *OSSL_default_cipher_list(void)
  1922. {
  1923. return "ALL:!COMPLEMENTOFDEFAULT:!eNULL";
  1924. }
  1925. /*
  1926. * Default list of TLSv1.3 (and later) ciphers
  1927. * TLS_DEFAULT_CIPHERSUITES deprecated in 3.0.0
  1928. * Update both macro and function simultaneously
  1929. */
  1930. const char *OSSL_default_ciphersuites(void)
  1931. {
  1932. return "TLS_AES_256_GCM_SHA384:"
  1933. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1934. "TLS_CHACHA20_POLY1305_SHA256:"
  1935. #endif
  1936. "TLS_AES_128_GCM_SHA256";
  1937. }