t1_lib.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/hmac.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/ocsp.h>
  16. #include <openssl/conf.h>
  17. #include <openssl/x509v3.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/provider.h>
  21. #include <openssl/param_build.h>
  22. #include "internal/nelem.h"
  23. #include "internal/sizes.h"
  24. #include "internal/tlsgroups.h"
  25. #include "internal/cryptlib.h"
  26. #include "ssl_local.h"
  27. #include <openssl/ct.h>
  28. static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey);
  29. static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu);
  30. SSL3_ENC_METHOD const TLSv1_enc_data = {
  31. tls1_enc,
  32. tls1_mac,
  33. tls1_setup_key_block,
  34. tls1_generate_master_secret,
  35. tls1_change_cipher_state,
  36. tls1_final_finish_mac,
  37. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  38. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  39. tls1_alert_code,
  40. tls1_export_keying_material,
  41. 0,
  42. ssl3_set_handshake_header,
  43. tls_close_construct_packet,
  44. ssl3_handshake_write
  45. };
  46. SSL3_ENC_METHOD const TLSv1_1_enc_data = {
  47. tls1_enc,
  48. tls1_mac,
  49. tls1_setup_key_block,
  50. tls1_generate_master_secret,
  51. tls1_change_cipher_state,
  52. tls1_final_finish_mac,
  53. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  54. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  55. tls1_alert_code,
  56. tls1_export_keying_material,
  57. SSL_ENC_FLAG_EXPLICIT_IV,
  58. ssl3_set_handshake_header,
  59. tls_close_construct_packet,
  60. ssl3_handshake_write
  61. };
  62. SSL3_ENC_METHOD const TLSv1_2_enc_data = {
  63. tls1_enc,
  64. tls1_mac,
  65. tls1_setup_key_block,
  66. tls1_generate_master_secret,
  67. tls1_change_cipher_state,
  68. tls1_final_finish_mac,
  69. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  70. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  71. tls1_alert_code,
  72. tls1_export_keying_material,
  73. SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
  74. | SSL_ENC_FLAG_TLS1_2_CIPHERS,
  75. ssl3_set_handshake_header,
  76. tls_close_construct_packet,
  77. ssl3_handshake_write
  78. };
  79. SSL3_ENC_METHOD const TLSv1_3_enc_data = {
  80. tls13_enc,
  81. tls1_mac,
  82. tls13_setup_key_block,
  83. tls13_generate_master_secret,
  84. tls13_change_cipher_state,
  85. tls13_final_finish_mac,
  86. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  87. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  88. tls13_alert_code,
  89. tls13_export_keying_material,
  90. SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
  91. ssl3_set_handshake_header,
  92. tls_close_construct_packet,
  93. ssl3_handshake_write
  94. };
  95. long tls1_default_timeout(void)
  96. {
  97. /*
  98. * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
  99. * http, the cache would over fill
  100. */
  101. return (60 * 60 * 2);
  102. }
  103. int tls1_new(SSL *s)
  104. {
  105. if (!ssl3_new(s))
  106. return 0;
  107. if (!s->method->ssl_clear(s))
  108. return 0;
  109. return 1;
  110. }
  111. void tls1_free(SSL *s)
  112. {
  113. OPENSSL_free(s->ext.session_ticket);
  114. ssl3_free(s);
  115. }
  116. int tls1_clear(SSL *s)
  117. {
  118. if (!ssl3_clear(s))
  119. return 0;
  120. if (s->method->version == TLS_ANY_VERSION)
  121. s->version = TLS_MAX_VERSION_INTERNAL;
  122. else
  123. s->version = s->method->version;
  124. return 1;
  125. }
  126. /* Legacy NID to group_id mapping. Only works for groups we know about */
  127. static struct {
  128. int nid;
  129. uint16_t group_id;
  130. } nid_to_group[] = {
  131. {NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1},
  132. {NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1},
  133. {NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2},
  134. {NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1},
  135. {NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2},
  136. {NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1},
  137. {NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1},
  138. {NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1},
  139. {NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1},
  140. {NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1},
  141. {NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1},
  142. {NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1},
  143. {NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1},
  144. {NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1},
  145. {NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1},
  146. {NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1},
  147. {NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2},
  148. {NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1},
  149. {NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1},
  150. {NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1},
  151. {NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1},
  152. {NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1},
  153. {NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1},
  154. {NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1},
  155. {NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1},
  156. {NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1},
  157. {NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1},
  158. {NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1},
  159. {EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519},
  160. {EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448},
  161. {NID_id_tc26_gost_3410_2012_256_paramSetA, 0x0022},
  162. {NID_id_tc26_gost_3410_2012_256_paramSetB, 0x0023},
  163. {NID_id_tc26_gost_3410_2012_256_paramSetC, 0x0024},
  164. {NID_id_tc26_gost_3410_2012_256_paramSetD, 0x0025},
  165. {NID_id_tc26_gost_3410_2012_512_paramSetA, 0x0026},
  166. {NID_id_tc26_gost_3410_2012_512_paramSetB, 0x0027},
  167. {NID_id_tc26_gost_3410_2012_512_paramSetC, 0x0028},
  168. {NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048},
  169. {NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072},
  170. {NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096},
  171. {NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144},
  172. {NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192}
  173. };
  174. static const unsigned char ecformats_default[] = {
  175. TLSEXT_ECPOINTFORMAT_uncompressed,
  176. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
  177. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
  178. };
  179. /* The default curves */
  180. static const uint16_t supported_groups_default[] = {
  181. 29, /* X25519 (29) */
  182. 23, /* secp256r1 (23) */
  183. 30, /* X448 (30) */
  184. 25, /* secp521r1 (25) */
  185. 24, /* secp384r1 (24) */
  186. 34, /* GC256A (34) */
  187. 35, /* GC256B (35) */
  188. 36, /* GC256C (36) */
  189. 37, /* GC256D (37) */
  190. 38, /* GC512A (38) */
  191. 39, /* GC512B (39) */
  192. 40, /* GC512C (40) */
  193. 0x100, /* ffdhe2048 (0x100) */
  194. 0x101, /* ffdhe3072 (0x101) */
  195. 0x102, /* ffdhe4096 (0x102) */
  196. 0x103, /* ffdhe6144 (0x103) */
  197. 0x104, /* ffdhe8192 (0x104) */
  198. };
  199. static const uint16_t suiteb_curves[] = {
  200. TLSEXT_curve_P_256,
  201. TLSEXT_curve_P_384
  202. };
  203. struct provider_group_data_st {
  204. SSL_CTX *ctx;
  205. OSSL_PROVIDER *provider;
  206. };
  207. #define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10
  208. static OSSL_CALLBACK add_provider_groups;
  209. static int add_provider_groups(const OSSL_PARAM params[], void *data)
  210. {
  211. struct provider_group_data_st *pgd = data;
  212. SSL_CTX *ctx = pgd->ctx;
  213. OSSL_PROVIDER *provider = pgd->provider;
  214. const OSSL_PARAM *p;
  215. TLS_GROUP_INFO *ginf = NULL;
  216. EVP_KEYMGMT *keymgmt;
  217. unsigned int gid;
  218. unsigned int is_kem = 0;
  219. int ret = 0;
  220. if (ctx->group_list_max_len == ctx->group_list_len) {
  221. TLS_GROUP_INFO *tmp = NULL;
  222. if (ctx->group_list_max_len == 0)
  223. tmp = OPENSSL_malloc(sizeof(TLS_GROUP_INFO)
  224. * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);
  225. else
  226. tmp = OPENSSL_realloc(ctx->group_list,
  227. (ctx->group_list_max_len
  228. + TLS_GROUP_LIST_MALLOC_BLOCK_SIZE)
  229. * sizeof(TLS_GROUP_INFO));
  230. if (tmp == NULL) {
  231. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  232. return 0;
  233. }
  234. ctx->group_list = tmp;
  235. memset(tmp + ctx->group_list_max_len,
  236. 0,
  237. sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE);
  238. ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE;
  239. }
  240. ginf = &ctx->group_list[ctx->group_list_len];
  241. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME);
  242. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
  243. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  244. goto err;
  245. }
  246. ginf->tlsname = OPENSSL_strdup(p->data);
  247. if (ginf->tlsname == NULL) {
  248. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  249. goto err;
  250. }
  251. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL);
  252. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
  253. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  254. goto err;
  255. }
  256. ginf->realname = OPENSSL_strdup(p->data);
  257. if (ginf->realname == NULL) {
  258. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  259. goto err;
  260. }
  261. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID);
  262. if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) {
  263. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  264. goto err;
  265. }
  266. ginf->group_id = (uint16_t)gid;
  267. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG);
  268. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) {
  269. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  270. goto err;
  271. }
  272. ginf->algorithm = OPENSSL_strdup(p->data);
  273. if (ginf->algorithm == NULL) {
  274. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  275. goto err;
  276. }
  277. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS);
  278. if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) {
  279. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  280. goto err;
  281. }
  282. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM);
  283. if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) {
  284. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  285. goto err;
  286. }
  287. ginf->is_kem = 1 & is_kem;
  288. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS);
  289. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) {
  290. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  291. goto err;
  292. }
  293. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS);
  294. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) {
  295. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  296. goto err;
  297. }
  298. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS);
  299. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) {
  300. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  301. goto err;
  302. }
  303. p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS);
  304. if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) {
  305. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  306. goto err;
  307. }
  308. /*
  309. * Now check that the algorithm is actually usable for our property query
  310. * string. Regardless of the result we still return success because we have
  311. * successfully processed this group, even though we may decide not to use
  312. * it.
  313. */
  314. ret = 1;
  315. ERR_set_mark();
  316. keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq);
  317. if (keymgmt != NULL) {
  318. /*
  319. * We have successfully fetched the algorithm - however if the provider
  320. * doesn't match this one then we ignore it.
  321. *
  322. * Note: We're cheating a little here. Technically if the same algorithm
  323. * is available from more than one provider then it is undefined which
  324. * implementation you will get back. Theoretically this could be
  325. * different every time...we assume here that you'll always get the
  326. * same one back if you repeat the exact same fetch. Is this a reasonable
  327. * assumption to make (in which case perhaps we should document this
  328. * behaviour)?
  329. */
  330. if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) {
  331. /* We have a match - so we will use this group */
  332. ctx->group_list_len++;
  333. ginf = NULL;
  334. }
  335. EVP_KEYMGMT_free(keymgmt);
  336. }
  337. ERR_pop_to_mark();
  338. err:
  339. if (ginf != NULL) {
  340. OPENSSL_free(ginf->tlsname);
  341. OPENSSL_free(ginf->realname);
  342. OPENSSL_free(ginf->algorithm);
  343. ginf->algorithm = ginf->tlsname = ginf->realname = NULL;
  344. }
  345. return ret;
  346. }
  347. static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx)
  348. {
  349. struct provider_group_data_st pgd;
  350. pgd.ctx = vctx;
  351. pgd.provider = provider;
  352. return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP",
  353. add_provider_groups, &pgd);
  354. }
  355. int ssl_load_groups(SSL_CTX *ctx)
  356. {
  357. size_t i, j, num_deflt_grps = 0;
  358. uint16_t tmp_supp_groups[OSSL_NELEM(supported_groups_default)];
  359. if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx))
  360. return 0;
  361. for (i = 0; i < OSSL_NELEM(supported_groups_default); i++) {
  362. for (j = 0; j < ctx->group_list_len; j++) {
  363. if (ctx->group_list[j].group_id == supported_groups_default[i]) {
  364. tmp_supp_groups[num_deflt_grps++] = ctx->group_list[j].group_id;
  365. break;
  366. }
  367. }
  368. }
  369. if (num_deflt_grps == 0)
  370. return 1;
  371. ctx->ext.supported_groups_default
  372. = OPENSSL_malloc(sizeof(uint16_t) * num_deflt_grps);
  373. if (ctx->ext.supported_groups_default == NULL) {
  374. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  375. return 0;
  376. }
  377. memcpy(ctx->ext.supported_groups_default,
  378. tmp_supp_groups,
  379. num_deflt_grps * sizeof(tmp_supp_groups[0]));
  380. ctx->ext.supported_groups_default_len = num_deflt_grps;
  381. return 1;
  382. }
  383. static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name)
  384. {
  385. size_t i;
  386. for (i = 0; i < ctx->group_list_len; i++) {
  387. if (strcmp(ctx->group_list[i].tlsname, name) == 0
  388. || strcmp(ctx->group_list[i].realname, name) == 0)
  389. return ctx->group_list[i].group_id;
  390. }
  391. return 0;
  392. }
  393. const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id)
  394. {
  395. size_t i;
  396. for (i = 0; i < ctx->group_list_len; i++) {
  397. if (ctx->group_list[i].group_id == group_id)
  398. return &ctx->group_list[i];
  399. }
  400. return NULL;
  401. }
  402. int tls1_group_id2nid(uint16_t group_id, int include_unknown)
  403. {
  404. size_t i;
  405. if (group_id == 0)
  406. return NID_undef;
  407. /*
  408. * Return well known Group NIDs - for backwards compatibility. This won't
  409. * work for groups we don't know about.
  410. */
  411. for (i = 0; i < OSSL_NELEM(nid_to_group); i++)
  412. {
  413. if (nid_to_group[i].group_id == group_id)
  414. return nid_to_group[i].nid;
  415. }
  416. if (!include_unknown)
  417. return NID_undef;
  418. return TLSEXT_nid_unknown | (int)group_id;
  419. }
  420. uint16_t tls1_nid2group_id(int nid)
  421. {
  422. size_t i;
  423. /*
  424. * Return well known Group ids - for backwards compatibility. This won't
  425. * work for groups we don't know about.
  426. */
  427. for (i = 0; i < OSSL_NELEM(nid_to_group); i++)
  428. {
  429. if (nid_to_group[i].nid == nid)
  430. return nid_to_group[i].group_id;
  431. }
  432. return 0;
  433. }
  434. /*
  435. * Set *pgroups to the supported groups list and *pgroupslen to
  436. * the number of groups supported.
  437. */
  438. void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
  439. size_t *pgroupslen)
  440. {
  441. /* For Suite B mode only include P-256, P-384 */
  442. switch (tls1_suiteb(s)) {
  443. case SSL_CERT_FLAG_SUITEB_128_LOS:
  444. *pgroups = suiteb_curves;
  445. *pgroupslen = OSSL_NELEM(suiteb_curves);
  446. break;
  447. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  448. *pgroups = suiteb_curves;
  449. *pgroupslen = 1;
  450. break;
  451. case SSL_CERT_FLAG_SUITEB_192_LOS:
  452. *pgroups = suiteb_curves + 1;
  453. *pgroupslen = 1;
  454. break;
  455. default:
  456. if (s->ext.supportedgroups == NULL) {
  457. *pgroups = s->ctx->ext.supported_groups_default;
  458. *pgroupslen = s->ctx->ext.supported_groups_default_len;
  459. } else {
  460. *pgroups = s->ext.supportedgroups;
  461. *pgroupslen = s->ext.supportedgroups_len;
  462. }
  463. break;
  464. }
  465. }
  466. int tls_valid_group(SSL *s, uint16_t group_id, int minversion, int maxversion,
  467. int isec, int *okfortls13)
  468. {
  469. const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(s->ctx, group_id);
  470. int ret;
  471. if (okfortls13 != NULL)
  472. *okfortls13 = 0;
  473. if (ginfo == NULL)
  474. return 0;
  475. if (SSL_IS_DTLS(s)) {
  476. if (ginfo->mindtls < 0 || ginfo->maxdtls < 0)
  477. return 0;
  478. if (ginfo->maxdtls == 0)
  479. ret = 1;
  480. else
  481. ret = DTLS_VERSION_LE(minversion, ginfo->maxdtls);
  482. if (ginfo->mindtls > 0)
  483. ret &= DTLS_VERSION_GE(maxversion, ginfo->mindtls);
  484. } else {
  485. if (ginfo->mintls < 0 || ginfo->maxtls < 0)
  486. return 0;
  487. if (ginfo->maxtls == 0)
  488. ret = 1;
  489. else
  490. ret = (minversion <= ginfo->maxtls);
  491. if (ginfo->mintls > 0)
  492. ret &= (maxversion >= ginfo->mintls);
  493. if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION)
  494. *okfortls13 = (ginfo->maxtls == 0)
  495. || (ginfo->maxtls >= TLS1_3_VERSION);
  496. }
  497. ret &= !isec
  498. || strcmp(ginfo->algorithm, "EC") == 0
  499. || strcmp(ginfo->algorithm, "X25519") == 0
  500. || strcmp(ginfo->algorithm, "X448") == 0;
  501. return ret;
  502. }
  503. /* See if group is allowed by security callback */
  504. int tls_group_allowed(SSL *s, uint16_t group, int op)
  505. {
  506. const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(s->ctx, group);
  507. unsigned char gtmp[2];
  508. if (ginfo == NULL)
  509. return 0;
  510. gtmp[0] = group >> 8;
  511. gtmp[1] = group & 0xff;
  512. return ssl_security(s, op, ginfo->secbits,
  513. tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp);
  514. }
  515. /* Return 1 if "id" is in "list" */
  516. static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
  517. {
  518. size_t i;
  519. for (i = 0; i < listlen; i++)
  520. if (list[i] == id)
  521. return 1;
  522. return 0;
  523. }
  524. /*-
  525. * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
  526. * if there is no match.
  527. * For nmatch == -1, return number of matches
  528. * For nmatch == -2, return the id of the group to use for
  529. * a tmp key, or 0 if there is no match.
  530. */
  531. uint16_t tls1_shared_group(SSL *s, int nmatch)
  532. {
  533. const uint16_t *pref, *supp;
  534. size_t num_pref, num_supp, i;
  535. int k;
  536. SSL_CTX *ctx = s->ctx;
  537. /* Can't do anything on client side */
  538. if (s->server == 0)
  539. return 0;
  540. if (nmatch == -2) {
  541. if (tls1_suiteb(s)) {
  542. /*
  543. * For Suite B ciphersuite determines curve: we already know
  544. * these are acceptable due to previous checks.
  545. */
  546. unsigned long cid = s->s3.tmp.new_cipher->id;
  547. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  548. return TLSEXT_curve_P_256;
  549. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  550. return TLSEXT_curve_P_384;
  551. /* Should never happen */
  552. return 0;
  553. }
  554. /* If not Suite B just return first preference shared curve */
  555. nmatch = 0;
  556. }
  557. /*
  558. * If server preference set, our groups are the preference order
  559. * otherwise peer decides.
  560. */
  561. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  562. tls1_get_supported_groups(s, &pref, &num_pref);
  563. tls1_get_peer_groups(s, &supp, &num_supp);
  564. } else {
  565. tls1_get_peer_groups(s, &pref, &num_pref);
  566. tls1_get_supported_groups(s, &supp, &num_supp);
  567. }
  568. for (k = 0, i = 0; i < num_pref; i++) {
  569. uint16_t id = pref[i];
  570. const TLS_GROUP_INFO *inf;
  571. if (!tls1_in_list(id, supp, num_supp)
  572. || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED))
  573. continue;
  574. inf = tls1_group_id_lookup(ctx, id);
  575. if (!ossl_assert(inf != NULL))
  576. return 0;
  577. if (SSL_IS_DTLS(s)) {
  578. if (inf->maxdtls == -1)
  579. continue;
  580. if ((inf->mindtls != 0 && DTLS_VERSION_LT(s->version, inf->mindtls))
  581. || (inf->maxdtls != 0
  582. && DTLS_VERSION_GT(s->version, inf->maxdtls)))
  583. continue;
  584. } else {
  585. if (inf->maxtls == -1)
  586. continue;
  587. if ((inf->mintls != 0 && s->version < inf->mintls)
  588. || (inf->maxtls != 0 && s->version > inf->maxtls))
  589. continue;
  590. }
  591. if (nmatch == k)
  592. return id;
  593. k++;
  594. }
  595. if (nmatch == -1)
  596. return k;
  597. /* Out of range (nmatch > k). */
  598. return 0;
  599. }
  600. int tls1_set_groups(uint16_t **pext, size_t *pextlen,
  601. int *groups, size_t ngroups)
  602. {
  603. uint16_t *glist;
  604. size_t i;
  605. /*
  606. * Bitmap of groups included to detect duplicates: two variables are added
  607. * to detect duplicates as some values are more than 32.
  608. */
  609. unsigned long *dup_list = NULL;
  610. unsigned long dup_list_egrp = 0;
  611. unsigned long dup_list_dhgrp = 0;
  612. if (ngroups == 0) {
  613. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  614. return 0;
  615. }
  616. if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) {
  617. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  618. return 0;
  619. }
  620. for (i = 0; i < ngroups; i++) {
  621. unsigned long idmask;
  622. uint16_t id;
  623. id = tls1_nid2group_id(groups[i]);
  624. if ((id & 0x00FF) >= (sizeof(unsigned long) * 8))
  625. goto err;
  626. idmask = 1L << (id & 0x00FF);
  627. dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp;
  628. if (!id || ((*dup_list) & idmask))
  629. goto err;
  630. *dup_list |= idmask;
  631. glist[i] = id;
  632. }
  633. OPENSSL_free(*pext);
  634. *pext = glist;
  635. *pextlen = ngroups;
  636. return 1;
  637. err:
  638. OPENSSL_free(glist);
  639. return 0;
  640. }
  641. # define GROUPLIST_INCREMENT 40
  642. # define GROUP_NAME_BUFFER_LENGTH 64
  643. typedef struct {
  644. SSL_CTX *ctx;
  645. size_t gidcnt;
  646. size_t gidmax;
  647. uint16_t *gid_arr;
  648. } gid_cb_st;
  649. static int gid_cb(const char *elem, int len, void *arg)
  650. {
  651. gid_cb_st *garg = arg;
  652. size_t i;
  653. uint16_t gid = 0;
  654. char etmp[GROUP_NAME_BUFFER_LENGTH];
  655. if (elem == NULL)
  656. return 0;
  657. if (garg->gidcnt == garg->gidmax) {
  658. uint16_t *tmp =
  659. OPENSSL_realloc(garg->gid_arr,
  660. (garg->gidmax + GROUPLIST_INCREMENT) * sizeof(*garg->gid_arr));
  661. if (tmp == NULL)
  662. return 0;
  663. garg->gidmax += GROUPLIST_INCREMENT;
  664. garg->gid_arr = tmp;
  665. }
  666. if (len > (int)(sizeof(etmp) - 1))
  667. return 0;
  668. memcpy(etmp, elem, len);
  669. etmp[len] = 0;
  670. gid = tls1_group_name2id(garg->ctx, etmp);
  671. if (gid == 0) {
  672. ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,
  673. "group '%s' cannot be set", etmp);
  674. return 0;
  675. }
  676. for (i = 0; i < garg->gidcnt; i++)
  677. if (garg->gid_arr[i] == gid)
  678. return 0;
  679. garg->gid_arr[garg->gidcnt++] = gid;
  680. return 1;
  681. }
  682. /* Set groups based on a colon separated list */
  683. int tls1_set_groups_list(SSL_CTX *ctx, uint16_t **pext, size_t *pextlen,
  684. const char *str)
  685. {
  686. gid_cb_st gcb;
  687. uint16_t *tmparr;
  688. int ret = 0;
  689. gcb.gidcnt = 0;
  690. gcb.gidmax = GROUPLIST_INCREMENT;
  691. gcb.gid_arr = OPENSSL_malloc(gcb.gidmax * sizeof(*gcb.gid_arr));
  692. if (gcb.gid_arr == NULL)
  693. return 0;
  694. gcb.ctx = ctx;
  695. if (!CONF_parse_list(str, ':', 1, gid_cb, &gcb))
  696. goto end;
  697. if (pext == NULL) {
  698. ret = 1;
  699. goto end;
  700. }
  701. /*
  702. * gid_cb ensurse there are no duplicates so we can just go ahead and set
  703. * the result
  704. */
  705. tmparr = OPENSSL_memdup(gcb.gid_arr, gcb.gidcnt * sizeof(*tmparr));
  706. if (tmparr == NULL)
  707. goto end;
  708. OPENSSL_free(*pext);
  709. *pext = tmparr;
  710. *pextlen = gcb.gidcnt;
  711. ret = 1;
  712. end:
  713. OPENSSL_free(gcb.gid_arr);
  714. return ret;
  715. }
  716. /* Check a group id matches preferences */
  717. int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups)
  718. {
  719. const uint16_t *groups;
  720. size_t groups_len;
  721. if (group_id == 0)
  722. return 0;
  723. /* Check for Suite B compliance */
  724. if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) {
  725. unsigned long cid = s->s3.tmp.new_cipher->id;
  726. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
  727. if (group_id != TLSEXT_curve_P_256)
  728. return 0;
  729. } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
  730. if (group_id != TLSEXT_curve_P_384)
  731. return 0;
  732. } else {
  733. /* Should never happen */
  734. return 0;
  735. }
  736. }
  737. if (check_own_groups) {
  738. /* Check group is one of our preferences */
  739. tls1_get_supported_groups(s, &groups, &groups_len);
  740. if (!tls1_in_list(group_id, groups, groups_len))
  741. return 0;
  742. }
  743. if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
  744. return 0;
  745. /* For clients, nothing more to check */
  746. if (!s->server)
  747. return 1;
  748. /* Check group is one of peers preferences */
  749. tls1_get_peer_groups(s, &groups, &groups_len);
  750. /*
  751. * RFC 4492 does not require the supported elliptic curves extension
  752. * so if it is not sent we can just choose any curve.
  753. * It is invalid to send an empty list in the supported groups
  754. * extension, so groups_len == 0 always means no extension.
  755. */
  756. if (groups_len == 0)
  757. return 1;
  758. return tls1_in_list(group_id, groups, groups_len);
  759. }
  760. void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
  761. size_t *num_formats)
  762. {
  763. /*
  764. * If we have a custom point format list use it otherwise use default
  765. */
  766. if (s->ext.ecpointformats) {
  767. *pformats = s->ext.ecpointformats;
  768. *num_formats = s->ext.ecpointformats_len;
  769. } else {
  770. *pformats = ecformats_default;
  771. /* For Suite B we don't support char2 fields */
  772. if (tls1_suiteb(s))
  773. *num_formats = sizeof(ecformats_default) - 1;
  774. else
  775. *num_formats = sizeof(ecformats_default);
  776. }
  777. }
  778. /* Check a key is compatible with compression extension */
  779. static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
  780. {
  781. unsigned char comp_id;
  782. size_t i;
  783. int point_conv;
  784. /* If not an EC key nothing to check */
  785. if (!EVP_PKEY_is_a(pkey, "EC"))
  786. return 1;
  787. /* Get required compression id */
  788. point_conv = EVP_PKEY_get_ec_point_conv_form(pkey);
  789. if (point_conv == 0)
  790. return 0;
  791. if (point_conv == POINT_CONVERSION_UNCOMPRESSED) {
  792. comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
  793. } else if (SSL_IS_TLS13(s)) {
  794. /*
  795. * ec_point_formats extension is not used in TLSv1.3 so we ignore
  796. * this check.
  797. */
  798. return 1;
  799. } else {
  800. int field_type = EVP_PKEY_get_field_type(pkey);
  801. if (field_type == NID_X9_62_prime_field)
  802. comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  803. else if (field_type == NID_X9_62_characteristic_two_field)
  804. comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  805. else
  806. return 0;
  807. }
  808. /*
  809. * If point formats extension present check it, otherwise everything is
  810. * supported (see RFC4492).
  811. */
  812. if (s->ext.peer_ecpointformats == NULL)
  813. return 1;
  814. for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
  815. if (s->ext.peer_ecpointformats[i] == comp_id)
  816. return 1;
  817. }
  818. return 0;
  819. }
  820. /* Return group id of a key */
  821. static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
  822. {
  823. int curve_nid = ssl_get_EC_curve_nid(pkey);
  824. if (curve_nid == NID_undef)
  825. return 0;
  826. return tls1_nid2group_id(curve_nid);
  827. }
  828. /*
  829. * Check cert parameters compatible with extensions: currently just checks EC
  830. * certificates have compatible curves and compression.
  831. */
  832. static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
  833. {
  834. uint16_t group_id;
  835. EVP_PKEY *pkey;
  836. pkey = X509_get0_pubkey(x);
  837. if (pkey == NULL)
  838. return 0;
  839. /* If not EC nothing to do */
  840. if (!EVP_PKEY_is_a(pkey, "EC"))
  841. return 1;
  842. /* Check compression */
  843. if (!tls1_check_pkey_comp(s, pkey))
  844. return 0;
  845. group_id = tls1_get_group_id(pkey);
  846. /*
  847. * For a server we allow the certificate to not be in our list of supported
  848. * groups.
  849. */
  850. if (!tls1_check_group_id(s, group_id, !s->server))
  851. return 0;
  852. /*
  853. * Special case for suite B. We *MUST* sign using SHA256+P-256 or
  854. * SHA384+P-384.
  855. */
  856. if (check_ee_md && tls1_suiteb(s)) {
  857. int check_md;
  858. size_t i;
  859. /* Check to see we have necessary signing algorithm */
  860. if (group_id == TLSEXT_curve_P_256)
  861. check_md = NID_ecdsa_with_SHA256;
  862. else if (group_id == TLSEXT_curve_P_384)
  863. check_md = NID_ecdsa_with_SHA384;
  864. else
  865. return 0; /* Should never happen */
  866. for (i = 0; i < s->shared_sigalgslen; i++) {
  867. if (check_md == s->shared_sigalgs[i]->sigandhash)
  868. return 1;;
  869. }
  870. return 0;
  871. }
  872. return 1;
  873. }
  874. /*
  875. * tls1_check_ec_tmp_key - Check EC temporary key compatibility
  876. * @s: SSL connection
  877. * @cid: Cipher ID we're considering using
  878. *
  879. * Checks that the kECDHE cipher suite we're considering using
  880. * is compatible with the client extensions.
  881. *
  882. * Returns 0 when the cipher can't be used or 1 when it can.
  883. */
  884. int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
  885. {
  886. /* If not Suite B just need a shared group */
  887. if (!tls1_suiteb(s))
  888. return tls1_shared_group(s, 0) != 0;
  889. /*
  890. * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
  891. * curves permitted.
  892. */
  893. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  894. return tls1_check_group_id(s, TLSEXT_curve_P_256, 1);
  895. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  896. return tls1_check_group_id(s, TLSEXT_curve_P_384, 1);
  897. return 0;
  898. }
  899. /* Default sigalg schemes */
  900. static const uint16_t tls12_sigalgs[] = {
  901. TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  902. TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
  903. TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
  904. TLSEXT_SIGALG_ed25519,
  905. TLSEXT_SIGALG_ed448,
  906. TLSEXT_SIGALG_rsa_pss_pss_sha256,
  907. TLSEXT_SIGALG_rsa_pss_pss_sha384,
  908. TLSEXT_SIGALG_rsa_pss_pss_sha512,
  909. TLSEXT_SIGALG_rsa_pss_rsae_sha256,
  910. TLSEXT_SIGALG_rsa_pss_rsae_sha384,
  911. TLSEXT_SIGALG_rsa_pss_rsae_sha512,
  912. TLSEXT_SIGALG_rsa_pkcs1_sha256,
  913. TLSEXT_SIGALG_rsa_pkcs1_sha384,
  914. TLSEXT_SIGALG_rsa_pkcs1_sha512,
  915. TLSEXT_SIGALG_ecdsa_sha224,
  916. TLSEXT_SIGALG_ecdsa_sha1,
  917. TLSEXT_SIGALG_rsa_pkcs1_sha224,
  918. TLSEXT_SIGALG_rsa_pkcs1_sha1,
  919. TLSEXT_SIGALG_dsa_sha224,
  920. TLSEXT_SIGALG_dsa_sha1,
  921. TLSEXT_SIGALG_dsa_sha256,
  922. TLSEXT_SIGALG_dsa_sha384,
  923. TLSEXT_SIGALG_dsa_sha512,
  924. #ifndef OPENSSL_NO_GOST
  925. TLSEXT_SIGALG_gostr34102012_256_intrinsic,
  926. TLSEXT_SIGALG_gostr34102012_512_intrinsic,
  927. TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
  928. TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
  929. TLSEXT_SIGALG_gostr34102001_gostr3411,
  930. #endif
  931. };
  932. static const uint16_t suiteb_sigalgs[] = {
  933. TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  934. TLSEXT_SIGALG_ecdsa_secp384r1_sha384
  935. };
  936. static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
  937. {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
  938. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  939. NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1},
  940. {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
  941. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  942. NID_ecdsa_with_SHA384, NID_secp384r1, 1},
  943. {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
  944. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  945. NID_ecdsa_with_SHA512, NID_secp521r1, 1},
  946. {"ed25519", TLSEXT_SIGALG_ed25519,
  947. NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
  948. NID_undef, NID_undef, 1},
  949. {"ed448", TLSEXT_SIGALG_ed448,
  950. NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448,
  951. NID_undef, NID_undef, 1},
  952. {NULL, TLSEXT_SIGALG_ecdsa_sha224,
  953. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  954. NID_ecdsa_with_SHA224, NID_undef, 1},
  955. {NULL, TLSEXT_SIGALG_ecdsa_sha1,
  956. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
  957. NID_ecdsa_with_SHA1, NID_undef, 1},
  958. {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,
  959. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  960. NID_undef, NID_undef, 1},
  961. {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,
  962. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  963. NID_undef, NID_undef, 1},
  964. {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,
  965. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
  966. NID_undef, NID_undef, 1},
  967. {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256,
  968. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  969. NID_undef, NID_undef, 1},
  970. {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384,
  971. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  972. NID_undef, NID_undef, 1},
  973. {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512,
  974. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
  975. NID_undef, NID_undef, 1},
  976. {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
  977. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  978. NID_sha256WithRSAEncryption, NID_undef, 1},
  979. {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
  980. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  981. NID_sha384WithRSAEncryption, NID_undef, 1},
  982. {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
  983. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  984. NID_sha512WithRSAEncryption, NID_undef, 1},
  985. {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
  986. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  987. NID_sha224WithRSAEncryption, NID_undef, 1},
  988. {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
  989. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
  990. NID_sha1WithRSAEncryption, NID_undef, 1},
  991. {NULL, TLSEXT_SIGALG_dsa_sha256,
  992. NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  993. NID_dsa_with_SHA256, NID_undef, 1},
  994. {NULL, TLSEXT_SIGALG_dsa_sha384,
  995. NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  996. NID_undef, NID_undef, 1},
  997. {NULL, TLSEXT_SIGALG_dsa_sha512,
  998. NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  999. NID_undef, NID_undef, 1},
  1000. {NULL, TLSEXT_SIGALG_dsa_sha224,
  1001. NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  1002. NID_undef, NID_undef, 1},
  1003. {NULL, TLSEXT_SIGALG_dsa_sha1,
  1004. NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
  1005. NID_dsaWithSHA1, NID_undef, 1},
  1006. #ifndef OPENSSL_NO_GOST
  1007. {NULL, TLSEXT_SIGALG_gostr34102012_256_intrinsic,
  1008. NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
  1009. NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
  1010. NID_undef, NID_undef, 1},
  1011. {NULL, TLSEXT_SIGALG_gostr34102012_512_intrinsic,
  1012. NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
  1013. NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
  1014. NID_undef, NID_undef, 1},
  1015. {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
  1016. NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
  1017. NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
  1018. NID_undef, NID_undef, 1},
  1019. {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
  1020. NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
  1021. NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
  1022. NID_undef, NID_undef, 1},
  1023. {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
  1024. NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
  1025. NID_id_GostR3410_2001, SSL_PKEY_GOST01,
  1026. NID_undef, NID_undef, 1}
  1027. #endif
  1028. };
  1029. /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
  1030. static const SIGALG_LOOKUP legacy_rsa_sigalg = {
  1031. "rsa_pkcs1_md5_sha1", 0,
  1032. NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
  1033. EVP_PKEY_RSA, SSL_PKEY_RSA,
  1034. NID_undef, NID_undef, 1
  1035. };
  1036. /*
  1037. * Default signature algorithm values used if signature algorithms not present.
  1038. * From RFC5246. Note: order must match certificate index order.
  1039. */
  1040. static const uint16_t tls_default_sigalg[] = {
  1041. TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
  1042. 0, /* SSL_PKEY_RSA_PSS_SIGN */
  1043. TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
  1044. TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
  1045. TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
  1046. TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */
  1047. TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */
  1048. 0, /* SSL_PKEY_ED25519 */
  1049. 0, /* SSL_PKEY_ED448 */
  1050. };
  1051. int ssl_setup_sig_algs(SSL_CTX *ctx)
  1052. {
  1053. size_t i;
  1054. const SIGALG_LOOKUP *lu;
  1055. SIGALG_LOOKUP *cache
  1056. = OPENSSL_malloc(sizeof(*lu) * OSSL_NELEM(sigalg_lookup_tbl));
  1057. EVP_PKEY *tmpkey = EVP_PKEY_new();
  1058. int ret = 0;
  1059. if (cache == NULL || tmpkey == NULL)
  1060. goto err;
  1061. ERR_set_mark();
  1062. for (i = 0, lu = sigalg_lookup_tbl;
  1063. i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) {
  1064. EVP_PKEY_CTX *pctx;
  1065. cache[i] = *lu;
  1066. /*
  1067. * Check hash is available.
  1068. * This test is not perfect. A provider could have support
  1069. * for a signature scheme, but not a particular hash. However the hash
  1070. * could be available from some other loaded provider. In that case it
  1071. * could be that the signature is available, and the hash is available
  1072. * independently - but not as a combination. We ignore this for now.
  1073. */
  1074. if (lu->hash != NID_undef
  1075. && ctx->ssl_digest_methods[lu->hash_idx] == NULL) {
  1076. cache[i].enabled = 0;
  1077. continue;
  1078. }
  1079. if (!EVP_PKEY_set_type(tmpkey, lu->sig)) {
  1080. cache[i].enabled = 0;
  1081. continue;
  1082. }
  1083. pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq);
  1084. /* If unable to create pctx we assume the sig algorithm is unavailable */
  1085. if (pctx == NULL)
  1086. cache[i].enabled = 0;
  1087. EVP_PKEY_CTX_free(pctx);
  1088. }
  1089. ERR_pop_to_mark();
  1090. ctx->sigalg_lookup_cache = cache;
  1091. cache = NULL;
  1092. ret = 1;
  1093. err:
  1094. OPENSSL_free(cache);
  1095. EVP_PKEY_free(tmpkey);
  1096. return ret;
  1097. }
  1098. /* Lookup TLS signature algorithm */
  1099. static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL *s, uint16_t sigalg)
  1100. {
  1101. size_t i;
  1102. const SIGALG_LOOKUP *lu;
  1103. for (i = 0, lu = s->ctx->sigalg_lookup_cache;
  1104. /* cache should have the same number of elements as sigalg_lookup_tbl */
  1105. i < OSSL_NELEM(sigalg_lookup_tbl);
  1106. lu++, i++) {
  1107. if (lu->sigalg == sigalg) {
  1108. if (!lu->enabled)
  1109. return NULL;
  1110. return lu;
  1111. }
  1112. }
  1113. return NULL;
  1114. }
  1115. /* Lookup hash: return 0 if invalid or not enabled */
  1116. int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
  1117. {
  1118. const EVP_MD *md;
  1119. if (lu == NULL)
  1120. return 0;
  1121. /* lu->hash == NID_undef means no associated digest */
  1122. if (lu->hash == NID_undef) {
  1123. md = NULL;
  1124. } else {
  1125. md = ssl_md(ctx, lu->hash_idx);
  1126. if (md == NULL)
  1127. return 0;
  1128. }
  1129. if (pmd)
  1130. *pmd = md;
  1131. return 1;
  1132. }
  1133. /*
  1134. * Check if key is large enough to generate RSA-PSS signature.
  1135. *
  1136. * The key must greater than or equal to 2 * hash length + 2.
  1137. * SHA512 has a hash length of 64 bytes, which is incompatible
  1138. * with a 128 byte (1024 bit) key.
  1139. */
  1140. #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_get_size(md) + 2)
  1141. static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey,
  1142. const SIGALG_LOOKUP *lu)
  1143. {
  1144. const EVP_MD *md;
  1145. if (pkey == NULL)
  1146. return 0;
  1147. if (!tls1_lookup_md(ctx, lu, &md) || md == NULL)
  1148. return 0;
  1149. if (EVP_PKEY_get_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md))
  1150. return 0;
  1151. return 1;
  1152. }
  1153. /*
  1154. * Returns a signature algorithm when the peer did not send a list of supported
  1155. * signature algorithms. The signature algorithm is fixed for the certificate
  1156. * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the
  1157. * certificate type from |s| will be used.
  1158. * Returns the signature algorithm to use, or NULL on error.
  1159. */
  1160. static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
  1161. {
  1162. if (idx == -1) {
  1163. if (s->server) {
  1164. size_t i;
  1165. /* Work out index corresponding to ciphersuite */
  1166. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1167. const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
  1168. if (clu == NULL)
  1169. continue;
  1170. if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) {
  1171. idx = i;
  1172. break;
  1173. }
  1174. }
  1175. /*
  1176. * Some GOST ciphersuites allow more than one signature algorithms
  1177. * */
  1178. if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) {
  1179. int real_idx;
  1180. for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01;
  1181. real_idx--) {
  1182. if (s->cert->pkeys[real_idx].privatekey != NULL) {
  1183. idx = real_idx;
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. /*
  1189. * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used
  1190. * with new (aGOST12-only) ciphersuites, we should find out which one is available really.
  1191. */
  1192. else if (idx == SSL_PKEY_GOST12_256) {
  1193. int real_idx;
  1194. for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256;
  1195. real_idx--) {
  1196. if (s->cert->pkeys[real_idx].privatekey != NULL) {
  1197. idx = real_idx;
  1198. break;
  1199. }
  1200. }
  1201. }
  1202. } else {
  1203. idx = s->cert->key - s->cert->pkeys;
  1204. }
  1205. }
  1206. if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
  1207. return NULL;
  1208. if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
  1209. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, tls_default_sigalg[idx]);
  1210. if (lu == NULL)
  1211. return NULL;
  1212. if (!tls1_lookup_md(s->ctx, lu, NULL))
  1213. return NULL;
  1214. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
  1215. return NULL;
  1216. return lu;
  1217. }
  1218. if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg))
  1219. return NULL;
  1220. return &legacy_rsa_sigalg;
  1221. }
  1222. /* Set peer sigalg based key type */
  1223. int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
  1224. {
  1225. size_t idx;
  1226. const SIGALG_LOOKUP *lu;
  1227. if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
  1228. return 0;
  1229. lu = tls1_get_legacy_sigalg(s, idx);
  1230. if (lu == NULL)
  1231. return 0;
  1232. s->s3.tmp.peer_sigalg = lu;
  1233. return 1;
  1234. }
  1235. size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
  1236. {
  1237. /*
  1238. * If Suite B mode use Suite B sigalgs only, ignore any other
  1239. * preferences.
  1240. */
  1241. switch (tls1_suiteb(s)) {
  1242. case SSL_CERT_FLAG_SUITEB_128_LOS:
  1243. *psigs = suiteb_sigalgs;
  1244. return OSSL_NELEM(suiteb_sigalgs);
  1245. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  1246. *psigs = suiteb_sigalgs;
  1247. return 1;
  1248. case SSL_CERT_FLAG_SUITEB_192_LOS:
  1249. *psigs = suiteb_sigalgs + 1;
  1250. return 1;
  1251. }
  1252. /*
  1253. * We use client_sigalgs (if not NULL) if we're a server
  1254. * and sending a certificate request or if we're a client and
  1255. * determining which shared algorithm to use.
  1256. */
  1257. if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
  1258. *psigs = s->cert->client_sigalgs;
  1259. return s->cert->client_sigalgslen;
  1260. } else if (s->cert->conf_sigalgs) {
  1261. *psigs = s->cert->conf_sigalgs;
  1262. return s->cert->conf_sigalgslen;
  1263. } else {
  1264. *psigs = tls12_sigalgs;
  1265. return OSSL_NELEM(tls12_sigalgs);
  1266. }
  1267. }
  1268. /*
  1269. * Called by servers only. Checks that we have a sig alg that supports the
  1270. * specified EC curve.
  1271. */
  1272. int tls_check_sigalg_curve(const SSL *s, int curve)
  1273. {
  1274. const uint16_t *sigs;
  1275. size_t siglen, i;
  1276. if (s->cert->conf_sigalgs) {
  1277. sigs = s->cert->conf_sigalgs;
  1278. siglen = s->cert->conf_sigalgslen;
  1279. } else {
  1280. sigs = tls12_sigalgs;
  1281. siglen = OSSL_NELEM(tls12_sigalgs);
  1282. }
  1283. for (i = 0; i < siglen; i++) {
  1284. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, sigs[i]);
  1285. if (lu == NULL)
  1286. continue;
  1287. if (lu->sig == EVP_PKEY_EC
  1288. && lu->curve != NID_undef
  1289. && curve == lu->curve)
  1290. return 1;
  1291. }
  1292. return 0;
  1293. }
  1294. /*
  1295. * Return the number of security bits for the signature algorithm, or 0 on
  1296. * error.
  1297. */
  1298. static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu)
  1299. {
  1300. const EVP_MD *md = NULL;
  1301. int secbits = 0;
  1302. if (!tls1_lookup_md(ctx, lu, &md))
  1303. return 0;
  1304. if (md != NULL)
  1305. {
  1306. int md_type = EVP_MD_get_type(md);
  1307. /* Security bits: half digest bits */
  1308. secbits = EVP_MD_get_size(md) * 4;
  1309. /*
  1310. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  1311. * they're no longer accepted at security level 1. The real values don't
  1312. * really matter as long as they're lower than 80, which is our
  1313. * security level 1.
  1314. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for
  1315. * SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2
  1316. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  1317. * puts a chosen-prefix attack for MD5 at 2^39.
  1318. */
  1319. if (md_type == NID_sha1)
  1320. secbits = 64;
  1321. else if (md_type == NID_md5_sha1)
  1322. secbits = 67;
  1323. else if (md_type == NID_md5)
  1324. secbits = 39;
  1325. } else {
  1326. /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */
  1327. if (lu->sigalg == TLSEXT_SIGALG_ed25519)
  1328. secbits = 128;
  1329. else if (lu->sigalg == TLSEXT_SIGALG_ed448)
  1330. secbits = 224;
  1331. }
  1332. return secbits;
  1333. }
  1334. /*
  1335. * Check signature algorithm is consistent with sent supported signature
  1336. * algorithms and if so set relevant digest and signature scheme in
  1337. * s.
  1338. */
  1339. int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
  1340. {
  1341. const uint16_t *sent_sigs;
  1342. const EVP_MD *md = NULL;
  1343. char sigalgstr[2];
  1344. size_t sent_sigslen, i, cidx;
  1345. int pkeyid = -1;
  1346. const SIGALG_LOOKUP *lu;
  1347. int secbits = 0;
  1348. pkeyid = EVP_PKEY_get_id(pkey);
  1349. /* Should never happen */
  1350. if (pkeyid == -1)
  1351. return -1;
  1352. if (SSL_IS_TLS13(s)) {
  1353. /* Disallow DSA for TLS 1.3 */
  1354. if (pkeyid == EVP_PKEY_DSA) {
  1355. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);
  1356. return 0;
  1357. }
  1358. /* Only allow PSS for TLS 1.3 */
  1359. if (pkeyid == EVP_PKEY_RSA)
  1360. pkeyid = EVP_PKEY_RSA_PSS;
  1361. }
  1362. lu = tls1_lookup_sigalg(s, sig);
  1363. /*
  1364. * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
  1365. * is consistent with signature: RSA keys can be used for RSA-PSS
  1366. */
  1367. if (lu == NULL
  1368. || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
  1369. || (pkeyid != lu->sig
  1370. && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
  1371. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);
  1372. return 0;
  1373. }
  1374. /* Check the sigalg is consistent with the key OID */
  1375. if (!ssl_cert_lookup_by_nid(EVP_PKEY_get_id(pkey), &cidx)
  1376. || lu->sig_idx != (int)cidx) {
  1377. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE);
  1378. return 0;
  1379. }
  1380. if (pkeyid == EVP_PKEY_EC) {
  1381. /* Check point compression is permitted */
  1382. if (!tls1_check_pkey_comp(s, pkey)) {
  1383. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1384. SSL_R_ILLEGAL_POINT_COMPRESSION);
  1385. return 0;
  1386. }
  1387. /* For TLS 1.3 or Suite B check curve matches signature algorithm */
  1388. if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
  1389. int curve = ssl_get_EC_curve_nid(pkey);
  1390. if (lu->curve != NID_undef && curve != lu->curve) {
  1391. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
  1392. return 0;
  1393. }
  1394. }
  1395. if (!SSL_IS_TLS13(s)) {
  1396. /* Check curve matches extensions */
  1397. if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) {
  1398. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
  1399. return 0;
  1400. }
  1401. if (tls1_suiteb(s)) {
  1402. /* Check sigalg matches a permissible Suite B value */
  1403. if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
  1404. && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
  1405. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1406. SSL_R_WRONG_SIGNATURE_TYPE);
  1407. return 0;
  1408. }
  1409. }
  1410. }
  1411. } else if (tls1_suiteb(s)) {
  1412. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);
  1413. return 0;
  1414. }
  1415. /* Check signature matches a type we sent */
  1416. sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  1417. for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
  1418. if (sig == *sent_sigs)
  1419. break;
  1420. }
  1421. /* Allow fallback to SHA1 if not strict mode */
  1422. if (i == sent_sigslen && (lu->hash != NID_sha1
  1423. || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
  1424. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);
  1425. return 0;
  1426. }
  1427. if (!tls1_lookup_md(s->ctx, lu, &md)) {
  1428. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST);
  1429. return 0;
  1430. }
  1431. /*
  1432. * Make sure security callback allows algorithm. For historical
  1433. * reasons we have to pass the sigalg as a two byte char array.
  1434. */
  1435. sigalgstr[0] = (sig >> 8) & 0xff;
  1436. sigalgstr[1] = sig & 0xff;
  1437. secbits = sigalg_security_bits(s->ctx, lu);
  1438. if (secbits == 0 ||
  1439. !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits,
  1440. md != NULL ? EVP_MD_get_type(md) : NID_undef,
  1441. (void *)sigalgstr)) {
  1442. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);
  1443. return 0;
  1444. }
  1445. /* Store the sigalg the peer uses */
  1446. s->s3.tmp.peer_sigalg = lu;
  1447. return 1;
  1448. }
  1449. int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
  1450. {
  1451. if (s->s3.tmp.peer_sigalg == NULL)
  1452. return 0;
  1453. *pnid = s->s3.tmp.peer_sigalg->sig;
  1454. return 1;
  1455. }
  1456. int SSL_get_signature_type_nid(const SSL *s, int *pnid)
  1457. {
  1458. if (s->s3.tmp.sigalg == NULL)
  1459. return 0;
  1460. *pnid = s->s3.tmp.sigalg->sig;
  1461. return 1;
  1462. }
  1463. /*
  1464. * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
  1465. * supported, doesn't appear in supported signature algorithms, isn't supported
  1466. * by the enabled protocol versions or by the security level.
  1467. *
  1468. * This function should only be used for checking which ciphers are supported
  1469. * by the client.
  1470. *
  1471. * Call ssl_cipher_disabled() to check that it's enabled or not.
  1472. */
  1473. int ssl_set_client_disabled(SSL *s)
  1474. {
  1475. s->s3.tmp.mask_a = 0;
  1476. s->s3.tmp.mask_k = 0;
  1477. ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
  1478. if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver,
  1479. &s->s3.tmp.max_ver, NULL) != 0)
  1480. return 0;
  1481. #ifndef OPENSSL_NO_PSK
  1482. /* with PSK there must be client callback set */
  1483. if (!s->psk_client_callback) {
  1484. s->s3.tmp.mask_a |= SSL_aPSK;
  1485. s->s3.tmp.mask_k |= SSL_PSK;
  1486. }
  1487. #endif /* OPENSSL_NO_PSK */
  1488. #ifndef OPENSSL_NO_SRP
  1489. if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
  1490. s->s3.tmp.mask_a |= SSL_aSRP;
  1491. s->s3.tmp.mask_k |= SSL_kSRP;
  1492. }
  1493. #endif
  1494. return 1;
  1495. }
  1496. /*
  1497. * ssl_cipher_disabled - check that a cipher is disabled or not
  1498. * @s: SSL connection that you want to use the cipher on
  1499. * @c: cipher to check
  1500. * @op: Security check that you want to do
  1501. * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
  1502. *
  1503. * Returns 1 when it's disabled, 0 when enabled.
  1504. */
  1505. int ssl_cipher_disabled(const SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
  1506. {
  1507. if (c->algorithm_mkey & s->s3.tmp.mask_k
  1508. || c->algorithm_auth & s->s3.tmp.mask_a)
  1509. return 1;
  1510. if (s->s3.tmp.max_ver == 0)
  1511. return 1;
  1512. if (!SSL_IS_DTLS(s)) {
  1513. int min_tls = c->min_tls;
  1514. /*
  1515. * For historical reasons we will allow ECHDE to be selected by a server
  1516. * in SSLv3 if we are a client
  1517. */
  1518. if (min_tls == TLS1_VERSION && ecdhe
  1519. && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
  1520. min_tls = SSL3_VERSION;
  1521. if ((min_tls > s->s3.tmp.max_ver) || (c->max_tls < s->s3.tmp.min_ver))
  1522. return 1;
  1523. }
  1524. if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3.tmp.max_ver)
  1525. || DTLS_VERSION_LT(c->max_dtls, s->s3.tmp.min_ver)))
  1526. return 1;
  1527. return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
  1528. }
  1529. int tls_use_ticket(SSL *s)
  1530. {
  1531. if ((s->options & SSL_OP_NO_TICKET))
  1532. return 0;
  1533. return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
  1534. }
  1535. int tls1_set_server_sigalgs(SSL *s)
  1536. {
  1537. size_t i;
  1538. /* Clear any shared signature algorithms */
  1539. OPENSSL_free(s->shared_sigalgs);
  1540. s->shared_sigalgs = NULL;
  1541. s->shared_sigalgslen = 0;
  1542. /* Clear certificate validity flags */
  1543. for (i = 0; i < SSL_PKEY_NUM; i++)
  1544. s->s3.tmp.valid_flags[i] = 0;
  1545. /*
  1546. * If peer sent no signature algorithms check to see if we support
  1547. * the default algorithm for each certificate type
  1548. */
  1549. if (s->s3.tmp.peer_cert_sigalgs == NULL
  1550. && s->s3.tmp.peer_sigalgs == NULL) {
  1551. const uint16_t *sent_sigs;
  1552. size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  1553. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1554. const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
  1555. size_t j;
  1556. if (lu == NULL)
  1557. continue;
  1558. /* Check default matches a type we sent */
  1559. for (j = 0; j < sent_sigslen; j++) {
  1560. if (lu->sigalg == sent_sigs[j]) {
  1561. s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN;
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. return 1;
  1567. }
  1568. if (!tls1_process_sigalgs(s)) {
  1569. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1570. return 0;
  1571. }
  1572. if (s->shared_sigalgs != NULL)
  1573. return 1;
  1574. /* Fatal error if no shared signature algorithms */
  1575. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1576. SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
  1577. return 0;
  1578. }
  1579. /*-
  1580. * Gets the ticket information supplied by the client if any.
  1581. *
  1582. * hello: The parsed ClientHello data
  1583. * ret: (output) on return, if a ticket was decrypted, then this is set to
  1584. * point to the resulting session.
  1585. */
  1586. SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
  1587. SSL_SESSION **ret)
  1588. {
  1589. size_t size;
  1590. RAW_EXTENSION *ticketext;
  1591. *ret = NULL;
  1592. s->ext.ticket_expected = 0;
  1593. /*
  1594. * If tickets disabled or not supported by the protocol version
  1595. * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
  1596. * resumption.
  1597. */
  1598. if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
  1599. return SSL_TICKET_NONE;
  1600. ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
  1601. if (!ticketext->present)
  1602. return SSL_TICKET_NONE;
  1603. size = PACKET_remaining(&ticketext->data);
  1604. return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
  1605. hello->session_id, hello->session_id_len, ret);
  1606. }
  1607. /*-
  1608. * tls_decrypt_ticket attempts to decrypt a session ticket.
  1609. *
  1610. * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are
  1611. * expecting a pre-shared key ciphersuite, in which case we have no use for
  1612. * session tickets and one will never be decrypted, nor will
  1613. * s->ext.ticket_expected be set to 1.
  1614. *
  1615. * Side effects:
  1616. * Sets s->ext.ticket_expected to 1 if the server will have to issue
  1617. * a new session ticket to the client because the client indicated support
  1618. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  1619. * a session ticket or we couldn't use the one it gave us, or if
  1620. * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
  1621. * Otherwise, s->ext.ticket_expected is set to 0.
  1622. *
  1623. * etick: points to the body of the session ticket extension.
  1624. * eticklen: the length of the session tickets extension.
  1625. * sess_id: points at the session ID.
  1626. * sesslen: the length of the session ID.
  1627. * psess: (output) on return, if a ticket was decrypted, then this is set to
  1628. * point to the resulting session.
  1629. */
  1630. SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
  1631. size_t eticklen, const unsigned char *sess_id,
  1632. size_t sesslen, SSL_SESSION **psess)
  1633. {
  1634. SSL_SESSION *sess = NULL;
  1635. unsigned char *sdec;
  1636. const unsigned char *p;
  1637. int slen, ivlen, renew_ticket = 0, declen;
  1638. SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;
  1639. size_t mlen;
  1640. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  1641. SSL_HMAC *hctx = NULL;
  1642. EVP_CIPHER_CTX *ctx = NULL;
  1643. SSL_CTX *tctx = s->session_ctx;
  1644. if (eticklen == 0) {
  1645. /*
  1646. * The client will accept a ticket but doesn't currently have
  1647. * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3
  1648. */
  1649. ret = SSL_TICKET_EMPTY;
  1650. goto end;
  1651. }
  1652. if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) {
  1653. /*
  1654. * Indicate that the ticket couldn't be decrypted rather than
  1655. * generating the session from ticket now, trigger
  1656. * abbreviated handshake based on external mechanism to
  1657. * calculate the master secret later.
  1658. */
  1659. ret = SSL_TICKET_NO_DECRYPT;
  1660. goto end;
  1661. }
  1662. /* Need at least keyname + iv */
  1663. if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) {
  1664. ret = SSL_TICKET_NO_DECRYPT;
  1665. goto end;
  1666. }
  1667. /* Initialize session ticket encryption and HMAC contexts */
  1668. hctx = ssl_hmac_new(tctx);
  1669. if (hctx == NULL) {
  1670. ret = SSL_TICKET_FATAL_ERR_MALLOC;
  1671. goto end;
  1672. }
  1673. ctx = EVP_CIPHER_CTX_new();
  1674. if (ctx == NULL) {
  1675. ret = SSL_TICKET_FATAL_ERR_MALLOC;
  1676. goto end;
  1677. }
  1678. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1679. if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
  1680. #else
  1681. if (tctx->ext.ticket_key_evp_cb != NULL)
  1682. #endif
  1683. {
  1684. unsigned char *nctick = (unsigned char *)etick;
  1685. int rv = 0;
  1686. if (tctx->ext.ticket_key_evp_cb != NULL)
  1687. rv = tctx->ext.ticket_key_evp_cb(s, nctick,
  1688. nctick + TLSEXT_KEYNAME_LENGTH,
  1689. ctx,
  1690. ssl_hmac_get0_EVP_MAC_CTX(hctx),
  1691. 0);
  1692. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1693. else if (tctx->ext.ticket_key_cb != NULL)
  1694. /* if 0 is returned, write an empty ticket */
  1695. rv = tctx->ext.ticket_key_cb(s, nctick,
  1696. nctick + TLSEXT_KEYNAME_LENGTH,
  1697. ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0);
  1698. #endif
  1699. if (rv < 0) {
  1700. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1701. goto end;
  1702. }
  1703. if (rv == 0) {
  1704. ret = SSL_TICKET_NO_DECRYPT;
  1705. goto end;
  1706. }
  1707. if (rv == 2)
  1708. renew_ticket = 1;
  1709. } else {
  1710. EVP_CIPHER *aes256cbc = NULL;
  1711. /* Check key name matches */
  1712. if (memcmp(etick, tctx->ext.tick_key_name,
  1713. TLSEXT_KEYNAME_LENGTH) != 0) {
  1714. ret = SSL_TICKET_NO_DECRYPT;
  1715. goto end;
  1716. }
  1717. aes256cbc = EVP_CIPHER_fetch(s->ctx->libctx, "AES-256-CBC",
  1718. s->ctx->propq);
  1719. if (aes256cbc == NULL
  1720. || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
  1721. sizeof(tctx->ext.secure->tick_hmac_key),
  1722. "SHA256") <= 0
  1723. || EVP_DecryptInit_ex(ctx, aes256cbc, NULL,
  1724. tctx->ext.secure->tick_aes_key,
  1725. etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
  1726. EVP_CIPHER_free(aes256cbc);
  1727. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1728. goto end;
  1729. }
  1730. EVP_CIPHER_free(aes256cbc);
  1731. if (SSL_IS_TLS13(s))
  1732. renew_ticket = 1;
  1733. }
  1734. /*
  1735. * Attempt to process session ticket, first conduct sanity and integrity
  1736. * checks on ticket.
  1737. */
  1738. mlen = ssl_hmac_size(hctx);
  1739. if (mlen == 0) {
  1740. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1741. goto end;
  1742. }
  1743. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  1744. if (ivlen < 0) {
  1745. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1746. goto end;
  1747. }
  1748. /* Sanity check ticket length: must exceed keyname + IV + HMAC */
  1749. if (eticklen <= TLSEXT_KEYNAME_LENGTH + ivlen + mlen) {
  1750. ret = SSL_TICKET_NO_DECRYPT;
  1751. goto end;
  1752. }
  1753. eticklen -= mlen;
  1754. /* Check HMAC of encrypted ticket */
  1755. if (ssl_hmac_update(hctx, etick, eticklen) <= 0
  1756. || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) {
  1757. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1758. goto end;
  1759. }
  1760. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
  1761. ret = SSL_TICKET_NO_DECRYPT;
  1762. goto end;
  1763. }
  1764. /* Attempt to decrypt session data */
  1765. /* Move p after IV to start of encrypted ticket, update length */
  1766. p = etick + TLSEXT_KEYNAME_LENGTH + ivlen;
  1767. eticklen -= TLSEXT_KEYNAME_LENGTH + ivlen;
  1768. sdec = OPENSSL_malloc(eticklen);
  1769. if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
  1770. (int)eticklen) <= 0) {
  1771. OPENSSL_free(sdec);
  1772. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1773. goto end;
  1774. }
  1775. if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
  1776. OPENSSL_free(sdec);
  1777. ret = SSL_TICKET_NO_DECRYPT;
  1778. goto end;
  1779. }
  1780. slen += declen;
  1781. p = sdec;
  1782. sess = d2i_SSL_SESSION(NULL, &p, slen);
  1783. slen -= p - sdec;
  1784. OPENSSL_free(sdec);
  1785. if (sess) {
  1786. /* Some additional consistency checks */
  1787. if (slen != 0) {
  1788. SSL_SESSION_free(sess);
  1789. sess = NULL;
  1790. ret = SSL_TICKET_NO_DECRYPT;
  1791. goto end;
  1792. }
  1793. /*
  1794. * The session ID, if non-empty, is used by some clients to detect
  1795. * that the ticket has been accepted. So we copy it to the session
  1796. * structure. If it is empty set length to zero as required by
  1797. * standard.
  1798. */
  1799. if (sesslen) {
  1800. memcpy(sess->session_id, sess_id, sesslen);
  1801. sess->session_id_length = sesslen;
  1802. }
  1803. if (renew_ticket)
  1804. ret = SSL_TICKET_SUCCESS_RENEW;
  1805. else
  1806. ret = SSL_TICKET_SUCCESS;
  1807. goto end;
  1808. }
  1809. ERR_clear_error();
  1810. /*
  1811. * For session parse failure, indicate that we need to send a new ticket.
  1812. */
  1813. ret = SSL_TICKET_NO_DECRYPT;
  1814. end:
  1815. EVP_CIPHER_CTX_free(ctx);
  1816. ssl_hmac_free(hctx);
  1817. /*
  1818. * If set, the decrypt_ticket_cb() is called unless a fatal error was
  1819. * detected above. The callback is responsible for checking |ret| before it
  1820. * performs any action
  1821. */
  1822. if (s->session_ctx->decrypt_ticket_cb != NULL
  1823. && (ret == SSL_TICKET_EMPTY
  1824. || ret == SSL_TICKET_NO_DECRYPT
  1825. || ret == SSL_TICKET_SUCCESS
  1826. || ret == SSL_TICKET_SUCCESS_RENEW)) {
  1827. size_t keyname_len = eticklen;
  1828. int retcb;
  1829. if (keyname_len > TLSEXT_KEYNAME_LENGTH)
  1830. keyname_len = TLSEXT_KEYNAME_LENGTH;
  1831. retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len,
  1832. ret,
  1833. s->session_ctx->ticket_cb_data);
  1834. switch (retcb) {
  1835. case SSL_TICKET_RETURN_ABORT:
  1836. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1837. break;
  1838. case SSL_TICKET_RETURN_IGNORE:
  1839. ret = SSL_TICKET_NONE;
  1840. SSL_SESSION_free(sess);
  1841. sess = NULL;
  1842. break;
  1843. case SSL_TICKET_RETURN_IGNORE_RENEW:
  1844. if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT)
  1845. ret = SSL_TICKET_NO_DECRYPT;
  1846. /* else the value of |ret| will already do the right thing */
  1847. SSL_SESSION_free(sess);
  1848. sess = NULL;
  1849. break;
  1850. case SSL_TICKET_RETURN_USE:
  1851. case SSL_TICKET_RETURN_USE_RENEW:
  1852. if (ret != SSL_TICKET_SUCCESS
  1853. && ret != SSL_TICKET_SUCCESS_RENEW)
  1854. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1855. else if (retcb == SSL_TICKET_RETURN_USE)
  1856. ret = SSL_TICKET_SUCCESS;
  1857. else
  1858. ret = SSL_TICKET_SUCCESS_RENEW;
  1859. break;
  1860. default:
  1861. ret = SSL_TICKET_FATAL_ERR_OTHER;
  1862. }
  1863. }
  1864. if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) {
  1865. switch (ret) {
  1866. case SSL_TICKET_NO_DECRYPT:
  1867. case SSL_TICKET_SUCCESS_RENEW:
  1868. case SSL_TICKET_EMPTY:
  1869. s->ext.ticket_expected = 1;
  1870. }
  1871. }
  1872. *psess = sess;
  1873. return ret;
  1874. }
  1875. /* Check to see if a signature algorithm is allowed */
  1876. static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu)
  1877. {
  1878. unsigned char sigalgstr[2];
  1879. int secbits;
  1880. if (lu == NULL || !lu->enabled)
  1881. return 0;
  1882. /* DSA is not allowed in TLS 1.3 */
  1883. if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
  1884. return 0;
  1885. /*
  1886. * At some point we should fully axe DSA/etc. in ClientHello as per TLS 1.3
  1887. * spec
  1888. */
  1889. if (!s->server && !SSL_IS_DTLS(s) && s->s3.tmp.min_ver >= TLS1_3_VERSION
  1890. && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
  1891. || lu->hash_idx == SSL_MD_MD5_IDX
  1892. || lu->hash_idx == SSL_MD_SHA224_IDX))
  1893. return 0;
  1894. /* See if public key algorithm allowed */
  1895. if (ssl_cert_is_disabled(s->ctx, lu->sig_idx))
  1896. return 0;
  1897. if (lu->sig == NID_id_GostR3410_2012_256
  1898. || lu->sig == NID_id_GostR3410_2012_512
  1899. || lu->sig == NID_id_GostR3410_2001) {
  1900. /* We never allow GOST sig algs on the server with TLSv1.3 */
  1901. if (s->server && SSL_IS_TLS13(s))
  1902. return 0;
  1903. if (!s->server
  1904. && s->method->version == TLS_ANY_VERSION
  1905. && s->s3.tmp.max_ver >= TLS1_3_VERSION) {
  1906. int i, num;
  1907. STACK_OF(SSL_CIPHER) *sk;
  1908. /*
  1909. * We're a client that could negotiate TLSv1.3. We only allow GOST
  1910. * sig algs if we could negotiate TLSv1.2 or below and we have GOST
  1911. * ciphersuites enabled.
  1912. */
  1913. if (s->s3.tmp.min_ver >= TLS1_3_VERSION)
  1914. return 0;
  1915. sk = SSL_get_ciphers(s);
  1916. num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0;
  1917. for (i = 0; i < num; i++) {
  1918. const SSL_CIPHER *c;
  1919. c = sk_SSL_CIPHER_value(sk, i);
  1920. /* Skip disabled ciphers */
  1921. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  1922. continue;
  1923. if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0)
  1924. break;
  1925. }
  1926. if (i == num)
  1927. return 0;
  1928. }
  1929. }
  1930. /* Finally see if security callback allows it */
  1931. secbits = sigalg_security_bits(s->ctx, lu);
  1932. sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
  1933. sigalgstr[1] = lu->sigalg & 0xff;
  1934. return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
  1935. }
  1936. /*
  1937. * Get a mask of disabled public key algorithms based on supported signature
  1938. * algorithms. For example if no signature algorithm supports RSA then RSA is
  1939. * disabled.
  1940. */
  1941. void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
  1942. {
  1943. const uint16_t *sigalgs;
  1944. size_t i, sigalgslen;
  1945. uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
  1946. /*
  1947. * Go through all signature algorithms seeing if we support any
  1948. * in disabled_mask.
  1949. */
  1950. sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
  1951. for (i = 0; i < sigalgslen; i++, sigalgs++) {
  1952. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *sigalgs);
  1953. const SSL_CERT_LOOKUP *clu;
  1954. if (lu == NULL)
  1955. continue;
  1956. clu = ssl_cert_lookup_by_idx(lu->sig_idx);
  1957. if (clu == NULL)
  1958. continue;
  1959. /* If algorithm is disabled see if we can enable it */
  1960. if ((clu->amask & disabled_mask) != 0
  1961. && tls12_sigalg_allowed(s, op, lu))
  1962. disabled_mask &= ~clu->amask;
  1963. }
  1964. *pmask_a |= disabled_mask;
  1965. }
  1966. int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
  1967. const uint16_t *psig, size_t psiglen)
  1968. {
  1969. size_t i;
  1970. int rv = 0;
  1971. for (i = 0; i < psiglen; i++, psig++) {
  1972. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *psig);
  1973. if (lu == NULL
  1974. || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
  1975. continue;
  1976. if (!WPACKET_put_bytes_u16(pkt, *psig))
  1977. return 0;
  1978. /*
  1979. * If TLS 1.3 must have at least one valid TLS 1.3 message
  1980. * signing algorithm: i.e. neither RSA nor SHA1/SHA224
  1981. */
  1982. if (rv == 0 && (!SSL_IS_TLS13(s)
  1983. || (lu->sig != EVP_PKEY_RSA
  1984. && lu->hash != NID_sha1
  1985. && lu->hash != NID_sha224)))
  1986. rv = 1;
  1987. }
  1988. if (rv == 0)
  1989. ERR_raise(ERR_LIB_SSL, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  1990. return rv;
  1991. }
  1992. /* Given preference and allowed sigalgs set shared sigalgs */
  1993. static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
  1994. const uint16_t *pref, size_t preflen,
  1995. const uint16_t *allow, size_t allowlen)
  1996. {
  1997. const uint16_t *ptmp, *atmp;
  1998. size_t i, j, nmatch = 0;
  1999. for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
  2000. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *ptmp);
  2001. /* Skip disabled hashes or signature algorithms */
  2002. if (lu == NULL
  2003. || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
  2004. continue;
  2005. for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
  2006. if (*ptmp == *atmp) {
  2007. nmatch++;
  2008. if (shsig)
  2009. *shsig++ = lu;
  2010. break;
  2011. }
  2012. }
  2013. }
  2014. return nmatch;
  2015. }
  2016. /* Set shared signature algorithms for SSL structures */
  2017. static int tls1_set_shared_sigalgs(SSL *s)
  2018. {
  2019. const uint16_t *pref, *allow, *conf;
  2020. size_t preflen, allowlen, conflen;
  2021. size_t nmatch;
  2022. const SIGALG_LOOKUP **salgs = NULL;
  2023. CERT *c = s->cert;
  2024. unsigned int is_suiteb = tls1_suiteb(s);
  2025. OPENSSL_free(s->shared_sigalgs);
  2026. s->shared_sigalgs = NULL;
  2027. s->shared_sigalgslen = 0;
  2028. /* If client use client signature algorithms if not NULL */
  2029. if (!s->server && c->client_sigalgs && !is_suiteb) {
  2030. conf = c->client_sigalgs;
  2031. conflen = c->client_sigalgslen;
  2032. } else if (c->conf_sigalgs && !is_suiteb) {
  2033. conf = c->conf_sigalgs;
  2034. conflen = c->conf_sigalgslen;
  2035. } else
  2036. conflen = tls12_get_psigalgs(s, 0, &conf);
  2037. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
  2038. pref = conf;
  2039. preflen = conflen;
  2040. allow = s->s3.tmp.peer_sigalgs;
  2041. allowlen = s->s3.tmp.peer_sigalgslen;
  2042. } else {
  2043. allow = conf;
  2044. allowlen = conflen;
  2045. pref = s->s3.tmp.peer_sigalgs;
  2046. preflen = s->s3.tmp.peer_sigalgslen;
  2047. }
  2048. nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
  2049. if (nmatch) {
  2050. if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) {
  2051. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2052. return 0;
  2053. }
  2054. nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
  2055. } else {
  2056. salgs = NULL;
  2057. }
  2058. s->shared_sigalgs = salgs;
  2059. s->shared_sigalgslen = nmatch;
  2060. return 1;
  2061. }
  2062. int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
  2063. {
  2064. unsigned int stmp;
  2065. size_t size, i;
  2066. uint16_t *buf;
  2067. size = PACKET_remaining(pkt);
  2068. /* Invalid data length */
  2069. if (size == 0 || (size & 1) != 0)
  2070. return 0;
  2071. size >>= 1;
  2072. if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) {
  2073. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2074. return 0;
  2075. }
  2076. for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
  2077. buf[i] = stmp;
  2078. if (i != size) {
  2079. OPENSSL_free(buf);
  2080. return 0;
  2081. }
  2082. OPENSSL_free(*pdest);
  2083. *pdest = buf;
  2084. *pdestlen = size;
  2085. return 1;
  2086. }
  2087. int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert)
  2088. {
  2089. /* Extension ignored for inappropriate versions */
  2090. if (!SSL_USE_SIGALGS(s))
  2091. return 1;
  2092. /* Should never happen */
  2093. if (s->cert == NULL)
  2094. return 0;
  2095. if (cert)
  2096. return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs,
  2097. &s->s3.tmp.peer_cert_sigalgslen);
  2098. else
  2099. return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs,
  2100. &s->s3.tmp.peer_sigalgslen);
  2101. }
  2102. /* Set preferred digest for each key type */
  2103. int tls1_process_sigalgs(SSL *s)
  2104. {
  2105. size_t i;
  2106. uint32_t *pvalid = s->s3.tmp.valid_flags;
  2107. if (!tls1_set_shared_sigalgs(s))
  2108. return 0;
  2109. for (i = 0; i < SSL_PKEY_NUM; i++)
  2110. pvalid[i] = 0;
  2111. for (i = 0; i < s->shared_sigalgslen; i++) {
  2112. const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i];
  2113. int idx = sigptr->sig_idx;
  2114. /* Ignore PKCS1 based sig algs in TLSv1.3 */
  2115. if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
  2116. continue;
  2117. /* If not disabled indicate we can explicitly sign */
  2118. if (pvalid[idx] == 0 && !ssl_cert_is_disabled(s->ctx, idx))
  2119. pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  2120. }
  2121. return 1;
  2122. }
  2123. int SSL_get_sigalgs(SSL *s, int idx,
  2124. int *psign, int *phash, int *psignhash,
  2125. unsigned char *rsig, unsigned char *rhash)
  2126. {
  2127. uint16_t *psig = s->s3.tmp.peer_sigalgs;
  2128. size_t numsigalgs = s->s3.tmp.peer_sigalgslen;
  2129. if (psig == NULL || numsigalgs > INT_MAX)
  2130. return 0;
  2131. if (idx >= 0) {
  2132. const SIGALG_LOOKUP *lu;
  2133. if (idx >= (int)numsigalgs)
  2134. return 0;
  2135. psig += idx;
  2136. if (rhash != NULL)
  2137. *rhash = (unsigned char)((*psig >> 8) & 0xff);
  2138. if (rsig != NULL)
  2139. *rsig = (unsigned char)(*psig & 0xff);
  2140. lu = tls1_lookup_sigalg(s, *psig);
  2141. if (psign != NULL)
  2142. *psign = lu != NULL ? lu->sig : NID_undef;
  2143. if (phash != NULL)
  2144. *phash = lu != NULL ? lu->hash : NID_undef;
  2145. if (psignhash != NULL)
  2146. *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
  2147. }
  2148. return (int)numsigalgs;
  2149. }
  2150. int SSL_get_shared_sigalgs(SSL *s, int idx,
  2151. int *psign, int *phash, int *psignhash,
  2152. unsigned char *rsig, unsigned char *rhash)
  2153. {
  2154. const SIGALG_LOOKUP *shsigalgs;
  2155. if (s->shared_sigalgs == NULL
  2156. || idx < 0
  2157. || idx >= (int)s->shared_sigalgslen
  2158. || s->shared_sigalgslen > INT_MAX)
  2159. return 0;
  2160. shsigalgs = s->shared_sigalgs[idx];
  2161. if (phash != NULL)
  2162. *phash = shsigalgs->hash;
  2163. if (psign != NULL)
  2164. *psign = shsigalgs->sig;
  2165. if (psignhash != NULL)
  2166. *psignhash = shsigalgs->sigandhash;
  2167. if (rsig != NULL)
  2168. *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
  2169. if (rhash != NULL)
  2170. *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
  2171. return (int)s->shared_sigalgslen;
  2172. }
  2173. /* Maximum possible number of unique entries in sigalgs array */
  2174. #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
  2175. typedef struct {
  2176. size_t sigalgcnt;
  2177. /* TLSEXT_SIGALG_XXX values */
  2178. uint16_t sigalgs[TLS_MAX_SIGALGCNT];
  2179. } sig_cb_st;
  2180. static void get_sigorhash(int *psig, int *phash, const char *str)
  2181. {
  2182. if (strcmp(str, "RSA") == 0) {
  2183. *psig = EVP_PKEY_RSA;
  2184. } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
  2185. *psig = EVP_PKEY_RSA_PSS;
  2186. } else if (strcmp(str, "DSA") == 0) {
  2187. *psig = EVP_PKEY_DSA;
  2188. } else if (strcmp(str, "ECDSA") == 0) {
  2189. *psig = EVP_PKEY_EC;
  2190. } else {
  2191. *phash = OBJ_sn2nid(str);
  2192. if (*phash == NID_undef)
  2193. *phash = OBJ_ln2nid(str);
  2194. }
  2195. }
  2196. /* Maximum length of a signature algorithm string component */
  2197. #define TLS_MAX_SIGSTRING_LEN 40
  2198. static int sig_cb(const char *elem, int len, void *arg)
  2199. {
  2200. sig_cb_st *sarg = arg;
  2201. size_t i;
  2202. const SIGALG_LOOKUP *s;
  2203. char etmp[TLS_MAX_SIGSTRING_LEN], *p;
  2204. int sig_alg = NID_undef, hash_alg = NID_undef;
  2205. if (elem == NULL)
  2206. return 0;
  2207. if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
  2208. return 0;
  2209. if (len > (int)(sizeof(etmp) - 1))
  2210. return 0;
  2211. memcpy(etmp, elem, len);
  2212. etmp[len] = 0;
  2213. p = strchr(etmp, '+');
  2214. /*
  2215. * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
  2216. * if there's no '+' in the provided name, look for the new-style combined
  2217. * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
  2218. * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
  2219. * rsa_pss_rsae_* that differ only by public key OID; in such cases
  2220. * we will pick the _rsae_ variant, by virtue of them appearing earlier
  2221. * in the table.
  2222. */
  2223. if (p == NULL) {
  2224. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  2225. i++, s++) {
  2226. if (s->name != NULL && strcmp(etmp, s->name) == 0) {
  2227. sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
  2228. break;
  2229. }
  2230. }
  2231. if (i == OSSL_NELEM(sigalg_lookup_tbl))
  2232. return 0;
  2233. } else {
  2234. *p = 0;
  2235. p++;
  2236. if (*p == 0)
  2237. return 0;
  2238. get_sigorhash(&sig_alg, &hash_alg, etmp);
  2239. get_sigorhash(&sig_alg, &hash_alg, p);
  2240. if (sig_alg == NID_undef || hash_alg == NID_undef)
  2241. return 0;
  2242. for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
  2243. i++, s++) {
  2244. if (s->hash == hash_alg && s->sig == sig_alg) {
  2245. sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
  2246. break;
  2247. }
  2248. }
  2249. if (i == OSSL_NELEM(sigalg_lookup_tbl))
  2250. return 0;
  2251. }
  2252. /* Reject duplicates */
  2253. for (i = 0; i < sarg->sigalgcnt - 1; i++) {
  2254. if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) {
  2255. sarg->sigalgcnt--;
  2256. return 0;
  2257. }
  2258. }
  2259. return 1;
  2260. }
  2261. /*
  2262. * Set supported signature algorithms based on a colon separated list of the
  2263. * form sig+hash e.g. RSA+SHA512:DSA+SHA512
  2264. */
  2265. int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
  2266. {
  2267. sig_cb_st sig;
  2268. sig.sigalgcnt = 0;
  2269. if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
  2270. return 0;
  2271. if (c == NULL)
  2272. return 1;
  2273. return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
  2274. }
  2275. int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
  2276. int client)
  2277. {
  2278. uint16_t *sigalgs;
  2279. if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) {
  2280. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2281. return 0;
  2282. }
  2283. memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));
  2284. if (client) {
  2285. OPENSSL_free(c->client_sigalgs);
  2286. c->client_sigalgs = sigalgs;
  2287. c->client_sigalgslen = salglen;
  2288. } else {
  2289. OPENSSL_free(c->conf_sigalgs);
  2290. c->conf_sigalgs = sigalgs;
  2291. c->conf_sigalgslen = salglen;
  2292. }
  2293. return 1;
  2294. }
  2295. int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
  2296. {
  2297. uint16_t *sigalgs, *sptr;
  2298. size_t i;
  2299. if (salglen & 1)
  2300. return 0;
  2301. if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) {
  2302. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2303. return 0;
  2304. }
  2305. for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
  2306. size_t j;
  2307. const SIGALG_LOOKUP *curr;
  2308. int md_id = *psig_nids++;
  2309. int sig_id = *psig_nids++;
  2310. for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
  2311. j++, curr++) {
  2312. if (curr->hash == md_id && curr->sig == sig_id) {
  2313. *sptr++ = curr->sigalg;
  2314. break;
  2315. }
  2316. }
  2317. if (j == OSSL_NELEM(sigalg_lookup_tbl))
  2318. goto err;
  2319. }
  2320. if (client) {
  2321. OPENSSL_free(c->client_sigalgs);
  2322. c->client_sigalgs = sigalgs;
  2323. c->client_sigalgslen = salglen / 2;
  2324. } else {
  2325. OPENSSL_free(c->conf_sigalgs);
  2326. c->conf_sigalgs = sigalgs;
  2327. c->conf_sigalgslen = salglen / 2;
  2328. }
  2329. return 1;
  2330. err:
  2331. OPENSSL_free(sigalgs);
  2332. return 0;
  2333. }
  2334. static int tls1_check_sig_alg(SSL *s, X509 *x, int default_nid)
  2335. {
  2336. int sig_nid, use_pc_sigalgs = 0;
  2337. size_t i;
  2338. const SIGALG_LOOKUP *sigalg;
  2339. size_t sigalgslen;
  2340. if (default_nid == -1)
  2341. return 1;
  2342. sig_nid = X509_get_signature_nid(x);
  2343. if (default_nid)
  2344. return sig_nid == default_nid ? 1 : 0;
  2345. if (SSL_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) {
  2346. /*
  2347. * If we're in TLSv1.3 then we only get here if we're checking the
  2348. * chain. If the peer has specified peer_cert_sigalgs then we use them
  2349. * otherwise we default to normal sigalgs.
  2350. */
  2351. sigalgslen = s->s3.tmp.peer_cert_sigalgslen;
  2352. use_pc_sigalgs = 1;
  2353. } else {
  2354. sigalgslen = s->shared_sigalgslen;
  2355. }
  2356. for (i = 0; i < sigalgslen; i++) {
  2357. sigalg = use_pc_sigalgs
  2358. ? tls1_lookup_sigalg(s, s->s3.tmp.peer_cert_sigalgs[i])
  2359. : s->shared_sigalgs[i];
  2360. if (sigalg != NULL && sig_nid == sigalg->sigandhash)
  2361. return 1;
  2362. }
  2363. return 0;
  2364. }
  2365. /* Check to see if a certificate issuer name matches list of CA names */
  2366. static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
  2367. {
  2368. const X509_NAME *nm;
  2369. int i;
  2370. nm = X509_get_issuer_name(x);
  2371. for (i = 0; i < sk_X509_NAME_num(names); i++) {
  2372. if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
  2373. return 1;
  2374. }
  2375. return 0;
  2376. }
  2377. /*
  2378. * Check certificate chain is consistent with TLS extensions and is usable by
  2379. * server. This servers two purposes: it allows users to check chains before
  2380. * passing them to the server and it allows the server to check chains before
  2381. * attempting to use them.
  2382. */
  2383. /* Flags which need to be set for a certificate when strict mode not set */
  2384. #define CERT_PKEY_VALID_FLAGS \
  2385. (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
  2386. /* Strict mode flags */
  2387. #define CERT_PKEY_STRICT_FLAGS \
  2388. (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
  2389. | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
  2390. int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
  2391. int idx)
  2392. {
  2393. int i;
  2394. int rv = 0;
  2395. int check_flags = 0, strict_mode;
  2396. CERT_PKEY *cpk = NULL;
  2397. CERT *c = s->cert;
  2398. uint32_t *pvalid;
  2399. unsigned int suiteb_flags = tls1_suiteb(s);
  2400. /* idx == -1 means checking server chains */
  2401. if (idx != -1) {
  2402. /* idx == -2 means checking client certificate chains */
  2403. if (idx == -2) {
  2404. cpk = c->key;
  2405. idx = (int)(cpk - c->pkeys);
  2406. } else
  2407. cpk = c->pkeys + idx;
  2408. pvalid = s->s3.tmp.valid_flags + idx;
  2409. x = cpk->x509;
  2410. pk = cpk->privatekey;
  2411. chain = cpk->chain;
  2412. strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
  2413. /* If no cert or key, forget it */
  2414. if (!x || !pk)
  2415. goto end;
  2416. } else {
  2417. size_t certidx;
  2418. if (!x || !pk)
  2419. return 0;
  2420. if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
  2421. return 0;
  2422. idx = certidx;
  2423. pvalid = s->s3.tmp.valid_flags + idx;
  2424. if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
  2425. check_flags = CERT_PKEY_STRICT_FLAGS;
  2426. else
  2427. check_flags = CERT_PKEY_VALID_FLAGS;
  2428. strict_mode = 1;
  2429. }
  2430. if (suiteb_flags) {
  2431. int ok;
  2432. if (check_flags)
  2433. check_flags |= CERT_PKEY_SUITEB;
  2434. ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
  2435. if (ok == X509_V_OK)
  2436. rv |= CERT_PKEY_SUITEB;
  2437. else if (!check_flags)
  2438. goto end;
  2439. }
  2440. /*
  2441. * Check all signature algorithms are consistent with signature
  2442. * algorithms extension if TLS 1.2 or later and strict mode.
  2443. */
  2444. if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
  2445. int default_nid;
  2446. int rsign = 0;
  2447. if (s->s3.tmp.peer_cert_sigalgs != NULL
  2448. || s->s3.tmp.peer_sigalgs != NULL) {
  2449. default_nid = 0;
  2450. /* If no sigalgs extension use defaults from RFC5246 */
  2451. } else {
  2452. switch (idx) {
  2453. case SSL_PKEY_RSA:
  2454. rsign = EVP_PKEY_RSA;
  2455. default_nid = NID_sha1WithRSAEncryption;
  2456. break;
  2457. case SSL_PKEY_DSA_SIGN:
  2458. rsign = EVP_PKEY_DSA;
  2459. default_nid = NID_dsaWithSHA1;
  2460. break;
  2461. case SSL_PKEY_ECC:
  2462. rsign = EVP_PKEY_EC;
  2463. default_nid = NID_ecdsa_with_SHA1;
  2464. break;
  2465. case SSL_PKEY_GOST01:
  2466. rsign = NID_id_GostR3410_2001;
  2467. default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
  2468. break;
  2469. case SSL_PKEY_GOST12_256:
  2470. rsign = NID_id_GostR3410_2012_256;
  2471. default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
  2472. break;
  2473. case SSL_PKEY_GOST12_512:
  2474. rsign = NID_id_GostR3410_2012_512;
  2475. default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
  2476. break;
  2477. default:
  2478. default_nid = -1;
  2479. break;
  2480. }
  2481. }
  2482. /*
  2483. * If peer sent no signature algorithms extension and we have set
  2484. * preferred signature algorithms check we support sha1.
  2485. */
  2486. if (default_nid > 0 && c->conf_sigalgs) {
  2487. size_t j;
  2488. const uint16_t *p = c->conf_sigalgs;
  2489. for (j = 0; j < c->conf_sigalgslen; j++, p++) {
  2490. const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *p);
  2491. if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
  2492. break;
  2493. }
  2494. if (j == c->conf_sigalgslen) {
  2495. if (check_flags)
  2496. goto skip_sigs;
  2497. else
  2498. goto end;
  2499. }
  2500. }
  2501. /* Check signature algorithm of each cert in chain */
  2502. if (SSL_IS_TLS13(s)) {
  2503. /*
  2504. * We only get here if the application has called SSL_check_chain(),
  2505. * so check_flags is always set.
  2506. */
  2507. if (find_sig_alg(s, x, pk) != NULL)
  2508. rv |= CERT_PKEY_EE_SIGNATURE;
  2509. } else if (!tls1_check_sig_alg(s, x, default_nid)) {
  2510. if (!check_flags)
  2511. goto end;
  2512. } else
  2513. rv |= CERT_PKEY_EE_SIGNATURE;
  2514. rv |= CERT_PKEY_CA_SIGNATURE;
  2515. for (i = 0; i < sk_X509_num(chain); i++) {
  2516. if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) {
  2517. if (check_flags) {
  2518. rv &= ~CERT_PKEY_CA_SIGNATURE;
  2519. break;
  2520. } else
  2521. goto end;
  2522. }
  2523. }
  2524. }
  2525. /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
  2526. else if (check_flags)
  2527. rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
  2528. skip_sigs:
  2529. /* Check cert parameters are consistent */
  2530. if (tls1_check_cert_param(s, x, 1))
  2531. rv |= CERT_PKEY_EE_PARAM;
  2532. else if (!check_flags)
  2533. goto end;
  2534. if (!s->server)
  2535. rv |= CERT_PKEY_CA_PARAM;
  2536. /* In strict mode check rest of chain too */
  2537. else if (strict_mode) {
  2538. rv |= CERT_PKEY_CA_PARAM;
  2539. for (i = 0; i < sk_X509_num(chain); i++) {
  2540. X509 *ca = sk_X509_value(chain, i);
  2541. if (!tls1_check_cert_param(s, ca, 0)) {
  2542. if (check_flags) {
  2543. rv &= ~CERT_PKEY_CA_PARAM;
  2544. break;
  2545. } else
  2546. goto end;
  2547. }
  2548. }
  2549. }
  2550. if (!s->server && strict_mode) {
  2551. STACK_OF(X509_NAME) *ca_dn;
  2552. int check_type = 0;
  2553. if (EVP_PKEY_is_a(pk, "RSA"))
  2554. check_type = TLS_CT_RSA_SIGN;
  2555. else if (EVP_PKEY_is_a(pk, "DSA"))
  2556. check_type = TLS_CT_DSS_SIGN;
  2557. else if (EVP_PKEY_is_a(pk, "EC"))
  2558. check_type = TLS_CT_ECDSA_SIGN;
  2559. if (check_type) {
  2560. const uint8_t *ctypes = s->s3.tmp.ctype;
  2561. size_t j;
  2562. for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) {
  2563. if (*ctypes == check_type) {
  2564. rv |= CERT_PKEY_CERT_TYPE;
  2565. break;
  2566. }
  2567. }
  2568. if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
  2569. goto end;
  2570. } else {
  2571. rv |= CERT_PKEY_CERT_TYPE;
  2572. }
  2573. ca_dn = s->s3.tmp.peer_ca_names;
  2574. if (ca_dn == NULL
  2575. || sk_X509_NAME_num(ca_dn) == 0
  2576. || ssl_check_ca_name(ca_dn, x))
  2577. rv |= CERT_PKEY_ISSUER_NAME;
  2578. else
  2579. for (i = 0; i < sk_X509_num(chain); i++) {
  2580. X509 *xtmp = sk_X509_value(chain, i);
  2581. if (ssl_check_ca_name(ca_dn, xtmp)) {
  2582. rv |= CERT_PKEY_ISSUER_NAME;
  2583. break;
  2584. }
  2585. }
  2586. if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
  2587. goto end;
  2588. } else
  2589. rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
  2590. if (!check_flags || (rv & check_flags) == check_flags)
  2591. rv |= CERT_PKEY_VALID;
  2592. end:
  2593. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  2594. rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
  2595. else
  2596. rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
  2597. /*
  2598. * When checking a CERT_PKEY structure all flags are irrelevant if the
  2599. * chain is invalid.
  2600. */
  2601. if (!check_flags) {
  2602. if (rv & CERT_PKEY_VALID) {
  2603. *pvalid = rv;
  2604. } else {
  2605. /* Preserve sign and explicit sign flag, clear rest */
  2606. *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  2607. return 0;
  2608. }
  2609. }
  2610. return rv;
  2611. }
  2612. /* Set validity of certificates in an SSL structure */
  2613. void tls1_set_cert_validity(SSL *s)
  2614. {
  2615. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
  2616. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
  2617. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
  2618. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
  2619. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
  2620. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
  2621. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
  2622. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
  2623. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448);
  2624. }
  2625. /* User level utility function to check a chain is suitable */
  2626. int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
  2627. {
  2628. return tls1_check_chain(s, x, pk, chain, -1);
  2629. }
  2630. EVP_PKEY *ssl_get_auto_dh(SSL *s)
  2631. {
  2632. EVP_PKEY *dhp = NULL;
  2633. BIGNUM *p;
  2634. int dh_secbits = 80, sec_level_bits;
  2635. EVP_PKEY_CTX *pctx = NULL;
  2636. OSSL_PARAM_BLD *tmpl = NULL;
  2637. OSSL_PARAM *params = NULL;
  2638. if (s->cert->dh_tmp_auto != 2) {
  2639. if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
  2640. if (s->s3.tmp.new_cipher->strength_bits == 256)
  2641. dh_secbits = 128;
  2642. else
  2643. dh_secbits = 80;
  2644. } else {
  2645. if (s->s3.tmp.cert == NULL)
  2646. return NULL;
  2647. dh_secbits = EVP_PKEY_get_security_bits(s->s3.tmp.cert->privatekey);
  2648. }
  2649. }
  2650. /* Do not pick a prime that is too weak for the current security level */
  2651. sec_level_bits = ssl_get_security_level_bits(s, NULL, NULL);
  2652. if (dh_secbits < sec_level_bits)
  2653. dh_secbits = sec_level_bits;
  2654. if (dh_secbits >= 192)
  2655. p = BN_get_rfc3526_prime_8192(NULL);
  2656. else if (dh_secbits >= 152)
  2657. p = BN_get_rfc3526_prime_4096(NULL);
  2658. else if (dh_secbits >= 128)
  2659. p = BN_get_rfc3526_prime_3072(NULL);
  2660. else if (dh_secbits >= 112)
  2661. p = BN_get_rfc3526_prime_2048(NULL);
  2662. else
  2663. p = BN_get_rfc2409_prime_1024(NULL);
  2664. if (p == NULL)
  2665. goto err;
  2666. pctx = EVP_PKEY_CTX_new_from_name(s->ctx->libctx, "DH", s->ctx->propq);
  2667. if (pctx == NULL
  2668. || EVP_PKEY_fromdata_init(pctx) != 1)
  2669. goto err;
  2670. tmpl = OSSL_PARAM_BLD_new();
  2671. if (tmpl == NULL
  2672. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
  2673. || !OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2))
  2674. goto err;
  2675. params = OSSL_PARAM_BLD_to_param(tmpl);
  2676. if (params == NULL
  2677. || EVP_PKEY_fromdata(pctx, &dhp, EVP_PKEY_KEY_PARAMETERS, params) != 1)
  2678. goto err;
  2679. err:
  2680. OSSL_PARAM_free(params);
  2681. OSSL_PARAM_BLD_free(tmpl);
  2682. EVP_PKEY_CTX_free(pctx);
  2683. BN_free(p);
  2684. return dhp;
  2685. }
  2686. static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
  2687. {
  2688. int secbits = -1;
  2689. EVP_PKEY *pkey = X509_get0_pubkey(x);
  2690. if (pkey) {
  2691. /*
  2692. * If no parameters this will return -1 and fail using the default
  2693. * security callback for any non-zero security level. This will
  2694. * reject keys which omit parameters but this only affects DSA and
  2695. * omission of parameters is never (?) done in practice.
  2696. */
  2697. secbits = EVP_PKEY_get_security_bits(pkey);
  2698. }
  2699. if (s)
  2700. return ssl_security(s, op, secbits, 0, x);
  2701. else
  2702. return ssl_ctx_security(ctx, op, secbits, 0, x);
  2703. }
  2704. static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
  2705. {
  2706. /* Lookup signature algorithm digest */
  2707. int secbits, nid, pknid;
  2708. /* Don't check signature if self signed */
  2709. if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
  2710. return 1;
  2711. if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
  2712. secbits = -1;
  2713. /* If digest NID not defined use signature NID */
  2714. if (nid == NID_undef)
  2715. nid = pknid;
  2716. if (s)
  2717. return ssl_security(s, op, secbits, nid, x);
  2718. else
  2719. return ssl_ctx_security(ctx, op, secbits, nid, x);
  2720. }
  2721. int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
  2722. {
  2723. if (vfy)
  2724. vfy = SSL_SECOP_PEER;
  2725. if (is_ee) {
  2726. if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
  2727. return SSL_R_EE_KEY_TOO_SMALL;
  2728. } else {
  2729. if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
  2730. return SSL_R_CA_KEY_TOO_SMALL;
  2731. }
  2732. if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
  2733. return SSL_R_CA_MD_TOO_WEAK;
  2734. return 1;
  2735. }
  2736. /*
  2737. * Check security of a chain, if |sk| includes the end entity certificate then
  2738. * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
  2739. * one to the peer. Return values: 1 if ok otherwise error code to use
  2740. */
  2741. int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
  2742. {
  2743. int rv, start_idx, i;
  2744. if (x == NULL) {
  2745. x = sk_X509_value(sk, 0);
  2746. if (x == NULL)
  2747. return ERR_R_INTERNAL_ERROR;
  2748. start_idx = 1;
  2749. } else
  2750. start_idx = 0;
  2751. rv = ssl_security_cert(s, NULL, x, vfy, 1);
  2752. if (rv != 1)
  2753. return rv;
  2754. for (i = start_idx; i < sk_X509_num(sk); i++) {
  2755. x = sk_X509_value(sk, i);
  2756. rv = ssl_security_cert(s, NULL, x, vfy, 0);
  2757. if (rv != 1)
  2758. return rv;
  2759. }
  2760. return 1;
  2761. }
  2762. /*
  2763. * For TLS 1.2 servers check if we have a certificate which can be used
  2764. * with the signature algorithm "lu" and return index of certificate.
  2765. */
  2766. static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
  2767. {
  2768. int sig_idx = lu->sig_idx;
  2769. const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
  2770. /* If not recognised or not supported by cipher mask it is not suitable */
  2771. if (clu == NULL
  2772. || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0
  2773. || (clu->nid == EVP_PKEY_RSA_PSS
  2774. && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0))
  2775. return -1;
  2776. return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
  2777. }
  2778. /*
  2779. * Checks the given cert against signature_algorithm_cert restrictions sent by
  2780. * the peer (if any) as well as whether the hash from the sigalg is usable with
  2781. * the key.
  2782. * Returns true if the cert is usable and false otherwise.
  2783. */
  2784. static int check_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x,
  2785. EVP_PKEY *pkey)
  2786. {
  2787. const SIGALG_LOOKUP *lu;
  2788. int mdnid, pknid, supported;
  2789. size_t i;
  2790. const char *mdname = NULL;
  2791. /*
  2792. * If the given EVP_PKEY cannot support signing with this digest,
  2793. * the answer is simply 'no'.
  2794. */
  2795. if (sig->hash != NID_undef)
  2796. mdname = OBJ_nid2sn(sig->hash);
  2797. supported = EVP_PKEY_digestsign_supports_digest(pkey, s->ctx->libctx,
  2798. mdname,
  2799. s->ctx->propq);
  2800. if (supported <= 0)
  2801. return 0;
  2802. /*
  2803. * The TLS 1.3 signature_algorithms_cert extension places restrictions
  2804. * on the sigalg with which the certificate was signed (by its issuer).
  2805. */
  2806. if (s->s3.tmp.peer_cert_sigalgs != NULL) {
  2807. if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL))
  2808. return 0;
  2809. for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) {
  2810. lu = tls1_lookup_sigalg(s, s->s3.tmp.peer_cert_sigalgs[i]);
  2811. if (lu == NULL)
  2812. continue;
  2813. /*
  2814. * This does not differentiate between the
  2815. * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
  2816. * have a chain here that lets us look at the key OID in the
  2817. * signing certificate.
  2818. */
  2819. if (mdnid == lu->hash && pknid == lu->sig)
  2820. return 1;
  2821. }
  2822. return 0;
  2823. }
  2824. /*
  2825. * Without signat_algorithms_cert, any certificate for which we have
  2826. * a viable public key is permitted.
  2827. */
  2828. return 1;
  2829. }
  2830. /*
  2831. * Returns true if |s| has a usable certificate configured for use
  2832. * with signature scheme |sig|.
  2833. * "Usable" includes a check for presence as well as applying
  2834. * the signature_algorithm_cert restrictions sent by the peer (if any).
  2835. * Returns false if no usable certificate is found.
  2836. */
  2837. static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx)
  2838. {
  2839. /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */
  2840. if (idx == -1)
  2841. idx = sig->sig_idx;
  2842. if (!ssl_has_cert(s, idx))
  2843. return 0;
  2844. return check_cert_usable(s, sig, s->cert->pkeys[idx].x509,
  2845. s->cert->pkeys[idx].privatekey);
  2846. }
  2847. /*
  2848. * Returns true if the supplied cert |x| and key |pkey| is usable with the
  2849. * specified signature scheme |sig|, or false otherwise.
  2850. */
  2851. static int is_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x,
  2852. EVP_PKEY *pkey)
  2853. {
  2854. size_t idx;
  2855. if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
  2856. return 0;
  2857. /* Check the key is consistent with the sig alg */
  2858. if ((int)idx != sig->sig_idx)
  2859. return 0;
  2860. return check_cert_usable(s, sig, x, pkey);
  2861. }
  2862. /*
  2863. * Find a signature scheme that works with the supplied certificate |x| and key
  2864. * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our
  2865. * available certs/keys to find one that works.
  2866. */
  2867. static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey)
  2868. {
  2869. const SIGALG_LOOKUP *lu = NULL;
  2870. size_t i;
  2871. int curve = -1;
  2872. EVP_PKEY *tmppkey;
  2873. /* Look for a shared sigalgs matching possible certificates */
  2874. for (i = 0; i < s->shared_sigalgslen; i++) {
  2875. lu = s->shared_sigalgs[i];
  2876. /* Skip SHA1, SHA224, DSA and RSA if not PSS */
  2877. if (lu->hash == NID_sha1
  2878. || lu->hash == NID_sha224
  2879. || lu->sig == EVP_PKEY_DSA
  2880. || lu->sig == EVP_PKEY_RSA)
  2881. continue;
  2882. /* Check that we have a cert, and signature_algorithms_cert */
  2883. if (!tls1_lookup_md(s->ctx, lu, NULL))
  2884. continue;
  2885. if ((pkey == NULL && !has_usable_cert(s, lu, -1))
  2886. || (pkey != NULL && !is_cert_usable(s, lu, x, pkey)))
  2887. continue;
  2888. tmppkey = (pkey != NULL) ? pkey
  2889. : s->cert->pkeys[lu->sig_idx].privatekey;
  2890. if (lu->sig == EVP_PKEY_EC) {
  2891. if (curve == -1)
  2892. curve = ssl_get_EC_curve_nid(tmppkey);
  2893. if (lu->curve != NID_undef && curve != lu->curve)
  2894. continue;
  2895. } else if (lu->sig == EVP_PKEY_RSA_PSS) {
  2896. /* validate that key is large enough for the signature algorithm */
  2897. if (!rsa_pss_check_min_key_size(s->ctx, tmppkey, lu))
  2898. continue;
  2899. }
  2900. break;
  2901. }
  2902. if (i == s->shared_sigalgslen)
  2903. return NULL;
  2904. return lu;
  2905. }
  2906. /*
  2907. * Choose an appropriate signature algorithm based on available certificates
  2908. * Sets chosen certificate and signature algorithm.
  2909. *
  2910. * For servers if we fail to find a required certificate it is a fatal error,
  2911. * an appropriate error code is set and a TLS alert is sent.
  2912. *
  2913. * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
  2914. * a fatal error: we will either try another certificate or not present one
  2915. * to the server. In this case no error is set.
  2916. */
  2917. int tls_choose_sigalg(SSL *s, int fatalerrs)
  2918. {
  2919. const SIGALG_LOOKUP *lu = NULL;
  2920. int sig_idx = -1;
  2921. s->s3.tmp.cert = NULL;
  2922. s->s3.tmp.sigalg = NULL;
  2923. if (SSL_IS_TLS13(s)) {
  2924. lu = find_sig_alg(s, NULL, NULL);
  2925. if (lu == NULL) {
  2926. if (!fatalerrs)
  2927. return 1;
  2928. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2929. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2930. return 0;
  2931. }
  2932. } else {
  2933. /* If ciphersuite doesn't require a cert nothing to do */
  2934. if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT))
  2935. return 1;
  2936. if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
  2937. return 1;
  2938. if (SSL_USE_SIGALGS(s)) {
  2939. size_t i;
  2940. if (s->s3.tmp.peer_sigalgs != NULL) {
  2941. int curve = -1;
  2942. /* For Suite B need to match signature algorithm to curve */
  2943. if (tls1_suiteb(s))
  2944. curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC]
  2945. .privatekey);
  2946. /*
  2947. * Find highest preference signature algorithm matching
  2948. * cert type
  2949. */
  2950. for (i = 0; i < s->shared_sigalgslen; i++) {
  2951. lu = s->shared_sigalgs[i];
  2952. if (s->server) {
  2953. if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
  2954. continue;
  2955. } else {
  2956. int cc_idx = s->cert->key - s->cert->pkeys;
  2957. sig_idx = lu->sig_idx;
  2958. if (cc_idx != sig_idx)
  2959. continue;
  2960. }
  2961. /* Check that we have a cert, and sig_algs_cert */
  2962. if (!has_usable_cert(s, lu, sig_idx))
  2963. continue;
  2964. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2965. /* validate that key is large enough for the signature algorithm */
  2966. EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;
  2967. if (!rsa_pss_check_min_key_size(s->ctx, pkey, lu))
  2968. continue;
  2969. }
  2970. if (curve == -1 || lu->curve == curve)
  2971. break;
  2972. }
  2973. #ifndef OPENSSL_NO_GOST
  2974. /*
  2975. * Some Windows-based implementations do not send GOST algorithms indication
  2976. * in supported_algorithms extension, so when we have GOST-based ciphersuite,
  2977. * we have to assume GOST support.
  2978. */
  2979. if (i == s->shared_sigalgslen && s->s3.tmp.new_cipher->algorithm_auth & (SSL_aGOST01 | SSL_aGOST12)) {
  2980. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  2981. if (!fatalerrs)
  2982. return 1;
  2983. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2984. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2985. return 0;
  2986. } else {
  2987. i = 0;
  2988. sig_idx = lu->sig_idx;
  2989. }
  2990. }
  2991. #endif
  2992. if (i == s->shared_sigalgslen) {
  2993. if (!fatalerrs)
  2994. return 1;
  2995. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2996. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  2997. return 0;
  2998. }
  2999. } else {
  3000. /*
  3001. * If we have no sigalg use defaults
  3002. */
  3003. const uint16_t *sent_sigs;
  3004. size_t sent_sigslen;
  3005. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  3006. if (!fatalerrs)
  3007. return 1;
  3008. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3009. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  3010. return 0;
  3011. }
  3012. /* Check signature matches a type we sent */
  3013. sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
  3014. for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
  3015. if (lu->sigalg == *sent_sigs
  3016. && has_usable_cert(s, lu, lu->sig_idx))
  3017. break;
  3018. }
  3019. if (i == sent_sigslen) {
  3020. if (!fatalerrs)
  3021. return 1;
  3022. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3023. SSL_R_WRONG_SIGNATURE_TYPE);
  3024. return 0;
  3025. }
  3026. }
  3027. } else {
  3028. if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
  3029. if (!fatalerrs)
  3030. return 1;
  3031. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3032. SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
  3033. return 0;
  3034. }
  3035. }
  3036. }
  3037. if (sig_idx == -1)
  3038. sig_idx = lu->sig_idx;
  3039. s->s3.tmp.cert = &s->cert->pkeys[sig_idx];
  3040. s->cert->key = s->s3.tmp.cert;
  3041. s->s3.tmp.sigalg = lu;
  3042. return 1;
  3043. }
  3044. int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
  3045. {
  3046. if (mode != TLSEXT_max_fragment_length_DISABLED
  3047. && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
  3048. ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  3049. return 0;
  3050. }
  3051. ctx->ext.max_fragment_len_mode = mode;
  3052. return 1;
  3053. }
  3054. int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
  3055. {
  3056. if (mode != TLSEXT_max_fragment_length_DISABLED
  3057. && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
  3058. ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  3059. return 0;
  3060. }
  3061. ssl->ext.max_fragment_len_mode = mode;
  3062. return 1;
  3063. }
  3064. uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
  3065. {
  3066. return session->ext.max_fragment_len_mode;
  3067. }
  3068. /*
  3069. * Helper functions for HMAC access with legacy support included.
  3070. */
  3071. SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx)
  3072. {
  3073. SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret));
  3074. EVP_MAC *mac = NULL;
  3075. if (ret == NULL)
  3076. return NULL;
  3077. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3078. if (ctx->ext.ticket_key_evp_cb == NULL
  3079. && ctx->ext.ticket_key_cb != NULL) {
  3080. if (!ssl_hmac_old_new(ret))
  3081. goto err;
  3082. return ret;
  3083. }
  3084. #endif
  3085. mac = EVP_MAC_fetch(ctx->libctx, "HMAC", ctx->propq);
  3086. if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL)
  3087. goto err;
  3088. EVP_MAC_free(mac);
  3089. return ret;
  3090. err:
  3091. EVP_MAC_CTX_free(ret->ctx);
  3092. EVP_MAC_free(mac);
  3093. OPENSSL_free(ret);
  3094. return NULL;
  3095. }
  3096. void ssl_hmac_free(SSL_HMAC *ctx)
  3097. {
  3098. if (ctx != NULL) {
  3099. EVP_MAC_CTX_free(ctx->ctx);
  3100. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3101. ssl_hmac_old_free(ctx);
  3102. #endif
  3103. OPENSSL_free(ctx);
  3104. }
  3105. }
  3106. EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx)
  3107. {
  3108. return ctx->ctx;
  3109. }
  3110. int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md)
  3111. {
  3112. OSSL_PARAM params[2], *p = params;
  3113. if (ctx->ctx != NULL) {
  3114. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0);
  3115. *p = OSSL_PARAM_construct_end();
  3116. if (EVP_MAC_init(ctx->ctx, key, len, params))
  3117. return 1;
  3118. }
  3119. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3120. if (ctx->old_ctx != NULL)
  3121. return ssl_hmac_old_init(ctx, key, len, md);
  3122. #endif
  3123. return 0;
  3124. }
  3125. int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len)
  3126. {
  3127. if (ctx->ctx != NULL)
  3128. return EVP_MAC_update(ctx->ctx, data, len);
  3129. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3130. if (ctx->old_ctx != NULL)
  3131. return ssl_hmac_old_update(ctx, data, len);
  3132. #endif
  3133. return 0;
  3134. }
  3135. int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,
  3136. size_t max_size)
  3137. {
  3138. if (ctx->ctx != NULL)
  3139. return EVP_MAC_final(ctx->ctx, md, len, max_size);
  3140. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3141. if (ctx->old_ctx != NULL)
  3142. return ssl_hmac_old_final(ctx, md, len);
  3143. #endif
  3144. return 0;
  3145. }
  3146. size_t ssl_hmac_size(const SSL_HMAC *ctx)
  3147. {
  3148. if (ctx->ctx != NULL)
  3149. return EVP_MAC_CTX_get_mac_size(ctx->ctx);
  3150. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3151. if (ctx->old_ctx != NULL)
  3152. return ssl_hmac_old_size(ctx);
  3153. #endif
  3154. return 0;
  3155. }
  3156. int ssl_get_EC_curve_nid(const EVP_PKEY *pkey)
  3157. {
  3158. char gname[OSSL_MAX_NAME_SIZE];
  3159. if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) > 0)
  3160. return OBJ_txt2nid(gname);
  3161. return NID_undef;
  3162. }
  3163. __owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey,
  3164. const unsigned char *enckey,
  3165. size_t enckeylen)
  3166. {
  3167. if (EVP_PKEY_is_a(pkey, "DH")) {
  3168. int bits = EVP_PKEY_get_bits(pkey);
  3169. if (bits <= 0 || enckeylen != (size_t)bits / 8)
  3170. /* the encoded key must be padded to the length of the p */
  3171. return 0;
  3172. } else if (EVP_PKEY_is_a(pkey, "EC")) {
  3173. if (enckeylen < 3 /* point format and at least 1 byte for x and y */
  3174. || enckey[0] != 0x04)
  3175. return 0;
  3176. }
  3177. return EVP_PKEY_set1_encoded_public_key(pkey, enckey, enckeylen);
  3178. }