ssl_ciph.c 70 KB

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