ssl_ciph.c 68 KB

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