ssl_ciph.c 70 KB

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