ssl_ciph.c 66 KB

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