sslapitest.c 300 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * We need access to the deprecated low level HMAC APIs for legacy purposes
  11. * when the deprecated calls are not hidden
  12. */
  13. #ifndef OPENSSL_NO_DEPRECATED_3_0
  14. # define OPENSSL_SUPPRESS_DEPRECATED
  15. #endif
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <openssl/opensslconf.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/ssl.h>
  22. #include <openssl/ocsp.h>
  23. #include <openssl/srp.h>
  24. #include <openssl/txt_db.h>
  25. #include <openssl/aes.h>
  26. #include <openssl/rand.h>
  27. #include <openssl/core_names.h>
  28. #include <openssl/core_dispatch.h>
  29. #include <openssl/provider.h>
  30. #include <openssl/param_build.h>
  31. #include <openssl/x509v3.h>
  32. #include "helpers/ssltestlib.h"
  33. #include "testutil.h"
  34. #include "testutil/output.h"
  35. #include "internal/nelem.h"
  36. #include "internal/ktls.h"
  37. #include "../ssl/ssl_local.h"
  38. #include "filterprov.h"
  39. #undef OSSL_NO_USABLE_TLS1_3
  40. #if defined(OPENSSL_NO_TLS1_3) \
  41. || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
  42. /*
  43. * If we don't have ec or dh then there are no built-in groups that are usable
  44. * with TLSv1.3
  45. */
  46. # define OSSL_NO_USABLE_TLS1_3
  47. #endif
  48. /* Defined in tls-provider.c */
  49. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  50. const OSSL_DISPATCH *in,
  51. const OSSL_DISPATCH **out,
  52. void **provctx);
  53. static OSSL_LIB_CTX *libctx = NULL;
  54. static OSSL_PROVIDER *defctxnull = NULL;
  55. #ifndef OSSL_NO_USABLE_TLS1_3
  56. static SSL_SESSION *clientpsk = NULL;
  57. static SSL_SESSION *serverpsk = NULL;
  58. static const char *pskid = "Identity";
  59. static const char *srvid;
  60. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  61. size_t *idlen, SSL_SESSION **sess);
  62. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  63. size_t identity_len, SSL_SESSION **sess);
  64. static int use_session_cb_cnt = 0;
  65. static int find_session_cb_cnt = 0;
  66. static SSL_SESSION *create_a_psk(SSL *ssl);
  67. #endif
  68. static char *certsdir = NULL;
  69. static char *cert = NULL;
  70. static char *privkey = NULL;
  71. static char *cert2 = NULL;
  72. static char *privkey2 = NULL;
  73. static char *cert1024 = NULL;
  74. static char *privkey1024 = NULL;
  75. static char *cert3072 = NULL;
  76. static char *privkey3072 = NULL;
  77. static char *cert4096 = NULL;
  78. static char *privkey4096 = NULL;
  79. static char *cert8192 = NULL;
  80. static char *privkey8192 = NULL;
  81. static char *srpvfile = NULL;
  82. static char *tmpfilename = NULL;
  83. static int is_fips = 0;
  84. #define LOG_BUFFER_SIZE 2048
  85. static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  86. static size_t server_log_buffer_index = 0;
  87. static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  88. static size_t client_log_buffer_index = 0;
  89. static int error_writing_log = 0;
  90. #ifndef OPENSSL_NO_OCSP
  91. static const unsigned char orespder[] = "Dummy OCSP Response";
  92. static int ocsp_server_called = 0;
  93. static int ocsp_client_called = 0;
  94. static int cdummyarg = 1;
  95. static X509 *ocspcert = NULL;
  96. #endif
  97. #define NUM_EXTRA_CERTS 40
  98. #define CLIENT_VERSION_LEN 2
  99. /*
  100. * This structure is used to validate that the correct number of log messages
  101. * of various types are emitted when emitting secret logs.
  102. */
  103. struct sslapitest_log_counts {
  104. unsigned int rsa_key_exchange_count;
  105. unsigned int master_secret_count;
  106. unsigned int client_early_secret_count;
  107. unsigned int client_handshake_secret_count;
  108. unsigned int server_handshake_secret_count;
  109. unsigned int client_application_secret_count;
  110. unsigned int server_application_secret_count;
  111. unsigned int early_exporter_secret_count;
  112. unsigned int exporter_secret_count;
  113. };
  114. static unsigned char serverinfov1[] = {
  115. 0xff, 0xff, /* Dummy extension type */
  116. 0x00, 0x01, /* Extension length is 1 byte */
  117. 0xff /* Dummy extension data */
  118. };
  119. static unsigned char serverinfov2[] = {
  120. 0x00, 0x00, 0x00,
  121. (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */
  122. 0xff, 0xff, /* Dummy extension type */
  123. 0x00, 0x01, /* Extension length is 1 byte */
  124. 0xff /* Dummy extension data */
  125. };
  126. static int hostname_cb(SSL *s, int *al, void *arg)
  127. {
  128. const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  129. if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
  130. || strcmp(hostname, "altgoodhost") == 0))
  131. return SSL_TLSEXT_ERR_OK;
  132. return SSL_TLSEXT_ERR_NOACK;
  133. }
  134. static void client_keylog_callback(const SSL *ssl, const char *line)
  135. {
  136. int line_length = strlen(line);
  137. /* If the log doesn't fit, error out. */
  138. if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
  139. TEST_info("Client log too full");
  140. error_writing_log = 1;
  141. return;
  142. }
  143. strcat(client_log_buffer, line);
  144. client_log_buffer_index += line_length;
  145. client_log_buffer[client_log_buffer_index++] = '\n';
  146. }
  147. static void server_keylog_callback(const SSL *ssl, const char *line)
  148. {
  149. int line_length = strlen(line);
  150. /* If the log doesn't fit, error out. */
  151. if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
  152. TEST_info("Server log too full");
  153. error_writing_log = 1;
  154. return;
  155. }
  156. strcat(server_log_buffer, line);
  157. server_log_buffer_index += line_length;
  158. server_log_buffer[server_log_buffer_index++] = '\n';
  159. }
  160. static int compare_hex_encoded_buffer(const char *hex_encoded,
  161. size_t hex_length,
  162. const uint8_t *raw,
  163. size_t raw_length)
  164. {
  165. size_t i, j;
  166. char hexed[3];
  167. if (!TEST_size_t_eq(raw_length * 2, hex_length))
  168. return 1;
  169. for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
  170. sprintf(hexed, "%02x", raw[i]);
  171. if (!TEST_int_eq(hexed[0], hex_encoded[j])
  172. || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
  173. return 1;
  174. }
  175. return 0;
  176. }
  177. static int test_keylog_output(char *buffer, const SSL *ssl,
  178. const SSL_SESSION *session,
  179. struct sslapitest_log_counts *expected)
  180. {
  181. char *token = NULL;
  182. unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
  183. size_t client_random_size = SSL3_RANDOM_SIZE;
  184. unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
  185. size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
  186. unsigned int rsa_key_exchange_count = 0;
  187. unsigned int master_secret_count = 0;
  188. unsigned int client_early_secret_count = 0;
  189. unsigned int client_handshake_secret_count = 0;
  190. unsigned int server_handshake_secret_count = 0;
  191. unsigned int client_application_secret_count = 0;
  192. unsigned int server_application_secret_count = 0;
  193. unsigned int early_exporter_secret_count = 0;
  194. unsigned int exporter_secret_count = 0;
  195. for (token = strtok(buffer, " \n"); token != NULL;
  196. token = strtok(NULL, " \n")) {
  197. if (strcmp(token, "RSA") == 0) {
  198. /*
  199. * Premaster secret. Tokens should be: 16 ASCII bytes of
  200. * hex-encoded encrypted secret, then the hex-encoded pre-master
  201. * secret.
  202. */
  203. if (!TEST_ptr(token = strtok(NULL, " \n")))
  204. return 0;
  205. if (!TEST_size_t_eq(strlen(token), 16))
  206. return 0;
  207. if (!TEST_ptr(token = strtok(NULL, " \n")))
  208. return 0;
  209. /*
  210. * We can't sensibly check the log because the premaster secret is
  211. * transient, and OpenSSL doesn't keep hold of it once the master
  212. * secret is generated.
  213. */
  214. rsa_key_exchange_count++;
  215. } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
  216. /*
  217. * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
  218. * client random, then the hex-encoded master secret.
  219. */
  220. client_random_size = SSL_get_client_random(ssl,
  221. actual_client_random,
  222. SSL3_RANDOM_SIZE);
  223. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  224. return 0;
  225. if (!TEST_ptr(token = strtok(NULL, " \n")))
  226. return 0;
  227. if (!TEST_size_t_eq(strlen(token), 64))
  228. return 0;
  229. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  230. actual_client_random,
  231. client_random_size)))
  232. return 0;
  233. if (!TEST_ptr(token = strtok(NULL, " \n")))
  234. return 0;
  235. master_key_size = SSL_SESSION_get_master_key(session,
  236. actual_master_key,
  237. master_key_size);
  238. if (!TEST_size_t_ne(master_key_size, 0))
  239. return 0;
  240. if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
  241. actual_master_key,
  242. master_key_size)))
  243. return 0;
  244. master_secret_count++;
  245. } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
  246. || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
  247. || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
  248. || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
  249. || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
  250. || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
  251. || strcmp(token, "EXPORTER_SECRET") == 0) {
  252. /*
  253. * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
  254. * client random, and then the hex-encoded secret. In this case,
  255. * we treat all of these secrets identically and then just
  256. * distinguish between them when counting what we saw.
  257. */
  258. if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
  259. client_early_secret_count++;
  260. else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
  261. client_handshake_secret_count++;
  262. else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
  263. server_handshake_secret_count++;
  264. else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
  265. client_application_secret_count++;
  266. else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
  267. server_application_secret_count++;
  268. else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
  269. early_exporter_secret_count++;
  270. else if (strcmp(token, "EXPORTER_SECRET") == 0)
  271. exporter_secret_count++;
  272. client_random_size = SSL_get_client_random(ssl,
  273. actual_client_random,
  274. SSL3_RANDOM_SIZE);
  275. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  276. return 0;
  277. if (!TEST_ptr(token = strtok(NULL, " \n")))
  278. return 0;
  279. if (!TEST_size_t_eq(strlen(token), 64))
  280. return 0;
  281. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  282. actual_client_random,
  283. client_random_size)))
  284. return 0;
  285. if (!TEST_ptr(token = strtok(NULL, " \n")))
  286. return 0;
  287. /*
  288. * TODO(TLS1.3): test that application traffic secrets are what
  289. * we expect */
  290. } else {
  291. TEST_info("Unexpected token %s\n", token);
  292. return 0;
  293. }
  294. }
  295. /* Got what we expected? */
  296. if (!TEST_size_t_eq(rsa_key_exchange_count,
  297. expected->rsa_key_exchange_count)
  298. || !TEST_size_t_eq(master_secret_count,
  299. expected->master_secret_count)
  300. || !TEST_size_t_eq(client_early_secret_count,
  301. expected->client_early_secret_count)
  302. || !TEST_size_t_eq(client_handshake_secret_count,
  303. expected->client_handshake_secret_count)
  304. || !TEST_size_t_eq(server_handshake_secret_count,
  305. expected->server_handshake_secret_count)
  306. || !TEST_size_t_eq(client_application_secret_count,
  307. expected->client_application_secret_count)
  308. || !TEST_size_t_eq(server_application_secret_count,
  309. expected->server_application_secret_count)
  310. || !TEST_size_t_eq(early_exporter_secret_count,
  311. expected->early_exporter_secret_count)
  312. || !TEST_size_t_eq(exporter_secret_count,
  313. expected->exporter_secret_count))
  314. return 0;
  315. return 1;
  316. }
  317. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  318. static int test_keylog(void)
  319. {
  320. SSL_CTX *cctx = NULL, *sctx = NULL;
  321. SSL *clientssl = NULL, *serverssl = NULL;
  322. int testresult = 0;
  323. struct sslapitest_log_counts expected;
  324. /* Clean up logging space */
  325. memset(&expected, 0, sizeof(expected));
  326. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  327. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  328. client_log_buffer_index = 0;
  329. server_log_buffer_index = 0;
  330. error_writing_log = 0;
  331. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  332. TLS_client_method(),
  333. TLS1_VERSION, 0,
  334. &sctx, &cctx, cert, privkey)))
  335. return 0;
  336. /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
  337. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  338. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  339. /* We also want to ensure that we use RSA-based key exchange. */
  340. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
  341. goto end;
  342. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  343. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  344. goto end;
  345. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  346. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  347. == client_keylog_callback))
  348. goto end;
  349. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  350. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  351. == server_keylog_callback))
  352. goto end;
  353. /* Now do a handshake and check that the logs have been written to. */
  354. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  355. &clientssl, NULL, NULL))
  356. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  357. SSL_ERROR_NONE))
  358. || !TEST_false(error_writing_log)
  359. || !TEST_int_gt(client_log_buffer_index, 0)
  360. || !TEST_int_gt(server_log_buffer_index, 0))
  361. goto end;
  362. /*
  363. * Now we want to test that our output data was vaguely sensible. We
  364. * do that by using strtok and confirming that we have more or less the
  365. * data we expect. For both client and server, we expect to see one master
  366. * secret. The client should also see a RSA key exchange.
  367. */
  368. expected.rsa_key_exchange_count = 1;
  369. expected.master_secret_count = 1;
  370. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  371. SSL_get_session(clientssl), &expected)))
  372. goto end;
  373. expected.rsa_key_exchange_count = 0;
  374. if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
  375. SSL_get_session(serverssl), &expected)))
  376. goto end;
  377. testresult = 1;
  378. end:
  379. SSL_free(serverssl);
  380. SSL_free(clientssl);
  381. SSL_CTX_free(sctx);
  382. SSL_CTX_free(cctx);
  383. return testresult;
  384. }
  385. #endif
  386. #ifndef OSSL_NO_USABLE_TLS1_3
  387. static int test_keylog_no_master_key(void)
  388. {
  389. SSL_CTX *cctx = NULL, *sctx = NULL;
  390. SSL *clientssl = NULL, *serverssl = NULL;
  391. SSL_SESSION *sess = NULL;
  392. int testresult = 0;
  393. struct sslapitest_log_counts expected;
  394. unsigned char buf[1];
  395. size_t readbytes, written;
  396. /* Clean up logging space */
  397. memset(&expected, 0, sizeof(expected));
  398. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  399. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  400. client_log_buffer_index = 0;
  401. server_log_buffer_index = 0;
  402. error_writing_log = 0;
  403. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  404. TLS_client_method(), TLS1_VERSION, 0,
  405. &sctx, &cctx, cert, privkey))
  406. || !TEST_true(SSL_CTX_set_max_early_data(sctx,
  407. SSL3_RT_MAX_PLAIN_LENGTH)))
  408. return 0;
  409. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  410. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  411. goto end;
  412. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  413. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  414. == client_keylog_callback))
  415. goto end;
  416. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  417. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  418. == server_keylog_callback))
  419. goto end;
  420. /* Now do a handshake and check that the logs have been written to. */
  421. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  422. &clientssl, NULL, NULL))
  423. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  424. SSL_ERROR_NONE))
  425. || !TEST_false(error_writing_log))
  426. goto end;
  427. /*
  428. * Now we want to test that our output data was vaguely sensible. For this
  429. * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
  430. * TLSv1.3, but we do expect both client and server to emit keys.
  431. */
  432. expected.client_handshake_secret_count = 1;
  433. expected.server_handshake_secret_count = 1;
  434. expected.client_application_secret_count = 1;
  435. expected.server_application_secret_count = 1;
  436. expected.exporter_secret_count = 1;
  437. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  438. SSL_get_session(clientssl), &expected))
  439. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  440. SSL_get_session(serverssl),
  441. &expected)))
  442. goto end;
  443. /* Terminate old session and resume with early data. */
  444. sess = SSL_get1_session(clientssl);
  445. SSL_shutdown(clientssl);
  446. SSL_shutdown(serverssl);
  447. SSL_free(serverssl);
  448. SSL_free(clientssl);
  449. serverssl = clientssl = NULL;
  450. /* Reset key log */
  451. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  452. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  453. client_log_buffer_index = 0;
  454. server_log_buffer_index = 0;
  455. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  456. &clientssl, NULL, NULL))
  457. || !TEST_true(SSL_set_session(clientssl, sess))
  458. /* Here writing 0 length early data is enough. */
  459. || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  460. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  461. &readbytes),
  462. SSL_READ_EARLY_DATA_ERROR)
  463. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  464. SSL_EARLY_DATA_ACCEPTED)
  465. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  466. SSL_ERROR_NONE))
  467. || !TEST_true(SSL_session_reused(clientssl)))
  468. goto end;
  469. /* In addition to the previous entries, expect early secrets. */
  470. expected.client_early_secret_count = 1;
  471. expected.early_exporter_secret_count = 1;
  472. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  473. SSL_get_session(clientssl), &expected))
  474. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  475. SSL_get_session(serverssl),
  476. &expected)))
  477. goto end;
  478. testresult = 1;
  479. end:
  480. SSL_SESSION_free(sess);
  481. SSL_free(serverssl);
  482. SSL_free(clientssl);
  483. SSL_CTX_free(sctx);
  484. SSL_CTX_free(cctx);
  485. return testresult;
  486. }
  487. #endif
  488. static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
  489. {
  490. int res = X509_verify_cert(ctx);
  491. if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
  492. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
  493. return -1; /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
  494. return res;
  495. }
  496. static int test_client_cert_verify_cb(void)
  497. {
  498. /* server key, cert, chain, and root */
  499. char *skey = test_mk_file_path(certsdir, "leaf.key");
  500. char *leaf = test_mk_file_path(certsdir, "leaf.pem");
  501. char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
  502. char *int1 = test_mk_file_path(certsdir, "interCA.pem");
  503. char *root = test_mk_file_path(certsdir, "rootCA.pem");
  504. X509 *crt1 = NULL, *crt2 = NULL;
  505. STACK_OF(X509) *server_chain;
  506. SSL_CTX *cctx = NULL, *sctx = NULL;
  507. SSL *clientssl = NULL, *serverssl = NULL;
  508. int testresult = 0;
  509. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  510. TLS_client_method(), TLS1_VERSION, 0,
  511. &sctx, &cctx, NULL, NULL)))
  512. goto end;
  513. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
  514. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
  515. SSL_FILETYPE_PEM), 1)
  516. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
  517. goto end;
  518. if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
  519. goto end;
  520. SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
  521. SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
  522. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  523. &clientssl, NULL, NULL)))
  524. goto end;
  525. /* attempt SSL_connect() with incomplete server chain */
  526. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  527. SSL_ERROR_WANT_RETRY_VERIFY)))
  528. goto end;
  529. /* application provides intermediate certs needed to verify server cert */
  530. if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
  531. || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
  532. || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
  533. goto end;
  534. /* add certs in reverse order to demonstrate real chain building */
  535. if (!TEST_true(sk_X509_push(server_chain, crt1)))
  536. goto end;
  537. crt1 = NULL;
  538. if (!TEST_true(sk_X509_push(server_chain, crt2)))
  539. goto end;
  540. crt2 = NULL;
  541. /* continue SSL_connect(), must now succeed with completed server chain */
  542. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  543. SSL_ERROR_NONE)))
  544. goto end;
  545. testresult = 1;
  546. end:
  547. X509_free(crt1);
  548. X509_free(crt2);
  549. if (clientssl != NULL) {
  550. SSL_shutdown(clientssl);
  551. SSL_free(clientssl);
  552. }
  553. if (serverssl != NULL) {
  554. SSL_shutdown(serverssl);
  555. SSL_free(serverssl);
  556. }
  557. SSL_CTX_free(sctx);
  558. SSL_CTX_free(cctx);
  559. OPENSSL_free(skey);
  560. OPENSSL_free(leaf);
  561. OPENSSL_free(int2);
  562. OPENSSL_free(int1);
  563. OPENSSL_free(root);
  564. return testresult;
  565. }
  566. static int test_ssl_build_cert_chain(void)
  567. {
  568. int ret = 0;
  569. SSL_CTX *ssl_ctx = NULL;
  570. SSL *ssl = NULL;
  571. char *skey = test_mk_file_path(certsdir, "leaf.key");
  572. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  573. if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  574. goto end;
  575. if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
  576. goto end;
  577. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  578. if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
  579. || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
  580. || !TEST_int_eq(SSL_check_private_key(ssl), 1))
  581. goto end;
  582. if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  583. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  584. goto end;
  585. ret = 1;
  586. end:
  587. SSL_free(ssl);
  588. SSL_CTX_free(ssl_ctx);
  589. OPENSSL_free(leaf_chain);
  590. OPENSSL_free(skey);
  591. return ret;
  592. }
  593. static int test_ssl_ctx_build_cert_chain(void)
  594. {
  595. int ret = 0;
  596. SSL_CTX *ctx = NULL;
  597. char *skey = test_mk_file_path(certsdir, "leaf.key");
  598. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  599. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  600. goto end;
  601. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  602. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
  603. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
  604. SSL_FILETYPE_PEM), 1)
  605. || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
  606. goto end;
  607. if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  608. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  609. goto end;
  610. ret = 1;
  611. end:
  612. SSL_CTX_free(ctx);
  613. OPENSSL_free(leaf_chain);
  614. OPENSSL_free(skey);
  615. return ret;
  616. }
  617. #ifndef OPENSSL_NO_TLS1_2
  618. static int full_client_hello_callback(SSL *s, int *al, void *arg)
  619. {
  620. int *ctr = arg;
  621. const unsigned char *p;
  622. int *exts;
  623. /* We only configure two ciphers, but the SCSV is added automatically. */
  624. #ifdef OPENSSL_NO_EC
  625. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
  626. #else
  627. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
  628. 0x2c, 0x00, 0xff};
  629. #endif
  630. const int expected_extensions[] = {
  631. #ifndef OPENSSL_NO_EC
  632. 11, 10,
  633. #endif
  634. 35, 22, 23, 13};
  635. size_t len;
  636. /* Make sure we can defer processing and get called back. */
  637. if ((*ctr)++ == 0)
  638. return SSL_CLIENT_HELLO_RETRY;
  639. len = SSL_client_hello_get0_ciphers(s, &p);
  640. if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
  641. || !TEST_size_t_eq(
  642. SSL_client_hello_get0_compression_methods(s, &p), 1)
  643. || !TEST_int_eq(*p, 0))
  644. return SSL_CLIENT_HELLO_ERROR;
  645. if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
  646. return SSL_CLIENT_HELLO_ERROR;
  647. if (len != OSSL_NELEM(expected_extensions) ||
  648. memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
  649. printf("ClientHello callback expected extensions mismatch\n");
  650. OPENSSL_free(exts);
  651. return SSL_CLIENT_HELLO_ERROR;
  652. }
  653. OPENSSL_free(exts);
  654. return SSL_CLIENT_HELLO_SUCCESS;
  655. }
  656. static int test_client_hello_cb(void)
  657. {
  658. SSL_CTX *cctx = NULL, *sctx = NULL;
  659. SSL *clientssl = NULL, *serverssl = NULL;
  660. int testctr = 0, testresult = 0;
  661. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  662. TLS_client_method(), TLS1_VERSION, 0,
  663. &sctx, &cctx, cert, privkey)))
  664. goto end;
  665. SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
  666. /* The gimpy cipher list we configure can't do TLS 1.3. */
  667. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  668. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  669. "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
  670. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  671. &clientssl, NULL, NULL))
  672. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  673. SSL_ERROR_WANT_CLIENT_HELLO_CB))
  674. /*
  675. * Passing a -1 literal is a hack since
  676. * the real value was lost.
  677. * */
  678. || !TEST_int_eq(SSL_get_error(serverssl, -1),
  679. SSL_ERROR_WANT_CLIENT_HELLO_CB)
  680. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  681. SSL_ERROR_NONE)))
  682. goto end;
  683. testresult = 1;
  684. end:
  685. SSL_free(serverssl);
  686. SSL_free(clientssl);
  687. SSL_CTX_free(sctx);
  688. SSL_CTX_free(cctx);
  689. return testresult;
  690. }
  691. static int test_no_ems(void)
  692. {
  693. SSL_CTX *cctx = NULL, *sctx = NULL;
  694. SSL *clientssl = NULL, *serverssl = NULL;
  695. int testresult = 0;
  696. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  697. TLS1_VERSION, TLS1_2_VERSION,
  698. &sctx, &cctx, cert, privkey)) {
  699. printf("Unable to create SSL_CTX pair\n");
  700. goto end;
  701. }
  702. SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
  703. if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
  704. printf("Unable to create SSL objects\n");
  705. goto end;
  706. }
  707. if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
  708. printf("Creating SSL connection failed\n");
  709. goto end;
  710. }
  711. if (SSL_get_extms_support(serverssl)) {
  712. printf("Server reports Extended Master Secret support\n");
  713. goto end;
  714. }
  715. if (SSL_get_extms_support(clientssl)) {
  716. printf("Client reports Extended Master Secret support\n");
  717. goto end;
  718. }
  719. testresult = 1;
  720. end:
  721. SSL_free(serverssl);
  722. SSL_free(clientssl);
  723. SSL_CTX_free(sctx);
  724. SSL_CTX_free(cctx);
  725. return testresult;
  726. }
  727. /*
  728. * Very focused test to exercise a single case in the server-side state
  729. * machine, when the ChangeCipherState message needs to actually change
  730. * from one cipher to a different cipher (i.e., not changing from null
  731. * encryption to real encryption).
  732. */
  733. static int test_ccs_change_cipher(void)
  734. {
  735. SSL_CTX *cctx = NULL, *sctx = NULL;
  736. SSL *clientssl = NULL, *serverssl = NULL;
  737. SSL_SESSION *sess = NULL, *sesspre, *sesspost;
  738. int testresult = 0;
  739. int i;
  740. unsigned char buf;
  741. size_t readbytes;
  742. /*
  743. * Create a conection so we can resume and potentially (but not) use
  744. * a different cipher in the second connection.
  745. */
  746. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  747. TLS_client_method(),
  748. TLS1_VERSION, TLS1_2_VERSION,
  749. &sctx, &cctx, cert, privkey))
  750. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
  751. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  752. NULL, NULL))
  753. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  754. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  755. SSL_ERROR_NONE))
  756. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  757. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  758. goto end;
  759. shutdown_ssl_connection(serverssl, clientssl);
  760. serverssl = clientssl = NULL;
  761. /* Resume, preferring a different cipher. Our server will force the
  762. * same cipher to be used as the initial handshake. */
  763. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  764. NULL, NULL))
  765. || !TEST_true(SSL_set_session(clientssl, sess))
  766. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
  767. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  768. SSL_ERROR_NONE))
  769. || !TEST_true(SSL_session_reused(clientssl))
  770. || !TEST_true(SSL_session_reused(serverssl))
  771. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  772. || !TEST_ptr_eq(sesspre, sesspost)
  773. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  774. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  775. goto end;
  776. shutdown_ssl_connection(serverssl, clientssl);
  777. serverssl = clientssl = NULL;
  778. /*
  779. * Now create a fresh connection and try to renegotiate a different
  780. * cipher on it.
  781. */
  782. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  783. NULL, NULL))
  784. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  785. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  786. SSL_ERROR_NONE))
  787. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  788. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
  789. || !TEST_true(SSL_renegotiate(clientssl))
  790. || !TEST_true(SSL_renegotiate_pending(clientssl)))
  791. goto end;
  792. /* Actually drive the renegotiation. */
  793. for (i = 0; i < 3; i++) {
  794. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  795. if (!TEST_ulong_eq(readbytes, 0))
  796. goto end;
  797. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  798. SSL_ERROR_WANT_READ)) {
  799. goto end;
  800. }
  801. if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
  802. if (!TEST_ulong_eq(readbytes, 0))
  803. goto end;
  804. } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
  805. SSL_ERROR_WANT_READ)) {
  806. goto end;
  807. }
  808. }
  809. /* sesspre and sesspost should be different since the cipher changed. */
  810. if (!TEST_false(SSL_renegotiate_pending(clientssl))
  811. || !TEST_false(SSL_session_reused(clientssl))
  812. || !TEST_false(SSL_session_reused(serverssl))
  813. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  814. || !TEST_ptr_ne(sesspre, sesspost)
  815. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  816. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  817. goto end;
  818. shutdown_ssl_connection(serverssl, clientssl);
  819. serverssl = clientssl = NULL;
  820. testresult = 1;
  821. end:
  822. SSL_free(serverssl);
  823. SSL_free(clientssl);
  824. SSL_CTX_free(sctx);
  825. SSL_CTX_free(cctx);
  826. SSL_SESSION_free(sess);
  827. return testresult;
  828. }
  829. #endif
  830. static int execute_test_large_message(const SSL_METHOD *smeth,
  831. const SSL_METHOD *cmeth,
  832. int min_version, int max_version,
  833. int read_ahead)
  834. {
  835. SSL_CTX *cctx = NULL, *sctx = NULL;
  836. SSL *clientssl = NULL, *serverssl = NULL;
  837. int testresult = 0;
  838. int i;
  839. BIO *certbio = NULL;
  840. X509 *chaincert = NULL;
  841. int certlen;
  842. if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
  843. goto end;
  844. if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
  845. goto end;
  846. if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
  847. goto end;
  848. BIO_free(certbio);
  849. certbio = NULL;
  850. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  851. max_version, &sctx, &cctx, cert,
  852. privkey)))
  853. goto end;
  854. if (read_ahead) {
  855. /*
  856. * Test that read_ahead works correctly when dealing with large
  857. * records
  858. */
  859. SSL_CTX_set_read_ahead(cctx, 1);
  860. }
  861. /*
  862. * We assume the supplied certificate is big enough so that if we add
  863. * NUM_EXTRA_CERTS it will make the overall message large enough. The
  864. * default buffer size is requested to be 16k, but due to the way BUF_MEM
  865. * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
  866. * test we need to have a message larger than that.
  867. */
  868. certlen = i2d_X509(chaincert, NULL);
  869. OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
  870. (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
  871. for (i = 0; i < NUM_EXTRA_CERTS; i++) {
  872. if (!X509_up_ref(chaincert))
  873. goto end;
  874. if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
  875. X509_free(chaincert);
  876. goto end;
  877. }
  878. }
  879. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  880. NULL, NULL))
  881. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  882. SSL_ERROR_NONE)))
  883. goto end;
  884. /*
  885. * Calling SSL_clear() first is not required but this tests that SSL_clear()
  886. * doesn't leak.
  887. */
  888. if (!TEST_true(SSL_clear(serverssl)))
  889. goto end;
  890. testresult = 1;
  891. end:
  892. BIO_free(certbio);
  893. X509_free(chaincert);
  894. SSL_free(serverssl);
  895. SSL_free(clientssl);
  896. SSL_CTX_free(sctx);
  897. SSL_CTX_free(cctx);
  898. return testresult;
  899. }
  900. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
  901. !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
  902. #define TLS_CIPHER_MAX_REC_SEQ_SIZE 8
  903. /* sock must be connected */
  904. static int ktls_chk_platform(int sock)
  905. {
  906. if (!ktls_enable(sock))
  907. return 0;
  908. return 1;
  909. }
  910. static int ping_pong_query(SSL *clientssl, SSL *serverssl, int cfd, int sfd, int rec_seq_size)
  911. {
  912. static char count = 1;
  913. unsigned char cbuf[16000] = {0};
  914. unsigned char sbuf[16000];
  915. size_t err = 0;
  916. char crec_wseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  917. char crec_wseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  918. char crec_rseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  919. char crec_rseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  920. char srec_wseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  921. char srec_wseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  922. char srec_rseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  923. char srec_rseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE];
  924. cbuf[0] = count++;
  925. memcpy(crec_wseq_before, &clientssl->rlayer.write_sequence, rec_seq_size);
  926. memcpy(crec_rseq_before, &clientssl->rlayer.read_sequence, rec_seq_size);
  927. memcpy(srec_wseq_before, &serverssl->rlayer.write_sequence, rec_seq_size);
  928. memcpy(srec_rseq_before, &serverssl->rlayer.read_sequence, rec_seq_size);
  929. if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
  930. goto end;
  931. while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
  932. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
  933. goto end;
  934. }
  935. }
  936. if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
  937. goto end;
  938. while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
  939. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
  940. goto end;
  941. }
  942. }
  943. memcpy(crec_wseq_after, &clientssl->rlayer.write_sequence, rec_seq_size);
  944. memcpy(crec_rseq_after, &clientssl->rlayer.read_sequence, rec_seq_size);
  945. memcpy(srec_wseq_after, &serverssl->rlayer.write_sequence, rec_seq_size);
  946. memcpy(srec_rseq_after, &serverssl->rlayer.read_sequence, rec_seq_size);
  947. /* verify the payload */
  948. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  949. goto end;
  950. /* ktls is used then kernel sequences are used instead of OpenSSL sequences */
  951. if ((SSL_get_options(clientssl) & SSL_OP_ENABLE_KTLS) == 0) {
  952. if (!TEST_mem_ne(crec_wseq_before, rec_seq_size,
  953. crec_wseq_after, rec_seq_size))
  954. goto end;
  955. } else {
  956. if (!TEST_mem_eq(crec_wseq_before, rec_seq_size,
  957. crec_wseq_after, rec_seq_size))
  958. goto end;
  959. }
  960. if ((SSL_get_options(serverssl) & SSL_OP_ENABLE_KTLS) == 0) {
  961. if (!TEST_mem_ne(srec_wseq_before, rec_seq_size,
  962. srec_wseq_after, rec_seq_size))
  963. goto end;
  964. } else {
  965. if (!TEST_mem_eq(srec_wseq_before, rec_seq_size,
  966. srec_wseq_after, rec_seq_size))
  967. goto end;
  968. }
  969. if ((SSL_get_options(clientssl) & SSL_OP_ENABLE_KTLS) == 0
  970. #if defined(OPENSSL_NO_KTLS_RX)
  971. || 1
  972. #endif
  973. ) {
  974. if (!TEST_mem_ne(crec_rseq_before, rec_seq_size,
  975. crec_rseq_after, rec_seq_size))
  976. goto end;
  977. } else {
  978. if (!TEST_mem_eq(crec_rseq_before, rec_seq_size,
  979. crec_rseq_after, rec_seq_size))
  980. goto end;
  981. }
  982. if ((SSL_get_options(serverssl) & SSL_OP_ENABLE_KTLS) == 0
  983. #if defined(OPENSSL_NO_KTLS_RX)
  984. || 1
  985. #endif
  986. ) {
  987. if (!TEST_mem_ne(srec_rseq_before, rec_seq_size,
  988. srec_rseq_after, rec_seq_size))
  989. goto end;
  990. } else {
  991. if (!TEST_mem_eq(srec_rseq_before, rec_seq_size,
  992. srec_rseq_after, rec_seq_size))
  993. goto end;
  994. }
  995. return 1;
  996. end:
  997. return 0;
  998. }
  999. static int execute_test_ktls(int cis_ktls, int sis_ktls,
  1000. int tls_version, const char *cipher,
  1001. int rec_seq_size)
  1002. {
  1003. SSL_CTX *cctx = NULL, *sctx = NULL;
  1004. SSL *clientssl = NULL, *serverssl = NULL;
  1005. int testresult = 0;
  1006. int cfd, sfd;
  1007. if (!TEST_true(create_test_sockets(&cfd, &sfd)))
  1008. goto end;
  1009. /* Skip this test if the platform does not support ktls */
  1010. if (!ktls_chk_platform(cfd))
  1011. return 1;
  1012. /* Create a session based on SHA-256 */
  1013. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1014. TLS_client_method(),
  1015. tls_version, tls_version,
  1016. &sctx, &cctx, cert, privkey))
  1017. || !TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1018. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher))
  1019. || !TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1020. &clientssl, sfd, cfd)))
  1021. goto end;
  1022. if (cis_ktls) {
  1023. if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
  1024. goto end;
  1025. }
  1026. if (sis_ktls) {
  1027. if (!TEST_true(SSL_set_mode(serverssl, SSL_OP_ENABLE_KTLS)))
  1028. goto end;
  1029. }
  1030. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1031. SSL_ERROR_NONE)))
  1032. goto end;
  1033. if (!cis_ktls) {
  1034. if (!TEST_false(BIO_get_ktls_send(clientssl->wbio)))
  1035. goto end;
  1036. } else {
  1037. if (!TEST_true(BIO_get_ktls_send(clientssl->wbio)))
  1038. goto end;
  1039. }
  1040. if (!sis_ktls) {
  1041. if (!TEST_false(BIO_get_ktls_send(serverssl->wbio)))
  1042. goto end;
  1043. } else {
  1044. if (!TEST_true(BIO_get_ktls_send(serverssl->wbio)))
  1045. goto end;
  1046. }
  1047. if (!cis_ktls
  1048. #if defined(OPENSSL_NO_KTLS_RX)
  1049. || 1
  1050. #endif
  1051. ) {
  1052. if (!TEST_false(BIO_get_ktls_recv(clientssl->rbio)))
  1053. goto end;
  1054. } else {
  1055. if (!TEST_true(BIO_get_ktls_recv(clientssl->rbio)))
  1056. goto end;
  1057. }
  1058. if (!sis_ktls
  1059. #if defined(OPENSSL_NO_KTLS_RX)
  1060. || 1
  1061. #endif
  1062. ) {
  1063. if (!TEST_false(BIO_get_ktls_recv(serverssl->rbio)))
  1064. goto end;
  1065. } else {
  1066. if (!TEST_true(BIO_get_ktls_recv(serverssl->rbio)))
  1067. goto end;
  1068. }
  1069. if (!TEST_true(ping_pong_query(clientssl, serverssl, cfd, sfd,
  1070. rec_seq_size)))
  1071. goto end;
  1072. testresult = 1;
  1073. end:
  1074. if (clientssl) {
  1075. SSL_shutdown(clientssl);
  1076. SSL_free(clientssl);
  1077. }
  1078. if (serverssl) {
  1079. SSL_shutdown(serverssl);
  1080. SSL_free(serverssl);
  1081. }
  1082. SSL_CTX_free(sctx);
  1083. SSL_CTX_free(cctx);
  1084. serverssl = clientssl = NULL;
  1085. return testresult;
  1086. }
  1087. #define SENDFILE_SZ (16 * 4096)
  1088. #define SENDFILE_CHUNK (4 * 4096)
  1089. #define min(a,b) ((a) > (b) ? (b) : (a))
  1090. static int test_ktls_sendfile(int tls_version, const char *cipher)
  1091. {
  1092. SSL_CTX *cctx = NULL, *sctx = NULL;
  1093. SSL *clientssl = NULL, *serverssl = NULL;
  1094. unsigned char *buf, *buf_dst;
  1095. BIO *out = NULL, *in = NULL;
  1096. int cfd, sfd, ffd, err;
  1097. ssize_t chunk_size = 0;
  1098. off_t chunk_off = 0;
  1099. int testresult = 0;
  1100. FILE *ffdp;
  1101. buf = OPENSSL_zalloc(SENDFILE_SZ);
  1102. buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
  1103. if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
  1104. || !TEST_true(create_test_sockets(&cfd, &sfd)))
  1105. goto end;
  1106. /* Skip this test if the platform does not support ktls */
  1107. if (!ktls_chk_platform(sfd)) {
  1108. testresult = 1;
  1109. goto end;
  1110. }
  1111. /* Create a session based on SHA-256 */
  1112. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1113. TLS_client_method(),
  1114. tls_version, tls_version,
  1115. &sctx, &cctx, cert, privkey))
  1116. || !TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1117. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher))
  1118. || !TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1119. &clientssl, sfd, cfd)))
  1120. goto end;
  1121. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1122. SSL_ERROR_NONE))
  1123. || !TEST_true(BIO_get_ktls_send(serverssl->wbio)))
  1124. goto end;
  1125. if (!TEST_true(RAND_bytes_ex(libctx, buf, SENDFILE_SZ)))
  1126. goto end;
  1127. out = BIO_new_file(tmpfilename, "wb");
  1128. if (!TEST_ptr(out))
  1129. goto end;
  1130. if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
  1131. goto end;
  1132. BIO_free(out);
  1133. out = NULL;
  1134. in = BIO_new_file(tmpfilename, "rb");
  1135. BIO_get_fp(in, &ffdp);
  1136. ffd = fileno(ffdp);
  1137. while (chunk_off < SENDFILE_SZ) {
  1138. chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
  1139. while ((err = SSL_sendfile(serverssl,
  1140. ffd,
  1141. chunk_off,
  1142. chunk_size,
  1143. 0)) != chunk_size) {
  1144. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
  1145. goto end;
  1146. }
  1147. while ((err = SSL_read(clientssl,
  1148. buf_dst + chunk_off,
  1149. chunk_size)) != chunk_size) {
  1150. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
  1151. goto end;
  1152. }
  1153. /* verify the payload */
  1154. if (!TEST_mem_eq(buf_dst + chunk_off,
  1155. chunk_size,
  1156. buf + chunk_off,
  1157. chunk_size))
  1158. goto end;
  1159. chunk_off += chunk_size;
  1160. }
  1161. testresult = 1;
  1162. end:
  1163. if (clientssl) {
  1164. SSL_shutdown(clientssl);
  1165. SSL_free(clientssl);
  1166. }
  1167. if (serverssl) {
  1168. SSL_shutdown(serverssl);
  1169. SSL_free(serverssl);
  1170. }
  1171. SSL_CTX_free(sctx);
  1172. SSL_CTX_free(cctx);
  1173. serverssl = clientssl = NULL;
  1174. BIO_free(out);
  1175. BIO_free(in);
  1176. OPENSSL_free(buf);
  1177. OPENSSL_free(buf_dst);
  1178. return testresult;
  1179. }
  1180. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  1181. static int test_ktls(int test)
  1182. {
  1183. int cis_ktls, sis_ktls;
  1184. int tlsver, testresult;
  1185. if (test > 3) {
  1186. #if defined(OSSL_NO_USABLE_TLS1_3)
  1187. return 1;
  1188. #else
  1189. test -= 4;
  1190. tlsver = TLS1_3_VERSION;
  1191. #endif
  1192. } else {
  1193. #if defined(OPENSSL_NO_TLS1_2)
  1194. return 1;
  1195. #else
  1196. tlsver = TLS1_2_VERSION;
  1197. #endif
  1198. }
  1199. cis_ktls = (test & 1) != 0;
  1200. sis_ktls = (test & 2) != 0;
  1201. testresult = 1;
  1202. #ifdef OPENSSL_KTLS_AES_GCM_128
  1203. testresult &= execute_test_ktls(cis_ktls, sis_ktls, tlsver,
  1204. "AES128-GCM-SHA256",
  1205. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  1206. #endif
  1207. #ifdef OPENSSL_KTLS_AES_CCM_128
  1208. testresult &= execute_test_ktls(cis_ktls, sis_ktls, tlsver, "AES128-CCM",
  1209. TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
  1210. #endif
  1211. #ifdef OPENSSL_KTLS_AES_GCM_256
  1212. testresult &= execute_test_ktls(cis_ktls, sis_ktls, tlsver,
  1213. "AES256-GCM-SHA384",
  1214. TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
  1215. #endif
  1216. return testresult;
  1217. }
  1218. static int test_ktls_sendfile_anytls(int tst)
  1219. {
  1220. char *cipher[] = {"AES128-GCM-SHA256","AES128-CCM","AES256-GCM-SHA384"};
  1221. int tlsver;
  1222. if (tst > 2) {
  1223. #if defined(OSSL_NO_USABLE_TLS1_3)
  1224. return 1;
  1225. #else
  1226. tst -= 3;
  1227. tlsver = TLS1_3_VERSION;
  1228. #endif
  1229. } else {
  1230. #if defined(OPENSSL_NO_TLS1_2)
  1231. return 1;
  1232. #else
  1233. tlsver = TLS1_2_VERSION;
  1234. #endif
  1235. }
  1236. #ifndef OPENSSL_KTLS_AES_GCM_128
  1237. if(tst == 0) return 1;
  1238. #endif
  1239. #ifndef OPENSSL_KTLS_AES_CCM_128
  1240. if(tst == 1) return 1;
  1241. #endif
  1242. #ifndef OPENSSL_KTLS_AES_GCM_256
  1243. if(tst == 2) return 1;
  1244. #endif
  1245. return test_ktls_sendfile(tlsver, cipher[tst]);
  1246. }
  1247. #endif
  1248. #endif
  1249. static int test_large_message_tls(void)
  1250. {
  1251. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1252. TLS1_VERSION, 0, 0);
  1253. }
  1254. static int test_large_message_tls_read_ahead(void)
  1255. {
  1256. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1257. TLS1_VERSION, 0, 1);
  1258. }
  1259. #ifndef OPENSSL_NO_DTLS
  1260. static int test_large_message_dtls(void)
  1261. {
  1262. /*
  1263. * read_ahead is not relevant to DTLS because DTLS always acts as if
  1264. * read_ahead is set.
  1265. */
  1266. return execute_test_large_message(DTLS_server_method(),
  1267. DTLS_client_method(),
  1268. DTLS1_VERSION, 0, 0);
  1269. }
  1270. #endif
  1271. static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
  1272. const SSL_METHOD *cmeth,
  1273. int min_version, int max_version)
  1274. {
  1275. size_t i;
  1276. SSL_CTX *cctx = NULL, *sctx = NULL;
  1277. SSL *clientssl = NULL, *serverssl = NULL;
  1278. int testresult = 0;
  1279. SSL3_RECORD *rr;
  1280. void *zbuf;
  1281. static unsigned char cbuf[16000];
  1282. static unsigned char sbuf[16000];
  1283. if (!TEST_true(create_ssl_ctx_pair(libctx,
  1284. smeth, cmeth,
  1285. min_version, max_version,
  1286. &sctx, &cctx, cert,
  1287. privkey)))
  1288. goto end;
  1289. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1290. NULL, NULL)))
  1291. goto end;
  1292. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
  1293. goto end;
  1294. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1295. SSL_ERROR_NONE)))
  1296. goto end;
  1297. for (i = 0; i < sizeof(cbuf); i++) {
  1298. cbuf[i] = i & 0xff;
  1299. }
  1300. if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
  1301. goto end;
  1302. if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1303. goto end;
  1304. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  1305. goto end;
  1306. /*
  1307. * Since we called SSL_peek(), we know the data in the record
  1308. * layer is a plaintext record. We can gather the pointer to check
  1309. * for zeroization after SSL_read().
  1310. */
  1311. rr = serverssl->rlayer.rrec;
  1312. zbuf = &rr->data[rr->off];
  1313. if (!TEST_int_eq(rr->length, sizeof(cbuf)))
  1314. goto end;
  1315. /*
  1316. * After SSL_peek() the plaintext must still be stored in the
  1317. * record.
  1318. */
  1319. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1320. goto end;
  1321. memset(sbuf, 0, sizeof(sbuf));
  1322. if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1323. goto end;
  1324. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
  1325. goto end;
  1326. /* Check if rbuf is cleansed */
  1327. memset(cbuf, 0, sizeof(cbuf));
  1328. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1329. goto end;
  1330. testresult = 1;
  1331. end:
  1332. SSL_free(serverssl);
  1333. SSL_free(clientssl);
  1334. SSL_CTX_free(sctx);
  1335. SSL_CTX_free(cctx);
  1336. return testresult;
  1337. }
  1338. static int test_cleanse_plaintext(void)
  1339. {
  1340. #if !defined(OPENSSL_NO_TLS1_2)
  1341. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1342. TLS_client_method(),
  1343. TLS1_2_VERSION,
  1344. TLS1_2_VERSION)))
  1345. return 0;
  1346. #endif
  1347. #if !defined(OSSL_NO_USABLE_TLS1_3)
  1348. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1349. TLS_client_method(),
  1350. TLS1_3_VERSION,
  1351. TLS1_3_VERSION)))
  1352. return 0;
  1353. #endif
  1354. #if !defined(OPENSSL_NO_DTLS)
  1355. if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
  1356. DTLS_client_method(),
  1357. DTLS1_VERSION,
  1358. 0)))
  1359. return 0;
  1360. #endif
  1361. return 1;
  1362. }
  1363. #ifndef OPENSSL_NO_OCSP
  1364. static int ocsp_server_cb(SSL *s, void *arg)
  1365. {
  1366. int *argi = (int *)arg;
  1367. unsigned char *copy = NULL;
  1368. STACK_OF(OCSP_RESPID) *ids = NULL;
  1369. OCSP_RESPID *id = NULL;
  1370. if (*argi == 2) {
  1371. /* In this test we are expecting exactly 1 OCSP_RESPID */
  1372. SSL_get_tlsext_status_ids(s, &ids);
  1373. if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
  1374. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1375. id = sk_OCSP_RESPID_value(ids, 0);
  1376. if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
  1377. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1378. } else if (*argi != 1) {
  1379. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1380. }
  1381. if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
  1382. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1383. SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
  1384. ocsp_server_called = 1;
  1385. return SSL_TLSEXT_ERR_OK;
  1386. }
  1387. static int ocsp_client_cb(SSL *s, void *arg)
  1388. {
  1389. int *argi = (int *)arg;
  1390. const unsigned char *respderin;
  1391. size_t len;
  1392. if (*argi != 1 && *argi != 2)
  1393. return 0;
  1394. len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
  1395. if (!TEST_mem_eq(orespder, len, respderin, len))
  1396. return 0;
  1397. ocsp_client_called = 1;
  1398. return 1;
  1399. }
  1400. static int test_tlsext_status_type(void)
  1401. {
  1402. SSL_CTX *cctx = NULL, *sctx = NULL;
  1403. SSL *clientssl = NULL, *serverssl = NULL;
  1404. int testresult = 0;
  1405. STACK_OF(OCSP_RESPID) *ids = NULL;
  1406. OCSP_RESPID *id = NULL;
  1407. BIO *certbio = NULL;
  1408. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  1409. TLS1_VERSION, 0,
  1410. &sctx, &cctx, cert, privkey))
  1411. return 0;
  1412. if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
  1413. goto end;
  1414. /* First just do various checks getting and setting tlsext_status_type */
  1415. clientssl = SSL_new(cctx);
  1416. if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
  1417. || !TEST_true(SSL_set_tlsext_status_type(clientssl,
  1418. TLSEXT_STATUSTYPE_ocsp))
  1419. || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
  1420. TLSEXT_STATUSTYPE_ocsp))
  1421. goto end;
  1422. SSL_free(clientssl);
  1423. clientssl = NULL;
  1424. if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
  1425. || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
  1426. goto end;
  1427. clientssl = SSL_new(cctx);
  1428. if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
  1429. goto end;
  1430. SSL_free(clientssl);
  1431. clientssl = NULL;
  1432. /*
  1433. * Now actually do a handshake and check OCSP information is exchanged and
  1434. * the callbacks get called
  1435. */
  1436. SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
  1437. SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
  1438. SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
  1439. SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
  1440. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1441. &clientssl, NULL, NULL))
  1442. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  1443. SSL_ERROR_NONE))
  1444. || !TEST_true(ocsp_client_called)
  1445. || !TEST_true(ocsp_server_called))
  1446. goto end;
  1447. SSL_free(serverssl);
  1448. SSL_free(clientssl);
  1449. serverssl = NULL;
  1450. clientssl = NULL;
  1451. /* Try again but this time force the server side callback to fail */
  1452. ocsp_client_called = 0;
  1453. ocsp_server_called = 0;
  1454. cdummyarg = 0;
  1455. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1456. &clientssl, NULL, NULL))
  1457. /* This should fail because the callback will fail */
  1458. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  1459. SSL_ERROR_NONE))
  1460. || !TEST_false(ocsp_client_called)
  1461. || !TEST_false(ocsp_server_called))
  1462. goto end;
  1463. SSL_free(serverssl);
  1464. SSL_free(clientssl);
  1465. serverssl = NULL;
  1466. clientssl = NULL;
  1467. /*
  1468. * This time we'll get the client to send an OCSP_RESPID that it will
  1469. * accept.
  1470. */
  1471. ocsp_client_called = 0;
  1472. ocsp_server_called = 0;
  1473. cdummyarg = 2;
  1474. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1475. &clientssl, NULL, NULL)))
  1476. goto end;
  1477. /*
  1478. * We'll just use any old cert for this test - it doesn't have to be an OCSP
  1479. * specific one. We'll use the server cert.
  1480. */
  1481. if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
  1482. || !TEST_ptr(id = OCSP_RESPID_new())
  1483. || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
  1484. || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
  1485. || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
  1486. || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
  1487. || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
  1488. goto end;
  1489. id = NULL;
  1490. SSL_set_tlsext_status_ids(clientssl, ids);
  1491. /* Control has been transferred */
  1492. ids = NULL;
  1493. BIO_free(certbio);
  1494. certbio = NULL;
  1495. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1496. SSL_ERROR_NONE))
  1497. || !TEST_true(ocsp_client_called)
  1498. || !TEST_true(ocsp_server_called))
  1499. goto end;
  1500. testresult = 1;
  1501. end:
  1502. SSL_free(serverssl);
  1503. SSL_free(clientssl);
  1504. SSL_CTX_free(sctx);
  1505. SSL_CTX_free(cctx);
  1506. sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
  1507. OCSP_RESPID_free(id);
  1508. BIO_free(certbio);
  1509. X509_free(ocspcert);
  1510. ocspcert = NULL;
  1511. return testresult;
  1512. }
  1513. #endif
  1514. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  1515. static int new_called, remove_called, get_called;
  1516. static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
  1517. {
  1518. new_called++;
  1519. /*
  1520. * sess has been up-refed for us, but we don't actually need it so free it
  1521. * immediately.
  1522. */
  1523. SSL_SESSION_free(sess);
  1524. return 1;
  1525. }
  1526. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  1527. {
  1528. remove_called++;
  1529. }
  1530. static SSL_SESSION *get_sess_val = NULL;
  1531. static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
  1532. int *copy)
  1533. {
  1534. get_called++;
  1535. *copy = 1;
  1536. return get_sess_val;
  1537. }
  1538. static int execute_test_session(int maxprot, int use_int_cache,
  1539. int use_ext_cache, long s_options)
  1540. {
  1541. SSL_CTX *sctx = NULL, *cctx = NULL;
  1542. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  1543. SSL *serverssl2 = NULL, *clientssl2 = NULL;
  1544. # ifndef OPENSSL_NO_TLS1_1
  1545. SSL *serverssl3 = NULL, *clientssl3 = NULL;
  1546. # endif
  1547. SSL_SESSION *sess1 = NULL, *sess2 = NULL;
  1548. int testresult = 0, numnewsesstick = 1;
  1549. new_called = remove_called = 0;
  1550. /* TLSv1.3 sends 2 NewSessionTickets */
  1551. if (maxprot == TLS1_3_VERSION)
  1552. numnewsesstick = 2;
  1553. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1554. TLS_client_method(), TLS1_VERSION, 0,
  1555. &sctx, &cctx, cert, privkey)))
  1556. return 0;
  1557. /*
  1558. * Only allow the max protocol version so we can force a connection failure
  1559. * later
  1560. */
  1561. SSL_CTX_set_min_proto_version(cctx, maxprot);
  1562. SSL_CTX_set_max_proto_version(cctx, maxprot);
  1563. /* Set up session cache */
  1564. if (use_ext_cache) {
  1565. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  1566. SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
  1567. }
  1568. if (use_int_cache) {
  1569. /* Also covers instance where both are set */
  1570. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
  1571. } else {
  1572. SSL_CTX_set_session_cache_mode(cctx,
  1573. SSL_SESS_CACHE_CLIENT
  1574. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1575. }
  1576. if (s_options) {
  1577. SSL_CTX_set_options(sctx, s_options);
  1578. }
  1579. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1580. NULL, NULL))
  1581. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1582. SSL_ERROR_NONE))
  1583. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
  1584. goto end;
  1585. /* Should fail because it should already be in the cache */
  1586. if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
  1587. goto end;
  1588. if (use_ext_cache
  1589. && (!TEST_int_eq(new_called, numnewsesstick)
  1590. || !TEST_int_eq(remove_called, 0)))
  1591. goto end;
  1592. new_called = remove_called = 0;
  1593. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1594. &clientssl2, NULL, NULL))
  1595. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1596. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1597. SSL_ERROR_NONE))
  1598. || !TEST_true(SSL_session_reused(clientssl2)))
  1599. goto end;
  1600. if (maxprot == TLS1_3_VERSION) {
  1601. /*
  1602. * In TLSv1.3 we should have created a new session even though we have
  1603. * resumed. Since we attempted a resume we should also have removed the
  1604. * old ticket from the cache so that we try to only use tickets once.
  1605. */
  1606. if (use_ext_cache
  1607. && (!TEST_int_eq(new_called, 1)
  1608. || !TEST_int_eq(remove_called, 1)))
  1609. goto end;
  1610. } else {
  1611. /*
  1612. * In TLSv1.2 we expect to have resumed so no sessions added or
  1613. * removed.
  1614. */
  1615. if (use_ext_cache
  1616. && (!TEST_int_eq(new_called, 0)
  1617. || !TEST_int_eq(remove_called, 0)))
  1618. goto end;
  1619. }
  1620. SSL_SESSION_free(sess1);
  1621. if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
  1622. goto end;
  1623. shutdown_ssl_connection(serverssl2, clientssl2);
  1624. serverssl2 = clientssl2 = NULL;
  1625. new_called = remove_called = 0;
  1626. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1627. &clientssl2, NULL, NULL))
  1628. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1629. SSL_ERROR_NONE)))
  1630. goto end;
  1631. if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
  1632. goto end;
  1633. if (use_ext_cache
  1634. && (!TEST_int_eq(new_called, numnewsesstick)
  1635. || !TEST_int_eq(remove_called, 0)))
  1636. goto end;
  1637. new_called = remove_called = 0;
  1638. /*
  1639. * This should clear sess2 from the cache because it is a "bad" session.
  1640. * See SSL_set_session() documentation.
  1641. */
  1642. if (!TEST_true(SSL_set_session(clientssl2, sess1)))
  1643. goto end;
  1644. if (use_ext_cache
  1645. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1646. goto end;
  1647. if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
  1648. goto end;
  1649. if (use_int_cache) {
  1650. /* Should succeeded because it should not already be in the cache */
  1651. if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
  1652. || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
  1653. goto end;
  1654. }
  1655. new_called = remove_called = 0;
  1656. /* This shouldn't be in the cache so should fail */
  1657. if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
  1658. goto end;
  1659. if (use_ext_cache
  1660. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1661. goto end;
  1662. # if !defined(OPENSSL_NO_TLS1_1)
  1663. new_called = remove_called = 0;
  1664. /* Force a connection failure */
  1665. SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
  1666. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
  1667. &clientssl3, NULL, NULL))
  1668. || !TEST_true(SSL_set_session(clientssl3, sess1))
  1669. /* This should fail because of the mismatched protocol versions */
  1670. || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
  1671. SSL_ERROR_NONE)))
  1672. goto end;
  1673. /* We should have automatically removed the session from the cache */
  1674. if (use_ext_cache
  1675. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1676. goto end;
  1677. /* Should succeed because it should not already be in the cache */
  1678. if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
  1679. goto end;
  1680. # endif
  1681. /* Now do some tests for server side caching */
  1682. if (use_ext_cache) {
  1683. SSL_CTX_sess_set_new_cb(cctx, NULL);
  1684. SSL_CTX_sess_set_remove_cb(cctx, NULL);
  1685. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  1686. SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
  1687. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  1688. get_sess_val = NULL;
  1689. }
  1690. SSL_CTX_set_session_cache_mode(cctx, 0);
  1691. /* Internal caching is the default on the server side */
  1692. if (!use_int_cache)
  1693. SSL_CTX_set_session_cache_mode(sctx,
  1694. SSL_SESS_CACHE_SERVER
  1695. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1696. SSL_free(serverssl1);
  1697. SSL_free(clientssl1);
  1698. serverssl1 = clientssl1 = NULL;
  1699. SSL_free(serverssl2);
  1700. SSL_free(clientssl2);
  1701. serverssl2 = clientssl2 = NULL;
  1702. SSL_SESSION_free(sess1);
  1703. sess1 = NULL;
  1704. SSL_SESSION_free(sess2);
  1705. sess2 = NULL;
  1706. SSL_CTX_set_max_proto_version(sctx, maxprot);
  1707. if (maxprot == TLS1_2_VERSION)
  1708. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  1709. new_called = remove_called = get_called = 0;
  1710. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1711. NULL, NULL))
  1712. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1713. SSL_ERROR_NONE))
  1714. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
  1715. || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
  1716. goto end;
  1717. if (use_int_cache) {
  1718. if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
  1719. /*
  1720. * In TLSv1.3 it should not have been added to the internal cache,
  1721. * except in the case where we also have an external cache (in that
  1722. * case it gets added to the cache in order to generate remove
  1723. * events after timeout).
  1724. */
  1725. if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
  1726. goto end;
  1727. } else {
  1728. /* Should fail because it should already be in the cache */
  1729. if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
  1730. goto end;
  1731. }
  1732. }
  1733. if (use_ext_cache) {
  1734. SSL_SESSION *tmp = sess2;
  1735. if (!TEST_int_eq(new_called, numnewsesstick)
  1736. || !TEST_int_eq(remove_called, 0)
  1737. || !TEST_int_eq(get_called, 0))
  1738. goto end;
  1739. /*
  1740. * Delete the session from the internal cache to force a lookup from
  1741. * the external cache. We take a copy first because
  1742. * SSL_CTX_remove_session() also marks the session as non-resumable.
  1743. */
  1744. if (use_int_cache && maxprot != TLS1_3_VERSION) {
  1745. if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
  1746. || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
  1747. goto end;
  1748. SSL_SESSION_free(sess2);
  1749. }
  1750. sess2 = tmp;
  1751. }
  1752. new_called = remove_called = get_called = 0;
  1753. get_sess_val = sess2;
  1754. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1755. &clientssl2, NULL, NULL))
  1756. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1757. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1758. SSL_ERROR_NONE))
  1759. || !TEST_true(SSL_session_reused(clientssl2)))
  1760. goto end;
  1761. if (use_ext_cache) {
  1762. if (!TEST_int_eq(remove_called, 0))
  1763. goto end;
  1764. if (maxprot == TLS1_3_VERSION) {
  1765. if (!TEST_int_eq(new_called, 1)
  1766. || !TEST_int_eq(get_called, 0))
  1767. goto end;
  1768. } else {
  1769. if (!TEST_int_eq(new_called, 0)
  1770. || !TEST_int_eq(get_called, 1))
  1771. goto end;
  1772. }
  1773. }
  1774. testresult = 1;
  1775. end:
  1776. SSL_free(serverssl1);
  1777. SSL_free(clientssl1);
  1778. SSL_free(serverssl2);
  1779. SSL_free(clientssl2);
  1780. # ifndef OPENSSL_NO_TLS1_1
  1781. SSL_free(serverssl3);
  1782. SSL_free(clientssl3);
  1783. # endif
  1784. SSL_SESSION_free(sess1);
  1785. SSL_SESSION_free(sess2);
  1786. SSL_CTX_free(sctx);
  1787. SSL_CTX_free(cctx);
  1788. return testresult;
  1789. }
  1790. #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  1791. static int test_session_with_only_int_cache(void)
  1792. {
  1793. #ifndef OSSL_NO_USABLE_TLS1_3
  1794. if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
  1795. return 0;
  1796. #endif
  1797. #ifndef OPENSSL_NO_TLS1_2
  1798. return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
  1799. #else
  1800. return 1;
  1801. #endif
  1802. }
  1803. static int test_session_with_only_ext_cache(void)
  1804. {
  1805. #ifndef OSSL_NO_USABLE_TLS1_3
  1806. if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
  1807. return 0;
  1808. #endif
  1809. #ifndef OPENSSL_NO_TLS1_2
  1810. return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
  1811. #else
  1812. return 1;
  1813. #endif
  1814. }
  1815. static int test_session_with_both_cache(void)
  1816. {
  1817. #ifndef OSSL_NO_USABLE_TLS1_3
  1818. if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
  1819. return 0;
  1820. #endif
  1821. #ifndef OPENSSL_NO_TLS1_2
  1822. return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
  1823. #else
  1824. return 1;
  1825. #endif
  1826. }
  1827. static int test_session_wo_ca_names(void)
  1828. {
  1829. #ifndef OSSL_NO_USABLE_TLS1_3
  1830. if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
  1831. return 0;
  1832. #endif
  1833. #ifndef OPENSSL_NO_TLS1_2
  1834. return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
  1835. #else
  1836. return 1;
  1837. #endif
  1838. }
  1839. #ifndef OSSL_NO_USABLE_TLS1_3
  1840. static SSL_SESSION *sesscache[6];
  1841. static int do_cache;
  1842. static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
  1843. {
  1844. if (do_cache) {
  1845. sesscache[new_called] = sess;
  1846. } else {
  1847. /* We don't need the reference to the session, so free it */
  1848. SSL_SESSION_free(sess);
  1849. }
  1850. new_called++;
  1851. return 1;
  1852. }
  1853. static int post_handshake_verify(SSL *sssl, SSL *cssl)
  1854. {
  1855. SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
  1856. if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
  1857. return 0;
  1858. /* Start handshake on the server and client */
  1859. if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
  1860. || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
  1861. || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
  1862. || !TEST_true(create_ssl_connection(sssl, cssl,
  1863. SSL_ERROR_NONE)))
  1864. return 0;
  1865. return 1;
  1866. }
  1867. static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
  1868. SSL_CTX **cctx)
  1869. {
  1870. int sess_id_ctx = 1;
  1871. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1872. TLS_client_method(), TLS1_VERSION, 0,
  1873. sctx, cctx, cert, privkey))
  1874. || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
  1875. || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
  1876. (void *)&sess_id_ctx,
  1877. sizeof(sess_id_ctx))))
  1878. return 0;
  1879. if (stateful)
  1880. SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
  1881. SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
  1882. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1883. SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
  1884. return 1;
  1885. }
  1886. static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
  1887. {
  1888. SSL *serverssl = NULL, *clientssl = NULL;
  1889. int i;
  1890. /* Test that we can resume with all the tickets we got given */
  1891. for (i = 0; i < idx * 2; i++) {
  1892. new_called = 0;
  1893. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1894. &clientssl, NULL, NULL))
  1895. || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
  1896. goto end;
  1897. SSL_set_post_handshake_auth(clientssl, 1);
  1898. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1899. SSL_ERROR_NONE)))
  1900. goto end;
  1901. /*
  1902. * Following a successful resumption we only get 1 ticket. After a
  1903. * failed one we should get idx tickets.
  1904. */
  1905. if (succ) {
  1906. if (!TEST_true(SSL_session_reused(clientssl))
  1907. || !TEST_int_eq(new_called, 1))
  1908. goto end;
  1909. } else {
  1910. if (!TEST_false(SSL_session_reused(clientssl))
  1911. || !TEST_int_eq(new_called, idx))
  1912. goto end;
  1913. }
  1914. new_called = 0;
  1915. /* After a post-handshake authentication we should get 1 new ticket */
  1916. if (succ
  1917. && (!post_handshake_verify(serverssl, clientssl)
  1918. || !TEST_int_eq(new_called, 1)))
  1919. goto end;
  1920. SSL_shutdown(clientssl);
  1921. SSL_shutdown(serverssl);
  1922. SSL_free(serverssl);
  1923. SSL_free(clientssl);
  1924. serverssl = clientssl = NULL;
  1925. SSL_SESSION_free(sesscache[i]);
  1926. sesscache[i] = NULL;
  1927. }
  1928. return 1;
  1929. end:
  1930. SSL_free(clientssl);
  1931. SSL_free(serverssl);
  1932. return 0;
  1933. }
  1934. static int test_tickets(int stateful, int idx)
  1935. {
  1936. SSL_CTX *sctx = NULL, *cctx = NULL;
  1937. SSL *serverssl = NULL, *clientssl = NULL;
  1938. int testresult = 0;
  1939. size_t j;
  1940. /* idx is the test number, but also the number of tickets we want */
  1941. new_called = 0;
  1942. do_cache = 1;
  1943. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  1944. goto end;
  1945. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1946. &clientssl, NULL, NULL)))
  1947. goto end;
  1948. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1949. SSL_ERROR_NONE))
  1950. /* Check we got the number of tickets we were expecting */
  1951. || !TEST_int_eq(idx, new_called))
  1952. goto end;
  1953. SSL_shutdown(clientssl);
  1954. SSL_shutdown(serverssl);
  1955. SSL_free(serverssl);
  1956. SSL_free(clientssl);
  1957. SSL_CTX_free(sctx);
  1958. SSL_CTX_free(cctx);
  1959. clientssl = serverssl = NULL;
  1960. sctx = cctx = NULL;
  1961. /*
  1962. * Now we try to resume with the tickets we previously created. The
  1963. * resumption attempt is expected to fail (because we're now using a new
  1964. * SSL_CTX). We should see idx number of tickets issued again.
  1965. */
  1966. /* Stop caching sessions - just count them */
  1967. do_cache = 0;
  1968. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  1969. goto end;
  1970. if (!check_resumption(idx, sctx, cctx, 0))
  1971. goto end;
  1972. /* Start again with caching sessions */
  1973. new_called = 0;
  1974. do_cache = 1;
  1975. SSL_CTX_free(sctx);
  1976. SSL_CTX_free(cctx);
  1977. sctx = cctx = NULL;
  1978. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  1979. goto end;
  1980. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1981. &clientssl, NULL, NULL)))
  1982. goto end;
  1983. SSL_set_post_handshake_auth(clientssl, 1);
  1984. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1985. SSL_ERROR_NONE))
  1986. /* Check we got the number of tickets we were expecting */
  1987. || !TEST_int_eq(idx, new_called))
  1988. goto end;
  1989. /* After a post-handshake authentication we should get new tickets issued */
  1990. if (!post_handshake_verify(serverssl, clientssl)
  1991. || !TEST_int_eq(idx * 2, new_called))
  1992. goto end;
  1993. SSL_shutdown(clientssl);
  1994. SSL_shutdown(serverssl);
  1995. SSL_free(serverssl);
  1996. SSL_free(clientssl);
  1997. serverssl = clientssl = NULL;
  1998. /* Stop caching sessions - just count them */
  1999. do_cache = 0;
  2000. /*
  2001. * Check we can resume with all the tickets we created. This time around the
  2002. * resumptions should all be successful.
  2003. */
  2004. if (!check_resumption(idx, sctx, cctx, 1))
  2005. goto end;
  2006. testresult = 1;
  2007. end:
  2008. SSL_free(serverssl);
  2009. SSL_free(clientssl);
  2010. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  2011. SSL_SESSION_free(sesscache[j]);
  2012. sesscache[j] = NULL;
  2013. }
  2014. SSL_CTX_free(sctx);
  2015. SSL_CTX_free(cctx);
  2016. return testresult;
  2017. }
  2018. static int test_stateless_tickets(int idx)
  2019. {
  2020. return test_tickets(0, idx);
  2021. }
  2022. static int test_stateful_tickets(int idx)
  2023. {
  2024. return test_tickets(1, idx);
  2025. }
  2026. static int test_psk_tickets(void)
  2027. {
  2028. SSL_CTX *sctx = NULL, *cctx = NULL;
  2029. SSL *serverssl = NULL, *clientssl = NULL;
  2030. int testresult = 0;
  2031. int sess_id_ctx = 1;
  2032. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2033. TLS_client_method(), TLS1_VERSION, 0,
  2034. &sctx, &cctx, NULL, NULL))
  2035. || !TEST_true(SSL_CTX_set_session_id_context(sctx,
  2036. (void *)&sess_id_ctx,
  2037. sizeof(sess_id_ctx))))
  2038. goto end;
  2039. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  2040. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  2041. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  2042. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  2043. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2044. use_session_cb_cnt = 0;
  2045. find_session_cb_cnt = 0;
  2046. srvid = pskid;
  2047. new_called = 0;
  2048. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2049. NULL, NULL)))
  2050. goto end;
  2051. clientpsk = serverpsk = create_a_psk(clientssl);
  2052. if (!TEST_ptr(clientpsk))
  2053. goto end;
  2054. SSL_SESSION_up_ref(clientpsk);
  2055. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2056. SSL_ERROR_NONE))
  2057. || !TEST_int_eq(1, find_session_cb_cnt)
  2058. || !TEST_int_eq(1, use_session_cb_cnt)
  2059. /* We should always get 1 ticket when using external PSK */
  2060. || !TEST_int_eq(1, new_called))
  2061. goto end;
  2062. testresult = 1;
  2063. end:
  2064. SSL_free(serverssl);
  2065. SSL_free(clientssl);
  2066. SSL_CTX_free(sctx);
  2067. SSL_CTX_free(cctx);
  2068. SSL_SESSION_free(clientpsk);
  2069. SSL_SESSION_free(serverpsk);
  2070. clientpsk = serverpsk = NULL;
  2071. return testresult;
  2072. }
  2073. static int test_extra_tickets(int idx)
  2074. {
  2075. SSL_CTX *sctx = NULL, *cctx = NULL;
  2076. SSL *serverssl = NULL, *clientssl = NULL;
  2077. BIO *bretry = BIO_new(bio_s_always_retry());
  2078. BIO *tmp = NULL;
  2079. int testresult = 0;
  2080. int stateful = 0;
  2081. size_t nbytes;
  2082. unsigned char c, buf[1];
  2083. new_called = 0;
  2084. do_cache = 1;
  2085. if (idx >= 3) {
  2086. idx -= 3;
  2087. stateful = 1;
  2088. }
  2089. if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
  2090. goto end;
  2091. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  2092. /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
  2093. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2094. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2095. &clientssl, NULL, NULL)))
  2096. goto end;
  2097. /*
  2098. * Note that we have new_session_cb on both sctx and cctx, so new_called is
  2099. * incremented by both client and server.
  2100. */
  2101. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2102. SSL_ERROR_NONE))
  2103. /* Check we got the number of tickets we were expecting */
  2104. || !TEST_int_eq(idx * 2, new_called)
  2105. || !TEST_true(SSL_new_session_ticket(serverssl))
  2106. || !TEST_true(SSL_new_session_ticket(serverssl))
  2107. || !TEST_int_eq(idx * 2, new_called))
  2108. goto end;
  2109. /* Now try a (real) write to actually send the tickets */
  2110. c = '1';
  2111. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2112. || !TEST_size_t_eq(1, nbytes)
  2113. || !TEST_int_eq(idx * 2 + 2, new_called)
  2114. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2115. || !TEST_int_eq(idx * 2 + 4, new_called)
  2116. || !TEST_int_eq(sizeof(buf), nbytes)
  2117. || !TEST_int_eq(c, buf[0])
  2118. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2119. goto end;
  2120. /* Try with only requesting one new ticket, too */
  2121. c = '2';
  2122. new_called = 0;
  2123. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2124. || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
  2125. || !TEST_size_t_eq(sizeof(c), nbytes)
  2126. || !TEST_int_eq(1, new_called)
  2127. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2128. || !TEST_int_eq(2, new_called)
  2129. || !TEST_size_t_eq(sizeof(buf), nbytes)
  2130. || !TEST_int_eq(c, buf[0]))
  2131. goto end;
  2132. /* Do it again but use dummy writes to drive the ticket generation */
  2133. c = '3';
  2134. new_called = 0;
  2135. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2136. || !TEST_true(SSL_new_session_ticket(serverssl))
  2137. || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
  2138. || !TEST_size_t_eq(0, nbytes)
  2139. || !TEST_int_eq(2, new_called)
  2140. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2141. || !TEST_int_eq(4, new_called))
  2142. goto end;
  2143. /*
  2144. * Use the always-retry BIO to exercise the logic that forces ticket
  2145. * generation to wait until a record boundary.
  2146. */
  2147. c = '4';
  2148. new_called = 0;
  2149. tmp = SSL_get_wbio(serverssl);
  2150. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  2151. tmp = NULL;
  2152. goto end;
  2153. }
  2154. SSL_set0_wbio(serverssl, bretry);
  2155. bretry = NULL;
  2156. if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2157. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
  2158. || !TEST_size_t_eq(nbytes, 0))
  2159. goto end;
  2160. /* Restore a BIO that will let the write succeed */
  2161. SSL_set0_wbio(serverssl, tmp);
  2162. tmp = NULL;
  2163. /* These calls should just queue the request and not send anything. */
  2164. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2165. || !TEST_true(SSL_new_session_ticket(serverssl))
  2166. || !TEST_int_eq(0, new_called))
  2167. goto end;
  2168. /* Re-do the write; still no tickets sent */
  2169. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2170. || !TEST_size_t_eq(1, nbytes)
  2171. || !TEST_int_eq(0, new_called)
  2172. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2173. || !TEST_int_eq(0, new_called)
  2174. || !TEST_int_eq(sizeof(buf), nbytes)
  2175. || !TEST_int_eq(c, buf[0])
  2176. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2177. goto end;
  2178. /* Now the *next* write should send the tickets */
  2179. c = '5';
  2180. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2181. || !TEST_size_t_eq(1, nbytes)
  2182. || !TEST_int_eq(2, new_called)
  2183. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2184. || !TEST_int_eq(4, new_called)
  2185. || !TEST_int_eq(sizeof(buf), nbytes)
  2186. || !TEST_int_eq(c, buf[0])
  2187. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2188. goto end;
  2189. SSL_shutdown(clientssl);
  2190. SSL_shutdown(serverssl);
  2191. testresult = 1;
  2192. end:
  2193. BIO_free(bretry);
  2194. BIO_free(tmp);
  2195. SSL_free(serverssl);
  2196. SSL_free(clientssl);
  2197. SSL_CTX_free(sctx);
  2198. SSL_CTX_free(cctx);
  2199. clientssl = serverssl = NULL;
  2200. sctx = cctx = NULL;
  2201. return testresult;
  2202. }
  2203. #endif
  2204. #define USE_NULL 0
  2205. #define USE_BIO_1 1
  2206. #define USE_BIO_2 2
  2207. #define USE_DEFAULT 3
  2208. #define CONNTYPE_CONNECTION_SUCCESS 0
  2209. #define CONNTYPE_CONNECTION_FAIL 1
  2210. #define CONNTYPE_NO_CONNECTION 2
  2211. #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
  2212. #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
  2213. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
  2214. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
  2215. #else
  2216. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
  2217. #endif
  2218. #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
  2219. + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
  2220. + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
  2221. static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
  2222. {
  2223. switch (type) {
  2224. case USE_NULL:
  2225. *res = NULL;
  2226. break;
  2227. case USE_BIO_1:
  2228. *res = bio1;
  2229. break;
  2230. case USE_BIO_2:
  2231. *res = bio2;
  2232. break;
  2233. }
  2234. }
  2235. /*
  2236. * Tests calls to SSL_set_bio() under various conditions.
  2237. *
  2238. * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
  2239. * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
  2240. * then do more tests where we create a successful connection first using our
  2241. * standard connection setup functions, and then call SSL_set_bio() with
  2242. * various combinations of valid BIOs or NULL. We then repeat these tests
  2243. * following a failed connection. In this last case we are looking to check that
  2244. * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
  2245. */
  2246. static int test_ssl_set_bio(int idx)
  2247. {
  2248. SSL_CTX *sctx = NULL, *cctx = NULL;
  2249. BIO *bio1 = NULL;
  2250. BIO *bio2 = NULL;
  2251. BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
  2252. SSL *serverssl = NULL, *clientssl = NULL;
  2253. int initrbio, initwbio, newrbio, newwbio, conntype;
  2254. int testresult = 0;
  2255. if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
  2256. initrbio = idx % 3;
  2257. idx /= 3;
  2258. initwbio = idx % 3;
  2259. idx /= 3;
  2260. newrbio = idx % 3;
  2261. idx /= 3;
  2262. newwbio = idx % 3;
  2263. conntype = CONNTYPE_NO_CONNECTION;
  2264. } else {
  2265. idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
  2266. initrbio = initwbio = USE_DEFAULT;
  2267. newrbio = idx % 2;
  2268. idx /= 2;
  2269. newwbio = idx % 2;
  2270. idx /= 2;
  2271. conntype = idx % 2;
  2272. }
  2273. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2274. TLS_client_method(), TLS1_VERSION, 0,
  2275. &sctx, &cctx, cert, privkey)))
  2276. goto end;
  2277. if (conntype == CONNTYPE_CONNECTION_FAIL) {
  2278. /*
  2279. * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
  2280. * because we reduced the number of tests in the definition of
  2281. * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
  2282. * mismatched protocol versions we will force a connection failure.
  2283. */
  2284. SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
  2285. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2286. }
  2287. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2288. NULL, NULL)))
  2289. goto end;
  2290. if (initrbio == USE_BIO_1
  2291. || initwbio == USE_BIO_1
  2292. || newrbio == USE_BIO_1
  2293. || newwbio == USE_BIO_1) {
  2294. if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
  2295. goto end;
  2296. }
  2297. if (initrbio == USE_BIO_2
  2298. || initwbio == USE_BIO_2
  2299. || newrbio == USE_BIO_2
  2300. || newwbio == USE_BIO_2) {
  2301. if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
  2302. goto end;
  2303. }
  2304. if (initrbio != USE_DEFAULT) {
  2305. setupbio(&irbio, bio1, bio2, initrbio);
  2306. setupbio(&iwbio, bio1, bio2, initwbio);
  2307. SSL_set_bio(clientssl, irbio, iwbio);
  2308. /*
  2309. * We want to maintain our own refs to these BIO, so do an up ref for
  2310. * each BIO that will have ownership transferred in the SSL_set_bio()
  2311. * call
  2312. */
  2313. if (irbio != NULL)
  2314. BIO_up_ref(irbio);
  2315. if (iwbio != NULL && iwbio != irbio)
  2316. BIO_up_ref(iwbio);
  2317. }
  2318. if (conntype != CONNTYPE_NO_CONNECTION
  2319. && !TEST_true(create_ssl_connection(serverssl, clientssl,
  2320. SSL_ERROR_NONE)
  2321. == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
  2322. goto end;
  2323. setupbio(&nrbio, bio1, bio2, newrbio);
  2324. setupbio(&nwbio, bio1, bio2, newwbio);
  2325. /*
  2326. * We will (maybe) transfer ownership again so do more up refs.
  2327. * SSL_set_bio() has some really complicated ownership rules where BIOs have
  2328. * already been set!
  2329. */
  2330. if (nrbio != NULL
  2331. && nrbio != irbio
  2332. && (nwbio != iwbio || nrbio != nwbio))
  2333. BIO_up_ref(nrbio);
  2334. if (nwbio != NULL
  2335. && nwbio != nrbio
  2336. && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
  2337. BIO_up_ref(nwbio);
  2338. SSL_set_bio(clientssl, nrbio, nwbio);
  2339. testresult = 1;
  2340. end:
  2341. BIO_free(bio1);
  2342. BIO_free(bio2);
  2343. /*
  2344. * This test is checking that the ref counting for SSL_set_bio is correct.
  2345. * If we get here and we did too many frees then we will fail in the above
  2346. * functions.
  2347. */
  2348. SSL_free(serverssl);
  2349. SSL_free(clientssl);
  2350. SSL_CTX_free(sctx);
  2351. SSL_CTX_free(cctx);
  2352. return testresult;
  2353. }
  2354. typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
  2355. static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
  2356. {
  2357. BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
  2358. SSL_CTX *ctx;
  2359. SSL *ssl = NULL;
  2360. int testresult = 0;
  2361. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  2362. || !TEST_ptr(ssl = SSL_new(ctx))
  2363. || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
  2364. || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
  2365. goto end;
  2366. BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
  2367. /*
  2368. * If anything goes wrong here then we could leak memory.
  2369. */
  2370. BIO_push(sslbio, membio1);
  2371. /* Verify changing the rbio/wbio directly does not cause leaks */
  2372. if (change_bio != NO_BIO_CHANGE) {
  2373. if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
  2374. ssl = NULL;
  2375. goto end;
  2376. }
  2377. if (change_bio == CHANGE_RBIO)
  2378. SSL_set0_rbio(ssl, membio2);
  2379. else
  2380. SSL_set0_wbio(ssl, membio2);
  2381. }
  2382. ssl = NULL;
  2383. if (pop_ssl)
  2384. BIO_pop(sslbio);
  2385. else
  2386. BIO_pop(membio1);
  2387. testresult = 1;
  2388. end:
  2389. BIO_free(membio1);
  2390. BIO_free(sslbio);
  2391. SSL_free(ssl);
  2392. SSL_CTX_free(ctx);
  2393. return testresult;
  2394. }
  2395. static int test_ssl_bio_pop_next_bio(void)
  2396. {
  2397. return execute_test_ssl_bio(0, NO_BIO_CHANGE);
  2398. }
  2399. static int test_ssl_bio_pop_ssl_bio(void)
  2400. {
  2401. return execute_test_ssl_bio(1, NO_BIO_CHANGE);
  2402. }
  2403. static int test_ssl_bio_change_rbio(void)
  2404. {
  2405. return execute_test_ssl_bio(0, CHANGE_RBIO);
  2406. }
  2407. static int test_ssl_bio_change_wbio(void)
  2408. {
  2409. return execute_test_ssl_bio(0, CHANGE_WBIO);
  2410. }
  2411. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  2412. typedef struct {
  2413. /* The list of sig algs */
  2414. const int *list;
  2415. /* The length of the list */
  2416. size_t listlen;
  2417. /* A sigalgs list in string format */
  2418. const char *liststr;
  2419. /* Whether setting the list should succeed */
  2420. int valid;
  2421. /* Whether creating a connection with the list should succeed */
  2422. int connsuccess;
  2423. } sigalgs_list;
  2424. static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
  2425. # ifndef OPENSSL_NO_EC
  2426. static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
  2427. static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
  2428. # endif
  2429. static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
  2430. static const int invalidlist2[] = {NID_sha256, NID_undef};
  2431. static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
  2432. static const int invalidlist4[] = {NID_sha256};
  2433. static const sigalgs_list testsigalgs[] = {
  2434. {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
  2435. # ifndef OPENSSL_NO_EC
  2436. {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
  2437. {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
  2438. # endif
  2439. {NULL, 0, "RSA+SHA256", 1, 1},
  2440. # ifndef OPENSSL_NO_EC
  2441. {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
  2442. {NULL, 0, "ECDSA+SHA512", 1, 0},
  2443. # endif
  2444. {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
  2445. {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
  2446. {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
  2447. {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
  2448. {NULL, 0, "RSA", 0, 0},
  2449. {NULL, 0, "SHA256", 0, 0},
  2450. {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
  2451. {NULL, 0, "Invalid", 0, 0}
  2452. };
  2453. static int test_set_sigalgs(int idx)
  2454. {
  2455. SSL_CTX *cctx = NULL, *sctx = NULL;
  2456. SSL *clientssl = NULL, *serverssl = NULL;
  2457. int testresult = 0;
  2458. const sigalgs_list *curr;
  2459. int testctx;
  2460. /* Should never happen */
  2461. if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
  2462. return 0;
  2463. testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
  2464. curr = testctx ? &testsigalgs[idx]
  2465. : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
  2466. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2467. TLS_client_method(), TLS1_VERSION, 0,
  2468. &sctx, &cctx, cert, privkey)))
  2469. return 0;
  2470. /*
  2471. * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
  2472. * for TLSv1.2 for now until we add a new API.
  2473. */
  2474. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2475. if (testctx) {
  2476. int ret;
  2477. if (curr->list != NULL)
  2478. ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
  2479. else
  2480. ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
  2481. if (!ret) {
  2482. if (curr->valid)
  2483. TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
  2484. else
  2485. testresult = 1;
  2486. goto end;
  2487. }
  2488. if (!curr->valid) {
  2489. TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
  2490. goto end;
  2491. }
  2492. }
  2493. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2494. &clientssl, NULL, NULL)))
  2495. goto end;
  2496. if (!testctx) {
  2497. int ret;
  2498. if (curr->list != NULL)
  2499. ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
  2500. else
  2501. ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
  2502. if (!ret) {
  2503. if (curr->valid)
  2504. TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
  2505. else
  2506. testresult = 1;
  2507. goto end;
  2508. }
  2509. if (!curr->valid)
  2510. goto end;
  2511. }
  2512. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  2513. SSL_ERROR_NONE),
  2514. curr->connsuccess))
  2515. goto end;
  2516. testresult = 1;
  2517. end:
  2518. SSL_free(serverssl);
  2519. SSL_free(clientssl);
  2520. SSL_CTX_free(sctx);
  2521. SSL_CTX_free(cctx);
  2522. return testresult;
  2523. }
  2524. #endif
  2525. #ifndef OSSL_NO_USABLE_TLS1_3
  2526. static int psk_client_cb_cnt = 0;
  2527. static int psk_server_cb_cnt = 0;
  2528. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  2529. size_t *idlen, SSL_SESSION **sess)
  2530. {
  2531. switch (++use_session_cb_cnt) {
  2532. case 1:
  2533. /* The first call should always have a NULL md */
  2534. if (md != NULL)
  2535. return 0;
  2536. break;
  2537. case 2:
  2538. /* The second call should always have an md */
  2539. if (md == NULL)
  2540. return 0;
  2541. break;
  2542. default:
  2543. /* We should only be called a maximum of twice */
  2544. return 0;
  2545. }
  2546. if (clientpsk != NULL)
  2547. SSL_SESSION_up_ref(clientpsk);
  2548. *sess = clientpsk;
  2549. *id = (const unsigned char *)pskid;
  2550. *idlen = strlen(pskid);
  2551. return 1;
  2552. }
  2553. #ifndef OPENSSL_NO_PSK
  2554. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
  2555. unsigned int max_id_len,
  2556. unsigned char *psk,
  2557. unsigned int max_psk_len)
  2558. {
  2559. unsigned int psklen = 0;
  2560. psk_client_cb_cnt++;
  2561. if (strlen(pskid) + 1 > max_id_len)
  2562. return 0;
  2563. /* We should only ever be called a maximum of twice per connection */
  2564. if (psk_client_cb_cnt > 2)
  2565. return 0;
  2566. if (clientpsk == NULL)
  2567. return 0;
  2568. /* We'll reuse the PSK we set up for TLSv1.3 */
  2569. if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
  2570. return 0;
  2571. psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
  2572. strncpy(id, pskid, max_id_len);
  2573. return psklen;
  2574. }
  2575. #endif /* OPENSSL_NO_PSK */
  2576. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  2577. size_t identity_len, SSL_SESSION **sess)
  2578. {
  2579. find_session_cb_cnt++;
  2580. /* We should only ever be called a maximum of twice per connection */
  2581. if (find_session_cb_cnt > 2)
  2582. return 0;
  2583. if (serverpsk == NULL)
  2584. return 0;
  2585. /* Identity should match that set by the client */
  2586. if (strlen(srvid) != identity_len
  2587. || strncmp(srvid, (const char *)identity, identity_len) != 0) {
  2588. /* No PSK found, continue but without a PSK */
  2589. *sess = NULL;
  2590. return 1;
  2591. }
  2592. SSL_SESSION_up_ref(serverpsk);
  2593. *sess = serverpsk;
  2594. return 1;
  2595. }
  2596. #ifndef OPENSSL_NO_PSK
  2597. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  2598. unsigned char *psk, unsigned int max_psk_len)
  2599. {
  2600. unsigned int psklen = 0;
  2601. psk_server_cb_cnt++;
  2602. /* We should only ever be called a maximum of twice per connection */
  2603. if (find_session_cb_cnt > 2)
  2604. return 0;
  2605. if (serverpsk == NULL)
  2606. return 0;
  2607. /* Identity should match that set by the client */
  2608. if (strcmp(srvid, identity) != 0) {
  2609. return 0;
  2610. }
  2611. /* We'll reuse the PSK we set up for TLSv1.3 */
  2612. if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
  2613. return 0;
  2614. psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
  2615. return psklen;
  2616. }
  2617. #endif /* OPENSSL_NO_PSK */
  2618. #define MSG1 "Hello"
  2619. #define MSG2 "World."
  2620. #define MSG3 "This"
  2621. #define MSG4 "is"
  2622. #define MSG5 "a"
  2623. #define MSG6 "test"
  2624. #define MSG7 "message."
  2625. #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
  2626. #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
  2627. #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
  2628. #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
  2629. #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
  2630. static SSL_SESSION *create_a_psk(SSL *ssl)
  2631. {
  2632. const SSL_CIPHER *cipher = NULL;
  2633. const unsigned char key[] = {
  2634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  2635. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  2636. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  2637. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  2638. 0x2c, 0x2d, 0x2e, 0x2f
  2639. };
  2640. SSL_SESSION *sess = NULL;
  2641. cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
  2642. sess = SSL_SESSION_new();
  2643. if (!TEST_ptr(sess)
  2644. || !TEST_ptr(cipher)
  2645. || !TEST_true(SSL_SESSION_set1_master_key(sess, key,
  2646. sizeof(key)))
  2647. || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
  2648. || !TEST_true(
  2649. SSL_SESSION_set_protocol_version(sess,
  2650. TLS1_3_VERSION))) {
  2651. SSL_SESSION_free(sess);
  2652. return NULL;
  2653. }
  2654. return sess;
  2655. }
  2656. /*
  2657. * Helper method to setup objects for early data test. Caller frees objects on
  2658. * error.
  2659. */
  2660. static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
  2661. SSL **serverssl, SSL_SESSION **sess, int idx)
  2662. {
  2663. if (*sctx == NULL
  2664. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2665. TLS_client_method(),
  2666. TLS1_VERSION, 0,
  2667. sctx, cctx, cert, privkey)))
  2668. return 0;
  2669. if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
  2670. return 0;
  2671. if (idx == 1) {
  2672. /* When idx == 1 we repeat the tests with read_ahead set */
  2673. SSL_CTX_set_read_ahead(*cctx, 1);
  2674. SSL_CTX_set_read_ahead(*sctx, 1);
  2675. } else if (idx == 2) {
  2676. /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
  2677. SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
  2678. SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
  2679. use_session_cb_cnt = 0;
  2680. find_session_cb_cnt = 0;
  2681. srvid = pskid;
  2682. }
  2683. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
  2684. NULL, NULL)))
  2685. return 0;
  2686. /*
  2687. * For one of the run throughs (doesn't matter which one), we'll try sending
  2688. * some SNI data in the initial ClientHello. This will be ignored (because
  2689. * there is no SNI cb set up by the server), so it should not impact
  2690. * early_data.
  2691. */
  2692. if (idx == 1
  2693. && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
  2694. return 0;
  2695. if (idx == 2) {
  2696. clientpsk = create_a_psk(*clientssl);
  2697. if (!TEST_ptr(clientpsk)
  2698. /*
  2699. * We just choose an arbitrary value for max_early_data which
  2700. * should be big enough for testing purposes.
  2701. */
  2702. || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
  2703. 0x100))
  2704. || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2705. SSL_SESSION_free(clientpsk);
  2706. clientpsk = NULL;
  2707. return 0;
  2708. }
  2709. serverpsk = clientpsk;
  2710. if (sess != NULL) {
  2711. if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2712. SSL_SESSION_free(clientpsk);
  2713. SSL_SESSION_free(serverpsk);
  2714. clientpsk = serverpsk = NULL;
  2715. return 0;
  2716. }
  2717. *sess = clientpsk;
  2718. }
  2719. return 1;
  2720. }
  2721. if (sess == NULL)
  2722. return 1;
  2723. if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
  2724. SSL_ERROR_NONE)))
  2725. return 0;
  2726. *sess = SSL_get1_session(*clientssl);
  2727. SSL_shutdown(*clientssl);
  2728. SSL_shutdown(*serverssl);
  2729. SSL_free(*serverssl);
  2730. SSL_free(*clientssl);
  2731. *serverssl = *clientssl = NULL;
  2732. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
  2733. clientssl, NULL, NULL))
  2734. || !TEST_true(SSL_set_session(*clientssl, *sess)))
  2735. return 0;
  2736. return 1;
  2737. }
  2738. static int test_early_data_read_write(int idx)
  2739. {
  2740. SSL_CTX *cctx = NULL, *sctx = NULL;
  2741. SSL *clientssl = NULL, *serverssl = NULL;
  2742. int testresult = 0;
  2743. SSL_SESSION *sess = NULL;
  2744. unsigned char buf[20], data[1024];
  2745. size_t readbytes, written, eoedlen, rawread, rawwritten;
  2746. BIO *rbio;
  2747. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2748. &serverssl, &sess, idx)))
  2749. goto end;
  2750. /* Write and read some early data */
  2751. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2752. &written))
  2753. || !TEST_size_t_eq(written, strlen(MSG1))
  2754. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  2755. sizeof(buf), &readbytes),
  2756. SSL_READ_EARLY_DATA_SUCCESS)
  2757. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  2758. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2759. SSL_EARLY_DATA_ACCEPTED))
  2760. goto end;
  2761. /*
  2762. * Server should be able to write data, and client should be able to
  2763. * read it.
  2764. */
  2765. if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
  2766. &written))
  2767. || !TEST_size_t_eq(written, strlen(MSG2))
  2768. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2769. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2770. goto end;
  2771. /* Even after reading normal data, client should be able write early data */
  2772. if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
  2773. &written))
  2774. || !TEST_size_t_eq(written, strlen(MSG3)))
  2775. goto end;
  2776. /* Server should still be able read early data after writing data */
  2777. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2778. &readbytes),
  2779. SSL_READ_EARLY_DATA_SUCCESS)
  2780. || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
  2781. goto end;
  2782. /* Write more data from server and read it from client */
  2783. if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
  2784. &written))
  2785. || !TEST_size_t_eq(written, strlen(MSG4))
  2786. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2787. || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
  2788. goto end;
  2789. /*
  2790. * If client writes normal data it should mean writing early data is no
  2791. * longer possible.
  2792. */
  2793. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  2794. || !TEST_size_t_eq(written, strlen(MSG5))
  2795. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2796. SSL_EARLY_DATA_ACCEPTED))
  2797. goto end;
  2798. /*
  2799. * At this point the client has written EndOfEarlyData, ClientFinished and
  2800. * normal (fully protected) data. We are going to cause a delay between the
  2801. * arrival of EndOfEarlyData and ClientFinished. We read out all the data
  2802. * in the read BIO, and then just put back the EndOfEarlyData message.
  2803. */
  2804. rbio = SSL_get_rbio(serverssl);
  2805. if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
  2806. || !TEST_size_t_lt(rawread, sizeof(data))
  2807. || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
  2808. goto end;
  2809. /* Record length is in the 4th and 5th bytes of the record header */
  2810. eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
  2811. if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
  2812. || !TEST_size_t_eq(rawwritten, eoedlen))
  2813. goto end;
  2814. /* Server should be told that there is no more early data */
  2815. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2816. &readbytes),
  2817. SSL_READ_EARLY_DATA_FINISH)
  2818. || !TEST_size_t_eq(readbytes, 0))
  2819. goto end;
  2820. /*
  2821. * Server has not finished init yet, so should still be able to write early
  2822. * data.
  2823. */
  2824. if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
  2825. &written))
  2826. || !TEST_size_t_eq(written, strlen(MSG6)))
  2827. goto end;
  2828. /* Push the ClientFinished and the normal data back into the server rbio */
  2829. if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
  2830. &rawwritten))
  2831. || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
  2832. goto end;
  2833. /* Server should be able to read normal data */
  2834. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2835. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  2836. goto end;
  2837. /* Client and server should not be able to write/read early data now */
  2838. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  2839. &written)))
  2840. goto end;
  2841. ERR_clear_error();
  2842. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2843. &readbytes),
  2844. SSL_READ_EARLY_DATA_ERROR))
  2845. goto end;
  2846. ERR_clear_error();
  2847. /* Client should be able to read the data sent by the server */
  2848. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2849. || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
  2850. goto end;
  2851. /*
  2852. * Make sure we process the two NewSessionTickets. These arrive
  2853. * post-handshake. We attempt reads which we do not expect to return any
  2854. * data.
  2855. */
  2856. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2857. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
  2858. &readbytes)))
  2859. goto end;
  2860. /* Server should be able to write normal data */
  2861. if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
  2862. || !TEST_size_t_eq(written, strlen(MSG7))
  2863. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2864. || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
  2865. goto end;
  2866. SSL_SESSION_free(sess);
  2867. sess = SSL_get1_session(clientssl);
  2868. use_session_cb_cnt = 0;
  2869. find_session_cb_cnt = 0;
  2870. SSL_shutdown(clientssl);
  2871. SSL_shutdown(serverssl);
  2872. SSL_free(serverssl);
  2873. SSL_free(clientssl);
  2874. serverssl = clientssl = NULL;
  2875. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2876. &clientssl, NULL, NULL))
  2877. || !TEST_true(SSL_set_session(clientssl, sess)))
  2878. goto end;
  2879. /* Write and read some early data */
  2880. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2881. &written))
  2882. || !TEST_size_t_eq(written, strlen(MSG1))
  2883. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2884. &readbytes),
  2885. SSL_READ_EARLY_DATA_SUCCESS)
  2886. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  2887. goto end;
  2888. if (!TEST_int_gt(SSL_connect(clientssl), 0)
  2889. || !TEST_int_gt(SSL_accept(serverssl), 0))
  2890. goto end;
  2891. /* Client and server should not be able to write/read early data now */
  2892. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  2893. &written)))
  2894. goto end;
  2895. ERR_clear_error();
  2896. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2897. &readbytes),
  2898. SSL_READ_EARLY_DATA_ERROR))
  2899. goto end;
  2900. ERR_clear_error();
  2901. /* Client and server should be able to write/read normal data */
  2902. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  2903. || !TEST_size_t_eq(written, strlen(MSG5))
  2904. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2905. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  2906. goto end;
  2907. testresult = 1;
  2908. end:
  2909. SSL_SESSION_free(sess);
  2910. SSL_SESSION_free(clientpsk);
  2911. SSL_SESSION_free(serverpsk);
  2912. clientpsk = serverpsk = NULL;
  2913. SSL_free(serverssl);
  2914. SSL_free(clientssl);
  2915. SSL_CTX_free(sctx);
  2916. SSL_CTX_free(cctx);
  2917. return testresult;
  2918. }
  2919. static int allow_ed_cb_called = 0;
  2920. static int allow_early_data_cb(SSL *s, void *arg)
  2921. {
  2922. int *usecb = (int *)arg;
  2923. allow_ed_cb_called++;
  2924. if (*usecb == 1)
  2925. return 0;
  2926. return 1;
  2927. }
  2928. /*
  2929. * idx == 0: Standard early_data setup
  2930. * idx == 1: early_data setup using read_ahead
  2931. * usecb == 0: Don't use a custom early data callback
  2932. * usecb == 1: Use a custom early data callback and reject the early data
  2933. * usecb == 2: Use a custom early data callback and accept the early data
  2934. * confopt == 0: Configure anti-replay directly
  2935. * confopt == 1: Configure anti-replay using SSL_CONF
  2936. */
  2937. static int test_early_data_replay_int(int idx, int usecb, int confopt)
  2938. {
  2939. SSL_CTX *cctx = NULL, *sctx = NULL;
  2940. SSL *clientssl = NULL, *serverssl = NULL;
  2941. int testresult = 0;
  2942. SSL_SESSION *sess = NULL;
  2943. size_t readbytes, written;
  2944. unsigned char buf[20];
  2945. allow_ed_cb_called = 0;
  2946. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2947. TLS_client_method(), TLS1_VERSION, 0,
  2948. &sctx, &cctx, cert, privkey)))
  2949. return 0;
  2950. if (usecb > 0) {
  2951. if (confopt == 0) {
  2952. SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
  2953. } else {
  2954. SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
  2955. if (!TEST_ptr(confctx))
  2956. goto end;
  2957. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  2958. | SSL_CONF_FLAG_SERVER);
  2959. SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
  2960. if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
  2961. 2)) {
  2962. SSL_CONF_CTX_free(confctx);
  2963. goto end;
  2964. }
  2965. SSL_CONF_CTX_free(confctx);
  2966. }
  2967. SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
  2968. }
  2969. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2970. &serverssl, &sess, idx)))
  2971. goto end;
  2972. /*
  2973. * The server is configured to accept early data. Create a connection to
  2974. * "use up" the ticket
  2975. */
  2976. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  2977. || !TEST_true(SSL_session_reused(clientssl)))
  2978. goto end;
  2979. SSL_shutdown(clientssl);
  2980. SSL_shutdown(serverssl);
  2981. SSL_free(serverssl);
  2982. SSL_free(clientssl);
  2983. serverssl = clientssl = NULL;
  2984. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2985. &clientssl, NULL, NULL))
  2986. || !TEST_true(SSL_set_session(clientssl, sess)))
  2987. goto end;
  2988. /* Write and read some early data */
  2989. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2990. &written))
  2991. || !TEST_size_t_eq(written, strlen(MSG1)))
  2992. goto end;
  2993. if (usecb <= 1) {
  2994. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2995. &readbytes),
  2996. SSL_READ_EARLY_DATA_FINISH)
  2997. /*
  2998. * The ticket was reused, so the we should have rejected the
  2999. * early data
  3000. */
  3001. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3002. SSL_EARLY_DATA_REJECTED))
  3003. goto end;
  3004. } else {
  3005. /* In this case the callback decides to accept the early data */
  3006. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3007. &readbytes),
  3008. SSL_READ_EARLY_DATA_SUCCESS)
  3009. || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
  3010. /*
  3011. * Server will have sent its flight so client can now send
  3012. * end of early data and complete its half of the handshake
  3013. */
  3014. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3015. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3016. &readbytes),
  3017. SSL_READ_EARLY_DATA_FINISH)
  3018. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3019. SSL_EARLY_DATA_ACCEPTED))
  3020. goto end;
  3021. }
  3022. /* Complete the connection */
  3023. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3024. || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
  3025. || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
  3026. goto end;
  3027. testresult = 1;
  3028. end:
  3029. SSL_SESSION_free(sess);
  3030. SSL_SESSION_free(clientpsk);
  3031. SSL_SESSION_free(serverpsk);
  3032. clientpsk = serverpsk = NULL;
  3033. SSL_free(serverssl);
  3034. SSL_free(clientssl);
  3035. SSL_CTX_free(sctx);
  3036. SSL_CTX_free(cctx);
  3037. return testresult;
  3038. }
  3039. static int test_early_data_replay(int idx)
  3040. {
  3041. int ret = 1, usecb, confopt;
  3042. for (usecb = 0; usecb < 3; usecb++) {
  3043. for (confopt = 0; confopt < 2; confopt++)
  3044. ret &= test_early_data_replay_int(idx, usecb, confopt);
  3045. }
  3046. return ret;
  3047. }
  3048. /*
  3049. * Helper function to test that a server attempting to read early data can
  3050. * handle a connection from a client where the early data should be skipped.
  3051. * testtype: 0 == No HRR
  3052. * testtype: 1 == HRR
  3053. * testtype: 2 == HRR, invalid early_data sent after HRR
  3054. * testtype: 3 == recv_max_early_data set to 0
  3055. */
  3056. static int early_data_skip_helper(int testtype, int idx)
  3057. {
  3058. SSL_CTX *cctx = NULL, *sctx = NULL;
  3059. SSL *clientssl = NULL, *serverssl = NULL;
  3060. int testresult = 0;
  3061. SSL_SESSION *sess = NULL;
  3062. unsigned char buf[20];
  3063. size_t readbytes, written;
  3064. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3065. &serverssl, &sess, idx)))
  3066. goto end;
  3067. if (testtype == 1 || testtype == 2) {
  3068. /* Force an HRR to occur */
  3069. #if defined(OPENSSL_NO_EC)
  3070. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  3071. goto end;
  3072. #else
  3073. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
  3074. goto end;
  3075. #endif
  3076. } else if (idx == 2) {
  3077. /*
  3078. * We force early_data rejection by ensuring the PSK identity is
  3079. * unrecognised
  3080. */
  3081. srvid = "Dummy Identity";
  3082. } else {
  3083. /*
  3084. * Deliberately corrupt the creation time. We take 20 seconds off the
  3085. * time. It could be any value as long as it is not within tolerance.
  3086. * This should mean the ticket is rejected.
  3087. */
  3088. if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
  3089. goto end;
  3090. }
  3091. if (testtype == 3
  3092. && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
  3093. goto end;
  3094. /* Write some early data */
  3095. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3096. &written))
  3097. || !TEST_size_t_eq(written, strlen(MSG1)))
  3098. goto end;
  3099. /* Server should reject the early data */
  3100. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3101. &readbytes),
  3102. SSL_READ_EARLY_DATA_FINISH)
  3103. || !TEST_size_t_eq(readbytes, 0)
  3104. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3105. SSL_EARLY_DATA_REJECTED))
  3106. goto end;
  3107. switch (testtype) {
  3108. case 0:
  3109. /* Nothing to do */
  3110. break;
  3111. case 1:
  3112. /*
  3113. * Finish off the handshake. We perform the same writes and reads as
  3114. * further down but we expect them to fail due to the incomplete
  3115. * handshake.
  3116. */
  3117. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3118. || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
  3119. &readbytes)))
  3120. goto end;
  3121. break;
  3122. case 2:
  3123. {
  3124. BIO *wbio = SSL_get_wbio(clientssl);
  3125. /* A record that will appear as bad early_data */
  3126. const unsigned char bad_early_data[] = {
  3127. 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
  3128. };
  3129. /*
  3130. * We force the client to attempt a write. This will fail because
  3131. * we're still in the handshake. It will cause the second
  3132. * ClientHello to be sent.
  3133. */
  3134. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
  3135. &written)))
  3136. goto end;
  3137. /*
  3138. * Inject some early_data after the second ClientHello. This should
  3139. * cause the server to fail
  3140. */
  3141. if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
  3142. sizeof(bad_early_data), &written)))
  3143. goto end;
  3144. }
  3145. /* fallthrough */
  3146. case 3:
  3147. /*
  3148. * This client has sent more early_data than we are willing to skip
  3149. * (case 3) or sent invalid early_data (case 2) so the connection should
  3150. * abort.
  3151. */
  3152. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3153. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
  3154. goto end;
  3155. /* Connection has failed - nothing more to do */
  3156. testresult = 1;
  3157. goto end;
  3158. default:
  3159. TEST_error("Invalid test type");
  3160. goto end;
  3161. }
  3162. /*
  3163. * Should be able to send normal data despite rejection of early data. The
  3164. * early_data should be skipped.
  3165. */
  3166. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3167. || !TEST_size_t_eq(written, strlen(MSG2))
  3168. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3169. SSL_EARLY_DATA_REJECTED)
  3170. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3171. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3172. goto end;
  3173. testresult = 1;
  3174. end:
  3175. SSL_SESSION_free(clientpsk);
  3176. SSL_SESSION_free(serverpsk);
  3177. clientpsk = serverpsk = NULL;
  3178. SSL_SESSION_free(sess);
  3179. SSL_free(serverssl);
  3180. SSL_free(clientssl);
  3181. SSL_CTX_free(sctx);
  3182. SSL_CTX_free(cctx);
  3183. return testresult;
  3184. }
  3185. /*
  3186. * Test that a server attempting to read early data can handle a connection
  3187. * from a client where the early data is not acceptable.
  3188. */
  3189. static int test_early_data_skip(int idx)
  3190. {
  3191. return early_data_skip_helper(0, idx);
  3192. }
  3193. /*
  3194. * Test that a server attempting to read early data can handle a connection
  3195. * from a client where an HRR occurs.
  3196. */
  3197. static int test_early_data_skip_hrr(int idx)
  3198. {
  3199. return early_data_skip_helper(1, idx);
  3200. }
  3201. /*
  3202. * Test that a server attempting to read early data can handle a connection
  3203. * from a client where an HRR occurs and correctly fails if early_data is sent
  3204. * after the HRR
  3205. */
  3206. static int test_early_data_skip_hrr_fail(int idx)
  3207. {
  3208. return early_data_skip_helper(2, idx);
  3209. }
  3210. /*
  3211. * Test that a server attempting to read early data will abort if it tries to
  3212. * skip over too much.
  3213. */
  3214. static int test_early_data_skip_abort(int idx)
  3215. {
  3216. return early_data_skip_helper(3, idx);
  3217. }
  3218. /*
  3219. * Test that a server attempting to read early data can handle a connection
  3220. * from a client that doesn't send any.
  3221. */
  3222. static int test_early_data_not_sent(int idx)
  3223. {
  3224. SSL_CTX *cctx = NULL, *sctx = NULL;
  3225. SSL *clientssl = NULL, *serverssl = NULL;
  3226. int testresult = 0;
  3227. SSL_SESSION *sess = NULL;
  3228. unsigned char buf[20];
  3229. size_t readbytes, written;
  3230. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3231. &serverssl, &sess, idx)))
  3232. goto end;
  3233. /* Write some data - should block due to handshake with server */
  3234. SSL_set_connect_state(clientssl);
  3235. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3236. goto end;
  3237. /* Server should detect that early data has not been sent */
  3238. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3239. &readbytes),
  3240. SSL_READ_EARLY_DATA_FINISH)
  3241. || !TEST_size_t_eq(readbytes, 0)
  3242. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3243. SSL_EARLY_DATA_NOT_SENT)
  3244. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3245. SSL_EARLY_DATA_NOT_SENT))
  3246. goto end;
  3247. /* Continue writing the message we started earlier */
  3248. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3249. || !TEST_size_t_eq(written, strlen(MSG1))
  3250. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3251. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3252. || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
  3253. || !TEST_size_t_eq(written, strlen(MSG2)))
  3254. goto end;
  3255. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3256. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3257. goto end;
  3258. testresult = 1;
  3259. end:
  3260. SSL_SESSION_free(sess);
  3261. SSL_SESSION_free(clientpsk);
  3262. SSL_SESSION_free(serverpsk);
  3263. clientpsk = serverpsk = NULL;
  3264. SSL_free(serverssl);
  3265. SSL_free(clientssl);
  3266. SSL_CTX_free(sctx);
  3267. SSL_CTX_free(cctx);
  3268. return testresult;
  3269. }
  3270. static const char *servalpn;
  3271. static int alpn_select_cb(SSL *ssl, const unsigned char **out,
  3272. unsigned char *outlen, const unsigned char *in,
  3273. unsigned int inlen, void *arg)
  3274. {
  3275. unsigned int protlen = 0;
  3276. const unsigned char *prot;
  3277. for (prot = in; prot < in + inlen; prot += protlen) {
  3278. protlen = *prot++;
  3279. if (in + inlen < prot + protlen)
  3280. return SSL_TLSEXT_ERR_NOACK;
  3281. if (protlen == strlen(servalpn)
  3282. && memcmp(prot, servalpn, protlen) == 0) {
  3283. *out = prot;
  3284. *outlen = protlen;
  3285. return SSL_TLSEXT_ERR_OK;
  3286. }
  3287. }
  3288. return SSL_TLSEXT_ERR_NOACK;
  3289. }
  3290. /* Test that a PSK can be used to send early_data */
  3291. static int test_early_data_psk(int idx)
  3292. {
  3293. SSL_CTX *cctx = NULL, *sctx = NULL;
  3294. SSL *clientssl = NULL, *serverssl = NULL;
  3295. int testresult = 0;
  3296. SSL_SESSION *sess = NULL;
  3297. unsigned char alpnlist[] = {
  3298. 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
  3299. 'l', 'p', 'n'
  3300. };
  3301. #define GOODALPNLEN 9
  3302. #define BADALPNLEN 8
  3303. #define GOODALPN (alpnlist)
  3304. #define BADALPN (alpnlist + GOODALPNLEN)
  3305. int err = 0;
  3306. unsigned char buf[20];
  3307. size_t readbytes, written;
  3308. int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
  3309. int edstatus = SSL_EARLY_DATA_ACCEPTED;
  3310. /* We always set this up with a final parameter of "2" for PSK */
  3311. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3312. &serverssl, &sess, 2)))
  3313. goto end;
  3314. servalpn = "goodalpn";
  3315. /*
  3316. * Note: There is no test for inconsistent SNI with late client detection.
  3317. * This is because servers do not acknowledge SNI even if they are using
  3318. * it in a resumption handshake - so it is not actually possible for a
  3319. * client to detect a problem.
  3320. */
  3321. switch (idx) {
  3322. case 0:
  3323. /* Set inconsistent SNI (early client detection) */
  3324. err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
  3325. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3326. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
  3327. goto end;
  3328. break;
  3329. case 1:
  3330. /* Set inconsistent ALPN (early client detection) */
  3331. err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
  3332. /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
  3333. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
  3334. GOODALPNLEN))
  3335. || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
  3336. BADALPNLEN)))
  3337. goto end;
  3338. break;
  3339. case 2:
  3340. /*
  3341. * Set invalid protocol version. Technically this affects PSKs without
  3342. * early_data too, but we test it here because it is similar to the
  3343. * SNI/ALPN consistency tests.
  3344. */
  3345. err = SSL_R_BAD_PSK;
  3346. if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
  3347. goto end;
  3348. break;
  3349. case 3:
  3350. /*
  3351. * Set inconsistent SNI (server side). In this case the connection
  3352. * will succeed and accept early_data. In TLSv1.3 on the server side SNI
  3353. * is associated with each handshake - not the session. Therefore it
  3354. * should not matter that we used a different server name last time.
  3355. */
  3356. SSL_SESSION_free(serverpsk);
  3357. serverpsk = SSL_SESSION_dup(clientpsk);
  3358. if (!TEST_ptr(serverpsk)
  3359. || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
  3360. goto end;
  3361. /* Fall through */
  3362. case 4:
  3363. /* Set consistent SNI */
  3364. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3365. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
  3366. || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  3367. hostname_cb)))
  3368. goto end;
  3369. break;
  3370. case 5:
  3371. /*
  3372. * Set inconsistent ALPN (server detected). In this case the connection
  3373. * will succeed but reject early_data.
  3374. */
  3375. servalpn = "badalpn";
  3376. edstatus = SSL_EARLY_DATA_REJECTED;
  3377. readearlyres = SSL_READ_EARLY_DATA_FINISH;
  3378. /* Fall through */
  3379. case 6:
  3380. /*
  3381. * Set consistent ALPN.
  3382. * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
  3383. * accepts a list of protos (each one length prefixed).
  3384. * SSL_set1_alpn_selected accepts a single protocol (not length
  3385. * prefixed)
  3386. */
  3387. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
  3388. GOODALPNLEN - 1))
  3389. || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
  3390. GOODALPNLEN)))
  3391. goto end;
  3392. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3393. break;
  3394. case 7:
  3395. /* Set inconsistent ALPN (late client detection) */
  3396. SSL_SESSION_free(serverpsk);
  3397. serverpsk = SSL_SESSION_dup(clientpsk);
  3398. if (!TEST_ptr(serverpsk)
  3399. || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
  3400. BADALPN + 1,
  3401. BADALPNLEN - 1))
  3402. || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
  3403. GOODALPN + 1,
  3404. GOODALPNLEN - 1))
  3405. || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
  3406. sizeof(alpnlist))))
  3407. goto end;
  3408. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3409. edstatus = SSL_EARLY_DATA_ACCEPTED;
  3410. readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
  3411. /* SSL_connect() call should fail */
  3412. connectres = -1;
  3413. break;
  3414. default:
  3415. TEST_error("Bad test index");
  3416. goto end;
  3417. }
  3418. SSL_set_connect_state(clientssl);
  3419. if (err != 0) {
  3420. if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3421. &written))
  3422. || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
  3423. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
  3424. goto end;
  3425. } else {
  3426. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3427. &written)))
  3428. goto end;
  3429. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3430. &readbytes), readearlyres)
  3431. || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
  3432. && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3433. || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
  3434. || !TEST_int_eq(SSL_connect(clientssl), connectres))
  3435. goto end;
  3436. }
  3437. testresult = 1;
  3438. end:
  3439. SSL_SESSION_free(sess);
  3440. SSL_SESSION_free(clientpsk);
  3441. SSL_SESSION_free(serverpsk);
  3442. clientpsk = serverpsk = NULL;
  3443. SSL_free(serverssl);
  3444. SSL_free(clientssl);
  3445. SSL_CTX_free(sctx);
  3446. SSL_CTX_free(cctx);
  3447. return testresult;
  3448. }
  3449. /*
  3450. * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
  3451. * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
  3452. * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
  3453. * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3454. * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
  3455. * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
  3456. */
  3457. static int test_early_data_psk_with_all_ciphers(int idx)
  3458. {
  3459. SSL_CTX *cctx = NULL, *sctx = NULL;
  3460. SSL *clientssl = NULL, *serverssl = NULL;
  3461. int testresult = 0;
  3462. SSL_SESSION *sess = NULL;
  3463. unsigned char buf[20];
  3464. size_t readbytes, written;
  3465. const SSL_CIPHER *cipher;
  3466. const char *cipher_str[] = {
  3467. TLS1_3_RFC_AES_128_GCM_SHA256,
  3468. TLS1_3_RFC_AES_256_GCM_SHA384,
  3469. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3470. TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3471. # else
  3472. NULL,
  3473. # endif
  3474. TLS1_3_RFC_AES_128_CCM_SHA256,
  3475. TLS1_3_RFC_AES_128_CCM_8_SHA256
  3476. };
  3477. const unsigned char *cipher_bytes[] = {
  3478. TLS13_AES_128_GCM_SHA256_BYTES,
  3479. TLS13_AES_256_GCM_SHA384_BYTES,
  3480. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3481. TLS13_CHACHA20_POLY1305_SHA256_BYTES,
  3482. # else
  3483. NULL,
  3484. # endif
  3485. TLS13_AES_128_CCM_SHA256_BYTES,
  3486. TLS13_AES_128_CCM_8_SHA256_BYTES
  3487. };
  3488. if (cipher_str[idx] == NULL)
  3489. return 1;
  3490. /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
  3491. if (idx == 2 && is_fips == 1)
  3492. return 1;
  3493. /* We always set this up with a final parameter of "2" for PSK */
  3494. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3495. &serverssl, &sess, 2)))
  3496. goto end;
  3497. if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
  3498. || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
  3499. goto end;
  3500. /*
  3501. * 'setupearly_data_test' creates only one instance of SSL_SESSION
  3502. * and assigns to both client and server with incremented reference
  3503. * and the same instance is updated in 'sess'.
  3504. * So updating ciphersuite in 'sess' which will get reflected in
  3505. * PSK handshake using psk use sess and find sess cb.
  3506. */
  3507. cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
  3508. if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
  3509. goto end;
  3510. SSL_set_connect_state(clientssl);
  3511. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3512. &written)))
  3513. goto end;
  3514. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3515. &readbytes),
  3516. SSL_READ_EARLY_DATA_SUCCESS)
  3517. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3518. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3519. SSL_EARLY_DATA_ACCEPTED)
  3520. || !TEST_int_eq(SSL_connect(clientssl), 1)
  3521. || !TEST_int_eq(SSL_accept(serverssl), 1))
  3522. goto end;
  3523. /* Send some normal data from client to server */
  3524. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3525. || !TEST_size_t_eq(written, strlen(MSG2)))
  3526. goto end;
  3527. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3528. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3529. goto end;
  3530. testresult = 1;
  3531. end:
  3532. SSL_SESSION_free(sess);
  3533. SSL_SESSION_free(clientpsk);
  3534. SSL_SESSION_free(serverpsk);
  3535. clientpsk = serverpsk = NULL;
  3536. if (clientssl != NULL)
  3537. SSL_shutdown(clientssl);
  3538. if (serverssl != NULL)
  3539. SSL_shutdown(serverssl);
  3540. SSL_free(serverssl);
  3541. SSL_free(clientssl);
  3542. SSL_CTX_free(sctx);
  3543. SSL_CTX_free(cctx);
  3544. return testresult;
  3545. }
  3546. /*
  3547. * Test that a server that doesn't try to read early data can handle a
  3548. * client sending some.
  3549. */
  3550. static int test_early_data_not_expected(int idx)
  3551. {
  3552. SSL_CTX *cctx = NULL, *sctx = NULL;
  3553. SSL *clientssl = NULL, *serverssl = NULL;
  3554. int testresult = 0;
  3555. SSL_SESSION *sess = NULL;
  3556. unsigned char buf[20];
  3557. size_t readbytes, written;
  3558. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3559. &serverssl, &sess, idx)))
  3560. goto end;
  3561. /* Write some early data */
  3562. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3563. &written)))
  3564. goto end;
  3565. /*
  3566. * Server should skip over early data and then block waiting for client to
  3567. * continue handshake
  3568. */
  3569. if (!TEST_int_le(SSL_accept(serverssl), 0)
  3570. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3571. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3572. SSL_EARLY_DATA_REJECTED)
  3573. || !TEST_int_gt(SSL_accept(serverssl), 0)
  3574. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3575. SSL_EARLY_DATA_REJECTED))
  3576. goto end;
  3577. /* Send some normal data from client to server */
  3578. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3579. || !TEST_size_t_eq(written, strlen(MSG2)))
  3580. goto end;
  3581. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3582. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3583. goto end;
  3584. testresult = 1;
  3585. end:
  3586. SSL_SESSION_free(sess);
  3587. SSL_SESSION_free(clientpsk);
  3588. SSL_SESSION_free(serverpsk);
  3589. clientpsk = serverpsk = NULL;
  3590. SSL_free(serverssl);
  3591. SSL_free(clientssl);
  3592. SSL_CTX_free(sctx);
  3593. SSL_CTX_free(cctx);
  3594. return testresult;
  3595. }
  3596. # ifndef OPENSSL_NO_TLS1_2
  3597. /*
  3598. * Test that a server attempting to read early data can handle a connection
  3599. * from a TLSv1.2 client.
  3600. */
  3601. static int test_early_data_tls1_2(int idx)
  3602. {
  3603. SSL_CTX *cctx = NULL, *sctx = NULL;
  3604. SSL *clientssl = NULL, *serverssl = NULL;
  3605. int testresult = 0;
  3606. unsigned char buf[20];
  3607. size_t readbytes, written;
  3608. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3609. &serverssl, NULL, idx)))
  3610. goto end;
  3611. /* Write some data - should block due to handshake with server */
  3612. SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
  3613. SSL_set_connect_state(clientssl);
  3614. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3615. goto end;
  3616. /*
  3617. * Server should do TLSv1.2 handshake. First it will block waiting for more
  3618. * messages from client after ServerDone. Then SSL_read_early_data should
  3619. * finish and detect that early data has not been sent
  3620. */
  3621. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3622. &readbytes),
  3623. SSL_READ_EARLY_DATA_ERROR))
  3624. goto end;
  3625. /*
  3626. * Continue writing the message we started earlier. Will still block waiting
  3627. * for the CCS/Finished from server
  3628. */
  3629. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3630. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3631. &readbytes),
  3632. SSL_READ_EARLY_DATA_FINISH)
  3633. || !TEST_size_t_eq(readbytes, 0)
  3634. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3635. SSL_EARLY_DATA_NOT_SENT))
  3636. goto end;
  3637. /* Continue writing the message we started earlier */
  3638. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3639. || !TEST_size_t_eq(written, strlen(MSG1))
  3640. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3641. SSL_EARLY_DATA_NOT_SENT)
  3642. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3643. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3644. || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
  3645. || !TEST_size_t_eq(written, strlen(MSG2))
  3646. || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
  3647. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3648. goto end;
  3649. testresult = 1;
  3650. end:
  3651. SSL_SESSION_free(clientpsk);
  3652. SSL_SESSION_free(serverpsk);
  3653. clientpsk = serverpsk = NULL;
  3654. SSL_free(serverssl);
  3655. SSL_free(clientssl);
  3656. SSL_CTX_free(sctx);
  3657. SSL_CTX_free(cctx);
  3658. return testresult;
  3659. }
  3660. # endif /* OPENSSL_NO_TLS1_2 */
  3661. /*
  3662. * Test configuring the TLSv1.3 ciphersuites
  3663. *
  3664. * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
  3665. * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
  3666. * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
  3667. * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
  3668. * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  3669. * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  3670. * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
  3671. * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
  3672. * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
  3673. * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
  3674. */
  3675. static int test_set_ciphersuite(int idx)
  3676. {
  3677. SSL_CTX *cctx = NULL, *sctx = NULL;
  3678. SSL *clientssl = NULL, *serverssl = NULL;
  3679. int testresult = 0;
  3680. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3681. TLS_client_method(), TLS1_VERSION, 0,
  3682. &sctx, &cctx, cert, privkey))
  3683. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3684. "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
  3685. goto end;
  3686. if (idx >=4 && idx <= 7) {
  3687. /* SSL_CTX explicit cipher list */
  3688. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
  3689. goto end;
  3690. }
  3691. if (idx == 0 || idx == 4) {
  3692. /* Default ciphersuite */
  3693. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3694. "TLS_AES_128_GCM_SHA256")))
  3695. goto end;
  3696. } else if (idx == 1 || idx == 5) {
  3697. /* Non default ciphersuite */
  3698. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3699. "TLS_AES_128_CCM_SHA256")))
  3700. goto end;
  3701. }
  3702. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3703. &clientssl, NULL, NULL)))
  3704. goto end;
  3705. if (idx == 8 || idx == 9) {
  3706. /* SSL explicit cipher list */
  3707. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
  3708. goto end;
  3709. }
  3710. if (idx == 2 || idx == 6 || idx == 8) {
  3711. /* Default ciphersuite */
  3712. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  3713. "TLS_AES_128_GCM_SHA256")))
  3714. goto end;
  3715. } else if (idx == 3 || idx == 7 || idx == 9) {
  3716. /* Non default ciphersuite */
  3717. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  3718. "TLS_AES_128_CCM_SHA256")))
  3719. goto end;
  3720. }
  3721. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  3722. goto end;
  3723. testresult = 1;
  3724. end:
  3725. SSL_free(serverssl);
  3726. SSL_free(clientssl);
  3727. SSL_CTX_free(sctx);
  3728. SSL_CTX_free(cctx);
  3729. return testresult;
  3730. }
  3731. static int test_ciphersuite_change(void)
  3732. {
  3733. SSL_CTX *cctx = NULL, *sctx = NULL;
  3734. SSL *clientssl = NULL, *serverssl = NULL;
  3735. SSL_SESSION *clntsess = NULL;
  3736. int testresult = 0;
  3737. const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
  3738. /* Create a session based on SHA-256 */
  3739. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3740. TLS_client_method(), TLS1_VERSION, 0,
  3741. &sctx, &cctx, cert, privkey))
  3742. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3743. "TLS_AES_128_GCM_SHA256:"
  3744. "TLS_AES_256_GCM_SHA384:"
  3745. "TLS_AES_128_CCM_SHA256"))
  3746. || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3747. "TLS_AES_128_GCM_SHA256"))
  3748. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3749. &clientssl, NULL, NULL))
  3750. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3751. SSL_ERROR_NONE)))
  3752. goto end;
  3753. clntsess = SSL_get1_session(clientssl);
  3754. /* Save for later */
  3755. aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
  3756. SSL_shutdown(clientssl);
  3757. SSL_shutdown(serverssl);
  3758. SSL_free(serverssl);
  3759. SSL_free(clientssl);
  3760. serverssl = clientssl = NULL;
  3761. /* Check we can resume a session with a different SHA-256 ciphersuite */
  3762. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3763. "TLS_AES_128_CCM_SHA256"))
  3764. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3765. &clientssl, NULL, NULL))
  3766. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3767. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3768. SSL_ERROR_NONE))
  3769. || !TEST_true(SSL_session_reused(clientssl)))
  3770. goto end;
  3771. SSL_SESSION_free(clntsess);
  3772. clntsess = SSL_get1_session(clientssl);
  3773. SSL_shutdown(clientssl);
  3774. SSL_shutdown(serverssl);
  3775. SSL_free(serverssl);
  3776. SSL_free(clientssl);
  3777. serverssl = clientssl = NULL;
  3778. /*
  3779. * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
  3780. * succeeds but does not resume.
  3781. */
  3782. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  3783. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3784. NULL, NULL))
  3785. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3786. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3787. SSL_ERROR_SSL))
  3788. || !TEST_false(SSL_session_reused(clientssl)))
  3789. goto end;
  3790. SSL_SESSION_free(clntsess);
  3791. clntsess = NULL;
  3792. SSL_shutdown(clientssl);
  3793. SSL_shutdown(serverssl);
  3794. SSL_free(serverssl);
  3795. SSL_free(clientssl);
  3796. serverssl = clientssl = NULL;
  3797. /* Create a session based on SHA384 */
  3798. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  3799. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3800. &clientssl, NULL, NULL))
  3801. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3802. SSL_ERROR_NONE)))
  3803. goto end;
  3804. clntsess = SSL_get1_session(clientssl);
  3805. SSL_shutdown(clientssl);
  3806. SSL_shutdown(serverssl);
  3807. SSL_free(serverssl);
  3808. SSL_free(clientssl);
  3809. serverssl = clientssl = NULL;
  3810. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3811. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
  3812. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3813. "TLS_AES_256_GCM_SHA384"))
  3814. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3815. NULL, NULL))
  3816. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3817. /*
  3818. * We use SSL_ERROR_WANT_READ below so that we can pause the
  3819. * connection after the initial ClientHello has been sent to
  3820. * enable us to make some session changes.
  3821. */
  3822. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3823. SSL_ERROR_WANT_READ)))
  3824. goto end;
  3825. /* Trick the client into thinking this session is for a different digest */
  3826. clntsess->cipher = aes_128_gcm_sha256;
  3827. clntsess->cipher_id = clntsess->cipher->id;
  3828. /*
  3829. * Continue the previously started connection. Server has selected a SHA-384
  3830. * ciphersuite, but client thinks the session is for SHA-256, so it should
  3831. * bail out.
  3832. */
  3833. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  3834. SSL_ERROR_SSL))
  3835. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
  3836. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
  3837. goto end;
  3838. testresult = 1;
  3839. end:
  3840. SSL_SESSION_free(clntsess);
  3841. SSL_free(serverssl);
  3842. SSL_free(clientssl);
  3843. SSL_CTX_free(sctx);
  3844. SSL_CTX_free(cctx);
  3845. return testresult;
  3846. }
  3847. /*
  3848. * Test TLSv1.3 Key exchange
  3849. * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
  3850. * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
  3851. * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
  3852. * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
  3853. * Test 4 = Test NID_X25519 with TLSv1.3 client and server
  3854. * Test 5 = Test NID_X448 with TLSv1.3 client and server
  3855. * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
  3856. * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
  3857. * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
  3858. * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
  3859. * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
  3860. * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
  3861. * Test 12 = Test all ECDHE with TLSv1.2 client and server
  3862. * Test 13 = Test all FFDHE with TLSv1.2 client and server
  3863. */
  3864. static int test_key_exchange(int idx)
  3865. {
  3866. SSL_CTX *sctx = NULL, *cctx = NULL;
  3867. SSL *serverssl = NULL, *clientssl = NULL;
  3868. int testresult = 0;
  3869. # ifndef OPENSSL_NO_EC
  3870. int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
  3871. NID_secp521r1, NID_X25519, NID_X448};
  3872. # endif
  3873. # ifndef OPENSSL_NO_DH
  3874. int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
  3875. NID_ffdhe6144, NID_ffdhe8192};
  3876. # endif
  3877. int kexch_alg;
  3878. int *kexch_groups = &kexch_alg;
  3879. int kexch_groups_size = 1;
  3880. int max_version = TLS1_3_VERSION;
  3881. char *kexch_name0 = NULL;
  3882. switch (idx) {
  3883. # ifndef OPENSSL_NO_EC
  3884. # ifndef OPENSSL_NO_TLS1_2
  3885. case 12:
  3886. max_version = TLS1_2_VERSION;
  3887. # endif
  3888. /* Fall through */
  3889. case 0:
  3890. kexch_groups = ecdhe_kexch_groups;
  3891. kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
  3892. kexch_name0 = "secp256r1";
  3893. break;
  3894. case 1:
  3895. kexch_alg = NID_X9_62_prime256v1;
  3896. kexch_name0 = "secp256r1";
  3897. break;
  3898. case 2:
  3899. kexch_alg = NID_secp384r1;
  3900. kexch_name0 = "secp384r1";
  3901. break;
  3902. case 3:
  3903. kexch_alg = NID_secp521r1;
  3904. kexch_name0 = "secp521r1";
  3905. break;
  3906. case 4:
  3907. kexch_alg = NID_X25519;
  3908. kexch_name0 = "x25519";
  3909. break;
  3910. case 5:
  3911. kexch_alg = NID_X448;
  3912. kexch_name0 = "x448";
  3913. break;
  3914. # endif
  3915. # ifndef OPENSSL_NO_DH
  3916. # ifndef OPENSSL_NO_TLS1_2
  3917. case 13:
  3918. max_version = TLS1_2_VERSION;
  3919. kexch_name0 = "ffdhe2048";
  3920. # endif
  3921. /* Fall through */
  3922. case 6:
  3923. kexch_groups = ffdhe_kexch_groups;
  3924. kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
  3925. kexch_name0 = "ffdhe2048";
  3926. break;
  3927. case 7:
  3928. kexch_alg = NID_ffdhe2048;
  3929. kexch_name0 = "ffdhe2048";
  3930. break;
  3931. case 8:
  3932. kexch_alg = NID_ffdhe3072;
  3933. kexch_name0 = "ffdhe3072";
  3934. break;
  3935. case 9:
  3936. kexch_alg = NID_ffdhe4096;
  3937. kexch_name0 = "ffdhe4096";
  3938. break;
  3939. case 10:
  3940. kexch_alg = NID_ffdhe6144;
  3941. kexch_name0 = "ffdhe6144";
  3942. break;
  3943. case 11:
  3944. kexch_alg = NID_ffdhe8192;
  3945. kexch_name0 = "ffdhe8192";
  3946. break;
  3947. # endif
  3948. default:
  3949. /* We're skipping this test */
  3950. return 1;
  3951. }
  3952. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3953. TLS_client_method(), TLS1_VERSION,
  3954. max_version, &sctx, &cctx, cert,
  3955. privkey)))
  3956. goto end;
  3957. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3958. TLS1_3_RFC_AES_128_GCM_SHA256)))
  3959. goto end;
  3960. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3961. TLS1_3_RFC_AES_128_GCM_SHA256)))
  3962. goto end;
  3963. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  3964. TLS1_TXT_RSA_WITH_AES_128_SHA)))
  3965. goto end;
  3966. /*
  3967. * Must include an EC ciphersuite so that we send supported groups in
  3968. * TLSv1.2
  3969. */
  3970. # ifndef OPENSSL_NO_TLS1_2
  3971. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  3972. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CCM ":"
  3973. TLS1_TXT_RSA_WITH_AES_128_SHA)))
  3974. goto end;
  3975. # endif
  3976. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3977. NULL, NULL)))
  3978. goto end;
  3979. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
  3980. || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
  3981. goto end;
  3982. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  3983. goto end;
  3984. /*
  3985. * If Handshake succeeds the negotiated kexch alg should be the first one in
  3986. * configured, except in the case of FFDHE groups (idx 13), which are
  3987. * TLSv1.3 only so we expect no shared group to exist.
  3988. */
  3989. if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
  3990. idx == 13 ? 0 : kexch_groups[0]))
  3991. goto end;
  3992. if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
  3993. kexch_name0))
  3994. goto end;
  3995. if (max_version == TLS1_3_VERSION) {
  3996. if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
  3997. goto end;
  3998. if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
  3999. goto end;
  4000. }
  4001. testresult = 1;
  4002. end:
  4003. SSL_free(serverssl);
  4004. SSL_free(clientssl);
  4005. SSL_CTX_free(sctx);
  4006. SSL_CTX_free(cctx);
  4007. return testresult;
  4008. }
  4009. /*
  4010. * Test TLSv1.3 Cipher Suite
  4011. * Test 0 = Set TLS1.3 cipher on context
  4012. * Test 1 = Set TLS1.3 cipher on SSL
  4013. * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
  4014. * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
  4015. */
  4016. static int test_tls13_ciphersuite(int idx)
  4017. {
  4018. SSL_CTX *sctx = NULL, *cctx = NULL;
  4019. SSL *serverssl = NULL, *clientssl = NULL;
  4020. static const struct {
  4021. const char *ciphername;
  4022. int fipscapable;
  4023. } t13_ciphers[] = {
  4024. { TLS1_3_RFC_AES_128_GCM_SHA256, 1 },
  4025. { TLS1_3_RFC_AES_256_GCM_SHA384, 1 },
  4026. { TLS1_3_RFC_AES_128_CCM_SHA256, 1 },
  4027. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  4028. { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
  4029. { TLS1_3_RFC_AES_256_GCM_SHA384
  4030. ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
  4031. # endif
  4032. { TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1 }
  4033. };
  4034. const char *t13_cipher = NULL;
  4035. const char *t12_cipher = NULL;
  4036. const char *negotiated_scipher;
  4037. const char *negotiated_ccipher;
  4038. int set_at_ctx = 0;
  4039. int set_at_ssl = 0;
  4040. int testresult = 0;
  4041. int max_ver;
  4042. size_t i;
  4043. switch (idx) {
  4044. case 0:
  4045. set_at_ctx = 1;
  4046. break;
  4047. case 1:
  4048. set_at_ssl = 1;
  4049. break;
  4050. case 2:
  4051. set_at_ctx = 1;
  4052. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4053. break;
  4054. case 3:
  4055. set_at_ssl = 1;
  4056. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4057. break;
  4058. }
  4059. for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
  4060. # ifdef OPENSSL_NO_TLS1_2
  4061. if (max_ver == TLS1_2_VERSION)
  4062. continue;
  4063. # endif
  4064. for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
  4065. if (is_fips && !t13_ciphers[i].fipscapable)
  4066. continue;
  4067. t13_cipher = t13_ciphers[i].ciphername;
  4068. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4069. TLS_client_method(),
  4070. TLS1_VERSION, max_ver,
  4071. &sctx, &cctx, cert, privkey)))
  4072. goto end;
  4073. if (set_at_ctx) {
  4074. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
  4075. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
  4076. goto end;
  4077. if (t12_cipher != NULL) {
  4078. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
  4079. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  4080. t12_cipher)))
  4081. goto end;
  4082. }
  4083. }
  4084. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4085. &clientssl, NULL, NULL)))
  4086. goto end;
  4087. if (set_at_ssl) {
  4088. if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
  4089. || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
  4090. goto end;
  4091. if (t12_cipher != NULL) {
  4092. if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
  4093. || !TEST_true(SSL_set_cipher_list(clientssl,
  4094. t12_cipher)))
  4095. goto end;
  4096. }
  4097. }
  4098. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4099. SSL_ERROR_NONE)))
  4100. goto end;
  4101. negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4102. serverssl));
  4103. negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4104. clientssl));
  4105. if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
  4106. goto end;
  4107. /*
  4108. * TEST_strn_eq is used below because t13_cipher can contain
  4109. * multiple ciphersuites
  4110. */
  4111. if (max_ver == TLS1_3_VERSION
  4112. && !TEST_strn_eq(t13_cipher, negotiated_scipher,
  4113. strlen(negotiated_scipher)))
  4114. goto end;
  4115. # ifndef OPENSSL_NO_TLS1_2
  4116. /* Below validation is not done when t12_cipher is NULL */
  4117. if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
  4118. && !TEST_str_eq(t12_cipher, negotiated_scipher))
  4119. goto end;
  4120. # endif
  4121. SSL_free(serverssl);
  4122. serverssl = NULL;
  4123. SSL_free(clientssl);
  4124. clientssl = NULL;
  4125. SSL_CTX_free(sctx);
  4126. sctx = NULL;
  4127. SSL_CTX_free(cctx);
  4128. cctx = NULL;
  4129. }
  4130. }
  4131. testresult = 1;
  4132. end:
  4133. SSL_free(serverssl);
  4134. SSL_free(clientssl);
  4135. SSL_CTX_free(sctx);
  4136. SSL_CTX_free(cctx);
  4137. return testresult;
  4138. }
  4139. /*
  4140. * Test TLSv1.3 PSKs
  4141. * Test 0 = Test new style callbacks
  4142. * Test 1 = Test both new and old style callbacks
  4143. * Test 2 = Test old style callbacks
  4144. * Test 3 = Test old style callbacks with no certificate
  4145. */
  4146. static int test_tls13_psk(int idx)
  4147. {
  4148. SSL_CTX *sctx = NULL, *cctx = NULL;
  4149. SSL *serverssl = NULL, *clientssl = NULL;
  4150. const SSL_CIPHER *cipher = NULL;
  4151. const unsigned char key[] = {
  4152. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4153. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4154. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  4155. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  4156. };
  4157. int testresult = 0;
  4158. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4159. TLS_client_method(), TLS1_VERSION, 0,
  4160. &sctx, &cctx, idx == 3 ? NULL : cert,
  4161. idx == 3 ? NULL : privkey)))
  4162. goto end;
  4163. if (idx != 3) {
  4164. /*
  4165. * We use a ciphersuite with SHA256 to ease testing old style PSK
  4166. * callbacks which will always default to SHA256. This should not be
  4167. * necessary if we have no cert/priv key. In that case the server should
  4168. * prefer SHA256 automatically.
  4169. */
  4170. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4171. "TLS_AES_128_GCM_SHA256")))
  4172. goto end;
  4173. } else {
  4174. /*
  4175. * As noted above the server should prefer SHA256 automatically. However
  4176. * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
  4177. * code works even if we are testing with only the FIPS provider loaded.
  4178. */
  4179. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4180. "TLS_AES_256_GCM_SHA384:"
  4181. "TLS_AES_128_GCM_SHA256")))
  4182. goto end;
  4183. }
  4184. /*
  4185. * Test 0: New style callbacks only
  4186. * Test 1: New and old style callbacks (only the new ones should be used)
  4187. * Test 2: Old style callbacks only
  4188. */
  4189. if (idx == 0 || idx == 1) {
  4190. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  4191. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  4192. }
  4193. #ifndef OPENSSL_NO_PSK
  4194. if (idx >= 1) {
  4195. SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
  4196. SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
  4197. }
  4198. #endif
  4199. srvid = pskid;
  4200. use_session_cb_cnt = 0;
  4201. find_session_cb_cnt = 0;
  4202. psk_client_cb_cnt = 0;
  4203. psk_server_cb_cnt = 0;
  4204. if (idx != 3) {
  4205. /*
  4206. * Check we can create a connection if callback decides not to send a
  4207. * PSK
  4208. */
  4209. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4210. NULL, NULL))
  4211. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4212. SSL_ERROR_NONE))
  4213. || !TEST_false(SSL_session_reused(clientssl))
  4214. || !TEST_false(SSL_session_reused(serverssl)))
  4215. goto end;
  4216. if (idx == 0 || idx == 1) {
  4217. if (!TEST_true(use_session_cb_cnt == 1)
  4218. || !TEST_true(find_session_cb_cnt == 0)
  4219. /*
  4220. * If no old style callback then below should be 0
  4221. * otherwise 1
  4222. */
  4223. || !TEST_true(psk_client_cb_cnt == idx)
  4224. || !TEST_true(psk_server_cb_cnt == 0))
  4225. goto end;
  4226. } else {
  4227. if (!TEST_true(use_session_cb_cnt == 0)
  4228. || !TEST_true(find_session_cb_cnt == 0)
  4229. || !TEST_true(psk_client_cb_cnt == 1)
  4230. || !TEST_true(psk_server_cb_cnt == 0))
  4231. goto end;
  4232. }
  4233. shutdown_ssl_connection(serverssl, clientssl);
  4234. serverssl = clientssl = NULL;
  4235. use_session_cb_cnt = psk_client_cb_cnt = 0;
  4236. }
  4237. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4238. NULL, NULL)))
  4239. goto end;
  4240. /* Create the PSK */
  4241. cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
  4242. clientpsk = SSL_SESSION_new();
  4243. if (!TEST_ptr(clientpsk)
  4244. || !TEST_ptr(cipher)
  4245. || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
  4246. sizeof(key)))
  4247. || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
  4248. || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
  4249. TLS1_3_VERSION))
  4250. || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
  4251. goto end;
  4252. serverpsk = clientpsk;
  4253. /* Check we can create a connection and the PSK is used */
  4254. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4255. || !TEST_true(SSL_session_reused(clientssl))
  4256. || !TEST_true(SSL_session_reused(serverssl)))
  4257. goto end;
  4258. if (idx == 0 || idx == 1) {
  4259. if (!TEST_true(use_session_cb_cnt == 1)
  4260. || !TEST_true(find_session_cb_cnt == 1)
  4261. || !TEST_true(psk_client_cb_cnt == 0)
  4262. || !TEST_true(psk_server_cb_cnt == 0))
  4263. goto end;
  4264. } else {
  4265. if (!TEST_true(use_session_cb_cnt == 0)
  4266. || !TEST_true(find_session_cb_cnt == 0)
  4267. || !TEST_true(psk_client_cb_cnt == 1)
  4268. || !TEST_true(psk_server_cb_cnt == 1))
  4269. goto end;
  4270. }
  4271. shutdown_ssl_connection(serverssl, clientssl);
  4272. serverssl = clientssl = NULL;
  4273. use_session_cb_cnt = find_session_cb_cnt = 0;
  4274. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4275. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4276. NULL, NULL)))
  4277. goto end;
  4278. /* Force an HRR */
  4279. #if defined(OPENSSL_NO_EC)
  4280. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  4281. goto end;
  4282. #else
  4283. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
  4284. goto end;
  4285. #endif
  4286. /*
  4287. * Check we can create a connection, the PSK is used and the callbacks are
  4288. * called twice.
  4289. */
  4290. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4291. || !TEST_true(SSL_session_reused(clientssl))
  4292. || !TEST_true(SSL_session_reused(serverssl)))
  4293. goto end;
  4294. if (idx == 0 || idx == 1) {
  4295. if (!TEST_true(use_session_cb_cnt == 2)
  4296. || !TEST_true(find_session_cb_cnt == 2)
  4297. || !TEST_true(psk_client_cb_cnt == 0)
  4298. || !TEST_true(psk_server_cb_cnt == 0))
  4299. goto end;
  4300. } else {
  4301. if (!TEST_true(use_session_cb_cnt == 0)
  4302. || !TEST_true(find_session_cb_cnt == 0)
  4303. || !TEST_true(psk_client_cb_cnt == 2)
  4304. || !TEST_true(psk_server_cb_cnt == 2))
  4305. goto end;
  4306. }
  4307. shutdown_ssl_connection(serverssl, clientssl);
  4308. serverssl = clientssl = NULL;
  4309. use_session_cb_cnt = find_session_cb_cnt = 0;
  4310. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4311. if (idx != 3) {
  4312. /*
  4313. * Check that if the server rejects the PSK we can still connect, but with
  4314. * a full handshake
  4315. */
  4316. srvid = "Dummy Identity";
  4317. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4318. NULL, NULL))
  4319. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4320. SSL_ERROR_NONE))
  4321. || !TEST_false(SSL_session_reused(clientssl))
  4322. || !TEST_false(SSL_session_reused(serverssl)))
  4323. goto end;
  4324. if (idx == 0 || idx == 1) {
  4325. if (!TEST_true(use_session_cb_cnt == 1)
  4326. || !TEST_true(find_session_cb_cnt == 1)
  4327. || !TEST_true(psk_client_cb_cnt == 0)
  4328. /*
  4329. * If no old style callback then below should be 0
  4330. * otherwise 1
  4331. */
  4332. || !TEST_true(psk_server_cb_cnt == idx))
  4333. goto end;
  4334. } else {
  4335. if (!TEST_true(use_session_cb_cnt == 0)
  4336. || !TEST_true(find_session_cb_cnt == 0)
  4337. || !TEST_true(psk_client_cb_cnt == 1)
  4338. || !TEST_true(psk_server_cb_cnt == 1))
  4339. goto end;
  4340. }
  4341. shutdown_ssl_connection(serverssl, clientssl);
  4342. serverssl = clientssl = NULL;
  4343. }
  4344. testresult = 1;
  4345. end:
  4346. SSL_SESSION_free(clientpsk);
  4347. SSL_SESSION_free(serverpsk);
  4348. clientpsk = serverpsk = NULL;
  4349. SSL_free(serverssl);
  4350. SSL_free(clientssl);
  4351. SSL_CTX_free(sctx);
  4352. SSL_CTX_free(cctx);
  4353. return testresult;
  4354. }
  4355. static unsigned char cookie_magic_value[] = "cookie magic";
  4356. static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  4357. unsigned int *cookie_len)
  4358. {
  4359. /*
  4360. * Not suitable as a real cookie generation function but good enough for
  4361. * testing!
  4362. */
  4363. memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
  4364. *cookie_len = sizeof(cookie_magic_value) - 1;
  4365. return 1;
  4366. }
  4367. static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  4368. unsigned int cookie_len)
  4369. {
  4370. if (cookie_len == sizeof(cookie_magic_value) - 1
  4371. && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
  4372. return 1;
  4373. return 0;
  4374. }
  4375. static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
  4376. size_t *cookie_len)
  4377. {
  4378. unsigned int temp;
  4379. int res = generate_cookie_callback(ssl, cookie, &temp);
  4380. *cookie_len = temp;
  4381. return res;
  4382. }
  4383. static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
  4384. size_t cookie_len)
  4385. {
  4386. return verify_cookie_callback(ssl, cookie, cookie_len);
  4387. }
  4388. static int test_stateless(void)
  4389. {
  4390. SSL_CTX *sctx = NULL, *cctx = NULL;
  4391. SSL *serverssl = NULL, *clientssl = NULL;
  4392. int testresult = 0;
  4393. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4394. TLS_client_method(), TLS1_VERSION, 0,
  4395. &sctx, &cctx, cert, privkey)))
  4396. goto end;
  4397. /* The arrival of CCS messages can confuse the test */
  4398. SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  4399. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4400. NULL, NULL))
  4401. /* Send the first ClientHello */
  4402. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4403. SSL_ERROR_WANT_READ))
  4404. /*
  4405. * This should fail with a -1 return because we have no callbacks
  4406. * set up
  4407. */
  4408. || !TEST_int_eq(SSL_stateless(serverssl), -1))
  4409. goto end;
  4410. /* Fatal error so abandon the connection from this client */
  4411. SSL_free(clientssl);
  4412. clientssl = NULL;
  4413. /* Set up the cookie generation and verification callbacks */
  4414. SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
  4415. SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
  4416. /*
  4417. * Create a new connection from the client (we can reuse the server SSL
  4418. * object).
  4419. */
  4420. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4421. NULL, NULL))
  4422. /* Send the first ClientHello */
  4423. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4424. SSL_ERROR_WANT_READ))
  4425. /* This should fail because there is no cookie */
  4426. || !TEST_int_eq(SSL_stateless(serverssl), 0))
  4427. goto end;
  4428. /* Abandon the connection from this client */
  4429. SSL_free(clientssl);
  4430. clientssl = NULL;
  4431. /*
  4432. * Now create a connection from a new client but with the same server SSL
  4433. * object
  4434. */
  4435. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4436. NULL, NULL))
  4437. /* Send the first ClientHello */
  4438. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4439. SSL_ERROR_WANT_READ))
  4440. /* This should fail because there is no cookie */
  4441. || !TEST_int_eq(SSL_stateless(serverssl), 0)
  4442. /* Send the second ClientHello */
  4443. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4444. SSL_ERROR_WANT_READ))
  4445. /* This should succeed because a cookie is now present */
  4446. || !TEST_int_eq(SSL_stateless(serverssl), 1)
  4447. /* Complete the connection */
  4448. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4449. SSL_ERROR_NONE)))
  4450. goto end;
  4451. shutdown_ssl_connection(serverssl, clientssl);
  4452. serverssl = clientssl = NULL;
  4453. testresult = 1;
  4454. end:
  4455. SSL_free(serverssl);
  4456. SSL_free(clientssl);
  4457. SSL_CTX_free(sctx);
  4458. SSL_CTX_free(cctx);
  4459. return testresult;
  4460. }
  4461. #endif /* OSSL_NO_USABLE_TLS1_3 */
  4462. static int clntaddoldcb = 0;
  4463. static int clntparseoldcb = 0;
  4464. static int srvaddoldcb = 0;
  4465. static int srvparseoldcb = 0;
  4466. static int clntaddnewcb = 0;
  4467. static int clntparsenewcb = 0;
  4468. static int srvaddnewcb = 0;
  4469. static int srvparsenewcb = 0;
  4470. static int snicb = 0;
  4471. #define TEST_EXT_TYPE1 0xff00
  4472. static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
  4473. size_t *outlen, int *al, void *add_arg)
  4474. {
  4475. int *server = (int *)add_arg;
  4476. unsigned char *data;
  4477. if (SSL_is_server(s))
  4478. srvaddoldcb++;
  4479. else
  4480. clntaddoldcb++;
  4481. if (*server != SSL_is_server(s)
  4482. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  4483. return -1;
  4484. *data = 1;
  4485. *out = data;
  4486. *outlen = sizeof(char);
  4487. return 1;
  4488. }
  4489. static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
  4490. void *add_arg)
  4491. {
  4492. OPENSSL_free((unsigned char *)out);
  4493. }
  4494. static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
  4495. size_t inlen, int *al, void *parse_arg)
  4496. {
  4497. int *server = (int *)parse_arg;
  4498. if (SSL_is_server(s))
  4499. srvparseoldcb++;
  4500. else
  4501. clntparseoldcb++;
  4502. if (*server != SSL_is_server(s)
  4503. || inlen != sizeof(char)
  4504. || *in != 1)
  4505. return -1;
  4506. return 1;
  4507. }
  4508. static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
  4509. const unsigned char **out, size_t *outlen, X509 *x,
  4510. size_t chainidx, int *al, void *add_arg)
  4511. {
  4512. int *server = (int *)add_arg;
  4513. unsigned char *data;
  4514. if (SSL_is_server(s))
  4515. srvaddnewcb++;
  4516. else
  4517. clntaddnewcb++;
  4518. if (*server != SSL_is_server(s)
  4519. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  4520. return -1;
  4521. *data = 1;
  4522. *out = data;
  4523. *outlen = sizeof(*data);
  4524. return 1;
  4525. }
  4526. static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
  4527. const unsigned char *out, void *add_arg)
  4528. {
  4529. OPENSSL_free((unsigned char *)out);
  4530. }
  4531. static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
  4532. const unsigned char *in, size_t inlen, X509 *x,
  4533. size_t chainidx, int *al, void *parse_arg)
  4534. {
  4535. int *server = (int *)parse_arg;
  4536. if (SSL_is_server(s))
  4537. srvparsenewcb++;
  4538. else
  4539. clntparsenewcb++;
  4540. if (*server != SSL_is_server(s)
  4541. || inlen != sizeof(char) || *in != 1)
  4542. return -1;
  4543. return 1;
  4544. }
  4545. static int sni_cb(SSL *s, int *al, void *arg)
  4546. {
  4547. SSL_CTX *ctx = (SSL_CTX *)arg;
  4548. if (SSL_set_SSL_CTX(s, ctx) == NULL) {
  4549. *al = SSL_AD_INTERNAL_ERROR;
  4550. return SSL_TLSEXT_ERR_ALERT_FATAL;
  4551. }
  4552. snicb++;
  4553. return SSL_TLSEXT_ERR_OK;
  4554. }
  4555. /*
  4556. * Custom call back tests.
  4557. * Test 0: Old style callbacks in TLSv1.2
  4558. * Test 1: New style callbacks in TLSv1.2
  4559. * Test 2: New style callbacks in TLSv1.2 with SNI
  4560. * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
  4561. * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
  4562. */
  4563. static int test_custom_exts(int tst)
  4564. {
  4565. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  4566. SSL *clientssl = NULL, *serverssl = NULL;
  4567. int testresult = 0;
  4568. static int server = 1;
  4569. static int client = 0;
  4570. SSL_SESSION *sess = NULL;
  4571. unsigned int context;
  4572. #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  4573. /* Skip tests for TLSv1.2 and below in this case */
  4574. if (tst < 3)
  4575. return 1;
  4576. #endif
  4577. /* Reset callback counters */
  4578. clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
  4579. clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
  4580. snicb = 0;
  4581. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4582. TLS_client_method(), TLS1_VERSION, 0,
  4583. &sctx, &cctx, cert, privkey)))
  4584. goto end;
  4585. if (tst == 2
  4586. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
  4587. TLS1_VERSION, 0,
  4588. &sctx2, NULL, cert, privkey)))
  4589. goto end;
  4590. if (tst < 3) {
  4591. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  4592. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  4593. if (sctx2 != NULL)
  4594. SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
  4595. }
  4596. if (tst == 4) {
  4597. context = SSL_EXT_CLIENT_HELLO
  4598. | SSL_EXT_TLS1_2_SERVER_HELLO
  4599. | SSL_EXT_TLS1_3_SERVER_HELLO
  4600. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  4601. | SSL_EXT_TLS1_3_CERTIFICATE
  4602. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
  4603. } else {
  4604. context = SSL_EXT_CLIENT_HELLO
  4605. | SSL_EXT_TLS1_2_SERVER_HELLO
  4606. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
  4607. }
  4608. /* Create a client side custom extension */
  4609. if (tst == 0) {
  4610. if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  4611. old_add_cb, old_free_cb,
  4612. &client, old_parse_cb,
  4613. &client)))
  4614. goto end;
  4615. } else {
  4616. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
  4617. new_add_cb, new_free_cb,
  4618. &client, new_parse_cb, &client)))
  4619. goto end;
  4620. }
  4621. /* Should not be able to add duplicates */
  4622. if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  4623. old_add_cb, old_free_cb,
  4624. &client, old_parse_cb,
  4625. &client))
  4626. || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
  4627. context, new_add_cb,
  4628. new_free_cb, &client,
  4629. new_parse_cb, &client)))
  4630. goto end;
  4631. /* Create a server side custom extension */
  4632. if (tst == 0) {
  4633. if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  4634. old_add_cb, old_free_cb,
  4635. &server, old_parse_cb,
  4636. &server)))
  4637. goto end;
  4638. } else {
  4639. if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
  4640. new_add_cb, new_free_cb,
  4641. &server, new_parse_cb, &server)))
  4642. goto end;
  4643. if (sctx2 != NULL
  4644. && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
  4645. context, new_add_cb,
  4646. new_free_cb, &server,
  4647. new_parse_cb, &server)))
  4648. goto end;
  4649. }
  4650. /* Should not be able to add duplicates */
  4651. if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  4652. old_add_cb, old_free_cb,
  4653. &server, old_parse_cb,
  4654. &server))
  4655. || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
  4656. context, new_add_cb,
  4657. new_free_cb, &server,
  4658. new_parse_cb, &server)))
  4659. goto end;
  4660. if (tst == 2) {
  4661. /* Set up SNI */
  4662. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  4663. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  4664. goto end;
  4665. }
  4666. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4667. &clientssl, NULL, NULL))
  4668. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4669. SSL_ERROR_NONE)))
  4670. goto end;
  4671. if (tst == 0) {
  4672. if (clntaddoldcb != 1
  4673. || clntparseoldcb != 1
  4674. || srvaddoldcb != 1
  4675. || srvparseoldcb != 1)
  4676. goto end;
  4677. } else if (tst == 1 || tst == 2 || tst == 3) {
  4678. if (clntaddnewcb != 1
  4679. || clntparsenewcb != 1
  4680. || srvaddnewcb != 1
  4681. || srvparsenewcb != 1
  4682. || (tst != 2 && snicb != 0)
  4683. || (tst == 2 && snicb != 1))
  4684. goto end;
  4685. } else {
  4686. /* In this case there 2 NewSessionTicket messages created */
  4687. if (clntaddnewcb != 1
  4688. || clntparsenewcb != 5
  4689. || srvaddnewcb != 5
  4690. || srvparsenewcb != 1)
  4691. goto end;
  4692. }
  4693. sess = SSL_get1_session(clientssl);
  4694. SSL_shutdown(clientssl);
  4695. SSL_shutdown(serverssl);
  4696. SSL_free(serverssl);
  4697. SSL_free(clientssl);
  4698. serverssl = clientssl = NULL;
  4699. if (tst == 3) {
  4700. /* We don't bother with the resumption aspects for this test */
  4701. testresult = 1;
  4702. goto end;
  4703. }
  4704. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4705. NULL, NULL))
  4706. || !TEST_true(SSL_set_session(clientssl, sess))
  4707. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4708. SSL_ERROR_NONE)))
  4709. goto end;
  4710. /*
  4711. * For a resumed session we expect to add the ClientHello extension. For the
  4712. * old style callbacks we ignore it on the server side because they set
  4713. * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
  4714. * them.
  4715. */
  4716. if (tst == 0) {
  4717. if (clntaddoldcb != 2
  4718. || clntparseoldcb != 1
  4719. || srvaddoldcb != 1
  4720. || srvparseoldcb != 1)
  4721. goto end;
  4722. } else if (tst == 1 || tst == 2 || tst == 3) {
  4723. if (clntaddnewcb != 2
  4724. || clntparsenewcb != 2
  4725. || srvaddnewcb != 2
  4726. || srvparsenewcb != 2)
  4727. goto end;
  4728. } else {
  4729. /*
  4730. * No Certificate message extensions in the resumption handshake,
  4731. * 2 NewSessionTickets in the initial handshake, 1 in the resumption
  4732. */
  4733. if (clntaddnewcb != 2
  4734. || clntparsenewcb != 8
  4735. || srvaddnewcb != 8
  4736. || srvparsenewcb != 2)
  4737. goto end;
  4738. }
  4739. testresult = 1;
  4740. end:
  4741. SSL_SESSION_free(sess);
  4742. SSL_free(serverssl);
  4743. SSL_free(clientssl);
  4744. SSL_CTX_free(sctx2);
  4745. SSL_CTX_free(sctx);
  4746. SSL_CTX_free(cctx);
  4747. return testresult;
  4748. }
  4749. /*
  4750. * Test loading of serverinfo data in various formats. test_sslmessages actually
  4751. * tests to make sure the extensions appear in the handshake
  4752. */
  4753. static int test_serverinfo(int tst)
  4754. {
  4755. unsigned int version;
  4756. unsigned char *sibuf;
  4757. size_t sibuflen;
  4758. int ret, expected, testresult = 0;
  4759. SSL_CTX *ctx;
  4760. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method());
  4761. if (!TEST_ptr(ctx))
  4762. goto end;
  4763. if ((tst & 0x01) == 0x01)
  4764. version = SSL_SERVERINFOV2;
  4765. else
  4766. version = SSL_SERVERINFOV1;
  4767. if ((tst & 0x02) == 0x02) {
  4768. sibuf = serverinfov2;
  4769. sibuflen = sizeof(serverinfov2);
  4770. expected = (version == SSL_SERVERINFOV2);
  4771. } else {
  4772. sibuf = serverinfov1;
  4773. sibuflen = sizeof(serverinfov1);
  4774. expected = (version == SSL_SERVERINFOV1);
  4775. }
  4776. if ((tst & 0x04) == 0x04) {
  4777. ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen);
  4778. } else {
  4779. ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen);
  4780. /*
  4781. * The version variable is irrelevant in this case - it's what is in the
  4782. * buffer that matters
  4783. */
  4784. if ((tst & 0x02) == 0x02)
  4785. expected = 0;
  4786. else
  4787. expected = 1;
  4788. }
  4789. if (!TEST_true(ret == expected))
  4790. goto end;
  4791. testresult = 1;
  4792. end:
  4793. SSL_CTX_free(ctx);
  4794. return testresult;
  4795. }
  4796. /*
  4797. * Test that SSL_export_keying_material() produces expected results. There are
  4798. * no test vectors so all we do is test that both sides of the communication
  4799. * produce the same results for different protocol versions.
  4800. */
  4801. #define SMALL_LABEL_LEN 10
  4802. #define LONG_LABEL_LEN 249
  4803. static int test_export_key_mat(int tst)
  4804. {
  4805. int testresult = 0;
  4806. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  4807. SSL *clientssl = NULL, *serverssl = NULL;
  4808. const char label[LONG_LABEL_LEN + 1] = "test label";
  4809. const unsigned char context[] = "context";
  4810. const unsigned char *emptycontext = NULL;
  4811. unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
  4812. unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
  4813. size_t labellen;
  4814. const int protocols[] = {
  4815. TLS1_VERSION,
  4816. TLS1_1_VERSION,
  4817. TLS1_2_VERSION,
  4818. TLS1_3_VERSION,
  4819. TLS1_3_VERSION,
  4820. TLS1_3_VERSION
  4821. };
  4822. #ifdef OPENSSL_NO_TLS1
  4823. if (tst == 0)
  4824. return 1;
  4825. #endif
  4826. #ifdef OPENSSL_NO_TLS1_1
  4827. if (tst == 1)
  4828. return 1;
  4829. #endif
  4830. if (is_fips && (tst == 0 || tst == 1))
  4831. return 1;
  4832. #ifdef OPENSSL_NO_TLS1_2
  4833. if (tst == 2)
  4834. return 1;
  4835. #endif
  4836. #ifdef OSSL_NO_USABLE_TLS1_3
  4837. if (tst >= 3)
  4838. return 1;
  4839. #endif
  4840. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4841. TLS_client_method(), TLS1_VERSION, 0,
  4842. &sctx, &cctx, cert, privkey)))
  4843. goto end;
  4844. OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
  4845. SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
  4846. SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
  4847. if ((protocols[tst] < TLS1_2_VERSION) &&
  4848. (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
  4849. || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
  4850. goto end;
  4851. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  4852. NULL)))
  4853. goto end;
  4854. /*
  4855. * Premature call of SSL_export_keying_material should just fail.
  4856. */
  4857. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  4858. sizeof(ckeymat1), label,
  4859. SMALL_LABEL_LEN + 1, context,
  4860. sizeof(context) - 1, 1), 0))
  4861. goto end;
  4862. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4863. SSL_ERROR_NONE)))
  4864. goto end;
  4865. if (tst == 5) {
  4866. /*
  4867. * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
  4868. * go over that.
  4869. */
  4870. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  4871. sizeof(ckeymat1), label,
  4872. LONG_LABEL_LEN + 1, context,
  4873. sizeof(context) - 1, 1), 0))
  4874. goto end;
  4875. testresult = 1;
  4876. goto end;
  4877. } else if (tst == 4) {
  4878. labellen = LONG_LABEL_LEN;
  4879. } else {
  4880. labellen = SMALL_LABEL_LEN;
  4881. }
  4882. if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
  4883. sizeof(ckeymat1), label,
  4884. labellen, context,
  4885. sizeof(context) - 1, 1), 1)
  4886. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
  4887. sizeof(ckeymat2), label,
  4888. labellen,
  4889. emptycontext,
  4890. 0, 1), 1)
  4891. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
  4892. sizeof(ckeymat3), label,
  4893. labellen,
  4894. NULL, 0, 0), 1)
  4895. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
  4896. sizeof(skeymat1), label,
  4897. labellen,
  4898. context,
  4899. sizeof(context) -1, 1),
  4900. 1)
  4901. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
  4902. sizeof(skeymat2), label,
  4903. labellen,
  4904. emptycontext,
  4905. 0, 1), 1)
  4906. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
  4907. sizeof(skeymat3), label,
  4908. labellen,
  4909. NULL, 0, 0), 1)
  4910. /*
  4911. * Check that both sides created the same key material with the
  4912. * same context.
  4913. */
  4914. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  4915. sizeof(skeymat1))
  4916. /*
  4917. * Check that both sides created the same key material with an
  4918. * empty context.
  4919. */
  4920. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  4921. sizeof(skeymat2))
  4922. /*
  4923. * Check that both sides created the same key material without a
  4924. * context.
  4925. */
  4926. || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
  4927. sizeof(skeymat3))
  4928. /* Different contexts should produce different results */
  4929. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  4930. sizeof(ckeymat2)))
  4931. goto end;
  4932. /*
  4933. * Check that an empty context and no context produce different results in
  4934. * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
  4935. */
  4936. if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
  4937. sizeof(ckeymat3)))
  4938. || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
  4939. sizeof(ckeymat3))))
  4940. goto end;
  4941. testresult = 1;
  4942. end:
  4943. SSL_free(serverssl);
  4944. SSL_free(clientssl);
  4945. SSL_CTX_free(sctx2);
  4946. SSL_CTX_free(sctx);
  4947. SSL_CTX_free(cctx);
  4948. return testresult;
  4949. }
  4950. #ifndef OSSL_NO_USABLE_TLS1_3
  4951. /*
  4952. * Test that SSL_export_keying_material_early() produces expected
  4953. * results. There are no test vectors so all we do is test that both
  4954. * sides of the communication produce the same results for different
  4955. * protocol versions.
  4956. */
  4957. static int test_export_key_mat_early(int idx)
  4958. {
  4959. static const char label[] = "test label";
  4960. static const unsigned char context[] = "context";
  4961. int testresult = 0;
  4962. SSL_CTX *cctx = NULL, *sctx = NULL;
  4963. SSL *clientssl = NULL, *serverssl = NULL;
  4964. SSL_SESSION *sess = NULL;
  4965. const unsigned char *emptycontext = NULL;
  4966. unsigned char ckeymat1[80], ckeymat2[80];
  4967. unsigned char skeymat1[80], skeymat2[80];
  4968. unsigned char buf[1];
  4969. size_t readbytes, written;
  4970. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
  4971. &sess, idx)))
  4972. goto end;
  4973. /* Here writing 0 length early data is enough. */
  4974. if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  4975. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  4976. &readbytes),
  4977. SSL_READ_EARLY_DATA_ERROR)
  4978. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  4979. SSL_EARLY_DATA_ACCEPTED))
  4980. goto end;
  4981. if (!TEST_int_eq(SSL_export_keying_material_early(
  4982. clientssl, ckeymat1, sizeof(ckeymat1), label,
  4983. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  4984. || !TEST_int_eq(SSL_export_keying_material_early(
  4985. clientssl, ckeymat2, sizeof(ckeymat2), label,
  4986. sizeof(label) - 1, emptycontext, 0), 1)
  4987. || !TEST_int_eq(SSL_export_keying_material_early(
  4988. serverssl, skeymat1, sizeof(skeymat1), label,
  4989. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  4990. || !TEST_int_eq(SSL_export_keying_material_early(
  4991. serverssl, skeymat2, sizeof(skeymat2), label,
  4992. sizeof(label) - 1, emptycontext, 0), 1)
  4993. /*
  4994. * Check that both sides created the same key material with the
  4995. * same context.
  4996. */
  4997. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  4998. sizeof(skeymat1))
  4999. /*
  5000. * Check that both sides created the same key material with an
  5001. * empty context.
  5002. */
  5003. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5004. sizeof(skeymat2))
  5005. /* Different contexts should produce different results */
  5006. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5007. sizeof(ckeymat2)))
  5008. goto end;
  5009. testresult = 1;
  5010. end:
  5011. SSL_SESSION_free(sess);
  5012. SSL_SESSION_free(clientpsk);
  5013. SSL_SESSION_free(serverpsk);
  5014. clientpsk = serverpsk = NULL;
  5015. SSL_free(serverssl);
  5016. SSL_free(clientssl);
  5017. SSL_CTX_free(sctx);
  5018. SSL_CTX_free(cctx);
  5019. return testresult;
  5020. }
  5021. #define NUM_KEY_UPDATE_MESSAGES 40
  5022. /*
  5023. * Test KeyUpdate.
  5024. */
  5025. static int test_key_update(void)
  5026. {
  5027. SSL_CTX *cctx = NULL, *sctx = NULL;
  5028. SSL *clientssl = NULL, *serverssl = NULL;
  5029. int testresult = 0, i, j;
  5030. char buf[20];
  5031. static char *mess = "A test message";
  5032. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5033. TLS_client_method(),
  5034. TLS1_3_VERSION,
  5035. 0,
  5036. &sctx, &cctx, cert, privkey))
  5037. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5038. NULL, NULL))
  5039. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5040. SSL_ERROR_NONE)))
  5041. goto end;
  5042. for (j = 0; j < 2; j++) {
  5043. /* Send lots of KeyUpdate messages */
  5044. for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
  5045. if (!TEST_true(SSL_key_update(clientssl,
  5046. (j == 0)
  5047. ? SSL_KEY_UPDATE_NOT_REQUESTED
  5048. : SSL_KEY_UPDATE_REQUESTED))
  5049. || !TEST_true(SSL_do_handshake(clientssl)))
  5050. goto end;
  5051. }
  5052. /* Check that sending and receiving app data is ok */
  5053. if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
  5054. || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
  5055. strlen(mess)))
  5056. goto end;
  5057. if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
  5058. || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
  5059. strlen(mess)))
  5060. goto end;
  5061. }
  5062. testresult = 1;
  5063. end:
  5064. SSL_free(serverssl);
  5065. SSL_free(clientssl);
  5066. SSL_CTX_free(sctx);
  5067. SSL_CTX_free(cctx);
  5068. return testresult;
  5069. }
  5070. /*
  5071. * Test we can handle a KeyUpdate (update requested) message while write data
  5072. * is pending.
  5073. * Test 0: Client sends KeyUpdate while Server is writing
  5074. * Test 1: Server sends KeyUpdate while Client is writing
  5075. */
  5076. static int test_key_update_in_write(int tst)
  5077. {
  5078. SSL_CTX *cctx = NULL, *sctx = NULL;
  5079. SSL *clientssl = NULL, *serverssl = NULL;
  5080. int testresult = 0;
  5081. char buf[20];
  5082. static char *mess = "A test message";
  5083. BIO *bretry = BIO_new(bio_s_always_retry());
  5084. BIO *tmp = NULL;
  5085. SSL *peerupdate = NULL, *peerwrite = NULL;
  5086. if (!TEST_ptr(bretry)
  5087. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5088. TLS_client_method(),
  5089. TLS1_3_VERSION,
  5090. 0,
  5091. &sctx, &cctx, cert, privkey))
  5092. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5093. NULL, NULL))
  5094. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5095. SSL_ERROR_NONE)))
  5096. goto end;
  5097. peerupdate = tst == 0 ? clientssl : serverssl;
  5098. peerwrite = tst == 0 ? serverssl : clientssl;
  5099. if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
  5100. || !TEST_true(SSL_do_handshake(peerupdate)))
  5101. goto end;
  5102. /* Swap the writing endpoint's write BIO to force a retry */
  5103. tmp = SSL_get_wbio(peerwrite);
  5104. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  5105. tmp = NULL;
  5106. goto end;
  5107. }
  5108. SSL_set0_wbio(peerwrite, bretry);
  5109. bretry = NULL;
  5110. /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
  5111. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
  5112. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
  5113. goto end;
  5114. /* Reinstate the original writing endpoint's write BIO */
  5115. SSL_set0_wbio(peerwrite, tmp);
  5116. tmp = NULL;
  5117. /* Now read some data - we will read the key update */
  5118. if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
  5119. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
  5120. goto end;
  5121. /*
  5122. * Complete the write we started previously and read it from the other
  5123. * endpoint
  5124. */
  5125. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5126. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5127. goto end;
  5128. /* Write more data to ensure we send the KeyUpdate message back */
  5129. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5130. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5131. goto end;
  5132. testresult = 1;
  5133. end:
  5134. SSL_free(serverssl);
  5135. SSL_free(clientssl);
  5136. SSL_CTX_free(sctx);
  5137. SSL_CTX_free(cctx);
  5138. BIO_free(bretry);
  5139. BIO_free(tmp);
  5140. return testresult;
  5141. }
  5142. #endif /* OSSL_NO_USABLE_TLS1_3 */
  5143. static int test_ssl_clear(int idx)
  5144. {
  5145. SSL_CTX *cctx = NULL, *sctx = NULL;
  5146. SSL *clientssl = NULL, *serverssl = NULL;
  5147. int testresult = 0;
  5148. #ifdef OPENSSL_NO_TLS1_2
  5149. if (idx == 1)
  5150. return 1;
  5151. #endif
  5152. /* Create an initial connection */
  5153. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5154. TLS_client_method(), TLS1_VERSION, 0,
  5155. &sctx, &cctx, cert, privkey))
  5156. || (idx == 1
  5157. && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
  5158. TLS1_2_VERSION)))
  5159. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5160. &clientssl, NULL, NULL))
  5161. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5162. SSL_ERROR_NONE)))
  5163. goto end;
  5164. SSL_shutdown(clientssl);
  5165. SSL_shutdown(serverssl);
  5166. SSL_free(serverssl);
  5167. serverssl = NULL;
  5168. /* Clear clientssl - we're going to reuse the object */
  5169. if (!TEST_true(SSL_clear(clientssl)))
  5170. goto end;
  5171. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5172. NULL, NULL))
  5173. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5174. SSL_ERROR_NONE))
  5175. || !TEST_true(SSL_session_reused(clientssl)))
  5176. goto end;
  5177. SSL_shutdown(clientssl);
  5178. SSL_shutdown(serverssl);
  5179. testresult = 1;
  5180. end:
  5181. SSL_free(serverssl);
  5182. SSL_free(clientssl);
  5183. SSL_CTX_free(sctx);
  5184. SSL_CTX_free(cctx);
  5185. return testresult;
  5186. }
  5187. /* Parse CH and retrieve any MFL extension value if present */
  5188. static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
  5189. {
  5190. long len;
  5191. unsigned char *data;
  5192. PACKET pkt, pkt2, pkt3;
  5193. unsigned int MFL_code = 0, type = 0;
  5194. if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
  5195. goto end;
  5196. memset(&pkt, 0, sizeof(pkt));
  5197. memset(&pkt2, 0, sizeof(pkt2));
  5198. memset(&pkt3, 0, sizeof(pkt3));
  5199. if (!TEST_long_gt(len, 0)
  5200. || !TEST_true( PACKET_buf_init( &pkt, data, len ) )
  5201. /* Skip the record header */
  5202. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
  5203. /* Skip the handshake message header */
  5204. || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  5205. /* Skip client version and random */
  5206. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  5207. + SSL3_RANDOM_SIZE))
  5208. /* Skip session id */
  5209. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  5210. /* Skip ciphers */
  5211. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  5212. /* Skip compression */
  5213. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  5214. /* Extensions len */
  5215. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  5216. goto end;
  5217. /* Loop through all extensions */
  5218. while (PACKET_remaining(&pkt2)) {
  5219. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  5220. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  5221. goto end;
  5222. if (type == TLSEXT_TYPE_max_fragment_length) {
  5223. if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
  5224. || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
  5225. goto end;
  5226. *mfl_codemfl_code = MFL_code;
  5227. return 1;
  5228. }
  5229. }
  5230. end:
  5231. return 0;
  5232. }
  5233. /* Maximum-Fragment-Length TLS extension mode to test */
  5234. static const unsigned char max_fragment_len_test[] = {
  5235. TLSEXT_max_fragment_length_512,
  5236. TLSEXT_max_fragment_length_1024,
  5237. TLSEXT_max_fragment_length_2048,
  5238. TLSEXT_max_fragment_length_4096
  5239. };
  5240. static int test_max_fragment_len_ext(int idx_tst)
  5241. {
  5242. SSL_CTX *ctx = NULL;
  5243. SSL *con = NULL;
  5244. int testresult = 0, MFL_mode = 0;
  5245. BIO *rbio, *wbio;
  5246. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
  5247. TLS1_VERSION, 0, NULL, &ctx, NULL,
  5248. NULL)))
  5249. return 0;
  5250. if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
  5251. ctx, max_fragment_len_test[idx_tst])))
  5252. goto end;
  5253. con = SSL_new(ctx);
  5254. if (!TEST_ptr(con))
  5255. goto end;
  5256. rbio = BIO_new(BIO_s_mem());
  5257. wbio = BIO_new(BIO_s_mem());
  5258. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  5259. BIO_free(rbio);
  5260. BIO_free(wbio);
  5261. goto end;
  5262. }
  5263. SSL_set_bio(con, rbio, wbio);
  5264. if (!TEST_int_le(SSL_connect(con), 0)) {
  5265. /* This shouldn't succeed because we don't have a server! */
  5266. goto end;
  5267. }
  5268. if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
  5269. /* no MFL in client hello */
  5270. goto end;
  5271. if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
  5272. goto end;
  5273. testresult = 1;
  5274. end:
  5275. SSL_free(con);
  5276. SSL_CTX_free(ctx);
  5277. return testresult;
  5278. }
  5279. #ifndef OSSL_NO_USABLE_TLS1_3
  5280. static int test_pha_key_update(void)
  5281. {
  5282. SSL_CTX *cctx = NULL, *sctx = NULL;
  5283. SSL *clientssl = NULL, *serverssl = NULL;
  5284. int testresult = 0;
  5285. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5286. TLS_client_method(), TLS1_VERSION, 0,
  5287. &sctx, &cctx, cert, privkey)))
  5288. return 0;
  5289. if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
  5290. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
  5291. || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
  5292. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
  5293. goto end;
  5294. SSL_CTX_set_post_handshake_auth(cctx, 1);
  5295. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5296. NULL, NULL)))
  5297. goto end;
  5298. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5299. SSL_ERROR_NONE)))
  5300. goto end;
  5301. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  5302. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  5303. goto end;
  5304. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  5305. goto end;
  5306. /* Start handshake on the server */
  5307. if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
  5308. goto end;
  5309. /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
  5310. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5311. SSL_ERROR_NONE)))
  5312. goto end;
  5313. SSL_shutdown(clientssl);
  5314. SSL_shutdown(serverssl);
  5315. testresult = 1;
  5316. end:
  5317. SSL_free(serverssl);
  5318. SSL_free(clientssl);
  5319. SSL_CTX_free(sctx);
  5320. SSL_CTX_free(cctx);
  5321. return testresult;
  5322. }
  5323. #endif
  5324. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  5325. static SRP_VBASE *vbase = NULL;
  5326. static int ssl_srp_cb(SSL *s, int *ad, void *arg)
  5327. {
  5328. int ret = SSL3_AL_FATAL;
  5329. char *username;
  5330. SRP_user_pwd *user = NULL;
  5331. username = SSL_get_srp_username(s);
  5332. if (username == NULL) {
  5333. *ad = SSL_AD_INTERNAL_ERROR;
  5334. goto err;
  5335. }
  5336. user = SRP_VBASE_get1_by_user(vbase, username);
  5337. if (user == NULL) {
  5338. *ad = SSL_AD_INTERNAL_ERROR;
  5339. goto err;
  5340. }
  5341. if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
  5342. user->info) <= 0) {
  5343. *ad = SSL_AD_INTERNAL_ERROR;
  5344. goto err;
  5345. }
  5346. ret = 0;
  5347. err:
  5348. SRP_user_pwd_free(user);
  5349. return ret;
  5350. }
  5351. static int create_new_vfile(char *userid, char *password, const char *filename)
  5352. {
  5353. char *gNid = NULL;
  5354. OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
  5355. TXT_DB *db = NULL;
  5356. int ret = 0;
  5357. BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
  5358. size_t i;
  5359. if (!TEST_ptr(dummy) || !TEST_ptr(row))
  5360. goto end;
  5361. gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
  5362. &row[DB_srpverifier], NULL, NULL, libctx, NULL);
  5363. if (!TEST_ptr(gNid))
  5364. goto end;
  5365. /*
  5366. * The only way to create an empty TXT_DB is to provide a BIO with no data
  5367. * in it!
  5368. */
  5369. db = TXT_DB_read(dummy, DB_NUMBER);
  5370. if (!TEST_ptr(db))
  5371. goto end;
  5372. out = BIO_new_file(filename, "w");
  5373. if (!TEST_ptr(out))
  5374. goto end;
  5375. row[DB_srpid] = OPENSSL_strdup(userid);
  5376. row[DB_srptype] = OPENSSL_strdup("V");
  5377. row[DB_srpgN] = OPENSSL_strdup(gNid);
  5378. if (!TEST_ptr(row[DB_srpid])
  5379. || !TEST_ptr(row[DB_srptype])
  5380. || !TEST_ptr(row[DB_srpgN])
  5381. || !TEST_true(TXT_DB_insert(db, row)))
  5382. goto end;
  5383. row = NULL;
  5384. if (!TXT_DB_write(out, db))
  5385. goto end;
  5386. ret = 1;
  5387. end:
  5388. if (row != NULL) {
  5389. for (i = 0; i < DB_NUMBER; i++)
  5390. OPENSSL_free(row[i]);
  5391. }
  5392. OPENSSL_free(row);
  5393. BIO_free(dummy);
  5394. BIO_free(out);
  5395. TXT_DB_free(db);
  5396. return ret;
  5397. }
  5398. static int create_new_vbase(char *userid, char *password)
  5399. {
  5400. BIGNUM *verifier = NULL, *salt = NULL;
  5401. const SRP_gN *lgN = NULL;
  5402. SRP_user_pwd *user_pwd = NULL;
  5403. int ret = 0;
  5404. lgN = SRP_get_default_gN(NULL);
  5405. if (!TEST_ptr(lgN))
  5406. goto end;
  5407. if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
  5408. lgN->N, lgN->g, libctx, NULL)))
  5409. goto end;
  5410. user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
  5411. if (!TEST_ptr(user_pwd))
  5412. goto end;
  5413. user_pwd->N = lgN->N;
  5414. user_pwd->g = lgN->g;
  5415. user_pwd->id = OPENSSL_strdup(userid);
  5416. if (!TEST_ptr(user_pwd->id))
  5417. goto end;
  5418. user_pwd->v = verifier;
  5419. user_pwd->s = salt;
  5420. verifier = salt = NULL;
  5421. if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
  5422. goto end;
  5423. user_pwd = NULL;
  5424. ret = 1;
  5425. end:
  5426. SRP_user_pwd_free(user_pwd);
  5427. BN_free(salt);
  5428. BN_free(verifier);
  5429. return ret;
  5430. }
  5431. /*
  5432. * SRP tests
  5433. *
  5434. * Test 0: Simple successful SRP connection, new vbase
  5435. * Test 1: Connection failure due to bad password, new vbase
  5436. * Test 2: Simple successful SRP connection, vbase loaded from existing file
  5437. * Test 3: Connection failure due to bad password, vbase loaded from existing
  5438. * file
  5439. * Test 4: Simple successful SRP connection, vbase loaded from new file
  5440. * Test 5: Connection failure due to bad password, vbase loaded from new file
  5441. */
  5442. static int test_srp(int tst)
  5443. {
  5444. char *userid = "test", *password = "password", *tstsrpfile;
  5445. SSL_CTX *cctx = NULL, *sctx = NULL;
  5446. SSL *clientssl = NULL, *serverssl = NULL;
  5447. int ret, testresult = 0;
  5448. vbase = SRP_VBASE_new(NULL);
  5449. if (!TEST_ptr(vbase))
  5450. goto end;
  5451. if (tst == 0 || tst == 1) {
  5452. if (!TEST_true(create_new_vbase(userid, password)))
  5453. goto end;
  5454. } else {
  5455. if (tst == 4 || tst == 5) {
  5456. if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
  5457. goto end;
  5458. tstsrpfile = tmpfilename;
  5459. } else {
  5460. tstsrpfile = srpvfile;
  5461. }
  5462. if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
  5463. goto end;
  5464. }
  5465. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5466. TLS_client_method(), TLS1_VERSION, 0,
  5467. &sctx, &cctx, cert, privkey)))
  5468. goto end;
  5469. if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
  5470. || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
  5471. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
  5472. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
  5473. || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
  5474. goto end;
  5475. if (tst % 2 == 1) {
  5476. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
  5477. goto end;
  5478. } else {
  5479. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
  5480. goto end;
  5481. }
  5482. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5483. NULL, NULL)))
  5484. goto end;
  5485. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  5486. if (ret) {
  5487. if (!TEST_true(tst % 2 == 0))
  5488. goto end;
  5489. } else {
  5490. if (!TEST_true(tst % 2 == 1))
  5491. goto end;
  5492. }
  5493. testresult = 1;
  5494. end:
  5495. SRP_VBASE_free(vbase);
  5496. vbase = NULL;
  5497. SSL_free(serverssl);
  5498. SSL_free(clientssl);
  5499. SSL_CTX_free(sctx);
  5500. SSL_CTX_free(cctx);
  5501. return testresult;
  5502. }
  5503. #endif
  5504. static int info_cb_failed = 0;
  5505. static int info_cb_offset = 0;
  5506. static int info_cb_this_state = -1;
  5507. static struct info_cb_states_st {
  5508. int where;
  5509. const char *statestr;
  5510. } info_cb_states[][60] = {
  5511. {
  5512. /* TLSv1.2 server followed by resumption */
  5513. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5514. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  5515. {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
  5516. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
  5517. {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
  5518. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  5519. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  5520. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  5521. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"},
  5522. {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  5523. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
  5524. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  5525. {SSL_CB_EXIT, NULL}, {0, NULL},
  5526. }, {
  5527. /* TLSv1.2 client followed by resumption */
  5528. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5529. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  5530. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
  5531. {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
  5532. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
  5533. {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  5534. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  5535. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5536. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  5537. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  5538. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  5539. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
  5540. }, {
  5541. /* TLSv1.3 server followed by resumption */
  5542. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5543. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  5544. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
  5545. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  5546. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  5547. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  5548. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  5549. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5550. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  5551. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  5552. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  5553. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  5554. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  5555. }, {
  5556. /* TLSv1.3 client followed by resumption */
  5557. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5558. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  5559. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
  5560. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  5561. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  5562. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "},
  5563. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "},
  5564. {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  5565. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  5566. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  5567. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  5568. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  5569. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  5570. {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
  5571. {SSL_CB_EXIT, NULL}, {0, NULL},
  5572. }, {
  5573. /* TLSv1.3 server, early_data */
  5574. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5575. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  5576. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  5577. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  5578. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  5579. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
  5580. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  5581. {SSL_CB_EXIT, NULL}, {0, NULL},
  5582. }, {
  5583. /* TLSv1.3 client, early_data */
  5584. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  5585. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
  5586. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  5587. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  5588. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  5589. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
  5590. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  5591. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "},
  5592. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  5593. }, {
  5594. {0, NULL},
  5595. }
  5596. };
  5597. static void sslapi_info_callback(const SSL *s, int where, int ret)
  5598. {
  5599. struct info_cb_states_st *state = info_cb_states[info_cb_offset];
  5600. /* We do not ever expect a connection to fail in this test */
  5601. if (!TEST_false(ret == 0)) {
  5602. info_cb_failed = 1;
  5603. return;
  5604. }
  5605. /*
  5606. * Do some sanity checks. We never expect these things to happen in this
  5607. * test
  5608. */
  5609. if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
  5610. || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
  5611. || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
  5612. info_cb_failed = 1;
  5613. return;
  5614. }
  5615. /* Now check we're in the right state */
  5616. if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
  5617. info_cb_failed = 1;
  5618. return;
  5619. }
  5620. if ((where & SSL_CB_LOOP) != 0
  5621. && !TEST_int_eq(strcmp(SSL_state_string(s),
  5622. state[info_cb_this_state].statestr), 0)) {
  5623. info_cb_failed = 1;
  5624. return;
  5625. }
  5626. /*
  5627. * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
  5628. */
  5629. if ((where & SSL_CB_HANDSHAKE_DONE)
  5630. && SSL_in_init((SSL *)s) != 0) {
  5631. info_cb_failed = 1;
  5632. return;
  5633. }
  5634. }
  5635. /*
  5636. * Test the info callback gets called when we expect it to.
  5637. *
  5638. * Test 0: TLSv1.2, server
  5639. * Test 1: TLSv1.2, client
  5640. * Test 2: TLSv1.3, server
  5641. * Test 3: TLSv1.3, client
  5642. * Test 4: TLSv1.3, server, early_data
  5643. * Test 5: TLSv1.3, client, early_data
  5644. */
  5645. static int test_info_callback(int tst)
  5646. {
  5647. SSL_CTX *cctx = NULL, *sctx = NULL;
  5648. SSL *clientssl = NULL, *serverssl = NULL;
  5649. SSL_SESSION *clntsess = NULL;
  5650. int testresult = 0;
  5651. int tlsvers;
  5652. if (tst < 2) {
  5653. /* We need either ECDHE or DHE for the TLSv1.2 test to work */
  5654. #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
  5655. || !defined(OPENSSL_NO_DH))
  5656. tlsvers = TLS1_2_VERSION;
  5657. #else
  5658. return 1;
  5659. #endif
  5660. } else {
  5661. #ifndef OSSL_NO_USABLE_TLS1_3
  5662. tlsvers = TLS1_3_VERSION;
  5663. #else
  5664. return 1;
  5665. #endif
  5666. }
  5667. /* Reset globals */
  5668. info_cb_failed = 0;
  5669. info_cb_this_state = -1;
  5670. info_cb_offset = tst;
  5671. #ifndef OSSL_NO_USABLE_TLS1_3
  5672. if (tst >= 4) {
  5673. SSL_SESSION *sess = NULL;
  5674. size_t written, readbytes;
  5675. unsigned char buf[80];
  5676. /* early_data tests */
  5677. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  5678. &serverssl, &sess, 0)))
  5679. goto end;
  5680. /* We don't actually need this reference */
  5681. SSL_SESSION_free(sess);
  5682. SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
  5683. sslapi_info_callback);
  5684. /* Write and read some early data and then complete the connection */
  5685. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  5686. &written))
  5687. || !TEST_size_t_eq(written, strlen(MSG1))
  5688. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  5689. sizeof(buf), &readbytes),
  5690. SSL_READ_EARLY_DATA_SUCCESS)
  5691. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  5692. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  5693. SSL_EARLY_DATA_ACCEPTED)
  5694. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5695. SSL_ERROR_NONE))
  5696. || !TEST_false(info_cb_failed))
  5697. goto end;
  5698. testresult = 1;
  5699. goto end;
  5700. }
  5701. #endif
  5702. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5703. TLS_client_method(),
  5704. tlsvers, tlsvers, &sctx, &cctx, cert,
  5705. privkey)))
  5706. goto end;
  5707. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  5708. goto end;
  5709. /*
  5710. * For even numbered tests we check the server callbacks. For odd numbers we
  5711. * check the client.
  5712. */
  5713. SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
  5714. sslapi_info_callback);
  5715. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5716. &clientssl, NULL, NULL))
  5717. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5718. SSL_ERROR_NONE))
  5719. || !TEST_false(info_cb_failed))
  5720. goto end;
  5721. clntsess = SSL_get1_session(clientssl);
  5722. SSL_shutdown(clientssl);
  5723. SSL_shutdown(serverssl);
  5724. SSL_free(serverssl);
  5725. SSL_free(clientssl);
  5726. serverssl = clientssl = NULL;
  5727. /* Now do a resumption */
  5728. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  5729. NULL))
  5730. || !TEST_true(SSL_set_session(clientssl, clntsess))
  5731. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5732. SSL_ERROR_NONE))
  5733. || !TEST_true(SSL_session_reused(clientssl))
  5734. || !TEST_false(info_cb_failed))
  5735. goto end;
  5736. testresult = 1;
  5737. end:
  5738. SSL_free(serverssl);
  5739. SSL_free(clientssl);
  5740. SSL_SESSION_free(clntsess);
  5741. SSL_CTX_free(sctx);
  5742. SSL_CTX_free(cctx);
  5743. return testresult;
  5744. }
  5745. static int test_ssl_pending(int tst)
  5746. {
  5747. SSL_CTX *cctx = NULL, *sctx = NULL;
  5748. SSL *clientssl = NULL, *serverssl = NULL;
  5749. int testresult = 0;
  5750. char msg[] = "A test message";
  5751. char buf[5];
  5752. size_t written, readbytes;
  5753. if (tst == 0) {
  5754. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5755. TLS_client_method(),
  5756. TLS1_VERSION, 0,
  5757. &sctx, &cctx, cert, privkey)))
  5758. goto end;
  5759. } else {
  5760. #ifndef OPENSSL_NO_DTLS
  5761. if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
  5762. DTLS_client_method(),
  5763. DTLS1_VERSION, 0,
  5764. &sctx, &cctx, cert, privkey)))
  5765. goto end;
  5766. #else
  5767. return 1;
  5768. #endif
  5769. }
  5770. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5771. NULL, NULL))
  5772. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5773. SSL_ERROR_NONE)))
  5774. goto end;
  5775. if (!TEST_int_eq(SSL_pending(clientssl), 0)
  5776. || !TEST_false(SSL_has_pending(clientssl))
  5777. || !TEST_int_eq(SSL_pending(serverssl), 0)
  5778. || !TEST_false(SSL_has_pending(serverssl))
  5779. || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  5780. || !TEST_size_t_eq(written, sizeof(msg))
  5781. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  5782. || !TEST_size_t_eq(readbytes, sizeof(buf))
  5783. || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
  5784. || !TEST_true(SSL_has_pending(clientssl)))
  5785. goto end;
  5786. testresult = 1;
  5787. end:
  5788. SSL_free(serverssl);
  5789. SSL_free(clientssl);
  5790. SSL_CTX_free(sctx);
  5791. SSL_CTX_free(cctx);
  5792. return testresult;
  5793. }
  5794. static struct {
  5795. unsigned int maxprot;
  5796. const char *clntciphers;
  5797. const char *clnttls13ciphers;
  5798. const char *srvrciphers;
  5799. const char *srvrtls13ciphers;
  5800. const char *shared;
  5801. const char *fipsshared;
  5802. } shared_ciphers_data[] = {
  5803. /*
  5804. * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
  5805. * TLSv1.3 is enabled but TLSv1.2 is disabled.
  5806. */
  5807. #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  5808. {
  5809. TLS1_2_VERSION,
  5810. "AES128-SHA:AES256-SHA",
  5811. NULL,
  5812. "AES256-SHA:DHE-RSA-AES128-SHA",
  5813. NULL,
  5814. "AES256-SHA",
  5815. "AES256-SHA"
  5816. },
  5817. # if !defined(OPENSSL_NO_CHACHA) \
  5818. && !defined(OPENSSL_NO_POLY1305) \
  5819. && !defined(OPENSSL_NO_EC)
  5820. {
  5821. TLS1_2_VERSION,
  5822. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  5823. NULL,
  5824. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  5825. NULL,
  5826. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  5827. "AES128-SHA"
  5828. },
  5829. # endif
  5830. {
  5831. TLS1_2_VERSION,
  5832. "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
  5833. NULL,
  5834. "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
  5835. NULL,
  5836. "AES128-SHA:AES256-SHA",
  5837. "AES128-SHA:AES256-SHA"
  5838. },
  5839. {
  5840. TLS1_2_VERSION,
  5841. "AES128-SHA:AES256-SHA",
  5842. NULL,
  5843. "AES128-SHA:DHE-RSA-AES128-SHA",
  5844. NULL,
  5845. "AES128-SHA",
  5846. "AES128-SHA"
  5847. },
  5848. #endif
  5849. /*
  5850. * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
  5851. * enabled.
  5852. */
  5853. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
  5854. && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  5855. {
  5856. TLS1_3_VERSION,
  5857. "AES128-SHA:AES256-SHA",
  5858. NULL,
  5859. "AES256-SHA:AES128-SHA256",
  5860. NULL,
  5861. "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
  5862. "TLS_AES_128_GCM_SHA256:AES256-SHA",
  5863. "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
  5864. },
  5865. #endif
  5866. #ifndef OSSL_NO_USABLE_TLS1_3
  5867. {
  5868. TLS1_3_VERSION,
  5869. "AES128-SHA",
  5870. "TLS_AES_256_GCM_SHA384",
  5871. "AES256-SHA",
  5872. "TLS_AES_256_GCM_SHA384",
  5873. "TLS_AES_256_GCM_SHA384",
  5874. "TLS_AES_256_GCM_SHA384"
  5875. },
  5876. #endif
  5877. };
  5878. static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
  5879. {
  5880. SSL_CTX *cctx = NULL, *sctx = NULL;
  5881. SSL *clientssl = NULL, *serverssl = NULL;
  5882. int testresult = 0;
  5883. char buf[1024];
  5884. OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
  5885. if (!TEST_ptr(tmplibctx))
  5886. goto end;
  5887. /*
  5888. * Regardless of whether we're testing with the FIPS provider loaded into
  5889. * libctx, we want one peer to always use the full set of ciphersuites
  5890. * available. Therefore we use a separate libctx with the default provider
  5891. * loaded into it. We run the same tests twice - once with the client side
  5892. * having the full set of ciphersuites and once with the server side.
  5893. */
  5894. if (clnt) {
  5895. cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
  5896. if (!TEST_ptr(cctx))
  5897. goto end;
  5898. } else {
  5899. sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
  5900. if (!TEST_ptr(sctx))
  5901. goto end;
  5902. }
  5903. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5904. TLS_client_method(),
  5905. TLS1_VERSION,
  5906. shared_ciphers_data[tst].maxprot,
  5907. &sctx, &cctx, cert, privkey)))
  5908. goto end;
  5909. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  5910. shared_ciphers_data[tst].clntciphers))
  5911. || (shared_ciphers_data[tst].clnttls13ciphers != NULL
  5912. && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  5913. shared_ciphers_data[tst].clnttls13ciphers)))
  5914. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  5915. shared_ciphers_data[tst].srvrciphers))
  5916. || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
  5917. && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  5918. shared_ciphers_data[tst].srvrtls13ciphers))))
  5919. goto end;
  5920. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5921. NULL, NULL))
  5922. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5923. SSL_ERROR_NONE)))
  5924. goto end;
  5925. if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
  5926. || !TEST_int_eq(strcmp(buf,
  5927. is_fips
  5928. ? shared_ciphers_data[tst].fipsshared
  5929. : shared_ciphers_data[tst].shared),
  5930. 0)) {
  5931. TEST_info("Shared ciphers are: %s\n", buf);
  5932. goto end;
  5933. }
  5934. testresult = 1;
  5935. end:
  5936. SSL_free(serverssl);
  5937. SSL_free(clientssl);
  5938. SSL_CTX_free(sctx);
  5939. SSL_CTX_free(cctx);
  5940. OSSL_LIB_CTX_free(tmplibctx);
  5941. return testresult;
  5942. }
  5943. static int test_ssl_get_shared_ciphers(int tst)
  5944. {
  5945. return int_test_ssl_get_shared_ciphers(tst, 0)
  5946. && int_test_ssl_get_shared_ciphers(tst, 1);
  5947. }
  5948. static const char *appdata = "Hello World";
  5949. static int gen_tick_called, dec_tick_called, tick_key_cb_called;
  5950. static int tick_key_renew = 0;
  5951. static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  5952. static int gen_tick_cb(SSL *s, void *arg)
  5953. {
  5954. gen_tick_called = 1;
  5955. return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
  5956. strlen(appdata));
  5957. }
  5958. static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
  5959. const unsigned char *keyname,
  5960. size_t keyname_length,
  5961. SSL_TICKET_STATUS status,
  5962. void *arg)
  5963. {
  5964. void *tickdata;
  5965. size_t tickdlen;
  5966. dec_tick_called = 1;
  5967. if (status == SSL_TICKET_EMPTY)
  5968. return SSL_TICKET_RETURN_IGNORE_RENEW;
  5969. if (!TEST_true(status == SSL_TICKET_SUCCESS
  5970. || status == SSL_TICKET_SUCCESS_RENEW))
  5971. return SSL_TICKET_RETURN_ABORT;
  5972. if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
  5973. &tickdlen))
  5974. || !TEST_size_t_eq(tickdlen, strlen(appdata))
  5975. || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
  5976. return SSL_TICKET_RETURN_ABORT;
  5977. if (tick_key_cb_called) {
  5978. /* Don't change what the ticket key callback wanted to do */
  5979. switch (status) {
  5980. case SSL_TICKET_NO_DECRYPT:
  5981. return SSL_TICKET_RETURN_IGNORE_RENEW;
  5982. case SSL_TICKET_SUCCESS:
  5983. return SSL_TICKET_RETURN_USE;
  5984. case SSL_TICKET_SUCCESS_RENEW:
  5985. return SSL_TICKET_RETURN_USE_RENEW;
  5986. default:
  5987. return SSL_TICKET_RETURN_ABORT;
  5988. }
  5989. }
  5990. return tick_dec_ret;
  5991. }
  5992. #ifndef OPENSSL_NO_DEPRECATED_3_0
  5993. static int tick_key_cb(SSL *s, unsigned char key_name[16],
  5994. unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
  5995. HMAC_CTX *hctx, int enc)
  5996. {
  5997. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  5998. const unsigned char tick_hmac_key[16] = "0123456789abcdef";
  5999. EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  6000. EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
  6001. int ret;
  6002. tick_key_cb_called = 1;
  6003. memset(iv, 0, AES_BLOCK_SIZE);
  6004. memset(key_name, 0, 16);
  6005. if (aes128cbc == NULL
  6006. || sha256 == NULL
  6007. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  6008. || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
  6009. NULL))
  6010. ret = -1;
  6011. else
  6012. ret = tick_key_renew ? 2 : 1;
  6013. EVP_CIPHER_free(aes128cbc);
  6014. EVP_MD_free(sha256);
  6015. return ret;
  6016. }
  6017. #endif
  6018. static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
  6019. unsigned char iv[EVP_MAX_IV_LENGTH],
  6020. EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
  6021. {
  6022. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  6023. unsigned char tick_hmac_key[16] = "0123456789abcdef";
  6024. OSSL_PARAM params[2];
  6025. EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  6026. int ret;
  6027. tick_key_cb_called = 1;
  6028. memset(iv, 0, AES_BLOCK_SIZE);
  6029. memset(key_name, 0, 16);
  6030. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  6031. "SHA256", 0);
  6032. params[1] = OSSL_PARAM_construct_end();
  6033. if (aes128cbc == NULL
  6034. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  6035. || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
  6036. params))
  6037. ret = -1;
  6038. else
  6039. ret = tick_key_renew ? 2 : 1;
  6040. EVP_CIPHER_free(aes128cbc);
  6041. return ret;
  6042. }
  6043. /*
  6044. * Test the various ticket callbacks
  6045. * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
  6046. * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
  6047. * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
  6048. * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
  6049. * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
  6050. * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
  6051. * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
  6052. * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
  6053. * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
  6054. * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
  6055. * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
  6056. * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
  6057. * Test 12: TLSv1.2, ticket key callback, ticket, no renewal
  6058. * Test 13: TLSv1.3, ticket key callback, ticket, no renewal
  6059. * Test 14: TLSv1.2, ticket key callback, ticket, renewal
  6060. * Test 15: TLSv1.3, ticket key callback, ticket, renewal
  6061. */
  6062. static int test_ticket_callbacks(int tst)
  6063. {
  6064. SSL_CTX *cctx = NULL, *sctx = NULL;
  6065. SSL *clientssl = NULL, *serverssl = NULL;
  6066. SSL_SESSION *clntsess = NULL;
  6067. int testresult = 0;
  6068. #ifdef OPENSSL_NO_TLS1_2
  6069. if (tst % 2 == 0)
  6070. return 1;
  6071. #endif
  6072. #ifdef OSSL_NO_USABLE_TLS1_3
  6073. if (tst % 2 == 1)
  6074. return 1;
  6075. #endif
  6076. #ifdef OPENSSL_NO_DEPRECATED_3_0
  6077. if (tst >= 8 && tst <= 11)
  6078. return 1;
  6079. #endif
  6080. gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
  6081. /* Which tests the ticket key callback should request renewal for */
  6082. if (tst == 10 || tst == 11 || tst == 14 || tst == 15)
  6083. tick_key_renew = 1;
  6084. else
  6085. tick_key_renew = 0;
  6086. /* Which tests the decrypt ticket callback should request renewal for */
  6087. switch (tst) {
  6088. case 0:
  6089. case 1:
  6090. tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
  6091. break;
  6092. case 2:
  6093. case 3:
  6094. tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
  6095. break;
  6096. case 4:
  6097. case 5:
  6098. tick_dec_ret = SSL_TICKET_RETURN_USE;
  6099. break;
  6100. case 6:
  6101. case 7:
  6102. tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
  6103. break;
  6104. default:
  6105. tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  6106. }
  6107. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6108. TLS_client_method(),
  6109. TLS1_VERSION,
  6110. ((tst % 2) == 0) ? TLS1_2_VERSION
  6111. : TLS1_3_VERSION,
  6112. &sctx, &cctx, cert, privkey)))
  6113. goto end;
  6114. /*
  6115. * We only want sessions to resume from tickets - not the session cache. So
  6116. * switch the cache off.
  6117. */
  6118. if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
  6119. goto end;
  6120. if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
  6121. NULL)))
  6122. goto end;
  6123. if (tst >= 12) {
  6124. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
  6125. goto end;
  6126. #ifndef OPENSSL_NO_DEPRECATED_3_0
  6127. } else if (tst >= 8) {
  6128. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
  6129. goto end;
  6130. #endif
  6131. }
  6132. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6133. NULL, NULL))
  6134. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6135. SSL_ERROR_NONE)))
  6136. goto end;
  6137. /*
  6138. * The decrypt ticket key callback in TLSv1.2 should be called even though
  6139. * we have no ticket yet, because it gets called with a status of
  6140. * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
  6141. * actually send any ticket data). This does not happen in TLSv1.3 because
  6142. * it is not valid to send empty ticket data in TLSv1.3.
  6143. */
  6144. if (!TEST_int_eq(gen_tick_called, 1)
  6145. || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
  6146. goto end;
  6147. gen_tick_called = dec_tick_called = 0;
  6148. clntsess = SSL_get1_session(clientssl);
  6149. SSL_shutdown(clientssl);
  6150. SSL_shutdown(serverssl);
  6151. SSL_free(serverssl);
  6152. SSL_free(clientssl);
  6153. serverssl = clientssl = NULL;
  6154. /* Now do a resumption */
  6155. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  6156. NULL))
  6157. || !TEST_true(SSL_set_session(clientssl, clntsess))
  6158. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6159. SSL_ERROR_NONE)))
  6160. goto end;
  6161. if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
  6162. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) {
  6163. if (!TEST_false(SSL_session_reused(clientssl)))
  6164. goto end;
  6165. } else {
  6166. if (!TEST_true(SSL_session_reused(clientssl)))
  6167. goto end;
  6168. }
  6169. if (!TEST_int_eq(gen_tick_called,
  6170. (tick_key_renew
  6171. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  6172. || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
  6173. ? 1 : 0)
  6174. || !TEST_int_eq(dec_tick_called, 1))
  6175. goto end;
  6176. testresult = 1;
  6177. end:
  6178. SSL_SESSION_free(clntsess);
  6179. SSL_free(serverssl);
  6180. SSL_free(clientssl);
  6181. SSL_CTX_free(sctx);
  6182. SSL_CTX_free(cctx);
  6183. return testresult;
  6184. }
  6185. /*
  6186. * Test incorrect shutdown.
  6187. * Test 0: client does not shutdown properly,
  6188. * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
  6189. * server should get SSL_ERROR_SSL
  6190. * Test 1: client does not shutdown properly,
  6191. * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
  6192. * server should get SSL_ERROR_ZERO_RETURN
  6193. */
  6194. static int test_incorrect_shutdown(int tst)
  6195. {
  6196. SSL_CTX *cctx = NULL, *sctx = NULL;
  6197. SSL *clientssl = NULL, *serverssl = NULL;
  6198. int testresult = 0;
  6199. char buf[80];
  6200. BIO *c2s;
  6201. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6202. TLS_client_method(), 0, 0,
  6203. &sctx, &cctx, cert, privkey)))
  6204. goto end;
  6205. if (tst == 1)
  6206. SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
  6207. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6208. NULL, NULL)))
  6209. goto end;
  6210. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6211. SSL_ERROR_NONE)))
  6212. goto end;
  6213. c2s = SSL_get_rbio(serverssl);
  6214. BIO_set_mem_eof_return(c2s, 0);
  6215. if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
  6216. goto end;
  6217. if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
  6218. goto end;
  6219. if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
  6220. goto end;
  6221. testresult = 1;
  6222. end:
  6223. SSL_free(serverssl);
  6224. SSL_free(clientssl);
  6225. SSL_CTX_free(sctx);
  6226. SSL_CTX_free(cctx);
  6227. return testresult;
  6228. }
  6229. /*
  6230. * Test bi-directional shutdown.
  6231. * Test 0: TLSv1.2
  6232. * Test 1: TLSv1.2, server continues to read/write after client shutdown
  6233. * Test 2: TLSv1.3, no pending NewSessionTicket messages
  6234. * Test 3: TLSv1.3, pending NewSessionTicket messages
  6235. * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
  6236. * sends key update, client reads it
  6237. * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
  6238. * sends CertificateRequest, client reads and ignores it
  6239. * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
  6240. * doesn't read it
  6241. */
  6242. static int test_shutdown(int tst)
  6243. {
  6244. SSL_CTX *cctx = NULL, *sctx = NULL;
  6245. SSL *clientssl = NULL, *serverssl = NULL;
  6246. int testresult = 0;
  6247. char msg[] = "A test message";
  6248. char buf[80];
  6249. size_t written, readbytes;
  6250. SSL_SESSION *sess;
  6251. #ifdef OPENSSL_NO_TLS1_2
  6252. if (tst <= 1)
  6253. return 1;
  6254. #endif
  6255. #ifdef OSSL_NO_USABLE_TLS1_3
  6256. if (tst >= 2)
  6257. return 1;
  6258. #endif
  6259. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6260. TLS_client_method(),
  6261. TLS1_VERSION,
  6262. (tst <= 1) ? TLS1_2_VERSION
  6263. : TLS1_3_VERSION,
  6264. &sctx, &cctx, cert, privkey)))
  6265. goto end;
  6266. if (tst == 5)
  6267. SSL_CTX_set_post_handshake_auth(cctx, 1);
  6268. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6269. NULL, NULL)))
  6270. goto end;
  6271. if (tst == 3) {
  6272. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  6273. SSL_ERROR_NONE, 1))
  6274. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  6275. || !TEST_false(SSL_SESSION_is_resumable(sess)))
  6276. goto end;
  6277. } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6278. SSL_ERROR_NONE))
  6279. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  6280. || !TEST_true(SSL_SESSION_is_resumable(sess))) {
  6281. goto end;
  6282. }
  6283. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  6284. goto end;
  6285. if (tst >= 4) {
  6286. /*
  6287. * Reading on the server after the client has sent close_notify should
  6288. * fail and provide SSL_ERROR_ZERO_RETURN
  6289. */
  6290. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  6291. || !TEST_int_eq(SSL_get_error(serverssl, 0),
  6292. SSL_ERROR_ZERO_RETURN)
  6293. || !TEST_int_eq(SSL_get_shutdown(serverssl),
  6294. SSL_RECEIVED_SHUTDOWN)
  6295. /*
  6296. * Even though we're shutdown on receive we should still be
  6297. * able to write.
  6298. */
  6299. || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  6300. goto end;
  6301. if (tst == 4
  6302. && !TEST_true(SSL_key_update(serverssl,
  6303. SSL_KEY_UPDATE_REQUESTED)))
  6304. goto end;
  6305. if (tst == 5) {
  6306. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  6307. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  6308. goto end;
  6309. }
  6310. if ((tst == 4 || tst == 5)
  6311. && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  6312. goto end;
  6313. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  6314. goto end;
  6315. if (tst == 4 || tst == 5) {
  6316. /* Should still be able to read data from server */
  6317. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  6318. &readbytes))
  6319. || !TEST_size_t_eq(readbytes, sizeof(msg))
  6320. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
  6321. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  6322. &readbytes))
  6323. || !TEST_size_t_eq(readbytes, sizeof(msg))
  6324. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
  6325. goto end;
  6326. }
  6327. }
  6328. /* Writing on the client after sending close_notify shouldn't be possible */
  6329. if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
  6330. goto end;
  6331. if (tst < 4) {
  6332. /*
  6333. * For these tests the client has sent close_notify but it has not yet
  6334. * been received by the server. The server has not sent close_notify
  6335. * yet.
  6336. */
  6337. if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
  6338. /*
  6339. * Writing on the server after sending close_notify shouldn't
  6340. * be possible.
  6341. */
  6342. || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  6343. || !TEST_int_eq(SSL_shutdown(clientssl), 1)
  6344. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  6345. || !TEST_true(SSL_SESSION_is_resumable(sess))
  6346. || !TEST_int_eq(SSL_shutdown(serverssl), 1))
  6347. goto end;
  6348. } else if (tst == 4 || tst == 5) {
  6349. /*
  6350. * In this test the client has sent close_notify and it has been
  6351. * received by the server which has responded with a close_notify. The
  6352. * client needs to read the close_notify sent by the server.
  6353. */
  6354. if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
  6355. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  6356. || !TEST_true(SSL_SESSION_is_resumable(sess)))
  6357. goto end;
  6358. } else {
  6359. /*
  6360. * tst == 6
  6361. *
  6362. * The client has sent close_notify and is expecting a close_notify
  6363. * back, but instead there is application data first. The shutdown
  6364. * should fail with a fatal error.
  6365. */
  6366. if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
  6367. || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
  6368. goto end;
  6369. }
  6370. testresult = 1;
  6371. end:
  6372. SSL_free(serverssl);
  6373. SSL_free(clientssl);
  6374. SSL_CTX_free(sctx);
  6375. SSL_CTX_free(cctx);
  6376. return testresult;
  6377. }
  6378. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  6379. static int cert_cb_cnt;
  6380. static int cert_cb(SSL *s, void *arg)
  6381. {
  6382. SSL_CTX *ctx = (SSL_CTX *)arg;
  6383. BIO *in = NULL;
  6384. EVP_PKEY *pkey = NULL;
  6385. X509 *x509 = NULL, *rootx = NULL;
  6386. STACK_OF(X509) *chain = NULL;
  6387. char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
  6388. int ret = 0;
  6389. if (cert_cb_cnt == 0) {
  6390. /* Suspend the handshake */
  6391. cert_cb_cnt++;
  6392. return -1;
  6393. } else if (cert_cb_cnt == 1) {
  6394. /*
  6395. * Update the SSL_CTX, set the certificate and private key and then
  6396. * continue the handshake normally.
  6397. */
  6398. if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
  6399. return 0;
  6400. if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
  6401. || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
  6402. SSL_FILETYPE_PEM))
  6403. || !TEST_true(SSL_check_private_key(s)))
  6404. return 0;
  6405. cert_cb_cnt++;
  6406. return 1;
  6407. } else if (cert_cb_cnt == 3) {
  6408. int rv;
  6409. rootfile = test_mk_file_path(certsdir, "rootcert.pem");
  6410. ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  6411. ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  6412. if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
  6413. goto out;
  6414. chain = sk_X509_new_null();
  6415. if (!TEST_ptr(chain))
  6416. goto out;
  6417. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  6418. || !TEST_int_ge(BIO_read_filename(in, rootfile), 0)
  6419. || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
  6420. || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
  6421. || !TEST_true(sk_X509_push(chain, rootx)))
  6422. goto out;
  6423. rootx = NULL;
  6424. BIO_free(in);
  6425. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  6426. || !TEST_int_ge(BIO_read_filename(in, ecdsacert), 0)
  6427. || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
  6428. || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
  6429. goto out;
  6430. BIO_free(in);
  6431. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  6432. || !TEST_int_ge(BIO_read_filename(in, ecdsakey), 0)
  6433. || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  6434. NULL, NULL,
  6435. libctx, NULL)))
  6436. goto out;
  6437. rv = SSL_check_chain(s, x509, pkey, chain);
  6438. /*
  6439. * If the cert doesn't show as valid here (e.g., because we don't
  6440. * have any shared sigalgs), then we will not set it, and there will
  6441. * be no certificate at all on the SSL or SSL_CTX. This, in turn,
  6442. * will cause tls_choose_sigalgs() to fail the connection.
  6443. */
  6444. if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
  6445. == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
  6446. if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
  6447. goto out;
  6448. }
  6449. ret = 1;
  6450. }
  6451. /* Abort the handshake */
  6452. out:
  6453. OPENSSL_free(ecdsacert);
  6454. OPENSSL_free(ecdsakey);
  6455. OPENSSL_free(rootfile);
  6456. BIO_free(in);
  6457. EVP_PKEY_free(pkey);
  6458. X509_free(x509);
  6459. X509_free(rootx);
  6460. sk_X509_pop_free(chain, X509_free);
  6461. return ret;
  6462. }
  6463. /*
  6464. * Test the certificate callback.
  6465. * Test 0: Callback fails
  6466. * Test 1: Success - no SSL_set_SSL_CTX() in the callback
  6467. * Test 2: Success - SSL_set_SSL_CTX() in the callback
  6468. * Test 3: Success - Call SSL_check_chain from the callback
  6469. * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
  6470. * chain
  6471. * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
  6472. */
  6473. static int test_cert_cb_int(int prot, int tst)
  6474. {
  6475. SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
  6476. SSL *clientssl = NULL, *serverssl = NULL;
  6477. int testresult = 0, ret;
  6478. #ifdef OPENSSL_NO_EC
  6479. /* We use an EC cert in these tests, so we skip in a no-ec build */
  6480. if (tst >= 3)
  6481. return 1;
  6482. #endif
  6483. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6484. TLS_client_method(),
  6485. TLS1_VERSION,
  6486. prot,
  6487. &sctx, &cctx, NULL, NULL)))
  6488. goto end;
  6489. if (tst == 0)
  6490. cert_cb_cnt = -1;
  6491. else if (tst >= 3)
  6492. cert_cb_cnt = 3;
  6493. else
  6494. cert_cb_cnt = 0;
  6495. if (tst == 2)
  6496. snictx = SSL_CTX_new(TLS_server_method());
  6497. SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
  6498. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6499. NULL, NULL)))
  6500. goto end;
  6501. if (tst == 4) {
  6502. /*
  6503. * We cause SSL_check_chain() to fail by specifying sig_algs that
  6504. * the chain doesn't meet (the root uses an RSA cert)
  6505. */
  6506. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  6507. "ecdsa_secp256r1_sha256")))
  6508. goto end;
  6509. } else if (tst == 5) {
  6510. /*
  6511. * We cause SSL_check_chain() to fail by specifying sig_algs that
  6512. * the ee cert doesn't meet (the ee uses an ECDSA cert)
  6513. */
  6514. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  6515. "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
  6516. goto end;
  6517. }
  6518. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  6519. if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
  6520. || (tst > 0
  6521. && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
  6522. goto end;
  6523. }
  6524. testresult = 1;
  6525. end:
  6526. SSL_free(serverssl);
  6527. SSL_free(clientssl);
  6528. SSL_CTX_free(sctx);
  6529. SSL_CTX_free(cctx);
  6530. SSL_CTX_free(snictx);
  6531. return testresult;
  6532. }
  6533. #endif
  6534. static int test_cert_cb(int tst)
  6535. {
  6536. int testresult = 1;
  6537. #ifndef OPENSSL_NO_TLS1_2
  6538. testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
  6539. #endif
  6540. #ifndef OSSL_NO_USABLE_TLS1_3
  6541. testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
  6542. #endif
  6543. return testresult;
  6544. }
  6545. static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
  6546. {
  6547. X509 *xcert;
  6548. EVP_PKEY *privpkey;
  6549. BIO *in = NULL;
  6550. BIO *priv_in = NULL;
  6551. /* Check that SSL_get0_peer_certificate() returns something sensible */
  6552. if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
  6553. return 0;
  6554. in = BIO_new_file(cert, "r");
  6555. if (!TEST_ptr(in))
  6556. return 0;
  6557. if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
  6558. || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
  6559. || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
  6560. || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
  6561. NULL, NULL,
  6562. libctx, NULL)))
  6563. goto err;
  6564. *x509 = xcert;
  6565. *pkey = privpkey;
  6566. BIO_free(in);
  6567. BIO_free(priv_in);
  6568. return 1;
  6569. err:
  6570. X509_free(xcert);
  6571. BIO_free(in);
  6572. BIO_free(priv_in);
  6573. return 0;
  6574. }
  6575. static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  6576. {
  6577. return 1;
  6578. }
  6579. static int test_client_cert_cb(int tst)
  6580. {
  6581. SSL_CTX *cctx = NULL, *sctx = NULL;
  6582. SSL *clientssl = NULL, *serverssl = NULL;
  6583. int testresult = 0;
  6584. #ifdef OPENSSL_NO_TLS1_2
  6585. if (tst == 0)
  6586. return 1;
  6587. #endif
  6588. #ifdef OSSL_NO_USABLE_TLS1_3
  6589. if (tst == 1)
  6590. return 1;
  6591. #endif
  6592. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6593. TLS_client_method(),
  6594. TLS1_VERSION,
  6595. tst == 0 ? TLS1_2_VERSION
  6596. : TLS1_3_VERSION,
  6597. &sctx, &cctx, cert, privkey)))
  6598. goto end;
  6599. /*
  6600. * Test that setting a client_cert_cb results in a client certificate being
  6601. * sent.
  6602. */
  6603. SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
  6604. SSL_CTX_set_verify(sctx,
  6605. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  6606. verify_cb);
  6607. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6608. NULL, NULL))
  6609. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6610. SSL_ERROR_NONE)))
  6611. goto end;
  6612. testresult = 1;
  6613. end:
  6614. SSL_free(serverssl);
  6615. SSL_free(clientssl);
  6616. SSL_CTX_free(sctx);
  6617. SSL_CTX_free(cctx);
  6618. return testresult;
  6619. }
  6620. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  6621. /*
  6622. * Test setting certificate authorities on both client and server.
  6623. *
  6624. * Test 0: SSL_CTX_set0_CA_list() only
  6625. * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
  6626. * Test 2: Only SSL_CTX_set_client_CA_list()
  6627. */
  6628. static int test_ca_names_int(int prot, int tst)
  6629. {
  6630. SSL_CTX *cctx = NULL, *sctx = NULL;
  6631. SSL *clientssl = NULL, *serverssl = NULL;
  6632. int testresult = 0;
  6633. size_t i;
  6634. X509_NAME *name[] = { NULL, NULL, NULL, NULL };
  6635. char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
  6636. STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
  6637. const STACK_OF(X509_NAME) *sktmp = NULL;
  6638. for (i = 0; i < OSSL_NELEM(name); i++) {
  6639. name[i] = X509_NAME_new();
  6640. if (!TEST_ptr(name[i])
  6641. || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
  6642. MBSTRING_ASC,
  6643. (unsigned char *)
  6644. strnames[i],
  6645. -1, -1, 0)))
  6646. goto end;
  6647. }
  6648. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6649. TLS_client_method(),
  6650. TLS1_VERSION,
  6651. prot,
  6652. &sctx, &cctx, cert, privkey)))
  6653. goto end;
  6654. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  6655. if (tst == 0 || tst == 1) {
  6656. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  6657. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
  6658. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
  6659. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  6660. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
  6661. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
  6662. goto end;
  6663. SSL_CTX_set0_CA_list(sctx, sk1);
  6664. SSL_CTX_set0_CA_list(cctx, sk2);
  6665. sk1 = sk2 = NULL;
  6666. }
  6667. if (tst == 1 || tst == 2) {
  6668. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  6669. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
  6670. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
  6671. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  6672. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
  6673. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
  6674. goto end;
  6675. SSL_CTX_set_client_CA_list(sctx, sk1);
  6676. SSL_CTX_set_client_CA_list(cctx, sk2);
  6677. sk1 = sk2 = NULL;
  6678. }
  6679. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6680. NULL, NULL))
  6681. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6682. SSL_ERROR_NONE)))
  6683. goto end;
  6684. /*
  6685. * We only expect certificate authorities to have been sent to the server
  6686. * if we are using TLSv1.3 and SSL_set0_CA_list() was used
  6687. */
  6688. sktmp = SSL_get0_peer_CA_list(serverssl);
  6689. if (prot == TLS1_3_VERSION
  6690. && (tst == 0 || tst == 1)) {
  6691. if (!TEST_ptr(sktmp)
  6692. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  6693. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  6694. name[0]), 0)
  6695. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  6696. name[1]), 0))
  6697. goto end;
  6698. } else if (!TEST_ptr_null(sktmp)) {
  6699. goto end;
  6700. }
  6701. /*
  6702. * In all tests we expect certificate authorities to have been sent to the
  6703. * client. However, SSL_set_client_CA_list() should override
  6704. * SSL_set0_CA_list()
  6705. */
  6706. sktmp = SSL_get0_peer_CA_list(clientssl);
  6707. if (!TEST_ptr(sktmp)
  6708. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  6709. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  6710. name[tst == 0 ? 0 : 2]), 0)
  6711. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  6712. name[tst == 0 ? 1 : 3]), 0))
  6713. goto end;
  6714. testresult = 1;
  6715. end:
  6716. SSL_free(serverssl);
  6717. SSL_free(clientssl);
  6718. SSL_CTX_free(sctx);
  6719. SSL_CTX_free(cctx);
  6720. for (i = 0; i < OSSL_NELEM(name); i++)
  6721. X509_NAME_free(name[i]);
  6722. sk_X509_NAME_pop_free(sk1, X509_NAME_free);
  6723. sk_X509_NAME_pop_free(sk2, X509_NAME_free);
  6724. return testresult;
  6725. }
  6726. #endif
  6727. static int test_ca_names(int tst)
  6728. {
  6729. int testresult = 1;
  6730. #ifndef OPENSSL_NO_TLS1_2
  6731. testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
  6732. #endif
  6733. #ifndef OSSL_NO_USABLE_TLS1_3
  6734. testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
  6735. #endif
  6736. return testresult;
  6737. }
  6738. #ifndef OPENSSL_NO_TLS1_2
  6739. static const char *multiblock_cipherlist_data[]=
  6740. {
  6741. "AES128-SHA",
  6742. "AES128-SHA256",
  6743. "AES256-SHA",
  6744. "AES256-SHA256",
  6745. };
  6746. /* Reduce the fragment size - so the multiblock test buffer can be small */
  6747. # define MULTIBLOCK_FRAGSIZE 512
  6748. static int test_multiblock_write(int test_index)
  6749. {
  6750. static const char *fetchable_ciphers[]=
  6751. {
  6752. "AES-128-CBC-HMAC-SHA1",
  6753. "AES-128-CBC-HMAC-SHA256",
  6754. "AES-256-CBC-HMAC-SHA1",
  6755. "AES-256-CBC-HMAC-SHA256"
  6756. };
  6757. const char *cipherlist = multiblock_cipherlist_data[test_index];
  6758. const SSL_METHOD *smeth = TLS_server_method();
  6759. const SSL_METHOD *cmeth = TLS_client_method();
  6760. int min_version = TLS1_VERSION;
  6761. int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
  6762. SSL_CTX *cctx = NULL, *sctx = NULL;
  6763. SSL *clientssl = NULL, *serverssl = NULL;
  6764. int testresult = 0;
  6765. /*
  6766. * Choose a buffer large enough to perform a multi-block operation
  6767. * i.e: write_len >= 4 * frag_size
  6768. * 9 * is chosen so that multiple multiblocks are used + some leftover.
  6769. */
  6770. unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
  6771. unsigned char buf[sizeof(msg)], *p = buf;
  6772. size_t readbytes, written, len;
  6773. EVP_CIPHER *ciph = NULL;
  6774. /*
  6775. * Check if the cipher exists before attempting to use it since it only has
  6776. * a hardware specific implementation.
  6777. */
  6778. ciph = EVP_CIPHER_fetch(NULL, fetchable_ciphers[test_index], "");
  6779. if (ciph == NULL) {
  6780. TEST_skip("Multiblock cipher is not available for %s", cipherlist);
  6781. return 1;
  6782. }
  6783. EVP_CIPHER_free(ciph);
  6784. /* Set up a buffer with some data that will be sent to the client */
  6785. RAND_bytes(msg, sizeof(msg));
  6786. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  6787. max_version, &sctx, &cctx, cert,
  6788. privkey)))
  6789. goto end;
  6790. if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
  6791. goto end;
  6792. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6793. NULL, NULL)))
  6794. goto end;
  6795. /* settings to force it to use AES-CBC-HMAC_SHA */
  6796. SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
  6797. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
  6798. goto end;
  6799. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  6800. goto end;
  6801. if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  6802. || !TEST_size_t_eq(written, sizeof(msg)))
  6803. goto end;
  6804. len = written;
  6805. while (len > 0) {
  6806. if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
  6807. goto end;
  6808. p += readbytes;
  6809. len -= readbytes;
  6810. }
  6811. if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
  6812. goto end;
  6813. testresult = 1;
  6814. end:
  6815. SSL_free(serverssl);
  6816. SSL_free(clientssl);
  6817. SSL_CTX_free(sctx);
  6818. SSL_CTX_free(cctx);
  6819. return testresult;
  6820. }
  6821. #endif /* OPENSSL_NO_TLS1_2 */
  6822. /*
  6823. * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
  6824. * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
  6825. * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
  6826. * Test 3: Client does not set servername on initial handshake (TLSv1.2)
  6827. * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
  6828. * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
  6829. * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
  6830. * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
  6831. * Test 8: Client does not set servername on initial handshake(TLSv1.3)
  6832. * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
  6833. */
  6834. static int test_servername(int tst)
  6835. {
  6836. SSL_CTX *cctx = NULL, *sctx = NULL;
  6837. SSL *clientssl = NULL, *serverssl = NULL;
  6838. int testresult = 0;
  6839. SSL_SESSION *sess = NULL;
  6840. const char *sexpectedhost = NULL, *cexpectedhost = NULL;
  6841. #ifdef OPENSSL_NO_TLS1_2
  6842. if (tst <= 4)
  6843. return 1;
  6844. #endif
  6845. #ifdef OSSL_NO_USABLE_TLS1_3
  6846. if (tst >= 5)
  6847. return 1;
  6848. #endif
  6849. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6850. TLS_client_method(),
  6851. TLS1_VERSION,
  6852. (tst <= 4) ? TLS1_2_VERSION
  6853. : TLS1_3_VERSION,
  6854. &sctx, &cctx, cert, privkey))
  6855. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6856. NULL, NULL)))
  6857. goto end;
  6858. if (tst != 1 && tst != 6) {
  6859. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  6860. hostname_cb)))
  6861. goto end;
  6862. }
  6863. if (tst != 3 && tst != 8) {
  6864. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  6865. goto end;
  6866. sexpectedhost = cexpectedhost = "goodhost";
  6867. }
  6868. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  6869. goto end;
  6870. if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
  6871. cexpectedhost)
  6872. || !TEST_str_eq(SSL_get_servername(serverssl,
  6873. TLSEXT_NAMETYPE_host_name),
  6874. sexpectedhost))
  6875. goto end;
  6876. /* Now repeat with a resumption handshake */
  6877. if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
  6878. || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
  6879. || !TEST_true(SSL_SESSION_is_resumable(sess))
  6880. || !TEST_int_eq(SSL_shutdown(serverssl), 0))
  6881. goto end;
  6882. SSL_free(clientssl);
  6883. SSL_free(serverssl);
  6884. clientssl = serverssl = NULL;
  6885. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  6886. NULL)))
  6887. goto end;
  6888. if (!TEST_true(SSL_set_session(clientssl, sess)))
  6889. goto end;
  6890. sexpectedhost = cexpectedhost = "goodhost";
  6891. if (tst == 2 || tst == 7) {
  6892. /* Set an inconsistent hostname */
  6893. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
  6894. goto end;
  6895. /*
  6896. * In TLSv1.2 we expect the hostname from the original handshake, in
  6897. * TLSv1.3 we expect the hostname from this handshake
  6898. */
  6899. if (tst == 7)
  6900. sexpectedhost = cexpectedhost = "altgoodhost";
  6901. if (!TEST_str_eq(SSL_get_servername(clientssl,
  6902. TLSEXT_NAMETYPE_host_name),
  6903. "altgoodhost"))
  6904. goto end;
  6905. } else if (tst == 4 || tst == 9) {
  6906. /*
  6907. * A TLSv1.3 session does not associate a session with a servername,
  6908. * but a TLSv1.2 session does.
  6909. */
  6910. if (tst == 9)
  6911. sexpectedhost = cexpectedhost = NULL;
  6912. if (!TEST_str_eq(SSL_get_servername(clientssl,
  6913. TLSEXT_NAMETYPE_host_name),
  6914. cexpectedhost))
  6915. goto end;
  6916. } else {
  6917. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  6918. goto end;
  6919. /*
  6920. * In a TLSv1.2 resumption where the hostname was not acknowledged
  6921. * we expect the hostname on the server to be empty. On the client we
  6922. * return what was requested in this case.
  6923. *
  6924. * Similarly if the client didn't set a hostname on an original TLSv1.2
  6925. * session but is now, the server hostname will be empty, but the client
  6926. * is as we set it.
  6927. */
  6928. if (tst == 1 || tst == 3)
  6929. sexpectedhost = NULL;
  6930. if (!TEST_str_eq(SSL_get_servername(clientssl,
  6931. TLSEXT_NAMETYPE_host_name),
  6932. "goodhost"))
  6933. goto end;
  6934. }
  6935. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  6936. goto end;
  6937. if (!TEST_true(SSL_session_reused(clientssl))
  6938. || !TEST_true(SSL_session_reused(serverssl))
  6939. || !TEST_str_eq(SSL_get_servername(clientssl,
  6940. TLSEXT_NAMETYPE_host_name),
  6941. cexpectedhost)
  6942. || !TEST_str_eq(SSL_get_servername(serverssl,
  6943. TLSEXT_NAMETYPE_host_name),
  6944. sexpectedhost))
  6945. goto end;
  6946. testresult = 1;
  6947. end:
  6948. SSL_SESSION_free(sess);
  6949. SSL_free(serverssl);
  6950. SSL_free(clientssl);
  6951. SSL_CTX_free(sctx);
  6952. SSL_CTX_free(cctx);
  6953. return testresult;
  6954. }
  6955. #if !defined(OPENSSL_NO_EC) \
  6956. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  6957. /*
  6958. * Test that if signature algorithms are not available, then we do not offer or
  6959. * accept them.
  6960. * Test 0: Two RSA sig algs available: both RSA sig algs shared
  6961. * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
  6962. * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
  6963. * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
  6964. * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
  6965. * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
  6966. */
  6967. static int test_sigalgs_available(int idx)
  6968. {
  6969. SSL_CTX *cctx = NULL, *sctx = NULL;
  6970. SSL *clientssl = NULL, *serverssl = NULL;
  6971. int testresult = 0;
  6972. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  6973. OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
  6974. OSSL_PROVIDER *filterprov = NULL;
  6975. int sig, hash;
  6976. if (!TEST_ptr(tmpctx))
  6977. goto end;
  6978. if (idx != 0 && idx != 3) {
  6979. if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
  6980. filter_provider_init)))
  6981. goto end;
  6982. filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
  6983. if (!TEST_ptr(filterprov))
  6984. goto end;
  6985. if (idx < 3) {
  6986. /*
  6987. * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
  6988. * or accepted for the peer that uses this libctx. Note that libssl
  6989. * *requires* SHA2-256 to be available so we cannot disable that. We
  6990. * also need SHA1 for our certificate.
  6991. */
  6992. if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
  6993. "SHA2-256:SHA1")))
  6994. goto end;
  6995. } else {
  6996. if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
  6997. "ECDSA"))
  6998. || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
  6999. "EC:X25519:X448")))
  7000. goto end;
  7001. }
  7002. if (idx == 1 || idx == 4)
  7003. clientctx = tmpctx;
  7004. else
  7005. serverctx = tmpctx;
  7006. }
  7007. cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
  7008. sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
  7009. if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
  7010. goto end;
  7011. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7012. TLS_client_method(),
  7013. TLS1_VERSION,
  7014. 0,
  7015. &sctx, &cctx, cert, privkey)))
  7016. goto end;
  7017. /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
  7018. if (idx < 4) {
  7019. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  7020. "ECDHE-RSA-AES128-GCM-SHA256")))
  7021. goto end;
  7022. } else {
  7023. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  7024. "ECDHE-ECDSA-AES128-GCM-SHA256")))
  7025. goto end;
  7026. }
  7027. if (idx < 3) {
  7028. if (!SSL_CTX_set1_sigalgs_list(cctx,
  7029. "rsa_pss_rsae_sha384"
  7030. ":rsa_pss_rsae_sha256")
  7031. || !SSL_CTX_set1_sigalgs_list(sctx,
  7032. "rsa_pss_rsae_sha384"
  7033. ":rsa_pss_rsae_sha256"))
  7034. goto end;
  7035. } else {
  7036. if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
  7037. || !SSL_CTX_set1_sigalgs_list(sctx,
  7038. "rsa_pss_rsae_sha256:ECDSA+SHA256"))
  7039. goto end;
  7040. }
  7041. if (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
  7042. SSL_FILETYPE_PEM), 1)
  7043. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
  7044. privkey2,
  7045. SSL_FILETYPE_PEM), 1)
  7046. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
  7047. goto end;
  7048. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7049. NULL, NULL)))
  7050. goto end;
  7051. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7052. goto end;
  7053. /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
  7054. if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
  7055. NULL, NULL),
  7056. (idx == 0 || idx == 3) ? 2 : 1))
  7057. goto end;
  7058. if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
  7059. goto end;
  7060. if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
  7061. : NID_rsassaPss))
  7062. goto end;
  7063. testresult = filter_provider_check_clean_finish();
  7064. end:
  7065. SSL_free(serverssl);
  7066. SSL_free(clientssl);
  7067. SSL_CTX_free(sctx);
  7068. SSL_CTX_free(cctx);
  7069. OSSL_PROVIDER_unload(filterprov);
  7070. OSSL_LIB_CTX_free(tmpctx);
  7071. return testresult;
  7072. }
  7073. #endif /*
  7074. * !defined(OPENSSL_NO_EC) \
  7075. * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  7076. */
  7077. #ifndef OPENSSL_NO_TLS1_3
  7078. /* This test can run in TLSv1.3 even if ec and dh are disabled */
  7079. static int test_pluggable_group(int idx)
  7080. {
  7081. SSL_CTX *cctx = NULL, *sctx = NULL;
  7082. SSL *clientssl = NULL, *serverssl = NULL;
  7083. int testresult = 0;
  7084. OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
  7085. /* Check that we are not impacted by a provider without any groups */
  7086. OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
  7087. const char *group_name = idx == 0 ? "xorgroup" : "xorkemgroup";
  7088. if (!TEST_ptr(tlsprov))
  7089. goto end;
  7090. if (legacyprov == NULL) {
  7091. /*
  7092. * In this case we assume we've been built with "no-legacy" and skip
  7093. * this test (there is no OPENSSL_NO_LEGACY)
  7094. */
  7095. testresult = 1;
  7096. goto end;
  7097. }
  7098. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7099. TLS_client_method(),
  7100. TLS1_3_VERSION,
  7101. TLS1_3_VERSION,
  7102. &sctx, &cctx, cert, privkey))
  7103. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7104. NULL, NULL)))
  7105. goto end;
  7106. if (!TEST_true(SSL_set1_groups_list(serverssl, group_name))
  7107. || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
  7108. goto end;
  7109. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7110. goto end;
  7111. if (!TEST_str_eq(group_name,
  7112. SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
  7113. goto end;
  7114. testresult = 1;
  7115. end:
  7116. SSL_free(serverssl);
  7117. SSL_free(clientssl);
  7118. SSL_CTX_free(sctx);
  7119. SSL_CTX_free(cctx);
  7120. OSSL_PROVIDER_unload(tlsprov);
  7121. OSSL_PROVIDER_unload(legacyprov);
  7122. return testresult;
  7123. }
  7124. #endif
  7125. #ifndef OPENSSL_NO_TLS1_2
  7126. static int test_ssl_dup(void)
  7127. {
  7128. SSL_CTX *cctx = NULL, *sctx = NULL;
  7129. SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
  7130. int testresult = 0;
  7131. BIO *rbio = NULL, *wbio = NULL;
  7132. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7133. TLS_client_method(),
  7134. 0,
  7135. 0,
  7136. &sctx, &cctx, cert, privkey)))
  7137. goto end;
  7138. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7139. NULL, NULL)))
  7140. goto end;
  7141. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  7142. || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
  7143. goto end;
  7144. client2ssl = SSL_dup(clientssl);
  7145. rbio = SSL_get_rbio(clientssl);
  7146. if (!TEST_ptr(rbio)
  7147. || !TEST_true(BIO_up_ref(rbio)))
  7148. goto end;
  7149. SSL_set0_rbio(client2ssl, rbio);
  7150. rbio = NULL;
  7151. wbio = SSL_get_wbio(clientssl);
  7152. if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
  7153. goto end;
  7154. SSL_set0_wbio(client2ssl, wbio);
  7155. rbio = NULL;
  7156. if (!TEST_ptr(client2ssl)
  7157. /* Handshake not started so pointers should be different */
  7158. || !TEST_ptr_ne(clientssl, client2ssl))
  7159. goto end;
  7160. if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
  7161. || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
  7162. goto end;
  7163. if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
  7164. goto end;
  7165. SSL_free(clientssl);
  7166. clientssl = SSL_dup(client2ssl);
  7167. if (!TEST_ptr(clientssl)
  7168. /* Handshake has finished so pointers should be the same */
  7169. || !TEST_ptr_eq(clientssl, client2ssl))
  7170. goto end;
  7171. testresult = 1;
  7172. end:
  7173. SSL_free(serverssl);
  7174. SSL_free(clientssl);
  7175. SSL_free(client2ssl);
  7176. SSL_CTX_free(sctx);
  7177. SSL_CTX_free(cctx);
  7178. return testresult;
  7179. }
  7180. # ifndef OPENSSL_NO_DH
  7181. static EVP_PKEY *tmp_dh_params = NULL;
  7182. /* Helper function for the test_set_tmp_dh() tests */
  7183. static EVP_PKEY *get_tmp_dh_params(void)
  7184. {
  7185. if (tmp_dh_params == NULL) {
  7186. BIGNUM *p = NULL;
  7187. OSSL_PARAM_BLD *tmpl = NULL;
  7188. EVP_PKEY_CTX *pctx = NULL;
  7189. OSSL_PARAM *params = NULL;
  7190. EVP_PKEY *dhpkey = NULL;
  7191. p = BN_get_rfc3526_prime_2048(NULL);
  7192. if (!TEST_ptr(p))
  7193. goto end;
  7194. pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
  7195. if (!TEST_ptr(pctx)
  7196. || !TEST_true(EVP_PKEY_fromdata_init(pctx)))
  7197. goto end;
  7198. tmpl = OSSL_PARAM_BLD_new();
  7199. if (!TEST_ptr(tmpl)
  7200. || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
  7201. OSSL_PKEY_PARAM_FFC_P,
  7202. p))
  7203. || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
  7204. OSSL_PKEY_PARAM_FFC_G,
  7205. 2)))
  7206. goto end;
  7207. params = OSSL_PARAM_BLD_to_param(tmpl);
  7208. if (!TEST_ptr(params)
  7209. || !TEST_true(EVP_PKEY_fromdata(pctx, &dhpkey,
  7210. EVP_PKEY_KEY_PARAMETERS, params)))
  7211. goto end;
  7212. tmp_dh_params = dhpkey;
  7213. end:
  7214. BN_free(p);
  7215. EVP_PKEY_CTX_free(pctx);
  7216. OSSL_PARAM_BLD_free(tmpl);
  7217. OSSL_PARAM_free(params);
  7218. }
  7219. if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
  7220. return NULL;
  7221. return tmp_dh_params;
  7222. }
  7223. # ifndef OPENSSL_NO_DEPRECATED_3_0
  7224. /* Callback used by test_set_tmp_dh() */
  7225. static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
  7226. {
  7227. EVP_PKEY *dhpkey = get_tmp_dh_params();
  7228. DH *ret = NULL;
  7229. if (!TEST_ptr(dhpkey))
  7230. return NULL;
  7231. /*
  7232. * libssl does not free the returned DH, so we free it now knowing that even
  7233. * after we free dhpkey, there will still be a reference to the owning
  7234. * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
  7235. * of time we need it for.
  7236. */
  7237. ret = EVP_PKEY_get1_DH(dhpkey);
  7238. DH_free(ret);
  7239. EVP_PKEY_free(dhpkey);
  7240. return ret;
  7241. }
  7242. # endif
  7243. /*
  7244. * Test the various methods for setting temporary DH parameters
  7245. *
  7246. * Test 0: Default (no auto) setting
  7247. * Test 1: Explicit SSL_CTX auto off
  7248. * Test 2: Explicit SSL auto off
  7249. * Test 3: Explicit SSL_CTX auto on
  7250. * Test 4: Explicit SSL auto on
  7251. * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
  7252. * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
  7253. *
  7254. * The following are testing deprecated APIs, so we only run them if available
  7255. * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
  7256. * Test 8: Explicit SSL auto off, custom DH params via DH
  7257. * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
  7258. * Test 10: Explicit SSL auto off, custom DH params via callback
  7259. */
  7260. static int test_set_tmp_dh(int idx)
  7261. {
  7262. SSL_CTX *cctx = NULL, *sctx = NULL;
  7263. SSL *clientssl = NULL, *serverssl = NULL;
  7264. int testresult = 0;
  7265. int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
  7266. int expected = (idx <= 2) ? 0 : 1;
  7267. EVP_PKEY *dhpkey = NULL;
  7268. # ifndef OPENSSL_NO_DEPRECATED_3_0
  7269. DH *dh = NULL;
  7270. # else
  7271. if (idx >= 7)
  7272. return 1;
  7273. # endif
  7274. if (idx >= 5 && idx <= 8) {
  7275. dhpkey = get_tmp_dh_params();
  7276. if (!TEST_ptr(dhpkey))
  7277. goto end;
  7278. }
  7279. # ifndef OPENSSL_NO_DEPRECATED_3_0
  7280. if (idx == 7 || idx == 8) {
  7281. dh = EVP_PKEY_get1_DH(dhpkey);
  7282. if (!TEST_ptr(dh))
  7283. goto end;
  7284. }
  7285. # endif
  7286. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7287. TLS_client_method(),
  7288. 0,
  7289. 0,
  7290. &sctx, &cctx, cert, privkey)))
  7291. goto end;
  7292. if ((idx & 1) == 1) {
  7293. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
  7294. goto end;
  7295. }
  7296. if (idx == 5) {
  7297. if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
  7298. goto end;
  7299. dhpkey = NULL;
  7300. }
  7301. # ifndef OPENSSL_NO_DEPRECATED_3_0
  7302. else if (idx == 7) {
  7303. if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
  7304. goto end;
  7305. } else if (idx == 9) {
  7306. SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
  7307. }
  7308. # endif
  7309. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7310. NULL, NULL)))
  7311. goto end;
  7312. if ((idx & 1) == 0 && idx != 0) {
  7313. if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
  7314. goto end;
  7315. }
  7316. if (idx == 6) {
  7317. if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
  7318. goto end;
  7319. dhpkey = NULL;
  7320. }
  7321. # ifndef OPENSSL_NO_DEPRECATED_3_0
  7322. else if (idx == 8) {
  7323. if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
  7324. goto end;
  7325. } else if (idx == 10) {
  7326. SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
  7327. }
  7328. # endif
  7329. if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  7330. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  7331. || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
  7332. goto end;
  7333. /*
  7334. * If autoon then we should succeed. Otherwise we expect failure because
  7335. * there are no parameters
  7336. */
  7337. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  7338. SSL_ERROR_NONE), expected))
  7339. goto end;
  7340. testresult = 1;
  7341. end:
  7342. # ifndef OPENSSL_NO_DEPRECATED_3_0
  7343. DH_free(dh);
  7344. # endif
  7345. SSL_free(serverssl);
  7346. SSL_free(clientssl);
  7347. SSL_CTX_free(sctx);
  7348. SSL_CTX_free(cctx);
  7349. EVP_PKEY_free(dhpkey);
  7350. return testresult;
  7351. }
  7352. /*
  7353. * Test the auto DH keys are appropriately sized
  7354. */
  7355. static int test_dh_auto(int idx)
  7356. {
  7357. SSL_CTX *cctx = NULL, *sctx = NULL;
  7358. SSL *clientssl = NULL, *serverssl = NULL;
  7359. int testresult = 0;
  7360. EVP_PKEY *tmpkey = NULL;
  7361. char *thiscert = NULL, *thiskey = NULL;
  7362. size_t expdhsize = 0;
  7363. const char *ciphersuite = "DHE-RSA-AES128-SHA";
  7364. switch (idx) {
  7365. case 0:
  7366. /* The FIPS provider doesn't support this DH size - so we ignore it */
  7367. if (is_fips)
  7368. return 1;
  7369. thiscert = cert1024;
  7370. thiskey = privkey1024;
  7371. expdhsize = 1024;
  7372. break;
  7373. case 1:
  7374. /* 2048 bit prime */
  7375. thiscert = cert;
  7376. thiskey = privkey;
  7377. expdhsize = 2048;
  7378. break;
  7379. case 2:
  7380. thiscert = cert3072;
  7381. thiskey = privkey3072;
  7382. expdhsize = 3072;
  7383. break;
  7384. case 3:
  7385. thiscert = cert4096;
  7386. thiskey = privkey4096;
  7387. expdhsize = 4096;
  7388. break;
  7389. case 4:
  7390. thiscert = cert8192;
  7391. thiskey = privkey8192;
  7392. expdhsize = 8192;
  7393. break;
  7394. /* No certificate cases */
  7395. case 5:
  7396. /* The FIPS provider doesn't support this DH size - so we ignore it */
  7397. if (is_fips)
  7398. return 1;
  7399. ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
  7400. expdhsize = 1024;
  7401. break;
  7402. case 6:
  7403. ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
  7404. expdhsize = 3072;
  7405. break;
  7406. default:
  7407. TEST_error("Invalid text index");
  7408. goto end;
  7409. }
  7410. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7411. TLS_client_method(),
  7412. 0,
  7413. 0,
  7414. &sctx, &cctx, thiscert, thiskey)))
  7415. goto end;
  7416. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7417. NULL, NULL)))
  7418. goto end;
  7419. if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
  7420. || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  7421. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  7422. || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
  7423. || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
  7424. goto end;
  7425. /*
  7426. * Send the server's first flight. At this point the server has created the
  7427. * temporary DH key but hasn't finished using it yet. Once used it is
  7428. * removed, so we cannot test it.
  7429. */
  7430. if (!TEST_int_le(SSL_connect(clientssl), 0)
  7431. || !TEST_int_le(SSL_accept(serverssl), 0))
  7432. goto end;
  7433. if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
  7434. goto end;
  7435. if (!TEST_size_t_eq(EVP_PKEY_bits(tmpkey), expdhsize))
  7436. goto end;
  7437. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7438. goto end;
  7439. testresult = 1;
  7440. end:
  7441. SSL_free(serverssl);
  7442. SSL_free(clientssl);
  7443. SSL_CTX_free(sctx);
  7444. SSL_CTX_free(cctx);
  7445. EVP_PKEY_free(tmpkey);
  7446. return testresult;
  7447. }
  7448. # endif /* OPENSSL_NO_DH */
  7449. #endif /* OPENSSL_NO_TLS1_2 */
  7450. #ifndef OSSL_NO_USABLE_TLS1_3
  7451. /*
  7452. * Test that setting an SNI callback works with TLSv1.3. Specifically we check
  7453. * that it works even without a certificate configured for the original
  7454. * SSL_CTX
  7455. */
  7456. static int test_sni_tls13(void)
  7457. {
  7458. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  7459. SSL *clientssl = NULL, *serverssl = NULL;
  7460. int testresult = 0;
  7461. /* Reset callback counter */
  7462. snicb = 0;
  7463. /* Create an initial SSL_CTX with no certificate configured */
  7464. sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  7465. if (!TEST_ptr(sctx))
  7466. goto end;
  7467. /* Require TLSv1.3 as a minimum */
  7468. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7469. TLS_client_method(), TLS1_3_VERSION, 0,
  7470. &sctx2, &cctx, cert, privkey)))
  7471. goto end;
  7472. /* Set up SNI */
  7473. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  7474. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  7475. goto end;
  7476. /*
  7477. * Connection should still succeed because the final SSL_CTX has the right
  7478. * certificates configured.
  7479. */
  7480. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  7481. &clientssl, NULL, NULL))
  7482. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7483. SSL_ERROR_NONE)))
  7484. goto end;
  7485. /* We should have had the SNI callback called exactly once */
  7486. if (!TEST_int_eq(snicb, 1))
  7487. goto end;
  7488. testresult = 1;
  7489. end:
  7490. SSL_free(serverssl);
  7491. SSL_free(clientssl);
  7492. SSL_CTX_free(sctx2);
  7493. SSL_CTX_free(sctx);
  7494. SSL_CTX_free(cctx);
  7495. return testresult;
  7496. }
  7497. #endif
  7498. /*
  7499. * Test that setting an ALPN does not violate RFC
  7500. */
  7501. static int test_set_alpn(void)
  7502. {
  7503. SSL_CTX *ctx = NULL;
  7504. SSL *ssl = NULL;
  7505. int testresult = 0;
  7506. unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
  7507. unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
  7508. unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
  7509. unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
  7510. unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
  7511. unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
  7512. /* Create an initial SSL_CTX with no certificate configured */
  7513. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  7514. if (!TEST_ptr(ctx))
  7515. goto end;
  7516. /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
  7517. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
  7518. goto end;
  7519. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
  7520. goto end;
  7521. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
  7522. goto end;
  7523. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
  7524. goto end;
  7525. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
  7526. goto end;
  7527. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
  7528. goto end;
  7529. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
  7530. goto end;
  7531. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
  7532. goto end;
  7533. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
  7534. goto end;
  7535. ssl = SSL_new(ctx);
  7536. if (!TEST_ptr(ssl))
  7537. goto end;
  7538. if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
  7539. goto end;
  7540. if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
  7541. goto end;
  7542. if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
  7543. goto end;
  7544. if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
  7545. goto end;
  7546. if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
  7547. goto end;
  7548. if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
  7549. goto end;
  7550. if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
  7551. goto end;
  7552. if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
  7553. goto end;
  7554. if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
  7555. goto end;
  7556. testresult = 1;
  7557. end:
  7558. SSL_free(ssl);
  7559. SSL_CTX_free(ctx);
  7560. return testresult;
  7561. }
  7562. static int test_inherit_verify_param(void)
  7563. {
  7564. int testresult = 0;
  7565. SSL_CTX *ctx = NULL;
  7566. X509_VERIFY_PARAM *cp = NULL;
  7567. SSL *ssl = NULL;
  7568. X509_VERIFY_PARAM *sp = NULL;
  7569. int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
  7570. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  7571. if (!TEST_ptr(ctx))
  7572. goto end;
  7573. cp = SSL_CTX_get0_param(ctx);
  7574. if (!TEST_ptr(cp))
  7575. goto end;
  7576. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
  7577. goto end;
  7578. X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
  7579. ssl = SSL_new(ctx);
  7580. if (!TEST_ptr(ssl))
  7581. goto end;
  7582. sp = SSL_get0_param(ssl);
  7583. if (!TEST_ptr(sp))
  7584. goto end;
  7585. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
  7586. goto end;
  7587. testresult = 1;
  7588. end:
  7589. SSL_free(ssl);
  7590. SSL_CTX_free(ctx);
  7591. return testresult;
  7592. }
  7593. OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config\n")
  7594. int setup_tests(void)
  7595. {
  7596. char *modulename;
  7597. char *configfile;
  7598. libctx = OSSL_LIB_CTX_new();
  7599. if (!TEST_ptr(libctx))
  7600. return 0;
  7601. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  7602. /*
  7603. * Verify that the default and fips providers in the default libctx are not
  7604. * available
  7605. */
  7606. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  7607. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  7608. return 0;
  7609. if (!test_skip_common_options()) {
  7610. TEST_error("Error parsing test options\n");
  7611. return 0;
  7612. }
  7613. if (!TEST_ptr(certsdir = test_get_argument(0))
  7614. || !TEST_ptr(srpvfile = test_get_argument(1))
  7615. || !TEST_ptr(tmpfilename = test_get_argument(2))
  7616. || !TEST_ptr(modulename = test_get_argument(3))
  7617. || !TEST_ptr(configfile = test_get_argument(4)))
  7618. return 0;
  7619. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  7620. return 0;
  7621. /* Check we have the expected provider available */
  7622. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  7623. return 0;
  7624. /* Check the default provider is not available */
  7625. if (strcmp(modulename, "default") != 0
  7626. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  7627. return 0;
  7628. if (strcmp(modulename, "fips") == 0)
  7629. is_fips = 1;
  7630. /*
  7631. * We add, but don't load the test "tls-provider". We'll load it when we
  7632. * need it.
  7633. */
  7634. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
  7635. tls_provider_init)))
  7636. return 0;
  7637. if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
  7638. #ifdef OPENSSL_NO_CRYPTO_MDEBUG
  7639. TEST_error("not supported in this build");
  7640. return 0;
  7641. #else
  7642. int i, mcount, rcount, fcount;
  7643. for (i = 0; i < 4; i++)
  7644. test_export_key_mat(i);
  7645. CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
  7646. test_printf_stdout("malloc %d realloc %d free %d\n",
  7647. mcount, rcount, fcount);
  7648. return 1;
  7649. #endif
  7650. }
  7651. cert = test_mk_file_path(certsdir, "servercert.pem");
  7652. if (cert == NULL)
  7653. goto err;
  7654. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  7655. if (privkey == NULL)
  7656. goto err;
  7657. cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  7658. if (cert2 == NULL)
  7659. goto err;
  7660. privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  7661. if (privkey2 == NULL)
  7662. goto err;
  7663. cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
  7664. if (cert1024 == NULL)
  7665. goto err;
  7666. privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
  7667. if (privkey1024 == NULL)
  7668. goto err;
  7669. cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
  7670. if (cert3072 == NULL)
  7671. goto err;
  7672. privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
  7673. if (privkey3072 == NULL)
  7674. goto err;
  7675. cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
  7676. if (cert4096 == NULL)
  7677. goto err;
  7678. privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
  7679. if (privkey4096 == NULL)
  7680. goto err;
  7681. cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
  7682. if (cert8192 == NULL)
  7683. goto err;
  7684. privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
  7685. if (privkey8192 == NULL)
  7686. goto err;
  7687. #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
  7688. # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7689. ADD_ALL_TESTS(test_ktls, 8);
  7690. ADD_ALL_TESTS(test_ktls_sendfile_anytls, 6);
  7691. # endif
  7692. #endif
  7693. ADD_TEST(test_large_message_tls);
  7694. ADD_TEST(test_large_message_tls_read_ahead);
  7695. #ifndef OPENSSL_NO_DTLS
  7696. ADD_TEST(test_large_message_dtls);
  7697. #endif
  7698. ADD_TEST(test_cleanse_plaintext);
  7699. #ifndef OPENSSL_NO_OCSP
  7700. ADD_TEST(test_tlsext_status_type);
  7701. #endif
  7702. ADD_TEST(test_session_with_only_int_cache);
  7703. ADD_TEST(test_session_with_only_ext_cache);
  7704. ADD_TEST(test_session_with_both_cache);
  7705. ADD_TEST(test_session_wo_ca_names);
  7706. #ifndef OSSL_NO_USABLE_TLS1_3
  7707. ADD_ALL_TESTS(test_stateful_tickets, 3);
  7708. ADD_ALL_TESTS(test_stateless_tickets, 3);
  7709. ADD_TEST(test_psk_tickets);
  7710. ADD_ALL_TESTS(test_extra_tickets, 6);
  7711. #endif
  7712. ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
  7713. ADD_TEST(test_ssl_bio_pop_next_bio);
  7714. ADD_TEST(test_ssl_bio_pop_ssl_bio);
  7715. ADD_TEST(test_ssl_bio_change_rbio);
  7716. ADD_TEST(test_ssl_bio_change_wbio);
  7717. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  7718. ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
  7719. ADD_TEST(test_keylog);
  7720. #endif
  7721. #ifndef OSSL_NO_USABLE_TLS1_3
  7722. ADD_TEST(test_keylog_no_master_key);
  7723. #endif
  7724. ADD_TEST(test_client_cert_verify_cb);
  7725. ADD_TEST(test_ssl_build_cert_chain);
  7726. ADD_TEST(test_ssl_ctx_build_cert_chain);
  7727. #ifndef OPENSSL_NO_TLS1_2
  7728. ADD_TEST(test_client_hello_cb);
  7729. ADD_TEST(test_no_ems);
  7730. ADD_TEST(test_ccs_change_cipher);
  7731. #endif
  7732. #ifndef OSSL_NO_USABLE_TLS1_3
  7733. ADD_ALL_TESTS(test_early_data_read_write, 3);
  7734. /*
  7735. * We don't do replay tests for external PSK. Replay protection isn't used
  7736. * in that scenario.
  7737. */
  7738. ADD_ALL_TESTS(test_early_data_replay, 2);
  7739. ADD_ALL_TESTS(test_early_data_skip, 3);
  7740. ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
  7741. ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3);
  7742. ADD_ALL_TESTS(test_early_data_skip_abort, 3);
  7743. ADD_ALL_TESTS(test_early_data_not_sent, 3);
  7744. ADD_ALL_TESTS(test_early_data_psk, 8);
  7745. ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
  7746. ADD_ALL_TESTS(test_early_data_not_expected, 3);
  7747. # ifndef OPENSSL_NO_TLS1_2
  7748. ADD_ALL_TESTS(test_early_data_tls1_2, 3);
  7749. # endif
  7750. #endif
  7751. #ifndef OSSL_NO_USABLE_TLS1_3
  7752. ADD_ALL_TESTS(test_set_ciphersuite, 10);
  7753. ADD_TEST(test_ciphersuite_change);
  7754. ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
  7755. # ifdef OPENSSL_NO_PSK
  7756. ADD_ALL_TESTS(test_tls13_psk, 1);
  7757. # else
  7758. ADD_ALL_TESTS(test_tls13_psk, 4);
  7759. # endif /* OPENSSL_NO_PSK */
  7760. # ifndef OPENSSL_NO_TLS1_2
  7761. /* Test with both TLSv1.3 and 1.2 versions */
  7762. ADD_ALL_TESTS(test_key_exchange, 14);
  7763. # else
  7764. /* Test with only TLSv1.3 versions */
  7765. ADD_ALL_TESTS(test_key_exchange, 12);
  7766. # endif
  7767. ADD_ALL_TESTS(test_custom_exts, 5);
  7768. ADD_TEST(test_stateless);
  7769. ADD_TEST(test_pha_key_update);
  7770. #else
  7771. ADD_ALL_TESTS(test_custom_exts, 3);
  7772. #endif
  7773. ADD_ALL_TESTS(test_serverinfo, 8);
  7774. ADD_ALL_TESTS(test_export_key_mat, 6);
  7775. #ifndef OSSL_NO_USABLE_TLS1_3
  7776. ADD_ALL_TESTS(test_export_key_mat_early, 3);
  7777. ADD_TEST(test_key_update);
  7778. ADD_ALL_TESTS(test_key_update_in_write, 2);
  7779. #endif
  7780. ADD_ALL_TESTS(test_ssl_clear, 2);
  7781. ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
  7782. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  7783. ADD_ALL_TESTS(test_srp, 6);
  7784. #endif
  7785. ADD_ALL_TESTS(test_info_callback, 6);
  7786. ADD_ALL_TESTS(test_ssl_pending, 2);
  7787. ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
  7788. ADD_ALL_TESTS(test_ticket_callbacks, 16);
  7789. ADD_ALL_TESTS(test_shutdown, 7);
  7790. ADD_ALL_TESTS(test_incorrect_shutdown, 2);
  7791. ADD_ALL_TESTS(test_cert_cb, 6);
  7792. ADD_ALL_TESTS(test_client_cert_cb, 2);
  7793. ADD_ALL_TESTS(test_ca_names, 3);
  7794. #ifndef OPENSSL_NO_TLS1_2
  7795. ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
  7796. #endif
  7797. ADD_ALL_TESTS(test_servername, 10);
  7798. #if !defined(OPENSSL_NO_EC) \
  7799. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  7800. ADD_ALL_TESTS(test_sigalgs_available, 6);
  7801. #endif
  7802. #ifndef OPENSSL_NO_TLS1_3
  7803. ADD_ALL_TESTS(test_pluggable_group, 2);
  7804. #endif
  7805. #ifndef OPENSSL_NO_TLS1_2
  7806. ADD_TEST(test_ssl_dup);
  7807. # ifndef OPENSSL_NO_DH
  7808. ADD_ALL_TESTS(test_set_tmp_dh, 11);
  7809. ADD_ALL_TESTS(test_dh_auto, 7);
  7810. # endif
  7811. #endif
  7812. #ifndef OSSL_NO_USABLE_TLS1_3
  7813. ADD_TEST(test_sni_tls13);
  7814. #endif
  7815. ADD_TEST(test_inherit_verify_param);
  7816. ADD_TEST(test_set_alpn);
  7817. return 1;
  7818. err:
  7819. OPENSSL_free(cert);
  7820. OPENSSL_free(privkey);
  7821. OPENSSL_free(cert2);
  7822. OPENSSL_free(privkey2);
  7823. return 0;
  7824. }
  7825. void cleanup_tests(void)
  7826. {
  7827. # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
  7828. EVP_PKEY_free(tmp_dh_params);
  7829. #endif
  7830. OPENSSL_free(cert);
  7831. OPENSSL_free(privkey);
  7832. OPENSSL_free(cert2);
  7833. OPENSSL_free(privkey2);
  7834. OPENSSL_free(cert1024);
  7835. OPENSSL_free(privkey1024);
  7836. OPENSSL_free(cert3072);
  7837. OPENSSL_free(privkey3072);
  7838. OPENSSL_free(cert4096);
  7839. OPENSSL_free(privkey4096);
  7840. OPENSSL_free(cert8192);
  7841. OPENSSL_free(privkey8192);
  7842. bio_s_mempacket_test_free();
  7843. bio_s_always_retry_free();
  7844. OSSL_PROVIDER_unload(defctxnull);
  7845. OSSL_LIB_CTX_free(libctx);
  7846. }