sslapitest.c 404 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154
  1. /*
  2. * Copyright 2016-2024 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 <openssl/dh.h>
  33. #include <openssl/engine.h>
  34. #include "helpers/ssltestlib.h"
  35. #include "testutil.h"
  36. #include "testutil/output.h"
  37. #include "internal/nelem.h"
  38. #include "internal/tlsgroups.h"
  39. #include "internal/ktls.h"
  40. #include "../ssl/ssl_local.h"
  41. #include "../ssl/record/methods/recmethod_local.h"
  42. #include "filterprov.h"
  43. #undef OSSL_NO_USABLE_TLS1_3
  44. #if defined(OPENSSL_NO_TLS1_3) \
  45. || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
  46. /*
  47. * If we don't have ec or dh then there are no built-in groups that are usable
  48. * with TLSv1.3
  49. */
  50. # define OSSL_NO_USABLE_TLS1_3
  51. #endif
  52. /* Defined in tls-provider.c */
  53. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  54. const OSSL_DISPATCH *in,
  55. const OSSL_DISPATCH **out,
  56. void **provctx);
  57. static OSSL_LIB_CTX *libctx = NULL;
  58. static OSSL_PROVIDER *defctxnull = NULL;
  59. #ifndef OSSL_NO_USABLE_TLS1_3
  60. static SSL_SESSION *clientpsk = NULL;
  61. static SSL_SESSION *serverpsk = NULL;
  62. static const char *pskid = "Identity";
  63. static const char *srvid;
  64. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  65. size_t *idlen, SSL_SESSION **sess);
  66. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  67. size_t identity_len, SSL_SESSION **sess);
  68. static int use_session_cb_cnt = 0;
  69. static int find_session_cb_cnt = 0;
  70. #endif
  71. static char *certsdir = NULL;
  72. static char *cert = NULL;
  73. static char *privkey = NULL;
  74. static char *cert2 = NULL;
  75. static char *privkey2 = NULL;
  76. static char *cert1024 = NULL;
  77. static char *privkey1024 = NULL;
  78. static char *cert3072 = NULL;
  79. static char *privkey3072 = NULL;
  80. static char *cert4096 = NULL;
  81. static char *privkey4096 = NULL;
  82. static char *cert8192 = NULL;
  83. static char *privkey8192 = NULL;
  84. static char *srpvfile = NULL;
  85. static char *tmpfilename = NULL;
  86. static char *dhfile = NULL;
  87. static int is_fips = 0;
  88. static int fips_ems_check = 0;
  89. #define LOG_BUFFER_SIZE 2048
  90. static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  91. static size_t server_log_buffer_index = 0;
  92. static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  93. static size_t client_log_buffer_index = 0;
  94. static int error_writing_log = 0;
  95. #ifndef OPENSSL_NO_OCSP
  96. static const unsigned char orespder[] = "Dummy OCSP Response";
  97. static int ocsp_server_called = 0;
  98. static int ocsp_client_called = 0;
  99. static int cdummyarg = 1;
  100. static X509 *ocspcert = NULL;
  101. #endif
  102. #define CLIENT_VERSION_LEN 2
  103. /*
  104. * This structure is used to validate that the correct number of log messages
  105. * of various types are emitted when emitting secret logs.
  106. */
  107. struct sslapitest_log_counts {
  108. unsigned int rsa_key_exchange_count;
  109. unsigned int master_secret_count;
  110. unsigned int client_early_secret_count;
  111. unsigned int client_handshake_secret_count;
  112. unsigned int server_handshake_secret_count;
  113. unsigned int client_application_secret_count;
  114. unsigned int server_application_secret_count;
  115. unsigned int early_exporter_secret_count;
  116. unsigned int exporter_secret_count;
  117. };
  118. static int hostname_cb(SSL *s, int *al, void *arg)
  119. {
  120. const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  121. if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
  122. || strcmp(hostname, "altgoodhost") == 0))
  123. return SSL_TLSEXT_ERR_OK;
  124. return SSL_TLSEXT_ERR_NOACK;
  125. }
  126. static void client_keylog_callback(const SSL *ssl, const char *line)
  127. {
  128. int line_length = strlen(line);
  129. /* If the log doesn't fit, error out. */
  130. if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
  131. TEST_info("Client log too full");
  132. error_writing_log = 1;
  133. return;
  134. }
  135. strcat(client_log_buffer, line);
  136. client_log_buffer_index += line_length;
  137. client_log_buffer[client_log_buffer_index++] = '\n';
  138. }
  139. static void server_keylog_callback(const SSL *ssl, const char *line)
  140. {
  141. int line_length = strlen(line);
  142. /* If the log doesn't fit, error out. */
  143. if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
  144. TEST_info("Server log too full");
  145. error_writing_log = 1;
  146. return;
  147. }
  148. strcat(server_log_buffer, line);
  149. server_log_buffer_index += line_length;
  150. server_log_buffer[server_log_buffer_index++] = '\n';
  151. }
  152. static int compare_hex_encoded_buffer(const char *hex_encoded,
  153. size_t hex_length,
  154. const uint8_t *raw,
  155. size_t raw_length)
  156. {
  157. size_t i, j;
  158. char hexed[3];
  159. if (!TEST_size_t_eq(raw_length * 2, hex_length))
  160. return 1;
  161. for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
  162. sprintf(hexed, "%02x", raw[i]);
  163. if (!TEST_int_eq(hexed[0], hex_encoded[j])
  164. || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. static int test_keylog_output(char *buffer, const SSL *ssl,
  170. const SSL_SESSION *session,
  171. struct sslapitest_log_counts *expected)
  172. {
  173. char *token = NULL;
  174. unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
  175. size_t client_random_size = SSL3_RANDOM_SIZE;
  176. unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
  177. size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
  178. unsigned int rsa_key_exchange_count = 0;
  179. unsigned int master_secret_count = 0;
  180. unsigned int client_early_secret_count = 0;
  181. unsigned int client_handshake_secret_count = 0;
  182. unsigned int server_handshake_secret_count = 0;
  183. unsigned int client_application_secret_count = 0;
  184. unsigned int server_application_secret_count = 0;
  185. unsigned int early_exporter_secret_count = 0;
  186. unsigned int exporter_secret_count = 0;
  187. for (token = strtok(buffer, " \n"); token != NULL;
  188. token = strtok(NULL, " \n")) {
  189. if (strcmp(token, "RSA") == 0) {
  190. /*
  191. * Premaster secret. Tokens should be: 16 ASCII bytes of
  192. * hex-encoded encrypted secret, then the hex-encoded pre-master
  193. * secret.
  194. */
  195. if (!TEST_ptr(token = strtok(NULL, " \n")))
  196. return 0;
  197. if (!TEST_size_t_eq(strlen(token), 16))
  198. return 0;
  199. if (!TEST_ptr(token = strtok(NULL, " \n")))
  200. return 0;
  201. /*
  202. * We can't sensibly check the log because the premaster secret is
  203. * transient, and OpenSSL doesn't keep hold of it once the master
  204. * secret is generated.
  205. */
  206. rsa_key_exchange_count++;
  207. } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
  208. /*
  209. * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
  210. * client random, then the hex-encoded master secret.
  211. */
  212. client_random_size = SSL_get_client_random(ssl,
  213. actual_client_random,
  214. SSL3_RANDOM_SIZE);
  215. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  216. return 0;
  217. if (!TEST_ptr(token = strtok(NULL, " \n")))
  218. return 0;
  219. if (!TEST_size_t_eq(strlen(token), 64))
  220. return 0;
  221. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  222. actual_client_random,
  223. client_random_size)))
  224. return 0;
  225. if (!TEST_ptr(token = strtok(NULL, " \n")))
  226. return 0;
  227. master_key_size = SSL_SESSION_get_master_key(session,
  228. actual_master_key,
  229. master_key_size);
  230. if (!TEST_size_t_ne(master_key_size, 0))
  231. return 0;
  232. if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
  233. actual_master_key,
  234. master_key_size)))
  235. return 0;
  236. master_secret_count++;
  237. } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
  238. || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
  239. || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
  240. || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
  241. || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
  242. || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
  243. || strcmp(token, "EXPORTER_SECRET") == 0) {
  244. /*
  245. * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
  246. * client random, and then the hex-encoded secret. In this case,
  247. * we treat all of these secrets identically and then just
  248. * distinguish between them when counting what we saw.
  249. */
  250. if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
  251. client_early_secret_count++;
  252. else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
  253. client_handshake_secret_count++;
  254. else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
  255. server_handshake_secret_count++;
  256. else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
  257. client_application_secret_count++;
  258. else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
  259. server_application_secret_count++;
  260. else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
  261. early_exporter_secret_count++;
  262. else if (strcmp(token, "EXPORTER_SECRET") == 0)
  263. exporter_secret_count++;
  264. client_random_size = SSL_get_client_random(ssl,
  265. actual_client_random,
  266. SSL3_RANDOM_SIZE);
  267. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  268. return 0;
  269. if (!TEST_ptr(token = strtok(NULL, " \n")))
  270. return 0;
  271. if (!TEST_size_t_eq(strlen(token), 64))
  272. return 0;
  273. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  274. actual_client_random,
  275. client_random_size)))
  276. return 0;
  277. if (!TEST_ptr(token = strtok(NULL, " \n")))
  278. return 0;
  279. } else {
  280. TEST_info("Unexpected token %s\n", token);
  281. return 0;
  282. }
  283. }
  284. /* Got what we expected? */
  285. if (!TEST_size_t_eq(rsa_key_exchange_count,
  286. expected->rsa_key_exchange_count)
  287. || !TEST_size_t_eq(master_secret_count,
  288. expected->master_secret_count)
  289. || !TEST_size_t_eq(client_early_secret_count,
  290. expected->client_early_secret_count)
  291. || !TEST_size_t_eq(client_handshake_secret_count,
  292. expected->client_handshake_secret_count)
  293. || !TEST_size_t_eq(server_handshake_secret_count,
  294. expected->server_handshake_secret_count)
  295. || !TEST_size_t_eq(client_application_secret_count,
  296. expected->client_application_secret_count)
  297. || !TEST_size_t_eq(server_application_secret_count,
  298. expected->server_application_secret_count)
  299. || !TEST_size_t_eq(early_exporter_secret_count,
  300. expected->early_exporter_secret_count)
  301. || !TEST_size_t_eq(exporter_secret_count,
  302. expected->exporter_secret_count))
  303. return 0;
  304. return 1;
  305. }
  306. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  307. static int test_keylog(void)
  308. {
  309. SSL_CTX *cctx = NULL, *sctx = NULL;
  310. SSL *clientssl = NULL, *serverssl = NULL;
  311. int testresult = 0;
  312. struct sslapitest_log_counts expected;
  313. /* Clean up logging space */
  314. memset(&expected, 0, sizeof(expected));
  315. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  316. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  317. client_log_buffer_index = 0;
  318. server_log_buffer_index = 0;
  319. error_writing_log = 0;
  320. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  321. TLS_client_method(),
  322. TLS1_VERSION, 0,
  323. &sctx, &cctx, cert, privkey)))
  324. return 0;
  325. /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
  326. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  327. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  328. /* We also want to ensure that we use RSA-based key exchange. */
  329. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
  330. goto end;
  331. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  332. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  333. goto end;
  334. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  335. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  336. == client_keylog_callback))
  337. goto end;
  338. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  339. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  340. == server_keylog_callback))
  341. goto end;
  342. /* Now do a handshake and check that the logs have been written to. */
  343. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  344. &clientssl, NULL, NULL))
  345. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  346. SSL_ERROR_NONE))
  347. || !TEST_false(error_writing_log)
  348. || !TEST_int_gt(client_log_buffer_index, 0)
  349. || !TEST_int_gt(server_log_buffer_index, 0))
  350. goto end;
  351. /*
  352. * Now we want to test that our output data was vaguely sensible. We
  353. * do that by using strtok and confirming that we have more or less the
  354. * data we expect. For both client and server, we expect to see one master
  355. * secret. The client should also see an RSA key exchange.
  356. */
  357. expected.rsa_key_exchange_count = 1;
  358. expected.master_secret_count = 1;
  359. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  360. SSL_get_session(clientssl), &expected)))
  361. goto end;
  362. expected.rsa_key_exchange_count = 0;
  363. if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
  364. SSL_get_session(serverssl), &expected)))
  365. goto end;
  366. testresult = 1;
  367. end:
  368. SSL_free(serverssl);
  369. SSL_free(clientssl);
  370. SSL_CTX_free(sctx);
  371. SSL_CTX_free(cctx);
  372. return testresult;
  373. }
  374. #endif
  375. #ifndef OSSL_NO_USABLE_TLS1_3
  376. static int test_keylog_no_master_key(void)
  377. {
  378. SSL_CTX *cctx = NULL, *sctx = NULL;
  379. SSL *clientssl = NULL, *serverssl = NULL;
  380. SSL_SESSION *sess = NULL;
  381. int testresult = 0;
  382. struct sslapitest_log_counts expected;
  383. unsigned char buf[1];
  384. size_t readbytes, written;
  385. /* Clean up logging space */
  386. memset(&expected, 0, sizeof(expected));
  387. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  388. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  389. client_log_buffer_index = 0;
  390. server_log_buffer_index = 0;
  391. error_writing_log = 0;
  392. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  393. TLS_client_method(), TLS1_VERSION, 0,
  394. &sctx, &cctx, cert, privkey))
  395. || !TEST_true(SSL_CTX_set_max_early_data(sctx,
  396. SSL3_RT_MAX_PLAIN_LENGTH)))
  397. return 0;
  398. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  399. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  400. goto end;
  401. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  402. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  403. == client_keylog_callback))
  404. goto end;
  405. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  406. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  407. == server_keylog_callback))
  408. goto end;
  409. /* Now do a handshake and check that the logs have been written to. */
  410. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  411. &clientssl, NULL, NULL))
  412. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  413. SSL_ERROR_NONE))
  414. || !TEST_false(error_writing_log))
  415. goto end;
  416. /*
  417. * Now we want to test that our output data was vaguely sensible. For this
  418. * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
  419. * TLSv1.3, but we do expect both client and server to emit keys.
  420. */
  421. expected.client_handshake_secret_count = 1;
  422. expected.server_handshake_secret_count = 1;
  423. expected.client_application_secret_count = 1;
  424. expected.server_application_secret_count = 1;
  425. expected.exporter_secret_count = 1;
  426. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  427. SSL_get_session(clientssl), &expected))
  428. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  429. SSL_get_session(serverssl),
  430. &expected)))
  431. goto end;
  432. /* Terminate old session and resume with early data. */
  433. sess = SSL_get1_session(clientssl);
  434. SSL_shutdown(clientssl);
  435. SSL_shutdown(serverssl);
  436. SSL_free(serverssl);
  437. SSL_free(clientssl);
  438. serverssl = clientssl = NULL;
  439. /* Reset key log */
  440. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  441. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  442. client_log_buffer_index = 0;
  443. server_log_buffer_index = 0;
  444. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  445. &clientssl, NULL, NULL))
  446. || !TEST_true(SSL_set_session(clientssl, sess))
  447. /* Here writing 0 length early data is enough. */
  448. || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  449. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  450. &readbytes),
  451. SSL_READ_EARLY_DATA_ERROR)
  452. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  453. SSL_EARLY_DATA_ACCEPTED)
  454. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  455. SSL_ERROR_NONE))
  456. || !TEST_true(SSL_session_reused(clientssl)))
  457. goto end;
  458. /* In addition to the previous entries, expect early secrets. */
  459. expected.client_early_secret_count = 1;
  460. expected.early_exporter_secret_count = 1;
  461. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  462. SSL_get_session(clientssl), &expected))
  463. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  464. SSL_get_session(serverssl),
  465. &expected)))
  466. goto end;
  467. testresult = 1;
  468. end:
  469. SSL_SESSION_free(sess);
  470. SSL_free(serverssl);
  471. SSL_free(clientssl);
  472. SSL_CTX_free(sctx);
  473. SSL_CTX_free(cctx);
  474. return testresult;
  475. }
  476. #endif
  477. static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
  478. {
  479. int res = X509_verify_cert(ctx);
  480. int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  481. SSL *ssl;
  482. /* this should not happen but check anyway */
  483. if (idx < 0
  484. || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
  485. return 0;
  486. if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
  487. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
  488. /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
  489. return SSL_set_retry_verify(ssl);
  490. return res;
  491. }
  492. static int test_client_cert_verify_cb(void)
  493. {
  494. /* server key, cert, chain, and root */
  495. char *skey = test_mk_file_path(certsdir, "leaf.key");
  496. char *leaf = test_mk_file_path(certsdir, "leaf.pem");
  497. char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
  498. char *int1 = test_mk_file_path(certsdir, "interCA.pem");
  499. char *root = test_mk_file_path(certsdir, "rootCA.pem");
  500. X509 *crt1 = NULL, *crt2 = NULL;
  501. STACK_OF(X509) *server_chain;
  502. SSL_CTX *cctx = NULL, *sctx = NULL;
  503. SSL *clientssl = NULL, *serverssl = NULL;
  504. int testresult = 0;
  505. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  506. TLS_client_method(), TLS1_VERSION, 0,
  507. &sctx, &cctx, NULL, NULL)))
  508. goto end;
  509. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
  510. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
  511. SSL_FILETYPE_PEM), 1)
  512. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
  513. goto end;
  514. if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
  515. goto end;
  516. SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
  517. SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
  518. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  519. &clientssl, NULL, NULL)))
  520. goto end;
  521. /* attempt SSL_connect() with incomplete server chain */
  522. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  523. SSL_ERROR_WANT_RETRY_VERIFY)))
  524. goto end;
  525. /* application provides intermediate certs needed to verify server cert */
  526. if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
  527. || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
  528. || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
  529. goto end;
  530. /* add certs in reverse order to demonstrate real chain building */
  531. if (!TEST_true(sk_X509_push(server_chain, crt1)))
  532. goto end;
  533. crt1 = NULL;
  534. if (!TEST_true(sk_X509_push(server_chain, crt2)))
  535. goto end;
  536. crt2 = NULL;
  537. /* continue SSL_connect(), must now succeed with completed server chain */
  538. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  539. SSL_ERROR_NONE)))
  540. goto end;
  541. testresult = 1;
  542. end:
  543. X509_free(crt1);
  544. X509_free(crt2);
  545. if (clientssl != NULL) {
  546. SSL_shutdown(clientssl);
  547. SSL_free(clientssl);
  548. }
  549. if (serverssl != NULL) {
  550. SSL_shutdown(serverssl);
  551. SSL_free(serverssl);
  552. }
  553. SSL_CTX_free(sctx);
  554. SSL_CTX_free(cctx);
  555. OPENSSL_free(skey);
  556. OPENSSL_free(leaf);
  557. OPENSSL_free(int2);
  558. OPENSSL_free(int1);
  559. OPENSSL_free(root);
  560. return testresult;
  561. }
  562. static int test_ssl_build_cert_chain(void)
  563. {
  564. int ret = 0;
  565. SSL_CTX *ssl_ctx = NULL;
  566. SSL *ssl = NULL;
  567. char *skey = test_mk_file_path(certsdir, "leaf.key");
  568. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  569. if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  570. goto end;
  571. if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
  572. goto end;
  573. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  574. if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
  575. || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
  576. || !TEST_int_eq(SSL_check_private_key(ssl), 1))
  577. goto end;
  578. if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  579. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  580. goto end;
  581. ret = 1;
  582. end:
  583. SSL_free(ssl);
  584. SSL_CTX_free(ssl_ctx);
  585. OPENSSL_free(leaf_chain);
  586. OPENSSL_free(skey);
  587. return ret;
  588. }
  589. static int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
  590. {
  591. static const char pass[] = "testpass";
  592. if (!TEST_int_eq(size, PEM_BUFSIZE))
  593. return -1;
  594. memcpy(buf, pass, sizeof(pass) - 1);
  595. return sizeof(pass) - 1;
  596. }
  597. static int test_ssl_ctx_build_cert_chain(void)
  598. {
  599. int ret = 0;
  600. SSL_CTX *ctx = NULL;
  601. char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
  602. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  603. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  604. goto end;
  605. SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
  606. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  607. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
  608. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
  609. SSL_FILETYPE_PEM), 1)
  610. || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
  611. goto end;
  612. if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  613. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  614. goto end;
  615. ret = 1;
  616. end:
  617. SSL_CTX_free(ctx);
  618. OPENSSL_free(leaf_chain);
  619. OPENSSL_free(skey);
  620. return ret;
  621. }
  622. #ifndef OPENSSL_NO_TLS1_2
  623. static int full_client_hello_callback(SSL *s, int *al, void *arg)
  624. {
  625. int *ctr = arg;
  626. const unsigned char *p;
  627. int *exts;
  628. /* We only configure two ciphers, but the SCSV is added automatically. */
  629. #ifdef OPENSSL_NO_EC
  630. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
  631. #else
  632. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
  633. 0x2c, 0x00, 0xff};
  634. #endif
  635. const int expected_extensions[] = {
  636. #ifndef OPENSSL_NO_EC
  637. 11, 10,
  638. #endif
  639. 35, 22, 23, 13};
  640. size_t len;
  641. /* Make sure we can defer processing and get called back. */
  642. if ((*ctr)++ == 0)
  643. return SSL_CLIENT_HELLO_RETRY;
  644. len = SSL_client_hello_get0_ciphers(s, &p);
  645. if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
  646. || !TEST_size_t_eq(
  647. SSL_client_hello_get0_compression_methods(s, &p), 1)
  648. || !TEST_int_eq(*p, 0))
  649. return SSL_CLIENT_HELLO_ERROR;
  650. if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
  651. return SSL_CLIENT_HELLO_ERROR;
  652. if (len != OSSL_NELEM(expected_extensions) ||
  653. memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
  654. printf("ClientHello callback expected extensions mismatch\n");
  655. OPENSSL_free(exts);
  656. return SSL_CLIENT_HELLO_ERROR;
  657. }
  658. OPENSSL_free(exts);
  659. return SSL_CLIENT_HELLO_SUCCESS;
  660. }
  661. static int test_client_hello_cb(void)
  662. {
  663. SSL_CTX *cctx = NULL, *sctx = NULL;
  664. SSL *clientssl = NULL, *serverssl = NULL;
  665. int testctr = 0, testresult = 0;
  666. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  667. TLS_client_method(), TLS1_VERSION, 0,
  668. &sctx, &cctx, cert, privkey)))
  669. goto end;
  670. SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
  671. /* The gimpy cipher list we configure can't do TLS 1.3. */
  672. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  673. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  674. "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
  675. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  676. &clientssl, NULL, NULL))
  677. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  678. SSL_ERROR_WANT_CLIENT_HELLO_CB))
  679. /*
  680. * Passing a -1 literal is a hack since
  681. * the real value was lost.
  682. * */
  683. || !TEST_int_eq(SSL_get_error(serverssl, -1),
  684. SSL_ERROR_WANT_CLIENT_HELLO_CB)
  685. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  686. SSL_ERROR_NONE)))
  687. goto end;
  688. testresult = 1;
  689. end:
  690. SSL_free(serverssl);
  691. SSL_free(clientssl);
  692. SSL_CTX_free(sctx);
  693. SSL_CTX_free(cctx);
  694. return testresult;
  695. }
  696. static int test_no_ems(void)
  697. {
  698. SSL_CTX *cctx = NULL, *sctx = NULL;
  699. SSL *clientssl = NULL, *serverssl = NULL;
  700. int testresult = 0, status;
  701. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  702. TLS1_VERSION, TLS1_2_VERSION,
  703. &sctx, &cctx, cert, privkey)) {
  704. printf("Unable to create SSL_CTX pair\n");
  705. goto end;
  706. }
  707. SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
  708. if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
  709. printf("Unable to create SSL objects\n");
  710. goto end;
  711. }
  712. status = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  713. if (fips_ems_check) {
  714. if (status == 1) {
  715. printf("When FIPS uses the EMS check a connection that doesn't use EMS should fail\n");
  716. goto end;
  717. }
  718. } else {
  719. if (!status) {
  720. printf("Creating SSL connection failed\n");
  721. goto end;
  722. }
  723. if (SSL_get_extms_support(serverssl)) {
  724. printf("Server reports Extended Master Secret support\n");
  725. goto end;
  726. }
  727. if (SSL_get_extms_support(clientssl)) {
  728. printf("Client reports Extended Master Secret support\n");
  729. goto end;
  730. }
  731. }
  732. testresult = 1;
  733. end:
  734. SSL_free(serverssl);
  735. SSL_free(clientssl);
  736. SSL_CTX_free(sctx);
  737. SSL_CTX_free(cctx);
  738. return testresult;
  739. }
  740. /*
  741. * Very focused test to exercise a single case in the server-side state
  742. * machine, when the ChangeCipherState message needs to actually change
  743. * from one cipher to a different cipher (i.e., not changing from null
  744. * encryption to real encryption).
  745. */
  746. static int test_ccs_change_cipher(void)
  747. {
  748. SSL_CTX *cctx = NULL, *sctx = NULL;
  749. SSL *clientssl = NULL, *serverssl = NULL;
  750. SSL_SESSION *sess = NULL, *sesspre, *sesspost;
  751. int testresult = 0;
  752. int i;
  753. unsigned char buf;
  754. size_t readbytes;
  755. /*
  756. * Create a connection so we can resume and potentially (but not) use
  757. * a different cipher in the second connection.
  758. */
  759. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  760. TLS_client_method(),
  761. TLS1_VERSION, TLS1_2_VERSION,
  762. &sctx, &cctx, cert, privkey))
  763. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
  764. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  765. NULL, NULL))
  766. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  767. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  768. SSL_ERROR_NONE))
  769. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  770. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  771. goto end;
  772. shutdown_ssl_connection(serverssl, clientssl);
  773. serverssl = clientssl = NULL;
  774. /* Resume, preferring a different cipher. Our server will force the
  775. * same cipher to be used as the initial handshake. */
  776. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  777. NULL, NULL))
  778. || !TEST_true(SSL_set_session(clientssl, sess))
  779. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
  780. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  781. SSL_ERROR_NONE))
  782. || !TEST_true(SSL_session_reused(clientssl))
  783. || !TEST_true(SSL_session_reused(serverssl))
  784. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  785. || !TEST_ptr_eq(sesspre, sesspost)
  786. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  787. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  788. goto end;
  789. shutdown_ssl_connection(serverssl, clientssl);
  790. serverssl = clientssl = NULL;
  791. /*
  792. * Now create a fresh connection and try to renegotiate a different
  793. * cipher on it.
  794. */
  795. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  796. NULL, NULL))
  797. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  798. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  799. SSL_ERROR_NONE))
  800. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  801. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
  802. || !TEST_true(SSL_renegotiate(clientssl))
  803. || !TEST_true(SSL_renegotiate_pending(clientssl)))
  804. goto end;
  805. /* Actually drive the renegotiation. */
  806. for (i = 0; i < 3; i++) {
  807. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  808. if (!TEST_ulong_eq(readbytes, 0))
  809. goto end;
  810. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  811. SSL_ERROR_WANT_READ)) {
  812. goto end;
  813. }
  814. if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
  815. if (!TEST_ulong_eq(readbytes, 0))
  816. goto end;
  817. } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
  818. SSL_ERROR_WANT_READ)) {
  819. goto end;
  820. }
  821. }
  822. /* sesspre and sesspost should be different since the cipher changed. */
  823. if (!TEST_false(SSL_renegotiate_pending(clientssl))
  824. || !TEST_false(SSL_session_reused(clientssl))
  825. || !TEST_false(SSL_session_reused(serverssl))
  826. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  827. || !TEST_ptr_ne(sesspre, sesspost)
  828. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  829. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  830. goto end;
  831. shutdown_ssl_connection(serverssl, clientssl);
  832. serverssl = clientssl = NULL;
  833. testresult = 1;
  834. end:
  835. SSL_free(serverssl);
  836. SSL_free(clientssl);
  837. SSL_CTX_free(sctx);
  838. SSL_CTX_free(cctx);
  839. SSL_SESSION_free(sess);
  840. return testresult;
  841. }
  842. #endif
  843. static int execute_test_large_message(const SSL_METHOD *smeth,
  844. const SSL_METHOD *cmeth,
  845. int min_version, int max_version,
  846. int read_ahead)
  847. {
  848. SSL_CTX *cctx = NULL, *sctx = NULL;
  849. SSL *clientssl = NULL, *serverssl = NULL;
  850. int testresult = 0;
  851. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  852. max_version, &sctx, &cctx, cert,
  853. privkey)))
  854. goto end;
  855. #ifdef OPENSSL_NO_DTLS1_2
  856. if (smeth == DTLS_server_method()) {
  857. /*
  858. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  859. * level 0
  860. */
  861. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  862. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  863. "DEFAULT:@SECLEVEL=0")))
  864. goto end;
  865. }
  866. #endif
  867. if (read_ahead) {
  868. /*
  869. * Test that read_ahead works correctly when dealing with large
  870. * records
  871. */
  872. SSL_CTX_set_read_ahead(cctx, 1);
  873. }
  874. if (!ssl_ctx_add_large_cert_chain(libctx, sctx, cert))
  875. goto end;
  876. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  877. NULL, NULL))
  878. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  879. SSL_ERROR_NONE)))
  880. goto end;
  881. /*
  882. * Calling SSL_clear() first is not required but this tests that SSL_clear()
  883. * doesn't leak.
  884. */
  885. if (!TEST_true(SSL_clear(serverssl)))
  886. goto end;
  887. testresult = 1;
  888. end:
  889. SSL_free(serverssl);
  890. SSL_free(clientssl);
  891. SSL_CTX_free(sctx);
  892. SSL_CTX_free(cctx);
  893. return testresult;
  894. }
  895. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
  896. !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
  897. /* sock must be connected */
  898. static int ktls_chk_platform(int sock)
  899. {
  900. if (!ktls_enable(sock))
  901. return 0;
  902. return 1;
  903. }
  904. static int ping_pong_query(SSL *clientssl, SSL *serverssl)
  905. {
  906. static char count = 1;
  907. unsigned char cbuf[16000] = {0};
  908. unsigned char sbuf[16000];
  909. size_t err = 0;
  910. char crec_wseq_before[SEQ_NUM_SIZE];
  911. char crec_wseq_after[SEQ_NUM_SIZE];
  912. char crec_rseq_before[SEQ_NUM_SIZE];
  913. char crec_rseq_after[SEQ_NUM_SIZE];
  914. char srec_wseq_before[SEQ_NUM_SIZE];
  915. char srec_wseq_after[SEQ_NUM_SIZE];
  916. char srec_rseq_before[SEQ_NUM_SIZE];
  917. char srec_rseq_after[SEQ_NUM_SIZE];
  918. SSL_CONNECTION *clientsc, *serversc;
  919. if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
  920. || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  921. goto end;
  922. cbuf[0] = count++;
  923. memcpy(crec_wseq_before, &clientsc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  924. memcpy(srec_wseq_before, &serversc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  925. memcpy(crec_rseq_before, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  926. memcpy(srec_rseq_before, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  927. if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
  928. goto end;
  929. while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
  930. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
  931. goto end;
  932. }
  933. }
  934. if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
  935. goto end;
  936. while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
  937. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
  938. goto end;
  939. }
  940. }
  941. memcpy(crec_wseq_after, &clientsc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  942. memcpy(srec_wseq_after, &serversc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  943. memcpy(crec_rseq_after, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  944. memcpy(srec_rseq_after, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  945. /* verify the payload */
  946. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  947. goto end;
  948. /*
  949. * If ktls is used then kernel sequences are used instead of
  950. * OpenSSL sequences
  951. */
  952. if (!BIO_get_ktls_send(clientsc->wbio)) {
  953. if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
  954. crec_wseq_after, SEQ_NUM_SIZE))
  955. goto end;
  956. } else {
  957. if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
  958. crec_wseq_after, SEQ_NUM_SIZE))
  959. goto end;
  960. }
  961. if (!BIO_get_ktls_send(serversc->wbio)) {
  962. if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
  963. srec_wseq_after, SEQ_NUM_SIZE))
  964. goto end;
  965. } else {
  966. if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
  967. srec_wseq_after, SEQ_NUM_SIZE))
  968. goto end;
  969. }
  970. if (!BIO_get_ktls_recv(clientsc->wbio)) {
  971. if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
  972. crec_rseq_after, SEQ_NUM_SIZE))
  973. goto end;
  974. } else {
  975. if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
  976. crec_rseq_after, SEQ_NUM_SIZE))
  977. goto end;
  978. }
  979. if (!BIO_get_ktls_recv(serversc->wbio)) {
  980. if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
  981. srec_rseq_after, SEQ_NUM_SIZE))
  982. goto end;
  983. } else {
  984. if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
  985. srec_rseq_after, SEQ_NUM_SIZE))
  986. goto end;
  987. }
  988. return 1;
  989. end:
  990. return 0;
  991. }
  992. static int execute_test_ktls(int cis_ktls, int sis_ktls,
  993. int tls_version, const char *cipher)
  994. {
  995. SSL_CTX *cctx = NULL, *sctx = NULL;
  996. SSL *clientssl = NULL, *serverssl = NULL;
  997. int ktls_used = 0, testresult = 0;
  998. int cfd = -1, sfd = -1;
  999. int rx_supported;
  1000. SSL_CONNECTION *clientsc, *serversc;
  1001. unsigned char *buf = NULL;
  1002. const size_t bufsz = SSL3_RT_MAX_PLAIN_LENGTH + 16;
  1003. int ret;
  1004. size_t offset = 0, i;
  1005. if (!TEST_true(create_test_sockets(&cfd, &sfd, SOCK_STREAM, NULL)))
  1006. goto end;
  1007. /* Skip this test if the platform does not support ktls */
  1008. if (!ktls_chk_platform(cfd)) {
  1009. testresult = TEST_skip("Kernel does not support KTLS");
  1010. goto end;
  1011. }
  1012. if (is_fips && strstr(cipher, "CHACHA") != NULL) {
  1013. testresult = TEST_skip("CHACHA is not supported in FIPS");
  1014. goto end;
  1015. }
  1016. /* Create a session based on SHA-256 */
  1017. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1018. TLS_client_method(),
  1019. tls_version, tls_version,
  1020. &sctx, &cctx, cert, privkey)))
  1021. goto end;
  1022. if (tls_version == TLS1_3_VERSION) {
  1023. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
  1024. || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
  1025. goto end;
  1026. } else {
  1027. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1028. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
  1029. goto end;
  1030. }
  1031. if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1032. &clientssl, sfd, cfd)))
  1033. goto end;
  1034. if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
  1035. || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1036. goto end;
  1037. if (cis_ktls) {
  1038. if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
  1039. goto end;
  1040. }
  1041. if (sis_ktls) {
  1042. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
  1043. goto end;
  1044. }
  1045. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  1046. goto end;
  1047. /*
  1048. * The running kernel may not support a given cipher suite
  1049. * or direction, so just check that KTLS isn't used when it
  1050. * isn't enabled.
  1051. */
  1052. if (!cis_ktls) {
  1053. if (!TEST_false(BIO_get_ktls_send(clientsc->wbio)))
  1054. goto end;
  1055. } else {
  1056. if (BIO_get_ktls_send(clientsc->wbio))
  1057. ktls_used = 1;
  1058. }
  1059. if (!sis_ktls) {
  1060. if (!TEST_false(BIO_get_ktls_send(serversc->wbio)))
  1061. goto end;
  1062. } else {
  1063. if (BIO_get_ktls_send(serversc->wbio))
  1064. ktls_used = 1;
  1065. }
  1066. #if defined(OPENSSL_NO_KTLS_RX)
  1067. rx_supported = 0;
  1068. #else
  1069. rx_supported = 1;
  1070. #endif
  1071. if (!cis_ktls || !rx_supported) {
  1072. if (!TEST_false(BIO_get_ktls_recv(clientsc->rbio)))
  1073. goto end;
  1074. } else {
  1075. if (BIO_get_ktls_send(clientsc->rbio))
  1076. ktls_used = 1;
  1077. }
  1078. if (!sis_ktls || !rx_supported) {
  1079. if (!TEST_false(BIO_get_ktls_recv(serversc->rbio)))
  1080. goto end;
  1081. } else {
  1082. if (BIO_get_ktls_send(serversc->rbio))
  1083. ktls_used = 1;
  1084. }
  1085. if ((cis_ktls || sis_ktls) && !ktls_used) {
  1086. testresult = TEST_skip("KTLS not supported for %s cipher %s",
  1087. tls_version == TLS1_3_VERSION ? "TLS 1.3" :
  1088. "TLS 1.2", cipher);
  1089. goto end;
  1090. }
  1091. if (!TEST_true(ping_pong_query(clientssl, serverssl)))
  1092. goto end;
  1093. buf = OPENSSL_zalloc(bufsz);
  1094. if (!TEST_ptr(buf))
  1095. goto end;
  1096. /*
  1097. * Write some data that exceeds the maximum record length. KTLS may choose
  1098. * to coalesce this data into a single buffer when we read it again.
  1099. */
  1100. while ((ret = SSL_write(clientssl, buf, bufsz)) != (int)bufsz) {
  1101. if (!TEST_true(SSL_get_error(clientssl, ret) == SSL_ERROR_WANT_WRITE))
  1102. goto end;
  1103. }
  1104. /* Now check that we can read all the data we wrote */
  1105. do {
  1106. ret = SSL_read(serverssl, buf + offset, bufsz - offset);
  1107. if (ret <= 0) {
  1108. if (!TEST_true(SSL_get_error(serverssl, ret) == SSL_ERROR_WANT_READ))
  1109. goto end;
  1110. } else {
  1111. offset += ret;
  1112. }
  1113. } while (offset < bufsz);
  1114. if (!TEST_true(offset == bufsz))
  1115. goto end;
  1116. for (i = 0; i < bufsz; i++)
  1117. if (!TEST_true(buf[i] == 0))
  1118. goto end;
  1119. testresult = 1;
  1120. end:
  1121. OPENSSL_free(buf);
  1122. if (clientssl) {
  1123. SSL_shutdown(clientssl);
  1124. SSL_free(clientssl);
  1125. }
  1126. if (serverssl) {
  1127. SSL_shutdown(serverssl);
  1128. SSL_free(serverssl);
  1129. }
  1130. SSL_CTX_free(sctx);
  1131. SSL_CTX_free(cctx);
  1132. serverssl = clientssl = NULL;
  1133. if (cfd != -1)
  1134. close(cfd);
  1135. if (sfd != -1)
  1136. close(sfd);
  1137. return testresult;
  1138. }
  1139. #define SENDFILE_SZ (16 * 4096)
  1140. #define SENDFILE_CHUNK (4 * 4096)
  1141. #define min(a,b) ((a) > (b) ? (b) : (a))
  1142. static int execute_test_ktls_sendfile(int tls_version, const char *cipher,
  1143. int zerocopy)
  1144. {
  1145. SSL_CTX *cctx = NULL, *sctx = NULL;
  1146. SSL *clientssl = NULL, *serverssl = NULL;
  1147. unsigned char *buf, *buf_dst;
  1148. BIO *out = NULL, *in = NULL;
  1149. int cfd = -1, sfd = -1, ffd, err;
  1150. ssize_t chunk_size = 0;
  1151. off_t chunk_off = 0;
  1152. int testresult = 0;
  1153. FILE *ffdp;
  1154. SSL_CONNECTION *serversc;
  1155. buf = OPENSSL_zalloc(SENDFILE_SZ);
  1156. buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
  1157. if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
  1158. || !TEST_true(create_test_sockets(&cfd, &sfd, SOCK_STREAM, NULL)))
  1159. goto end;
  1160. /* Skip this test if the platform does not support ktls */
  1161. if (!ktls_chk_platform(sfd)) {
  1162. testresult = TEST_skip("Kernel does not support KTLS");
  1163. goto end;
  1164. }
  1165. if (is_fips && strstr(cipher, "CHACHA") != NULL) {
  1166. testresult = TEST_skip("CHACHA is not supported in FIPS");
  1167. goto end;
  1168. }
  1169. /* Create a session based on SHA-256 */
  1170. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1171. TLS_client_method(),
  1172. tls_version, tls_version,
  1173. &sctx, &cctx, cert, privkey)))
  1174. goto end;
  1175. if (tls_version == TLS1_3_VERSION) {
  1176. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
  1177. || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
  1178. goto end;
  1179. } else {
  1180. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1181. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
  1182. goto end;
  1183. }
  1184. if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1185. &clientssl, sfd, cfd)))
  1186. goto end;
  1187. if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1188. goto end;
  1189. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
  1190. goto end;
  1191. if (zerocopy) {
  1192. if (!TEST_true(SSL_set_options(serverssl,
  1193. SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE)))
  1194. goto end;
  1195. }
  1196. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1197. SSL_ERROR_NONE)))
  1198. goto end;
  1199. if (!BIO_get_ktls_send(serversc->wbio)) {
  1200. testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
  1201. tls_version == TLS1_3_VERSION ? "TLS 1.3" :
  1202. "TLS 1.2", cipher);
  1203. goto end;
  1204. }
  1205. if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
  1206. goto end;
  1207. out = BIO_new_file(tmpfilename, "wb");
  1208. if (!TEST_ptr(out))
  1209. goto end;
  1210. if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
  1211. goto end;
  1212. BIO_free(out);
  1213. out = NULL;
  1214. in = BIO_new_file(tmpfilename, "rb");
  1215. BIO_get_fp(in, &ffdp);
  1216. ffd = fileno(ffdp);
  1217. while (chunk_off < SENDFILE_SZ) {
  1218. chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
  1219. while ((err = SSL_sendfile(serverssl,
  1220. ffd,
  1221. chunk_off,
  1222. chunk_size,
  1223. 0)) != chunk_size) {
  1224. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
  1225. goto end;
  1226. }
  1227. while ((err = SSL_read(clientssl,
  1228. buf_dst + chunk_off,
  1229. chunk_size)) != chunk_size) {
  1230. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
  1231. goto end;
  1232. }
  1233. /* verify the payload */
  1234. if (!TEST_mem_eq(buf_dst + chunk_off,
  1235. chunk_size,
  1236. buf + chunk_off,
  1237. chunk_size))
  1238. goto end;
  1239. chunk_off += chunk_size;
  1240. }
  1241. testresult = 1;
  1242. end:
  1243. if (clientssl) {
  1244. SSL_shutdown(clientssl);
  1245. SSL_free(clientssl);
  1246. }
  1247. if (serverssl) {
  1248. SSL_shutdown(serverssl);
  1249. SSL_free(serverssl);
  1250. }
  1251. SSL_CTX_free(sctx);
  1252. SSL_CTX_free(cctx);
  1253. serverssl = clientssl = NULL;
  1254. BIO_free(out);
  1255. BIO_free(in);
  1256. if (cfd != -1)
  1257. close(cfd);
  1258. if (sfd != -1)
  1259. close(sfd);
  1260. OPENSSL_free(buf);
  1261. OPENSSL_free(buf_dst);
  1262. return testresult;
  1263. }
  1264. static struct ktls_test_cipher {
  1265. int tls_version;
  1266. const char *cipher;
  1267. } ktls_test_ciphers[] = {
  1268. # if !defined(OPENSSL_NO_TLS1_2)
  1269. # ifdef OPENSSL_KTLS_AES_GCM_128
  1270. { TLS1_2_VERSION, "AES128-GCM-SHA256" },
  1271. # endif
  1272. # ifdef OPENSSL_KTLS_AES_CCM_128
  1273. { TLS1_2_VERSION, "AES128-CCM"},
  1274. # endif
  1275. # ifdef OPENSSL_KTLS_AES_GCM_256
  1276. { TLS1_2_VERSION, "AES256-GCM-SHA384"},
  1277. # endif
  1278. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  1279. # ifndef OPENSSL_NO_EC
  1280. { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
  1281. # endif
  1282. # endif
  1283. # endif
  1284. # if !defined(OSSL_NO_USABLE_TLS1_3)
  1285. # ifdef OPENSSL_KTLS_AES_GCM_128
  1286. { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
  1287. # endif
  1288. # ifdef OPENSSL_KTLS_AES_CCM_128
  1289. { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
  1290. # endif
  1291. # ifdef OPENSSL_KTLS_AES_GCM_256
  1292. { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
  1293. # endif
  1294. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  1295. { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
  1296. # endif
  1297. # endif
  1298. };
  1299. #define NUM_KTLS_TEST_CIPHERS \
  1300. (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
  1301. static int test_ktls(int test)
  1302. {
  1303. struct ktls_test_cipher *cipher;
  1304. int cis_ktls, sis_ktls;
  1305. OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
  1306. cipher = &ktls_test_ciphers[test / 4];
  1307. cis_ktls = (test & 1) != 0;
  1308. sis_ktls = (test & 2) != 0;
  1309. return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
  1310. cipher->cipher);
  1311. }
  1312. static int test_ktls_sendfile(int test)
  1313. {
  1314. struct ktls_test_cipher *cipher;
  1315. int tst = test >> 1;
  1316. OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
  1317. cipher = &ktls_test_ciphers[tst];
  1318. return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher,
  1319. test & 1);
  1320. }
  1321. #endif
  1322. static int test_large_message_tls(void)
  1323. {
  1324. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1325. TLS1_VERSION, 0, 0);
  1326. }
  1327. static int test_large_message_tls_read_ahead(void)
  1328. {
  1329. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1330. TLS1_VERSION, 0, 1);
  1331. }
  1332. #ifndef OPENSSL_NO_DTLS
  1333. static int test_large_message_dtls(void)
  1334. {
  1335. # ifdef OPENSSL_NO_DTLS1_2
  1336. /* Not supported in the FIPS provider */
  1337. if (is_fips)
  1338. return 1;
  1339. # endif
  1340. /*
  1341. * read_ahead is not relevant to DTLS because DTLS always acts as if
  1342. * read_ahead is set.
  1343. */
  1344. return execute_test_large_message(DTLS_server_method(),
  1345. DTLS_client_method(),
  1346. DTLS1_VERSION, 0, 0);
  1347. }
  1348. #endif
  1349. /*
  1350. * Test we can successfully send the maximum amount of application data. We
  1351. * test each protocol version individually, each with and without EtM enabled.
  1352. * TLSv1.3 doesn't use EtM so technically it is redundant to test both but it is
  1353. * simpler this way. We also test all combinations with and without the
  1354. * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS option which affects the size of the
  1355. * underlying buffer.
  1356. */
  1357. static int test_large_app_data(int tst)
  1358. {
  1359. SSL_CTX *cctx = NULL, *sctx = NULL;
  1360. SSL *clientssl = NULL, *serverssl = NULL;
  1361. int testresult = 0, prot;
  1362. unsigned char *msg, *buf = NULL;
  1363. size_t written, readbytes;
  1364. const SSL_METHOD *smeth = TLS_server_method();
  1365. const SSL_METHOD *cmeth = TLS_client_method();
  1366. switch (tst >> 2) {
  1367. case 0:
  1368. #ifndef OSSL_NO_USABLE_TLS1_3
  1369. prot = TLS1_3_VERSION;
  1370. break;
  1371. #else
  1372. return 1;
  1373. #endif
  1374. case 1:
  1375. #ifndef OPENSSL_NO_TLS1_2
  1376. prot = TLS1_2_VERSION;
  1377. break;
  1378. #else
  1379. return 1;
  1380. #endif
  1381. case 2:
  1382. #ifndef OPENSSL_NO_TLS1_1
  1383. prot = TLS1_1_VERSION;
  1384. break;
  1385. #else
  1386. return 1;
  1387. #endif
  1388. case 3:
  1389. #ifndef OPENSSL_NO_TLS1
  1390. prot = TLS1_VERSION;
  1391. break;
  1392. #else
  1393. return 1;
  1394. #endif
  1395. case 4:
  1396. #ifndef OPENSSL_NO_SSL3
  1397. prot = SSL3_VERSION;
  1398. break;
  1399. #else
  1400. return 1;
  1401. #endif
  1402. case 5:
  1403. #ifndef OPENSSL_NO_DTLS1_2
  1404. prot = DTLS1_2_VERSION;
  1405. smeth = DTLS_server_method();
  1406. cmeth = DTLS_client_method();
  1407. break;
  1408. #else
  1409. return 1;
  1410. #endif
  1411. case 6:
  1412. #ifndef OPENSSL_NO_DTLS1
  1413. prot = DTLS1_VERSION;
  1414. smeth = DTLS_server_method();
  1415. cmeth = DTLS_client_method();
  1416. break;
  1417. #else
  1418. return 1;
  1419. #endif
  1420. default:
  1421. /* Shouldn't happen */
  1422. return 0;
  1423. }
  1424. if ((prot < TLS1_2_VERSION || prot == DTLS1_VERSION) && is_fips)
  1425. return 1;
  1426. /* Maximal sized message of zeros */
  1427. msg = OPENSSL_zalloc(SSL3_RT_MAX_PLAIN_LENGTH);
  1428. if (!TEST_ptr(msg))
  1429. goto end;
  1430. buf = OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH + 1);
  1431. if (!TEST_ptr(buf))
  1432. goto end;
  1433. /* Set whole buffer to all bits set */
  1434. memset(buf, 0xff, SSL3_RT_MAX_PLAIN_LENGTH + 1);
  1435. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, prot, prot,
  1436. &sctx, &cctx, cert, privkey)))
  1437. goto end;
  1438. if (prot < TLS1_2_VERSION || prot == DTLS1_VERSION) {
  1439. /* Older protocol versions need SECLEVEL=0 due to SHA1 usage */
  1440. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))
  1441. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  1442. "DEFAULT:@SECLEVEL=0")))
  1443. goto end;
  1444. }
  1445. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1446. &clientssl, NULL, NULL)))
  1447. goto end;
  1448. if ((tst & 1) != 0) {
  1449. /* Setting this option gives us a minimally sized underlying buffer */
  1450. if (!TEST_true(SSL_set_options(serverssl,
  1451. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  1452. || !TEST_true(SSL_set_options(clientssl,
  1453. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)))
  1454. goto end;
  1455. }
  1456. if ((tst & 2) != 0) {
  1457. /*
  1458. * Setting this option means the MAC is added before encryption
  1459. * giving us a larger record for the encryption process
  1460. */
  1461. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC))
  1462. || !TEST_true(SSL_set_options(clientssl,
  1463. SSL_OP_NO_ENCRYPT_THEN_MAC)))
  1464. goto end;
  1465. }
  1466. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  1467. goto end;
  1468. if (!TEST_true(SSL_write_ex(clientssl, msg, SSL3_RT_MAX_PLAIN_LENGTH,
  1469. &written))
  1470. || !TEST_size_t_eq(written, SSL3_RT_MAX_PLAIN_LENGTH))
  1471. goto end;
  1472. /* We provide a buffer slightly larger than what we are actually expecting */
  1473. if (!TEST_true(SSL_read_ex(serverssl, buf, SSL3_RT_MAX_PLAIN_LENGTH + 1,
  1474. &readbytes)))
  1475. goto end;
  1476. if (!TEST_mem_eq(msg, written, buf, readbytes))
  1477. goto end;
  1478. testresult = 1;
  1479. end:
  1480. OPENSSL_free(msg);
  1481. OPENSSL_free(buf);
  1482. SSL_free(serverssl);
  1483. SSL_free(clientssl);
  1484. SSL_CTX_free(sctx);
  1485. SSL_CTX_free(cctx);
  1486. return testresult;
  1487. }
  1488. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) \
  1489. || !defined(OPENSSL_NO_DTLS)
  1490. static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
  1491. const SSL_METHOD *cmeth,
  1492. int min_version, int max_version)
  1493. {
  1494. size_t i;
  1495. SSL_CTX *cctx = NULL, *sctx = NULL;
  1496. SSL *clientssl = NULL, *serverssl = NULL;
  1497. int testresult = 0;
  1498. const unsigned char *zbuf;
  1499. SSL_CONNECTION *serversc;
  1500. TLS_RECORD *rr;
  1501. static unsigned char cbuf[16000];
  1502. static unsigned char sbuf[16000];
  1503. if (!TEST_true(create_ssl_ctx_pair(libctx,
  1504. smeth, cmeth,
  1505. min_version, max_version,
  1506. &sctx, &cctx, cert,
  1507. privkey)))
  1508. goto end;
  1509. # ifdef OPENSSL_NO_DTLS1_2
  1510. if (smeth == DTLS_server_method()) {
  1511. /* Not supported in the FIPS provider */
  1512. if (is_fips) {
  1513. testresult = 1;
  1514. goto end;
  1515. };
  1516. /*
  1517. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  1518. * level 0
  1519. */
  1520. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  1521. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  1522. "DEFAULT:@SECLEVEL=0")))
  1523. goto end;
  1524. }
  1525. # endif
  1526. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1527. NULL, NULL)))
  1528. goto end;
  1529. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
  1530. goto end;
  1531. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1532. SSL_ERROR_NONE)))
  1533. goto end;
  1534. for (i = 0; i < sizeof(cbuf); i++) {
  1535. cbuf[i] = i & 0xff;
  1536. }
  1537. if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
  1538. goto end;
  1539. if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1540. goto end;
  1541. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  1542. goto end;
  1543. /*
  1544. * Since we called SSL_peek(), we know the data in the record
  1545. * layer is a plaintext record. We can gather the pointer to check
  1546. * for zeroization after SSL_read().
  1547. */
  1548. if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1549. goto end;
  1550. rr = serversc->rlayer.tlsrecs;
  1551. zbuf = &rr->data[rr->off];
  1552. if (!TEST_int_eq(rr->length, sizeof(cbuf)))
  1553. goto end;
  1554. /*
  1555. * After SSL_peek() the plaintext must still be stored in the
  1556. * record.
  1557. */
  1558. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1559. goto end;
  1560. memset(sbuf, 0, sizeof(sbuf));
  1561. if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1562. goto end;
  1563. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
  1564. goto end;
  1565. /* Check if rbuf is cleansed */
  1566. memset(cbuf, 0, sizeof(cbuf));
  1567. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1568. goto end;
  1569. testresult = 1;
  1570. end:
  1571. SSL_free(serverssl);
  1572. SSL_free(clientssl);
  1573. SSL_CTX_free(sctx);
  1574. SSL_CTX_free(cctx);
  1575. return testresult;
  1576. }
  1577. #endif /*
  1578. * !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  1579. * || !defined(OPENSSL_NO_DTLS)
  1580. */
  1581. static int test_cleanse_plaintext(void)
  1582. {
  1583. #if !defined(OPENSSL_NO_TLS1_2)
  1584. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1585. TLS_client_method(),
  1586. TLS1_2_VERSION,
  1587. TLS1_2_VERSION)))
  1588. return 0;
  1589. #endif
  1590. #if !defined(OSSL_NO_USABLE_TLS1_3)
  1591. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1592. TLS_client_method(),
  1593. TLS1_3_VERSION,
  1594. TLS1_3_VERSION)))
  1595. return 0;
  1596. #endif
  1597. #if !defined(OPENSSL_NO_DTLS)
  1598. if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
  1599. DTLS_client_method(),
  1600. DTLS1_VERSION,
  1601. 0)))
  1602. return 0;
  1603. #endif
  1604. return 1;
  1605. }
  1606. #ifndef OPENSSL_NO_OCSP
  1607. static int ocsp_server_cb(SSL *s, void *arg)
  1608. {
  1609. int *argi = (int *)arg;
  1610. unsigned char *copy = NULL;
  1611. STACK_OF(OCSP_RESPID) *ids = NULL;
  1612. OCSP_RESPID *id = NULL;
  1613. if (*argi == 2) {
  1614. /* In this test we are expecting exactly 1 OCSP_RESPID */
  1615. SSL_get_tlsext_status_ids(s, &ids);
  1616. if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
  1617. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1618. id = sk_OCSP_RESPID_value(ids, 0);
  1619. if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
  1620. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1621. } else if (*argi != 1) {
  1622. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1623. }
  1624. if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
  1625. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1626. if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
  1627. sizeof(orespder)))) {
  1628. OPENSSL_free(copy);
  1629. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1630. }
  1631. ocsp_server_called = 1;
  1632. return SSL_TLSEXT_ERR_OK;
  1633. }
  1634. static int ocsp_client_cb(SSL *s, void *arg)
  1635. {
  1636. int *argi = (int *)arg;
  1637. const unsigned char *respderin;
  1638. size_t len;
  1639. if (*argi != 1 && *argi != 2)
  1640. return 0;
  1641. len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
  1642. if (!TEST_mem_eq(orespder, len, respderin, len))
  1643. return 0;
  1644. ocsp_client_called = 1;
  1645. return 1;
  1646. }
  1647. static int test_tlsext_status_type(void)
  1648. {
  1649. SSL_CTX *cctx = NULL, *sctx = NULL;
  1650. SSL *clientssl = NULL, *serverssl = NULL;
  1651. int testresult = 0;
  1652. STACK_OF(OCSP_RESPID) *ids = NULL;
  1653. OCSP_RESPID *id = NULL;
  1654. BIO *certbio = NULL;
  1655. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  1656. TLS1_VERSION, 0,
  1657. &sctx, &cctx, cert, privkey))
  1658. return 0;
  1659. if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
  1660. goto end;
  1661. /* First just do various checks getting and setting tlsext_status_type */
  1662. clientssl = SSL_new(cctx);
  1663. if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
  1664. || !TEST_true(SSL_set_tlsext_status_type(clientssl,
  1665. TLSEXT_STATUSTYPE_ocsp))
  1666. || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
  1667. TLSEXT_STATUSTYPE_ocsp))
  1668. goto end;
  1669. SSL_free(clientssl);
  1670. clientssl = NULL;
  1671. if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
  1672. || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
  1673. goto end;
  1674. clientssl = SSL_new(cctx);
  1675. if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
  1676. goto end;
  1677. SSL_free(clientssl);
  1678. clientssl = NULL;
  1679. /*
  1680. * Now actually do a handshake and check OCSP information is exchanged and
  1681. * the callbacks get called
  1682. */
  1683. SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
  1684. SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
  1685. SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
  1686. SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
  1687. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1688. &clientssl, NULL, NULL))
  1689. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  1690. SSL_ERROR_NONE))
  1691. || !TEST_true(ocsp_client_called)
  1692. || !TEST_true(ocsp_server_called))
  1693. goto end;
  1694. SSL_free(serverssl);
  1695. SSL_free(clientssl);
  1696. serverssl = NULL;
  1697. clientssl = NULL;
  1698. /* Try again but this time force the server side callback to fail */
  1699. ocsp_client_called = 0;
  1700. ocsp_server_called = 0;
  1701. cdummyarg = 0;
  1702. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1703. &clientssl, NULL, NULL))
  1704. /* This should fail because the callback will fail */
  1705. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  1706. SSL_ERROR_NONE))
  1707. || !TEST_false(ocsp_client_called)
  1708. || !TEST_false(ocsp_server_called))
  1709. goto end;
  1710. SSL_free(serverssl);
  1711. SSL_free(clientssl);
  1712. serverssl = NULL;
  1713. clientssl = NULL;
  1714. /*
  1715. * This time we'll get the client to send an OCSP_RESPID that it will
  1716. * accept.
  1717. */
  1718. ocsp_client_called = 0;
  1719. ocsp_server_called = 0;
  1720. cdummyarg = 2;
  1721. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1722. &clientssl, NULL, NULL)))
  1723. goto end;
  1724. /*
  1725. * We'll just use any old cert for this test - it doesn't have to be an OCSP
  1726. * specific one. We'll use the server cert.
  1727. */
  1728. if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
  1729. || !TEST_ptr(id = OCSP_RESPID_new())
  1730. || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
  1731. || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
  1732. || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
  1733. || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
  1734. || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
  1735. goto end;
  1736. id = NULL;
  1737. SSL_set_tlsext_status_ids(clientssl, ids);
  1738. /* Control has been transferred */
  1739. ids = NULL;
  1740. BIO_free(certbio);
  1741. certbio = NULL;
  1742. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1743. SSL_ERROR_NONE))
  1744. || !TEST_true(ocsp_client_called)
  1745. || !TEST_true(ocsp_server_called))
  1746. goto end;
  1747. testresult = 1;
  1748. end:
  1749. SSL_free(serverssl);
  1750. SSL_free(clientssl);
  1751. SSL_CTX_free(sctx);
  1752. SSL_CTX_free(cctx);
  1753. sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
  1754. OCSP_RESPID_free(id);
  1755. BIO_free(certbio);
  1756. X509_free(ocspcert);
  1757. ocspcert = NULL;
  1758. return testresult;
  1759. }
  1760. #endif
  1761. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  1762. static int new_called, remove_called, get_called;
  1763. static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
  1764. {
  1765. new_called++;
  1766. /*
  1767. * sess has been up-refed for us, but we don't actually need it so free it
  1768. * immediately.
  1769. */
  1770. SSL_SESSION_free(sess);
  1771. return 1;
  1772. }
  1773. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  1774. {
  1775. remove_called++;
  1776. }
  1777. static SSL_SESSION *get_sess_val = NULL;
  1778. static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
  1779. int *copy)
  1780. {
  1781. get_called++;
  1782. *copy = 1;
  1783. return get_sess_val;
  1784. }
  1785. static int execute_test_session(int maxprot, int use_int_cache,
  1786. int use_ext_cache, long s_options)
  1787. {
  1788. SSL_CTX *sctx = NULL, *cctx = NULL;
  1789. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  1790. SSL *serverssl2 = NULL, *clientssl2 = NULL;
  1791. # ifndef OPENSSL_NO_TLS1_1
  1792. SSL *serverssl3 = NULL, *clientssl3 = NULL;
  1793. # endif
  1794. SSL_SESSION *sess1 = NULL, *sess2 = NULL;
  1795. int testresult = 0, numnewsesstick = 1;
  1796. new_called = remove_called = 0;
  1797. /* TLSv1.3 sends 2 NewSessionTickets */
  1798. if (maxprot == TLS1_3_VERSION)
  1799. numnewsesstick = 2;
  1800. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1801. TLS_client_method(), TLS1_VERSION, 0,
  1802. &sctx, &cctx, cert, privkey)))
  1803. return 0;
  1804. /*
  1805. * Only allow the max protocol version so we can force a connection failure
  1806. * later
  1807. */
  1808. SSL_CTX_set_min_proto_version(cctx, maxprot);
  1809. SSL_CTX_set_max_proto_version(cctx, maxprot);
  1810. /* Set up session cache */
  1811. if (use_ext_cache) {
  1812. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  1813. SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
  1814. }
  1815. if (use_int_cache) {
  1816. /* Also covers instance where both are set */
  1817. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
  1818. } else {
  1819. SSL_CTX_set_session_cache_mode(cctx,
  1820. SSL_SESS_CACHE_CLIENT
  1821. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1822. }
  1823. if (s_options) {
  1824. SSL_CTX_set_options(sctx, s_options);
  1825. }
  1826. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1827. NULL, NULL))
  1828. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1829. SSL_ERROR_NONE))
  1830. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
  1831. goto end;
  1832. /* Should fail because it should already be in the cache */
  1833. if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
  1834. goto end;
  1835. if (use_ext_cache
  1836. && (!TEST_int_eq(new_called, numnewsesstick)
  1837. || !TEST_int_eq(remove_called, 0)))
  1838. goto end;
  1839. new_called = remove_called = 0;
  1840. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1841. &clientssl2, NULL, NULL))
  1842. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1843. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1844. SSL_ERROR_NONE))
  1845. || !TEST_true(SSL_session_reused(clientssl2)))
  1846. goto end;
  1847. if (maxprot == TLS1_3_VERSION) {
  1848. /*
  1849. * In TLSv1.3 we should have created a new session even though we have
  1850. * resumed. Since we attempted a resume we should also have removed the
  1851. * old ticket from the cache so that we try to only use tickets once.
  1852. */
  1853. if (use_ext_cache
  1854. && (!TEST_int_eq(new_called, 1)
  1855. || !TEST_int_eq(remove_called, 1)))
  1856. goto end;
  1857. } else {
  1858. /*
  1859. * In TLSv1.2 we expect to have resumed so no sessions added or
  1860. * removed.
  1861. */
  1862. if (use_ext_cache
  1863. && (!TEST_int_eq(new_called, 0)
  1864. || !TEST_int_eq(remove_called, 0)))
  1865. goto end;
  1866. }
  1867. SSL_SESSION_free(sess1);
  1868. if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
  1869. goto end;
  1870. shutdown_ssl_connection(serverssl2, clientssl2);
  1871. serverssl2 = clientssl2 = NULL;
  1872. new_called = remove_called = 0;
  1873. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1874. &clientssl2, NULL, NULL))
  1875. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1876. SSL_ERROR_NONE)))
  1877. goto end;
  1878. if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
  1879. goto end;
  1880. if (use_ext_cache
  1881. && (!TEST_int_eq(new_called, numnewsesstick)
  1882. || !TEST_int_eq(remove_called, 0)))
  1883. goto end;
  1884. new_called = remove_called = 0;
  1885. /*
  1886. * This should clear sess2 from the cache because it is a "bad" session.
  1887. * See SSL_set_session() documentation.
  1888. */
  1889. if (!TEST_true(SSL_set_session(clientssl2, sess1)))
  1890. goto end;
  1891. if (use_ext_cache
  1892. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1893. goto end;
  1894. if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
  1895. goto end;
  1896. if (use_int_cache) {
  1897. /* Should succeeded because it should not already be in the cache */
  1898. if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
  1899. || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
  1900. goto end;
  1901. }
  1902. new_called = remove_called = 0;
  1903. /* This shouldn't be in the cache so should fail */
  1904. if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
  1905. goto end;
  1906. if (use_ext_cache
  1907. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1908. goto end;
  1909. # if !defined(OPENSSL_NO_TLS1_1)
  1910. new_called = remove_called = 0;
  1911. /* Force a connection failure */
  1912. SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
  1913. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
  1914. &clientssl3, NULL, NULL))
  1915. || !TEST_true(SSL_set_session(clientssl3, sess1))
  1916. /* This should fail because of the mismatched protocol versions */
  1917. || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
  1918. SSL_ERROR_NONE)))
  1919. goto end;
  1920. /* We should have automatically removed the session from the cache */
  1921. if (use_ext_cache
  1922. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1923. goto end;
  1924. /* Should succeed because it should not already be in the cache */
  1925. if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
  1926. goto end;
  1927. # endif
  1928. /* Now do some tests for server side caching */
  1929. if (use_ext_cache) {
  1930. SSL_CTX_sess_set_new_cb(cctx, NULL);
  1931. SSL_CTX_sess_set_remove_cb(cctx, NULL);
  1932. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  1933. SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
  1934. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  1935. get_sess_val = NULL;
  1936. }
  1937. SSL_CTX_set_session_cache_mode(cctx, 0);
  1938. /* Internal caching is the default on the server side */
  1939. if (!use_int_cache)
  1940. SSL_CTX_set_session_cache_mode(sctx,
  1941. SSL_SESS_CACHE_SERVER
  1942. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1943. SSL_free(serverssl1);
  1944. SSL_free(clientssl1);
  1945. serverssl1 = clientssl1 = NULL;
  1946. SSL_free(serverssl2);
  1947. SSL_free(clientssl2);
  1948. serverssl2 = clientssl2 = NULL;
  1949. SSL_SESSION_free(sess1);
  1950. sess1 = NULL;
  1951. SSL_SESSION_free(sess2);
  1952. sess2 = NULL;
  1953. SSL_CTX_set_max_proto_version(sctx, maxprot);
  1954. if (maxprot == TLS1_2_VERSION)
  1955. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  1956. new_called = remove_called = get_called = 0;
  1957. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1958. NULL, NULL))
  1959. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1960. SSL_ERROR_NONE))
  1961. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
  1962. || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
  1963. goto end;
  1964. if (use_int_cache) {
  1965. if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
  1966. /*
  1967. * In TLSv1.3 it should not have been added to the internal cache,
  1968. * except in the case where we also have an external cache (in that
  1969. * case it gets added to the cache in order to generate remove
  1970. * events after timeout).
  1971. */
  1972. if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
  1973. goto end;
  1974. } else {
  1975. /* Should fail because it should already be in the cache */
  1976. if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
  1977. goto end;
  1978. }
  1979. }
  1980. if (use_ext_cache) {
  1981. SSL_SESSION *tmp = sess2;
  1982. if (!TEST_int_eq(new_called, numnewsesstick)
  1983. || !TEST_int_eq(remove_called, 0)
  1984. || !TEST_int_eq(get_called, 0))
  1985. goto end;
  1986. /*
  1987. * Delete the session from the internal cache to force a lookup from
  1988. * the external cache. We take a copy first because
  1989. * SSL_CTX_remove_session() also marks the session as non-resumable.
  1990. */
  1991. if (use_int_cache && maxprot != TLS1_3_VERSION) {
  1992. if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
  1993. || !TEST_true(sess2->owner != NULL)
  1994. || !TEST_true(tmp->owner == NULL)
  1995. || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
  1996. goto end;
  1997. SSL_SESSION_free(sess2);
  1998. }
  1999. sess2 = tmp;
  2000. }
  2001. new_called = remove_called = get_called = 0;
  2002. get_sess_val = sess2;
  2003. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  2004. &clientssl2, NULL, NULL))
  2005. || !TEST_true(SSL_set_session(clientssl2, sess1))
  2006. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  2007. SSL_ERROR_NONE))
  2008. || !TEST_true(SSL_session_reused(clientssl2)))
  2009. goto end;
  2010. if (use_ext_cache) {
  2011. if (!TEST_int_eq(remove_called, 0))
  2012. goto end;
  2013. if (maxprot == TLS1_3_VERSION) {
  2014. if (!TEST_int_eq(new_called, 1)
  2015. || !TEST_int_eq(get_called, 0))
  2016. goto end;
  2017. } else {
  2018. if (!TEST_int_eq(new_called, 0)
  2019. || !TEST_int_eq(get_called, 1))
  2020. goto end;
  2021. }
  2022. }
  2023. /*
  2024. * Make a small cache, force out all other sessions but
  2025. * sess2, try to add sess1, which should succeed. Then
  2026. * make sure it's there by checking the owners. Despite
  2027. * the timeouts, sess1 should have kicked out sess2
  2028. */
  2029. /* Make sess1 expire before sess2 */
  2030. if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
  2031. || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
  2032. || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
  2033. || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
  2034. goto end;
  2035. if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
  2036. goto end;
  2037. /* Don't care about results - cache should only be sess2 at end */
  2038. SSL_CTX_add_session(sctx, sess1);
  2039. SSL_CTX_add_session(sctx, sess2);
  2040. /* Now add sess1, and make sure it remains, despite timeout */
  2041. if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
  2042. || !TEST_ptr(sess1->owner)
  2043. || !TEST_ptr_null(sess2->owner))
  2044. goto end;
  2045. testresult = 1;
  2046. end:
  2047. SSL_free(serverssl1);
  2048. SSL_free(clientssl1);
  2049. SSL_free(serverssl2);
  2050. SSL_free(clientssl2);
  2051. # ifndef OPENSSL_NO_TLS1_1
  2052. SSL_free(serverssl3);
  2053. SSL_free(clientssl3);
  2054. # endif
  2055. SSL_SESSION_free(sess1);
  2056. SSL_SESSION_free(sess2);
  2057. SSL_CTX_free(sctx);
  2058. SSL_CTX_free(cctx);
  2059. return testresult;
  2060. }
  2061. #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  2062. static int test_session_with_only_int_cache(void)
  2063. {
  2064. #ifndef OSSL_NO_USABLE_TLS1_3
  2065. if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
  2066. return 0;
  2067. #endif
  2068. #ifndef OPENSSL_NO_TLS1_2
  2069. return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
  2070. #else
  2071. return 1;
  2072. #endif
  2073. }
  2074. static int test_session_with_only_ext_cache(void)
  2075. {
  2076. #ifndef OSSL_NO_USABLE_TLS1_3
  2077. if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
  2078. return 0;
  2079. #endif
  2080. #ifndef OPENSSL_NO_TLS1_2
  2081. return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
  2082. #else
  2083. return 1;
  2084. #endif
  2085. }
  2086. static int test_session_with_both_cache(void)
  2087. {
  2088. #ifndef OSSL_NO_USABLE_TLS1_3
  2089. if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
  2090. return 0;
  2091. #endif
  2092. #ifndef OPENSSL_NO_TLS1_2
  2093. return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
  2094. #else
  2095. return 1;
  2096. #endif
  2097. }
  2098. static int test_session_wo_ca_names(void)
  2099. {
  2100. #ifndef OSSL_NO_USABLE_TLS1_3
  2101. if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
  2102. return 0;
  2103. #endif
  2104. #ifndef OPENSSL_NO_TLS1_2
  2105. return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
  2106. #else
  2107. return 1;
  2108. #endif
  2109. }
  2110. #ifndef OSSL_NO_USABLE_TLS1_3
  2111. static SSL_SESSION *sesscache[6];
  2112. static int do_cache;
  2113. static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
  2114. {
  2115. if (do_cache) {
  2116. sesscache[new_called] = sess;
  2117. } else {
  2118. /* We don't need the reference to the session, so free it */
  2119. SSL_SESSION_free(sess);
  2120. }
  2121. new_called++;
  2122. return 1;
  2123. }
  2124. static int post_handshake_verify(SSL *sssl, SSL *cssl)
  2125. {
  2126. SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
  2127. if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
  2128. return 0;
  2129. /* Start handshake on the server and client */
  2130. if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
  2131. || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
  2132. || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
  2133. || !TEST_true(create_ssl_connection(sssl, cssl,
  2134. SSL_ERROR_NONE)))
  2135. return 0;
  2136. return 1;
  2137. }
  2138. static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
  2139. SSL_CTX **cctx)
  2140. {
  2141. int sess_id_ctx = 1;
  2142. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2143. TLS_client_method(), TLS1_VERSION, 0,
  2144. sctx, cctx, cert, privkey))
  2145. || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
  2146. || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
  2147. (void *)&sess_id_ctx,
  2148. sizeof(sess_id_ctx))))
  2149. return 0;
  2150. if (stateful)
  2151. SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
  2152. SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
  2153. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  2154. SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
  2155. return 1;
  2156. }
  2157. static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
  2158. {
  2159. SSL *serverssl = NULL, *clientssl = NULL;
  2160. int i;
  2161. /* Test that we can resume with all the tickets we got given */
  2162. for (i = 0; i < idx * 2; i++) {
  2163. new_called = 0;
  2164. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2165. &clientssl, NULL, NULL))
  2166. || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
  2167. goto end;
  2168. SSL_set_post_handshake_auth(clientssl, 1);
  2169. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2170. SSL_ERROR_NONE)))
  2171. goto end;
  2172. /*
  2173. * Following a successful resumption we only get 1 ticket. After a
  2174. * failed one we should get idx tickets.
  2175. */
  2176. if (succ) {
  2177. if (!TEST_true(SSL_session_reused(clientssl))
  2178. || !TEST_int_eq(new_called, 1))
  2179. goto end;
  2180. } else {
  2181. if (!TEST_false(SSL_session_reused(clientssl))
  2182. || !TEST_int_eq(new_called, idx))
  2183. goto end;
  2184. }
  2185. new_called = 0;
  2186. /* After a post-handshake authentication we should get 1 new ticket */
  2187. if (succ
  2188. && (!post_handshake_verify(serverssl, clientssl)
  2189. || !TEST_int_eq(new_called, 1)))
  2190. goto end;
  2191. SSL_shutdown(clientssl);
  2192. SSL_shutdown(serverssl);
  2193. SSL_free(serverssl);
  2194. SSL_free(clientssl);
  2195. serverssl = clientssl = NULL;
  2196. SSL_SESSION_free(sesscache[i]);
  2197. sesscache[i] = NULL;
  2198. }
  2199. return 1;
  2200. end:
  2201. SSL_free(clientssl);
  2202. SSL_free(serverssl);
  2203. return 0;
  2204. }
  2205. static int test_tickets(int stateful, int idx)
  2206. {
  2207. SSL_CTX *sctx = NULL, *cctx = NULL;
  2208. SSL *serverssl = NULL, *clientssl = NULL;
  2209. int testresult = 0;
  2210. size_t j;
  2211. /* idx is the test number, but also the number of tickets we want */
  2212. new_called = 0;
  2213. do_cache = 1;
  2214. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2215. goto end;
  2216. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2217. &clientssl, NULL, NULL)))
  2218. goto end;
  2219. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2220. SSL_ERROR_NONE))
  2221. /* Check we got the number of tickets we were expecting */
  2222. || !TEST_int_eq(idx, new_called))
  2223. goto end;
  2224. SSL_shutdown(clientssl);
  2225. SSL_shutdown(serverssl);
  2226. SSL_free(serverssl);
  2227. SSL_free(clientssl);
  2228. SSL_CTX_free(sctx);
  2229. SSL_CTX_free(cctx);
  2230. clientssl = serverssl = NULL;
  2231. sctx = cctx = NULL;
  2232. /*
  2233. * Now we try to resume with the tickets we previously created. The
  2234. * resumption attempt is expected to fail (because we're now using a new
  2235. * SSL_CTX). We should see idx number of tickets issued again.
  2236. */
  2237. /* Stop caching sessions - just count them */
  2238. do_cache = 0;
  2239. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2240. goto end;
  2241. if (!check_resumption(idx, sctx, cctx, 0))
  2242. goto end;
  2243. /* Start again with caching sessions */
  2244. new_called = 0;
  2245. do_cache = 1;
  2246. SSL_CTX_free(sctx);
  2247. SSL_CTX_free(cctx);
  2248. sctx = cctx = NULL;
  2249. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2250. goto end;
  2251. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2252. &clientssl, NULL, NULL)))
  2253. goto end;
  2254. SSL_set_post_handshake_auth(clientssl, 1);
  2255. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2256. SSL_ERROR_NONE))
  2257. /* Check we got the number of tickets we were expecting */
  2258. || !TEST_int_eq(idx, new_called))
  2259. goto end;
  2260. /* After a post-handshake authentication we should get new tickets issued */
  2261. if (!post_handshake_verify(serverssl, clientssl)
  2262. || !TEST_int_eq(idx * 2, new_called))
  2263. goto end;
  2264. SSL_shutdown(clientssl);
  2265. SSL_shutdown(serverssl);
  2266. SSL_free(serverssl);
  2267. SSL_free(clientssl);
  2268. serverssl = clientssl = NULL;
  2269. /* Stop caching sessions - just count them */
  2270. do_cache = 0;
  2271. /*
  2272. * Check we can resume with all the tickets we created. This time around the
  2273. * resumptions should all be successful.
  2274. */
  2275. if (!check_resumption(idx, sctx, cctx, 1))
  2276. goto end;
  2277. testresult = 1;
  2278. end:
  2279. SSL_free(serverssl);
  2280. SSL_free(clientssl);
  2281. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  2282. SSL_SESSION_free(sesscache[j]);
  2283. sesscache[j] = NULL;
  2284. }
  2285. SSL_CTX_free(sctx);
  2286. SSL_CTX_free(cctx);
  2287. return testresult;
  2288. }
  2289. static int test_stateless_tickets(int idx)
  2290. {
  2291. return test_tickets(0, idx);
  2292. }
  2293. static int test_stateful_tickets(int idx)
  2294. {
  2295. return test_tickets(1, idx);
  2296. }
  2297. static int test_psk_tickets(void)
  2298. {
  2299. SSL_CTX *sctx = NULL, *cctx = NULL;
  2300. SSL *serverssl = NULL, *clientssl = NULL;
  2301. int testresult = 0;
  2302. int sess_id_ctx = 1;
  2303. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2304. TLS_client_method(), TLS1_VERSION, 0,
  2305. &sctx, &cctx, NULL, NULL))
  2306. || !TEST_true(SSL_CTX_set_session_id_context(sctx,
  2307. (void *)&sess_id_ctx,
  2308. sizeof(sess_id_ctx))))
  2309. goto end;
  2310. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  2311. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  2312. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  2313. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  2314. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2315. use_session_cb_cnt = 0;
  2316. find_session_cb_cnt = 0;
  2317. srvid = pskid;
  2318. new_called = 0;
  2319. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2320. NULL, NULL)))
  2321. goto end;
  2322. clientpsk = serverpsk = create_a_psk(clientssl, SHA384_DIGEST_LENGTH);
  2323. if (!TEST_ptr(clientpsk))
  2324. goto end;
  2325. SSL_SESSION_up_ref(clientpsk);
  2326. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2327. SSL_ERROR_NONE))
  2328. || !TEST_int_eq(1, find_session_cb_cnt)
  2329. || !TEST_int_eq(1, use_session_cb_cnt)
  2330. /* We should always get 1 ticket when using external PSK */
  2331. || !TEST_int_eq(1, new_called))
  2332. goto end;
  2333. testresult = 1;
  2334. end:
  2335. SSL_free(serverssl);
  2336. SSL_free(clientssl);
  2337. SSL_CTX_free(sctx);
  2338. SSL_CTX_free(cctx);
  2339. SSL_SESSION_free(clientpsk);
  2340. SSL_SESSION_free(serverpsk);
  2341. clientpsk = serverpsk = NULL;
  2342. return testresult;
  2343. }
  2344. static int test_extra_tickets(int idx)
  2345. {
  2346. SSL_CTX *sctx = NULL, *cctx = NULL;
  2347. SSL *serverssl = NULL, *clientssl = NULL;
  2348. BIO *bretry = BIO_new(bio_s_always_retry());
  2349. BIO *tmp = NULL;
  2350. int testresult = 0;
  2351. int stateful = 0;
  2352. size_t nbytes;
  2353. unsigned char c, buf[1];
  2354. new_called = 0;
  2355. do_cache = 1;
  2356. if (idx >= 3) {
  2357. idx -= 3;
  2358. stateful = 1;
  2359. }
  2360. if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
  2361. goto end;
  2362. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  2363. /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
  2364. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2365. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2366. &clientssl, NULL, NULL)))
  2367. goto end;
  2368. /*
  2369. * Note that we have new_session_cb on both sctx and cctx, so new_called is
  2370. * incremented by both client and server.
  2371. */
  2372. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2373. SSL_ERROR_NONE))
  2374. /* Check we got the number of tickets we were expecting */
  2375. || !TEST_int_eq(idx * 2, new_called)
  2376. || !TEST_true(SSL_new_session_ticket(serverssl))
  2377. || !TEST_true(SSL_new_session_ticket(serverssl))
  2378. || !TEST_int_eq(idx * 2, new_called))
  2379. goto end;
  2380. /* Now try a (real) write to actually send the tickets */
  2381. c = '1';
  2382. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2383. || !TEST_size_t_eq(1, nbytes)
  2384. || !TEST_int_eq(idx * 2 + 2, new_called)
  2385. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2386. || !TEST_int_eq(idx * 2 + 4, new_called)
  2387. || !TEST_int_eq(sizeof(buf), nbytes)
  2388. || !TEST_int_eq(c, buf[0])
  2389. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2390. goto end;
  2391. /* Try with only requesting one new ticket, too */
  2392. c = '2';
  2393. new_called = 0;
  2394. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2395. || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
  2396. || !TEST_size_t_eq(sizeof(c), nbytes)
  2397. || !TEST_int_eq(1, new_called)
  2398. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2399. || !TEST_int_eq(2, new_called)
  2400. || !TEST_size_t_eq(sizeof(buf), nbytes)
  2401. || !TEST_int_eq(c, buf[0]))
  2402. goto end;
  2403. /* Do it again but use dummy writes to drive the ticket generation */
  2404. c = '3';
  2405. new_called = 0;
  2406. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2407. || !TEST_true(SSL_new_session_ticket(serverssl))
  2408. || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
  2409. || !TEST_size_t_eq(0, nbytes)
  2410. || !TEST_int_eq(2, new_called)
  2411. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2412. || !TEST_int_eq(4, new_called))
  2413. goto end;
  2414. /* Once more, but with SSL_do_handshake() to drive the ticket generation */
  2415. c = '4';
  2416. new_called = 0;
  2417. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2418. || !TEST_true(SSL_new_session_ticket(serverssl))
  2419. || !TEST_true(SSL_do_handshake(serverssl))
  2420. || !TEST_int_eq(2, new_called)
  2421. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2422. || !TEST_int_eq(4, new_called))
  2423. goto end;
  2424. /*
  2425. * Use the always-retry BIO to exercise the logic that forces ticket
  2426. * generation to wait until a record boundary.
  2427. */
  2428. c = '5';
  2429. new_called = 0;
  2430. tmp = SSL_get_wbio(serverssl);
  2431. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  2432. tmp = NULL;
  2433. goto end;
  2434. }
  2435. SSL_set0_wbio(serverssl, bretry);
  2436. bretry = NULL;
  2437. if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2438. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
  2439. || !TEST_size_t_eq(nbytes, 0))
  2440. goto end;
  2441. /* Restore a BIO that will let the write succeed */
  2442. SSL_set0_wbio(serverssl, tmp);
  2443. tmp = NULL;
  2444. /*
  2445. * These calls should just queue the request and not send anything
  2446. * even if we explicitly try to hit the state machine.
  2447. */
  2448. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2449. || !TEST_true(SSL_new_session_ticket(serverssl))
  2450. || !TEST_int_eq(0, new_called)
  2451. || !TEST_true(SSL_do_handshake(serverssl))
  2452. || !TEST_int_eq(0, new_called))
  2453. goto end;
  2454. /* Re-do the write; still no tickets sent */
  2455. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2456. || !TEST_size_t_eq(1, nbytes)
  2457. || !TEST_int_eq(0, new_called)
  2458. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2459. || !TEST_int_eq(0, new_called)
  2460. || !TEST_int_eq(sizeof(buf), nbytes)
  2461. || !TEST_int_eq(c, buf[0])
  2462. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2463. goto end;
  2464. /* Even trying to hit the state machine now will still not send tickets */
  2465. if (!TEST_true(SSL_do_handshake(serverssl))
  2466. || !TEST_int_eq(0, new_called))
  2467. goto end;
  2468. /* Now the *next* write should send the tickets */
  2469. c = '6';
  2470. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2471. || !TEST_size_t_eq(1, nbytes)
  2472. || !TEST_int_eq(2, new_called)
  2473. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2474. || !TEST_int_eq(4, new_called)
  2475. || !TEST_int_eq(sizeof(buf), nbytes)
  2476. || !TEST_int_eq(c, buf[0])
  2477. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2478. goto end;
  2479. SSL_shutdown(clientssl);
  2480. SSL_shutdown(serverssl);
  2481. testresult = 1;
  2482. end:
  2483. BIO_free(bretry);
  2484. BIO_free(tmp);
  2485. SSL_free(serverssl);
  2486. SSL_free(clientssl);
  2487. SSL_CTX_free(sctx);
  2488. SSL_CTX_free(cctx);
  2489. clientssl = serverssl = NULL;
  2490. sctx = cctx = NULL;
  2491. return testresult;
  2492. }
  2493. #endif
  2494. #define USE_NULL 0
  2495. #define USE_BIO_1 1
  2496. #define USE_BIO_2 2
  2497. #define USE_DEFAULT 3
  2498. #define CONNTYPE_CONNECTION_SUCCESS 0
  2499. #define CONNTYPE_CONNECTION_FAIL 1
  2500. #define CONNTYPE_NO_CONNECTION 2
  2501. #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
  2502. #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
  2503. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
  2504. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
  2505. #else
  2506. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
  2507. #endif
  2508. #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
  2509. + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
  2510. + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
  2511. static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
  2512. {
  2513. switch (type) {
  2514. case USE_NULL:
  2515. *res = NULL;
  2516. break;
  2517. case USE_BIO_1:
  2518. *res = bio1;
  2519. break;
  2520. case USE_BIO_2:
  2521. *res = bio2;
  2522. break;
  2523. }
  2524. }
  2525. /*
  2526. * Tests calls to SSL_set_bio() under various conditions.
  2527. *
  2528. * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
  2529. * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
  2530. * then do more tests where we create a successful connection first using our
  2531. * standard connection setup functions, and then call SSL_set_bio() with
  2532. * various combinations of valid BIOs or NULL. We then repeat these tests
  2533. * following a failed connection. In this last case we are looking to check that
  2534. * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
  2535. */
  2536. static int test_ssl_set_bio(int idx)
  2537. {
  2538. SSL_CTX *sctx = NULL, *cctx = NULL;
  2539. BIO *bio1 = NULL;
  2540. BIO *bio2 = NULL;
  2541. BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
  2542. SSL *serverssl = NULL, *clientssl = NULL;
  2543. int initrbio, initwbio, newrbio, newwbio, conntype;
  2544. int testresult = 0;
  2545. if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
  2546. initrbio = idx % 3;
  2547. idx /= 3;
  2548. initwbio = idx % 3;
  2549. idx /= 3;
  2550. newrbio = idx % 3;
  2551. idx /= 3;
  2552. newwbio = idx % 3;
  2553. conntype = CONNTYPE_NO_CONNECTION;
  2554. } else {
  2555. idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
  2556. initrbio = initwbio = USE_DEFAULT;
  2557. newrbio = idx % 2;
  2558. idx /= 2;
  2559. newwbio = idx % 2;
  2560. idx /= 2;
  2561. conntype = idx % 2;
  2562. }
  2563. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2564. TLS_client_method(), TLS1_VERSION, 0,
  2565. &sctx, &cctx, cert, privkey)))
  2566. goto end;
  2567. if (conntype == CONNTYPE_CONNECTION_FAIL) {
  2568. /*
  2569. * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
  2570. * because we reduced the number of tests in the definition of
  2571. * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
  2572. * mismatched protocol versions we will force a connection failure.
  2573. */
  2574. SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
  2575. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2576. }
  2577. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2578. NULL, NULL)))
  2579. goto end;
  2580. if (initrbio == USE_BIO_1
  2581. || initwbio == USE_BIO_1
  2582. || newrbio == USE_BIO_1
  2583. || newwbio == USE_BIO_1) {
  2584. if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
  2585. goto end;
  2586. }
  2587. if (initrbio == USE_BIO_2
  2588. || initwbio == USE_BIO_2
  2589. || newrbio == USE_BIO_2
  2590. || newwbio == USE_BIO_2) {
  2591. if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
  2592. goto end;
  2593. }
  2594. if (initrbio != USE_DEFAULT) {
  2595. setupbio(&irbio, bio1, bio2, initrbio);
  2596. setupbio(&iwbio, bio1, bio2, initwbio);
  2597. SSL_set_bio(clientssl, irbio, iwbio);
  2598. /*
  2599. * We want to maintain our own refs to these BIO, so do an up ref for
  2600. * each BIO that will have ownership transferred in the SSL_set_bio()
  2601. * call
  2602. */
  2603. if (irbio != NULL)
  2604. BIO_up_ref(irbio);
  2605. if (iwbio != NULL && iwbio != irbio)
  2606. BIO_up_ref(iwbio);
  2607. }
  2608. if (conntype != CONNTYPE_NO_CONNECTION
  2609. && !TEST_true(create_ssl_connection(serverssl, clientssl,
  2610. SSL_ERROR_NONE)
  2611. == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
  2612. goto end;
  2613. setupbio(&nrbio, bio1, bio2, newrbio);
  2614. setupbio(&nwbio, bio1, bio2, newwbio);
  2615. /*
  2616. * We will (maybe) transfer ownership again so do more up refs.
  2617. * SSL_set_bio() has some really complicated ownership rules where BIOs have
  2618. * already been set!
  2619. */
  2620. if (nrbio != NULL
  2621. && nrbio != irbio
  2622. && (nwbio != iwbio || nrbio != nwbio))
  2623. BIO_up_ref(nrbio);
  2624. if (nwbio != NULL
  2625. && nwbio != nrbio
  2626. && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
  2627. BIO_up_ref(nwbio);
  2628. SSL_set_bio(clientssl, nrbio, nwbio);
  2629. testresult = 1;
  2630. end:
  2631. BIO_free(bio1);
  2632. BIO_free(bio2);
  2633. /*
  2634. * This test is checking that the ref counting for SSL_set_bio is correct.
  2635. * If we get here and we did too many frees then we will fail in the above
  2636. * functions.
  2637. */
  2638. SSL_free(serverssl);
  2639. SSL_free(clientssl);
  2640. SSL_CTX_free(sctx);
  2641. SSL_CTX_free(cctx);
  2642. return testresult;
  2643. }
  2644. typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
  2645. static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
  2646. {
  2647. BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
  2648. SSL_CTX *ctx;
  2649. SSL *ssl = NULL;
  2650. int testresult = 0;
  2651. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  2652. || !TEST_ptr(ssl = SSL_new(ctx))
  2653. || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
  2654. || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
  2655. goto end;
  2656. BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
  2657. /*
  2658. * If anything goes wrong here then we could leak memory.
  2659. */
  2660. BIO_push(sslbio, membio1);
  2661. /* Verify changing the rbio/wbio directly does not cause leaks */
  2662. if (change_bio != NO_BIO_CHANGE) {
  2663. if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
  2664. ssl = NULL;
  2665. goto end;
  2666. }
  2667. if (change_bio == CHANGE_RBIO)
  2668. SSL_set0_rbio(ssl, membio2);
  2669. else
  2670. SSL_set0_wbio(ssl, membio2);
  2671. }
  2672. ssl = NULL;
  2673. if (pop_ssl)
  2674. BIO_pop(sslbio);
  2675. else
  2676. BIO_pop(membio1);
  2677. testresult = 1;
  2678. end:
  2679. BIO_free(membio1);
  2680. BIO_free(sslbio);
  2681. SSL_free(ssl);
  2682. SSL_CTX_free(ctx);
  2683. return testresult;
  2684. }
  2685. static int test_ssl_bio_pop_next_bio(void)
  2686. {
  2687. return execute_test_ssl_bio(0, NO_BIO_CHANGE);
  2688. }
  2689. static int test_ssl_bio_pop_ssl_bio(void)
  2690. {
  2691. return execute_test_ssl_bio(1, NO_BIO_CHANGE);
  2692. }
  2693. static int test_ssl_bio_change_rbio(void)
  2694. {
  2695. return execute_test_ssl_bio(0, CHANGE_RBIO);
  2696. }
  2697. static int test_ssl_bio_change_wbio(void)
  2698. {
  2699. return execute_test_ssl_bio(0, CHANGE_WBIO);
  2700. }
  2701. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  2702. typedef struct {
  2703. /* The list of sig algs */
  2704. const int *list;
  2705. /* The length of the list */
  2706. size_t listlen;
  2707. /* A sigalgs list in string format */
  2708. const char *liststr;
  2709. /* Whether setting the list should succeed */
  2710. int valid;
  2711. /* Whether creating a connection with the list should succeed */
  2712. int connsuccess;
  2713. } sigalgs_list;
  2714. static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
  2715. # ifndef OPENSSL_NO_EC
  2716. static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
  2717. static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
  2718. # endif
  2719. static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
  2720. static const int invalidlist2[] = {NID_sha256, NID_undef};
  2721. static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
  2722. static const int invalidlist4[] = {NID_sha256};
  2723. static const sigalgs_list testsigalgs[] = {
  2724. {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
  2725. # ifndef OPENSSL_NO_EC
  2726. {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
  2727. {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
  2728. # endif
  2729. {NULL, 0, "RSA+SHA256", 1, 1},
  2730. {NULL, 0, "RSA+SHA256:?Invalid", 1, 1},
  2731. # ifndef OPENSSL_NO_EC
  2732. {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
  2733. {NULL, 0, "ECDSA+SHA512", 1, 0},
  2734. # endif
  2735. {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
  2736. {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
  2737. {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
  2738. {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
  2739. {NULL, 0, "RSA", 0, 0},
  2740. {NULL, 0, "SHA256", 0, 0},
  2741. {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
  2742. {NULL, 0, "Invalid", 0, 0}
  2743. };
  2744. static int test_set_sigalgs(int idx)
  2745. {
  2746. SSL_CTX *cctx = NULL, *sctx = NULL;
  2747. SSL *clientssl = NULL, *serverssl = NULL;
  2748. int testresult = 0;
  2749. const sigalgs_list *curr;
  2750. int testctx;
  2751. /* Should never happen */
  2752. if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
  2753. return 0;
  2754. testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
  2755. curr = testctx ? &testsigalgs[idx]
  2756. : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
  2757. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2758. TLS_client_method(), TLS1_VERSION, 0,
  2759. &sctx, &cctx, cert, privkey)))
  2760. return 0;
  2761. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2762. if (testctx) {
  2763. int ret;
  2764. if (curr->list != NULL)
  2765. ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
  2766. else
  2767. ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
  2768. if (!ret) {
  2769. if (curr->valid)
  2770. TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
  2771. else
  2772. testresult = 1;
  2773. goto end;
  2774. }
  2775. if (!curr->valid) {
  2776. TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
  2777. goto end;
  2778. }
  2779. }
  2780. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2781. &clientssl, NULL, NULL)))
  2782. goto end;
  2783. if (!testctx) {
  2784. int ret;
  2785. if (curr->list != NULL)
  2786. ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
  2787. else
  2788. ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
  2789. if (!ret) {
  2790. if (curr->valid)
  2791. TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
  2792. else
  2793. testresult = 1;
  2794. goto end;
  2795. }
  2796. if (!curr->valid)
  2797. goto end;
  2798. }
  2799. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  2800. SSL_ERROR_NONE),
  2801. curr->connsuccess))
  2802. goto end;
  2803. testresult = 1;
  2804. end:
  2805. SSL_free(serverssl);
  2806. SSL_free(clientssl);
  2807. SSL_CTX_free(sctx);
  2808. SSL_CTX_free(cctx);
  2809. return testresult;
  2810. }
  2811. #endif
  2812. #ifndef OSSL_NO_USABLE_TLS1_3
  2813. static int psk_client_cb_cnt = 0;
  2814. static int psk_server_cb_cnt = 0;
  2815. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  2816. size_t *idlen, SSL_SESSION **sess)
  2817. {
  2818. switch (++use_session_cb_cnt) {
  2819. case 1:
  2820. /* The first call should always have a NULL md */
  2821. if (md != NULL)
  2822. return 0;
  2823. break;
  2824. case 2:
  2825. /* The second call should always have an md */
  2826. if (md == NULL)
  2827. return 0;
  2828. break;
  2829. default:
  2830. /* We should only be called a maximum of twice */
  2831. return 0;
  2832. }
  2833. if (clientpsk != NULL)
  2834. SSL_SESSION_up_ref(clientpsk);
  2835. *sess = clientpsk;
  2836. *id = (const unsigned char *)pskid;
  2837. *idlen = strlen(pskid);
  2838. return 1;
  2839. }
  2840. #ifndef OPENSSL_NO_PSK
  2841. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
  2842. unsigned int max_id_len,
  2843. unsigned char *psk,
  2844. unsigned int max_psk_len)
  2845. {
  2846. unsigned int psklen = 0;
  2847. psk_client_cb_cnt++;
  2848. if (strlen(pskid) + 1 > max_id_len)
  2849. return 0;
  2850. /* We should only ever be called a maximum of twice per connection */
  2851. if (psk_client_cb_cnt > 2)
  2852. return 0;
  2853. if (clientpsk == NULL)
  2854. return 0;
  2855. /* We'll reuse the PSK we set up for TLSv1.3 */
  2856. if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
  2857. return 0;
  2858. psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
  2859. strncpy(id, pskid, max_id_len);
  2860. return psklen;
  2861. }
  2862. #endif /* OPENSSL_NO_PSK */
  2863. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  2864. size_t identity_len, SSL_SESSION **sess)
  2865. {
  2866. find_session_cb_cnt++;
  2867. /* We should only ever be called a maximum of twice per connection */
  2868. if (find_session_cb_cnt > 2)
  2869. return 0;
  2870. if (serverpsk == NULL)
  2871. return 0;
  2872. /* Identity should match that set by the client */
  2873. if (strlen(srvid) != identity_len
  2874. || strncmp(srvid, (const char *)identity, identity_len) != 0) {
  2875. /* No PSK found, continue but without a PSK */
  2876. *sess = NULL;
  2877. return 1;
  2878. }
  2879. SSL_SESSION_up_ref(serverpsk);
  2880. *sess = serverpsk;
  2881. return 1;
  2882. }
  2883. #ifndef OPENSSL_NO_PSK
  2884. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  2885. unsigned char *psk, unsigned int max_psk_len)
  2886. {
  2887. unsigned int psklen = 0;
  2888. psk_server_cb_cnt++;
  2889. /* We should only ever be called a maximum of twice per connection */
  2890. if (find_session_cb_cnt > 2)
  2891. return 0;
  2892. if (serverpsk == NULL)
  2893. return 0;
  2894. /* Identity should match that set by the client */
  2895. if (strcmp(srvid, identity) != 0) {
  2896. return 0;
  2897. }
  2898. /* We'll reuse the PSK we set up for TLSv1.3 */
  2899. if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
  2900. return 0;
  2901. psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
  2902. return psklen;
  2903. }
  2904. #endif /* OPENSSL_NO_PSK */
  2905. #define MSG1 "Hello"
  2906. #define MSG2 "World."
  2907. #define MSG3 "This"
  2908. #define MSG4 "is"
  2909. #define MSG5 "a"
  2910. #define MSG6 "test"
  2911. #define MSG7 "message."
  2912. static int artificial_ticket_time = 0;
  2913. static int ed_gen_cb(SSL *s, void *arg)
  2914. {
  2915. SSL_SESSION *sess = SSL_get0_session(s);
  2916. if (sess == NULL)
  2917. return 0;
  2918. /*
  2919. * Artificially give the ticket some age. Just do it for the number of
  2920. * tickets we've been told to do.
  2921. */
  2922. if (artificial_ticket_time == 0)
  2923. return 1;
  2924. artificial_ticket_time--;
  2925. if (SSL_SESSION_set_time_ex(sess, SSL_SESSION_get_time_ex(sess) - 10) == 0)
  2926. return 0;
  2927. return 1;
  2928. }
  2929. /*
  2930. * Helper method to setup objects for early data test. Caller frees objects on
  2931. * error.
  2932. */
  2933. static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
  2934. SSL **serverssl, SSL_SESSION **sess, int idx,
  2935. size_t mdsize)
  2936. {
  2937. int artificial = (artificial_ticket_time > 0);
  2938. if (*sctx == NULL
  2939. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2940. TLS_client_method(),
  2941. TLS1_VERSION, 0,
  2942. sctx, cctx, cert, privkey)))
  2943. return 0;
  2944. if (artificial)
  2945. SSL_CTX_set_session_ticket_cb(*sctx, ed_gen_cb, NULL, NULL);
  2946. if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
  2947. return 0;
  2948. if (idx == 1) {
  2949. /* When idx == 1 we repeat the tests with read_ahead set */
  2950. SSL_CTX_set_read_ahead(*cctx, 1);
  2951. SSL_CTX_set_read_ahead(*sctx, 1);
  2952. } else if (idx == 2) {
  2953. /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
  2954. SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
  2955. SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
  2956. use_session_cb_cnt = 0;
  2957. find_session_cb_cnt = 0;
  2958. srvid = pskid;
  2959. }
  2960. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
  2961. NULL, NULL)))
  2962. return 0;
  2963. /*
  2964. * For one of the run throughs (doesn't matter which one), we'll try sending
  2965. * some SNI data in the initial ClientHello. This will be ignored (because
  2966. * there is no SNI cb set up by the server), so it should not impact
  2967. * early_data.
  2968. */
  2969. if (idx == 1
  2970. && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
  2971. return 0;
  2972. if (idx == 2) {
  2973. clientpsk = create_a_psk(*clientssl, mdsize);
  2974. if (!TEST_ptr(clientpsk)
  2975. /*
  2976. * We just choose an arbitrary value for max_early_data which
  2977. * should be big enough for testing purposes.
  2978. */
  2979. || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
  2980. 0x100))
  2981. || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2982. SSL_SESSION_free(clientpsk);
  2983. clientpsk = NULL;
  2984. return 0;
  2985. }
  2986. serverpsk = clientpsk;
  2987. if (sess != NULL) {
  2988. if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2989. SSL_SESSION_free(clientpsk);
  2990. SSL_SESSION_free(serverpsk);
  2991. clientpsk = serverpsk = NULL;
  2992. return 0;
  2993. }
  2994. *sess = clientpsk;
  2995. }
  2996. return 1;
  2997. }
  2998. if (sess == NULL)
  2999. return 1;
  3000. if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
  3001. SSL_ERROR_NONE)))
  3002. return 0;
  3003. *sess = SSL_get1_session(*clientssl);
  3004. SSL_shutdown(*clientssl);
  3005. SSL_shutdown(*serverssl);
  3006. SSL_free(*serverssl);
  3007. SSL_free(*clientssl);
  3008. *serverssl = *clientssl = NULL;
  3009. /*
  3010. * Artificially give the ticket some age to match the artificial age we
  3011. * gave it on the server side
  3012. */
  3013. if (artificial
  3014. && !TEST_time_t_gt(SSL_SESSION_set_time_ex(*sess,
  3015. SSL_SESSION_get_time_ex(*sess) - 10), 0))
  3016. return 0;
  3017. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
  3018. clientssl, NULL, NULL))
  3019. || !TEST_true(SSL_set_session(*clientssl, *sess)))
  3020. return 0;
  3021. return 1;
  3022. }
  3023. static int test_early_data_read_write(int idx)
  3024. {
  3025. SSL_CTX *cctx = NULL, *sctx = NULL;
  3026. SSL *clientssl = NULL, *serverssl = NULL;
  3027. int testresult = 0;
  3028. SSL_SESSION *sess = NULL;
  3029. unsigned char buf[20], data[1024];
  3030. size_t readbytes, written, eoedlen, rawread, rawwritten;
  3031. BIO *rbio;
  3032. /* Artificially give the next 2 tickets some age for non PSK sessions */
  3033. if (idx != 2)
  3034. artificial_ticket_time = 2;
  3035. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3036. &serverssl, &sess, idx,
  3037. SHA384_DIGEST_LENGTH))) {
  3038. artificial_ticket_time = 0;
  3039. goto end;
  3040. }
  3041. artificial_ticket_time = 0;
  3042. /* Write and read some early data */
  3043. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3044. &written))
  3045. || !TEST_size_t_eq(written, strlen(MSG1))
  3046. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  3047. sizeof(buf), &readbytes),
  3048. SSL_READ_EARLY_DATA_SUCCESS)
  3049. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  3050. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3051. SSL_EARLY_DATA_ACCEPTED))
  3052. goto end;
  3053. /*
  3054. * Server should be able to write data, and client should be able to
  3055. * read it.
  3056. */
  3057. if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
  3058. &written))
  3059. || !TEST_size_t_eq(written, strlen(MSG2))
  3060. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3061. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3062. goto end;
  3063. /* Even after reading normal data, client should be able write early data */
  3064. if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
  3065. &written))
  3066. || !TEST_size_t_eq(written, strlen(MSG3)))
  3067. goto end;
  3068. /* Server should still be able read early data after writing data */
  3069. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3070. &readbytes),
  3071. SSL_READ_EARLY_DATA_SUCCESS)
  3072. || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
  3073. goto end;
  3074. /* Write more data from server and read it from client */
  3075. if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
  3076. &written))
  3077. || !TEST_size_t_eq(written, strlen(MSG4))
  3078. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3079. || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
  3080. goto end;
  3081. /*
  3082. * If client writes normal data it should mean writing early data is no
  3083. * longer possible.
  3084. */
  3085. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  3086. || !TEST_size_t_eq(written, strlen(MSG5))
  3087. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3088. SSL_EARLY_DATA_ACCEPTED))
  3089. goto end;
  3090. /*
  3091. * At this point the client has written EndOfEarlyData, ClientFinished and
  3092. * normal (fully protected) data. We are going to cause a delay between the
  3093. * arrival of EndOfEarlyData and ClientFinished. We read out all the data
  3094. * in the read BIO, and then just put back the EndOfEarlyData message.
  3095. */
  3096. rbio = SSL_get_rbio(serverssl);
  3097. if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
  3098. || !TEST_size_t_lt(rawread, sizeof(data))
  3099. || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
  3100. goto end;
  3101. /* Record length is in the 4th and 5th bytes of the record header */
  3102. eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
  3103. if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
  3104. || !TEST_size_t_eq(rawwritten, eoedlen))
  3105. goto end;
  3106. /* Server should be told that there is no more early data */
  3107. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3108. &readbytes),
  3109. SSL_READ_EARLY_DATA_FINISH)
  3110. || !TEST_size_t_eq(readbytes, 0))
  3111. goto end;
  3112. /*
  3113. * Server has not finished init yet, so should still be able to write early
  3114. * data.
  3115. */
  3116. if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
  3117. &written))
  3118. || !TEST_size_t_eq(written, strlen(MSG6)))
  3119. goto end;
  3120. /* Push the ClientFinished and the normal data back into the server rbio */
  3121. if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
  3122. &rawwritten))
  3123. || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
  3124. goto end;
  3125. /* Server should be able to read normal data */
  3126. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3127. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  3128. goto end;
  3129. /* Client and server should not be able to write/read early data now */
  3130. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  3131. &written)))
  3132. goto end;
  3133. ERR_clear_error();
  3134. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3135. &readbytes),
  3136. SSL_READ_EARLY_DATA_ERROR))
  3137. goto end;
  3138. ERR_clear_error();
  3139. /* Client should be able to read the data sent by the server */
  3140. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3141. || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
  3142. goto end;
  3143. /*
  3144. * Make sure we process the two NewSessionTickets. These arrive
  3145. * post-handshake. We attempt reads which we do not expect to return any
  3146. * data.
  3147. */
  3148. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3149. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
  3150. &readbytes)))
  3151. goto end;
  3152. /* Server should be able to write normal data */
  3153. if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
  3154. || !TEST_size_t_eq(written, strlen(MSG7))
  3155. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3156. || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
  3157. goto end;
  3158. SSL_SESSION_free(sess);
  3159. sess = SSL_get1_session(clientssl);
  3160. use_session_cb_cnt = 0;
  3161. find_session_cb_cnt = 0;
  3162. SSL_shutdown(clientssl);
  3163. SSL_shutdown(serverssl);
  3164. SSL_free(serverssl);
  3165. SSL_free(clientssl);
  3166. serverssl = clientssl = NULL;
  3167. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3168. &clientssl, NULL, NULL))
  3169. || !TEST_true(SSL_set_session(clientssl, sess)))
  3170. goto end;
  3171. /* Write and read some early data */
  3172. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3173. &written))
  3174. || !TEST_size_t_eq(written, strlen(MSG1))
  3175. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3176. &readbytes),
  3177. SSL_READ_EARLY_DATA_SUCCESS)
  3178. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3179. goto end;
  3180. if (!TEST_int_gt(SSL_connect(clientssl), 0)
  3181. || !TEST_int_gt(SSL_accept(serverssl), 0))
  3182. goto end;
  3183. /* Client and server should not be able to write/read early data now */
  3184. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  3185. &written)))
  3186. goto end;
  3187. ERR_clear_error();
  3188. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3189. &readbytes),
  3190. SSL_READ_EARLY_DATA_ERROR))
  3191. goto end;
  3192. ERR_clear_error();
  3193. /* Client and server should be able to write/read normal data */
  3194. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  3195. || !TEST_size_t_eq(written, strlen(MSG5))
  3196. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3197. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  3198. goto end;
  3199. testresult = 1;
  3200. end:
  3201. SSL_SESSION_free(sess);
  3202. SSL_SESSION_free(clientpsk);
  3203. SSL_SESSION_free(serverpsk);
  3204. clientpsk = serverpsk = NULL;
  3205. SSL_free(serverssl);
  3206. SSL_free(clientssl);
  3207. SSL_CTX_free(sctx);
  3208. SSL_CTX_free(cctx);
  3209. return testresult;
  3210. }
  3211. static int allow_ed_cb_called = 0;
  3212. static int allow_early_data_cb(SSL *s, void *arg)
  3213. {
  3214. int *usecb = (int *)arg;
  3215. allow_ed_cb_called++;
  3216. if (*usecb == 1)
  3217. return 0;
  3218. return 1;
  3219. }
  3220. /*
  3221. * idx == 0: Standard early_data setup
  3222. * idx == 1: early_data setup using read_ahead
  3223. * usecb == 0: Don't use a custom early data callback
  3224. * usecb == 1: Use a custom early data callback and reject the early data
  3225. * usecb == 2: Use a custom early data callback and accept the early data
  3226. * confopt == 0: Configure anti-replay directly
  3227. * confopt == 1: Configure anti-replay using SSL_CONF
  3228. */
  3229. static int test_early_data_replay_int(int idx, int usecb, int confopt)
  3230. {
  3231. SSL_CTX *cctx = NULL, *sctx = NULL;
  3232. SSL *clientssl = NULL, *serverssl = NULL;
  3233. int testresult = 0;
  3234. SSL_SESSION *sess = NULL;
  3235. size_t readbytes, written;
  3236. unsigned char buf[20];
  3237. allow_ed_cb_called = 0;
  3238. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3239. TLS_client_method(), TLS1_VERSION, 0,
  3240. &sctx, &cctx, cert, privkey)))
  3241. return 0;
  3242. if (usecb > 0) {
  3243. if (confopt == 0) {
  3244. SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
  3245. } else {
  3246. SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
  3247. if (!TEST_ptr(confctx))
  3248. goto end;
  3249. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  3250. | SSL_CONF_FLAG_SERVER);
  3251. SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
  3252. if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
  3253. 2)) {
  3254. SSL_CONF_CTX_free(confctx);
  3255. goto end;
  3256. }
  3257. SSL_CONF_CTX_free(confctx);
  3258. }
  3259. SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
  3260. }
  3261. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3262. &serverssl, &sess, idx,
  3263. SHA384_DIGEST_LENGTH)))
  3264. goto end;
  3265. /*
  3266. * The server is configured to accept early data. Create a connection to
  3267. * "use up" the ticket
  3268. */
  3269. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3270. || !TEST_true(SSL_session_reused(clientssl)))
  3271. goto end;
  3272. SSL_shutdown(clientssl);
  3273. SSL_shutdown(serverssl);
  3274. SSL_free(serverssl);
  3275. SSL_free(clientssl);
  3276. serverssl = clientssl = NULL;
  3277. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3278. &clientssl, NULL, NULL))
  3279. || !TEST_true(SSL_set_session(clientssl, sess)))
  3280. goto end;
  3281. /* Write and read some early data */
  3282. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3283. &written))
  3284. || !TEST_size_t_eq(written, strlen(MSG1)))
  3285. goto end;
  3286. if (usecb <= 1) {
  3287. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3288. &readbytes),
  3289. SSL_READ_EARLY_DATA_FINISH)
  3290. /*
  3291. * The ticket was reused, so the we should have rejected the
  3292. * early data
  3293. */
  3294. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3295. SSL_EARLY_DATA_REJECTED))
  3296. goto end;
  3297. } else {
  3298. /* In this case the callback decides to accept the early data */
  3299. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3300. &readbytes),
  3301. SSL_READ_EARLY_DATA_SUCCESS)
  3302. || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
  3303. /*
  3304. * Server will have sent its flight so client can now send
  3305. * end of early data and complete its half of the handshake
  3306. */
  3307. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3308. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3309. &readbytes),
  3310. SSL_READ_EARLY_DATA_FINISH)
  3311. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3312. SSL_EARLY_DATA_ACCEPTED))
  3313. goto end;
  3314. }
  3315. /* Complete the connection */
  3316. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3317. || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
  3318. || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
  3319. goto end;
  3320. testresult = 1;
  3321. end:
  3322. SSL_SESSION_free(sess);
  3323. SSL_SESSION_free(clientpsk);
  3324. SSL_SESSION_free(serverpsk);
  3325. clientpsk = serverpsk = NULL;
  3326. SSL_free(serverssl);
  3327. SSL_free(clientssl);
  3328. SSL_CTX_free(sctx);
  3329. SSL_CTX_free(cctx);
  3330. return testresult;
  3331. }
  3332. static int test_early_data_replay(int idx)
  3333. {
  3334. int ret = 1, usecb, confopt;
  3335. for (usecb = 0; usecb < 3; usecb++) {
  3336. for (confopt = 0; confopt < 2; confopt++)
  3337. ret &= test_early_data_replay_int(idx, usecb, confopt);
  3338. }
  3339. return ret;
  3340. }
  3341. static const char *ciphersuites[] = {
  3342. "TLS_AES_128_CCM_8_SHA256",
  3343. "TLS_AES_128_GCM_SHA256",
  3344. "TLS_AES_256_GCM_SHA384",
  3345. "TLS_AES_128_CCM_SHA256",
  3346. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3347. "TLS_CHACHA20_POLY1305_SHA256"
  3348. #endif
  3349. };
  3350. /*
  3351. * Helper function to test that a server attempting to read early data can
  3352. * handle a connection from a client where the early data should be skipped.
  3353. * testtype: 0 == No HRR
  3354. * testtype: 1 == HRR
  3355. * testtype: 2 == HRR, invalid early_data sent after HRR
  3356. * testtype: 3 == recv_max_early_data set to 0
  3357. */
  3358. static int early_data_skip_helper(int testtype, int cipher, int idx)
  3359. {
  3360. SSL_CTX *cctx = NULL, *sctx = NULL;
  3361. SSL *clientssl = NULL, *serverssl = NULL;
  3362. int testresult = 0;
  3363. SSL_SESSION *sess = NULL;
  3364. unsigned char buf[20];
  3365. size_t readbytes, written;
  3366. if (is_fips && cipher == 4)
  3367. return 1;
  3368. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3369. TLS_client_method(),
  3370. TLS1_VERSION, 0,
  3371. &sctx, &cctx, cert, privkey)))
  3372. goto end;
  3373. if (cipher == 0) {
  3374. SSL_CTX_set_security_level(sctx, 0);
  3375. SSL_CTX_set_security_level(cctx, 0);
  3376. }
  3377. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, ciphersuites[cipher]))
  3378. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ciphersuites[cipher])))
  3379. goto end;
  3380. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3381. &serverssl, &sess, idx,
  3382. cipher == 2 ? SHA384_DIGEST_LENGTH
  3383. : SHA256_DIGEST_LENGTH)))
  3384. goto end;
  3385. if (testtype == 1 || testtype == 2) {
  3386. /* Force an HRR to occur */
  3387. #if defined(OPENSSL_NO_EC)
  3388. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  3389. goto end;
  3390. #else
  3391. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
  3392. goto end;
  3393. #endif
  3394. } else if (idx == 2) {
  3395. /*
  3396. * We force early_data rejection by ensuring the PSK identity is
  3397. * unrecognised
  3398. */
  3399. srvid = "Dummy Identity";
  3400. } else {
  3401. /*
  3402. * Deliberately corrupt the creation time. We take 20 seconds off the
  3403. * time. It could be any value as long as it is not within tolerance.
  3404. * This should mean the ticket is rejected.
  3405. */
  3406. if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
  3407. goto end;
  3408. }
  3409. if (testtype == 3
  3410. && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
  3411. goto end;
  3412. /* Write some early data */
  3413. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3414. &written))
  3415. || !TEST_size_t_eq(written, strlen(MSG1)))
  3416. goto end;
  3417. /* Server should reject the early data */
  3418. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3419. &readbytes),
  3420. SSL_READ_EARLY_DATA_FINISH)
  3421. || !TEST_size_t_eq(readbytes, 0)
  3422. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3423. SSL_EARLY_DATA_REJECTED))
  3424. goto end;
  3425. switch (testtype) {
  3426. case 0:
  3427. /* Nothing to do */
  3428. break;
  3429. case 1:
  3430. /*
  3431. * Finish off the handshake. We perform the same writes and reads as
  3432. * further down but we expect them to fail due to the incomplete
  3433. * handshake.
  3434. */
  3435. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3436. || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
  3437. &readbytes)))
  3438. goto end;
  3439. break;
  3440. case 2:
  3441. {
  3442. BIO *wbio = SSL_get_wbio(clientssl);
  3443. /* A record that will appear as bad early_data */
  3444. const unsigned char bad_early_data[] = {
  3445. 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
  3446. };
  3447. /*
  3448. * We force the client to attempt a write. This will fail because
  3449. * we're still in the handshake. It will cause the second
  3450. * ClientHello to be sent.
  3451. */
  3452. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
  3453. &written)))
  3454. goto end;
  3455. /*
  3456. * Inject some early_data after the second ClientHello. This should
  3457. * cause the server to fail
  3458. */
  3459. if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
  3460. sizeof(bad_early_data), &written)))
  3461. goto end;
  3462. }
  3463. /* fallthrough */
  3464. case 3:
  3465. /*
  3466. * This client has sent more early_data than we are willing to skip
  3467. * (case 3) or sent invalid early_data (case 2) so the connection should
  3468. * abort.
  3469. */
  3470. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3471. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
  3472. goto end;
  3473. /* Connection has failed - nothing more to do */
  3474. testresult = 1;
  3475. goto end;
  3476. default:
  3477. TEST_error("Invalid test type");
  3478. goto end;
  3479. }
  3480. ERR_clear_error();
  3481. /*
  3482. * Should be able to send normal data despite rejection of early data. The
  3483. * early_data should be skipped.
  3484. */
  3485. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3486. || !TEST_size_t_eq(written, strlen(MSG2))
  3487. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3488. SSL_EARLY_DATA_REJECTED)
  3489. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3490. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3491. goto end;
  3492. /*
  3493. * Failure to decrypt early data records should not leave spurious errors
  3494. * on the error stack
  3495. */
  3496. if (!TEST_long_eq(ERR_peek_error(), 0))
  3497. goto end;
  3498. testresult = 1;
  3499. end:
  3500. SSL_SESSION_free(clientpsk);
  3501. SSL_SESSION_free(serverpsk);
  3502. clientpsk = serverpsk = NULL;
  3503. SSL_SESSION_free(sess);
  3504. SSL_free(serverssl);
  3505. SSL_free(clientssl);
  3506. SSL_CTX_free(sctx);
  3507. SSL_CTX_free(cctx);
  3508. return testresult;
  3509. }
  3510. /*
  3511. * Test that a server attempting to read early data can handle a connection
  3512. * from a client where the early data is not acceptable.
  3513. */
  3514. static int test_early_data_skip(int idx)
  3515. {
  3516. return early_data_skip_helper(0,
  3517. idx % OSSL_NELEM(ciphersuites),
  3518. idx / OSSL_NELEM(ciphersuites));
  3519. }
  3520. /*
  3521. * Test that a server attempting to read early data can handle a connection
  3522. * from a client where an HRR occurs.
  3523. */
  3524. static int test_early_data_skip_hrr(int idx)
  3525. {
  3526. return early_data_skip_helper(1,
  3527. idx % OSSL_NELEM(ciphersuites),
  3528. idx / OSSL_NELEM(ciphersuites));
  3529. }
  3530. /*
  3531. * Test that a server attempting to read early data can handle a connection
  3532. * from a client where an HRR occurs and correctly fails if early_data is sent
  3533. * after the HRR
  3534. */
  3535. static int test_early_data_skip_hrr_fail(int idx)
  3536. {
  3537. return early_data_skip_helper(2,
  3538. idx % OSSL_NELEM(ciphersuites),
  3539. idx / OSSL_NELEM(ciphersuites));
  3540. }
  3541. /*
  3542. * Test that a server attempting to read early data will abort if it tries to
  3543. * skip over too much.
  3544. */
  3545. static int test_early_data_skip_abort(int idx)
  3546. {
  3547. return early_data_skip_helper(3,
  3548. idx % OSSL_NELEM(ciphersuites),
  3549. idx / OSSL_NELEM(ciphersuites));
  3550. }
  3551. /*
  3552. * Test that a server attempting to read early data can handle a connection
  3553. * from a client that doesn't send any.
  3554. */
  3555. static int test_early_data_not_sent(int idx)
  3556. {
  3557. SSL_CTX *cctx = NULL, *sctx = NULL;
  3558. SSL *clientssl = NULL, *serverssl = NULL;
  3559. int testresult = 0;
  3560. SSL_SESSION *sess = NULL;
  3561. unsigned char buf[20];
  3562. size_t readbytes, written;
  3563. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3564. &serverssl, &sess, idx,
  3565. SHA384_DIGEST_LENGTH)))
  3566. goto end;
  3567. /* Write some data - should block due to handshake with server */
  3568. SSL_set_connect_state(clientssl);
  3569. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3570. goto end;
  3571. /* Server should detect that early data has not been sent */
  3572. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3573. &readbytes),
  3574. SSL_READ_EARLY_DATA_FINISH)
  3575. || !TEST_size_t_eq(readbytes, 0)
  3576. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3577. SSL_EARLY_DATA_NOT_SENT)
  3578. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3579. SSL_EARLY_DATA_NOT_SENT))
  3580. goto end;
  3581. /* Continue writing the message we started earlier */
  3582. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3583. || !TEST_size_t_eq(written, strlen(MSG1))
  3584. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3585. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3586. || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
  3587. || !TEST_size_t_eq(written, strlen(MSG2)))
  3588. goto end;
  3589. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3590. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3591. goto end;
  3592. testresult = 1;
  3593. end:
  3594. SSL_SESSION_free(sess);
  3595. SSL_SESSION_free(clientpsk);
  3596. SSL_SESSION_free(serverpsk);
  3597. clientpsk = serverpsk = NULL;
  3598. SSL_free(serverssl);
  3599. SSL_free(clientssl);
  3600. SSL_CTX_free(sctx);
  3601. SSL_CTX_free(cctx);
  3602. return testresult;
  3603. }
  3604. static const char *servalpn;
  3605. static int alpn_select_cb(SSL *ssl, const unsigned char **out,
  3606. unsigned char *outlen, const unsigned char *in,
  3607. unsigned int inlen, void *arg)
  3608. {
  3609. unsigned int protlen = 0;
  3610. const unsigned char *prot;
  3611. for (prot = in; prot < in + inlen; prot += protlen) {
  3612. protlen = *prot++;
  3613. if (in + inlen < prot + protlen)
  3614. return SSL_TLSEXT_ERR_NOACK;
  3615. if (protlen == strlen(servalpn)
  3616. && memcmp(prot, servalpn, protlen) == 0) {
  3617. *out = prot;
  3618. *outlen = protlen;
  3619. return SSL_TLSEXT_ERR_OK;
  3620. }
  3621. }
  3622. return SSL_TLSEXT_ERR_NOACK;
  3623. }
  3624. /* Test that a PSK can be used to send early_data */
  3625. static int test_early_data_psk(int idx)
  3626. {
  3627. SSL_CTX *cctx = NULL, *sctx = NULL;
  3628. SSL *clientssl = NULL, *serverssl = NULL;
  3629. int testresult = 0;
  3630. SSL_SESSION *sess = NULL;
  3631. unsigned char alpnlist[] = {
  3632. 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
  3633. 'l', 'p', 'n'
  3634. };
  3635. #define GOODALPNLEN 9
  3636. #define BADALPNLEN 8
  3637. #define GOODALPN (alpnlist)
  3638. #define BADALPN (alpnlist + GOODALPNLEN)
  3639. int err = 0;
  3640. unsigned char buf[20];
  3641. size_t readbytes, written;
  3642. int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
  3643. int edstatus = SSL_EARLY_DATA_ACCEPTED;
  3644. /* We always set this up with a final parameter of "2" for PSK */
  3645. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3646. &serverssl, &sess, 2,
  3647. SHA384_DIGEST_LENGTH)))
  3648. goto end;
  3649. servalpn = "goodalpn";
  3650. /*
  3651. * Note: There is no test for inconsistent SNI with late client detection.
  3652. * This is because servers do not acknowledge SNI even if they are using
  3653. * it in a resumption handshake - so it is not actually possible for a
  3654. * client to detect a problem.
  3655. */
  3656. switch (idx) {
  3657. case 0:
  3658. /* Set inconsistent SNI (early client detection) */
  3659. err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
  3660. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3661. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
  3662. goto end;
  3663. break;
  3664. case 1:
  3665. /* Set inconsistent ALPN (early client detection) */
  3666. err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
  3667. /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
  3668. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
  3669. GOODALPNLEN))
  3670. || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
  3671. BADALPNLEN)))
  3672. goto end;
  3673. break;
  3674. case 2:
  3675. /*
  3676. * Set invalid protocol version. Technically this affects PSKs without
  3677. * early_data too, but we test it here because it is similar to the
  3678. * SNI/ALPN consistency tests.
  3679. */
  3680. err = SSL_R_BAD_PSK;
  3681. if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
  3682. goto end;
  3683. break;
  3684. case 3:
  3685. /*
  3686. * Set inconsistent SNI (server side). In this case the connection
  3687. * will succeed and accept early_data. In TLSv1.3 on the server side SNI
  3688. * is associated with each handshake - not the session. Therefore it
  3689. * should not matter that we used a different server name last time.
  3690. */
  3691. SSL_SESSION_free(serverpsk);
  3692. serverpsk = SSL_SESSION_dup(clientpsk);
  3693. if (!TEST_ptr(serverpsk)
  3694. || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
  3695. goto end;
  3696. /* Fall through */
  3697. case 4:
  3698. /* Set consistent SNI */
  3699. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3700. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
  3701. || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  3702. hostname_cb)))
  3703. goto end;
  3704. break;
  3705. case 5:
  3706. /*
  3707. * Set inconsistent ALPN (server detected). In this case the connection
  3708. * will succeed but reject early_data.
  3709. */
  3710. servalpn = "badalpn";
  3711. edstatus = SSL_EARLY_DATA_REJECTED;
  3712. readearlyres = SSL_READ_EARLY_DATA_FINISH;
  3713. /* Fall through */
  3714. case 6:
  3715. /*
  3716. * Set consistent ALPN.
  3717. * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
  3718. * accepts a list of protos (each one length prefixed).
  3719. * SSL_set1_alpn_selected accepts a single protocol (not length
  3720. * prefixed)
  3721. */
  3722. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
  3723. GOODALPNLEN - 1))
  3724. || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
  3725. GOODALPNLEN)))
  3726. goto end;
  3727. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3728. break;
  3729. case 7:
  3730. /* Set inconsistent ALPN (late client detection) */
  3731. SSL_SESSION_free(serverpsk);
  3732. serverpsk = SSL_SESSION_dup(clientpsk);
  3733. if (!TEST_ptr(serverpsk)
  3734. || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
  3735. BADALPN + 1,
  3736. BADALPNLEN - 1))
  3737. || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
  3738. GOODALPN + 1,
  3739. GOODALPNLEN - 1))
  3740. || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
  3741. sizeof(alpnlist))))
  3742. goto end;
  3743. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3744. edstatus = SSL_EARLY_DATA_ACCEPTED;
  3745. readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
  3746. /* SSL_connect() call should fail */
  3747. connectres = -1;
  3748. break;
  3749. default:
  3750. TEST_error("Bad test index");
  3751. goto end;
  3752. }
  3753. SSL_set_connect_state(clientssl);
  3754. if (err != 0) {
  3755. if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3756. &written))
  3757. || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
  3758. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
  3759. goto end;
  3760. } else {
  3761. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3762. &written)))
  3763. goto end;
  3764. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3765. &readbytes), readearlyres)
  3766. || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
  3767. && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3768. || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
  3769. || !TEST_int_eq(SSL_connect(clientssl), connectres))
  3770. goto end;
  3771. }
  3772. testresult = 1;
  3773. end:
  3774. SSL_SESSION_free(sess);
  3775. SSL_SESSION_free(clientpsk);
  3776. SSL_SESSION_free(serverpsk);
  3777. clientpsk = serverpsk = NULL;
  3778. SSL_free(serverssl);
  3779. SSL_free(clientssl);
  3780. SSL_CTX_free(sctx);
  3781. SSL_CTX_free(cctx);
  3782. return testresult;
  3783. }
  3784. /*
  3785. * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
  3786. * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
  3787. * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
  3788. * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3789. * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
  3790. * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
  3791. */
  3792. static int test_early_data_psk_with_all_ciphers(int idx)
  3793. {
  3794. SSL_CTX *cctx = NULL, *sctx = NULL;
  3795. SSL *clientssl = NULL, *serverssl = NULL;
  3796. int testresult = 0;
  3797. SSL_SESSION *sess = NULL;
  3798. unsigned char buf[20];
  3799. size_t readbytes, written;
  3800. const SSL_CIPHER *cipher;
  3801. const char *cipher_str[] = {
  3802. TLS1_3_RFC_AES_128_GCM_SHA256,
  3803. TLS1_3_RFC_AES_256_GCM_SHA384,
  3804. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3805. TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3806. # else
  3807. NULL,
  3808. # endif
  3809. TLS1_3_RFC_AES_128_CCM_SHA256,
  3810. TLS1_3_RFC_AES_128_CCM_8_SHA256
  3811. };
  3812. const unsigned char *cipher_bytes[] = {
  3813. TLS13_AES_128_GCM_SHA256_BYTES,
  3814. TLS13_AES_256_GCM_SHA384_BYTES,
  3815. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3816. TLS13_CHACHA20_POLY1305_SHA256_BYTES,
  3817. # else
  3818. NULL,
  3819. # endif
  3820. TLS13_AES_128_CCM_SHA256_BYTES,
  3821. TLS13_AES_128_CCM_8_SHA256_BYTES
  3822. };
  3823. if (cipher_str[idx] == NULL)
  3824. return 1;
  3825. /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
  3826. if (idx == 2 && is_fips == 1)
  3827. return 1;
  3828. /* We always set this up with a final parameter of "2" for PSK */
  3829. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3830. &serverssl, &sess, 2,
  3831. SHA384_DIGEST_LENGTH)))
  3832. goto end;
  3833. if (idx == 4) {
  3834. /* CCM8 ciphers are considered low security due to their short tag */
  3835. SSL_set_security_level(clientssl, 0);
  3836. SSL_set_security_level(serverssl, 0);
  3837. }
  3838. if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
  3839. || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
  3840. goto end;
  3841. /*
  3842. * 'setupearly_data_test' creates only one instance of SSL_SESSION
  3843. * and assigns to both client and server with incremented reference
  3844. * and the same instance is updated in 'sess'.
  3845. * So updating ciphersuite in 'sess' which will get reflected in
  3846. * PSK handshake using psk use sess and find sess cb.
  3847. */
  3848. cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
  3849. if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
  3850. goto end;
  3851. SSL_set_connect_state(clientssl);
  3852. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3853. &written)))
  3854. goto end;
  3855. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3856. &readbytes),
  3857. SSL_READ_EARLY_DATA_SUCCESS)
  3858. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3859. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3860. SSL_EARLY_DATA_ACCEPTED)
  3861. || !TEST_int_eq(SSL_connect(clientssl), 1)
  3862. || !TEST_int_eq(SSL_accept(serverssl), 1))
  3863. goto end;
  3864. /* Send some normal data from client to server */
  3865. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3866. || !TEST_size_t_eq(written, strlen(MSG2)))
  3867. goto end;
  3868. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3869. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3870. goto end;
  3871. testresult = 1;
  3872. end:
  3873. SSL_SESSION_free(sess);
  3874. SSL_SESSION_free(clientpsk);
  3875. SSL_SESSION_free(serverpsk);
  3876. clientpsk = serverpsk = NULL;
  3877. if (clientssl != NULL)
  3878. SSL_shutdown(clientssl);
  3879. if (serverssl != NULL)
  3880. SSL_shutdown(serverssl);
  3881. SSL_free(serverssl);
  3882. SSL_free(clientssl);
  3883. SSL_CTX_free(sctx);
  3884. SSL_CTX_free(cctx);
  3885. return testresult;
  3886. }
  3887. /*
  3888. * Test that a server that doesn't try to read early data can handle a
  3889. * client sending some.
  3890. */
  3891. static int test_early_data_not_expected(int idx)
  3892. {
  3893. SSL_CTX *cctx = NULL, *sctx = NULL;
  3894. SSL *clientssl = NULL, *serverssl = NULL;
  3895. int testresult = 0;
  3896. SSL_SESSION *sess = NULL;
  3897. unsigned char buf[20];
  3898. size_t readbytes, written;
  3899. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3900. &serverssl, &sess, idx,
  3901. SHA384_DIGEST_LENGTH)))
  3902. goto end;
  3903. /* Write some early data */
  3904. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3905. &written)))
  3906. goto end;
  3907. /*
  3908. * Server should skip over early data and then block waiting for client to
  3909. * continue handshake
  3910. */
  3911. if (!TEST_int_le(SSL_accept(serverssl), 0)
  3912. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3913. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3914. SSL_EARLY_DATA_REJECTED)
  3915. || !TEST_int_gt(SSL_accept(serverssl), 0)
  3916. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3917. SSL_EARLY_DATA_REJECTED))
  3918. goto end;
  3919. /* Send some normal data from client to server */
  3920. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3921. || !TEST_size_t_eq(written, strlen(MSG2)))
  3922. goto end;
  3923. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3924. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3925. goto end;
  3926. testresult = 1;
  3927. end:
  3928. SSL_SESSION_free(sess);
  3929. SSL_SESSION_free(clientpsk);
  3930. SSL_SESSION_free(serverpsk);
  3931. clientpsk = serverpsk = NULL;
  3932. SSL_free(serverssl);
  3933. SSL_free(clientssl);
  3934. SSL_CTX_free(sctx);
  3935. SSL_CTX_free(cctx);
  3936. return testresult;
  3937. }
  3938. # ifndef OPENSSL_NO_TLS1_2
  3939. /*
  3940. * Test that a server attempting to read early data can handle a connection
  3941. * from a TLSv1.2 client.
  3942. */
  3943. static int test_early_data_tls1_2(int idx)
  3944. {
  3945. SSL_CTX *cctx = NULL, *sctx = NULL;
  3946. SSL *clientssl = NULL, *serverssl = NULL;
  3947. int testresult = 0;
  3948. unsigned char buf[20];
  3949. size_t readbytes, written;
  3950. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3951. &serverssl, NULL, idx,
  3952. SHA384_DIGEST_LENGTH)))
  3953. goto end;
  3954. /* Write some data - should block due to handshake with server */
  3955. SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
  3956. SSL_set_connect_state(clientssl);
  3957. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3958. goto end;
  3959. /*
  3960. * Server should do TLSv1.2 handshake. First it will block waiting for more
  3961. * messages from client after ServerDone. Then SSL_read_early_data should
  3962. * finish and detect that early data has not been sent
  3963. */
  3964. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3965. &readbytes),
  3966. SSL_READ_EARLY_DATA_ERROR))
  3967. goto end;
  3968. /*
  3969. * Continue writing the message we started earlier. Will still block waiting
  3970. * for the CCS/Finished from server
  3971. */
  3972. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3973. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3974. &readbytes),
  3975. SSL_READ_EARLY_DATA_FINISH)
  3976. || !TEST_size_t_eq(readbytes, 0)
  3977. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3978. SSL_EARLY_DATA_NOT_SENT))
  3979. goto end;
  3980. /* Continue writing the message we started earlier */
  3981. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3982. || !TEST_size_t_eq(written, strlen(MSG1))
  3983. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3984. SSL_EARLY_DATA_NOT_SENT)
  3985. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3986. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3987. || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
  3988. || !TEST_size_t_eq(written, strlen(MSG2))
  3989. || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
  3990. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3991. goto end;
  3992. testresult = 1;
  3993. end:
  3994. SSL_SESSION_free(clientpsk);
  3995. SSL_SESSION_free(serverpsk);
  3996. clientpsk = serverpsk = NULL;
  3997. SSL_free(serverssl);
  3998. SSL_free(clientssl);
  3999. SSL_CTX_free(sctx);
  4000. SSL_CTX_free(cctx);
  4001. return testresult;
  4002. }
  4003. # endif /* OPENSSL_NO_TLS1_2 */
  4004. /*
  4005. * Test configuring the TLSv1.3 ciphersuites
  4006. *
  4007. * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
  4008. * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
  4009. * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
  4010. * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
  4011. * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  4012. * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  4013. * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
  4014. * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
  4015. * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
  4016. * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
  4017. */
  4018. static int test_set_ciphersuite(int idx)
  4019. {
  4020. SSL_CTX *cctx = NULL, *sctx = NULL;
  4021. SSL *clientssl = NULL, *serverssl = NULL;
  4022. int testresult = 0;
  4023. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4024. TLS_client_method(), TLS1_VERSION, 0,
  4025. &sctx, &cctx, cert, privkey))
  4026. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4027. "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
  4028. goto end;
  4029. if (idx >=4 && idx <= 7) {
  4030. /* SSL_CTX explicit cipher list */
  4031. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
  4032. goto end;
  4033. }
  4034. if (idx == 0 || idx == 4) {
  4035. /* Default ciphersuite */
  4036. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4037. "TLS_AES_128_GCM_SHA256")))
  4038. goto end;
  4039. } else if (idx == 1 || idx == 5) {
  4040. /* Non default ciphersuite */
  4041. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4042. "TLS_AES_128_CCM_SHA256")))
  4043. goto end;
  4044. }
  4045. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4046. &clientssl, NULL, NULL)))
  4047. goto end;
  4048. if (idx == 8 || idx == 9) {
  4049. /* SSL explicit cipher list */
  4050. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
  4051. goto end;
  4052. }
  4053. if (idx == 2 || idx == 6 || idx == 8) {
  4054. /* Default ciphersuite */
  4055. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  4056. "TLS_AES_128_GCM_SHA256")))
  4057. goto end;
  4058. } else if (idx == 3 || idx == 7 || idx == 9) {
  4059. /* Non default ciphersuite */
  4060. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  4061. "TLS_AES_128_CCM_SHA256")))
  4062. goto end;
  4063. }
  4064. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4065. goto end;
  4066. testresult = 1;
  4067. end:
  4068. SSL_free(serverssl);
  4069. SSL_free(clientssl);
  4070. SSL_CTX_free(sctx);
  4071. SSL_CTX_free(cctx);
  4072. return testresult;
  4073. }
  4074. static int test_ciphersuite_change(void)
  4075. {
  4076. SSL_CTX *cctx = NULL, *sctx = NULL;
  4077. SSL *clientssl = NULL, *serverssl = NULL;
  4078. SSL_SESSION *clntsess = NULL;
  4079. int testresult = 0;
  4080. const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
  4081. /* Create a session based on SHA-256 */
  4082. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4083. TLS_client_method(), TLS1_VERSION, 0,
  4084. &sctx, &cctx, cert, privkey))
  4085. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4086. "TLS_AES_128_GCM_SHA256:"
  4087. "TLS_AES_256_GCM_SHA384:"
  4088. "TLS_AES_128_CCM_SHA256"))
  4089. || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4090. "TLS_AES_128_GCM_SHA256")))
  4091. goto end;
  4092. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4093. NULL, NULL))
  4094. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4095. SSL_ERROR_NONE)))
  4096. goto end;
  4097. clntsess = SSL_get1_session(clientssl);
  4098. /* Save for later */
  4099. aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
  4100. SSL_shutdown(clientssl);
  4101. SSL_shutdown(serverssl);
  4102. SSL_free(serverssl);
  4103. SSL_free(clientssl);
  4104. serverssl = clientssl = NULL;
  4105. /* Check we can resume a session with a different SHA-256 ciphersuite */
  4106. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4107. "TLS_AES_128_CCM_SHA256"))
  4108. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4109. &clientssl, NULL, NULL))
  4110. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4111. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4112. SSL_ERROR_NONE))
  4113. || !TEST_true(SSL_session_reused(clientssl)))
  4114. goto end;
  4115. SSL_SESSION_free(clntsess);
  4116. clntsess = SSL_get1_session(clientssl);
  4117. SSL_shutdown(clientssl);
  4118. SSL_shutdown(serverssl);
  4119. SSL_free(serverssl);
  4120. SSL_free(clientssl);
  4121. serverssl = clientssl = NULL;
  4122. /*
  4123. * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
  4124. * succeeds but does not resume.
  4125. */
  4126. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  4127. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4128. NULL, NULL))
  4129. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4130. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4131. SSL_ERROR_SSL))
  4132. || !TEST_false(SSL_session_reused(clientssl)))
  4133. goto end;
  4134. SSL_SESSION_free(clntsess);
  4135. clntsess = NULL;
  4136. SSL_shutdown(clientssl);
  4137. SSL_shutdown(serverssl);
  4138. SSL_free(serverssl);
  4139. SSL_free(clientssl);
  4140. serverssl = clientssl = NULL;
  4141. /* Create a session based on SHA384 */
  4142. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  4143. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4144. &clientssl, NULL, NULL))
  4145. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4146. SSL_ERROR_NONE)))
  4147. goto end;
  4148. clntsess = SSL_get1_session(clientssl);
  4149. SSL_shutdown(clientssl);
  4150. SSL_shutdown(serverssl);
  4151. SSL_free(serverssl);
  4152. SSL_free(clientssl);
  4153. serverssl = clientssl = NULL;
  4154. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4155. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
  4156. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4157. "TLS_AES_256_GCM_SHA384"))
  4158. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4159. NULL, NULL))
  4160. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4161. /*
  4162. * We use SSL_ERROR_WANT_READ below so that we can pause the
  4163. * connection after the initial ClientHello has been sent to
  4164. * enable us to make some session changes.
  4165. */
  4166. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4167. SSL_ERROR_WANT_READ)))
  4168. goto end;
  4169. /* Trick the client into thinking this session is for a different digest */
  4170. clntsess->cipher = aes_128_gcm_sha256;
  4171. clntsess->cipher_id = clntsess->cipher->id;
  4172. /*
  4173. * Continue the previously started connection. Server has selected a SHA-384
  4174. * ciphersuite, but client thinks the session is for SHA-256, so it should
  4175. * bail out.
  4176. */
  4177. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  4178. SSL_ERROR_SSL))
  4179. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
  4180. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
  4181. goto end;
  4182. testresult = 1;
  4183. end:
  4184. SSL_SESSION_free(clntsess);
  4185. SSL_free(serverssl);
  4186. SSL_free(clientssl);
  4187. SSL_CTX_free(sctx);
  4188. SSL_CTX_free(cctx);
  4189. return testresult;
  4190. }
  4191. /*
  4192. * Test TLSv1.3 Key exchange
  4193. * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
  4194. * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
  4195. * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
  4196. * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
  4197. * Test 4 = Test NID_X25519 with TLSv1.3 client and server
  4198. * Test 5 = Test NID_X448 with TLSv1.3 client and server
  4199. * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
  4200. * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
  4201. * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
  4202. * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
  4203. * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
  4204. * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
  4205. * Test 12 = Test all ECDHE with TLSv1.2 client and server
  4206. * Test 13 = Test all FFDHE with TLSv1.2 client and server
  4207. */
  4208. # ifndef OPENSSL_NO_EC
  4209. static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
  4210. NID_secp521r1,
  4211. # ifndef OPENSSL_NO_ECX
  4212. NID_X25519, NID_X448
  4213. # endif
  4214. };
  4215. # endif
  4216. # ifndef OPENSSL_NO_DH
  4217. static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
  4218. NID_ffdhe6144, NID_ffdhe8192};
  4219. # endif
  4220. static int test_key_exchange(int idx)
  4221. {
  4222. SSL_CTX *sctx = NULL, *cctx = NULL;
  4223. SSL *serverssl = NULL, *clientssl = NULL;
  4224. int testresult = 0;
  4225. int kexch_alg;
  4226. int *kexch_groups = &kexch_alg;
  4227. int kexch_groups_size = 1;
  4228. int max_version = TLS1_3_VERSION;
  4229. char *kexch_name0 = NULL;
  4230. switch (idx) {
  4231. # ifndef OPENSSL_NO_EC
  4232. # ifndef OPENSSL_NO_TLS1_2
  4233. case 12:
  4234. max_version = TLS1_2_VERSION;
  4235. # endif
  4236. /* Fall through */
  4237. case 0:
  4238. kexch_groups = ecdhe_kexch_groups;
  4239. kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
  4240. kexch_name0 = "secp256r1";
  4241. break;
  4242. case 1:
  4243. kexch_alg = NID_X9_62_prime256v1;
  4244. kexch_name0 = "secp256r1";
  4245. break;
  4246. case 2:
  4247. kexch_alg = NID_secp384r1;
  4248. kexch_name0 = "secp384r1";
  4249. break;
  4250. case 3:
  4251. kexch_alg = NID_secp521r1;
  4252. kexch_name0 = "secp521r1";
  4253. break;
  4254. # ifndef OPENSSL_NO_ECX
  4255. case 4:
  4256. kexch_alg = NID_X25519;
  4257. kexch_name0 = "x25519";
  4258. break;
  4259. case 5:
  4260. kexch_alg = NID_X448;
  4261. kexch_name0 = "x448";
  4262. break;
  4263. # endif
  4264. # endif
  4265. # ifndef OPENSSL_NO_DH
  4266. # ifndef OPENSSL_NO_TLS1_2
  4267. case 13:
  4268. max_version = TLS1_2_VERSION;
  4269. kexch_name0 = "ffdhe2048";
  4270. # endif
  4271. /* Fall through */
  4272. case 6:
  4273. kexch_groups = ffdhe_kexch_groups;
  4274. kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
  4275. kexch_name0 = "ffdhe2048";
  4276. break;
  4277. case 7:
  4278. kexch_alg = NID_ffdhe2048;
  4279. kexch_name0 = "ffdhe2048";
  4280. break;
  4281. case 8:
  4282. kexch_alg = NID_ffdhe3072;
  4283. kexch_name0 = "ffdhe3072";
  4284. break;
  4285. case 9:
  4286. kexch_alg = NID_ffdhe4096;
  4287. kexch_name0 = "ffdhe4096";
  4288. break;
  4289. case 10:
  4290. kexch_alg = NID_ffdhe6144;
  4291. kexch_name0 = "ffdhe6144";
  4292. break;
  4293. case 11:
  4294. kexch_alg = NID_ffdhe8192;
  4295. kexch_name0 = "ffdhe8192";
  4296. break;
  4297. # endif
  4298. default:
  4299. /* We're skipping this test */
  4300. return 1;
  4301. }
  4302. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4303. TLS_client_method(), TLS1_VERSION,
  4304. max_version, &sctx, &cctx, cert,
  4305. privkey)))
  4306. goto end;
  4307. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4308. TLS1_3_RFC_AES_128_GCM_SHA256)))
  4309. goto end;
  4310. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4311. TLS1_3_RFC_AES_128_GCM_SHA256)))
  4312. goto end;
  4313. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  4314. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4315. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
  4316. || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  4317. goto end;
  4318. /*
  4319. * Must include an EC ciphersuite so that we send supported groups in
  4320. * TLSv1.2
  4321. */
  4322. # ifndef OPENSSL_NO_TLS1_2
  4323. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4324. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4325. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
  4326. goto end;
  4327. # endif
  4328. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4329. NULL, NULL)))
  4330. goto end;
  4331. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
  4332. || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
  4333. goto end;
  4334. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4335. goto end;
  4336. /*
  4337. * If Handshake succeeds the negotiated kexch alg should be the first one in
  4338. * configured, except in the case of FFDHE groups (idx 13), which are
  4339. * TLSv1.3 only so we expect no shared group to exist.
  4340. */
  4341. if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
  4342. idx == 13 ? 0 : kexch_groups[0]))
  4343. goto end;
  4344. if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
  4345. kexch_name0))
  4346. goto end;
  4347. /* We don't implement RFC 7919 named groups for TLS 1.2. */
  4348. if (idx != 13) {
  4349. if (!TEST_str_eq(SSL_get0_group_name(serverssl), kexch_name0)
  4350. || !TEST_str_eq(SSL_get0_group_name(clientssl), kexch_name0))
  4351. goto end;
  4352. if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
  4353. goto end;
  4354. if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
  4355. goto end;
  4356. }
  4357. testresult = 1;
  4358. end:
  4359. SSL_free(serverssl);
  4360. SSL_free(clientssl);
  4361. SSL_CTX_free(sctx);
  4362. SSL_CTX_free(cctx);
  4363. return testresult;
  4364. }
  4365. # if !defined(OPENSSL_NO_TLS1_2) \
  4366. && !defined(OPENSSL_NO_EC) \
  4367. && !defined(OPENSSL_NO_DH)
  4368. static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
  4369. int isecdhe, int idx)
  4370. {
  4371. int kexch_alg;
  4372. int *kexch_groups = &kexch_alg;
  4373. int numec, numff;
  4374. numec = OSSL_NELEM(ecdhe_kexch_groups);
  4375. numff = OSSL_NELEM(ffdhe_kexch_groups);
  4376. if (isecdhe)
  4377. kexch_alg = ecdhe_kexch_groups[idx];
  4378. else
  4379. kexch_alg = ffdhe_kexch_groups[idx];
  4380. if (clientmulti) {
  4381. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
  4382. return 0;
  4383. if (isecdhe) {
  4384. if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
  4385. numec)))
  4386. return 0;
  4387. } else {
  4388. if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
  4389. numff)))
  4390. return 0;
  4391. }
  4392. } else {
  4393. if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
  4394. return 0;
  4395. if (isecdhe) {
  4396. if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
  4397. numec)))
  4398. return 0;
  4399. } else {
  4400. if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
  4401. numff)))
  4402. return 0;
  4403. }
  4404. }
  4405. return 1;
  4406. }
  4407. /*-
  4408. * Test the SSL_get_negotiated_group() API across a battery of scenarios.
  4409. * Run through both the ECDHE and FFDHE group lists used in the previous
  4410. * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
  4411. * confirming the expected result; then perform a resumption handshake
  4412. * while offering the same group list, and another resumption handshake
  4413. * offering a different group list. The returned value should be the
  4414. * negotiated group for the initial handshake; for TLS 1.3 resumption
  4415. * handshakes the returned value will be negotiated on the resumption
  4416. * handshake itself, but for TLS 1.2 resumption handshakes the value will
  4417. * be cached in the session from the original handshake, regardless of what
  4418. * was offered in the resumption ClientHello.
  4419. *
  4420. * Using E for the number of EC groups and F for the number of FF groups:
  4421. * E tests of ECDHE with TLS 1.3, server only has one group
  4422. * F tests of FFDHE with TLS 1.3, server only has one group
  4423. * E tests of ECDHE with TLS 1.2, server only has one group
  4424. * F tests of FFDHE with TLS 1.2, server only has one group
  4425. * E tests of ECDHE with TLS 1.3, client sends only one group
  4426. * F tests of FFDHE with TLS 1.3, client sends only one group
  4427. * E tests of ECDHE with TLS 1.2, client sends only one group
  4428. * F tests of FFDHE with TLS 1.2, client sends only one group
  4429. */
  4430. static int test_negotiated_group(int idx)
  4431. {
  4432. int clientmulti, istls13, isecdhe, numec, numff, numgroups;
  4433. int expectednid;
  4434. SSL_CTX *sctx = NULL, *cctx = NULL;
  4435. SSL *serverssl = NULL, *clientssl = NULL;
  4436. SSL_SESSION *origsess = NULL;
  4437. int testresult = 0;
  4438. int kexch_alg;
  4439. int max_version = TLS1_3_VERSION;
  4440. numec = OSSL_NELEM(ecdhe_kexch_groups);
  4441. numff = OSSL_NELEM(ffdhe_kexch_groups);
  4442. numgroups = numec + numff;
  4443. clientmulti = (idx < 2 * numgroups);
  4444. idx = idx % (2 * numgroups);
  4445. istls13 = (idx < numgroups);
  4446. idx = idx % numgroups;
  4447. isecdhe = (idx < numec);
  4448. if (!isecdhe)
  4449. idx -= numec;
  4450. /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
  4451. if (isecdhe)
  4452. kexch_alg = ecdhe_kexch_groups[idx];
  4453. else
  4454. kexch_alg = ffdhe_kexch_groups[idx];
  4455. /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
  4456. if (!istls13 && !isecdhe)
  4457. expectednid = NID_undef;
  4458. else
  4459. expectednid = kexch_alg;
  4460. if (!istls13)
  4461. max_version = TLS1_2_VERSION;
  4462. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4463. TLS_client_method(), TLS1_VERSION,
  4464. max_version, &sctx, &cctx, cert,
  4465. privkey)))
  4466. goto end;
  4467. /*
  4468. * Force (EC)DHE ciphers for TLS 1.2.
  4469. * Be sure to enable auto tmp DH so that FFDHE can succeed.
  4470. */
  4471. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  4472. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4473. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
  4474. || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  4475. goto end;
  4476. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4477. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4478. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
  4479. goto end;
  4480. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4481. NULL, NULL)))
  4482. goto end;
  4483. if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
  4484. idx)))
  4485. goto end;
  4486. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4487. goto end;
  4488. /* Initial handshake; always the configured one */
  4489. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4490. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4491. goto end;
  4492. if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
  4493. goto end;
  4494. SSL_shutdown(clientssl);
  4495. SSL_shutdown(serverssl);
  4496. SSL_free(serverssl);
  4497. SSL_free(clientssl);
  4498. serverssl = clientssl = NULL;
  4499. /* First resumption attempt; use the same config as initial handshake */
  4500. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4501. NULL, NULL))
  4502. || !TEST_true(SSL_set_session(clientssl, origsess))
  4503. || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
  4504. isecdhe, idx)))
  4505. goto end;
  4506. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4507. || !TEST_true(SSL_session_reused(clientssl)))
  4508. goto end;
  4509. /* Still had better agree, since nothing changed... */
  4510. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4511. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4512. goto end;
  4513. SSL_shutdown(clientssl);
  4514. SSL_shutdown(serverssl);
  4515. SSL_free(serverssl);
  4516. SSL_free(clientssl);
  4517. serverssl = clientssl = NULL;
  4518. /*-
  4519. * Second resumption attempt
  4520. * The party that picks one group changes it, which we effectuate by
  4521. * changing 'idx' and updating what we expect.
  4522. */
  4523. if (idx == 0)
  4524. idx = 1;
  4525. else
  4526. idx--;
  4527. if (istls13) {
  4528. if (isecdhe)
  4529. expectednid = ecdhe_kexch_groups[idx];
  4530. else
  4531. expectednid = ffdhe_kexch_groups[idx];
  4532. /* Verify that we are changing what we expect. */
  4533. if (!TEST_int_ne(expectednid, kexch_alg))
  4534. goto end;
  4535. } else {
  4536. /* TLS 1.2 only supports named groups for ECDHE. */
  4537. if (isecdhe)
  4538. expectednid = kexch_alg;
  4539. else
  4540. expectednid = 0;
  4541. }
  4542. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4543. NULL, NULL))
  4544. || !TEST_true(SSL_set_session(clientssl, origsess))
  4545. || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
  4546. isecdhe, idx)))
  4547. goto end;
  4548. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4549. || !TEST_true(SSL_session_reused(clientssl)))
  4550. goto end;
  4551. /* Check that we get what we expected */
  4552. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4553. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4554. goto end;
  4555. testresult = 1;
  4556. end:
  4557. SSL_free(serverssl);
  4558. SSL_free(clientssl);
  4559. SSL_CTX_free(sctx);
  4560. SSL_CTX_free(cctx);
  4561. SSL_SESSION_free(origsess);
  4562. return testresult;
  4563. }
  4564. # endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
  4565. /*
  4566. * Test TLSv1.3 Cipher Suite
  4567. * Test 0 = Set TLS1.3 cipher on context
  4568. * Test 1 = Set TLS1.3 cipher on SSL
  4569. * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
  4570. * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
  4571. */
  4572. static int test_tls13_ciphersuite(int idx)
  4573. {
  4574. SSL_CTX *sctx = NULL, *cctx = NULL;
  4575. SSL *serverssl = NULL, *clientssl = NULL;
  4576. static const struct {
  4577. const char *ciphername;
  4578. int fipscapable;
  4579. int low_security;
  4580. } t13_ciphers[] = {
  4581. { TLS1_3_RFC_AES_128_GCM_SHA256, 1, 0 },
  4582. { TLS1_3_RFC_AES_256_GCM_SHA384, 1, 0 },
  4583. { TLS1_3_RFC_AES_128_CCM_SHA256, 1, 0 },
  4584. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  4585. { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
  4586. { TLS1_3_RFC_AES_256_GCM_SHA384
  4587. ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
  4588. # endif
  4589. /* CCM8 ciphers are considered low security due to their short tag */
  4590. { TLS1_3_RFC_AES_128_CCM_8_SHA256
  4591. ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1, 1 }
  4592. };
  4593. const char *t13_cipher = NULL;
  4594. const char *t12_cipher = NULL;
  4595. const char *negotiated_scipher;
  4596. const char *negotiated_ccipher;
  4597. int set_at_ctx = 0;
  4598. int set_at_ssl = 0;
  4599. int testresult = 0;
  4600. int max_ver;
  4601. size_t i;
  4602. switch (idx) {
  4603. case 0:
  4604. set_at_ctx = 1;
  4605. break;
  4606. case 1:
  4607. set_at_ssl = 1;
  4608. break;
  4609. case 2:
  4610. set_at_ctx = 1;
  4611. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4612. break;
  4613. case 3:
  4614. set_at_ssl = 1;
  4615. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4616. break;
  4617. }
  4618. for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
  4619. # ifdef OPENSSL_NO_TLS1_2
  4620. if (max_ver == TLS1_2_VERSION)
  4621. continue;
  4622. # endif
  4623. for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
  4624. if (is_fips && !t13_ciphers[i].fipscapable)
  4625. continue;
  4626. t13_cipher = t13_ciphers[i].ciphername;
  4627. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4628. TLS_client_method(),
  4629. TLS1_VERSION, max_ver,
  4630. &sctx, &cctx, cert, privkey)))
  4631. goto end;
  4632. if (t13_ciphers[i].low_security) {
  4633. SSL_CTX_set_security_level(sctx, 0);
  4634. SSL_CTX_set_security_level(cctx, 0);
  4635. }
  4636. if (set_at_ctx) {
  4637. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
  4638. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
  4639. goto end;
  4640. if (t12_cipher != NULL) {
  4641. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
  4642. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  4643. t12_cipher)))
  4644. goto end;
  4645. }
  4646. }
  4647. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4648. &clientssl, NULL, NULL)))
  4649. goto end;
  4650. if (set_at_ssl) {
  4651. if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
  4652. || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
  4653. goto end;
  4654. if (t12_cipher != NULL) {
  4655. if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
  4656. || !TEST_true(SSL_set_cipher_list(clientssl,
  4657. t12_cipher)))
  4658. goto end;
  4659. }
  4660. }
  4661. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4662. SSL_ERROR_NONE)))
  4663. goto end;
  4664. negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4665. serverssl));
  4666. negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4667. clientssl));
  4668. if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
  4669. goto end;
  4670. /*
  4671. * TEST_strn_eq is used below because t13_cipher can contain
  4672. * multiple ciphersuites
  4673. */
  4674. if (max_ver == TLS1_3_VERSION
  4675. && !TEST_strn_eq(t13_cipher, negotiated_scipher,
  4676. strlen(negotiated_scipher)))
  4677. goto end;
  4678. # ifndef OPENSSL_NO_TLS1_2
  4679. /* Below validation is not done when t12_cipher is NULL */
  4680. if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
  4681. && !TEST_str_eq(t12_cipher, negotiated_scipher))
  4682. goto end;
  4683. # endif
  4684. SSL_free(serverssl);
  4685. serverssl = NULL;
  4686. SSL_free(clientssl);
  4687. clientssl = NULL;
  4688. SSL_CTX_free(sctx);
  4689. sctx = NULL;
  4690. SSL_CTX_free(cctx);
  4691. cctx = NULL;
  4692. }
  4693. }
  4694. testresult = 1;
  4695. end:
  4696. SSL_free(serverssl);
  4697. SSL_free(clientssl);
  4698. SSL_CTX_free(sctx);
  4699. SSL_CTX_free(cctx);
  4700. return testresult;
  4701. }
  4702. /*
  4703. * Test TLSv1.3 PSKs
  4704. * Test 0 = Test new style callbacks
  4705. * Test 1 = Test both new and old style callbacks
  4706. * Test 2 = Test old style callbacks
  4707. * Test 3 = Test old style callbacks with no certificate
  4708. */
  4709. static int test_tls13_psk(int idx)
  4710. {
  4711. SSL_CTX *sctx = NULL, *cctx = NULL;
  4712. SSL *serverssl = NULL, *clientssl = NULL;
  4713. const SSL_CIPHER *cipher = NULL;
  4714. const unsigned char key[] = {
  4715. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4716. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4717. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  4718. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  4719. };
  4720. int testresult = 0;
  4721. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4722. TLS_client_method(), TLS1_VERSION, 0,
  4723. &sctx, &cctx, idx == 3 ? NULL : cert,
  4724. idx == 3 ? NULL : privkey)))
  4725. goto end;
  4726. if (idx != 3) {
  4727. /*
  4728. * We use a ciphersuite with SHA256 to ease testing old style PSK
  4729. * callbacks which will always default to SHA256. This should not be
  4730. * necessary if we have no cert/priv key. In that case the server should
  4731. * prefer SHA256 automatically.
  4732. */
  4733. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4734. "TLS_AES_128_GCM_SHA256")))
  4735. goto end;
  4736. } else {
  4737. /*
  4738. * As noted above the server should prefer SHA256 automatically. However
  4739. * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
  4740. * code works even if we are testing with only the FIPS provider loaded.
  4741. */
  4742. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4743. "TLS_AES_256_GCM_SHA384:"
  4744. "TLS_AES_128_GCM_SHA256")))
  4745. goto end;
  4746. }
  4747. /*
  4748. * Test 0: New style callbacks only
  4749. * Test 1: New and old style callbacks (only the new ones should be used)
  4750. * Test 2: Old style callbacks only
  4751. */
  4752. if (idx == 0 || idx == 1) {
  4753. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  4754. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  4755. }
  4756. #ifndef OPENSSL_NO_PSK
  4757. if (idx >= 1) {
  4758. SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
  4759. SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
  4760. }
  4761. #endif
  4762. srvid = pskid;
  4763. use_session_cb_cnt = 0;
  4764. find_session_cb_cnt = 0;
  4765. psk_client_cb_cnt = 0;
  4766. psk_server_cb_cnt = 0;
  4767. if (idx != 3) {
  4768. /*
  4769. * Check we can create a connection if callback decides not to send a
  4770. * PSK
  4771. */
  4772. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4773. NULL, NULL))
  4774. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4775. SSL_ERROR_NONE))
  4776. || !TEST_false(SSL_session_reused(clientssl))
  4777. || !TEST_false(SSL_session_reused(serverssl)))
  4778. goto end;
  4779. if (idx == 0 || idx == 1) {
  4780. if (!TEST_true(use_session_cb_cnt == 1)
  4781. || !TEST_true(find_session_cb_cnt == 0)
  4782. /*
  4783. * If no old style callback then below should be 0
  4784. * otherwise 1
  4785. */
  4786. || !TEST_true(psk_client_cb_cnt == idx)
  4787. || !TEST_true(psk_server_cb_cnt == 0))
  4788. goto end;
  4789. } else {
  4790. if (!TEST_true(use_session_cb_cnt == 0)
  4791. || !TEST_true(find_session_cb_cnt == 0)
  4792. || !TEST_true(psk_client_cb_cnt == 1)
  4793. || !TEST_true(psk_server_cb_cnt == 0))
  4794. goto end;
  4795. }
  4796. shutdown_ssl_connection(serverssl, clientssl);
  4797. serverssl = clientssl = NULL;
  4798. use_session_cb_cnt = psk_client_cb_cnt = 0;
  4799. }
  4800. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4801. NULL, NULL)))
  4802. goto end;
  4803. /* Create the PSK */
  4804. cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
  4805. clientpsk = SSL_SESSION_new();
  4806. if (!TEST_ptr(clientpsk)
  4807. || !TEST_ptr(cipher)
  4808. || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
  4809. sizeof(key)))
  4810. || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
  4811. || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
  4812. TLS1_3_VERSION))
  4813. || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
  4814. goto end;
  4815. serverpsk = clientpsk;
  4816. /* Check we can create a connection and the PSK is used */
  4817. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4818. || !TEST_true(SSL_session_reused(clientssl))
  4819. || !TEST_true(SSL_session_reused(serverssl)))
  4820. goto end;
  4821. if (idx == 0 || idx == 1) {
  4822. if (!TEST_true(use_session_cb_cnt == 1)
  4823. || !TEST_true(find_session_cb_cnt == 1)
  4824. || !TEST_true(psk_client_cb_cnt == 0)
  4825. || !TEST_true(psk_server_cb_cnt == 0))
  4826. goto end;
  4827. } else {
  4828. if (!TEST_true(use_session_cb_cnt == 0)
  4829. || !TEST_true(find_session_cb_cnt == 0)
  4830. || !TEST_true(psk_client_cb_cnt == 1)
  4831. || !TEST_true(psk_server_cb_cnt == 1))
  4832. goto end;
  4833. }
  4834. shutdown_ssl_connection(serverssl, clientssl);
  4835. serverssl = clientssl = NULL;
  4836. use_session_cb_cnt = find_session_cb_cnt = 0;
  4837. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4838. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4839. NULL, NULL)))
  4840. goto end;
  4841. /* Force an HRR */
  4842. #if defined(OPENSSL_NO_EC)
  4843. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  4844. goto end;
  4845. #else
  4846. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
  4847. goto end;
  4848. #endif
  4849. /*
  4850. * Check we can create a connection, the PSK is used and the callbacks are
  4851. * called twice.
  4852. */
  4853. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4854. || !TEST_true(SSL_session_reused(clientssl))
  4855. || !TEST_true(SSL_session_reused(serverssl)))
  4856. goto end;
  4857. if (idx == 0 || idx == 1) {
  4858. if (!TEST_true(use_session_cb_cnt == 2)
  4859. || !TEST_true(find_session_cb_cnt == 2)
  4860. || !TEST_true(psk_client_cb_cnt == 0)
  4861. || !TEST_true(psk_server_cb_cnt == 0))
  4862. goto end;
  4863. } else {
  4864. if (!TEST_true(use_session_cb_cnt == 0)
  4865. || !TEST_true(find_session_cb_cnt == 0)
  4866. || !TEST_true(psk_client_cb_cnt == 2)
  4867. || !TEST_true(psk_server_cb_cnt == 2))
  4868. goto end;
  4869. }
  4870. shutdown_ssl_connection(serverssl, clientssl);
  4871. serverssl = clientssl = NULL;
  4872. use_session_cb_cnt = find_session_cb_cnt = 0;
  4873. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4874. if (idx != 3) {
  4875. /*
  4876. * Check that if the server rejects the PSK we can still connect, but with
  4877. * a full handshake
  4878. */
  4879. srvid = "Dummy Identity";
  4880. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4881. NULL, NULL))
  4882. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4883. SSL_ERROR_NONE))
  4884. || !TEST_false(SSL_session_reused(clientssl))
  4885. || !TEST_false(SSL_session_reused(serverssl)))
  4886. goto end;
  4887. if (idx == 0 || idx == 1) {
  4888. if (!TEST_true(use_session_cb_cnt == 1)
  4889. || !TEST_true(find_session_cb_cnt == 1)
  4890. || !TEST_true(psk_client_cb_cnt == 0)
  4891. /*
  4892. * If no old style callback then below should be 0
  4893. * otherwise 1
  4894. */
  4895. || !TEST_true(psk_server_cb_cnt == idx))
  4896. goto end;
  4897. } else {
  4898. if (!TEST_true(use_session_cb_cnt == 0)
  4899. || !TEST_true(find_session_cb_cnt == 0)
  4900. || !TEST_true(psk_client_cb_cnt == 1)
  4901. || !TEST_true(psk_server_cb_cnt == 1))
  4902. goto end;
  4903. }
  4904. shutdown_ssl_connection(serverssl, clientssl);
  4905. serverssl = clientssl = NULL;
  4906. }
  4907. testresult = 1;
  4908. end:
  4909. SSL_SESSION_free(clientpsk);
  4910. SSL_SESSION_free(serverpsk);
  4911. clientpsk = serverpsk = NULL;
  4912. SSL_free(serverssl);
  4913. SSL_free(clientssl);
  4914. SSL_CTX_free(sctx);
  4915. SSL_CTX_free(cctx);
  4916. return testresult;
  4917. }
  4918. #ifndef OSSL_NO_USABLE_TLS1_3
  4919. /*
  4920. * Test TLS1.3 connection establishment succeeds with various configurations of
  4921. * the options `SSL_OP_ALLOW_NO_DHE_KEX` and `SSL_OP_PREFER_NO_DHE_KEX`.
  4922. * The verification of whether the right KEX mode is chosen is not covered by
  4923. * this test but by `test_tls13kexmodes`.
  4924. *
  4925. * Tests (idx & 1): Server has `SSL_OP_ALLOW_NO_DHE_KEX` set.
  4926. * Tests (idx & 2): Server has `SSL_OP_PREFER_NO_DHE_KEX` set.
  4927. * Tests (idx & 4): Client has `SSL_OP_ALLOW_NO_DHE_KEX` set.
  4928. */
  4929. static int test_tls13_no_dhe_kex(const int idx)
  4930. {
  4931. SSL_CTX *sctx = NULL, *cctx = NULL;
  4932. SSL *serverssl = NULL, *clientssl = NULL;
  4933. int testresult = 0;
  4934. size_t j;
  4935. SSL_SESSION *saved_session;
  4936. int server_allow_no_dhe = (idx & 1) != 0;
  4937. int server_prefer_no_dhe = (idx & 2) != 0;
  4938. int client_allow_no_dhe = (idx & 4) != 0;
  4939. uint64_t server_options = 0
  4940. | (server_allow_no_dhe ? SSL_OP_ALLOW_NO_DHE_KEX : 0)
  4941. | (server_prefer_no_dhe ? SSL_OP_PREFER_NO_DHE_KEX : 0);
  4942. uint64_t client_options = 0
  4943. | (client_allow_no_dhe ? SSL_OP_ALLOW_NO_DHE_KEX : 0);
  4944. new_called = 0;
  4945. do_cache = 1;
  4946. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4947. TLS_client_method(), TLS1_3_VERSION, 0,
  4948. &sctx, &cctx, cert, privkey)))
  4949. goto end;
  4950. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  4951. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  4952. SSL_CTX_set_options(sctx, server_options);
  4953. SSL_CTX_set_options(cctx, client_options);
  4954. SSL_CTX_sess_set_new_cb(cctx, new_cachesession_cb);
  4955. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4956. &clientssl, NULL, NULL)))
  4957. goto end;
  4958. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4959. SSL_ERROR_NONE))
  4960. /* Check we got the number of tickets we were expecting */
  4961. || !TEST_int_eq(2, new_called))
  4962. goto end;
  4963. /* We'll reuse the last ticket. */
  4964. saved_session = sesscache[new_called - 1];
  4965. SSL_shutdown(clientssl);
  4966. SSL_shutdown(serverssl);
  4967. SSL_free(serverssl);
  4968. SSL_free(clientssl);
  4969. SSL_CTX_free(cctx);
  4970. clientssl = serverssl = NULL;
  4971. cctx = NULL;
  4972. /*
  4973. * Now we resume with the last ticket we created.
  4974. */
  4975. /* The server context already exists, so we only create the client. */
  4976. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4977. TLS_client_method(), TLS1_3_VERSION, 0,
  4978. NULL, &cctx, cert, privkey)))
  4979. goto end;
  4980. SSL_CTX_set_options(cctx, client_options);
  4981. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4982. &clientssl, NULL, NULL))
  4983. || !TEST_true(SSL_set_session(clientssl, saved_session)))
  4984. goto end;
  4985. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4986. SSL_ERROR_NONE)))
  4987. goto end;
  4988. /*
  4989. * Make sure, the session was resumed.
  4990. */
  4991. if (!TEST_true(SSL_session_reused(clientssl)))
  4992. goto end;
  4993. SSL_shutdown(clientssl);
  4994. SSL_shutdown(serverssl);
  4995. testresult = 1;
  4996. end:
  4997. SSL_free(serverssl);
  4998. SSL_free(clientssl);
  4999. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  5000. SSL_SESSION_free(sesscache[j]);
  5001. sesscache[j] = NULL;
  5002. }
  5003. SSL_CTX_free(sctx);
  5004. SSL_CTX_free(cctx);
  5005. return testresult;
  5006. }
  5007. #endif /* OSSL_NO_USABLE_TLS1_3 */
  5008. static unsigned char cookie_magic_value[] = "cookie magic";
  5009. static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  5010. unsigned int *cookie_len)
  5011. {
  5012. /*
  5013. * Not suitable as a real cookie generation function but good enough for
  5014. * testing!
  5015. */
  5016. memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
  5017. *cookie_len = sizeof(cookie_magic_value) - 1;
  5018. return 1;
  5019. }
  5020. static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  5021. unsigned int cookie_len)
  5022. {
  5023. if (cookie_len == sizeof(cookie_magic_value) - 1
  5024. && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
  5025. return 1;
  5026. return 0;
  5027. }
  5028. static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
  5029. size_t *cookie_len)
  5030. {
  5031. unsigned int temp;
  5032. int res = generate_cookie_callback(ssl, cookie, &temp);
  5033. *cookie_len = temp;
  5034. return res;
  5035. }
  5036. static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
  5037. size_t cookie_len)
  5038. {
  5039. return verify_cookie_callback(ssl, cookie, cookie_len);
  5040. }
  5041. static int test_stateless(void)
  5042. {
  5043. SSL_CTX *sctx = NULL, *cctx = NULL;
  5044. SSL *serverssl = NULL, *clientssl = NULL;
  5045. int testresult = 0;
  5046. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5047. TLS_client_method(), TLS1_VERSION, 0,
  5048. &sctx, &cctx, cert, privkey)))
  5049. goto end;
  5050. /* The arrival of CCS messages can confuse the test */
  5051. SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  5052. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5053. NULL, NULL))
  5054. /* Send the first ClientHello */
  5055. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5056. SSL_ERROR_WANT_READ))
  5057. /*
  5058. * This should fail with a -1 return because we have no callbacks
  5059. * set up
  5060. */
  5061. || !TEST_int_eq(SSL_stateless(serverssl), -1))
  5062. goto end;
  5063. /* Fatal error so abandon the connection from this client */
  5064. SSL_free(clientssl);
  5065. clientssl = NULL;
  5066. /* Set up the cookie generation and verification callbacks */
  5067. SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
  5068. SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
  5069. /*
  5070. * Create a new connection from the client (we can reuse the server SSL
  5071. * object).
  5072. */
  5073. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5074. NULL, NULL))
  5075. /* Send the first ClientHello */
  5076. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5077. SSL_ERROR_WANT_READ))
  5078. /* This should fail because there is no cookie */
  5079. || !TEST_int_eq(SSL_stateless(serverssl), 0))
  5080. goto end;
  5081. /* Abandon the connection from this client */
  5082. SSL_free(clientssl);
  5083. clientssl = NULL;
  5084. /*
  5085. * Now create a connection from a new client but with the same server SSL
  5086. * object
  5087. */
  5088. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5089. NULL, NULL))
  5090. /* Send the first ClientHello */
  5091. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5092. SSL_ERROR_WANT_READ))
  5093. /* This should fail because there is no cookie */
  5094. || !TEST_int_eq(SSL_stateless(serverssl), 0)
  5095. /* Send the second ClientHello */
  5096. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5097. SSL_ERROR_WANT_READ))
  5098. /* This should succeed because a cookie is now present */
  5099. || !TEST_int_eq(SSL_stateless(serverssl), 1)
  5100. /* Complete the connection */
  5101. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5102. SSL_ERROR_NONE)))
  5103. goto end;
  5104. shutdown_ssl_connection(serverssl, clientssl);
  5105. serverssl = clientssl = NULL;
  5106. testresult = 1;
  5107. end:
  5108. SSL_free(serverssl);
  5109. SSL_free(clientssl);
  5110. SSL_CTX_free(sctx);
  5111. SSL_CTX_free(cctx);
  5112. return testresult;
  5113. }
  5114. #endif /* OSSL_NO_USABLE_TLS1_3 */
  5115. static int clntaddoldcb = 0;
  5116. static int clntparseoldcb = 0;
  5117. static int srvaddoldcb = 0;
  5118. static int srvparseoldcb = 0;
  5119. static int clntaddnewcb = 0;
  5120. static int clntparsenewcb = 0;
  5121. static int srvaddnewcb = 0;
  5122. static int srvparsenewcb = 0;
  5123. static int snicb = 0;
  5124. #define TEST_EXT_TYPE1 0xff00
  5125. static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
  5126. size_t *outlen, int *al, void *add_arg)
  5127. {
  5128. int *server = (int *)add_arg;
  5129. unsigned char *data;
  5130. if (SSL_is_server(s))
  5131. srvaddoldcb++;
  5132. else
  5133. clntaddoldcb++;
  5134. if (*server != SSL_is_server(s)
  5135. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  5136. return -1;
  5137. *data = 1;
  5138. *out = data;
  5139. *outlen = sizeof(char);
  5140. return 1;
  5141. }
  5142. static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
  5143. void *add_arg)
  5144. {
  5145. OPENSSL_free((unsigned char *)out);
  5146. }
  5147. static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
  5148. size_t inlen, int *al, void *parse_arg)
  5149. {
  5150. int *server = (int *)parse_arg;
  5151. if (SSL_is_server(s))
  5152. srvparseoldcb++;
  5153. else
  5154. clntparseoldcb++;
  5155. if (*server != SSL_is_server(s)
  5156. || inlen != sizeof(char)
  5157. || *in != 1)
  5158. return -1;
  5159. return 1;
  5160. }
  5161. static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
  5162. const unsigned char **out, size_t *outlen, X509 *x,
  5163. size_t chainidx, int *al, void *add_arg)
  5164. {
  5165. int *server = (int *)add_arg;
  5166. unsigned char *data;
  5167. if (SSL_is_server(s))
  5168. srvaddnewcb++;
  5169. else
  5170. clntaddnewcb++;
  5171. if (*server != SSL_is_server(s)
  5172. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  5173. return -1;
  5174. *data = 1;
  5175. *out = data;
  5176. *outlen = sizeof(*data);
  5177. return 1;
  5178. }
  5179. static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
  5180. const unsigned char *out, void *add_arg)
  5181. {
  5182. OPENSSL_free((unsigned char *)out);
  5183. }
  5184. static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
  5185. const unsigned char *in, size_t inlen, X509 *x,
  5186. size_t chainidx, int *al, void *parse_arg)
  5187. {
  5188. int *server = (int *)parse_arg;
  5189. if (SSL_is_server(s))
  5190. srvparsenewcb++;
  5191. else
  5192. clntparsenewcb++;
  5193. if (*server != SSL_is_server(s)
  5194. || inlen != sizeof(char) || *in != 1)
  5195. return -1;
  5196. return 1;
  5197. }
  5198. static int sni_cb(SSL *s, int *al, void *arg)
  5199. {
  5200. SSL_CTX *ctx = (SSL_CTX *)arg;
  5201. if (SSL_set_SSL_CTX(s, ctx) == NULL) {
  5202. *al = SSL_AD_INTERNAL_ERROR;
  5203. return SSL_TLSEXT_ERR_ALERT_FATAL;
  5204. }
  5205. snicb++;
  5206. return SSL_TLSEXT_ERR_OK;
  5207. }
  5208. static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  5209. {
  5210. return 1;
  5211. }
  5212. /*
  5213. * Custom call back tests.
  5214. * Test 0: Old style callbacks in TLSv1.2
  5215. * Test 1: New style callbacks in TLSv1.2
  5216. * Test 2: New style callbacks in TLSv1.2 with SNI
  5217. * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
  5218. * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
  5219. * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
  5220. */
  5221. static int test_custom_exts(int tst)
  5222. {
  5223. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  5224. SSL *clientssl = NULL, *serverssl = NULL;
  5225. int testresult = 0;
  5226. static int server = 1;
  5227. static int client = 0;
  5228. SSL_SESSION *sess = NULL;
  5229. unsigned int context;
  5230. #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  5231. /* Skip tests for TLSv1.2 and below in this case */
  5232. if (tst < 3)
  5233. return 1;
  5234. #endif
  5235. /* Reset callback counters */
  5236. clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
  5237. clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
  5238. snicb = 0;
  5239. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5240. TLS_client_method(), TLS1_VERSION, 0,
  5241. &sctx, &cctx, cert, privkey)))
  5242. goto end;
  5243. if (tst == 2
  5244. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
  5245. TLS1_VERSION, 0,
  5246. &sctx2, NULL, cert, privkey)))
  5247. goto end;
  5248. if (tst < 3) {
  5249. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  5250. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  5251. if (sctx2 != NULL)
  5252. SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
  5253. }
  5254. if (tst == 5) {
  5255. context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  5256. | SSL_EXT_TLS1_3_CERTIFICATE;
  5257. SSL_CTX_set_verify(sctx,
  5258. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  5259. verify_cb);
  5260. if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
  5261. SSL_FILETYPE_PEM), 1)
  5262. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
  5263. SSL_FILETYPE_PEM), 1)
  5264. || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
  5265. goto end;
  5266. } else if (tst == 4) {
  5267. context = SSL_EXT_CLIENT_HELLO
  5268. | SSL_EXT_TLS1_2_SERVER_HELLO
  5269. | SSL_EXT_TLS1_3_SERVER_HELLO
  5270. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  5271. | SSL_EXT_TLS1_3_CERTIFICATE
  5272. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
  5273. } else {
  5274. context = SSL_EXT_CLIENT_HELLO
  5275. | SSL_EXT_TLS1_2_SERVER_HELLO
  5276. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
  5277. }
  5278. /* Create a client side custom extension */
  5279. if (tst == 0) {
  5280. if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  5281. old_add_cb, old_free_cb,
  5282. &client, old_parse_cb,
  5283. &client)))
  5284. goto end;
  5285. } else {
  5286. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
  5287. new_add_cb, new_free_cb,
  5288. &client, new_parse_cb, &client)))
  5289. goto end;
  5290. }
  5291. /* Should not be able to add duplicates */
  5292. if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  5293. old_add_cb, old_free_cb,
  5294. &client, old_parse_cb,
  5295. &client))
  5296. || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
  5297. context, new_add_cb,
  5298. new_free_cb, &client,
  5299. new_parse_cb, &client)))
  5300. goto end;
  5301. /* Create a server side custom extension */
  5302. if (tst == 0) {
  5303. if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  5304. old_add_cb, old_free_cb,
  5305. &server, old_parse_cb,
  5306. &server)))
  5307. goto end;
  5308. } else {
  5309. if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
  5310. new_add_cb, new_free_cb,
  5311. &server, new_parse_cb, &server)))
  5312. goto end;
  5313. if (sctx2 != NULL
  5314. && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
  5315. context, new_add_cb,
  5316. new_free_cb, &server,
  5317. new_parse_cb, &server)))
  5318. goto end;
  5319. }
  5320. /* Should not be able to add duplicates */
  5321. if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  5322. old_add_cb, old_free_cb,
  5323. &server, old_parse_cb,
  5324. &server))
  5325. || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
  5326. context, new_add_cb,
  5327. new_free_cb, &server,
  5328. new_parse_cb, &server)))
  5329. goto end;
  5330. if (tst == 2) {
  5331. /* Set up SNI */
  5332. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  5333. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  5334. goto end;
  5335. }
  5336. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5337. &clientssl, NULL, NULL))
  5338. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5339. SSL_ERROR_NONE)))
  5340. goto end;
  5341. if (tst == 0) {
  5342. if (clntaddoldcb != 1
  5343. || clntparseoldcb != 1
  5344. || srvaddoldcb != 1
  5345. || srvparseoldcb != 1)
  5346. goto end;
  5347. } else if (tst == 1 || tst == 2 || tst == 3) {
  5348. if (clntaddnewcb != 1
  5349. || clntparsenewcb != 1
  5350. || srvaddnewcb != 1
  5351. || srvparsenewcb != 1
  5352. || (tst != 2 && snicb != 0)
  5353. || (tst == 2 && snicb != 1))
  5354. goto end;
  5355. } else if (tst == 5) {
  5356. if (clntaddnewcb != 1
  5357. || clntparsenewcb != 1
  5358. || srvaddnewcb != 1
  5359. || srvparsenewcb != 1)
  5360. goto end;
  5361. } else {
  5362. /* In this case there 2 NewSessionTicket messages created */
  5363. if (clntaddnewcb != 1
  5364. || clntparsenewcb != 5
  5365. || srvaddnewcb != 5
  5366. || srvparsenewcb != 1)
  5367. goto end;
  5368. }
  5369. sess = SSL_get1_session(clientssl);
  5370. SSL_shutdown(clientssl);
  5371. SSL_shutdown(serverssl);
  5372. SSL_free(serverssl);
  5373. SSL_free(clientssl);
  5374. serverssl = clientssl = NULL;
  5375. if (tst == 3 || tst == 5) {
  5376. /* We don't bother with the resumption aspects for these tests */
  5377. testresult = 1;
  5378. goto end;
  5379. }
  5380. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5381. NULL, NULL))
  5382. || !TEST_true(SSL_set_session(clientssl, sess))
  5383. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5384. SSL_ERROR_NONE)))
  5385. goto end;
  5386. /*
  5387. * For a resumed session we expect to add the ClientHello extension. For the
  5388. * old style callbacks we ignore it on the server side because they set
  5389. * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
  5390. * them.
  5391. */
  5392. if (tst == 0) {
  5393. if (clntaddoldcb != 2
  5394. || clntparseoldcb != 1
  5395. || srvaddoldcb != 1
  5396. || srvparseoldcb != 1)
  5397. goto end;
  5398. } else if (tst == 1 || tst == 2 || tst == 3) {
  5399. if (clntaddnewcb != 2
  5400. || clntparsenewcb != 2
  5401. || srvaddnewcb != 2
  5402. || srvparsenewcb != 2)
  5403. goto end;
  5404. } else {
  5405. /*
  5406. * No Certificate message extensions in the resumption handshake,
  5407. * 2 NewSessionTickets in the initial handshake, 1 in the resumption
  5408. */
  5409. if (clntaddnewcb != 2
  5410. || clntparsenewcb != 8
  5411. || srvaddnewcb != 8
  5412. || srvparsenewcb != 2)
  5413. goto end;
  5414. }
  5415. testresult = 1;
  5416. end:
  5417. SSL_SESSION_free(sess);
  5418. SSL_free(serverssl);
  5419. SSL_free(clientssl);
  5420. SSL_CTX_free(sctx2);
  5421. SSL_CTX_free(sctx);
  5422. SSL_CTX_free(cctx);
  5423. return testresult;
  5424. }
  5425. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  5426. #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
  5427. | SSL_EXT_CLIENT_HELLO \
  5428. | SSL_EXT_TLS1_2_SERVER_HELLO \
  5429. | SSL_EXT_IGNORE_ON_RESUMPTION)
  5430. #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
  5431. | SSL_EXT_TLS1_2_SERVER_HELLO \
  5432. | SSL_EXT_CLIENT_HELLO)
  5433. #define SERVERINFO_CUSTOM \
  5434. 0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
  5435. 0x00, 0x03, \
  5436. 0x04, 0x05, 0x06 \
  5437. static const unsigned char serverinfo_custom_tls13[] = {
  5438. 0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
  5439. SERVERINFO_CUSTOM
  5440. };
  5441. static const unsigned char serverinfo_custom_v2[] = {
  5442. 0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff, SYNTHV1CONTEXT & 0xff,
  5443. SERVERINFO_CUSTOM
  5444. };
  5445. static const unsigned char serverinfo_custom_v1[] = {
  5446. SERVERINFO_CUSTOM
  5447. };
  5448. static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
  5449. static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
  5450. static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
  5451. static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
  5452. unsigned int context,
  5453. const unsigned char *in,
  5454. size_t inlen, X509 *x,
  5455. size_t chainidx, int *al,
  5456. void *parse_arg)
  5457. {
  5458. const size_t len = serverinfo_custom_v1_len;
  5459. const unsigned char *si = &serverinfo_custom_v1[len - 3];
  5460. int *p_cb_result = (int*)parse_arg;
  5461. *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
  5462. return 1;
  5463. }
  5464. static int test_serverinfo_custom(const int idx)
  5465. {
  5466. SSL_CTX *sctx = NULL, *cctx = NULL;
  5467. SSL *clientssl = NULL, *serverssl = NULL;
  5468. int testresult = 0;
  5469. int cb_result = 0;
  5470. /*
  5471. * Following variables are set in the switch statement
  5472. * according to the test iteration.
  5473. * Default values do not make much sense: test would fail with them.
  5474. */
  5475. int serverinfo_version = 0;
  5476. int protocol_version = 0;
  5477. unsigned int extension_context = 0;
  5478. const unsigned char *si = NULL;
  5479. size_t si_len = 0;
  5480. const int call_use_serverinfo_ex = idx > 0;
  5481. switch (idx) {
  5482. case 0: /* FALLTHROUGH */
  5483. case 1:
  5484. serverinfo_version = SSL_SERVERINFOV1;
  5485. protocol_version = TLS1_2_VERSION;
  5486. extension_context = SYNTHV1CONTEXT;
  5487. si = serverinfo_custom_v1;
  5488. si_len = serverinfo_custom_v1_len;
  5489. break;
  5490. case 2:
  5491. serverinfo_version = SSL_SERVERINFOV2;
  5492. protocol_version = TLS1_2_VERSION;
  5493. extension_context = SYNTHV1CONTEXT;
  5494. si = serverinfo_custom_v2;
  5495. si_len = serverinfo_custom_v2_len;
  5496. break;
  5497. case 3:
  5498. serverinfo_version = SSL_SERVERINFOV2;
  5499. protocol_version = TLS1_3_VERSION;
  5500. extension_context = TLS13CONTEXT;
  5501. si = serverinfo_custom_tls13;
  5502. si_len = serverinfo_custom_tls13_len;
  5503. break;
  5504. }
  5505. if (!TEST_true(create_ssl_ctx_pair(libctx,
  5506. TLS_method(),
  5507. TLS_method(),
  5508. protocol_version,
  5509. protocol_version,
  5510. &sctx, &cctx, cert, privkey)))
  5511. goto end;
  5512. if (call_use_serverinfo_ex) {
  5513. if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
  5514. si, si_len)))
  5515. goto end;
  5516. } else {
  5517. if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
  5518. goto end;
  5519. }
  5520. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
  5521. extension_context,
  5522. NULL, NULL, NULL,
  5523. serverinfo_custom_parse_cb,
  5524. &cb_result))
  5525. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5526. NULL, NULL))
  5527. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5528. SSL_ERROR_NONE))
  5529. || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
  5530. goto end;
  5531. if (!TEST_true(cb_result))
  5532. goto end;
  5533. testresult = 1;
  5534. end:
  5535. SSL_free(serverssl);
  5536. SSL_free(clientssl);
  5537. SSL_CTX_free(sctx);
  5538. SSL_CTX_free(cctx);
  5539. return testresult;
  5540. }
  5541. #endif
  5542. /*
  5543. * Test that SSL_export_keying_material() produces expected results. There are
  5544. * no test vectors so all we do is test that both sides of the communication
  5545. * produce the same results for different protocol versions.
  5546. */
  5547. #define SMALL_LABEL_LEN 10
  5548. #define LONG_LABEL_LEN 249
  5549. static int test_export_key_mat(int tst)
  5550. {
  5551. int testresult = 0;
  5552. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  5553. SSL *clientssl = NULL, *serverssl = NULL;
  5554. const char label[LONG_LABEL_LEN + 1] = "test label";
  5555. const unsigned char context[] = "context";
  5556. const unsigned char *emptycontext = NULL;
  5557. unsigned char longcontext[1280];
  5558. int test_longcontext = fips_provider_version_ge(libctx, 3, 3, 0);
  5559. unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80], ckeymat4[80];
  5560. unsigned char skeymat1[80], skeymat2[80], skeymat3[80], skeymat4[80];
  5561. size_t labellen;
  5562. const int protocols[] = {
  5563. TLS1_VERSION,
  5564. TLS1_1_VERSION,
  5565. TLS1_2_VERSION,
  5566. TLS1_3_VERSION,
  5567. TLS1_3_VERSION,
  5568. TLS1_3_VERSION
  5569. };
  5570. #ifdef OPENSSL_NO_TLS1
  5571. if (tst == 0)
  5572. return 1;
  5573. #endif
  5574. #ifdef OPENSSL_NO_TLS1_1
  5575. if (tst == 1)
  5576. return 1;
  5577. #endif
  5578. if (is_fips && (tst == 0 || tst == 1))
  5579. return 1;
  5580. #ifdef OPENSSL_NO_TLS1_2
  5581. if (tst == 2)
  5582. return 1;
  5583. #endif
  5584. #ifdef OSSL_NO_USABLE_TLS1_3
  5585. if (tst >= 3)
  5586. return 1;
  5587. #endif
  5588. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5589. TLS_client_method(), TLS1_VERSION, 0,
  5590. &sctx, &cctx, cert, privkey)))
  5591. goto end;
  5592. OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
  5593. SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
  5594. SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
  5595. if ((protocols[tst] < TLS1_2_VERSION) &&
  5596. (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
  5597. || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
  5598. goto end;
  5599. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  5600. NULL)))
  5601. goto end;
  5602. /*
  5603. * Premature call of SSL_export_keying_material should just fail.
  5604. */
  5605. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  5606. sizeof(ckeymat1), label,
  5607. SMALL_LABEL_LEN + 1, context,
  5608. sizeof(context) - 1, 1), 0))
  5609. goto end;
  5610. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5611. SSL_ERROR_NONE)))
  5612. goto end;
  5613. if (tst == 5) {
  5614. /*
  5615. * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
  5616. * go over that.
  5617. */
  5618. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  5619. sizeof(ckeymat1), label,
  5620. LONG_LABEL_LEN + 1, context,
  5621. sizeof(context) - 1, 1), 0))
  5622. goto end;
  5623. testresult = 1;
  5624. goto end;
  5625. } else if (tst == 4) {
  5626. labellen = LONG_LABEL_LEN;
  5627. } else {
  5628. labellen = SMALL_LABEL_LEN;
  5629. }
  5630. memset(longcontext, 1, sizeof(longcontext));
  5631. if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
  5632. sizeof(ckeymat1), label,
  5633. labellen, context,
  5634. sizeof(context) - 1, 1), 1)
  5635. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
  5636. sizeof(ckeymat2), label,
  5637. labellen,
  5638. emptycontext,
  5639. 0, 1), 1)
  5640. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
  5641. sizeof(ckeymat3), label,
  5642. labellen,
  5643. NULL, 0, 0), 1)
  5644. || (test_longcontext
  5645. && !TEST_int_eq(SSL_export_keying_material(clientssl,
  5646. ckeymat4,
  5647. sizeof(ckeymat4), label,
  5648. labellen,
  5649. longcontext,
  5650. sizeof(longcontext), 1),
  5651. 1))
  5652. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
  5653. sizeof(skeymat1), label,
  5654. labellen,
  5655. context,
  5656. sizeof(context) -1, 1),
  5657. 1)
  5658. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
  5659. sizeof(skeymat2), label,
  5660. labellen,
  5661. emptycontext,
  5662. 0, 1), 1)
  5663. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
  5664. sizeof(skeymat3), label,
  5665. labellen,
  5666. NULL, 0, 0), 1)
  5667. || (test_longcontext
  5668. && !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat4,
  5669. sizeof(skeymat4), label,
  5670. labellen,
  5671. longcontext,
  5672. sizeof(longcontext), 1),
  5673. 1))
  5674. /*
  5675. * Check that both sides created the same key material with the
  5676. * same context.
  5677. */
  5678. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  5679. sizeof(skeymat1))
  5680. /*
  5681. * Check that both sides created the same key material with an
  5682. * empty context.
  5683. */
  5684. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5685. sizeof(skeymat2))
  5686. /*
  5687. * Check that both sides created the same key material without a
  5688. * context.
  5689. */
  5690. || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
  5691. sizeof(skeymat3))
  5692. /*
  5693. * Check that both sides created the same key material with a
  5694. * long context.
  5695. */
  5696. || (test_longcontext
  5697. && !TEST_mem_eq(ckeymat4, sizeof(ckeymat4), skeymat4,
  5698. sizeof(skeymat4)))
  5699. /* Different contexts should produce different results */
  5700. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5701. sizeof(ckeymat2)))
  5702. goto end;
  5703. /*
  5704. * Check that an empty context and no context produce different results in
  5705. * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
  5706. */
  5707. if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
  5708. sizeof(ckeymat3)))
  5709. || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
  5710. sizeof(ckeymat3))))
  5711. goto end;
  5712. testresult = 1;
  5713. end:
  5714. SSL_free(serverssl);
  5715. SSL_free(clientssl);
  5716. SSL_CTX_free(sctx2);
  5717. SSL_CTX_free(sctx);
  5718. SSL_CTX_free(cctx);
  5719. return testresult;
  5720. }
  5721. #ifndef OSSL_NO_USABLE_TLS1_3
  5722. /*
  5723. * Test that SSL_export_keying_material_early() produces expected
  5724. * results. There are no test vectors so all we do is test that both
  5725. * sides of the communication produce the same results for different
  5726. * protocol versions.
  5727. */
  5728. static int test_export_key_mat_early(int idx)
  5729. {
  5730. static const char label[] = "test label";
  5731. static const unsigned char context[] = "context";
  5732. int testresult = 0;
  5733. SSL_CTX *cctx = NULL, *sctx = NULL;
  5734. SSL *clientssl = NULL, *serverssl = NULL;
  5735. SSL_SESSION *sess = NULL;
  5736. const unsigned char *emptycontext = NULL;
  5737. unsigned char ckeymat1[80], ckeymat2[80];
  5738. unsigned char skeymat1[80], skeymat2[80];
  5739. unsigned char buf[1];
  5740. size_t readbytes, written;
  5741. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
  5742. &sess, idx, SHA384_DIGEST_LENGTH)))
  5743. goto end;
  5744. /* Here writing 0 length early data is enough. */
  5745. if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  5746. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  5747. &readbytes),
  5748. SSL_READ_EARLY_DATA_ERROR)
  5749. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  5750. SSL_EARLY_DATA_ACCEPTED))
  5751. goto end;
  5752. if (!TEST_int_eq(SSL_export_keying_material_early(
  5753. clientssl, ckeymat1, sizeof(ckeymat1), label,
  5754. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  5755. || !TEST_int_eq(SSL_export_keying_material_early(
  5756. clientssl, ckeymat2, sizeof(ckeymat2), label,
  5757. sizeof(label) - 1, emptycontext, 0), 1)
  5758. || !TEST_int_eq(SSL_export_keying_material_early(
  5759. serverssl, skeymat1, sizeof(skeymat1), label,
  5760. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  5761. || !TEST_int_eq(SSL_export_keying_material_early(
  5762. serverssl, skeymat2, sizeof(skeymat2), label,
  5763. sizeof(label) - 1, emptycontext, 0), 1)
  5764. /*
  5765. * Check that both sides created the same key material with the
  5766. * same context.
  5767. */
  5768. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  5769. sizeof(skeymat1))
  5770. /*
  5771. * Check that both sides created the same key material with an
  5772. * empty context.
  5773. */
  5774. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5775. sizeof(skeymat2))
  5776. /* Different contexts should produce different results */
  5777. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5778. sizeof(ckeymat2)))
  5779. goto end;
  5780. testresult = 1;
  5781. end:
  5782. SSL_SESSION_free(sess);
  5783. SSL_SESSION_free(clientpsk);
  5784. SSL_SESSION_free(serverpsk);
  5785. clientpsk = serverpsk = NULL;
  5786. SSL_free(serverssl);
  5787. SSL_free(clientssl);
  5788. SSL_CTX_free(sctx);
  5789. SSL_CTX_free(cctx);
  5790. return testresult;
  5791. }
  5792. #define NUM_KEY_UPDATE_MESSAGES 40
  5793. /*
  5794. * Test KeyUpdate.
  5795. */
  5796. static int test_key_update(void)
  5797. {
  5798. SSL_CTX *cctx = NULL, *sctx = NULL;
  5799. SSL *clientssl = NULL, *serverssl = NULL;
  5800. int testresult = 0, i, j;
  5801. char buf[20];
  5802. static char *mess = "A test message";
  5803. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5804. TLS_client_method(),
  5805. TLS1_3_VERSION,
  5806. 0,
  5807. &sctx, &cctx, cert, privkey))
  5808. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5809. NULL, NULL))
  5810. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5811. SSL_ERROR_NONE)))
  5812. goto end;
  5813. for (j = 0; j < 2; j++) {
  5814. /* Send lots of KeyUpdate messages */
  5815. for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
  5816. if (!TEST_true(SSL_key_update(clientssl,
  5817. (j == 0)
  5818. ? SSL_KEY_UPDATE_NOT_REQUESTED
  5819. : SSL_KEY_UPDATE_REQUESTED))
  5820. || !TEST_true(SSL_do_handshake(clientssl)))
  5821. goto end;
  5822. }
  5823. /* Check that sending and receiving app data is ok */
  5824. if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
  5825. || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
  5826. strlen(mess)))
  5827. goto end;
  5828. if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
  5829. || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
  5830. strlen(mess)))
  5831. goto end;
  5832. }
  5833. testresult = 1;
  5834. end:
  5835. SSL_free(serverssl);
  5836. SSL_free(clientssl);
  5837. SSL_CTX_free(sctx);
  5838. SSL_CTX_free(cctx);
  5839. return testresult;
  5840. }
  5841. /*
  5842. * Test we can handle a KeyUpdate (update requested) message while
  5843. * write data is pending in peer.
  5844. * Test 0: Client sends KeyUpdate while Server is writing
  5845. * Test 1: Server sends KeyUpdate while Client is writing
  5846. */
  5847. static int test_key_update_peer_in_write(int tst)
  5848. {
  5849. SSL_CTX *cctx = NULL, *sctx = NULL;
  5850. SSL *clientssl = NULL, *serverssl = NULL;
  5851. int testresult = 0;
  5852. char buf[20];
  5853. static char *mess = "A test message";
  5854. BIO *bretry = BIO_new(bio_s_always_retry());
  5855. BIO *tmp = NULL;
  5856. SSL *peerupdate = NULL, *peerwrite = NULL;
  5857. if (!TEST_ptr(bretry)
  5858. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5859. TLS_client_method(),
  5860. TLS1_3_VERSION,
  5861. 0,
  5862. &sctx, &cctx, cert, privkey))
  5863. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5864. NULL, NULL))
  5865. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5866. SSL_ERROR_NONE)))
  5867. goto end;
  5868. peerupdate = tst == 0 ? clientssl : serverssl;
  5869. peerwrite = tst == 0 ? serverssl : clientssl;
  5870. if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
  5871. || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
  5872. goto end;
  5873. /* Swap the writing endpoint's write BIO to force a retry */
  5874. tmp = SSL_get_wbio(peerwrite);
  5875. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  5876. tmp = NULL;
  5877. goto end;
  5878. }
  5879. SSL_set0_wbio(peerwrite, bretry);
  5880. bretry = NULL;
  5881. /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
  5882. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
  5883. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE)
  5884. || !TEST_true(SSL_want_write(peerwrite))
  5885. || !TEST_true(SSL_net_write_desired(peerwrite)))
  5886. goto end;
  5887. /* Reinstate the original writing endpoint's write BIO */
  5888. SSL_set0_wbio(peerwrite, tmp);
  5889. tmp = NULL;
  5890. /* Now read some data - we will read the key update */
  5891. if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
  5892. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ)
  5893. || !TEST_true(SSL_want_read(peerwrite))
  5894. || !TEST_true(SSL_net_read_desired(peerwrite)))
  5895. goto end;
  5896. /*
  5897. * Complete the write we started previously and read it from the other
  5898. * endpoint
  5899. */
  5900. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5901. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5902. goto end;
  5903. /* Write more data to ensure we send the KeyUpdate message back */
  5904. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5905. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5906. goto end;
  5907. if (!TEST_false(SSL_net_read_desired(peerwrite))
  5908. || !TEST_false(SSL_net_write_desired(peerwrite))
  5909. || !TEST_int_eq(SSL_want(peerwrite), SSL_NOTHING))
  5910. goto end;
  5911. testresult = 1;
  5912. end:
  5913. SSL_free(serverssl);
  5914. SSL_free(clientssl);
  5915. SSL_CTX_free(sctx);
  5916. SSL_CTX_free(cctx);
  5917. BIO_free(bretry);
  5918. BIO_free(tmp);
  5919. return testresult;
  5920. }
  5921. /*
  5922. * Test we can handle a KeyUpdate (update requested) message while
  5923. * peer read data is pending after peer accepted keyupdate(the msg header
  5924. * had been read 5 bytes).
  5925. * Test 0: Client sends KeyUpdate while Server is reading
  5926. * Test 1: Server sends KeyUpdate while Client is reading
  5927. */
  5928. static int test_key_update_peer_in_read(int tst)
  5929. {
  5930. SSL_CTX *cctx = NULL, *sctx = NULL;
  5931. SSL *clientssl = NULL, *serverssl = NULL;
  5932. int testresult = 0;
  5933. char prbuf[515], lwbuf[515] = {0};
  5934. static char *mess = "A test message";
  5935. BIO *lbio = NULL, *pbio = NULL;
  5936. SSL *local = NULL, *peer = NULL;
  5937. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5938. TLS_client_method(),
  5939. TLS1_3_VERSION,
  5940. 0,
  5941. &sctx, &cctx, cert, privkey))
  5942. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5943. NULL, NULL))
  5944. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5945. SSL_ERROR_NONE)))
  5946. goto end;
  5947. local = tst == 0 ? clientssl : serverssl;
  5948. peer = tst == 0 ? serverssl : clientssl;
  5949. if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
  5950. goto end;
  5951. SSL_set_bio(local, lbio, lbio);
  5952. SSL_set_bio(peer, pbio, pbio);
  5953. /*
  5954. * we first write keyupdate msg then appdata in local
  5955. * write data in local will fail with SSL_ERROR_WANT_WRITE,because
  5956. * lwbuf app data msg size + key updata msg size > 512(the size of
  5957. * the bio pair buffer)
  5958. */
  5959. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  5960. || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
  5961. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
  5962. goto end;
  5963. /*
  5964. * first read keyupdate msg in peer in peer
  5965. * then read appdata that we know will fail with SSL_ERROR_WANT_READ
  5966. */
  5967. if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
  5968. || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
  5969. goto end;
  5970. /* Now write some data in peer - we will write the key update */
  5971. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
  5972. goto end;
  5973. /*
  5974. * write data in local previously that we will complete
  5975. * read data in peer previously that we will complete
  5976. */
  5977. if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
  5978. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
  5979. goto end;
  5980. /* check that sending and receiving appdata ok */
  5981. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  5982. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
  5983. goto end;
  5984. testresult = 1;
  5985. end:
  5986. SSL_free(serverssl);
  5987. SSL_free(clientssl);
  5988. SSL_CTX_free(sctx);
  5989. SSL_CTX_free(cctx);
  5990. return testresult;
  5991. }
  5992. /*
  5993. * Test we can't send a KeyUpdate (update requested) message while
  5994. * local write data is pending.
  5995. * Test 0: Client sends KeyUpdate while Client is writing
  5996. * Test 1: Server sends KeyUpdate while Server is writing
  5997. */
  5998. static int test_key_update_local_in_write(int tst)
  5999. {
  6000. SSL_CTX *cctx = NULL, *sctx = NULL;
  6001. SSL *clientssl = NULL, *serverssl = NULL;
  6002. int testresult = 0;
  6003. char buf[20];
  6004. static char *mess = "A test message";
  6005. BIO *bretry = BIO_new(bio_s_always_retry());
  6006. BIO *tmp = NULL;
  6007. SSL *local = NULL, *peer = NULL;
  6008. if (!TEST_ptr(bretry)
  6009. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6010. TLS_client_method(),
  6011. TLS1_3_VERSION,
  6012. 0,
  6013. &sctx, &cctx, cert, privkey))
  6014. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6015. NULL, NULL))
  6016. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6017. SSL_ERROR_NONE)))
  6018. goto end;
  6019. local = tst == 0 ? clientssl : serverssl;
  6020. peer = tst == 0 ? serverssl : clientssl;
  6021. /* Swap the writing endpoint's write BIO to force a retry */
  6022. tmp = SSL_get_wbio(local);
  6023. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  6024. tmp = NULL;
  6025. goto end;
  6026. }
  6027. SSL_set0_wbio(local, bretry);
  6028. bretry = NULL;
  6029. /* write data in local will fail with SSL_ERROR_WANT_WRITE */
  6030. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
  6031. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
  6032. goto end;
  6033. /* Reinstate the original writing endpoint's write BIO */
  6034. SSL_set0_wbio(local, tmp);
  6035. tmp = NULL;
  6036. /* SSL_key_update will fail, because writing in local*/
  6037. if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6038. || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
  6039. goto end;
  6040. ERR_clear_error();
  6041. /* write data in local previously that we will complete */
  6042. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
  6043. goto end;
  6044. /* SSL_key_update will succeed because there is no pending write data */
  6045. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6046. || !TEST_int_eq(SSL_do_handshake(local), 1))
  6047. goto end;
  6048. /*
  6049. * we write some appdata in local
  6050. * read data in peer - we will read the keyupdate msg
  6051. */
  6052. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  6053. || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
  6054. goto end;
  6055. /* Write more peer more data to ensure we send the keyupdate message back */
  6056. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
  6057. || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
  6058. goto end;
  6059. testresult = 1;
  6060. end:
  6061. SSL_free(serverssl);
  6062. SSL_free(clientssl);
  6063. SSL_CTX_free(sctx);
  6064. SSL_CTX_free(cctx);
  6065. BIO_free(bretry);
  6066. BIO_free(tmp);
  6067. return testresult;
  6068. }
  6069. /*
  6070. * Test we can handle a KeyUpdate (update requested) message while
  6071. * local read data is pending(the msg header had been read 5 bytes).
  6072. * Test 0: Client sends KeyUpdate while Client is reading
  6073. * Test 1: Server sends KeyUpdate while Server is reading
  6074. */
  6075. static int test_key_update_local_in_read(int tst)
  6076. {
  6077. SSL_CTX *cctx = NULL, *sctx = NULL;
  6078. SSL *clientssl = NULL, *serverssl = NULL;
  6079. int testresult = 0;
  6080. char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
  6081. static char *mess = "A test message";
  6082. BIO *lbio = NULL, *pbio = NULL;
  6083. SSL *local = NULL, *peer = NULL;
  6084. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6085. TLS_client_method(),
  6086. TLS1_3_VERSION,
  6087. 0,
  6088. &sctx, &cctx, cert, privkey))
  6089. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6090. NULL, NULL))
  6091. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6092. SSL_ERROR_NONE)))
  6093. goto end;
  6094. local = tst == 0 ? clientssl : serverssl;
  6095. peer = tst == 0 ? serverssl : clientssl;
  6096. if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
  6097. goto end;
  6098. SSL_set_bio(local, lbio, lbio);
  6099. SSL_set_bio(peer, pbio, pbio);
  6100. /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
  6101. if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
  6102. || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
  6103. goto end;
  6104. /* read appdata in local will fail with SSL_ERROR_WANT_READ */
  6105. if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
  6106. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
  6107. goto end;
  6108. /* SSL_do_handshake will send keyupdate msg */
  6109. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6110. || !TEST_int_eq(SSL_do_handshake(local), 1))
  6111. goto end;
  6112. /*
  6113. * write data in peer previously that we will complete
  6114. * read data in local previously that we will complete
  6115. */
  6116. if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
  6117. || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
  6118. goto end;
  6119. /*
  6120. * write data in local
  6121. * read data in peer - we will read the key update
  6122. */
  6123. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  6124. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
  6125. goto end;
  6126. /* Write more peer data to ensure we send the keyupdate message back */
  6127. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
  6128. || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
  6129. goto end;
  6130. testresult = 1;
  6131. end:
  6132. SSL_free(serverssl);
  6133. SSL_free(clientssl);
  6134. SSL_CTX_free(sctx);
  6135. SSL_CTX_free(cctx);
  6136. return testresult;
  6137. }
  6138. #endif /* OSSL_NO_USABLE_TLS1_3 */
  6139. /*
  6140. * Test clearing a connection via SSL_clear(), or resetting it via
  6141. * SSL_set_connect_state()/SSL_set_accept_state()
  6142. * Test 0: SSL_set_connect_state, TLSv1.3
  6143. * Test 1: SSL_set_connect_state, TLSv1.2
  6144. * Test 2: SSL_set_accept_state, TLSv1.3
  6145. * Test 3: SSL_set_accept_state, TLSv1.2
  6146. * Test 4: SSL_clear (client), TLSv1.3
  6147. * Test 5: SSL_clear (client), TLSv1.2
  6148. * Test 6: SSL_clear (server), TLSv1.3
  6149. * Test 7: SSL_clear (server), TLSv1.2
  6150. */
  6151. static int test_ssl_clear(int idx)
  6152. {
  6153. SSL_CTX *cctx = NULL, *sctx = NULL;
  6154. SSL *clientssl = NULL, *serverssl = NULL;
  6155. SSL *writer, *reader;
  6156. int testresult = 0;
  6157. int tls12test, servertest, cleartest;
  6158. size_t written, readbytes;
  6159. const char *msg = "Hello World";
  6160. unsigned char buf[5];
  6161. tls12test = idx & 1;
  6162. idx >>= 1;
  6163. servertest = idx & 1;
  6164. idx >>= 1;
  6165. cleartest = idx & 1;
  6166. #ifdef OPENSSL_NO_TLS1_2
  6167. if (tls12test == 1)
  6168. return TEST_skip("No TLSv1.2 in this build");
  6169. #endif
  6170. /* Create an initial connection */
  6171. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6172. TLS_client_method(), TLS1_VERSION, 0,
  6173. &sctx, &cctx, cert, privkey))
  6174. || (tls12test
  6175. && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
  6176. TLS1_2_VERSION)))
  6177. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  6178. &clientssl, NULL, NULL))
  6179. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6180. SSL_ERROR_NONE)))
  6181. goto end;
  6182. if (servertest) {
  6183. writer = clientssl;
  6184. reader = serverssl;
  6185. } else {
  6186. writer = serverssl;
  6187. reader = clientssl;
  6188. }
  6189. /* Write some data */
  6190. if (!TEST_true(SSL_write_ex(writer, msg, strlen(msg), &written))
  6191. || written != strlen(msg))
  6192. goto end;
  6193. /*
  6194. * Read a partial record. The remaining buffered data should be cleared by
  6195. * the subsequent clear/reset
  6196. */
  6197. if (!TEST_true(SSL_read_ex(reader, buf, sizeof(buf), &readbytes))
  6198. || readbytes != sizeof(buf))
  6199. goto end;
  6200. SSL_shutdown(clientssl);
  6201. SSL_shutdown(serverssl);
  6202. /* Reset/clear one SSL object in order to reuse it. We free the other one */
  6203. if (servertest) {
  6204. if (cleartest) {
  6205. if (!TEST_true(SSL_clear(serverssl)))
  6206. goto end;
  6207. } else {
  6208. SSL_set_accept_state(serverssl);
  6209. }
  6210. /*
  6211. * A peculiarity of SSL_clear() is that it does not clear the session.
  6212. * This is intended behaviour so that a client can create a new
  6213. * connection and reuse the session. But this doesn't make much sense
  6214. * on the server side - and causes incorrect behaviour due to the
  6215. * handshake failing (even though the documentation does say SSL_clear()
  6216. * is supposed to work on the server side). We clear the session
  6217. * explicitly - although note that the documentation for
  6218. * SSL_set_session() says that its only useful for clients!
  6219. */
  6220. if (!TEST_true(SSL_set_session(serverssl, NULL)))
  6221. goto end;
  6222. SSL_free(clientssl);
  6223. clientssl = NULL;
  6224. } else {
  6225. if (cleartest) {
  6226. if (!TEST_true(SSL_clear(clientssl)))
  6227. goto end;
  6228. } else {
  6229. SSL_set_connect_state(clientssl);
  6230. }
  6231. SSL_free(serverssl);
  6232. serverssl = NULL;
  6233. }
  6234. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6235. NULL, NULL))
  6236. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6237. SSL_ERROR_NONE))
  6238. || !TEST_true(servertest || SSL_session_reused(clientssl)))
  6239. goto end;
  6240. SSL_shutdown(clientssl);
  6241. SSL_shutdown(serverssl);
  6242. testresult = 1;
  6243. end:
  6244. SSL_free(serverssl);
  6245. SSL_free(clientssl);
  6246. SSL_CTX_free(sctx);
  6247. SSL_CTX_free(cctx);
  6248. return testresult;
  6249. }
  6250. /* Parse CH and retrieve any MFL extension value if present */
  6251. static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
  6252. {
  6253. long len;
  6254. unsigned char *data;
  6255. PACKET pkt, pkt2, pkt3;
  6256. unsigned int MFL_code = 0, type = 0;
  6257. if (!TEST_uint_gt(len = BIO_get_mem_data(bio, (char **) &data), 0))
  6258. goto end;
  6259. memset(&pkt, 0, sizeof(pkt));
  6260. memset(&pkt2, 0, sizeof(pkt2));
  6261. memset(&pkt3, 0, sizeof(pkt3));
  6262. if (!TEST_long_gt(len, 0)
  6263. || !TEST_true(PACKET_buf_init(&pkt, data, len))
  6264. /* Skip the record header */
  6265. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
  6266. /* Skip the handshake message header */
  6267. || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  6268. /* Skip client version and random */
  6269. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  6270. + SSL3_RANDOM_SIZE))
  6271. /* Skip session id */
  6272. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  6273. /* Skip ciphers */
  6274. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  6275. /* Skip compression */
  6276. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  6277. /* Extensions len */
  6278. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  6279. goto end;
  6280. /* Loop through all extensions */
  6281. while (PACKET_remaining(&pkt2)) {
  6282. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  6283. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  6284. goto end;
  6285. if (type == TLSEXT_TYPE_max_fragment_length) {
  6286. if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
  6287. || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
  6288. goto end;
  6289. *mfl_codemfl_code = MFL_code;
  6290. return 1;
  6291. }
  6292. }
  6293. end:
  6294. return 0;
  6295. }
  6296. /* Maximum-Fragment-Length TLS extension mode to test */
  6297. static const unsigned char max_fragment_len_test[] = {
  6298. TLSEXT_max_fragment_length_512,
  6299. TLSEXT_max_fragment_length_1024,
  6300. TLSEXT_max_fragment_length_2048,
  6301. TLSEXT_max_fragment_length_4096
  6302. };
  6303. static int test_max_fragment_len_ext(int idx_tst)
  6304. {
  6305. SSL_CTX *ctx = NULL;
  6306. SSL *con = NULL;
  6307. int testresult = 0, MFL_mode = 0;
  6308. BIO *rbio, *wbio;
  6309. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
  6310. TLS1_VERSION, 0, NULL, &ctx, NULL,
  6311. NULL)))
  6312. return 0;
  6313. if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
  6314. ctx, max_fragment_len_test[idx_tst])))
  6315. goto end;
  6316. con = SSL_new(ctx);
  6317. if (!TEST_ptr(con))
  6318. goto end;
  6319. rbio = BIO_new(BIO_s_mem());
  6320. wbio = BIO_new(BIO_s_mem());
  6321. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  6322. BIO_free(rbio);
  6323. BIO_free(wbio);
  6324. goto end;
  6325. }
  6326. SSL_set_bio(con, rbio, wbio);
  6327. if (!TEST_int_le(SSL_connect(con), 0)) {
  6328. /* This shouldn't succeed because we don't have a server! */
  6329. goto end;
  6330. }
  6331. if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
  6332. /* no MFL in client hello */
  6333. goto end;
  6334. if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
  6335. goto end;
  6336. testresult = 1;
  6337. end:
  6338. SSL_free(con);
  6339. SSL_CTX_free(ctx);
  6340. return testresult;
  6341. }
  6342. #ifndef OSSL_NO_USABLE_TLS1_3
  6343. static int test_pha_key_update(void)
  6344. {
  6345. SSL_CTX *cctx = NULL, *sctx = NULL;
  6346. SSL *clientssl = NULL, *serverssl = NULL;
  6347. int testresult = 0;
  6348. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6349. TLS_client_method(), TLS1_VERSION, 0,
  6350. &sctx, &cctx, cert, privkey)))
  6351. return 0;
  6352. if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
  6353. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
  6354. || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
  6355. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
  6356. goto end;
  6357. SSL_CTX_set_post_handshake_auth(cctx, 1);
  6358. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6359. NULL, NULL)))
  6360. goto end;
  6361. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6362. SSL_ERROR_NONE)))
  6363. goto end;
  6364. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  6365. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  6366. goto end;
  6367. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  6368. goto end;
  6369. /* Start handshake on the server */
  6370. if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
  6371. goto end;
  6372. /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
  6373. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6374. SSL_ERROR_NONE)))
  6375. goto end;
  6376. SSL_shutdown(clientssl);
  6377. SSL_shutdown(serverssl);
  6378. testresult = 1;
  6379. end:
  6380. SSL_free(serverssl);
  6381. SSL_free(clientssl);
  6382. SSL_CTX_free(sctx);
  6383. SSL_CTX_free(cctx);
  6384. return testresult;
  6385. }
  6386. #endif
  6387. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  6388. static SRP_VBASE *vbase = NULL;
  6389. static int ssl_srp_cb(SSL *s, int *ad, void *arg)
  6390. {
  6391. int ret = SSL3_AL_FATAL;
  6392. char *username;
  6393. SRP_user_pwd *user = NULL;
  6394. username = SSL_get_srp_username(s);
  6395. if (username == NULL) {
  6396. *ad = SSL_AD_INTERNAL_ERROR;
  6397. goto err;
  6398. }
  6399. user = SRP_VBASE_get1_by_user(vbase, username);
  6400. if (user == NULL) {
  6401. *ad = SSL_AD_INTERNAL_ERROR;
  6402. goto err;
  6403. }
  6404. if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
  6405. user->info) <= 0) {
  6406. *ad = SSL_AD_INTERNAL_ERROR;
  6407. goto err;
  6408. }
  6409. ret = 0;
  6410. err:
  6411. SRP_user_pwd_free(user);
  6412. return ret;
  6413. }
  6414. static int create_new_vfile(char *userid, char *password, const char *filename)
  6415. {
  6416. char *gNid = NULL;
  6417. OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
  6418. TXT_DB *db = NULL;
  6419. int ret = 0;
  6420. BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
  6421. size_t i;
  6422. if (!TEST_ptr(dummy) || !TEST_ptr(row))
  6423. goto end;
  6424. gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
  6425. &row[DB_srpverifier], NULL, NULL, libctx, NULL);
  6426. if (!TEST_ptr(gNid))
  6427. goto end;
  6428. /*
  6429. * The only way to create an empty TXT_DB is to provide a BIO with no data
  6430. * in it!
  6431. */
  6432. db = TXT_DB_read(dummy, DB_NUMBER);
  6433. if (!TEST_ptr(db))
  6434. goto end;
  6435. out = BIO_new_file(filename, "w");
  6436. if (!TEST_ptr(out))
  6437. goto end;
  6438. row[DB_srpid] = OPENSSL_strdup(userid);
  6439. row[DB_srptype] = OPENSSL_strdup("V");
  6440. row[DB_srpgN] = OPENSSL_strdup(gNid);
  6441. if (!TEST_ptr(row[DB_srpid])
  6442. || !TEST_ptr(row[DB_srptype])
  6443. || !TEST_ptr(row[DB_srpgN])
  6444. || !TEST_true(TXT_DB_insert(db, row)))
  6445. goto end;
  6446. row = NULL;
  6447. if (TXT_DB_write(out, db) <= 0)
  6448. goto end;
  6449. ret = 1;
  6450. end:
  6451. if (row != NULL) {
  6452. for (i = 0; i < DB_NUMBER; i++)
  6453. OPENSSL_free(row[i]);
  6454. }
  6455. OPENSSL_free(row);
  6456. BIO_free(dummy);
  6457. BIO_free(out);
  6458. TXT_DB_free(db);
  6459. return ret;
  6460. }
  6461. static int create_new_vbase(char *userid, char *password)
  6462. {
  6463. BIGNUM *verifier = NULL, *salt = NULL;
  6464. const SRP_gN *lgN = NULL;
  6465. SRP_user_pwd *user_pwd = NULL;
  6466. int ret = 0;
  6467. lgN = SRP_get_default_gN(NULL);
  6468. if (!TEST_ptr(lgN))
  6469. goto end;
  6470. if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
  6471. lgN->N, lgN->g, libctx, NULL)))
  6472. goto end;
  6473. user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
  6474. if (!TEST_ptr(user_pwd))
  6475. goto end;
  6476. user_pwd->N = lgN->N;
  6477. user_pwd->g = lgN->g;
  6478. user_pwd->id = OPENSSL_strdup(userid);
  6479. if (!TEST_ptr(user_pwd->id))
  6480. goto end;
  6481. user_pwd->v = verifier;
  6482. user_pwd->s = salt;
  6483. verifier = salt = NULL;
  6484. if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
  6485. goto end;
  6486. user_pwd = NULL;
  6487. ret = 1;
  6488. end:
  6489. SRP_user_pwd_free(user_pwd);
  6490. BN_free(salt);
  6491. BN_free(verifier);
  6492. return ret;
  6493. }
  6494. /*
  6495. * SRP tests
  6496. *
  6497. * Test 0: Simple successful SRP connection, new vbase
  6498. * Test 1: Connection failure due to bad password, new vbase
  6499. * Test 2: Simple successful SRP connection, vbase loaded from existing file
  6500. * Test 3: Connection failure due to bad password, vbase loaded from existing
  6501. * file
  6502. * Test 4: Simple successful SRP connection, vbase loaded from new file
  6503. * Test 5: Connection failure due to bad password, vbase loaded from new file
  6504. */
  6505. static int test_srp(int tst)
  6506. {
  6507. char *userid = "test", *password = "password", *tstsrpfile;
  6508. SSL_CTX *cctx = NULL, *sctx = NULL;
  6509. SSL *clientssl = NULL, *serverssl = NULL;
  6510. int ret, testresult = 0;
  6511. vbase = SRP_VBASE_new(NULL);
  6512. if (!TEST_ptr(vbase))
  6513. goto end;
  6514. if (tst == 0 || tst == 1) {
  6515. if (!TEST_true(create_new_vbase(userid, password)))
  6516. goto end;
  6517. } else {
  6518. if (tst == 4 || tst == 5) {
  6519. if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
  6520. goto end;
  6521. tstsrpfile = tmpfilename;
  6522. } else {
  6523. tstsrpfile = srpvfile;
  6524. }
  6525. if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
  6526. goto end;
  6527. }
  6528. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6529. TLS_client_method(), TLS1_VERSION, 0,
  6530. &sctx, &cctx, cert, privkey)))
  6531. goto end;
  6532. if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
  6533. || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
  6534. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
  6535. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
  6536. || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
  6537. goto end;
  6538. if (tst % 2 == 1) {
  6539. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
  6540. goto end;
  6541. } else {
  6542. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
  6543. goto end;
  6544. }
  6545. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6546. NULL, NULL)))
  6547. goto end;
  6548. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  6549. if (ret) {
  6550. if (!TEST_true(tst % 2 == 0))
  6551. goto end;
  6552. } else {
  6553. if (!TEST_true(tst % 2 == 1))
  6554. goto end;
  6555. }
  6556. testresult = 1;
  6557. end:
  6558. SRP_VBASE_free(vbase);
  6559. vbase = NULL;
  6560. SSL_free(serverssl);
  6561. SSL_free(clientssl);
  6562. SSL_CTX_free(sctx);
  6563. SSL_CTX_free(cctx);
  6564. return testresult;
  6565. }
  6566. #endif
  6567. static int info_cb_failed = 0;
  6568. static int info_cb_offset = 0;
  6569. static int info_cb_this_state = -1;
  6570. static struct info_cb_states_st {
  6571. int where;
  6572. const char *statestr;
  6573. } info_cb_states[][60] = {
  6574. {
  6575. /* TLSv1.2 server followed by resumption */
  6576. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6577. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6578. {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
  6579. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
  6580. {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
  6581. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6582. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6583. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6584. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
  6585. {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6586. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
  6587. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6588. {SSL_CB_EXIT, NULL}, {0, NULL},
  6589. }, {
  6590. /* TLSv1.2 client followed by resumption */
  6591. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6592. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6593. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
  6594. {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
  6595. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
  6596. {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  6597. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6598. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6599. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6600. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  6601. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6602. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6603. }, {
  6604. /* TLSv1.3 server followed by resumption */
  6605. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6606. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6607. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
  6608. {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  6609. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  6610. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6611. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6612. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6613. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6614. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6615. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  6616. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6617. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6618. }, {
  6619. /* TLSv1.3 client followed by resumption */
  6620. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6621. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6622. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
  6623. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  6624. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6625. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6626. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
  6627. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  6628. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6629. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  6630. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6631. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6632. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6633. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
  6634. {SSL_CB_EXIT, NULL}, {0, NULL},
  6635. }, {
  6636. /* TLSv1.3 server, early_data */
  6637. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6638. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6639. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6640. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6641. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  6642. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
  6643. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6644. {SSL_CB_EXIT, NULL}, {0, NULL},
  6645. }, {
  6646. /* TLSv1.3 client, early_data */
  6647. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6648. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
  6649. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6650. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  6651. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6652. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
  6653. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6654. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6655. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6656. }, {
  6657. /* TLSv1.3 server, certificate compression, followed by resumption */
  6658. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6659. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6660. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSCC"},
  6661. {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  6662. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  6663. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6664. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6665. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6666. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6667. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6668. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  6669. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6670. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6671. }, {
  6672. /* TLSv1.3 client, certificate compression, followed by resumption */
  6673. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6674. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6675. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSCC"},
  6676. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  6677. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6678. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6679. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
  6680. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  6681. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6682. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  6683. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6684. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6685. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6686. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
  6687. {SSL_CB_EXIT, NULL}, {0, NULL},
  6688. }, {
  6689. {0, NULL},
  6690. }
  6691. };
  6692. static void sslapi_info_callback(const SSL *s, int where, int ret)
  6693. {
  6694. struct info_cb_states_st *state = info_cb_states[info_cb_offset];
  6695. /* We do not ever expect a connection to fail in this test */
  6696. if (!TEST_false(ret == 0)) {
  6697. info_cb_failed = 1;
  6698. return;
  6699. }
  6700. /*
  6701. * Do some sanity checks. We never expect these things to happen in this
  6702. * test
  6703. */
  6704. if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
  6705. || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
  6706. || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
  6707. info_cb_failed = 1;
  6708. return;
  6709. }
  6710. /* Now check we're in the right state */
  6711. if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
  6712. info_cb_failed = 1;
  6713. return;
  6714. }
  6715. if ((where & SSL_CB_LOOP) != 0
  6716. && !TEST_int_eq(strcmp(SSL_state_string(s),
  6717. state[info_cb_this_state].statestr), 0)) {
  6718. info_cb_failed = 1;
  6719. return;
  6720. }
  6721. /*
  6722. * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
  6723. */
  6724. if ((where & SSL_CB_HANDSHAKE_DONE)
  6725. && SSL_in_init((SSL *)s) != 0) {
  6726. info_cb_failed = 1;
  6727. return;
  6728. }
  6729. }
  6730. /*
  6731. * Test the info callback gets called when we expect it to.
  6732. *
  6733. * Test 0: TLSv1.2, server
  6734. * Test 1: TLSv1.2, client
  6735. * Test 2: TLSv1.3, server
  6736. * Test 3: TLSv1.3, client
  6737. * Test 4: TLSv1.3, server, early_data
  6738. * Test 5: TLSv1.3, client, early_data
  6739. * Test 6: TLSv1.3, server, compressed certificate
  6740. * Test 7: TLSv1.3, client, compressed certificate
  6741. */
  6742. static int test_info_callback(int tst)
  6743. {
  6744. SSL_CTX *cctx = NULL, *sctx = NULL;
  6745. SSL *clientssl = NULL, *serverssl = NULL;
  6746. SSL_SESSION *clntsess = NULL;
  6747. int testresult = 0;
  6748. int tlsvers;
  6749. if (tst < 2) {
  6750. /* We need either ECDHE or DHE for the TLSv1.2 test to work */
  6751. #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
  6752. || !defined(OPENSSL_NO_DH))
  6753. tlsvers = TLS1_2_VERSION;
  6754. #else
  6755. return 1;
  6756. #endif
  6757. } else {
  6758. #ifndef OSSL_NO_USABLE_TLS1_3
  6759. tlsvers = TLS1_3_VERSION;
  6760. #else
  6761. return 1;
  6762. #endif
  6763. }
  6764. /* Reset globals */
  6765. info_cb_failed = 0;
  6766. info_cb_this_state = -1;
  6767. info_cb_offset = tst;
  6768. #ifndef OSSL_NO_USABLE_TLS1_3
  6769. if (tst >= 4 && tst < 6) {
  6770. SSL_SESSION *sess = NULL;
  6771. size_t written, readbytes;
  6772. unsigned char buf[80];
  6773. /* early_data tests */
  6774. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  6775. &serverssl, &sess, 0,
  6776. SHA384_DIGEST_LENGTH)))
  6777. goto end;
  6778. /* We don't actually need this reference */
  6779. SSL_SESSION_free(sess);
  6780. SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
  6781. sslapi_info_callback);
  6782. /* Write and read some early data and then complete the connection */
  6783. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  6784. &written))
  6785. || !TEST_size_t_eq(written, strlen(MSG1))
  6786. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  6787. sizeof(buf), &readbytes),
  6788. SSL_READ_EARLY_DATA_SUCCESS)
  6789. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  6790. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  6791. SSL_EARLY_DATA_ACCEPTED)
  6792. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6793. SSL_ERROR_NONE))
  6794. || !TEST_false(info_cb_failed))
  6795. goto end;
  6796. testresult = 1;
  6797. goto end;
  6798. }
  6799. #endif
  6800. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6801. TLS_client_method(),
  6802. tlsvers, tlsvers, &sctx, &cctx, cert,
  6803. privkey)))
  6804. goto end;
  6805. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  6806. goto end;
  6807. /*
  6808. * For even numbered tests we check the server callbacks. For odd numbers we
  6809. * check the client.
  6810. */
  6811. SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
  6812. sslapi_info_callback);
  6813. if (tst >= 6) {
  6814. if (!SSL_CTX_compress_certs(sctx, 0))
  6815. goto end;
  6816. }
  6817. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  6818. &clientssl, NULL, NULL))
  6819. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6820. SSL_ERROR_NONE))
  6821. || !TEST_false(info_cb_failed))
  6822. goto end;
  6823. clntsess = SSL_get1_session(clientssl);
  6824. SSL_shutdown(clientssl);
  6825. SSL_shutdown(serverssl);
  6826. SSL_free(serverssl);
  6827. SSL_free(clientssl);
  6828. serverssl = clientssl = NULL;
  6829. /* Now do a resumption */
  6830. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  6831. NULL))
  6832. || !TEST_true(SSL_set_session(clientssl, clntsess))
  6833. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6834. SSL_ERROR_NONE))
  6835. || !TEST_true(SSL_session_reused(clientssl))
  6836. || !TEST_false(info_cb_failed))
  6837. goto end;
  6838. testresult = 1;
  6839. end:
  6840. SSL_free(serverssl);
  6841. SSL_free(clientssl);
  6842. SSL_SESSION_free(clntsess);
  6843. SSL_CTX_free(sctx);
  6844. SSL_CTX_free(cctx);
  6845. return testresult;
  6846. }
  6847. static int test_ssl_pending(int tst)
  6848. {
  6849. SSL_CTX *cctx = NULL, *sctx = NULL;
  6850. SSL *clientssl = NULL, *serverssl = NULL;
  6851. int testresult = 0;
  6852. char msg[] = "A test message";
  6853. char buf[5];
  6854. size_t written, readbytes;
  6855. if (tst == 0) {
  6856. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6857. TLS_client_method(),
  6858. TLS1_VERSION, 0,
  6859. &sctx, &cctx, cert, privkey)))
  6860. goto end;
  6861. } else {
  6862. #ifndef OPENSSL_NO_DTLS
  6863. if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
  6864. DTLS_client_method(),
  6865. DTLS1_VERSION, 0,
  6866. &sctx, &cctx, cert, privkey)))
  6867. goto end;
  6868. # ifdef OPENSSL_NO_DTLS1_2
  6869. /* Not supported in the FIPS provider */
  6870. if (is_fips) {
  6871. testresult = 1;
  6872. goto end;
  6873. };
  6874. /*
  6875. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  6876. * level 0
  6877. */
  6878. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  6879. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  6880. "DEFAULT:@SECLEVEL=0")))
  6881. goto end;
  6882. # endif
  6883. #else
  6884. return 1;
  6885. #endif
  6886. }
  6887. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6888. NULL, NULL))
  6889. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6890. SSL_ERROR_NONE)))
  6891. goto end;
  6892. if (!TEST_int_eq(SSL_pending(clientssl), 0)
  6893. || !TEST_false(SSL_has_pending(clientssl))
  6894. || !TEST_int_eq(SSL_pending(serverssl), 0)
  6895. || !TEST_false(SSL_has_pending(serverssl))
  6896. || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  6897. || !TEST_size_t_eq(written, sizeof(msg))
  6898. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  6899. || !TEST_size_t_eq(readbytes, sizeof(buf))
  6900. || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
  6901. || !TEST_true(SSL_has_pending(clientssl)))
  6902. goto end;
  6903. testresult = 1;
  6904. end:
  6905. SSL_free(serverssl);
  6906. SSL_free(clientssl);
  6907. SSL_CTX_free(sctx);
  6908. SSL_CTX_free(cctx);
  6909. return testresult;
  6910. }
  6911. static struct {
  6912. unsigned int maxprot;
  6913. const char *clntciphers;
  6914. const char *clnttls13ciphers;
  6915. const char *srvrciphers;
  6916. const char *srvrtls13ciphers;
  6917. const char *shared;
  6918. const char *fipsshared;
  6919. } shared_ciphers_data[] = {
  6920. /*
  6921. * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
  6922. * TLSv1.3 is enabled but TLSv1.2 is disabled.
  6923. */
  6924. #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  6925. {
  6926. TLS1_2_VERSION,
  6927. "AES128-SHA:AES256-SHA",
  6928. NULL,
  6929. "AES256-SHA:DHE-RSA-AES128-SHA",
  6930. NULL,
  6931. "AES256-SHA",
  6932. "AES256-SHA"
  6933. },
  6934. # if !defined(OPENSSL_NO_CHACHA) \
  6935. && !defined(OPENSSL_NO_POLY1305) \
  6936. && !defined(OPENSSL_NO_EC)
  6937. {
  6938. TLS1_2_VERSION,
  6939. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6940. NULL,
  6941. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6942. NULL,
  6943. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6944. "AES128-SHA"
  6945. },
  6946. # endif
  6947. {
  6948. TLS1_2_VERSION,
  6949. "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
  6950. NULL,
  6951. "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
  6952. NULL,
  6953. "AES128-SHA:AES256-SHA",
  6954. "AES128-SHA:AES256-SHA"
  6955. },
  6956. {
  6957. TLS1_2_VERSION,
  6958. "AES128-SHA:AES256-SHA",
  6959. NULL,
  6960. "AES128-SHA:DHE-RSA-AES128-SHA",
  6961. NULL,
  6962. "AES128-SHA",
  6963. "AES128-SHA"
  6964. },
  6965. #endif
  6966. /*
  6967. * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
  6968. * enabled.
  6969. */
  6970. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
  6971. && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  6972. {
  6973. TLS1_3_VERSION,
  6974. "AES128-SHA:AES256-SHA",
  6975. NULL,
  6976. "AES256-SHA:AES128-SHA256",
  6977. NULL,
  6978. "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
  6979. "TLS_AES_128_GCM_SHA256:AES256-SHA",
  6980. "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
  6981. },
  6982. #endif
  6983. #ifndef OSSL_NO_USABLE_TLS1_3
  6984. {
  6985. TLS1_3_VERSION,
  6986. "AES128-SHA",
  6987. "TLS_AES_256_GCM_SHA384",
  6988. "AES256-SHA",
  6989. "TLS_AES_256_GCM_SHA384",
  6990. "TLS_AES_256_GCM_SHA384",
  6991. "TLS_AES_256_GCM_SHA384"
  6992. },
  6993. #endif
  6994. };
  6995. static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
  6996. {
  6997. SSL_CTX *cctx = NULL, *sctx = NULL;
  6998. SSL *clientssl = NULL, *serverssl = NULL;
  6999. int testresult = 0;
  7000. char buf[1024];
  7001. OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
  7002. if (!TEST_ptr(tmplibctx))
  7003. goto end;
  7004. /*
  7005. * Regardless of whether we're testing with the FIPS provider loaded into
  7006. * libctx, we want one peer to always use the full set of ciphersuites
  7007. * available. Therefore we use a separate libctx with the default provider
  7008. * loaded into it. We run the same tests twice - once with the client side
  7009. * having the full set of ciphersuites and once with the server side.
  7010. */
  7011. if (clnt) {
  7012. cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
  7013. if (!TEST_ptr(cctx))
  7014. goto end;
  7015. } else {
  7016. sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
  7017. if (!TEST_ptr(sctx))
  7018. goto end;
  7019. }
  7020. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7021. TLS_client_method(),
  7022. TLS1_VERSION,
  7023. shared_ciphers_data[tst].maxprot,
  7024. &sctx, &cctx, cert, privkey)))
  7025. goto end;
  7026. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  7027. shared_ciphers_data[tst].clntciphers))
  7028. || (shared_ciphers_data[tst].clnttls13ciphers != NULL
  7029. && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  7030. shared_ciphers_data[tst].clnttls13ciphers)))
  7031. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  7032. shared_ciphers_data[tst].srvrciphers))
  7033. || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
  7034. && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  7035. shared_ciphers_data[tst].srvrtls13ciphers))))
  7036. goto end;
  7037. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7038. NULL, NULL))
  7039. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7040. SSL_ERROR_NONE)))
  7041. goto end;
  7042. if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
  7043. || !TEST_int_eq(strcmp(buf,
  7044. is_fips
  7045. ? shared_ciphers_data[tst].fipsshared
  7046. : shared_ciphers_data[tst].shared),
  7047. 0)) {
  7048. TEST_info("Shared ciphers are: %s\n", buf);
  7049. goto end;
  7050. }
  7051. testresult = 1;
  7052. end:
  7053. SSL_free(serverssl);
  7054. SSL_free(clientssl);
  7055. SSL_CTX_free(sctx);
  7056. SSL_CTX_free(cctx);
  7057. OSSL_LIB_CTX_free(tmplibctx);
  7058. return testresult;
  7059. }
  7060. static int test_ssl_get_shared_ciphers(int tst)
  7061. {
  7062. return int_test_ssl_get_shared_ciphers(tst, 0)
  7063. && int_test_ssl_get_shared_ciphers(tst, 1);
  7064. }
  7065. static const char *appdata = "Hello World";
  7066. static int gen_tick_called, dec_tick_called, tick_key_cb_called;
  7067. static int tick_key_renew = 0;
  7068. static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  7069. static int gen_tick_cb(SSL *s, void *arg)
  7070. {
  7071. gen_tick_called = 1;
  7072. return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
  7073. strlen(appdata));
  7074. }
  7075. static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
  7076. const unsigned char *keyname,
  7077. size_t keyname_length,
  7078. SSL_TICKET_STATUS status,
  7079. void *arg)
  7080. {
  7081. void *tickdata;
  7082. size_t tickdlen;
  7083. dec_tick_called = 1;
  7084. if (status == SSL_TICKET_EMPTY)
  7085. return SSL_TICKET_RETURN_IGNORE_RENEW;
  7086. if (!TEST_true(status == SSL_TICKET_SUCCESS
  7087. || status == SSL_TICKET_SUCCESS_RENEW))
  7088. return SSL_TICKET_RETURN_ABORT;
  7089. if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
  7090. &tickdlen))
  7091. || !TEST_size_t_eq(tickdlen, strlen(appdata))
  7092. || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
  7093. return SSL_TICKET_RETURN_ABORT;
  7094. if (tick_key_cb_called) {
  7095. /* Don't change what the ticket key callback wanted to do */
  7096. switch (status) {
  7097. case SSL_TICKET_NO_DECRYPT:
  7098. return SSL_TICKET_RETURN_IGNORE_RENEW;
  7099. case SSL_TICKET_SUCCESS:
  7100. return SSL_TICKET_RETURN_USE;
  7101. case SSL_TICKET_SUCCESS_RENEW:
  7102. return SSL_TICKET_RETURN_USE_RENEW;
  7103. default:
  7104. return SSL_TICKET_RETURN_ABORT;
  7105. }
  7106. }
  7107. return tick_dec_ret;
  7108. }
  7109. #ifndef OPENSSL_NO_DEPRECATED_3_0
  7110. static int tick_key_cb(SSL *s, unsigned char key_name[16],
  7111. unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
  7112. HMAC_CTX *hctx, int enc)
  7113. {
  7114. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  7115. const unsigned char tick_hmac_key[16] = "0123456789abcdef";
  7116. EVP_CIPHER *aes128cbc;
  7117. EVP_MD *sha256;
  7118. int ret;
  7119. tick_key_cb_called = 1;
  7120. if (tick_key_renew == -1)
  7121. return 0;
  7122. aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  7123. if (!TEST_ptr(aes128cbc))
  7124. return 0;
  7125. sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
  7126. if (!TEST_ptr(sha256)) {
  7127. EVP_CIPHER_free(aes128cbc);
  7128. return 0;
  7129. }
  7130. memset(iv, 0, AES_BLOCK_SIZE);
  7131. memset(key_name, 0, 16);
  7132. if (aes128cbc == NULL
  7133. || sha256 == NULL
  7134. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  7135. || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
  7136. NULL))
  7137. ret = -1;
  7138. else
  7139. ret = tick_key_renew ? 2 : 1;
  7140. EVP_CIPHER_free(aes128cbc);
  7141. EVP_MD_free(sha256);
  7142. return ret;
  7143. }
  7144. #endif
  7145. static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
  7146. unsigned char iv[EVP_MAX_IV_LENGTH],
  7147. EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
  7148. {
  7149. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  7150. unsigned char tick_hmac_key[16] = "0123456789abcdef";
  7151. OSSL_PARAM params[2];
  7152. EVP_CIPHER *aes128cbc;
  7153. int ret;
  7154. tick_key_cb_called = 1;
  7155. if (tick_key_renew == -1)
  7156. return 0;
  7157. aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  7158. if (!TEST_ptr(aes128cbc))
  7159. return 0;
  7160. memset(iv, 0, AES_BLOCK_SIZE);
  7161. memset(key_name, 0, 16);
  7162. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  7163. "SHA256", 0);
  7164. params[1] = OSSL_PARAM_construct_end();
  7165. if (aes128cbc == NULL
  7166. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  7167. || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
  7168. params))
  7169. ret = -1;
  7170. else
  7171. ret = tick_key_renew ? 2 : 1;
  7172. EVP_CIPHER_free(aes128cbc);
  7173. return ret;
  7174. }
  7175. /*
  7176. * Test the various ticket callbacks
  7177. * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
  7178. * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
  7179. * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
  7180. * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
  7181. * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
  7182. * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
  7183. * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
  7184. * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
  7185. * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
  7186. * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
  7187. * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
  7188. * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
  7189. * Test 12: TLSv1.2, old ticket key callback, no ticket
  7190. * Test 13: TLSv1.3, old ticket key callback, no ticket
  7191. * Test 14: TLSv1.2, ticket key callback, ticket, no renewal
  7192. * Test 15: TLSv1.3, ticket key callback, ticket, no renewal
  7193. * Test 16: TLSv1.2, ticket key callback, ticket, renewal
  7194. * Test 17: TLSv1.3, ticket key callback, ticket, renewal
  7195. * Test 18: TLSv1.2, ticket key callback, no ticket
  7196. * Test 19: TLSv1.3, ticket key callback, no ticket
  7197. */
  7198. static int test_ticket_callbacks(int tst)
  7199. {
  7200. SSL_CTX *cctx = NULL, *sctx = NULL;
  7201. SSL *clientssl = NULL, *serverssl = NULL;
  7202. SSL_SESSION *clntsess = NULL;
  7203. int testresult = 0;
  7204. #ifdef OPENSSL_NO_TLS1_2
  7205. if (tst % 2 == 0)
  7206. return 1;
  7207. #endif
  7208. #ifdef OSSL_NO_USABLE_TLS1_3
  7209. if (tst % 2 == 1)
  7210. return 1;
  7211. #endif
  7212. #ifdef OPENSSL_NO_DEPRECATED_3_0
  7213. if (tst >= 8 && tst <= 13)
  7214. return 1;
  7215. #endif
  7216. gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
  7217. /* Which tests the ticket key callback should request renewal for */
  7218. if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
  7219. tick_key_renew = 1;
  7220. else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
  7221. tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
  7222. else
  7223. tick_key_renew = 0;
  7224. /* Which tests the decrypt ticket callback should request renewal for */
  7225. switch (tst) {
  7226. case 0:
  7227. case 1:
  7228. tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
  7229. break;
  7230. case 2:
  7231. case 3:
  7232. tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
  7233. break;
  7234. case 4:
  7235. case 5:
  7236. tick_dec_ret = SSL_TICKET_RETURN_USE;
  7237. break;
  7238. case 6:
  7239. case 7:
  7240. tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
  7241. break;
  7242. default:
  7243. tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  7244. }
  7245. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7246. TLS_client_method(),
  7247. TLS1_VERSION,
  7248. ((tst % 2) == 0) ? TLS1_2_VERSION
  7249. : TLS1_3_VERSION,
  7250. &sctx, &cctx, cert, privkey)))
  7251. goto end;
  7252. /*
  7253. * We only want sessions to resume from tickets - not the session cache. So
  7254. * switch the cache off.
  7255. */
  7256. if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
  7257. goto end;
  7258. if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
  7259. NULL)))
  7260. goto end;
  7261. if (tst >= 14) {
  7262. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
  7263. goto end;
  7264. #ifndef OPENSSL_NO_DEPRECATED_3_0
  7265. } else if (tst >= 8) {
  7266. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
  7267. goto end;
  7268. #endif
  7269. }
  7270. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7271. NULL, NULL))
  7272. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7273. SSL_ERROR_NONE)))
  7274. goto end;
  7275. /*
  7276. * The decrypt ticket key callback in TLSv1.2 should be called even though
  7277. * we have no ticket yet, because it gets called with a status of
  7278. * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
  7279. * actually send any ticket data). This does not happen in TLSv1.3 because
  7280. * it is not valid to send empty ticket data in TLSv1.3.
  7281. */
  7282. if (!TEST_int_eq(gen_tick_called, 1)
  7283. || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
  7284. goto end;
  7285. gen_tick_called = dec_tick_called = 0;
  7286. clntsess = SSL_get1_session(clientssl);
  7287. SSL_shutdown(clientssl);
  7288. SSL_shutdown(serverssl);
  7289. SSL_free(serverssl);
  7290. SSL_free(clientssl);
  7291. serverssl = clientssl = NULL;
  7292. /* Now do a resumption */
  7293. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  7294. NULL))
  7295. || !TEST_true(SSL_set_session(clientssl, clntsess))
  7296. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7297. SSL_ERROR_NONE)))
  7298. goto end;
  7299. if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
  7300. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  7301. || tick_key_renew == -1) {
  7302. if (!TEST_false(SSL_session_reused(clientssl)))
  7303. goto end;
  7304. } else {
  7305. if (!TEST_true(SSL_session_reused(clientssl)))
  7306. goto end;
  7307. }
  7308. if (!TEST_int_eq(gen_tick_called,
  7309. (tick_key_renew
  7310. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  7311. || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
  7312. ? 1 : 0)
  7313. /* There is no ticket to decrypt in tests 13 and 19 */
  7314. || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1))
  7315. goto end;
  7316. testresult = 1;
  7317. end:
  7318. SSL_SESSION_free(clntsess);
  7319. SSL_free(serverssl);
  7320. SSL_free(clientssl);
  7321. SSL_CTX_free(sctx);
  7322. SSL_CTX_free(cctx);
  7323. return testresult;
  7324. }
  7325. /*
  7326. * Test incorrect shutdown.
  7327. * Test 0: client does not shutdown properly,
  7328. * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
  7329. * server should get SSL_ERROR_SSL
  7330. * Test 1: client does not shutdown properly,
  7331. * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
  7332. * server should get SSL_ERROR_ZERO_RETURN
  7333. */
  7334. static int test_incorrect_shutdown(int tst)
  7335. {
  7336. SSL_CTX *cctx = NULL, *sctx = NULL;
  7337. SSL *clientssl = NULL, *serverssl = NULL;
  7338. int testresult = 0;
  7339. char buf[80];
  7340. BIO *c2s;
  7341. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7342. TLS_client_method(), 0, 0,
  7343. &sctx, &cctx, cert, privkey)))
  7344. goto end;
  7345. if (tst == 1)
  7346. SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
  7347. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7348. NULL, NULL)))
  7349. goto end;
  7350. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  7351. SSL_ERROR_NONE)))
  7352. goto end;
  7353. c2s = SSL_get_rbio(serverssl);
  7354. BIO_set_mem_eof_return(c2s, 0);
  7355. if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
  7356. goto end;
  7357. if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
  7358. goto end;
  7359. if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
  7360. goto end;
  7361. testresult = 1;
  7362. end:
  7363. SSL_free(serverssl);
  7364. SSL_free(clientssl);
  7365. SSL_CTX_free(sctx);
  7366. SSL_CTX_free(cctx);
  7367. return testresult;
  7368. }
  7369. /*
  7370. * Test bi-directional shutdown.
  7371. * Test 0: TLSv1.2
  7372. * Test 1: TLSv1.2, server continues to read/write after client shutdown
  7373. * Test 2: TLSv1.3, no pending NewSessionTicket messages
  7374. * Test 3: TLSv1.3, pending NewSessionTicket messages
  7375. * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
  7376. * sends key update, client reads it
  7377. * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
  7378. * sends CertificateRequest, client reads and ignores it
  7379. * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
  7380. * doesn't read it
  7381. */
  7382. static int test_shutdown(int tst)
  7383. {
  7384. SSL_CTX *cctx = NULL, *sctx = NULL;
  7385. SSL *clientssl = NULL, *serverssl = NULL;
  7386. int testresult = 0;
  7387. char msg[] = "A test message";
  7388. char buf[80];
  7389. size_t written, readbytes;
  7390. SSL_SESSION *sess;
  7391. #ifdef OPENSSL_NO_TLS1_2
  7392. if (tst <= 1)
  7393. return 1;
  7394. #endif
  7395. #ifdef OSSL_NO_USABLE_TLS1_3
  7396. if (tst >= 2)
  7397. return 1;
  7398. #endif
  7399. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7400. TLS_client_method(),
  7401. TLS1_VERSION,
  7402. (tst <= 1) ? TLS1_2_VERSION
  7403. : TLS1_3_VERSION,
  7404. &sctx, &cctx, cert, privkey)))
  7405. goto end;
  7406. if (tst == 5)
  7407. SSL_CTX_set_post_handshake_auth(cctx, 1);
  7408. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7409. NULL, NULL)))
  7410. goto end;
  7411. if (tst == 3) {
  7412. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  7413. SSL_ERROR_NONE, 1, 0))
  7414. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7415. || !TEST_false(SSL_SESSION_is_resumable(sess)))
  7416. goto end;
  7417. } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  7418. SSL_ERROR_NONE))
  7419. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7420. || !TEST_true(SSL_SESSION_is_resumable(sess))) {
  7421. goto end;
  7422. }
  7423. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  7424. goto end;
  7425. if (tst >= 4) {
  7426. /*
  7427. * Reading on the server after the client has sent close_notify should
  7428. * fail and provide SSL_ERROR_ZERO_RETURN
  7429. */
  7430. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  7431. || !TEST_int_eq(SSL_get_error(serverssl, 0),
  7432. SSL_ERROR_ZERO_RETURN)
  7433. || !TEST_int_eq(SSL_get_shutdown(serverssl),
  7434. SSL_RECEIVED_SHUTDOWN)
  7435. /*
  7436. * Even though we're shutdown on receive we should still be
  7437. * able to write.
  7438. */
  7439. || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  7440. goto end;
  7441. if (tst == 4
  7442. && !TEST_true(SSL_key_update(serverssl,
  7443. SSL_KEY_UPDATE_REQUESTED)))
  7444. goto end;
  7445. if (tst == 5) {
  7446. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  7447. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  7448. goto end;
  7449. }
  7450. if ((tst == 4 || tst == 5)
  7451. && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  7452. goto end;
  7453. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  7454. goto end;
  7455. if (tst == 4 || tst == 5) {
  7456. /* Should still be able to read data from server */
  7457. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  7458. &readbytes))
  7459. || !TEST_size_t_eq(readbytes, sizeof(msg))
  7460. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
  7461. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  7462. &readbytes))
  7463. || !TEST_size_t_eq(readbytes, sizeof(msg))
  7464. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
  7465. goto end;
  7466. }
  7467. }
  7468. /* Writing on the client after sending close_notify shouldn't be possible */
  7469. if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
  7470. goto end;
  7471. if (tst < 4) {
  7472. /*
  7473. * For these tests the client has sent close_notify but it has not yet
  7474. * been received by the server. The server has not sent close_notify
  7475. * yet.
  7476. */
  7477. if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
  7478. /*
  7479. * Writing on the server after sending close_notify shouldn't
  7480. * be possible.
  7481. */
  7482. || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  7483. || !TEST_int_eq(SSL_shutdown(clientssl), 1)
  7484. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7485. || !TEST_true(SSL_SESSION_is_resumable(sess))
  7486. || !TEST_int_eq(SSL_shutdown(serverssl), 1))
  7487. goto end;
  7488. } else if (tst == 4 || tst == 5) {
  7489. /*
  7490. * In this test the client has sent close_notify and it has been
  7491. * received by the server which has responded with a close_notify. The
  7492. * client needs to read the close_notify sent by the server.
  7493. */
  7494. if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
  7495. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7496. || !TEST_true(SSL_SESSION_is_resumable(sess)))
  7497. goto end;
  7498. } else {
  7499. /*
  7500. * tst == 6
  7501. *
  7502. * The client has sent close_notify and is expecting a close_notify
  7503. * back, but instead there is application data first. The shutdown
  7504. * should fail with a fatal error.
  7505. */
  7506. if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
  7507. || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
  7508. goto end;
  7509. }
  7510. testresult = 1;
  7511. end:
  7512. SSL_free(serverssl);
  7513. SSL_free(clientssl);
  7514. SSL_CTX_free(sctx);
  7515. SSL_CTX_free(cctx);
  7516. return testresult;
  7517. }
  7518. /*
  7519. * Test that sending close_notify alerts works correctly in the case of a
  7520. * retryable write failure.
  7521. */
  7522. static int test_async_shutdown(void)
  7523. {
  7524. SSL_CTX *cctx = NULL, *sctx = NULL;
  7525. SSL *clientssl = NULL, *serverssl = NULL;
  7526. int testresult = 0;
  7527. BIO *bretry = BIO_new(bio_s_always_retry()), *tmp = NULL;
  7528. if (!TEST_ptr(bretry))
  7529. goto end;
  7530. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7531. TLS_client_method(),
  7532. 0, 0,
  7533. &sctx, &cctx, cert, privkey)))
  7534. goto end;
  7535. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  7536. NULL)))
  7537. goto end;
  7538. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7539. goto end;
  7540. /* Close write side of clientssl */
  7541. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  7542. goto end;
  7543. tmp = SSL_get_wbio(serverssl);
  7544. if (!TEST_true(BIO_up_ref(tmp))) {
  7545. tmp = NULL;
  7546. goto end;
  7547. }
  7548. SSL_set0_wbio(serverssl, bretry);
  7549. bretry = NULL;
  7550. /* First server shutdown should fail because of a retrable write failure */
  7551. if (!TEST_int_eq(SSL_shutdown(serverssl), -1)
  7552. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
  7553. goto end;
  7554. /* Second server shutdown should fail for the same reason */
  7555. if (!TEST_int_eq(SSL_shutdown(serverssl), -1)
  7556. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
  7557. goto end;
  7558. SSL_set0_wbio(serverssl, tmp);
  7559. tmp = NULL;
  7560. /* Third server shutdown should send close_notify */
  7561. if (!TEST_int_eq(SSL_shutdown(serverssl), 0))
  7562. goto end;
  7563. /* Fourth server shutdown should read close_notify from client and finish */
  7564. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  7565. goto end;
  7566. /* Client should also successfully fully shutdown */
  7567. if (!TEST_int_eq(SSL_shutdown(clientssl), 1))
  7568. goto end;
  7569. testresult = 1;
  7570. end:
  7571. SSL_free(serverssl);
  7572. SSL_free(clientssl);
  7573. SSL_CTX_free(sctx);
  7574. SSL_CTX_free(cctx);
  7575. BIO_free(bretry);
  7576. BIO_free(tmp);
  7577. return testresult;
  7578. }
  7579. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7580. static int cert_cb_cnt;
  7581. static int cert_cb(SSL *s, void *arg)
  7582. {
  7583. SSL_CTX *ctx = (SSL_CTX *)arg;
  7584. BIO *in = NULL;
  7585. EVP_PKEY *pkey = NULL;
  7586. X509 *x509 = NULL, *rootx = NULL;
  7587. STACK_OF(X509) *chain = NULL;
  7588. char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
  7589. int ret = 0;
  7590. if (cert_cb_cnt == 0) {
  7591. /* Suspend the handshake */
  7592. cert_cb_cnt++;
  7593. return -1;
  7594. } else if (cert_cb_cnt == 1) {
  7595. /*
  7596. * Update the SSL_CTX, set the certificate and private key and then
  7597. * continue the handshake normally.
  7598. */
  7599. if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
  7600. return 0;
  7601. if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
  7602. || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
  7603. SSL_FILETYPE_PEM))
  7604. || !TEST_true(SSL_check_private_key(s)))
  7605. return 0;
  7606. cert_cb_cnt++;
  7607. return 1;
  7608. } else if (cert_cb_cnt == 3) {
  7609. int rv;
  7610. rootfile = test_mk_file_path(certsdir, "rootcert.pem");
  7611. ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  7612. ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  7613. if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
  7614. goto out;
  7615. chain = sk_X509_new_null();
  7616. if (!TEST_ptr(chain))
  7617. goto out;
  7618. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7619. || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
  7620. || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
  7621. || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
  7622. || !TEST_true(sk_X509_push(chain, rootx)))
  7623. goto out;
  7624. rootx = NULL;
  7625. BIO_free(in);
  7626. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7627. || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
  7628. || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
  7629. || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
  7630. goto out;
  7631. BIO_free(in);
  7632. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7633. || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
  7634. || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  7635. NULL, NULL,
  7636. libctx, NULL)))
  7637. goto out;
  7638. rv = SSL_check_chain(s, x509, pkey, chain);
  7639. /*
  7640. * If the cert doesn't show as valid here (e.g., because we don't
  7641. * have any shared sigalgs), then we will not set it, and there will
  7642. * be no certificate at all on the SSL or SSL_CTX. This, in turn,
  7643. * will cause tls_choose_sigalgs() to fail the connection.
  7644. */
  7645. if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
  7646. == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
  7647. if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
  7648. goto out;
  7649. }
  7650. ret = 1;
  7651. }
  7652. /* Abort the handshake */
  7653. out:
  7654. OPENSSL_free(ecdsacert);
  7655. OPENSSL_free(ecdsakey);
  7656. OPENSSL_free(rootfile);
  7657. BIO_free(in);
  7658. EVP_PKEY_free(pkey);
  7659. X509_free(x509);
  7660. X509_free(rootx);
  7661. OSSL_STACK_OF_X509_free(chain);
  7662. return ret;
  7663. }
  7664. /*
  7665. * Test the certificate callback.
  7666. * Test 0: Callback fails
  7667. * Test 1: Success - no SSL_set_SSL_CTX() in the callback
  7668. * Test 2: Success - SSL_set_SSL_CTX() in the callback
  7669. * Test 3: Success - Call SSL_check_chain from the callback
  7670. * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
  7671. * chain
  7672. * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
  7673. */
  7674. static int test_cert_cb_int(int prot, int tst)
  7675. {
  7676. SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
  7677. SSL *clientssl = NULL, *serverssl = NULL;
  7678. int testresult = 0, ret;
  7679. #ifdef OPENSSL_NO_EC
  7680. /* We use an EC cert in these tests, so we skip in a no-ec build */
  7681. if (tst >= 3)
  7682. return 1;
  7683. #endif
  7684. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7685. TLS_client_method(),
  7686. TLS1_VERSION,
  7687. prot,
  7688. &sctx, &cctx, NULL, NULL)))
  7689. goto end;
  7690. if (tst == 0)
  7691. cert_cb_cnt = -1;
  7692. else if (tst >= 3)
  7693. cert_cb_cnt = 3;
  7694. else
  7695. cert_cb_cnt = 0;
  7696. if (tst == 2) {
  7697. snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  7698. if (!TEST_ptr(snictx))
  7699. goto end;
  7700. }
  7701. SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
  7702. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7703. NULL, NULL)))
  7704. goto end;
  7705. if (tst == 4) {
  7706. /*
  7707. * We cause SSL_check_chain() to fail by specifying sig_algs that
  7708. * the chain doesn't meet (the root uses an RSA cert)
  7709. */
  7710. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  7711. "ecdsa_secp256r1_sha256")))
  7712. goto end;
  7713. } else if (tst == 5) {
  7714. /*
  7715. * We cause SSL_check_chain() to fail by specifying sig_algs that
  7716. * the ee cert doesn't meet (the ee uses an ECDSA cert)
  7717. */
  7718. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  7719. "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
  7720. goto end;
  7721. }
  7722. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  7723. if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
  7724. || (tst > 0
  7725. && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
  7726. goto end;
  7727. }
  7728. testresult = 1;
  7729. end:
  7730. SSL_free(serverssl);
  7731. SSL_free(clientssl);
  7732. SSL_CTX_free(sctx);
  7733. SSL_CTX_free(cctx);
  7734. SSL_CTX_free(snictx);
  7735. return testresult;
  7736. }
  7737. #endif
  7738. static int test_cert_cb(int tst)
  7739. {
  7740. int testresult = 1;
  7741. #ifndef OPENSSL_NO_TLS1_2
  7742. testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
  7743. #endif
  7744. #ifndef OSSL_NO_USABLE_TLS1_3
  7745. testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
  7746. #endif
  7747. return testresult;
  7748. }
  7749. static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
  7750. {
  7751. X509 *xcert;
  7752. EVP_PKEY *privpkey;
  7753. BIO *in = NULL;
  7754. BIO *priv_in = NULL;
  7755. /* Check that SSL_get0_peer_certificate() returns something sensible */
  7756. if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
  7757. return 0;
  7758. in = BIO_new_file(cert, "r");
  7759. if (!TEST_ptr(in))
  7760. return 0;
  7761. if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
  7762. || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
  7763. || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
  7764. || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
  7765. NULL, NULL,
  7766. libctx, NULL)))
  7767. goto err;
  7768. *x509 = xcert;
  7769. *pkey = privpkey;
  7770. BIO_free(in);
  7771. BIO_free(priv_in);
  7772. return 1;
  7773. err:
  7774. X509_free(xcert);
  7775. BIO_free(in);
  7776. BIO_free(priv_in);
  7777. return 0;
  7778. }
  7779. static int test_client_cert_cb(int tst)
  7780. {
  7781. SSL_CTX *cctx = NULL, *sctx = NULL;
  7782. SSL *clientssl = NULL, *serverssl = NULL;
  7783. int testresult = 0;
  7784. #ifdef OPENSSL_NO_TLS1_2
  7785. if (tst == 0)
  7786. return 1;
  7787. #endif
  7788. #ifdef OSSL_NO_USABLE_TLS1_3
  7789. if (tst == 1)
  7790. return 1;
  7791. #endif
  7792. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7793. TLS_client_method(),
  7794. TLS1_VERSION,
  7795. tst == 0 ? TLS1_2_VERSION
  7796. : TLS1_3_VERSION,
  7797. &sctx, &cctx, cert, privkey)))
  7798. goto end;
  7799. /*
  7800. * Test that setting a client_cert_cb results in a client certificate being
  7801. * sent.
  7802. */
  7803. SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
  7804. SSL_CTX_set_verify(sctx,
  7805. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  7806. verify_cb);
  7807. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7808. NULL, NULL))
  7809. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7810. SSL_ERROR_NONE)))
  7811. goto end;
  7812. testresult = 1;
  7813. end:
  7814. SSL_free(serverssl);
  7815. SSL_free(clientssl);
  7816. SSL_CTX_free(sctx);
  7817. SSL_CTX_free(cctx);
  7818. return testresult;
  7819. }
  7820. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7821. /*
  7822. * Test setting certificate authorities on both client and server.
  7823. *
  7824. * Test 0: SSL_CTX_set0_CA_list() only
  7825. * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
  7826. * Test 2: Only SSL_CTX_set_client_CA_list()
  7827. */
  7828. static int test_ca_names_int(int prot, int tst)
  7829. {
  7830. SSL_CTX *cctx = NULL, *sctx = NULL;
  7831. SSL *clientssl = NULL, *serverssl = NULL;
  7832. int testresult = 0;
  7833. size_t i;
  7834. X509_NAME *name[] = { NULL, NULL, NULL, NULL };
  7835. char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
  7836. STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
  7837. const STACK_OF(X509_NAME) *sktmp = NULL;
  7838. for (i = 0; i < OSSL_NELEM(name); i++) {
  7839. name[i] = X509_NAME_new();
  7840. if (!TEST_ptr(name[i])
  7841. || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
  7842. MBSTRING_ASC,
  7843. (unsigned char *)
  7844. strnames[i],
  7845. -1, -1, 0)))
  7846. goto end;
  7847. }
  7848. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7849. TLS_client_method(),
  7850. TLS1_VERSION,
  7851. prot,
  7852. &sctx, &cctx, cert, privkey)))
  7853. goto end;
  7854. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  7855. if (tst == 0 || tst == 1) {
  7856. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  7857. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
  7858. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
  7859. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  7860. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
  7861. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
  7862. goto end;
  7863. SSL_CTX_set0_CA_list(sctx, sk1);
  7864. SSL_CTX_set0_CA_list(cctx, sk2);
  7865. sk1 = sk2 = NULL;
  7866. }
  7867. if (tst == 1 || tst == 2) {
  7868. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  7869. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
  7870. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
  7871. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  7872. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
  7873. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
  7874. goto end;
  7875. SSL_CTX_set_client_CA_list(sctx, sk1);
  7876. SSL_CTX_set_client_CA_list(cctx, sk2);
  7877. sk1 = sk2 = NULL;
  7878. }
  7879. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7880. NULL, NULL))
  7881. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7882. SSL_ERROR_NONE)))
  7883. goto end;
  7884. /*
  7885. * We only expect certificate authorities to have been sent to the server
  7886. * if we are using TLSv1.3 and SSL_set0_CA_list() was used
  7887. */
  7888. sktmp = SSL_get0_peer_CA_list(serverssl);
  7889. if (prot == TLS1_3_VERSION
  7890. && (tst == 0 || tst == 1)) {
  7891. if (!TEST_ptr(sktmp)
  7892. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  7893. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  7894. name[0]), 0)
  7895. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  7896. name[1]), 0))
  7897. goto end;
  7898. } else if (!TEST_ptr_null(sktmp)) {
  7899. goto end;
  7900. }
  7901. /*
  7902. * In all tests we expect certificate authorities to have been sent to the
  7903. * client. However, SSL_set_client_CA_list() should override
  7904. * SSL_set0_CA_list()
  7905. */
  7906. sktmp = SSL_get0_peer_CA_list(clientssl);
  7907. if (!TEST_ptr(sktmp)
  7908. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  7909. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  7910. name[tst == 0 ? 0 : 2]), 0)
  7911. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  7912. name[tst == 0 ? 1 : 3]), 0))
  7913. goto end;
  7914. testresult = 1;
  7915. end:
  7916. SSL_free(serverssl);
  7917. SSL_free(clientssl);
  7918. SSL_CTX_free(sctx);
  7919. SSL_CTX_free(cctx);
  7920. for (i = 0; i < OSSL_NELEM(name); i++)
  7921. X509_NAME_free(name[i]);
  7922. sk_X509_NAME_pop_free(sk1, X509_NAME_free);
  7923. sk_X509_NAME_pop_free(sk2, X509_NAME_free);
  7924. return testresult;
  7925. }
  7926. #endif
  7927. static int test_ca_names(int tst)
  7928. {
  7929. int testresult = 1;
  7930. #ifndef OPENSSL_NO_TLS1_2
  7931. testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
  7932. #endif
  7933. #ifndef OSSL_NO_USABLE_TLS1_3
  7934. testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
  7935. #endif
  7936. return testresult;
  7937. }
  7938. #ifndef OPENSSL_NO_TLS1_2
  7939. static const char *multiblock_cipherlist_data[]=
  7940. {
  7941. "AES128-SHA",
  7942. "AES128-SHA256",
  7943. "AES256-SHA",
  7944. "AES256-SHA256",
  7945. };
  7946. /* Reduce the fragment size - so the multiblock test buffer can be small */
  7947. # define MULTIBLOCK_FRAGSIZE 512
  7948. static int test_multiblock_write(int test_index)
  7949. {
  7950. static const char *fetchable_ciphers[]=
  7951. {
  7952. "AES-128-CBC-HMAC-SHA1",
  7953. "AES-128-CBC-HMAC-SHA256",
  7954. "AES-256-CBC-HMAC-SHA1",
  7955. "AES-256-CBC-HMAC-SHA256"
  7956. };
  7957. const char *cipherlist = multiblock_cipherlist_data[test_index];
  7958. const SSL_METHOD *smeth = TLS_server_method();
  7959. const SSL_METHOD *cmeth = TLS_client_method();
  7960. int min_version = TLS1_VERSION;
  7961. int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
  7962. SSL_CTX *cctx = NULL, *sctx = NULL;
  7963. SSL *clientssl = NULL, *serverssl = NULL;
  7964. int testresult = 0;
  7965. /*
  7966. * Choose a buffer large enough to perform a multi-block operation
  7967. * i.e: write_len >= 4 * frag_size
  7968. * 9 * is chosen so that multiple multiblocks are used + some leftover.
  7969. */
  7970. unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
  7971. unsigned char buf[sizeof(msg)], *p = buf;
  7972. size_t readbytes, written, len;
  7973. EVP_CIPHER *ciph = NULL;
  7974. /*
  7975. * Check if the cipher exists before attempting to use it since it only has
  7976. * a hardware specific implementation.
  7977. */
  7978. ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
  7979. if (ciph == NULL) {
  7980. TEST_skip("Multiblock cipher is not available for %s", cipherlist);
  7981. return 1;
  7982. }
  7983. EVP_CIPHER_free(ciph);
  7984. /* Set up a buffer with some data that will be sent to the client */
  7985. RAND_bytes(msg, sizeof(msg));
  7986. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  7987. max_version, &sctx, &cctx, cert,
  7988. privkey)))
  7989. goto end;
  7990. if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
  7991. goto end;
  7992. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7993. NULL, NULL)))
  7994. goto end;
  7995. /* settings to force it to use AES-CBC-HMAC_SHA */
  7996. SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
  7997. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
  7998. goto end;
  7999. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8000. goto end;
  8001. if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  8002. || !TEST_size_t_eq(written, sizeof(msg)))
  8003. goto end;
  8004. len = written;
  8005. while (len > 0) {
  8006. if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
  8007. goto end;
  8008. p += readbytes;
  8009. len -= readbytes;
  8010. }
  8011. if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
  8012. goto end;
  8013. testresult = 1;
  8014. end:
  8015. SSL_free(serverssl);
  8016. SSL_free(clientssl);
  8017. SSL_CTX_free(sctx);
  8018. SSL_CTX_free(cctx);
  8019. return testresult;
  8020. }
  8021. #endif /* OPENSSL_NO_TLS1_2 */
  8022. static int test_session_timeout(int test)
  8023. {
  8024. /*
  8025. * Test session ordering and timeout
  8026. * Can't explicitly test performance of the new code,
  8027. * but can test to see if the ordering of the sessions
  8028. * are correct, and they are removed as expected
  8029. */
  8030. SSL_SESSION *early = NULL;
  8031. SSL_SESSION *middle = NULL;
  8032. SSL_SESSION *late = NULL;
  8033. SSL_CTX *ctx;
  8034. int testresult = 0;
  8035. long now = (long)time(NULL);
  8036. #define TIMEOUT 10
  8037. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  8038. || !TEST_ptr(early = SSL_SESSION_new())
  8039. || !TEST_ptr(middle = SSL_SESSION_new())
  8040. || !TEST_ptr(late = SSL_SESSION_new()))
  8041. goto end;
  8042. /* assign unique session ids */
  8043. early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  8044. memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
  8045. middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  8046. memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
  8047. late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  8048. memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
  8049. if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  8050. || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
  8051. || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
  8052. goto end;
  8053. /* Make sure they are all added */
  8054. if (!TEST_ptr(early->prev)
  8055. || !TEST_ptr(middle->prev)
  8056. || !TEST_ptr(late->prev))
  8057. goto end;
  8058. if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
  8059. || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
  8060. || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
  8061. goto end;
  8062. if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
  8063. || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
  8064. || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
  8065. goto end;
  8066. /* Make sure they are all still there */
  8067. if (!TEST_ptr(early->prev)
  8068. || !TEST_ptr(middle->prev)
  8069. || !TEST_ptr(late->prev))
  8070. goto end;
  8071. /* Make sure they are in the expected order */
  8072. if (!TEST_ptr_eq(late->next, middle)
  8073. || !TEST_ptr_eq(middle->next, early)
  8074. || !TEST_ptr_eq(early->prev, middle)
  8075. || !TEST_ptr_eq(middle->prev, late))
  8076. goto end;
  8077. /* This should remove "early" */
  8078. SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
  8079. if (!TEST_ptr_null(early->prev)
  8080. || !TEST_ptr(middle->prev)
  8081. || !TEST_ptr(late->prev))
  8082. goto end;
  8083. /* This should remove "middle" */
  8084. SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
  8085. if (!TEST_ptr_null(early->prev)
  8086. || !TEST_ptr_null(middle->prev)
  8087. || !TEST_ptr(late->prev))
  8088. goto end;
  8089. /* This should remove "late" */
  8090. SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
  8091. if (!TEST_ptr_null(early->prev)
  8092. || !TEST_ptr_null(middle->prev)
  8093. || !TEST_ptr_null(late->prev))
  8094. goto end;
  8095. /* Add them back in again */
  8096. if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  8097. || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
  8098. || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
  8099. goto end;
  8100. /* Make sure they are all added */
  8101. if (!TEST_ptr(early->prev)
  8102. || !TEST_ptr(middle->prev)
  8103. || !TEST_ptr(late->prev))
  8104. goto end;
  8105. /* This should remove all of them */
  8106. SSL_CTX_flush_sessions(ctx, 0);
  8107. if (!TEST_ptr_null(early->prev)
  8108. || !TEST_ptr_null(middle->prev)
  8109. || !TEST_ptr_null(late->prev))
  8110. goto end;
  8111. (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
  8112. | SSL_CTX_get_session_cache_mode(ctx));
  8113. /* make sure |now| is NOT equal to the current time */
  8114. now -= 10;
  8115. if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
  8116. || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  8117. || !TEST_long_ne(SSL_SESSION_get_time(early), now))
  8118. goto end;
  8119. testresult = 1;
  8120. end:
  8121. SSL_CTX_free(ctx);
  8122. SSL_SESSION_free(early);
  8123. SSL_SESSION_free(middle);
  8124. SSL_SESSION_free(late);
  8125. return testresult;
  8126. }
  8127. /*
  8128. * Test that a session cache overflow works as expected
  8129. * Test 0: TLSv1.3, timeout on new session later than old session
  8130. * Test 1: TLSv1.2, timeout on new session later than old session
  8131. * Test 2: TLSv1.3, timeout on new session earlier than old session
  8132. * Test 3: TLSv1.2, timeout on new session earlier than old session
  8133. */
  8134. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  8135. static int test_session_cache_overflow(int idx)
  8136. {
  8137. SSL_CTX *sctx = NULL, *cctx = NULL;
  8138. SSL *serverssl = NULL, *clientssl = NULL;
  8139. int testresult = 0;
  8140. SSL_SESSION *sess = NULL;
  8141. #ifdef OSSL_NO_USABLE_TLS1_3
  8142. /* If no TLSv1.3 available then do nothing in this case */
  8143. if (idx % 2 == 0)
  8144. return TEST_skip("No TLSv1.3 available");
  8145. #endif
  8146. #ifdef OPENSSL_NO_TLS1_2
  8147. /* If no TLSv1.2 available then do nothing in this case */
  8148. if (idx % 2 == 1)
  8149. return TEST_skip("No TLSv1.2 available");
  8150. #endif
  8151. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8152. TLS_client_method(), TLS1_VERSION,
  8153. (idx % 2 == 0) ? TLS1_3_VERSION
  8154. : TLS1_2_VERSION,
  8155. &sctx, &cctx, cert, privkey))
  8156. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)))
  8157. goto end;
  8158. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  8159. get_sess_val = NULL;
  8160. SSL_CTX_sess_set_cache_size(sctx, 1);
  8161. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8162. NULL, NULL)))
  8163. goto end;
  8164. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8165. goto end;
  8166. if (idx > 1) {
  8167. sess = SSL_get_session(serverssl);
  8168. if (!TEST_ptr(sess))
  8169. goto end;
  8170. /*
  8171. * Cause this session to have a longer timeout than the next session to
  8172. * be added.
  8173. */
  8174. if (!TEST_true(SSL_SESSION_set_timeout(sess, LONG_MAX))) {
  8175. sess = NULL;
  8176. goto end;
  8177. }
  8178. sess = NULL;
  8179. }
  8180. SSL_shutdown(serverssl);
  8181. SSL_shutdown(clientssl);
  8182. SSL_free(serverssl);
  8183. SSL_free(clientssl);
  8184. serverssl = clientssl = NULL;
  8185. /*
  8186. * Session cache size is 1 and we already populated the cache with a session
  8187. * so the next connection should cause an overflow.
  8188. */
  8189. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8190. NULL, NULL)))
  8191. goto end;
  8192. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8193. goto end;
  8194. /*
  8195. * The session we just negotiated may have been already removed from the
  8196. * internal cache - but we will return it anyway from our external cache.
  8197. */
  8198. get_sess_val = SSL_get_session(serverssl);
  8199. if (!TEST_ptr(get_sess_val))
  8200. goto end;
  8201. sess = SSL_get1_session(clientssl);
  8202. if (!TEST_ptr(sess))
  8203. goto end;
  8204. SSL_shutdown(serverssl);
  8205. SSL_shutdown(clientssl);
  8206. SSL_free(serverssl);
  8207. SSL_free(clientssl);
  8208. serverssl = clientssl = NULL;
  8209. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8210. NULL, NULL)))
  8211. goto end;
  8212. if (!TEST_true(SSL_set_session(clientssl, sess)))
  8213. goto end;
  8214. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8215. goto end;
  8216. testresult = 1;
  8217. end:
  8218. SSL_free(serverssl);
  8219. SSL_free(clientssl);
  8220. SSL_CTX_free(sctx);
  8221. SSL_CTX_free(cctx);
  8222. SSL_SESSION_free(sess);
  8223. return testresult;
  8224. }
  8225. #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  8226. /*
  8227. * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
  8228. * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
  8229. * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
  8230. * Test 3: Client does not set servername on initial handshake (TLSv1.2)
  8231. * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
  8232. * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
  8233. * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
  8234. * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
  8235. * Test 8: Client does not set servername on initial handshake(TLSv1.3)
  8236. * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
  8237. */
  8238. static int test_servername(int tst)
  8239. {
  8240. SSL_CTX *cctx = NULL, *sctx = NULL;
  8241. SSL *clientssl = NULL, *serverssl = NULL;
  8242. int testresult = 0;
  8243. SSL_SESSION *sess = NULL;
  8244. const char *sexpectedhost = NULL, *cexpectedhost = NULL;
  8245. #ifdef OPENSSL_NO_TLS1_2
  8246. if (tst <= 4)
  8247. return 1;
  8248. #endif
  8249. #ifdef OSSL_NO_USABLE_TLS1_3
  8250. if (tst >= 5)
  8251. return 1;
  8252. #endif
  8253. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8254. TLS_client_method(),
  8255. TLS1_VERSION,
  8256. (tst <= 4) ? TLS1_2_VERSION
  8257. : TLS1_3_VERSION,
  8258. &sctx, &cctx, cert, privkey))
  8259. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8260. NULL, NULL)))
  8261. goto end;
  8262. if (tst != 1 && tst != 6) {
  8263. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  8264. hostname_cb)))
  8265. goto end;
  8266. }
  8267. if (tst != 3 && tst != 8) {
  8268. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  8269. goto end;
  8270. sexpectedhost = cexpectedhost = "goodhost";
  8271. }
  8272. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8273. goto end;
  8274. if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
  8275. cexpectedhost)
  8276. || !TEST_str_eq(SSL_get_servername(serverssl,
  8277. TLSEXT_NAMETYPE_host_name),
  8278. sexpectedhost))
  8279. goto end;
  8280. /* Now repeat with a resumption handshake */
  8281. if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
  8282. || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
  8283. || !TEST_true(SSL_SESSION_is_resumable(sess))
  8284. || !TEST_int_eq(SSL_shutdown(serverssl), 0))
  8285. goto end;
  8286. SSL_free(clientssl);
  8287. SSL_free(serverssl);
  8288. clientssl = serverssl = NULL;
  8289. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  8290. NULL)))
  8291. goto end;
  8292. if (!TEST_true(SSL_set_session(clientssl, sess)))
  8293. goto end;
  8294. sexpectedhost = cexpectedhost = "goodhost";
  8295. if (tst == 2 || tst == 7) {
  8296. /* Set an inconsistent hostname */
  8297. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
  8298. goto end;
  8299. /*
  8300. * In TLSv1.2 we expect the hostname from the original handshake, in
  8301. * TLSv1.3 we expect the hostname from this handshake
  8302. */
  8303. if (tst == 7)
  8304. sexpectedhost = cexpectedhost = "altgoodhost";
  8305. if (!TEST_str_eq(SSL_get_servername(clientssl,
  8306. TLSEXT_NAMETYPE_host_name),
  8307. "altgoodhost"))
  8308. goto end;
  8309. } else if (tst == 4 || tst == 9) {
  8310. /*
  8311. * A TLSv1.3 session does not associate a session with a servername,
  8312. * but a TLSv1.2 session does.
  8313. */
  8314. if (tst == 9)
  8315. sexpectedhost = cexpectedhost = NULL;
  8316. if (!TEST_str_eq(SSL_get_servername(clientssl,
  8317. TLSEXT_NAMETYPE_host_name),
  8318. cexpectedhost))
  8319. goto end;
  8320. } else {
  8321. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  8322. goto end;
  8323. /*
  8324. * In a TLSv1.2 resumption where the hostname was not acknowledged
  8325. * we expect the hostname on the server to be empty. On the client we
  8326. * return what was requested in this case.
  8327. *
  8328. * Similarly if the client didn't set a hostname on an original TLSv1.2
  8329. * session but is now, the server hostname will be empty, but the client
  8330. * is as we set it.
  8331. */
  8332. if (tst == 1 || tst == 3)
  8333. sexpectedhost = NULL;
  8334. if (!TEST_str_eq(SSL_get_servername(clientssl,
  8335. TLSEXT_NAMETYPE_host_name),
  8336. "goodhost"))
  8337. goto end;
  8338. }
  8339. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8340. goto end;
  8341. if (!TEST_true(SSL_session_reused(clientssl))
  8342. || !TEST_true(SSL_session_reused(serverssl))
  8343. || !TEST_str_eq(SSL_get_servername(clientssl,
  8344. TLSEXT_NAMETYPE_host_name),
  8345. cexpectedhost)
  8346. || !TEST_str_eq(SSL_get_servername(serverssl,
  8347. TLSEXT_NAMETYPE_host_name),
  8348. sexpectedhost))
  8349. goto end;
  8350. testresult = 1;
  8351. end:
  8352. SSL_SESSION_free(sess);
  8353. SSL_free(serverssl);
  8354. SSL_free(clientssl);
  8355. SSL_CTX_free(sctx);
  8356. SSL_CTX_free(cctx);
  8357. return testresult;
  8358. }
  8359. static int test_unknown_sigalgs_groups(void)
  8360. {
  8361. int ret = 0;
  8362. SSL_CTX *ctx = NULL;
  8363. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  8364. goto end;
  8365. if (!TEST_int_gt(SSL_CTX_set1_sigalgs_list(ctx,
  8366. "RSA+SHA256:?nonexistent:?RSA+SHA512"),
  8367. 0))
  8368. goto end;
  8369. if (!TEST_size_t_eq(ctx->cert->conf_sigalgslen, 2)
  8370. || !TEST_int_eq(ctx->cert->conf_sigalgs[0], TLSEXT_SIGALG_rsa_pkcs1_sha256)
  8371. || !TEST_int_eq(ctx->cert->conf_sigalgs[1], TLSEXT_SIGALG_rsa_pkcs1_sha512))
  8372. goto end;
  8373. if (!TEST_int_gt(SSL_CTX_set1_client_sigalgs_list(ctx,
  8374. "RSA+SHA256:?nonexistent:?RSA+SHA512"),
  8375. 0))
  8376. goto end;
  8377. if (!TEST_size_t_eq(ctx->cert->client_sigalgslen, 2)
  8378. || !TEST_int_eq(ctx->cert->client_sigalgs[0], TLSEXT_SIGALG_rsa_pkcs1_sha256)
  8379. || !TEST_int_eq(ctx->cert->client_sigalgs[1], TLSEXT_SIGALG_rsa_pkcs1_sha512))
  8380. goto end;
  8381. if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx,
  8382. "nonexistent"),
  8383. 0))
  8384. goto end;
  8385. if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx,
  8386. "?nonexistent1:?nonexistent2:?nonexistent3"),
  8387. 0))
  8388. goto end;
  8389. #ifndef OPENSSL_NO_EC
  8390. if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx,
  8391. "P-256:nonexistent"),
  8392. 0))
  8393. goto end;
  8394. if (!TEST_int_gt(SSL_CTX_set1_groups_list(ctx,
  8395. "P-384:?nonexistent:?P-521"),
  8396. 0))
  8397. goto end;
  8398. if (!TEST_size_t_eq(ctx->ext.supportedgroups_len, 2)
  8399. || !TEST_int_eq(ctx->ext.supportedgroups[0], OSSL_TLS_GROUP_ID_secp384r1)
  8400. || !TEST_int_eq(ctx->ext.supportedgroups[1], OSSL_TLS_GROUP_ID_secp521r1))
  8401. goto end;
  8402. #endif
  8403. ret = 1;
  8404. end:
  8405. SSL_CTX_free(ctx);
  8406. return ret;
  8407. }
  8408. #if !defined(OPENSSL_NO_EC) \
  8409. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  8410. /*
  8411. * Test that if signature algorithms are not available, then we do not offer or
  8412. * accept them.
  8413. * Test 0: Two RSA sig algs available: both RSA sig algs shared
  8414. * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
  8415. * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
  8416. * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
  8417. * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
  8418. * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
  8419. */
  8420. static int test_sigalgs_available(int idx)
  8421. {
  8422. SSL_CTX *cctx = NULL, *sctx = NULL;
  8423. SSL *clientssl = NULL, *serverssl = NULL;
  8424. int testresult = 0;
  8425. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  8426. OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
  8427. OSSL_PROVIDER *filterprov = NULL;
  8428. int sig, hash;
  8429. if (!TEST_ptr(tmpctx))
  8430. goto end;
  8431. if (idx != 0 && idx != 3) {
  8432. if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
  8433. filter_provider_init)))
  8434. goto end;
  8435. filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
  8436. if (!TEST_ptr(filterprov))
  8437. goto end;
  8438. if (idx < 3) {
  8439. /*
  8440. * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
  8441. * or accepted for the peer that uses this libctx. Note that libssl
  8442. * *requires* SHA2-256 to be available so we cannot disable that. We
  8443. * also need SHA1 for our certificate.
  8444. */
  8445. if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
  8446. "SHA2-256:SHA1")))
  8447. goto end;
  8448. } else {
  8449. if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
  8450. "ECDSA"))
  8451. # ifdef OPENSSL_NO_ECX
  8452. || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT, "EC"))
  8453. # else
  8454. || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
  8455. "EC:X25519:X448"))
  8456. # endif
  8457. )
  8458. goto end;
  8459. }
  8460. if (idx == 1 || idx == 4)
  8461. clientctx = tmpctx;
  8462. else
  8463. serverctx = tmpctx;
  8464. }
  8465. cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
  8466. sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
  8467. if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
  8468. goto end;
  8469. if (idx != 5) {
  8470. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8471. TLS_client_method(),
  8472. TLS1_VERSION,
  8473. 0,
  8474. &sctx, &cctx, cert, privkey)))
  8475. goto end;
  8476. } else {
  8477. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8478. TLS_client_method(),
  8479. TLS1_VERSION,
  8480. 0,
  8481. &sctx, &cctx, cert2, privkey2)))
  8482. goto end;
  8483. }
  8484. /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
  8485. if (idx < 4) {
  8486. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  8487. "ECDHE-RSA-AES128-GCM-SHA256")))
  8488. goto end;
  8489. } else {
  8490. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  8491. "ECDHE-ECDSA-AES128-GCM-SHA256")))
  8492. goto end;
  8493. }
  8494. if (idx < 3) {
  8495. if (!SSL_CTX_set1_sigalgs_list(cctx,
  8496. "rsa_pss_rsae_sha384"
  8497. ":rsa_pss_rsae_sha256")
  8498. || !SSL_CTX_set1_sigalgs_list(sctx,
  8499. "rsa_pss_rsae_sha384"
  8500. ":rsa_pss_rsae_sha256"))
  8501. goto end;
  8502. } else {
  8503. if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
  8504. || !SSL_CTX_set1_sigalgs_list(sctx,
  8505. "rsa_pss_rsae_sha256:ECDSA+SHA256"))
  8506. goto end;
  8507. }
  8508. if (idx != 5
  8509. && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
  8510. SSL_FILETYPE_PEM), 1)
  8511. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
  8512. privkey2,
  8513. SSL_FILETYPE_PEM), 1)
  8514. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
  8515. goto end;
  8516. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8517. NULL, NULL)))
  8518. goto end;
  8519. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8520. goto end;
  8521. /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
  8522. if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
  8523. NULL, NULL),
  8524. (idx == 0 || idx == 3) ? 2 : 1))
  8525. goto end;
  8526. if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
  8527. goto end;
  8528. if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
  8529. : NID_rsassaPss))
  8530. goto end;
  8531. testresult = filter_provider_check_clean_finish();
  8532. end:
  8533. SSL_free(serverssl);
  8534. SSL_free(clientssl);
  8535. SSL_CTX_free(sctx);
  8536. SSL_CTX_free(cctx);
  8537. OSSL_PROVIDER_unload(filterprov);
  8538. OSSL_LIB_CTX_free(tmpctx);
  8539. return testresult;
  8540. }
  8541. #endif /*
  8542. * !defined(OPENSSL_NO_EC) \
  8543. * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  8544. */
  8545. #ifndef OPENSSL_NO_TLS1_3
  8546. /* This test can run in TLSv1.3 even if ec and dh are disabled */
  8547. static int test_pluggable_group(int idx)
  8548. {
  8549. SSL_CTX *cctx = NULL, *sctx = NULL;
  8550. SSL *clientssl = NULL, *serverssl = NULL;
  8551. int testresult = 0;
  8552. OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
  8553. /* Check that we are not impacted by a provider without any groups */
  8554. OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
  8555. const char *group_name = idx == 0 ? "xorkemgroup" : "xorgroup";
  8556. if (!TEST_ptr(tlsprov))
  8557. goto end;
  8558. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8559. TLS_client_method(),
  8560. TLS1_3_VERSION,
  8561. TLS1_3_VERSION,
  8562. &sctx, &cctx, cert, privkey))
  8563. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8564. NULL, NULL)))
  8565. goto end;
  8566. /* ensure GROUPLIST_INCREMENT (=40) logic triggers: */
  8567. if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup:xorkemgroup:dummy1:dummy2:dummy3:dummy4:dummy5:dummy6:dummy7:dummy8:dummy9:dummy10:dummy11:dummy12:dummy13:dummy14:dummy15:dummy16:dummy17:dummy18:dummy19:dummy20:dummy21:dummy22:dummy23:dummy24:dummy25:dummy26:dummy27:dummy28:dummy29:dummy30:dummy31:dummy32:dummy33:dummy34:dummy35:dummy36:dummy37:dummy38:dummy39:dummy40:dummy41:dummy42:dummy43"))
  8568. /* removing a single algorithm from the list makes the test pass */
  8569. || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
  8570. goto end;
  8571. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8572. goto end;
  8573. if (!TEST_str_eq(group_name,
  8574. SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
  8575. goto end;
  8576. if (!TEST_str_eq(group_name, SSL_get0_group_name(serverssl))
  8577. || !TEST_str_eq(group_name, SSL_get0_group_name(clientssl)))
  8578. goto end;
  8579. testresult = 1;
  8580. end:
  8581. SSL_free(serverssl);
  8582. SSL_free(clientssl);
  8583. SSL_CTX_free(sctx);
  8584. SSL_CTX_free(cctx);
  8585. OSSL_PROVIDER_unload(tlsprov);
  8586. OSSL_PROVIDER_unload(legacyprov);
  8587. return testresult;
  8588. }
  8589. /*
  8590. * This function triggers encode, decode and sign functions
  8591. * of the artificial "xorhmacsig" algorithm implemented in tls-provider
  8592. * creating private key and certificate files for use in TLS testing.
  8593. */
  8594. static int create_cert_key(int idx, char *certfilename, char *privkeyfilename)
  8595. {
  8596. EVP_PKEY_CTX *evpctx = EVP_PKEY_CTX_new_from_name(libctx,
  8597. (idx == 0) ? "xorhmacsig" : "xorhmacsha2sig", NULL);
  8598. EVP_PKEY *pkey = NULL;
  8599. X509 *x509 = X509_new();
  8600. X509_NAME *name = NULL;
  8601. BIO *keybio = NULL, *certbio = NULL;
  8602. int ret = 1;
  8603. if (!TEST_ptr(evpctx)
  8604. || !TEST_true(EVP_PKEY_keygen_init(evpctx))
  8605. || !TEST_true(EVP_PKEY_generate(evpctx, &pkey))
  8606. || !TEST_ptr(pkey)
  8607. || !TEST_ptr(x509)
  8608. || !TEST_true(ASN1_INTEGER_set(X509_get_serialNumber(x509), 1))
  8609. || !TEST_true(X509_gmtime_adj(X509_getm_notBefore(x509), 0))
  8610. || !TEST_true(X509_gmtime_adj(X509_getm_notAfter(x509), 31536000L))
  8611. || !TEST_true(X509_set_pubkey(x509, pkey))
  8612. || !TEST_ptr(name = X509_get_subject_name(x509))
  8613. || !TEST_true(X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC,
  8614. (unsigned char *)"CH", -1, -1, 0))
  8615. || !TEST_true(X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
  8616. (unsigned char *)"test.org", -1, -1, 0))
  8617. || !TEST_true(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
  8618. (unsigned char *)"localhost", -1, -1, 0))
  8619. || !TEST_true(X509_set_issuer_name(x509, name))
  8620. || !TEST_true(X509_sign(x509, pkey, EVP_sha1()))
  8621. || !TEST_ptr(keybio = BIO_new_file(privkeyfilename, "wb"))
  8622. || !TEST_true(PEM_write_bio_PrivateKey(keybio, pkey, NULL, NULL, 0, NULL, NULL))
  8623. || !TEST_ptr(certbio = BIO_new_file(certfilename, "wb"))
  8624. || !TEST_true(PEM_write_bio_X509(certbio, x509)))
  8625. ret = 0;
  8626. EVP_PKEY_free(pkey);
  8627. X509_free(x509);
  8628. EVP_PKEY_CTX_free(evpctx);
  8629. BIO_free(keybio);
  8630. BIO_free(certbio);
  8631. return ret;
  8632. }
  8633. /*
  8634. * Test that signature algorithms loaded via the provider interface can
  8635. * correctly establish a TLS (1.3) connection.
  8636. * Test 0: Signature algorithm with built-in hashing functionality: "xorhmacsig"
  8637. * Test 1: Signature algorithm using external SHA2 hashing: "xorhmacsha2sig"
  8638. * Test 2: Test 0 using RPK
  8639. * Test 3: Test 1 using RPK
  8640. */
  8641. static int test_pluggable_signature(int idx)
  8642. {
  8643. static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 };
  8644. SSL_CTX *cctx = NULL, *sctx = NULL;
  8645. SSL *clientssl = NULL, *serverssl = NULL;
  8646. int testresult = 0;
  8647. OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
  8648. OSSL_PROVIDER *defaultprov = OSSL_PROVIDER_load(libctx, "default");
  8649. char *certfilename = "tls-prov-cert.pem";
  8650. char *privkeyfilename = "tls-prov-key.pem";
  8651. int sigidx = idx % 2;
  8652. int rpkidx = idx / 2;
  8653. /* create key and certificate for the different algorithm types */
  8654. if (!TEST_ptr(tlsprov)
  8655. || !TEST_true(create_cert_key(sigidx, certfilename, privkeyfilename)))
  8656. goto end;
  8657. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8658. TLS_client_method(),
  8659. TLS1_3_VERSION,
  8660. TLS1_3_VERSION,
  8661. &sctx, &cctx, certfilename, privkeyfilename))
  8662. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8663. NULL, NULL)))
  8664. goto end;
  8665. /* Enable RPK for server cert */
  8666. if (rpkidx) {
  8667. if (!TEST_true(SSL_set1_server_cert_type(serverssl, cert_type_rpk, sizeof(cert_type_rpk)))
  8668. || !TEST_true(SSL_set1_server_cert_type(clientssl, cert_type_rpk, sizeof(cert_type_rpk))))
  8669. goto end;
  8670. }
  8671. /* This is necessary to pass minimal setup w/o other groups configured */
  8672. if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup"))
  8673. || !TEST_true(SSL_set1_groups_list(clientssl, "xorgroup")))
  8674. goto end;
  8675. /*
  8676. * If this connection gets established, it must have been completed
  8677. * via the tls-provider-implemented "hmacsig" algorithm, testing
  8678. * both sign and verify functions during handshake.
  8679. */
  8680. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8681. goto end;
  8682. /* If using RPK, make sure we got one */
  8683. if (rpkidx && !TEST_long_eq(SSL_get_verify_result(clientssl), X509_V_ERR_RPK_UNTRUSTED))
  8684. goto end;
  8685. testresult = 1;
  8686. end:
  8687. SSL_free(serverssl);
  8688. SSL_free(clientssl);
  8689. SSL_CTX_free(sctx);
  8690. SSL_CTX_free(cctx);
  8691. OSSL_PROVIDER_unload(tlsprov);
  8692. OSSL_PROVIDER_unload(defaultprov);
  8693. return testresult;
  8694. }
  8695. #endif
  8696. #ifndef OPENSSL_NO_TLS1_2
  8697. static int test_ssl_dup(void)
  8698. {
  8699. SSL_CTX *cctx = NULL, *sctx = NULL;
  8700. SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
  8701. int testresult = 0;
  8702. BIO *rbio = NULL, *wbio = NULL;
  8703. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8704. TLS_client_method(),
  8705. 0,
  8706. 0,
  8707. &sctx, &cctx, cert, privkey)))
  8708. goto end;
  8709. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8710. NULL, NULL)))
  8711. goto end;
  8712. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  8713. || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
  8714. goto end;
  8715. client2ssl = SSL_dup(clientssl);
  8716. rbio = SSL_get_rbio(clientssl);
  8717. if (!TEST_ptr(rbio)
  8718. || !TEST_true(BIO_up_ref(rbio)))
  8719. goto end;
  8720. SSL_set0_rbio(client2ssl, rbio);
  8721. rbio = NULL;
  8722. wbio = SSL_get_wbio(clientssl);
  8723. if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
  8724. goto end;
  8725. SSL_set0_wbio(client2ssl, wbio);
  8726. rbio = NULL;
  8727. if (!TEST_ptr(client2ssl)
  8728. /* Handshake not started so pointers should be different */
  8729. || !TEST_ptr_ne(clientssl, client2ssl))
  8730. goto end;
  8731. if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
  8732. || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
  8733. goto end;
  8734. if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
  8735. goto end;
  8736. SSL_free(clientssl);
  8737. clientssl = SSL_dup(client2ssl);
  8738. if (!TEST_ptr(clientssl)
  8739. /* Handshake has finished so pointers should be the same */
  8740. || !TEST_ptr_eq(clientssl, client2ssl))
  8741. goto end;
  8742. testresult = 1;
  8743. end:
  8744. SSL_free(serverssl);
  8745. SSL_free(clientssl);
  8746. SSL_free(client2ssl);
  8747. SSL_CTX_free(sctx);
  8748. SSL_CTX_free(cctx);
  8749. return testresult;
  8750. }
  8751. # ifndef OPENSSL_NO_DH
  8752. static EVP_PKEY *tmp_dh_params = NULL;
  8753. /* Helper function for the test_set_tmp_dh() tests */
  8754. static EVP_PKEY *get_tmp_dh_params(void)
  8755. {
  8756. if (tmp_dh_params == NULL) {
  8757. BIGNUM *p = NULL;
  8758. OSSL_PARAM_BLD *tmpl = NULL;
  8759. EVP_PKEY_CTX *pctx = NULL;
  8760. OSSL_PARAM *params = NULL;
  8761. EVP_PKEY *dhpkey = NULL;
  8762. p = BN_get_rfc3526_prime_2048(NULL);
  8763. if (!TEST_ptr(p))
  8764. goto end;
  8765. pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
  8766. if (!TEST_ptr(pctx)
  8767. || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
  8768. goto end;
  8769. tmpl = OSSL_PARAM_BLD_new();
  8770. if (!TEST_ptr(tmpl)
  8771. || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
  8772. OSSL_PKEY_PARAM_FFC_P,
  8773. p))
  8774. || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
  8775. OSSL_PKEY_PARAM_FFC_G,
  8776. 2)))
  8777. goto end;
  8778. params = OSSL_PARAM_BLD_to_param(tmpl);
  8779. if (!TEST_ptr(params)
  8780. || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
  8781. EVP_PKEY_KEY_PARAMETERS,
  8782. params), 1))
  8783. goto end;
  8784. tmp_dh_params = dhpkey;
  8785. end:
  8786. BN_free(p);
  8787. EVP_PKEY_CTX_free(pctx);
  8788. OSSL_PARAM_BLD_free(tmpl);
  8789. OSSL_PARAM_free(params);
  8790. }
  8791. if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
  8792. return NULL;
  8793. return tmp_dh_params;
  8794. }
  8795. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8796. /* Callback used by test_set_tmp_dh() */
  8797. static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
  8798. {
  8799. EVP_PKEY *dhpkey = get_tmp_dh_params();
  8800. DH *ret = NULL;
  8801. if (!TEST_ptr(dhpkey))
  8802. return NULL;
  8803. /*
  8804. * libssl does not free the returned DH, so we free it now knowing that even
  8805. * after we free dhpkey, there will still be a reference to the owning
  8806. * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
  8807. * of time we need it for.
  8808. */
  8809. ret = EVP_PKEY_get1_DH(dhpkey);
  8810. DH_free(ret);
  8811. EVP_PKEY_free(dhpkey);
  8812. return ret;
  8813. }
  8814. # endif
  8815. /*
  8816. * Test the various methods for setting temporary DH parameters
  8817. *
  8818. * Test 0: Default (no auto) setting
  8819. * Test 1: Explicit SSL_CTX auto off
  8820. * Test 2: Explicit SSL auto off
  8821. * Test 3: Explicit SSL_CTX auto on
  8822. * Test 4: Explicit SSL auto on
  8823. * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
  8824. * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
  8825. *
  8826. * The following are testing deprecated APIs, so we only run them if available
  8827. * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
  8828. * Test 8: Explicit SSL auto off, custom DH params via DH
  8829. * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
  8830. * Test 10: Explicit SSL auto off, custom DH params via callback
  8831. */
  8832. static int test_set_tmp_dh(int idx)
  8833. {
  8834. SSL_CTX *cctx = NULL, *sctx = NULL;
  8835. SSL *clientssl = NULL, *serverssl = NULL;
  8836. int testresult = 0;
  8837. int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
  8838. int expected = (idx <= 2) ? 0 : 1;
  8839. EVP_PKEY *dhpkey = NULL;
  8840. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8841. DH *dh = NULL;
  8842. # else
  8843. if (idx >= 7)
  8844. return 1;
  8845. # endif
  8846. if (idx >= 5 && idx <= 8) {
  8847. dhpkey = get_tmp_dh_params();
  8848. if (!TEST_ptr(dhpkey))
  8849. goto end;
  8850. }
  8851. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8852. if (idx == 7 || idx == 8) {
  8853. dh = EVP_PKEY_get1_DH(dhpkey);
  8854. if (!TEST_ptr(dh))
  8855. goto end;
  8856. }
  8857. # endif
  8858. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8859. TLS_client_method(),
  8860. 0,
  8861. 0,
  8862. &sctx, &cctx, cert, privkey)))
  8863. goto end;
  8864. if ((idx & 1) == 1) {
  8865. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
  8866. goto end;
  8867. }
  8868. if (idx == 5) {
  8869. if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
  8870. goto end;
  8871. dhpkey = NULL;
  8872. }
  8873. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8874. else if (idx == 7) {
  8875. if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
  8876. goto end;
  8877. } else if (idx == 9) {
  8878. SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
  8879. }
  8880. # endif
  8881. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8882. NULL, NULL)))
  8883. goto end;
  8884. if ((idx & 1) == 0 && idx != 0) {
  8885. if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
  8886. goto end;
  8887. }
  8888. if (idx == 6) {
  8889. if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
  8890. goto end;
  8891. dhpkey = NULL;
  8892. }
  8893. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8894. else if (idx == 8) {
  8895. if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
  8896. goto end;
  8897. } else if (idx == 10) {
  8898. SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
  8899. }
  8900. # endif
  8901. if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  8902. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  8903. || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
  8904. goto end;
  8905. /*
  8906. * If autoon then we should succeed. Otherwise we expect failure because
  8907. * there are no parameters
  8908. */
  8909. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  8910. SSL_ERROR_NONE), expected))
  8911. goto end;
  8912. testresult = 1;
  8913. end:
  8914. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8915. DH_free(dh);
  8916. # endif
  8917. SSL_free(serverssl);
  8918. SSL_free(clientssl);
  8919. SSL_CTX_free(sctx);
  8920. SSL_CTX_free(cctx);
  8921. EVP_PKEY_free(dhpkey);
  8922. return testresult;
  8923. }
  8924. /*
  8925. * Test the auto DH keys are appropriately sized
  8926. */
  8927. static int test_dh_auto(int idx)
  8928. {
  8929. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
  8930. SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8931. SSL *clientssl = NULL, *serverssl = NULL;
  8932. int testresult = 0;
  8933. EVP_PKEY *tmpkey = NULL;
  8934. char *thiscert = NULL, *thiskey = NULL;
  8935. size_t expdhsize = 0;
  8936. const char *ciphersuite = "DHE-RSA-AES128-SHA";
  8937. if (!TEST_ptr(sctx) || !TEST_ptr(cctx))
  8938. goto end;
  8939. switch (idx) {
  8940. case 0:
  8941. /* The FIPS provider doesn't support this DH size - so we ignore it */
  8942. if (is_fips) {
  8943. testresult = 1;
  8944. goto end;
  8945. }
  8946. thiscert = cert1024;
  8947. thiskey = privkey1024;
  8948. expdhsize = 1024;
  8949. SSL_CTX_set_security_level(sctx, 1);
  8950. SSL_CTX_set_security_level(cctx, 1);
  8951. break;
  8952. case 1:
  8953. /* 2048 bit prime */
  8954. thiscert = cert;
  8955. thiskey = privkey;
  8956. expdhsize = 2048;
  8957. break;
  8958. case 2:
  8959. thiscert = cert3072;
  8960. thiskey = privkey3072;
  8961. expdhsize = 3072;
  8962. break;
  8963. case 3:
  8964. thiscert = cert4096;
  8965. thiskey = privkey4096;
  8966. expdhsize = 4096;
  8967. break;
  8968. case 4:
  8969. thiscert = cert8192;
  8970. thiskey = privkey8192;
  8971. expdhsize = 8192;
  8972. break;
  8973. /* No certificate cases */
  8974. case 5:
  8975. /* The FIPS provider doesn't support this DH size - so we ignore it */
  8976. if (is_fips) {
  8977. testresult = 1;
  8978. goto end;
  8979. }
  8980. ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
  8981. expdhsize = 1024;
  8982. break;
  8983. case 6:
  8984. ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
  8985. expdhsize = 3072;
  8986. break;
  8987. default:
  8988. TEST_error("Invalid text index");
  8989. goto end;
  8990. }
  8991. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL,
  8992. NULL,
  8993. 0,
  8994. 0,
  8995. &sctx, &cctx, thiscert, thiskey)))
  8996. goto end;
  8997. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8998. NULL, NULL)))
  8999. goto end;
  9000. if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
  9001. || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  9002. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  9003. || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
  9004. || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
  9005. goto end;
  9006. /*
  9007. * Send the server's first flight. At this point the server has created the
  9008. * temporary DH key but hasn't finished using it yet. Once used it is
  9009. * removed, so we cannot test it.
  9010. */
  9011. if (!TEST_int_le(SSL_connect(clientssl), 0)
  9012. || !TEST_int_le(SSL_accept(serverssl), 0))
  9013. goto end;
  9014. if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
  9015. goto end;
  9016. if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
  9017. goto end;
  9018. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9019. goto end;
  9020. testresult = 1;
  9021. end:
  9022. SSL_free(serverssl);
  9023. SSL_free(clientssl);
  9024. SSL_CTX_free(sctx);
  9025. SSL_CTX_free(cctx);
  9026. EVP_PKEY_free(tmpkey);
  9027. return testresult;
  9028. }
  9029. # endif /* OPENSSL_NO_DH */
  9030. #endif /* OPENSSL_NO_TLS1_2 */
  9031. #ifndef OSSL_NO_USABLE_TLS1_3
  9032. /*
  9033. * Test that setting an SNI callback works with TLSv1.3. Specifically we check
  9034. * that it works even without a certificate configured for the original
  9035. * SSL_CTX
  9036. */
  9037. static int test_sni_tls13(void)
  9038. {
  9039. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  9040. SSL *clientssl = NULL, *serverssl = NULL;
  9041. int testresult = 0;
  9042. /* Reset callback counter */
  9043. snicb = 0;
  9044. /* Create an initial SSL_CTX with no certificate configured */
  9045. sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9046. if (!TEST_ptr(sctx))
  9047. goto end;
  9048. /* Require TLSv1.3 as a minimum */
  9049. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9050. TLS_client_method(), TLS1_3_VERSION, 0,
  9051. &sctx2, &cctx, cert, privkey)))
  9052. goto end;
  9053. /* Set up SNI */
  9054. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  9055. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  9056. goto end;
  9057. /*
  9058. * Connection should still succeed because the final SSL_CTX has the right
  9059. * certificates configured.
  9060. */
  9061. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9062. &clientssl, NULL, NULL))
  9063. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  9064. SSL_ERROR_NONE)))
  9065. goto end;
  9066. /* We should have had the SNI callback called exactly once */
  9067. if (!TEST_int_eq(snicb, 1))
  9068. goto end;
  9069. testresult = 1;
  9070. end:
  9071. SSL_free(serverssl);
  9072. SSL_free(clientssl);
  9073. SSL_CTX_free(sctx2);
  9074. SSL_CTX_free(sctx);
  9075. SSL_CTX_free(cctx);
  9076. return testresult;
  9077. }
  9078. /*
  9079. * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
  9080. * 0 = TLSv1.2
  9081. * 1 = TLSv1.3
  9082. */
  9083. static int test_ticket_lifetime(int idx)
  9084. {
  9085. SSL_CTX *cctx = NULL, *sctx = NULL;
  9086. SSL *clientssl = NULL, *serverssl = NULL;
  9087. int testresult = 0;
  9088. int version = TLS1_3_VERSION;
  9089. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  9090. #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
  9091. if (idx == 0) {
  9092. #ifdef OPENSSL_NO_TLS1_2
  9093. return TEST_skip("TLS 1.2 is disabled.");
  9094. #else
  9095. version = TLS1_2_VERSION;
  9096. #endif
  9097. }
  9098. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9099. TLS_client_method(), version, version,
  9100. &sctx, &cctx, cert, privkey)))
  9101. goto end;
  9102. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9103. &clientssl, NULL, NULL)))
  9104. goto end;
  9105. /*
  9106. * Set the timeout to be more than 1 week
  9107. * make sure the returned value is the default
  9108. */
  9109. if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
  9110. SSL_get_default_timeout(serverssl)))
  9111. goto end;
  9112. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9113. goto end;
  9114. if (idx == 0) {
  9115. /* TLSv1.2 uses the set value */
  9116. if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
  9117. goto end;
  9118. } else {
  9119. /* TLSv1.3 uses the limited value */
  9120. if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
  9121. goto end;
  9122. }
  9123. testresult = 1;
  9124. end:
  9125. SSL_free(serverssl);
  9126. SSL_free(clientssl);
  9127. SSL_CTX_free(sctx);
  9128. SSL_CTX_free(cctx);
  9129. return testresult;
  9130. }
  9131. #endif
  9132. /*
  9133. * Test that setting an ALPN does not violate RFC
  9134. */
  9135. static int test_set_alpn(void)
  9136. {
  9137. SSL_CTX *ctx = NULL;
  9138. SSL *ssl = NULL;
  9139. int testresult = 0;
  9140. unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
  9141. unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
  9142. unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
  9143. unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
  9144. unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
  9145. unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
  9146. /* Create an initial SSL_CTX with no certificate configured */
  9147. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9148. if (!TEST_ptr(ctx))
  9149. goto end;
  9150. /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
  9151. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
  9152. goto end;
  9153. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
  9154. goto end;
  9155. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
  9156. goto end;
  9157. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
  9158. goto end;
  9159. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
  9160. goto end;
  9161. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
  9162. goto end;
  9163. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
  9164. goto end;
  9165. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
  9166. goto end;
  9167. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
  9168. goto end;
  9169. ssl = SSL_new(ctx);
  9170. if (!TEST_ptr(ssl))
  9171. goto end;
  9172. if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
  9173. goto end;
  9174. if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
  9175. goto end;
  9176. if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
  9177. goto end;
  9178. if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
  9179. goto end;
  9180. if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
  9181. goto end;
  9182. if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
  9183. goto end;
  9184. if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
  9185. goto end;
  9186. if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
  9187. goto end;
  9188. if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
  9189. goto end;
  9190. testresult = 1;
  9191. end:
  9192. SSL_free(ssl);
  9193. SSL_CTX_free(ctx);
  9194. return testresult;
  9195. }
  9196. /*
  9197. * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
  9198. */
  9199. static int test_set_verify_cert_store_ssl_ctx(void)
  9200. {
  9201. SSL_CTX *ctx = NULL;
  9202. int testresult = 0;
  9203. X509_STORE *store = NULL, *new_store = NULL,
  9204. *cstore = NULL, *new_cstore = NULL;
  9205. /* Create an initial SSL_CTX. */
  9206. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9207. if (!TEST_ptr(ctx))
  9208. goto end;
  9209. /* Retrieve verify store pointer. */
  9210. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  9211. goto end;
  9212. /* Retrieve chain store pointer. */
  9213. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  9214. goto end;
  9215. /* We haven't set any yet, so this should be NULL. */
  9216. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9217. goto end;
  9218. /* Create stores. We use separate stores so pointers are different. */
  9219. new_store = X509_STORE_new();
  9220. if (!TEST_ptr(new_store))
  9221. goto end;
  9222. new_cstore = X509_STORE_new();
  9223. if (!TEST_ptr(new_cstore))
  9224. goto end;
  9225. /* Set stores. */
  9226. if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
  9227. goto end;
  9228. if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
  9229. goto end;
  9230. /* Should be able to retrieve the same pointer. */
  9231. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  9232. goto end;
  9233. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  9234. goto end;
  9235. if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
  9236. goto end;
  9237. /* Should be able to unset again. */
  9238. if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
  9239. goto end;
  9240. if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
  9241. goto end;
  9242. /* Should now be NULL. */
  9243. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  9244. goto end;
  9245. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  9246. goto end;
  9247. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9248. goto end;
  9249. testresult = 1;
  9250. end:
  9251. X509_STORE_free(new_store);
  9252. X509_STORE_free(new_cstore);
  9253. SSL_CTX_free(ctx);
  9254. return testresult;
  9255. }
  9256. /*
  9257. * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
  9258. */
  9259. static int test_set_verify_cert_store_ssl(void)
  9260. {
  9261. SSL_CTX *ctx = NULL;
  9262. SSL *ssl = NULL;
  9263. int testresult = 0;
  9264. X509_STORE *store = NULL, *new_store = NULL,
  9265. *cstore = NULL, *new_cstore = NULL;
  9266. /* Create an initial SSL_CTX. */
  9267. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9268. if (!TEST_ptr(ctx))
  9269. goto end;
  9270. /* Create an SSL object. */
  9271. ssl = SSL_new(ctx);
  9272. if (!TEST_ptr(ssl))
  9273. goto end;
  9274. /* Retrieve verify store pointer. */
  9275. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  9276. goto end;
  9277. /* Retrieve chain store pointer. */
  9278. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  9279. goto end;
  9280. /* We haven't set any yet, so this should be NULL. */
  9281. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9282. goto end;
  9283. /* Create stores. We use separate stores so pointers are different. */
  9284. new_store = X509_STORE_new();
  9285. if (!TEST_ptr(new_store))
  9286. goto end;
  9287. new_cstore = X509_STORE_new();
  9288. if (!TEST_ptr(new_cstore))
  9289. goto end;
  9290. /* Set stores. */
  9291. if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
  9292. goto end;
  9293. if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
  9294. goto end;
  9295. /* Should be able to retrieve the same pointer. */
  9296. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  9297. goto end;
  9298. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  9299. goto end;
  9300. if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
  9301. goto end;
  9302. /* Should be able to unset again. */
  9303. if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
  9304. goto end;
  9305. if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
  9306. goto end;
  9307. /* Should now be NULL. */
  9308. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  9309. goto end;
  9310. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  9311. goto end;
  9312. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9313. goto end;
  9314. testresult = 1;
  9315. end:
  9316. X509_STORE_free(new_store);
  9317. X509_STORE_free(new_cstore);
  9318. SSL_free(ssl);
  9319. SSL_CTX_free(ctx);
  9320. return testresult;
  9321. }
  9322. static int test_inherit_verify_param(void)
  9323. {
  9324. int testresult = 0;
  9325. SSL_CTX *ctx = NULL;
  9326. X509_VERIFY_PARAM *cp = NULL;
  9327. SSL *ssl = NULL;
  9328. X509_VERIFY_PARAM *sp = NULL;
  9329. int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
  9330. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9331. if (!TEST_ptr(ctx))
  9332. goto end;
  9333. cp = SSL_CTX_get0_param(ctx);
  9334. if (!TEST_ptr(cp))
  9335. goto end;
  9336. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
  9337. goto end;
  9338. X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
  9339. ssl = SSL_new(ctx);
  9340. if (!TEST_ptr(ssl))
  9341. goto end;
  9342. sp = SSL_get0_param(ssl);
  9343. if (!TEST_ptr(sp))
  9344. goto end;
  9345. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
  9346. goto end;
  9347. testresult = 1;
  9348. end:
  9349. SSL_free(ssl);
  9350. SSL_CTX_free(ctx);
  9351. return testresult;
  9352. }
  9353. static int test_load_dhfile(void)
  9354. {
  9355. #ifndef OPENSSL_NO_DH
  9356. int testresult = 0;
  9357. SSL_CTX *ctx = NULL;
  9358. SSL_CONF_CTX *cctx = NULL;
  9359. if (dhfile == NULL)
  9360. return 1;
  9361. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
  9362. || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
  9363. goto end;
  9364. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  9365. SSL_CONF_CTX_set_flags(cctx,
  9366. SSL_CONF_FLAG_CERTIFICATE
  9367. | SSL_CONF_FLAG_SERVER
  9368. | SSL_CONF_FLAG_FILE);
  9369. if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
  9370. goto end;
  9371. testresult = 1;
  9372. end:
  9373. SSL_CONF_CTX_free(cctx);
  9374. SSL_CTX_free(ctx);
  9375. return testresult;
  9376. #else
  9377. return TEST_skip("DH not supported by this build");
  9378. #endif
  9379. }
  9380. #ifndef OSSL_NO_USABLE_TLS1_3
  9381. /* Test that read_ahead works across a key change */
  9382. static int test_read_ahead_key_change(void)
  9383. {
  9384. SSL_CTX *cctx = NULL, *sctx = NULL;
  9385. SSL *clientssl = NULL, *serverssl = NULL;
  9386. int testresult = 0;
  9387. char *msg = "Hello World";
  9388. size_t written, readbytes;
  9389. char buf[80];
  9390. int i;
  9391. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9392. TLS_client_method(), TLS1_3_VERSION, 0,
  9393. &sctx, &cctx, cert, privkey)))
  9394. goto end;
  9395. SSL_CTX_set_read_ahead(sctx, 1);
  9396. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9397. &clientssl, NULL, NULL)))
  9398. goto end;
  9399. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9400. goto end;
  9401. /* Write some data, send a key update, write more data */
  9402. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  9403. || !TEST_size_t_eq(written, strlen(msg)))
  9404. goto end;
  9405. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  9406. goto end;
  9407. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  9408. || !TEST_size_t_eq(written, strlen(msg)))
  9409. goto end;
  9410. /*
  9411. * Since read_ahead is on the first read below should read the record with
  9412. * the first app data, the second record with the key update message, and
  9413. * the third record with the app data all in one go. We should be able to
  9414. * still process the read_ahead data correctly even though it crosses
  9415. * epochs
  9416. */
  9417. for (i = 0; i < 2; i++) {
  9418. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
  9419. &readbytes)))
  9420. goto end;
  9421. buf[readbytes] = '\0';
  9422. if (!TEST_str_eq(buf, msg))
  9423. goto end;
  9424. }
  9425. testresult = 1;
  9426. end:
  9427. SSL_free(serverssl);
  9428. SSL_free(clientssl);
  9429. SSL_CTX_free(sctx);
  9430. SSL_CTX_free(cctx);
  9431. return testresult;
  9432. }
  9433. static size_t record_pad_cb(SSL *s, int type, size_t len, void *arg)
  9434. {
  9435. int *called = arg;
  9436. switch ((*called)++) {
  9437. case 0:
  9438. /* Add some padding to first record */
  9439. return 512;
  9440. case 1:
  9441. /* Maximally pad the second record */
  9442. return SSL3_RT_MAX_PLAIN_LENGTH - len;
  9443. case 2:
  9444. /*
  9445. * Exceeding the maximum padding should be fine. It should just pad to
  9446. * the maximum anyway
  9447. */
  9448. return SSL3_RT_MAX_PLAIN_LENGTH + 1 - len;
  9449. case 3:
  9450. /*
  9451. * Very large padding should also be ok. Should just pad to the maximum
  9452. * allowed
  9453. */
  9454. return SIZE_MAX;
  9455. default:
  9456. return 0;
  9457. }
  9458. }
  9459. /*
  9460. * Test that setting record padding in TLSv1.3 works as expected
  9461. * Test 0: Record padding callback on the SSL_CTX
  9462. * Test 1: Record padding callback on the SSL
  9463. * Test 2: Record block padding on the SSL_CTX
  9464. * Test 3: Record block padding on the SSL
  9465. */
  9466. static int test_tls13_record_padding(int idx)
  9467. {
  9468. SSL_CTX *cctx = NULL, *sctx = NULL;
  9469. SSL *clientssl = NULL, *serverssl = NULL;
  9470. int testresult = 0;
  9471. char *msg = "Hello World";
  9472. size_t written, readbytes;
  9473. char buf[80];
  9474. int i;
  9475. int called = 0;
  9476. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9477. TLS_client_method(), TLS1_3_VERSION, 0,
  9478. &sctx, &cctx, cert, privkey)))
  9479. goto end;
  9480. if (idx == 0) {
  9481. SSL_CTX_set_record_padding_callback(cctx, record_pad_cb);
  9482. SSL_CTX_set_record_padding_callback_arg(cctx, &called);
  9483. if (!TEST_ptr_eq(SSL_CTX_get_record_padding_callback_arg(cctx), &called))
  9484. goto end;
  9485. } else if (idx == 2) {
  9486. /* Exceeding the max plain length should fail */
  9487. if (!TEST_false(SSL_CTX_set_block_padding(cctx,
  9488. SSL3_RT_MAX_PLAIN_LENGTH + 1)))
  9489. goto end;
  9490. if (!TEST_true(SSL_CTX_set_block_padding(cctx, 512)))
  9491. goto end;
  9492. }
  9493. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9494. &clientssl, NULL, NULL)))
  9495. goto end;
  9496. if (idx == 1) {
  9497. SSL_set_record_padding_callback(clientssl, record_pad_cb);
  9498. SSL_set_record_padding_callback_arg(clientssl, &called);
  9499. if (!TEST_ptr_eq(SSL_get_record_padding_callback_arg(clientssl), &called))
  9500. goto end;
  9501. } else if (idx == 3) {
  9502. /* Exceeding the max plain length should fail */
  9503. if (!TEST_false(SSL_set_block_padding(clientssl,
  9504. SSL3_RT_MAX_PLAIN_LENGTH + 1)))
  9505. goto end;
  9506. if (!TEST_true(SSL_set_block_padding(clientssl, 512)))
  9507. goto end;
  9508. }
  9509. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9510. goto end;
  9511. called = 0;
  9512. /*
  9513. * Write some data, then check we can read it. Do this four times to check
  9514. * we can continue to write and read padded data after the initial record
  9515. * padding has been added. We don't actually check that the padding has
  9516. * been applied to the record - just that we can continue to communicate
  9517. * normally and that the callback has been called (if appropriate).
  9518. */
  9519. for (i = 0; i < 4; i++) {
  9520. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  9521. || !TEST_size_t_eq(written, strlen(msg)))
  9522. goto end;
  9523. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
  9524. &readbytes))
  9525. || !TEST_size_t_eq(written, readbytes))
  9526. goto end;
  9527. buf[readbytes] = '\0';
  9528. if (!TEST_str_eq(buf, msg))
  9529. goto end;
  9530. }
  9531. if ((idx == 0 || idx == 1) && !TEST_int_eq(called, 4))
  9532. goto end;
  9533. testresult = 1;
  9534. end:
  9535. SSL_free(serverssl);
  9536. SSL_free(clientssl);
  9537. SSL_CTX_free(sctx);
  9538. SSL_CTX_free(cctx);
  9539. return testresult;
  9540. }
  9541. #endif /* OSSL_NO_USABLE_TLS1_3 */
  9542. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  9543. static ENGINE *load_dasync(void)
  9544. {
  9545. ENGINE *e;
  9546. if (!TEST_ptr(e = ENGINE_by_id("dasync")))
  9547. return NULL;
  9548. if (!TEST_true(ENGINE_init(e))) {
  9549. ENGINE_free(e);
  9550. return NULL;
  9551. }
  9552. if (!TEST_true(ENGINE_register_ciphers(e))) {
  9553. ENGINE_free(e);
  9554. return NULL;
  9555. }
  9556. return e;
  9557. }
  9558. /*
  9559. * Test TLSv1.2 with a pipeline capable cipher. TLSv1.3 and DTLS do not
  9560. * support this yet. The only pipeline capable cipher that we have is in the
  9561. * dasync engine (providers don't support this yet), so we have to use
  9562. * deprecated APIs for this test.
  9563. *
  9564. * Test 0: Client has pipelining enabled, server does not
  9565. * Test 1: Server has pipelining enabled, client does not
  9566. * Test 2: Client has pipelining enabled, server does not: not enough data to
  9567. * fill all the pipelines
  9568. * Test 3: Client has pipelining enabled, server does not: not enough data to
  9569. * fill all the pipelines by more than a full pipeline's worth
  9570. * Test 4: Client has pipelining enabled, server does not: more data than all
  9571. * the available pipelines can take
  9572. * Test 5: Client has pipelining enabled, server does not: Maximum size pipeline
  9573. * Test 6: Repeat of test 0, but the engine is loaded late (after the SSL_CTX
  9574. * is created)
  9575. */
  9576. static int test_pipelining(int idx)
  9577. {
  9578. SSL_CTX *cctx = NULL, *sctx = NULL;
  9579. SSL *clientssl = NULL, *serverssl = NULL, *peera, *peerb;
  9580. int testresult = 0, numreads;
  9581. /* A 55 byte message */
  9582. unsigned char *msg = (unsigned char *)
  9583. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123";
  9584. size_t written, readbytes, offset, msglen, fragsize = 10, numpipes = 5;
  9585. size_t expectedreads;
  9586. unsigned char *buf = NULL;
  9587. ENGINE *e = NULL;
  9588. if (idx != 6) {
  9589. e = load_dasync();
  9590. if (e == NULL)
  9591. return 0;
  9592. }
  9593. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9594. TLS_client_method(), 0,
  9595. TLS1_2_VERSION, &sctx, &cctx, cert,
  9596. privkey)))
  9597. goto end;
  9598. if (idx == 6) {
  9599. e = load_dasync();
  9600. if (e == NULL)
  9601. goto end;
  9602. /* Now act like test 0 */
  9603. idx = 0;
  9604. }
  9605. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9606. &clientssl, NULL, NULL)))
  9607. goto end;
  9608. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES128-SHA")))
  9609. goto end;
  9610. /* peera is always configured for pipelining, while peerb is not. */
  9611. if (idx == 1) {
  9612. peera = serverssl;
  9613. peerb = clientssl;
  9614. } else {
  9615. peera = clientssl;
  9616. peerb = serverssl;
  9617. }
  9618. if (idx == 5) {
  9619. numpipes = 2;
  9620. /* Maximum allowed fragment size */
  9621. fragsize = SSL3_RT_MAX_PLAIN_LENGTH;
  9622. msglen = fragsize * numpipes;
  9623. msg = OPENSSL_malloc(msglen);
  9624. if (!TEST_ptr(msg))
  9625. goto end;
  9626. if (!TEST_int_gt(RAND_bytes_ex(libctx, msg, msglen, 0), 0))
  9627. goto end;
  9628. } else if (idx == 4) {
  9629. msglen = 55;
  9630. } else {
  9631. msglen = 50;
  9632. }
  9633. if (idx == 2)
  9634. msglen -= 2; /* Send 2 less bytes */
  9635. else if (idx == 3)
  9636. msglen -= 12; /* Send 12 less bytes */
  9637. buf = OPENSSL_malloc(msglen);
  9638. if (!TEST_ptr(buf))
  9639. goto end;
  9640. if (idx == 5) {
  9641. /*
  9642. * Test that setting a split send fragment longer than the maximum
  9643. * allowed fails
  9644. */
  9645. if (!TEST_false(SSL_set_split_send_fragment(peera, fragsize + 1)))
  9646. goto end;
  9647. }
  9648. /*
  9649. * In the normal case. We have 5 pipelines with 10 bytes per pipeline
  9650. * (50 bytes in total). This is a ridiculously small number of bytes -
  9651. * but sufficient for our purposes
  9652. */
  9653. if (!TEST_true(SSL_set_max_pipelines(peera, numpipes))
  9654. || !TEST_true(SSL_set_split_send_fragment(peera, fragsize)))
  9655. goto end;
  9656. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9657. goto end;
  9658. /* Write some data from peera to peerb */
  9659. if (!TEST_true(SSL_write_ex(peera, msg, msglen, &written))
  9660. || !TEST_size_t_eq(written, msglen))
  9661. goto end;
  9662. /*
  9663. * If the pipelining code worked, then we expect all |numpipes| pipelines to
  9664. * have been used - except in test 3 where only |numpipes - 1| pipelines
  9665. * will be used. This will result in |numpipes| records (|numpipes - 1| for
  9666. * test 3) having been sent to peerb. Since peerb is not using read_ahead we
  9667. * expect this to be read in |numpipes| or |numpipes - 1| separate
  9668. * SSL_read_ex calls. In the case of test 4, there is then one additional
  9669. * read for left over data that couldn't fit in the previous pipelines
  9670. */
  9671. for (offset = 0, numreads = 0;
  9672. offset < msglen;
  9673. offset += readbytes, numreads++) {
  9674. if (!TEST_true(SSL_read_ex(peerb, buf + offset,
  9675. msglen - offset, &readbytes)))
  9676. goto end;
  9677. }
  9678. expectedreads = idx == 4 ? numpipes + 1
  9679. : (idx == 3 ? numpipes - 1 : numpipes);
  9680. if (!TEST_mem_eq(msg, msglen, buf, offset)
  9681. || !TEST_int_eq(numreads, expectedreads))
  9682. goto end;
  9683. /*
  9684. * Write some data from peerb to peera. We do this in up to |numpipes + 1|
  9685. * chunks to exercise the read pipelining code on peera.
  9686. */
  9687. for (offset = 0; offset < msglen; offset += fragsize) {
  9688. size_t sendlen = msglen - offset;
  9689. if (sendlen > fragsize)
  9690. sendlen = fragsize;
  9691. if (!TEST_true(SSL_write_ex(peerb, msg + offset, sendlen, &written))
  9692. || !TEST_size_t_eq(written, sendlen))
  9693. goto end;
  9694. }
  9695. /*
  9696. * The data was written in |numpipes|, |numpipes - 1| or |numpipes + 1|
  9697. * separate chunks (depending on which test we are running). If the
  9698. * pipelining is working then we expect peera to read up to numpipes chunks
  9699. * and process them in parallel, giving back the complete result in a single
  9700. * call to SSL_read_ex
  9701. */
  9702. if (!TEST_true(SSL_read_ex(peera, buf, msglen, &readbytes))
  9703. || !TEST_size_t_le(readbytes, msglen))
  9704. goto end;
  9705. if (idx == 4) {
  9706. size_t readbytes2;
  9707. if (!TEST_true(SSL_read_ex(peera, buf + readbytes,
  9708. msglen - readbytes, &readbytes2)))
  9709. goto end;
  9710. readbytes += readbytes2;
  9711. if (!TEST_size_t_le(readbytes, msglen))
  9712. goto end;
  9713. }
  9714. if (!TEST_mem_eq(msg, msglen, buf, readbytes))
  9715. goto end;
  9716. testresult = 1;
  9717. end:
  9718. SSL_free(serverssl);
  9719. SSL_free(clientssl);
  9720. SSL_CTX_free(sctx);
  9721. SSL_CTX_free(cctx);
  9722. if (e != NULL) {
  9723. ENGINE_unregister_ciphers(e);
  9724. ENGINE_finish(e);
  9725. ENGINE_free(e);
  9726. }
  9727. OPENSSL_free(buf);
  9728. if (fragsize == SSL3_RT_MAX_PLAIN_LENGTH)
  9729. OPENSSL_free(msg);
  9730. return testresult;
  9731. }
  9732. #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) */
  9733. static int check_version_string(SSL *s, int version)
  9734. {
  9735. const char *verstr = NULL;
  9736. switch (version) {
  9737. case SSL3_VERSION:
  9738. verstr = "SSLv3";
  9739. break;
  9740. case TLS1_VERSION:
  9741. verstr = "TLSv1";
  9742. break;
  9743. case TLS1_1_VERSION:
  9744. verstr = "TLSv1.1";
  9745. break;
  9746. case TLS1_2_VERSION:
  9747. verstr = "TLSv1.2";
  9748. break;
  9749. case TLS1_3_VERSION:
  9750. verstr = "TLSv1.3";
  9751. break;
  9752. case DTLS1_VERSION:
  9753. verstr = "DTLSv1";
  9754. break;
  9755. case DTLS1_2_VERSION:
  9756. verstr = "DTLSv1.2";
  9757. }
  9758. return TEST_str_eq(verstr, SSL_get_version(s));
  9759. }
  9760. /*
  9761. * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
  9762. * SSL_is_dtls return the expected results for a (D)TLS connection. Compare with
  9763. * test_version() in quicapitest.c which does the same thing for QUIC
  9764. * connections.
  9765. */
  9766. static int test_version(int idx)
  9767. {
  9768. SSL_CTX *cctx = NULL, *sctx = NULL;
  9769. SSL *clientssl = NULL, *serverssl = NULL;
  9770. int testresult = 0, version;
  9771. const SSL_METHOD *servmeth = TLS_server_method();
  9772. const SSL_METHOD *clientmeth = TLS_client_method();
  9773. switch (idx) {
  9774. #if !defined(OPENSSL_NO_SSL3)
  9775. case 0:
  9776. version = SSL3_VERSION;
  9777. break;
  9778. #endif
  9779. #if !defined(OPENSSL_NO_TLS1)
  9780. case 1:
  9781. version = TLS1_VERSION;
  9782. break;
  9783. #endif
  9784. #if !defined(OPENSSL_NO_TLS1_2)
  9785. case 2:
  9786. version = TLS1_2_VERSION;
  9787. break;
  9788. #endif
  9789. #if !defined(OSSL_NO_USABLE_TLS1_3)
  9790. case 3:
  9791. version = TLS1_3_VERSION;
  9792. break;
  9793. #endif
  9794. #if !defined(OPENSSL_NO_DTLS1)
  9795. case 4:
  9796. version = DTLS1_VERSION;
  9797. break;
  9798. #endif
  9799. #if !defined(OPENSSL_NO_DTLS1_2)
  9800. case 5:
  9801. version = DTLS1_2_VERSION;
  9802. break;
  9803. #endif
  9804. /*
  9805. * NB we do not support QUIC in this test. That is covered by quicapitest.c
  9806. * We also don't support DTLS1_BAD_VER since we have no server support for
  9807. * that.
  9808. */
  9809. default:
  9810. TEST_skip("Unsupported protocol version");
  9811. return 1;
  9812. }
  9813. if (is_fips
  9814. && (version == SSL3_VERSION
  9815. || version == TLS1_VERSION
  9816. || version == DTLS1_VERSION)) {
  9817. TEST_skip("Protocol version not supported with FIPS");
  9818. return 1;
  9819. }
  9820. #if !defined(OPENSSL_NO_DTLS)
  9821. if (version == DTLS1_VERSION || version == DTLS1_2_VERSION) {
  9822. servmeth = DTLS_server_method();
  9823. clientmeth = DTLS_client_method();
  9824. }
  9825. #endif
  9826. if (!TEST_true(create_ssl_ctx_pair(libctx, servmeth, clientmeth, version,
  9827. version, &sctx, &cctx, cert, privkey)))
  9828. goto end;
  9829. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  9830. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  9831. "DEFAULT:@SECLEVEL=0")))
  9832. goto end;
  9833. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9834. &clientssl, NULL, NULL)))
  9835. goto end;
  9836. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9837. goto end;
  9838. if (!TEST_int_eq(SSL_version(serverssl), version)
  9839. || !TEST_int_eq(SSL_version(clientssl), version)
  9840. || !TEST_true(check_version_string(serverssl, version))
  9841. || !TEST_true(check_version_string(clientssl, version)))
  9842. goto end;
  9843. if (version == DTLS1_VERSION || version == DTLS1_2_VERSION) {
  9844. if (!TEST_true(SSL_is_dtls(serverssl))
  9845. || !TEST_true(SSL_is_dtls(clientssl))
  9846. || !TEST_false(SSL_is_tls(serverssl))
  9847. || !TEST_false(SSL_is_tls(clientssl))
  9848. || !TEST_false(SSL_is_quic(serverssl))
  9849. || !TEST_false(SSL_is_quic(clientssl)))
  9850. goto end;
  9851. } else {
  9852. if (!TEST_true(SSL_is_tls(serverssl))
  9853. || !TEST_true(SSL_is_tls(clientssl))
  9854. || !TEST_false(SSL_is_dtls(serverssl))
  9855. || !TEST_false(SSL_is_dtls(clientssl))
  9856. || !TEST_false(SSL_is_quic(serverssl))
  9857. || !TEST_false(SSL_is_quic(clientssl)))
  9858. goto end;
  9859. }
  9860. testresult = 1;
  9861. end:
  9862. SSL_free(serverssl);
  9863. SSL_free(clientssl);
  9864. SSL_CTX_free(sctx);
  9865. SSL_CTX_free(cctx);
  9866. return testresult;
  9867. }
  9868. /*
  9869. * Test that the SSL_rstate_string*() APIs return sane results
  9870. */
  9871. static int test_rstate_string(void)
  9872. {
  9873. SSL_CTX *cctx = NULL, *sctx = NULL;
  9874. SSL *clientssl = NULL, *serverssl = NULL;
  9875. int testresult = 0, version;
  9876. const SSL_METHOD *servmeth = TLS_server_method();
  9877. const SSL_METHOD *clientmeth = TLS_client_method();
  9878. size_t written, readbytes;
  9879. unsigned char buf[2];
  9880. unsigned char dummyheader[SSL3_RT_HEADER_LENGTH] = {
  9881. SSL3_RT_APPLICATION_DATA,
  9882. TLS1_2_VERSION_MAJOR,
  9883. 0, /* To be filled in later */
  9884. 0,
  9885. 1
  9886. };
  9887. if (!TEST_true(create_ssl_ctx_pair(libctx, servmeth, clientmeth, 0,
  9888. 0, &sctx, &cctx, cert, privkey)))
  9889. goto end;
  9890. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9891. &clientssl, NULL, NULL)))
  9892. goto end;
  9893. if (!TEST_str_eq(SSL_rstate_string(serverssl), "RH")
  9894. || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read header"))
  9895. goto end;
  9896. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9897. goto end;
  9898. if (!TEST_str_eq(SSL_rstate_string(serverssl), "RH")
  9899. || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read header"))
  9900. goto end;
  9901. /* Fill in the correct version for the record header */
  9902. version = SSL_version(serverssl);
  9903. if (version == TLS1_3_VERSION)
  9904. version = TLS1_2_VERSION;
  9905. dummyheader[2] = version & 0xff;
  9906. /*
  9907. * Send a dummy header. If we continued to read the body as well this
  9908. * would fail with a bad record mac, but we're not going to go that far.
  9909. */
  9910. if (!TEST_true(BIO_write_ex(SSL_get_rbio(serverssl), dummyheader,
  9911. sizeof(dummyheader), &written))
  9912. || !TEST_size_t_eq(written, SSL3_RT_HEADER_LENGTH))
  9913. goto end;
  9914. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)))
  9915. goto end;
  9916. if (!TEST_str_eq(SSL_rstate_string(serverssl), "RB")
  9917. || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read body"))
  9918. goto end;
  9919. testresult = 1;
  9920. end:
  9921. SSL_free(serverssl);
  9922. SSL_free(clientssl);
  9923. SSL_CTX_free(sctx);
  9924. SSL_CTX_free(cctx);
  9925. return testresult;
  9926. }
  9927. /*
  9928. * Force a write retry during handshaking. We test various combinations of
  9929. * scenarios. We test a large certificate message which will fill the buffering
  9930. * BIO used in the handshake. We try with client auth on and off. Finally we
  9931. * also try a BIO that indicates retry via a 0 return. BIO_write() is documented
  9932. * to indicate retry via -1 - but sometimes BIOs don't do that.
  9933. *
  9934. * Test 0: Standard certificate message
  9935. * Test 1: Large certificate message
  9936. * Test 2: Standard cert, verify peer
  9937. * Test 3: Large cert, verify peer
  9938. * Test 4: Standard cert, BIO returns 0 on retry
  9939. * Test 5: Large cert, BIO returns 0 on retry
  9940. * Test 6: Standard cert, verify peer, BIO returns 0 on retry
  9941. * Test 7: Large cert, verify peer, BIO returns 0 on retry
  9942. * Test 8-15: Repeat of above with TLSv1.2
  9943. */
  9944. static int test_handshake_retry(int idx)
  9945. {
  9946. SSL_CTX *cctx = NULL, *sctx = NULL;
  9947. SSL *clientssl = NULL, *serverssl = NULL;
  9948. int testresult = 0;
  9949. BIO *tmp = NULL, *bretry = BIO_new(bio_s_always_retry());
  9950. int maxversion = 0;
  9951. if (!TEST_ptr(bretry))
  9952. goto end;
  9953. #ifndef OPENSSL_NO_TLS1_2
  9954. if ((idx & 8) == 8)
  9955. maxversion = TLS1_2_VERSION;
  9956. #else
  9957. if ((idx & 8) == 8)
  9958. return TEST_skip("No TLSv1.2");
  9959. #endif
  9960. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9961. TLS_client_method(), 0, maxversion,
  9962. &sctx, &cctx, cert, privkey)))
  9963. goto end;
  9964. /*
  9965. * Add a large amount of data to fill the buffering BIO used by the SSL
  9966. * object
  9967. */
  9968. if ((idx & 1) == 1 && !ssl_ctx_add_large_cert_chain(libctx, sctx, cert))
  9969. goto end;
  9970. /*
  9971. * We don't actually configure a client cert, but neither do we fail if one
  9972. * isn't present.
  9973. */
  9974. if ((idx & 2) == 2)
  9975. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  9976. if ((idx & 4) == 4)
  9977. set_always_retry_err_val(0);
  9978. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9979. &clientssl, NULL, NULL)))
  9980. goto end;
  9981. tmp = SSL_get_wbio(serverssl);
  9982. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  9983. tmp = NULL;
  9984. goto end;
  9985. }
  9986. SSL_set0_wbio(serverssl, bretry);
  9987. bretry = NULL;
  9988. if (!TEST_int_eq(SSL_connect(clientssl), -1))
  9989. goto end;
  9990. if (!TEST_int_eq(SSL_accept(serverssl), -1)
  9991. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
  9992. goto end;
  9993. /* Restore a BIO that will let the write succeed */
  9994. SSL_set0_wbio(serverssl, tmp);
  9995. tmp = NULL;
  9996. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9997. goto end;
  9998. testresult = 1;
  9999. end:
  10000. SSL_free(serverssl);
  10001. SSL_free(clientssl);
  10002. SSL_CTX_free(sctx);
  10003. SSL_CTX_free(cctx);
  10004. BIO_free(bretry);
  10005. BIO_free(tmp);
  10006. set_always_retry_err_val(-1);
  10007. return testresult;
  10008. }
  10009. /*
  10010. * Test that receiving retries when writing application data works as expected
  10011. */
  10012. static int test_data_retry(void)
  10013. {
  10014. SSL_CTX *cctx = NULL, *sctx = NULL;
  10015. SSL *clientssl = NULL, *serverssl = NULL;
  10016. int testresult = 0;
  10017. unsigned char inbuf[1200], outbuf[1200];
  10018. size_t i;
  10019. BIO *tmp = NULL;
  10020. BIO *bretry = BIO_new(bio_s_maybe_retry());
  10021. size_t written, readbytes, totread = 0;
  10022. if (!TEST_ptr(bretry))
  10023. goto end;
  10024. for (i = 0; i < sizeof(inbuf); i++)
  10025. inbuf[i] = (unsigned char)(0xff & i);
  10026. memset(outbuf, 0, sizeof(outbuf));
  10027. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10028. TLS_client_method(), 0, 0, &sctx, &cctx,
  10029. cert, privkey)))
  10030. goto end;
  10031. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  10032. NULL)))
  10033. goto end;
  10034. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10035. goto end;
  10036. /* Smallest possible max send fragment is 512 */
  10037. if (!TEST_true(SSL_set_max_send_fragment(clientssl, 512)))
  10038. goto end;
  10039. tmp = SSL_get_wbio(clientssl);
  10040. if (!TEST_ptr(tmp))
  10041. goto end;
  10042. if (!TEST_true(BIO_up_ref(tmp)))
  10043. goto end;
  10044. BIO_push(bretry, tmp);
  10045. tmp = NULL;
  10046. SSL_set0_wbio(clientssl, bretry);
  10047. if (!BIO_up_ref(bretry)) {
  10048. bretry = NULL;
  10049. goto end;
  10050. }
  10051. for (i = 0; i < 3; i++) {
  10052. /* We expect this call to make no progress and indicate retry */
  10053. if (!TEST_false(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written)))
  10054. goto end;
  10055. if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_WRITE))
  10056. goto end;
  10057. /* Allow one write to progress, but the next one to signal retry */
  10058. if (!TEST_true(BIO_ctrl(bretry, MAYBE_RETRY_CTRL_SET_RETRY_AFTER_CNT, 1,
  10059. NULL)))
  10060. goto end;
  10061. if (i == 2)
  10062. break;
  10063. /*
  10064. * This call will hopefully make progress but will still indicate retry
  10065. * because there is more data than will fit into a single record.
  10066. */
  10067. if (!TEST_false(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written)))
  10068. goto end;
  10069. if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_WRITE))
  10070. goto end;
  10071. }
  10072. /* The final call should write the last chunk of data and succeed */
  10073. if (!TEST_true(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written)))
  10074. goto end;
  10075. /* Read all the data available */
  10076. while (SSL_read_ex(serverssl, outbuf + totread, sizeof(outbuf) - totread,
  10077. &readbytes))
  10078. totread += readbytes;
  10079. if (!TEST_mem_eq(inbuf, sizeof(inbuf), outbuf, totread))
  10080. goto end;
  10081. testresult = 1;
  10082. end:
  10083. SSL_free(serverssl);
  10084. SSL_free(clientssl);
  10085. SSL_CTX_free(sctx);
  10086. SSL_CTX_free(cctx);
  10087. BIO_free_all(bretry);
  10088. BIO_free(tmp);
  10089. return testresult;
  10090. }
  10091. struct resume_servername_cb_data {
  10092. int i;
  10093. SSL_CTX *cctx;
  10094. SSL_CTX *sctx;
  10095. SSL_SESSION *sess;
  10096. int recurse;
  10097. };
  10098. /*
  10099. * Servername callback. We use it here to run another complete handshake using
  10100. * the same session - and mark the session as not_resuamble at the end
  10101. */
  10102. static int resume_servername_cb(SSL *s, int *ad, void *arg)
  10103. {
  10104. struct resume_servername_cb_data *cbdata = arg;
  10105. SSL *serverssl = NULL, *clientssl = NULL;
  10106. int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  10107. if (cbdata->recurse)
  10108. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10109. if ((cbdata->i % 3) != 1)
  10110. return SSL_TLSEXT_ERR_OK;
  10111. cbdata->recurse = 1;
  10112. if (!TEST_true(create_ssl_objects(cbdata->sctx, cbdata->cctx, &serverssl,
  10113. &clientssl, NULL, NULL))
  10114. || !TEST_true(SSL_set_session(clientssl, cbdata->sess)))
  10115. goto end;
  10116. ERR_set_mark();
  10117. /*
  10118. * We expect this to fail - because the servername cb will fail. This will
  10119. * mark the session as not_resumable.
  10120. */
  10121. if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
  10122. ERR_clear_last_mark();
  10123. goto end;
  10124. }
  10125. ERR_pop_to_mark();
  10126. ret = SSL_TLSEXT_ERR_OK;
  10127. end:
  10128. SSL_free(serverssl);
  10129. SSL_free(clientssl);
  10130. cbdata->recurse = 0;
  10131. return ret;
  10132. }
  10133. /*
  10134. * Test multiple resumptions and cache size handling
  10135. * Test 0: TLSv1.3 (max_early_data set)
  10136. * Test 1: TLSv1.3 (SSL_OP_NO_TICKET set)
  10137. * Test 2: TLSv1.3 (max_early_data and SSL_OP_NO_TICKET set)
  10138. * Test 3: TLSv1.3 (SSL_OP_NO_TICKET, simultaneous resumes)
  10139. * Test 4: TLSv1.2
  10140. */
  10141. static int test_multi_resume(int idx)
  10142. {
  10143. SSL_CTX *sctx = NULL, *cctx = NULL;
  10144. SSL *serverssl = NULL, *clientssl = NULL;
  10145. SSL_SESSION *sess = NULL;
  10146. int max_version = TLS1_3_VERSION;
  10147. int i, testresult = 0;
  10148. struct resume_servername_cb_data cbdata;
  10149. #if defined(OPENSSL_NO_TLS1_2)
  10150. if (idx == 4)
  10151. return TEST_skip("TLSv1.2 is disabled in this build");
  10152. #else
  10153. if (idx == 4)
  10154. max_version = TLS1_2_VERSION;
  10155. #endif
  10156. #if defined(OSSL_NO_USABLE_TLS1_3)
  10157. if (idx != 4)
  10158. return TEST_skip("No usable TLSv1.3 in this build");
  10159. #endif
  10160. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10161. TLS_client_method(), TLS1_VERSION,
  10162. max_version, &sctx, &cctx, cert,
  10163. privkey)))
  10164. goto end;
  10165. /*
  10166. * TLSv1.3 only uses a session cache if either max_early_data > 0 (used for
  10167. * replay protection), or if SSL_OP_NO_TICKET is in use
  10168. */
  10169. if (idx == 0 || idx == 2) {
  10170. if (!TEST_true(SSL_CTX_set_max_early_data(sctx, 1024)))
  10171. goto end;
  10172. }
  10173. if (idx == 1 || idx == 2 || idx == 3)
  10174. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  10175. SSL_CTX_sess_set_cache_size(sctx, 5);
  10176. if (idx == 3) {
  10177. SSL_CTX_set_tlsext_servername_callback(sctx, resume_servername_cb);
  10178. SSL_CTX_set_tlsext_servername_arg(sctx, &cbdata);
  10179. cbdata.cctx = cctx;
  10180. cbdata.sctx = sctx;
  10181. cbdata.recurse = 0;
  10182. }
  10183. for (i = 0; i < 30; i++) {
  10184. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  10185. NULL, NULL))
  10186. || !TEST_true(SSL_set_session(clientssl, sess)))
  10187. goto end;
  10188. /*
  10189. * Check simultaneous resumes. We pause the connection part way through
  10190. * the handshake by (mis)using the servername_cb. The pause occurs after
  10191. * session resumption has already occurred, but before any session
  10192. * tickets have been issued. While paused we run another complete
  10193. * handshake resuming the same session.
  10194. */
  10195. if (idx == 3) {
  10196. cbdata.i = i;
  10197. cbdata.sess = sess;
  10198. }
  10199. /*
  10200. * Recreate a bug where dynamically changing the max_early_data value
  10201. * can cause sessions in the session cache which cannot be deleted.
  10202. */
  10203. if ((idx == 0 || idx == 2) && (i % 3) == 2)
  10204. SSL_set_max_early_data(serverssl, 0);
  10205. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10206. goto end;
  10207. if (sess == NULL || (idx == 0 && (i % 3) == 2)) {
  10208. if (!TEST_false(SSL_session_reused(clientssl)))
  10209. goto end;
  10210. } else {
  10211. if (!TEST_true(SSL_session_reused(clientssl)))
  10212. goto end;
  10213. }
  10214. SSL_SESSION_free(sess);
  10215. /* Do a full handshake, followed by two resumptions */
  10216. if ((i % 3) == 2) {
  10217. sess = NULL;
  10218. } else {
  10219. if (!TEST_ptr((sess = SSL_get1_session(clientssl))))
  10220. goto end;
  10221. }
  10222. SSL_shutdown(clientssl);
  10223. SSL_shutdown(serverssl);
  10224. SSL_free(serverssl);
  10225. SSL_free(clientssl);
  10226. serverssl = clientssl = NULL;
  10227. }
  10228. /* We should never exceed the session cache size limit */
  10229. if (!TEST_long_le(SSL_CTX_sess_number(sctx), 5))
  10230. goto end;
  10231. testresult = 1;
  10232. end:
  10233. SSL_free(serverssl);
  10234. SSL_free(clientssl);
  10235. SSL_CTX_free(sctx);
  10236. SSL_CTX_free(cctx);
  10237. SSL_SESSION_free(sess);
  10238. return testresult;
  10239. }
  10240. OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
  10241. int setup_tests(void)
  10242. {
  10243. char *modulename;
  10244. char *configfile;
  10245. libctx = OSSL_LIB_CTX_new();
  10246. if (!TEST_ptr(libctx))
  10247. return 0;
  10248. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  10249. /*
  10250. * Verify that the default and fips providers in the default libctx are not
  10251. * available
  10252. */
  10253. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  10254. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  10255. return 0;
  10256. if (!test_skip_common_options()) {
  10257. TEST_error("Error parsing test options\n");
  10258. return 0;
  10259. }
  10260. if (!TEST_ptr(certsdir = test_get_argument(0))
  10261. || !TEST_ptr(srpvfile = test_get_argument(1))
  10262. || !TEST_ptr(tmpfilename = test_get_argument(2))
  10263. || !TEST_ptr(modulename = test_get_argument(3))
  10264. || !TEST_ptr(configfile = test_get_argument(4))
  10265. || !TEST_ptr(dhfile = test_get_argument(5)))
  10266. return 0;
  10267. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  10268. return 0;
  10269. /* Check we have the expected provider available */
  10270. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  10271. return 0;
  10272. /* Check the default provider is not available */
  10273. if (strcmp(modulename, "default") != 0
  10274. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  10275. return 0;
  10276. if (strcmp(modulename, "fips") == 0) {
  10277. OSSL_PROVIDER *prov = NULL;
  10278. OSSL_PARAM params[2];
  10279. is_fips = 1;
  10280. prov = OSSL_PROVIDER_load(libctx, "fips");
  10281. if (prov != NULL) {
  10282. /* Query the fips provider to check if the check ems option is enabled */
  10283. params[0] =
  10284. OSSL_PARAM_construct_int(OSSL_PROV_PARAM_TLS1_PRF_EMS_CHECK,
  10285. &fips_ems_check);
  10286. params[1] = OSSL_PARAM_construct_end();
  10287. OSSL_PROVIDER_get_params(prov, params);
  10288. OSSL_PROVIDER_unload(prov);
  10289. }
  10290. }
  10291. /*
  10292. * We add, but don't load the test "tls-provider". We'll load it when we
  10293. * need it.
  10294. */
  10295. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
  10296. tls_provider_init)))
  10297. return 0;
  10298. if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
  10299. #ifdef OPENSSL_NO_CRYPTO_MDEBUG
  10300. TEST_error("not supported in this build");
  10301. return 0;
  10302. #else
  10303. int i, mcount, rcount, fcount;
  10304. for (i = 0; i < 4; i++)
  10305. test_export_key_mat(i);
  10306. CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
  10307. test_printf_stdout("malloc %d realloc %d free %d\n",
  10308. mcount, rcount, fcount);
  10309. return 1;
  10310. #endif
  10311. }
  10312. cert = test_mk_file_path(certsdir, "servercert.pem");
  10313. if (cert == NULL)
  10314. goto err;
  10315. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  10316. if (privkey == NULL)
  10317. goto err;
  10318. cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  10319. if (cert2 == NULL)
  10320. goto err;
  10321. privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  10322. if (privkey2 == NULL)
  10323. goto err;
  10324. cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
  10325. if (cert1024 == NULL)
  10326. goto err;
  10327. privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
  10328. if (privkey1024 == NULL)
  10329. goto err;
  10330. cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
  10331. if (cert3072 == NULL)
  10332. goto err;
  10333. privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
  10334. if (privkey3072 == NULL)
  10335. goto err;
  10336. cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
  10337. if (cert4096 == NULL)
  10338. goto err;
  10339. privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
  10340. if (privkey4096 == NULL)
  10341. goto err;
  10342. cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
  10343. if (cert8192 == NULL)
  10344. goto err;
  10345. privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
  10346. if (privkey8192 == NULL)
  10347. goto err;
  10348. if (fips_ems_check) {
  10349. #ifndef OPENSSL_NO_TLS1_2
  10350. ADD_TEST(test_no_ems);
  10351. #endif
  10352. return 1;
  10353. }
  10354. #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
  10355. # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  10356. ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
  10357. ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS * 2);
  10358. # endif
  10359. #endif
  10360. ADD_TEST(test_large_message_tls);
  10361. ADD_TEST(test_large_message_tls_read_ahead);
  10362. #ifndef OPENSSL_NO_DTLS
  10363. ADD_TEST(test_large_message_dtls);
  10364. #endif
  10365. ADD_ALL_TESTS(test_large_app_data, 28);
  10366. ADD_TEST(test_cleanse_plaintext);
  10367. #ifndef OPENSSL_NO_OCSP
  10368. ADD_TEST(test_tlsext_status_type);
  10369. #endif
  10370. ADD_TEST(test_session_with_only_int_cache);
  10371. ADD_TEST(test_session_with_only_ext_cache);
  10372. ADD_TEST(test_session_with_both_cache);
  10373. ADD_TEST(test_session_wo_ca_names);
  10374. #ifndef OSSL_NO_USABLE_TLS1_3
  10375. ADD_ALL_TESTS(test_stateful_tickets, 3);
  10376. ADD_ALL_TESTS(test_stateless_tickets, 3);
  10377. ADD_TEST(test_psk_tickets);
  10378. ADD_ALL_TESTS(test_extra_tickets, 6);
  10379. #endif
  10380. ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
  10381. ADD_TEST(test_ssl_bio_pop_next_bio);
  10382. ADD_TEST(test_ssl_bio_pop_ssl_bio);
  10383. ADD_TEST(test_ssl_bio_change_rbio);
  10384. ADD_TEST(test_ssl_bio_change_wbio);
  10385. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  10386. ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
  10387. ADD_TEST(test_keylog);
  10388. #endif
  10389. #ifndef OSSL_NO_USABLE_TLS1_3
  10390. ADD_TEST(test_keylog_no_master_key);
  10391. #endif
  10392. ADD_TEST(test_client_cert_verify_cb);
  10393. ADD_TEST(test_ssl_build_cert_chain);
  10394. ADD_TEST(test_ssl_ctx_build_cert_chain);
  10395. #ifndef OPENSSL_NO_TLS1_2
  10396. ADD_TEST(test_client_hello_cb);
  10397. ADD_TEST(test_no_ems);
  10398. ADD_TEST(test_ccs_change_cipher);
  10399. #endif
  10400. #ifndef OSSL_NO_USABLE_TLS1_3
  10401. ADD_ALL_TESTS(test_early_data_read_write, 6);
  10402. /*
  10403. * We don't do replay tests for external PSK. Replay protection isn't used
  10404. * in that scenario.
  10405. */
  10406. ADD_ALL_TESTS(test_early_data_replay, 2);
  10407. ADD_ALL_TESTS(test_early_data_skip, OSSL_NELEM(ciphersuites) * 3);
  10408. ADD_ALL_TESTS(test_early_data_skip_hrr, OSSL_NELEM(ciphersuites) * 3);
  10409. ADD_ALL_TESTS(test_early_data_skip_hrr_fail, OSSL_NELEM(ciphersuites) * 3);
  10410. ADD_ALL_TESTS(test_early_data_skip_abort, OSSL_NELEM(ciphersuites) * 3);
  10411. ADD_ALL_TESTS(test_early_data_not_sent, 3);
  10412. ADD_ALL_TESTS(test_early_data_psk, 8);
  10413. ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
  10414. ADD_ALL_TESTS(test_early_data_not_expected, 3);
  10415. # ifndef OPENSSL_NO_TLS1_2
  10416. ADD_ALL_TESTS(test_early_data_tls1_2, 3);
  10417. # endif
  10418. #endif
  10419. #ifndef OSSL_NO_USABLE_TLS1_3
  10420. ADD_ALL_TESTS(test_set_ciphersuite, 10);
  10421. ADD_TEST(test_ciphersuite_change);
  10422. ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
  10423. # ifdef OPENSSL_NO_PSK
  10424. ADD_ALL_TESTS(test_tls13_psk, 1);
  10425. # else
  10426. ADD_ALL_TESTS(test_tls13_psk, 4);
  10427. # endif /* OPENSSL_NO_PSK */
  10428. #ifndef OSSL_NO_USABLE_TLS1_3
  10429. ADD_ALL_TESTS(test_tls13_no_dhe_kex, 8);
  10430. #endif /* OSSL_NO_USABLE_TLS1_3 */
  10431. # ifndef OPENSSL_NO_TLS1_2
  10432. /* Test with both TLSv1.3 and 1.2 versions */
  10433. ADD_ALL_TESTS(test_key_exchange, 14);
  10434. # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
  10435. ADD_ALL_TESTS(test_negotiated_group,
  10436. 4 * (OSSL_NELEM(ecdhe_kexch_groups)
  10437. + OSSL_NELEM(ffdhe_kexch_groups)));
  10438. # endif
  10439. # else
  10440. /* Test with only TLSv1.3 versions */
  10441. ADD_ALL_TESTS(test_key_exchange, 12);
  10442. # endif
  10443. ADD_ALL_TESTS(test_custom_exts, 6);
  10444. ADD_TEST(test_stateless);
  10445. ADD_TEST(test_pha_key_update);
  10446. #else
  10447. ADD_ALL_TESTS(test_custom_exts, 3);
  10448. #endif
  10449. ADD_ALL_TESTS(test_export_key_mat, 6);
  10450. #ifndef OSSL_NO_USABLE_TLS1_3
  10451. ADD_ALL_TESTS(test_export_key_mat_early, 3);
  10452. ADD_TEST(test_key_update);
  10453. ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
  10454. ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
  10455. ADD_ALL_TESTS(test_key_update_local_in_write, 2);
  10456. ADD_ALL_TESTS(test_key_update_local_in_read, 2);
  10457. #endif
  10458. ADD_ALL_TESTS(test_ssl_clear, 8);
  10459. ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
  10460. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  10461. ADD_ALL_TESTS(test_srp, 6);
  10462. #endif
  10463. #if !defined(OPENSSL_NO_COMP_ALG)
  10464. /* Add compression case */
  10465. ADD_ALL_TESTS(test_info_callback, 8);
  10466. #else
  10467. ADD_ALL_TESTS(test_info_callback, 6);
  10468. #endif
  10469. ADD_ALL_TESTS(test_ssl_pending, 2);
  10470. ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
  10471. ADD_ALL_TESTS(test_ticket_callbacks, 20);
  10472. ADD_ALL_TESTS(test_shutdown, 7);
  10473. ADD_TEST(test_async_shutdown);
  10474. ADD_ALL_TESTS(test_incorrect_shutdown, 2);
  10475. ADD_ALL_TESTS(test_cert_cb, 6);
  10476. ADD_ALL_TESTS(test_client_cert_cb, 2);
  10477. ADD_ALL_TESTS(test_ca_names, 3);
  10478. #ifndef OPENSSL_NO_TLS1_2
  10479. ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
  10480. #endif
  10481. ADD_ALL_TESTS(test_servername, 10);
  10482. ADD_TEST(test_unknown_sigalgs_groups);
  10483. #if !defined(OPENSSL_NO_EC) \
  10484. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  10485. ADD_ALL_TESTS(test_sigalgs_available, 6);
  10486. #endif
  10487. #ifndef OPENSSL_NO_TLS1_3
  10488. ADD_ALL_TESTS(test_pluggable_group, 2);
  10489. ADD_ALL_TESTS(test_pluggable_signature, 4);
  10490. #endif
  10491. #ifndef OPENSSL_NO_TLS1_2
  10492. ADD_TEST(test_ssl_dup);
  10493. # ifndef OPENSSL_NO_DH
  10494. ADD_ALL_TESTS(test_set_tmp_dh, 11);
  10495. ADD_ALL_TESTS(test_dh_auto, 7);
  10496. # endif
  10497. #endif
  10498. #ifndef OSSL_NO_USABLE_TLS1_3
  10499. ADD_TEST(test_sni_tls13);
  10500. ADD_ALL_TESTS(test_ticket_lifetime, 2);
  10501. #endif
  10502. ADD_TEST(test_inherit_verify_param);
  10503. ADD_TEST(test_set_alpn);
  10504. ADD_TEST(test_set_verify_cert_store_ssl_ctx);
  10505. ADD_TEST(test_set_verify_cert_store_ssl);
  10506. ADD_ALL_TESTS(test_session_timeout, 1);
  10507. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  10508. ADD_ALL_TESTS(test_session_cache_overflow, 4);
  10509. #endif
  10510. ADD_TEST(test_load_dhfile);
  10511. #ifndef OSSL_NO_USABLE_TLS1_3
  10512. ADD_TEST(test_read_ahead_key_change);
  10513. ADD_ALL_TESTS(test_tls13_record_padding, 4);
  10514. #endif
  10515. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  10516. ADD_ALL_TESTS(test_serverinfo_custom, 4);
  10517. #endif
  10518. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  10519. ADD_ALL_TESTS(test_pipelining, 7);
  10520. #endif
  10521. ADD_ALL_TESTS(test_version, 6);
  10522. ADD_TEST(test_rstate_string);
  10523. ADD_ALL_TESTS(test_handshake_retry, 16);
  10524. ADD_TEST(test_data_retry);
  10525. ADD_ALL_TESTS(test_multi_resume, 5);
  10526. return 1;
  10527. err:
  10528. OPENSSL_free(cert);
  10529. OPENSSL_free(privkey);
  10530. OPENSSL_free(cert2);
  10531. OPENSSL_free(privkey2);
  10532. return 0;
  10533. }
  10534. void cleanup_tests(void)
  10535. {
  10536. # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
  10537. EVP_PKEY_free(tmp_dh_params);
  10538. #endif
  10539. OPENSSL_free(cert);
  10540. OPENSSL_free(privkey);
  10541. OPENSSL_free(cert2);
  10542. OPENSSL_free(privkey2);
  10543. OPENSSL_free(cert1024);
  10544. OPENSSL_free(privkey1024);
  10545. OPENSSL_free(cert3072);
  10546. OPENSSL_free(privkey3072);
  10547. OPENSSL_free(cert4096);
  10548. OPENSSL_free(privkey4096);
  10549. OPENSSL_free(cert8192);
  10550. OPENSSL_free(privkey8192);
  10551. bio_s_mempacket_test_free();
  10552. bio_s_always_retry_free();
  10553. bio_s_maybe_retry_free();
  10554. OSSL_PROVIDER_unload(defctxnull);
  10555. OSSL_LIB_CTX_free(libctx);
  10556. }