t1_lib.c 113 KB

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