ssl_lib.c 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include "ssl_local.h"
  13. #include "internal/e_os.h"
  14. #include <openssl/objects.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/rand.h>
  17. #include <openssl/ocsp.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/engine.h>
  20. #include <openssl/async.h>
  21. #include <openssl/ct.h>
  22. #include <openssl/trace.h>
  23. #include "internal/cryptlib.h"
  24. #include "internal/refcount.h"
  25. #include "internal/ktls.h"
  26. static int ssl_undefined_function_1(SSL *ssl, SSL3_RECORD *r, size_t s, int t,
  27. SSL_MAC_BUF *mac, size_t macsize)
  28. {
  29. return ssl_undefined_function(ssl);
  30. }
  31. static int ssl_undefined_function_2(SSL *ssl, SSL3_RECORD *r, unsigned char *s,
  32. int t)
  33. {
  34. return ssl_undefined_function(ssl);
  35. }
  36. static int ssl_undefined_function_3(SSL *ssl, unsigned char *r,
  37. unsigned char *s, size_t t, size_t *u)
  38. {
  39. return ssl_undefined_function(ssl);
  40. }
  41. static int ssl_undefined_function_4(SSL *ssl, int r)
  42. {
  43. return ssl_undefined_function(ssl);
  44. }
  45. static size_t ssl_undefined_function_5(SSL *ssl, const char *r, size_t s,
  46. unsigned char *t)
  47. {
  48. return ssl_undefined_function(ssl);
  49. }
  50. static int ssl_undefined_function_6(int r)
  51. {
  52. return ssl_undefined_function(NULL);
  53. }
  54. static int ssl_undefined_function_7(SSL *ssl, unsigned char *r, size_t s,
  55. const char *t, size_t u,
  56. const unsigned char *v, size_t w, int x)
  57. {
  58. return ssl_undefined_function(ssl);
  59. }
  60. SSL3_ENC_METHOD ssl3_undef_enc_method = {
  61. ssl_undefined_function_1,
  62. ssl_undefined_function_2,
  63. ssl_undefined_function,
  64. ssl_undefined_function_3,
  65. ssl_undefined_function_4,
  66. ssl_undefined_function_5,
  67. NULL, /* client_finished_label */
  68. 0, /* client_finished_label_len */
  69. NULL, /* server_finished_label */
  70. 0, /* server_finished_label_len */
  71. ssl_undefined_function_6,
  72. ssl_undefined_function_7,
  73. };
  74. struct ssl_async_args {
  75. SSL *s;
  76. void *buf;
  77. size_t num;
  78. enum { READFUNC, WRITEFUNC, OTHERFUNC } type;
  79. union {
  80. int (*func_read) (SSL *, void *, size_t, size_t *);
  81. int (*func_write) (SSL *, const void *, size_t, size_t *);
  82. int (*func_other) (SSL *);
  83. } f;
  84. };
  85. static const struct {
  86. uint8_t mtype;
  87. uint8_t ord;
  88. int nid;
  89. } dane_mds[] = {
  90. {
  91. DANETLS_MATCHING_FULL, 0, NID_undef
  92. },
  93. {
  94. DANETLS_MATCHING_2256, 1, NID_sha256
  95. },
  96. {
  97. DANETLS_MATCHING_2512, 2, NID_sha512
  98. },
  99. };
  100. static int dane_ctx_enable(struct dane_ctx_st *dctx)
  101. {
  102. const EVP_MD **mdevp;
  103. uint8_t *mdord;
  104. uint8_t mdmax = DANETLS_MATCHING_LAST;
  105. int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
  106. size_t i;
  107. if (dctx->mdevp != NULL)
  108. return 1;
  109. mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
  110. mdord = OPENSSL_zalloc(n * sizeof(*mdord));
  111. if (mdord == NULL || mdevp == NULL) {
  112. OPENSSL_free(mdord);
  113. OPENSSL_free(mdevp);
  114. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  115. return 0;
  116. }
  117. /* Install default entries */
  118. for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
  119. const EVP_MD *md;
  120. if (dane_mds[i].nid == NID_undef ||
  121. (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
  122. continue;
  123. mdevp[dane_mds[i].mtype] = md;
  124. mdord[dane_mds[i].mtype] = dane_mds[i].ord;
  125. }
  126. dctx->mdevp = mdevp;
  127. dctx->mdord = mdord;
  128. dctx->mdmax = mdmax;
  129. return 1;
  130. }
  131. static void dane_ctx_final(struct dane_ctx_st *dctx)
  132. {
  133. OPENSSL_free(dctx->mdevp);
  134. dctx->mdevp = NULL;
  135. OPENSSL_free(dctx->mdord);
  136. dctx->mdord = NULL;
  137. dctx->mdmax = 0;
  138. }
  139. static void tlsa_free(danetls_record *t)
  140. {
  141. if (t == NULL)
  142. return;
  143. OPENSSL_free(t->data);
  144. EVP_PKEY_free(t->spki);
  145. OPENSSL_free(t);
  146. }
  147. static void dane_final(SSL_DANE *dane)
  148. {
  149. sk_danetls_record_pop_free(dane->trecs, tlsa_free);
  150. dane->trecs = NULL;
  151. OSSL_STACK_OF_X509_free(dane->certs);
  152. dane->certs = NULL;
  153. X509_free(dane->mcert);
  154. dane->mcert = NULL;
  155. dane->mtlsa = NULL;
  156. dane->mdpth = -1;
  157. dane->pdpth = -1;
  158. }
  159. /*
  160. * dane_copy - Copy dane configuration, sans verification state.
  161. */
  162. static int ssl_dane_dup(SSL *to, SSL *from)
  163. {
  164. int num;
  165. int i;
  166. if (!DANETLS_ENABLED(&from->dane))
  167. return 1;
  168. num = sk_danetls_record_num(from->dane.trecs);
  169. dane_final(&to->dane);
  170. to->dane.flags = from->dane.flags;
  171. to->dane.dctx = &to->ctx->dane;
  172. to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
  173. if (to->dane.trecs == NULL) {
  174. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  175. return 0;
  176. }
  177. for (i = 0; i < num; ++i) {
  178. danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
  179. if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype,
  180. t->data, t->dlen) <= 0)
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. static int dane_mtype_set(struct dane_ctx_st *dctx,
  186. const EVP_MD *md, uint8_t mtype, uint8_t ord)
  187. {
  188. int i;
  189. if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
  190. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
  191. return 0;
  192. }
  193. if (mtype > dctx->mdmax) {
  194. const EVP_MD **mdevp;
  195. uint8_t *mdord;
  196. int n = ((int)mtype) + 1;
  197. mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
  198. if (mdevp == NULL) {
  199. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  200. return -1;
  201. }
  202. dctx->mdevp = mdevp;
  203. mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
  204. if (mdord == NULL) {
  205. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  206. return -1;
  207. }
  208. dctx->mdord = mdord;
  209. /* Zero-fill any gaps */
  210. for (i = dctx->mdmax + 1; i < mtype; ++i) {
  211. mdevp[i] = NULL;
  212. mdord[i] = 0;
  213. }
  214. dctx->mdmax = mtype;
  215. }
  216. dctx->mdevp[mtype] = md;
  217. /* Coerce ordinal of disabled matching types to 0 */
  218. dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
  219. return 1;
  220. }
  221. static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
  222. {
  223. if (mtype > dane->dctx->mdmax)
  224. return NULL;
  225. return dane->dctx->mdevp[mtype];
  226. }
  227. static int dane_tlsa_add(SSL_DANE *dane,
  228. uint8_t usage,
  229. uint8_t selector,
  230. uint8_t mtype, const unsigned char *data, size_t dlen)
  231. {
  232. danetls_record *t;
  233. const EVP_MD *md = NULL;
  234. int ilen = (int)dlen;
  235. int i;
  236. int num;
  237. if (dane->trecs == NULL) {
  238. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED);
  239. return -1;
  240. }
  241. if (ilen < 0 || dlen != (size_t)ilen) {
  242. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
  243. return 0;
  244. }
  245. if (usage > DANETLS_USAGE_LAST) {
  246. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
  247. return 0;
  248. }
  249. if (selector > DANETLS_SELECTOR_LAST) {
  250. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR);
  251. return 0;
  252. }
  253. if (mtype != DANETLS_MATCHING_FULL) {
  254. md = tlsa_md_get(dane, mtype);
  255. if (md == NULL) {
  256. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
  257. return 0;
  258. }
  259. }
  260. if (md != NULL && dlen != (size_t)EVP_MD_get_size(md)) {
  261. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
  262. return 0;
  263. }
  264. if (!data) {
  265. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA);
  266. return 0;
  267. }
  268. if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) {
  269. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  270. return -1;
  271. }
  272. t->usage = usage;
  273. t->selector = selector;
  274. t->mtype = mtype;
  275. t->data = OPENSSL_malloc(dlen);
  276. if (t->data == NULL) {
  277. tlsa_free(t);
  278. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  279. return -1;
  280. }
  281. memcpy(t->data, data, dlen);
  282. t->dlen = dlen;
  283. /* Validate and cache full certificate or public key */
  284. if (mtype == DANETLS_MATCHING_FULL) {
  285. const unsigned char *p = data;
  286. X509 *cert = NULL;
  287. EVP_PKEY *pkey = NULL;
  288. switch (selector) {
  289. case DANETLS_SELECTOR_CERT:
  290. if (!d2i_X509(&cert, &p, ilen) || p < data ||
  291. dlen != (size_t)(p - data)) {
  292. tlsa_free(t);
  293. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  294. return 0;
  295. }
  296. if (X509_get0_pubkey(cert) == NULL) {
  297. tlsa_free(t);
  298. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  299. return 0;
  300. }
  301. if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
  302. X509_free(cert);
  303. break;
  304. }
  305. /*
  306. * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
  307. * records that contain full certificates of trust-anchors that are
  308. * not present in the wire chain. For usage PKIX-TA(0), we augment
  309. * the chain with untrusted Full(0) certificates from DNS, in case
  310. * they are missing from the chain.
  311. */
  312. if ((dane->certs == NULL &&
  313. (dane->certs = sk_X509_new_null()) == NULL) ||
  314. !sk_X509_push(dane->certs, cert)) {
  315. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  316. X509_free(cert);
  317. tlsa_free(t);
  318. return -1;
  319. }
  320. break;
  321. case DANETLS_SELECTOR_SPKI:
  322. if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data ||
  323. dlen != (size_t)(p - data)) {
  324. tlsa_free(t);
  325. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
  326. return 0;
  327. }
  328. /*
  329. * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
  330. * records that contain full bare keys of trust-anchors that are
  331. * not present in the wire chain.
  332. */
  333. if (usage == DANETLS_USAGE_DANE_TA)
  334. t->spki = pkey;
  335. else
  336. EVP_PKEY_free(pkey);
  337. break;
  338. }
  339. }
  340. /*-
  341. * Find the right insertion point for the new record.
  342. *
  343. * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
  344. * they can be processed first, as they require no chain building, and no
  345. * expiration or hostname checks. Because DANE-EE(3) is numerically
  346. * largest, this is accomplished via descending sort by "usage".
  347. *
  348. * We also sort in descending order by matching ordinal to simplify
  349. * the implementation of digest agility in the verification code.
  350. *
  351. * The choice of order for the selector is not significant, so we
  352. * use the same descending order for consistency.
  353. */
  354. num = sk_danetls_record_num(dane->trecs);
  355. for (i = 0; i < num; ++i) {
  356. danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
  357. if (rec->usage > usage)
  358. continue;
  359. if (rec->usage < usage)
  360. break;
  361. if (rec->selector > selector)
  362. continue;
  363. if (rec->selector < selector)
  364. break;
  365. if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
  366. continue;
  367. break;
  368. }
  369. if (!sk_danetls_record_insert(dane->trecs, t, i)) {
  370. tlsa_free(t);
  371. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  372. return -1;
  373. }
  374. dane->umask |= DANETLS_USAGE_BIT(usage);
  375. return 1;
  376. }
  377. /*
  378. * Return 0 if there is only one version configured and it was disabled
  379. * at configure time. Return 1 otherwise.
  380. */
  381. static int ssl_check_allowed_versions(int min_version, int max_version)
  382. {
  383. int minisdtls = 0, maxisdtls = 0;
  384. /* Figure out if we're doing DTLS versions or TLS versions */
  385. if (min_version == DTLS1_BAD_VER
  386. || min_version >> 8 == DTLS1_VERSION_MAJOR)
  387. minisdtls = 1;
  388. if (max_version == DTLS1_BAD_VER
  389. || max_version >> 8 == DTLS1_VERSION_MAJOR)
  390. maxisdtls = 1;
  391. /* A wildcard version of 0 could be DTLS or TLS. */
  392. if ((minisdtls && !maxisdtls && max_version != 0)
  393. || (maxisdtls && !minisdtls && min_version != 0)) {
  394. /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
  395. return 0;
  396. }
  397. if (minisdtls || maxisdtls) {
  398. /* Do DTLS version checks. */
  399. if (min_version == 0)
  400. /* Ignore DTLS1_BAD_VER */
  401. min_version = DTLS1_VERSION;
  402. if (max_version == 0)
  403. max_version = DTLS1_2_VERSION;
  404. #ifdef OPENSSL_NO_DTLS1_2
  405. if (max_version == DTLS1_2_VERSION)
  406. max_version = DTLS1_VERSION;
  407. #endif
  408. #ifdef OPENSSL_NO_DTLS1
  409. if (min_version == DTLS1_VERSION)
  410. min_version = DTLS1_2_VERSION;
  411. #endif
  412. /* Done massaging versions; do the check. */
  413. if (0
  414. #ifdef OPENSSL_NO_DTLS1
  415. || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
  416. && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
  417. #endif
  418. #ifdef OPENSSL_NO_DTLS1_2
  419. || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
  420. && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
  421. #endif
  422. )
  423. return 0;
  424. } else {
  425. /* Regular TLS version checks. */
  426. if (min_version == 0)
  427. min_version = SSL3_VERSION;
  428. if (max_version == 0)
  429. max_version = TLS1_3_VERSION;
  430. #ifdef OPENSSL_NO_TLS1_3
  431. if (max_version == TLS1_3_VERSION)
  432. max_version = TLS1_2_VERSION;
  433. #endif
  434. #ifdef OPENSSL_NO_TLS1_2
  435. if (max_version == TLS1_2_VERSION)
  436. max_version = TLS1_1_VERSION;
  437. #endif
  438. #ifdef OPENSSL_NO_TLS1_1
  439. if (max_version == TLS1_1_VERSION)
  440. max_version = TLS1_VERSION;
  441. #endif
  442. #ifdef OPENSSL_NO_TLS1
  443. if (max_version == TLS1_VERSION)
  444. max_version = SSL3_VERSION;
  445. #endif
  446. #ifdef OPENSSL_NO_SSL3
  447. if (min_version == SSL3_VERSION)
  448. min_version = TLS1_VERSION;
  449. #endif
  450. #ifdef OPENSSL_NO_TLS1
  451. if (min_version == TLS1_VERSION)
  452. min_version = TLS1_1_VERSION;
  453. #endif
  454. #ifdef OPENSSL_NO_TLS1_1
  455. if (min_version == TLS1_1_VERSION)
  456. min_version = TLS1_2_VERSION;
  457. #endif
  458. #ifdef OPENSSL_NO_TLS1_2
  459. if (min_version == TLS1_2_VERSION)
  460. min_version = TLS1_3_VERSION;
  461. #endif
  462. /* Done massaging versions; do the check. */
  463. if (0
  464. #ifdef OPENSSL_NO_SSL3
  465. || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
  466. #endif
  467. #ifdef OPENSSL_NO_TLS1
  468. || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
  469. #endif
  470. #ifdef OPENSSL_NO_TLS1_1
  471. || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
  472. #endif
  473. #ifdef OPENSSL_NO_TLS1_2
  474. || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
  475. #endif
  476. #ifdef OPENSSL_NO_TLS1_3
  477. || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
  478. #endif
  479. )
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. #if defined(__TANDEM) && defined(OPENSSL_VPROC)
  485. /*
  486. * Define a VPROC function for HP NonStop build ssl library.
  487. * This is used by platform version identification tools.
  488. * Do not inline this procedure or make it static.
  489. */
  490. # define OPENSSL_VPROC_STRING_(x) x##_SSL
  491. # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
  492. # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
  493. void OPENSSL_VPROC_FUNC(void) {}
  494. #endif
  495. static void clear_ciphers(SSL *s)
  496. {
  497. /* clear the current cipher */
  498. ssl_clear_cipher_ctx(s);
  499. ssl_clear_hash_ctx(&s->read_hash);
  500. ssl_clear_hash_ctx(&s->write_hash);
  501. }
  502. int SSL_clear(SSL *s)
  503. {
  504. if (s->method == NULL) {
  505. ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED);
  506. return 0;
  507. }
  508. if (ssl_clear_bad_session(s)) {
  509. SSL_SESSION_free(s->session);
  510. s->session = NULL;
  511. }
  512. SSL_SESSION_free(s->psksession);
  513. s->psksession = NULL;
  514. OPENSSL_free(s->psksession_id);
  515. s->psksession_id = NULL;
  516. s->psksession_id_len = 0;
  517. s->hello_retry_request = 0;
  518. s->sent_tickets = 0;
  519. s->error = 0;
  520. s->hit = 0;
  521. s->shutdown = 0;
  522. if (s->renegotiate) {
  523. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  524. return 0;
  525. }
  526. ossl_statem_clear(s);
  527. s->version = s->method->version;
  528. s->client_version = s->version;
  529. s->rwstate = SSL_NOTHING;
  530. BUF_MEM_free(s->init_buf);
  531. s->init_buf = NULL;
  532. clear_ciphers(s);
  533. s->first_packet = 0;
  534. s->key_update = SSL_KEY_UPDATE_NONE;
  535. EVP_MD_CTX_free(s->pha_dgst);
  536. s->pha_dgst = NULL;
  537. /* Reset DANE verification result state */
  538. s->dane.mdpth = -1;
  539. s->dane.pdpth = -1;
  540. X509_free(s->dane.mcert);
  541. s->dane.mcert = NULL;
  542. s->dane.mtlsa = NULL;
  543. /* Clear the verification result peername */
  544. X509_VERIFY_PARAM_move_peername(s->param, NULL);
  545. /* Clear any shared connection state */
  546. OPENSSL_free(s->shared_sigalgs);
  547. s->shared_sigalgs = NULL;
  548. s->shared_sigalgslen = 0;
  549. /*
  550. * Check to see if we were changed into a different method, if so, revert
  551. * back.
  552. */
  553. if (s->method != s->ctx->method) {
  554. s->method->ssl_free(s);
  555. s->method = s->ctx->method;
  556. if (!s->method->ssl_new(s))
  557. return 0;
  558. } else {
  559. if (!s->method->ssl_clear(s))
  560. return 0;
  561. }
  562. RECORD_LAYER_clear(&s->rlayer);
  563. return 1;
  564. }
  565. #ifndef OPENSSL_NO_DEPRECATED_3_0
  566. /** Used to change an SSL_CTXs default SSL method type */
  567. int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
  568. {
  569. STACK_OF(SSL_CIPHER) *sk;
  570. ctx->method = meth;
  571. if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
  572. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  573. return 0;
  574. }
  575. sk = ssl_create_cipher_list(ctx,
  576. ctx->tls13_ciphersuites,
  577. &(ctx->cipher_list),
  578. &(ctx->cipher_list_by_id),
  579. OSSL_default_cipher_list(), ctx->cert);
  580. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
  581. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  582. return 0;
  583. }
  584. return 1;
  585. }
  586. #endif
  587. SSL *SSL_new(SSL_CTX *ctx)
  588. {
  589. SSL *s;
  590. if (ctx == NULL) {
  591. ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
  592. return NULL;
  593. }
  594. if (ctx->method == NULL) {
  595. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  596. return NULL;
  597. }
  598. s = OPENSSL_zalloc(sizeof(*s));
  599. if (s == NULL)
  600. goto err;
  601. s->references = 1;
  602. s->lock = CRYPTO_THREAD_lock_new();
  603. if (s->lock == NULL) {
  604. OPENSSL_free(s);
  605. s = NULL;
  606. goto err;
  607. }
  608. RECORD_LAYER_init(&s->rlayer, s);
  609. s->options = ctx->options;
  610. s->dane.flags = ctx->dane.flags;
  611. s->min_proto_version = ctx->min_proto_version;
  612. s->max_proto_version = ctx->max_proto_version;
  613. s->mode = ctx->mode;
  614. s->max_cert_list = ctx->max_cert_list;
  615. s->max_early_data = ctx->max_early_data;
  616. s->recv_max_early_data = ctx->recv_max_early_data;
  617. s->num_tickets = ctx->num_tickets;
  618. s->pha_enabled = ctx->pha_enabled;
  619. /* Shallow copy of the ciphersuites stack */
  620. s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
  621. if (s->tls13_ciphersuites == NULL)
  622. goto err;
  623. /*
  624. * Earlier library versions used to copy the pointer to the CERT, not
  625. * its contents; only when setting new parameters for the per-SSL
  626. * copy, ssl_cert_new would be called (and the direct reference to
  627. * the per-SSL_CTX settings would be lost, but those still were
  628. * indirectly accessed for various purposes, and for that reason they
  629. * used to be known as s->ctx->default_cert). Now we don't look at the
  630. * SSL_CTX's CERT after having duplicated it once.
  631. */
  632. s->cert = ssl_cert_dup(ctx->cert);
  633. if (s->cert == NULL)
  634. goto err;
  635. RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
  636. s->msg_callback = ctx->msg_callback;
  637. s->msg_callback_arg = ctx->msg_callback_arg;
  638. s->verify_mode = ctx->verify_mode;
  639. s->not_resumable_session_cb = ctx->not_resumable_session_cb;
  640. s->record_padding_cb = ctx->record_padding_cb;
  641. s->record_padding_arg = ctx->record_padding_arg;
  642. s->block_padding = ctx->block_padding;
  643. s->sid_ctx_length = ctx->sid_ctx_length;
  644. if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
  645. goto err;
  646. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  647. s->verify_callback = ctx->default_verify_callback;
  648. s->generate_session_id = ctx->generate_session_id;
  649. s->param = X509_VERIFY_PARAM_new();
  650. if (s->param == NULL)
  651. goto err;
  652. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  653. s->quiet_shutdown = ctx->quiet_shutdown;
  654. s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
  655. s->max_send_fragment = ctx->max_send_fragment;
  656. s->split_send_fragment = ctx->split_send_fragment;
  657. s->max_pipelines = ctx->max_pipelines;
  658. if (s->max_pipelines > 1)
  659. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  660. if (ctx->default_read_buf_len > 0)
  661. SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
  662. SSL_CTX_up_ref(ctx);
  663. s->ctx = ctx;
  664. s->ext.debug_cb = 0;
  665. s->ext.debug_arg = NULL;
  666. s->ext.ticket_expected = 0;
  667. s->ext.status_type = ctx->ext.status_type;
  668. s->ext.status_expected = 0;
  669. s->ext.ocsp.ids = NULL;
  670. s->ext.ocsp.exts = NULL;
  671. s->ext.ocsp.resp = NULL;
  672. s->ext.ocsp.resp_len = 0;
  673. SSL_CTX_up_ref(ctx);
  674. s->session_ctx = ctx;
  675. if (ctx->ext.ecpointformats) {
  676. s->ext.ecpointformats =
  677. OPENSSL_memdup(ctx->ext.ecpointformats,
  678. ctx->ext.ecpointformats_len);
  679. if (!s->ext.ecpointformats) {
  680. s->ext.ecpointformats_len = 0;
  681. goto err;
  682. }
  683. s->ext.ecpointformats_len =
  684. ctx->ext.ecpointformats_len;
  685. }
  686. if (ctx->ext.supportedgroups) {
  687. s->ext.supportedgroups =
  688. OPENSSL_memdup(ctx->ext.supportedgroups,
  689. ctx->ext.supportedgroups_len
  690. * sizeof(*ctx->ext.supportedgroups));
  691. if (!s->ext.supportedgroups) {
  692. s->ext.supportedgroups_len = 0;
  693. goto err;
  694. }
  695. s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
  696. }
  697. #ifndef OPENSSL_NO_NEXTPROTONEG
  698. s->ext.npn = NULL;
  699. #endif
  700. if (s->ctx->ext.alpn) {
  701. s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);
  702. if (s->ext.alpn == NULL) {
  703. s->ext.alpn_len = 0;
  704. goto err;
  705. }
  706. memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);
  707. s->ext.alpn_len = s->ctx->ext.alpn_len;
  708. }
  709. s->verified_chain = NULL;
  710. s->verify_result = X509_V_OK;
  711. s->default_passwd_callback = ctx->default_passwd_callback;
  712. s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
  713. s->method = ctx->method;
  714. s->key_update = SSL_KEY_UPDATE_NONE;
  715. s->allow_early_data_cb = ctx->allow_early_data_cb;
  716. s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
  717. if (!s->method->ssl_new(s))
  718. goto err;
  719. s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
  720. if (!SSL_clear(s))
  721. goto err;
  722. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
  723. goto err;
  724. #ifndef OPENSSL_NO_PSK
  725. s->psk_client_callback = ctx->psk_client_callback;
  726. s->psk_server_callback = ctx->psk_server_callback;
  727. #endif
  728. s->psk_find_session_cb = ctx->psk_find_session_cb;
  729. s->psk_use_session_cb = ctx->psk_use_session_cb;
  730. s->async_cb = ctx->async_cb;
  731. s->async_cb_arg = ctx->async_cb_arg;
  732. s->job = NULL;
  733. #ifndef OPENSSL_NO_CT
  734. if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
  735. ctx->ct_validation_callback_arg))
  736. goto err;
  737. #endif
  738. return s;
  739. err:
  740. SSL_free(s);
  741. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  742. return NULL;
  743. }
  744. int SSL_is_dtls(const SSL *s)
  745. {
  746. return SSL_IS_DTLS(s) ? 1 : 0;
  747. }
  748. int SSL_up_ref(SSL *s)
  749. {
  750. int i;
  751. if (CRYPTO_UP_REF(&s->references, &i, s->lock) <= 0)
  752. return 0;
  753. REF_PRINT_COUNT("SSL", s);
  754. REF_ASSERT_ISNT(i < 2);
  755. return ((i > 1) ? 1 : 0);
  756. }
  757. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
  758. unsigned int sid_ctx_len)
  759. {
  760. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  761. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  762. return 0;
  763. }
  764. ctx->sid_ctx_length = sid_ctx_len;
  765. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  766. return 1;
  767. }
  768. int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
  769. unsigned int sid_ctx_len)
  770. {
  771. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  772. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  773. return 0;
  774. }
  775. ssl->sid_ctx_length = sid_ctx_len;
  776. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  777. return 1;
  778. }
  779. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
  780. {
  781. if (!CRYPTO_THREAD_write_lock(ctx->lock))
  782. return 0;
  783. ctx->generate_session_id = cb;
  784. CRYPTO_THREAD_unlock(ctx->lock);
  785. return 1;
  786. }
  787. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
  788. {
  789. if (!CRYPTO_THREAD_write_lock(ssl->lock))
  790. return 0;
  791. ssl->generate_session_id = cb;
  792. CRYPTO_THREAD_unlock(ssl->lock);
  793. return 1;
  794. }
  795. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  796. unsigned int id_len)
  797. {
  798. /*
  799. * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
  800. * we can "construct" a session to give us the desired check - i.e. to
  801. * find if there's a session in the hash table that would conflict with
  802. * any new session built out of this id/id_len and the ssl_version in use
  803. * by this SSL.
  804. */
  805. SSL_SESSION r, *p;
  806. if (id_len > sizeof(r.session_id))
  807. return 0;
  808. r.ssl_version = ssl->version;
  809. r.session_id_length = id_len;
  810. memcpy(r.session_id, id, id_len);
  811. if (!CRYPTO_THREAD_read_lock(ssl->session_ctx->lock))
  812. return 0;
  813. p = lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &r);
  814. CRYPTO_THREAD_unlock(ssl->session_ctx->lock);
  815. return (p != NULL);
  816. }
  817. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
  818. {
  819. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  820. }
  821. int SSL_set_purpose(SSL *s, int purpose)
  822. {
  823. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  824. }
  825. int SSL_CTX_set_trust(SSL_CTX *s, int trust)
  826. {
  827. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  828. }
  829. int SSL_set_trust(SSL *s, int trust)
  830. {
  831. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  832. }
  833. int SSL_set1_host(SSL *s, const char *hostname)
  834. {
  835. /* If a hostname is provided and parses as an IP address,
  836. * treat it as such. */
  837. if (hostname && X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname) == 1)
  838. return 1;
  839. return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
  840. }
  841. int SSL_add1_host(SSL *s, const char *hostname)
  842. {
  843. /* If a hostname is provided and parses as an IP address,
  844. * treat it as such. */
  845. if (hostname)
  846. {
  847. ASN1_OCTET_STRING *ip;
  848. char *old_ip;
  849. ip = a2i_IPADDRESS(hostname);
  850. if (ip) {
  851. /* We didn't want it; only to check if it *is* an IP address */
  852. ASN1_OCTET_STRING_free(ip);
  853. old_ip = X509_VERIFY_PARAM_get1_ip_asc(s->param);
  854. if (old_ip)
  855. {
  856. OPENSSL_free(old_ip);
  857. /* There can be only one IP address */
  858. return 0;
  859. }
  860. return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
  861. }
  862. }
  863. return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
  864. }
  865. void SSL_set_hostflags(SSL *s, unsigned int flags)
  866. {
  867. X509_VERIFY_PARAM_set_hostflags(s->param, flags);
  868. }
  869. const char *SSL_get0_peername(SSL *s)
  870. {
  871. return X509_VERIFY_PARAM_get0_peername(s->param);
  872. }
  873. int SSL_CTX_dane_enable(SSL_CTX *ctx)
  874. {
  875. return dane_ctx_enable(&ctx->dane);
  876. }
  877. unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
  878. {
  879. unsigned long orig = ctx->dane.flags;
  880. ctx->dane.flags |= flags;
  881. return orig;
  882. }
  883. unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
  884. {
  885. unsigned long orig = ctx->dane.flags;
  886. ctx->dane.flags &= ~flags;
  887. return orig;
  888. }
  889. int SSL_dane_enable(SSL *s, const char *basedomain)
  890. {
  891. SSL_DANE *dane = &s->dane;
  892. if (s->ctx->dane.mdmax == 0) {
  893. ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED);
  894. return 0;
  895. }
  896. if (dane->trecs != NULL) {
  897. ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED);
  898. return 0;
  899. }
  900. /*
  901. * Default SNI name. This rejects empty names, while set1_host below
  902. * accepts them and disables host name checks. To avoid side-effects with
  903. * invalid input, set the SNI name first.
  904. */
  905. if (s->ext.hostname == NULL) {
  906. if (!SSL_set_tlsext_host_name(s, basedomain)) {
  907. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  908. return -1;
  909. }
  910. }
  911. /* Primary RFC6125 reference identifier */
  912. if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {
  913. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  914. return -1;
  915. }
  916. dane->mdpth = -1;
  917. dane->pdpth = -1;
  918. dane->dctx = &s->ctx->dane;
  919. dane->trecs = sk_danetls_record_new_null();
  920. if (dane->trecs == NULL) {
  921. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  922. return -1;
  923. }
  924. return 1;
  925. }
  926. unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
  927. {
  928. unsigned long orig = ssl->dane.flags;
  929. ssl->dane.flags |= flags;
  930. return orig;
  931. }
  932. unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
  933. {
  934. unsigned long orig = ssl->dane.flags;
  935. ssl->dane.flags &= ~flags;
  936. return orig;
  937. }
  938. int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
  939. {
  940. SSL_DANE *dane = &s->dane;
  941. if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
  942. return -1;
  943. if (dane->mtlsa) {
  944. if (mcert)
  945. *mcert = dane->mcert;
  946. if (mspki)
  947. *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
  948. }
  949. return dane->mdpth;
  950. }
  951. int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
  952. uint8_t *mtype, const unsigned char **data, size_t *dlen)
  953. {
  954. SSL_DANE *dane = &s->dane;
  955. if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
  956. return -1;
  957. if (dane->mtlsa) {
  958. if (usage)
  959. *usage = dane->mtlsa->usage;
  960. if (selector)
  961. *selector = dane->mtlsa->selector;
  962. if (mtype)
  963. *mtype = dane->mtlsa->mtype;
  964. if (data)
  965. *data = dane->mtlsa->data;
  966. if (dlen)
  967. *dlen = dane->mtlsa->dlen;
  968. }
  969. return dane->mdpth;
  970. }
  971. SSL_DANE *SSL_get0_dane(SSL *s)
  972. {
  973. return &s->dane;
  974. }
  975. int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
  976. uint8_t mtype, const unsigned char *data, size_t dlen)
  977. {
  978. return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen);
  979. }
  980. int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
  981. uint8_t ord)
  982. {
  983. return dane_mtype_set(&ctx->dane, md, mtype, ord);
  984. }
  985. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
  986. {
  987. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  988. }
  989. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
  990. {
  991. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  992. }
  993. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
  994. {
  995. return ctx->param;
  996. }
  997. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
  998. {
  999. return ssl->param;
  1000. }
  1001. void SSL_certs_clear(SSL *s)
  1002. {
  1003. ssl_cert_clear_certs(s->cert);
  1004. }
  1005. void SSL_free(SSL *s)
  1006. {
  1007. int i;
  1008. if (s == NULL)
  1009. return;
  1010. CRYPTO_DOWN_REF(&s->references, &i, s->lock);
  1011. REF_PRINT_COUNT("SSL", s);
  1012. if (i > 0)
  1013. return;
  1014. REF_ASSERT_ISNT(i < 0);
  1015. X509_VERIFY_PARAM_free(s->param);
  1016. dane_final(&s->dane);
  1017. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  1018. RECORD_LAYER_release(&s->rlayer);
  1019. /* Ignore return value */
  1020. ssl_free_wbio_buffer(s);
  1021. BIO_free_all(s->wbio);
  1022. s->wbio = NULL;
  1023. BIO_free_all(s->rbio);
  1024. s->rbio = NULL;
  1025. BUF_MEM_free(s->init_buf);
  1026. /* add extra stuff */
  1027. sk_SSL_CIPHER_free(s->cipher_list);
  1028. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1029. sk_SSL_CIPHER_free(s->tls13_ciphersuites);
  1030. sk_SSL_CIPHER_free(s->peer_ciphers);
  1031. /* Make the next call work :-) */
  1032. if (s->session != NULL) {
  1033. ssl_clear_bad_session(s);
  1034. SSL_SESSION_free(s->session);
  1035. }
  1036. SSL_SESSION_free(s->psksession);
  1037. OPENSSL_free(s->psksession_id);
  1038. clear_ciphers(s);
  1039. ssl_cert_free(s->cert);
  1040. OPENSSL_free(s->shared_sigalgs);
  1041. /* Free up if allocated */
  1042. OPENSSL_free(s->ext.hostname);
  1043. SSL_CTX_free(s->session_ctx);
  1044. OPENSSL_free(s->ext.ecpointformats);
  1045. OPENSSL_free(s->ext.peer_ecpointformats);
  1046. OPENSSL_free(s->ext.supportedgroups);
  1047. OPENSSL_free(s->ext.peer_supportedgroups);
  1048. sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
  1049. #ifndef OPENSSL_NO_OCSP
  1050. sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
  1051. #endif
  1052. #ifndef OPENSSL_NO_CT
  1053. SCT_LIST_free(s->scts);
  1054. OPENSSL_free(s->ext.scts);
  1055. #endif
  1056. OPENSSL_free(s->ext.ocsp.resp);
  1057. OPENSSL_free(s->ext.alpn);
  1058. OPENSSL_free(s->ext.tls13_cookie);
  1059. if (s->clienthello != NULL)
  1060. OPENSSL_free(s->clienthello->pre_proc_exts);
  1061. OPENSSL_free(s->clienthello);
  1062. OPENSSL_free(s->pha_context);
  1063. EVP_MD_CTX_free(s->pha_dgst);
  1064. sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
  1065. sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
  1066. OSSL_STACK_OF_X509_free(s->verified_chain);
  1067. if (s->method != NULL)
  1068. s->method->ssl_free(s);
  1069. SSL_CTX_free(s->ctx);
  1070. ASYNC_WAIT_CTX_free(s->waitctx);
  1071. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1072. OPENSSL_free(s->ext.npn);
  1073. #endif
  1074. #ifndef OPENSSL_NO_SRTP
  1075. sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
  1076. #endif
  1077. CRYPTO_THREAD_lock_free(s->lock);
  1078. OPENSSL_free(s);
  1079. }
  1080. void SSL_set0_rbio(SSL *s, BIO *rbio)
  1081. {
  1082. BIO_free_all(s->rbio);
  1083. s->rbio = rbio;
  1084. }
  1085. void SSL_set0_wbio(SSL *s, BIO *wbio)
  1086. {
  1087. /*
  1088. * If the output buffering BIO is still in place, remove it
  1089. */
  1090. if (s->bbio != NULL)
  1091. s->wbio = BIO_pop(s->wbio);
  1092. BIO_free_all(s->wbio);
  1093. s->wbio = wbio;
  1094. /* Re-attach |bbio| to the new |wbio|. */
  1095. if (s->bbio != NULL)
  1096. s->wbio = BIO_push(s->bbio, s->wbio);
  1097. }
  1098. void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
  1099. {
  1100. /*
  1101. * For historical reasons, this function has many different cases in
  1102. * ownership handling.
  1103. */
  1104. /* If nothing has changed, do nothing */
  1105. if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
  1106. return;
  1107. /*
  1108. * If the two arguments are equal then one fewer reference is granted by the
  1109. * caller than we want to take
  1110. */
  1111. if (rbio != NULL && rbio == wbio)
  1112. BIO_up_ref(rbio);
  1113. /*
  1114. * If only the wbio is changed only adopt one reference.
  1115. */
  1116. if (rbio == SSL_get_rbio(s)) {
  1117. SSL_set0_wbio(s, wbio);
  1118. return;
  1119. }
  1120. /*
  1121. * There is an asymmetry here for historical reasons. If only the rbio is
  1122. * changed AND the rbio and wbio were originally different, then we only
  1123. * adopt one reference.
  1124. */
  1125. if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
  1126. SSL_set0_rbio(s, rbio);
  1127. return;
  1128. }
  1129. /* Otherwise, adopt both references. */
  1130. SSL_set0_rbio(s, rbio);
  1131. SSL_set0_wbio(s, wbio);
  1132. }
  1133. BIO *SSL_get_rbio(const SSL *s)
  1134. {
  1135. return s->rbio;
  1136. }
  1137. BIO *SSL_get_wbio(const SSL *s)
  1138. {
  1139. if (s->bbio != NULL) {
  1140. /*
  1141. * If |bbio| is active, the true caller-configured BIO is its
  1142. * |next_bio|.
  1143. */
  1144. return BIO_next(s->bbio);
  1145. }
  1146. return s->wbio;
  1147. }
  1148. int SSL_get_fd(const SSL *s)
  1149. {
  1150. return SSL_get_rfd(s);
  1151. }
  1152. int SSL_get_rfd(const SSL *s)
  1153. {
  1154. int ret = -1;
  1155. BIO *b, *r;
  1156. b = SSL_get_rbio(s);
  1157. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  1158. if (r != NULL)
  1159. BIO_get_fd(r, &ret);
  1160. return ret;
  1161. }
  1162. int SSL_get_wfd(const SSL *s)
  1163. {
  1164. int ret = -1;
  1165. BIO *b, *r;
  1166. b = SSL_get_wbio(s);
  1167. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  1168. if (r != NULL)
  1169. BIO_get_fd(r, &ret);
  1170. return ret;
  1171. }
  1172. #ifndef OPENSSL_NO_SOCK
  1173. int SSL_set_fd(SSL *s, int fd)
  1174. {
  1175. int ret = 0;
  1176. BIO *bio = NULL;
  1177. bio = BIO_new(BIO_s_socket());
  1178. if (bio == NULL) {
  1179. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  1180. goto err;
  1181. }
  1182. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1183. SSL_set_bio(s, bio, bio);
  1184. #ifndef OPENSSL_NO_KTLS
  1185. /*
  1186. * The new socket is created successfully regardless of ktls_enable.
  1187. * ktls_enable doesn't change any functionality of the socket, except
  1188. * changing the setsockopt to enable the processing of ktls_start.
  1189. * Thus, it is not a problem to call it for non-TLS sockets.
  1190. */
  1191. ktls_enable(fd);
  1192. #endif /* OPENSSL_NO_KTLS */
  1193. ret = 1;
  1194. err:
  1195. return ret;
  1196. }
  1197. int SSL_set_wfd(SSL *s, int fd)
  1198. {
  1199. BIO *rbio = SSL_get_rbio(s);
  1200. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET
  1201. || (int)BIO_get_fd(rbio, NULL) != fd) {
  1202. BIO *bio = BIO_new(BIO_s_socket());
  1203. if (bio == NULL) {
  1204. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  1205. return 0;
  1206. }
  1207. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1208. SSL_set0_wbio(s, bio);
  1209. #ifndef OPENSSL_NO_KTLS
  1210. /*
  1211. * The new socket is created successfully regardless of ktls_enable.
  1212. * ktls_enable doesn't change any functionality of the socket, except
  1213. * changing the setsockopt to enable the processing of ktls_start.
  1214. * Thus, it is not a problem to call it for non-TLS sockets.
  1215. */
  1216. ktls_enable(fd);
  1217. #endif /* OPENSSL_NO_KTLS */
  1218. } else {
  1219. BIO_up_ref(rbio);
  1220. SSL_set0_wbio(s, rbio);
  1221. }
  1222. return 1;
  1223. }
  1224. int SSL_set_rfd(SSL *s, int fd)
  1225. {
  1226. BIO *wbio = SSL_get_wbio(s);
  1227. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET
  1228. || ((int)BIO_get_fd(wbio, NULL) != fd)) {
  1229. BIO *bio = BIO_new(BIO_s_socket());
  1230. if (bio == NULL) {
  1231. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  1232. return 0;
  1233. }
  1234. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1235. SSL_set0_rbio(s, bio);
  1236. } else {
  1237. BIO_up_ref(wbio);
  1238. SSL_set0_rbio(s, wbio);
  1239. }
  1240. return 1;
  1241. }
  1242. #endif
  1243. /* return length of latest Finished message we sent, copy to 'buf' */
  1244. size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
  1245. {
  1246. size_t ret = 0;
  1247. ret = s->s3.tmp.finish_md_len;
  1248. if (count > ret)
  1249. count = ret;
  1250. memcpy(buf, s->s3.tmp.finish_md, count);
  1251. return ret;
  1252. }
  1253. /* return length of latest Finished message we expected, copy to 'buf' */
  1254. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
  1255. {
  1256. size_t ret = 0;
  1257. ret = s->s3.tmp.peer_finish_md_len;
  1258. if (count > ret)
  1259. count = ret;
  1260. memcpy(buf, s->s3.tmp.peer_finish_md, count);
  1261. return ret;
  1262. }
  1263. int SSL_get_verify_mode(const SSL *s)
  1264. {
  1265. return s->verify_mode;
  1266. }
  1267. int SSL_get_verify_depth(const SSL *s)
  1268. {
  1269. return X509_VERIFY_PARAM_get_depth(s->param);
  1270. }
  1271. int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
  1272. return s->verify_callback;
  1273. }
  1274. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  1275. {
  1276. return ctx->verify_mode;
  1277. }
  1278. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  1279. {
  1280. return X509_VERIFY_PARAM_get_depth(ctx->param);
  1281. }
  1282. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
  1283. return ctx->default_verify_callback;
  1284. }
  1285. void SSL_set_verify(SSL *s, int mode,
  1286. int (*callback) (int ok, X509_STORE_CTX *ctx))
  1287. {
  1288. s->verify_mode = mode;
  1289. if (callback != NULL)
  1290. s->verify_callback = callback;
  1291. }
  1292. void SSL_set_verify_depth(SSL *s, int depth)
  1293. {
  1294. X509_VERIFY_PARAM_set_depth(s->param, depth);
  1295. }
  1296. void SSL_set_read_ahead(SSL *s, int yes)
  1297. {
  1298. RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
  1299. }
  1300. int SSL_get_read_ahead(const SSL *s)
  1301. {
  1302. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1303. }
  1304. int SSL_pending(const SSL *s)
  1305. {
  1306. size_t pending = s->method->ssl_pending(s);
  1307. /*
  1308. * SSL_pending cannot work properly if read-ahead is enabled
  1309. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
  1310. * impossible to fix since SSL_pending cannot report errors that may be
  1311. * observed while scanning the new data. (Note that SSL_pending() is
  1312. * often used as a boolean value, so we'd better not return -1.)
  1313. *
  1314. * SSL_pending also cannot work properly if the value >INT_MAX. In that case
  1315. * we just return INT_MAX.
  1316. */
  1317. return pending < INT_MAX ? (int)pending : INT_MAX;
  1318. }
  1319. int SSL_has_pending(const SSL *s)
  1320. {
  1321. /*
  1322. * Similar to SSL_pending() but returns a 1 to indicate that we have
  1323. * unprocessed data available or 0 otherwise (as opposed to the number of
  1324. * bytes available). Unlike SSL_pending() this will take into account
  1325. * read_ahead data. A 1 return simply indicates that we have unprocessed
  1326. * data. That data may not result in any application data, or we may fail
  1327. * to parse the records for some reason.
  1328. */
  1329. if (RECORD_LAYER_processed_read_pending(&s->rlayer))
  1330. return 1;
  1331. return RECORD_LAYER_read_pending(&s->rlayer);
  1332. }
  1333. X509 *SSL_get1_peer_certificate(const SSL *s)
  1334. {
  1335. X509 *r = SSL_get0_peer_certificate(s);
  1336. if (r != NULL)
  1337. X509_up_ref(r);
  1338. return r;
  1339. }
  1340. X509 *SSL_get0_peer_certificate(const SSL *s)
  1341. {
  1342. if ((s == NULL) || (s->session == NULL))
  1343. return NULL;
  1344. else
  1345. return s->session->peer;
  1346. }
  1347. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  1348. {
  1349. STACK_OF(X509) *r;
  1350. if ((s == NULL) || (s->session == NULL))
  1351. r = NULL;
  1352. else
  1353. r = s->session->peer_chain;
  1354. /*
  1355. * If we are a client, cert_chain includes the peer's own certificate; if
  1356. * we are a server, it does not.
  1357. */
  1358. return r;
  1359. }
  1360. /*
  1361. * Now in theory, since the calling process own 't' it should be safe to
  1362. * modify. We need to be able to read f without being hassled
  1363. */
  1364. int SSL_copy_session_id(SSL *t, const SSL *f)
  1365. {
  1366. int i;
  1367. /* Do we need to do SSL locking? */
  1368. if (!SSL_set_session(t, SSL_get_session(f))) {
  1369. return 0;
  1370. }
  1371. /*
  1372. * what if we are setup for one protocol version but want to talk another
  1373. */
  1374. if (t->method != f->method) {
  1375. t->method->ssl_free(t);
  1376. t->method = f->method;
  1377. if (t->method->ssl_new(t) == 0)
  1378. return 0;
  1379. }
  1380. CRYPTO_UP_REF(&f->cert->references, &i, f->cert->lock);
  1381. ssl_cert_free(t->cert);
  1382. t->cert = f->cert;
  1383. if (!SSL_set_session_id_context(t, f->sid_ctx, (int)f->sid_ctx_length)) {
  1384. return 0;
  1385. }
  1386. return 1;
  1387. }
  1388. /* Fix this so it checks all the valid key/cert options */
  1389. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  1390. {
  1391. if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
  1392. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1393. return 0;
  1394. }
  1395. if (ctx->cert->key->privatekey == NULL) {
  1396. ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1397. return 0;
  1398. }
  1399. return X509_check_private_key
  1400. (ctx->cert->key->x509, ctx->cert->key->privatekey);
  1401. }
  1402. /* Fix this function so that it takes an optional type parameter */
  1403. int SSL_check_private_key(const SSL *ssl)
  1404. {
  1405. if (ssl == NULL) {
  1406. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  1407. return 0;
  1408. }
  1409. if (ssl->cert->key->x509 == NULL) {
  1410. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1411. return 0;
  1412. }
  1413. if (ssl->cert->key->privatekey == NULL) {
  1414. ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1415. return 0;
  1416. }
  1417. return X509_check_private_key(ssl->cert->key->x509,
  1418. ssl->cert->key->privatekey);
  1419. }
  1420. int SSL_waiting_for_async(SSL *s)
  1421. {
  1422. if (s->job)
  1423. return 1;
  1424. return 0;
  1425. }
  1426. int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
  1427. {
  1428. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1429. if (ctx == NULL)
  1430. return 0;
  1431. return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
  1432. }
  1433. int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
  1434. OSSL_ASYNC_FD *delfd, size_t *numdelfds)
  1435. {
  1436. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1437. if (ctx == NULL)
  1438. return 0;
  1439. return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
  1440. numdelfds);
  1441. }
  1442. int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
  1443. {
  1444. ctx->async_cb = callback;
  1445. return 1;
  1446. }
  1447. int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
  1448. {
  1449. ctx->async_cb_arg = arg;
  1450. return 1;
  1451. }
  1452. int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
  1453. {
  1454. s->async_cb = callback;
  1455. return 1;
  1456. }
  1457. int SSL_set_async_callback_arg(SSL *s, void *arg)
  1458. {
  1459. s->async_cb_arg = arg;
  1460. return 1;
  1461. }
  1462. int SSL_get_async_status(SSL *s, int *status)
  1463. {
  1464. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1465. if (ctx == NULL)
  1466. return 0;
  1467. *status = ASYNC_WAIT_CTX_get_status(ctx);
  1468. return 1;
  1469. }
  1470. int SSL_accept(SSL *s)
  1471. {
  1472. if (s->handshake_func == NULL) {
  1473. /* Not properly initialized yet */
  1474. SSL_set_accept_state(s);
  1475. }
  1476. return SSL_do_handshake(s);
  1477. }
  1478. int SSL_connect(SSL *s)
  1479. {
  1480. if (s->handshake_func == NULL) {
  1481. /* Not properly initialized yet */
  1482. SSL_set_connect_state(s);
  1483. }
  1484. return SSL_do_handshake(s);
  1485. }
  1486. long SSL_get_default_timeout(const SSL *s)
  1487. {
  1488. return s->method->get_timeout();
  1489. }
  1490. static int ssl_async_wait_ctx_cb(void *arg)
  1491. {
  1492. SSL *s = (SSL *)arg;
  1493. return s->async_cb(s, s->async_cb_arg);
  1494. }
  1495. static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
  1496. int (*func) (void *))
  1497. {
  1498. int ret;
  1499. if (s->waitctx == NULL) {
  1500. s->waitctx = ASYNC_WAIT_CTX_new();
  1501. if (s->waitctx == NULL)
  1502. return -1;
  1503. if (s->async_cb != NULL
  1504. && !ASYNC_WAIT_CTX_set_callback
  1505. (s->waitctx, ssl_async_wait_ctx_cb, s))
  1506. return -1;
  1507. }
  1508. s->rwstate = SSL_NOTHING;
  1509. switch (ASYNC_start_job(&s->job, s->waitctx, &ret, func, args,
  1510. sizeof(struct ssl_async_args))) {
  1511. case ASYNC_ERR:
  1512. s->rwstate = SSL_NOTHING;
  1513. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC);
  1514. return -1;
  1515. case ASYNC_PAUSE:
  1516. s->rwstate = SSL_ASYNC_PAUSED;
  1517. return -1;
  1518. case ASYNC_NO_JOBS:
  1519. s->rwstate = SSL_ASYNC_NO_JOBS;
  1520. return -1;
  1521. case ASYNC_FINISH:
  1522. s->job = NULL;
  1523. return ret;
  1524. default:
  1525. s->rwstate = SSL_NOTHING;
  1526. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  1527. /* Shouldn't happen */
  1528. return -1;
  1529. }
  1530. }
  1531. static int ssl_io_intern(void *vargs)
  1532. {
  1533. struct ssl_async_args *args;
  1534. SSL *s;
  1535. void *buf;
  1536. size_t num;
  1537. args = (struct ssl_async_args *)vargs;
  1538. s = args->s;
  1539. buf = args->buf;
  1540. num = args->num;
  1541. switch (args->type) {
  1542. case READFUNC:
  1543. return args->f.func_read(s, buf, num, &s->asyncrw);
  1544. case WRITEFUNC:
  1545. return args->f.func_write(s, buf, num, &s->asyncrw);
  1546. case OTHERFUNC:
  1547. return args->f.func_other(s);
  1548. }
  1549. return -1;
  1550. }
  1551. int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1552. {
  1553. if (s->handshake_func == NULL) {
  1554. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1555. return -1;
  1556. }
  1557. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1558. s->rwstate = SSL_NOTHING;
  1559. return 0;
  1560. }
  1561. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1562. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
  1563. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1564. return 0;
  1565. }
  1566. /*
  1567. * If we are a client and haven't received the ServerHello etc then we
  1568. * better do that
  1569. */
  1570. ossl_statem_check_finish_init(s, 0);
  1571. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1572. struct ssl_async_args args;
  1573. int ret;
  1574. args.s = s;
  1575. args.buf = buf;
  1576. args.num = num;
  1577. args.type = READFUNC;
  1578. args.f.func_read = s->method->ssl_read;
  1579. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1580. *readbytes = s->asyncrw;
  1581. return ret;
  1582. } else {
  1583. return s->method->ssl_read(s, buf, num, readbytes);
  1584. }
  1585. }
  1586. int SSL_read(SSL *s, void *buf, int num)
  1587. {
  1588. int ret;
  1589. size_t readbytes;
  1590. if (num < 0) {
  1591. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  1592. return -1;
  1593. }
  1594. ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
  1595. /*
  1596. * The cast is safe here because ret should be <= INT_MAX because num is
  1597. * <= INT_MAX
  1598. */
  1599. if (ret > 0)
  1600. ret = (int)readbytes;
  1601. return ret;
  1602. }
  1603. int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1604. {
  1605. int ret = ssl_read_internal(s, buf, num, readbytes);
  1606. if (ret < 0)
  1607. ret = 0;
  1608. return ret;
  1609. }
  1610. int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
  1611. {
  1612. int ret;
  1613. if (!s->server) {
  1614. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1615. return SSL_READ_EARLY_DATA_ERROR;
  1616. }
  1617. switch (s->early_data_state) {
  1618. case SSL_EARLY_DATA_NONE:
  1619. if (!SSL_in_before(s)) {
  1620. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1621. return SSL_READ_EARLY_DATA_ERROR;
  1622. }
  1623. /* fall through */
  1624. case SSL_EARLY_DATA_ACCEPT_RETRY:
  1625. s->early_data_state = SSL_EARLY_DATA_ACCEPTING;
  1626. ret = SSL_accept(s);
  1627. if (ret <= 0) {
  1628. /* NBIO or error */
  1629. s->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
  1630. return SSL_READ_EARLY_DATA_ERROR;
  1631. }
  1632. /* fall through */
  1633. case SSL_EARLY_DATA_READ_RETRY:
  1634. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  1635. s->early_data_state = SSL_EARLY_DATA_READING;
  1636. ret = SSL_read_ex(s, buf, num, readbytes);
  1637. /*
  1638. * State machine will update early_data_state to
  1639. * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
  1640. * message
  1641. */
  1642. if (ret > 0 || (ret <= 0 && s->early_data_state
  1643. != SSL_EARLY_DATA_FINISHED_READING)) {
  1644. s->early_data_state = SSL_EARLY_DATA_READ_RETRY;
  1645. return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
  1646. : SSL_READ_EARLY_DATA_ERROR;
  1647. }
  1648. } else {
  1649. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  1650. }
  1651. *readbytes = 0;
  1652. return SSL_READ_EARLY_DATA_FINISH;
  1653. default:
  1654. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1655. return SSL_READ_EARLY_DATA_ERROR;
  1656. }
  1657. }
  1658. int SSL_get_early_data_status(const SSL *s)
  1659. {
  1660. return s->ext.early_data;
  1661. }
  1662. static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1663. {
  1664. if (s->handshake_func == NULL) {
  1665. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1666. return -1;
  1667. }
  1668. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1669. return 0;
  1670. }
  1671. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1672. struct ssl_async_args args;
  1673. int ret;
  1674. args.s = s;
  1675. args.buf = buf;
  1676. args.num = num;
  1677. args.type = READFUNC;
  1678. args.f.func_read = s->method->ssl_peek;
  1679. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1680. *readbytes = s->asyncrw;
  1681. return ret;
  1682. } else {
  1683. return s->method->ssl_peek(s, buf, num, readbytes);
  1684. }
  1685. }
  1686. int SSL_peek(SSL *s, void *buf, int num)
  1687. {
  1688. int ret;
  1689. size_t readbytes;
  1690. if (num < 0) {
  1691. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  1692. return -1;
  1693. }
  1694. ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
  1695. /*
  1696. * The cast is safe here because ret should be <= INT_MAX because num is
  1697. * <= INT_MAX
  1698. */
  1699. if (ret > 0)
  1700. ret = (int)readbytes;
  1701. return ret;
  1702. }
  1703. int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1704. {
  1705. int ret = ssl_peek_internal(s, buf, num, readbytes);
  1706. if (ret < 0)
  1707. ret = 0;
  1708. return ret;
  1709. }
  1710. int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
  1711. {
  1712. if (s->handshake_func == NULL) {
  1713. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1714. return -1;
  1715. }
  1716. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1717. s->rwstate = SSL_NOTHING;
  1718. ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1719. return -1;
  1720. }
  1721. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1722. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
  1723. || s->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
  1724. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1725. return 0;
  1726. }
  1727. /* If we are a client and haven't sent the Finished we better do that */
  1728. ossl_statem_check_finish_init(s, 1);
  1729. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1730. int ret;
  1731. struct ssl_async_args args;
  1732. args.s = s;
  1733. args.buf = (void *)buf;
  1734. args.num = num;
  1735. args.type = WRITEFUNC;
  1736. args.f.func_write = s->method->ssl_write;
  1737. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1738. *written = s->asyncrw;
  1739. return ret;
  1740. } else {
  1741. return s->method->ssl_write(s, buf, num, written);
  1742. }
  1743. }
  1744. ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
  1745. {
  1746. ossl_ssize_t ret;
  1747. if (s->handshake_func == NULL) {
  1748. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1749. return -1;
  1750. }
  1751. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1752. s->rwstate = SSL_NOTHING;
  1753. ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1754. return -1;
  1755. }
  1756. if (!BIO_get_ktls_send(s->wbio)) {
  1757. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1758. return -1;
  1759. }
  1760. /* If we have an alert to send, lets send it */
  1761. if (s->s3.alert_dispatch) {
  1762. ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
  1763. if (ret <= 0) {
  1764. /* SSLfatal() already called if appropriate */
  1765. return ret;
  1766. }
  1767. /* if it went, fall through and send more stuff */
  1768. }
  1769. s->rwstate = SSL_WRITING;
  1770. if (BIO_flush(s->wbio) <= 0) {
  1771. if (!BIO_should_retry(s->wbio)) {
  1772. s->rwstate = SSL_NOTHING;
  1773. } else {
  1774. #ifdef EAGAIN
  1775. set_sys_error(EAGAIN);
  1776. #endif
  1777. }
  1778. return -1;
  1779. }
  1780. #ifdef OPENSSL_NO_KTLS
  1781. ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
  1782. "can't call ktls_sendfile(), ktls disabled");
  1783. return -1;
  1784. #else
  1785. ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
  1786. if (ret < 0) {
  1787. #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
  1788. if ((get_last_sys_error() == EAGAIN) ||
  1789. (get_last_sys_error() == EINTR) ||
  1790. (get_last_sys_error() == EBUSY))
  1791. BIO_set_retry_write(s->wbio);
  1792. else
  1793. #endif
  1794. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1795. return ret;
  1796. }
  1797. s->rwstate = SSL_NOTHING;
  1798. return ret;
  1799. #endif
  1800. }
  1801. int SSL_write(SSL *s, const void *buf, int num)
  1802. {
  1803. int ret;
  1804. size_t written;
  1805. if (num < 0) {
  1806. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  1807. return -1;
  1808. }
  1809. ret = ssl_write_internal(s, buf, (size_t)num, &written);
  1810. /*
  1811. * The cast is safe here because ret should be <= INT_MAX because num is
  1812. * <= INT_MAX
  1813. */
  1814. if (ret > 0)
  1815. ret = (int)written;
  1816. return ret;
  1817. }
  1818. int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
  1819. {
  1820. int ret = ssl_write_internal(s, buf, num, written);
  1821. if (ret < 0)
  1822. ret = 0;
  1823. return ret;
  1824. }
  1825. int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
  1826. {
  1827. int ret, early_data_state;
  1828. size_t writtmp;
  1829. uint32_t partialwrite;
  1830. switch (s->early_data_state) {
  1831. case SSL_EARLY_DATA_NONE:
  1832. if (s->server
  1833. || !SSL_in_before(s)
  1834. || ((s->session == NULL || s->session->ext.max_early_data == 0)
  1835. && (s->psk_use_session_cb == NULL))) {
  1836. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1837. return 0;
  1838. }
  1839. /* fall through */
  1840. case SSL_EARLY_DATA_CONNECT_RETRY:
  1841. s->early_data_state = SSL_EARLY_DATA_CONNECTING;
  1842. ret = SSL_connect(s);
  1843. if (ret <= 0) {
  1844. /* NBIO or error */
  1845. s->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
  1846. return 0;
  1847. }
  1848. /* fall through */
  1849. case SSL_EARLY_DATA_WRITE_RETRY:
  1850. s->early_data_state = SSL_EARLY_DATA_WRITING;
  1851. /*
  1852. * We disable partial write for early data because we don't keep track
  1853. * of how many bytes we've written between the SSL_write_ex() call and
  1854. * the flush if the flush needs to be retried)
  1855. */
  1856. partialwrite = s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
  1857. s->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
  1858. ret = SSL_write_ex(s, buf, num, &writtmp);
  1859. s->mode |= partialwrite;
  1860. if (!ret) {
  1861. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1862. return ret;
  1863. }
  1864. s->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
  1865. /* fall through */
  1866. case SSL_EARLY_DATA_WRITE_FLUSH:
  1867. /* The buffering BIO is still in place so we need to flush it */
  1868. if (statem_flush(s) != 1)
  1869. return 0;
  1870. *written = num;
  1871. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1872. return 1;
  1873. case SSL_EARLY_DATA_FINISHED_READING:
  1874. case SSL_EARLY_DATA_READ_RETRY:
  1875. early_data_state = s->early_data_state;
  1876. /* We are a server writing to an unauthenticated client */
  1877. s->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
  1878. ret = SSL_write_ex(s, buf, num, written);
  1879. /* The buffering BIO is still in place */
  1880. if (ret)
  1881. (void)BIO_flush(s->wbio);
  1882. s->early_data_state = early_data_state;
  1883. return ret;
  1884. default:
  1885. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1886. return 0;
  1887. }
  1888. }
  1889. int SSL_shutdown(SSL *s)
  1890. {
  1891. /*
  1892. * Note that this function behaves differently from what one might
  1893. * expect. Return values are 0 for no success (yet), 1 for success; but
  1894. * calling it once is usually not enough, even if blocking I/O is used
  1895. * (see ssl3_shutdown).
  1896. */
  1897. if (s->handshake_func == NULL) {
  1898. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  1899. return -1;
  1900. }
  1901. if (!SSL_in_init(s)) {
  1902. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1903. struct ssl_async_args args;
  1904. memset(&args, 0, sizeof(args));
  1905. args.s = s;
  1906. args.type = OTHERFUNC;
  1907. args.f.func_other = s->method->ssl_shutdown;
  1908. return ssl_start_async_job(s, &args, ssl_io_intern);
  1909. } else {
  1910. return s->method->ssl_shutdown(s);
  1911. }
  1912. } else {
  1913. ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  1914. return -1;
  1915. }
  1916. }
  1917. int SSL_key_update(SSL *s, int updatetype)
  1918. {
  1919. if (!SSL_IS_TLS13(s)) {
  1920. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  1921. return 0;
  1922. }
  1923. if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
  1924. && updatetype != SSL_KEY_UPDATE_REQUESTED) {
  1925. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
  1926. return 0;
  1927. }
  1928. if (!SSL_is_init_finished(s)) {
  1929. ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
  1930. return 0;
  1931. }
  1932. if (RECORD_LAYER_write_pending(&s->rlayer)) {
  1933. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY);
  1934. return 0;
  1935. }
  1936. ossl_statem_set_in_init(s, 1);
  1937. s->key_update = updatetype;
  1938. return 1;
  1939. }
  1940. int SSL_get_key_update_type(const SSL *s)
  1941. {
  1942. return s->key_update;
  1943. }
  1944. /*
  1945. * Can we accept a renegotiation request? If yes, set the flag and
  1946. * return 1 if yes. If not, raise error and return 0.
  1947. */
  1948. static int can_renegotiate(const SSL *s)
  1949. {
  1950. if (SSL_IS_TLS13(s)) {
  1951. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  1952. return 0;
  1953. }
  1954. if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0) {
  1955. ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
  1956. return 0;
  1957. }
  1958. return 1;
  1959. }
  1960. int SSL_renegotiate(SSL *s)
  1961. {
  1962. if (!can_renegotiate(s))
  1963. return 0;
  1964. s->renegotiate = 1;
  1965. s->new_session = 1;
  1966. return s->method->ssl_renegotiate(s);
  1967. }
  1968. int SSL_renegotiate_abbreviated(SSL *s)
  1969. {
  1970. if (!can_renegotiate(s))
  1971. return 0;
  1972. s->renegotiate = 1;
  1973. s->new_session = 0;
  1974. return s->method->ssl_renegotiate(s);
  1975. }
  1976. int SSL_renegotiate_pending(const SSL *s)
  1977. {
  1978. /*
  1979. * becomes true when negotiation is requested; false again once a
  1980. * handshake has finished
  1981. */
  1982. return (s->renegotiate != 0);
  1983. }
  1984. int SSL_new_session_ticket(SSL *s)
  1985. {
  1986. /* If we are in init because we're sending tickets, okay to send more. */
  1987. if ((SSL_in_init(s) && s->ext.extra_tickets_expected == 0)
  1988. || SSL_IS_FIRST_HANDSHAKE(s) || !s->server
  1989. || !SSL_IS_TLS13(s))
  1990. return 0;
  1991. s->ext.extra_tickets_expected++;
  1992. if (!RECORD_LAYER_write_pending(&s->rlayer) && !SSL_in_init(s))
  1993. ossl_statem_set_in_init(s, 1);
  1994. return 1;
  1995. }
  1996. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
  1997. {
  1998. long l;
  1999. switch (cmd) {
  2000. case SSL_CTRL_GET_READ_AHEAD:
  2001. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  2002. case SSL_CTRL_SET_READ_AHEAD:
  2003. l = RECORD_LAYER_get_read_ahead(&s->rlayer);
  2004. RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
  2005. return l;
  2006. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  2007. s->msg_callback_arg = parg;
  2008. return 1;
  2009. case SSL_CTRL_MODE:
  2010. return (s->mode |= larg);
  2011. case SSL_CTRL_CLEAR_MODE:
  2012. return (s->mode &= ~larg);
  2013. case SSL_CTRL_GET_MAX_CERT_LIST:
  2014. return (long)s->max_cert_list;
  2015. case SSL_CTRL_SET_MAX_CERT_LIST:
  2016. if (larg < 0)
  2017. return 0;
  2018. l = (long)s->max_cert_list;
  2019. s->max_cert_list = (size_t)larg;
  2020. return l;
  2021. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2022. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2023. return 0;
  2024. #ifndef OPENSSL_NO_KTLS
  2025. if (s->wbio != NULL && BIO_get_ktls_send(s->wbio))
  2026. return 0;
  2027. #endif /* OPENSSL_NO_KTLS */
  2028. s->max_send_fragment = larg;
  2029. if (s->max_send_fragment < s->split_send_fragment)
  2030. s->split_send_fragment = s->max_send_fragment;
  2031. return 1;
  2032. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2033. if ((size_t)larg > s->max_send_fragment || larg == 0)
  2034. return 0;
  2035. s->split_send_fragment = larg;
  2036. return 1;
  2037. case SSL_CTRL_SET_MAX_PIPELINES:
  2038. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2039. return 0;
  2040. s->max_pipelines = larg;
  2041. if (larg > 1)
  2042. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  2043. return 1;
  2044. case SSL_CTRL_GET_RI_SUPPORT:
  2045. return s->s3.send_connection_binding;
  2046. case SSL_CTRL_SET_RETRY_VERIFY:
  2047. s->rwstate = SSL_RETRY_VERIFY;
  2048. return 1;
  2049. case SSL_CTRL_CERT_FLAGS:
  2050. return (s->cert->cert_flags |= larg);
  2051. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2052. return (s->cert->cert_flags &= ~larg);
  2053. case SSL_CTRL_GET_RAW_CIPHERLIST:
  2054. if (parg) {
  2055. if (s->s3.tmp.ciphers_raw == NULL)
  2056. return 0;
  2057. *(unsigned char **)parg = s->s3.tmp.ciphers_raw;
  2058. return (int)s->s3.tmp.ciphers_rawlen;
  2059. } else {
  2060. return TLS_CIPHER_LEN;
  2061. }
  2062. case SSL_CTRL_GET_EXTMS_SUPPORT:
  2063. if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
  2064. return -1;
  2065. if (s->session->flags & SSL_SESS_FLAG_EXTMS)
  2066. return 1;
  2067. else
  2068. return 0;
  2069. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2070. return ssl_check_allowed_versions(larg, s->max_proto_version)
  2071. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  2072. &s->min_proto_version);
  2073. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2074. return s->min_proto_version;
  2075. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2076. return ssl_check_allowed_versions(s->min_proto_version, larg)
  2077. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  2078. &s->max_proto_version);
  2079. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2080. return s->max_proto_version;
  2081. default:
  2082. return s->method->ssl_ctrl(s, cmd, larg, parg);
  2083. }
  2084. }
  2085. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  2086. {
  2087. switch (cmd) {
  2088. case SSL_CTRL_SET_MSG_CALLBACK:
  2089. s->msg_callback = (void (*)
  2090. (int write_p, int version, int content_type,
  2091. const void *buf, size_t len, SSL *ssl,
  2092. void *arg))(fp);
  2093. return 1;
  2094. default:
  2095. return s->method->ssl_callback_ctrl(s, cmd, fp);
  2096. }
  2097. }
  2098. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  2099. {
  2100. return ctx->sessions;
  2101. }
  2102. static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat)
  2103. {
  2104. int res = 0;
  2105. if (ssl_tsan_lock(ctx)) {
  2106. res = tsan_load(stat);
  2107. ssl_tsan_unlock(ctx);
  2108. }
  2109. return res;
  2110. }
  2111. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  2112. {
  2113. long l;
  2114. /* For some cases with ctx == NULL perform syntax checks */
  2115. if (ctx == NULL) {
  2116. switch (cmd) {
  2117. case SSL_CTRL_SET_GROUPS_LIST:
  2118. return tls1_set_groups_list(ctx, NULL, NULL, parg);
  2119. case SSL_CTRL_SET_SIGALGS_LIST:
  2120. case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
  2121. return tls1_set_sigalgs_list(NULL, parg, 0);
  2122. default:
  2123. return 0;
  2124. }
  2125. }
  2126. switch (cmd) {
  2127. case SSL_CTRL_GET_READ_AHEAD:
  2128. return ctx->read_ahead;
  2129. case SSL_CTRL_SET_READ_AHEAD:
  2130. l = ctx->read_ahead;
  2131. ctx->read_ahead = larg;
  2132. return l;
  2133. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  2134. ctx->msg_callback_arg = parg;
  2135. return 1;
  2136. case SSL_CTRL_GET_MAX_CERT_LIST:
  2137. return (long)ctx->max_cert_list;
  2138. case SSL_CTRL_SET_MAX_CERT_LIST:
  2139. if (larg < 0)
  2140. return 0;
  2141. l = (long)ctx->max_cert_list;
  2142. ctx->max_cert_list = (size_t)larg;
  2143. return l;
  2144. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  2145. if (larg < 0)
  2146. return 0;
  2147. l = (long)ctx->session_cache_size;
  2148. ctx->session_cache_size = (size_t)larg;
  2149. return l;
  2150. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  2151. return (long)ctx->session_cache_size;
  2152. case SSL_CTRL_SET_SESS_CACHE_MODE:
  2153. l = ctx->session_cache_mode;
  2154. ctx->session_cache_mode = larg;
  2155. return l;
  2156. case SSL_CTRL_GET_SESS_CACHE_MODE:
  2157. return ctx->session_cache_mode;
  2158. case SSL_CTRL_SESS_NUMBER:
  2159. return lh_SSL_SESSION_num_items(ctx->sessions);
  2160. case SSL_CTRL_SESS_CONNECT:
  2161. return ssl_tsan_load(ctx, &ctx->stats.sess_connect);
  2162. case SSL_CTRL_SESS_CONNECT_GOOD:
  2163. return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good);
  2164. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  2165. return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate);
  2166. case SSL_CTRL_SESS_ACCEPT:
  2167. return ssl_tsan_load(ctx, &ctx->stats.sess_accept);
  2168. case SSL_CTRL_SESS_ACCEPT_GOOD:
  2169. return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good);
  2170. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  2171. return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate);
  2172. case SSL_CTRL_SESS_HIT:
  2173. return ssl_tsan_load(ctx, &ctx->stats.sess_hit);
  2174. case SSL_CTRL_SESS_CB_HIT:
  2175. return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit);
  2176. case SSL_CTRL_SESS_MISSES:
  2177. return ssl_tsan_load(ctx, &ctx->stats.sess_miss);
  2178. case SSL_CTRL_SESS_TIMEOUTS:
  2179. return ssl_tsan_load(ctx, &ctx->stats.sess_timeout);
  2180. case SSL_CTRL_SESS_CACHE_FULL:
  2181. return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full);
  2182. case SSL_CTRL_MODE:
  2183. return (ctx->mode |= larg);
  2184. case SSL_CTRL_CLEAR_MODE:
  2185. return (ctx->mode &= ~larg);
  2186. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2187. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2188. return 0;
  2189. ctx->max_send_fragment = larg;
  2190. if (ctx->max_send_fragment < ctx->split_send_fragment)
  2191. ctx->split_send_fragment = ctx->max_send_fragment;
  2192. return 1;
  2193. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2194. if ((size_t)larg > ctx->max_send_fragment || larg == 0)
  2195. return 0;
  2196. ctx->split_send_fragment = larg;
  2197. return 1;
  2198. case SSL_CTRL_SET_MAX_PIPELINES:
  2199. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2200. return 0;
  2201. ctx->max_pipelines = larg;
  2202. return 1;
  2203. case SSL_CTRL_CERT_FLAGS:
  2204. return (ctx->cert->cert_flags |= larg);
  2205. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2206. return (ctx->cert->cert_flags &= ~larg);
  2207. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2208. return ssl_check_allowed_versions(larg, ctx->max_proto_version)
  2209. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2210. &ctx->min_proto_version);
  2211. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2212. return ctx->min_proto_version;
  2213. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2214. return ssl_check_allowed_versions(ctx->min_proto_version, larg)
  2215. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2216. &ctx->max_proto_version);
  2217. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2218. return ctx->max_proto_version;
  2219. default:
  2220. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  2221. }
  2222. }
  2223. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  2224. {
  2225. switch (cmd) {
  2226. case SSL_CTRL_SET_MSG_CALLBACK:
  2227. ctx->msg_callback = (void (*)
  2228. (int write_p, int version, int content_type,
  2229. const void *buf, size_t len, SSL *ssl,
  2230. void *arg))(fp);
  2231. return 1;
  2232. default:
  2233. return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
  2234. }
  2235. }
  2236. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  2237. {
  2238. if (a->id > b->id)
  2239. return 1;
  2240. if (a->id < b->id)
  2241. return -1;
  2242. return 0;
  2243. }
  2244. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
  2245. const SSL_CIPHER *const *bp)
  2246. {
  2247. if ((*ap)->id > (*bp)->id)
  2248. return 1;
  2249. if ((*ap)->id < (*bp)->id)
  2250. return -1;
  2251. return 0;
  2252. }
  2253. /** return a STACK of the ciphers available for the SSL and in order of
  2254. * preference */
  2255. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  2256. {
  2257. if (s != NULL) {
  2258. if (s->cipher_list != NULL) {
  2259. return s->cipher_list;
  2260. } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
  2261. return s->ctx->cipher_list;
  2262. }
  2263. }
  2264. return NULL;
  2265. }
  2266. STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
  2267. {
  2268. if ((s == NULL) || !s->server)
  2269. return NULL;
  2270. return s->peer_ciphers;
  2271. }
  2272. STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
  2273. {
  2274. STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
  2275. int i;
  2276. ciphers = SSL_get_ciphers(s);
  2277. if (!ciphers)
  2278. return NULL;
  2279. if (!ssl_set_client_disabled(s))
  2280. return NULL;
  2281. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  2282. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  2283. if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
  2284. if (!sk)
  2285. sk = sk_SSL_CIPHER_new_null();
  2286. if (!sk)
  2287. return NULL;
  2288. if (!sk_SSL_CIPHER_push(sk, c)) {
  2289. sk_SSL_CIPHER_free(sk);
  2290. return NULL;
  2291. }
  2292. }
  2293. }
  2294. return sk;
  2295. }
  2296. /** return a STACK of the ciphers available for the SSL and in order of
  2297. * algorithm id */
  2298. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  2299. {
  2300. if (s != NULL) {
  2301. if (s->cipher_list_by_id != NULL) {
  2302. return s->cipher_list_by_id;
  2303. } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
  2304. return s->ctx->cipher_list_by_id;
  2305. }
  2306. }
  2307. return NULL;
  2308. }
  2309. /** The old interface to get the same thing as SSL_get_ciphers() */
  2310. const char *SSL_get_cipher_list(const SSL *s, int n)
  2311. {
  2312. const SSL_CIPHER *c;
  2313. STACK_OF(SSL_CIPHER) *sk;
  2314. if (s == NULL)
  2315. return NULL;
  2316. sk = SSL_get_ciphers(s);
  2317. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  2318. return NULL;
  2319. c = sk_SSL_CIPHER_value(sk, n);
  2320. if (c == NULL)
  2321. return NULL;
  2322. return c->name;
  2323. }
  2324. /** return a STACK of the ciphers available for the SSL_CTX and in order of
  2325. * preference */
  2326. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
  2327. {
  2328. if (ctx != NULL)
  2329. return ctx->cipher_list;
  2330. return NULL;
  2331. }
  2332. /*
  2333. * Distinguish between ciphers controlled by set_ciphersuite() and
  2334. * set_cipher_list() when counting.
  2335. */
  2336. static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
  2337. {
  2338. int i, num = 0;
  2339. const SSL_CIPHER *c;
  2340. if (sk == NULL)
  2341. return 0;
  2342. for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
  2343. c = sk_SSL_CIPHER_value(sk, i);
  2344. if (c->min_tls >= TLS1_3_VERSION)
  2345. continue;
  2346. num++;
  2347. }
  2348. return num;
  2349. }
  2350. /** specify the ciphers to be used by default by the SSL_CTX */
  2351. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  2352. {
  2353. STACK_OF(SSL_CIPHER) *sk;
  2354. sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
  2355. &ctx->cipher_list, &ctx->cipher_list_by_id, str,
  2356. ctx->cert);
  2357. /*
  2358. * ssl_create_cipher_list may return an empty stack if it was unable to
  2359. * find a cipher matching the given rule string (for example if the rule
  2360. * string specifies a cipher which has been disabled). This is not an
  2361. * error as far as ssl_create_cipher_list is concerned, and hence
  2362. * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
  2363. */
  2364. if (sk == NULL)
  2365. return 0;
  2366. else if (cipher_list_tls12_num(sk) == 0) {
  2367. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
  2368. return 0;
  2369. }
  2370. return 1;
  2371. }
  2372. /** specify the ciphers to be used by the SSL */
  2373. int SSL_set_cipher_list(SSL *s, const char *str)
  2374. {
  2375. STACK_OF(SSL_CIPHER) *sk;
  2376. sk = ssl_create_cipher_list(s->ctx, s->tls13_ciphersuites,
  2377. &s->cipher_list, &s->cipher_list_by_id, str,
  2378. s->cert);
  2379. /* see comment in SSL_CTX_set_cipher_list */
  2380. if (sk == NULL)
  2381. return 0;
  2382. else if (cipher_list_tls12_num(sk) == 0) {
  2383. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
  2384. return 0;
  2385. }
  2386. return 1;
  2387. }
  2388. char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
  2389. {
  2390. char *p;
  2391. STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
  2392. const SSL_CIPHER *c;
  2393. int i;
  2394. if (!s->server
  2395. || s->peer_ciphers == NULL
  2396. || size < 2)
  2397. return NULL;
  2398. p = buf;
  2399. clntsk = s->peer_ciphers;
  2400. srvrsk = SSL_get_ciphers(s);
  2401. if (clntsk == NULL || srvrsk == NULL)
  2402. return NULL;
  2403. if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
  2404. return NULL;
  2405. for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
  2406. int n;
  2407. c = sk_SSL_CIPHER_value(clntsk, i);
  2408. if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
  2409. continue;
  2410. n = strlen(c->name);
  2411. if (n + 1 > size) {
  2412. if (p != buf)
  2413. --p;
  2414. *p = '\0';
  2415. return buf;
  2416. }
  2417. strcpy(p, c->name);
  2418. p += n;
  2419. *(p++) = ':';
  2420. size -= n + 1;
  2421. }
  2422. p[-1] = '\0';
  2423. return buf;
  2424. }
  2425. /**
  2426. * Return the requested servername (SNI) value. Note that the behaviour varies
  2427. * depending on:
  2428. * - whether this is called by the client or the server,
  2429. * - if we are before or during/after the handshake,
  2430. * - if a resumption or normal handshake is being attempted/has occurred
  2431. * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
  2432. *
  2433. * Note that only the host_name type is defined (RFC 3546).
  2434. */
  2435. const char *SSL_get_servername(const SSL *s, const int type)
  2436. {
  2437. /*
  2438. * If we don't know if we are the client or the server yet then we assume
  2439. * client.
  2440. */
  2441. int server = s->handshake_func == NULL ? 0 : s->server;
  2442. if (type != TLSEXT_NAMETYPE_host_name)
  2443. return NULL;
  2444. if (server) {
  2445. /**
  2446. * Server side
  2447. * In TLSv1.3 on the server SNI is not associated with the session
  2448. * but in TLSv1.2 or below it is.
  2449. *
  2450. * Before the handshake:
  2451. * - return NULL
  2452. *
  2453. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2454. * - If a servername was accepted by the server in the original
  2455. * handshake then it will return that servername, or NULL otherwise.
  2456. *
  2457. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2458. * - The function will return the servername requested by the client in
  2459. * this handshake or NULL if none was requested.
  2460. */
  2461. if (s->hit && !SSL_IS_TLS13(s))
  2462. return s->session->ext.hostname;
  2463. } else {
  2464. /**
  2465. * Client side
  2466. *
  2467. * Before the handshake:
  2468. * - If a servername has been set via a call to
  2469. * SSL_set_tlsext_host_name() then it will return that servername
  2470. * - If one has not been set, but a TLSv1.2 resumption is being
  2471. * attempted and the session from the original handshake had a
  2472. * servername accepted by the server then it will return that
  2473. * servername
  2474. * - Otherwise it returns NULL
  2475. *
  2476. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2477. * - If the session from the original handshake had a servername accepted
  2478. * by the server then it will return that servername.
  2479. * - Otherwise it returns the servername set via
  2480. * SSL_set_tlsext_host_name() (or NULL if it was not called).
  2481. *
  2482. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2483. * - It will return the servername set via SSL_set_tlsext_host_name()
  2484. * (or NULL if it was not called).
  2485. */
  2486. if (SSL_in_before(s)) {
  2487. if (s->ext.hostname == NULL
  2488. && s->session != NULL
  2489. && s->session->ssl_version != TLS1_3_VERSION)
  2490. return s->session->ext.hostname;
  2491. } else {
  2492. if (!SSL_IS_TLS13(s) && s->hit && s->session->ext.hostname != NULL)
  2493. return s->session->ext.hostname;
  2494. }
  2495. }
  2496. return s->ext.hostname;
  2497. }
  2498. int SSL_get_servername_type(const SSL *s)
  2499. {
  2500. if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
  2501. return TLSEXT_NAMETYPE_host_name;
  2502. return -1;
  2503. }
  2504. /*
  2505. * SSL_select_next_proto implements the standard protocol selection. It is
  2506. * expected that this function is called from the callback set by
  2507. * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
  2508. * vector of 8-bit, length prefixed byte strings. The length byte itself is
  2509. * not included in the length. A byte string of length 0 is invalid. No byte
  2510. * string may be truncated. The current, but experimental algorithm for
  2511. * selecting the protocol is: 1) If the server doesn't support NPN then this
  2512. * is indicated to the callback. In this case, the client application has to
  2513. * abort the connection or have a default application level protocol. 2) If
  2514. * the server supports NPN, but advertises an empty list then the client
  2515. * selects the first protocol in its list, but indicates via the API that this
  2516. * fallback case was enacted. 3) Otherwise, the client finds the first
  2517. * protocol in the server's list that it supports and selects this protocol.
  2518. * This is because it's assumed that the server has better information about
  2519. * which protocol a client should use. 4) If the client doesn't support any
  2520. * of the server's advertised protocols, then this is treated the same as
  2521. * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
  2522. * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  2523. */
  2524. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
  2525. const unsigned char *server,
  2526. unsigned int server_len,
  2527. const unsigned char *client, unsigned int client_len)
  2528. {
  2529. unsigned int i, j;
  2530. const unsigned char *result;
  2531. int status = OPENSSL_NPN_UNSUPPORTED;
  2532. /*
  2533. * For each protocol in server preference order, see if we support it.
  2534. */
  2535. for (i = 0; i < server_len;) {
  2536. for (j = 0; j < client_len;) {
  2537. if (server[i] == client[j] &&
  2538. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  2539. /* We found a match */
  2540. result = &server[i];
  2541. status = OPENSSL_NPN_NEGOTIATED;
  2542. goto found;
  2543. }
  2544. j += client[j];
  2545. j++;
  2546. }
  2547. i += server[i];
  2548. i++;
  2549. }
  2550. /* There's no overlap between our protocols and the server's list. */
  2551. result = client;
  2552. status = OPENSSL_NPN_NO_OVERLAP;
  2553. found:
  2554. *out = (unsigned char *)result + 1;
  2555. *outlen = result[0];
  2556. return status;
  2557. }
  2558. #ifndef OPENSSL_NO_NEXTPROTONEG
  2559. /*
  2560. * SSL_get0_next_proto_negotiated sets *data and *len to point to the
  2561. * client's requested protocol for this connection and returns 0. If the
  2562. * client didn't request any protocol, then *data is set to NULL. Note that
  2563. * the client can request any protocol it chooses. The value returned from
  2564. * this function need not be a member of the list of supported protocols
  2565. * provided by the callback.
  2566. */
  2567. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
  2568. unsigned *len)
  2569. {
  2570. *data = s->ext.npn;
  2571. if (*data == NULL) {
  2572. *len = 0;
  2573. } else {
  2574. *len = (unsigned int)s->ext.npn_len;
  2575. }
  2576. }
  2577. /*
  2578. * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
  2579. * a TLS server needs a list of supported protocols for Next Protocol
  2580. * Negotiation. The returned list must be in wire format. The list is
  2581. * returned by setting |out| to point to it and |outlen| to its length. This
  2582. * memory will not be modified, but one should assume that the SSL* keeps a
  2583. * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
  2584. * wishes to advertise. Otherwise, no such extension will be included in the
  2585. * ServerHello.
  2586. */
  2587. void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
  2588. SSL_CTX_npn_advertised_cb_func cb,
  2589. void *arg)
  2590. {
  2591. ctx->ext.npn_advertised_cb = cb;
  2592. ctx->ext.npn_advertised_cb_arg = arg;
  2593. }
  2594. /*
  2595. * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  2596. * client needs to select a protocol from the server's provided list. |out|
  2597. * must be set to point to the selected protocol (which may be within |in|).
  2598. * The length of the protocol name must be written into |outlen|. The
  2599. * server's advertised protocols are provided in |in| and |inlen|. The
  2600. * callback can assume that |in| is syntactically valid. The client must
  2601. * select a protocol. It is fatal to the connection if this callback returns
  2602. * a value other than SSL_TLSEXT_ERR_OK.
  2603. */
  2604. void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
  2605. SSL_CTX_npn_select_cb_func cb,
  2606. void *arg)
  2607. {
  2608. ctx->ext.npn_select_cb = cb;
  2609. ctx->ext.npn_select_cb_arg = arg;
  2610. }
  2611. #endif
  2612. static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
  2613. {
  2614. unsigned int idx;
  2615. if (protos_len < 2 || protos == NULL)
  2616. return 0;
  2617. for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
  2618. if (protos[idx] == 0)
  2619. return 0;
  2620. }
  2621. return idx == protos_len;
  2622. }
  2623. /*
  2624. * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  2625. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2626. * length-prefixed strings). Returns 0 on success.
  2627. */
  2628. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
  2629. unsigned int protos_len)
  2630. {
  2631. unsigned char *alpn;
  2632. if (protos_len == 0 || protos == NULL) {
  2633. OPENSSL_free(ctx->ext.alpn);
  2634. ctx->ext.alpn = NULL;
  2635. ctx->ext.alpn_len = 0;
  2636. return 0;
  2637. }
  2638. /* Not valid per RFC */
  2639. if (!alpn_value_ok(protos, protos_len))
  2640. return 1;
  2641. alpn = OPENSSL_memdup(protos, protos_len);
  2642. if (alpn == NULL) {
  2643. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2644. return 1;
  2645. }
  2646. OPENSSL_free(ctx->ext.alpn);
  2647. ctx->ext.alpn = alpn;
  2648. ctx->ext.alpn_len = protos_len;
  2649. return 0;
  2650. }
  2651. /*
  2652. * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
  2653. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2654. * length-prefixed strings). Returns 0 on success.
  2655. */
  2656. int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
  2657. unsigned int protos_len)
  2658. {
  2659. unsigned char *alpn;
  2660. if (protos_len == 0 || protos == NULL) {
  2661. OPENSSL_free(ssl->ext.alpn);
  2662. ssl->ext.alpn = NULL;
  2663. ssl->ext.alpn_len = 0;
  2664. return 0;
  2665. }
  2666. /* Not valid per RFC */
  2667. if (!alpn_value_ok(protos, protos_len))
  2668. return 1;
  2669. alpn = OPENSSL_memdup(protos, protos_len);
  2670. if (alpn == NULL) {
  2671. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2672. return 1;
  2673. }
  2674. OPENSSL_free(ssl->ext.alpn);
  2675. ssl->ext.alpn = alpn;
  2676. ssl->ext.alpn_len = protos_len;
  2677. return 0;
  2678. }
  2679. /*
  2680. * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
  2681. * called during ClientHello processing in order to select an ALPN protocol
  2682. * from the client's list of offered protocols.
  2683. */
  2684. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  2685. SSL_CTX_alpn_select_cb_func cb,
  2686. void *arg)
  2687. {
  2688. ctx->ext.alpn_select_cb = cb;
  2689. ctx->ext.alpn_select_cb_arg = arg;
  2690. }
  2691. /*
  2692. * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  2693. * On return it sets |*data| to point to |*len| bytes of protocol name
  2694. * (not including the leading length-prefix byte). If the server didn't
  2695. * respond with a negotiated protocol then |*len| will be zero.
  2696. */
  2697. void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
  2698. unsigned int *len)
  2699. {
  2700. *data = ssl->s3.alpn_selected;
  2701. if (*data == NULL)
  2702. *len = 0;
  2703. else
  2704. *len = (unsigned int)ssl->s3.alpn_selected_len;
  2705. }
  2706. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  2707. const char *label, size_t llen,
  2708. const unsigned char *context, size_t contextlen,
  2709. int use_context)
  2710. {
  2711. if (s->session == NULL
  2712. || (s->version < TLS1_VERSION && s->version != DTLS1_BAD_VER))
  2713. return -1;
  2714. return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
  2715. llen, context,
  2716. contextlen, use_context);
  2717. }
  2718. int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  2719. const char *label, size_t llen,
  2720. const unsigned char *context,
  2721. size_t contextlen)
  2722. {
  2723. if (s->version != TLS1_3_VERSION)
  2724. return 0;
  2725. return tls13_export_keying_material_early(s, out, olen, label, llen,
  2726. context, contextlen);
  2727. }
  2728. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  2729. {
  2730. const unsigned char *session_id = a->session_id;
  2731. unsigned long l;
  2732. unsigned char tmp_storage[4];
  2733. if (a->session_id_length < sizeof(tmp_storage)) {
  2734. memset(tmp_storage, 0, sizeof(tmp_storage));
  2735. memcpy(tmp_storage, a->session_id, a->session_id_length);
  2736. session_id = tmp_storage;
  2737. }
  2738. l = (unsigned long)
  2739. ((unsigned long)session_id[0]) |
  2740. ((unsigned long)session_id[1] << 8L) |
  2741. ((unsigned long)session_id[2] << 16L) |
  2742. ((unsigned long)session_id[3] << 24L);
  2743. return l;
  2744. }
  2745. /*
  2746. * NB: If this function (or indeed the hash function which uses a sort of
  2747. * coarser function than this one) is changed, ensure
  2748. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
  2749. * being able to construct an SSL_SESSION that will collide with any existing
  2750. * session with a matching session ID.
  2751. */
  2752. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
  2753. {
  2754. if (a->ssl_version != b->ssl_version)
  2755. return 1;
  2756. if (a->session_id_length != b->session_id_length)
  2757. return 1;
  2758. return memcmp(a->session_id, b->session_id, a->session_id_length);
  2759. }
  2760. /*
  2761. * These wrapper functions should remain rather than redeclaring
  2762. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  2763. * variable. The reason is that the functions aren't static, they're exposed
  2764. * via ssl.h.
  2765. */
  2766. SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  2767. const SSL_METHOD *meth)
  2768. {
  2769. SSL_CTX *ret = NULL;
  2770. if (meth == NULL) {
  2771. ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  2772. return NULL;
  2773. }
  2774. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
  2775. return NULL;
  2776. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  2777. ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  2778. goto err;
  2779. }
  2780. ret = OPENSSL_zalloc(sizeof(*ret));
  2781. if (ret == NULL)
  2782. goto err;
  2783. /* Init the reference counting before any call to SSL_CTX_free */
  2784. ret->references = 1;
  2785. ret->lock = CRYPTO_THREAD_lock_new();
  2786. if (ret->lock == NULL) {
  2787. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2788. OPENSSL_free(ret);
  2789. return NULL;
  2790. }
  2791. #ifdef TSAN_REQUIRES_LOCKING
  2792. ret->tsan_lock = CRYPTO_THREAD_lock_new();
  2793. if (ret->tsan_lock == NULL) {
  2794. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2795. goto err;
  2796. }
  2797. #endif
  2798. ret->libctx = libctx;
  2799. if (propq != NULL) {
  2800. ret->propq = OPENSSL_strdup(propq);
  2801. if (ret->propq == NULL)
  2802. goto err;
  2803. }
  2804. ret->method = meth;
  2805. ret->min_proto_version = 0;
  2806. ret->max_proto_version = 0;
  2807. ret->mode = SSL_MODE_AUTO_RETRY;
  2808. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  2809. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  2810. /* We take the system default. */
  2811. ret->session_timeout = meth->get_timeout();
  2812. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  2813. ret->verify_mode = SSL_VERIFY_NONE;
  2814. if ((ret->cert = ssl_cert_new()) == NULL)
  2815. goto err;
  2816. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  2817. if (ret->sessions == NULL)
  2818. goto err;
  2819. ret->cert_store = X509_STORE_new();
  2820. if (ret->cert_store == NULL)
  2821. goto err;
  2822. #ifndef OPENSSL_NO_CT
  2823. ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
  2824. if (ret->ctlog_store == NULL)
  2825. goto err;
  2826. #endif
  2827. /* initialize cipher/digest methods table */
  2828. if (!ssl_load_ciphers(ret))
  2829. goto err2;
  2830. /* initialise sig algs */
  2831. if (!ssl_setup_sig_algs(ret))
  2832. goto err2;
  2833. if (!ssl_load_groups(ret))
  2834. goto err2;
  2835. if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites()))
  2836. goto err;
  2837. if (!ssl_create_cipher_list(ret,
  2838. ret->tls13_ciphersuites,
  2839. &ret->cipher_list, &ret->cipher_list_by_id,
  2840. OSSL_default_cipher_list(), ret->cert)
  2841. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
  2842. ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  2843. goto err2;
  2844. }
  2845. ret->param = X509_VERIFY_PARAM_new();
  2846. if (ret->param == NULL)
  2847. goto err;
  2848. /*
  2849. * If these aren't available from the provider we'll get NULL returns.
  2850. * That's fine but will cause errors later if SSLv3 is negotiated
  2851. */
  2852. ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq);
  2853. ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq);
  2854. if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL)
  2855. goto err;
  2856. if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL)
  2857. goto err;
  2858. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data))
  2859. goto err;
  2860. if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
  2861. goto err;
  2862. /* No compression for DTLS */
  2863. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
  2864. ret->comp_methods = SSL_COMP_get_compression_methods();
  2865. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2866. ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2867. /* Setup RFC5077 ticket keys */
  2868. if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
  2869. sizeof(ret->ext.tick_key_name), 0) <= 0)
  2870. || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
  2871. sizeof(ret->ext.secure->tick_hmac_key), 0) <= 0)
  2872. || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
  2873. sizeof(ret->ext.secure->tick_aes_key), 0) <= 0))
  2874. ret->options |= SSL_OP_NO_TICKET;
  2875. if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
  2876. sizeof(ret->ext.cookie_hmac_key), 0) <= 0)
  2877. goto err;
  2878. #ifndef OPENSSL_NO_SRP
  2879. if (!ssl_ctx_srp_ctx_init_intern(ret))
  2880. goto err;
  2881. #endif
  2882. #ifndef OPENSSL_NO_ENGINE
  2883. # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  2884. # define eng_strx(x) #x
  2885. # define eng_str(x) eng_strx(x)
  2886. /* Use specific client engine automatically... ignore errors */
  2887. {
  2888. ENGINE *eng;
  2889. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2890. if (!eng) {
  2891. ERR_clear_error();
  2892. ENGINE_load_builtin_engines();
  2893. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2894. }
  2895. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  2896. ERR_clear_error();
  2897. }
  2898. # endif
  2899. #endif
  2900. /*
  2901. * Disable compression by default to prevent CRIME. Applications can
  2902. * re-enable compression by configuring
  2903. * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
  2904. * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
  2905. * middlebox compatibility by default. This may be disabled by default in
  2906. * a later OpenSSL version.
  2907. */
  2908. ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  2909. ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  2910. /*
  2911. * We cannot usefully set a default max_early_data here (which gets
  2912. * propagated in SSL_new(), for the following reason: setting the
  2913. * SSL field causes tls_construct_stoc_early_data() to tell the
  2914. * client that early data will be accepted when constructing a TLS 1.3
  2915. * session ticket, and the client will accordingly send us early data
  2916. * when using that ticket (if the client has early data to send).
  2917. * However, in order for the early data to actually be consumed by
  2918. * the application, the application must also have calls to
  2919. * SSL_read_early_data(); otherwise we'll just skip past the early data
  2920. * and ignore it. So, since the application must add calls to
  2921. * SSL_read_early_data(), we also require them to add
  2922. * calls to SSL_CTX_set_max_early_data() in order to use early data,
  2923. * eliminating the bandwidth-wasting early data in the case described
  2924. * above.
  2925. */
  2926. ret->max_early_data = 0;
  2927. /*
  2928. * Default recv_max_early_data is a fully loaded single record. Could be
  2929. * split across multiple records in practice. We set this differently to
  2930. * max_early_data so that, in the default case, we do not advertise any
  2931. * support for early_data, but if a client were to send us some (e.g.
  2932. * because of an old, stale ticket) then we will tolerate it and skip over
  2933. * it.
  2934. */
  2935. ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
  2936. /* By default we send two session tickets automatically in TLSv1.3 */
  2937. ret->num_tickets = 2;
  2938. ssl_ctx_system_config(ret);
  2939. return ret;
  2940. err:
  2941. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  2942. err2:
  2943. SSL_CTX_free(ret);
  2944. return NULL;
  2945. }
  2946. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  2947. {
  2948. return SSL_CTX_new_ex(NULL, NULL, meth);
  2949. }
  2950. int SSL_CTX_up_ref(SSL_CTX *ctx)
  2951. {
  2952. int i;
  2953. if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0)
  2954. return 0;
  2955. REF_PRINT_COUNT("SSL_CTX", ctx);
  2956. REF_ASSERT_ISNT(i < 2);
  2957. return ((i > 1) ? 1 : 0);
  2958. }
  2959. void SSL_CTX_free(SSL_CTX *a)
  2960. {
  2961. int i;
  2962. size_t j;
  2963. if (a == NULL)
  2964. return;
  2965. CRYPTO_DOWN_REF(&a->references, &i, a->lock);
  2966. REF_PRINT_COUNT("SSL_CTX", a);
  2967. if (i > 0)
  2968. return;
  2969. REF_ASSERT_ISNT(i < 0);
  2970. X509_VERIFY_PARAM_free(a->param);
  2971. dane_ctx_final(&a->dane);
  2972. /*
  2973. * Free internal session cache. However: the remove_cb() may reference
  2974. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  2975. * after the sessions were flushed.
  2976. * As the ex_data handling routines might also touch the session cache,
  2977. * the most secure solution seems to be: empty (flush) the cache, then
  2978. * free ex_data, then finally free the cache.
  2979. * (See ticket [openssl.org #212].)
  2980. */
  2981. if (a->sessions != NULL)
  2982. SSL_CTX_flush_sessions(a, 0);
  2983. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  2984. lh_SSL_SESSION_free(a->sessions);
  2985. X509_STORE_free(a->cert_store);
  2986. #ifndef OPENSSL_NO_CT
  2987. CTLOG_STORE_free(a->ctlog_store);
  2988. #endif
  2989. sk_SSL_CIPHER_free(a->cipher_list);
  2990. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  2991. sk_SSL_CIPHER_free(a->tls13_ciphersuites);
  2992. ssl_cert_free(a->cert);
  2993. sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
  2994. sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
  2995. OSSL_STACK_OF_X509_free(a->extra_certs);
  2996. a->comp_methods = NULL;
  2997. #ifndef OPENSSL_NO_SRTP
  2998. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  2999. #endif
  3000. #ifndef OPENSSL_NO_SRP
  3001. ssl_ctx_srp_ctx_free_intern(a);
  3002. #endif
  3003. #ifndef OPENSSL_NO_ENGINE
  3004. tls_engine_finish(a->client_cert_engine);
  3005. #endif
  3006. OPENSSL_free(a->ext.ecpointformats);
  3007. OPENSSL_free(a->ext.supportedgroups);
  3008. OPENSSL_free(a->ext.supported_groups_default);
  3009. OPENSSL_free(a->ext.alpn);
  3010. OPENSSL_secure_free(a->ext.secure);
  3011. ssl_evp_md_free(a->md5);
  3012. ssl_evp_md_free(a->sha1);
  3013. for (j = 0; j < SSL_ENC_NUM_IDX; j++)
  3014. ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
  3015. for (j = 0; j < SSL_MD_NUM_IDX; j++)
  3016. ssl_evp_md_free(a->ssl_digest_methods[j]);
  3017. for (j = 0; j < a->group_list_len; j++) {
  3018. OPENSSL_free(a->group_list[j].tlsname);
  3019. OPENSSL_free(a->group_list[j].realname);
  3020. OPENSSL_free(a->group_list[j].algorithm);
  3021. }
  3022. OPENSSL_free(a->group_list);
  3023. OPENSSL_free(a->sigalg_lookup_cache);
  3024. CRYPTO_THREAD_lock_free(a->lock);
  3025. #ifdef TSAN_REQUIRES_LOCKING
  3026. CRYPTO_THREAD_lock_free(a->tsan_lock);
  3027. #endif
  3028. OPENSSL_free(a->propq);
  3029. OPENSSL_free(a);
  3030. }
  3031. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  3032. {
  3033. ctx->default_passwd_callback = cb;
  3034. }
  3035. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  3036. {
  3037. ctx->default_passwd_callback_userdata = u;
  3038. }
  3039. pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
  3040. {
  3041. return ctx->default_passwd_callback;
  3042. }
  3043. void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
  3044. {
  3045. return ctx->default_passwd_callback_userdata;
  3046. }
  3047. void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
  3048. {
  3049. s->default_passwd_callback = cb;
  3050. }
  3051. void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
  3052. {
  3053. s->default_passwd_callback_userdata = u;
  3054. }
  3055. pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
  3056. {
  3057. return s->default_passwd_callback;
  3058. }
  3059. void *SSL_get_default_passwd_cb_userdata(SSL *s)
  3060. {
  3061. return s->default_passwd_callback_userdata;
  3062. }
  3063. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  3064. int (*cb) (X509_STORE_CTX *, void *),
  3065. void *arg)
  3066. {
  3067. ctx->app_verify_callback = cb;
  3068. ctx->app_verify_arg = arg;
  3069. }
  3070. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  3071. int (*cb) (int, X509_STORE_CTX *))
  3072. {
  3073. ctx->verify_mode = mode;
  3074. ctx->default_verify_callback = cb;
  3075. }
  3076. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  3077. {
  3078. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  3079. }
  3080. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  3081. {
  3082. ssl_cert_set_cert_cb(c->cert, cb, arg);
  3083. }
  3084. void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
  3085. {
  3086. ssl_cert_set_cert_cb(s->cert, cb, arg);
  3087. }
  3088. void ssl_set_masks(SSL *s)
  3089. {
  3090. CERT *c = s->cert;
  3091. uint32_t *pvalid = s->s3.tmp.valid_flags;
  3092. int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
  3093. unsigned long mask_k, mask_a;
  3094. int have_ecc_cert, ecdsa_ok;
  3095. if (c == NULL)
  3096. return;
  3097. dh_tmp = (c->dh_tmp != NULL
  3098. || c->dh_tmp_cb != NULL
  3099. || c->dh_tmp_auto);
  3100. rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  3101. rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  3102. dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
  3103. have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
  3104. mask_k = 0;
  3105. mask_a = 0;
  3106. OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
  3107. dh_tmp, rsa_enc, rsa_sign, dsa_sign);
  3108. #ifndef OPENSSL_NO_GOST
  3109. if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
  3110. mask_k |= SSL_kGOST | SSL_kGOST18;
  3111. mask_a |= SSL_aGOST12;
  3112. }
  3113. if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
  3114. mask_k |= SSL_kGOST | SSL_kGOST18;
  3115. mask_a |= SSL_aGOST12;
  3116. }
  3117. if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
  3118. mask_k |= SSL_kGOST;
  3119. mask_a |= SSL_aGOST01;
  3120. }
  3121. #endif
  3122. if (rsa_enc)
  3123. mask_k |= SSL_kRSA;
  3124. if (dh_tmp)
  3125. mask_k |= SSL_kDHE;
  3126. /*
  3127. * If we only have an RSA-PSS certificate allow RSA authentication
  3128. * if TLS 1.2 and peer supports it.
  3129. */
  3130. if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
  3131. && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
  3132. && TLS1_get_version(s) == TLS1_2_VERSION))
  3133. mask_a |= SSL_aRSA;
  3134. if (dsa_sign) {
  3135. mask_a |= SSL_aDSS;
  3136. }
  3137. mask_a |= SSL_aNULL;
  3138. /*
  3139. * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
  3140. * depending on the key usage extension.
  3141. */
  3142. if (have_ecc_cert) {
  3143. uint32_t ex_kusage;
  3144. ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
  3145. ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
  3146. if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
  3147. ecdsa_ok = 0;
  3148. if (ecdsa_ok)
  3149. mask_a |= SSL_aECDSA;
  3150. }
  3151. /* Allow Ed25519 for TLS 1.2 if peer supports it */
  3152. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
  3153. && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
  3154. && TLS1_get_version(s) == TLS1_2_VERSION)
  3155. mask_a |= SSL_aECDSA;
  3156. /* Allow Ed448 for TLS 1.2 if peer supports it */
  3157. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
  3158. && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
  3159. && TLS1_get_version(s) == TLS1_2_VERSION)
  3160. mask_a |= SSL_aECDSA;
  3161. mask_k |= SSL_kECDHE;
  3162. #ifndef OPENSSL_NO_PSK
  3163. mask_k |= SSL_kPSK;
  3164. mask_a |= SSL_aPSK;
  3165. if (mask_k & SSL_kRSA)
  3166. mask_k |= SSL_kRSAPSK;
  3167. if (mask_k & SSL_kDHE)
  3168. mask_k |= SSL_kDHEPSK;
  3169. if (mask_k & SSL_kECDHE)
  3170. mask_k |= SSL_kECDHEPSK;
  3171. #endif
  3172. s->s3.tmp.mask_k = mask_k;
  3173. s->s3.tmp.mask_a = mask_a;
  3174. }
  3175. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
  3176. {
  3177. if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
  3178. /* key usage, if present, must allow signing */
  3179. if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
  3180. ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  3181. return 0;
  3182. }
  3183. }
  3184. return 1; /* all checks are ok */
  3185. }
  3186. int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
  3187. size_t *serverinfo_length)
  3188. {
  3189. CERT_PKEY *cpk = s->s3.tmp.cert;
  3190. *serverinfo_length = 0;
  3191. if (cpk == NULL || cpk->serverinfo == NULL)
  3192. return 0;
  3193. *serverinfo = cpk->serverinfo;
  3194. *serverinfo_length = cpk->serverinfo_length;
  3195. return 1;
  3196. }
  3197. void ssl_update_cache(SSL *s, int mode)
  3198. {
  3199. int i;
  3200. /*
  3201. * If the session_id_length is 0, we are not supposed to cache it, and it
  3202. * would be rather hard to do anyway :-)
  3203. */
  3204. if (s->session->session_id_length == 0)
  3205. return;
  3206. /*
  3207. * If sid_ctx_length is 0 there is no specific application context
  3208. * associated with this session, so when we try to resume it and
  3209. * SSL_VERIFY_PEER is requested to verify the client identity, we have no
  3210. * indication that this is actually a session for the proper application
  3211. * context, and the *handshake* will fail, not just the resumption attempt.
  3212. * Do not cache (on the server) these sessions that are not resumable
  3213. * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
  3214. */
  3215. if (s->server && s->session->sid_ctx_length == 0
  3216. && (s->verify_mode & SSL_VERIFY_PEER) != 0)
  3217. return;
  3218. i = s->session_ctx->session_cache_mode;
  3219. if ((i & mode) != 0
  3220. && (!s->hit || SSL_IS_TLS13(s))) {
  3221. /*
  3222. * Add the session to the internal cache. In server side TLSv1.3 we
  3223. * normally don't do this because by default it's a full stateless ticket
  3224. * with only a dummy session id so there is no reason to cache it,
  3225. * unless:
  3226. * - we are doing early_data, in which case we cache so that we can
  3227. * detect replays
  3228. * - the application has set a remove_session_cb so needs to know about
  3229. * session timeout events
  3230. * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
  3231. */
  3232. if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
  3233. && (!SSL_IS_TLS13(s)
  3234. || !s->server
  3235. || (s->max_early_data > 0
  3236. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
  3237. || s->session_ctx->remove_session_cb != NULL
  3238. || (s->options & SSL_OP_NO_TICKET) != 0))
  3239. SSL_CTX_add_session(s->session_ctx, s->session);
  3240. /*
  3241. * Add the session to the external cache. We do this even in server side
  3242. * TLSv1.3 without early data because some applications just want to
  3243. * know about the creation of a session and aren't doing a full cache.
  3244. */
  3245. if (s->session_ctx->new_session_cb != NULL) {
  3246. SSL_SESSION_up_ref(s->session);
  3247. if (!s->session_ctx->new_session_cb(s, s->session))
  3248. SSL_SESSION_free(s->session);
  3249. }
  3250. }
  3251. /* auto flush every 255 connections */
  3252. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
  3253. TSAN_QUALIFIER int *stat;
  3254. if (mode & SSL_SESS_CACHE_CLIENT)
  3255. stat = &s->session_ctx->stats.sess_connect_good;
  3256. else
  3257. stat = &s->session_ctx->stats.sess_accept_good;
  3258. if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff)
  3259. SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
  3260. }
  3261. }
  3262. const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
  3263. {
  3264. return ctx->method;
  3265. }
  3266. const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
  3267. {
  3268. return s->method;
  3269. }
  3270. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  3271. {
  3272. int ret = 1;
  3273. if (s->method != meth) {
  3274. const SSL_METHOD *sm = s->method;
  3275. int (*hf) (SSL *) = s->handshake_func;
  3276. if (sm->version == meth->version)
  3277. s->method = meth;
  3278. else {
  3279. sm->ssl_free(s);
  3280. s->method = meth;
  3281. ret = s->method->ssl_new(s);
  3282. }
  3283. if (hf == sm->ssl_connect)
  3284. s->handshake_func = meth->ssl_connect;
  3285. else if (hf == sm->ssl_accept)
  3286. s->handshake_func = meth->ssl_accept;
  3287. }
  3288. return ret;
  3289. }
  3290. int SSL_get_error(const SSL *s, int i)
  3291. {
  3292. int reason;
  3293. unsigned long l;
  3294. BIO *bio;
  3295. if (i > 0)
  3296. return SSL_ERROR_NONE;
  3297. /*
  3298. * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  3299. * where we do encode the error
  3300. */
  3301. if ((l = ERR_peek_error()) != 0) {
  3302. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  3303. return SSL_ERROR_SYSCALL;
  3304. else
  3305. return SSL_ERROR_SSL;
  3306. }
  3307. if (SSL_want_read(s)) {
  3308. bio = SSL_get_rbio(s);
  3309. if (BIO_should_read(bio))
  3310. return SSL_ERROR_WANT_READ;
  3311. else if (BIO_should_write(bio))
  3312. /*
  3313. * This one doesn't make too much sense ... We never try to write
  3314. * to the rbio, and an application program where rbio and wbio
  3315. * are separate couldn't even know what it should wait for.
  3316. * However if we ever set s->rwstate incorrectly (so that we have
  3317. * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
  3318. * wbio *are* the same, this test works around that bug; so it
  3319. * might be safer to keep it.
  3320. */
  3321. return SSL_ERROR_WANT_WRITE;
  3322. else if (BIO_should_io_special(bio)) {
  3323. reason = BIO_get_retry_reason(bio);
  3324. if (reason == BIO_RR_CONNECT)
  3325. return SSL_ERROR_WANT_CONNECT;
  3326. else if (reason == BIO_RR_ACCEPT)
  3327. return SSL_ERROR_WANT_ACCEPT;
  3328. else
  3329. return SSL_ERROR_SYSCALL; /* unknown */
  3330. }
  3331. }
  3332. if (SSL_want_write(s)) {
  3333. /* Access wbio directly - in order to use the buffered bio if present */
  3334. bio = s->wbio;
  3335. if (BIO_should_write(bio))
  3336. return SSL_ERROR_WANT_WRITE;
  3337. else if (BIO_should_read(bio))
  3338. /*
  3339. * See above (SSL_want_read(s) with BIO_should_write(bio))
  3340. */
  3341. return SSL_ERROR_WANT_READ;
  3342. else if (BIO_should_io_special(bio)) {
  3343. reason = BIO_get_retry_reason(bio);
  3344. if (reason == BIO_RR_CONNECT)
  3345. return SSL_ERROR_WANT_CONNECT;
  3346. else if (reason == BIO_RR_ACCEPT)
  3347. return SSL_ERROR_WANT_ACCEPT;
  3348. else
  3349. return SSL_ERROR_SYSCALL;
  3350. }
  3351. }
  3352. if (SSL_want_x509_lookup(s))
  3353. return SSL_ERROR_WANT_X509_LOOKUP;
  3354. if (SSL_want_retry_verify(s))
  3355. return SSL_ERROR_WANT_RETRY_VERIFY;
  3356. if (SSL_want_async(s))
  3357. return SSL_ERROR_WANT_ASYNC;
  3358. if (SSL_want_async_job(s))
  3359. return SSL_ERROR_WANT_ASYNC_JOB;
  3360. if (SSL_want_client_hello_cb(s))
  3361. return SSL_ERROR_WANT_CLIENT_HELLO_CB;
  3362. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  3363. (s->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
  3364. return SSL_ERROR_ZERO_RETURN;
  3365. return SSL_ERROR_SYSCALL;
  3366. }
  3367. static int ssl_do_handshake_intern(void *vargs)
  3368. {
  3369. struct ssl_async_args *args;
  3370. SSL *s;
  3371. args = (struct ssl_async_args *)vargs;
  3372. s = args->s;
  3373. return s->handshake_func(s);
  3374. }
  3375. int SSL_do_handshake(SSL *s)
  3376. {
  3377. int ret = 1;
  3378. if (s->handshake_func == NULL) {
  3379. ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  3380. return -1;
  3381. }
  3382. ossl_statem_check_finish_init(s, -1);
  3383. s->method->ssl_renegotiate_check(s, 0);
  3384. if (SSL_in_init(s) || SSL_in_before(s)) {
  3385. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  3386. struct ssl_async_args args;
  3387. memset(&args, 0, sizeof(args));
  3388. args.s = s;
  3389. ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
  3390. } else {
  3391. ret = s->handshake_func(s);
  3392. }
  3393. }
  3394. return ret;
  3395. }
  3396. void SSL_set_accept_state(SSL *s)
  3397. {
  3398. s->server = 1;
  3399. s->shutdown = 0;
  3400. ossl_statem_clear(s);
  3401. s->handshake_func = s->method->ssl_accept;
  3402. clear_ciphers(s);
  3403. }
  3404. void SSL_set_connect_state(SSL *s)
  3405. {
  3406. s->server = 0;
  3407. s->shutdown = 0;
  3408. ossl_statem_clear(s);
  3409. s->handshake_func = s->method->ssl_connect;
  3410. clear_ciphers(s);
  3411. }
  3412. int ssl_undefined_function(SSL *s)
  3413. {
  3414. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3415. return 0;
  3416. }
  3417. int ssl_undefined_void_function(void)
  3418. {
  3419. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3420. return 0;
  3421. }
  3422. int ssl_undefined_const_function(const SSL *s)
  3423. {
  3424. return 0;
  3425. }
  3426. const SSL_METHOD *ssl_bad_method(int ver)
  3427. {
  3428. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3429. return NULL;
  3430. }
  3431. const char *ssl_protocol_to_string(int version)
  3432. {
  3433. switch (version)
  3434. {
  3435. case TLS1_3_VERSION:
  3436. return "TLSv1.3";
  3437. case TLS1_2_VERSION:
  3438. return "TLSv1.2";
  3439. case TLS1_1_VERSION:
  3440. return "TLSv1.1";
  3441. case TLS1_VERSION:
  3442. return "TLSv1";
  3443. case SSL3_VERSION:
  3444. return "SSLv3";
  3445. case DTLS1_BAD_VER:
  3446. return "DTLSv0.9";
  3447. case DTLS1_VERSION:
  3448. return "DTLSv1";
  3449. case DTLS1_2_VERSION:
  3450. return "DTLSv1.2";
  3451. default:
  3452. return "unknown";
  3453. }
  3454. }
  3455. const char *SSL_get_version(const SSL *s)
  3456. {
  3457. return ssl_protocol_to_string(s->version);
  3458. }
  3459. static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
  3460. {
  3461. STACK_OF(X509_NAME) *sk;
  3462. X509_NAME *xn;
  3463. int i;
  3464. if (src == NULL) {
  3465. *dst = NULL;
  3466. return 1;
  3467. }
  3468. if ((sk = sk_X509_NAME_new_null()) == NULL)
  3469. return 0;
  3470. for (i = 0; i < sk_X509_NAME_num(src); i++) {
  3471. xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
  3472. if (xn == NULL) {
  3473. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3474. return 0;
  3475. }
  3476. if (sk_X509_NAME_insert(sk, xn, i) == 0) {
  3477. X509_NAME_free(xn);
  3478. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3479. return 0;
  3480. }
  3481. }
  3482. *dst = sk;
  3483. return 1;
  3484. }
  3485. SSL *SSL_dup(SSL *s)
  3486. {
  3487. SSL *ret;
  3488. int i;
  3489. /* If we're not quiescent, just up_ref! */
  3490. if (!SSL_in_init(s) || !SSL_in_before(s)) {
  3491. CRYPTO_UP_REF(&s->references, &i, s->lock);
  3492. return s;
  3493. }
  3494. /*
  3495. * Otherwise, copy configuration state, and session if set.
  3496. */
  3497. if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  3498. return NULL;
  3499. if (s->session != NULL) {
  3500. /*
  3501. * Arranges to share the same session via up_ref. This "copies"
  3502. * session-id, SSL_METHOD, sid_ctx, and 'cert'
  3503. */
  3504. if (!SSL_copy_session_id(ret, s))
  3505. goto err;
  3506. } else {
  3507. /*
  3508. * No session has been established yet, so we have to expect that
  3509. * s->cert or ret->cert will be changed later -- they should not both
  3510. * point to the same object, and thus we can't use
  3511. * SSL_copy_session_id.
  3512. */
  3513. if (!SSL_set_ssl_method(ret, s->method))
  3514. goto err;
  3515. if (s->cert != NULL) {
  3516. ssl_cert_free(ret->cert);
  3517. ret->cert = ssl_cert_dup(s->cert);
  3518. if (ret->cert == NULL)
  3519. goto err;
  3520. }
  3521. if (!SSL_set_session_id_context(ret, s->sid_ctx,
  3522. (int)s->sid_ctx_length))
  3523. goto err;
  3524. }
  3525. if (!ssl_dane_dup(ret, s))
  3526. goto err;
  3527. ret->version = s->version;
  3528. ret->options = s->options;
  3529. ret->min_proto_version = s->min_proto_version;
  3530. ret->max_proto_version = s->max_proto_version;
  3531. ret->mode = s->mode;
  3532. SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
  3533. SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
  3534. ret->msg_callback = s->msg_callback;
  3535. ret->msg_callback_arg = s->msg_callback_arg;
  3536. SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
  3537. SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
  3538. ret->generate_session_id = s->generate_session_id;
  3539. SSL_set_info_callback(ret, SSL_get_info_callback(s));
  3540. /* copy app data, a little dangerous perhaps */
  3541. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  3542. goto err;
  3543. ret->server = s->server;
  3544. if (s->handshake_func) {
  3545. if (s->server)
  3546. SSL_set_accept_state(ret);
  3547. else
  3548. SSL_set_connect_state(ret);
  3549. }
  3550. ret->shutdown = s->shutdown;
  3551. ret->hit = s->hit;
  3552. ret->default_passwd_callback = s->default_passwd_callback;
  3553. ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
  3554. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  3555. /* dup the cipher_list and cipher_list_by_id stacks */
  3556. if (s->cipher_list != NULL) {
  3557. if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  3558. goto err;
  3559. }
  3560. if (s->cipher_list_by_id != NULL)
  3561. if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  3562. == NULL)
  3563. goto err;
  3564. /* Dup the client_CA list */
  3565. if (!dup_ca_names(&ret->ca_names, s->ca_names)
  3566. || !dup_ca_names(&ret->client_ca_names, s->client_ca_names))
  3567. goto err;
  3568. return ret;
  3569. err:
  3570. SSL_free(ret);
  3571. return NULL;
  3572. }
  3573. void ssl_clear_cipher_ctx(SSL *s)
  3574. {
  3575. if (s->enc_read_ctx != NULL) {
  3576. EVP_CIPHER_CTX_free(s->enc_read_ctx);
  3577. s->enc_read_ctx = NULL;
  3578. }
  3579. if (s->enc_write_ctx != NULL) {
  3580. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  3581. s->enc_write_ctx = NULL;
  3582. }
  3583. #ifndef OPENSSL_NO_COMP
  3584. COMP_CTX_free(s->expand);
  3585. s->expand = NULL;
  3586. COMP_CTX_free(s->compress);
  3587. s->compress = NULL;
  3588. #endif
  3589. }
  3590. X509 *SSL_get_certificate(const SSL *s)
  3591. {
  3592. if (s->cert != NULL)
  3593. return s->cert->key->x509;
  3594. else
  3595. return NULL;
  3596. }
  3597. EVP_PKEY *SSL_get_privatekey(const SSL *s)
  3598. {
  3599. if (s->cert != NULL)
  3600. return s->cert->key->privatekey;
  3601. else
  3602. return NULL;
  3603. }
  3604. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
  3605. {
  3606. if (ctx->cert != NULL)
  3607. return ctx->cert->key->x509;
  3608. else
  3609. return NULL;
  3610. }
  3611. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
  3612. {
  3613. if (ctx->cert != NULL)
  3614. return ctx->cert->key->privatekey;
  3615. else
  3616. return NULL;
  3617. }
  3618. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  3619. {
  3620. if ((s->session != NULL) && (s->session->cipher != NULL))
  3621. return s->session->cipher;
  3622. return NULL;
  3623. }
  3624. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
  3625. {
  3626. return s->s3.tmp.new_cipher;
  3627. }
  3628. const COMP_METHOD *SSL_get_current_compression(const SSL *s)
  3629. {
  3630. #ifndef OPENSSL_NO_COMP
  3631. return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
  3632. #else
  3633. return NULL;
  3634. #endif
  3635. }
  3636. const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
  3637. {
  3638. #ifndef OPENSSL_NO_COMP
  3639. return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
  3640. #else
  3641. return NULL;
  3642. #endif
  3643. }
  3644. int ssl_init_wbio_buffer(SSL *s)
  3645. {
  3646. BIO *bbio;
  3647. if (s->bbio != NULL) {
  3648. /* Already buffered. */
  3649. return 1;
  3650. }
  3651. bbio = BIO_new(BIO_f_buffer());
  3652. if (bbio == NULL || !BIO_set_read_buffer_size(bbio, 1)) {
  3653. BIO_free(bbio);
  3654. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  3655. return 0;
  3656. }
  3657. s->bbio = bbio;
  3658. s->wbio = BIO_push(bbio, s->wbio);
  3659. return 1;
  3660. }
  3661. int ssl_free_wbio_buffer(SSL *s)
  3662. {
  3663. /* callers ensure s is never null */
  3664. if (s->bbio == NULL)
  3665. return 1;
  3666. s->wbio = BIO_pop(s->wbio);
  3667. BIO_free(s->bbio);
  3668. s->bbio = NULL;
  3669. return 1;
  3670. }
  3671. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
  3672. {
  3673. ctx->quiet_shutdown = mode;
  3674. }
  3675. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  3676. {
  3677. return ctx->quiet_shutdown;
  3678. }
  3679. void SSL_set_quiet_shutdown(SSL *s, int mode)
  3680. {
  3681. s->quiet_shutdown = mode;
  3682. }
  3683. int SSL_get_quiet_shutdown(const SSL *s)
  3684. {
  3685. return s->quiet_shutdown;
  3686. }
  3687. void SSL_set_shutdown(SSL *s, int mode)
  3688. {
  3689. s->shutdown = mode;
  3690. }
  3691. int SSL_get_shutdown(const SSL *s)
  3692. {
  3693. return s->shutdown;
  3694. }
  3695. int SSL_version(const SSL *s)
  3696. {
  3697. return s->version;
  3698. }
  3699. int SSL_client_version(const SSL *s)
  3700. {
  3701. return s->client_version;
  3702. }
  3703. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  3704. {
  3705. return ssl->ctx;
  3706. }
  3707. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
  3708. {
  3709. CERT *new_cert;
  3710. if (ssl->ctx == ctx)
  3711. return ssl->ctx;
  3712. if (ctx == NULL)
  3713. ctx = ssl->session_ctx;
  3714. new_cert = ssl_cert_dup(ctx->cert);
  3715. if (new_cert == NULL) {
  3716. return NULL;
  3717. }
  3718. if (!custom_exts_copy_flags(&new_cert->custext, &ssl->cert->custext)) {
  3719. ssl_cert_free(new_cert);
  3720. return NULL;
  3721. }
  3722. ssl_cert_free(ssl->cert);
  3723. ssl->cert = new_cert;
  3724. /*
  3725. * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
  3726. * so setter APIs must prevent invalid lengths from entering the system.
  3727. */
  3728. if (!ossl_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)))
  3729. return NULL;
  3730. /*
  3731. * If the session ID context matches that of the parent SSL_CTX,
  3732. * inherit it from the new SSL_CTX as well. If however the context does
  3733. * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
  3734. * leave it unchanged.
  3735. */
  3736. if ((ssl->ctx != NULL) &&
  3737. (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
  3738. (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
  3739. ssl->sid_ctx_length = ctx->sid_ctx_length;
  3740. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  3741. }
  3742. SSL_CTX_up_ref(ctx);
  3743. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  3744. ssl->ctx = ctx;
  3745. return ssl->ctx;
  3746. }
  3747. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  3748. {
  3749. return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
  3750. ctx->propq);
  3751. }
  3752. int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
  3753. {
  3754. X509_LOOKUP *lookup;
  3755. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
  3756. if (lookup == NULL)
  3757. return 0;
  3758. /* We ignore errors, in case the directory doesn't exist */
  3759. ERR_set_mark();
  3760. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  3761. ERR_pop_to_mark();
  3762. return 1;
  3763. }
  3764. int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
  3765. {
  3766. X509_LOOKUP *lookup;
  3767. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
  3768. if (lookup == NULL)
  3769. return 0;
  3770. /* We ignore errors, in case the file doesn't exist */
  3771. ERR_set_mark();
  3772. X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
  3773. ctx->propq);
  3774. ERR_pop_to_mark();
  3775. return 1;
  3776. }
  3777. int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
  3778. {
  3779. X509_LOOKUP *lookup;
  3780. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
  3781. if (lookup == NULL)
  3782. return 0;
  3783. /* We ignore errors, in case the directory doesn't exist */
  3784. ERR_set_mark();
  3785. X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
  3786. ERR_pop_to_mark();
  3787. return 1;
  3788. }
  3789. int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
  3790. {
  3791. return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
  3792. ctx->propq);
  3793. }
  3794. int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
  3795. {
  3796. return X509_STORE_load_path(ctx->cert_store, CApath);
  3797. }
  3798. int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
  3799. {
  3800. return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
  3801. ctx->propq);
  3802. }
  3803. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  3804. const char *CApath)
  3805. {
  3806. if (CAfile == NULL && CApath == NULL)
  3807. return 0;
  3808. if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
  3809. return 0;
  3810. if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
  3811. return 0;
  3812. return 1;
  3813. }
  3814. void SSL_set_info_callback(SSL *ssl,
  3815. void (*cb) (const SSL *ssl, int type, int val))
  3816. {
  3817. ssl->info_callback = cb;
  3818. }
  3819. /*
  3820. * One compiler (Diab DCC) doesn't like argument names in returned function
  3821. * pointer.
  3822. */
  3823. void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
  3824. int /* type */ ,
  3825. int /* val */ ) {
  3826. return ssl->info_callback;
  3827. }
  3828. void SSL_set_verify_result(SSL *ssl, long arg)
  3829. {
  3830. ssl->verify_result = arg;
  3831. }
  3832. long SSL_get_verify_result(const SSL *ssl)
  3833. {
  3834. return ssl->verify_result;
  3835. }
  3836. size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3837. {
  3838. if (outlen == 0)
  3839. return sizeof(ssl->s3.client_random);
  3840. if (outlen > sizeof(ssl->s3.client_random))
  3841. outlen = sizeof(ssl->s3.client_random);
  3842. memcpy(out, ssl->s3.client_random, outlen);
  3843. return outlen;
  3844. }
  3845. size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3846. {
  3847. if (outlen == 0)
  3848. return sizeof(ssl->s3.server_random);
  3849. if (outlen > sizeof(ssl->s3.server_random))
  3850. outlen = sizeof(ssl->s3.server_random);
  3851. memcpy(out, ssl->s3.server_random, outlen);
  3852. return outlen;
  3853. }
  3854. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
  3855. unsigned char *out, size_t outlen)
  3856. {
  3857. if (outlen == 0)
  3858. return session->master_key_length;
  3859. if (outlen > session->master_key_length)
  3860. outlen = session->master_key_length;
  3861. memcpy(out, session->master_key, outlen);
  3862. return outlen;
  3863. }
  3864. int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
  3865. size_t len)
  3866. {
  3867. if (len > sizeof(sess->master_key))
  3868. return 0;
  3869. memcpy(sess->master_key, in, len);
  3870. sess->master_key_length = len;
  3871. return 1;
  3872. }
  3873. int SSL_set_ex_data(SSL *s, int idx, void *arg)
  3874. {
  3875. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3876. }
  3877. void *SSL_get_ex_data(const SSL *s, int idx)
  3878. {
  3879. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3880. }
  3881. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
  3882. {
  3883. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3884. }
  3885. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
  3886. {
  3887. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3888. }
  3889. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  3890. {
  3891. return ctx->cert_store;
  3892. }
  3893. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3894. {
  3895. X509_STORE_free(ctx->cert_store);
  3896. ctx->cert_store = store;
  3897. }
  3898. void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3899. {
  3900. if (store != NULL)
  3901. X509_STORE_up_ref(store);
  3902. SSL_CTX_set_cert_store(ctx, store);
  3903. }
  3904. int SSL_want(const SSL *s)
  3905. {
  3906. return s->rwstate;
  3907. }
  3908. #ifndef OPENSSL_NO_PSK
  3909. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  3910. {
  3911. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3912. ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  3913. return 0;
  3914. }
  3915. OPENSSL_free(ctx->cert->psk_identity_hint);
  3916. if (identity_hint != NULL) {
  3917. ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3918. if (ctx->cert->psk_identity_hint == NULL)
  3919. return 0;
  3920. } else
  3921. ctx->cert->psk_identity_hint = NULL;
  3922. return 1;
  3923. }
  3924. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  3925. {
  3926. if (s == NULL)
  3927. return 0;
  3928. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3929. ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  3930. return 0;
  3931. }
  3932. OPENSSL_free(s->cert->psk_identity_hint);
  3933. if (identity_hint != NULL) {
  3934. s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3935. if (s->cert->psk_identity_hint == NULL)
  3936. return 0;
  3937. } else
  3938. s->cert->psk_identity_hint = NULL;
  3939. return 1;
  3940. }
  3941. const char *SSL_get_psk_identity_hint(const SSL *s)
  3942. {
  3943. if (s == NULL || s->session == NULL)
  3944. return NULL;
  3945. return s->session->psk_identity_hint;
  3946. }
  3947. const char *SSL_get_psk_identity(const SSL *s)
  3948. {
  3949. if (s == NULL || s->session == NULL)
  3950. return NULL;
  3951. return s->session->psk_identity;
  3952. }
  3953. void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
  3954. {
  3955. s->psk_client_callback = cb;
  3956. }
  3957. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
  3958. {
  3959. ctx->psk_client_callback = cb;
  3960. }
  3961. void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
  3962. {
  3963. s->psk_server_callback = cb;
  3964. }
  3965. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
  3966. {
  3967. ctx->psk_server_callback = cb;
  3968. }
  3969. #endif
  3970. void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
  3971. {
  3972. s->psk_find_session_cb = cb;
  3973. }
  3974. void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
  3975. SSL_psk_find_session_cb_func cb)
  3976. {
  3977. ctx->psk_find_session_cb = cb;
  3978. }
  3979. void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
  3980. {
  3981. s->psk_use_session_cb = cb;
  3982. }
  3983. void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
  3984. SSL_psk_use_session_cb_func cb)
  3985. {
  3986. ctx->psk_use_session_cb = cb;
  3987. }
  3988. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  3989. void (*cb) (int write_p, int version,
  3990. int content_type, const void *buf,
  3991. size_t len, SSL *ssl, void *arg))
  3992. {
  3993. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3994. }
  3995. void SSL_set_msg_callback(SSL *ssl,
  3996. void (*cb) (int write_p, int version,
  3997. int content_type, const void *buf,
  3998. size_t len, SSL *ssl, void *arg))
  3999. {
  4000. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  4001. }
  4002. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  4003. int (*cb) (SSL *ssl,
  4004. int
  4005. is_forward_secure))
  4006. {
  4007. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  4008. (void (*)(void))cb);
  4009. }
  4010. void SSL_set_not_resumable_session_callback(SSL *ssl,
  4011. int (*cb) (SSL *ssl,
  4012. int is_forward_secure))
  4013. {
  4014. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  4015. (void (*)(void))cb);
  4016. }
  4017. void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
  4018. size_t (*cb) (SSL *ssl, int type,
  4019. size_t len, void *arg))
  4020. {
  4021. ctx->record_padding_cb = cb;
  4022. }
  4023. void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
  4024. {
  4025. ctx->record_padding_arg = arg;
  4026. }
  4027. void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
  4028. {
  4029. return ctx->record_padding_arg;
  4030. }
  4031. int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
  4032. {
  4033. /* block size of 0 or 1 is basically no padding */
  4034. if (block_size == 1)
  4035. ctx->block_padding = 0;
  4036. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  4037. ctx->block_padding = block_size;
  4038. else
  4039. return 0;
  4040. return 1;
  4041. }
  4042. int SSL_set_record_padding_callback(SSL *ssl,
  4043. size_t (*cb) (SSL *ssl, int type,
  4044. size_t len, void *arg))
  4045. {
  4046. BIO *b;
  4047. b = SSL_get_wbio(ssl);
  4048. if (b == NULL || !BIO_get_ktls_send(b)) {
  4049. ssl->record_padding_cb = cb;
  4050. return 1;
  4051. }
  4052. return 0;
  4053. }
  4054. void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
  4055. {
  4056. ssl->record_padding_arg = arg;
  4057. }
  4058. void *SSL_get_record_padding_callback_arg(const SSL *ssl)
  4059. {
  4060. return ssl->record_padding_arg;
  4061. }
  4062. int SSL_set_block_padding(SSL *ssl, size_t block_size)
  4063. {
  4064. /* block size of 0 or 1 is basically no padding */
  4065. if (block_size == 1)
  4066. ssl->block_padding = 0;
  4067. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  4068. ssl->block_padding = block_size;
  4069. else
  4070. return 0;
  4071. return 1;
  4072. }
  4073. int SSL_set_num_tickets(SSL *s, size_t num_tickets)
  4074. {
  4075. s->num_tickets = num_tickets;
  4076. return 1;
  4077. }
  4078. size_t SSL_get_num_tickets(const SSL *s)
  4079. {
  4080. return s->num_tickets;
  4081. }
  4082. int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
  4083. {
  4084. ctx->num_tickets = num_tickets;
  4085. return 1;
  4086. }
  4087. size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
  4088. {
  4089. return ctx->num_tickets;
  4090. }
  4091. /*
  4092. * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  4093. * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
  4094. * If EVP_MD pointer is passed, initializes ctx with this |md|.
  4095. * Returns the newly allocated ctx;
  4096. */
  4097. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
  4098. {
  4099. ssl_clear_hash_ctx(hash);
  4100. *hash = EVP_MD_CTX_new();
  4101. if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
  4102. EVP_MD_CTX_free(*hash);
  4103. *hash = NULL;
  4104. return NULL;
  4105. }
  4106. return *hash;
  4107. }
  4108. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  4109. {
  4110. EVP_MD_CTX_free(*hash);
  4111. *hash = NULL;
  4112. }
  4113. /* Retrieve handshake hashes */
  4114. int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
  4115. size_t *hashlen)
  4116. {
  4117. EVP_MD_CTX *ctx = NULL;
  4118. EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
  4119. int hashleni = EVP_MD_CTX_get_size(hdgst);
  4120. int ret = 0;
  4121. if (hashleni < 0 || (size_t)hashleni > outlen) {
  4122. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4123. goto err;
  4124. }
  4125. ctx = EVP_MD_CTX_new();
  4126. if (ctx == NULL) {
  4127. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4128. goto err;
  4129. }
  4130. if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
  4131. || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
  4132. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4133. goto err;
  4134. }
  4135. *hashlen = hashleni;
  4136. ret = 1;
  4137. err:
  4138. EVP_MD_CTX_free(ctx);
  4139. return ret;
  4140. }
  4141. int SSL_session_reused(const SSL *s)
  4142. {
  4143. return s->hit;
  4144. }
  4145. int SSL_is_server(const SSL *s)
  4146. {
  4147. return s->server;
  4148. }
  4149. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  4150. void SSL_set_debug(SSL *s, int debug)
  4151. {
  4152. /* Old function was do-nothing anyway... */
  4153. (void)s;
  4154. (void)debug;
  4155. }
  4156. #endif
  4157. void SSL_set_security_level(SSL *s, int level)
  4158. {
  4159. s->cert->sec_level = level;
  4160. }
  4161. int SSL_get_security_level(const SSL *s)
  4162. {
  4163. return s->cert->sec_level;
  4164. }
  4165. void SSL_set_security_callback(SSL *s,
  4166. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4167. int op, int bits, int nid,
  4168. void *other, void *ex))
  4169. {
  4170. s->cert->sec_cb = cb;
  4171. }
  4172. int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
  4173. const SSL_CTX *ctx, int op,
  4174. int bits, int nid, void *other,
  4175. void *ex) {
  4176. return s->cert->sec_cb;
  4177. }
  4178. void SSL_set0_security_ex_data(SSL *s, void *ex)
  4179. {
  4180. s->cert->sec_ex = ex;
  4181. }
  4182. void *SSL_get0_security_ex_data(const SSL *s)
  4183. {
  4184. return s->cert->sec_ex;
  4185. }
  4186. void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
  4187. {
  4188. ctx->cert->sec_level = level;
  4189. }
  4190. int SSL_CTX_get_security_level(const SSL_CTX *ctx)
  4191. {
  4192. return ctx->cert->sec_level;
  4193. }
  4194. void SSL_CTX_set_security_callback(SSL_CTX *ctx,
  4195. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4196. int op, int bits, int nid,
  4197. void *other, void *ex))
  4198. {
  4199. ctx->cert->sec_cb = cb;
  4200. }
  4201. int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
  4202. const SSL_CTX *ctx,
  4203. int op, int bits,
  4204. int nid,
  4205. void *other,
  4206. void *ex) {
  4207. return ctx->cert->sec_cb;
  4208. }
  4209. void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
  4210. {
  4211. ctx->cert->sec_ex = ex;
  4212. }
  4213. void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
  4214. {
  4215. return ctx->cert->sec_ex;
  4216. }
  4217. uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
  4218. {
  4219. return ctx->options;
  4220. }
  4221. uint64_t SSL_get_options(const SSL *s)
  4222. {
  4223. return s->options;
  4224. }
  4225. uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
  4226. {
  4227. return ctx->options |= op;
  4228. }
  4229. uint64_t SSL_set_options(SSL *s, uint64_t op)
  4230. {
  4231. return s->options |= op;
  4232. }
  4233. uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
  4234. {
  4235. return ctx->options &= ~op;
  4236. }
  4237. uint64_t SSL_clear_options(SSL *s, uint64_t op)
  4238. {
  4239. return s->options &= ~op;
  4240. }
  4241. STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
  4242. {
  4243. return s->verified_chain;
  4244. }
  4245. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
  4246. #ifndef OPENSSL_NO_CT
  4247. /*
  4248. * Moves SCTs from the |src| stack to the |dst| stack.
  4249. * The source of each SCT will be set to |origin|.
  4250. * If |dst| points to a NULL pointer, a new stack will be created and owned by
  4251. * the caller.
  4252. * Returns the number of SCTs moved, or a negative integer if an error occurs.
  4253. */
  4254. static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
  4255. sct_source_t origin)
  4256. {
  4257. int scts_moved = 0;
  4258. SCT *sct = NULL;
  4259. if (*dst == NULL) {
  4260. *dst = sk_SCT_new_null();
  4261. if (*dst == NULL) {
  4262. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4263. goto err;
  4264. }
  4265. }
  4266. while ((sct = sk_SCT_pop(src)) != NULL) {
  4267. if (SCT_set_source(sct, origin) != 1)
  4268. goto err;
  4269. if (sk_SCT_push(*dst, sct) <= 0)
  4270. goto err;
  4271. scts_moved += 1;
  4272. }
  4273. return scts_moved;
  4274. err:
  4275. if (sct != NULL)
  4276. sk_SCT_push(src, sct); /* Put the SCT back */
  4277. return -1;
  4278. }
  4279. /*
  4280. * Look for data collected during ServerHello and parse if found.
  4281. * Returns the number of SCTs extracted.
  4282. */
  4283. static int ct_extract_tls_extension_scts(SSL *s)
  4284. {
  4285. int scts_extracted = 0;
  4286. if (s->ext.scts != NULL) {
  4287. const unsigned char *p = s->ext.scts;
  4288. STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
  4289. scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
  4290. SCT_LIST_free(scts);
  4291. }
  4292. return scts_extracted;
  4293. }
  4294. /*
  4295. * Checks for an OCSP response and then attempts to extract any SCTs found if it
  4296. * contains an SCT X509 extension. They will be stored in |s->scts|.
  4297. * Returns:
  4298. * - The number of SCTs extracted, assuming an OCSP response exists.
  4299. * - 0 if no OCSP response exists or it contains no SCTs.
  4300. * - A negative integer if an error occurs.
  4301. */
  4302. static int ct_extract_ocsp_response_scts(SSL *s)
  4303. {
  4304. # ifndef OPENSSL_NO_OCSP
  4305. int scts_extracted = 0;
  4306. const unsigned char *p;
  4307. OCSP_BASICRESP *br = NULL;
  4308. OCSP_RESPONSE *rsp = NULL;
  4309. STACK_OF(SCT) *scts = NULL;
  4310. int i;
  4311. if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
  4312. goto err;
  4313. p = s->ext.ocsp.resp;
  4314. rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
  4315. if (rsp == NULL)
  4316. goto err;
  4317. br = OCSP_response_get1_basic(rsp);
  4318. if (br == NULL)
  4319. goto err;
  4320. for (i = 0; i < OCSP_resp_count(br); ++i) {
  4321. OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
  4322. if (single == NULL)
  4323. continue;
  4324. scts =
  4325. OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
  4326. scts_extracted =
  4327. ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
  4328. if (scts_extracted < 0)
  4329. goto err;
  4330. }
  4331. err:
  4332. SCT_LIST_free(scts);
  4333. OCSP_BASICRESP_free(br);
  4334. OCSP_RESPONSE_free(rsp);
  4335. return scts_extracted;
  4336. # else
  4337. /* Behave as if no OCSP response exists */
  4338. return 0;
  4339. # endif
  4340. }
  4341. /*
  4342. * Attempts to extract SCTs from the peer certificate.
  4343. * Return the number of SCTs extracted, or a negative integer if an error
  4344. * occurs.
  4345. */
  4346. static int ct_extract_x509v3_extension_scts(SSL *s)
  4347. {
  4348. int scts_extracted = 0;
  4349. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4350. if (cert != NULL) {
  4351. STACK_OF(SCT) *scts =
  4352. X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
  4353. scts_extracted =
  4354. ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
  4355. SCT_LIST_free(scts);
  4356. }
  4357. return scts_extracted;
  4358. }
  4359. /*
  4360. * Attempts to find all received SCTs by checking TLS extensions, the OCSP
  4361. * response (if it exists) and X509v3 extensions in the certificate.
  4362. * Returns NULL if an error occurs.
  4363. */
  4364. const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
  4365. {
  4366. if (!s->scts_parsed) {
  4367. if (ct_extract_tls_extension_scts(s) < 0 ||
  4368. ct_extract_ocsp_response_scts(s) < 0 ||
  4369. ct_extract_x509v3_extension_scts(s) < 0)
  4370. goto err;
  4371. s->scts_parsed = 1;
  4372. }
  4373. return s->scts;
  4374. err:
  4375. return NULL;
  4376. }
  4377. static int ct_permissive(const CT_POLICY_EVAL_CTX * ctx,
  4378. const STACK_OF(SCT) *scts, void *unused_arg)
  4379. {
  4380. return 1;
  4381. }
  4382. static int ct_strict(const CT_POLICY_EVAL_CTX * ctx,
  4383. const STACK_OF(SCT) *scts, void *unused_arg)
  4384. {
  4385. int count = scts != NULL ? sk_SCT_num(scts) : 0;
  4386. int i;
  4387. for (i = 0; i < count; ++i) {
  4388. SCT *sct = sk_SCT_value(scts, i);
  4389. int status = SCT_get_validation_status(sct);
  4390. if (status == SCT_VALIDATION_STATUS_VALID)
  4391. return 1;
  4392. }
  4393. ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
  4394. return 0;
  4395. }
  4396. int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
  4397. void *arg)
  4398. {
  4399. /*
  4400. * Since code exists that uses the custom extension handler for CT, look
  4401. * for this and throw an error if they have already registered to use CT.
  4402. */
  4403. if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
  4404. TLSEXT_TYPE_signed_certificate_timestamp))
  4405. {
  4406. ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4407. return 0;
  4408. }
  4409. if (callback != NULL) {
  4410. /*
  4411. * If we are validating CT, then we MUST accept SCTs served via OCSP
  4412. */
  4413. if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
  4414. return 0;
  4415. }
  4416. s->ct_validation_callback = callback;
  4417. s->ct_validation_callback_arg = arg;
  4418. return 1;
  4419. }
  4420. int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
  4421. ssl_ct_validation_cb callback, void *arg)
  4422. {
  4423. /*
  4424. * Since code exists that uses the custom extension handler for CT, look for
  4425. * this and throw an error if they have already registered to use CT.
  4426. */
  4427. if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
  4428. TLSEXT_TYPE_signed_certificate_timestamp))
  4429. {
  4430. ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4431. return 0;
  4432. }
  4433. ctx->ct_validation_callback = callback;
  4434. ctx->ct_validation_callback_arg = arg;
  4435. return 1;
  4436. }
  4437. int SSL_ct_is_enabled(const SSL *s)
  4438. {
  4439. return s->ct_validation_callback != NULL;
  4440. }
  4441. int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
  4442. {
  4443. return ctx->ct_validation_callback != NULL;
  4444. }
  4445. int ssl_validate_ct(SSL *s)
  4446. {
  4447. int ret = 0;
  4448. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4449. X509 *issuer;
  4450. SSL_DANE *dane = &s->dane;
  4451. CT_POLICY_EVAL_CTX *ctx = NULL;
  4452. const STACK_OF(SCT) *scts;
  4453. /*
  4454. * If no callback is set, the peer is anonymous, or its chain is invalid,
  4455. * skip SCT validation - just return success. Applications that continue
  4456. * handshakes without certificates, with unverified chains, or pinned leaf
  4457. * certificates are outside the scope of the WebPKI and CT.
  4458. *
  4459. * The above exclusions notwithstanding the vast majority of peers will
  4460. * have rather ordinary certificate chains validated by typical
  4461. * applications that perform certificate verification and therefore will
  4462. * process SCTs when enabled.
  4463. */
  4464. if (s->ct_validation_callback == NULL || cert == NULL ||
  4465. s->verify_result != X509_V_OK ||
  4466. s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
  4467. return 1;
  4468. /*
  4469. * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
  4470. * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
  4471. */
  4472. if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
  4473. switch (dane->mtlsa->usage) {
  4474. case DANETLS_USAGE_DANE_TA:
  4475. case DANETLS_USAGE_DANE_EE:
  4476. return 1;
  4477. }
  4478. }
  4479. ctx = CT_POLICY_EVAL_CTX_new_ex(s->ctx->libctx, s->ctx->propq);
  4480. if (ctx == NULL) {
  4481. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4482. goto end;
  4483. }
  4484. issuer = sk_X509_value(s->verified_chain, 1);
  4485. CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
  4486. CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
  4487. CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, s->ctx->ctlog_store);
  4488. CT_POLICY_EVAL_CTX_set_time(
  4489. ctx, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s)) * 1000);
  4490. scts = SSL_get0_peer_scts(s);
  4491. /*
  4492. * This function returns success (> 0) only when all the SCTs are valid, 0
  4493. * when some are invalid, and < 0 on various internal errors (out of
  4494. * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
  4495. * reason to abort the handshake, that decision is up to the callback.
  4496. * Therefore, we error out only in the unexpected case that the return
  4497. * value is negative.
  4498. *
  4499. * XXX: One might well argue that the return value of this function is an
  4500. * unfortunate design choice. Its job is only to determine the validation
  4501. * status of each of the provided SCTs. So long as it correctly separates
  4502. * the wheat from the chaff it should return success. Failure in this case
  4503. * ought to correspond to an inability to carry out its duties.
  4504. */
  4505. if (SCT_LIST_validate(scts, ctx) < 0) {
  4506. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
  4507. goto end;
  4508. }
  4509. ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
  4510. if (ret < 0)
  4511. ret = 0; /* This function returns 0 on failure */
  4512. if (!ret)
  4513. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
  4514. end:
  4515. CT_POLICY_EVAL_CTX_free(ctx);
  4516. /*
  4517. * With SSL_VERIFY_NONE the session may be cached and re-used despite a
  4518. * failure return code here. Also the application may wish the complete
  4519. * the handshake, and then disconnect cleanly at a higher layer, after
  4520. * checking the verification status of the completed connection.
  4521. *
  4522. * We therefore force a certificate verification failure which will be
  4523. * visible via SSL_get_verify_result() and cached as part of any resumed
  4524. * session.
  4525. *
  4526. * Note: the permissive callback is for information gathering only, always
  4527. * returns success, and does not affect verification status. Only the
  4528. * strict callback or a custom application-specified callback can trigger
  4529. * connection failure or record a verification error.
  4530. */
  4531. if (ret <= 0)
  4532. s->verify_result = X509_V_ERR_NO_VALID_SCTS;
  4533. return ret;
  4534. }
  4535. int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
  4536. {
  4537. switch (validation_mode) {
  4538. default:
  4539. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4540. return 0;
  4541. case SSL_CT_VALIDATION_PERMISSIVE:
  4542. return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
  4543. case SSL_CT_VALIDATION_STRICT:
  4544. return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
  4545. }
  4546. }
  4547. int SSL_enable_ct(SSL *s, int validation_mode)
  4548. {
  4549. switch (validation_mode) {
  4550. default:
  4551. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4552. return 0;
  4553. case SSL_CT_VALIDATION_PERMISSIVE:
  4554. return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
  4555. case SSL_CT_VALIDATION_STRICT:
  4556. return SSL_set_ct_validation_callback(s, ct_strict, NULL);
  4557. }
  4558. }
  4559. int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
  4560. {
  4561. return CTLOG_STORE_load_default_file(ctx->ctlog_store);
  4562. }
  4563. int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
  4564. {
  4565. return CTLOG_STORE_load_file(ctx->ctlog_store, path);
  4566. }
  4567. void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE * logs)
  4568. {
  4569. CTLOG_STORE_free(ctx->ctlog_store);
  4570. ctx->ctlog_store = logs;
  4571. }
  4572. const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
  4573. {
  4574. return ctx->ctlog_store;
  4575. }
  4576. #endif /* OPENSSL_NO_CT */
  4577. void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
  4578. void *arg)
  4579. {
  4580. c->client_hello_cb = cb;
  4581. c->client_hello_cb_arg = arg;
  4582. }
  4583. int SSL_client_hello_isv2(SSL *s)
  4584. {
  4585. if (s->clienthello == NULL)
  4586. return 0;
  4587. return s->clienthello->isv2;
  4588. }
  4589. unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
  4590. {
  4591. if (s->clienthello == NULL)
  4592. return 0;
  4593. return s->clienthello->legacy_version;
  4594. }
  4595. size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
  4596. {
  4597. if (s->clienthello == NULL)
  4598. return 0;
  4599. if (out != NULL)
  4600. *out = s->clienthello->random;
  4601. return SSL3_RANDOM_SIZE;
  4602. }
  4603. size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
  4604. {
  4605. if (s->clienthello == NULL)
  4606. return 0;
  4607. if (out != NULL)
  4608. *out = s->clienthello->session_id;
  4609. return s->clienthello->session_id_len;
  4610. }
  4611. size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
  4612. {
  4613. if (s->clienthello == NULL)
  4614. return 0;
  4615. if (out != NULL)
  4616. *out = PACKET_data(&s->clienthello->ciphersuites);
  4617. return PACKET_remaining(&s->clienthello->ciphersuites);
  4618. }
  4619. size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
  4620. {
  4621. if (s->clienthello == NULL)
  4622. return 0;
  4623. if (out != NULL)
  4624. *out = s->clienthello->compressions;
  4625. return s->clienthello->compressions_len;
  4626. }
  4627. int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
  4628. {
  4629. RAW_EXTENSION *ext;
  4630. int *present;
  4631. size_t num = 0, i;
  4632. if (s->clienthello == NULL || out == NULL || outlen == NULL)
  4633. return 0;
  4634. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4635. ext = s->clienthello->pre_proc_exts + i;
  4636. if (ext->present)
  4637. num++;
  4638. }
  4639. if (num == 0) {
  4640. *out = NULL;
  4641. *outlen = 0;
  4642. return 1;
  4643. }
  4644. if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) {
  4645. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4646. return 0;
  4647. }
  4648. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4649. ext = s->clienthello->pre_proc_exts + i;
  4650. if (ext->present) {
  4651. if (ext->received_order >= num)
  4652. goto err;
  4653. present[ext->received_order] = ext->type;
  4654. }
  4655. }
  4656. *out = present;
  4657. *outlen = num;
  4658. return 1;
  4659. err:
  4660. OPENSSL_free(present);
  4661. return 0;
  4662. }
  4663. int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts, size_t *num_exts)
  4664. {
  4665. RAW_EXTENSION *ext;
  4666. size_t num = 0, i;
  4667. if (s->clienthello == NULL || num_exts == NULL)
  4668. return 0;
  4669. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4670. ext = s->clienthello->pre_proc_exts + i;
  4671. if (ext->present)
  4672. num++;
  4673. }
  4674. if (num == 0) {
  4675. *num_exts = 0;
  4676. return 1;
  4677. }
  4678. if (exts == NULL) {
  4679. *num_exts = num;
  4680. return 1;
  4681. }
  4682. if (*num_exts < num)
  4683. return 0;
  4684. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4685. ext = s->clienthello->pre_proc_exts + i;
  4686. if (ext->present) {
  4687. if (ext->received_order >= num)
  4688. return 0;
  4689. exts[ext->received_order] = ext->type;
  4690. }
  4691. }
  4692. *num_exts = num;
  4693. return 1;
  4694. }
  4695. int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
  4696. size_t *outlen)
  4697. {
  4698. size_t i;
  4699. RAW_EXTENSION *r;
  4700. if (s->clienthello == NULL)
  4701. return 0;
  4702. for (i = 0; i < s->clienthello->pre_proc_exts_len; ++i) {
  4703. r = s->clienthello->pre_proc_exts + i;
  4704. if (r->present && r->type == type) {
  4705. if (out != NULL)
  4706. *out = PACKET_data(&r->data);
  4707. if (outlen != NULL)
  4708. *outlen = PACKET_remaining(&r->data);
  4709. return 1;
  4710. }
  4711. }
  4712. return 0;
  4713. }
  4714. int SSL_free_buffers(SSL *ssl)
  4715. {
  4716. RECORD_LAYER *rl = &ssl->rlayer;
  4717. if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl))
  4718. return 0;
  4719. RECORD_LAYER_release(rl);
  4720. return 1;
  4721. }
  4722. int SSL_alloc_buffers(SSL *ssl)
  4723. {
  4724. return ssl3_setup_buffers(ssl);
  4725. }
  4726. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
  4727. {
  4728. ctx->keylog_callback = cb;
  4729. }
  4730. SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
  4731. {
  4732. return ctx->keylog_callback;
  4733. }
  4734. static int nss_keylog_int(const char *prefix,
  4735. SSL *ssl,
  4736. const uint8_t *parameter_1,
  4737. size_t parameter_1_len,
  4738. const uint8_t *parameter_2,
  4739. size_t parameter_2_len)
  4740. {
  4741. char *out = NULL;
  4742. char *cursor = NULL;
  4743. size_t out_len = 0;
  4744. size_t i;
  4745. size_t prefix_len;
  4746. if (ssl->ctx->keylog_callback == NULL)
  4747. return 1;
  4748. /*
  4749. * Our output buffer will contain the following strings, rendered with
  4750. * space characters in between, terminated by a NULL character: first the
  4751. * prefix, then the first parameter, then the second parameter. The
  4752. * meaning of each parameter depends on the specific key material being
  4753. * logged. Note that the first and second parameters are encoded in
  4754. * hexadecimal, so we need a buffer that is twice their lengths.
  4755. */
  4756. prefix_len = strlen(prefix);
  4757. out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
  4758. if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
  4759. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4760. return 0;
  4761. }
  4762. strcpy(cursor, prefix);
  4763. cursor += prefix_len;
  4764. *cursor++ = ' ';
  4765. for (i = 0; i < parameter_1_len; i++) {
  4766. sprintf(cursor, "%02x", parameter_1[i]);
  4767. cursor += 2;
  4768. }
  4769. *cursor++ = ' ';
  4770. for (i = 0; i < parameter_2_len; i++) {
  4771. sprintf(cursor, "%02x", parameter_2[i]);
  4772. cursor += 2;
  4773. }
  4774. *cursor = '\0';
  4775. ssl->ctx->keylog_callback(ssl, (const char *)out);
  4776. OPENSSL_clear_free(out, out_len);
  4777. return 1;
  4778. }
  4779. int ssl_log_rsa_client_key_exchange(SSL *ssl,
  4780. const uint8_t *encrypted_premaster,
  4781. size_t encrypted_premaster_len,
  4782. const uint8_t *premaster,
  4783. size_t premaster_len)
  4784. {
  4785. if (encrypted_premaster_len < 8) {
  4786. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4787. return 0;
  4788. }
  4789. /* We only want the first 8 bytes of the encrypted premaster as a tag. */
  4790. return nss_keylog_int("RSA",
  4791. ssl,
  4792. encrypted_premaster,
  4793. 8,
  4794. premaster,
  4795. premaster_len);
  4796. }
  4797. int ssl_log_secret(SSL *ssl,
  4798. const char *label,
  4799. const uint8_t *secret,
  4800. size_t secret_len)
  4801. {
  4802. return nss_keylog_int(label,
  4803. ssl,
  4804. ssl->s3.client_random,
  4805. SSL3_RANDOM_SIZE,
  4806. secret,
  4807. secret_len);
  4808. }
  4809. #define SSLV2_CIPHER_LEN 3
  4810. int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format)
  4811. {
  4812. int n;
  4813. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4814. if (PACKET_remaining(cipher_suites) == 0) {
  4815. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
  4816. return 0;
  4817. }
  4818. if (PACKET_remaining(cipher_suites) % n != 0) {
  4819. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4820. return 0;
  4821. }
  4822. OPENSSL_free(s->s3.tmp.ciphers_raw);
  4823. s->s3.tmp.ciphers_raw = NULL;
  4824. s->s3.tmp.ciphers_rawlen = 0;
  4825. if (sslv2format) {
  4826. size_t numciphers = PACKET_remaining(cipher_suites) / n;
  4827. PACKET sslv2ciphers = *cipher_suites;
  4828. unsigned int leadbyte;
  4829. unsigned char *raw;
  4830. /*
  4831. * We store the raw ciphers list in SSLv3+ format so we need to do some
  4832. * preprocessing to convert the list first. If there are any SSLv2 only
  4833. * ciphersuites with a non-zero leading byte then we are going to
  4834. * slightly over allocate because we won't store those. But that isn't a
  4835. * problem.
  4836. */
  4837. raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
  4838. s->s3.tmp.ciphers_raw = raw;
  4839. if (raw == NULL) {
  4840. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4841. return 0;
  4842. }
  4843. for (s->s3.tmp.ciphers_rawlen = 0;
  4844. PACKET_remaining(&sslv2ciphers) > 0;
  4845. raw += TLS_CIPHER_LEN) {
  4846. if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
  4847. || (leadbyte == 0
  4848. && !PACKET_copy_bytes(&sslv2ciphers, raw,
  4849. TLS_CIPHER_LEN))
  4850. || (leadbyte != 0
  4851. && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
  4852. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
  4853. OPENSSL_free(s->s3.tmp.ciphers_raw);
  4854. s->s3.tmp.ciphers_raw = NULL;
  4855. s->s3.tmp.ciphers_rawlen = 0;
  4856. return 0;
  4857. }
  4858. if (leadbyte == 0)
  4859. s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
  4860. }
  4861. } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
  4862. &s->s3.tmp.ciphers_rawlen)) {
  4863. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4864. return 0;
  4865. }
  4866. return 1;
  4867. }
  4868. int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
  4869. int isv2format, STACK_OF(SSL_CIPHER) **sk,
  4870. STACK_OF(SSL_CIPHER) **scsvs)
  4871. {
  4872. PACKET pkt;
  4873. if (!PACKET_buf_init(&pkt, bytes, len))
  4874. return 0;
  4875. return bytes_to_cipher_list(s, &pkt, sk, scsvs, isv2format, 0);
  4876. }
  4877. int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,
  4878. STACK_OF(SSL_CIPHER) **skp,
  4879. STACK_OF(SSL_CIPHER) **scsvs_out,
  4880. int sslv2format, int fatal)
  4881. {
  4882. const SSL_CIPHER *c;
  4883. STACK_OF(SSL_CIPHER) *sk = NULL;
  4884. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  4885. int n;
  4886. /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
  4887. unsigned char cipher[SSLV2_CIPHER_LEN];
  4888. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4889. if (PACKET_remaining(cipher_suites) == 0) {
  4890. if (fatal)
  4891. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
  4892. else
  4893. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
  4894. return 0;
  4895. }
  4896. if (PACKET_remaining(cipher_suites) % n != 0) {
  4897. if (fatal)
  4898. SSLfatal(s, SSL_AD_DECODE_ERROR,
  4899. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4900. else
  4901. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4902. return 0;
  4903. }
  4904. sk = sk_SSL_CIPHER_new_null();
  4905. scsvs = sk_SSL_CIPHER_new_null();
  4906. if (sk == NULL || scsvs == NULL) {
  4907. if (fatal)
  4908. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4909. else
  4910. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4911. goto err;
  4912. }
  4913. while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
  4914. /*
  4915. * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
  4916. * first byte set to zero, while true SSLv2 ciphers have a non-zero
  4917. * first byte. We don't support any true SSLv2 ciphers, so skip them.
  4918. */
  4919. if (sslv2format && cipher[0] != '\0')
  4920. continue;
  4921. /* For SSLv2-compat, ignore leading 0-byte. */
  4922. c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
  4923. if (c != NULL) {
  4924. if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
  4925. (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
  4926. if (fatal)
  4927. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  4928. else
  4929. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  4930. goto err;
  4931. }
  4932. }
  4933. }
  4934. if (PACKET_remaining(cipher_suites) > 0) {
  4935. if (fatal)
  4936. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  4937. else
  4938. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  4939. goto err;
  4940. }
  4941. if (skp != NULL)
  4942. *skp = sk;
  4943. else
  4944. sk_SSL_CIPHER_free(sk);
  4945. if (scsvs_out != NULL)
  4946. *scsvs_out = scsvs;
  4947. else
  4948. sk_SSL_CIPHER_free(scsvs);
  4949. return 1;
  4950. err:
  4951. sk_SSL_CIPHER_free(sk);
  4952. sk_SSL_CIPHER_free(scsvs);
  4953. return 0;
  4954. }
  4955. int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
  4956. {
  4957. ctx->max_early_data = max_early_data;
  4958. return 1;
  4959. }
  4960. uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
  4961. {
  4962. return ctx->max_early_data;
  4963. }
  4964. int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
  4965. {
  4966. s->max_early_data = max_early_data;
  4967. return 1;
  4968. }
  4969. uint32_t SSL_get_max_early_data(const SSL *s)
  4970. {
  4971. return s->max_early_data;
  4972. }
  4973. int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
  4974. {
  4975. ctx->recv_max_early_data = recv_max_early_data;
  4976. return 1;
  4977. }
  4978. uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
  4979. {
  4980. return ctx->recv_max_early_data;
  4981. }
  4982. int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
  4983. {
  4984. s->recv_max_early_data = recv_max_early_data;
  4985. return 1;
  4986. }
  4987. uint32_t SSL_get_recv_max_early_data(const SSL *s)
  4988. {
  4989. return s->recv_max_early_data;
  4990. }
  4991. __owur unsigned int ssl_get_max_send_fragment(const SSL *ssl)
  4992. {
  4993. /* Return any active Max Fragment Len extension */
  4994. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session))
  4995. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4996. /* return current SSL connection setting */
  4997. return ssl->max_send_fragment;
  4998. }
  4999. __owur unsigned int ssl_get_split_send_fragment(const SSL *ssl)
  5000. {
  5001. /* Return a value regarding an active Max Fragment Len extension */
  5002. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session)
  5003. && ssl->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(ssl->session))
  5004. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  5005. /* else limit |split_send_fragment| to current |max_send_fragment| */
  5006. if (ssl->split_send_fragment > ssl->max_send_fragment)
  5007. return ssl->max_send_fragment;
  5008. /* return current SSL connection setting */
  5009. return ssl->split_send_fragment;
  5010. }
  5011. int SSL_stateless(SSL *s)
  5012. {
  5013. int ret;
  5014. /* Ensure there is no state left over from a previous invocation */
  5015. if (!SSL_clear(s))
  5016. return 0;
  5017. ERR_clear_error();
  5018. s->s3.flags |= TLS1_FLAGS_STATELESS;
  5019. ret = SSL_accept(s);
  5020. s->s3.flags &= ~TLS1_FLAGS_STATELESS;
  5021. if (ret > 0 && s->ext.cookieok)
  5022. return 1;
  5023. if (s->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(s))
  5024. return 0;
  5025. return -1;
  5026. }
  5027. void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
  5028. {
  5029. ctx->pha_enabled = val;
  5030. }
  5031. void SSL_set_post_handshake_auth(SSL *ssl, int val)
  5032. {
  5033. ssl->pha_enabled = val;
  5034. }
  5035. int SSL_verify_client_post_handshake(SSL *ssl)
  5036. {
  5037. if (!SSL_IS_TLS13(ssl)) {
  5038. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  5039. return 0;
  5040. }
  5041. if (!ssl->server) {
  5042. ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
  5043. return 0;
  5044. }
  5045. if (!SSL_is_init_finished(ssl)) {
  5046. ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
  5047. return 0;
  5048. }
  5049. switch (ssl->post_handshake_auth) {
  5050. case SSL_PHA_NONE:
  5051. ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
  5052. return 0;
  5053. default:
  5054. case SSL_PHA_EXT_SENT:
  5055. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  5056. return 0;
  5057. case SSL_PHA_EXT_RECEIVED:
  5058. break;
  5059. case SSL_PHA_REQUEST_PENDING:
  5060. ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
  5061. return 0;
  5062. case SSL_PHA_REQUESTED:
  5063. ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
  5064. return 0;
  5065. }
  5066. ssl->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
  5067. /* checks verify_mode and algorithm_auth */
  5068. if (!send_certificate_request(ssl)) {
  5069. ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
  5070. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
  5071. return 0;
  5072. }
  5073. ossl_statem_set_in_init(ssl, 1);
  5074. return 1;
  5075. }
  5076. int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
  5077. SSL_CTX_generate_session_ticket_fn gen_cb,
  5078. SSL_CTX_decrypt_session_ticket_fn dec_cb,
  5079. void *arg)
  5080. {
  5081. ctx->generate_ticket_cb = gen_cb;
  5082. ctx->decrypt_ticket_cb = dec_cb;
  5083. ctx->ticket_cb_data = arg;
  5084. return 1;
  5085. }
  5086. void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
  5087. SSL_allow_early_data_cb_fn cb,
  5088. void *arg)
  5089. {
  5090. ctx->allow_early_data_cb = cb;
  5091. ctx->allow_early_data_cb_data = arg;
  5092. }
  5093. void SSL_set_allow_early_data_cb(SSL *s,
  5094. SSL_allow_early_data_cb_fn cb,
  5095. void *arg)
  5096. {
  5097. s->allow_early_data_cb = cb;
  5098. s->allow_early_data_cb_data = arg;
  5099. }
  5100. const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
  5101. int nid,
  5102. const char *properties)
  5103. {
  5104. const EVP_CIPHER *ciph;
  5105. ciph = tls_get_cipher_from_engine(nid);
  5106. if (ciph != NULL)
  5107. return ciph;
  5108. /*
  5109. * If there is no engine cipher then we do an explicit fetch. This may fail
  5110. * and that could be ok
  5111. */
  5112. ERR_set_mark();
  5113. ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties);
  5114. ERR_pop_to_mark();
  5115. return ciph;
  5116. }
  5117. int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
  5118. {
  5119. /* Don't up-ref an implicit EVP_CIPHER */
  5120. if (EVP_CIPHER_get0_provider(cipher) == NULL)
  5121. return 1;
  5122. /*
  5123. * The cipher was explicitly fetched and therefore it is safe to cast
  5124. * away the const
  5125. */
  5126. return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
  5127. }
  5128. void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
  5129. {
  5130. if (cipher == NULL)
  5131. return;
  5132. if (EVP_CIPHER_get0_provider(cipher) != NULL) {
  5133. /*
  5134. * The cipher was explicitly fetched and therefore it is safe to cast
  5135. * away the const
  5136. */
  5137. EVP_CIPHER_free((EVP_CIPHER *)cipher);
  5138. }
  5139. }
  5140. const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
  5141. int nid,
  5142. const char *properties)
  5143. {
  5144. const EVP_MD *md;
  5145. md = tls_get_digest_from_engine(nid);
  5146. if (md != NULL)
  5147. return md;
  5148. /* Otherwise we do an explicit fetch */
  5149. ERR_set_mark();
  5150. md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties);
  5151. ERR_pop_to_mark();
  5152. return md;
  5153. }
  5154. int ssl_evp_md_up_ref(const EVP_MD *md)
  5155. {
  5156. /* Don't up-ref an implicit EVP_MD */
  5157. if (EVP_MD_get0_provider(md) == NULL)
  5158. return 1;
  5159. /*
  5160. * The digest was explicitly fetched and therefore it is safe to cast
  5161. * away the const
  5162. */
  5163. return EVP_MD_up_ref((EVP_MD *)md);
  5164. }
  5165. void ssl_evp_md_free(const EVP_MD *md)
  5166. {
  5167. if (md == NULL)
  5168. return;
  5169. if (EVP_MD_get0_provider(md) != NULL) {
  5170. /*
  5171. * The digest was explicitly fetched and therefore it is safe to cast
  5172. * away the const
  5173. */
  5174. EVP_MD_free((EVP_MD *)md);
  5175. }
  5176. }
  5177. int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
  5178. {
  5179. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  5180. EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
  5181. ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
  5182. return 0;
  5183. }
  5184. EVP_PKEY_free(s->cert->dh_tmp);
  5185. s->cert->dh_tmp = dhpkey;
  5186. return 1;
  5187. }
  5188. int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
  5189. {
  5190. if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
  5191. EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
  5192. ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
  5193. return 0;
  5194. }
  5195. EVP_PKEY_free(ctx->cert->dh_tmp);
  5196. ctx->cert->dh_tmp = dhpkey;
  5197. return 1;
  5198. }