sslapitest.c 223 KB

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