sslapitest.c 251 KB

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