sslapitest.c 278 KB

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