2
0

ssl_lib.c 188 KB

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