sslapitest.c 363 KB

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