ssl_lib.c 210 KB

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