ssl_lib.c 209 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824
  1. /*
  2. * Copyright 1995-2024 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. 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 certifiate 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.q);
  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, size_t *written)
  2057. {
  2058. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2059. #ifndef OPENSSL_NO_QUIC
  2060. if (IS_QUIC(s))
  2061. return s->method->ssl_write(s, buf, num, written);
  2062. #endif
  2063. if (sc == NULL)
  2064. return 0;
  2065. if (sc->handshake_func == NULL) {
  2066. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  2067. return -1;
  2068. }
  2069. if (sc->shutdown & SSL_SENT_SHUTDOWN) {
  2070. sc->rwstate = SSL_NOTHING;
  2071. ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  2072. return -1;
  2073. }
  2074. if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  2075. || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
  2076. || sc->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
  2077. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2078. return 0;
  2079. }
  2080. /* If we are a client and haven't sent the Finished we better do that */
  2081. ossl_statem_check_finish_init(sc, 1);
  2082. if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  2083. int ret;
  2084. struct ssl_async_args args;
  2085. args.s = s;
  2086. args.buf = (void *)buf;
  2087. args.num = num;
  2088. args.type = WRITEFUNC;
  2089. args.f.func_write = s->method->ssl_write;
  2090. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  2091. *written = sc->asyncrw;
  2092. return ret;
  2093. } else {
  2094. return s->method->ssl_write(s, buf, num, written);
  2095. }
  2096. }
  2097. ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
  2098. {
  2099. ossl_ssize_t ret;
  2100. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  2101. if (sc == NULL)
  2102. return 0;
  2103. if (sc->handshake_func == NULL) {
  2104. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  2105. return -1;
  2106. }
  2107. if (sc->shutdown & SSL_SENT_SHUTDOWN) {
  2108. sc->rwstate = SSL_NOTHING;
  2109. ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  2110. return -1;
  2111. }
  2112. if (!BIO_get_ktls_send(sc->wbio)) {
  2113. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  2114. return -1;
  2115. }
  2116. /* If we have an alert to send, lets send it */
  2117. if (sc->s3.alert_dispatch > 0) {
  2118. ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
  2119. if (ret <= 0) {
  2120. /* SSLfatal() already called if appropriate */
  2121. return ret;
  2122. }
  2123. /* if it went, fall through and send more stuff */
  2124. }
  2125. sc->rwstate = SSL_WRITING;
  2126. if (BIO_flush(sc->wbio) <= 0) {
  2127. if (!BIO_should_retry(sc->wbio)) {
  2128. sc->rwstate = SSL_NOTHING;
  2129. } else {
  2130. #ifdef EAGAIN
  2131. set_sys_error(EAGAIN);
  2132. #endif
  2133. }
  2134. return -1;
  2135. }
  2136. #ifdef OPENSSL_NO_KTLS
  2137. ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
  2138. "can't call ktls_sendfile(), ktls disabled");
  2139. return -1;
  2140. #else
  2141. ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
  2142. if (ret < 0) {
  2143. #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
  2144. if ((get_last_sys_error() == EAGAIN) ||
  2145. (get_last_sys_error() == EINTR) ||
  2146. (get_last_sys_error() == EBUSY))
  2147. BIO_set_retry_write(sc->wbio);
  2148. else
  2149. #endif
  2150. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  2151. return ret;
  2152. }
  2153. sc->rwstate = SSL_NOTHING;
  2154. return ret;
  2155. #endif
  2156. }
  2157. int SSL_write(SSL *s, const void *buf, int num)
  2158. {
  2159. int ret;
  2160. size_t written;
  2161. if (num < 0) {
  2162. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  2163. return -1;
  2164. }
  2165. ret = ssl_write_internal(s, buf, (size_t)num, &written);
  2166. /*
  2167. * The cast is safe here because ret should be <= INT_MAX because num is
  2168. * <= INT_MAX
  2169. */
  2170. if (ret > 0)
  2171. ret = (int)written;
  2172. return ret;
  2173. }
  2174. int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
  2175. {
  2176. int ret = ssl_write_internal(s, buf, num, written);
  2177. if (ret < 0)
  2178. ret = 0;
  2179. return ret;
  2180. }
  2181. int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
  2182. {
  2183. int ret, early_data_state;
  2184. size_t writtmp;
  2185. uint32_t partialwrite;
  2186. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  2187. /* TODO(QUIC 0RTT): This will need special handling for QUIC */
  2188. if (sc == NULL)
  2189. return 0;
  2190. switch (sc->early_data_state) {
  2191. case SSL_EARLY_DATA_NONE:
  2192. if (sc->server
  2193. || !SSL_in_before(s)
  2194. || ((sc->session == NULL || sc->session->ext.max_early_data == 0)
  2195. && (sc->psk_use_session_cb == NULL))) {
  2196. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2197. return 0;
  2198. }
  2199. /* fall through */
  2200. case SSL_EARLY_DATA_CONNECT_RETRY:
  2201. sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
  2202. ret = SSL_connect(s);
  2203. if (ret <= 0) {
  2204. /* NBIO or error */
  2205. sc->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
  2206. return 0;
  2207. }
  2208. /* fall through */
  2209. case SSL_EARLY_DATA_WRITE_RETRY:
  2210. sc->early_data_state = SSL_EARLY_DATA_WRITING;
  2211. /*
  2212. * We disable partial write for early data because we don't keep track
  2213. * of how many bytes we've written between the SSL_write_ex() call and
  2214. * the flush if the flush needs to be retried)
  2215. */
  2216. partialwrite = sc->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
  2217. sc->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
  2218. ret = SSL_write_ex(s, buf, num, &writtmp);
  2219. sc->mode |= partialwrite;
  2220. if (!ret) {
  2221. sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  2222. return ret;
  2223. }
  2224. sc->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
  2225. /* fall through */
  2226. case SSL_EARLY_DATA_WRITE_FLUSH:
  2227. /* The buffering BIO is still in place so we need to flush it */
  2228. if (statem_flush(sc) != 1)
  2229. return 0;
  2230. *written = num;
  2231. sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  2232. return 1;
  2233. case SSL_EARLY_DATA_FINISHED_READING:
  2234. case SSL_EARLY_DATA_READ_RETRY:
  2235. early_data_state = sc->early_data_state;
  2236. /* We are a server writing to an unauthenticated client */
  2237. sc->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
  2238. ret = SSL_write_ex(s, buf, num, written);
  2239. /* The buffering BIO is still in place */
  2240. if (ret)
  2241. (void)BIO_flush(sc->wbio);
  2242. sc->early_data_state = early_data_state;
  2243. return ret;
  2244. default:
  2245. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2246. return 0;
  2247. }
  2248. }
  2249. int SSL_shutdown(SSL *s)
  2250. {
  2251. /*
  2252. * Note that this function behaves differently from what one might
  2253. * expect. Return values are 0 for no success (yet), 1 for success; but
  2254. * calling it once is usually not enough, even if blocking I/O is used
  2255. * (see ssl3_shutdown).
  2256. */
  2257. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2258. #ifndef OPENSSL_NO_QUIC
  2259. if (IS_QUIC(s))
  2260. return ossl_quic_conn_shutdown(s, 0, NULL, 0);
  2261. #endif
  2262. if (sc == NULL)
  2263. return -1;
  2264. if (sc->handshake_func == NULL) {
  2265. ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
  2266. return -1;
  2267. }
  2268. if (!SSL_in_init(s)) {
  2269. if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  2270. struct ssl_async_args args;
  2271. memset(&args, 0, sizeof(args));
  2272. args.s = s;
  2273. args.type = OTHERFUNC;
  2274. args.f.func_other = s->method->ssl_shutdown;
  2275. return ssl_start_async_job(s, &args, ssl_io_intern);
  2276. } else {
  2277. return s->method->ssl_shutdown(s);
  2278. }
  2279. } else {
  2280. ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  2281. return -1;
  2282. }
  2283. }
  2284. int SSL_key_update(SSL *s, int updatetype)
  2285. {
  2286. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2287. #ifndef OPENSSL_NO_QUIC
  2288. if (IS_QUIC(s))
  2289. return ossl_quic_key_update(s, updatetype);
  2290. #endif
  2291. if (sc == NULL)
  2292. return 0;
  2293. if (!SSL_CONNECTION_IS_TLS13(sc)) {
  2294. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  2295. return 0;
  2296. }
  2297. if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
  2298. && updatetype != SSL_KEY_UPDATE_REQUESTED) {
  2299. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
  2300. return 0;
  2301. }
  2302. if (!SSL_is_init_finished(s)) {
  2303. ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
  2304. return 0;
  2305. }
  2306. if (RECORD_LAYER_write_pending(&sc->rlayer)) {
  2307. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY);
  2308. return 0;
  2309. }
  2310. ossl_statem_set_in_init(sc, 1);
  2311. sc->key_update = updatetype;
  2312. return 1;
  2313. }
  2314. int SSL_get_key_update_type(const SSL *s)
  2315. {
  2316. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  2317. #ifndef OPENSSL_NO_QUIC
  2318. if (IS_QUIC(s))
  2319. return ossl_quic_get_key_update_type(s);
  2320. #endif
  2321. if (sc == NULL)
  2322. return 0;
  2323. return sc->key_update;
  2324. }
  2325. /*
  2326. * Can we accept a renegotiation request? If yes, set the flag and
  2327. * return 1 if yes. If not, raise error and return 0.
  2328. */
  2329. static int can_renegotiate(const SSL_CONNECTION *sc)
  2330. {
  2331. if (SSL_CONNECTION_IS_TLS13(sc)) {
  2332. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  2333. return 0;
  2334. }
  2335. if ((sc->options & SSL_OP_NO_RENEGOTIATION) != 0) {
  2336. ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
  2337. return 0;
  2338. }
  2339. return 1;
  2340. }
  2341. int SSL_renegotiate(SSL *s)
  2342. {
  2343. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  2344. if (sc == NULL)
  2345. return 0;
  2346. if (!can_renegotiate(sc))
  2347. return 0;
  2348. sc->renegotiate = 1;
  2349. sc->new_session = 1;
  2350. return s->method->ssl_renegotiate(s);
  2351. }
  2352. int SSL_renegotiate_abbreviated(SSL *s)
  2353. {
  2354. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  2355. if (sc == NULL)
  2356. return 0;
  2357. if (!can_renegotiate(sc))
  2358. return 0;
  2359. sc->renegotiate = 1;
  2360. sc->new_session = 0;
  2361. return s->method->ssl_renegotiate(s);
  2362. }
  2363. int SSL_renegotiate_pending(const SSL *s)
  2364. {
  2365. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  2366. if (sc == NULL)
  2367. return 0;
  2368. /*
  2369. * becomes true when negotiation is requested; false again once a
  2370. * handshake has finished
  2371. */
  2372. return (sc->renegotiate != 0);
  2373. }
  2374. int SSL_new_session_ticket(SSL *s)
  2375. {
  2376. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2377. if (sc == NULL)
  2378. return 0;
  2379. /* If we are in init because we're sending tickets, okay to send more. */
  2380. if ((SSL_in_init(s) && sc->ext.extra_tickets_expected == 0)
  2381. || SSL_IS_FIRST_HANDSHAKE(sc) || !sc->server
  2382. || !SSL_CONNECTION_IS_TLS13(sc))
  2383. return 0;
  2384. sc->ext.extra_tickets_expected++;
  2385. if (!RECORD_LAYER_write_pending(&sc->rlayer) && !SSL_in_init(s))
  2386. ossl_statem_set_in_init(sc, 1);
  2387. return 1;
  2388. }
  2389. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
  2390. {
  2391. return ossl_ctrl_internal(s, cmd, larg, parg, /*no_quic=*/0);
  2392. }
  2393. long ossl_ctrl_internal(SSL *s, int cmd, long larg, void *parg, int no_quic)
  2394. {
  2395. long l;
  2396. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2397. /*
  2398. * Routing of ctrl calls for QUIC is a little counterintuitive:
  2399. *
  2400. * - Firstly (no_quic=0), we pass the ctrl directly to our QUIC
  2401. * implementation in case it wants to handle the ctrl specially.
  2402. *
  2403. * - If our QUIC implementation does not care about the ctrl, it
  2404. * will reenter this function with no_quic=1 and we will try to handle
  2405. * it directly using the QCSO SSL object stub (not the handshake layer
  2406. * SSL object). This is important for e.g. the version configuration
  2407. * ctrls below, which must use s->defltmeth (and not sc->defltmeth).
  2408. *
  2409. * - If we don't handle a ctrl here specially, then processing is
  2410. * redirected to the handshake layer SSL object.
  2411. */
  2412. if (!no_quic && IS_QUIC(s))
  2413. return s->method->ssl_ctrl(s, cmd, larg, parg);
  2414. if (sc == NULL)
  2415. return 0;
  2416. switch (cmd) {
  2417. case SSL_CTRL_GET_READ_AHEAD:
  2418. return RECORD_LAYER_get_read_ahead(&sc->rlayer);
  2419. case SSL_CTRL_SET_READ_AHEAD:
  2420. l = RECORD_LAYER_get_read_ahead(&sc->rlayer);
  2421. RECORD_LAYER_set_read_ahead(&sc->rlayer, larg);
  2422. return l;
  2423. case SSL_CTRL_MODE:
  2424. {
  2425. OSSL_PARAM options[2], *opts = options;
  2426. sc->mode |= larg;
  2427. *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
  2428. &sc->mode);
  2429. *opts = OSSL_PARAM_construct_end();
  2430. /* Ignore return value */
  2431. sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
  2432. return sc->mode;
  2433. }
  2434. case SSL_CTRL_CLEAR_MODE:
  2435. return (sc->mode &= ~larg);
  2436. case SSL_CTRL_GET_MAX_CERT_LIST:
  2437. return (long)sc->max_cert_list;
  2438. case SSL_CTRL_SET_MAX_CERT_LIST:
  2439. if (larg < 0)
  2440. return 0;
  2441. l = (long)sc->max_cert_list;
  2442. sc->max_cert_list = (size_t)larg;
  2443. return l;
  2444. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2445. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2446. return 0;
  2447. #ifndef OPENSSL_NO_KTLS
  2448. if (sc->wbio != NULL && BIO_get_ktls_send(sc->wbio))
  2449. return 0;
  2450. #endif /* OPENSSL_NO_KTLS */
  2451. sc->max_send_fragment = larg;
  2452. if (sc->max_send_fragment < sc->split_send_fragment)
  2453. sc->split_send_fragment = sc->max_send_fragment;
  2454. sc->rlayer.wrlmethod->set_max_frag_len(sc->rlayer.wrl, larg);
  2455. return 1;
  2456. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2457. if ((size_t)larg > sc->max_send_fragment || larg == 0)
  2458. return 0;
  2459. sc->split_send_fragment = larg;
  2460. return 1;
  2461. case SSL_CTRL_SET_MAX_PIPELINES:
  2462. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2463. return 0;
  2464. sc->max_pipelines = larg;
  2465. if (sc->rlayer.rrlmethod->set_max_pipelines != NULL)
  2466. sc->rlayer.rrlmethod->set_max_pipelines(sc->rlayer.rrl, (size_t)larg);
  2467. return 1;
  2468. case SSL_CTRL_GET_RI_SUPPORT:
  2469. return sc->s3.send_connection_binding;
  2470. case SSL_CTRL_SET_RETRY_VERIFY:
  2471. sc->rwstate = SSL_RETRY_VERIFY;
  2472. return 1;
  2473. case SSL_CTRL_CERT_FLAGS:
  2474. return (sc->cert->cert_flags |= larg);
  2475. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2476. return (sc->cert->cert_flags &= ~larg);
  2477. case SSL_CTRL_GET_RAW_CIPHERLIST:
  2478. if (parg) {
  2479. if (sc->s3.tmp.ciphers_raw == NULL)
  2480. return 0;
  2481. *(unsigned char **)parg = sc->s3.tmp.ciphers_raw;
  2482. return (int)sc->s3.tmp.ciphers_rawlen;
  2483. } else {
  2484. return TLS_CIPHER_LEN;
  2485. }
  2486. case SSL_CTRL_GET_EXTMS_SUPPORT:
  2487. if (!sc->session || SSL_in_init(s) || ossl_statem_get_in_handshake(sc))
  2488. return -1;
  2489. if (sc->session->flags & SSL_SESS_FLAG_EXTMS)
  2490. return 1;
  2491. else
  2492. return 0;
  2493. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2494. return ssl_check_allowed_versions(larg, sc->max_proto_version)
  2495. && ssl_set_version_bound(s->defltmeth->version, (int)larg,
  2496. &sc->min_proto_version);
  2497. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2498. return sc->min_proto_version;
  2499. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2500. return ssl_check_allowed_versions(sc->min_proto_version, larg)
  2501. && ssl_set_version_bound(s->defltmeth->version, (int)larg,
  2502. &sc->max_proto_version);
  2503. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2504. return sc->max_proto_version;
  2505. default:
  2506. if (IS_QUIC(s))
  2507. return SSL_ctrl((SSL *)sc, cmd, larg, parg);
  2508. else
  2509. return s->method->ssl_ctrl(s, cmd, larg, parg);
  2510. }
  2511. }
  2512. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  2513. {
  2514. return s->method->ssl_callback_ctrl(s, cmd, fp);
  2515. }
  2516. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  2517. {
  2518. return ctx->sessions;
  2519. }
  2520. static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat)
  2521. {
  2522. int res = 0;
  2523. if (ssl_tsan_lock(ctx)) {
  2524. res = tsan_load(stat);
  2525. ssl_tsan_unlock(ctx);
  2526. }
  2527. return res;
  2528. }
  2529. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  2530. {
  2531. long l;
  2532. /* For some cases with ctx == NULL perform syntax checks */
  2533. if (ctx == NULL) {
  2534. switch (cmd) {
  2535. case SSL_CTRL_SET_GROUPS_LIST:
  2536. return tls1_set_groups_list(ctx, NULL, NULL, parg);
  2537. case SSL_CTRL_SET_SIGALGS_LIST:
  2538. case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
  2539. return tls1_set_sigalgs_list(ctx, NULL, parg, 0);
  2540. default:
  2541. return 0;
  2542. }
  2543. }
  2544. switch (cmd) {
  2545. case SSL_CTRL_GET_READ_AHEAD:
  2546. return ctx->read_ahead;
  2547. case SSL_CTRL_SET_READ_AHEAD:
  2548. l = ctx->read_ahead;
  2549. ctx->read_ahead = larg;
  2550. return l;
  2551. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  2552. ctx->msg_callback_arg = parg;
  2553. return 1;
  2554. case SSL_CTRL_GET_MAX_CERT_LIST:
  2555. return (long)ctx->max_cert_list;
  2556. case SSL_CTRL_SET_MAX_CERT_LIST:
  2557. if (larg < 0)
  2558. return 0;
  2559. l = (long)ctx->max_cert_list;
  2560. ctx->max_cert_list = (size_t)larg;
  2561. return l;
  2562. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  2563. if (larg < 0)
  2564. return 0;
  2565. l = (long)ctx->session_cache_size;
  2566. ctx->session_cache_size = (size_t)larg;
  2567. return l;
  2568. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  2569. return (long)ctx->session_cache_size;
  2570. case SSL_CTRL_SET_SESS_CACHE_MODE:
  2571. l = ctx->session_cache_mode;
  2572. ctx->session_cache_mode = larg;
  2573. return l;
  2574. case SSL_CTRL_GET_SESS_CACHE_MODE:
  2575. return ctx->session_cache_mode;
  2576. case SSL_CTRL_SESS_NUMBER:
  2577. return lh_SSL_SESSION_num_items(ctx->sessions);
  2578. case SSL_CTRL_SESS_CONNECT:
  2579. return ssl_tsan_load(ctx, &ctx->stats.sess_connect);
  2580. case SSL_CTRL_SESS_CONNECT_GOOD:
  2581. return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good);
  2582. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  2583. return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate);
  2584. case SSL_CTRL_SESS_ACCEPT:
  2585. return ssl_tsan_load(ctx, &ctx->stats.sess_accept);
  2586. case SSL_CTRL_SESS_ACCEPT_GOOD:
  2587. return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good);
  2588. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  2589. return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate);
  2590. case SSL_CTRL_SESS_HIT:
  2591. return ssl_tsan_load(ctx, &ctx->stats.sess_hit);
  2592. case SSL_CTRL_SESS_CB_HIT:
  2593. return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit);
  2594. case SSL_CTRL_SESS_MISSES:
  2595. return ssl_tsan_load(ctx, &ctx->stats.sess_miss);
  2596. case SSL_CTRL_SESS_TIMEOUTS:
  2597. return ssl_tsan_load(ctx, &ctx->stats.sess_timeout);
  2598. case SSL_CTRL_SESS_CACHE_FULL:
  2599. return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full);
  2600. case SSL_CTRL_MODE:
  2601. return (ctx->mode |= larg);
  2602. case SSL_CTRL_CLEAR_MODE:
  2603. return (ctx->mode &= ~larg);
  2604. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2605. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2606. return 0;
  2607. ctx->max_send_fragment = larg;
  2608. if (ctx->max_send_fragment < ctx->split_send_fragment)
  2609. ctx->split_send_fragment = ctx->max_send_fragment;
  2610. return 1;
  2611. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2612. if ((size_t)larg > ctx->max_send_fragment || larg == 0)
  2613. return 0;
  2614. ctx->split_send_fragment = larg;
  2615. return 1;
  2616. case SSL_CTRL_SET_MAX_PIPELINES:
  2617. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2618. return 0;
  2619. ctx->max_pipelines = larg;
  2620. return 1;
  2621. case SSL_CTRL_CERT_FLAGS:
  2622. return (ctx->cert->cert_flags |= larg);
  2623. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2624. return (ctx->cert->cert_flags &= ~larg);
  2625. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2626. return ssl_check_allowed_versions(larg, ctx->max_proto_version)
  2627. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2628. &ctx->min_proto_version);
  2629. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2630. return ctx->min_proto_version;
  2631. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2632. return ssl_check_allowed_versions(ctx->min_proto_version, larg)
  2633. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2634. &ctx->max_proto_version);
  2635. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2636. return ctx->max_proto_version;
  2637. default:
  2638. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  2639. }
  2640. }
  2641. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  2642. {
  2643. switch (cmd) {
  2644. case SSL_CTRL_SET_MSG_CALLBACK:
  2645. ctx->msg_callback = (void (*)
  2646. (int write_p, int version, int content_type,
  2647. const void *buf, size_t len, SSL *ssl,
  2648. void *arg))(fp);
  2649. return 1;
  2650. default:
  2651. return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
  2652. }
  2653. }
  2654. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  2655. {
  2656. if (a->id > b->id)
  2657. return 1;
  2658. if (a->id < b->id)
  2659. return -1;
  2660. return 0;
  2661. }
  2662. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
  2663. const SSL_CIPHER *const *bp)
  2664. {
  2665. if ((*ap)->id > (*bp)->id)
  2666. return 1;
  2667. if ((*ap)->id < (*bp)->id)
  2668. return -1;
  2669. return 0;
  2670. }
  2671. /*
  2672. * return a STACK of the ciphers available for the SSL and in order of
  2673. * preference
  2674. */
  2675. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  2676. {
  2677. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  2678. if (sc != NULL) {
  2679. if (sc->cipher_list != NULL) {
  2680. return sc->cipher_list;
  2681. } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
  2682. return s->ctx->cipher_list;
  2683. }
  2684. }
  2685. return NULL;
  2686. }
  2687. STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
  2688. {
  2689. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  2690. if (sc == NULL || !sc->server)
  2691. return NULL;
  2692. return sc->peer_ciphers;
  2693. }
  2694. STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
  2695. {
  2696. STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
  2697. int i;
  2698. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2699. if (sc == NULL)
  2700. return NULL;
  2701. ciphers = SSL_get_ciphers(s);
  2702. if (!ciphers)
  2703. return NULL;
  2704. if (!ssl_set_client_disabled(sc))
  2705. return NULL;
  2706. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  2707. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  2708. if (!ssl_cipher_disabled(sc, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
  2709. if (!sk)
  2710. sk = sk_SSL_CIPHER_new_null();
  2711. if (!sk)
  2712. return NULL;
  2713. if (!sk_SSL_CIPHER_push(sk, c)) {
  2714. sk_SSL_CIPHER_free(sk);
  2715. return NULL;
  2716. }
  2717. }
  2718. }
  2719. return sk;
  2720. }
  2721. /** return a STACK of the ciphers available for the SSL and in order of
  2722. * algorithm id */
  2723. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *s)
  2724. {
  2725. if (s != NULL) {
  2726. if (s->cipher_list_by_id != NULL)
  2727. return s->cipher_list_by_id;
  2728. else if (s->ssl.ctx != NULL
  2729. && s->ssl.ctx->cipher_list_by_id != NULL)
  2730. return s->ssl.ctx->cipher_list_by_id;
  2731. }
  2732. return NULL;
  2733. }
  2734. /** The old interface to get the same thing as SSL_get_ciphers() */
  2735. const char *SSL_get_cipher_list(const SSL *s, int n)
  2736. {
  2737. const SSL_CIPHER *c;
  2738. STACK_OF(SSL_CIPHER) *sk;
  2739. if (s == NULL)
  2740. return NULL;
  2741. sk = SSL_get_ciphers(s);
  2742. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  2743. return NULL;
  2744. c = sk_SSL_CIPHER_value(sk, n);
  2745. if (c == NULL)
  2746. return NULL;
  2747. return c->name;
  2748. }
  2749. /** return a STACK of the ciphers available for the SSL_CTX and in order of
  2750. * preference */
  2751. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
  2752. {
  2753. if (ctx != NULL)
  2754. return ctx->cipher_list;
  2755. return NULL;
  2756. }
  2757. /*
  2758. * Distinguish between ciphers controlled by set_ciphersuite() and
  2759. * set_cipher_list() when counting.
  2760. */
  2761. static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
  2762. {
  2763. int i, num = 0;
  2764. const SSL_CIPHER *c;
  2765. if (sk == NULL)
  2766. return 0;
  2767. for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
  2768. c = sk_SSL_CIPHER_value(sk, i);
  2769. if (c->min_tls >= TLS1_3_VERSION)
  2770. continue;
  2771. num++;
  2772. }
  2773. return num;
  2774. }
  2775. /** specify the ciphers to be used by default by the SSL_CTX */
  2776. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  2777. {
  2778. STACK_OF(SSL_CIPHER) *sk;
  2779. sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
  2780. &ctx->cipher_list, &ctx->cipher_list_by_id, str,
  2781. ctx->cert);
  2782. /*
  2783. * ssl_create_cipher_list may return an empty stack if it was unable to
  2784. * find a cipher matching the given rule string (for example if the rule
  2785. * string specifies a cipher which has been disabled). This is not an
  2786. * error as far as ssl_create_cipher_list is concerned, and hence
  2787. * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
  2788. */
  2789. if (sk == NULL)
  2790. return 0;
  2791. if (ctx->method->num_ciphers() > 0 && cipher_list_tls12_num(sk) == 0) {
  2792. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
  2793. return 0;
  2794. }
  2795. return 1;
  2796. }
  2797. /** specify the ciphers to be used by the SSL */
  2798. int SSL_set_cipher_list(SSL *s, const char *str)
  2799. {
  2800. STACK_OF(SSL_CIPHER) *sk;
  2801. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  2802. SSL_CTX *ctx;
  2803. if (sc == NULL)
  2804. return 0;
  2805. ctx = s->ctx;
  2806. sk = ssl_create_cipher_list(ctx, sc->tls13_ciphersuites,
  2807. &sc->cipher_list, &sc->cipher_list_by_id, str,
  2808. sc->cert);
  2809. /* see comment in SSL_CTX_set_cipher_list */
  2810. if (sk == NULL)
  2811. return 0;
  2812. if (ctx->method->num_ciphers() > 0 && cipher_list_tls12_num(sk) == 0) {
  2813. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
  2814. return 0;
  2815. }
  2816. return 1;
  2817. }
  2818. char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
  2819. {
  2820. char *p;
  2821. STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
  2822. const SSL_CIPHER *c;
  2823. int i;
  2824. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  2825. if (sc == NULL)
  2826. return NULL;
  2827. if (!sc->server
  2828. || sc->peer_ciphers == NULL
  2829. || size < 2)
  2830. return NULL;
  2831. p = buf;
  2832. clntsk = sc->peer_ciphers;
  2833. srvrsk = SSL_get_ciphers(s);
  2834. if (clntsk == NULL || srvrsk == NULL)
  2835. return NULL;
  2836. if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
  2837. return NULL;
  2838. for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
  2839. int n;
  2840. c = sk_SSL_CIPHER_value(clntsk, i);
  2841. if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
  2842. continue;
  2843. n = OPENSSL_strnlen(c->name, size);
  2844. if (n >= size) {
  2845. if (p != buf)
  2846. --p;
  2847. *p = '\0';
  2848. return buf;
  2849. }
  2850. memcpy(p, c->name, n);
  2851. p += n;
  2852. *(p++) = ':';
  2853. size -= n + 1;
  2854. }
  2855. p[-1] = '\0';
  2856. return buf;
  2857. }
  2858. /**
  2859. * Return the requested servername (SNI) value. Note that the behaviour varies
  2860. * depending on:
  2861. * - whether this is called by the client or the server,
  2862. * - if we are before or during/after the handshake,
  2863. * - if a resumption or normal handshake is being attempted/has occurred
  2864. * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
  2865. *
  2866. * Note that only the host_name type is defined (RFC 3546).
  2867. */
  2868. const char *SSL_get_servername(const SSL *s, const int type)
  2869. {
  2870. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  2871. int server;
  2872. if (sc == NULL)
  2873. return NULL;
  2874. /*
  2875. * If we don't know if we are the client or the server yet then we assume
  2876. * client.
  2877. */
  2878. server = sc->handshake_func == NULL ? 0 : sc->server;
  2879. if (type != TLSEXT_NAMETYPE_host_name)
  2880. return NULL;
  2881. if (server) {
  2882. /**
  2883. * Server side
  2884. * In TLSv1.3 on the server SNI is not associated with the session
  2885. * but in TLSv1.2 or below it is.
  2886. *
  2887. * Before the handshake:
  2888. * - return NULL
  2889. *
  2890. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2891. * - If a servername was accepted by the server in the original
  2892. * handshake then it will return that servername, or NULL otherwise.
  2893. *
  2894. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2895. * - The function will return the servername requested by the client in
  2896. * this handshake or NULL if none was requested.
  2897. */
  2898. if (sc->hit && !SSL_CONNECTION_IS_TLS13(sc))
  2899. return sc->session->ext.hostname;
  2900. } else {
  2901. /**
  2902. * Client side
  2903. *
  2904. * Before the handshake:
  2905. * - If a servername has been set via a call to
  2906. * SSL_set_tlsext_host_name() then it will return that servername
  2907. * - If one has not been set, but a TLSv1.2 resumption is being
  2908. * attempted and the session from the original handshake had a
  2909. * servername accepted by the server then it will return that
  2910. * servername
  2911. * - Otherwise it returns NULL
  2912. *
  2913. * During/after the handshake (TLSv1.2 or below resumption occurred):
  2914. * - If the session from the original handshake had a servername accepted
  2915. * by the server then it will return that servername.
  2916. * - Otherwise it returns the servername set via
  2917. * SSL_set_tlsext_host_name() (or NULL if it was not called).
  2918. *
  2919. * During/after the handshake (TLSv1.2 or below resumption did not occur):
  2920. * - It will return the servername set via SSL_set_tlsext_host_name()
  2921. * (or NULL if it was not called).
  2922. */
  2923. if (SSL_in_before(s)) {
  2924. if (sc->ext.hostname == NULL
  2925. && sc->session != NULL
  2926. && sc->session->ssl_version != TLS1_3_VERSION)
  2927. return sc->session->ext.hostname;
  2928. } else {
  2929. if (!SSL_CONNECTION_IS_TLS13(sc) && sc->hit
  2930. && sc->session->ext.hostname != NULL)
  2931. return sc->session->ext.hostname;
  2932. }
  2933. }
  2934. return sc->ext.hostname;
  2935. }
  2936. int SSL_get_servername_type(const SSL *s)
  2937. {
  2938. if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
  2939. return TLSEXT_NAMETYPE_host_name;
  2940. return -1;
  2941. }
  2942. /*
  2943. * SSL_select_next_proto implements the standard protocol selection. It is
  2944. * expected that this function is called from the callback set by
  2945. * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
  2946. * vector of 8-bit, length prefixed byte strings. The length byte itself is
  2947. * not included in the length. A byte string of length 0 is invalid. No byte
  2948. * string may be truncated. The current, but experimental algorithm for
  2949. * selecting the protocol is: 1) If the server doesn't support NPN then this
  2950. * is indicated to the callback. In this case, the client application has to
  2951. * abort the connection or have a default application level protocol. 2) If
  2952. * the server supports NPN, but advertises an empty list then the client
  2953. * selects the first protocol in its list, but indicates via the API that this
  2954. * fallback case was enacted. 3) Otherwise, the client finds the first
  2955. * protocol in the server's list that it supports and selects this protocol.
  2956. * This is because it's assumed that the server has better information about
  2957. * which protocol a client should use. 4) If the client doesn't support any
  2958. * of the server's advertised protocols, then this is treated the same as
  2959. * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
  2960. * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  2961. */
  2962. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
  2963. const unsigned char *server,
  2964. unsigned int server_len,
  2965. const unsigned char *client, unsigned int client_len)
  2966. {
  2967. PACKET cpkt, csubpkt, spkt, ssubpkt;
  2968. if (!PACKET_buf_init(&cpkt, client, client_len)
  2969. || !PACKET_get_length_prefixed_1(&cpkt, &csubpkt)
  2970. || PACKET_remaining(&csubpkt) == 0) {
  2971. *out = NULL;
  2972. *outlen = 0;
  2973. return OPENSSL_NPN_NO_OVERLAP;
  2974. }
  2975. /*
  2976. * Set the default opportunistic protocol. Will be overwritten if we find
  2977. * a match.
  2978. */
  2979. *out = (unsigned char *)PACKET_data(&csubpkt);
  2980. *outlen = (unsigned char)PACKET_remaining(&csubpkt);
  2981. /*
  2982. * For each protocol in server preference order, see if we support it.
  2983. */
  2984. if (PACKET_buf_init(&spkt, server, server_len)) {
  2985. while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) {
  2986. if (PACKET_remaining(&ssubpkt) == 0)
  2987. continue; /* Invalid - ignore it */
  2988. if (PACKET_buf_init(&cpkt, client, client_len)) {
  2989. while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) {
  2990. if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt),
  2991. PACKET_remaining(&ssubpkt))) {
  2992. /* We found a match */
  2993. *out = (unsigned char *)PACKET_data(&ssubpkt);
  2994. *outlen = (unsigned char)PACKET_remaining(&ssubpkt);
  2995. return OPENSSL_NPN_NEGOTIATED;
  2996. }
  2997. }
  2998. /* Ignore spurious trailing bytes in the client list */
  2999. } else {
  3000. /* This should never happen */
  3001. return OPENSSL_NPN_NO_OVERLAP;
  3002. }
  3003. }
  3004. /* Ignore spurious trailing bytes in the server list */
  3005. }
  3006. /*
  3007. * There's no overlap between our protocols and the server's list. We use
  3008. * the default opportunistic protocol selected earlier
  3009. */
  3010. return OPENSSL_NPN_NO_OVERLAP;
  3011. }
  3012. #ifndef OPENSSL_NO_NEXTPROTONEG
  3013. /*
  3014. * SSL_get0_next_proto_negotiated sets *data and *len to point to the
  3015. * client's requested protocol for this connection and returns 0. If the
  3016. * client didn't request any protocol, then *data is set to NULL. Note that
  3017. * the client can request any protocol it chooses. The value returned from
  3018. * this function need not be a member of the list of supported protocols
  3019. * provided by the callback.
  3020. */
  3021. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
  3022. unsigned *len)
  3023. {
  3024. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  3025. if (sc == NULL) {
  3026. /* We have no other way to indicate error */
  3027. *data = NULL;
  3028. *len = 0;
  3029. return;
  3030. }
  3031. *data = sc->ext.npn;
  3032. if (*data == NULL) {
  3033. *len = 0;
  3034. } else {
  3035. *len = (unsigned int)sc->ext.npn_len;
  3036. }
  3037. }
  3038. /*
  3039. * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
  3040. * a TLS server needs a list of supported protocols for Next Protocol
  3041. * Negotiation. The returned list must be in wire format. The list is
  3042. * returned by setting |out| to point to it and |outlen| to its length. This
  3043. * memory will not be modified, but one should assume that the SSL* keeps a
  3044. * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
  3045. * wishes to advertise. Otherwise, no such extension will be included in the
  3046. * ServerHello.
  3047. */
  3048. void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
  3049. SSL_CTX_npn_advertised_cb_func cb,
  3050. void *arg)
  3051. {
  3052. if (IS_QUIC_CTX(ctx))
  3053. /* NPN not allowed for QUIC */
  3054. return;
  3055. ctx->ext.npn_advertised_cb = cb;
  3056. ctx->ext.npn_advertised_cb_arg = arg;
  3057. }
  3058. /*
  3059. * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  3060. * client needs to select a protocol from the server's provided list. |out|
  3061. * must be set to point to the selected protocol (which may be within |in|).
  3062. * The length of the protocol name must be written into |outlen|. The
  3063. * server's advertised protocols are provided in |in| and |inlen|. The
  3064. * callback can assume that |in| is syntactically valid. The client must
  3065. * select a protocol. It is fatal to the connection if this callback returns
  3066. * a value other than SSL_TLSEXT_ERR_OK.
  3067. */
  3068. void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
  3069. SSL_CTX_npn_select_cb_func cb,
  3070. void *arg)
  3071. {
  3072. if (IS_QUIC_CTX(ctx))
  3073. /* NPN not allowed for QUIC */
  3074. return;
  3075. ctx->ext.npn_select_cb = cb;
  3076. ctx->ext.npn_select_cb_arg = arg;
  3077. }
  3078. #endif
  3079. static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
  3080. {
  3081. unsigned int idx;
  3082. if (protos_len < 2 || protos == NULL)
  3083. return 0;
  3084. for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
  3085. if (protos[idx] == 0)
  3086. return 0;
  3087. }
  3088. return idx == protos_len;
  3089. }
  3090. /*
  3091. * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  3092. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  3093. * length-prefixed strings). Returns 0 on success.
  3094. */
  3095. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
  3096. unsigned int protos_len)
  3097. {
  3098. unsigned char *alpn;
  3099. if (protos_len == 0 || protos == NULL) {
  3100. OPENSSL_free(ctx->ext.alpn);
  3101. ctx->ext.alpn = NULL;
  3102. ctx->ext.alpn_len = 0;
  3103. return 0;
  3104. }
  3105. /* Not valid per RFC */
  3106. if (!alpn_value_ok(protos, protos_len))
  3107. return 1;
  3108. alpn = OPENSSL_memdup(protos, protos_len);
  3109. if (alpn == NULL)
  3110. return 1;
  3111. OPENSSL_free(ctx->ext.alpn);
  3112. ctx->ext.alpn = alpn;
  3113. ctx->ext.alpn_len = protos_len;
  3114. return 0;
  3115. }
  3116. /*
  3117. * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
  3118. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  3119. * length-prefixed strings). Returns 0 on success.
  3120. */
  3121. int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
  3122. unsigned int protos_len)
  3123. {
  3124. unsigned char *alpn;
  3125. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  3126. if (sc == NULL)
  3127. return 1;
  3128. if (protos_len == 0 || protos == NULL) {
  3129. OPENSSL_free(sc->ext.alpn);
  3130. sc->ext.alpn = NULL;
  3131. sc->ext.alpn_len = 0;
  3132. return 0;
  3133. }
  3134. /* Not valid per RFC */
  3135. if (!alpn_value_ok(protos, protos_len))
  3136. return 1;
  3137. alpn = OPENSSL_memdup(protos, protos_len);
  3138. if (alpn == NULL)
  3139. return 1;
  3140. OPENSSL_free(sc->ext.alpn);
  3141. sc->ext.alpn = alpn;
  3142. sc->ext.alpn_len = protos_len;
  3143. return 0;
  3144. }
  3145. /*
  3146. * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
  3147. * called during ClientHello processing in order to select an ALPN protocol
  3148. * from the client's list of offered protocols.
  3149. */
  3150. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  3151. SSL_CTX_alpn_select_cb_func cb,
  3152. void *arg)
  3153. {
  3154. ctx->ext.alpn_select_cb = cb;
  3155. ctx->ext.alpn_select_cb_arg = arg;
  3156. }
  3157. /*
  3158. * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  3159. * On return it sets |*data| to point to |*len| bytes of protocol name
  3160. * (not including the leading length-prefix byte). If the server didn't
  3161. * respond with a negotiated protocol then |*len| will be zero.
  3162. */
  3163. void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
  3164. unsigned int *len)
  3165. {
  3166. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  3167. if (sc == NULL) {
  3168. /* We have no other way to indicate error */
  3169. *data = NULL;
  3170. *len = 0;
  3171. return;
  3172. }
  3173. *data = sc->s3.alpn_selected;
  3174. if (*data == NULL)
  3175. *len = 0;
  3176. else
  3177. *len = (unsigned int)sc->s3.alpn_selected_len;
  3178. }
  3179. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  3180. const char *label, size_t llen,
  3181. const unsigned char *context, size_t contextlen,
  3182. int use_context)
  3183. {
  3184. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3185. if (sc == NULL)
  3186. return -1;
  3187. if (sc->session == NULL
  3188. || (sc->version < TLS1_VERSION && sc->version != DTLS1_BAD_VER))
  3189. return -1;
  3190. return sc->ssl.method->ssl3_enc->export_keying_material(sc, out, olen, label,
  3191. llen, context,
  3192. contextlen,
  3193. use_context);
  3194. }
  3195. int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  3196. const char *label, size_t llen,
  3197. const unsigned char *context,
  3198. size_t contextlen)
  3199. {
  3200. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3201. if (sc == NULL)
  3202. return -1;
  3203. if (sc->version != TLS1_3_VERSION)
  3204. return 0;
  3205. return tls13_export_keying_material_early(sc, out, olen, label, llen,
  3206. context, contextlen);
  3207. }
  3208. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  3209. {
  3210. const unsigned char *session_id = a->session_id;
  3211. unsigned long l;
  3212. unsigned char tmp_storage[4];
  3213. if (a->session_id_length < sizeof(tmp_storage)) {
  3214. memset(tmp_storage, 0, sizeof(tmp_storage));
  3215. memcpy(tmp_storage, a->session_id, a->session_id_length);
  3216. session_id = tmp_storage;
  3217. }
  3218. l = (unsigned long)
  3219. ((unsigned long)session_id[0]) |
  3220. ((unsigned long)session_id[1] << 8L) |
  3221. ((unsigned long)session_id[2] << 16L) |
  3222. ((unsigned long)session_id[3] << 24L);
  3223. return l;
  3224. }
  3225. /*
  3226. * NB: If this function (or indeed the hash function which uses a sort of
  3227. * coarser function than this one) is changed, ensure
  3228. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
  3229. * being able to construct an SSL_SESSION that will collide with any existing
  3230. * session with a matching session ID.
  3231. */
  3232. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
  3233. {
  3234. if (a->ssl_version != b->ssl_version)
  3235. return 1;
  3236. if (a->session_id_length != b->session_id_length)
  3237. return 1;
  3238. return memcmp(a->session_id, b->session_id, a->session_id_length);
  3239. }
  3240. /*
  3241. * These wrapper functions should remain rather than redeclaring
  3242. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  3243. * variable. The reason is that the functions aren't static, they're exposed
  3244. * via ssl.h.
  3245. */
  3246. SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  3247. const SSL_METHOD *meth)
  3248. {
  3249. SSL_CTX *ret = NULL;
  3250. #ifndef OPENSSL_NO_COMP_ALG
  3251. int i;
  3252. #endif
  3253. if (meth == NULL) {
  3254. ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  3255. return NULL;
  3256. }
  3257. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
  3258. return NULL;
  3259. /* Doing this for the run once effect */
  3260. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  3261. ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  3262. goto err;
  3263. }
  3264. ret = OPENSSL_zalloc(sizeof(*ret));
  3265. if (ret == NULL)
  3266. return NULL;
  3267. /* Init the reference counting before any call to SSL_CTX_free */
  3268. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  3269. OPENSSL_free(ret);
  3270. return NULL;
  3271. }
  3272. ret->lock = CRYPTO_THREAD_lock_new();
  3273. if (ret->lock == NULL) {
  3274. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  3275. goto err;
  3276. }
  3277. #ifdef TSAN_REQUIRES_LOCKING
  3278. ret->tsan_lock = CRYPTO_THREAD_lock_new();
  3279. if (ret->tsan_lock == NULL) {
  3280. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  3281. goto err;
  3282. }
  3283. #endif
  3284. ret->libctx = libctx;
  3285. if (propq != NULL) {
  3286. ret->propq = OPENSSL_strdup(propq);
  3287. if (ret->propq == NULL)
  3288. goto err;
  3289. }
  3290. ret->method = meth;
  3291. ret->min_proto_version = 0;
  3292. ret->max_proto_version = 0;
  3293. ret->mode = SSL_MODE_AUTO_RETRY;
  3294. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  3295. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  3296. /* We take the system default. */
  3297. ret->session_timeout = meth->get_timeout();
  3298. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  3299. ret->verify_mode = SSL_VERIFY_NONE;
  3300. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  3301. if (ret->sessions == NULL) {
  3302. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  3303. goto err;
  3304. }
  3305. ret->cert_store = X509_STORE_new();
  3306. if (ret->cert_store == NULL) {
  3307. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  3308. goto err;
  3309. }
  3310. #ifndef OPENSSL_NO_CT
  3311. ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
  3312. if (ret->ctlog_store == NULL) {
  3313. ERR_raise(ERR_LIB_SSL, ERR_R_CT_LIB);
  3314. goto err;
  3315. }
  3316. #endif
  3317. /* initialize cipher/digest methods table */
  3318. if (!ssl_load_ciphers(ret)) {
  3319. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3320. goto err;
  3321. }
  3322. if (!ssl_load_groups(ret)) {
  3323. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3324. goto err;
  3325. }
  3326. /* load provider sigalgs */
  3327. if (!ssl_load_sigalgs(ret)) {
  3328. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3329. goto err;
  3330. }
  3331. /* initialise sig algs */
  3332. if (!ssl_setup_sigalgs(ret)) {
  3333. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3334. goto err;
  3335. }
  3336. if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites())) {
  3337. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3338. goto err;
  3339. }
  3340. if ((ret->cert = ssl_cert_new(SSL_PKEY_NUM + ret->sigalg_list_len)) == NULL) {
  3341. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3342. goto err;
  3343. }
  3344. if (!ssl_create_cipher_list(ret,
  3345. ret->tls13_ciphersuites,
  3346. &ret->cipher_list, &ret->cipher_list_by_id,
  3347. OSSL_default_cipher_list(), ret->cert)
  3348. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
  3349. ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  3350. goto err;
  3351. }
  3352. ret->param = X509_VERIFY_PARAM_new();
  3353. if (ret->param == NULL) {
  3354. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  3355. goto err;
  3356. }
  3357. /*
  3358. * If these aren't available from the provider we'll get NULL returns.
  3359. * That's fine but will cause errors later if SSLv3 is negotiated
  3360. */
  3361. ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq);
  3362. ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq);
  3363. if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL) {
  3364. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  3365. goto err;
  3366. }
  3367. if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL) {
  3368. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  3369. goto err;
  3370. }
  3371. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data)) {
  3372. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  3373. goto err;
  3374. }
  3375. if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
  3376. goto err;
  3377. /* No compression for DTLS */
  3378. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
  3379. ret->comp_methods = SSL_COMP_get_compression_methods();
  3380. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  3381. ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  3382. /* Setup RFC5077 ticket keys */
  3383. if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
  3384. sizeof(ret->ext.tick_key_name), 0) <= 0)
  3385. || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
  3386. sizeof(ret->ext.secure->tick_hmac_key), 0) <= 0)
  3387. || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
  3388. sizeof(ret->ext.secure->tick_aes_key), 0) <= 0))
  3389. ret->options |= SSL_OP_NO_TICKET;
  3390. if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
  3391. sizeof(ret->ext.cookie_hmac_key), 0) <= 0) {
  3392. ERR_raise(ERR_LIB_SSL, ERR_R_RAND_LIB);
  3393. goto err;
  3394. }
  3395. #ifndef OPENSSL_NO_SRP
  3396. if (!ssl_ctx_srp_ctx_init_intern(ret)) {
  3397. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  3398. goto err;
  3399. }
  3400. #endif
  3401. #ifndef OPENSSL_NO_ENGINE
  3402. # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  3403. # define eng_strx(x) #x
  3404. # define eng_str(x) eng_strx(x)
  3405. /* Use specific client engine automatically... ignore errors */
  3406. {
  3407. ENGINE *eng;
  3408. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  3409. if (!eng) {
  3410. ERR_clear_error();
  3411. ENGINE_load_builtin_engines();
  3412. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  3413. }
  3414. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  3415. ERR_clear_error();
  3416. }
  3417. # endif
  3418. #endif
  3419. #ifndef OPENSSL_NO_COMP_ALG
  3420. /*
  3421. * Set the default order: brotli, zlib, zstd
  3422. * Including only those enabled algorithms
  3423. */
  3424. memset(ret->cert_comp_prefs, 0, sizeof(ret->cert_comp_prefs));
  3425. i = 0;
  3426. if (ossl_comp_has_alg(TLSEXT_comp_cert_brotli))
  3427. ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_brotli;
  3428. if (ossl_comp_has_alg(TLSEXT_comp_cert_zlib))
  3429. ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zlib;
  3430. if (ossl_comp_has_alg(TLSEXT_comp_cert_zstd))
  3431. ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zstd;
  3432. #endif
  3433. /*
  3434. * Disable compression by default to prevent CRIME. Applications can
  3435. * re-enable compression by configuring
  3436. * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
  3437. * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
  3438. * middlebox compatibility by default. This may be disabled by default in
  3439. * a later OpenSSL version.
  3440. */
  3441. ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  3442. ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  3443. /*
  3444. * We cannot usefully set a default max_early_data here (which gets
  3445. * propagated in SSL_new(), for the following reason: setting the
  3446. * SSL field causes tls_construct_stoc_early_data() to tell the
  3447. * client that early data will be accepted when constructing a TLS 1.3
  3448. * session ticket, and the client will accordingly send us early data
  3449. * when using that ticket (if the client has early data to send).
  3450. * However, in order for the early data to actually be consumed by
  3451. * the application, the application must also have calls to
  3452. * SSL_read_early_data(); otherwise we'll just skip past the early data
  3453. * and ignore it. So, since the application must add calls to
  3454. * SSL_read_early_data(), we also require them to add
  3455. * calls to SSL_CTX_set_max_early_data() in order to use early data,
  3456. * eliminating the bandwidth-wasting early data in the case described
  3457. * above.
  3458. */
  3459. ret->max_early_data = 0;
  3460. /*
  3461. * Default recv_max_early_data is a fully loaded single record. Could be
  3462. * split across multiple records in practice. We set this differently to
  3463. * max_early_data so that, in the default case, we do not advertise any
  3464. * support for early_data, but if a client were to send us some (e.g.
  3465. * because of an old, stale ticket) then we will tolerate it and skip over
  3466. * it.
  3467. */
  3468. ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
  3469. /* By default we send two session tickets automatically in TLSv1.3 */
  3470. ret->num_tickets = 2;
  3471. ssl_ctx_system_config(ret);
  3472. return ret;
  3473. err:
  3474. SSL_CTX_free(ret);
  3475. return NULL;
  3476. }
  3477. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  3478. {
  3479. return SSL_CTX_new_ex(NULL, NULL, meth);
  3480. }
  3481. int SSL_CTX_up_ref(SSL_CTX *ctx)
  3482. {
  3483. int i;
  3484. if (CRYPTO_UP_REF(&ctx->references, &i) <= 0)
  3485. return 0;
  3486. REF_PRINT_COUNT("SSL_CTX", i, ctx);
  3487. REF_ASSERT_ISNT(i < 2);
  3488. return ((i > 1) ? 1 : 0);
  3489. }
  3490. void SSL_CTX_free(SSL_CTX *a)
  3491. {
  3492. int i;
  3493. size_t j;
  3494. if (a == NULL)
  3495. return;
  3496. CRYPTO_DOWN_REF(&a->references, &i);
  3497. REF_PRINT_COUNT("SSL_CTX", i, a);
  3498. if (i > 0)
  3499. return;
  3500. REF_ASSERT_ISNT(i < 0);
  3501. X509_VERIFY_PARAM_free(a->param);
  3502. dane_ctx_final(&a->dane);
  3503. /*
  3504. * Free internal session cache. However: the remove_cb() may reference
  3505. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  3506. * after the sessions were flushed.
  3507. * As the ex_data handling routines might also touch the session cache,
  3508. * the most secure solution seems to be: empty (flush) the cache, then
  3509. * free ex_data, then finally free the cache.
  3510. * (See ticket [openssl.org #212].)
  3511. */
  3512. if (a->sessions != NULL)
  3513. SSL_CTX_flush_sessions(a, 0);
  3514. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  3515. lh_SSL_SESSION_free(a->sessions);
  3516. X509_STORE_free(a->cert_store);
  3517. #ifndef OPENSSL_NO_CT
  3518. CTLOG_STORE_free(a->ctlog_store);
  3519. #endif
  3520. sk_SSL_CIPHER_free(a->cipher_list);
  3521. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  3522. sk_SSL_CIPHER_free(a->tls13_ciphersuites);
  3523. ssl_cert_free(a->cert);
  3524. sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
  3525. sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
  3526. OSSL_STACK_OF_X509_free(a->extra_certs);
  3527. a->comp_methods = NULL;
  3528. #ifndef OPENSSL_NO_SRTP
  3529. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  3530. #endif
  3531. #ifndef OPENSSL_NO_SRP
  3532. ssl_ctx_srp_ctx_free_intern(a);
  3533. #endif
  3534. #ifndef OPENSSL_NO_ENGINE
  3535. tls_engine_finish(a->client_cert_engine);
  3536. #endif
  3537. OPENSSL_free(a->ext.ecpointformats);
  3538. OPENSSL_free(a->ext.supportedgroups);
  3539. OPENSSL_free(a->ext.supported_groups_default);
  3540. OPENSSL_free(a->ext.alpn);
  3541. OPENSSL_secure_free(a->ext.secure);
  3542. ssl_evp_md_free(a->md5);
  3543. ssl_evp_md_free(a->sha1);
  3544. for (j = 0; j < SSL_ENC_NUM_IDX; j++)
  3545. ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
  3546. for (j = 0; j < SSL_MD_NUM_IDX; j++)
  3547. ssl_evp_md_free(a->ssl_digest_methods[j]);
  3548. for (j = 0; j < a->group_list_len; j++) {
  3549. OPENSSL_free(a->group_list[j].tlsname);
  3550. OPENSSL_free(a->group_list[j].realname);
  3551. OPENSSL_free(a->group_list[j].algorithm);
  3552. }
  3553. OPENSSL_free(a->group_list);
  3554. for (j = 0; j < a->sigalg_list_len; j++) {
  3555. OPENSSL_free(a->sigalg_list[j].name);
  3556. OPENSSL_free(a->sigalg_list[j].sigalg_name);
  3557. OPENSSL_free(a->sigalg_list[j].sigalg_oid);
  3558. OPENSSL_free(a->sigalg_list[j].sig_name);
  3559. OPENSSL_free(a->sigalg_list[j].sig_oid);
  3560. OPENSSL_free(a->sigalg_list[j].hash_name);
  3561. OPENSSL_free(a->sigalg_list[j].hash_oid);
  3562. OPENSSL_free(a->sigalg_list[j].keytype);
  3563. OPENSSL_free(a->sigalg_list[j].keytype_oid);
  3564. }
  3565. OPENSSL_free(a->sigalg_list);
  3566. OPENSSL_free(a->ssl_cert_info);
  3567. OPENSSL_free(a->sigalg_lookup_cache);
  3568. OPENSSL_free(a->tls12_sigalgs);
  3569. OPENSSL_free(a->client_cert_type);
  3570. OPENSSL_free(a->server_cert_type);
  3571. CRYPTO_THREAD_lock_free(a->lock);
  3572. CRYPTO_FREE_REF(&a->references);
  3573. #ifdef TSAN_REQUIRES_LOCKING
  3574. CRYPTO_THREAD_lock_free(a->tsan_lock);
  3575. #endif
  3576. OPENSSL_free(a->propq);
  3577. OPENSSL_free(a);
  3578. }
  3579. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  3580. {
  3581. ctx->default_passwd_callback = cb;
  3582. }
  3583. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  3584. {
  3585. ctx->default_passwd_callback_userdata = u;
  3586. }
  3587. pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
  3588. {
  3589. return ctx->default_passwd_callback;
  3590. }
  3591. void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
  3592. {
  3593. return ctx->default_passwd_callback_userdata;
  3594. }
  3595. void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
  3596. {
  3597. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3598. if (sc == NULL)
  3599. return;
  3600. sc->default_passwd_callback = cb;
  3601. }
  3602. void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
  3603. {
  3604. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3605. if (sc == NULL)
  3606. return;
  3607. sc->default_passwd_callback_userdata = u;
  3608. }
  3609. pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
  3610. {
  3611. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3612. if (sc == NULL)
  3613. return NULL;
  3614. return sc->default_passwd_callback;
  3615. }
  3616. void *SSL_get_default_passwd_cb_userdata(SSL *s)
  3617. {
  3618. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3619. if (sc == NULL)
  3620. return NULL;
  3621. return sc->default_passwd_callback_userdata;
  3622. }
  3623. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  3624. int (*cb) (X509_STORE_CTX *, void *),
  3625. void *arg)
  3626. {
  3627. ctx->app_verify_callback = cb;
  3628. ctx->app_verify_arg = arg;
  3629. }
  3630. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  3631. int (*cb) (int, X509_STORE_CTX *))
  3632. {
  3633. ctx->verify_mode = mode;
  3634. ctx->default_verify_callback = cb;
  3635. }
  3636. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  3637. {
  3638. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  3639. }
  3640. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  3641. {
  3642. ssl_cert_set_cert_cb(c->cert, cb, arg);
  3643. }
  3644. void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
  3645. {
  3646. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3647. if (sc == NULL)
  3648. return;
  3649. ssl_cert_set_cert_cb(sc->cert, cb, arg);
  3650. }
  3651. void ssl_set_masks(SSL_CONNECTION *s)
  3652. {
  3653. CERT *c = s->cert;
  3654. uint32_t *pvalid = s->s3.tmp.valid_flags;
  3655. int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
  3656. unsigned long mask_k, mask_a;
  3657. int have_ecc_cert, ecdsa_ok;
  3658. if (c == NULL)
  3659. return;
  3660. dh_tmp = (c->dh_tmp != NULL
  3661. || c->dh_tmp_cb != NULL
  3662. || c->dh_tmp_auto);
  3663. rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  3664. rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  3665. dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
  3666. have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
  3667. mask_k = 0;
  3668. mask_a = 0;
  3669. OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
  3670. dh_tmp, rsa_enc, rsa_sign, dsa_sign);
  3671. #ifndef OPENSSL_NO_GOST
  3672. if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
  3673. mask_k |= SSL_kGOST | SSL_kGOST18;
  3674. mask_a |= SSL_aGOST12;
  3675. }
  3676. if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
  3677. mask_k |= SSL_kGOST | SSL_kGOST18;
  3678. mask_a |= SSL_aGOST12;
  3679. }
  3680. if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
  3681. mask_k |= SSL_kGOST;
  3682. mask_a |= SSL_aGOST01;
  3683. }
  3684. #endif
  3685. if (rsa_enc)
  3686. mask_k |= SSL_kRSA;
  3687. if (dh_tmp)
  3688. mask_k |= SSL_kDHE;
  3689. /*
  3690. * If we only have an RSA-PSS certificate allow RSA authentication
  3691. * if TLS 1.2 and peer supports it.
  3692. */
  3693. if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
  3694. && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
  3695. && TLS1_get_version(&s->ssl) == TLS1_2_VERSION))
  3696. mask_a |= SSL_aRSA;
  3697. if (dsa_sign) {
  3698. mask_a |= SSL_aDSS;
  3699. }
  3700. mask_a |= SSL_aNULL;
  3701. /*
  3702. * You can do anything with an RPK key, since there's no cert to restrict it
  3703. * But we need to check for private keys
  3704. */
  3705. if (pvalid[SSL_PKEY_RSA] & CERT_PKEY_RPK) {
  3706. mask_a |= SSL_aRSA;
  3707. mask_k |= SSL_kRSA;
  3708. }
  3709. if (pvalid[SSL_PKEY_ECC] & CERT_PKEY_RPK)
  3710. mask_a |= SSL_aECDSA;
  3711. if (TLS1_get_version(&s->ssl) == TLS1_2_VERSION) {
  3712. if (pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_RPK)
  3713. mask_a |= SSL_aRSA;
  3714. if (pvalid[SSL_PKEY_ED25519] & CERT_PKEY_RPK
  3715. || pvalid[SSL_PKEY_ED448] & CERT_PKEY_RPK)
  3716. mask_a |= SSL_aECDSA;
  3717. }
  3718. /*
  3719. * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
  3720. * depending on the key usage extension.
  3721. */
  3722. if (have_ecc_cert) {
  3723. uint32_t ex_kusage;
  3724. ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
  3725. ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
  3726. if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
  3727. ecdsa_ok = 0;
  3728. if (ecdsa_ok)
  3729. mask_a |= SSL_aECDSA;
  3730. }
  3731. /* Allow Ed25519 for TLS 1.2 if peer supports it */
  3732. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
  3733. && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
  3734. && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)
  3735. mask_a |= SSL_aECDSA;
  3736. /* Allow Ed448 for TLS 1.2 if peer supports it */
  3737. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
  3738. && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
  3739. && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)
  3740. mask_a |= SSL_aECDSA;
  3741. mask_k |= SSL_kECDHE;
  3742. #ifndef OPENSSL_NO_PSK
  3743. mask_k |= SSL_kPSK;
  3744. mask_a |= SSL_aPSK;
  3745. if (mask_k & SSL_kRSA)
  3746. mask_k |= SSL_kRSAPSK;
  3747. if (mask_k & SSL_kDHE)
  3748. mask_k |= SSL_kDHEPSK;
  3749. if (mask_k & SSL_kECDHE)
  3750. mask_k |= SSL_kECDHEPSK;
  3751. #endif
  3752. s->s3.tmp.mask_k = mask_k;
  3753. s->s3.tmp.mask_a = mask_a;
  3754. }
  3755. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s)
  3756. {
  3757. if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
  3758. /* key usage, if present, must allow signing */
  3759. if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
  3760. ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  3761. return 0;
  3762. }
  3763. }
  3764. return 1; /* all checks are ok */
  3765. }
  3766. int ssl_get_server_cert_serverinfo(SSL_CONNECTION *s,
  3767. const unsigned char **serverinfo,
  3768. size_t *serverinfo_length)
  3769. {
  3770. CERT_PKEY *cpk = s->s3.tmp.cert;
  3771. *serverinfo_length = 0;
  3772. if (cpk == NULL || cpk->serverinfo == NULL)
  3773. return 0;
  3774. *serverinfo = cpk->serverinfo;
  3775. *serverinfo_length = cpk->serverinfo_length;
  3776. return 1;
  3777. }
  3778. void ssl_update_cache(SSL_CONNECTION *s, int mode)
  3779. {
  3780. int i;
  3781. /*
  3782. * If the session_id_length is 0, we are not supposed to cache it, and it
  3783. * would be rather hard to do anyway :-). Also if the session has already
  3784. * been marked as not_resumable we should not cache it for later reuse.
  3785. */
  3786. if (s->session->session_id_length == 0 || s->session->not_resumable)
  3787. return;
  3788. /*
  3789. * If sid_ctx_length is 0 there is no specific application context
  3790. * associated with this session, so when we try to resume it and
  3791. * SSL_VERIFY_PEER is requested to verify the client identity, we have no
  3792. * indication that this is actually a session for the proper application
  3793. * context, and the *handshake* will fail, not just the resumption attempt.
  3794. * Do not cache (on the server) these sessions that are not resumable
  3795. * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
  3796. */
  3797. if (s->server && s->session->sid_ctx_length == 0
  3798. && (s->verify_mode & SSL_VERIFY_PEER) != 0)
  3799. return;
  3800. i = s->session_ctx->session_cache_mode;
  3801. if ((i & mode) != 0
  3802. && (!s->hit || SSL_CONNECTION_IS_TLS13(s))) {
  3803. /*
  3804. * Add the session to the internal cache. In server side TLSv1.3 we
  3805. * normally don't do this because by default it's a full stateless ticket
  3806. * with only a dummy session id so there is no reason to cache it,
  3807. * unless:
  3808. * - we are doing early_data, in which case we cache so that we can
  3809. * detect replays
  3810. * - the application has set a remove_session_cb so needs to know about
  3811. * session timeout events
  3812. * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
  3813. */
  3814. if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
  3815. && (!SSL_CONNECTION_IS_TLS13(s)
  3816. || !s->server
  3817. || (s->max_early_data > 0
  3818. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
  3819. || s->session_ctx->remove_session_cb != NULL
  3820. || (s->options & SSL_OP_NO_TICKET) != 0))
  3821. SSL_CTX_add_session(s->session_ctx, s->session);
  3822. /*
  3823. * Add the session to the external cache. We do this even in server side
  3824. * TLSv1.3 without early data because some applications just want to
  3825. * know about the creation of a session and aren't doing a full cache.
  3826. */
  3827. if (s->session_ctx->new_session_cb != NULL) {
  3828. SSL_SESSION_up_ref(s->session);
  3829. if (!s->session_ctx->new_session_cb(SSL_CONNECTION_GET_USER_SSL(s),
  3830. s->session))
  3831. SSL_SESSION_free(s->session);
  3832. }
  3833. }
  3834. /* auto flush every 255 connections */
  3835. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
  3836. TSAN_QUALIFIER int *stat;
  3837. if (mode & SSL_SESS_CACHE_CLIENT)
  3838. stat = &s->session_ctx->stats.sess_connect_good;
  3839. else
  3840. stat = &s->session_ctx->stats.sess_accept_good;
  3841. if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff)
  3842. SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
  3843. }
  3844. }
  3845. const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
  3846. {
  3847. return ctx->method;
  3848. }
  3849. const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
  3850. {
  3851. return s->method;
  3852. }
  3853. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  3854. {
  3855. int ret = 1;
  3856. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3857. /* Not allowed for QUIC */
  3858. if (sc == NULL
  3859. || (s->type != SSL_TYPE_SSL_CONNECTION && s->method != meth)
  3860. || (s->type == SSL_TYPE_SSL_CONNECTION && IS_QUIC_METHOD(meth)))
  3861. return 0;
  3862. if (s->method != meth) {
  3863. const SSL_METHOD *sm = s->method;
  3864. int (*hf) (SSL *) = sc->handshake_func;
  3865. if (sm->version == meth->version)
  3866. s->method = meth;
  3867. else {
  3868. sm->ssl_deinit(s);
  3869. s->method = meth;
  3870. ret = s->method->ssl_init(s);
  3871. }
  3872. if (hf == sm->ssl_connect)
  3873. sc->handshake_func = meth->ssl_connect;
  3874. else if (hf == sm->ssl_accept)
  3875. sc->handshake_func = meth->ssl_accept;
  3876. }
  3877. return ret;
  3878. }
  3879. int SSL_get_error(const SSL *s, int i)
  3880. {
  3881. return ossl_ssl_get_error(s, i, /*check_err=*/1);
  3882. }
  3883. int ossl_ssl_get_error(const SSL *s, int i, int check_err)
  3884. {
  3885. int reason;
  3886. unsigned long l;
  3887. BIO *bio;
  3888. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  3889. if (i > 0)
  3890. return SSL_ERROR_NONE;
  3891. #ifndef OPENSSL_NO_QUIC
  3892. if (IS_QUIC(s)) {
  3893. reason = ossl_quic_get_error(s, i);
  3894. if (reason != SSL_ERROR_NONE)
  3895. return reason;
  3896. }
  3897. #endif
  3898. if (sc == NULL)
  3899. return SSL_ERROR_SSL;
  3900. /*
  3901. * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  3902. * where we do encode the error
  3903. */
  3904. if (check_err && (l = ERR_peek_error()) != 0) {
  3905. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  3906. return SSL_ERROR_SYSCALL;
  3907. else
  3908. return SSL_ERROR_SSL;
  3909. }
  3910. #ifndef OPENSSL_NO_QUIC
  3911. if (!IS_QUIC(s))
  3912. #endif
  3913. {
  3914. if (SSL_want_read(s)) {
  3915. bio = SSL_get_rbio(s);
  3916. if (BIO_should_read(bio))
  3917. return SSL_ERROR_WANT_READ;
  3918. else if (BIO_should_write(bio))
  3919. /*
  3920. * This one doesn't make too much sense ... We never try to
  3921. * write to the rbio, and an application program where rbio and
  3922. * wbio are separate couldn't even know what it should wait for.
  3923. * However if we ever set s->rwstate incorrectly (so that we
  3924. * have SSL_want_read(s) instead of SSL_want_write(s)) and rbio
  3925. * and wbio *are* the same, this test works around that bug; so
  3926. * it might be safer to keep it.
  3927. */
  3928. return SSL_ERROR_WANT_WRITE;
  3929. else if (BIO_should_io_special(bio)) {
  3930. reason = BIO_get_retry_reason(bio);
  3931. if (reason == BIO_RR_CONNECT)
  3932. return SSL_ERROR_WANT_CONNECT;
  3933. else if (reason == BIO_RR_ACCEPT)
  3934. return SSL_ERROR_WANT_ACCEPT;
  3935. else
  3936. return SSL_ERROR_SYSCALL; /* unknown */
  3937. }
  3938. }
  3939. if (SSL_want_write(s)) {
  3940. /*
  3941. * Access wbio directly - in order to use the buffered bio if
  3942. * present
  3943. */
  3944. bio = sc->wbio;
  3945. if (BIO_should_write(bio))
  3946. return SSL_ERROR_WANT_WRITE;
  3947. else if (BIO_should_read(bio))
  3948. /*
  3949. * See above (SSL_want_read(s) with BIO_should_write(bio))
  3950. */
  3951. return SSL_ERROR_WANT_READ;
  3952. else if (BIO_should_io_special(bio)) {
  3953. reason = BIO_get_retry_reason(bio);
  3954. if (reason == BIO_RR_CONNECT)
  3955. return SSL_ERROR_WANT_CONNECT;
  3956. else if (reason == BIO_RR_ACCEPT)
  3957. return SSL_ERROR_WANT_ACCEPT;
  3958. else
  3959. return SSL_ERROR_SYSCALL;
  3960. }
  3961. }
  3962. }
  3963. if (SSL_want_x509_lookup(s))
  3964. return SSL_ERROR_WANT_X509_LOOKUP;
  3965. if (SSL_want_retry_verify(s))
  3966. return SSL_ERROR_WANT_RETRY_VERIFY;
  3967. if (SSL_want_async(s))
  3968. return SSL_ERROR_WANT_ASYNC;
  3969. if (SSL_want_async_job(s))
  3970. return SSL_ERROR_WANT_ASYNC_JOB;
  3971. if (SSL_want_client_hello_cb(s))
  3972. return SSL_ERROR_WANT_CLIENT_HELLO_CB;
  3973. if ((sc->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  3974. (sc->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
  3975. return SSL_ERROR_ZERO_RETURN;
  3976. return SSL_ERROR_SYSCALL;
  3977. }
  3978. static int ssl_do_handshake_intern(void *vargs)
  3979. {
  3980. struct ssl_async_args *args = (struct ssl_async_args *)vargs;
  3981. SSL *s = args->s;
  3982. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3983. if (sc == NULL)
  3984. return -1;
  3985. return sc->handshake_func(s);
  3986. }
  3987. int SSL_do_handshake(SSL *s)
  3988. {
  3989. int ret = 1;
  3990. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  3991. #ifndef OPENSSL_NO_QUIC
  3992. if (IS_QUIC(s))
  3993. return ossl_quic_do_handshake(s);
  3994. #endif
  3995. if (sc->handshake_func == NULL) {
  3996. ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  3997. return -1;
  3998. }
  3999. ossl_statem_check_finish_init(sc, -1);
  4000. s->method->ssl_renegotiate_check(s, 0);
  4001. if (SSL_in_init(s) || SSL_in_before(s)) {
  4002. if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  4003. struct ssl_async_args args;
  4004. memset(&args, 0, sizeof(args));
  4005. args.s = s;
  4006. ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
  4007. } else {
  4008. ret = sc->handshake_func(s);
  4009. }
  4010. }
  4011. return ret;
  4012. }
  4013. void SSL_set_accept_state(SSL *s)
  4014. {
  4015. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  4016. #ifndef OPENSSL_NO_QUIC
  4017. if (IS_QUIC(s)) {
  4018. ossl_quic_set_accept_state(s);
  4019. return;
  4020. }
  4021. #endif
  4022. sc->server = 1;
  4023. sc->shutdown = 0;
  4024. ossl_statem_clear(sc);
  4025. sc->handshake_func = s->method->ssl_accept;
  4026. /* Ignore return value. Its a void public API function */
  4027. RECORD_LAYER_reset(&sc->rlayer);
  4028. }
  4029. void SSL_set_connect_state(SSL *s)
  4030. {
  4031. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  4032. #ifndef OPENSSL_NO_QUIC
  4033. if (IS_QUIC(s)) {
  4034. ossl_quic_set_connect_state(s);
  4035. return;
  4036. }
  4037. #endif
  4038. sc->server = 0;
  4039. sc->shutdown = 0;
  4040. ossl_statem_clear(sc);
  4041. sc->handshake_func = s->method->ssl_connect;
  4042. /* Ignore return value. Its a void public API function */
  4043. RECORD_LAYER_reset(&sc->rlayer);
  4044. }
  4045. int ssl_undefined_function(SSL *s)
  4046. {
  4047. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  4048. return 0;
  4049. }
  4050. int ssl_undefined_void_function(void)
  4051. {
  4052. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  4053. return 0;
  4054. }
  4055. int ssl_undefined_const_function(const SSL *s)
  4056. {
  4057. return 0;
  4058. }
  4059. const SSL_METHOD *ssl_bad_method(int ver)
  4060. {
  4061. ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  4062. return NULL;
  4063. }
  4064. const char *ssl_protocol_to_string(int version)
  4065. {
  4066. switch (version)
  4067. {
  4068. case TLS1_3_VERSION:
  4069. return "TLSv1.3";
  4070. case TLS1_2_VERSION:
  4071. return "TLSv1.2";
  4072. case TLS1_1_VERSION:
  4073. return "TLSv1.1";
  4074. case TLS1_VERSION:
  4075. return "TLSv1";
  4076. case SSL3_VERSION:
  4077. return "SSLv3";
  4078. case DTLS1_BAD_VER:
  4079. return "DTLSv0.9";
  4080. case DTLS1_VERSION:
  4081. return "DTLSv1";
  4082. case DTLS1_2_VERSION:
  4083. return "DTLSv1.2";
  4084. default:
  4085. return "unknown";
  4086. }
  4087. }
  4088. const char *SSL_get_version(const SSL *s)
  4089. {
  4090. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4091. #ifndef OPENSSL_NO_QUIC
  4092. /* We only support QUICv1 - so if its QUIC its QUICv1 */
  4093. if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
  4094. return "QUICv1";
  4095. #endif
  4096. if (sc == NULL)
  4097. return NULL;
  4098. return ssl_protocol_to_string(sc->version);
  4099. }
  4100. __owur int SSL_get_handshake_rtt(const SSL *s, uint64_t *rtt)
  4101. {
  4102. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4103. if (sc == NULL)
  4104. return -1;
  4105. if (sc->ts_msg_write.t <= 0 || sc->ts_msg_read.t <= 0)
  4106. return 0; /* data not (yet) available */
  4107. if (sc->ts_msg_read.t < sc->ts_msg_write.t)
  4108. return -1;
  4109. *rtt = ossl_time2us(ossl_time_subtract(sc->ts_msg_read, sc->ts_msg_write));
  4110. return 1;
  4111. }
  4112. static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
  4113. {
  4114. STACK_OF(X509_NAME) *sk;
  4115. X509_NAME *xn;
  4116. int i;
  4117. if (src == NULL) {
  4118. *dst = NULL;
  4119. return 1;
  4120. }
  4121. if ((sk = sk_X509_NAME_new_null()) == NULL)
  4122. return 0;
  4123. for (i = 0; i < sk_X509_NAME_num(src); i++) {
  4124. xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
  4125. if (xn == NULL) {
  4126. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  4127. return 0;
  4128. }
  4129. if (sk_X509_NAME_insert(sk, xn, i) == 0) {
  4130. X509_NAME_free(xn);
  4131. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  4132. return 0;
  4133. }
  4134. }
  4135. *dst = sk;
  4136. return 1;
  4137. }
  4138. SSL *SSL_dup(SSL *s)
  4139. {
  4140. SSL *ret;
  4141. int i;
  4142. /* TODO(QUIC FUTURE): Add a SSL_METHOD function for duplication */
  4143. SSL_CONNECTION *retsc;
  4144. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  4145. if (sc == NULL)
  4146. return NULL;
  4147. /* If we're not quiescent, just up_ref! */
  4148. if (!SSL_in_init(s) || !SSL_in_before(s)) {
  4149. CRYPTO_UP_REF(&s->references, &i);
  4150. return s;
  4151. }
  4152. /*
  4153. * Otherwise, copy configuration state, and session if set.
  4154. */
  4155. if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  4156. return NULL;
  4157. if ((retsc = SSL_CONNECTION_FROM_SSL_ONLY(ret)) == NULL)
  4158. goto err;
  4159. if (sc->session != NULL) {
  4160. /*
  4161. * Arranges to share the same session via up_ref. This "copies"
  4162. * session-id, SSL_METHOD, sid_ctx, and 'cert'
  4163. */
  4164. if (!SSL_copy_session_id(ret, s))
  4165. goto err;
  4166. } else {
  4167. /*
  4168. * No session has been established yet, so we have to expect that
  4169. * s->cert or ret->cert will be changed later -- they should not both
  4170. * point to the same object, and thus we can't use
  4171. * SSL_copy_session_id.
  4172. */
  4173. if (!SSL_set_ssl_method(ret, s->method))
  4174. goto err;
  4175. if (sc->cert != NULL) {
  4176. ssl_cert_free(retsc->cert);
  4177. retsc->cert = ssl_cert_dup(sc->cert);
  4178. if (retsc->cert == NULL)
  4179. goto err;
  4180. }
  4181. if (!SSL_set_session_id_context(ret, sc->sid_ctx,
  4182. (int)sc->sid_ctx_length))
  4183. goto err;
  4184. }
  4185. if (!ssl_dane_dup(retsc, sc))
  4186. goto err;
  4187. retsc->version = sc->version;
  4188. retsc->options = sc->options;
  4189. retsc->min_proto_version = sc->min_proto_version;
  4190. retsc->max_proto_version = sc->max_proto_version;
  4191. retsc->mode = sc->mode;
  4192. SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
  4193. SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
  4194. retsc->msg_callback = sc->msg_callback;
  4195. retsc->msg_callback_arg = sc->msg_callback_arg;
  4196. SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
  4197. SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
  4198. retsc->generate_session_id = sc->generate_session_id;
  4199. SSL_set_info_callback(ret, SSL_get_info_callback(s));
  4200. /* copy app data, a little dangerous perhaps */
  4201. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  4202. goto err;
  4203. retsc->server = sc->server;
  4204. if (sc->handshake_func) {
  4205. if (sc->server)
  4206. SSL_set_accept_state(ret);
  4207. else
  4208. SSL_set_connect_state(ret);
  4209. }
  4210. retsc->shutdown = sc->shutdown;
  4211. retsc->hit = sc->hit;
  4212. retsc->default_passwd_callback = sc->default_passwd_callback;
  4213. retsc->default_passwd_callback_userdata = sc->default_passwd_callback_userdata;
  4214. X509_VERIFY_PARAM_inherit(retsc->param, sc->param);
  4215. /* dup the cipher_list and cipher_list_by_id stacks */
  4216. if (sc->cipher_list != NULL) {
  4217. if ((retsc->cipher_list = sk_SSL_CIPHER_dup(sc->cipher_list)) == NULL)
  4218. goto err;
  4219. }
  4220. if (sc->cipher_list_by_id != NULL)
  4221. if ((retsc->cipher_list_by_id = sk_SSL_CIPHER_dup(sc->cipher_list_by_id))
  4222. == NULL)
  4223. goto err;
  4224. /* Dup the client_CA list */
  4225. if (!dup_ca_names(&retsc->ca_names, sc->ca_names)
  4226. || !dup_ca_names(&retsc->client_ca_names, sc->client_ca_names))
  4227. goto err;
  4228. return ret;
  4229. err:
  4230. SSL_free(ret);
  4231. return NULL;
  4232. }
  4233. X509 *SSL_get_certificate(const SSL *s)
  4234. {
  4235. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4236. if (sc == NULL)
  4237. return NULL;
  4238. if (sc->cert != NULL)
  4239. return sc->cert->key->x509;
  4240. else
  4241. return NULL;
  4242. }
  4243. EVP_PKEY *SSL_get_privatekey(const SSL *s)
  4244. {
  4245. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4246. if (sc == NULL)
  4247. return NULL;
  4248. if (sc->cert != NULL)
  4249. return sc->cert->key->privatekey;
  4250. else
  4251. return NULL;
  4252. }
  4253. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
  4254. {
  4255. if (ctx->cert != NULL)
  4256. return ctx->cert->key->x509;
  4257. else
  4258. return NULL;
  4259. }
  4260. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
  4261. {
  4262. if (ctx->cert != NULL)
  4263. return ctx->cert->key->privatekey;
  4264. else
  4265. return NULL;
  4266. }
  4267. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  4268. {
  4269. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4270. if (sc == NULL)
  4271. return NULL;
  4272. if ((sc->session != NULL) && (sc->session->cipher != NULL))
  4273. return sc->session->cipher;
  4274. return NULL;
  4275. }
  4276. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
  4277. {
  4278. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4279. if (sc == NULL)
  4280. return NULL;
  4281. return sc->s3.tmp.new_cipher;
  4282. }
  4283. const COMP_METHOD *SSL_get_current_compression(const SSL *s)
  4284. {
  4285. #ifndef OPENSSL_NO_COMP
  4286. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
  4287. if (sc == NULL)
  4288. return NULL;
  4289. return sc->rlayer.wrlmethod->get_compression(sc->rlayer.wrl);
  4290. #else
  4291. return NULL;
  4292. #endif
  4293. }
  4294. const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
  4295. {
  4296. #ifndef OPENSSL_NO_COMP
  4297. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
  4298. if (sc == NULL)
  4299. return NULL;
  4300. return sc->rlayer.rrlmethod->get_compression(sc->rlayer.rrl);
  4301. #else
  4302. return NULL;
  4303. #endif
  4304. }
  4305. int ssl_init_wbio_buffer(SSL_CONNECTION *s)
  4306. {
  4307. BIO *bbio;
  4308. if (s->bbio != NULL) {
  4309. /* Already buffered. */
  4310. return 1;
  4311. }
  4312. bbio = BIO_new(BIO_f_buffer());
  4313. if (bbio == NULL || BIO_set_read_buffer_size(bbio, 1) <= 0) {
  4314. BIO_free(bbio);
  4315. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  4316. return 0;
  4317. }
  4318. s->bbio = bbio;
  4319. s->wbio = BIO_push(bbio, s->wbio);
  4320. s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
  4321. return 1;
  4322. }
  4323. int ssl_free_wbio_buffer(SSL_CONNECTION *s)
  4324. {
  4325. /* callers ensure s is never null */
  4326. if (s->bbio == NULL)
  4327. return 1;
  4328. s->wbio = BIO_pop(s->wbio);
  4329. s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
  4330. BIO_free(s->bbio);
  4331. s->bbio = NULL;
  4332. return 1;
  4333. }
  4334. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
  4335. {
  4336. ctx->quiet_shutdown = mode;
  4337. }
  4338. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  4339. {
  4340. return ctx->quiet_shutdown;
  4341. }
  4342. void SSL_set_quiet_shutdown(SSL *s, int mode)
  4343. {
  4344. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  4345. /* Not supported with QUIC */
  4346. if (sc == NULL)
  4347. return;
  4348. sc->quiet_shutdown = mode;
  4349. }
  4350. int SSL_get_quiet_shutdown(const SSL *s)
  4351. {
  4352. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
  4353. /* Not supported with QUIC */
  4354. if (sc == NULL)
  4355. return 0;
  4356. return sc->quiet_shutdown;
  4357. }
  4358. void SSL_set_shutdown(SSL *s, int mode)
  4359. {
  4360. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  4361. /* Not supported with QUIC */
  4362. if (sc == NULL)
  4363. return;
  4364. sc->shutdown = mode;
  4365. }
  4366. int SSL_get_shutdown(const SSL *s)
  4367. {
  4368. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
  4369. #ifndef OPENSSL_NO_QUIC
  4370. /* QUIC: Just indicate whether the connection was shutdown cleanly. */
  4371. if (IS_QUIC(s))
  4372. return ossl_quic_get_shutdown(s);
  4373. #endif
  4374. if (sc == NULL)
  4375. return 0;
  4376. return sc->shutdown;
  4377. }
  4378. int SSL_version(const SSL *s)
  4379. {
  4380. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4381. #ifndef OPENSSL_NO_QUIC
  4382. /* We only support QUICv1 - so if its QUIC its QUICv1 */
  4383. if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
  4384. return OSSL_QUIC1_VERSION;
  4385. #endif
  4386. if (sc == NULL)
  4387. return 0;
  4388. return sc->version;
  4389. }
  4390. int SSL_client_version(const SSL *s)
  4391. {
  4392. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4393. #ifndef OPENSSL_NO_QUIC
  4394. /* We only support QUICv1 - so if its QUIC its QUICv1 */
  4395. if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
  4396. return OSSL_QUIC1_VERSION;
  4397. #endif
  4398. if (sc == NULL)
  4399. return 0;
  4400. return sc->client_version;
  4401. }
  4402. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  4403. {
  4404. return ssl->ctx;
  4405. }
  4406. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
  4407. {
  4408. CERT *new_cert;
  4409. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  4410. /* TODO(QUIC FUTURE): Add support for QUIC */
  4411. if (sc == NULL)
  4412. return NULL;
  4413. if (ssl->ctx == ctx)
  4414. return ssl->ctx;
  4415. if (ctx == NULL)
  4416. ctx = sc->session_ctx;
  4417. new_cert = ssl_cert_dup(ctx->cert);
  4418. if (new_cert == NULL) {
  4419. return NULL;
  4420. }
  4421. if (!custom_exts_copy_flags(&new_cert->custext, &sc->cert->custext)) {
  4422. ssl_cert_free(new_cert);
  4423. return NULL;
  4424. }
  4425. ssl_cert_free(sc->cert);
  4426. sc->cert = new_cert;
  4427. /*
  4428. * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
  4429. * so setter APIs must prevent invalid lengths from entering the system.
  4430. */
  4431. if (!ossl_assert(sc->sid_ctx_length <= sizeof(sc->sid_ctx)))
  4432. return NULL;
  4433. /*
  4434. * If the session ID context matches that of the parent SSL_CTX,
  4435. * inherit it from the new SSL_CTX as well. If however the context does
  4436. * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
  4437. * leave it unchanged.
  4438. */
  4439. if ((ssl->ctx != NULL) &&
  4440. (sc->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
  4441. (memcmp(sc->sid_ctx, ssl->ctx->sid_ctx, sc->sid_ctx_length) == 0)) {
  4442. sc->sid_ctx_length = ctx->sid_ctx_length;
  4443. memcpy(&sc->sid_ctx, &ctx->sid_ctx, sizeof(sc->sid_ctx));
  4444. }
  4445. SSL_CTX_up_ref(ctx);
  4446. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  4447. ssl->ctx = ctx;
  4448. return ssl->ctx;
  4449. }
  4450. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  4451. {
  4452. return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
  4453. ctx->propq);
  4454. }
  4455. int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
  4456. {
  4457. X509_LOOKUP *lookup;
  4458. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
  4459. if (lookup == NULL)
  4460. return 0;
  4461. /* We ignore errors, in case the directory doesn't exist */
  4462. ERR_set_mark();
  4463. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  4464. ERR_pop_to_mark();
  4465. return 1;
  4466. }
  4467. int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
  4468. {
  4469. X509_LOOKUP *lookup;
  4470. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
  4471. if (lookup == NULL)
  4472. return 0;
  4473. /* We ignore errors, in case the file doesn't exist */
  4474. ERR_set_mark();
  4475. X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
  4476. ctx->propq);
  4477. ERR_pop_to_mark();
  4478. return 1;
  4479. }
  4480. int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
  4481. {
  4482. X509_LOOKUP *lookup;
  4483. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
  4484. if (lookup == NULL)
  4485. return 0;
  4486. /* We ignore errors, in case the directory doesn't exist */
  4487. ERR_set_mark();
  4488. X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
  4489. ERR_pop_to_mark();
  4490. return 1;
  4491. }
  4492. int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
  4493. {
  4494. return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
  4495. ctx->propq);
  4496. }
  4497. int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
  4498. {
  4499. return X509_STORE_load_path(ctx->cert_store, CApath);
  4500. }
  4501. int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
  4502. {
  4503. return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
  4504. ctx->propq);
  4505. }
  4506. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  4507. const char *CApath)
  4508. {
  4509. if (CAfile == NULL && CApath == NULL)
  4510. return 0;
  4511. if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
  4512. return 0;
  4513. if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
  4514. return 0;
  4515. return 1;
  4516. }
  4517. void SSL_set_info_callback(SSL *ssl,
  4518. void (*cb) (const SSL *ssl, int type, int val))
  4519. {
  4520. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  4521. if (sc == NULL)
  4522. return;
  4523. sc->info_callback = cb;
  4524. }
  4525. /*
  4526. * One compiler (Diab DCC) doesn't like argument names in returned function
  4527. * pointer.
  4528. */
  4529. void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
  4530. int /* type */ ,
  4531. int /* val */ ) {
  4532. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  4533. if (sc == NULL)
  4534. return NULL;
  4535. return sc->info_callback;
  4536. }
  4537. void SSL_set_verify_result(SSL *ssl, long arg)
  4538. {
  4539. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  4540. if (sc == NULL)
  4541. return;
  4542. sc->verify_result = arg;
  4543. }
  4544. long SSL_get_verify_result(const SSL *ssl)
  4545. {
  4546. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  4547. if (sc == NULL)
  4548. return 0;
  4549. return sc->verify_result;
  4550. }
  4551. size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
  4552. {
  4553. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  4554. if (sc == NULL)
  4555. return 0;
  4556. if (outlen == 0)
  4557. return sizeof(sc->s3.client_random);
  4558. if (outlen > sizeof(sc->s3.client_random))
  4559. outlen = sizeof(sc->s3.client_random);
  4560. memcpy(out, sc->s3.client_random, outlen);
  4561. return outlen;
  4562. }
  4563. size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
  4564. {
  4565. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  4566. if (sc == NULL)
  4567. return 0;
  4568. if (outlen == 0)
  4569. return sizeof(sc->s3.server_random);
  4570. if (outlen > sizeof(sc->s3.server_random))
  4571. outlen = sizeof(sc->s3.server_random);
  4572. memcpy(out, sc->s3.server_random, outlen);
  4573. return outlen;
  4574. }
  4575. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
  4576. unsigned char *out, size_t outlen)
  4577. {
  4578. if (outlen == 0)
  4579. return session->master_key_length;
  4580. if (outlen > session->master_key_length)
  4581. outlen = session->master_key_length;
  4582. memcpy(out, session->master_key, outlen);
  4583. return outlen;
  4584. }
  4585. int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
  4586. size_t len)
  4587. {
  4588. if (len > sizeof(sess->master_key))
  4589. return 0;
  4590. memcpy(sess->master_key, in, len);
  4591. sess->master_key_length = len;
  4592. return 1;
  4593. }
  4594. int SSL_set_ex_data(SSL *s, int idx, void *arg)
  4595. {
  4596. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  4597. }
  4598. void *SSL_get_ex_data(const SSL *s, int idx)
  4599. {
  4600. return CRYPTO_get_ex_data(&s->ex_data, idx);
  4601. }
  4602. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
  4603. {
  4604. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  4605. }
  4606. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
  4607. {
  4608. return CRYPTO_get_ex_data(&s->ex_data, idx);
  4609. }
  4610. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  4611. {
  4612. return ctx->cert_store;
  4613. }
  4614. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
  4615. {
  4616. X509_STORE_free(ctx->cert_store);
  4617. ctx->cert_store = store;
  4618. }
  4619. void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
  4620. {
  4621. if (store != NULL)
  4622. X509_STORE_up_ref(store);
  4623. SSL_CTX_set_cert_store(ctx, store);
  4624. }
  4625. int SSL_want(const SSL *s)
  4626. {
  4627. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4628. #ifndef OPENSSL_NO_QUIC
  4629. if (IS_QUIC(s))
  4630. return ossl_quic_want(s);
  4631. #endif
  4632. if (sc == NULL)
  4633. return SSL_NOTHING;
  4634. return sc->rwstate;
  4635. }
  4636. #ifndef OPENSSL_NO_PSK
  4637. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  4638. {
  4639. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  4640. ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  4641. return 0;
  4642. }
  4643. OPENSSL_free(ctx->cert->psk_identity_hint);
  4644. if (identity_hint != NULL) {
  4645. ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  4646. if (ctx->cert->psk_identity_hint == NULL)
  4647. return 0;
  4648. } else
  4649. ctx->cert->psk_identity_hint = NULL;
  4650. return 1;
  4651. }
  4652. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  4653. {
  4654. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4655. if (sc == NULL)
  4656. return 0;
  4657. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  4658. ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  4659. return 0;
  4660. }
  4661. OPENSSL_free(sc->cert->psk_identity_hint);
  4662. if (identity_hint != NULL) {
  4663. sc->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  4664. if (sc->cert->psk_identity_hint == NULL)
  4665. return 0;
  4666. } else
  4667. sc->cert->psk_identity_hint = NULL;
  4668. return 1;
  4669. }
  4670. const char *SSL_get_psk_identity_hint(const SSL *s)
  4671. {
  4672. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4673. if (sc == NULL || sc->session == NULL)
  4674. return NULL;
  4675. return sc->session->psk_identity_hint;
  4676. }
  4677. const char *SSL_get_psk_identity(const SSL *s)
  4678. {
  4679. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4680. if (sc == NULL || sc->session == NULL)
  4681. return NULL;
  4682. return sc->session->psk_identity;
  4683. }
  4684. void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
  4685. {
  4686. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4687. if (sc == NULL)
  4688. return;
  4689. sc->psk_client_callback = cb;
  4690. }
  4691. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
  4692. {
  4693. ctx->psk_client_callback = cb;
  4694. }
  4695. void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
  4696. {
  4697. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4698. if (sc == NULL)
  4699. return;
  4700. sc->psk_server_callback = cb;
  4701. }
  4702. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
  4703. {
  4704. ctx->psk_server_callback = cb;
  4705. }
  4706. #endif
  4707. void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
  4708. {
  4709. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4710. if (sc == NULL)
  4711. return;
  4712. sc->psk_find_session_cb = cb;
  4713. }
  4714. void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
  4715. SSL_psk_find_session_cb_func cb)
  4716. {
  4717. ctx->psk_find_session_cb = cb;
  4718. }
  4719. void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
  4720. {
  4721. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4722. if (sc == NULL)
  4723. return;
  4724. sc->psk_use_session_cb = cb;
  4725. }
  4726. void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
  4727. SSL_psk_use_session_cb_func cb)
  4728. {
  4729. ctx->psk_use_session_cb = cb;
  4730. }
  4731. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  4732. void (*cb) (int write_p, int version,
  4733. int content_type, const void *buf,
  4734. size_t len, SSL *ssl, void *arg))
  4735. {
  4736. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  4737. }
  4738. void SSL_set_msg_callback(SSL *ssl,
  4739. void (*cb) (int write_p, int version,
  4740. int content_type, const void *buf,
  4741. size_t len, SSL *ssl, void *arg))
  4742. {
  4743. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  4744. }
  4745. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  4746. int (*cb) (SSL *ssl,
  4747. int
  4748. is_forward_secure))
  4749. {
  4750. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  4751. (void (*)(void))cb);
  4752. }
  4753. void SSL_set_not_resumable_session_callback(SSL *ssl,
  4754. int (*cb) (SSL *ssl,
  4755. int is_forward_secure))
  4756. {
  4757. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  4758. (void (*)(void))cb);
  4759. }
  4760. void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
  4761. size_t (*cb) (SSL *ssl, int type,
  4762. size_t len, void *arg))
  4763. {
  4764. ctx->record_padding_cb = cb;
  4765. }
  4766. void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
  4767. {
  4768. ctx->record_padding_arg = arg;
  4769. }
  4770. void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
  4771. {
  4772. return ctx->record_padding_arg;
  4773. }
  4774. int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
  4775. {
  4776. if (IS_QUIC_CTX(ctx) && block_size > 1)
  4777. return 0;
  4778. /* block size of 0 or 1 is basically no padding */
  4779. if (block_size == 1)
  4780. ctx->block_padding = 0;
  4781. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  4782. ctx->block_padding = block_size;
  4783. else
  4784. return 0;
  4785. return 1;
  4786. }
  4787. int SSL_set_record_padding_callback(SSL *ssl,
  4788. size_t (*cb) (SSL *ssl, int type,
  4789. size_t len, void *arg))
  4790. {
  4791. BIO *b;
  4792. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  4793. if (sc == NULL)
  4794. return 0;
  4795. b = SSL_get_wbio(ssl);
  4796. if (b == NULL || !BIO_get_ktls_send(b)) {
  4797. sc->rlayer.record_padding_cb = cb;
  4798. return 1;
  4799. }
  4800. return 0;
  4801. }
  4802. void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
  4803. {
  4804. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  4805. if (sc == NULL)
  4806. return;
  4807. sc->rlayer.record_padding_arg = arg;
  4808. }
  4809. void *SSL_get_record_padding_callback_arg(const SSL *ssl)
  4810. {
  4811. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  4812. if (sc == NULL)
  4813. return NULL;
  4814. return sc->rlayer.record_padding_arg;
  4815. }
  4816. int SSL_set_block_padding(SSL *ssl, size_t block_size)
  4817. {
  4818. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  4819. if (sc == NULL || (IS_QUIC(ssl) && block_size > 1))
  4820. return 0;
  4821. /* block size of 0 or 1 is basically no padding */
  4822. if (block_size == 1)
  4823. sc->rlayer.block_padding = 0;
  4824. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  4825. sc->rlayer.block_padding = block_size;
  4826. else
  4827. return 0;
  4828. return 1;
  4829. }
  4830. int SSL_set_num_tickets(SSL *s, size_t num_tickets)
  4831. {
  4832. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4833. if (sc == NULL)
  4834. return 0;
  4835. sc->num_tickets = num_tickets;
  4836. return 1;
  4837. }
  4838. size_t SSL_get_num_tickets(const SSL *s)
  4839. {
  4840. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4841. if (sc == NULL)
  4842. return 0;
  4843. return sc->num_tickets;
  4844. }
  4845. int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
  4846. {
  4847. ctx->num_tickets = num_tickets;
  4848. return 1;
  4849. }
  4850. size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
  4851. {
  4852. return ctx->num_tickets;
  4853. }
  4854. /* Retrieve handshake hashes */
  4855. int ssl_handshake_hash(SSL_CONNECTION *s,
  4856. unsigned char *out, size_t outlen,
  4857. size_t *hashlen)
  4858. {
  4859. EVP_MD_CTX *ctx = NULL;
  4860. EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
  4861. int hashleni = EVP_MD_CTX_get_size(hdgst);
  4862. int ret = 0;
  4863. if (hashleni < 0 || (size_t)hashleni > outlen) {
  4864. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4865. goto err;
  4866. }
  4867. ctx = EVP_MD_CTX_new();
  4868. if (ctx == NULL) {
  4869. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4870. goto err;
  4871. }
  4872. if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
  4873. || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
  4874. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  4875. goto err;
  4876. }
  4877. *hashlen = hashleni;
  4878. ret = 1;
  4879. err:
  4880. EVP_MD_CTX_free(ctx);
  4881. return ret;
  4882. }
  4883. int SSL_session_reused(const SSL *s)
  4884. {
  4885. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4886. if (sc == NULL)
  4887. return 0;
  4888. return sc->hit;
  4889. }
  4890. int SSL_is_server(const SSL *s)
  4891. {
  4892. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4893. if (sc == NULL)
  4894. return 0;
  4895. return sc->server;
  4896. }
  4897. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  4898. void SSL_set_debug(SSL *s, int debug)
  4899. {
  4900. /* Old function was do-nothing anyway... */
  4901. (void)s;
  4902. (void)debug;
  4903. }
  4904. #endif
  4905. void SSL_set_security_level(SSL *s, int level)
  4906. {
  4907. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4908. if (sc == NULL)
  4909. return;
  4910. sc->cert->sec_level = level;
  4911. }
  4912. int SSL_get_security_level(const SSL *s)
  4913. {
  4914. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4915. if (sc == NULL)
  4916. return 0;
  4917. return sc->cert->sec_level;
  4918. }
  4919. void SSL_set_security_callback(SSL *s,
  4920. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4921. int op, int bits, int nid,
  4922. void *other, void *ex))
  4923. {
  4924. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4925. if (sc == NULL)
  4926. return;
  4927. sc->cert->sec_cb = cb;
  4928. }
  4929. int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
  4930. const SSL_CTX *ctx, int op,
  4931. int bits, int nid, void *other,
  4932. void *ex) {
  4933. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4934. if (sc == NULL)
  4935. return NULL;
  4936. return sc->cert->sec_cb;
  4937. }
  4938. void SSL_set0_security_ex_data(SSL *s, void *ex)
  4939. {
  4940. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  4941. if (sc == NULL)
  4942. return;
  4943. sc->cert->sec_ex = ex;
  4944. }
  4945. void *SSL_get0_security_ex_data(const SSL *s)
  4946. {
  4947. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4948. if (sc == NULL)
  4949. return NULL;
  4950. return sc->cert->sec_ex;
  4951. }
  4952. void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
  4953. {
  4954. ctx->cert->sec_level = level;
  4955. }
  4956. int SSL_CTX_get_security_level(const SSL_CTX *ctx)
  4957. {
  4958. return ctx->cert->sec_level;
  4959. }
  4960. void SSL_CTX_set_security_callback(SSL_CTX *ctx,
  4961. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4962. int op, int bits, int nid,
  4963. void *other, void *ex))
  4964. {
  4965. ctx->cert->sec_cb = cb;
  4966. }
  4967. int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
  4968. const SSL_CTX *ctx,
  4969. int op, int bits,
  4970. int nid,
  4971. void *other,
  4972. void *ex) {
  4973. return ctx->cert->sec_cb;
  4974. }
  4975. void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
  4976. {
  4977. ctx->cert->sec_ex = ex;
  4978. }
  4979. void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
  4980. {
  4981. return ctx->cert->sec_ex;
  4982. }
  4983. uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
  4984. {
  4985. return ctx->options;
  4986. }
  4987. uint64_t SSL_get_options(const SSL *s)
  4988. {
  4989. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  4990. #ifndef OPENSSL_NO_QUIC
  4991. if (IS_QUIC(s))
  4992. return ossl_quic_get_options(s);
  4993. #endif
  4994. if (sc == NULL)
  4995. return 0;
  4996. return sc->options;
  4997. }
  4998. uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
  4999. {
  5000. return ctx->options |= op;
  5001. }
  5002. uint64_t SSL_set_options(SSL *s, uint64_t op)
  5003. {
  5004. SSL_CONNECTION *sc;
  5005. OSSL_PARAM options[2], *opts = options;
  5006. #ifndef OPENSSL_NO_QUIC
  5007. if (IS_QUIC(s))
  5008. return ossl_quic_set_options(s, op);
  5009. #endif
  5010. sc = SSL_CONNECTION_FROM_SSL(s);
  5011. if (sc == NULL)
  5012. return 0;
  5013. sc->options |= op;
  5014. *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
  5015. &sc->options);
  5016. *opts = OSSL_PARAM_construct_end();
  5017. /* Ignore return value */
  5018. sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
  5019. sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
  5020. return sc->options;
  5021. }
  5022. uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
  5023. {
  5024. return ctx->options &= ~op;
  5025. }
  5026. uint64_t SSL_clear_options(SSL *s, uint64_t op)
  5027. {
  5028. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5029. OSSL_PARAM options[2], *opts = options;
  5030. #ifndef OPENSSL_NO_QUIC
  5031. if (IS_QUIC(s))
  5032. return ossl_quic_clear_options(s, op);
  5033. #endif
  5034. if (sc == NULL)
  5035. return 0;
  5036. sc->options &= ~op;
  5037. *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
  5038. &sc->options);
  5039. *opts = OSSL_PARAM_construct_end();
  5040. /* Ignore return value */
  5041. sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
  5042. sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
  5043. return sc->options;
  5044. }
  5045. STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
  5046. {
  5047. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  5048. if (sc == NULL)
  5049. return NULL;
  5050. return sc->verified_chain;
  5051. }
  5052. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
  5053. #ifndef OPENSSL_NO_CT
  5054. /*
  5055. * Moves SCTs from the |src| stack to the |dst| stack.
  5056. * The source of each SCT will be set to |origin|.
  5057. * If |dst| points to a NULL pointer, a new stack will be created and owned by
  5058. * the caller.
  5059. * Returns the number of SCTs moved, or a negative integer if an error occurs.
  5060. * The |dst| stack is created and possibly partially populated even in case
  5061. * of error, likewise the |src| stack may be left in an intermediate state.
  5062. */
  5063. static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
  5064. sct_source_t origin)
  5065. {
  5066. int scts_moved = 0;
  5067. SCT *sct = NULL;
  5068. if (*dst == NULL) {
  5069. *dst = sk_SCT_new_null();
  5070. if (*dst == NULL) {
  5071. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  5072. goto err;
  5073. }
  5074. }
  5075. while ((sct = sk_SCT_pop(src)) != NULL) {
  5076. if (SCT_set_source(sct, origin) != 1)
  5077. goto err;
  5078. if (!sk_SCT_push(*dst, sct))
  5079. goto err;
  5080. scts_moved += 1;
  5081. }
  5082. return scts_moved;
  5083. err:
  5084. SCT_free(sct);
  5085. return -1;
  5086. }
  5087. /*
  5088. * Look for data collected during ServerHello and parse if found.
  5089. * Returns the number of SCTs extracted.
  5090. */
  5091. static int ct_extract_tls_extension_scts(SSL_CONNECTION *s)
  5092. {
  5093. int scts_extracted = 0;
  5094. if (s->ext.scts != NULL) {
  5095. const unsigned char *p = s->ext.scts;
  5096. STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
  5097. scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
  5098. SCT_LIST_free(scts);
  5099. }
  5100. return scts_extracted;
  5101. }
  5102. /*
  5103. * Checks for an OCSP response and then attempts to extract any SCTs found if it
  5104. * contains an SCT X509 extension. They will be stored in |s->scts|.
  5105. * Returns:
  5106. * - The number of SCTs extracted, assuming an OCSP response exists.
  5107. * - 0 if no OCSP response exists or it contains no SCTs.
  5108. * - A negative integer if an error occurs.
  5109. */
  5110. static int ct_extract_ocsp_response_scts(SSL_CONNECTION *s)
  5111. {
  5112. # ifndef OPENSSL_NO_OCSP
  5113. int scts_extracted = 0;
  5114. const unsigned char *p;
  5115. OCSP_BASICRESP *br = NULL;
  5116. OCSP_RESPONSE *rsp = NULL;
  5117. STACK_OF(SCT) *scts = NULL;
  5118. int i;
  5119. if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
  5120. goto err;
  5121. p = s->ext.ocsp.resp;
  5122. rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
  5123. if (rsp == NULL)
  5124. goto err;
  5125. br = OCSP_response_get1_basic(rsp);
  5126. if (br == NULL)
  5127. goto err;
  5128. for (i = 0; i < OCSP_resp_count(br); ++i) {
  5129. OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
  5130. if (single == NULL)
  5131. continue;
  5132. scts =
  5133. OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
  5134. scts_extracted =
  5135. ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
  5136. if (scts_extracted < 0)
  5137. goto err;
  5138. }
  5139. err:
  5140. SCT_LIST_free(scts);
  5141. OCSP_BASICRESP_free(br);
  5142. OCSP_RESPONSE_free(rsp);
  5143. return scts_extracted;
  5144. # else
  5145. /* Behave as if no OCSP response exists */
  5146. return 0;
  5147. # endif
  5148. }
  5149. /*
  5150. * Attempts to extract SCTs from the peer certificate.
  5151. * Return the number of SCTs extracted, or a negative integer if an error
  5152. * occurs.
  5153. */
  5154. static int ct_extract_x509v3_extension_scts(SSL_CONNECTION *s)
  5155. {
  5156. int scts_extracted = 0;
  5157. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  5158. if (cert != NULL) {
  5159. STACK_OF(SCT) *scts =
  5160. X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
  5161. scts_extracted =
  5162. ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
  5163. SCT_LIST_free(scts);
  5164. }
  5165. return scts_extracted;
  5166. }
  5167. /*
  5168. * Attempts to find all received SCTs by checking TLS extensions, the OCSP
  5169. * response (if it exists) and X509v3 extensions in the certificate.
  5170. * Returns NULL if an error occurs.
  5171. */
  5172. const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
  5173. {
  5174. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5175. if (sc == NULL)
  5176. return NULL;
  5177. if (!sc->scts_parsed) {
  5178. if (ct_extract_tls_extension_scts(sc) < 0 ||
  5179. ct_extract_ocsp_response_scts(sc) < 0 ||
  5180. ct_extract_x509v3_extension_scts(sc) < 0)
  5181. goto err;
  5182. sc->scts_parsed = 1;
  5183. }
  5184. return sc->scts;
  5185. err:
  5186. return NULL;
  5187. }
  5188. static int ct_permissive(const CT_POLICY_EVAL_CTX *ctx,
  5189. const STACK_OF(SCT) *scts, void *unused_arg)
  5190. {
  5191. return 1;
  5192. }
  5193. static int ct_strict(const CT_POLICY_EVAL_CTX *ctx,
  5194. const STACK_OF(SCT) *scts, void *unused_arg)
  5195. {
  5196. int count = scts != NULL ? sk_SCT_num(scts) : 0;
  5197. int i;
  5198. for (i = 0; i < count; ++i) {
  5199. SCT *sct = sk_SCT_value(scts, i);
  5200. int status = SCT_get_validation_status(sct);
  5201. if (status == SCT_VALIDATION_STATUS_VALID)
  5202. return 1;
  5203. }
  5204. ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
  5205. return 0;
  5206. }
  5207. int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
  5208. void *arg)
  5209. {
  5210. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5211. if (sc == NULL)
  5212. return 0;
  5213. /*
  5214. * Since code exists that uses the custom extension handler for CT, look
  5215. * for this and throw an error if they have already registered to use CT.
  5216. */
  5217. if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
  5218. TLSEXT_TYPE_signed_certificate_timestamp))
  5219. {
  5220. ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  5221. return 0;
  5222. }
  5223. if (callback != NULL) {
  5224. /*
  5225. * If we are validating CT, then we MUST accept SCTs served via OCSP
  5226. */
  5227. if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
  5228. return 0;
  5229. }
  5230. sc->ct_validation_callback = callback;
  5231. sc->ct_validation_callback_arg = arg;
  5232. return 1;
  5233. }
  5234. int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
  5235. ssl_ct_validation_cb callback, void *arg)
  5236. {
  5237. /*
  5238. * Since code exists that uses the custom extension handler for CT, look for
  5239. * this and throw an error if they have already registered to use CT.
  5240. */
  5241. if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
  5242. TLSEXT_TYPE_signed_certificate_timestamp))
  5243. {
  5244. ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  5245. return 0;
  5246. }
  5247. ctx->ct_validation_callback = callback;
  5248. ctx->ct_validation_callback_arg = arg;
  5249. return 1;
  5250. }
  5251. int SSL_ct_is_enabled(const SSL *s)
  5252. {
  5253. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  5254. if (sc == NULL)
  5255. return 0;
  5256. return sc->ct_validation_callback != NULL;
  5257. }
  5258. int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
  5259. {
  5260. return ctx->ct_validation_callback != NULL;
  5261. }
  5262. int ssl_validate_ct(SSL_CONNECTION *s)
  5263. {
  5264. int ret = 0;
  5265. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  5266. X509 *issuer;
  5267. SSL_DANE *dane = &s->dane;
  5268. CT_POLICY_EVAL_CTX *ctx = NULL;
  5269. const STACK_OF(SCT) *scts;
  5270. /*
  5271. * If no callback is set, the peer is anonymous, or its chain is invalid,
  5272. * skip SCT validation - just return success. Applications that continue
  5273. * handshakes without certificates, with unverified chains, or pinned leaf
  5274. * certificates are outside the scope of the WebPKI and CT.
  5275. *
  5276. * The above exclusions notwithstanding the vast majority of peers will
  5277. * have rather ordinary certificate chains validated by typical
  5278. * applications that perform certificate verification and therefore will
  5279. * process SCTs when enabled.
  5280. */
  5281. if (s->ct_validation_callback == NULL || cert == NULL ||
  5282. s->verify_result != X509_V_OK ||
  5283. s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
  5284. return 1;
  5285. /*
  5286. * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
  5287. * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
  5288. */
  5289. if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
  5290. switch (dane->mtlsa->usage) {
  5291. case DANETLS_USAGE_DANE_TA:
  5292. case DANETLS_USAGE_DANE_EE:
  5293. return 1;
  5294. }
  5295. }
  5296. ctx = CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
  5297. SSL_CONNECTION_GET_CTX(s)->propq);
  5298. if (ctx == NULL) {
  5299. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CT_LIB);
  5300. goto end;
  5301. }
  5302. issuer = sk_X509_value(s->verified_chain, 1);
  5303. CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
  5304. CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
  5305. CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx,
  5306. SSL_CONNECTION_GET_CTX(s)->ctlog_store);
  5307. CT_POLICY_EVAL_CTX_set_time(
  5308. ctx, (uint64_t)SSL_SESSION_get_time(s->session) * 1000);
  5309. scts = SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s));
  5310. /*
  5311. * This function returns success (> 0) only when all the SCTs are valid, 0
  5312. * when some are invalid, and < 0 on various internal errors (out of
  5313. * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
  5314. * reason to abort the handshake, that decision is up to the callback.
  5315. * Therefore, we error out only in the unexpected case that the return
  5316. * value is negative.
  5317. *
  5318. * XXX: One might well argue that the return value of this function is an
  5319. * unfortunate design choice. Its job is only to determine the validation
  5320. * status of each of the provided SCTs. So long as it correctly separates
  5321. * the wheat from the chaff it should return success. Failure in this case
  5322. * ought to correspond to an inability to carry out its duties.
  5323. */
  5324. if (SCT_LIST_validate(scts, ctx) < 0) {
  5325. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
  5326. goto end;
  5327. }
  5328. ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
  5329. if (ret < 0)
  5330. ret = 0; /* This function returns 0 on failure */
  5331. if (!ret)
  5332. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
  5333. end:
  5334. CT_POLICY_EVAL_CTX_free(ctx);
  5335. /*
  5336. * With SSL_VERIFY_NONE the session may be cached and re-used despite a
  5337. * failure return code here. Also the application may wish the complete
  5338. * the handshake, and then disconnect cleanly at a higher layer, after
  5339. * checking the verification status of the completed connection.
  5340. *
  5341. * We therefore force a certificate verification failure which will be
  5342. * visible via SSL_get_verify_result() and cached as part of any resumed
  5343. * session.
  5344. *
  5345. * Note: the permissive callback is for information gathering only, always
  5346. * returns success, and does not affect verification status. Only the
  5347. * strict callback or a custom application-specified callback can trigger
  5348. * connection failure or record a verification error.
  5349. */
  5350. if (ret <= 0)
  5351. s->verify_result = X509_V_ERR_NO_VALID_SCTS;
  5352. return ret;
  5353. }
  5354. int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
  5355. {
  5356. switch (validation_mode) {
  5357. default:
  5358. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
  5359. return 0;
  5360. case SSL_CT_VALIDATION_PERMISSIVE:
  5361. return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
  5362. case SSL_CT_VALIDATION_STRICT:
  5363. return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
  5364. }
  5365. }
  5366. int SSL_enable_ct(SSL *s, int validation_mode)
  5367. {
  5368. switch (validation_mode) {
  5369. default:
  5370. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
  5371. return 0;
  5372. case SSL_CT_VALIDATION_PERMISSIVE:
  5373. return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
  5374. case SSL_CT_VALIDATION_STRICT:
  5375. return SSL_set_ct_validation_callback(s, ct_strict, NULL);
  5376. }
  5377. }
  5378. int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
  5379. {
  5380. return CTLOG_STORE_load_default_file(ctx->ctlog_store);
  5381. }
  5382. int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
  5383. {
  5384. return CTLOG_STORE_load_file(ctx->ctlog_store, path);
  5385. }
  5386. void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE *logs)
  5387. {
  5388. CTLOG_STORE_free(ctx->ctlog_store);
  5389. ctx->ctlog_store = logs;
  5390. }
  5391. const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
  5392. {
  5393. return ctx->ctlog_store;
  5394. }
  5395. #endif /* OPENSSL_NO_CT */
  5396. void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
  5397. void *arg)
  5398. {
  5399. c->client_hello_cb = cb;
  5400. c->client_hello_cb_arg = arg;
  5401. }
  5402. int SSL_client_hello_isv2(SSL *s)
  5403. {
  5404. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5405. if (sc == NULL)
  5406. return 0;
  5407. if (sc->clienthello == NULL)
  5408. return 0;
  5409. return sc->clienthello->isv2;
  5410. }
  5411. unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
  5412. {
  5413. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5414. if (sc == NULL)
  5415. return 0;
  5416. if (sc->clienthello == NULL)
  5417. return 0;
  5418. return sc->clienthello->legacy_version;
  5419. }
  5420. size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
  5421. {
  5422. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5423. if (sc == NULL)
  5424. return 0;
  5425. if (sc->clienthello == NULL)
  5426. return 0;
  5427. if (out != NULL)
  5428. *out = sc->clienthello->random;
  5429. return SSL3_RANDOM_SIZE;
  5430. }
  5431. size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
  5432. {
  5433. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5434. if (sc == NULL)
  5435. return 0;
  5436. if (sc->clienthello == NULL)
  5437. return 0;
  5438. if (out != NULL)
  5439. *out = sc->clienthello->session_id;
  5440. return sc->clienthello->session_id_len;
  5441. }
  5442. size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
  5443. {
  5444. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5445. if (sc == NULL)
  5446. return 0;
  5447. if (sc->clienthello == NULL)
  5448. return 0;
  5449. if (out != NULL)
  5450. *out = PACKET_data(&sc->clienthello->ciphersuites);
  5451. return PACKET_remaining(&sc->clienthello->ciphersuites);
  5452. }
  5453. size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
  5454. {
  5455. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5456. if (sc == NULL)
  5457. return 0;
  5458. if (sc->clienthello == NULL)
  5459. return 0;
  5460. if (out != NULL)
  5461. *out = sc->clienthello->compressions;
  5462. return sc->clienthello->compressions_len;
  5463. }
  5464. int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
  5465. {
  5466. RAW_EXTENSION *ext;
  5467. int *present;
  5468. size_t num = 0, i;
  5469. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5470. if (sc == NULL)
  5471. return 0;
  5472. if (sc->clienthello == NULL || out == NULL || outlen == NULL)
  5473. return 0;
  5474. for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
  5475. ext = sc->clienthello->pre_proc_exts + i;
  5476. if (ext->present)
  5477. num++;
  5478. }
  5479. if (num == 0) {
  5480. *out = NULL;
  5481. *outlen = 0;
  5482. return 1;
  5483. }
  5484. if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL)
  5485. return 0;
  5486. for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
  5487. ext = sc->clienthello->pre_proc_exts + i;
  5488. if (ext->present) {
  5489. if (ext->received_order >= num)
  5490. goto err;
  5491. present[ext->received_order] = ext->type;
  5492. }
  5493. }
  5494. *out = present;
  5495. *outlen = num;
  5496. return 1;
  5497. err:
  5498. OPENSSL_free(present);
  5499. return 0;
  5500. }
  5501. int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts, size_t *num_exts)
  5502. {
  5503. RAW_EXTENSION *ext;
  5504. size_t num = 0, i;
  5505. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5506. if (sc == NULL)
  5507. return 0;
  5508. if (sc->clienthello == NULL || num_exts == NULL)
  5509. return 0;
  5510. for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
  5511. ext = sc->clienthello->pre_proc_exts + i;
  5512. if (ext->present)
  5513. num++;
  5514. }
  5515. if (num == 0) {
  5516. *num_exts = 0;
  5517. return 1;
  5518. }
  5519. if (exts == NULL) {
  5520. *num_exts = num;
  5521. return 1;
  5522. }
  5523. if (*num_exts < num)
  5524. return 0;
  5525. for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
  5526. ext = sc->clienthello->pre_proc_exts + i;
  5527. if (ext->present) {
  5528. if (ext->received_order >= num)
  5529. return 0;
  5530. exts[ext->received_order] = ext->type;
  5531. }
  5532. }
  5533. *num_exts = num;
  5534. return 1;
  5535. }
  5536. int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
  5537. size_t *outlen)
  5538. {
  5539. size_t i;
  5540. RAW_EXTENSION *r;
  5541. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5542. if (sc == NULL)
  5543. return 0;
  5544. if (sc->clienthello == NULL)
  5545. return 0;
  5546. for (i = 0; i < sc->clienthello->pre_proc_exts_len; ++i) {
  5547. r = sc->clienthello->pre_proc_exts + i;
  5548. if (r->present && r->type == type) {
  5549. if (out != NULL)
  5550. *out = PACKET_data(&r->data);
  5551. if (outlen != NULL)
  5552. *outlen = PACKET_remaining(&r->data);
  5553. return 1;
  5554. }
  5555. }
  5556. return 0;
  5557. }
  5558. int SSL_free_buffers(SSL *ssl)
  5559. {
  5560. RECORD_LAYER *rl;
  5561. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  5562. if (sc == NULL)
  5563. return 0;
  5564. rl = &sc->rlayer;
  5565. return rl->rrlmethod->free_buffers(rl->rrl)
  5566. && rl->wrlmethod->free_buffers(rl->wrl);
  5567. }
  5568. int SSL_alloc_buffers(SSL *ssl)
  5569. {
  5570. RECORD_LAYER *rl;
  5571. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  5572. if (sc == NULL)
  5573. return 0;
  5574. /* QUIC always has buffers allocated. */
  5575. if (IS_QUIC(ssl))
  5576. return 1;
  5577. rl = &sc->rlayer;
  5578. return rl->rrlmethod->alloc_buffers(rl->rrl)
  5579. && rl->wrlmethod->alloc_buffers(rl->wrl);
  5580. }
  5581. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
  5582. {
  5583. ctx->keylog_callback = cb;
  5584. }
  5585. SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
  5586. {
  5587. return ctx->keylog_callback;
  5588. }
  5589. static int nss_keylog_int(const char *prefix,
  5590. SSL_CONNECTION *sc,
  5591. const uint8_t *parameter_1,
  5592. size_t parameter_1_len,
  5593. const uint8_t *parameter_2,
  5594. size_t parameter_2_len)
  5595. {
  5596. char *out = NULL;
  5597. char *cursor = NULL;
  5598. size_t out_len = 0;
  5599. size_t i;
  5600. size_t prefix_len;
  5601. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
  5602. if (sctx->keylog_callback == NULL)
  5603. return 1;
  5604. /*
  5605. * Our output buffer will contain the following strings, rendered with
  5606. * space characters in between, terminated by a NULL character: first the
  5607. * prefix, then the first parameter, then the second parameter. The
  5608. * meaning of each parameter depends on the specific key material being
  5609. * logged. Note that the first and second parameters are encoded in
  5610. * hexadecimal, so we need a buffer that is twice their lengths.
  5611. */
  5612. prefix_len = strlen(prefix);
  5613. out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
  5614. if ((out = cursor = OPENSSL_malloc(out_len)) == NULL)
  5615. return 0;
  5616. strcpy(cursor, prefix);
  5617. cursor += prefix_len;
  5618. *cursor++ = ' ';
  5619. for (i = 0; i < parameter_1_len; i++) {
  5620. sprintf(cursor, "%02x", parameter_1[i]);
  5621. cursor += 2;
  5622. }
  5623. *cursor++ = ' ';
  5624. for (i = 0; i < parameter_2_len; i++) {
  5625. sprintf(cursor, "%02x", parameter_2[i]);
  5626. cursor += 2;
  5627. }
  5628. *cursor = '\0';
  5629. sctx->keylog_callback(SSL_CONNECTION_GET_USER_SSL(sc), (const char *)out);
  5630. OPENSSL_clear_free(out, out_len);
  5631. return 1;
  5632. }
  5633. int ssl_log_rsa_client_key_exchange(SSL_CONNECTION *sc,
  5634. const uint8_t *encrypted_premaster,
  5635. size_t encrypted_premaster_len,
  5636. const uint8_t *premaster,
  5637. size_t premaster_len)
  5638. {
  5639. if (encrypted_premaster_len < 8) {
  5640. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  5641. return 0;
  5642. }
  5643. /* We only want the first 8 bytes of the encrypted premaster as a tag. */
  5644. return nss_keylog_int("RSA",
  5645. sc,
  5646. encrypted_premaster,
  5647. 8,
  5648. premaster,
  5649. premaster_len);
  5650. }
  5651. int ssl_log_secret(SSL_CONNECTION *sc,
  5652. const char *label,
  5653. const uint8_t *secret,
  5654. size_t secret_len)
  5655. {
  5656. return nss_keylog_int(label,
  5657. sc,
  5658. sc->s3.client_random,
  5659. SSL3_RANDOM_SIZE,
  5660. secret,
  5661. secret_len);
  5662. }
  5663. #define SSLV2_CIPHER_LEN 3
  5664. int ssl_cache_cipherlist(SSL_CONNECTION *s, PACKET *cipher_suites, int sslv2format)
  5665. {
  5666. int n;
  5667. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  5668. if (PACKET_remaining(cipher_suites) == 0) {
  5669. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
  5670. return 0;
  5671. }
  5672. if (PACKET_remaining(cipher_suites) % n != 0) {
  5673. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  5674. return 0;
  5675. }
  5676. OPENSSL_free(s->s3.tmp.ciphers_raw);
  5677. s->s3.tmp.ciphers_raw = NULL;
  5678. s->s3.tmp.ciphers_rawlen = 0;
  5679. if (sslv2format) {
  5680. size_t numciphers = PACKET_remaining(cipher_suites) / n;
  5681. PACKET sslv2ciphers = *cipher_suites;
  5682. unsigned int leadbyte;
  5683. unsigned char *raw;
  5684. /*
  5685. * We store the raw ciphers list in SSLv3+ format so we need to do some
  5686. * preprocessing to convert the list first. If there are any SSLv2 only
  5687. * ciphersuites with a non-zero leading byte then we are going to
  5688. * slightly over allocate because we won't store those. But that isn't a
  5689. * problem.
  5690. */
  5691. raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
  5692. s->s3.tmp.ciphers_raw = raw;
  5693. if (raw == NULL) {
  5694. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  5695. return 0;
  5696. }
  5697. for (s->s3.tmp.ciphers_rawlen = 0;
  5698. PACKET_remaining(&sslv2ciphers) > 0;
  5699. raw += TLS_CIPHER_LEN) {
  5700. if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
  5701. || (leadbyte == 0
  5702. && !PACKET_copy_bytes(&sslv2ciphers, raw,
  5703. TLS_CIPHER_LEN))
  5704. || (leadbyte != 0
  5705. && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
  5706. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
  5707. OPENSSL_free(s->s3.tmp.ciphers_raw);
  5708. s->s3.tmp.ciphers_raw = NULL;
  5709. s->s3.tmp.ciphers_rawlen = 0;
  5710. return 0;
  5711. }
  5712. if (leadbyte == 0)
  5713. s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
  5714. }
  5715. } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
  5716. &s->s3.tmp.ciphers_rawlen)) {
  5717. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  5718. return 0;
  5719. }
  5720. return 1;
  5721. }
  5722. int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
  5723. int isv2format, STACK_OF(SSL_CIPHER) **sk,
  5724. STACK_OF(SSL_CIPHER) **scsvs)
  5725. {
  5726. PACKET pkt;
  5727. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  5728. if (sc == NULL)
  5729. return 0;
  5730. if (!PACKET_buf_init(&pkt, bytes, len))
  5731. return 0;
  5732. return ossl_bytes_to_cipher_list(sc, &pkt, sk, scsvs, isv2format, 0);
  5733. }
  5734. int ossl_bytes_to_cipher_list(SSL_CONNECTION *s, PACKET *cipher_suites,
  5735. STACK_OF(SSL_CIPHER) **skp,
  5736. STACK_OF(SSL_CIPHER) **scsvs_out,
  5737. int sslv2format, int fatal)
  5738. {
  5739. const SSL_CIPHER *c;
  5740. STACK_OF(SSL_CIPHER) *sk = NULL;
  5741. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  5742. int n;
  5743. /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
  5744. unsigned char cipher[SSLV2_CIPHER_LEN];
  5745. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  5746. if (PACKET_remaining(cipher_suites) == 0) {
  5747. if (fatal)
  5748. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
  5749. else
  5750. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
  5751. return 0;
  5752. }
  5753. if (PACKET_remaining(cipher_suites) % n != 0) {
  5754. if (fatal)
  5755. SSLfatal(s, SSL_AD_DECODE_ERROR,
  5756. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  5757. else
  5758. ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  5759. return 0;
  5760. }
  5761. sk = sk_SSL_CIPHER_new_null();
  5762. scsvs = sk_SSL_CIPHER_new_null();
  5763. if (sk == NULL || scsvs == NULL) {
  5764. if (fatal)
  5765. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  5766. else
  5767. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  5768. goto err;
  5769. }
  5770. while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
  5771. /*
  5772. * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
  5773. * first byte set to zero, while true SSLv2 ciphers have a non-zero
  5774. * first byte. We don't support any true SSLv2 ciphers, so skip them.
  5775. */
  5776. if (sslv2format && cipher[0] != '\0')
  5777. continue;
  5778. /* For SSLv2-compat, ignore leading 0-byte. */
  5779. c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
  5780. if (c != NULL) {
  5781. if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
  5782. (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
  5783. if (fatal)
  5784. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  5785. else
  5786. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  5787. goto err;
  5788. }
  5789. }
  5790. }
  5791. if (PACKET_remaining(cipher_suites) > 0) {
  5792. if (fatal)
  5793. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  5794. else
  5795. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  5796. goto err;
  5797. }
  5798. if (skp != NULL)
  5799. *skp = sk;
  5800. else
  5801. sk_SSL_CIPHER_free(sk);
  5802. if (scsvs_out != NULL)
  5803. *scsvs_out = scsvs;
  5804. else
  5805. sk_SSL_CIPHER_free(scsvs);
  5806. return 1;
  5807. err:
  5808. sk_SSL_CIPHER_free(sk);
  5809. sk_SSL_CIPHER_free(scsvs);
  5810. return 0;
  5811. }
  5812. int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
  5813. {
  5814. ctx->max_early_data = max_early_data;
  5815. return 1;
  5816. }
  5817. uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
  5818. {
  5819. return ctx->max_early_data;
  5820. }
  5821. int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
  5822. {
  5823. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  5824. if (sc == NULL)
  5825. return 0;
  5826. sc->max_early_data = max_early_data;
  5827. return 1;
  5828. }
  5829. uint32_t SSL_get_max_early_data(const SSL *s)
  5830. {
  5831. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  5832. if (sc == NULL)
  5833. return 0;
  5834. return sc->max_early_data;
  5835. }
  5836. int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
  5837. {
  5838. ctx->recv_max_early_data = recv_max_early_data;
  5839. return 1;
  5840. }
  5841. uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
  5842. {
  5843. return ctx->recv_max_early_data;
  5844. }
  5845. int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
  5846. {
  5847. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  5848. if (sc == NULL)
  5849. return 0;
  5850. sc->recv_max_early_data = recv_max_early_data;
  5851. return 1;
  5852. }
  5853. uint32_t SSL_get_recv_max_early_data(const SSL *s)
  5854. {
  5855. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  5856. if (sc == NULL)
  5857. return 0;
  5858. return sc->recv_max_early_data;
  5859. }
  5860. __owur unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION *sc)
  5861. {
  5862. /* Return any active Max Fragment Len extension */
  5863. if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session))
  5864. return GET_MAX_FRAGMENT_LENGTH(sc->session);
  5865. /* return current SSL connection setting */
  5866. return sc->max_send_fragment;
  5867. }
  5868. __owur unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION *sc)
  5869. {
  5870. /* Return a value regarding an active Max Fragment Len extension */
  5871. if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session)
  5872. && sc->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(sc->session))
  5873. return GET_MAX_FRAGMENT_LENGTH(sc->session);
  5874. /* else limit |split_send_fragment| to current |max_send_fragment| */
  5875. if (sc->split_send_fragment > sc->max_send_fragment)
  5876. return sc->max_send_fragment;
  5877. /* return current SSL connection setting */
  5878. return sc->split_send_fragment;
  5879. }
  5880. int SSL_stateless(SSL *s)
  5881. {
  5882. int ret;
  5883. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  5884. if (sc == NULL)
  5885. return 0;
  5886. /* Ensure there is no state left over from a previous invocation */
  5887. if (!SSL_clear(s))
  5888. return 0;
  5889. ERR_clear_error();
  5890. sc->s3.flags |= TLS1_FLAGS_STATELESS;
  5891. ret = SSL_accept(s);
  5892. sc->s3.flags &= ~TLS1_FLAGS_STATELESS;
  5893. if (ret > 0 && sc->ext.cookieok)
  5894. return 1;
  5895. if (sc->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(sc))
  5896. return 0;
  5897. return -1;
  5898. }
  5899. void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
  5900. {
  5901. ctx->pha_enabled = val;
  5902. }
  5903. void SSL_set_post_handshake_auth(SSL *ssl, int val)
  5904. {
  5905. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  5906. if (sc == NULL)
  5907. return;
  5908. sc->pha_enabled = val;
  5909. }
  5910. int SSL_verify_client_post_handshake(SSL *ssl)
  5911. {
  5912. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  5913. #ifndef OPENSSL_NO_QUIC
  5914. if (IS_QUIC(ssl)) {
  5915. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  5916. return 0;
  5917. }
  5918. #endif
  5919. if (sc == NULL)
  5920. return 0;
  5921. if (!SSL_CONNECTION_IS_TLS13(sc)) {
  5922. ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
  5923. return 0;
  5924. }
  5925. if (!sc->server) {
  5926. ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
  5927. return 0;
  5928. }
  5929. if (!SSL_is_init_finished(ssl)) {
  5930. ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
  5931. return 0;
  5932. }
  5933. switch (sc->post_handshake_auth) {
  5934. case SSL_PHA_NONE:
  5935. ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
  5936. return 0;
  5937. default:
  5938. case SSL_PHA_EXT_SENT:
  5939. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  5940. return 0;
  5941. case SSL_PHA_EXT_RECEIVED:
  5942. break;
  5943. case SSL_PHA_REQUEST_PENDING:
  5944. ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
  5945. return 0;
  5946. case SSL_PHA_REQUESTED:
  5947. ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
  5948. return 0;
  5949. }
  5950. sc->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
  5951. /* checks verify_mode and algorithm_auth */
  5952. if (!send_certificate_request(sc)) {
  5953. sc->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
  5954. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
  5955. return 0;
  5956. }
  5957. ossl_statem_set_in_init(sc, 1);
  5958. return 1;
  5959. }
  5960. int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
  5961. SSL_CTX_generate_session_ticket_fn gen_cb,
  5962. SSL_CTX_decrypt_session_ticket_fn dec_cb,
  5963. void *arg)
  5964. {
  5965. ctx->generate_ticket_cb = gen_cb;
  5966. ctx->decrypt_ticket_cb = dec_cb;
  5967. ctx->ticket_cb_data = arg;
  5968. return 1;
  5969. }
  5970. void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
  5971. SSL_allow_early_data_cb_fn cb,
  5972. void *arg)
  5973. {
  5974. ctx->allow_early_data_cb = cb;
  5975. ctx->allow_early_data_cb_data = arg;
  5976. }
  5977. void SSL_set_allow_early_data_cb(SSL *s,
  5978. SSL_allow_early_data_cb_fn cb,
  5979. void *arg)
  5980. {
  5981. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  5982. if (sc == NULL)
  5983. return;
  5984. sc->allow_early_data_cb = cb;
  5985. sc->allow_early_data_cb_data = arg;
  5986. }
  5987. const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
  5988. int nid,
  5989. const char *properties)
  5990. {
  5991. const EVP_CIPHER *ciph;
  5992. ciph = tls_get_cipher_from_engine(nid);
  5993. if (ciph != NULL)
  5994. return ciph;
  5995. /*
  5996. * If there is no engine cipher then we do an explicit fetch. This may fail
  5997. * and that could be ok
  5998. */
  5999. ERR_set_mark();
  6000. ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties);
  6001. ERR_pop_to_mark();
  6002. return ciph;
  6003. }
  6004. int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
  6005. {
  6006. /* Don't up-ref an implicit EVP_CIPHER */
  6007. if (EVP_CIPHER_get0_provider(cipher) == NULL)
  6008. return 1;
  6009. /*
  6010. * The cipher was explicitly fetched and therefore it is safe to cast
  6011. * away the const
  6012. */
  6013. return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
  6014. }
  6015. void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
  6016. {
  6017. if (cipher == NULL)
  6018. return;
  6019. if (EVP_CIPHER_get0_provider(cipher) != NULL) {
  6020. /*
  6021. * The cipher was explicitly fetched and therefore it is safe to cast
  6022. * away the const
  6023. */
  6024. EVP_CIPHER_free((EVP_CIPHER *)cipher);
  6025. }
  6026. }
  6027. const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
  6028. int nid,
  6029. const char *properties)
  6030. {
  6031. const EVP_MD *md;
  6032. md = tls_get_digest_from_engine(nid);
  6033. if (md != NULL)
  6034. return md;
  6035. /* Otherwise we do an explicit fetch */
  6036. ERR_set_mark();
  6037. md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties);
  6038. ERR_pop_to_mark();
  6039. return md;
  6040. }
  6041. int ssl_evp_md_up_ref(const EVP_MD *md)
  6042. {
  6043. /* Don't up-ref an implicit EVP_MD */
  6044. if (EVP_MD_get0_provider(md) == NULL)
  6045. return 1;
  6046. /*
  6047. * The digest was explicitly fetched and therefore it is safe to cast
  6048. * away the const
  6049. */
  6050. return EVP_MD_up_ref((EVP_MD *)md);
  6051. }
  6052. void ssl_evp_md_free(const EVP_MD *md)
  6053. {
  6054. if (md == NULL)
  6055. return;
  6056. if (EVP_MD_get0_provider(md) != NULL) {
  6057. /*
  6058. * The digest was explicitly fetched and therefore it is safe to cast
  6059. * away the const
  6060. */
  6061. EVP_MD_free((EVP_MD *)md);
  6062. }
  6063. }
  6064. int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
  6065. {
  6066. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6067. if (sc == NULL)
  6068. return 0;
  6069. if (!ssl_security(sc, SSL_SECOP_TMP_DH,
  6070. EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
  6071. ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
  6072. return 0;
  6073. }
  6074. EVP_PKEY_free(sc->cert->dh_tmp);
  6075. sc->cert->dh_tmp = dhpkey;
  6076. return 1;
  6077. }
  6078. int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
  6079. {
  6080. if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
  6081. EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
  6082. ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
  6083. return 0;
  6084. }
  6085. EVP_PKEY_free(ctx->cert->dh_tmp);
  6086. ctx->cert->dh_tmp = dhpkey;
  6087. return 1;
  6088. }
  6089. /* QUIC-specific methods which are supported on QUIC connections only. */
  6090. int SSL_handle_events(SSL *s)
  6091. {
  6092. SSL_CONNECTION *sc;
  6093. #ifndef OPENSSL_NO_QUIC
  6094. if (IS_QUIC(s))
  6095. return ossl_quic_handle_events(s);
  6096. #endif
  6097. sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  6098. if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc))
  6099. /*
  6100. * DTLSv1_handle_timeout returns 0 if the timer wasn't expired yet,
  6101. * which we consider a success case. Theoretically DTLSv1_handle_timeout
  6102. * can also return 0 if s is NULL or not a DTLS object, but we've
  6103. * already ruled out those possibilities above, so this is not possible
  6104. * here. Thus the only failure cases are where DTLSv1_handle_timeout
  6105. * returns -1.
  6106. */
  6107. return DTLSv1_handle_timeout(s) >= 0;
  6108. return 1;
  6109. }
  6110. int SSL_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
  6111. {
  6112. SSL_CONNECTION *sc;
  6113. #ifndef OPENSSL_NO_QUIC
  6114. if (IS_QUIC(s))
  6115. return ossl_quic_get_event_timeout(s, tv, is_infinite);
  6116. #endif
  6117. sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  6118. if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc)
  6119. && DTLSv1_get_timeout(s, tv)) {
  6120. *is_infinite = 0;
  6121. return 1;
  6122. }
  6123. tv->tv_sec = 1000000;
  6124. tv->tv_usec = 0;
  6125. *is_infinite = 1;
  6126. return 1;
  6127. }
  6128. int SSL_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
  6129. {
  6130. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6131. #ifndef OPENSSL_NO_QUIC
  6132. if (IS_QUIC(s))
  6133. return ossl_quic_get_rpoll_descriptor(s, desc);
  6134. #endif
  6135. if (sc == NULL || sc->rbio == NULL)
  6136. return 0;
  6137. return BIO_get_rpoll_descriptor(sc->rbio, desc);
  6138. }
  6139. int SSL_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
  6140. {
  6141. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6142. #ifndef OPENSSL_NO_QUIC
  6143. if (IS_QUIC(s))
  6144. return ossl_quic_get_wpoll_descriptor(s, desc);
  6145. #endif
  6146. if (sc == NULL || sc->wbio == NULL)
  6147. return 0;
  6148. return BIO_get_wpoll_descriptor(sc->wbio, desc);
  6149. }
  6150. int SSL_net_read_desired(SSL *s)
  6151. {
  6152. #ifndef OPENSSL_NO_QUIC
  6153. if (!IS_QUIC(s))
  6154. return SSL_want_read(s);
  6155. return ossl_quic_get_net_read_desired(s);
  6156. #else
  6157. return SSL_want_read(s);
  6158. #endif
  6159. }
  6160. int SSL_net_write_desired(SSL *s)
  6161. {
  6162. #ifndef OPENSSL_NO_QUIC
  6163. if (!IS_QUIC(s))
  6164. return SSL_want_write(s);
  6165. return ossl_quic_get_net_write_desired(s);
  6166. #else
  6167. return SSL_want_write(s);
  6168. #endif
  6169. }
  6170. int SSL_set_blocking_mode(SSL *s, int blocking)
  6171. {
  6172. #ifndef OPENSSL_NO_QUIC
  6173. if (!IS_QUIC(s))
  6174. return 0;
  6175. return ossl_quic_conn_set_blocking_mode(s, blocking);
  6176. #else
  6177. return 0;
  6178. #endif
  6179. }
  6180. int SSL_get_blocking_mode(SSL *s)
  6181. {
  6182. #ifndef OPENSSL_NO_QUIC
  6183. if (!IS_QUIC(s))
  6184. return -1;
  6185. return ossl_quic_conn_get_blocking_mode(s);
  6186. #else
  6187. return -1;
  6188. #endif
  6189. }
  6190. int SSL_set1_initial_peer_addr(SSL *s, const BIO_ADDR *peer_addr)
  6191. {
  6192. #ifndef OPENSSL_NO_QUIC
  6193. if (!IS_QUIC(s))
  6194. return 0;
  6195. return ossl_quic_conn_set_initial_peer_addr(s, peer_addr);
  6196. #else
  6197. return 0;
  6198. #endif
  6199. }
  6200. int SSL_shutdown_ex(SSL *ssl, uint64_t flags,
  6201. const SSL_SHUTDOWN_EX_ARGS *args,
  6202. size_t args_len)
  6203. {
  6204. #ifndef OPENSSL_NO_QUIC
  6205. if (!IS_QUIC(ssl))
  6206. return SSL_shutdown(ssl);
  6207. return ossl_quic_conn_shutdown(ssl, flags, args, args_len);
  6208. #else
  6209. return SSL_shutdown(ssl);
  6210. #endif
  6211. }
  6212. int SSL_stream_conclude(SSL *ssl, uint64_t flags)
  6213. {
  6214. #ifndef OPENSSL_NO_QUIC
  6215. if (!IS_QUIC(ssl))
  6216. return 0;
  6217. return ossl_quic_conn_stream_conclude(ssl);
  6218. #else
  6219. return 0;
  6220. #endif
  6221. }
  6222. SSL *SSL_new_stream(SSL *s, uint64_t flags)
  6223. {
  6224. #ifndef OPENSSL_NO_QUIC
  6225. if (!IS_QUIC(s))
  6226. return NULL;
  6227. return ossl_quic_conn_stream_new(s, flags);
  6228. #else
  6229. return NULL;
  6230. #endif
  6231. }
  6232. SSL *SSL_get0_connection(SSL *s)
  6233. {
  6234. #ifndef OPENSSL_NO_QUIC
  6235. if (!IS_QUIC(s))
  6236. return s;
  6237. return ossl_quic_get0_connection(s);
  6238. #else
  6239. return s;
  6240. #endif
  6241. }
  6242. int SSL_is_connection(SSL *s)
  6243. {
  6244. return SSL_get0_connection(s) == s;
  6245. }
  6246. int SSL_get_stream_type(SSL *s)
  6247. {
  6248. #ifndef OPENSSL_NO_QUIC
  6249. if (!IS_QUIC(s))
  6250. return SSL_STREAM_TYPE_BIDI;
  6251. return ossl_quic_get_stream_type(s);
  6252. #else
  6253. return SSL_STREAM_TYPE_BIDI;
  6254. #endif
  6255. }
  6256. uint64_t SSL_get_stream_id(SSL *s)
  6257. {
  6258. #ifndef OPENSSL_NO_QUIC
  6259. if (!IS_QUIC(s))
  6260. return UINT64_MAX;
  6261. return ossl_quic_get_stream_id(s);
  6262. #else
  6263. return UINT64_MAX;
  6264. #endif
  6265. }
  6266. int SSL_is_stream_local(SSL *s)
  6267. {
  6268. #ifndef OPENSSL_NO_QUIC
  6269. if (!IS_QUIC(s))
  6270. return -1;
  6271. return ossl_quic_is_stream_local(s);
  6272. #else
  6273. return -1;
  6274. #endif
  6275. }
  6276. int SSL_set_default_stream_mode(SSL *s, uint32_t mode)
  6277. {
  6278. #ifndef OPENSSL_NO_QUIC
  6279. if (!IS_QUIC(s))
  6280. return 0;
  6281. return ossl_quic_set_default_stream_mode(s, mode);
  6282. #else
  6283. return 0;
  6284. #endif
  6285. }
  6286. int SSL_set_incoming_stream_policy(SSL *s, int policy, uint64_t aec)
  6287. {
  6288. #ifndef OPENSSL_NO_QUIC
  6289. if (!IS_QUIC(s))
  6290. return 0;
  6291. return ossl_quic_set_incoming_stream_policy(s, policy, aec);
  6292. #else
  6293. return 0;
  6294. #endif
  6295. }
  6296. SSL *SSL_accept_stream(SSL *s, uint64_t flags)
  6297. {
  6298. #ifndef OPENSSL_NO_QUIC
  6299. if (!IS_QUIC(s))
  6300. return NULL;
  6301. return ossl_quic_accept_stream(s, flags);
  6302. #else
  6303. return NULL;
  6304. #endif
  6305. }
  6306. size_t SSL_get_accept_stream_queue_len(SSL *s)
  6307. {
  6308. #ifndef OPENSSL_NO_QUIC
  6309. if (!IS_QUIC(s))
  6310. return 0;
  6311. return ossl_quic_get_accept_stream_queue_len(s);
  6312. #else
  6313. return 0;
  6314. #endif
  6315. }
  6316. int SSL_stream_reset(SSL *s,
  6317. const SSL_STREAM_RESET_ARGS *args,
  6318. size_t args_len)
  6319. {
  6320. #ifndef OPENSSL_NO_QUIC
  6321. if (!IS_QUIC(s))
  6322. return 0;
  6323. return ossl_quic_stream_reset(s, args, args_len);
  6324. #else
  6325. return 0;
  6326. #endif
  6327. }
  6328. int SSL_get_stream_read_state(SSL *s)
  6329. {
  6330. #ifndef OPENSSL_NO_QUIC
  6331. if (!IS_QUIC(s))
  6332. return SSL_STREAM_STATE_NONE;
  6333. return ossl_quic_get_stream_read_state(s);
  6334. #else
  6335. return SSL_STREAM_STATE_NONE;
  6336. #endif
  6337. }
  6338. int SSL_get_stream_write_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_write_state(s);
  6344. #else
  6345. return SSL_STREAM_STATE_NONE;
  6346. #endif
  6347. }
  6348. int SSL_get_stream_read_error_code(SSL *s, uint64_t *app_error_code)
  6349. {
  6350. #ifndef OPENSSL_NO_QUIC
  6351. if (!IS_QUIC(s))
  6352. return -1;
  6353. return ossl_quic_get_stream_read_error_code(s, app_error_code);
  6354. #else
  6355. return -1;
  6356. #endif
  6357. }
  6358. int SSL_get_stream_write_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_write_error_code(s, app_error_code);
  6364. #else
  6365. return -1;
  6366. #endif
  6367. }
  6368. int SSL_get_conn_close_info(SSL *s, SSL_CONN_CLOSE_INFO *info,
  6369. size_t info_len)
  6370. {
  6371. #ifndef OPENSSL_NO_QUIC
  6372. if (!IS_QUIC(s))
  6373. return -1;
  6374. return ossl_quic_get_conn_close_info(s, info, info_len);
  6375. #else
  6376. return -1;
  6377. #endif
  6378. }
  6379. int SSL_add_expected_rpk(SSL *s, EVP_PKEY *rpk)
  6380. {
  6381. unsigned char *data = NULL;
  6382. SSL_DANE *dane = SSL_get0_dane(s);
  6383. int ret;
  6384. if (dane == NULL || dane->dctx == NULL)
  6385. return 0;
  6386. if ((ret = i2d_PUBKEY(rpk, &data)) <= 0)
  6387. return 0;
  6388. ret = SSL_dane_tlsa_add(s, DANETLS_USAGE_DANE_EE,
  6389. DANETLS_SELECTOR_SPKI,
  6390. DANETLS_MATCHING_FULL,
  6391. data, (size_t)ret) > 0;
  6392. OPENSSL_free(data);
  6393. return ret;
  6394. }
  6395. EVP_PKEY *SSL_get0_peer_rpk(const SSL *s)
  6396. {
  6397. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6398. if (sc == NULL || sc->session == NULL)
  6399. return NULL;
  6400. return sc->session->peer_rpk;
  6401. }
  6402. int SSL_get_negotiated_client_cert_type(const SSL *s)
  6403. {
  6404. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6405. if (sc == NULL)
  6406. return 0;
  6407. return sc->ext.client_cert_type;
  6408. }
  6409. int SSL_get_negotiated_server_cert_type(const SSL *s)
  6410. {
  6411. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6412. if (sc == NULL)
  6413. return 0;
  6414. return sc->ext.server_cert_type;
  6415. }
  6416. static int validate_cert_type(const unsigned char *val, size_t len)
  6417. {
  6418. size_t i;
  6419. int saw_rpk = 0;
  6420. int saw_x509 = 0;
  6421. if (val == NULL && len == 0)
  6422. return 1;
  6423. if (val == NULL || len == 0)
  6424. return 0;
  6425. for (i = 0; i < len; i++) {
  6426. switch (val[i]) {
  6427. case TLSEXT_cert_type_rpk:
  6428. if (saw_rpk)
  6429. return 0;
  6430. saw_rpk = 1;
  6431. break;
  6432. case TLSEXT_cert_type_x509:
  6433. if (saw_x509)
  6434. return 0;
  6435. saw_x509 = 1;
  6436. break;
  6437. case TLSEXT_cert_type_pgp:
  6438. case TLSEXT_cert_type_1609dot2:
  6439. default:
  6440. return 0;
  6441. }
  6442. }
  6443. return 1;
  6444. }
  6445. static int set_cert_type(unsigned char **cert_type,
  6446. size_t *cert_type_len,
  6447. const unsigned char *val,
  6448. size_t len)
  6449. {
  6450. unsigned char *tmp = NULL;
  6451. if (!validate_cert_type(val, len))
  6452. return 0;
  6453. if (val != NULL && (tmp = OPENSSL_memdup(val, len)) == NULL)
  6454. return 0;
  6455. OPENSSL_free(*cert_type);
  6456. *cert_type = tmp;
  6457. *cert_type_len = len;
  6458. return 1;
  6459. }
  6460. int SSL_set1_client_cert_type(SSL *s, const unsigned char *val, size_t len)
  6461. {
  6462. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6463. return set_cert_type(&sc->client_cert_type, &sc->client_cert_type_len,
  6464. val, len);
  6465. }
  6466. int SSL_set1_server_cert_type(SSL *s, const unsigned char *val, size_t len)
  6467. {
  6468. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  6469. return set_cert_type(&sc->server_cert_type, &sc->server_cert_type_len,
  6470. val, len);
  6471. }
  6472. int SSL_CTX_set1_client_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
  6473. {
  6474. return set_cert_type(&ctx->client_cert_type, &ctx->client_cert_type_len,
  6475. val, len);
  6476. }
  6477. int SSL_CTX_set1_server_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
  6478. {
  6479. return set_cert_type(&ctx->server_cert_type, &ctx->server_cert_type_len,
  6480. val, len);
  6481. }
  6482. int SSL_get0_client_cert_type(const SSL *s, unsigned char **t, size_t *len)
  6483. {
  6484. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  6485. if (t == NULL || len == NULL)
  6486. return 0;
  6487. *t = sc->client_cert_type;
  6488. *len = sc->client_cert_type_len;
  6489. return 1;
  6490. }
  6491. int SSL_get0_server_cert_type(const SSL *s, unsigned char **t, size_t *len)
  6492. {
  6493. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  6494. if (t == NULL || len == NULL)
  6495. return 0;
  6496. *t = sc->server_cert_type;
  6497. *len = sc->server_cert_type_len;
  6498. return 1;
  6499. }
  6500. int SSL_CTX_get0_client_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
  6501. {
  6502. if (t == NULL || len == NULL)
  6503. return 0;
  6504. *t = ctx->client_cert_type;
  6505. *len = ctx->client_cert_type_len;
  6506. return 1;
  6507. }
  6508. int SSL_CTX_get0_server_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
  6509. {
  6510. if (t == NULL || len == NULL)
  6511. return 0;
  6512. *t = ctx->server_cert_type;
  6513. *len = ctx->server_cert_type_len;
  6514. return 1;
  6515. }