ssl_ciph.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /*
  2. * Copyright 1995-2018 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 "internal/nelem.h"
  19. #include "ssl_locl.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_locl.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. };
  157. static size_t ssl_mac_secret_size[SSL_MD_NUM_IDX];
  158. #define CIPHER_ADD 1
  159. #define CIPHER_KILL 2
  160. #define CIPHER_DEL 3
  161. #define CIPHER_ORD 4
  162. #define CIPHER_SPECIAL 5
  163. /*
  164. * Bump the ciphers to the top of the list.
  165. * This rule isn't currently supported by the public cipherstring API.
  166. */
  167. #define CIPHER_BUMP 6
  168. typedef struct cipher_order_st {
  169. const SSL_CIPHER *cipher;
  170. int active;
  171. int dead;
  172. struct cipher_order_st *next, *prev;
  173. } CIPHER_ORDER;
  174. static const SSL_CIPHER cipher_aliases[] = {
  175. /* "ALL" doesn't include eNULL (must be specifically enabled) */
  176. {0, SSL_TXT_ALL, NULL, 0, 0, 0, ~SSL_eNULL},
  177. /* "COMPLEMENTOFALL" */
  178. {0, SSL_TXT_CMPALL, NULL, 0, 0, 0, SSL_eNULL},
  179. /*
  180. * "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in
  181. * ALL!)
  182. */
  183. {0, SSL_TXT_CMPDEF, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_NOT_DEFAULT},
  184. /*
  185. * key exchange aliases (some of those using only a single bit here
  186. * combine multiple key exchange algs according to the RFCs, e.g. kDHE
  187. * combines DHE_DSS and DHE_RSA)
  188. */
  189. {0, SSL_TXT_kRSA, NULL, 0, SSL_kRSA},
  190. {0, SSL_TXT_kEDH, NULL, 0, SSL_kDHE},
  191. {0, SSL_TXT_kDHE, NULL, 0, SSL_kDHE},
  192. {0, SSL_TXT_DH, NULL, 0, SSL_kDHE},
  193. {0, SSL_TXT_kEECDH, NULL, 0, SSL_kECDHE},
  194. {0, SSL_TXT_kECDHE, NULL, 0, SSL_kECDHE},
  195. {0, SSL_TXT_ECDH, NULL, 0, SSL_kECDHE},
  196. {0, SSL_TXT_kPSK, NULL, 0, SSL_kPSK},
  197. {0, SSL_TXT_kRSAPSK, NULL, 0, SSL_kRSAPSK},
  198. {0, SSL_TXT_kECDHEPSK, NULL, 0, SSL_kECDHEPSK},
  199. {0, SSL_TXT_kDHEPSK, NULL, 0, SSL_kDHEPSK},
  200. {0, SSL_TXT_kSRP, NULL, 0, SSL_kSRP},
  201. {0, SSL_TXT_kGOST, NULL, 0, SSL_kGOST},
  202. /* server authentication aliases */
  203. {0, SSL_TXT_aRSA, NULL, 0, 0, SSL_aRSA},
  204. {0, SSL_TXT_aDSS, NULL, 0, 0, SSL_aDSS},
  205. {0, SSL_TXT_DSS, NULL, 0, 0, SSL_aDSS},
  206. {0, SSL_TXT_aNULL, NULL, 0, 0, SSL_aNULL},
  207. {0, SSL_TXT_aECDSA, NULL, 0, 0, SSL_aECDSA},
  208. {0, SSL_TXT_ECDSA, NULL, 0, 0, SSL_aECDSA},
  209. {0, SSL_TXT_aPSK, NULL, 0, 0, SSL_aPSK},
  210. {0, SSL_TXT_aGOST01, NULL, 0, 0, SSL_aGOST01},
  211. {0, SSL_TXT_aGOST12, NULL, 0, 0, SSL_aGOST12},
  212. {0, SSL_TXT_aGOST, NULL, 0, 0, SSL_aGOST01 | SSL_aGOST12},
  213. {0, SSL_TXT_aSRP, NULL, 0, 0, SSL_aSRP},
  214. /* aliases combining key exchange and server authentication */
  215. {0, SSL_TXT_EDH, NULL, 0, SSL_kDHE, ~SSL_aNULL},
  216. {0, SSL_TXT_DHE, NULL, 0, SSL_kDHE, ~SSL_aNULL},
  217. {0, SSL_TXT_EECDH, NULL, 0, SSL_kECDHE, ~SSL_aNULL},
  218. {0, SSL_TXT_ECDHE, NULL, 0, SSL_kECDHE, ~SSL_aNULL},
  219. {0, SSL_TXT_NULL, NULL, 0, 0, 0, SSL_eNULL},
  220. {0, SSL_TXT_RSA, NULL, 0, SSL_kRSA, SSL_aRSA},
  221. {0, SSL_TXT_ADH, NULL, 0, SSL_kDHE, SSL_aNULL},
  222. {0, SSL_TXT_AECDH, NULL, 0, SSL_kECDHE, SSL_aNULL},
  223. {0, SSL_TXT_PSK, NULL, 0, SSL_PSK},
  224. {0, SSL_TXT_SRP, NULL, 0, SSL_kSRP},
  225. /* symmetric encryption aliases */
  226. {0, SSL_TXT_3DES, NULL, 0, 0, 0, SSL_3DES},
  227. {0, SSL_TXT_RC4, NULL, 0, 0, 0, SSL_RC4},
  228. {0, SSL_TXT_RC2, NULL, 0, 0, 0, SSL_RC2},
  229. {0, SSL_TXT_IDEA, NULL, 0, 0, 0, SSL_IDEA},
  230. {0, SSL_TXT_SEED, NULL, 0, 0, 0, SSL_SEED},
  231. {0, SSL_TXT_eNULL, NULL, 0, 0, 0, SSL_eNULL},
  232. {0, SSL_TXT_GOST, NULL, 0, 0, 0, SSL_eGOST2814789CNT | SSL_eGOST2814789CNT12},
  233. {0, SSL_TXT_AES128, NULL, 0, 0, 0,
  234. SSL_AES128 | SSL_AES128GCM | SSL_AES128CCM | SSL_AES128CCM8},
  235. {0, SSL_TXT_AES256, NULL, 0, 0, 0,
  236. SSL_AES256 | SSL_AES256GCM | SSL_AES256CCM | SSL_AES256CCM8},
  237. {0, SSL_TXT_AES, NULL, 0, 0, 0, SSL_AES},
  238. {0, SSL_TXT_AES_GCM, NULL, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM},
  239. {0, SSL_TXT_AES_CCM, NULL, 0, 0, 0,
  240. SSL_AES128CCM | SSL_AES256CCM | SSL_AES128CCM8 | SSL_AES256CCM8},
  241. {0, SSL_TXT_AES_CCM_8, NULL, 0, 0, 0, SSL_AES128CCM8 | SSL_AES256CCM8},
  242. {0, SSL_TXT_CAMELLIA128, NULL, 0, 0, 0, SSL_CAMELLIA128},
  243. {0, SSL_TXT_CAMELLIA256, NULL, 0, 0, 0, SSL_CAMELLIA256},
  244. {0, SSL_TXT_CAMELLIA, NULL, 0, 0, 0, SSL_CAMELLIA},
  245. {0, SSL_TXT_CHACHA20, NULL, 0, 0, 0, SSL_CHACHA20},
  246. {0, SSL_TXT_ARIA, NULL, 0, 0, 0, SSL_ARIA},
  247. {0, SSL_TXT_ARIA_GCM, NULL, 0, 0, 0, SSL_ARIA128GCM | SSL_ARIA256GCM},
  248. {0, SSL_TXT_ARIA128, NULL, 0, 0, 0, SSL_ARIA128GCM},
  249. {0, SSL_TXT_ARIA256, NULL, 0, 0, 0, SSL_ARIA256GCM},
  250. /* MAC aliases */
  251. {0, SSL_TXT_MD5, NULL, 0, 0, 0, 0, SSL_MD5},
  252. {0, SSL_TXT_SHA1, NULL, 0, 0, 0, 0, SSL_SHA1},
  253. {0, SSL_TXT_SHA, NULL, 0, 0, 0, 0, SSL_SHA1},
  254. {0, SSL_TXT_GOST94, NULL, 0, 0, 0, 0, SSL_GOST94},
  255. {0, SSL_TXT_GOST89MAC, NULL, 0, 0, 0, 0, SSL_GOST89MAC | SSL_GOST89MAC12},
  256. {0, SSL_TXT_SHA256, NULL, 0, 0, 0, 0, SSL_SHA256},
  257. {0, SSL_TXT_SHA384, NULL, 0, 0, 0, 0, SSL_SHA384},
  258. {0, SSL_TXT_GOST12, NULL, 0, 0, 0, 0, SSL_GOST12_256},
  259. /* protocol version aliases */
  260. {0, SSL_TXT_SSLV3, NULL, 0, 0, 0, 0, 0, SSL3_VERSION},
  261. {0, SSL_TXT_TLSV1, NULL, 0, 0, 0, 0, 0, TLS1_VERSION},
  262. {0, "TLSv1.0", NULL, 0, 0, 0, 0, 0, TLS1_VERSION},
  263. {0, SSL_TXT_TLSV1_2, NULL, 0, 0, 0, 0, 0, TLS1_2_VERSION},
  264. /* strength classes */
  265. {0, SSL_TXT_LOW, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_LOW},
  266. {0, SSL_TXT_MEDIUM, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_MEDIUM},
  267. {0, SSL_TXT_HIGH, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_HIGH},
  268. /* FIPS 140-2 approved ciphersuite */
  269. {0, SSL_TXT_FIPS, NULL, 0, 0, 0, ~SSL_eNULL, 0, 0, 0, 0, 0, SSL_FIPS},
  270. /* "EDH-" aliases to "DHE-" labels (for backward compatibility) */
  271. {0, SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA, NULL, 0,
  272. SSL_kDHE, SSL_aDSS, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS},
  273. {0, SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA, NULL, 0,
  274. SSL_kDHE, SSL_aRSA, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS},
  275. };
  276. /*
  277. * Search for public key algorithm with given name and return its pkey_id if
  278. * it is available. Otherwise return 0
  279. */
  280. #ifdef OPENSSL_NO_ENGINE
  281. static int get_optional_pkey_id(const char *pkey_name)
  282. {
  283. const EVP_PKEY_ASN1_METHOD *ameth;
  284. int pkey_id = 0;
  285. ameth = EVP_PKEY_asn1_find_str(NULL, pkey_name, -1);
  286. if (ameth && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
  287. ameth) > 0)
  288. return pkey_id;
  289. return 0;
  290. }
  291. #else
  292. static int get_optional_pkey_id(const char *pkey_name)
  293. {
  294. const EVP_PKEY_ASN1_METHOD *ameth;
  295. ENGINE *tmpeng = NULL;
  296. int pkey_id = 0;
  297. ameth = EVP_PKEY_asn1_find_str(&tmpeng, pkey_name, -1);
  298. if (ameth) {
  299. if (EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
  300. ameth) <= 0)
  301. pkey_id = 0;
  302. }
  303. ENGINE_finish(tmpeng);
  304. return pkey_id;
  305. }
  306. #endif
  307. /* masks of disabled algorithms */
  308. static uint32_t disabled_enc_mask;
  309. static uint32_t disabled_mac_mask;
  310. static uint32_t disabled_mkey_mask;
  311. static uint32_t disabled_auth_mask;
  312. int ssl_load_ciphers(void)
  313. {
  314. size_t i;
  315. const ssl_cipher_table *t;
  316. disabled_enc_mask = 0;
  317. ssl_sort_cipher_list();
  318. for (i = 0, t = ssl_cipher_table_cipher; i < SSL_ENC_NUM_IDX; i++, t++) {
  319. if (t->nid == NID_undef) {
  320. ssl_cipher_methods[i] = NULL;
  321. } else {
  322. const EVP_CIPHER *cipher = EVP_get_cipherbynid(t->nid);
  323. ssl_cipher_methods[i] = cipher;
  324. if (cipher == NULL)
  325. disabled_enc_mask |= t->mask;
  326. }
  327. }
  328. disabled_mac_mask = 0;
  329. for (i = 0, t = ssl_cipher_table_mac; i < SSL_MD_NUM_IDX; i++, t++) {
  330. const EVP_MD *md = EVP_get_digestbynid(t->nid);
  331. ssl_digest_methods[i] = md;
  332. if (md == NULL) {
  333. disabled_mac_mask |= t->mask;
  334. } else {
  335. int tmpsize = EVP_MD_size(md);
  336. if (!ossl_assert(tmpsize >= 0))
  337. return 0;
  338. ssl_mac_secret_size[i] = tmpsize;
  339. }
  340. }
  341. /* Make sure we can access MD5 and SHA1 */
  342. if (!ossl_assert(ssl_digest_methods[SSL_MD_MD5_IDX] != NULL))
  343. return 0;
  344. if (!ossl_assert(ssl_digest_methods[SSL_MD_SHA1_IDX] != NULL))
  345. return 0;
  346. disabled_mkey_mask = 0;
  347. disabled_auth_mask = 0;
  348. #ifdef OPENSSL_NO_RSA
  349. disabled_mkey_mask |= SSL_kRSA | SSL_kRSAPSK;
  350. disabled_auth_mask |= SSL_aRSA;
  351. #endif
  352. #ifdef OPENSSL_NO_DSA
  353. disabled_auth_mask |= SSL_aDSS;
  354. #endif
  355. #ifdef OPENSSL_NO_DH
  356. disabled_mkey_mask |= SSL_kDHE | SSL_kDHEPSK;
  357. #endif
  358. #ifdef OPENSSL_NO_EC
  359. disabled_mkey_mask |= SSL_kECDHE | SSL_kECDHEPSK;
  360. disabled_auth_mask |= SSL_aECDSA;
  361. #endif
  362. #ifdef OPENSSL_NO_PSK
  363. disabled_mkey_mask |= SSL_PSK;
  364. disabled_auth_mask |= SSL_aPSK;
  365. #endif
  366. #ifdef OPENSSL_NO_SRP
  367. disabled_mkey_mask |= SSL_kSRP;
  368. #endif
  369. /*
  370. * Check for presence of GOST 34.10 algorithms, and if they are not
  371. * present, disable appropriate auth and key exchange
  372. */
  373. ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id("gost-mac");
  374. if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
  375. ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
  376. else
  377. disabled_mac_mask |= SSL_GOST89MAC;
  378. ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] =
  379. get_optional_pkey_id("gost-mac-12");
  380. if (ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
  381. ssl_mac_secret_size[SSL_MD_GOST89MAC12_IDX] = 32;
  382. else
  383. disabled_mac_mask |= SSL_GOST89MAC12;
  384. if (!get_optional_pkey_id("gost2001"))
  385. disabled_auth_mask |= SSL_aGOST01 | SSL_aGOST12;
  386. if (!get_optional_pkey_id("gost2012_256"))
  387. disabled_auth_mask |= SSL_aGOST12;
  388. if (!get_optional_pkey_id("gost2012_512"))
  389. disabled_auth_mask |= SSL_aGOST12;
  390. /*
  391. * Disable GOST key exchange if no GOST signature algs are available *
  392. */
  393. if ((disabled_auth_mask & (SSL_aGOST01 | SSL_aGOST12)) ==
  394. (SSL_aGOST01 | SSL_aGOST12))
  395. disabled_mkey_mask |= SSL_kGOST;
  396. return 1;
  397. }
  398. #ifndef OPENSSL_NO_COMP
  399. static int sk_comp_cmp(const SSL_COMP *const *a, const SSL_COMP *const *b)
  400. {
  401. return ((*a)->id - (*b)->id);
  402. }
  403. DEFINE_RUN_ONCE_STATIC(do_load_builtin_compressions)
  404. {
  405. SSL_COMP *comp = NULL;
  406. COMP_METHOD *method = COMP_zlib();
  407. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  408. ssl_comp_methods = sk_SSL_COMP_new(sk_comp_cmp);
  409. if (COMP_get_type(method) != NID_undef && ssl_comp_methods != NULL) {
  410. comp = OPENSSL_malloc(sizeof(*comp));
  411. if (comp != NULL) {
  412. comp->method = method;
  413. comp->id = SSL_COMP_ZLIB_IDX;
  414. comp->name = COMP_get_name(method);
  415. sk_SSL_COMP_push(ssl_comp_methods, comp);
  416. sk_SSL_COMP_sort(ssl_comp_methods);
  417. }
  418. }
  419. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  420. return 1;
  421. }
  422. static int load_builtin_compressions(void)
  423. {
  424. return RUN_ONCE(&ssl_load_builtin_comp_once, do_load_builtin_compressions);
  425. }
  426. #endif
  427. int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
  428. const EVP_MD **md, int *mac_pkey_type,
  429. size_t *mac_secret_size, SSL_COMP **comp, int use_etm)
  430. {
  431. int i;
  432. const SSL_CIPHER *c;
  433. c = s->cipher;
  434. if (c == NULL)
  435. return 0;
  436. if (comp != NULL) {
  437. SSL_COMP ctmp;
  438. #ifndef OPENSSL_NO_COMP
  439. if (!load_builtin_compressions()) {
  440. /*
  441. * Currently don't care, since a failure only means that
  442. * ssl_comp_methods is NULL, which is perfectly OK
  443. */
  444. }
  445. #endif
  446. *comp = NULL;
  447. ctmp.id = s->compress_meth;
  448. if (ssl_comp_methods != NULL) {
  449. i = sk_SSL_COMP_find(ssl_comp_methods, &ctmp);
  450. *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
  451. }
  452. /* If were only interested in comp then return success */
  453. if ((enc == NULL) && (md == NULL))
  454. return 1;
  455. }
  456. if ((enc == NULL) || (md == NULL))
  457. return 0;
  458. i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
  459. if (i == -1) {
  460. *enc = NULL;
  461. } else {
  462. if (i == SSL_ENC_NULL_IDX)
  463. *enc = EVP_enc_null();
  464. else
  465. *enc = ssl_cipher_methods[i];
  466. }
  467. i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
  468. if (i == -1) {
  469. *md = NULL;
  470. if (mac_pkey_type != NULL)
  471. *mac_pkey_type = NID_undef;
  472. if (mac_secret_size != NULL)
  473. *mac_secret_size = 0;
  474. if (c->algorithm_mac == SSL_AEAD)
  475. mac_pkey_type = NULL;
  476. } else {
  477. *md = ssl_digest_methods[i];
  478. if (mac_pkey_type != NULL)
  479. *mac_pkey_type = ssl_mac_pkey_id[i];
  480. if (mac_secret_size != NULL)
  481. *mac_secret_size = ssl_mac_secret_size[i];
  482. }
  483. if ((*enc != NULL) &&
  484. (*md != NULL || (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
  485. && (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
  486. const EVP_CIPHER *evp;
  487. if (use_etm)
  488. return 1;
  489. if (s->ssl_version >> 8 != TLS1_VERSION_MAJOR ||
  490. s->ssl_version < TLS1_VERSION)
  491. return 1;
  492. if (c->algorithm_enc == SSL_RC4 &&
  493. c->algorithm_mac == SSL_MD5 &&
  494. (evp = EVP_get_cipherbyname("RC4-HMAC-MD5")))
  495. *enc = evp, *md = NULL;
  496. else if (c->algorithm_enc == SSL_AES128 &&
  497. c->algorithm_mac == SSL_SHA1 &&
  498. (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
  499. *enc = evp, *md = NULL;
  500. else if (c->algorithm_enc == SSL_AES256 &&
  501. c->algorithm_mac == SSL_SHA1 &&
  502. (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
  503. *enc = evp, *md = NULL;
  504. else if (c->algorithm_enc == SSL_AES128 &&
  505. c->algorithm_mac == SSL_SHA256 &&
  506. (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA256")))
  507. *enc = evp, *md = NULL;
  508. else if (c->algorithm_enc == SSL_AES256 &&
  509. c->algorithm_mac == SSL_SHA256 &&
  510. (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA256")))
  511. *enc = evp, *md = NULL;
  512. return 1;
  513. } else {
  514. return 0;
  515. }
  516. }
  517. const EVP_MD *ssl_md(int idx)
  518. {
  519. idx &= SSL_HANDSHAKE_MAC_MASK;
  520. if (idx < 0 || idx >= SSL_MD_NUM_IDX)
  521. return NULL;
  522. return ssl_digest_methods[idx];
  523. }
  524. const EVP_MD *ssl_handshake_md(SSL *s)
  525. {
  526. return ssl_md(ssl_get_algorithm2(s));
  527. }
  528. const EVP_MD *ssl_prf_md(SSL *s)
  529. {
  530. return ssl_md(ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
  531. }
  532. #define ITEM_SEP(a) \
  533. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  534. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  535. CIPHER_ORDER **tail)
  536. {
  537. if (curr == *tail)
  538. return;
  539. if (curr == *head)
  540. *head = curr->next;
  541. if (curr->prev != NULL)
  542. curr->prev->next = curr->next;
  543. if (curr->next != NULL)
  544. curr->next->prev = curr->prev;
  545. (*tail)->next = curr;
  546. curr->prev = *tail;
  547. curr->next = NULL;
  548. *tail = curr;
  549. }
  550. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  551. CIPHER_ORDER **tail)
  552. {
  553. if (curr == *head)
  554. return;
  555. if (curr == *tail)
  556. *tail = curr->prev;
  557. if (curr->next != NULL)
  558. curr->next->prev = curr->prev;
  559. if (curr->prev != NULL)
  560. curr->prev->next = curr->next;
  561. (*head)->prev = curr;
  562. curr->next = *head;
  563. curr->prev = NULL;
  564. *head = curr;
  565. }
  566. static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
  567. int num_of_ciphers,
  568. uint32_t disabled_mkey,
  569. uint32_t disabled_auth,
  570. uint32_t disabled_enc,
  571. uint32_t disabled_mac,
  572. CIPHER_ORDER *co_list,
  573. CIPHER_ORDER **head_p,
  574. CIPHER_ORDER **tail_p)
  575. {
  576. int i, co_list_num;
  577. const SSL_CIPHER *c;
  578. /*
  579. * We have num_of_ciphers descriptions compiled in, depending on the
  580. * method selected (SSLv3, TLSv1 etc).
  581. * These will later be sorted in a linked list with at most num
  582. * entries.
  583. */
  584. /* Get the initial list of ciphers */
  585. co_list_num = 0; /* actual count of ciphers */
  586. for (i = 0; i < num_of_ciphers; i++) {
  587. c = ssl_method->get_cipher(i);
  588. /* drop those that use any of that is not available */
  589. if (c == NULL || !c->valid)
  590. continue;
  591. if ((c->algorithm_mkey & disabled_mkey) ||
  592. (c->algorithm_auth & disabled_auth) ||
  593. (c->algorithm_enc & disabled_enc) ||
  594. (c->algorithm_mac & disabled_mac))
  595. continue;
  596. if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) == 0) &&
  597. c->min_tls == 0)
  598. continue;
  599. if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) != 0) &&
  600. c->min_dtls == 0)
  601. continue;
  602. co_list[co_list_num].cipher = c;
  603. co_list[co_list_num].next = NULL;
  604. co_list[co_list_num].prev = NULL;
  605. co_list[co_list_num].active = 0;
  606. co_list_num++;
  607. }
  608. /*
  609. * Prepare linked list from list entries
  610. */
  611. if (co_list_num > 0) {
  612. co_list[0].prev = NULL;
  613. if (co_list_num > 1) {
  614. co_list[0].next = &co_list[1];
  615. for (i = 1; i < co_list_num - 1; i++) {
  616. co_list[i].prev = &co_list[i - 1];
  617. co_list[i].next = &co_list[i + 1];
  618. }
  619. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  620. }
  621. co_list[co_list_num - 1].next = NULL;
  622. *head_p = &co_list[0];
  623. *tail_p = &co_list[co_list_num - 1];
  624. }
  625. }
  626. static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
  627. int num_of_group_aliases,
  628. uint32_t disabled_mkey,
  629. uint32_t disabled_auth,
  630. uint32_t disabled_enc,
  631. uint32_t disabled_mac,
  632. CIPHER_ORDER *head)
  633. {
  634. CIPHER_ORDER *ciph_curr;
  635. const SSL_CIPHER **ca_curr;
  636. int i;
  637. uint32_t mask_mkey = ~disabled_mkey;
  638. uint32_t mask_auth = ~disabled_auth;
  639. uint32_t mask_enc = ~disabled_enc;
  640. uint32_t mask_mac = ~disabled_mac;
  641. /*
  642. * First, add the real ciphers as already collected
  643. */
  644. ciph_curr = head;
  645. ca_curr = ca_list;
  646. while (ciph_curr != NULL) {
  647. *ca_curr = ciph_curr->cipher;
  648. ca_curr++;
  649. ciph_curr = ciph_curr->next;
  650. }
  651. /*
  652. * Now we add the available ones from the cipher_aliases[] table.
  653. * They represent either one or more algorithms, some of which
  654. * in any affected category must be supported (set in enabled_mask),
  655. * or represent a cipher strength value (will be added in any case because algorithms=0).
  656. */
  657. for (i = 0; i < num_of_group_aliases; i++) {
  658. uint32_t algorithm_mkey = cipher_aliases[i].algorithm_mkey;
  659. uint32_t algorithm_auth = cipher_aliases[i].algorithm_auth;
  660. uint32_t algorithm_enc = cipher_aliases[i].algorithm_enc;
  661. uint32_t algorithm_mac = cipher_aliases[i].algorithm_mac;
  662. if (algorithm_mkey)
  663. if ((algorithm_mkey & mask_mkey) == 0)
  664. continue;
  665. if (algorithm_auth)
  666. if ((algorithm_auth & mask_auth) == 0)
  667. continue;
  668. if (algorithm_enc)
  669. if ((algorithm_enc & mask_enc) == 0)
  670. continue;
  671. if (algorithm_mac)
  672. if ((algorithm_mac & mask_mac) == 0)
  673. continue;
  674. *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
  675. ca_curr++;
  676. }
  677. *ca_curr = NULL; /* end of list */
  678. }
  679. static void ssl_cipher_apply_rule(uint32_t cipher_id, uint32_t alg_mkey,
  680. uint32_t alg_auth, uint32_t alg_enc,
  681. uint32_t alg_mac, int min_tls,
  682. uint32_t algo_strength, int rule,
  683. int32_t strength_bits, CIPHER_ORDER **head_p,
  684. CIPHER_ORDER **tail_p)
  685. {
  686. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  687. const SSL_CIPHER *cp;
  688. int reverse = 0;
  689. #ifdef CIPHER_DEBUG
  690. fprintf(stderr,
  691. "Applying rule %d with %08x/%08x/%08x/%08x/%08x %08x (%d)\n",
  692. rule, alg_mkey, alg_auth, alg_enc, alg_mac, min_tls,
  693. algo_strength, strength_bits);
  694. #endif
  695. if (rule == CIPHER_DEL || rule == CIPHER_BUMP)
  696. reverse = 1; /* needed to maintain sorting between currently
  697. * deleted ciphers */
  698. head = *head_p;
  699. tail = *tail_p;
  700. if (reverse) {
  701. next = tail;
  702. last = head;
  703. } else {
  704. next = head;
  705. last = tail;
  706. }
  707. curr = NULL;
  708. for (;;) {
  709. if (curr == last)
  710. break;
  711. curr = next;
  712. if (curr == NULL)
  713. break;
  714. next = reverse ? curr->prev : curr->next;
  715. cp = curr->cipher;
  716. /*
  717. * Selection criteria is either the value of strength_bits
  718. * or the algorithms used.
  719. */
  720. if (strength_bits >= 0) {
  721. if (strength_bits != cp->strength_bits)
  722. continue;
  723. } else {
  724. #ifdef CIPHER_DEBUG
  725. fprintf(stderr,
  726. "\nName: %s:\nAlgo = %08x/%08x/%08x/%08x/%08x Algo_strength = %08x\n",
  727. cp->name, cp->algorithm_mkey, cp->algorithm_auth,
  728. cp->algorithm_enc, cp->algorithm_mac, cp->min_tls,
  729. cp->algo_strength);
  730. #endif
  731. if (cipher_id != 0 && (cipher_id != cp->id))
  732. continue;
  733. if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
  734. continue;
  735. if (alg_auth && !(alg_auth & cp->algorithm_auth))
  736. continue;
  737. if (alg_enc && !(alg_enc & cp->algorithm_enc))
  738. continue;
  739. if (alg_mac && !(alg_mac & cp->algorithm_mac))
  740. continue;
  741. if (min_tls && (min_tls != cp->min_tls))
  742. continue;
  743. if ((algo_strength & SSL_STRONG_MASK)
  744. && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
  745. continue;
  746. if ((algo_strength & SSL_DEFAULT_MASK)
  747. && !(algo_strength & SSL_DEFAULT_MASK & cp->algo_strength))
  748. continue;
  749. }
  750. #ifdef CIPHER_DEBUG
  751. fprintf(stderr, "Action = %d\n", rule);
  752. #endif
  753. /* add the cipher if it has not been added yet. */
  754. if (rule == CIPHER_ADD) {
  755. /* reverse == 0 */
  756. if (!curr->active) {
  757. ll_append_tail(&head, curr, &tail);
  758. curr->active = 1;
  759. }
  760. }
  761. /* Move the added cipher to this location */
  762. else if (rule == CIPHER_ORD) {
  763. /* reverse == 0 */
  764. if (curr->active) {
  765. ll_append_tail(&head, curr, &tail);
  766. }
  767. } else if (rule == CIPHER_DEL) {
  768. /* reverse == 1 */
  769. if (curr->active) {
  770. /*
  771. * most recently deleted ciphersuites get best positions for
  772. * any future CIPHER_ADD (note that the CIPHER_DEL loop works
  773. * in reverse to maintain the order)
  774. */
  775. ll_append_head(&head, curr, &tail);
  776. curr->active = 0;
  777. }
  778. } else if (rule == CIPHER_BUMP) {
  779. if (curr->active)
  780. ll_append_head(&head, curr, &tail);
  781. } else if (rule == CIPHER_KILL) {
  782. /* reverse == 0 */
  783. if (head == curr)
  784. head = curr->next;
  785. else
  786. curr->prev->next = curr->next;
  787. if (tail == curr)
  788. tail = curr->prev;
  789. curr->active = 0;
  790. if (curr->next != NULL)
  791. curr->next->prev = curr->prev;
  792. if (curr->prev != NULL)
  793. curr->prev->next = curr->next;
  794. curr->next = NULL;
  795. curr->prev = NULL;
  796. }
  797. }
  798. *head_p = head;
  799. *tail_p = tail;
  800. }
  801. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  802. CIPHER_ORDER **tail_p)
  803. {
  804. int32_t max_strength_bits;
  805. int i, *number_uses;
  806. CIPHER_ORDER *curr;
  807. /*
  808. * This routine sorts the ciphers with descending strength. The sorting
  809. * must keep the pre-sorted sequence, so we apply the normal sorting
  810. * routine as '+' movement to the end of the list.
  811. */
  812. max_strength_bits = 0;
  813. curr = *head_p;
  814. while (curr != NULL) {
  815. if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
  816. max_strength_bits = curr->cipher->strength_bits;
  817. curr = curr->next;
  818. }
  819. number_uses = OPENSSL_zalloc(sizeof(int) * (max_strength_bits + 1));
  820. if (number_uses == NULL) {
  821. SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
  822. return 0;
  823. }
  824. /*
  825. * Now find the strength_bits values actually used
  826. */
  827. curr = *head_p;
  828. while (curr != NULL) {
  829. if (curr->active)
  830. number_uses[curr->cipher->strength_bits]++;
  831. curr = curr->next;
  832. }
  833. /*
  834. * Go through the list of used strength_bits values in descending
  835. * order.
  836. */
  837. for (i = max_strength_bits; i >= 0; i--)
  838. if (number_uses[i] > 0)
  839. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p,
  840. tail_p);
  841. OPENSSL_free(number_uses);
  842. return 1;
  843. }
  844. static int ssl_cipher_process_rulestr(const char *rule_str,
  845. CIPHER_ORDER **head_p,
  846. CIPHER_ORDER **tail_p,
  847. const SSL_CIPHER **ca_list, CERT *c)
  848. {
  849. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, algo_strength;
  850. int min_tls;
  851. const char *l, *buf;
  852. int j, multi, found, rule, retval, ok, buflen;
  853. uint32_t cipher_id = 0;
  854. char ch;
  855. retval = 1;
  856. l = rule_str;
  857. for ( ; ; ) {
  858. ch = *l;
  859. if (ch == '\0')
  860. break; /* done */
  861. if (ch == '-') {
  862. rule = CIPHER_DEL;
  863. l++;
  864. } else if (ch == '+') {
  865. rule = CIPHER_ORD;
  866. l++;
  867. } else if (ch == '!') {
  868. rule = CIPHER_KILL;
  869. l++;
  870. } else if (ch == '@') {
  871. rule = CIPHER_SPECIAL;
  872. l++;
  873. } else {
  874. rule = CIPHER_ADD;
  875. }
  876. if (ITEM_SEP(ch)) {
  877. l++;
  878. continue;
  879. }
  880. alg_mkey = 0;
  881. alg_auth = 0;
  882. alg_enc = 0;
  883. alg_mac = 0;
  884. min_tls = 0;
  885. algo_strength = 0;
  886. for (;;) {
  887. ch = *l;
  888. buf = l;
  889. buflen = 0;
  890. #ifndef CHARSET_EBCDIC
  891. while (((ch >= 'A') && (ch <= 'Z')) ||
  892. ((ch >= '0') && (ch <= '9')) ||
  893. ((ch >= 'a') && (ch <= 'z')) ||
  894. (ch == '-') || (ch == '.') || (ch == '='))
  895. #else
  896. while (isalnum((unsigned char)ch) || (ch == '-') || (ch == '.')
  897. || (ch == '='))
  898. #endif
  899. {
  900. ch = *(++l);
  901. buflen++;
  902. }
  903. if (buflen == 0) {
  904. /*
  905. * We hit something we cannot deal with,
  906. * it is no command or separator nor
  907. * alphanumeric, so we call this an error.
  908. */
  909. SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, SSL_R_INVALID_COMMAND);
  910. retval = found = 0;
  911. l++;
  912. break;
  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. /* We already have a cipher_list, so we need to update it */
  1219. return update_cipher_list(&ctx->cipher_list, &ctx->cipher_list_by_id,
  1220. ctx->tls13_ciphersuites);
  1221. }
  1222. return ret;
  1223. }
  1224. int SSL_set_ciphersuites(SSL *s, const char *str)
  1225. {
  1226. int ret = set_ciphersuites(&(s->tls13_ciphersuites), str);
  1227. if (ret && s->cipher_list != NULL) {
  1228. /* We already have a cipher_list, so we need to update it */
  1229. return update_cipher_list(&s->cipher_list, &s->cipher_list_by_id,
  1230. s->tls13_ciphersuites);
  1231. }
  1232. return ret;
  1233. }
  1234. STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
  1235. STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
  1236. STACK_OF(SSL_CIPHER) **cipher_list,
  1237. STACK_OF(SSL_CIPHER) **cipher_list_by_id,
  1238. const char *rule_str,
  1239. CERT *c)
  1240. {
  1241. int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases, i;
  1242. uint32_t disabled_mkey, disabled_auth, disabled_enc, disabled_mac;
  1243. STACK_OF(SSL_CIPHER) *cipherstack;
  1244. const char *rule_p;
  1245. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1246. const SSL_CIPHER **ca_list = NULL;
  1247. /*
  1248. * Return with error if nothing to do.
  1249. */
  1250. if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
  1251. return NULL;
  1252. #ifndef OPENSSL_NO_EC
  1253. if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
  1254. return NULL;
  1255. #endif
  1256. /*
  1257. * To reduce the work to do we only want to process the compiled
  1258. * in algorithms, so we first get the mask of disabled ciphers.
  1259. */
  1260. disabled_mkey = disabled_mkey_mask;
  1261. disabled_auth = disabled_auth_mask;
  1262. disabled_enc = disabled_enc_mask;
  1263. disabled_mac = disabled_mac_mask;
  1264. /*
  1265. * Now we have to collect the available ciphers from the compiled
  1266. * in ciphers. We cannot get more than the number compiled in, so
  1267. * it is used for allocation.
  1268. */
  1269. num_of_ciphers = ssl_method->num_ciphers();
  1270. co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
  1271. if (co_list == NULL) {
  1272. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  1273. return NULL; /* Failure */
  1274. }
  1275. ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
  1276. disabled_mkey, disabled_auth, disabled_enc,
  1277. disabled_mac, co_list, &head, &tail);
  1278. /* Now arrange all ciphers by preference. */
  1279. /*
  1280. * Everything else being equal, prefer ephemeral ECDH over other key
  1281. * exchange mechanisms.
  1282. * For consistency, prefer ECDSA over RSA (though this only matters if the
  1283. * server has both certificates, and is using the DEFAULT, or a client
  1284. * preference).
  1285. */
  1286. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD,
  1287. -1, &head, &tail);
  1288. ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head,
  1289. &tail);
  1290. ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
  1291. &tail);
  1292. /* Within each strength group, we prefer GCM over CHACHA... */
  1293. ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
  1294. &head, &tail);
  1295. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
  1296. &head, &tail);
  1297. /*
  1298. * ...and generally, our preferred cipher is AES.
  1299. * Note that AEADs will be bumped to take preference after sorting by
  1300. * strength.
  1301. */
  1302. ssl_cipher_apply_rule(0, 0, 0, SSL_AES ^ SSL_AESGCM, 0, 0, 0, CIPHER_ADD,
  1303. -1, &head, &tail);
  1304. /* Temporarily enable everything else for sorting */
  1305. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
  1306. /* Low priority for MD5 */
  1307. ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head,
  1308. &tail);
  1309. /*
  1310. * Move anonymous ciphers to the end. Usually, these will remain
  1311. * disabled. (For applications that allow them, they aren't too bad, but
  1312. * we prefer authenticated ciphers.)
  1313. */
  1314. ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
  1315. &tail);
  1316. ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
  1317. &tail);
  1318. ssl_cipher_apply_rule(0, SSL_kPSK, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
  1319. &tail);
  1320. /* RC4 is sort-of broken -- move to the end */
  1321. ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head,
  1322. &tail);
  1323. /*
  1324. * Now sort by symmetric encryption strength. The above ordering remains
  1325. * in force within each class
  1326. */
  1327. if (!ssl_cipher_strength_sort(&head, &tail)) {
  1328. OPENSSL_free(co_list);
  1329. return NULL;
  1330. }
  1331. /*
  1332. * Partially overrule strength sort to prefer TLS 1.2 ciphers/PRFs.
  1333. * TODO(openssl-team): is there an easier way to accomplish all this?
  1334. */
  1335. ssl_cipher_apply_rule(0, 0, 0, 0, 0, TLS1_2_VERSION, 0, CIPHER_BUMP, -1,
  1336. &head, &tail);
  1337. /*
  1338. * Irrespective of strength, enforce the following order:
  1339. * (EC)DHE + AEAD > (EC)DHE > rest of AEAD > rest.
  1340. * Within each group, ciphers remain sorted by strength and previous
  1341. * preference, i.e.,
  1342. * 1) ECDHE > DHE
  1343. * 2) GCM > CHACHA
  1344. * 3) AES > rest
  1345. * 4) TLS 1.2 > legacy
  1346. *
  1347. * Because we now bump ciphers to the top of the list, we proceed in
  1348. * reverse order of preference.
  1349. */
  1350. ssl_cipher_apply_rule(0, 0, 0, 0, SSL_AEAD, 0, 0, CIPHER_BUMP, -1,
  1351. &head, &tail);
  1352. ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, 0, 0, 0,
  1353. CIPHER_BUMP, -1, &head, &tail);
  1354. ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, SSL_AEAD, 0, 0,
  1355. CIPHER_BUMP, -1, &head, &tail);
  1356. /* Now disable everything (maintaining the ordering!) */
  1357. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
  1358. /*
  1359. * We also need cipher aliases for selecting based on the rule_str.
  1360. * There might be two types of entries in the rule_str: 1) names
  1361. * of ciphers themselves 2) aliases for groups of ciphers.
  1362. * For 1) we need the available ciphers and for 2) the cipher
  1363. * groups of cipher_aliases added together in one list (otherwise
  1364. * we would be happy with just the cipher_aliases table).
  1365. */
  1366. num_of_group_aliases = OSSL_NELEM(cipher_aliases);
  1367. num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
  1368. ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
  1369. if (ca_list == NULL) {
  1370. OPENSSL_free(co_list);
  1371. SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  1372. return NULL; /* Failure */
  1373. }
  1374. ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
  1375. disabled_mkey, disabled_auth, disabled_enc,
  1376. disabled_mac, head);
  1377. /*
  1378. * If the rule_string begins with DEFAULT, apply the default rule
  1379. * before using the (possibly available) additional rules.
  1380. */
  1381. ok = 1;
  1382. rule_p = rule_str;
  1383. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1384. ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
  1385. &head, &tail, ca_list, c);
  1386. rule_p += 7;
  1387. if (*rule_p == ':')
  1388. rule_p++;
  1389. }
  1390. if (ok && (strlen(rule_p) > 0))
  1391. ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, c);
  1392. OPENSSL_free(ca_list); /* Not needed anymore */
  1393. if (!ok) { /* Rule processing failure */
  1394. OPENSSL_free(co_list);
  1395. return NULL;
  1396. }
  1397. /*
  1398. * Allocate new "cipherstack" for the result, return with error
  1399. * if we cannot get one.
  1400. */
  1401. if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
  1402. OPENSSL_free(co_list);
  1403. return NULL;
  1404. }
  1405. /* Add TLSv1.3 ciphers first - we always prefer those if possible */
  1406. for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++) {
  1407. if (!sk_SSL_CIPHER_push(cipherstack,
  1408. sk_SSL_CIPHER_value(tls13_ciphersuites, i))) {
  1409. sk_SSL_CIPHER_free(cipherstack);
  1410. return NULL;
  1411. }
  1412. }
  1413. /*
  1414. * The cipher selection for the list is done. The ciphers are added
  1415. * to the resulting precedence to the STACK_OF(SSL_CIPHER).
  1416. */
  1417. for (curr = head; curr != NULL; curr = curr->next) {
  1418. if (curr->active) {
  1419. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1420. OPENSSL_free(co_list);
  1421. sk_SSL_CIPHER_free(cipherstack);
  1422. return NULL;
  1423. }
  1424. #ifdef CIPHER_DEBUG
  1425. fprintf(stderr, "<%s>\n", curr->cipher->name);
  1426. #endif
  1427. }
  1428. }
  1429. OPENSSL_free(co_list); /* Not needed any longer */
  1430. if (!update_cipher_list_by_id(cipher_list_by_id, cipherstack)) {
  1431. sk_SSL_CIPHER_free(cipherstack);
  1432. return NULL;
  1433. }
  1434. sk_SSL_CIPHER_free(*cipher_list);
  1435. *cipher_list = cipherstack;
  1436. return cipherstack;
  1437. }
  1438. char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
  1439. {
  1440. const char *ver;
  1441. const char *kx, *au, *enc, *mac;
  1442. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
  1443. static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
  1444. if (buf == NULL) {
  1445. len = 128;
  1446. if ((buf = OPENSSL_malloc(len)) == NULL) {
  1447. SSLerr(SSL_F_SSL_CIPHER_DESCRIPTION, ERR_R_MALLOC_FAILURE);
  1448. return NULL;
  1449. }
  1450. } else if (len < 128) {
  1451. return NULL;
  1452. }
  1453. alg_mkey = cipher->algorithm_mkey;
  1454. alg_auth = cipher->algorithm_auth;
  1455. alg_enc = cipher->algorithm_enc;
  1456. alg_mac = cipher->algorithm_mac;
  1457. ver = ssl_protocol_to_string(cipher->min_tls);
  1458. switch (alg_mkey) {
  1459. case SSL_kRSA:
  1460. kx = "RSA";
  1461. break;
  1462. case SSL_kDHE:
  1463. kx = "DH";
  1464. break;
  1465. case SSL_kECDHE:
  1466. kx = "ECDH";
  1467. break;
  1468. case SSL_kPSK:
  1469. kx = "PSK";
  1470. break;
  1471. case SSL_kRSAPSK:
  1472. kx = "RSAPSK";
  1473. break;
  1474. case SSL_kECDHEPSK:
  1475. kx = "ECDHEPSK";
  1476. break;
  1477. case SSL_kDHEPSK:
  1478. kx = "DHEPSK";
  1479. break;
  1480. case SSL_kSRP:
  1481. kx = "SRP";
  1482. break;
  1483. case SSL_kGOST:
  1484. kx = "GOST";
  1485. break;
  1486. case SSL_kANY:
  1487. kx = "any";
  1488. break;
  1489. default:
  1490. kx = "unknown";
  1491. }
  1492. switch (alg_auth) {
  1493. case SSL_aRSA:
  1494. au = "RSA";
  1495. break;
  1496. case SSL_aDSS:
  1497. au = "DSS";
  1498. break;
  1499. case SSL_aNULL:
  1500. au = "None";
  1501. break;
  1502. case SSL_aECDSA:
  1503. au = "ECDSA";
  1504. break;
  1505. case SSL_aPSK:
  1506. au = "PSK";
  1507. break;
  1508. case SSL_aSRP:
  1509. au = "SRP";
  1510. break;
  1511. case SSL_aGOST01:
  1512. au = "GOST01";
  1513. break;
  1514. /* New GOST ciphersuites have both SSL_aGOST12 and SSL_aGOST01 bits */
  1515. case (SSL_aGOST12 | SSL_aGOST01):
  1516. au = "GOST12";
  1517. break;
  1518. case SSL_aANY:
  1519. au = "any";
  1520. break;
  1521. default:
  1522. au = "unknown";
  1523. break;
  1524. }
  1525. switch (alg_enc) {
  1526. case SSL_DES:
  1527. enc = "DES(56)";
  1528. break;
  1529. case SSL_3DES:
  1530. enc = "3DES(168)";
  1531. break;
  1532. case SSL_RC4:
  1533. enc = "RC4(128)";
  1534. break;
  1535. case SSL_RC2:
  1536. enc = "RC2(128)";
  1537. break;
  1538. case SSL_IDEA:
  1539. enc = "IDEA(128)";
  1540. break;
  1541. case SSL_eNULL:
  1542. enc = "None";
  1543. break;
  1544. case SSL_AES128:
  1545. enc = "AES(128)";
  1546. break;
  1547. case SSL_AES256:
  1548. enc = "AES(256)";
  1549. break;
  1550. case SSL_AES128GCM:
  1551. enc = "AESGCM(128)";
  1552. break;
  1553. case SSL_AES256GCM:
  1554. enc = "AESGCM(256)";
  1555. break;
  1556. case SSL_AES128CCM:
  1557. enc = "AESCCM(128)";
  1558. break;
  1559. case SSL_AES256CCM:
  1560. enc = "AESCCM(256)";
  1561. break;
  1562. case SSL_AES128CCM8:
  1563. enc = "AESCCM8(128)";
  1564. break;
  1565. case SSL_AES256CCM8:
  1566. enc = "AESCCM8(256)";
  1567. break;
  1568. case SSL_CAMELLIA128:
  1569. enc = "Camellia(128)";
  1570. break;
  1571. case SSL_CAMELLIA256:
  1572. enc = "Camellia(256)";
  1573. break;
  1574. case SSL_ARIA128GCM:
  1575. enc = "ARIAGCM(128)";
  1576. break;
  1577. case SSL_ARIA256GCM:
  1578. enc = "ARIAGCM(256)";
  1579. break;
  1580. case SSL_SEED:
  1581. enc = "SEED(128)";
  1582. break;
  1583. case SSL_eGOST2814789CNT:
  1584. case SSL_eGOST2814789CNT12:
  1585. enc = "GOST89(256)";
  1586. break;
  1587. case SSL_CHACHA20POLY1305:
  1588. enc = "CHACHA20/POLY1305(256)";
  1589. break;
  1590. default:
  1591. enc = "unknown";
  1592. break;
  1593. }
  1594. switch (alg_mac) {
  1595. case SSL_MD5:
  1596. mac = "MD5";
  1597. break;
  1598. case SSL_SHA1:
  1599. mac = "SHA1";
  1600. break;
  1601. case SSL_SHA256:
  1602. mac = "SHA256";
  1603. break;
  1604. case SSL_SHA384:
  1605. mac = "SHA384";
  1606. break;
  1607. case SSL_AEAD:
  1608. mac = "AEAD";
  1609. break;
  1610. case SSL_GOST89MAC:
  1611. case SSL_GOST89MAC12:
  1612. mac = "GOST89";
  1613. break;
  1614. case SSL_GOST94:
  1615. mac = "GOST94";
  1616. break;
  1617. case SSL_GOST12_256:
  1618. case SSL_GOST12_512:
  1619. mac = "GOST2012";
  1620. break;
  1621. default:
  1622. mac = "unknown";
  1623. break;
  1624. }
  1625. BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
  1626. return buf;
  1627. }
  1628. const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
  1629. {
  1630. if (c == NULL)
  1631. return "(NONE)";
  1632. /*
  1633. * Backwards-compatibility crutch. In almost all contexts we report TLS
  1634. * 1.0 as "TLSv1", but for ciphers we report "TLSv1.0".
  1635. */
  1636. if (c->min_tls == TLS1_VERSION)
  1637. return "TLSv1.0";
  1638. return ssl_protocol_to_string(c->min_tls);
  1639. }
  1640. /* return the actual cipher being used */
  1641. const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
  1642. {
  1643. if (c != NULL)
  1644. return c->name;
  1645. return "(NONE)";
  1646. }
  1647. /* return the actual cipher being used in RFC standard name */
  1648. const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
  1649. {
  1650. if (c != NULL)
  1651. return c->stdname;
  1652. return "(NONE)";
  1653. }
  1654. /* return the OpenSSL name based on given RFC standard name */
  1655. const char *OPENSSL_cipher_name(const char *stdname)
  1656. {
  1657. const SSL_CIPHER *c;
  1658. if (stdname == NULL)
  1659. return "(NONE)";
  1660. c = ssl3_get_cipher_by_std_name(stdname);
  1661. return SSL_CIPHER_get_name(c);
  1662. }
  1663. /* number of bits for symmetric cipher */
  1664. int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
  1665. {
  1666. int ret = 0;
  1667. if (c != NULL) {
  1668. if (alg_bits != NULL)
  1669. *alg_bits = (int)c->alg_bits;
  1670. ret = (int)c->strength_bits;
  1671. }
  1672. return ret;
  1673. }
  1674. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c)
  1675. {
  1676. return c->id;
  1677. }
  1678. uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *c)
  1679. {
  1680. return c->id & 0xFFFF;
  1681. }
  1682. SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
  1683. {
  1684. SSL_COMP *ctmp;
  1685. int i, nn;
  1686. if ((n == 0) || (sk == NULL))
  1687. return NULL;
  1688. nn = sk_SSL_COMP_num(sk);
  1689. for (i = 0; i < nn; i++) {
  1690. ctmp = sk_SSL_COMP_value(sk, i);
  1691. if (ctmp->id == n)
  1692. return ctmp;
  1693. }
  1694. return NULL;
  1695. }
  1696. #ifdef OPENSSL_NO_COMP
  1697. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1698. {
  1699. return NULL;
  1700. }
  1701. STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
  1702. *meths)
  1703. {
  1704. return meths;
  1705. }
  1706. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1707. {
  1708. return 1;
  1709. }
  1710. #else
  1711. STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  1712. {
  1713. load_builtin_compressions();
  1714. return ssl_comp_methods;
  1715. }
  1716. STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
  1717. *meths)
  1718. {
  1719. STACK_OF(SSL_COMP) *old_meths = ssl_comp_methods;
  1720. ssl_comp_methods = meths;
  1721. return old_meths;
  1722. }
  1723. static void cmeth_free(SSL_COMP *cm)
  1724. {
  1725. OPENSSL_free(cm);
  1726. }
  1727. void ssl_comp_free_compression_methods_int(void)
  1728. {
  1729. STACK_OF(SSL_COMP) *old_meths = ssl_comp_methods;
  1730. ssl_comp_methods = NULL;
  1731. sk_SSL_COMP_pop_free(old_meths, cmeth_free);
  1732. }
  1733. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
  1734. {
  1735. SSL_COMP *comp;
  1736. if (cm == NULL || COMP_get_type(cm) == NID_undef)
  1737. return 1;
  1738. /*-
  1739. * According to draft-ietf-tls-compression-04.txt, the
  1740. * compression number ranges should be the following:
  1741. *
  1742. * 0 to 63: methods defined by the IETF
  1743. * 64 to 192: external party methods assigned by IANA
  1744. * 193 to 255: reserved for private use
  1745. */
  1746. if (id < 193 || id > 255) {
  1747. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
  1748. SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
  1749. return 1;
  1750. }
  1751. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  1752. comp = OPENSSL_malloc(sizeof(*comp));
  1753. if (comp == NULL) {
  1754. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1755. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
  1756. return 1;
  1757. }
  1758. comp->id = id;
  1759. comp->method = cm;
  1760. load_builtin_compressions();
  1761. if (ssl_comp_methods && sk_SSL_COMP_find(ssl_comp_methods, comp) >= 0) {
  1762. OPENSSL_free(comp);
  1763. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1764. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,
  1765. SSL_R_DUPLICATE_COMPRESSION_ID);
  1766. return 1;
  1767. }
  1768. if (ssl_comp_methods == NULL || !sk_SSL_COMP_push(ssl_comp_methods, comp)) {
  1769. OPENSSL_free(comp);
  1770. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1771. SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD, ERR_R_MALLOC_FAILURE);
  1772. return 1;
  1773. }
  1774. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  1775. return 0;
  1776. }
  1777. #endif
  1778. const char *SSL_COMP_get_name(const COMP_METHOD *comp)
  1779. {
  1780. #ifndef OPENSSL_NO_COMP
  1781. return comp ? COMP_get_name(comp) : NULL;
  1782. #else
  1783. return NULL;
  1784. #endif
  1785. }
  1786. const char *SSL_COMP_get0_name(const SSL_COMP *comp)
  1787. {
  1788. #ifndef OPENSSL_NO_COMP
  1789. return comp->name;
  1790. #else
  1791. return NULL;
  1792. #endif
  1793. }
  1794. int SSL_COMP_get_id(const SSL_COMP *comp)
  1795. {
  1796. #ifndef OPENSSL_NO_COMP
  1797. return comp->id;
  1798. #else
  1799. return -1;
  1800. #endif
  1801. }
  1802. const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr,
  1803. int all)
  1804. {
  1805. const SSL_CIPHER *c = ssl->method->get_cipher_by_char(ptr);
  1806. if (c == NULL || (!all && c->valid == 0))
  1807. return NULL;
  1808. return c;
  1809. }
  1810. const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
  1811. {
  1812. return ssl->method->get_cipher_by_char(ptr);
  1813. }
  1814. int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
  1815. {
  1816. int i;
  1817. if (c == NULL)
  1818. return NID_undef;
  1819. i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
  1820. if (i == -1)
  1821. return NID_undef;
  1822. return ssl_cipher_table_cipher[i].nid;
  1823. }
  1824. int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
  1825. {
  1826. int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
  1827. if (i == -1)
  1828. return NID_undef;
  1829. return ssl_cipher_table_mac[i].nid;
  1830. }
  1831. int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
  1832. {
  1833. int i = ssl_cipher_info_lookup(ssl_cipher_table_kx, c->algorithm_mkey);
  1834. if (i == -1)
  1835. return NID_undef;
  1836. return ssl_cipher_table_kx[i].nid;
  1837. }
  1838. int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
  1839. {
  1840. int i = ssl_cipher_info_lookup(ssl_cipher_table_auth, c->algorithm_auth);
  1841. if (i == -1)
  1842. return NID_undef;
  1843. return ssl_cipher_table_auth[i].nid;
  1844. }
  1845. const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c)
  1846. {
  1847. int idx = c->algorithm2 & SSL_HANDSHAKE_MAC_MASK;
  1848. if (idx < 0 || idx >= SSL_MD_NUM_IDX)
  1849. return NULL;
  1850. return ssl_digest_methods[idx];
  1851. }
  1852. int SSL_CIPHER_is_aead(const SSL_CIPHER *c)
  1853. {
  1854. return (c->algorithm_mac & SSL_AEAD) ? 1 : 0;
  1855. }
  1856. int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead,
  1857. size_t *int_overhead, size_t *blocksize,
  1858. size_t *ext_overhead)
  1859. {
  1860. size_t mac = 0, in = 0, blk = 0, out = 0;
  1861. /* Some hard-coded numbers for the CCM/Poly1305 MAC overhead
  1862. * because there are no handy #defines for those. */
  1863. if (c->algorithm_enc & (SSL_AESGCM | SSL_ARIAGCM)) {
  1864. out = EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  1865. } else if (c->algorithm_enc & (SSL_AES128CCM | SSL_AES256CCM)) {
  1866. out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 16;
  1867. } else if (c->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8)) {
  1868. out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 8;
  1869. } else if (c->algorithm_enc & SSL_CHACHA20POLY1305) {
  1870. out = 16;
  1871. } else if (c->algorithm_mac & SSL_AEAD) {
  1872. /* We're supposed to have handled all the AEAD modes above */
  1873. return 0;
  1874. } else {
  1875. /* Non-AEAD modes. Calculate MAC/cipher overhead separately */
  1876. int digest_nid = SSL_CIPHER_get_digest_nid(c);
  1877. const EVP_MD *e_md = EVP_get_digestbynid(digest_nid);
  1878. if (e_md == NULL)
  1879. return 0;
  1880. mac = EVP_MD_size(e_md);
  1881. if (c->algorithm_enc != SSL_eNULL) {
  1882. int cipher_nid = SSL_CIPHER_get_cipher_nid(c);
  1883. const EVP_CIPHER *e_ciph = EVP_get_cipherbynid(cipher_nid);
  1884. /* If it wasn't AEAD or SSL_eNULL, we expect it to be a
  1885. known CBC cipher. */
  1886. if (e_ciph == NULL ||
  1887. EVP_CIPHER_mode(e_ciph) != EVP_CIPH_CBC_MODE)
  1888. return 0;
  1889. in = 1; /* padding length byte */
  1890. out = EVP_CIPHER_iv_length(e_ciph);
  1891. blk = EVP_CIPHER_block_size(e_ciph);
  1892. }
  1893. }
  1894. *mac_overhead = mac;
  1895. *int_overhead = in;
  1896. *blocksize = blk;
  1897. *ext_overhead = out;
  1898. return 1;
  1899. }
  1900. int ssl_cert_is_disabled(size_t idx)
  1901. {
  1902. const SSL_CERT_LOOKUP *cl = ssl_cert_lookup_by_idx(idx);
  1903. if (cl == NULL || (cl->amask & disabled_auth_mask) != 0)
  1904. return 1;
  1905. return 0;
  1906. }