sslapitest.c 286 KB

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