sslapitest.c 288 KB

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