t1_lib.c 112 KB

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