sslapitest.c 249 KB

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