ssl_ciph.c 70 KB

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