ssl_ciph.c 70 KB

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