openssl.c 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. * Source file for all OpenSSL-specific code for the TLS/SSL layer. No code
  26. * but vtls.c should ever call or use these functions.
  27. */
  28. #include "curl_setup.h"
  29. #if defined(USE_QUICHE) || defined(USE_OPENSSL)
  30. #include <limits.h>
  31. /* Wincrypt must be included before anything that could include OpenSSL. */
  32. #if defined(USE_WIN32_CRYPTO)
  33. #include <wincrypt.h>
  34. /* Undefine wincrypt conflicting symbols for BoringSSL. */
  35. #undef X509_NAME
  36. #undef X509_EXTENSIONS
  37. #undef PKCS7_ISSUER_AND_SERIAL
  38. #undef PKCS7_SIGNER_INFO
  39. #undef OCSP_REQUEST
  40. #undef OCSP_RESPONSE
  41. #endif
  42. #include "urldata.h"
  43. #include "sendf.h"
  44. #include "formdata.h" /* for the boundary function */
  45. #include "url.h" /* for the ssl config check function */
  46. #include "inet_pton.h"
  47. #include "openssl.h"
  48. #include "connect.h"
  49. #include "slist.h"
  50. #include "select.h"
  51. #include "vtls.h"
  52. #include "vtls_int.h"
  53. #include "vauth/vauth.h"
  54. #include "keylog.h"
  55. #include "strcase.h"
  56. #include "hostcheck.h"
  57. #include "multiif.h"
  58. #include "strerror.h"
  59. #include "curl_printf.h"
  60. #include <openssl/ssl.h>
  61. #include <openssl/rand.h>
  62. #include <openssl/x509v3.h>
  63. #ifndef OPENSSL_NO_DSA
  64. #include <openssl/dsa.h>
  65. #endif
  66. #include <openssl/dh.h>
  67. #include <openssl/err.h>
  68. #include <openssl/md5.h>
  69. #include <openssl/conf.h>
  70. #include <openssl/bn.h>
  71. #include <openssl/rsa.h>
  72. #include <openssl/bio.h>
  73. #include <openssl/buffer.h>
  74. #include <openssl/pkcs12.h>
  75. #include <openssl/tls1.h>
  76. #include <openssl/evp.h>
  77. #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_OCSP)
  78. #include <openssl/ocsp.h>
  79. #endif
  80. #if (OPENSSL_VERSION_NUMBER >= 0x0090700fL) && /* 0.9.7 or later */ \
  81. !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_UI_CONSOLE)
  82. #define USE_OPENSSL_ENGINE
  83. #include <openssl/engine.h>
  84. #endif
  85. #include "warnless.h"
  86. /* The last #include files should be: */
  87. #include "curl_memory.h"
  88. #include "memdebug.h"
  89. #ifndef ARRAYSIZE
  90. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  91. #endif
  92. /* Uncomment the ALLOW_RENEG line to a real #define if you want to allow TLS
  93. renegotiations when built with BoringSSL. Renegotiating is non-compliant
  94. with HTTP/2 and "an extremely dangerous protocol feature". Beware.
  95. #define ALLOW_RENEG 1
  96. */
  97. #ifndef OPENSSL_VERSION_NUMBER
  98. #error "OPENSSL_VERSION_NUMBER not defined"
  99. #endif
  100. #ifdef USE_OPENSSL_ENGINE
  101. #include <openssl/ui.h>
  102. #endif
  103. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  104. #define SSL_METHOD_QUAL const
  105. #else
  106. #define SSL_METHOD_QUAL
  107. #endif
  108. #if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
  109. #define HAVE_ERR_REMOVE_THREAD_STATE 1
  110. #endif
  111. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && /* OpenSSL 1.1.0+ */ \
  112. !(defined(LIBRESSL_VERSION_NUMBER) && \
  113. LIBRESSL_VERSION_NUMBER < 0x20700000L)
  114. #define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER
  115. #define HAVE_X509_GET0_EXTENSIONS 1 /* added in 1.1.0 -pre1 */
  116. #define HAVE_OPAQUE_EVP_PKEY 1 /* since 1.1.0 -pre3 */
  117. #define HAVE_OPAQUE_RSA_DSA_DH 1 /* since 1.1.0 -pre5 */
  118. #define CONST_EXTS const
  119. #define HAVE_ERR_REMOVE_THREAD_STATE_DEPRECATED 1
  120. /* funny typecast define due to difference in API */
  121. #ifdef LIBRESSL_VERSION_NUMBER
  122. #define ARG2_X509_signature_print (X509_ALGOR *)
  123. #else
  124. #define ARG2_X509_signature_print
  125. #endif
  126. #else
  127. /* For OpenSSL before 1.1.0 */
  128. #define ASN1_STRING_get0_data(x) ASN1_STRING_data(x)
  129. #define X509_get0_notBefore(x) X509_get_notBefore(x)
  130. #define X509_get0_notAfter(x) X509_get_notAfter(x)
  131. #define CONST_EXTS /* nope */
  132. #ifndef LIBRESSL_VERSION_NUMBER
  133. #define OpenSSL_version_num() SSLeay()
  134. #endif
  135. #endif
  136. #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && /* 1.0.2 or later */ \
  137. !(defined(LIBRESSL_VERSION_NUMBER) && \
  138. LIBRESSL_VERSION_NUMBER < 0x20700000L)
  139. #define HAVE_X509_GET0_SIGNATURE 1
  140. #endif
  141. #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) /* 1.0.2 or later */
  142. #define HAVE_SSL_GET_SHUTDOWN 1
  143. #endif
  144. #if OPENSSL_VERSION_NUMBER >= 0x10002003L && \
  145. OPENSSL_VERSION_NUMBER <= 0x10002FFFL && \
  146. !defined(OPENSSL_NO_COMP)
  147. #define HAVE_SSL_COMP_FREE_COMPRESSION_METHODS 1
  148. #endif
  149. #if (OPENSSL_VERSION_NUMBER < 0x0090808fL)
  150. /* not present in older OpenSSL */
  151. #define OPENSSL_load_builtin_modules(x)
  152. #endif
  153. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  154. #define HAVE_EVP_PKEY_GET_PARAMS 1
  155. #endif
  156. #ifdef HAVE_EVP_PKEY_GET_PARAMS
  157. #include <openssl/core_names.h>
  158. #define DECLARE_PKEY_PARAM_BIGNUM(name) BIGNUM *name = NULL
  159. #define FREE_PKEY_PARAM_BIGNUM(name) BN_clear_free(name)
  160. #else
  161. #define DECLARE_PKEY_PARAM_BIGNUM(name) const BIGNUM *name
  162. #define FREE_PKEY_PARAM_BIGNUM(name)
  163. #endif
  164. /*
  165. * Whether SSL_CTX_set_keylog_callback is available.
  166. * OpenSSL: supported since 1.1.1 https://github.com/openssl/openssl/pull/2287
  167. * BoringSSL: supported since d28f59c27bac (committed 2015-11-19)
  168. * LibreSSL: supported since 3.5.0 (released 2022-02-24)
  169. */
  170. #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && \
  171. !defined(LIBRESSL_VERSION_NUMBER)) || \
  172. (defined(LIBRESSL_VERSION_NUMBER) && \
  173. LIBRESSL_VERSION_NUMBER >= 0x3050000fL) || \
  174. defined(OPENSSL_IS_BORINGSSL)
  175. #define HAVE_KEYLOG_CALLBACK
  176. #endif
  177. /* Whether SSL_CTX_set_ciphersuites is available.
  178. * OpenSSL: supported since 1.1.1 (commit a53b5be6a05)
  179. * BoringSSL: no
  180. * LibreSSL: supported since 3.4.1 (released 2021-10-14)
  181. */
  182. #if ((OPENSSL_VERSION_NUMBER >= 0x10101000L && \
  183. !defined(LIBRESSL_VERSION_NUMBER)) || \
  184. (defined(LIBRESSL_VERSION_NUMBER) && \
  185. LIBRESSL_VERSION_NUMBER >= 0x3040100fL)) && \
  186. !defined(OPENSSL_IS_BORINGSSL)
  187. #define HAVE_SSL_CTX_SET_CIPHERSUITES
  188. #if !defined(OPENSSL_IS_AWSLC)
  189. #define HAVE_SSL_CTX_SET_POST_HANDSHAKE_AUTH
  190. #endif
  191. #endif
  192. /*
  193. * Whether SSL_CTX_set1_curves_list is available.
  194. * OpenSSL: supported since 1.0.2, see
  195. * https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set1_groups.html
  196. * BoringSSL: supported since 5fd1807d95f7 (committed 2016-09-30)
  197. * LibreSSL: since 2.5.3 (April 12, 2017)
  198. */
  199. #if ((OPENSSL_VERSION_NUMBER >= 0x10002000L) && \
  200. !(defined(LIBRESSL_VERSION_NUMBER) && \
  201. LIBRESSL_VERSION_NUMBER < 0x20503000L)) || \
  202. defined(OPENSSL_IS_BORINGSSL)
  203. #define HAVE_SSL_CTX_SET_EC_CURVES
  204. #endif
  205. #if defined(LIBRESSL_VERSION_NUMBER)
  206. #define OSSL_PACKAGE "LibreSSL"
  207. #elif defined(OPENSSL_IS_BORINGSSL)
  208. #define OSSL_PACKAGE "BoringSSL"
  209. #elif defined(OPENSSL_IS_AWSLC)
  210. #define OSSL_PACKAGE "AWS-LC"
  211. #else
  212. # if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
  213. # define OSSL_PACKAGE "quictls"
  214. # else
  215. # define OSSL_PACKAGE "OpenSSL"
  216. #endif
  217. #endif
  218. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  219. /* up2date versions of OpenSSL maintain reasonably secure defaults without
  220. * breaking compatibility, so it is better not to override the defaults in curl
  221. */
  222. #define DEFAULT_CIPHER_SELECTION NULL
  223. #else
  224. /* ... but it is not the case with old versions of OpenSSL */
  225. #define DEFAULT_CIPHER_SELECTION \
  226. "ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH"
  227. #endif
  228. #ifdef HAVE_OPENSSL_SRP
  229. /* the function exists */
  230. #ifdef USE_TLS_SRP
  231. /* the functionality is not disabled */
  232. #define USE_OPENSSL_SRP
  233. #endif
  234. #endif
  235. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  236. #define HAVE_RANDOM_INIT_BY_DEFAULT 1
  237. #endif
  238. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
  239. !(defined(LIBRESSL_VERSION_NUMBER) && \
  240. LIBRESSL_VERSION_NUMBER < 0x2070100fL) && \
  241. !defined(OPENSSL_IS_BORINGSSL) && \
  242. !defined(OPENSSL_IS_AWSLC)
  243. #define HAVE_OPENSSL_VERSION
  244. #endif
  245. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  246. typedef uint32_t sslerr_t;
  247. #else
  248. typedef unsigned long sslerr_t;
  249. #endif
  250. /*
  251. * Whether the OpenSSL version has the API needed to support sharing an
  252. * X509_STORE between connections. The API is:
  253. * * `X509_STORE_up_ref` -- Introduced: OpenSSL 1.1.0.
  254. */
  255. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) /* OpenSSL >= 1.1.0 */
  256. #define HAVE_SSL_X509_STORE_SHARE
  257. #endif
  258. /* FIXME: On LCC <= 1.23, OpenSSL 2.0.0 may be
  259. * found but does not seem to have X509_STORE_up_ref. */
  260. #if defined(__LCC__) && defined(__EDG__) && (__LCC__ <= 123)
  261. #undef HAVE_SSL_X509_STORE_SHARE
  262. #endif
  263. /* What API version do we use? */
  264. #if defined(LIBRESSL_VERSION_NUMBER)
  265. #define USE_PRE_1_1_API (LIBRESSL_VERSION_NUMBER < 0x2070000f)
  266. #else /* !LIBRESSL_VERSION_NUMBER */
  267. #define USE_PRE_1_1_API (OPENSSL_VERSION_NUMBER < 0x10100000L)
  268. #endif /* !LIBRESSL_VERSION_NUMBER */
  269. struct ossl_ssl_backend_data {
  270. /* these ones requires specific SSL-types */
  271. SSL_CTX* ctx;
  272. SSL* handle;
  273. X509* server_cert;
  274. BIO_METHOD *bio_method;
  275. CURLcode io_result; /* result of last BIO cfilter operation */
  276. #ifndef HAVE_KEYLOG_CALLBACK
  277. /* Set to true once a valid keylog entry has been created to avoid dupes. */
  278. bool keylog_done;
  279. #endif
  280. bool x509_store_setup; /* x509 store has been set up */
  281. };
  282. #if defined(HAVE_SSL_X509_STORE_SHARE)
  283. struct multi_ssl_backend_data {
  284. char *CAfile; /* CAfile path used to generate X509 store */
  285. X509_STORE *store; /* cached X509 store or NULL if none */
  286. struct curltime time; /* when the cached store was created */
  287. };
  288. #endif /* HAVE_SSL_X509_STORE_SHARE */
  289. #define push_certinfo(_label, _num) \
  290. do { \
  291. long info_len = BIO_get_mem_data(mem, &ptr); \
  292. Curl_ssl_push_certinfo_len(data, _num, _label, ptr, info_len); \
  293. if(1 != BIO_reset(mem)) \
  294. break; \
  295. } while(0)
  296. static void pubkey_show(struct Curl_easy *data,
  297. BIO *mem,
  298. int num,
  299. const char *type,
  300. const char *name,
  301. const BIGNUM *bn)
  302. {
  303. char *ptr;
  304. char namebuf[32];
  305. msnprintf(namebuf, sizeof(namebuf), "%s(%s)", type, name);
  306. if(bn)
  307. BN_print(mem, bn);
  308. push_certinfo(namebuf, num);
  309. }
  310. #ifdef HAVE_OPAQUE_RSA_DSA_DH
  311. #define print_pubkey_BN(_type, _name, _num) \
  312. pubkey_show(data, mem, _num, #_type, #_name, _name)
  313. #else
  314. #define print_pubkey_BN(_type, _name, _num) \
  315. do { \
  316. if(_type->_name) { \
  317. pubkey_show(data, mem, _num, #_type, #_name, _type->_name); \
  318. } \
  319. } while(0)
  320. #endif
  321. static int asn1_object_dump(ASN1_OBJECT *a, char *buf, size_t len)
  322. {
  323. int i, ilen;
  324. ilen = (int)len;
  325. if(ilen < 0)
  326. return 1; /* buffer too big */
  327. i = i2t_ASN1_OBJECT(buf, ilen, a);
  328. if(i >= ilen)
  329. return 1; /* buffer too small */
  330. return 0;
  331. }
  332. static void X509V3_ext(struct Curl_easy *data,
  333. int certnum,
  334. CONST_EXTS STACK_OF(X509_EXTENSION) *exts)
  335. {
  336. int i;
  337. if((int)sk_X509_EXTENSION_num(exts) <= 0)
  338. /* no extensions, bail out */
  339. return;
  340. for(i = 0; i < (int)sk_X509_EXTENSION_num(exts); i++) {
  341. ASN1_OBJECT *obj;
  342. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  343. BUF_MEM *biomem;
  344. char namebuf[128];
  345. BIO *bio_out = BIO_new(BIO_s_mem());
  346. if(!bio_out)
  347. return;
  348. obj = X509_EXTENSION_get_object(ext);
  349. asn1_object_dump(obj, namebuf, sizeof(namebuf));
  350. if(!X509V3_EXT_print(bio_out, ext, 0, 0))
  351. ASN1_STRING_print(bio_out, (ASN1_STRING *)X509_EXTENSION_get_data(ext));
  352. BIO_get_mem_ptr(bio_out, &biomem);
  353. Curl_ssl_push_certinfo_len(data, certnum, namebuf, biomem->data,
  354. biomem->length);
  355. BIO_free(bio_out);
  356. }
  357. }
  358. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  359. typedef size_t numcert_t;
  360. #else
  361. typedef int numcert_t;
  362. #endif
  363. CURLcode Curl_ossl_certchain(struct Curl_easy *data, SSL *ssl)
  364. {
  365. CURLcode result;
  366. STACK_OF(X509) *sk;
  367. int i;
  368. numcert_t numcerts;
  369. BIO *mem;
  370. DEBUGASSERT(ssl);
  371. sk = SSL_get_peer_cert_chain(ssl);
  372. if(!sk) {
  373. return CURLE_OUT_OF_MEMORY;
  374. }
  375. numcerts = sk_X509_num(sk);
  376. result = Curl_ssl_init_certinfo(data, (int)numcerts);
  377. if(result) {
  378. return result;
  379. }
  380. mem = BIO_new(BIO_s_mem());
  381. if(!mem) {
  382. return CURLE_OUT_OF_MEMORY;
  383. }
  384. for(i = 0; i < (int)numcerts; i++) {
  385. ASN1_INTEGER *num;
  386. X509 *x = sk_X509_value(sk, i);
  387. EVP_PKEY *pubkey = NULL;
  388. int j;
  389. char *ptr;
  390. const ASN1_BIT_STRING *psig = NULL;
  391. X509_NAME_print_ex(mem, X509_get_subject_name(x), 0, XN_FLAG_ONELINE);
  392. push_certinfo("Subject", i);
  393. X509_NAME_print_ex(mem, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE);
  394. push_certinfo("Issuer", i);
  395. BIO_printf(mem, "%lx", X509_get_version(x));
  396. push_certinfo("Version", i);
  397. num = X509_get_serialNumber(x);
  398. if(num->type == V_ASN1_NEG_INTEGER)
  399. BIO_puts(mem, "-");
  400. for(j = 0; j < num->length; j++)
  401. BIO_printf(mem, "%02x", num->data[j]);
  402. push_certinfo("Serial Number", i);
  403. #if defined(HAVE_X509_GET0_SIGNATURE) && defined(HAVE_X509_GET0_EXTENSIONS)
  404. {
  405. const X509_ALGOR *sigalg = NULL;
  406. X509_PUBKEY *xpubkey = NULL;
  407. ASN1_OBJECT *pubkeyoid = NULL;
  408. X509_get0_signature(&psig, &sigalg, x);
  409. if(sigalg) {
  410. const ASN1_OBJECT *sigalgoid = NULL;
  411. X509_ALGOR_get0(&sigalgoid, NULL, NULL, sigalg);
  412. i2a_ASN1_OBJECT(mem, sigalgoid);
  413. push_certinfo("Signature Algorithm", i);
  414. }
  415. xpubkey = X509_get_X509_PUBKEY(x);
  416. if(xpubkey) {
  417. X509_PUBKEY_get0_param(&pubkeyoid, NULL, NULL, NULL, xpubkey);
  418. if(pubkeyoid) {
  419. i2a_ASN1_OBJECT(mem, pubkeyoid);
  420. push_certinfo("Public Key Algorithm", i);
  421. }
  422. }
  423. X509V3_ext(data, i, X509_get0_extensions(x));
  424. }
  425. #else
  426. {
  427. /* before OpenSSL 1.0.2 */
  428. X509_CINF *cinf = x->cert_info;
  429. i2a_ASN1_OBJECT(mem, cinf->signature->algorithm);
  430. push_certinfo("Signature Algorithm", i);
  431. i2a_ASN1_OBJECT(mem, cinf->key->algor->algorithm);
  432. push_certinfo("Public Key Algorithm", i);
  433. X509V3_ext(data, i, cinf->extensions);
  434. psig = x->signature;
  435. }
  436. #endif
  437. ASN1_TIME_print(mem, X509_get0_notBefore(x));
  438. push_certinfo("Start date", i);
  439. ASN1_TIME_print(mem, X509_get0_notAfter(x));
  440. push_certinfo("Expire date", i);
  441. pubkey = X509_get_pubkey(x);
  442. if(!pubkey)
  443. infof(data, " Unable to load public key");
  444. else {
  445. int pktype;
  446. #ifdef HAVE_OPAQUE_EVP_PKEY
  447. pktype = EVP_PKEY_id(pubkey);
  448. #else
  449. pktype = pubkey->type;
  450. #endif
  451. switch(pktype) {
  452. case EVP_PKEY_RSA:
  453. {
  454. #ifndef HAVE_EVP_PKEY_GET_PARAMS
  455. RSA *rsa;
  456. #ifdef HAVE_OPAQUE_EVP_PKEY
  457. rsa = EVP_PKEY_get0_RSA(pubkey);
  458. #else
  459. rsa = pubkey->pkey.rsa;
  460. #endif /* HAVE_OPAQUE_EVP_PKEY */
  461. #endif /* !HAVE_EVP_PKEY_GET_PARAMS */
  462. {
  463. #ifdef HAVE_OPAQUE_RSA_DSA_DH
  464. DECLARE_PKEY_PARAM_BIGNUM(n);
  465. DECLARE_PKEY_PARAM_BIGNUM(e);
  466. #ifdef HAVE_EVP_PKEY_GET_PARAMS
  467. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &n);
  468. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &e);
  469. #else
  470. RSA_get0_key(rsa, &n, &e, NULL);
  471. #endif /* HAVE_EVP_PKEY_GET_PARAMS */
  472. BIO_printf(mem, "%d", n ? BN_num_bits(n) : 0);
  473. #else
  474. BIO_printf(mem, "%d", rsa->n ? BN_num_bits(rsa->n) : 0);
  475. #endif /* HAVE_OPAQUE_RSA_DSA_DH */
  476. push_certinfo("RSA Public Key", i);
  477. print_pubkey_BN(rsa, n, i);
  478. print_pubkey_BN(rsa, e, i);
  479. FREE_PKEY_PARAM_BIGNUM(n);
  480. FREE_PKEY_PARAM_BIGNUM(e);
  481. }
  482. break;
  483. }
  484. case EVP_PKEY_DSA:
  485. {
  486. #ifndef OPENSSL_NO_DSA
  487. #ifndef HAVE_EVP_PKEY_GET_PARAMS
  488. DSA *dsa;
  489. #ifdef HAVE_OPAQUE_EVP_PKEY
  490. dsa = EVP_PKEY_get0_DSA(pubkey);
  491. #else
  492. dsa = pubkey->pkey.dsa;
  493. #endif /* HAVE_OPAQUE_EVP_PKEY */
  494. #endif /* !HAVE_EVP_PKEY_GET_PARAMS */
  495. {
  496. #ifdef HAVE_OPAQUE_RSA_DSA_DH
  497. DECLARE_PKEY_PARAM_BIGNUM(p);
  498. DECLARE_PKEY_PARAM_BIGNUM(q);
  499. DECLARE_PKEY_PARAM_BIGNUM(g);
  500. DECLARE_PKEY_PARAM_BIGNUM(pub_key);
  501. #ifdef HAVE_EVP_PKEY_GET_PARAMS
  502. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_P, &p);
  503. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_Q, &q);
  504. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_G, &g);
  505. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_PUB_KEY, &pub_key);
  506. #else
  507. DSA_get0_pqg(dsa, &p, &q, &g);
  508. DSA_get0_key(dsa, &pub_key, NULL);
  509. #endif /* HAVE_EVP_PKEY_GET_PARAMS */
  510. #endif /* HAVE_OPAQUE_RSA_DSA_DH */
  511. print_pubkey_BN(dsa, p, i);
  512. print_pubkey_BN(dsa, q, i);
  513. print_pubkey_BN(dsa, g, i);
  514. print_pubkey_BN(dsa, pub_key, i);
  515. FREE_PKEY_PARAM_BIGNUM(p);
  516. FREE_PKEY_PARAM_BIGNUM(q);
  517. FREE_PKEY_PARAM_BIGNUM(g);
  518. FREE_PKEY_PARAM_BIGNUM(pub_key);
  519. }
  520. #endif /* !OPENSSL_NO_DSA */
  521. break;
  522. }
  523. case EVP_PKEY_DH:
  524. {
  525. #ifndef HAVE_EVP_PKEY_GET_PARAMS
  526. DH *dh;
  527. #ifdef HAVE_OPAQUE_EVP_PKEY
  528. dh = EVP_PKEY_get0_DH(pubkey);
  529. #else
  530. dh = pubkey->pkey.dh;
  531. #endif /* HAVE_OPAQUE_EVP_PKEY */
  532. #endif /* !HAVE_EVP_PKEY_GET_PARAMS */
  533. {
  534. #ifdef HAVE_OPAQUE_RSA_DSA_DH
  535. DECLARE_PKEY_PARAM_BIGNUM(p);
  536. DECLARE_PKEY_PARAM_BIGNUM(q);
  537. DECLARE_PKEY_PARAM_BIGNUM(g);
  538. DECLARE_PKEY_PARAM_BIGNUM(pub_key);
  539. #ifdef HAVE_EVP_PKEY_GET_PARAMS
  540. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_P, &p);
  541. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_Q, &q);
  542. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_G, &g);
  543. EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_PUB_KEY, &pub_key);
  544. #else
  545. DH_get0_pqg(dh, &p, &q, &g);
  546. DH_get0_key(dh, &pub_key, NULL);
  547. #endif /* HAVE_EVP_PKEY_GET_PARAMS */
  548. print_pubkey_BN(dh, p, i);
  549. print_pubkey_BN(dh, q, i);
  550. print_pubkey_BN(dh, g, i);
  551. #else
  552. print_pubkey_BN(dh, p, i);
  553. print_pubkey_BN(dh, g, i);
  554. #endif /* HAVE_OPAQUE_RSA_DSA_DH */
  555. print_pubkey_BN(dh, pub_key, i);
  556. FREE_PKEY_PARAM_BIGNUM(p);
  557. FREE_PKEY_PARAM_BIGNUM(q);
  558. FREE_PKEY_PARAM_BIGNUM(g);
  559. FREE_PKEY_PARAM_BIGNUM(pub_key);
  560. }
  561. break;
  562. }
  563. }
  564. EVP_PKEY_free(pubkey);
  565. }
  566. if(psig) {
  567. for(j = 0; j < psig->length; j++)
  568. BIO_printf(mem, "%02x:", psig->data[j]);
  569. push_certinfo("Signature", i);
  570. }
  571. PEM_write_bio_X509(mem, x);
  572. push_certinfo("Cert", i);
  573. }
  574. BIO_free(mem);
  575. return CURLE_OK;
  576. }
  577. #endif /* quiche or OpenSSL */
  578. #ifdef USE_OPENSSL
  579. #if USE_PRE_1_1_API
  580. #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x2070000fL
  581. #define BIO_set_init(x,v) ((x)->init=(v))
  582. #define BIO_get_data(x) ((x)->ptr)
  583. #define BIO_set_data(x,v) ((x)->ptr=(v))
  584. #endif
  585. #define BIO_get_shutdown(x) ((x)->shutdown)
  586. #define BIO_set_shutdown(x,v) ((x)->shutdown=(v))
  587. #endif /* USE_PRE_1_1_API */
  588. static int ossl_bio_cf_create(BIO *bio)
  589. {
  590. BIO_set_shutdown(bio, 1);
  591. BIO_set_init(bio, 1);
  592. #if USE_PRE_1_1_API
  593. bio->num = -1;
  594. #endif
  595. BIO_set_data(bio, NULL);
  596. return 1;
  597. }
  598. static int ossl_bio_cf_destroy(BIO *bio)
  599. {
  600. if(!bio)
  601. return 0;
  602. return 1;
  603. }
  604. static long ossl_bio_cf_ctrl(BIO *bio, int cmd, long num, void *ptr)
  605. {
  606. struct Curl_cfilter *cf = BIO_get_data(bio);
  607. long ret = 1;
  608. (void)cf;
  609. (void)ptr;
  610. switch(cmd) {
  611. case BIO_CTRL_GET_CLOSE:
  612. ret = (long)BIO_get_shutdown(bio);
  613. break;
  614. case BIO_CTRL_SET_CLOSE:
  615. BIO_set_shutdown(bio, (int)num);
  616. break;
  617. case BIO_CTRL_FLUSH:
  618. /* we do no delayed writes, but if we ever would, this
  619. * needs to trigger it. */
  620. ret = 1;
  621. break;
  622. case BIO_CTRL_DUP:
  623. ret = 1;
  624. break;
  625. #ifdef BIO_CTRL_EOF
  626. case BIO_CTRL_EOF:
  627. /* EOF has been reached on input? */
  628. return (!cf->next || !cf->next->connected);
  629. #endif
  630. default:
  631. ret = 0;
  632. break;
  633. }
  634. return ret;
  635. }
  636. static int ossl_bio_cf_out_write(BIO *bio, const char *buf, int blen)
  637. {
  638. struct Curl_cfilter *cf = BIO_get_data(bio);
  639. struct ssl_connect_data *connssl = cf->ctx;
  640. struct ossl_ssl_backend_data *backend =
  641. (struct ossl_ssl_backend_data *)connssl->backend;
  642. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  643. ssize_t nwritten;
  644. CURLcode result = CURLE_SEND_ERROR;
  645. DEBUGASSERT(data);
  646. nwritten = Curl_conn_cf_send(cf->next, data, buf, blen, &result);
  647. CURL_TRC_CF(data, cf, "ossl_bio_cf_out_write(len=%d) -> %d, err=%d",
  648. blen, (int)nwritten, result);
  649. BIO_clear_retry_flags(bio);
  650. backend->io_result = result;
  651. if(nwritten < 0) {
  652. if(CURLE_AGAIN == result)
  653. BIO_set_retry_write(bio);
  654. }
  655. return (int)nwritten;
  656. }
  657. static int ossl_bio_cf_in_read(BIO *bio, char *buf, int blen)
  658. {
  659. struct Curl_cfilter *cf = BIO_get_data(bio);
  660. struct ssl_connect_data *connssl = cf->ctx;
  661. struct ossl_ssl_backend_data *backend =
  662. (struct ossl_ssl_backend_data *)connssl->backend;
  663. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  664. ssize_t nread;
  665. CURLcode result = CURLE_RECV_ERROR;
  666. DEBUGASSERT(data);
  667. /* OpenSSL catches this case, so should we. */
  668. if(!buf)
  669. return 0;
  670. nread = Curl_conn_cf_recv(cf->next, data, buf, blen, &result);
  671. CURL_TRC_CF(data, cf, "ossl_bio_cf_in_read(len=%d) -> %d, err=%d",
  672. blen, (int)nread, result);
  673. BIO_clear_retry_flags(bio);
  674. backend->io_result = result;
  675. if(nread < 0) {
  676. if(CURLE_AGAIN == result)
  677. BIO_set_retry_read(bio);
  678. }
  679. else if(nread == 0) {
  680. connssl->peer_closed = TRUE;
  681. }
  682. /* Before returning server replies to the SSL instance, we need
  683. * to have setup the x509 store or verification will fail. */
  684. if(!backend->x509_store_setup) {
  685. result = Curl_ssl_setup_x509_store(cf, data, backend->ctx);
  686. if(result) {
  687. backend->io_result = result;
  688. return -1;
  689. }
  690. backend->x509_store_setup = TRUE;
  691. }
  692. return (int)nread;
  693. }
  694. #if USE_PRE_1_1_API
  695. static BIO_METHOD ossl_bio_cf_meth_1_0 = {
  696. BIO_TYPE_MEM,
  697. "OpenSSL CF BIO",
  698. ossl_bio_cf_out_write,
  699. ossl_bio_cf_in_read,
  700. NULL, /* puts is never called */
  701. NULL, /* gets is never called */
  702. ossl_bio_cf_ctrl,
  703. ossl_bio_cf_create,
  704. ossl_bio_cf_destroy,
  705. NULL
  706. };
  707. static BIO_METHOD *ossl_bio_cf_method_create(void)
  708. {
  709. return &ossl_bio_cf_meth_1_0;
  710. }
  711. #define ossl_bio_cf_method_free(m) Curl_nop_stmt
  712. #else
  713. static BIO_METHOD *ossl_bio_cf_method_create(void)
  714. {
  715. BIO_METHOD *m = BIO_meth_new(BIO_TYPE_MEM, "OpenSSL CF BIO");
  716. if(m) {
  717. BIO_meth_set_write(m, &ossl_bio_cf_out_write);
  718. BIO_meth_set_read(m, &ossl_bio_cf_in_read);
  719. BIO_meth_set_ctrl(m, &ossl_bio_cf_ctrl);
  720. BIO_meth_set_create(m, &ossl_bio_cf_create);
  721. BIO_meth_set_destroy(m, &ossl_bio_cf_destroy);
  722. }
  723. return m;
  724. }
  725. static void ossl_bio_cf_method_free(BIO_METHOD *m)
  726. {
  727. if(m)
  728. BIO_meth_free(m);
  729. }
  730. #endif
  731. /*
  732. * Number of bytes to read from the random number seed file. This must be
  733. * a finite value (because some entropy "files" like /dev/urandom have
  734. * an infinite length), but must be large enough to provide enough
  735. * entropy to properly seed OpenSSL's PRNG.
  736. */
  737. #define RAND_LOAD_LENGTH 1024
  738. #ifdef HAVE_KEYLOG_CALLBACK
  739. static void ossl_keylog_callback(const SSL *ssl, const char *line)
  740. {
  741. (void)ssl;
  742. Curl_tls_keylog_write_line(line);
  743. }
  744. #else
  745. /*
  746. * ossl_log_tls12_secret is called by libcurl to make the CLIENT_RANDOMs if the
  747. * OpenSSL being used doesn't have native support for doing that.
  748. */
  749. static void
  750. ossl_log_tls12_secret(const SSL *ssl, bool *keylog_done)
  751. {
  752. const SSL_SESSION *session = SSL_get_session(ssl);
  753. unsigned char client_random[SSL3_RANDOM_SIZE];
  754. unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];
  755. int master_key_length = 0;
  756. if(!session || *keylog_done)
  757. return;
  758. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
  759. !(defined(LIBRESSL_VERSION_NUMBER) && \
  760. LIBRESSL_VERSION_NUMBER < 0x20700000L)
  761. /* ssl->s3 is not checked in openssl 1.1.0-pre6, but let's assume that
  762. * we have a valid SSL context if we have a non-NULL session. */
  763. SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE);
  764. master_key_length = (int)
  765. SSL_SESSION_get_master_key(session, master_key, SSL_MAX_MASTER_KEY_LENGTH);
  766. #else
  767. if(ssl->s3 && session->master_key_length > 0) {
  768. master_key_length = session->master_key_length;
  769. memcpy(master_key, session->master_key, session->master_key_length);
  770. memcpy(client_random, ssl->s3->client_random, SSL3_RANDOM_SIZE);
  771. }
  772. #endif
  773. /* The handshake has not progressed sufficiently yet, or this is a TLS 1.3
  774. * session (when curl was built with older OpenSSL headers and running with
  775. * newer OpenSSL runtime libraries). */
  776. if(master_key_length <= 0)
  777. return;
  778. *keylog_done = true;
  779. Curl_tls_keylog_write("CLIENT_RANDOM", client_random,
  780. master_key, master_key_length);
  781. }
  782. #endif /* !HAVE_KEYLOG_CALLBACK */
  783. static const char *SSL_ERROR_to_str(int err)
  784. {
  785. switch(err) {
  786. case SSL_ERROR_NONE:
  787. return "SSL_ERROR_NONE";
  788. case SSL_ERROR_SSL:
  789. return "SSL_ERROR_SSL";
  790. case SSL_ERROR_WANT_READ:
  791. return "SSL_ERROR_WANT_READ";
  792. case SSL_ERROR_WANT_WRITE:
  793. return "SSL_ERROR_WANT_WRITE";
  794. case SSL_ERROR_WANT_X509_LOOKUP:
  795. return "SSL_ERROR_WANT_X509_LOOKUP";
  796. case SSL_ERROR_SYSCALL:
  797. return "SSL_ERROR_SYSCALL";
  798. case SSL_ERROR_ZERO_RETURN:
  799. return "SSL_ERROR_ZERO_RETURN";
  800. case SSL_ERROR_WANT_CONNECT:
  801. return "SSL_ERROR_WANT_CONNECT";
  802. case SSL_ERROR_WANT_ACCEPT:
  803. return "SSL_ERROR_WANT_ACCEPT";
  804. #if defined(SSL_ERROR_WANT_ASYNC)
  805. case SSL_ERROR_WANT_ASYNC:
  806. return "SSL_ERROR_WANT_ASYNC";
  807. #endif
  808. #if defined(SSL_ERROR_WANT_ASYNC_JOB)
  809. case SSL_ERROR_WANT_ASYNC_JOB:
  810. return "SSL_ERROR_WANT_ASYNC_JOB";
  811. #endif
  812. #if defined(SSL_ERROR_WANT_EARLY)
  813. case SSL_ERROR_WANT_EARLY:
  814. return "SSL_ERROR_WANT_EARLY";
  815. #endif
  816. default:
  817. return "SSL_ERROR unknown";
  818. }
  819. }
  820. static size_t ossl_version(char *buffer, size_t size);
  821. /* Return error string for last OpenSSL error
  822. */
  823. static char *ossl_strerror(unsigned long error, char *buf, size_t size)
  824. {
  825. size_t len;
  826. DEBUGASSERT(size);
  827. *buf = '\0';
  828. len = ossl_version(buf, size);
  829. DEBUGASSERT(len < (size - 2));
  830. if(len < (size - 2)) {
  831. buf += len;
  832. size -= (len + 2);
  833. *buf++ = ':';
  834. *buf++ = ' ';
  835. *buf = '\0';
  836. }
  837. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  838. ERR_error_string_n((uint32_t)error, buf, size);
  839. #else
  840. ERR_error_string_n(error, buf, size);
  841. #endif
  842. if(!*buf) {
  843. const char *msg = error ? "Unknown error" : "No error";
  844. if(strlen(msg) < size)
  845. strcpy(buf, msg);
  846. }
  847. return buf;
  848. }
  849. static int passwd_callback(char *buf, int num, int encrypting,
  850. void *global_passwd)
  851. {
  852. DEBUGASSERT(0 == encrypting);
  853. if(!encrypting) {
  854. int klen = curlx_uztosi(strlen((char *)global_passwd));
  855. if(num > klen) {
  856. memcpy(buf, global_passwd, klen + 1);
  857. return klen;
  858. }
  859. }
  860. return 0;
  861. }
  862. /*
  863. * rand_enough() returns TRUE if we have seeded the random engine properly.
  864. */
  865. static bool rand_enough(void)
  866. {
  867. return (0 != RAND_status()) ? TRUE : FALSE;
  868. }
  869. static CURLcode ossl_seed(struct Curl_easy *data)
  870. {
  871. /* This might get called before it has been added to a multi handle */
  872. if(data->multi && data->multi->ssl_seeded)
  873. return CURLE_OK;
  874. if(rand_enough()) {
  875. /* OpenSSL 1.1.0+ should return here */
  876. if(data->multi)
  877. data->multi->ssl_seeded = TRUE;
  878. return CURLE_OK;
  879. }
  880. #ifdef HAVE_RANDOM_INIT_BY_DEFAULT
  881. /* with OpenSSL 1.1.0+, a failed RAND_status is a showstopper */
  882. failf(data, "Insufficient randomness");
  883. return CURLE_SSL_CONNECT_ERROR;
  884. #else
  885. #ifdef RANDOM_FILE
  886. RAND_load_file(RANDOM_FILE, RAND_LOAD_LENGTH);
  887. if(rand_enough())
  888. return CURLE_OK;
  889. #endif
  890. /* fallback to a custom seeding of the PRNG using a hash based on a current
  891. time */
  892. do {
  893. unsigned char randb[64];
  894. size_t len = sizeof(randb);
  895. size_t i, i_max;
  896. for(i = 0, i_max = len / sizeof(struct curltime); i < i_max; ++i) {
  897. struct curltime tv = Curl_now();
  898. Curl_wait_ms(1);
  899. tv.tv_sec *= i + 1;
  900. tv.tv_usec *= (unsigned int)i + 2;
  901. tv.tv_sec ^= ((Curl_now().tv_sec + Curl_now().tv_usec) *
  902. (i + 3)) << 8;
  903. tv.tv_usec ^= (unsigned int) ((Curl_now().tv_sec +
  904. Curl_now().tv_usec) *
  905. (i + 4)) << 16;
  906. memcpy(&randb[i * sizeof(struct curltime)], &tv,
  907. sizeof(struct curltime));
  908. }
  909. RAND_add(randb, (int)len, (double)len/2);
  910. } while(!rand_enough());
  911. {
  912. /* generates a default path for the random seed file */
  913. char fname[256];
  914. fname[0] = 0; /* blank it first */
  915. RAND_file_name(fname, sizeof(fname));
  916. if(fname[0]) {
  917. /* we got a file name to try */
  918. RAND_load_file(fname, RAND_LOAD_LENGTH);
  919. if(rand_enough())
  920. return CURLE_OK;
  921. }
  922. }
  923. infof(data, "libcurl is now using a weak random seed");
  924. return (rand_enough() ? CURLE_OK :
  925. CURLE_SSL_CONNECT_ERROR /* confusing error code */);
  926. #endif
  927. }
  928. #ifndef SSL_FILETYPE_ENGINE
  929. #define SSL_FILETYPE_ENGINE 42
  930. #endif
  931. #ifndef SSL_FILETYPE_PKCS12
  932. #define SSL_FILETYPE_PKCS12 43
  933. #endif
  934. static int do_file_type(const char *type)
  935. {
  936. if(!type || !type[0])
  937. return SSL_FILETYPE_PEM;
  938. if(strcasecompare(type, "PEM"))
  939. return SSL_FILETYPE_PEM;
  940. if(strcasecompare(type, "DER"))
  941. return SSL_FILETYPE_ASN1;
  942. if(strcasecompare(type, "ENG"))
  943. return SSL_FILETYPE_ENGINE;
  944. if(strcasecompare(type, "P12"))
  945. return SSL_FILETYPE_PKCS12;
  946. return -1;
  947. }
  948. #ifdef USE_OPENSSL_ENGINE
  949. /*
  950. * Supply default password to the engine user interface conversation.
  951. * The password is passed by OpenSSL engine from ENGINE_load_private_key()
  952. * last argument to the ui and can be obtained by UI_get0_user_data(ui) here.
  953. */
  954. static int ssl_ui_reader(UI *ui, UI_STRING *uis)
  955. {
  956. const char *password;
  957. switch(UI_get_string_type(uis)) {
  958. case UIT_PROMPT:
  959. case UIT_VERIFY:
  960. password = (const char *)UI_get0_user_data(ui);
  961. if(password && (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) {
  962. UI_set_result(ui, uis, password);
  963. return 1;
  964. }
  965. FALLTHROUGH();
  966. default:
  967. break;
  968. }
  969. return (UI_method_get_reader(UI_OpenSSL()))(ui, uis);
  970. }
  971. /*
  972. * Suppress interactive request for a default password if available.
  973. */
  974. static int ssl_ui_writer(UI *ui, UI_STRING *uis)
  975. {
  976. switch(UI_get_string_type(uis)) {
  977. case UIT_PROMPT:
  978. case UIT_VERIFY:
  979. if(UI_get0_user_data(ui) &&
  980. (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) {
  981. return 1;
  982. }
  983. FALLTHROUGH();
  984. default:
  985. break;
  986. }
  987. return (UI_method_get_writer(UI_OpenSSL()))(ui, uis);
  988. }
  989. /*
  990. * Check if a given string is a PKCS#11 URI
  991. */
  992. static bool is_pkcs11_uri(const char *string)
  993. {
  994. return (string && strncasecompare(string, "pkcs11:", 7));
  995. }
  996. #endif
  997. static CURLcode ossl_set_engine(struct Curl_easy *data, const char *engine);
  998. static int
  999. SSL_CTX_use_certificate_blob(SSL_CTX *ctx, const struct curl_blob *blob,
  1000. int type, const char *key_passwd)
  1001. {
  1002. int ret = 0;
  1003. X509 *x = NULL;
  1004. /* the typecast of blob->len is fine since it is guaranteed to never be
  1005. larger than CURL_MAX_INPUT_LENGTH */
  1006. BIO *in = BIO_new_mem_buf(blob->data, (int)(blob->len));
  1007. if(!in)
  1008. return CURLE_OUT_OF_MEMORY;
  1009. if(type == SSL_FILETYPE_ASN1) {
  1010. /* j = ERR_R_ASN1_LIB; */
  1011. x = d2i_X509_bio(in, NULL);
  1012. }
  1013. else if(type == SSL_FILETYPE_PEM) {
  1014. /* ERR_R_PEM_LIB; */
  1015. x = PEM_read_bio_X509(in, NULL,
  1016. passwd_callback, (void *)key_passwd);
  1017. }
  1018. else {
  1019. ret = 0;
  1020. goto end;
  1021. }
  1022. if(!x) {
  1023. ret = 0;
  1024. goto end;
  1025. }
  1026. ret = SSL_CTX_use_certificate(ctx, x);
  1027. end:
  1028. X509_free(x);
  1029. BIO_free(in);
  1030. return ret;
  1031. }
  1032. static int
  1033. SSL_CTX_use_PrivateKey_blob(SSL_CTX *ctx, const struct curl_blob *blob,
  1034. int type, const char *key_passwd)
  1035. {
  1036. int ret = 0;
  1037. EVP_PKEY *pkey = NULL;
  1038. BIO *in = BIO_new_mem_buf(blob->data, (int)(blob->len));
  1039. if(!in)
  1040. return CURLE_OUT_OF_MEMORY;
  1041. if(type == SSL_FILETYPE_PEM)
  1042. pkey = PEM_read_bio_PrivateKey(in, NULL, passwd_callback,
  1043. (void *)key_passwd);
  1044. else if(type == SSL_FILETYPE_ASN1)
  1045. pkey = d2i_PrivateKey_bio(in, NULL);
  1046. else {
  1047. ret = 0;
  1048. goto end;
  1049. }
  1050. if(!pkey) {
  1051. ret = 0;
  1052. goto end;
  1053. }
  1054. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  1055. EVP_PKEY_free(pkey);
  1056. end:
  1057. BIO_free(in);
  1058. return ret;
  1059. }
  1060. static int
  1061. SSL_CTX_use_certificate_chain_blob(SSL_CTX *ctx, const struct curl_blob *blob,
  1062. const char *key_passwd)
  1063. {
  1064. /* SSL_CTX_add1_chain_cert introduced in OpenSSL 1.0.2 */
  1065. #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && /* OpenSSL 1.0.2 or later */ \
  1066. !(defined(LIBRESSL_VERSION_NUMBER) && \
  1067. (LIBRESSL_VERSION_NUMBER < 0x2090100fL)) /* LibreSSL 2.9.1 or later */
  1068. int ret = 0;
  1069. X509 *x = NULL;
  1070. void *passwd_callback_userdata = (void *)key_passwd;
  1071. BIO *in = BIO_new_mem_buf(blob->data, (int)(blob->len));
  1072. if(!in)
  1073. return CURLE_OUT_OF_MEMORY;
  1074. ERR_clear_error();
  1075. x = PEM_read_bio_X509_AUX(in, NULL,
  1076. passwd_callback, (void *)key_passwd);
  1077. if(!x) {
  1078. ret = 0;
  1079. goto end;
  1080. }
  1081. ret = SSL_CTX_use_certificate(ctx, x);
  1082. if(ERR_peek_error() != 0)
  1083. ret = 0;
  1084. if(ret) {
  1085. X509 *ca;
  1086. sslerr_t err;
  1087. if(!SSL_CTX_clear_chain_certs(ctx)) {
  1088. ret = 0;
  1089. goto end;
  1090. }
  1091. while((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
  1092. passwd_callback_userdata))
  1093. != NULL) {
  1094. if(!SSL_CTX_add0_chain_cert(ctx, ca)) {
  1095. X509_free(ca);
  1096. ret = 0;
  1097. goto end;
  1098. }
  1099. }
  1100. err = ERR_peek_last_error();
  1101. if((ERR_GET_LIB(err) == ERR_LIB_PEM) &&
  1102. (ERR_GET_REASON(err) == PEM_R_NO_START_LINE))
  1103. ERR_clear_error();
  1104. else
  1105. ret = 0;
  1106. }
  1107. end:
  1108. X509_free(x);
  1109. BIO_free(in);
  1110. return ret;
  1111. #else
  1112. (void)ctx; /* unused */
  1113. (void)blob; /* unused */
  1114. (void)key_passwd; /* unused */
  1115. return 0;
  1116. #endif
  1117. }
  1118. static
  1119. int cert_stuff(struct Curl_easy *data,
  1120. SSL_CTX* ctx,
  1121. char *cert_file,
  1122. const struct curl_blob *cert_blob,
  1123. const char *cert_type,
  1124. char *key_file,
  1125. const struct curl_blob *key_blob,
  1126. const char *key_type,
  1127. char *key_passwd)
  1128. {
  1129. char error_buffer[256];
  1130. bool check_privkey = TRUE;
  1131. int file_type = do_file_type(cert_type);
  1132. if(cert_file || cert_blob || (file_type == SSL_FILETYPE_ENGINE)) {
  1133. SSL *ssl;
  1134. X509 *x509;
  1135. int cert_done = 0;
  1136. int cert_use_result;
  1137. if(key_passwd) {
  1138. /* set the password in the callback userdata */
  1139. SSL_CTX_set_default_passwd_cb_userdata(ctx, key_passwd);
  1140. /* Set passwd callback: */
  1141. SSL_CTX_set_default_passwd_cb(ctx, passwd_callback);
  1142. }
  1143. switch(file_type) {
  1144. case SSL_FILETYPE_PEM:
  1145. /* SSL_CTX_use_certificate_chain_file() only works on PEM files */
  1146. cert_use_result = cert_blob ?
  1147. SSL_CTX_use_certificate_chain_blob(ctx, cert_blob, key_passwd) :
  1148. SSL_CTX_use_certificate_chain_file(ctx, cert_file);
  1149. if(cert_use_result != 1) {
  1150. failf(data,
  1151. "could not load PEM client certificate from %s, " OSSL_PACKAGE
  1152. " error %s, "
  1153. "(no key found, wrong pass phrase, or wrong file format?)",
  1154. (cert_blob ? "CURLOPT_SSLCERT_BLOB" : cert_file),
  1155. ossl_strerror(ERR_get_error(), error_buffer,
  1156. sizeof(error_buffer)) );
  1157. return 0;
  1158. }
  1159. break;
  1160. case SSL_FILETYPE_ASN1:
  1161. /* SSL_CTX_use_certificate_file() works with either PEM or ASN1, but
  1162. we use the case above for PEM so this can only be performed with
  1163. ASN1 files. */
  1164. cert_use_result = cert_blob ?
  1165. SSL_CTX_use_certificate_blob(ctx, cert_blob,
  1166. file_type, key_passwd) :
  1167. SSL_CTX_use_certificate_file(ctx, cert_file, file_type);
  1168. if(cert_use_result != 1) {
  1169. failf(data,
  1170. "could not load ASN1 client certificate from %s, " OSSL_PACKAGE
  1171. " error %s, "
  1172. "(no key found, wrong pass phrase, or wrong file format?)",
  1173. (cert_blob ? "CURLOPT_SSLCERT_BLOB" : cert_file),
  1174. ossl_strerror(ERR_get_error(), error_buffer,
  1175. sizeof(error_buffer)) );
  1176. return 0;
  1177. }
  1178. break;
  1179. case SSL_FILETYPE_ENGINE:
  1180. #if defined(USE_OPENSSL_ENGINE) && defined(ENGINE_CTRL_GET_CMD_FROM_NAME)
  1181. {
  1182. /* Implicitly use pkcs11 engine if none was provided and the
  1183. * cert_file is a PKCS#11 URI */
  1184. if(!data->state.engine) {
  1185. if(is_pkcs11_uri(cert_file)) {
  1186. if(ossl_set_engine(data, "pkcs11") != CURLE_OK) {
  1187. return 0;
  1188. }
  1189. }
  1190. }
  1191. if(data->state.engine) {
  1192. const char *cmd_name = "LOAD_CERT_CTRL";
  1193. struct {
  1194. const char *cert_id;
  1195. X509 *cert;
  1196. } params;
  1197. params.cert_id = cert_file;
  1198. params.cert = NULL;
  1199. /* Does the engine supports LOAD_CERT_CTRL ? */
  1200. if(!ENGINE_ctrl(data->state.engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
  1201. 0, (void *)cmd_name, NULL)) {
  1202. failf(data, "ssl engine does not support loading certificates");
  1203. return 0;
  1204. }
  1205. /* Load the certificate from the engine */
  1206. if(!ENGINE_ctrl_cmd(data->state.engine, cmd_name,
  1207. 0, &params, NULL, 1)) {
  1208. failf(data, "ssl engine cannot load client cert with id"
  1209. " '%s' [%s]", cert_file,
  1210. ossl_strerror(ERR_get_error(), error_buffer,
  1211. sizeof(error_buffer)));
  1212. return 0;
  1213. }
  1214. if(!params.cert) {
  1215. failf(data, "ssl engine didn't initialized the certificate "
  1216. "properly.");
  1217. return 0;
  1218. }
  1219. if(SSL_CTX_use_certificate(ctx, params.cert) != 1) {
  1220. failf(data, "unable to set client certificate [%s]",
  1221. ossl_strerror(ERR_get_error(), error_buffer,
  1222. sizeof(error_buffer)));
  1223. return 0;
  1224. }
  1225. X509_free(params.cert); /* we don't need the handle any more... */
  1226. }
  1227. else {
  1228. failf(data, "crypto engine not set, can't load certificate");
  1229. return 0;
  1230. }
  1231. }
  1232. break;
  1233. #else
  1234. failf(data, "file type ENG for certificate not implemented");
  1235. return 0;
  1236. #endif
  1237. case SSL_FILETYPE_PKCS12:
  1238. {
  1239. BIO *cert_bio = NULL;
  1240. PKCS12 *p12 = NULL;
  1241. EVP_PKEY *pri;
  1242. STACK_OF(X509) *ca = NULL;
  1243. if(cert_blob) {
  1244. cert_bio = BIO_new_mem_buf(cert_blob->data, (int)(cert_blob->len));
  1245. if(!cert_bio) {
  1246. failf(data,
  1247. "BIO_new_mem_buf NULL, " OSSL_PACKAGE
  1248. " error %s",
  1249. ossl_strerror(ERR_get_error(), error_buffer,
  1250. sizeof(error_buffer)) );
  1251. return 0;
  1252. }
  1253. }
  1254. else {
  1255. cert_bio = BIO_new(BIO_s_file());
  1256. if(!cert_bio) {
  1257. failf(data,
  1258. "BIO_new return NULL, " OSSL_PACKAGE
  1259. " error %s",
  1260. ossl_strerror(ERR_get_error(), error_buffer,
  1261. sizeof(error_buffer)) );
  1262. return 0;
  1263. }
  1264. if(BIO_read_filename(cert_bio, cert_file) <= 0) {
  1265. failf(data, "could not open PKCS12 file '%s'", cert_file);
  1266. BIO_free(cert_bio);
  1267. return 0;
  1268. }
  1269. }
  1270. p12 = d2i_PKCS12_bio(cert_bio, NULL);
  1271. BIO_free(cert_bio);
  1272. if(!p12) {
  1273. failf(data, "error reading PKCS12 file '%s'",
  1274. cert_blob ? "(memory blob)" : cert_file);
  1275. return 0;
  1276. }
  1277. PKCS12_PBE_add();
  1278. if(!PKCS12_parse(p12, key_passwd, &pri, &x509,
  1279. &ca)) {
  1280. failf(data,
  1281. "could not parse PKCS12 file, check password, " OSSL_PACKAGE
  1282. " error %s",
  1283. ossl_strerror(ERR_get_error(), error_buffer,
  1284. sizeof(error_buffer)) );
  1285. PKCS12_free(p12);
  1286. return 0;
  1287. }
  1288. PKCS12_free(p12);
  1289. if(SSL_CTX_use_certificate(ctx, x509) != 1) {
  1290. failf(data,
  1291. "could not load PKCS12 client certificate, " OSSL_PACKAGE
  1292. " error %s",
  1293. ossl_strerror(ERR_get_error(), error_buffer,
  1294. sizeof(error_buffer)) );
  1295. goto fail;
  1296. }
  1297. if(SSL_CTX_use_PrivateKey(ctx, pri) != 1) {
  1298. failf(data, "unable to use private key from PKCS12 file '%s'",
  1299. cert_file);
  1300. goto fail;
  1301. }
  1302. if(!SSL_CTX_check_private_key (ctx)) {
  1303. failf(data, "private key from PKCS12 file '%s' "
  1304. "does not match certificate in same file", cert_file);
  1305. goto fail;
  1306. }
  1307. /* Set Certificate Verification chain */
  1308. if(ca) {
  1309. while(sk_X509_num(ca)) {
  1310. /*
  1311. * Note that sk_X509_pop() is used below to make sure the cert is
  1312. * removed from the stack properly before getting passed to
  1313. * SSL_CTX_add_extra_chain_cert(), which takes ownership. Previously
  1314. * we used sk_X509_value() instead, but then we'd clean it in the
  1315. * subsequent sk_X509_pop_free() call.
  1316. */
  1317. X509 *x = sk_X509_pop(ca);
  1318. if(!SSL_CTX_add_client_CA(ctx, x)) {
  1319. X509_free(x);
  1320. failf(data, "cannot add certificate to client CA list");
  1321. goto fail;
  1322. }
  1323. if(!SSL_CTX_add_extra_chain_cert(ctx, x)) {
  1324. X509_free(x);
  1325. failf(data, "cannot add certificate to certificate chain");
  1326. goto fail;
  1327. }
  1328. }
  1329. }
  1330. cert_done = 1;
  1331. fail:
  1332. EVP_PKEY_free(pri);
  1333. X509_free(x509);
  1334. sk_X509_pop_free(ca, X509_free);
  1335. if(!cert_done)
  1336. return 0; /* failure! */
  1337. break;
  1338. }
  1339. default:
  1340. failf(data, "not supported file type '%s' for certificate", cert_type);
  1341. return 0;
  1342. }
  1343. if((!key_file) && (!key_blob)) {
  1344. key_file = cert_file;
  1345. key_blob = cert_blob;
  1346. }
  1347. else
  1348. file_type = do_file_type(key_type);
  1349. switch(file_type) {
  1350. case SSL_FILETYPE_PEM:
  1351. if(cert_done)
  1352. break;
  1353. FALLTHROUGH();
  1354. case SSL_FILETYPE_ASN1:
  1355. cert_use_result = key_blob ?
  1356. SSL_CTX_use_PrivateKey_blob(ctx, key_blob, file_type, key_passwd) :
  1357. SSL_CTX_use_PrivateKey_file(ctx, key_file, file_type);
  1358. if(cert_use_result != 1) {
  1359. failf(data, "unable to set private key file: '%s' type %s",
  1360. key_file?key_file:"(memory blob)", key_type?key_type:"PEM");
  1361. return 0;
  1362. }
  1363. break;
  1364. case SSL_FILETYPE_ENGINE:
  1365. #ifdef USE_OPENSSL_ENGINE
  1366. {
  1367. EVP_PKEY *priv_key = NULL;
  1368. /* Implicitly use pkcs11 engine if none was provided and the
  1369. * key_file is a PKCS#11 URI */
  1370. if(!data->state.engine) {
  1371. if(is_pkcs11_uri(key_file)) {
  1372. if(ossl_set_engine(data, "pkcs11") != CURLE_OK) {
  1373. return 0;
  1374. }
  1375. }
  1376. }
  1377. if(data->state.engine) {
  1378. UI_METHOD *ui_method =
  1379. UI_create_method((char *)"curl user interface");
  1380. if(!ui_method) {
  1381. failf(data, "unable do create " OSSL_PACKAGE
  1382. " user-interface method");
  1383. return 0;
  1384. }
  1385. UI_method_set_opener(ui_method, UI_method_get_opener(UI_OpenSSL()));
  1386. UI_method_set_closer(ui_method, UI_method_get_closer(UI_OpenSSL()));
  1387. UI_method_set_reader(ui_method, ssl_ui_reader);
  1388. UI_method_set_writer(ui_method, ssl_ui_writer);
  1389. priv_key = ENGINE_load_private_key(data->state.engine, key_file,
  1390. ui_method,
  1391. key_passwd);
  1392. UI_destroy_method(ui_method);
  1393. if(!priv_key) {
  1394. failf(data, "failed to load private key from crypto engine");
  1395. return 0;
  1396. }
  1397. if(SSL_CTX_use_PrivateKey(ctx, priv_key) != 1) {
  1398. failf(data, "unable to set private key");
  1399. EVP_PKEY_free(priv_key);
  1400. return 0;
  1401. }
  1402. EVP_PKEY_free(priv_key); /* we don't need the handle any more... */
  1403. }
  1404. else {
  1405. failf(data, "crypto engine not set, can't load private key");
  1406. return 0;
  1407. }
  1408. }
  1409. break;
  1410. #else
  1411. failf(data, "file type ENG for private key not supported");
  1412. return 0;
  1413. #endif
  1414. case SSL_FILETYPE_PKCS12:
  1415. if(!cert_done) {
  1416. failf(data, "file type P12 for private key not supported");
  1417. return 0;
  1418. }
  1419. break;
  1420. default:
  1421. failf(data, "not supported file type for private key");
  1422. return 0;
  1423. }
  1424. ssl = SSL_new(ctx);
  1425. if(!ssl) {
  1426. failf(data, "unable to create an SSL structure");
  1427. return 0;
  1428. }
  1429. x509 = SSL_get_certificate(ssl);
  1430. /* This version was provided by Evan Jordan and is supposed to not
  1431. leak memory as the previous version: */
  1432. if(x509) {
  1433. EVP_PKEY *pktmp = X509_get_pubkey(x509);
  1434. EVP_PKEY_copy_parameters(pktmp, SSL_get_privatekey(ssl));
  1435. EVP_PKEY_free(pktmp);
  1436. }
  1437. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_IS_BORINGSSL) && \
  1438. !defined(OPENSSL_NO_DEPRECATED_3_0)
  1439. {
  1440. /* If RSA is used, don't check the private key if its flags indicate
  1441. * it doesn't support it. */
  1442. EVP_PKEY *priv_key = SSL_get_privatekey(ssl);
  1443. int pktype;
  1444. #ifdef HAVE_OPAQUE_EVP_PKEY
  1445. pktype = EVP_PKEY_id(priv_key);
  1446. #else
  1447. pktype = priv_key->type;
  1448. #endif
  1449. if(pktype == EVP_PKEY_RSA) {
  1450. RSA *rsa = EVP_PKEY_get1_RSA(priv_key);
  1451. if(RSA_flags(rsa) & RSA_METHOD_FLAG_NO_CHECK)
  1452. check_privkey = FALSE;
  1453. RSA_free(rsa); /* Decrement reference count */
  1454. }
  1455. }
  1456. #endif
  1457. SSL_free(ssl);
  1458. /* If we are using DSA, we can copy the parameters from
  1459. * the private key */
  1460. if(check_privkey == TRUE) {
  1461. /* Now we know that a key and cert have been set against
  1462. * the SSL context */
  1463. if(!SSL_CTX_check_private_key(ctx)) {
  1464. failf(data, "Private key does not match the certificate public key");
  1465. return 0;
  1466. }
  1467. }
  1468. }
  1469. return 1;
  1470. }
  1471. CURLcode Curl_ossl_set_client_cert(struct Curl_easy *data, SSL_CTX *ctx,
  1472. char *cert_file,
  1473. const struct curl_blob *cert_blob,
  1474. const char *cert_type, char *key_file,
  1475. const struct curl_blob *key_blob,
  1476. const char *key_type, char *key_passwd)
  1477. {
  1478. int rv = cert_stuff(data, ctx, cert_file, cert_blob, cert_type, key_file,
  1479. key_blob, key_type, key_passwd);
  1480. if(rv != 1) {
  1481. return CURLE_SSL_CERTPROBLEM;
  1482. }
  1483. return CURLE_OK;
  1484. }
  1485. /* returns non-zero on failure */
  1486. static int x509_name_oneline(X509_NAME *a, char *buf, size_t size)
  1487. {
  1488. BIO *bio_out = BIO_new(BIO_s_mem());
  1489. BUF_MEM *biomem;
  1490. int rc;
  1491. if(!bio_out)
  1492. return 1; /* alloc failed! */
  1493. rc = X509_NAME_print_ex(bio_out, a, 0, XN_FLAG_SEP_SPLUS_SPC);
  1494. BIO_get_mem_ptr(bio_out, &biomem);
  1495. if((size_t)biomem->length < size)
  1496. size = biomem->length;
  1497. else
  1498. size--; /* don't overwrite the buffer end */
  1499. memcpy(buf, biomem->data, size);
  1500. buf[size] = 0;
  1501. BIO_free(bio_out);
  1502. return !rc;
  1503. }
  1504. /**
  1505. * Global SSL init
  1506. *
  1507. * @retval 0 error initializing SSL
  1508. * @retval 1 SSL initialized successfully
  1509. */
  1510. static int ossl_init(void)
  1511. {
  1512. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
  1513. (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)
  1514. const uint64_t flags =
  1515. #ifdef OPENSSL_INIT_ENGINE_ALL_BUILTIN
  1516. /* not present in BoringSSL */
  1517. OPENSSL_INIT_ENGINE_ALL_BUILTIN |
  1518. #endif
  1519. #ifdef CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG
  1520. OPENSSL_INIT_NO_LOAD_CONFIG |
  1521. #else
  1522. OPENSSL_INIT_LOAD_CONFIG |
  1523. #endif
  1524. 0;
  1525. OPENSSL_init_ssl(flags, NULL);
  1526. #else
  1527. OPENSSL_load_builtin_modules();
  1528. #ifdef USE_OPENSSL_ENGINE
  1529. ENGINE_load_builtin_engines();
  1530. #endif
  1531. /* CONF_MFLAGS_DEFAULT_SECTION was introduced some time between 0.9.8b and
  1532. 0.9.8e */
  1533. #ifndef CONF_MFLAGS_DEFAULT_SECTION
  1534. #define CONF_MFLAGS_DEFAULT_SECTION 0x0
  1535. #endif
  1536. #ifndef CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG
  1537. CONF_modules_load_file(NULL, NULL,
  1538. CONF_MFLAGS_DEFAULT_SECTION|
  1539. CONF_MFLAGS_IGNORE_MISSING_FILE);
  1540. #endif
  1541. /* Let's get nice error messages */
  1542. SSL_load_error_strings();
  1543. /* Init the global ciphers and digests */
  1544. if(!SSLeay_add_ssl_algorithms())
  1545. return 0;
  1546. OpenSSL_add_all_algorithms();
  1547. #endif
  1548. Curl_tls_keylog_open();
  1549. return 1;
  1550. }
  1551. /* Global cleanup */
  1552. static void ossl_cleanup(void)
  1553. {
  1554. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
  1555. (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)
  1556. /* OpenSSL 1.1 deprecates all these cleanup functions and
  1557. turns them into no-ops in OpenSSL 1.0 compatibility mode */
  1558. #else
  1559. /* Free ciphers and digests lists */
  1560. EVP_cleanup();
  1561. #ifdef USE_OPENSSL_ENGINE
  1562. /* Free engine list */
  1563. ENGINE_cleanup();
  1564. #endif
  1565. /* Free OpenSSL error strings */
  1566. ERR_free_strings();
  1567. /* Free thread local error state, destroying hash upon zero refcount */
  1568. #ifdef HAVE_ERR_REMOVE_THREAD_STATE
  1569. ERR_remove_thread_state(NULL);
  1570. #else
  1571. ERR_remove_state(0);
  1572. #endif
  1573. /* Free all memory allocated by all configuration modules */
  1574. CONF_modules_free();
  1575. #ifdef HAVE_SSL_COMP_FREE_COMPRESSION_METHODS
  1576. SSL_COMP_free_compression_methods();
  1577. #endif
  1578. #endif
  1579. Curl_tls_keylog_close();
  1580. }
  1581. /* Selects an OpenSSL crypto engine
  1582. */
  1583. static CURLcode ossl_set_engine(struct Curl_easy *data, const char *engine)
  1584. {
  1585. #ifdef USE_OPENSSL_ENGINE
  1586. ENGINE *e;
  1587. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  1588. e = ENGINE_by_id(engine);
  1589. #else
  1590. /* avoid memory leak */
  1591. for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) {
  1592. const char *e_id = ENGINE_get_id(e);
  1593. if(!strcmp(engine, e_id))
  1594. break;
  1595. }
  1596. #endif
  1597. if(!e) {
  1598. failf(data, "SSL Engine '%s' not found", engine);
  1599. return CURLE_SSL_ENGINE_NOTFOUND;
  1600. }
  1601. if(data->state.engine) {
  1602. ENGINE_finish(data->state.engine);
  1603. ENGINE_free(data->state.engine);
  1604. data->state.engine = NULL;
  1605. }
  1606. if(!ENGINE_init(e)) {
  1607. char buf[256];
  1608. ENGINE_free(e);
  1609. failf(data, "Failed to initialise SSL Engine '%s': %s",
  1610. engine, ossl_strerror(ERR_get_error(), buf, sizeof(buf)));
  1611. return CURLE_SSL_ENGINE_INITFAILED;
  1612. }
  1613. data->state.engine = e;
  1614. return CURLE_OK;
  1615. #else
  1616. (void)engine;
  1617. failf(data, "SSL Engine not supported");
  1618. return CURLE_SSL_ENGINE_NOTFOUND;
  1619. #endif
  1620. }
  1621. /* Sets engine as default for all SSL operations
  1622. */
  1623. static CURLcode ossl_set_engine_default(struct Curl_easy *data)
  1624. {
  1625. #ifdef USE_OPENSSL_ENGINE
  1626. if(data->state.engine) {
  1627. if(ENGINE_set_default(data->state.engine, ENGINE_METHOD_ALL) > 0) {
  1628. infof(data, "set default crypto engine '%s'",
  1629. ENGINE_get_id(data->state.engine));
  1630. }
  1631. else {
  1632. failf(data, "set default crypto engine '%s' failed",
  1633. ENGINE_get_id(data->state.engine));
  1634. return CURLE_SSL_ENGINE_SETFAILED;
  1635. }
  1636. }
  1637. #else
  1638. (void) data;
  1639. #endif
  1640. return CURLE_OK;
  1641. }
  1642. /* Return list of OpenSSL crypto engine names.
  1643. */
  1644. static struct curl_slist *ossl_engines_list(struct Curl_easy *data)
  1645. {
  1646. struct curl_slist *list = NULL;
  1647. #ifdef USE_OPENSSL_ENGINE
  1648. struct curl_slist *beg;
  1649. ENGINE *e;
  1650. for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) {
  1651. beg = curl_slist_append(list, ENGINE_get_id(e));
  1652. if(!beg) {
  1653. curl_slist_free_all(list);
  1654. return NULL;
  1655. }
  1656. list = beg;
  1657. }
  1658. #endif
  1659. (void) data;
  1660. return list;
  1661. }
  1662. static void ossl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1663. {
  1664. struct ssl_connect_data *connssl = cf->ctx;
  1665. struct ossl_ssl_backend_data *backend =
  1666. (struct ossl_ssl_backend_data *)connssl->backend;
  1667. (void)data;
  1668. DEBUGASSERT(backend);
  1669. if(backend->handle) {
  1670. /* Send the TLS shutdown if we are still connected *and* if
  1671. * the peer did not already close the connection. */
  1672. if(cf->next && cf->next->connected && !connssl->peer_closed) {
  1673. char buf[1024];
  1674. int nread, err;
  1675. long sslerr;
  1676. /* Maybe the server has already sent a close notify alert.
  1677. Read it to avoid an RST on the TCP connection. */
  1678. ERR_clear_error();
  1679. nread = SSL_read(backend->handle, buf, (int)sizeof(buf));
  1680. err = SSL_get_error(backend->handle, nread);
  1681. if(!nread && err == SSL_ERROR_ZERO_RETURN) {
  1682. CURLcode result;
  1683. ssize_t n;
  1684. size_t blen = sizeof(buf);
  1685. CURL_TRC_CF(data, cf, "peer has shutdown TLS");
  1686. /* SSL_read() will not longer touch the socket, let's receive
  1687. * directly from the next filter to see if the underlying
  1688. * connection has also been closed. */
  1689. n = Curl_conn_cf_recv(cf->next, data, buf, blen, &result);
  1690. if(!n) {
  1691. connssl->peer_closed = TRUE;
  1692. CURL_TRC_CF(data, cf, "peer closed connection");
  1693. }
  1694. }
  1695. ERR_clear_error();
  1696. if(connssl->peer_closed) {
  1697. /* As the peer closed, we do not expect it to read anything more we
  1698. * may send. It may be harmful, leading to TCP RST and delaying
  1699. * a lingering close. Just leave. */
  1700. CURL_TRC_CF(data, cf, "not from sending TLS shutdown on "
  1701. "connection closed by peer");
  1702. }
  1703. else if(SSL_shutdown(backend->handle) == 1) {
  1704. CURL_TRC_CF(data, cf, "SSL shutdown finished");
  1705. }
  1706. else {
  1707. nread = SSL_read(backend->handle, buf, (int)sizeof(buf));
  1708. err = SSL_get_error(backend->handle, nread);
  1709. switch(err) {
  1710. case SSL_ERROR_NONE: /* this is not an error */
  1711. case SSL_ERROR_ZERO_RETURN: /* no more data */
  1712. CURL_TRC_CF(data, cf, "SSL shutdown, EOF from server");
  1713. break;
  1714. case SSL_ERROR_WANT_READ:
  1715. /* SSL has send its notify and now wants to read the reply
  1716. * from the server. We are not really interested in that. */
  1717. CURL_TRC_CF(data, cf, "SSL shutdown sent");
  1718. break;
  1719. case SSL_ERROR_WANT_WRITE:
  1720. CURL_TRC_CF(data, cf, "SSL shutdown send blocked");
  1721. break;
  1722. default:
  1723. sslerr = ERR_get_error();
  1724. CURL_TRC_CF(data, cf, "SSL shutdown, error: '%s', errno %d",
  1725. (sslerr ?
  1726. ossl_strerror(sslerr, buf, sizeof(buf)) :
  1727. SSL_ERROR_to_str(err)),
  1728. SOCKERRNO);
  1729. break;
  1730. }
  1731. }
  1732. ERR_clear_error();
  1733. SSL_set_connect_state(backend->handle);
  1734. }
  1735. SSL_free(backend->handle);
  1736. backend->handle = NULL;
  1737. }
  1738. if(backend->ctx) {
  1739. SSL_CTX_free(backend->ctx);
  1740. backend->ctx = NULL;
  1741. backend->x509_store_setup = FALSE;
  1742. }
  1743. if(backend->bio_method) {
  1744. ossl_bio_cf_method_free(backend->bio_method);
  1745. backend->bio_method = NULL;
  1746. }
  1747. }
  1748. /*
  1749. * This function is called to shut down the SSL layer but keep the
  1750. * socket open (CCC - Clear Command Channel)
  1751. */
  1752. static int ossl_shutdown(struct Curl_cfilter *cf,
  1753. struct Curl_easy *data)
  1754. {
  1755. int retval = 0;
  1756. struct ssl_connect_data *connssl = cf->ctx;
  1757. char buf[256]; /* We will use this for the OpenSSL error buffer, so it has
  1758. to be at least 256 bytes long. */
  1759. unsigned long sslerror;
  1760. int nread;
  1761. int buffsize;
  1762. int err;
  1763. bool done = FALSE;
  1764. struct ossl_ssl_backend_data *backend =
  1765. (struct ossl_ssl_backend_data *)connssl->backend;
  1766. int loop = 10;
  1767. DEBUGASSERT(backend);
  1768. #ifndef CURL_DISABLE_FTP
  1769. /* This has only been tested on the proftpd server, and the mod_tls code
  1770. sends a close notify alert without waiting for a close notify alert in
  1771. response. Thus we wait for a close notify alert from the server, but
  1772. we do not send one. Let's hope other servers do the same... */
  1773. if(data->set.ftp_ccc == CURLFTPSSL_CCC_ACTIVE)
  1774. (void)SSL_shutdown(backend->handle);
  1775. #endif
  1776. if(backend->handle) {
  1777. buffsize = (int)sizeof(buf);
  1778. while(!done && loop--) {
  1779. int what = SOCKET_READABLE(Curl_conn_cf_get_socket(cf, data),
  1780. SSL_SHUTDOWN_TIMEOUT);
  1781. if(what > 0) {
  1782. ERR_clear_error();
  1783. /* Something to read, let's do it and hope that it is the close
  1784. notify alert from the server */
  1785. nread = SSL_read(backend->handle, buf, buffsize);
  1786. err = SSL_get_error(backend->handle, nread);
  1787. switch(err) {
  1788. case SSL_ERROR_NONE: /* this is not an error */
  1789. case SSL_ERROR_ZERO_RETURN: /* no more data */
  1790. /* This is the expected response. There was no data but only
  1791. the close notify alert */
  1792. done = TRUE;
  1793. break;
  1794. case SSL_ERROR_WANT_READ:
  1795. /* there's data pending, re-invoke SSL_read() */
  1796. infof(data, "SSL_ERROR_WANT_READ");
  1797. break;
  1798. case SSL_ERROR_WANT_WRITE:
  1799. /* SSL wants a write. Really odd. Let's bail out. */
  1800. infof(data, "SSL_ERROR_WANT_WRITE");
  1801. done = TRUE;
  1802. break;
  1803. default:
  1804. /* openssl/ssl.h says "look at error stack/return value/errno" */
  1805. sslerror = ERR_get_error();
  1806. failf(data, OSSL_PACKAGE " SSL_read on shutdown: %s, errno %d",
  1807. (sslerror ?
  1808. ossl_strerror(sslerror, buf, sizeof(buf)) :
  1809. SSL_ERROR_to_str(err)),
  1810. SOCKERRNO);
  1811. done = TRUE;
  1812. break;
  1813. }
  1814. }
  1815. else if(0 == what) {
  1816. /* timeout */
  1817. failf(data, "SSL shutdown timeout");
  1818. done = TRUE;
  1819. }
  1820. else {
  1821. /* anything that gets here is fatally bad */
  1822. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1823. retval = -1;
  1824. done = TRUE;
  1825. }
  1826. } /* while()-loop for the select() */
  1827. if(data->set.verbose) {
  1828. #ifdef HAVE_SSL_GET_SHUTDOWN
  1829. switch(SSL_get_shutdown(backend->handle)) {
  1830. case SSL_SENT_SHUTDOWN:
  1831. infof(data, "SSL_get_shutdown() returned SSL_SENT_SHUTDOWN");
  1832. break;
  1833. case SSL_RECEIVED_SHUTDOWN:
  1834. infof(data, "SSL_get_shutdown() returned SSL_RECEIVED_SHUTDOWN");
  1835. break;
  1836. case SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN:
  1837. infof(data, "SSL_get_shutdown() returned SSL_SENT_SHUTDOWN|"
  1838. "SSL_RECEIVED__SHUTDOWN");
  1839. break;
  1840. }
  1841. #endif
  1842. }
  1843. SSL_free(backend->handle);
  1844. backend->handle = NULL;
  1845. }
  1846. return retval;
  1847. }
  1848. static void ossl_session_free(void *ptr)
  1849. {
  1850. /* free the ID */
  1851. SSL_SESSION_free(ptr);
  1852. }
  1853. /*
  1854. * This function is called when the 'data' struct is going away. Close
  1855. * down everything and free all resources!
  1856. */
  1857. static void ossl_close_all(struct Curl_easy *data)
  1858. {
  1859. #ifdef USE_OPENSSL_ENGINE
  1860. if(data->state.engine) {
  1861. ENGINE_finish(data->state.engine);
  1862. ENGINE_free(data->state.engine);
  1863. data->state.engine = NULL;
  1864. }
  1865. #else
  1866. (void)data;
  1867. #endif
  1868. #if !defined(HAVE_ERR_REMOVE_THREAD_STATE_DEPRECATED) && \
  1869. defined(HAVE_ERR_REMOVE_THREAD_STATE)
  1870. /* OpenSSL 1.0.1 and 1.0.2 build an error queue that is stored per-thread
  1871. so we need to clean it here in case the thread will be killed. All OpenSSL
  1872. code should extract the error in association with the error so clearing
  1873. this queue here should be harmless at worst. */
  1874. ERR_remove_thread_state(NULL);
  1875. #endif
  1876. }
  1877. /* ====================================================== */
  1878. /*
  1879. * Match subjectAltName against the host name.
  1880. */
  1881. static bool subj_alt_hostcheck(struct Curl_easy *data,
  1882. const char *match_pattern,
  1883. size_t matchlen,
  1884. const char *hostname,
  1885. size_t hostlen,
  1886. const char *dispname)
  1887. {
  1888. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  1889. (void)dispname;
  1890. (void)data;
  1891. #endif
  1892. if(Curl_cert_hostcheck(match_pattern, matchlen, hostname, hostlen)) {
  1893. infof(data, " subjectAltName: host \"%s\" matched cert's \"%s\"",
  1894. dispname, match_pattern);
  1895. return TRUE;
  1896. }
  1897. return FALSE;
  1898. }
  1899. /* Quote from RFC2818 section 3.1 "Server Identity"
  1900. If a subjectAltName extension of type dNSName is present, that MUST
  1901. be used as the identity. Otherwise, the (most specific) Common Name
  1902. field in the Subject field of the certificate MUST be used. Although
  1903. the use of the Common Name is existing practice, it is deprecated and
  1904. Certification Authorities are encouraged to use the dNSName instead.
  1905. Matching is performed using the matching rules specified by
  1906. [RFC2459]. If more than one identity of a given type is present in
  1907. the certificate (e.g., more than one dNSName name, a match in any one
  1908. of the set is considered acceptable.) Names may contain the wildcard
  1909. character * which is considered to match any single domain name
  1910. component or component fragment. E.g., *.a.com matches foo.a.com but
  1911. not bar.foo.a.com. f*.com matches foo.com but not bar.com.
  1912. In some cases, the URI is specified as an IP address rather than a
  1913. hostname. In this case, the iPAddress subjectAltName must be present
  1914. in the certificate and must exactly match the IP in the URI.
  1915. This function is now used from ngtcp2 (QUIC) as well.
  1916. */
  1917. CURLcode Curl_ossl_verifyhost(struct Curl_easy *data, struct connectdata *conn,
  1918. struct ssl_peer *peer, X509 *server_cert)
  1919. {
  1920. bool matched = FALSE;
  1921. int target; /* target type, GEN_DNS or GEN_IPADD */
  1922. size_t addrlen = 0;
  1923. STACK_OF(GENERAL_NAME) *altnames;
  1924. #ifdef ENABLE_IPV6
  1925. struct in6_addr addr;
  1926. #else
  1927. struct in_addr addr;
  1928. #endif
  1929. CURLcode result = CURLE_OK;
  1930. bool dNSName = FALSE; /* if a dNSName field exists in the cert */
  1931. bool iPAddress = FALSE; /* if a iPAddress field exists in the cert */
  1932. size_t hostlen;
  1933. (void)conn;
  1934. hostlen = strlen(peer->hostname);
  1935. switch(peer->type) {
  1936. case CURL_SSL_PEER_IPV4:
  1937. if(!Curl_inet_pton(AF_INET, peer->hostname, &addr))
  1938. return CURLE_PEER_FAILED_VERIFICATION;
  1939. target = GEN_IPADD;
  1940. addrlen = sizeof(struct in_addr);
  1941. break;
  1942. #ifdef ENABLE_IPV6
  1943. case CURL_SSL_PEER_IPV6:
  1944. if(!Curl_inet_pton(AF_INET6, peer->hostname, &addr))
  1945. return CURLE_PEER_FAILED_VERIFICATION;
  1946. target = GEN_IPADD;
  1947. addrlen = sizeof(struct in6_addr);
  1948. break;
  1949. #endif
  1950. case CURL_SSL_PEER_DNS:
  1951. target = GEN_DNS;
  1952. break;
  1953. default:
  1954. DEBUGASSERT(0);
  1955. failf(data, "unexpected ssl peer type: %d", peer->type);
  1956. return CURLE_PEER_FAILED_VERIFICATION;
  1957. }
  1958. /* get a "list" of alternative names */
  1959. altnames = X509_get_ext_d2i(server_cert, NID_subject_alt_name, NULL, NULL);
  1960. if(altnames) {
  1961. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  1962. size_t numalts;
  1963. size_t i;
  1964. #else
  1965. int numalts;
  1966. int i;
  1967. #endif
  1968. bool dnsmatched = FALSE;
  1969. bool ipmatched = FALSE;
  1970. /* get amount of alternatives, RFC2459 claims there MUST be at least
  1971. one, but we don't depend on it... */
  1972. numalts = sk_GENERAL_NAME_num(altnames);
  1973. /* loop through all alternatives - until a dnsmatch */
  1974. for(i = 0; (i < numalts) && !dnsmatched; i++) {
  1975. /* get a handle to alternative name number i */
  1976. const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i);
  1977. if(check->type == GEN_DNS)
  1978. dNSName = TRUE;
  1979. else if(check->type == GEN_IPADD)
  1980. iPAddress = TRUE;
  1981. /* only check alternatives of the same type the target is */
  1982. if(check->type == target) {
  1983. /* get data and length */
  1984. const char *altptr = (char *)ASN1_STRING_get0_data(check->d.ia5);
  1985. size_t altlen = (size_t) ASN1_STRING_length(check->d.ia5);
  1986. switch(target) {
  1987. case GEN_DNS: /* name/pattern comparison */
  1988. /* The OpenSSL man page explicitly says: "In general it cannot be
  1989. assumed that the data returned by ASN1_STRING_data() is null
  1990. terminated or does not contain embedded nulls." But also that
  1991. "The actual format of the data will depend on the actual string
  1992. type itself: for example for an IA5String the data will be ASCII"
  1993. It has been however verified that in 0.9.6 and 0.9.7, IA5String
  1994. is always null-terminated.
  1995. */
  1996. if((altlen == strlen(altptr)) &&
  1997. /* if this isn't true, there was an embedded zero in the name
  1998. string and we cannot match it. */
  1999. subj_alt_hostcheck(data, altptr, altlen,
  2000. peer->hostname, hostlen,
  2001. peer->dispname)) {
  2002. dnsmatched = TRUE;
  2003. }
  2004. break;
  2005. case GEN_IPADD: /* IP address comparison */
  2006. /* compare alternative IP address if the data chunk is the same size
  2007. our server IP address is */
  2008. if((altlen == addrlen) && !memcmp(altptr, &addr, altlen)) {
  2009. ipmatched = TRUE;
  2010. infof(data,
  2011. " subjectAltName: host \"%s\" matched cert's IP address!",
  2012. peer->dispname);
  2013. }
  2014. break;
  2015. }
  2016. }
  2017. }
  2018. GENERAL_NAMES_free(altnames);
  2019. if(dnsmatched || ipmatched)
  2020. matched = TRUE;
  2021. }
  2022. if(matched)
  2023. /* an alternative name matched */
  2024. ;
  2025. else if(dNSName || iPAddress) {
  2026. const char *tname = (peer->type == CURL_SSL_PEER_DNS) ? "host name" :
  2027. (peer->type == CURL_SSL_PEER_IPV4) ?
  2028. "ipv4 address" : "ipv6 address";
  2029. infof(data, " subjectAltName does not match %s %s", tname, peer->dispname);
  2030. failf(data, "SSL: no alternative certificate subject name matches "
  2031. "target %s '%s'", tname, peer->dispname);
  2032. result = CURLE_PEER_FAILED_VERIFICATION;
  2033. }
  2034. else {
  2035. /* we have to look to the last occurrence of a commonName in the
  2036. distinguished one to get the most significant one. */
  2037. int i = -1;
  2038. unsigned char *peer_CN = NULL;
  2039. int peerlen = 0;
  2040. /* The following is done because of a bug in 0.9.6b */
  2041. X509_NAME *name = X509_get_subject_name(server_cert);
  2042. if(name) {
  2043. int j;
  2044. while((j = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
  2045. i = j;
  2046. }
  2047. /* we have the name entry and we will now convert this to a string
  2048. that we can use for comparison. Doing this we support BMPstring,
  2049. UTF8, etc. */
  2050. if(i >= 0) {
  2051. ASN1_STRING *tmp =
  2052. X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
  2053. /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input
  2054. is already UTF-8 encoded. We check for this case and copy the raw
  2055. string manually to avoid the problem. This code can be made
  2056. conditional in the future when OpenSSL has been fixed. */
  2057. if(tmp) {
  2058. if(ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) {
  2059. peerlen = ASN1_STRING_length(tmp);
  2060. if(peerlen >= 0) {
  2061. peer_CN = OPENSSL_malloc(peerlen + 1);
  2062. if(peer_CN) {
  2063. memcpy(peer_CN, ASN1_STRING_get0_data(tmp), peerlen);
  2064. peer_CN[peerlen] = '\0';
  2065. }
  2066. else
  2067. result = CURLE_OUT_OF_MEMORY;
  2068. }
  2069. }
  2070. else /* not a UTF8 name */
  2071. peerlen = ASN1_STRING_to_UTF8(&peer_CN, tmp);
  2072. if(peer_CN && (curlx_uztosi(strlen((char *)peer_CN)) != peerlen)) {
  2073. /* there was a terminating zero before the end of string, this
  2074. cannot match and we return failure! */
  2075. failf(data, "SSL: illegal cert name field");
  2076. result = CURLE_PEER_FAILED_VERIFICATION;
  2077. }
  2078. }
  2079. }
  2080. if(result)
  2081. /* error already detected, pass through */
  2082. ;
  2083. else if(!peer_CN) {
  2084. failf(data,
  2085. "SSL: unable to obtain common name from peer certificate");
  2086. result = CURLE_PEER_FAILED_VERIFICATION;
  2087. }
  2088. else if(!Curl_cert_hostcheck((const char *)peer_CN,
  2089. peerlen, peer->hostname, hostlen)) {
  2090. failf(data, "SSL: certificate subject name '%s' does not match "
  2091. "target host name '%s'", peer_CN, peer->dispname);
  2092. result = CURLE_PEER_FAILED_VERIFICATION;
  2093. }
  2094. else {
  2095. infof(data, " common name: %s (matched)", peer_CN);
  2096. }
  2097. if(peer_CN)
  2098. OPENSSL_free(peer_CN);
  2099. }
  2100. return result;
  2101. }
  2102. #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
  2103. !defined(OPENSSL_NO_OCSP)
  2104. static CURLcode verifystatus(struct Curl_cfilter *cf,
  2105. struct Curl_easy *data)
  2106. {
  2107. struct ssl_connect_data *connssl = cf->ctx;
  2108. int i, ocsp_status;
  2109. #if defined(OPENSSL_IS_AWSLC)
  2110. const uint8_t *status;
  2111. #else
  2112. unsigned char *status;
  2113. #endif
  2114. const unsigned char *p;
  2115. CURLcode result = CURLE_OK;
  2116. OCSP_RESPONSE *rsp = NULL;
  2117. OCSP_BASICRESP *br = NULL;
  2118. X509_STORE *st = NULL;
  2119. STACK_OF(X509) *ch = NULL;
  2120. struct ossl_ssl_backend_data *backend =
  2121. (struct ossl_ssl_backend_data *)connssl->backend;
  2122. X509 *cert;
  2123. OCSP_CERTID *id = NULL;
  2124. int cert_status, crl_reason;
  2125. ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
  2126. int ret;
  2127. long len;
  2128. DEBUGASSERT(backend);
  2129. len = SSL_get_tlsext_status_ocsp_resp(backend->handle, &status);
  2130. if(!status) {
  2131. failf(data, "No OCSP response received");
  2132. result = CURLE_SSL_INVALIDCERTSTATUS;
  2133. goto end;
  2134. }
  2135. p = status;
  2136. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  2137. if(!rsp) {
  2138. failf(data, "Invalid OCSP response");
  2139. result = CURLE_SSL_INVALIDCERTSTATUS;
  2140. goto end;
  2141. }
  2142. ocsp_status = OCSP_response_status(rsp);
  2143. if(ocsp_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  2144. failf(data, "Invalid OCSP response status: %s (%d)",
  2145. OCSP_response_status_str(ocsp_status), ocsp_status);
  2146. result = CURLE_SSL_INVALIDCERTSTATUS;
  2147. goto end;
  2148. }
  2149. br = OCSP_response_get1_basic(rsp);
  2150. if(!br) {
  2151. failf(data, "Invalid OCSP response");
  2152. result = CURLE_SSL_INVALIDCERTSTATUS;
  2153. goto end;
  2154. }
  2155. ch = SSL_get_peer_cert_chain(backend->handle);
  2156. if(!ch) {
  2157. failf(data, "Could not get peer certificate chain");
  2158. result = CURLE_SSL_INVALIDCERTSTATUS;
  2159. goto end;
  2160. }
  2161. st = SSL_CTX_get_cert_store(backend->ctx);
  2162. #if ((OPENSSL_VERSION_NUMBER <= 0x1000201fL) /* Fixed after 1.0.2a */ || \
  2163. (defined(LIBRESSL_VERSION_NUMBER) && \
  2164. LIBRESSL_VERSION_NUMBER <= 0x2040200fL))
  2165. /* The authorized responder cert in the OCSP response MUST be signed by the
  2166. peer cert's issuer (see RFC6960 section 4.2.2.2). If that's a root cert,
  2167. no problem, but if it's an intermediate cert OpenSSL has a bug where it
  2168. expects this issuer to be present in the chain embedded in the OCSP
  2169. response. So we add it if necessary. */
  2170. /* First make sure the peer cert chain includes both a peer and an issuer,
  2171. and the OCSP response contains a responder cert. */
  2172. if(sk_X509_num(ch) >= 2 && sk_X509_num(br->certs) >= 1) {
  2173. X509 *responder = sk_X509_value(br->certs, sk_X509_num(br->certs) - 1);
  2174. /* Find issuer of responder cert and add it to the OCSP response chain */
  2175. for(i = 0; i < sk_X509_num(ch); i++) {
  2176. X509 *issuer = sk_X509_value(ch, i);
  2177. if(X509_check_issued(issuer, responder) == X509_V_OK) {
  2178. if(!OCSP_basic_add1_cert(br, issuer)) {
  2179. failf(data, "Could not add issuer cert to OCSP response");
  2180. result = CURLE_SSL_INVALIDCERTSTATUS;
  2181. goto end;
  2182. }
  2183. }
  2184. }
  2185. }
  2186. #endif
  2187. if(OCSP_basic_verify(br, ch, st, 0) <= 0) {
  2188. failf(data, "OCSP response verification failed");
  2189. result = CURLE_SSL_INVALIDCERTSTATUS;
  2190. goto end;
  2191. }
  2192. /* Compute the certificate's ID */
  2193. cert = SSL_get1_peer_certificate(backend->handle);
  2194. if(!cert) {
  2195. failf(data, "Error getting peer certificate");
  2196. result = CURLE_SSL_INVALIDCERTSTATUS;
  2197. goto end;
  2198. }
  2199. for(i = 0; i < (int)sk_X509_num(ch); i++) {
  2200. X509 *issuer = sk_X509_value(ch, i);
  2201. if(X509_check_issued(issuer, cert) == X509_V_OK) {
  2202. id = OCSP_cert_to_id(EVP_sha1(), cert, issuer);
  2203. break;
  2204. }
  2205. }
  2206. X509_free(cert);
  2207. if(!id) {
  2208. failf(data, "Error computing OCSP ID");
  2209. result = CURLE_SSL_INVALIDCERTSTATUS;
  2210. goto end;
  2211. }
  2212. /* Find the single OCSP response corresponding to the certificate ID */
  2213. ret = OCSP_resp_find_status(br, id, &cert_status, &crl_reason, &rev,
  2214. &thisupd, &nextupd);
  2215. OCSP_CERTID_free(id);
  2216. if(ret != 1) {
  2217. failf(data, "Could not find certificate ID in OCSP response");
  2218. result = CURLE_SSL_INVALIDCERTSTATUS;
  2219. goto end;
  2220. }
  2221. /* Validate the corresponding single OCSP response */
  2222. if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) {
  2223. failf(data, "OCSP response has expired");
  2224. result = CURLE_SSL_INVALIDCERTSTATUS;
  2225. goto end;
  2226. }
  2227. infof(data, "SSL certificate status: %s (%d)",
  2228. OCSP_cert_status_str(cert_status), cert_status);
  2229. switch(cert_status) {
  2230. case V_OCSP_CERTSTATUS_GOOD:
  2231. break;
  2232. case V_OCSP_CERTSTATUS_REVOKED:
  2233. result = CURLE_SSL_INVALIDCERTSTATUS;
  2234. failf(data, "SSL certificate revocation reason: %s (%d)",
  2235. OCSP_crl_reason_str(crl_reason), crl_reason);
  2236. goto end;
  2237. case V_OCSP_CERTSTATUS_UNKNOWN:
  2238. default:
  2239. result = CURLE_SSL_INVALIDCERTSTATUS;
  2240. goto end;
  2241. }
  2242. end:
  2243. if(br)
  2244. OCSP_BASICRESP_free(br);
  2245. OCSP_RESPONSE_free(rsp);
  2246. return result;
  2247. }
  2248. #endif
  2249. #endif /* USE_OPENSSL */
  2250. /* The SSL_CTRL_SET_MSG_CALLBACK doesn't exist in ancient OpenSSL versions
  2251. and thus this cannot be done there. */
  2252. #ifdef SSL_CTRL_SET_MSG_CALLBACK
  2253. static const char *ssl_msg_type(int ssl_ver, int msg)
  2254. {
  2255. #ifdef SSL2_VERSION_MAJOR
  2256. if(ssl_ver == SSL2_VERSION_MAJOR) {
  2257. switch(msg) {
  2258. case SSL2_MT_ERROR:
  2259. return "Error";
  2260. case SSL2_MT_CLIENT_HELLO:
  2261. return "Client hello";
  2262. case SSL2_MT_CLIENT_MASTER_KEY:
  2263. return "Client key";
  2264. case SSL2_MT_CLIENT_FINISHED:
  2265. return "Client finished";
  2266. case SSL2_MT_SERVER_HELLO:
  2267. return "Server hello";
  2268. case SSL2_MT_SERVER_VERIFY:
  2269. return "Server verify";
  2270. case SSL2_MT_SERVER_FINISHED:
  2271. return "Server finished";
  2272. case SSL2_MT_REQUEST_CERTIFICATE:
  2273. return "Request CERT";
  2274. case SSL2_MT_CLIENT_CERTIFICATE:
  2275. return "Client CERT";
  2276. }
  2277. }
  2278. else
  2279. #endif
  2280. if(ssl_ver == SSL3_VERSION_MAJOR) {
  2281. switch(msg) {
  2282. case SSL3_MT_HELLO_REQUEST:
  2283. return "Hello request";
  2284. case SSL3_MT_CLIENT_HELLO:
  2285. return "Client hello";
  2286. case SSL3_MT_SERVER_HELLO:
  2287. return "Server hello";
  2288. #ifdef SSL3_MT_NEWSESSION_TICKET
  2289. case SSL3_MT_NEWSESSION_TICKET:
  2290. return "Newsession Ticket";
  2291. #endif
  2292. case SSL3_MT_CERTIFICATE:
  2293. return "Certificate";
  2294. case SSL3_MT_SERVER_KEY_EXCHANGE:
  2295. return "Server key exchange";
  2296. case SSL3_MT_CLIENT_KEY_EXCHANGE:
  2297. return "Client key exchange";
  2298. case SSL3_MT_CERTIFICATE_REQUEST:
  2299. return "Request CERT";
  2300. case SSL3_MT_SERVER_DONE:
  2301. return "Server finished";
  2302. case SSL3_MT_CERTIFICATE_VERIFY:
  2303. return "CERT verify";
  2304. case SSL3_MT_FINISHED:
  2305. return "Finished";
  2306. #ifdef SSL3_MT_CERTIFICATE_STATUS
  2307. case SSL3_MT_CERTIFICATE_STATUS:
  2308. return "Certificate Status";
  2309. #endif
  2310. #ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
  2311. case SSL3_MT_ENCRYPTED_EXTENSIONS:
  2312. return "Encrypted Extensions";
  2313. #endif
  2314. #ifdef SSL3_MT_SUPPLEMENTAL_DATA
  2315. case SSL3_MT_SUPPLEMENTAL_DATA:
  2316. return "Supplemental data";
  2317. #endif
  2318. #ifdef SSL3_MT_END_OF_EARLY_DATA
  2319. case SSL3_MT_END_OF_EARLY_DATA:
  2320. return "End of early data";
  2321. #endif
  2322. #ifdef SSL3_MT_KEY_UPDATE
  2323. case SSL3_MT_KEY_UPDATE:
  2324. return "Key update";
  2325. #endif
  2326. #ifdef SSL3_MT_NEXT_PROTO
  2327. case SSL3_MT_NEXT_PROTO:
  2328. return "Next protocol";
  2329. #endif
  2330. #ifdef SSL3_MT_MESSAGE_HASH
  2331. case SSL3_MT_MESSAGE_HASH:
  2332. return "Message hash";
  2333. #endif
  2334. }
  2335. }
  2336. return "Unknown";
  2337. }
  2338. static const char *tls_rt_type(int type)
  2339. {
  2340. switch(type) {
  2341. #ifdef SSL3_RT_HEADER
  2342. case SSL3_RT_HEADER:
  2343. return "TLS header";
  2344. #endif
  2345. case SSL3_RT_CHANGE_CIPHER_SPEC:
  2346. return "TLS change cipher";
  2347. case SSL3_RT_ALERT:
  2348. return "TLS alert";
  2349. case SSL3_RT_HANDSHAKE:
  2350. return "TLS handshake";
  2351. case SSL3_RT_APPLICATION_DATA:
  2352. return "TLS app data";
  2353. default:
  2354. return "TLS Unknown";
  2355. }
  2356. }
  2357. /*
  2358. * Our callback from the SSL/TLS layers.
  2359. */
  2360. static void ossl_trace(int direction, int ssl_ver, int content_type,
  2361. const void *buf, size_t len, SSL *ssl,
  2362. void *userp)
  2363. {
  2364. const char *verstr = "???";
  2365. struct Curl_cfilter *cf = userp;
  2366. struct Curl_easy *data = NULL;
  2367. char unknown[32];
  2368. if(!cf)
  2369. return;
  2370. data = CF_DATA_CURRENT(cf);
  2371. if(!data || !data->set.fdebug || (direction && direction != 1))
  2372. return;
  2373. switch(ssl_ver) {
  2374. #ifdef SSL2_VERSION /* removed in recent versions */
  2375. case SSL2_VERSION:
  2376. verstr = "SSLv2";
  2377. break;
  2378. #endif
  2379. #ifdef SSL3_VERSION
  2380. case SSL3_VERSION:
  2381. verstr = "SSLv3";
  2382. break;
  2383. #endif
  2384. case TLS1_VERSION:
  2385. verstr = "TLSv1.0";
  2386. break;
  2387. #ifdef TLS1_1_VERSION
  2388. case TLS1_1_VERSION:
  2389. verstr = "TLSv1.1";
  2390. break;
  2391. #endif
  2392. #ifdef TLS1_2_VERSION
  2393. case TLS1_2_VERSION:
  2394. verstr = "TLSv1.2";
  2395. break;
  2396. #endif
  2397. #ifdef TLS1_3_VERSION
  2398. case TLS1_3_VERSION:
  2399. verstr = "TLSv1.3";
  2400. break;
  2401. #endif
  2402. case 0:
  2403. break;
  2404. default:
  2405. msnprintf(unknown, sizeof(unknown), "(%x)", ssl_ver);
  2406. verstr = unknown;
  2407. break;
  2408. }
  2409. /* Log progress for interesting records only (like Handshake or Alert), skip
  2410. * all raw record headers (content_type == SSL3_RT_HEADER or ssl_ver == 0).
  2411. * For TLS 1.3, skip notification of the decrypted inner Content-Type.
  2412. */
  2413. if(ssl_ver
  2414. #ifdef SSL3_RT_HEADER
  2415. && content_type != SSL3_RT_HEADER
  2416. #endif
  2417. #ifdef SSL3_RT_INNER_CONTENT_TYPE
  2418. && content_type != SSL3_RT_INNER_CONTENT_TYPE
  2419. #endif
  2420. ) {
  2421. const char *msg_name, *tls_rt_name;
  2422. char ssl_buf[1024];
  2423. int msg_type, txt_len;
  2424. /* the info given when the version is zero is not that useful for us */
  2425. ssl_ver >>= 8; /* check the upper 8 bits only below */
  2426. /* SSLv2 doesn't seem to have TLS record-type headers, so OpenSSL
  2427. * always pass-up content-type as 0. But the interesting message-type
  2428. * is at 'buf[0]'.
  2429. */
  2430. if(ssl_ver == SSL3_VERSION_MAJOR && content_type)
  2431. tls_rt_name = tls_rt_type(content_type);
  2432. else
  2433. tls_rt_name = "";
  2434. if(content_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  2435. msg_type = *(char *)buf;
  2436. msg_name = "Change cipher spec";
  2437. }
  2438. else if(content_type == SSL3_RT_ALERT) {
  2439. msg_type = (((char *)buf)[0] << 8) + ((char *)buf)[1];
  2440. msg_name = SSL_alert_desc_string_long(msg_type);
  2441. }
  2442. else {
  2443. msg_type = *(char *)buf;
  2444. msg_name = ssl_msg_type(ssl_ver, msg_type);
  2445. }
  2446. txt_len = msnprintf(ssl_buf, sizeof(ssl_buf),
  2447. "%s (%s), %s, %s (%d):\n",
  2448. verstr, direction?"OUT":"IN",
  2449. tls_rt_name, msg_name, msg_type);
  2450. if(0 <= txt_len && (unsigned)txt_len < sizeof(ssl_buf)) {
  2451. Curl_debug(data, CURLINFO_TEXT, ssl_buf, (size_t)txt_len);
  2452. }
  2453. }
  2454. Curl_debug(data, (direction == 1) ? CURLINFO_SSL_DATA_OUT :
  2455. CURLINFO_SSL_DATA_IN, (char *)buf, len);
  2456. (void) ssl;
  2457. }
  2458. #endif
  2459. #ifdef USE_OPENSSL
  2460. /* ====================================================== */
  2461. /* Check for OpenSSL 1.0.2 which has ALPN support. */
  2462. #undef HAS_ALPN
  2463. #if OPENSSL_VERSION_NUMBER >= 0x10002000L \
  2464. && !defined(OPENSSL_NO_TLSEXT)
  2465. # define HAS_ALPN 1
  2466. #endif
  2467. /* Check for OpenSSL 1.1.0 which has set_{min,max}_proto_version(). */
  2468. #undef HAS_MODERN_SET_PROTO_VER
  2469. #if OPENSSL_VERSION_NUMBER >= 0x10100000L \
  2470. && !(defined(LIBRESSL_VERSION_NUMBER) && \
  2471. LIBRESSL_VERSION_NUMBER < 0x20600000L)
  2472. # define HAS_MODERN_SET_PROTO_VER 1
  2473. #endif
  2474. #ifdef HAS_MODERN_SET_PROTO_VER
  2475. static CURLcode
  2476. ossl_set_ssl_version_min_max(struct Curl_cfilter *cf, SSL_CTX *ctx)
  2477. {
  2478. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  2479. /* first, TLS min version... */
  2480. long curl_ssl_version_min = conn_config->version;
  2481. long curl_ssl_version_max;
  2482. /* convert curl min SSL version option to OpenSSL constant */
  2483. #if (defined(OPENSSL_IS_BORINGSSL) || \
  2484. defined(OPENSSL_IS_AWSLC) || \
  2485. defined(LIBRESSL_VERSION_NUMBER))
  2486. uint16_t ossl_ssl_version_min = 0;
  2487. uint16_t ossl_ssl_version_max = 0;
  2488. #else
  2489. long ossl_ssl_version_min = 0;
  2490. long ossl_ssl_version_max = 0;
  2491. #endif
  2492. switch(curl_ssl_version_min) {
  2493. case CURL_SSLVERSION_TLSv1: /* TLS 1.x */
  2494. case CURL_SSLVERSION_TLSv1_0:
  2495. ossl_ssl_version_min = TLS1_VERSION;
  2496. break;
  2497. case CURL_SSLVERSION_TLSv1_1:
  2498. ossl_ssl_version_min = TLS1_1_VERSION;
  2499. break;
  2500. case CURL_SSLVERSION_TLSv1_2:
  2501. ossl_ssl_version_min = TLS1_2_VERSION;
  2502. break;
  2503. case CURL_SSLVERSION_TLSv1_3:
  2504. #ifdef TLS1_3_VERSION
  2505. ossl_ssl_version_min = TLS1_3_VERSION;
  2506. break;
  2507. #else
  2508. return CURLE_NOT_BUILT_IN;
  2509. #endif
  2510. }
  2511. /* CURL_SSLVERSION_DEFAULT means that no option was selected.
  2512. We don't want to pass 0 to SSL_CTX_set_min_proto_version as
  2513. it would enable all versions down to the lowest supported by
  2514. the library.
  2515. So we skip this, and stay with the library default
  2516. */
  2517. if(curl_ssl_version_min != CURL_SSLVERSION_DEFAULT) {
  2518. if(!SSL_CTX_set_min_proto_version(ctx, ossl_ssl_version_min)) {
  2519. return CURLE_SSL_CONNECT_ERROR;
  2520. }
  2521. }
  2522. /* ... then, TLS max version */
  2523. curl_ssl_version_max = conn_config->version_max;
  2524. /* convert curl max SSL version option to OpenSSL constant */
  2525. switch(curl_ssl_version_max) {
  2526. case CURL_SSLVERSION_MAX_TLSv1_0:
  2527. ossl_ssl_version_max = TLS1_VERSION;
  2528. break;
  2529. case CURL_SSLVERSION_MAX_TLSv1_1:
  2530. ossl_ssl_version_max = TLS1_1_VERSION;
  2531. break;
  2532. case CURL_SSLVERSION_MAX_TLSv1_2:
  2533. ossl_ssl_version_max = TLS1_2_VERSION;
  2534. break;
  2535. #ifdef TLS1_3_VERSION
  2536. case CURL_SSLVERSION_MAX_TLSv1_3:
  2537. ossl_ssl_version_max = TLS1_3_VERSION;
  2538. break;
  2539. #endif
  2540. case CURL_SSLVERSION_MAX_NONE: /* none selected */
  2541. case CURL_SSLVERSION_MAX_DEFAULT: /* max selected */
  2542. default:
  2543. /* SSL_CTX_set_max_proto_version states that:
  2544. setting the maximum to 0 will enable
  2545. protocol versions up to the highest version
  2546. supported by the library */
  2547. ossl_ssl_version_max = 0;
  2548. break;
  2549. }
  2550. if(!SSL_CTX_set_max_proto_version(ctx, ossl_ssl_version_max)) {
  2551. return CURLE_SSL_CONNECT_ERROR;
  2552. }
  2553. return CURLE_OK;
  2554. }
  2555. #endif /* HAS_MODERN_SET_PROTO_VER */
  2556. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  2557. typedef uint32_t ctx_option_t;
  2558. #elif OPENSSL_VERSION_NUMBER >= 0x30000000L
  2559. typedef uint64_t ctx_option_t;
  2560. #else
  2561. typedef long ctx_option_t;
  2562. #endif
  2563. #if !defined(HAS_MODERN_SET_PROTO_VER)
  2564. static CURLcode
  2565. ossl_set_ssl_version_min_max_legacy(ctx_option_t *ctx_options,
  2566. struct Curl_cfilter *cf,
  2567. struct Curl_easy *data)
  2568. {
  2569. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  2570. long ssl_version = conn_config->version;
  2571. long ssl_version_max = conn_config->version_max;
  2572. (void) data; /* In case it's unused. */
  2573. switch(ssl_version) {
  2574. case CURL_SSLVERSION_TLSv1_3:
  2575. #ifdef TLS1_3_VERSION
  2576. {
  2577. struct ssl_connect_data *connssl = cf->ctx;
  2578. struct ossl_ssl_backend_data *backend =
  2579. (struct ossl_ssl_backend_data *)connssl->backend;
  2580. DEBUGASSERT(backend);
  2581. SSL_CTX_set_max_proto_version(backend->ctx, TLS1_3_VERSION);
  2582. *ctx_options |= SSL_OP_NO_TLSv1_2;
  2583. }
  2584. #else
  2585. (void)ctx_options;
  2586. failf(data, OSSL_PACKAGE " was built without TLS 1.3 support");
  2587. return CURLE_NOT_BUILT_IN;
  2588. #endif
  2589. FALLTHROUGH();
  2590. case CURL_SSLVERSION_TLSv1_2:
  2591. #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
  2592. *ctx_options |= SSL_OP_NO_TLSv1_1;
  2593. #else
  2594. failf(data, OSSL_PACKAGE " was built without TLS 1.2 support");
  2595. return CURLE_NOT_BUILT_IN;
  2596. #endif
  2597. FALLTHROUGH();
  2598. case CURL_SSLVERSION_TLSv1_1:
  2599. #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
  2600. *ctx_options |= SSL_OP_NO_TLSv1;
  2601. #else
  2602. failf(data, OSSL_PACKAGE " was built without TLS 1.1 support");
  2603. return CURLE_NOT_BUILT_IN;
  2604. #endif
  2605. FALLTHROUGH();
  2606. case CURL_SSLVERSION_TLSv1_0:
  2607. case CURL_SSLVERSION_TLSv1:
  2608. break;
  2609. }
  2610. switch(ssl_version_max) {
  2611. case CURL_SSLVERSION_MAX_TLSv1_0:
  2612. #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
  2613. *ctx_options |= SSL_OP_NO_TLSv1_1;
  2614. #endif
  2615. FALLTHROUGH();
  2616. case CURL_SSLVERSION_MAX_TLSv1_1:
  2617. #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
  2618. *ctx_options |= SSL_OP_NO_TLSv1_2;
  2619. #endif
  2620. FALLTHROUGH();
  2621. case CURL_SSLVERSION_MAX_TLSv1_2:
  2622. #ifdef TLS1_3_VERSION
  2623. *ctx_options |= SSL_OP_NO_TLSv1_3;
  2624. #endif
  2625. break;
  2626. case CURL_SSLVERSION_MAX_TLSv1_3:
  2627. #ifdef TLS1_3_VERSION
  2628. break;
  2629. #else
  2630. failf(data, OSSL_PACKAGE " was built without TLS 1.3 support");
  2631. return CURLE_NOT_BUILT_IN;
  2632. #endif
  2633. }
  2634. return CURLE_OK;
  2635. }
  2636. #endif /* ! HAS_MODERN_SET_PROTO_VER */
  2637. /* The "new session" callback must return zero if the session can be removed
  2638. * or non-zero if the session has been put into the session cache.
  2639. */
  2640. static int ossl_new_session_cb(SSL *ssl, SSL_SESSION *ssl_sessionid)
  2641. {
  2642. int res = 0;
  2643. struct Curl_easy *data;
  2644. struct Curl_cfilter *cf;
  2645. const struct ssl_config_data *config;
  2646. struct ssl_connect_data *connssl;
  2647. bool isproxy;
  2648. cf = (struct Curl_cfilter*) SSL_get_app_data(ssl);
  2649. connssl = cf? cf->ctx : NULL;
  2650. data = connssl? CF_DATA_CURRENT(cf) : NULL;
  2651. /* The sockindex has been stored as a pointer to an array element */
  2652. if(!cf || !data)
  2653. return 0;
  2654. isproxy = Curl_ssl_cf_is_proxy(cf);
  2655. config = Curl_ssl_cf_get_config(cf, data);
  2656. if(config->primary.sessionid) {
  2657. bool incache;
  2658. bool added = FALSE;
  2659. void *old_ssl_sessionid = NULL;
  2660. Curl_ssl_sessionid_lock(data);
  2661. if(isproxy)
  2662. incache = FALSE;
  2663. else
  2664. incache = !(Curl_ssl_getsessionid(cf, data, &old_ssl_sessionid, NULL));
  2665. if(incache) {
  2666. if(old_ssl_sessionid != ssl_sessionid) {
  2667. infof(data, "old SSL session ID is stale, removing");
  2668. Curl_ssl_delsessionid(data, old_ssl_sessionid);
  2669. incache = FALSE;
  2670. }
  2671. }
  2672. if(!incache) {
  2673. if(!Curl_ssl_addsessionid(cf, data, ssl_sessionid,
  2674. 0 /* unknown size */, &added)) {
  2675. if(added) {
  2676. /* the session has been put into the session cache */
  2677. res = 1;
  2678. }
  2679. }
  2680. else
  2681. failf(data, "failed to store ssl session");
  2682. }
  2683. Curl_ssl_sessionid_unlock(data);
  2684. }
  2685. return res;
  2686. }
  2687. static CURLcode load_cacert_from_memory(X509_STORE *store,
  2688. const struct curl_blob *ca_info_blob)
  2689. {
  2690. /* these need to be freed at the end */
  2691. BIO *cbio = NULL;
  2692. STACK_OF(X509_INFO) *inf = NULL;
  2693. /* everything else is just a reference */
  2694. int i, count = 0;
  2695. X509_INFO *itmp = NULL;
  2696. if(ca_info_blob->len > (size_t)INT_MAX)
  2697. return CURLE_SSL_CACERT_BADFILE;
  2698. cbio = BIO_new_mem_buf(ca_info_blob->data, (int)ca_info_blob->len);
  2699. if(!cbio)
  2700. return CURLE_OUT_OF_MEMORY;
  2701. inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
  2702. if(!inf) {
  2703. BIO_free(cbio);
  2704. return CURLE_SSL_CACERT_BADFILE;
  2705. }
  2706. /* add each entry from PEM file to x509_store */
  2707. for(i = 0; i < (int)sk_X509_INFO_num(inf); ++i) {
  2708. itmp = sk_X509_INFO_value(inf, i);
  2709. if(itmp->x509) {
  2710. if(X509_STORE_add_cert(store, itmp->x509)) {
  2711. ++count;
  2712. }
  2713. else {
  2714. /* set count to 0 to return an error */
  2715. count = 0;
  2716. break;
  2717. }
  2718. }
  2719. if(itmp->crl) {
  2720. if(X509_STORE_add_crl(store, itmp->crl)) {
  2721. ++count;
  2722. }
  2723. else {
  2724. /* set count to 0 to return an error */
  2725. count = 0;
  2726. break;
  2727. }
  2728. }
  2729. }
  2730. sk_X509_INFO_pop_free(inf, X509_INFO_free);
  2731. BIO_free(cbio);
  2732. /* if we didn't end up importing anything, treat that as an error */
  2733. return (count > 0) ? CURLE_OK : CURLE_SSL_CACERT_BADFILE;
  2734. }
  2735. #if defined(USE_WIN32_CRYPTO)
  2736. static CURLcode import_windows_cert_store(struct Curl_easy *data,
  2737. const char *name,
  2738. X509_STORE *store,
  2739. bool *imported)
  2740. {
  2741. CURLcode result = CURLE_OK;
  2742. HCERTSTORE hStore;
  2743. *imported = false;
  2744. hStore = CertOpenSystemStoreA(0, name);
  2745. if(hStore) {
  2746. PCCERT_CONTEXT pContext = NULL;
  2747. /* The array of enhanced key usage OIDs will vary per certificate and
  2748. is declared outside of the loop so that rather than malloc/free each
  2749. iteration we can grow it with realloc, when necessary. */
  2750. CERT_ENHKEY_USAGE *enhkey_usage = NULL;
  2751. DWORD enhkey_usage_size = 0;
  2752. /* This loop makes a best effort to import all valid certificates from
  2753. the MS root store. If a certificate cannot be imported it is
  2754. skipped. 'result' is used to store only hard-fail conditions (such
  2755. as out of memory) that cause an early break. */
  2756. result = CURLE_OK;
  2757. for(;;) {
  2758. X509 *x509;
  2759. FILETIME now;
  2760. BYTE key_usage[2];
  2761. DWORD req_size;
  2762. const unsigned char *encoded_cert;
  2763. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  2764. char cert_name[256];
  2765. #endif
  2766. pContext = CertEnumCertificatesInStore(hStore, pContext);
  2767. if(!pContext)
  2768. break;
  2769. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  2770. if(!CertGetNameStringA(pContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
  2771. NULL, cert_name, sizeof(cert_name))) {
  2772. strcpy(cert_name, "Unknown");
  2773. }
  2774. infof(data, "SSL: Checking cert \"%s\"", cert_name);
  2775. #endif
  2776. encoded_cert = (const unsigned char *)pContext->pbCertEncoded;
  2777. if(!encoded_cert)
  2778. continue;
  2779. GetSystemTimeAsFileTime(&now);
  2780. if(CompareFileTime(&pContext->pCertInfo->NotBefore, &now) > 0 ||
  2781. CompareFileTime(&now, &pContext->pCertInfo->NotAfter) > 0)
  2782. continue;
  2783. /* If key usage exists check for signing attribute */
  2784. if(CertGetIntendedKeyUsage(pContext->dwCertEncodingType,
  2785. pContext->pCertInfo,
  2786. key_usage, sizeof(key_usage))) {
  2787. if(!(key_usage[0] & CERT_KEY_CERT_SIGN_KEY_USAGE))
  2788. continue;
  2789. }
  2790. else if(GetLastError())
  2791. continue;
  2792. /* If enhanced key usage exists check for server auth attribute.
  2793. *
  2794. * Note "In a Microsoft environment, a certificate might also have
  2795. * EKU extended properties that specify valid uses for the
  2796. * certificate." The call below checks both, and behavior varies
  2797. * depending on what is found. For more details see
  2798. * CertGetEnhancedKeyUsage doc.
  2799. */
  2800. if(CertGetEnhancedKeyUsage(pContext, 0, NULL, &req_size)) {
  2801. if(req_size && req_size > enhkey_usage_size) {
  2802. void *tmp = realloc(enhkey_usage, req_size);
  2803. if(!tmp) {
  2804. failf(data, "SSL: Out of memory allocating for OID list");
  2805. result = CURLE_OUT_OF_MEMORY;
  2806. break;
  2807. }
  2808. enhkey_usage = (CERT_ENHKEY_USAGE *)tmp;
  2809. enhkey_usage_size = req_size;
  2810. }
  2811. if(CertGetEnhancedKeyUsage(pContext, 0, enhkey_usage, &req_size)) {
  2812. if(!enhkey_usage->cUsageIdentifier) {
  2813. /* "If GetLastError returns CRYPT_E_NOT_FOUND, the certificate
  2814. is good for all uses. If it returns zero, the certificate
  2815. has no valid uses." */
  2816. if((HRESULT)GetLastError() != CRYPT_E_NOT_FOUND)
  2817. continue;
  2818. }
  2819. else {
  2820. DWORD i;
  2821. bool found = false;
  2822. for(i = 0; i < enhkey_usage->cUsageIdentifier; ++i) {
  2823. if(!strcmp("1.3.6.1.5.5.7.3.1" /* OID server auth */,
  2824. enhkey_usage->rgpszUsageIdentifier[i])) {
  2825. found = true;
  2826. break;
  2827. }
  2828. }
  2829. if(!found)
  2830. continue;
  2831. }
  2832. }
  2833. else
  2834. continue;
  2835. }
  2836. else
  2837. continue;
  2838. x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded);
  2839. if(!x509)
  2840. continue;
  2841. /* Try to import the certificate. This may fail for legitimate
  2842. reasons such as duplicate certificate, which is allowed by MS but
  2843. not OpenSSL. */
  2844. if(X509_STORE_add_cert(store, x509) == 1) {
  2845. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  2846. infof(data, "SSL: Imported cert \"%s\"", cert_name);
  2847. #endif
  2848. *imported = true;
  2849. }
  2850. X509_free(x509);
  2851. }
  2852. free(enhkey_usage);
  2853. CertFreeCertificateContext(pContext);
  2854. CertCloseStore(hStore, 0);
  2855. if(result)
  2856. return result;
  2857. }
  2858. return result;
  2859. }
  2860. #endif
  2861. static CURLcode populate_x509_store(struct Curl_cfilter *cf,
  2862. struct Curl_easy *data,
  2863. X509_STORE *store)
  2864. {
  2865. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  2866. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  2867. CURLcode result = CURLE_OK;
  2868. X509_LOOKUP *lookup = NULL;
  2869. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  2870. const char * const ssl_cafile =
  2871. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  2872. (ca_info_blob ? NULL : conn_config->CAfile);
  2873. const char * const ssl_capath = conn_config->CApath;
  2874. const char * const ssl_crlfile = ssl_config->primary.CRLfile;
  2875. const bool verifypeer = conn_config->verifypeer;
  2876. bool imported_native_ca = false;
  2877. bool imported_ca_info_blob = false;
  2878. CURL_TRC_CF(data, cf, "populate_x509_store, path=%s, blob=%d",
  2879. ssl_cafile? ssl_cafile : "none", !!ca_info_blob);
  2880. if(!store)
  2881. return CURLE_OUT_OF_MEMORY;
  2882. if(verifypeer) {
  2883. #if defined(USE_WIN32_CRYPTO)
  2884. /* Import certificates from the Windows root certificate store if
  2885. requested.
  2886. https://stackoverflow.com/questions/9507184/
  2887. https://github.com/d3x0r/SACK/blob/master/src/netlib/ssl_layer.c#L1037
  2888. https://datatracker.ietf.org/doc/html/rfc5280 */
  2889. if(ssl_config->native_ca_store) {
  2890. const char *storeNames[] = {
  2891. "ROOT", /* Trusted Root Certification Authorities */
  2892. "CA" /* Intermediate Certification Authorities */
  2893. };
  2894. size_t i;
  2895. for(i = 0; i < ARRAYSIZE(storeNames); ++i) {
  2896. bool imported = false;
  2897. result = import_windows_cert_store(data, storeNames[i], store,
  2898. &imported);
  2899. if(result)
  2900. return result;
  2901. if(imported) {
  2902. infof(data, "successfully imported Windows %s store", storeNames[i]);
  2903. imported_native_ca = true;
  2904. }
  2905. else
  2906. infof(data, "error importing Windows %s store, continuing anyway",
  2907. storeNames[i]);
  2908. }
  2909. }
  2910. #endif
  2911. if(ca_info_blob) {
  2912. result = load_cacert_from_memory(store, ca_info_blob);
  2913. if(result) {
  2914. failf(data, "error importing CA certificate blob");
  2915. return result;
  2916. }
  2917. else {
  2918. imported_ca_info_blob = true;
  2919. infof(data, "successfully imported CA certificate blob");
  2920. }
  2921. }
  2922. if(ssl_cafile || ssl_capath) {
  2923. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  2924. /* OpenSSL 3.0.0 has deprecated SSL_CTX_load_verify_locations */
  2925. if(ssl_cafile && !X509_STORE_load_file(store, ssl_cafile)) {
  2926. if(!imported_native_ca && !imported_ca_info_blob) {
  2927. /* Fail if we insist on successfully verifying the server. */
  2928. failf(data, "error setting certificate file: %s", ssl_cafile);
  2929. return CURLE_SSL_CACERT_BADFILE;
  2930. }
  2931. else
  2932. infof(data, "error setting certificate file, continuing anyway");
  2933. }
  2934. if(ssl_capath && !X509_STORE_load_path(store, ssl_capath)) {
  2935. if(!imported_native_ca && !imported_ca_info_blob) {
  2936. /* Fail if we insist on successfully verifying the server. */
  2937. failf(data, "error setting certificate path: %s", ssl_capath);
  2938. return CURLE_SSL_CACERT_BADFILE;
  2939. }
  2940. else
  2941. infof(data, "error setting certificate path, continuing anyway");
  2942. }
  2943. #else
  2944. /* tell OpenSSL where to find CA certificates that are used to verify the
  2945. server's certificate. */
  2946. if(!X509_STORE_load_locations(store, ssl_cafile, ssl_capath)) {
  2947. if(!imported_native_ca && !imported_ca_info_blob) {
  2948. /* Fail if we insist on successfully verifying the server. */
  2949. failf(data, "error setting certificate verify locations:"
  2950. " CAfile: %s CApath: %s",
  2951. ssl_cafile ? ssl_cafile : "none",
  2952. ssl_capath ? ssl_capath : "none");
  2953. return CURLE_SSL_CACERT_BADFILE;
  2954. }
  2955. else {
  2956. infof(data, "error setting certificate verify locations,"
  2957. " continuing anyway");
  2958. }
  2959. }
  2960. #endif
  2961. infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
  2962. infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
  2963. }
  2964. #ifdef CURL_CA_FALLBACK
  2965. if(!ssl_cafile && !ssl_capath &&
  2966. !imported_native_ca && !imported_ca_info_blob) {
  2967. /* verifying the peer without any CA certificates won't
  2968. work so use openssl's built-in default as fallback */
  2969. X509_STORE_set_default_paths(store);
  2970. }
  2971. #endif
  2972. }
  2973. if(ssl_crlfile) {
  2974. /* tell OpenSSL where to find CRL file that is used to check certificate
  2975. * revocation */
  2976. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  2977. if(!lookup ||
  2978. (!X509_load_crl_file(lookup, ssl_crlfile, X509_FILETYPE_PEM)) ) {
  2979. failf(data, "error loading CRL file: %s", ssl_crlfile);
  2980. return CURLE_SSL_CRL_BADFILE;
  2981. }
  2982. /* Everything is fine. */
  2983. infof(data, "successfully loaded CRL file:");
  2984. X509_STORE_set_flags(store,
  2985. X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
  2986. infof(data, " CRLfile: %s", ssl_crlfile);
  2987. }
  2988. if(verifypeer) {
  2989. /* Try building a chain using issuers in the trusted store first to avoid
  2990. problems with server-sent legacy intermediates. Newer versions of
  2991. OpenSSL do alternate chain checking by default but we do not know how to
  2992. determine that in a reliable manner.
  2993. https://rt.openssl.org/Ticket/Display.html?id=3621&user=guest&pass=guest
  2994. */
  2995. #if defined(X509_V_FLAG_TRUSTED_FIRST)
  2996. X509_STORE_set_flags(store, X509_V_FLAG_TRUSTED_FIRST);
  2997. #endif
  2998. #ifdef X509_V_FLAG_PARTIAL_CHAIN
  2999. if(!ssl_config->no_partialchain && !ssl_crlfile) {
  3000. /* Have intermediate certificates in the trust store be treated as
  3001. trust-anchors, in the same way as self-signed root CA certificates
  3002. are. This allows users to verify servers using the intermediate cert
  3003. only, instead of needing the whole chain.
  3004. Due to OpenSSL bug https://github.com/openssl/openssl/issues/5081 we
  3005. cannot do partial chains with a CRL check.
  3006. */
  3007. X509_STORE_set_flags(store, X509_V_FLAG_PARTIAL_CHAIN);
  3008. }
  3009. #endif
  3010. }
  3011. return result;
  3012. }
  3013. #if defined(HAVE_SSL_X509_STORE_SHARE)
  3014. static bool cached_x509_store_expired(const struct Curl_easy *data,
  3015. const struct multi_ssl_backend_data *mb)
  3016. {
  3017. const struct ssl_general_config *cfg = &data->set.general_ssl;
  3018. struct curltime now = Curl_now();
  3019. timediff_t elapsed_ms = Curl_timediff(now, mb->time);
  3020. timediff_t timeout_ms = cfg->ca_cache_timeout * (timediff_t)1000;
  3021. if(timeout_ms < 0)
  3022. return false;
  3023. return elapsed_ms >= timeout_ms;
  3024. }
  3025. static bool cached_x509_store_different(
  3026. struct Curl_cfilter *cf,
  3027. const struct multi_ssl_backend_data *mb)
  3028. {
  3029. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3030. if(!mb->CAfile || !conn_config->CAfile)
  3031. return mb->CAfile != conn_config->CAfile;
  3032. return strcmp(mb->CAfile, conn_config->CAfile);
  3033. }
  3034. static X509_STORE *get_cached_x509_store(struct Curl_cfilter *cf,
  3035. const struct Curl_easy *data)
  3036. {
  3037. struct Curl_multi *multi = data->multi_easy ? data->multi_easy : data->multi;
  3038. X509_STORE *store = NULL;
  3039. DEBUGASSERT(multi);
  3040. if(multi &&
  3041. multi->ssl_backend_data &&
  3042. multi->ssl_backend_data->store &&
  3043. !cached_x509_store_expired(data, multi->ssl_backend_data) &&
  3044. !cached_x509_store_different(cf, multi->ssl_backend_data)) {
  3045. store = multi->ssl_backend_data->store;
  3046. }
  3047. return store;
  3048. }
  3049. static void set_cached_x509_store(struct Curl_cfilter *cf,
  3050. const struct Curl_easy *data,
  3051. X509_STORE *store)
  3052. {
  3053. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3054. struct Curl_multi *multi = data->multi_easy ? data->multi_easy : data->multi;
  3055. struct multi_ssl_backend_data *mbackend;
  3056. DEBUGASSERT(multi);
  3057. if(!multi)
  3058. return;
  3059. if(!multi->ssl_backend_data) {
  3060. multi->ssl_backend_data = calloc(1, sizeof(struct multi_ssl_backend_data));
  3061. if(!multi->ssl_backend_data)
  3062. return;
  3063. }
  3064. mbackend = multi->ssl_backend_data;
  3065. if(X509_STORE_up_ref(store)) {
  3066. char *CAfile = NULL;
  3067. if(conn_config->CAfile) {
  3068. CAfile = strdup(conn_config->CAfile);
  3069. if(!CAfile) {
  3070. X509_STORE_free(store);
  3071. return;
  3072. }
  3073. }
  3074. if(mbackend->store) {
  3075. X509_STORE_free(mbackend->store);
  3076. free(mbackend->CAfile);
  3077. }
  3078. mbackend->time = Curl_now();
  3079. mbackend->store = store;
  3080. mbackend->CAfile = CAfile;
  3081. }
  3082. }
  3083. CURLcode Curl_ssl_setup_x509_store(struct Curl_cfilter *cf,
  3084. struct Curl_easy *data,
  3085. SSL_CTX *ssl_ctx)
  3086. {
  3087. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3088. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  3089. CURLcode result = CURLE_OK;
  3090. X509_STORE *cached_store;
  3091. bool cache_criteria_met;
  3092. /* Consider the X509 store cacheable if it comes exclusively from a CAfile,
  3093. or no source is provided and we are falling back to openssl's built-in
  3094. default. */
  3095. cache_criteria_met = (data->set.general_ssl.ca_cache_timeout != 0) &&
  3096. conn_config->verifypeer &&
  3097. !conn_config->CApath &&
  3098. !conn_config->ca_info_blob &&
  3099. !ssl_config->primary.CRLfile &&
  3100. !ssl_config->native_ca_store;
  3101. cached_store = get_cached_x509_store(cf, data);
  3102. if(cached_store && cache_criteria_met && X509_STORE_up_ref(cached_store)) {
  3103. SSL_CTX_set_cert_store(ssl_ctx, cached_store);
  3104. }
  3105. else {
  3106. X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
  3107. result = populate_x509_store(cf, data, store);
  3108. if(result == CURLE_OK && cache_criteria_met) {
  3109. set_cached_x509_store(cf, data, store);
  3110. }
  3111. }
  3112. return result;
  3113. }
  3114. #else /* HAVE_SSL_X509_STORE_SHARE */
  3115. CURLcode Curl_ssl_setup_x509_store(struct Curl_cfilter *cf,
  3116. struct Curl_easy *data,
  3117. SSL_CTX *ssl_ctx)
  3118. {
  3119. X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
  3120. return populate_x509_store(cf, data, store);
  3121. }
  3122. #endif /* HAVE_SSL_X509_STORE_SHARE */
  3123. static CURLcode ossl_connect_step1(struct Curl_cfilter *cf,
  3124. struct Curl_easy *data)
  3125. {
  3126. CURLcode result = CURLE_OK;
  3127. char *ciphers;
  3128. SSL_METHOD_QUAL SSL_METHOD *req_method = NULL;
  3129. struct ssl_connect_data *connssl = cf->ctx;
  3130. ctx_option_t ctx_options = 0;
  3131. void *ssl_sessionid = NULL;
  3132. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3133. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  3134. BIO *bio;
  3135. const long int ssl_version = conn_config->version;
  3136. char * const ssl_cert = ssl_config->primary.clientcert;
  3137. const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob;
  3138. const char * const ssl_cert_type = ssl_config->cert_type;
  3139. const bool verifypeer = conn_config->verifypeer;
  3140. char error_buffer[256];
  3141. struct ossl_ssl_backend_data *backend =
  3142. (struct ossl_ssl_backend_data *)connssl->backend;
  3143. DEBUGASSERT(ssl_connect_1 == connssl->connecting_state);
  3144. DEBUGASSERT(backend);
  3145. /* Make funny stuff to get random input */
  3146. result = ossl_seed(data);
  3147. if(result)
  3148. return result;
  3149. ssl_config->certverifyresult = !X509_V_OK;
  3150. /* check to see if we've been told to use an explicit SSL/TLS version */
  3151. switch(ssl_version) {
  3152. case CURL_SSLVERSION_DEFAULT:
  3153. case CURL_SSLVERSION_TLSv1:
  3154. case CURL_SSLVERSION_TLSv1_0:
  3155. case CURL_SSLVERSION_TLSv1_1:
  3156. case CURL_SSLVERSION_TLSv1_2:
  3157. case CURL_SSLVERSION_TLSv1_3:
  3158. /* it will be handled later with the context options */
  3159. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  3160. req_method = TLS_client_method();
  3161. #else
  3162. req_method = SSLv23_client_method();
  3163. #endif
  3164. break;
  3165. case CURL_SSLVERSION_SSLv2:
  3166. failf(data, "No SSLv2 support");
  3167. return CURLE_NOT_BUILT_IN;
  3168. case CURL_SSLVERSION_SSLv3:
  3169. failf(data, "No SSLv3 support");
  3170. return CURLE_NOT_BUILT_IN;
  3171. default:
  3172. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  3173. return CURLE_SSL_CONNECT_ERROR;
  3174. }
  3175. if(backend->ctx) {
  3176. /* This happens when an error was encountered before in this
  3177. * step and we are called to do it again. Get rid of any leftover
  3178. * from the previous call. */
  3179. ossl_close(cf, data);
  3180. }
  3181. backend->ctx = SSL_CTX_new(req_method);
  3182. if(!backend->ctx) {
  3183. failf(data, "SSL: couldn't create a context: %s",
  3184. ossl_strerror(ERR_peek_error(), error_buffer, sizeof(error_buffer)));
  3185. return CURLE_OUT_OF_MEMORY;
  3186. }
  3187. #ifdef SSL_MODE_RELEASE_BUFFERS
  3188. SSL_CTX_set_mode(backend->ctx, SSL_MODE_RELEASE_BUFFERS);
  3189. #endif
  3190. #ifdef SSL_CTRL_SET_MSG_CALLBACK
  3191. if(data->set.fdebug && data->set.verbose) {
  3192. /* the SSL trace callback is only used for verbose logging */
  3193. SSL_CTX_set_msg_callback(backend->ctx, ossl_trace);
  3194. SSL_CTX_set_msg_callback_arg(backend->ctx, cf);
  3195. }
  3196. #endif
  3197. /* OpenSSL contains code to work around lots of bugs and flaws in various
  3198. SSL-implementations. SSL_CTX_set_options() is used to enabled those
  3199. work-arounds. The man page for this option states that SSL_OP_ALL enables
  3200. all the work-arounds and that "It is usually safe to use SSL_OP_ALL to
  3201. enable the bug workaround options if compatibility with somewhat broken
  3202. implementations is desired."
  3203. The "-no_ticket" option was introduced in OpenSSL 0.9.8j. It's a flag to
  3204. disable "rfc4507bis session ticket support". rfc4507bis was later turned
  3205. into the proper RFC5077: https://datatracker.ietf.org/doc/html/rfc5077
  3206. The enabled extension concerns the session management. I wonder how often
  3207. libcurl stops a connection and then resumes a TLS session. Also, sending
  3208. the session data is some overhead. I suggest that you just use your
  3209. proposed patch (which explicitly disables TICKET).
  3210. If someone writes an application with libcurl and OpenSSL who wants to
  3211. enable the feature, one can do this in the SSL callback.
  3212. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG option enabling allowed proper
  3213. interoperability with web server Netscape Enterprise Server 2.0.1 which
  3214. was released back in 1996.
  3215. Due to CVE-2010-4180, option SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG has
  3216. become ineffective as of OpenSSL 0.9.8q and 1.0.0c. In order to mitigate
  3217. CVE-2010-4180 when using previous OpenSSL versions we no longer enable
  3218. this option regardless of OpenSSL version and SSL_OP_ALL definition.
  3219. OpenSSL added a work-around for a SSL 3.0/TLS 1.0 CBC vulnerability
  3220. (https://www.openssl.org/~bodo/tls-cbc.txt). In 0.9.6e they added a bit to
  3221. SSL_OP_ALL that _disables_ that work-around despite the fact that
  3222. SSL_OP_ALL is documented to do "rather harmless" workarounds. In order to
  3223. keep the secure work-around, the SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS bit
  3224. must not be set.
  3225. */
  3226. ctx_options = SSL_OP_ALL;
  3227. #ifdef SSL_OP_NO_TICKET
  3228. ctx_options |= SSL_OP_NO_TICKET;
  3229. #endif
  3230. #ifdef SSL_OP_NO_COMPRESSION
  3231. ctx_options |= SSL_OP_NO_COMPRESSION;
  3232. #endif
  3233. #ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
  3234. /* mitigate CVE-2010-4180 */
  3235. ctx_options &= ~SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG;
  3236. #endif
  3237. #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  3238. /* unless the user explicitly asks to allow the protocol vulnerability we
  3239. use the work-around */
  3240. if(!ssl_config->enable_beast)
  3241. ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
  3242. #endif
  3243. switch(ssl_version) {
  3244. case CURL_SSLVERSION_SSLv2:
  3245. case CURL_SSLVERSION_SSLv3:
  3246. return CURLE_NOT_BUILT_IN;
  3247. /* "--tlsv<x.y>" options mean TLS >= version <x.y> */
  3248. case CURL_SSLVERSION_DEFAULT:
  3249. case CURL_SSLVERSION_TLSv1: /* TLS >= version 1.0 */
  3250. case CURL_SSLVERSION_TLSv1_0: /* TLS >= version 1.0 */
  3251. case CURL_SSLVERSION_TLSv1_1: /* TLS >= version 1.1 */
  3252. case CURL_SSLVERSION_TLSv1_2: /* TLS >= version 1.2 */
  3253. case CURL_SSLVERSION_TLSv1_3: /* TLS >= version 1.3 */
  3254. /* asking for any TLS version as the minimum, means no SSL versions
  3255. allowed */
  3256. ctx_options |= SSL_OP_NO_SSLv2;
  3257. ctx_options |= SSL_OP_NO_SSLv3;
  3258. #if HAS_MODERN_SET_PROTO_VER /* 1.1.0 */
  3259. result = ossl_set_ssl_version_min_max(cf, backend->ctx);
  3260. #else
  3261. result = ossl_set_ssl_version_min_max_legacy(&ctx_options, cf, data);
  3262. #endif
  3263. if(result != CURLE_OK)
  3264. return result;
  3265. break;
  3266. default:
  3267. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  3268. return CURLE_SSL_CONNECT_ERROR;
  3269. }
  3270. SSL_CTX_set_options(backend->ctx, ctx_options);
  3271. #ifdef HAS_ALPN
  3272. if(connssl->alpn) {
  3273. struct alpn_proto_buf proto;
  3274. result = Curl_alpn_to_proto_buf(&proto, connssl->alpn);
  3275. if(result ||
  3276. SSL_CTX_set_alpn_protos(backend->ctx, proto.data, proto.len)) {
  3277. failf(data, "Error setting ALPN");
  3278. return CURLE_SSL_CONNECT_ERROR;
  3279. }
  3280. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  3281. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  3282. }
  3283. #endif
  3284. if(ssl_cert || ssl_cert_blob || ssl_cert_type) {
  3285. if(!result &&
  3286. !cert_stuff(data, backend->ctx,
  3287. ssl_cert, ssl_cert_blob, ssl_cert_type,
  3288. ssl_config->key, ssl_config->key_blob,
  3289. ssl_config->key_type, ssl_config->key_passwd))
  3290. result = CURLE_SSL_CERTPROBLEM;
  3291. if(result)
  3292. /* failf() is already done in cert_stuff() */
  3293. return result;
  3294. }
  3295. ciphers = conn_config->cipher_list;
  3296. if(!ciphers)
  3297. ciphers = (char *)DEFAULT_CIPHER_SELECTION;
  3298. if(ciphers) {
  3299. if(!SSL_CTX_set_cipher_list(backend->ctx, ciphers)) {
  3300. failf(data, "failed setting cipher list: %s", ciphers);
  3301. return CURLE_SSL_CIPHER;
  3302. }
  3303. infof(data, "Cipher selection: %s", ciphers);
  3304. }
  3305. #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
  3306. {
  3307. char *ciphers13 = conn_config->cipher_list13;
  3308. if(ciphers13) {
  3309. if(!SSL_CTX_set_ciphersuites(backend->ctx, ciphers13)) {
  3310. failf(data, "failed setting TLS 1.3 cipher suite: %s", ciphers13);
  3311. return CURLE_SSL_CIPHER;
  3312. }
  3313. infof(data, "TLS 1.3 cipher selection: %s", ciphers13);
  3314. }
  3315. }
  3316. #endif
  3317. #ifdef HAVE_SSL_CTX_SET_POST_HANDSHAKE_AUTH
  3318. /* OpenSSL 1.1.1 requires clients to opt-in for PHA */
  3319. SSL_CTX_set_post_handshake_auth(backend->ctx, 1);
  3320. #endif
  3321. #ifdef HAVE_SSL_CTX_SET_EC_CURVES
  3322. {
  3323. char *curves = conn_config->curves;
  3324. if(curves) {
  3325. if(!SSL_CTX_set1_curves_list(backend->ctx, curves)) {
  3326. failf(data, "failed setting curves list: '%s'", curves);
  3327. return CURLE_SSL_CIPHER;
  3328. }
  3329. }
  3330. }
  3331. #endif
  3332. #ifdef USE_OPENSSL_SRP
  3333. if(ssl_config->primary.username && Curl_auth_allowed_to_host(data)) {
  3334. char * const ssl_username = ssl_config->primary.username;
  3335. char * const ssl_password = ssl_config->primary.password;
  3336. infof(data, "Using TLS-SRP username: %s", ssl_username);
  3337. if(!SSL_CTX_set_srp_username(backend->ctx, ssl_username)) {
  3338. failf(data, "Unable to set SRP user name");
  3339. return CURLE_BAD_FUNCTION_ARGUMENT;
  3340. }
  3341. if(!SSL_CTX_set_srp_password(backend->ctx, ssl_password)) {
  3342. failf(data, "failed setting SRP password");
  3343. return CURLE_BAD_FUNCTION_ARGUMENT;
  3344. }
  3345. if(!conn_config->cipher_list) {
  3346. infof(data, "Setting cipher list SRP");
  3347. if(!SSL_CTX_set_cipher_list(backend->ctx, "SRP")) {
  3348. failf(data, "failed setting SRP cipher list");
  3349. return CURLE_SSL_CIPHER;
  3350. }
  3351. }
  3352. }
  3353. #endif
  3354. /* OpenSSL always tries to verify the peer, this only says whether it should
  3355. * fail to connect if the verification fails, or if it should continue
  3356. * anyway. In the latter case the result of the verification is checked with
  3357. * SSL_get_verify_result() below. */
  3358. SSL_CTX_set_verify(backend->ctx,
  3359. verifypeer ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
  3360. /* Enable logging of secrets to the file specified in env SSLKEYLOGFILE. */
  3361. #ifdef HAVE_KEYLOG_CALLBACK
  3362. if(Curl_tls_keylog_enabled()) {
  3363. SSL_CTX_set_keylog_callback(backend->ctx, ossl_keylog_callback);
  3364. }
  3365. #endif
  3366. /* Enable the session cache because it's a prerequisite for the "new session"
  3367. * callback. Use the "external storage" mode to prevent OpenSSL from creating
  3368. * an internal session cache.
  3369. */
  3370. SSL_CTX_set_session_cache_mode(backend->ctx,
  3371. SSL_SESS_CACHE_CLIENT |
  3372. SSL_SESS_CACHE_NO_INTERNAL);
  3373. SSL_CTX_sess_set_new_cb(backend->ctx, ossl_new_session_cb);
  3374. /* give application a chance to interfere with SSL set up. */
  3375. if(data->set.ssl.fsslctx) {
  3376. /* When a user callback is installed to modify the SSL_CTX,
  3377. * we need to do the full initialization before calling it.
  3378. * See: #11800 */
  3379. if(!backend->x509_store_setup) {
  3380. result = Curl_ssl_setup_x509_store(cf, data, backend->ctx);
  3381. if(result)
  3382. return result;
  3383. backend->x509_store_setup = TRUE;
  3384. }
  3385. Curl_set_in_callback(data, true);
  3386. result = (*data->set.ssl.fsslctx)(data, backend->ctx,
  3387. data->set.ssl.fsslctxp);
  3388. Curl_set_in_callback(data, false);
  3389. if(result) {
  3390. failf(data, "error signaled by ssl ctx callback");
  3391. return result;
  3392. }
  3393. }
  3394. /* Let's make an SSL structure */
  3395. if(backend->handle)
  3396. SSL_free(backend->handle);
  3397. backend->handle = SSL_new(backend->ctx);
  3398. if(!backend->handle) {
  3399. failf(data, "SSL: couldn't create a context (handle)");
  3400. return CURLE_OUT_OF_MEMORY;
  3401. }
  3402. SSL_set_app_data(backend->handle, cf);
  3403. #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
  3404. !defined(OPENSSL_NO_OCSP)
  3405. if(conn_config->verifystatus)
  3406. SSL_set_tlsext_status_type(backend->handle, TLSEXT_STATUSTYPE_ocsp);
  3407. #endif
  3408. #if (defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)) && \
  3409. defined(ALLOW_RENEG)
  3410. SSL_set_renegotiate_mode(backend->handle, ssl_renegotiate_freely);
  3411. #endif
  3412. SSL_set_connect_state(backend->handle);
  3413. backend->server_cert = 0x0;
  3414. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
  3415. if(connssl->peer.sni) {
  3416. if(!SSL_set_tlsext_host_name(backend->handle, connssl->peer.sni)) {
  3417. failf(data, "Failed set SNI");
  3418. return CURLE_SSL_CONNECT_ERROR;
  3419. }
  3420. }
  3421. #endif
  3422. SSL_set_app_data(backend->handle, cf);
  3423. connssl->reused_session = FALSE;
  3424. if(ssl_config->primary.sessionid) {
  3425. Curl_ssl_sessionid_lock(data);
  3426. if(!Curl_ssl_getsessionid(cf, data, &ssl_sessionid, NULL)) {
  3427. /* we got a session id, use it! */
  3428. if(!SSL_set_session(backend->handle, ssl_sessionid)) {
  3429. Curl_ssl_sessionid_unlock(data);
  3430. failf(data, "SSL: SSL_set_session failed: %s",
  3431. ossl_strerror(ERR_get_error(), error_buffer,
  3432. sizeof(error_buffer)));
  3433. return CURLE_SSL_CONNECT_ERROR;
  3434. }
  3435. /* Informational message */
  3436. infof(data, "SSL reusing session ID");
  3437. connssl->reused_session = TRUE;
  3438. }
  3439. Curl_ssl_sessionid_unlock(data);
  3440. }
  3441. backend->bio_method = ossl_bio_cf_method_create();
  3442. if(!backend->bio_method)
  3443. return CURLE_OUT_OF_MEMORY;
  3444. bio = BIO_new(backend->bio_method);
  3445. if(!bio)
  3446. return CURLE_OUT_OF_MEMORY;
  3447. BIO_set_data(bio, cf);
  3448. #ifdef HAVE_SSL_SET0_WBIO
  3449. /* with OpenSSL v1.1.1 we get an alternative to SSL_set_bio() that works
  3450. * without backward compat quirks. Every call takes one reference, so we
  3451. * up it and pass. SSL* then owns it and will free.
  3452. * We check on the function in configure, since libressl and friends
  3453. * each have their own versions to add support for this. */
  3454. BIO_up_ref(bio);
  3455. SSL_set0_rbio(backend->handle, bio);
  3456. SSL_set0_wbio(backend->handle, bio);
  3457. #else
  3458. SSL_set_bio(backend->handle, bio, bio);
  3459. #endif
  3460. connssl->connecting_state = ssl_connect_2;
  3461. return CURLE_OK;
  3462. }
  3463. static CURLcode ossl_connect_step2(struct Curl_cfilter *cf,
  3464. struct Curl_easy *data)
  3465. {
  3466. int err;
  3467. struct ssl_connect_data *connssl = cf->ctx;
  3468. struct ossl_ssl_backend_data *backend =
  3469. (struct ossl_ssl_backend_data *)connssl->backend;
  3470. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  3471. DEBUGASSERT(ssl_connect_2 == connssl->connecting_state
  3472. || ssl_connect_2_reading == connssl->connecting_state
  3473. || ssl_connect_2_writing == connssl->connecting_state);
  3474. DEBUGASSERT(backend);
  3475. ERR_clear_error();
  3476. err = SSL_connect(backend->handle);
  3477. if(!backend->x509_store_setup) {
  3478. /* After having send off the ClientHello, we prepare the x509
  3479. * store to verify the coming certificate from the server */
  3480. CURLcode result = Curl_ssl_setup_x509_store(cf, data, backend->ctx);
  3481. if(result)
  3482. return result;
  3483. backend->x509_store_setup = TRUE;
  3484. }
  3485. #ifndef HAVE_KEYLOG_CALLBACK
  3486. if(Curl_tls_keylog_enabled()) {
  3487. /* If key logging is enabled, wait for the handshake to complete and then
  3488. * proceed with logging secrets (for TLS 1.2 or older).
  3489. */
  3490. ossl_log_tls12_secret(backend->handle, &backend->keylog_done);
  3491. }
  3492. #endif
  3493. /* 1 is fine
  3494. 0 is "not successful but was shut down controlled"
  3495. <0 is "handshake was not successful, because a fatal error occurred" */
  3496. if(1 != err) {
  3497. int detail = SSL_get_error(backend->handle, err);
  3498. if(SSL_ERROR_WANT_READ == detail) {
  3499. connssl->connecting_state = ssl_connect_2_reading;
  3500. return CURLE_OK;
  3501. }
  3502. if(SSL_ERROR_WANT_WRITE == detail) {
  3503. connssl->connecting_state = ssl_connect_2_writing;
  3504. return CURLE_OK;
  3505. }
  3506. #ifdef SSL_ERROR_WANT_ASYNC
  3507. if(SSL_ERROR_WANT_ASYNC == detail) {
  3508. connssl->connecting_state = ssl_connect_2;
  3509. return CURLE_OK;
  3510. }
  3511. #endif
  3512. #ifdef SSL_ERROR_WANT_RETRY_VERIFY
  3513. if(SSL_ERROR_WANT_RETRY_VERIFY == detail) {
  3514. connssl->connecting_state = ssl_connect_2;
  3515. return CURLE_OK;
  3516. }
  3517. #endif
  3518. if(backend->io_result == CURLE_AGAIN) {
  3519. return CURLE_OK;
  3520. }
  3521. else {
  3522. /* untreated error */
  3523. sslerr_t errdetail;
  3524. char error_buffer[256]="";
  3525. CURLcode result;
  3526. long lerr;
  3527. int lib;
  3528. int reason;
  3529. /* the connection failed, we're not waiting for anything else. */
  3530. connssl->connecting_state = ssl_connect_2;
  3531. /* Get the earliest error code from the thread's error queue and remove
  3532. the entry. */
  3533. errdetail = ERR_get_error();
  3534. /* Extract which lib and reason */
  3535. lib = ERR_GET_LIB(errdetail);
  3536. reason = ERR_GET_REASON(errdetail);
  3537. if((lib == ERR_LIB_SSL) &&
  3538. ((reason == SSL_R_CERTIFICATE_VERIFY_FAILED) ||
  3539. (reason == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED))) {
  3540. result = CURLE_PEER_FAILED_VERIFICATION;
  3541. lerr = SSL_get_verify_result(backend->handle);
  3542. if(lerr != X509_V_OK) {
  3543. ssl_config->certverifyresult = lerr;
  3544. msnprintf(error_buffer, sizeof(error_buffer),
  3545. "SSL certificate problem: %s",
  3546. X509_verify_cert_error_string(lerr));
  3547. }
  3548. else
  3549. /* strcpy() is fine here as long as the string fits within
  3550. error_buffer */
  3551. strcpy(error_buffer, "SSL certificate verification failed");
  3552. }
  3553. #if defined(SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED)
  3554. /* SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED is only available on
  3555. OpenSSL version above v1.1.1, not LibreSSL, BoringSSL, or AWS-LC */
  3556. else if((lib == ERR_LIB_SSL) &&
  3557. (reason == SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED)) {
  3558. /* If client certificate is required, communicate the
  3559. error to client */
  3560. result = CURLE_SSL_CLIENTCERT;
  3561. ossl_strerror(errdetail, error_buffer, sizeof(error_buffer));
  3562. }
  3563. #endif
  3564. else {
  3565. result = CURLE_SSL_CONNECT_ERROR;
  3566. ossl_strerror(errdetail, error_buffer, sizeof(error_buffer));
  3567. }
  3568. /* detail is already set to the SSL error above */
  3569. /* If we e.g. use SSLv2 request-method and the server doesn't like us
  3570. * (RST connection, etc.), OpenSSL gives no explanation whatsoever and
  3571. * the SO_ERROR is also lost.
  3572. */
  3573. if(CURLE_SSL_CONNECT_ERROR == result && errdetail == 0) {
  3574. char extramsg[80]="";
  3575. int sockerr = SOCKERRNO;
  3576. if(sockerr && detail == SSL_ERROR_SYSCALL)
  3577. Curl_strerror(sockerr, extramsg, sizeof(extramsg));
  3578. failf(data, OSSL_PACKAGE " SSL_connect: %s in connection to %s:%d ",
  3579. extramsg[0] ? extramsg : SSL_ERROR_to_str(detail),
  3580. connssl->peer.hostname, connssl->port);
  3581. return result;
  3582. }
  3583. /* Could be a CERT problem */
  3584. failf(data, "%s", error_buffer);
  3585. return result;
  3586. }
  3587. }
  3588. else {
  3589. int psigtype_nid = NID_undef;
  3590. const char *negotiated_group_name = NULL;
  3591. /* we connected fine, we're not waiting for anything else. */
  3592. connssl->connecting_state = ssl_connect_3;
  3593. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  3594. SSL_get_peer_signature_type_nid(backend->handle, &psigtype_nid);
  3595. #if (OPENSSL_VERSION_NUMBER >= 0x30200000L)
  3596. negotiated_group_name = SSL_get0_group_name(backend->handle);
  3597. #else
  3598. negotiated_group_name =
  3599. OBJ_nid2sn(SSL_get_negotiated_group(backend->handle) & 0x0000FFFF);
  3600. #endif
  3601. #endif
  3602. /* Informational message */
  3603. infof(data, "SSL connection using %s / %s / %s / %s",
  3604. SSL_get_version(backend->handle),
  3605. SSL_get_cipher(backend->handle),
  3606. negotiated_group_name? negotiated_group_name : "[blank]",
  3607. OBJ_nid2sn(psigtype_nid));
  3608. #ifdef HAS_ALPN
  3609. /* Sets data and len to negotiated protocol, len is 0 if no protocol was
  3610. * negotiated
  3611. */
  3612. if(connssl->alpn) {
  3613. const unsigned char *neg_protocol;
  3614. unsigned int len;
  3615. SSL_get0_alpn_selected(backend->handle, &neg_protocol, &len);
  3616. return Curl_alpn_set_negotiated(cf, data, neg_protocol, len);
  3617. }
  3618. #endif
  3619. return CURLE_OK;
  3620. }
  3621. }
  3622. /*
  3623. * Heavily modified from:
  3624. * https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#OpenSSL
  3625. */
  3626. static CURLcode ossl_pkp_pin_peer_pubkey(struct Curl_easy *data, X509* cert,
  3627. const char *pinnedpubkey)
  3628. {
  3629. /* Scratch */
  3630. int len1 = 0, len2 = 0;
  3631. unsigned char *buff1 = NULL, *temp = NULL;
  3632. /* Result is returned to caller */
  3633. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  3634. /* if a path wasn't specified, don't pin */
  3635. if(!pinnedpubkey)
  3636. return CURLE_OK;
  3637. if(!cert)
  3638. return result;
  3639. do {
  3640. /* Begin Gyrations to get the subjectPublicKeyInfo */
  3641. /* Thanks to Viktor Dukhovni on the OpenSSL mailing list */
  3642. /* https://groups.google.com/group/mailing.openssl.users/browse_thread
  3643. /thread/d61858dae102c6c7 */
  3644. len1 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL);
  3645. if(len1 < 1)
  3646. break; /* failed */
  3647. buff1 = temp = malloc(len1);
  3648. if(!buff1)
  3649. break; /* failed */
  3650. /* https://www.openssl.org/docs/crypto/d2i_X509.html */
  3651. len2 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp);
  3652. /*
  3653. * These checks are verifying we got back the same values as when we
  3654. * sized the buffer. It's pretty weak since they should always be the
  3655. * same. But it gives us something to test.
  3656. */
  3657. if((len1 != len2) || !temp || ((temp - buff1) != len1))
  3658. break; /* failed */
  3659. /* End Gyrations */
  3660. /* The one good exit point */
  3661. result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
  3662. } while(0);
  3663. if(buff1)
  3664. free(buff1);
  3665. return result;
  3666. }
  3667. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
  3668. !(defined(LIBRESSL_VERSION_NUMBER) && \
  3669. LIBRESSL_VERSION_NUMBER < 0x3060000fL) && \
  3670. !defined(OPENSSL_IS_BORINGSSL) && \
  3671. !defined(OPENSSL_IS_AWSLC) && \
  3672. !defined(CURL_DISABLE_VERBOSE_STRINGS)
  3673. static void infof_certstack(struct Curl_easy *data, const SSL *ssl)
  3674. {
  3675. STACK_OF(X509) *certstack;
  3676. long verify_result;
  3677. int num_cert_levels;
  3678. int cert_level;
  3679. verify_result = SSL_get_verify_result(ssl);
  3680. if(verify_result != X509_V_OK)
  3681. certstack = SSL_get_peer_cert_chain(ssl);
  3682. else
  3683. certstack = SSL_get0_verified_chain(ssl);
  3684. num_cert_levels = sk_X509_num(certstack);
  3685. for(cert_level = 0; cert_level < num_cert_levels; cert_level++) {
  3686. char cert_algorithm[80] = "";
  3687. char group_name_final[80] = "";
  3688. const X509_ALGOR *palg_cert = NULL;
  3689. const ASN1_OBJECT *paobj_cert = NULL;
  3690. X509 *current_cert;
  3691. EVP_PKEY *current_pkey;
  3692. int key_bits;
  3693. int key_sec_bits;
  3694. int get_group_name;
  3695. const char *type_name;
  3696. current_cert = sk_X509_value(certstack, cert_level);
  3697. X509_get0_signature(NULL, &palg_cert, current_cert);
  3698. X509_ALGOR_get0(&paobj_cert, NULL, NULL, palg_cert);
  3699. OBJ_obj2txt(cert_algorithm, sizeof(cert_algorithm), paobj_cert, 0);
  3700. current_pkey = X509_get0_pubkey(current_cert);
  3701. key_bits = EVP_PKEY_bits(current_pkey);
  3702. #if (OPENSSL_VERSION_NUMBER < 0x30000000L)
  3703. #define EVP_PKEY_get_security_bits EVP_PKEY_security_bits
  3704. #endif
  3705. key_sec_bits = EVP_PKEY_get_security_bits(current_pkey);
  3706. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  3707. {
  3708. char group_name[80] = "";
  3709. get_group_name = EVP_PKEY_get_group_name(current_pkey, group_name,
  3710. sizeof(group_name), NULL);
  3711. msnprintf(group_name_final, sizeof(group_name_final), "/%s", group_name);
  3712. }
  3713. type_name = EVP_PKEY_get0_type_name(current_pkey);
  3714. #else
  3715. get_group_name = 0;
  3716. type_name = NULL;
  3717. #endif
  3718. infof(data,
  3719. " Certificate level %d: "
  3720. "Public key type %s%s (%d/%d Bits/secBits), signed using %s",
  3721. cert_level, type_name ? type_name : "?",
  3722. get_group_name == 0 ? "" : group_name_final,
  3723. key_bits, key_sec_bits, cert_algorithm);
  3724. }
  3725. }
  3726. #else
  3727. #define infof_certstack(data, ssl)
  3728. #endif
  3729. /*
  3730. * Get the server cert, verify it and show it, etc., only call failf() if the
  3731. * 'strict' argument is TRUE as otherwise all this is for informational
  3732. * purposes only!
  3733. *
  3734. * We check certificates to authenticate the server; otherwise we risk
  3735. * man-in-the-middle attack.
  3736. */
  3737. static CURLcode servercert(struct Curl_cfilter *cf,
  3738. struct Curl_easy *data,
  3739. bool strict)
  3740. {
  3741. struct connectdata *conn = cf->conn;
  3742. struct ssl_connect_data *connssl = cf->ctx;
  3743. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  3744. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3745. CURLcode result = CURLE_OK;
  3746. int rc;
  3747. long lerr;
  3748. X509 *issuer;
  3749. BIO *fp = NULL;
  3750. char error_buffer[256]="";
  3751. char buffer[2048];
  3752. const char *ptr;
  3753. BIO *mem = BIO_new(BIO_s_mem());
  3754. struct ossl_ssl_backend_data *backend =
  3755. (struct ossl_ssl_backend_data *)connssl->backend;
  3756. DEBUGASSERT(backend);
  3757. if(!mem) {
  3758. failf(data,
  3759. "BIO_new return NULL, " OSSL_PACKAGE
  3760. " error %s",
  3761. ossl_strerror(ERR_get_error(), error_buffer,
  3762. sizeof(error_buffer)) );
  3763. return CURLE_OUT_OF_MEMORY;
  3764. }
  3765. if(data->set.ssl.certinfo)
  3766. /* asked to gather certificate info */
  3767. (void)Curl_ossl_certchain(data, backend->handle);
  3768. backend->server_cert = SSL_get1_peer_certificate(backend->handle);
  3769. if(!backend->server_cert) {
  3770. BIO_free(mem);
  3771. if(!strict)
  3772. return CURLE_OK;
  3773. failf(data, "SSL: couldn't get peer certificate");
  3774. return CURLE_PEER_FAILED_VERIFICATION;
  3775. }
  3776. infof(data, "%s certificate:",
  3777. Curl_ssl_cf_is_proxy(cf)? "Proxy" : "Server");
  3778. rc = x509_name_oneline(X509_get_subject_name(backend->server_cert),
  3779. buffer, sizeof(buffer));
  3780. infof(data, " subject: %s", rc?"[NONE]":buffer);
  3781. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3782. {
  3783. long len;
  3784. ASN1_TIME_print(mem, X509_get0_notBefore(backend->server_cert));
  3785. len = BIO_get_mem_data(mem, (char **) &ptr);
  3786. infof(data, " start date: %.*s", (int)len, ptr);
  3787. (void)BIO_reset(mem);
  3788. ASN1_TIME_print(mem, X509_get0_notAfter(backend->server_cert));
  3789. len = BIO_get_mem_data(mem, (char **) &ptr);
  3790. infof(data, " expire date: %.*s", (int)len, ptr);
  3791. (void)BIO_reset(mem);
  3792. }
  3793. #endif
  3794. BIO_free(mem);
  3795. if(conn_config->verifyhost) {
  3796. result = Curl_ossl_verifyhost(data, conn, &connssl->peer,
  3797. backend->server_cert);
  3798. if(result) {
  3799. X509_free(backend->server_cert);
  3800. backend->server_cert = NULL;
  3801. return result;
  3802. }
  3803. }
  3804. rc = x509_name_oneline(X509_get_issuer_name(backend->server_cert),
  3805. buffer, sizeof(buffer));
  3806. if(rc) {
  3807. if(strict)
  3808. failf(data, "SSL: couldn't get X509-issuer name");
  3809. result = CURLE_PEER_FAILED_VERIFICATION;
  3810. }
  3811. else {
  3812. infof(data, " issuer: %s", buffer);
  3813. /* We could do all sorts of certificate verification stuff here before
  3814. deallocating the certificate. */
  3815. /* e.g. match issuer name with provided issuer certificate */
  3816. if(conn_config->issuercert || conn_config->issuercert_blob) {
  3817. if(conn_config->issuercert_blob) {
  3818. fp = BIO_new_mem_buf(conn_config->issuercert_blob->data,
  3819. (int)conn_config->issuercert_blob->len);
  3820. if(!fp) {
  3821. failf(data,
  3822. "BIO_new_mem_buf NULL, " OSSL_PACKAGE
  3823. " error %s",
  3824. ossl_strerror(ERR_get_error(), error_buffer,
  3825. sizeof(error_buffer)) );
  3826. X509_free(backend->server_cert);
  3827. backend->server_cert = NULL;
  3828. return CURLE_OUT_OF_MEMORY;
  3829. }
  3830. }
  3831. else {
  3832. fp = BIO_new(BIO_s_file());
  3833. if(!fp) {
  3834. failf(data,
  3835. "BIO_new return NULL, " OSSL_PACKAGE
  3836. " error %s",
  3837. ossl_strerror(ERR_get_error(), error_buffer,
  3838. sizeof(error_buffer)) );
  3839. X509_free(backend->server_cert);
  3840. backend->server_cert = NULL;
  3841. return CURLE_OUT_OF_MEMORY;
  3842. }
  3843. if(BIO_read_filename(fp, conn_config->issuercert) <= 0) {
  3844. if(strict)
  3845. failf(data, "SSL: Unable to open issuer cert (%s)",
  3846. conn_config->issuercert);
  3847. BIO_free(fp);
  3848. X509_free(backend->server_cert);
  3849. backend->server_cert = NULL;
  3850. return CURLE_SSL_ISSUER_ERROR;
  3851. }
  3852. }
  3853. issuer = PEM_read_bio_X509(fp, NULL, ZERO_NULL, NULL);
  3854. if(!issuer) {
  3855. if(strict)
  3856. failf(data, "SSL: Unable to read issuer cert (%s)",
  3857. conn_config->issuercert);
  3858. BIO_free(fp);
  3859. X509_free(issuer);
  3860. X509_free(backend->server_cert);
  3861. backend->server_cert = NULL;
  3862. return CURLE_SSL_ISSUER_ERROR;
  3863. }
  3864. if(X509_check_issued(issuer, backend->server_cert) != X509_V_OK) {
  3865. if(strict)
  3866. failf(data, "SSL: Certificate issuer check failed (%s)",
  3867. conn_config->issuercert);
  3868. BIO_free(fp);
  3869. X509_free(issuer);
  3870. X509_free(backend->server_cert);
  3871. backend->server_cert = NULL;
  3872. return CURLE_SSL_ISSUER_ERROR;
  3873. }
  3874. infof(data, " SSL certificate issuer check ok (%s)",
  3875. conn_config->issuercert);
  3876. BIO_free(fp);
  3877. X509_free(issuer);
  3878. }
  3879. lerr = SSL_get_verify_result(backend->handle);
  3880. ssl_config->certverifyresult = lerr;
  3881. if(lerr != X509_V_OK) {
  3882. if(conn_config->verifypeer) {
  3883. /* We probably never reach this, because SSL_connect() will fail
  3884. and we return earlier if verifypeer is set? */
  3885. if(strict)
  3886. failf(data, "SSL certificate verify result: %s (%ld)",
  3887. X509_verify_cert_error_string(lerr), lerr);
  3888. result = CURLE_PEER_FAILED_VERIFICATION;
  3889. }
  3890. else
  3891. infof(data, " SSL certificate verify result: %s (%ld),"
  3892. " continuing anyway.",
  3893. X509_verify_cert_error_string(lerr), lerr);
  3894. }
  3895. else
  3896. infof(data, " SSL certificate verify ok.");
  3897. }
  3898. infof_certstack(data, backend->handle);
  3899. #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
  3900. !defined(OPENSSL_NO_OCSP)
  3901. if(conn_config->verifystatus && !connssl->reused_session) {
  3902. /* don't do this after Session ID reuse */
  3903. result = verifystatus(cf, data);
  3904. if(result) {
  3905. /* when verifystatus failed, remove the session id from the cache again
  3906. if present */
  3907. if(!Curl_ssl_cf_is_proxy(cf)) {
  3908. void *old_ssl_sessionid = NULL;
  3909. bool incache;
  3910. Curl_ssl_sessionid_lock(data);
  3911. incache = !(Curl_ssl_getsessionid(cf, data, &old_ssl_sessionid, NULL));
  3912. if(incache) {
  3913. infof(data, "Remove session ID again from cache");
  3914. Curl_ssl_delsessionid(data, old_ssl_sessionid);
  3915. }
  3916. Curl_ssl_sessionid_unlock(data);
  3917. }
  3918. X509_free(backend->server_cert);
  3919. backend->server_cert = NULL;
  3920. return result;
  3921. }
  3922. }
  3923. #endif
  3924. if(!strict)
  3925. /* when not strict, we don't bother about the verify cert problems */
  3926. result = CURLE_OK;
  3927. ptr = Curl_ssl_cf_is_proxy(cf)?
  3928. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  3929. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  3930. if(!result && ptr) {
  3931. result = ossl_pkp_pin_peer_pubkey(data, backend->server_cert, ptr);
  3932. if(result)
  3933. failf(data, "SSL: public key does not match pinned public key");
  3934. }
  3935. X509_free(backend->server_cert);
  3936. backend->server_cert = NULL;
  3937. connssl->connecting_state = ssl_connect_done;
  3938. return result;
  3939. }
  3940. static CURLcode ossl_connect_step3(struct Curl_cfilter *cf,
  3941. struct Curl_easy *data)
  3942. {
  3943. CURLcode result = CURLE_OK;
  3944. struct ssl_connect_data *connssl = cf->ctx;
  3945. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3946. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  3947. /*
  3948. * We check certificates to authenticate the server; otherwise we risk
  3949. * man-in-the-middle attack; NEVERTHELESS, if we're told explicitly not to
  3950. * verify the peer, ignore faults and failures from the server cert
  3951. * operations.
  3952. */
  3953. result = servercert(cf, data, conn_config->verifypeer ||
  3954. conn_config->verifyhost);
  3955. if(!result)
  3956. connssl->connecting_state = ssl_connect_done;
  3957. return result;
  3958. }
  3959. static CURLcode ossl_connect_common(struct Curl_cfilter *cf,
  3960. struct Curl_easy *data,
  3961. bool nonblocking,
  3962. bool *done)
  3963. {
  3964. CURLcode result = CURLE_OK;
  3965. struct ssl_connect_data *connssl = cf->ctx;
  3966. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  3967. int what;
  3968. /* check if the connection has already been established */
  3969. if(ssl_connection_complete == connssl->state) {
  3970. *done = TRUE;
  3971. return CURLE_OK;
  3972. }
  3973. if(ssl_connect_1 == connssl->connecting_state) {
  3974. /* Find out how much more time we're allowed */
  3975. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  3976. if(timeout_ms < 0) {
  3977. /* no need to continue if time is already up */
  3978. failf(data, "SSL connection timeout");
  3979. return CURLE_OPERATION_TIMEDOUT;
  3980. }
  3981. result = ossl_connect_step1(cf, data);
  3982. if(result)
  3983. goto out;
  3984. }
  3985. while(ssl_connect_2 == connssl->connecting_state ||
  3986. ssl_connect_2_reading == connssl->connecting_state ||
  3987. ssl_connect_2_writing == connssl->connecting_state) {
  3988. /* check allowed time left */
  3989. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  3990. if(timeout_ms < 0) {
  3991. /* no need to continue if time already is up */
  3992. failf(data, "SSL connection timeout");
  3993. result = CURLE_OPERATION_TIMEDOUT;
  3994. goto out;
  3995. }
  3996. /* if ssl is expecting something, check if it's available. */
  3997. if(!nonblocking &&
  3998. (connssl->connecting_state == ssl_connect_2_reading ||
  3999. connssl->connecting_state == ssl_connect_2_writing)) {
  4000. curl_socket_t writefd = ssl_connect_2_writing ==
  4001. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  4002. curl_socket_t readfd = ssl_connect_2_reading ==
  4003. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  4004. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  4005. timeout_ms);
  4006. if(what < 0) {
  4007. /* fatal error */
  4008. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  4009. result = CURLE_SSL_CONNECT_ERROR;
  4010. goto out;
  4011. }
  4012. if(0 == what) {
  4013. /* timeout */
  4014. failf(data, "SSL connection timeout");
  4015. result = CURLE_OPERATION_TIMEDOUT;
  4016. goto out;
  4017. }
  4018. /* socket is readable or writable */
  4019. }
  4020. /* Run transaction, and return to the caller if it failed or if this
  4021. * connection is done nonblocking and this loop would execute again. This
  4022. * permits the owner of a multi handle to abort a connection attempt
  4023. * before step2 has completed while ensuring that a client using select()
  4024. * or epoll() will always have a valid fdset to wait on.
  4025. */
  4026. result = ossl_connect_step2(cf, data);
  4027. if(result || (nonblocking &&
  4028. (ssl_connect_2 == connssl->connecting_state ||
  4029. ssl_connect_2_reading == connssl->connecting_state ||
  4030. ssl_connect_2_writing == connssl->connecting_state)))
  4031. goto out;
  4032. } /* repeat step2 until all transactions are done. */
  4033. if(ssl_connect_3 == connssl->connecting_state) {
  4034. result = ossl_connect_step3(cf, data);
  4035. if(result)
  4036. goto out;
  4037. }
  4038. if(ssl_connect_done == connssl->connecting_state) {
  4039. connssl->state = ssl_connection_complete;
  4040. *done = TRUE;
  4041. }
  4042. else
  4043. *done = FALSE;
  4044. /* Reset our connect state machine */
  4045. connssl->connecting_state = ssl_connect_1;
  4046. out:
  4047. return result;
  4048. }
  4049. static CURLcode ossl_connect_nonblocking(struct Curl_cfilter *cf,
  4050. struct Curl_easy *data,
  4051. bool *done)
  4052. {
  4053. return ossl_connect_common(cf, data, TRUE, done);
  4054. }
  4055. static CURLcode ossl_connect(struct Curl_cfilter *cf,
  4056. struct Curl_easy *data)
  4057. {
  4058. CURLcode result;
  4059. bool done = FALSE;
  4060. result = ossl_connect_common(cf, data, FALSE, &done);
  4061. if(result)
  4062. return result;
  4063. DEBUGASSERT(done);
  4064. return CURLE_OK;
  4065. }
  4066. static bool ossl_data_pending(struct Curl_cfilter *cf,
  4067. const struct Curl_easy *data)
  4068. {
  4069. struct ssl_connect_data *connssl = cf->ctx;
  4070. struct ossl_ssl_backend_data *backend =
  4071. (struct ossl_ssl_backend_data *)connssl->backend;
  4072. (void)data;
  4073. DEBUGASSERT(connssl && backend);
  4074. if(backend->handle && SSL_pending(backend->handle))
  4075. return TRUE;
  4076. return FALSE;
  4077. }
  4078. static ssize_t ossl_send(struct Curl_cfilter *cf,
  4079. struct Curl_easy *data,
  4080. const void *mem,
  4081. size_t len,
  4082. CURLcode *curlcode)
  4083. {
  4084. /* SSL_write() is said to return 'int' while write() and send() returns
  4085. 'size_t' */
  4086. int err;
  4087. char error_buffer[256];
  4088. sslerr_t sslerror;
  4089. int memlen;
  4090. int rc;
  4091. struct ssl_connect_data *connssl = cf->ctx;
  4092. struct ossl_ssl_backend_data *backend =
  4093. (struct ossl_ssl_backend_data *)connssl->backend;
  4094. (void)data;
  4095. DEBUGASSERT(backend);
  4096. ERR_clear_error();
  4097. memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  4098. rc = SSL_write(backend->handle, mem, memlen);
  4099. if(rc <= 0) {
  4100. err = SSL_get_error(backend->handle, rc);
  4101. switch(err) {
  4102. case SSL_ERROR_WANT_READ:
  4103. case SSL_ERROR_WANT_WRITE:
  4104. /* The operation did not complete; the same TLS/SSL I/O function
  4105. should be called again later. This is basically an EWOULDBLOCK
  4106. equivalent. */
  4107. *curlcode = CURLE_AGAIN;
  4108. rc = -1;
  4109. goto out;
  4110. case SSL_ERROR_SYSCALL:
  4111. {
  4112. int sockerr = SOCKERRNO;
  4113. if(backend->io_result == CURLE_AGAIN) {
  4114. *curlcode = CURLE_AGAIN;
  4115. rc = -1;
  4116. goto out;
  4117. }
  4118. sslerror = ERR_get_error();
  4119. if(sslerror)
  4120. ossl_strerror(sslerror, error_buffer, sizeof(error_buffer));
  4121. else if(sockerr)
  4122. Curl_strerror(sockerr, error_buffer, sizeof(error_buffer));
  4123. else
  4124. msnprintf(error_buffer, sizeof(error_buffer), "%s",
  4125. SSL_ERROR_to_str(err));
  4126. failf(data, OSSL_PACKAGE " SSL_write: %s, errno %d",
  4127. error_buffer, sockerr);
  4128. *curlcode = CURLE_SEND_ERROR;
  4129. rc = -1;
  4130. goto out;
  4131. }
  4132. case SSL_ERROR_SSL: {
  4133. /* A failure in the SSL library occurred, usually a protocol error.
  4134. The OpenSSL error queue contains more information on the error. */
  4135. sslerror = ERR_get_error();
  4136. failf(data, "SSL_write() error: %s",
  4137. ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)));
  4138. *curlcode = CURLE_SEND_ERROR;
  4139. rc = -1;
  4140. goto out;
  4141. }
  4142. default:
  4143. /* a true error */
  4144. failf(data, OSSL_PACKAGE " SSL_write: %s, errno %d",
  4145. SSL_ERROR_to_str(err), SOCKERRNO);
  4146. *curlcode = CURLE_SEND_ERROR;
  4147. rc = -1;
  4148. goto out;
  4149. }
  4150. }
  4151. *curlcode = CURLE_OK;
  4152. out:
  4153. return (ssize_t)rc; /* number of bytes */
  4154. }
  4155. static ssize_t ossl_recv(struct Curl_cfilter *cf,
  4156. struct Curl_easy *data, /* transfer */
  4157. char *buf, /* store read data here */
  4158. size_t buffersize, /* max amount to read */
  4159. CURLcode *curlcode)
  4160. {
  4161. char error_buffer[256];
  4162. unsigned long sslerror;
  4163. ssize_t nread;
  4164. int buffsize;
  4165. struct connectdata *conn = cf->conn;
  4166. struct ssl_connect_data *connssl = cf->ctx;
  4167. struct ossl_ssl_backend_data *backend =
  4168. (struct ossl_ssl_backend_data *)connssl->backend;
  4169. (void)data;
  4170. DEBUGASSERT(backend);
  4171. ERR_clear_error();
  4172. buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  4173. nread = (ssize_t)SSL_read(backend->handle, buf, buffsize);
  4174. if(nread <= 0) {
  4175. /* failed SSL_read */
  4176. int err = SSL_get_error(backend->handle, (int)nread);
  4177. switch(err) {
  4178. case SSL_ERROR_NONE: /* this is not an error */
  4179. break;
  4180. case SSL_ERROR_ZERO_RETURN: /* no more data */
  4181. /* close_notify alert */
  4182. if(cf->sockindex == FIRSTSOCKET)
  4183. /* mark the connection for close if it is indeed the control
  4184. connection */
  4185. connclose(conn, "TLS close_notify");
  4186. break;
  4187. case SSL_ERROR_WANT_READ:
  4188. case SSL_ERROR_WANT_WRITE:
  4189. /* there's data pending, re-invoke SSL_read() */
  4190. *curlcode = CURLE_AGAIN;
  4191. nread = -1;
  4192. goto out;
  4193. default:
  4194. /* openssl/ssl.h for SSL_ERROR_SYSCALL says "look at error stack/return
  4195. value/errno" */
  4196. /* https://www.openssl.org/docs/crypto/ERR_get_error.html */
  4197. if(backend->io_result == CURLE_AGAIN) {
  4198. *curlcode = CURLE_AGAIN;
  4199. nread = -1;
  4200. goto out;
  4201. }
  4202. sslerror = ERR_get_error();
  4203. if((nread < 0) || sslerror) {
  4204. /* If the return code was negative or there actually is an error in the
  4205. queue */
  4206. int sockerr = SOCKERRNO;
  4207. if(sslerror)
  4208. ossl_strerror(sslerror, error_buffer, sizeof(error_buffer));
  4209. else if(sockerr && err == SSL_ERROR_SYSCALL)
  4210. Curl_strerror(sockerr, error_buffer, sizeof(error_buffer));
  4211. else
  4212. msnprintf(error_buffer, sizeof(error_buffer), "%s",
  4213. SSL_ERROR_to_str(err));
  4214. failf(data, OSSL_PACKAGE " SSL_read: %s, errno %d",
  4215. error_buffer, sockerr);
  4216. *curlcode = CURLE_RECV_ERROR;
  4217. nread = -1;
  4218. goto out;
  4219. }
  4220. /* For debug builds be a little stricter and error on any
  4221. SSL_ERROR_SYSCALL. For example a server may have closed the connection
  4222. abruptly without a close_notify alert. For compatibility with older
  4223. peers we don't do this by default. #4624
  4224. We can use this to gauge how many users may be affected, and
  4225. if it goes ok eventually transition to allow in dev and release with
  4226. the newest OpenSSL: #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) */
  4227. #ifdef DEBUGBUILD
  4228. if(err == SSL_ERROR_SYSCALL) {
  4229. int sockerr = SOCKERRNO;
  4230. if(sockerr)
  4231. Curl_strerror(sockerr, error_buffer, sizeof(error_buffer));
  4232. else {
  4233. msnprintf(error_buffer, sizeof(error_buffer),
  4234. "Connection closed abruptly");
  4235. }
  4236. failf(data, OSSL_PACKAGE " SSL_read: %s, errno %d"
  4237. " (Fatal because this is a curl debug build)",
  4238. error_buffer, sockerr);
  4239. *curlcode = CURLE_RECV_ERROR;
  4240. nread = -1;
  4241. goto out;
  4242. }
  4243. #endif
  4244. }
  4245. }
  4246. out:
  4247. return nread;
  4248. }
  4249. static size_t ossl_version(char *buffer, size_t size)
  4250. {
  4251. #ifdef LIBRESSL_VERSION_NUMBER
  4252. #ifdef HAVE_OPENSSL_VERSION
  4253. char *p;
  4254. int count;
  4255. const char *ver = OpenSSL_version(OPENSSL_VERSION);
  4256. const char expected[] = OSSL_PACKAGE " "; /* ie "LibreSSL " */
  4257. if(strncasecompare(ver, expected, sizeof(expected) - 1)) {
  4258. ver += sizeof(expected) - 1;
  4259. }
  4260. count = msnprintf(buffer, size, "%s/%s", OSSL_PACKAGE, ver);
  4261. for(p = buffer; *p; ++p) {
  4262. if(ISBLANK(*p))
  4263. *p = '_';
  4264. }
  4265. return count;
  4266. #else
  4267. return msnprintf(buffer, size, "%s/%lx.%lx.%lx",
  4268. OSSL_PACKAGE,
  4269. (LIBRESSL_VERSION_NUMBER>>28)&0xf,
  4270. (LIBRESSL_VERSION_NUMBER>>20)&0xff,
  4271. (LIBRESSL_VERSION_NUMBER>>12)&0xff);
  4272. #endif
  4273. #elif defined(OPENSSL_IS_BORINGSSL)
  4274. #ifdef CURL_BORINGSSL_VERSION
  4275. return msnprintf(buffer, size, "%s/%s",
  4276. OSSL_PACKAGE,
  4277. CURL_BORINGSSL_VERSION);
  4278. #else
  4279. return msnprintf(buffer, size, OSSL_PACKAGE);
  4280. #endif
  4281. #elif defined(OPENSSL_IS_AWSLC)
  4282. return msnprintf(buffer, size, "%s/%s",
  4283. OSSL_PACKAGE,
  4284. AWSLC_VERSION_NUMBER_STRING);
  4285. #elif defined(HAVE_OPENSSL_VERSION) && defined(OPENSSL_VERSION_STRING)
  4286. return msnprintf(buffer, size, "%s/%s",
  4287. OSSL_PACKAGE, OpenSSL_version(OPENSSL_VERSION_STRING));
  4288. #else
  4289. /* not LibreSSL, BoringSSL and not using OpenSSL_version */
  4290. char sub[3];
  4291. unsigned long ssleay_value;
  4292. sub[2]='\0';
  4293. sub[1]='\0';
  4294. ssleay_value = OpenSSL_version_num();
  4295. if(ssleay_value < 0x906000) {
  4296. ssleay_value = SSLEAY_VERSION_NUMBER;
  4297. sub[0]='\0';
  4298. }
  4299. else {
  4300. if(ssleay_value&0xff0) {
  4301. int minor_ver = (ssleay_value >> 4) & 0xff;
  4302. if(minor_ver > 26) {
  4303. /* handle extended version introduced for 0.9.8za */
  4304. sub[1] = (char) ((minor_ver - 1) % 26 + 'a' + 1);
  4305. sub[0] = 'z';
  4306. }
  4307. else {
  4308. sub[0] = (char) (minor_ver + 'a' - 1);
  4309. }
  4310. }
  4311. else
  4312. sub[0]='\0';
  4313. }
  4314. return msnprintf(buffer, size, "%s/%lx.%lx.%lx%s"
  4315. #ifdef OPENSSL_FIPS
  4316. "-fips"
  4317. #endif
  4318. ,
  4319. OSSL_PACKAGE,
  4320. (ssleay_value>>28)&0xf,
  4321. (ssleay_value>>20)&0xff,
  4322. (ssleay_value>>12)&0xff,
  4323. sub);
  4324. #endif /* OPENSSL_IS_BORINGSSL */
  4325. }
  4326. /* can be called with data == NULL */
  4327. static CURLcode ossl_random(struct Curl_easy *data,
  4328. unsigned char *entropy, size_t length)
  4329. {
  4330. int rc;
  4331. if(data) {
  4332. if(ossl_seed(data)) /* Initiate the seed if not already done */
  4333. return CURLE_FAILED_INIT; /* couldn't seed for some reason */
  4334. }
  4335. else {
  4336. if(!rand_enough())
  4337. return CURLE_FAILED_INIT;
  4338. }
  4339. /* RAND_bytes() returns 1 on success, 0 otherwise. */
  4340. rc = RAND_bytes(entropy, curlx_uztosi(length));
  4341. return (rc == 1 ? CURLE_OK : CURLE_FAILED_INIT);
  4342. }
  4343. #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
  4344. static CURLcode ossl_sha256sum(const unsigned char *tmp, /* input */
  4345. size_t tmplen,
  4346. unsigned char *sha256sum /* output */,
  4347. size_t unused)
  4348. {
  4349. EVP_MD_CTX *mdctx;
  4350. unsigned int len = 0;
  4351. (void) unused;
  4352. mdctx = EVP_MD_CTX_create();
  4353. if(!mdctx)
  4354. return CURLE_OUT_OF_MEMORY;
  4355. if(!EVP_DigestInit(mdctx, EVP_sha256())) {
  4356. EVP_MD_CTX_destroy(mdctx);
  4357. return CURLE_FAILED_INIT;
  4358. }
  4359. EVP_DigestUpdate(mdctx, tmp, tmplen);
  4360. EVP_DigestFinal_ex(mdctx, sha256sum, &len);
  4361. EVP_MD_CTX_destroy(mdctx);
  4362. return CURLE_OK;
  4363. }
  4364. #endif
  4365. static bool ossl_cert_status_request(void)
  4366. {
  4367. #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
  4368. !defined(OPENSSL_NO_OCSP)
  4369. return TRUE;
  4370. #else
  4371. return FALSE;
  4372. #endif
  4373. }
  4374. static void *ossl_get_internals(struct ssl_connect_data *connssl,
  4375. CURLINFO info)
  4376. {
  4377. /* Legacy: CURLINFO_TLS_SESSION must return an SSL_CTX pointer. */
  4378. struct ossl_ssl_backend_data *backend =
  4379. (struct ossl_ssl_backend_data *)connssl->backend;
  4380. DEBUGASSERT(backend);
  4381. return info == CURLINFO_TLS_SESSION ?
  4382. (void *)backend->ctx : (void *)backend->handle;
  4383. }
  4384. static void ossl_free_multi_ssl_backend_data(
  4385. struct multi_ssl_backend_data *mbackend)
  4386. {
  4387. #if defined(HAVE_SSL_X509_STORE_SHARE)
  4388. if(mbackend->store) {
  4389. X509_STORE_free(mbackend->store);
  4390. }
  4391. free(mbackend->CAfile);
  4392. free(mbackend);
  4393. #else /* HAVE_SSL_X509_STORE_SHARE */
  4394. (void)mbackend;
  4395. #endif /* HAVE_SSL_X509_STORE_SHARE */
  4396. }
  4397. const struct Curl_ssl Curl_ssl_openssl = {
  4398. { CURLSSLBACKEND_OPENSSL, "openssl" }, /* info */
  4399. SSLSUPP_CA_PATH |
  4400. SSLSUPP_CAINFO_BLOB |
  4401. SSLSUPP_CERTINFO |
  4402. SSLSUPP_PINNEDPUBKEY |
  4403. SSLSUPP_SSL_CTX |
  4404. #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
  4405. SSLSUPP_TLS13_CIPHERSUITES |
  4406. #endif
  4407. SSLSUPP_HTTPS_PROXY,
  4408. sizeof(struct ossl_ssl_backend_data),
  4409. ossl_init, /* init */
  4410. ossl_cleanup, /* cleanup */
  4411. ossl_version, /* version */
  4412. Curl_none_check_cxn, /* check_cxn */
  4413. ossl_shutdown, /* shutdown */
  4414. ossl_data_pending, /* data_pending */
  4415. ossl_random, /* random */
  4416. ossl_cert_status_request, /* cert_status_request */
  4417. ossl_connect, /* connect */
  4418. ossl_connect_nonblocking, /* connect_nonblocking */
  4419. Curl_ssl_adjust_pollset, /* adjust_pollset */
  4420. ossl_get_internals, /* get_internals */
  4421. ossl_close, /* close_one */
  4422. ossl_close_all, /* close_all */
  4423. ossl_session_free, /* session_free */
  4424. ossl_set_engine, /* set_engine */
  4425. ossl_set_engine_default, /* set_engine_default */
  4426. ossl_engines_list, /* engines_list */
  4427. Curl_none_false_start, /* false_start */
  4428. #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
  4429. ossl_sha256sum, /* sha256sum */
  4430. #else
  4431. NULL, /* sha256sum */
  4432. #endif
  4433. NULL, /* use of data in this connection */
  4434. NULL, /* remote of data from this connection */
  4435. ossl_free_multi_ssl_backend_data, /* free_multi_ssl_backend_data */
  4436. ossl_recv, /* recv decrypted data */
  4437. ossl_send, /* send data to encrypt */
  4438. };
  4439. #endif /* USE_OPENSSL */