ssl_lib.c 210 KB

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