ssl_ciph.c 69 KB

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