ssl_ciph.c 71 KB

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