1
0

quic_multistream_test.c 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997
  1. /*
  2. * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/ssl.h>
  10. #include <openssl/quic.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/lhash.h>
  13. #include <openssl/rand.h>
  14. #include "internal/quic_tserver.h"
  15. #include "internal/quic_ssl.h"
  16. #include "internal/quic_error.h"
  17. #include "internal/quic_stream_map.h"
  18. #include "internal/quic_engine.h"
  19. #include "testutil.h"
  20. #include "helpers/quictestlib.h"
  21. #if defined(OPENSSL_THREADS)
  22. # include "internal/thread_arch.h"
  23. #endif
  24. #include "internal/numbers.h" /* UINT64_C */
  25. static const char *certfile, *keyfile;
  26. #if defined(_AIX)
  27. /*
  28. * Some versions of AIX define macros for events and revents for use when
  29. * accessing pollfd structures (see Github issue #24236). That interferes
  30. * with our use of these names here. We simply undef them.
  31. */
  32. # undef revents
  33. # undef events
  34. #endif
  35. #if defined(OPENSSL_THREADS)
  36. struct child_thread_args {
  37. struct helper *h;
  38. const struct script_op *script;
  39. const char *script_name;
  40. int thread_idx;
  41. CRYPTO_THREAD *t;
  42. CRYPTO_MUTEX *m;
  43. int testresult;
  44. int done;
  45. int s_checked_out;
  46. };
  47. #endif
  48. typedef struct stream_info {
  49. const char *name;
  50. SSL *c_stream;
  51. uint64_t s_stream_id;
  52. } STREAM_INFO;
  53. DEFINE_LHASH_OF_EX(STREAM_INFO);
  54. struct helper {
  55. int s_fd;
  56. BIO *s_net_bio, *s_net_bio_own, *s_qtf_wbio, *s_qtf_wbio_own;
  57. /* The BIO_ADDR used for BIO_bind() */
  58. BIO_ADDR *s_net_bio_orig_addr;
  59. /* The resulting address, which is the one to connect to */
  60. BIO_ADDR *s_net_bio_addr;
  61. /*
  62. * When doing a blocking mode test run, s_priv always points to the TSERVER
  63. * and s is NULL when the main thread should not be touching s_priv.
  64. */
  65. QUIC_TSERVER *s, *s_priv;
  66. LHASH_OF(STREAM_INFO) *s_streams;
  67. int c_fd;
  68. BIO *c_net_bio, *c_net_bio_own;
  69. SSL_CTX *c_ctx;
  70. SSL *c_conn;
  71. LHASH_OF(STREAM_INFO) *c_streams;
  72. #if defined(OPENSSL_THREADS)
  73. struct child_thread_args *threads;
  74. size_t num_threads;
  75. CRYPTO_MUTEX *misc_m;
  76. CRYPTO_CONDVAR *misc_cv;
  77. #endif
  78. OSSL_TIME start_time;
  79. /*
  80. * This is a duration recording the amount of time we have skipped forwards
  81. * for testing purposes relative to the real ossl_time_now() clock. We add
  82. * a quantity of time to this every time we skip some time.
  83. */
  84. CRYPTO_RWLOCK *time_lock;
  85. OSSL_TIME time_slip; /* protected by time_lock */
  86. QTEST_FAULT *qtf;
  87. int init, blocking, check_spin_again;
  88. int free_order, need_injector;
  89. int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
  90. unsigned char *buf, size_t buf_len);
  91. int (*qtf_handshake_cb)(struct helper *h,
  92. unsigned char *buf, size_t buf_len);
  93. int (*qtf_datagram_cb)(struct helper *h,
  94. BIO_MSG *m, size_t stride);
  95. uint64_t inject_word0, inject_word1;
  96. uint64_t scratch0, scratch1, fail_count;
  97. #if defined(OPENSSL_THREADS)
  98. struct {
  99. CRYPTO_THREAD *t;
  100. CRYPTO_MUTEX *m;
  101. CRYPTO_CONDVAR *c;
  102. int ready, stop;
  103. } server_thread;
  104. int s_checked_out;
  105. #endif
  106. };
  107. struct helper_local {
  108. struct helper *h;
  109. LHASH_OF(STREAM_INFO) *c_streams;
  110. int thread_idx;
  111. const struct script_op *check_op;
  112. int explicit_event_handling;
  113. };
  114. struct script_op {
  115. uint32_t op;
  116. const void *arg0;
  117. size_t arg1;
  118. int (*check_func)(struct helper *h, struct helper_local *hl);
  119. const char *stream_name;
  120. uint64_t arg2;
  121. int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
  122. unsigned char *buf, size_t buf_len);
  123. int (*qtf_handshake_cb)(struct helper *h,
  124. unsigned char *buf, size_t buf_len);
  125. int (*qtf_datagram_cb)(struct helper *h,
  126. BIO_MSG *m, size_t stride);
  127. };
  128. #define OPK_END 0
  129. #define OPK_CHECK 1
  130. #define OPK_C_SET_ALPN 2
  131. #define OPK_C_CONNECT_WAIT 3
  132. #define OPK_C_WRITE 4
  133. #define OPK_S_WRITE 5
  134. #define OPK_C_READ_EXPECT 6
  135. #define OPK_S_READ_EXPECT 7
  136. #define OPK_C_EXPECT_FIN 8
  137. #define OPK_S_EXPECT_FIN 9
  138. #define OPK_C_CONCLUDE 10
  139. #define OPK_S_CONCLUDE 11
  140. #define OPK_C_DETACH 12
  141. #define OPK_C_ATTACH 13
  142. #define OPK_C_NEW_STREAM 14
  143. #define OPK_S_NEW_STREAM 15
  144. #define OPK_C_ACCEPT_STREAM_WAIT 16
  145. #define OPK_C_ACCEPT_STREAM_NONE 17
  146. #define OPK_C_FREE_STREAM 18
  147. #define OPK_C_SET_DEFAULT_STREAM_MODE 19
  148. #define OPK_C_SET_INCOMING_STREAM_POLICY 20
  149. #define OPK_C_SHUTDOWN_WAIT 21
  150. #define OPK_C_EXPECT_CONN_CLOSE_INFO 22
  151. #define OPK_S_EXPECT_CONN_CLOSE_INFO 23
  152. #define OPK_S_BIND_STREAM_ID 24
  153. #define OPK_C_WAIT_FOR_DATA 25
  154. #define OPK_C_WRITE_FAIL 26
  155. #define OPK_S_WRITE_FAIL 27
  156. #define OPK_C_READ_FAIL 28
  157. #define OPK_C_STREAM_RESET 29
  158. #define OPK_S_ACCEPT_STREAM_WAIT 30
  159. #define OPK_NEW_THREAD 31
  160. #define OPK_BEGIN_REPEAT 32
  161. #define OPK_END_REPEAT 33
  162. #define OPK_S_UNBIND_STREAM_ID 34
  163. #define OPK_C_READ_FAIL_WAIT 35
  164. #define OPK_C_CLOSE_SOCKET 36
  165. #define OPK_C_EXPECT_SSL_ERR 37
  166. #define OPK_EXPECT_ERR_REASON 38
  167. #define OPK_EXPECT_ERR_LIB 39
  168. #define OPK_SLEEP 40
  169. #define OPK_S_READ_FAIL 41
  170. #define OPK_S_SET_INJECT_PLAIN 42
  171. #define OPK_SET_INJECT_WORD 43
  172. #define OPK_C_INHIBIT_TICK 44
  173. #define OPK_C_SET_WRITE_BUF_SIZE 45
  174. #define OPK_S_SET_INJECT_HANDSHAKE 46
  175. #define OPK_S_NEW_TICKET 47
  176. #define OPK_C_SKIP_IF_UNBOUND 48
  177. #define OPK_S_SET_INJECT_DATAGRAM 49
  178. #define OPK_S_SHUTDOWN 50
  179. #define OPK_POP_ERR 51
  180. #define OPK_C_WRITE_EX2 52
  181. #define OPK_SKIP_IF_BLOCKING 53
  182. #define OPK_C_STREAM_RESET_FAIL 54
  183. #define EXPECT_CONN_CLOSE_APP (1U << 0)
  184. #define EXPECT_CONN_CLOSE_REMOTE (1U << 1)
  185. /* OPK_C_NEW_STREAM */
  186. #define ALLOW_FAIL (1U << 16)
  187. #define C_BIDI_ID(ordinal) \
  188. (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
  189. #define S_BIDI_ID(ordinal) \
  190. (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
  191. #define C_UNI_ID(ordinal) \
  192. (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
  193. #define S_UNI_ID(ordinal) \
  194. (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
  195. #define ANY_ID UINT64_MAX
  196. #define OP_END \
  197. {OPK_END}
  198. #define OP_CHECK(func, arg2) \
  199. {OPK_CHECK, NULL, 0, (func), NULL, (arg2)},
  200. #define OP_C_SET_ALPN(alpn) \
  201. {OPK_C_SET_ALPN, (alpn), 0, NULL, NULL},
  202. #define OP_C_CONNECT_WAIT() \
  203. {OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL},
  204. #define OP_C_CONNECT_WAIT_OR_FAIL() \
  205. {OPK_C_CONNECT_WAIT, NULL, 1, NULL, NULL},
  206. #define OP_C_WRITE(stream_name, buf, buf_len) \
  207. {OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name},
  208. #define OP_S_WRITE(stream_name, buf, buf_len) \
  209. {OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name},
  210. #define OP_C_READ_EXPECT(stream_name, buf, buf_len) \
  211. {OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
  212. #define OP_S_READ_EXPECT(stream_name, buf, buf_len) \
  213. {OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
  214. #define OP_C_EXPECT_FIN(stream_name) \
  215. {OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name},
  216. #define OP_S_EXPECT_FIN(stream_name) \
  217. {OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name},
  218. #define OP_C_CONCLUDE(stream_name) \
  219. {OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name},
  220. #define OP_S_CONCLUDE(stream_name) \
  221. {OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name},
  222. #define OP_C_DETACH(stream_name) \
  223. {OPK_C_DETACH, NULL, 0, NULL, #stream_name},
  224. #define OP_C_ATTACH(stream_name) \
  225. {OPK_C_ATTACH, NULL, 0, NULL, #stream_name},
  226. #define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \
  227. {OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
  228. #define OP_C_NEW_STREAM_BIDI_EX(stream_name, expect_id, flags) \
  229. {OPK_C_NEW_STREAM, NULL, (flags), NULL, #stream_name, (expect_id)},
  230. #define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \
  231. {OPK_C_NEW_STREAM, NULL, SSL_STREAM_FLAG_UNI, \
  232. NULL, #stream_name, (expect_id)},
  233. #define OP_C_NEW_STREAM_UNI_EX(stream_name, expect_id, flags) \
  234. {OPK_C_NEW_STREAM, NULL, (flags) | SSL_STREAM_FLAG_UNI, \
  235. NULL, #stream_name, (expect_id)},
  236. #define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \
  237. {OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
  238. #define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \
  239. {OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)},
  240. #define OP_C_ACCEPT_STREAM_WAIT(stream_name) \
  241. {OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
  242. #define OP_C_ACCEPT_STREAM_NONE() \
  243. {OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL},
  244. #define OP_C_FREE_STREAM(stream_name) \
  245. {OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name},
  246. #define OP_C_SET_DEFAULT_STREAM_MODE(mode) \
  247. {OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL},
  248. #define OP_C_SET_INCOMING_STREAM_POLICY(policy) \
  249. {OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL},
  250. #define OP_C_SHUTDOWN_WAIT(reason, flags) \
  251. {OPK_C_SHUTDOWN_WAIT, (reason), (flags), NULL, NULL},
  252. #define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
  253. {OPK_C_EXPECT_CONN_CLOSE_INFO, NULL, \
  254. ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \
  255. ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
  256. NULL, NULL, (ec)},
  257. #define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
  258. {OPK_S_EXPECT_CONN_CLOSE_INFO, NULL, \
  259. ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \
  260. ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
  261. NULL, NULL, (ec)},
  262. #define OP_S_BIND_STREAM_ID(stream_name, stream_id) \
  263. {OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id)},
  264. #define OP_C_WAIT_FOR_DATA(stream_name) \
  265. {OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name},
  266. #define OP_C_WRITE_FAIL(stream_name) \
  267. {OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name},
  268. #define OP_S_WRITE_FAIL(stream_name) \
  269. {OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name},
  270. #define OP_C_READ_FAIL(stream_name) \
  271. {OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name},
  272. #define OP_S_READ_FAIL(stream_name, allow_zero_len) \
  273. {OPK_S_READ_FAIL, NULL, (allow_zero_len), NULL, #stream_name},
  274. #define OP_C_STREAM_RESET(stream_name, aec) \
  275. {OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec)},
  276. #define OP_C_STREAM_RESET_FAIL(stream_name, aec) \
  277. {OPK_C_STREAM_RESET_FAIL, NULL, 0, NULL, #stream_name, (aec)},
  278. #define OP_S_ACCEPT_STREAM_WAIT(stream_name) \
  279. {OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
  280. #define OP_NEW_THREAD(num_threads, script) \
  281. {OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 },
  282. #define OP_BEGIN_REPEAT(n) \
  283. {OPK_BEGIN_REPEAT, NULL, (n)},
  284. #define OP_END_REPEAT() \
  285. {OPK_END_REPEAT},
  286. #define OP_S_UNBIND_STREAM_ID(stream_name) \
  287. {OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name},
  288. #define OP_C_READ_FAIL_WAIT(stream_name) \
  289. {OPK_C_READ_FAIL_WAIT, NULL, 0, NULL, #stream_name},
  290. #define OP_C_CLOSE_SOCKET() \
  291. {OPK_C_CLOSE_SOCKET},
  292. #define OP_C_EXPECT_SSL_ERR(stream_name, err) \
  293. {OPK_C_EXPECT_SSL_ERR, NULL, (err), NULL, #stream_name},
  294. #define OP_EXPECT_ERR_REASON(err) \
  295. {OPK_EXPECT_ERR_REASON, NULL, (err)},
  296. #define OP_EXPECT_ERR_LIB(lib) \
  297. {OPK_EXPECT_ERR_LIB, NULL, (lib)},
  298. #define OP_SLEEP(ms) \
  299. {OPK_SLEEP, NULL, 0, NULL, NULL, (ms)},
  300. #define OP_S_SET_INJECT_PLAIN(f) \
  301. {OPK_S_SET_INJECT_PLAIN, NULL, 0, NULL, NULL, 0, (f)},
  302. #define OP_SET_INJECT_WORD(w0, w1) \
  303. {OPK_SET_INJECT_WORD, NULL, (w0), NULL, NULL, (w1), NULL},
  304. #define OP_C_INHIBIT_TICK(inhibit) \
  305. {OPK_C_INHIBIT_TICK, NULL, (inhibit), NULL, NULL, 0, NULL},
  306. #define OP_C_SET_WRITE_BUF_SIZE(stream_name, size) \
  307. {OPK_C_SET_WRITE_BUF_SIZE, NULL, (size), NULL, #stream_name},
  308. #define OP_S_SET_INJECT_HANDSHAKE(f) \
  309. {OPK_S_SET_INJECT_HANDSHAKE, NULL, 0, NULL, NULL, 0, NULL, (f)},
  310. #define OP_S_NEW_TICKET() \
  311. {OPK_S_NEW_TICKET},
  312. #define OP_C_SKIP_IF_UNBOUND(stream_name, n) \
  313. {OPK_C_SKIP_IF_UNBOUND, NULL, (n), NULL, #stream_name},
  314. #define OP_S_SET_INJECT_DATAGRAM(f) \
  315. {OPK_S_SET_INJECT_DATAGRAM, NULL, 0, NULL, NULL, 0, NULL, NULL, (f)},
  316. #define OP_S_SHUTDOWN(error_code) \
  317. {OPK_S_SHUTDOWN, NULL, (error_code)},
  318. #define OP_POP_ERR() \
  319. {OPK_POP_ERR},
  320. #define OP_C_WRITE_EX2(stream_name, buf, buf_len, flags) \
  321. {OPK_C_WRITE_EX2, (buf), (buf_len), NULL, #stream_name, (flags)},
  322. #define OP_CHECK2(func, arg1, arg2) \
  323. {OPK_CHECK, NULL, (arg1), (func), NULL, (arg2)},
  324. #define OP_SKIP_IF_BLOCKING(n) \
  325. {OPK_SKIP_IF_BLOCKING, NULL, (n), NULL, 0},
  326. static OSSL_TIME get_time(void *arg)
  327. {
  328. struct helper *h = arg;
  329. OSSL_TIME t;
  330. if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock)))
  331. return ossl_time_zero();
  332. t = ossl_time_add(ossl_time_now(), h->time_slip);
  333. CRYPTO_THREAD_unlock(h->time_lock);
  334. return t;
  335. }
  336. static int skip_time_ms(struct helper *h, struct helper_local *hl)
  337. {
  338. if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock)))
  339. return 0;
  340. h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(hl->check_op->arg2));
  341. CRYPTO_THREAD_unlock(h->time_lock);
  342. return 1;
  343. }
  344. static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl);
  345. static void s_unlock(struct helper *h, struct helper_local *hl);
  346. #define ACQUIRE_S() s_lock(h, hl)
  347. #define ACQUIRE_S_NOHL() s_lock(h, NULL)
  348. static int check_rejected(struct helper *h, struct helper_local *hl)
  349. {
  350. uint64_t stream_id = hl->check_op->arg2;
  351. if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)
  352. || !ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, NULL)) {
  353. h->check_spin_again = 1;
  354. return 0;
  355. }
  356. return 1;
  357. }
  358. static int check_stream_reset(struct helper *h, struct helper_local *hl)
  359. {
  360. uint64_t stream_id = hl->check_op->arg2, aec = 0;
  361. if (!ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, &aec)) {
  362. h->check_spin_again = 1;
  363. return 0;
  364. }
  365. return TEST_uint64_t_eq(aec, 42);
  366. }
  367. static int check_stream_stopped(struct helper *h, struct helper_local *hl)
  368. {
  369. uint64_t stream_id = hl->check_op->arg2;
  370. if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)) {
  371. h->check_spin_again = 1;
  372. return 0;
  373. }
  374. return 1;
  375. }
  376. static int override_key_update(struct helper *h, struct helper_local *hl)
  377. {
  378. QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
  379. ossl_quic_channel_set_txku_threshold_override(ch, hl->check_op->arg2);
  380. return 1;
  381. }
  382. static int trigger_key_update(struct helper *h, struct helper_local *hl)
  383. {
  384. if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED)))
  385. return 0;
  386. return 1;
  387. }
  388. static int check_key_update_ge(struct helper *h, struct helper_local *hl)
  389. {
  390. QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
  391. int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch);
  392. int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch);
  393. int64_t diff = txke - rxke;
  394. /*
  395. * TXKE must always be equal to or ahead of RXKE.
  396. * It can be ahead of RXKE by at most 1.
  397. */
  398. if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1))
  399. return 0;
  400. /* Caller specifies a minimum number of RXKEs which must have happened. */
  401. if (!TEST_uint64_t_ge((uint64_t)rxke, hl->check_op->arg2))
  402. return 0;
  403. return 1;
  404. }
  405. static int check_key_update_lt(struct helper *h, struct helper_local *hl)
  406. {
  407. QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
  408. uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch);
  409. /* Caller specifies a maximum number of TXKEs which must have happened. */
  410. if (!TEST_uint64_t_lt(txke, hl->check_op->arg2))
  411. return 0;
  412. return 1;
  413. }
  414. static unsigned long stream_info_hash(const STREAM_INFO *info)
  415. {
  416. return OPENSSL_LH_strhash(info->name);
  417. }
  418. static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b)
  419. {
  420. return strcmp(a->name, b->name);
  421. }
  422. static void cleanup_stream(STREAM_INFO *info)
  423. {
  424. SSL_free(info->c_stream);
  425. OPENSSL_free(info);
  426. }
  427. static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh)
  428. {
  429. if (*lh == NULL)
  430. return;
  431. lh_STREAM_INFO_doall(*lh, cleanup_stream);
  432. lh_STREAM_INFO_free(*lh);
  433. *lh = NULL;
  434. }
  435. #if defined(OPENSSL_THREADS)
  436. static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg);
  437. static int join_threads(struct child_thread_args *threads, size_t num_threads)
  438. {
  439. int ok = 1;
  440. size_t i;
  441. CRYPTO_THREAD_RETVAL rv;
  442. for (i = 0; i < num_threads; ++i) {
  443. if (threads[i].t != NULL) {
  444. ossl_crypto_thread_native_join(threads[i].t, &rv);
  445. if (!threads[i].testresult)
  446. /* Do not log failure here, worker will do it. */
  447. ok = 0;
  448. ossl_crypto_thread_native_clean(threads[i].t);
  449. threads[i].t = NULL;
  450. }
  451. ossl_crypto_mutex_free(&threads[i].m);
  452. }
  453. return ok;
  454. }
  455. static int join_server_thread(struct helper *h)
  456. {
  457. CRYPTO_THREAD_RETVAL rv;
  458. if (h->server_thread.t == NULL)
  459. return 1;
  460. ossl_crypto_mutex_lock(h->server_thread.m);
  461. h->server_thread.stop = 1;
  462. ossl_crypto_condvar_signal(h->server_thread.c);
  463. ossl_crypto_mutex_unlock(h->server_thread.m);
  464. ossl_crypto_thread_native_join(h->server_thread.t, &rv);
  465. ossl_crypto_thread_native_clean(h->server_thread.t);
  466. h->server_thread.t = NULL;
  467. return 1;
  468. }
  469. /* Ensure the server-state lock is currently held. Idempotent. */
  470. static int *s_checked_out_p(struct helper *h, int thread_idx)
  471. {
  472. return (thread_idx < 0) ? &h->s_checked_out
  473. : &h->threads[thread_idx].s_checked_out;
  474. }
  475. static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl)
  476. {
  477. int *p_checked_out = s_checked_out_p(h, hl == NULL ? -1 : hl->thread_idx);
  478. if (h->server_thread.m == NULL || *p_checked_out)
  479. return h->s;
  480. ossl_crypto_mutex_lock(h->server_thread.m);
  481. h->s = h->s_priv;
  482. *p_checked_out = 1;
  483. return h->s;
  484. }
  485. /* Ensure the server-state lock is currently not held. Idempotent. */
  486. static void s_unlock(struct helper *h, struct helper_local *hl)
  487. {
  488. int *p_checked_out = s_checked_out_p(h, hl->thread_idx);
  489. if (h->server_thread.m == NULL || !*p_checked_out)
  490. return;
  491. *p_checked_out = 0;
  492. h->s = NULL;
  493. ossl_crypto_mutex_unlock(h->server_thread.m);
  494. }
  495. static unsigned int server_helper_thread(void *arg)
  496. {
  497. struct helper *h = arg;
  498. ossl_crypto_mutex_lock(h->server_thread.m);
  499. for (;;) {
  500. int ready, stop;
  501. ready = h->server_thread.ready;
  502. stop = h->server_thread.stop;
  503. if (stop)
  504. break;
  505. if (!ready) {
  506. ossl_crypto_condvar_wait(h->server_thread.c, h->server_thread.m);
  507. continue;
  508. }
  509. ossl_quic_tserver_tick(h->s_priv);
  510. ossl_crypto_mutex_unlock(h->server_thread.m);
  511. /*
  512. * Give the main thread an opportunity to get the mutex, which is
  513. * sometimes necessary in some script operations.
  514. */
  515. OSSL_sleep(1);
  516. ossl_crypto_mutex_lock(h->server_thread.m);
  517. }
  518. ossl_crypto_mutex_unlock(h->server_thread.m);
  519. return 1;
  520. }
  521. #else
  522. static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl)
  523. {
  524. return h->s;
  525. }
  526. static void s_unlock(struct helper *h, struct helper_local *hl)
  527. {}
  528. #endif
  529. static void helper_cleanup(struct helper *h)
  530. {
  531. #if defined(OPENSSL_THREADS)
  532. join_threads(h->threads, h->num_threads);
  533. join_server_thread(h);
  534. OPENSSL_free(h->threads);
  535. h->threads = NULL;
  536. h->num_threads = 0;
  537. #endif
  538. if (h->free_order == 0) {
  539. /* order 0: streams, then conn */
  540. helper_cleanup_streams(&h->c_streams);
  541. SSL_free(h->c_conn);
  542. h->c_conn = NULL;
  543. } else {
  544. /* order 1: conn, then streams */
  545. SSL_free(h->c_conn);
  546. h->c_conn = NULL;
  547. helper_cleanup_streams(&h->c_streams);
  548. }
  549. helper_cleanup_streams(&h->s_streams);
  550. ossl_quic_tserver_free(h->s_priv);
  551. h->s_priv = h->s = NULL;
  552. BIO_free(h->s_net_bio_own);
  553. h->s_net_bio_own = NULL;
  554. BIO_free(h->c_net_bio_own);
  555. h->c_net_bio_own = NULL;
  556. BIO_free(h->s_qtf_wbio_own);
  557. h->s_qtf_wbio_own = NULL;
  558. qtest_fault_free(h->qtf);
  559. h->qtf = NULL;
  560. if (h->s_fd >= 0) {
  561. BIO_closesocket(h->s_fd);
  562. h->s_fd = -1;
  563. }
  564. if (h->c_fd >= 0) {
  565. BIO_closesocket(h->c_fd);
  566. h->c_fd = -1;
  567. }
  568. BIO_ADDR_free(h->s_net_bio_addr);
  569. h->s_net_bio_addr = NULL;
  570. BIO_ADDR_free(h->s_net_bio_orig_addr);
  571. h->s_net_bio_orig_addr = NULL;
  572. SSL_CTX_free(h->c_ctx);
  573. h->c_ctx = NULL;
  574. CRYPTO_THREAD_lock_free(h->time_lock);
  575. h->time_lock = NULL;
  576. #if defined(OPENSSL_THREADS)
  577. ossl_crypto_mutex_free(&h->misc_m);
  578. ossl_crypto_condvar_free(&h->misc_cv);
  579. ossl_crypto_mutex_free(&h->server_thread.m);
  580. ossl_crypto_condvar_free(&h->server_thread.c);
  581. #endif
  582. }
  583. static int helper_init(struct helper *h, const char *script_name,
  584. int free_order, int blocking,
  585. int need_injector)
  586. {
  587. struct in_addr ina = {0};
  588. QUIC_TSERVER_ARGS s_args = {0};
  589. union BIO_sock_info_u info;
  590. char title[128];
  591. memset(h, 0, sizeof(*h));
  592. h->c_fd = -1;
  593. h->s_fd = -1;
  594. h->free_order = free_order;
  595. h->blocking = blocking;
  596. h->need_injector = need_injector;
  597. h->time_slip = ossl_time_zero();
  598. if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new()))
  599. goto err;
  600. if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash,
  601. stream_info_cmp)))
  602. goto err;
  603. if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash,
  604. stream_info_cmp)))
  605. goto err;
  606. ina.s_addr = htonl(0x7f000001UL);
  607. h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
  608. if (!TEST_int_ge(h->s_fd, 0))
  609. goto err;
  610. if (!TEST_true(BIO_socket_nbio(h->s_fd, 1)))
  611. goto err;
  612. if (!TEST_ptr(h->s_net_bio_orig_addr = BIO_ADDR_new())
  613. || !TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new()))
  614. goto err;
  615. if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_orig_addr, AF_INET,
  616. &ina, sizeof(ina), 0)))
  617. goto err;
  618. if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_orig_addr, 0)))
  619. goto err;
  620. info.addr = h->s_net_bio_addr;
  621. if (!TEST_true(BIO_sock_info(h->s_fd, BIO_SOCK_INFO_ADDRESS, &info)))
  622. goto err;
  623. if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0))
  624. goto err;
  625. if (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0)))
  626. goto err;
  627. if (!BIO_up_ref(h->s_net_bio))
  628. goto err;
  629. if (need_injector) {
  630. h->s_qtf_wbio = h->s_qtf_wbio_own = BIO_new(qtest_get_bio_method());
  631. if (!TEST_ptr(h->s_qtf_wbio))
  632. goto err;
  633. if (!TEST_ptr(BIO_push(h->s_qtf_wbio, h->s_net_bio)))
  634. goto err;
  635. s_args.net_wbio = h->s_qtf_wbio;
  636. } else {
  637. s_args.net_wbio = h->s_net_bio;
  638. }
  639. s_args.net_rbio = h->s_net_bio;
  640. s_args.alpn = NULL;
  641. s_args.now_cb = get_time;
  642. s_args.now_cb_arg = h;
  643. s_args.ctx = NULL;
  644. if (!TEST_ptr(h->s_priv = ossl_quic_tserver_new(&s_args, certfile, keyfile)))
  645. goto err;
  646. if (!blocking)
  647. h->s = h->s_priv;
  648. if (need_injector) {
  649. h->qtf = qtest_create_injector(h->s_priv);
  650. if (!TEST_ptr(h->qtf))
  651. goto err;
  652. BIO_set_data(h->s_qtf_wbio, h->qtf);
  653. }
  654. h->s_net_bio_own = NULL;
  655. h->s_qtf_wbio_own = NULL;
  656. h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
  657. if (!TEST_int_ge(h->c_fd, 0))
  658. goto err;
  659. if (!TEST_true(BIO_socket_nbio(h->c_fd, 1)))
  660. goto err;
  661. if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0)))
  662. goto err;
  663. if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr)))
  664. goto err;
  665. if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
  666. goto err;
  667. /* Set title for qlog purposes. */
  668. BIO_snprintf(title, sizeof(title), "quic_multistream_test: %s", script_name);
  669. if (!TEST_true(ossl_quic_set_diag_title(h->c_ctx, title)))
  670. goto err;
  671. if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx)))
  672. goto err;
  673. /* Use custom time function for virtual time skip. */
  674. if (!TEST_true(ossl_quic_conn_set_override_now_cb(h->c_conn, get_time, h)))
  675. goto err;
  676. /* Takes ownership of our reference to the BIO. */
  677. SSL_set0_rbio(h->c_conn, h->c_net_bio);
  678. h->c_net_bio_own = NULL;
  679. if (!TEST_true(BIO_up_ref(h->c_net_bio)))
  680. goto err;
  681. SSL_set0_wbio(h->c_conn, h->c_net_bio);
  682. if (!TEST_true(SSL_set_blocking_mode(h->c_conn, h->blocking)))
  683. goto err;
  684. #if defined(OPENSSL_THREADS)
  685. if (!TEST_ptr(h->misc_m = ossl_crypto_mutex_new()))
  686. goto err;
  687. if (!TEST_ptr(h->misc_cv = ossl_crypto_condvar_new()))
  688. goto err;
  689. #endif
  690. if (h->blocking) {
  691. #if defined(OPENSSL_THREADS)
  692. if (!TEST_ptr(h->server_thread.m = ossl_crypto_mutex_new()))
  693. goto err;
  694. if (!TEST_ptr(h->server_thread.c = ossl_crypto_condvar_new()))
  695. goto err;
  696. h->server_thread.t
  697. = ossl_crypto_thread_native_start(server_helper_thread, h, 1);
  698. if (!TEST_ptr(h->server_thread.t))
  699. goto err;
  700. #else
  701. TEST_error("cannot support blocking mode without threads");
  702. goto err;
  703. #endif
  704. }
  705. h->start_time = ossl_time_now();
  706. h->init = 1;
  707. return 1;
  708. err:
  709. helper_cleanup(h);
  710. return 0;
  711. }
  712. static int helper_local_init(struct helper_local *hl, struct helper *h,
  713. int thread_idx)
  714. {
  715. hl->h = h;
  716. hl->c_streams = NULL;
  717. hl->thread_idx = thread_idx;
  718. hl->explicit_event_handling = 0;
  719. if (!TEST_ptr(h))
  720. return 0;
  721. if (thread_idx < 0) {
  722. hl->c_streams = h->c_streams;
  723. } else {
  724. if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash,
  725. stream_info_cmp)))
  726. return 0;
  727. }
  728. return 1;
  729. }
  730. static void helper_local_cleanup(struct helper_local *hl)
  731. {
  732. if (hl->h == NULL)
  733. return;
  734. if (hl->thread_idx >= 0)
  735. helper_cleanup_streams(&hl->c_streams);
  736. hl->h = NULL;
  737. }
  738. static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh,
  739. const char *stream_name)
  740. {
  741. STREAM_INFO key, *info;
  742. if (!TEST_ptr(stream_name))
  743. return NULL;
  744. if (!strcmp(stream_name, "DEFAULT"))
  745. return NULL;
  746. key.name = stream_name;
  747. info = lh_STREAM_INFO_retrieve(lh, &key);
  748. if (info == NULL) {
  749. info = OPENSSL_zalloc(sizeof(*info));
  750. if (info == NULL)
  751. return NULL;
  752. info->name = stream_name;
  753. info->s_stream_id = UINT64_MAX;
  754. lh_STREAM_INFO_insert(lh, info);
  755. }
  756. return info;
  757. }
  758. static int helper_local_set_c_stream(struct helper_local *hl,
  759. const char *stream_name,
  760. SSL *c_stream)
  761. {
  762. STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name);
  763. if (info == NULL)
  764. return 0;
  765. info->c_stream = c_stream;
  766. info->s_stream_id = UINT64_MAX;
  767. return 1;
  768. }
  769. static SSL *helper_local_get_c_stream(struct helper_local *hl,
  770. const char *stream_name)
  771. {
  772. STREAM_INFO *info;
  773. if (!strcmp(stream_name, "DEFAULT"))
  774. return hl->h->c_conn;
  775. info = get_stream_info(hl->c_streams, stream_name);
  776. if (info == NULL)
  777. return NULL;
  778. return info->c_stream;
  779. }
  780. static int
  781. helper_set_s_stream(struct helper *h, const char *stream_name,
  782. uint64_t s_stream_id)
  783. {
  784. STREAM_INFO *info;
  785. if (!strcmp(stream_name, "DEFAULT"))
  786. return 0;
  787. info = get_stream_info(h->s_streams, stream_name);
  788. if (info == NULL)
  789. return 0;
  790. info->c_stream = NULL;
  791. info->s_stream_id = s_stream_id;
  792. return 1;
  793. }
  794. static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name)
  795. {
  796. STREAM_INFO *info;
  797. if (!strcmp(stream_name, "DEFAULT"))
  798. return UINT64_MAX;
  799. info = get_stream_info(h->s_streams, stream_name);
  800. if (info == NULL)
  801. return UINT64_MAX;
  802. return info->s_stream_id;
  803. }
  804. static int helper_packet_plain_listener(QTEST_FAULT *qtf, QUIC_PKT_HDR *hdr,
  805. unsigned char *buf, size_t buf_len,
  806. void *arg)
  807. {
  808. struct helper *h = arg;
  809. return h->qtf_packet_plain_cb(h, hdr, buf, buf_len);
  810. }
  811. static int helper_handshake_listener(QTEST_FAULT *fault,
  812. unsigned char *buf, size_t buf_len,
  813. void *arg)
  814. {
  815. struct helper *h = arg;
  816. return h->qtf_handshake_cb(h, buf, buf_len);
  817. }
  818. static int helper_datagram_listener(QTEST_FAULT *fault,
  819. BIO_MSG *msg, size_t stride,
  820. void *arg)
  821. {
  822. struct helper *h = arg;
  823. return h->qtf_datagram_cb(h, msg, stride);
  824. }
  825. static int is_want(SSL *s, int ret)
  826. {
  827. int ec = SSL_get_error(s, ret);
  828. return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
  829. }
  830. static int check_consistent_want(SSL *s, int ret)
  831. {
  832. int ec = SSL_get_error(s, ret);
  833. int w = SSL_want(s);
  834. int ok = TEST_true(
  835. (ec == SSL_ERROR_NONE && w == SSL_NOTHING)
  836. || (ec == SSL_ERROR_ZERO_RETURN && w == SSL_NOTHING)
  837. || (ec == SSL_ERROR_SSL && w == SSL_NOTHING)
  838. || (ec == SSL_ERROR_SYSCALL && w == SSL_NOTHING)
  839. || (ec == SSL_ERROR_WANT_READ && w == SSL_READING)
  840. || (ec == SSL_ERROR_WANT_WRITE && w == SSL_WRITING)
  841. || (ec == SSL_ERROR_WANT_CLIENT_HELLO_CB && w == SSL_CLIENT_HELLO_CB)
  842. || (ec == SSL_ERROR_WANT_X509_LOOKUP && w == SSL_X509_LOOKUP)
  843. || (ec == SSL_ERROR_WANT_RETRY_VERIFY && w == SSL_RETRY_VERIFY)
  844. );
  845. if (!ok)
  846. TEST_error("got error=%d, want=%d", ec, w);
  847. return ok;
  848. }
  849. static int run_script_worker(struct helper *h, const struct script_op *script,
  850. const char *script_name,
  851. int thread_idx)
  852. {
  853. int testresult = 0;
  854. unsigned char *tmp_buf = NULL;
  855. int connect_started = 0;
  856. size_t offset = 0;
  857. size_t op_idx = 0;
  858. const struct script_op *op = NULL;
  859. int no_advance = 0, first = 1;
  860. #if defined(OPENSSL_THREADS)
  861. int end_wait_warning = 0;
  862. #endif
  863. OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero();
  864. struct helper_local hl_, *hl = &hl_;
  865. #define REPEAT_SLOTS 8
  866. size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS];
  867. size_t repeat_stack_limit[REPEAT_SLOTS];
  868. size_t repeat_stack_len = 0;
  869. if (!TEST_true(helper_local_init(hl, h, thread_idx)))
  870. goto out;
  871. #define COMMON_SPIN_AGAIN() \
  872. { \
  873. no_advance = 1; \
  874. continue; \
  875. }
  876. #define S_SPIN_AGAIN() \
  877. { \
  878. s_lock(h, hl); \
  879. ossl_quic_tserver_tick(h->s); \
  880. COMMON_SPIN_AGAIN(); \
  881. }
  882. #define C_SPIN_AGAIN() \
  883. { \
  884. if (h->blocking) { \
  885. TEST_error("spin again in blocking mode"); \
  886. goto out; \
  887. } \
  888. COMMON_SPIN_AGAIN(); \
  889. }
  890. for (;;) {
  891. SSL *c_tgt = h->c_conn;
  892. uint64_t s_stream_id = UINT64_MAX;
  893. s_unlock(h, hl);
  894. if (no_advance) {
  895. no_advance = 0;
  896. } else {
  897. if (!first)
  898. ++op_idx;
  899. first = 0;
  900. offset = 0;
  901. op_start_time = ossl_time_now();
  902. op_deadline = ossl_time_add(op_start_time, ossl_ms2time(60000));
  903. }
  904. if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) {
  905. TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx);
  906. goto out;
  907. }
  908. op = &script[op_idx];
  909. if (op->stream_name != NULL) {
  910. c_tgt = helper_local_get_c_stream(hl, op->stream_name);
  911. if (thread_idx < 0)
  912. s_stream_id = helper_get_s_stream(h, op->stream_name);
  913. else
  914. s_stream_id = UINT64_MAX;
  915. }
  916. if (thread_idx < 0) {
  917. if (!h->blocking) {
  918. ossl_quic_tserver_tick(h->s);
  919. }
  920. #if defined(OPENSSL_THREADS)
  921. else if (h->blocking && !h->server_thread.ready) {
  922. ossl_crypto_mutex_lock(h->server_thread.m);
  923. h->server_thread.ready = 1;
  924. ossl_crypto_condvar_signal(h->server_thread.c);
  925. ossl_crypto_mutex_unlock(h->server_thread.m);
  926. }
  927. if (h->blocking)
  928. assert(h->s == NULL);
  929. #endif
  930. }
  931. if (!hl->explicit_event_handling
  932. && (thread_idx >= 0 || connect_started))
  933. SSL_handle_events(h->c_conn);
  934. if (thread_idx >= 0) {
  935. /* Only allow certain opcodes on child threads. */
  936. switch (op->op) {
  937. case OPK_END:
  938. case OPK_CHECK:
  939. case OPK_C_ACCEPT_STREAM_WAIT:
  940. case OPK_C_NEW_STREAM:
  941. case OPK_C_READ_EXPECT:
  942. case OPK_C_EXPECT_FIN:
  943. case OPK_C_WRITE:
  944. case OPK_C_WRITE_EX2:
  945. case OPK_C_CONCLUDE:
  946. case OPK_C_FREE_STREAM:
  947. case OPK_BEGIN_REPEAT:
  948. case OPK_END_REPEAT:
  949. case OPK_C_READ_FAIL_WAIT:
  950. case OPK_C_EXPECT_SSL_ERR:
  951. case OPK_EXPECT_ERR_REASON:
  952. case OPK_EXPECT_ERR_LIB:
  953. case OPK_POP_ERR:
  954. case OPK_SLEEP:
  955. break;
  956. default:
  957. TEST_error("opcode %lu not allowed on child thread",
  958. (unsigned long)op->op);
  959. goto out;
  960. }
  961. }
  962. switch (op->op) {
  963. case OPK_END:
  964. if (!TEST_size_t_eq(repeat_stack_len, 0))
  965. goto out;
  966. #if defined(OPENSSL_THREADS)
  967. if (thread_idx < 0) {
  968. int done;
  969. size_t i;
  970. for (i = 0; i < h->num_threads; ++i) {
  971. if (h->threads[i].m == NULL)
  972. continue;
  973. ossl_crypto_mutex_lock(h->threads[i].m);
  974. done = h->threads[i].done;
  975. ossl_crypto_mutex_unlock(h->threads[i].m);
  976. if (!done) {
  977. if (!end_wait_warning) {
  978. TEST_info("still waiting for other threads to finish (%zu)", i);
  979. end_wait_warning = 1;
  980. }
  981. S_SPIN_AGAIN();
  982. }
  983. }
  984. }
  985. #endif
  986. TEST_info("script \"%s\" finished on thread %d", script_name, thread_idx);
  987. testresult = 1;
  988. goto out;
  989. case OPK_BEGIN_REPEAT:
  990. if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx)))
  991. goto out;
  992. if (!TEST_size_t_gt(op->arg1, 0))
  993. goto out;
  994. repeat_stack_idx[repeat_stack_len] = op_idx + 1;
  995. repeat_stack_done[repeat_stack_len] = 0;
  996. repeat_stack_limit[repeat_stack_len] = op->arg1;
  997. ++repeat_stack_len;
  998. break;
  999. case OPK_C_SKIP_IF_UNBOUND:
  1000. if (c_tgt != NULL)
  1001. break;
  1002. op_idx += op->arg1;
  1003. break;
  1004. case OPK_SKIP_IF_BLOCKING:
  1005. if (!h->blocking)
  1006. break;
  1007. op_idx += op->arg1;
  1008. break;
  1009. case OPK_END_REPEAT:
  1010. if (!TEST_size_t_gt(repeat_stack_len, 0))
  1011. goto out;
  1012. if (++repeat_stack_done[repeat_stack_len - 1]
  1013. == repeat_stack_limit[repeat_stack_len - 1]) {
  1014. --repeat_stack_len;
  1015. } else {
  1016. op_idx = repeat_stack_idx[repeat_stack_len - 1];
  1017. no_advance = 1;
  1018. continue;
  1019. }
  1020. break;
  1021. case OPK_CHECK:
  1022. {
  1023. int ok;
  1024. hl->check_op = op;
  1025. ok = op->check_func(h, hl);
  1026. hl->check_op = NULL;
  1027. if (thread_idx < 0 && h->check_spin_again) {
  1028. h->check_spin_again = 0;
  1029. S_SPIN_AGAIN();
  1030. }
  1031. if (!TEST_true(ok))
  1032. goto out;
  1033. }
  1034. break;
  1035. case OPK_C_SET_ALPN:
  1036. {
  1037. const char *alpn = op->arg0;
  1038. size_t alpn_len = strlen(alpn);
  1039. if (!TEST_size_t_le(alpn_len, UINT8_MAX)
  1040. || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1)))
  1041. goto out;
  1042. memcpy(tmp_buf + 1, alpn, alpn_len);
  1043. tmp_buf[0] = (unsigned char)alpn_len;
  1044. /* 0 is the success case for SSL_set_alpn_protos(). */
  1045. if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf,
  1046. alpn_len + 1)))
  1047. goto out;
  1048. OPENSSL_free(tmp_buf);
  1049. tmp_buf = NULL;
  1050. }
  1051. break;
  1052. case OPK_C_CONNECT_WAIT:
  1053. {
  1054. int ret;
  1055. connect_started = 1;
  1056. ret = SSL_connect(h->c_conn);
  1057. if (!check_consistent_want(c_tgt, ret))
  1058. goto out;
  1059. if (ret != 1) {
  1060. if (!h->blocking && is_want(h->c_conn, ret))
  1061. C_SPIN_AGAIN();
  1062. if (op->arg1 == 0 && !TEST_int_eq(ret, 1))
  1063. goto out;
  1064. }
  1065. }
  1066. break;
  1067. case OPK_C_WRITE:
  1068. {
  1069. size_t bytes_written = 0;
  1070. int r;
  1071. if (!TEST_ptr(c_tgt))
  1072. goto out;
  1073. r = SSL_write_ex(c_tgt, op->arg0, op->arg1, &bytes_written);
  1074. if (!TEST_true(r)
  1075. || !check_consistent_want(c_tgt, r)
  1076. || !TEST_size_t_eq(bytes_written, op->arg1))
  1077. goto out;
  1078. }
  1079. break;
  1080. case OPK_C_WRITE_EX2:
  1081. {
  1082. size_t bytes_written = 0;
  1083. int r;
  1084. if (!TEST_ptr(c_tgt))
  1085. goto out;
  1086. r = SSL_write_ex2(c_tgt, op->arg0, op->arg1, op->arg2,
  1087. &bytes_written);
  1088. if (!TEST_true(r)
  1089. || !check_consistent_want(c_tgt, r)
  1090. || !TEST_size_t_eq(bytes_written, op->arg1))
  1091. goto out;
  1092. }
  1093. break;
  1094. case OPK_S_WRITE:
  1095. {
  1096. size_t bytes_written = 0;
  1097. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1098. goto out;
  1099. if (!TEST_true(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id,
  1100. op->arg0, op->arg1,
  1101. &bytes_written))
  1102. || !TEST_size_t_eq(bytes_written, op->arg1))
  1103. goto out;
  1104. }
  1105. break;
  1106. case OPK_C_CONCLUDE:
  1107. {
  1108. if (!TEST_true(SSL_stream_conclude(c_tgt, 0)))
  1109. goto out;
  1110. }
  1111. break;
  1112. case OPK_S_CONCLUDE:
  1113. {
  1114. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1115. goto out;
  1116. ossl_quic_tserver_conclude(ACQUIRE_S(), s_stream_id);
  1117. }
  1118. break;
  1119. case OPK_C_WAIT_FOR_DATA:
  1120. {
  1121. char buf[1];
  1122. size_t bytes_read = 0;
  1123. if (!TEST_ptr(c_tgt))
  1124. goto out;
  1125. if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read)
  1126. || bytes_read == 0)
  1127. C_SPIN_AGAIN();
  1128. }
  1129. break;
  1130. case OPK_C_READ_EXPECT:
  1131. {
  1132. size_t bytes_read = 0;
  1133. int r;
  1134. if (op->arg1 > 0 && tmp_buf == NULL
  1135. && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
  1136. goto out;
  1137. r = SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset,
  1138. &bytes_read);
  1139. if (!check_consistent_want(c_tgt, r))
  1140. goto out;
  1141. if (!r)
  1142. C_SPIN_AGAIN();
  1143. if (bytes_read + offset != op->arg1) {
  1144. offset += bytes_read;
  1145. C_SPIN_AGAIN();
  1146. }
  1147. if (op->arg1 > 0
  1148. && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
  1149. goto out;
  1150. OPENSSL_free(tmp_buf);
  1151. tmp_buf = NULL;
  1152. }
  1153. break;
  1154. case OPK_S_READ_EXPECT:
  1155. {
  1156. size_t bytes_read = 0;
  1157. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1158. goto out;
  1159. if (op->arg1 > 0 && tmp_buf == NULL
  1160. && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
  1161. goto out;
  1162. if (!TEST_true(ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id,
  1163. tmp_buf + offset,
  1164. op->arg1 - offset,
  1165. &bytes_read)))
  1166. goto out;
  1167. if (bytes_read + offset != op->arg1) {
  1168. offset += bytes_read;
  1169. S_SPIN_AGAIN();
  1170. }
  1171. if (op->arg1 > 0
  1172. && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
  1173. goto out;
  1174. OPENSSL_free(tmp_buf);
  1175. tmp_buf = NULL;
  1176. }
  1177. break;
  1178. case OPK_C_EXPECT_FIN:
  1179. {
  1180. char buf[1];
  1181. size_t bytes_read = 0;
  1182. int r;
  1183. r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
  1184. if (!check_consistent_want(c_tgt, r)
  1185. || !TEST_false(r)
  1186. || !TEST_size_t_eq(bytes_read, 0))
  1187. goto out;
  1188. if (is_want(c_tgt, 0))
  1189. C_SPIN_AGAIN();
  1190. if (!TEST_int_eq(SSL_get_error(c_tgt, 0),
  1191. SSL_ERROR_ZERO_RETURN))
  1192. goto out;
  1193. if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING))
  1194. goto out;
  1195. }
  1196. break;
  1197. case OPK_S_EXPECT_FIN:
  1198. {
  1199. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1200. goto out;
  1201. if (!ossl_quic_tserver_has_read_ended(ACQUIRE_S(), s_stream_id))
  1202. S_SPIN_AGAIN();
  1203. }
  1204. break;
  1205. case OPK_C_DETACH:
  1206. {
  1207. SSL *c_stream;
  1208. if (!TEST_ptr_null(c_tgt))
  1209. goto out; /* don't overwrite existing stream with same name */
  1210. if (!TEST_ptr(op->stream_name))
  1211. goto out;
  1212. if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn)))
  1213. goto out;
  1214. if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream)))
  1215. goto out;
  1216. }
  1217. break;
  1218. case OPK_C_ATTACH:
  1219. {
  1220. if (!TEST_ptr(c_tgt))
  1221. goto out;
  1222. if (!TEST_ptr(op->stream_name))
  1223. goto out;
  1224. if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt)))
  1225. goto out;
  1226. if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL)))
  1227. goto out;
  1228. }
  1229. break;
  1230. case OPK_C_NEW_STREAM:
  1231. {
  1232. SSL *c_stream;
  1233. uint64_t flags = op->arg1;
  1234. int allow_fail = ((flags & ALLOW_FAIL) != 0);
  1235. flags &= ~(uint64_t)ALLOW_FAIL;
  1236. if (!TEST_ptr_null(c_tgt))
  1237. goto out; /* don't overwrite existing stream with same name */
  1238. if (!TEST_ptr(op->stream_name))
  1239. goto out;
  1240. c_stream = SSL_new_stream(h->c_conn, flags);
  1241. if (!allow_fail && !TEST_ptr(c_stream))
  1242. goto out;
  1243. if (allow_fail && c_stream == NULL) {
  1244. if (!TEST_size_t_eq(ERR_GET_REASON(ERR_get_error()),
  1245. SSL_R_STREAM_COUNT_LIMITED))
  1246. goto out;
  1247. ++h->fail_count;
  1248. break;
  1249. }
  1250. if (op->arg2 != UINT64_MAX
  1251. && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream),
  1252. op->arg2))
  1253. goto out;
  1254. if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream)))
  1255. goto out;
  1256. }
  1257. break;
  1258. case OPK_S_NEW_STREAM:
  1259. {
  1260. uint64_t stream_id = UINT64_MAX;
  1261. if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
  1262. goto out; /* don't overwrite existing stream with same name */
  1263. if (!TEST_ptr(op->stream_name))
  1264. goto out;
  1265. if (!TEST_true(ossl_quic_tserver_stream_new(ACQUIRE_S(),
  1266. op->arg1 > 0,
  1267. &stream_id)))
  1268. goto out;
  1269. if (op->arg2 != UINT64_MAX
  1270. && !TEST_uint64_t_eq(stream_id, op->arg2))
  1271. goto out;
  1272. if (!TEST_true(helper_set_s_stream(h, op->stream_name,
  1273. stream_id)))
  1274. goto out;
  1275. }
  1276. break;
  1277. case OPK_C_ACCEPT_STREAM_WAIT:
  1278. {
  1279. SSL *c_stream;
  1280. if (!TEST_ptr_null(c_tgt))
  1281. goto out; /* don't overwrite existing stream with same name */
  1282. if (!TEST_ptr(op->stream_name))
  1283. goto out;
  1284. if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL)
  1285. C_SPIN_AGAIN();
  1286. if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name,
  1287. c_stream)))
  1288. goto out;
  1289. }
  1290. break;
  1291. case OPK_S_ACCEPT_STREAM_WAIT:
  1292. {
  1293. uint64_t new_stream_id;
  1294. if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
  1295. goto out;
  1296. if (!TEST_ptr(op->stream_name))
  1297. goto out;
  1298. new_stream_id = ossl_quic_tserver_pop_incoming_stream(ACQUIRE_S());
  1299. if (new_stream_id == UINT64_MAX)
  1300. S_SPIN_AGAIN();
  1301. if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id)))
  1302. goto out;
  1303. }
  1304. break;
  1305. case OPK_C_ACCEPT_STREAM_NONE:
  1306. {
  1307. SSL *c_stream;
  1308. if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn,
  1309. SSL_ACCEPT_STREAM_NO_BLOCK))) {
  1310. SSL_free(c_stream);
  1311. goto out;
  1312. }
  1313. }
  1314. break;
  1315. case OPK_C_FREE_STREAM:
  1316. {
  1317. if (!TEST_ptr(c_tgt)
  1318. || !TEST_true(!SSL_is_connection(c_tgt)))
  1319. goto out;
  1320. if (!TEST_ptr(op->stream_name))
  1321. goto out;
  1322. if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL)))
  1323. goto out;
  1324. SSL_free(c_tgt);
  1325. c_tgt = NULL;
  1326. }
  1327. break;
  1328. case OPK_C_SET_DEFAULT_STREAM_MODE:
  1329. {
  1330. if (!TEST_ptr(c_tgt))
  1331. goto out;
  1332. if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1)))
  1333. goto out;
  1334. }
  1335. break;
  1336. case OPK_C_SET_INCOMING_STREAM_POLICY:
  1337. {
  1338. if (!TEST_ptr(c_tgt))
  1339. goto out;
  1340. if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt,
  1341. op->arg1, 0)))
  1342. goto out;
  1343. }
  1344. break;
  1345. case OPK_C_SHUTDOWN_WAIT:
  1346. {
  1347. int ret;
  1348. QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
  1349. SSL_SHUTDOWN_EX_ARGS args = {0};
  1350. ossl_quic_engine_set_inhibit_tick(ossl_quic_channel_get0_engine(ch), 0);
  1351. if (!TEST_ptr(c_tgt))
  1352. goto out;
  1353. args.quic_reason = (const char *)op->arg0;
  1354. ret = SSL_shutdown_ex(c_tgt, op->arg1, &args, sizeof(args));
  1355. if (!TEST_int_ge(ret, 0))
  1356. goto out;
  1357. if (ret == 0)
  1358. C_SPIN_AGAIN();
  1359. }
  1360. break;
  1361. case OPK_S_SHUTDOWN:
  1362. {
  1363. ossl_quic_tserver_shutdown(ACQUIRE_S(), op->arg1);
  1364. }
  1365. break;
  1366. case OPK_C_EXPECT_CONN_CLOSE_INFO:
  1367. {
  1368. SSL_CONN_CLOSE_INFO cc_info = {0};
  1369. int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
  1370. int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
  1371. uint64_t error_code = op->arg2;
  1372. if (!TEST_ptr(c_tgt))
  1373. goto out;
  1374. if (h->blocking
  1375. && !TEST_true(SSL_shutdown_ex(c_tgt,
  1376. SSL_SHUTDOWN_FLAG_WAIT_PEER,
  1377. NULL, 0)))
  1378. goto out;
  1379. if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info)))
  1380. C_SPIN_AGAIN();
  1381. if (!TEST_int_eq(expect_app,
  1382. (cc_info.flags
  1383. & SSL_CONN_CLOSE_FLAG_TRANSPORT) == 0)
  1384. || !TEST_int_eq(expect_remote,
  1385. (cc_info.flags
  1386. & SSL_CONN_CLOSE_FLAG_LOCAL) == 0)
  1387. || !TEST_uint64_t_eq(error_code, cc_info.error_code)) {
  1388. TEST_info("Connection close reason: %s", cc_info.reason);
  1389. goto out;
  1390. }
  1391. }
  1392. break;
  1393. case OPK_S_EXPECT_CONN_CLOSE_INFO:
  1394. {
  1395. const QUIC_TERMINATE_CAUSE *tc;
  1396. int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
  1397. int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
  1398. uint64_t error_code = op->arg2;
  1399. if (!ossl_quic_tserver_is_term_any(ACQUIRE_S())) {
  1400. ossl_quic_tserver_ping(ACQUIRE_S());
  1401. S_SPIN_AGAIN();
  1402. }
  1403. if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S())))
  1404. goto out;
  1405. if (!TEST_uint64_t_eq(error_code, tc->error_code)
  1406. || !TEST_int_eq(expect_app, tc->app)
  1407. || !TEST_int_eq(expect_remote, tc->remote))
  1408. goto out;
  1409. }
  1410. break;
  1411. case OPK_S_BIND_STREAM_ID:
  1412. {
  1413. if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
  1414. goto out;
  1415. if (!TEST_ptr(op->stream_name))
  1416. goto out;
  1417. if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2)))
  1418. goto out;
  1419. }
  1420. break;
  1421. case OPK_S_UNBIND_STREAM_ID:
  1422. {
  1423. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1424. goto out;
  1425. if (!TEST_ptr(op->stream_name))
  1426. goto out;
  1427. if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX)))
  1428. goto out;
  1429. }
  1430. break;
  1431. case OPK_C_WRITE_FAIL:
  1432. {
  1433. size_t bytes_written = 0;
  1434. int r;
  1435. if (!TEST_ptr(c_tgt))
  1436. goto out;
  1437. r = SSL_write_ex(c_tgt, "apple", 5, &bytes_written);
  1438. if (!TEST_false(r)
  1439. || !check_consistent_want(c_tgt, r))
  1440. goto out;
  1441. }
  1442. break;
  1443. case OPK_S_WRITE_FAIL:
  1444. {
  1445. size_t bytes_written = 0;
  1446. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1447. goto out;
  1448. if (!TEST_false(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id,
  1449. (const unsigned char *)"apple", 5,
  1450. &bytes_written)))
  1451. goto out;
  1452. }
  1453. break;
  1454. case OPK_C_READ_FAIL:
  1455. {
  1456. size_t bytes_read = 0;
  1457. char buf[1];
  1458. int r;
  1459. if (!TEST_ptr(c_tgt))
  1460. goto out;
  1461. r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
  1462. if (!TEST_false(r))
  1463. goto out;
  1464. if (!check_consistent_want(c_tgt, r))
  1465. goto out;
  1466. }
  1467. break;
  1468. case OPK_C_READ_FAIL_WAIT:
  1469. {
  1470. size_t bytes_read = 0;
  1471. char buf[1];
  1472. int r;
  1473. if (!TEST_ptr(c_tgt))
  1474. goto out;
  1475. r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
  1476. if (!TEST_false(r))
  1477. goto out;
  1478. if (!check_consistent_want(c_tgt, r))
  1479. goto out;
  1480. if (is_want(c_tgt, 0))
  1481. C_SPIN_AGAIN();
  1482. }
  1483. break;
  1484. case OPK_S_READ_FAIL:
  1485. {
  1486. int ret;
  1487. size_t bytes_read = 0;
  1488. unsigned char buf[1];
  1489. if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
  1490. goto out;
  1491. ret = ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id,
  1492. buf, sizeof(buf),
  1493. &bytes_read);
  1494. if (!TEST_true(ret == 0 || (op->arg1 && bytes_read == 0)))
  1495. goto out;
  1496. }
  1497. break;
  1498. case OPK_C_STREAM_RESET:
  1499. case OPK_C_STREAM_RESET_FAIL:
  1500. {
  1501. SSL_STREAM_RESET_ARGS args = {0};
  1502. if (!TEST_ptr(c_tgt))
  1503. goto out;
  1504. args.quic_error_code = op->arg2;
  1505. if (op->op == OPK_C_STREAM_RESET) {
  1506. if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args))))
  1507. goto out;
  1508. } else {
  1509. if (!TEST_false(SSL_stream_reset(c_tgt, &args, sizeof(args))))
  1510. goto out;
  1511. }
  1512. }
  1513. break;
  1514. case OPK_NEW_THREAD:
  1515. {
  1516. #if !defined(OPENSSL_THREADS)
  1517. /*
  1518. * If this test script requires threading and we do not have
  1519. * support for it, skip the rest of it.
  1520. */
  1521. TEST_skip("threading not supported, skipping");
  1522. testresult = 1;
  1523. goto out;
  1524. #else
  1525. size_t i;
  1526. if (!TEST_ptr_null(h->threads)) {
  1527. TEST_error("max one NEW_THREAD operation per script");
  1528. goto out;
  1529. }
  1530. h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args));
  1531. if (!TEST_ptr(h->threads))
  1532. goto out;
  1533. h->num_threads = op->arg1;
  1534. for (i = 0; i < op->arg1; ++i) {
  1535. h->threads[i].h = h;
  1536. h->threads[i].script = op->arg0;
  1537. h->threads[i].script_name = script_name;
  1538. h->threads[i].thread_idx = i;
  1539. h->threads[i].m = ossl_crypto_mutex_new();
  1540. if (!TEST_ptr(h->threads[i].m))
  1541. goto out;
  1542. h->threads[i].t
  1543. = ossl_crypto_thread_native_start(run_script_child_thread,
  1544. &h->threads[i], 1);
  1545. if (!TEST_ptr(h->threads[i].t))
  1546. goto out;
  1547. }
  1548. #endif
  1549. }
  1550. break;
  1551. case OPK_C_CLOSE_SOCKET:
  1552. {
  1553. BIO_closesocket(h->c_fd);
  1554. h->c_fd = -1;
  1555. }
  1556. break;
  1557. case OPK_C_EXPECT_SSL_ERR:
  1558. {
  1559. if (!TEST_size_t_eq((size_t)SSL_get_error(c_tgt, 0), op->arg1))
  1560. goto out;
  1561. if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING))
  1562. goto out;
  1563. }
  1564. break;
  1565. case OPK_EXPECT_ERR_REASON:
  1566. {
  1567. if (!TEST_size_t_eq((size_t)ERR_GET_REASON(ERR_peek_last_error()), op->arg1))
  1568. goto out;
  1569. }
  1570. break;
  1571. case OPK_EXPECT_ERR_LIB:
  1572. {
  1573. if (!TEST_size_t_eq((size_t)ERR_GET_LIB(ERR_peek_last_error()), op->arg1))
  1574. goto out;
  1575. }
  1576. break;
  1577. case OPK_POP_ERR:
  1578. ERR_pop();
  1579. break;
  1580. case OPK_SLEEP:
  1581. {
  1582. OSSL_sleep(op->arg2);
  1583. }
  1584. break;
  1585. case OPK_S_SET_INJECT_PLAIN:
  1586. h->qtf_packet_plain_cb = op->qtf_packet_plain_cb;
  1587. if (!TEST_true(qtest_fault_set_packet_plain_listener(h->qtf,
  1588. h->qtf_packet_plain_cb != NULL ?
  1589. helper_packet_plain_listener : NULL,
  1590. h)))
  1591. goto out;
  1592. break;
  1593. case OPK_S_SET_INJECT_HANDSHAKE:
  1594. h->qtf_handshake_cb = op->qtf_handshake_cb;
  1595. if (!TEST_true(qtest_fault_set_handshake_listener(h->qtf,
  1596. h->qtf_handshake_cb != NULL ?
  1597. helper_handshake_listener : NULL,
  1598. h)))
  1599. goto out;
  1600. break;
  1601. case OPK_S_SET_INJECT_DATAGRAM:
  1602. h->qtf_datagram_cb = op->qtf_datagram_cb;
  1603. if (!TEST_true(qtest_fault_set_datagram_listener(h->qtf,
  1604. h->qtf_datagram_cb != NULL ?
  1605. helper_datagram_listener : NULL,
  1606. h)))
  1607. goto out;
  1608. break;
  1609. case OPK_SET_INJECT_WORD:
  1610. /*
  1611. * Must hold server tick lock - callbacks can be called from other
  1612. * thread when running test in blocking mode (tsan).
  1613. */
  1614. ACQUIRE_S();
  1615. h->inject_word0 = op->arg1;
  1616. h->inject_word1 = op->arg2;
  1617. break;
  1618. case OPK_C_INHIBIT_TICK:
  1619. {
  1620. QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
  1621. ossl_quic_engine_set_inhibit_tick(ossl_quic_channel_get0_engine(ch),
  1622. op->arg1);
  1623. }
  1624. break;
  1625. case OPK_C_SET_WRITE_BUF_SIZE:
  1626. if (!TEST_ptr(c_tgt))
  1627. goto out;
  1628. if (!TEST_true(ossl_quic_set_write_buffer_size(c_tgt, op->arg1)))
  1629. goto out;
  1630. break;
  1631. case OPK_S_NEW_TICKET:
  1632. if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S())))
  1633. goto out;
  1634. break;
  1635. default:
  1636. TEST_error("unknown op");
  1637. goto out;
  1638. }
  1639. }
  1640. out:
  1641. s_unlock(h, hl); /* idempotent */
  1642. if (!testresult) {
  1643. size_t i;
  1644. const QUIC_TERMINATE_CAUSE *tcause;
  1645. const char *e_str, *f_str;
  1646. TEST_error("failed in script \"%s\" at op %zu, thread %d\n",
  1647. script_name, op_idx + 1, thread_idx);
  1648. for (i = 0; i < repeat_stack_len; ++i)
  1649. TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu",
  1650. repeat_stack_done[i],
  1651. repeat_stack_limit[i],
  1652. repeat_stack_idx[i]);
  1653. ERR_print_errors_fp(stderr);
  1654. if (h->c_conn != NULL) {
  1655. SSL_CONN_CLOSE_INFO cc_info = {0};
  1656. if (SSL_get_conn_close_info(h->c_conn, &cc_info, sizeof(cc_info))) {
  1657. e_str = ossl_quic_err_to_string(cc_info.error_code);
  1658. f_str = ossl_quic_frame_type_to_string(cc_info.frame_type);
  1659. if (e_str == NULL)
  1660. e_str = "?";
  1661. if (f_str == NULL)
  1662. f_str = "?";
  1663. TEST_info("client side is closed: %llu(%s)/%llu(%s), "
  1664. "%s, %s, reason: \"%s\"",
  1665. (unsigned long long)cc_info.error_code,
  1666. e_str,
  1667. (unsigned long long)cc_info.frame_type,
  1668. f_str,
  1669. (cc_info.flags & SSL_CONN_CLOSE_FLAG_LOCAL) != 0
  1670. ? "local" : "remote",
  1671. (cc_info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0
  1672. ? "transport" : "app",
  1673. cc_info.reason != NULL ? cc_info.reason : "-");
  1674. }
  1675. }
  1676. tcause = (h->s != NULL
  1677. ? ossl_quic_tserver_get_terminate_cause(h->s) : NULL);
  1678. if (tcause != NULL) {
  1679. e_str = ossl_quic_err_to_string(tcause->error_code);
  1680. f_str = ossl_quic_frame_type_to_string(tcause->frame_type);
  1681. if (e_str == NULL)
  1682. e_str = "?";
  1683. if (f_str == NULL)
  1684. f_str = "?";
  1685. TEST_info("server side is closed: %llu(%s)/%llu(%s), "
  1686. "%s, %s, reason: \"%s\"",
  1687. (unsigned long long)tcause->error_code,
  1688. e_str,
  1689. (unsigned long long)tcause->frame_type,
  1690. f_str,
  1691. tcause->remote ? "remote" : "local",
  1692. tcause->app ? "app" : "transport",
  1693. tcause->reason != NULL ? tcause->reason : "-");
  1694. }
  1695. }
  1696. OPENSSL_free(tmp_buf);
  1697. helper_local_cleanup(hl);
  1698. return testresult;
  1699. }
  1700. static int run_script(const struct script_op *script,
  1701. const char *script_name,
  1702. int free_order,
  1703. int blocking)
  1704. {
  1705. int testresult = 0;
  1706. struct helper h;
  1707. if (!TEST_true(helper_init(&h, script_name,
  1708. free_order, blocking, 1)))
  1709. goto out;
  1710. if (!TEST_true(run_script_worker(&h, script, script_name, -1)))
  1711. goto out;
  1712. #if defined(OPENSSL_THREADS)
  1713. if (!TEST_true(join_threads(h.threads, h.num_threads)))
  1714. goto out;
  1715. #endif
  1716. testresult = 1;
  1717. out:
  1718. helper_cleanup(&h);
  1719. return testresult;
  1720. }
  1721. #if defined(OPENSSL_THREADS)
  1722. static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg)
  1723. {
  1724. int testresult;
  1725. struct child_thread_args *args = arg;
  1726. testresult = run_script_worker(args->h, args->script,
  1727. args->script_name,
  1728. args->thread_idx);
  1729. ossl_crypto_mutex_lock(args->m);
  1730. args->testresult = testresult;
  1731. args->done = 1;
  1732. ossl_crypto_mutex_unlock(args->m);
  1733. return 1;
  1734. }
  1735. #endif
  1736. /* 1. Simple single-stream test */
  1737. static const struct script_op script_1[] = {
  1738. OP_C_SET_ALPN ("ossltest")
  1739. OP_C_CONNECT_WAIT ()
  1740. OP_C_WRITE (DEFAULT, "apple", 5)
  1741. OP_C_CONCLUDE (DEFAULT)
  1742. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1743. OP_S_READ_EXPECT (a, "apple", 5)
  1744. OP_S_EXPECT_FIN (a)
  1745. OP_S_WRITE (a, "orange", 6)
  1746. OP_S_CONCLUDE (a)
  1747. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  1748. OP_C_EXPECT_FIN (DEFAULT)
  1749. OP_END
  1750. };
  1751. /* 2. Multi-stream test */
  1752. static const struct script_op script_2[] = {
  1753. OP_C_SET_ALPN ("ossltest")
  1754. OP_C_CONNECT_WAIT ()
  1755. OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
  1756. OP_C_WRITE (DEFAULT, "apple", 5)
  1757. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1758. OP_S_READ_EXPECT (a, "apple", 5)
  1759. OP_S_WRITE (a, "orange", 6)
  1760. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  1761. OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
  1762. OP_C_WRITE (b, "flamingo", 8)
  1763. OP_C_CONCLUDE (b)
  1764. OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
  1765. OP_S_READ_EXPECT (b, "flamingo", 8)
  1766. OP_S_EXPECT_FIN (b)
  1767. OP_S_WRITE (b, "gargoyle", 8)
  1768. OP_S_CONCLUDE (b)
  1769. OP_C_READ_EXPECT (b, "gargoyle", 8)
  1770. OP_C_EXPECT_FIN (b)
  1771. OP_C_NEW_STREAM_UNI (c, C_UNI_ID(0))
  1772. OP_C_WRITE (c, "elephant", 8)
  1773. OP_C_CONCLUDE (c)
  1774. OP_S_BIND_STREAM_ID (c, C_UNI_ID(0))
  1775. OP_S_READ_EXPECT (c, "elephant", 8)
  1776. OP_S_EXPECT_FIN (c)
  1777. OP_S_WRITE_FAIL (c)
  1778. OP_C_ACCEPT_STREAM_NONE ()
  1779. OP_S_NEW_STREAM_BIDI (d, S_BIDI_ID(0))
  1780. OP_S_WRITE (d, "frog", 4)
  1781. OP_S_CONCLUDE (d)
  1782. OP_C_ACCEPT_STREAM_WAIT (d)
  1783. OP_C_ACCEPT_STREAM_NONE ()
  1784. OP_C_READ_EXPECT (d, "frog", 4)
  1785. OP_C_EXPECT_FIN (d)
  1786. OP_S_NEW_STREAM_BIDI (e, S_BIDI_ID(1))
  1787. OP_S_WRITE (e, "mixture", 7)
  1788. OP_S_CONCLUDE (e)
  1789. OP_C_ACCEPT_STREAM_WAIT (e)
  1790. OP_C_READ_EXPECT (e, "mixture", 7)
  1791. OP_C_EXPECT_FIN (e)
  1792. OP_C_WRITE (e, "ramble", 6)
  1793. OP_S_READ_EXPECT (e, "ramble", 6)
  1794. OP_C_CONCLUDE (e)
  1795. OP_S_EXPECT_FIN (e)
  1796. OP_S_NEW_STREAM_UNI (f, S_UNI_ID(0))
  1797. OP_S_WRITE (f, "yonder", 6)
  1798. OP_S_CONCLUDE (f)
  1799. OP_C_ACCEPT_STREAM_WAIT (f)
  1800. OP_C_ACCEPT_STREAM_NONE ()
  1801. OP_C_READ_EXPECT (f, "yonder", 6)
  1802. OP_C_EXPECT_FIN (f)
  1803. OP_C_WRITE_FAIL (f)
  1804. OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT)
  1805. OP_S_NEW_STREAM_BIDI (g, S_BIDI_ID(2))
  1806. OP_S_WRITE (g, "unseen", 6)
  1807. OP_S_CONCLUDE (g)
  1808. OP_C_ACCEPT_STREAM_NONE ()
  1809. OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO)
  1810. OP_S_NEW_STREAM_BIDI (h, S_BIDI_ID(3))
  1811. OP_S_WRITE (h, "UNSEEN", 6)
  1812. OP_S_CONCLUDE (h)
  1813. OP_C_ACCEPT_STREAM_NONE ()
  1814. /*
  1815. * Streams g, h should have been rejected, so server should have got
  1816. * STOP_SENDING/RESET_STREAM.
  1817. */
  1818. OP_CHECK (check_rejected, S_BIDI_ID(2))
  1819. OP_CHECK (check_rejected, S_BIDI_ID(3))
  1820. OP_END
  1821. };
  1822. /* 3. Default stream detach/reattach test */
  1823. static const struct script_op script_3[] = {
  1824. OP_C_SET_ALPN ("ossltest")
  1825. OP_C_CONNECT_WAIT ()
  1826. OP_C_WRITE (DEFAULT, "apple", 5)
  1827. OP_C_DETACH (a) /* DEFAULT becomes stream 'a' */
  1828. OP_C_WRITE_FAIL (DEFAULT)
  1829. OP_C_WRITE (a, "by", 2)
  1830. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1831. OP_S_READ_EXPECT (a, "appleby", 7)
  1832. OP_S_WRITE (a, "hello", 5)
  1833. OP_C_READ_EXPECT (a, "hello", 5)
  1834. OP_C_WRITE_FAIL (DEFAULT)
  1835. OP_C_ATTACH (a)
  1836. OP_C_WRITE (DEFAULT, "is here", 7)
  1837. OP_S_READ_EXPECT (a, "is here", 7)
  1838. OP_C_DETACH (a)
  1839. OP_C_CONCLUDE (a)
  1840. OP_S_EXPECT_FIN (a)
  1841. OP_END
  1842. };
  1843. /* 4. Default stream mode test */
  1844. static const struct script_op script_4[] = {
  1845. OP_C_SET_ALPN ("ossltest")
  1846. OP_C_CONNECT_WAIT ()
  1847. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1848. OP_C_WRITE_FAIL (DEFAULT)
  1849. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  1850. OP_S_WRITE (a, "apple", 5)
  1851. OP_C_READ_FAIL (DEFAULT)
  1852. OP_C_ACCEPT_STREAM_WAIT (a)
  1853. OP_C_READ_EXPECT (a, "apple", 5)
  1854. OP_C_ATTACH (a)
  1855. OP_C_WRITE (DEFAULT, "orange", 6)
  1856. OP_S_READ_EXPECT (a, "orange", 6)
  1857. OP_END
  1858. };
  1859. /* 5. Test stream reset functionality */
  1860. static const struct script_op script_5[] = {
  1861. OP_C_SET_ALPN ("ossltest")
  1862. OP_C_CONNECT_WAIT ()
  1863. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1864. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  1865. OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
  1866. OP_C_WRITE (a, "apple", 5)
  1867. OP_C_STREAM_RESET (a, 42)
  1868. OP_C_WRITE (b, "strawberry", 10)
  1869. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1870. OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
  1871. OP_S_READ_EXPECT (b, "strawberry", 10)
  1872. /* Reset disrupts read of already sent data */
  1873. OP_S_READ_FAIL (a, 0)
  1874. OP_CHECK (check_stream_reset, C_BIDI_ID(0))
  1875. OP_END
  1876. };
  1877. /* 6. Test STOP_SENDING functionality */
  1878. static const struct script_op script_6[] = {
  1879. OP_C_SET_ALPN ("ossltest")
  1880. OP_C_CONNECT_WAIT ()
  1881. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1882. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  1883. OP_S_WRITE (a, "apple", 5)
  1884. OP_C_ACCEPT_STREAM_WAIT (a)
  1885. OP_C_FREE_STREAM (a)
  1886. OP_C_ACCEPT_STREAM_NONE ()
  1887. OP_CHECK (check_stream_stopped, S_BIDI_ID(0))
  1888. OP_END
  1889. };
  1890. /* 7. Unidirectional default stream mode test (client sends first) */
  1891. static const struct script_op script_7[] = {
  1892. OP_C_SET_ALPN ("ossltest")
  1893. OP_C_CONNECT_WAIT ()
  1894. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
  1895. OP_C_WRITE (DEFAULT, "apple", 5)
  1896. OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
  1897. OP_S_READ_EXPECT (a, "apple", 5)
  1898. OP_S_WRITE_FAIL (a)
  1899. OP_END
  1900. };
  1901. /* 8. Unidirectional default stream mode test (server sends first) */
  1902. static const struct script_op script_8[] = {
  1903. OP_C_SET_ALPN ("ossltest")
  1904. OP_C_CONNECT_WAIT ()
  1905. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
  1906. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  1907. OP_S_WRITE (a, "apple", 5)
  1908. OP_C_READ_EXPECT (DEFAULT, "apple", 5)
  1909. OP_C_WRITE_FAIL (DEFAULT)
  1910. OP_END
  1911. };
  1912. /* 9. Unidirectional default stream mode test (server sends first on bidi) */
  1913. static const struct script_op script_9[] = {
  1914. OP_C_SET_ALPN ("ossltest")
  1915. OP_C_CONNECT_WAIT ()
  1916. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
  1917. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  1918. OP_S_WRITE (a, "apple", 5)
  1919. OP_C_READ_EXPECT (DEFAULT, "apple", 5)
  1920. OP_C_WRITE (DEFAULT, "orange", 6)
  1921. OP_S_READ_EXPECT (a, "orange", 6)
  1922. OP_END
  1923. };
  1924. /* 10. Shutdown */
  1925. static const struct script_op script_10[] = {
  1926. OP_C_SET_ALPN ("ossltest")
  1927. OP_C_CONNECT_WAIT ()
  1928. OP_C_WRITE (DEFAULT, "apple", 5)
  1929. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1930. OP_S_READ_EXPECT (a, "apple", 5)
  1931. OP_C_SHUTDOWN_WAIT (NULL, 0)
  1932. OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
  1933. OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
  1934. OP_END
  1935. };
  1936. /* 11. Many threads accepted on the same client connection */
  1937. static const struct script_op script_11_child[] = {
  1938. OP_C_ACCEPT_STREAM_WAIT (a)
  1939. OP_C_READ_EXPECT (a, "foo", 3)
  1940. OP_SLEEP (10)
  1941. OP_C_EXPECT_FIN (a)
  1942. OP_END
  1943. };
  1944. static const struct script_op script_11[] = {
  1945. OP_C_SET_ALPN ("ossltest")
  1946. OP_C_CONNECT_WAIT ()
  1947. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1948. OP_NEW_THREAD (5, script_11_child)
  1949. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  1950. OP_S_WRITE (a, "foo", 3)
  1951. OP_S_CONCLUDE (a)
  1952. OP_S_NEW_STREAM_BIDI (b, ANY_ID)
  1953. OP_S_WRITE (b, "foo", 3)
  1954. OP_S_CONCLUDE (b)
  1955. OP_S_NEW_STREAM_BIDI (c, ANY_ID)
  1956. OP_S_WRITE (c, "foo", 3)
  1957. OP_S_CONCLUDE (c)
  1958. OP_S_NEW_STREAM_BIDI (d, ANY_ID)
  1959. OP_S_WRITE (d, "foo", 3)
  1960. OP_S_CONCLUDE (d)
  1961. OP_S_NEW_STREAM_BIDI (e, ANY_ID)
  1962. OP_S_WRITE (e, "foo", 3)
  1963. OP_S_CONCLUDE (e)
  1964. OP_END
  1965. };
  1966. /* 12. Many threads initiated on the same client connection */
  1967. static const struct script_op script_12_child[] = {
  1968. OP_C_NEW_STREAM_BIDI (a, ANY_ID)
  1969. OP_C_WRITE (a, "foo", 3)
  1970. OP_C_CONCLUDE (a)
  1971. OP_C_FREE_STREAM (a)
  1972. OP_END
  1973. };
  1974. static const struct script_op script_12[] = {
  1975. OP_C_SET_ALPN ("ossltest")
  1976. OP_C_CONNECT_WAIT ()
  1977. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1978. OP_NEW_THREAD (5, script_12_child)
  1979. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1980. OP_S_READ_EXPECT (a, "foo", 3)
  1981. OP_S_EXPECT_FIN (a)
  1982. OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
  1983. OP_S_READ_EXPECT (b, "foo", 3)
  1984. OP_S_EXPECT_FIN (b)
  1985. OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2))
  1986. OP_S_READ_EXPECT (c, "foo", 3)
  1987. OP_S_EXPECT_FIN (c)
  1988. OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3))
  1989. OP_S_READ_EXPECT (d, "foo", 3)
  1990. OP_S_EXPECT_FIN (d)
  1991. OP_S_BIND_STREAM_ID (e, C_BIDI_ID(4))
  1992. OP_S_READ_EXPECT (e, "foo", 3)
  1993. OP_S_EXPECT_FIN (e)
  1994. OP_END
  1995. };
  1996. /* 13. Many threads accepted on the same client connection (stress test) */
  1997. static const struct script_op script_13_child[] = {
  1998. OP_BEGIN_REPEAT (10)
  1999. OP_C_ACCEPT_STREAM_WAIT (a)
  2000. OP_C_READ_EXPECT (a, "foo", 3)
  2001. OP_C_EXPECT_FIN (a)
  2002. OP_C_FREE_STREAM (a)
  2003. OP_END_REPEAT ()
  2004. OP_END
  2005. };
  2006. static const struct script_op script_13[] = {
  2007. OP_C_SET_ALPN ("ossltest")
  2008. OP_C_CONNECT_WAIT ()
  2009. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2010. OP_NEW_THREAD (5, script_13_child)
  2011. OP_BEGIN_REPEAT (50)
  2012. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  2013. OP_S_WRITE (a, "foo", 3)
  2014. OP_S_CONCLUDE (a)
  2015. OP_S_UNBIND_STREAM_ID (a)
  2016. OP_END_REPEAT ()
  2017. OP_END
  2018. };
  2019. /* 14. Many threads initiating on the same client connection (stress test) */
  2020. static const struct script_op script_14_child[] = {
  2021. OP_BEGIN_REPEAT (10)
  2022. OP_C_NEW_STREAM_BIDI (a, ANY_ID)
  2023. OP_C_WRITE (a, "foo", 3)
  2024. OP_C_CONCLUDE (a)
  2025. OP_C_FREE_STREAM (a)
  2026. OP_END_REPEAT ()
  2027. OP_END
  2028. };
  2029. static const struct script_op script_14[] = {
  2030. OP_C_SET_ALPN ("ossltest")
  2031. OP_C_CONNECT_WAIT ()
  2032. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2033. OP_NEW_THREAD (5, script_14_child)
  2034. OP_BEGIN_REPEAT (50)
  2035. OP_S_ACCEPT_STREAM_WAIT (a)
  2036. OP_S_READ_EXPECT (a, "foo", 3)
  2037. OP_S_EXPECT_FIN (a)
  2038. OP_S_UNBIND_STREAM_ID (a)
  2039. OP_END_REPEAT ()
  2040. OP_END
  2041. };
  2042. /* 15. Client sending large number of streams, MAX_STREAMS test */
  2043. static const struct script_op script_15[] = {
  2044. OP_C_SET_ALPN ("ossltest")
  2045. OP_C_CONNECT_WAIT ()
  2046. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2047. /*
  2048. * This will cause a protocol violation to be raised by the server if we are
  2049. * not handling the stream limit correctly on the TX side.
  2050. */
  2051. OP_BEGIN_REPEAT (200)
  2052. OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, SSL_STREAM_FLAG_ADVANCE)
  2053. OP_C_WRITE (a, "foo", 3)
  2054. OP_C_CONCLUDE (a)
  2055. OP_C_FREE_STREAM (a)
  2056. OP_END_REPEAT ()
  2057. /* Prove the connection is still good. */
  2058. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  2059. OP_S_WRITE (a, "bar", 3)
  2060. OP_S_CONCLUDE (a)
  2061. OP_C_ACCEPT_STREAM_WAIT (a)
  2062. OP_C_READ_EXPECT (a, "bar", 3)
  2063. OP_C_EXPECT_FIN (a)
  2064. /*
  2065. * Drain the queue of incoming streams. We should be able to get all 200
  2066. * even though only 100 can be initiated at a time.
  2067. */
  2068. OP_BEGIN_REPEAT (200)
  2069. OP_S_ACCEPT_STREAM_WAIT (b)
  2070. OP_S_READ_EXPECT (b, "foo", 3)
  2071. OP_S_EXPECT_FIN (b)
  2072. OP_S_UNBIND_STREAM_ID (b)
  2073. OP_END_REPEAT ()
  2074. OP_END
  2075. };
  2076. /* 16. Server sending large number of streams, MAX_STREAMS test */
  2077. static const struct script_op script_16[] = {
  2078. OP_C_SET_ALPN ("ossltest")
  2079. OP_C_CONNECT_WAIT ()
  2080. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2081. /*
  2082. * This will cause a protocol violation to be raised by the client if we are
  2083. * not handling the stream limit correctly on the TX side.
  2084. */
  2085. OP_BEGIN_REPEAT (200)
  2086. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  2087. OP_S_WRITE (a, "foo", 3)
  2088. OP_S_CONCLUDE (a)
  2089. OP_S_UNBIND_STREAM_ID (a)
  2090. OP_END_REPEAT ()
  2091. /* Prove that the connection is still good. */
  2092. OP_C_NEW_STREAM_BIDI (a, ANY_ID)
  2093. OP_C_WRITE (a, "bar", 3)
  2094. OP_C_CONCLUDE (a)
  2095. OP_S_ACCEPT_STREAM_WAIT (b)
  2096. OP_S_READ_EXPECT (b, "bar", 3)
  2097. OP_S_EXPECT_FIN (b)
  2098. /* Drain the queue of incoming streams. */
  2099. OP_BEGIN_REPEAT (200)
  2100. OP_C_ACCEPT_STREAM_WAIT (b)
  2101. OP_C_READ_EXPECT (b, "foo", 3)
  2102. OP_C_EXPECT_FIN (b)
  2103. OP_C_FREE_STREAM (b)
  2104. OP_END_REPEAT ()
  2105. OP_END
  2106. };
  2107. /* 17. Key update test - unlimited */
  2108. static const struct script_op script_17[] = {
  2109. OP_C_SET_ALPN ("ossltest")
  2110. OP_C_CONNECT_WAIT ()
  2111. OP_C_WRITE (DEFAULT, "apple", 5)
  2112. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2113. OP_S_READ_EXPECT (a, "apple", 5)
  2114. OP_CHECK (override_key_update, 1)
  2115. OP_BEGIN_REPEAT (200)
  2116. OP_C_WRITE (DEFAULT, "apple", 5)
  2117. OP_S_READ_EXPECT (a, "apple", 5)
  2118. /*
  2119. * TXKU frequency is bounded by RTT because a previous TXKU needs to be
  2120. * acknowledged by the peer first before another one can be begin. By
  2121. * waiting this long, we eliminate any such concern and ensure as many key
  2122. * updates as possible can occur for the purposes of this test.
  2123. */
  2124. OP_CHECK (skip_time_ms, 100)
  2125. OP_END_REPEAT ()
  2126. /* At least 5 RXKUs detected */
  2127. OP_CHECK (check_key_update_ge, 5)
  2128. /*
  2129. * Prove the connection is still healthy by sending something in both
  2130. * directions.
  2131. */
  2132. OP_C_WRITE (DEFAULT, "xyzzy", 5)
  2133. OP_S_READ_EXPECT (a, "xyzzy", 5)
  2134. OP_S_WRITE (a, "plugh", 5)
  2135. OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
  2136. OP_END
  2137. };
  2138. /* 18. Key update test - RTT-bounded */
  2139. static const struct script_op script_18[] = {
  2140. OP_C_SET_ALPN ("ossltest")
  2141. OP_C_CONNECT_WAIT ()
  2142. OP_C_WRITE (DEFAULT, "apple", 5)
  2143. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2144. OP_S_READ_EXPECT (a, "apple", 5)
  2145. OP_CHECK (override_key_update, 1)
  2146. OP_BEGIN_REPEAT (200)
  2147. OP_C_WRITE (DEFAULT, "apple", 5)
  2148. OP_S_READ_EXPECT (a, "apple", 5)
  2149. OP_CHECK (skip_time_ms, 8)
  2150. OP_END_REPEAT ()
  2151. /*
  2152. * This time we simulate far less time passing between writes, so there are
  2153. * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every
  2154. * 1 packet above, which is absurd; thus this ensures we only actually
  2155. * generate TXKUs when we are allowed to.
  2156. */
  2157. OP_CHECK (check_key_update_lt, 240)
  2158. /*
  2159. * Prove the connection is still healthy by sending something in both
  2160. * directions.
  2161. */
  2162. OP_C_WRITE (DEFAULT, "xyzzy", 5)
  2163. OP_S_READ_EXPECT (a, "xyzzy", 5)
  2164. OP_S_WRITE (a, "plugh", 5)
  2165. OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
  2166. OP_END
  2167. };
  2168. /* 19. Key update test - artificially triggered */
  2169. static const struct script_op script_19[] = {
  2170. OP_C_SET_ALPN ("ossltest")
  2171. OP_C_CONNECT_WAIT ()
  2172. OP_C_WRITE (DEFAULT, "apple", 5)
  2173. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2174. OP_S_READ_EXPECT (a, "apple", 5)
  2175. OP_C_WRITE (DEFAULT, "orange", 6)
  2176. OP_S_READ_EXPECT (a, "orange", 6)
  2177. OP_S_WRITE (a, "strawberry", 10)
  2178. OP_C_READ_EXPECT (DEFAULT, "strawberry", 10)
  2179. OP_CHECK (check_key_update_lt, 1)
  2180. OP_CHECK (trigger_key_update, 0)
  2181. OP_C_WRITE (DEFAULT, "orange", 6)
  2182. OP_S_READ_EXPECT (a, "orange", 6)
  2183. OP_S_WRITE (a, "ok", 2)
  2184. OP_C_READ_EXPECT (DEFAULT, "ok", 2)
  2185. OP_CHECK (check_key_update_ge, 1)
  2186. OP_END
  2187. };
  2188. /* 20. Multiple threads accept stream with socket forcibly closed (error test) */
  2189. static int script_20_trigger(struct helper *h, volatile uint64_t *counter)
  2190. {
  2191. #if defined(OPENSSL_THREADS)
  2192. ossl_crypto_mutex_lock(h->misc_m);
  2193. ++*counter;
  2194. ossl_crypto_condvar_broadcast(h->misc_cv);
  2195. ossl_crypto_mutex_unlock(h->misc_m);
  2196. #endif
  2197. return 1;
  2198. }
  2199. static int script_20_wait(struct helper *h, volatile uint64_t *counter, uint64_t threshold)
  2200. {
  2201. #if defined(OPENSSL_THREADS)
  2202. int stop = 0;
  2203. ossl_crypto_mutex_lock(h->misc_m);
  2204. while (!stop) {
  2205. stop = (*counter >= threshold);
  2206. if (stop)
  2207. break;
  2208. ossl_crypto_condvar_wait(h->misc_cv, h->misc_m);
  2209. }
  2210. ossl_crypto_mutex_unlock(h->misc_m);
  2211. #endif
  2212. return 1;
  2213. }
  2214. static int script_20_trigger1(struct helper *h, struct helper_local *hl)
  2215. {
  2216. return script_20_trigger(h, &h->scratch0);
  2217. }
  2218. static int script_20_wait1(struct helper *h, struct helper_local *hl)
  2219. {
  2220. return script_20_wait(h, &h->scratch0, hl->check_op->arg2);
  2221. }
  2222. static int script_20_trigger2(struct helper *h, struct helper_local *hl)
  2223. {
  2224. return script_20_trigger(h, &h->scratch1);
  2225. }
  2226. static int script_20_wait2(struct helper *h, struct helper_local *hl)
  2227. {
  2228. return script_20_wait(h, &h->scratch1, hl->check_op->arg2);
  2229. }
  2230. static const struct script_op script_20_child[] = {
  2231. OP_C_ACCEPT_STREAM_WAIT (a)
  2232. OP_C_READ_EXPECT (a, "foo", 3)
  2233. OP_CHECK (script_20_trigger1, 0)
  2234. OP_CHECK (script_20_wait2, 1)
  2235. OP_C_READ_FAIL_WAIT (a)
  2236. OP_C_EXPECT_SSL_ERR (a, SSL_ERROR_SYSCALL)
  2237. OP_EXPECT_ERR_LIB (ERR_LIB_SSL)
  2238. OP_EXPECT_ERR_REASON (SSL_R_PROTOCOL_IS_SHUTDOWN)
  2239. OP_POP_ERR ()
  2240. OP_EXPECT_ERR_LIB (ERR_LIB_SSL)
  2241. OP_EXPECT_ERR_REASON (SSL_R_QUIC_NETWORK_ERROR)
  2242. OP_C_FREE_STREAM (a)
  2243. OP_END
  2244. };
  2245. static const struct script_op script_20[] = {
  2246. OP_C_SET_ALPN ("ossltest")
  2247. OP_C_CONNECT_WAIT ()
  2248. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2249. OP_NEW_THREAD (5, script_20_child)
  2250. OP_BEGIN_REPEAT (5)
  2251. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  2252. OP_S_WRITE (a, "foo", 3)
  2253. OP_S_UNBIND_STREAM_ID (a)
  2254. OP_END_REPEAT ()
  2255. OP_CHECK (script_20_wait1, 5)
  2256. OP_C_CLOSE_SOCKET ()
  2257. OP_CHECK (script_20_trigger2, 0)
  2258. OP_END
  2259. };
  2260. /* 21. Fault injection - unknown frame in 1-RTT packet */
  2261. static int script_21_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2262. unsigned char *buf, size_t len)
  2263. {
  2264. int ok = 0;
  2265. WPACKET wpkt;
  2266. unsigned char frame_buf[9];
  2267. size_t written;
  2268. if (h->inject_word0 == 0 || hdr->type != h->inject_word0)
  2269. return 1;
  2270. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2271. sizeof(frame_buf), 0)))
  2272. return 0;
  2273. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
  2274. goto err;
  2275. switch (h->inject_word1) {
  2276. case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
  2277. case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
  2278. if (!TEST_true(WPACKET_put_bytes_u64(&wpkt, (uint64_t)0)))
  2279. goto err;
  2280. break;
  2281. case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
  2282. case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
  2283. case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
  2284. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
  2285. goto err;
  2286. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, (uint64_t)0)))
  2287. goto err;
  2288. break;
  2289. }
  2290. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2291. goto err;
  2292. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2293. goto err;
  2294. ok = 1;
  2295. err:
  2296. if (ok)
  2297. WPACKET_finish(&wpkt);
  2298. else
  2299. WPACKET_cleanup(&wpkt);
  2300. return ok;
  2301. }
  2302. static const struct script_op script_21[] = {
  2303. OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
  2304. OP_C_SET_ALPN ("ossltest")
  2305. OP_C_CONNECT_WAIT ()
  2306. OP_C_WRITE (DEFAULT, "apple", 5)
  2307. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2308. OP_S_READ_EXPECT (a, "apple", 5)
  2309. OP_SET_INJECT_WORD (QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX)
  2310. OP_S_WRITE (a, "orange", 6)
  2311. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2312. OP_END
  2313. };
  2314. /* 22. Fault injection - non-zero packet header reserved bits */
  2315. static int script_22_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2316. unsigned char *buf, size_t len)
  2317. {
  2318. if (h->inject_word0 == 0)
  2319. return 1;
  2320. hdr->reserved = 1;
  2321. return 1;
  2322. }
  2323. static const struct script_op script_22[] = {
  2324. OP_S_SET_INJECT_PLAIN (script_22_inject_plain)
  2325. OP_C_SET_ALPN ("ossltest")
  2326. OP_C_CONNECT_WAIT ()
  2327. OP_C_WRITE (DEFAULT, "apple", 5)
  2328. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2329. OP_S_READ_EXPECT (a, "apple", 5)
  2330. OP_SET_INJECT_WORD (1, 0)
  2331. OP_S_WRITE (a, "orange", 6)
  2332. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION,0,0)
  2333. OP_END
  2334. };
  2335. /* 23. Fault injection - empty NEW_TOKEN */
  2336. static int script_23_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2337. unsigned char *buf, size_t len)
  2338. {
  2339. int ok = 0;
  2340. WPACKET wpkt;
  2341. unsigned char frame_buf[16];
  2342. size_t written;
  2343. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2344. return 1;
  2345. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2346. sizeof(frame_buf), 0)))
  2347. return 0;
  2348. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN))
  2349. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
  2350. goto err;
  2351. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2352. goto err;
  2353. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2354. goto err;
  2355. ok = 1;
  2356. err:
  2357. if (ok)
  2358. WPACKET_finish(&wpkt);
  2359. else
  2360. WPACKET_cleanup(&wpkt);
  2361. return ok;
  2362. }
  2363. static const struct script_op script_23[] = {
  2364. OP_S_SET_INJECT_PLAIN (script_23_inject_plain)
  2365. OP_C_SET_ALPN ("ossltest")
  2366. OP_C_CONNECT_WAIT ()
  2367. OP_C_WRITE (DEFAULT, "apple", 5)
  2368. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2369. OP_S_READ_EXPECT (a, "apple", 5)
  2370. OP_SET_INJECT_WORD (1, 0)
  2371. OP_S_WRITE (a, "orange", 6)
  2372. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2373. OP_END
  2374. };
  2375. /* 24. Fault injection - excess value of MAX_STREAMS_BIDI */
  2376. static int script_24_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2377. unsigned char *buf, size_t len)
  2378. {
  2379. int ok = 0;
  2380. WPACKET wpkt;
  2381. unsigned char frame_buf[16];
  2382. size_t written;
  2383. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2384. return 1;
  2385. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2386. sizeof(frame_buf), 0)))
  2387. return 0;
  2388. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2389. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, (((uint64_t)1) << 60) + 1)))
  2390. goto err;
  2391. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2392. goto err;
  2393. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2394. goto err;
  2395. ok = 1;
  2396. err:
  2397. if (ok)
  2398. WPACKET_finish(&wpkt);
  2399. else
  2400. WPACKET_cleanup(&wpkt);
  2401. return ok;
  2402. }
  2403. static const struct script_op script_24[] = {
  2404. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2405. OP_C_SET_ALPN ("ossltest")
  2406. OP_C_CONNECT_WAIT ()
  2407. OP_C_WRITE (DEFAULT, "apple", 5)
  2408. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2409. OP_S_READ_EXPECT (a, "apple", 5)
  2410. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
  2411. OP_S_WRITE (a, "orange", 6)
  2412. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2413. OP_END
  2414. };
  2415. /* 25. Fault injection - excess value of MAX_STREAMS_UNI */
  2416. static const struct script_op script_25[] = {
  2417. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2418. OP_C_SET_ALPN ("ossltest")
  2419. OP_C_CONNECT_WAIT ()
  2420. OP_C_WRITE (DEFAULT, "apple", 5)
  2421. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2422. OP_S_READ_EXPECT (a, "apple", 5)
  2423. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)
  2424. OP_S_WRITE (a, "orange", 6)
  2425. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2426. OP_END
  2427. };
  2428. /* 26. Fault injection - excess value of STREAMS_BLOCKED_BIDI */
  2429. static const struct script_op script_26[] = {
  2430. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2431. OP_C_SET_ALPN ("ossltest")
  2432. OP_C_CONNECT_WAIT ()
  2433. OP_C_WRITE (DEFAULT, "apple", 5)
  2434. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2435. OP_S_READ_EXPECT (a, "apple", 5)
  2436. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  2437. OP_S_WRITE (a, "orange", 6)
  2438. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  2439. OP_END
  2440. };
  2441. /* 27. Fault injection - excess value of STREAMS_BLOCKED_UNI */
  2442. static const struct script_op script_27[] = {
  2443. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2444. OP_C_SET_ALPN ("ossltest")
  2445. OP_C_CONNECT_WAIT ()
  2446. OP_C_WRITE (DEFAULT, "apple", 5)
  2447. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2448. OP_S_READ_EXPECT (a, "apple", 5)
  2449. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
  2450. OP_S_WRITE (a, "orange", 6)
  2451. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  2452. OP_END
  2453. };
  2454. /* 28. Fault injection - received RESET_STREAM for send-only stream */
  2455. static int script_28_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2456. unsigned char *buf, size_t len)
  2457. {
  2458. int ok = 0;
  2459. WPACKET wpkt;
  2460. unsigned char frame_buf[32];
  2461. size_t written;
  2462. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2463. return 1;
  2464. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2465. sizeof(frame_buf), 0)))
  2466. return 0;
  2467. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2468. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  2469. h->inject_word0 - 1))
  2470. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
  2471. || (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
  2472. && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 5)))) /* final size */
  2473. goto err;
  2474. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2475. goto err;
  2476. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2477. goto err;
  2478. ok = 1;
  2479. err:
  2480. if (ok)
  2481. WPACKET_finish(&wpkt);
  2482. else
  2483. WPACKET_cleanup(&wpkt);
  2484. return ok;
  2485. }
  2486. static const struct script_op script_28[] = {
  2487. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2488. OP_C_SET_ALPN ("ossltest")
  2489. OP_C_CONNECT_WAIT ()
  2490. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2491. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2492. OP_C_WRITE (a, "orange", 6)
  2493. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2494. OP_S_READ_EXPECT (a, "orange", 6)
  2495. OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
  2496. OP_C_WRITE (b, "apple", 5)
  2497. OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
  2498. OP_S_READ_EXPECT (b, "apple", 5)
  2499. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  2500. OP_S_WRITE (a, "fruit", 5)
  2501. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2502. OP_END
  2503. };
  2504. /* 29. Fault injection - received RESET_STREAM for nonexistent send-only stream */
  2505. static const struct script_op script_29[] = {
  2506. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2507. OP_C_SET_ALPN ("ossltest")
  2508. OP_C_CONNECT_WAIT ()
  2509. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2510. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2511. OP_C_WRITE (a, "orange", 6)
  2512. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2513. OP_S_READ_EXPECT (a, "orange", 6)
  2514. OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
  2515. OP_C_WRITE (b, "apple", 5)
  2516. OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
  2517. OP_S_READ_EXPECT (b, "apple", 5)
  2518. OP_SET_INJECT_WORD (C_UNI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  2519. OP_S_WRITE (a, "fruit", 5)
  2520. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2521. OP_END
  2522. };
  2523. /* 30. Fault injection - received STOP_SENDING for receive-only stream */
  2524. static const struct script_op script_30[] = {
  2525. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2526. OP_C_SET_ALPN ("ossltest")
  2527. OP_C_CONNECT_WAIT ()
  2528. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2529. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2530. OP_S_WRITE (a, "apple", 5)
  2531. OP_C_ACCEPT_STREAM_WAIT (a)
  2532. OP_C_READ_EXPECT (a, "apple", 5)
  2533. OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  2534. OP_S_WRITE (a, "orange", 6)
  2535. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2536. OP_END
  2537. };
  2538. /* 31. Fault injection - received STOP_SENDING for nonexistent receive-only stream */
  2539. static const struct script_op script_31[] = {
  2540. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2541. OP_C_SET_ALPN ("ossltest")
  2542. OP_C_CONNECT_WAIT ()
  2543. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2544. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2545. OP_S_WRITE (a, "apple", 5)
  2546. OP_C_ACCEPT_STREAM_WAIT (a)
  2547. OP_C_READ_EXPECT (a, "apple", 5)
  2548. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  2549. OP_S_WRITE (a, "orange", 6)
  2550. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2551. OP_END
  2552. };
  2553. /* 32. Fault injection - STREAM frame for nonexistent stream */
  2554. static int script_32_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2555. unsigned char *buf, size_t len)
  2556. {
  2557. int ok = 0;
  2558. WPACKET wpkt;
  2559. unsigned char frame_buf[64];
  2560. size_t written;
  2561. uint64_t type = OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN, offset, flen, i;
  2562. if (hdr->type != QUIC_PKT_TYPE_1RTT)
  2563. return 1;
  2564. switch (h->inject_word1) {
  2565. default:
  2566. return 0;
  2567. case 0:
  2568. return 1;
  2569. case 1:
  2570. offset = 0;
  2571. flen = 0;
  2572. break;
  2573. case 2:
  2574. offset = (((uint64_t)1)<<62) - 1;
  2575. flen = 5;
  2576. break;
  2577. case 3:
  2578. offset = 1 * 1024 * 1024 * 1024; /* 1G */
  2579. flen = 5;
  2580. break;
  2581. case 4:
  2582. offset = 0;
  2583. flen = 1;
  2584. break;
  2585. }
  2586. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2587. sizeof(frame_buf), 0)))
  2588. return 0;
  2589. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
  2590. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  2591. h->inject_word0 - 1))
  2592. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
  2593. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, flen)))
  2594. goto err;
  2595. for (i = 0; i < flen; ++i)
  2596. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  2597. goto err;
  2598. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2599. goto err;
  2600. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2601. goto err;
  2602. ok = 1;
  2603. err:
  2604. if (ok)
  2605. WPACKET_finish(&wpkt);
  2606. else
  2607. WPACKET_cleanup(&wpkt);
  2608. return ok;
  2609. }
  2610. static const struct script_op script_32[] = {
  2611. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  2612. OP_C_SET_ALPN ("ossltest")
  2613. OP_C_CONNECT_WAIT ()
  2614. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2615. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2616. OP_S_WRITE (a, "apple", 5)
  2617. OP_C_ACCEPT_STREAM_WAIT (a)
  2618. OP_C_READ_EXPECT (a, "apple", 5)
  2619. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, 1)
  2620. OP_S_WRITE (a, "orange", 6)
  2621. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2622. OP_END
  2623. };
  2624. /* 33. Fault injection - STREAM frame with illegal offset */
  2625. static const struct script_op script_33[] = {
  2626. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  2627. OP_C_SET_ALPN ("ossltest")
  2628. OP_C_CONNECT_WAIT ()
  2629. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2630. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2631. OP_C_WRITE (a, "apple", 5)
  2632. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2633. OP_S_READ_EXPECT (a, "apple", 5)
  2634. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 2)
  2635. OP_S_WRITE (a, "orange", 6)
  2636. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2637. OP_END
  2638. };
  2639. /* 34. Fault injection - STREAM frame which exceeds FC */
  2640. static const struct script_op script_34[] = {
  2641. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  2642. OP_C_SET_ALPN ("ossltest")
  2643. OP_C_CONNECT_WAIT ()
  2644. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2645. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2646. OP_C_WRITE (a, "apple", 5)
  2647. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2648. OP_S_READ_EXPECT (a, "apple", 5)
  2649. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 3)
  2650. OP_S_WRITE (a, "orange", 6)
  2651. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR,0,0)
  2652. OP_END
  2653. };
  2654. /* 35. Fault injection - MAX_STREAM_DATA for receive-only stream */
  2655. static const struct script_op script_35[] = {
  2656. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2657. OP_C_SET_ALPN ("ossltest")
  2658. OP_C_CONNECT_WAIT ()
  2659. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2660. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2661. OP_S_WRITE (a, "apple", 5)
  2662. OP_C_ACCEPT_STREAM_WAIT (a)
  2663. OP_C_READ_EXPECT (a, "apple", 5)
  2664. OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  2665. OP_S_WRITE (a, "orange", 6)
  2666. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2667. OP_END
  2668. };
  2669. /* 36. Fault injection - MAX_STREAM_DATA for nonexistent stream */
  2670. static const struct script_op script_36[] = {
  2671. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2672. OP_C_SET_ALPN ("ossltest")
  2673. OP_C_CONNECT_WAIT ()
  2674. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2675. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2676. OP_S_WRITE (a, "apple", 5)
  2677. OP_C_ACCEPT_STREAM_WAIT (a)
  2678. OP_C_READ_EXPECT (a, "apple", 5)
  2679. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  2680. OP_S_WRITE (a, "orange", 6)
  2681. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2682. OP_END
  2683. };
  2684. /* 37. Fault injection - STREAM_DATA_BLOCKED for send-only stream */
  2685. static const struct script_op script_37[] = {
  2686. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2687. OP_C_SET_ALPN ("ossltest")
  2688. OP_C_CONNECT_WAIT ()
  2689. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2690. OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0))
  2691. OP_C_WRITE (a, "apple", 5)
  2692. OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
  2693. OP_S_READ_EXPECT (a, "apple", 5)
  2694. OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
  2695. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  2696. OP_S_WRITE (b, "orange", 5)
  2697. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2698. OP_END
  2699. };
  2700. /* 38. Fault injection - STREAM_DATA_BLOCKED for non-existent stream */
  2701. static const struct script_op script_38[] = {
  2702. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2703. OP_C_SET_ALPN ("ossltest")
  2704. OP_C_CONNECT_WAIT ()
  2705. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2706. OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0))
  2707. OP_C_WRITE (a, "apple", 5)
  2708. OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
  2709. OP_S_READ_EXPECT (a, "apple", 5)
  2710. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  2711. OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
  2712. OP_S_WRITE (b, "orange", 5)
  2713. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2714. OP_END
  2715. };
  2716. /* 39. Fault injection - NEW_CONN_ID with zero-len CID */
  2717. static int script_39_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2718. unsigned char *buf, size_t len)
  2719. {
  2720. int ok = 0;
  2721. WPACKET wpkt;
  2722. unsigned char frame_buf[64];
  2723. size_t i, written;
  2724. uint64_t seq_no = 0, retire_prior_to = 0;
  2725. QUIC_CONN_ID new_cid = {0};
  2726. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(h->s_priv);
  2727. if (hdr->type != QUIC_PKT_TYPE_1RTT)
  2728. return 1;
  2729. switch (h->inject_word1) {
  2730. case 0:
  2731. return 1;
  2732. case 1:
  2733. new_cid.id_len = 0;
  2734. break;
  2735. case 2:
  2736. new_cid.id_len = 21;
  2737. break;
  2738. case 3:
  2739. new_cid.id_len = 1;
  2740. new_cid.id[0] = 0x55;
  2741. seq_no = 0;
  2742. retire_prior_to = 1;
  2743. break;
  2744. case 4:
  2745. /* Use our actual CID so we don't break connectivity. */
  2746. ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
  2747. seq_no = 2;
  2748. retire_prior_to = 2;
  2749. break;
  2750. case 5:
  2751. /*
  2752. * Use a bogus CID which will need to be ignored if connectivity is to
  2753. * be continued.
  2754. */
  2755. new_cid.id_len = 8;
  2756. new_cid.id[0] = 0x55;
  2757. seq_no = 1;
  2758. retire_prior_to = 1;
  2759. break;
  2760. }
  2761. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2762. sizeof(frame_buf), 0)))
  2763. return 0;
  2764. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID))
  2765. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, seq_no)) /* seq no */
  2766. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, retire_prior_to)) /* retire prior to */
  2767. || !TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id_len))) /* len */
  2768. goto err;
  2769. for (i = 0; i < new_cid.id_len && i < OSSL_NELEM(new_cid.id); ++i)
  2770. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id[i])))
  2771. goto err;
  2772. for (; i < new_cid.id_len; ++i)
  2773. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x55)))
  2774. goto err;
  2775. for (i = 0; i < QUIC_STATELESS_RESET_TOKEN_LEN; ++i)
  2776. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  2777. goto err;
  2778. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2779. goto err;
  2780. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2781. goto err;
  2782. ok = 1;
  2783. err:
  2784. if (ok)
  2785. WPACKET_finish(&wpkt);
  2786. else
  2787. WPACKET_cleanup(&wpkt);
  2788. return ok;
  2789. }
  2790. static const struct script_op script_39[] = {
  2791. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  2792. OP_C_SET_ALPN ("ossltest")
  2793. OP_C_CONNECT_WAIT ()
  2794. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2795. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2796. OP_C_WRITE (a, "apple", 5)
  2797. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2798. OP_S_READ_EXPECT (a, "apple", 5)
  2799. OP_SET_INJECT_WORD (0, 1)
  2800. OP_S_WRITE (a, "orange", 5)
  2801. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2802. OP_END
  2803. };
  2804. /* 40. Shutdown flush test */
  2805. static const unsigned char script_40_data[1024] = "strawberry";
  2806. static const struct script_op script_40[] = {
  2807. OP_C_SET_ALPN ("ossltest")
  2808. OP_C_CONNECT_WAIT ()
  2809. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2810. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2811. OP_C_WRITE (a, "apple", 5)
  2812. OP_C_INHIBIT_TICK (1)
  2813. OP_C_SET_WRITE_BUF_SIZE (a, 1024 * 100 * 3)
  2814. OP_BEGIN_REPEAT (100)
  2815. OP_C_WRITE (a, script_40_data, sizeof(script_40_data))
  2816. OP_END_REPEAT ()
  2817. OP_C_CONCLUDE (a)
  2818. OP_C_SHUTDOWN_WAIT (NULL, 0) /* disengages tick inhibition */
  2819. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2820. OP_S_READ_EXPECT (a, "apple", 5)
  2821. OP_BEGIN_REPEAT (100)
  2822. OP_S_READ_EXPECT (a, script_40_data, sizeof(script_40_data))
  2823. OP_END_REPEAT ()
  2824. OP_S_EXPECT_FIN (a)
  2825. OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
  2826. OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
  2827. OP_END
  2828. };
  2829. /* 41. Fault injection - PATH_CHALLENGE yields PATH_RESPONSE */
  2830. static const uint64_t path_challenge = UINT64_C(0xbdeb9451169c83aa);
  2831. static int script_41_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2832. unsigned char *buf, size_t len)
  2833. {
  2834. int ok = 0;
  2835. WPACKET wpkt;
  2836. unsigned char frame_buf[16];
  2837. size_t written;
  2838. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2839. return 1;
  2840. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2841. sizeof(frame_buf), 0)))
  2842. return 0;
  2843. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2844. || !TEST_true(WPACKET_put_bytes_u64(&wpkt, path_challenge)))
  2845. goto err;
  2846. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))
  2847. || !TEST_size_t_eq(written, 9))
  2848. goto err;
  2849. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2850. goto err;
  2851. --h->inject_word0;
  2852. ok = 1;
  2853. err:
  2854. if (ok)
  2855. WPACKET_finish(&wpkt);
  2856. else
  2857. WPACKET_cleanup(&wpkt);
  2858. return ok;
  2859. }
  2860. static void script_41_trace(int write_p, int version, int content_type,
  2861. const void *buf, size_t len, SSL *ssl, void *arg)
  2862. {
  2863. uint64_t frame_type, frame_data;
  2864. int was_minimal;
  2865. struct helper *h = arg;
  2866. PACKET pkt;
  2867. if (version != OSSL_QUIC1_VERSION
  2868. || content_type != SSL3_RT_QUIC_FRAME_FULL
  2869. || len < 1)
  2870. return;
  2871. if (!TEST_true(PACKET_buf_init(&pkt, buf, len))) {
  2872. ++h->scratch1;
  2873. return;
  2874. }
  2875. if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
  2876. &was_minimal))) {
  2877. ++h->scratch1;
  2878. return;
  2879. }
  2880. if (frame_type != OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  2881. return;
  2882. if (!TEST_true(ossl_quic_wire_decode_frame_path_response(&pkt, &frame_data))
  2883. || !TEST_uint64_t_eq(frame_data, path_challenge)) {
  2884. ++h->scratch1;
  2885. return;
  2886. }
  2887. ++h->scratch0;
  2888. }
  2889. static int script_41_setup(struct helper *h, struct helper_local *hl)
  2890. {
  2891. ossl_quic_tserver_set_msg_callback(ACQUIRE_S(), script_41_trace, h);
  2892. return 1;
  2893. }
  2894. static int script_41_check(struct helper *h, struct helper_local *hl)
  2895. {
  2896. /* At least one valid challenge/response echo? */
  2897. if (!TEST_uint64_t_gt(h->scratch0, 0))
  2898. return 0;
  2899. /* No failed tests? */
  2900. if (!TEST_uint64_t_eq(h->scratch1, 0))
  2901. return 0;
  2902. return 1;
  2903. }
  2904. static const struct script_op script_41[] = {
  2905. OP_S_SET_INJECT_PLAIN (script_41_inject_plain)
  2906. OP_C_SET_ALPN ("ossltest")
  2907. OP_C_CONNECT_WAIT ()
  2908. OP_CHECK (script_41_setup, 0)
  2909. OP_C_WRITE (DEFAULT, "apple", 5)
  2910. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2911. OP_S_READ_EXPECT (a, "apple", 5)
  2912. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
  2913. OP_S_WRITE (a, "orange", 6)
  2914. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  2915. OP_C_WRITE (DEFAULT, "strawberry", 10)
  2916. OP_S_READ_EXPECT (a, "strawberry", 10)
  2917. OP_CHECK (script_41_check, 0)
  2918. OP_END
  2919. };
  2920. /* 42. Fault injection - CRYPTO frame with illegal offset */
  2921. static int script_42_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2922. unsigned char *buf, size_t len)
  2923. {
  2924. int ok = 0;
  2925. unsigned char frame_buf[64];
  2926. size_t written;
  2927. WPACKET wpkt;
  2928. if (h->inject_word0 == 0)
  2929. return 1;
  2930. --h->inject_word0;
  2931. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2932. sizeof(frame_buf), 0)))
  2933. return 0;
  2934. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
  2935. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2936. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 1))
  2937. || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  2938. goto err;
  2939. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2940. goto err;
  2941. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2942. goto err;
  2943. ok = 1;
  2944. err:
  2945. if (ok)
  2946. WPACKET_finish(&wpkt);
  2947. else
  2948. WPACKET_cleanup(&wpkt);
  2949. return ok;
  2950. }
  2951. static const struct script_op script_42[] = {
  2952. OP_S_SET_INJECT_PLAIN (script_42_inject_plain)
  2953. OP_C_SET_ALPN ("ossltest")
  2954. OP_C_CONNECT_WAIT ()
  2955. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2956. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2957. OP_C_WRITE (a, "apple", 5)
  2958. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2959. OP_S_READ_EXPECT (a, "apple", 5)
  2960. OP_SET_INJECT_WORD (1, (((uint64_t)1) << 62) - 1)
  2961. OP_S_WRITE (a, "orange", 6)
  2962. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2963. OP_END
  2964. };
  2965. /* 43. Fault injection - CRYPTO frame exceeding FC */
  2966. static const struct script_op script_43[] = {
  2967. OP_S_SET_INJECT_PLAIN (script_42_inject_plain)
  2968. OP_C_SET_ALPN ("ossltest")
  2969. OP_C_CONNECT_WAIT ()
  2970. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2971. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2972. OP_C_WRITE (a, "apple", 5)
  2973. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2974. OP_S_READ_EXPECT (a, "apple", 5)
  2975. OP_SET_INJECT_WORD (1, 0x100000 /* 1 MiB */)
  2976. OP_S_WRITE (a, "orange", 6)
  2977. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0)
  2978. OP_END
  2979. };
  2980. /* 44. Fault injection - PADDING */
  2981. static int script_44_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2982. unsigned char *buf, size_t len)
  2983. {
  2984. int ok = 0;
  2985. WPACKET wpkt;
  2986. unsigned char frame_buf[16];
  2987. size_t written;
  2988. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2989. return 1;
  2990. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2991. sizeof(frame_buf), 0)))
  2992. return 0;
  2993. if (!TEST_true(ossl_quic_wire_encode_padding(&wpkt, 1)))
  2994. goto err;
  2995. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2996. goto err;
  2997. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2998. goto err;
  2999. ok = 1;
  3000. err:
  3001. if (ok)
  3002. WPACKET_finish(&wpkt);
  3003. else
  3004. WPACKET_cleanup(&wpkt);
  3005. return ok;
  3006. }
  3007. static const struct script_op script_44[] = {
  3008. OP_S_SET_INJECT_PLAIN (script_44_inject_plain)
  3009. OP_C_SET_ALPN ("ossltest")
  3010. OP_C_CONNECT_WAIT ()
  3011. OP_C_WRITE (DEFAULT, "apple", 5)
  3012. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3013. OP_S_READ_EXPECT (a, "apple", 5)
  3014. OP_SET_INJECT_WORD (1, 0)
  3015. OP_S_WRITE (a, "Strawberry", 10)
  3016. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3017. OP_END
  3018. };
  3019. /* 45. PING must generate ACK */
  3020. static int force_ping(struct helper *h, struct helper_local *hl)
  3021. {
  3022. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
  3023. h->scratch0 = ossl_quic_channel_get_diag_num_rx_ack(ch);
  3024. if (!TEST_true(ossl_quic_tserver_ping(ACQUIRE_S())))
  3025. return 0;
  3026. return 1;
  3027. }
  3028. static int wait_incoming_acks_increased(struct helper *h, struct helper_local *hl)
  3029. {
  3030. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
  3031. uint16_t count;
  3032. count = ossl_quic_channel_get_diag_num_rx_ack(ch);
  3033. if (count == h->scratch0) {
  3034. h->check_spin_again = 1;
  3035. return 0;
  3036. }
  3037. return 1;
  3038. }
  3039. static const struct script_op script_45[] = {
  3040. OP_C_SET_ALPN ("ossltest")
  3041. OP_C_CONNECT_WAIT ()
  3042. OP_C_WRITE (DEFAULT, "apple", 5)
  3043. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3044. OP_S_READ_EXPECT (a, "apple", 5)
  3045. OP_BEGIN_REPEAT (2)
  3046. OP_CHECK (force_ping, 0)
  3047. OP_CHECK (wait_incoming_acks_increased, 0)
  3048. OP_END_REPEAT ()
  3049. OP_S_WRITE (a, "Strawberry", 10)
  3050. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3051. OP_END
  3052. };
  3053. /* 46. Fault injection - ACK - malformed initial range */
  3054. static int script_46_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3055. unsigned char *buf, size_t len)
  3056. {
  3057. int ok = 0;
  3058. WPACKET wpkt;
  3059. unsigned char frame_buf[16];
  3060. size_t written;
  3061. uint64_t type = 0, largest_acked = 0, first_range = 0, range_count = 0;
  3062. uint64_t agap = 0, alen = 0;
  3063. uint64_t ect0 = 0, ect1 = 0, ecnce = 0;
  3064. if (h->inject_word0 == 0)
  3065. return 1;
  3066. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3067. sizeof(frame_buf), 0)))
  3068. return 0;
  3069. type = OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
  3070. switch (h->inject_word0) {
  3071. case 1:
  3072. largest_acked = 100;
  3073. first_range = 101;
  3074. range_count = 0;
  3075. break;
  3076. case 2:
  3077. largest_acked = 100;
  3078. first_range = 80;
  3079. /* [20..100]; [0..18] */
  3080. range_count = 1;
  3081. agap = 0;
  3082. alen = 19;
  3083. break;
  3084. case 3:
  3085. largest_acked = 100;
  3086. first_range = 80;
  3087. range_count = 1;
  3088. agap = 18;
  3089. alen = 1;
  3090. break;
  3091. case 4:
  3092. type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
  3093. largest_acked = 100;
  3094. first_range = 1;
  3095. range_count = 0;
  3096. break;
  3097. case 5:
  3098. type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
  3099. largest_acked = 0;
  3100. first_range = 0;
  3101. range_count = 0;
  3102. ect0 = 0;
  3103. ect1 = 50;
  3104. ecnce = 200;
  3105. break;
  3106. }
  3107. h->inject_word0 = 0;
  3108. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
  3109. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, largest_acked))
  3110. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_delay=*/0))
  3111. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_range_count=*/range_count))
  3112. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*first_ack_range=*/first_range)))
  3113. goto err;
  3114. if (range_count > 0)
  3115. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].gap=*/agap))
  3116. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].len=*/alen)))
  3117. goto err;
  3118. if (type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN)
  3119. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, ect0))
  3120. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ect1))
  3121. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ecnce)))
  3122. goto err;
  3123. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3124. goto err;
  3125. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3126. goto err;
  3127. ok = 1;
  3128. err:
  3129. if (ok)
  3130. WPACKET_finish(&wpkt);
  3131. else
  3132. WPACKET_cleanup(&wpkt);
  3133. return ok;
  3134. }
  3135. static const struct script_op script_46[] = {
  3136. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3137. OP_C_SET_ALPN ("ossltest")
  3138. OP_C_CONNECT_WAIT ()
  3139. OP_C_WRITE (DEFAULT, "apple", 5)
  3140. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3141. OP_S_READ_EXPECT (a, "apple", 5)
  3142. OP_SET_INJECT_WORD (1, 0)
  3143. OP_S_WRITE (a, "Strawberry", 10)
  3144. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3145. OP_END
  3146. };
  3147. /* 47. Fault injection - ACK - malformed subsequent range */
  3148. static const struct script_op script_47[] = {
  3149. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3150. OP_C_SET_ALPN ("ossltest")
  3151. OP_C_CONNECT_WAIT ()
  3152. OP_C_WRITE (DEFAULT, "apple", 5)
  3153. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3154. OP_S_READ_EXPECT (a, "apple", 5)
  3155. OP_SET_INJECT_WORD (2, 0)
  3156. OP_S_WRITE (a, "Strawberry", 10)
  3157. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3158. OP_END
  3159. };
  3160. /* 48. Fault injection - ACK - malformed subsequent range */
  3161. static const struct script_op script_48[] = {
  3162. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3163. OP_C_SET_ALPN ("ossltest")
  3164. OP_C_CONNECT_WAIT ()
  3165. OP_C_WRITE (DEFAULT, "apple", 5)
  3166. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3167. OP_S_READ_EXPECT (a, "apple", 5)
  3168. OP_SET_INJECT_WORD (3, 0)
  3169. OP_S_WRITE (a, "Strawberry", 10)
  3170. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3171. OP_END
  3172. };
  3173. /* 49. Fault injection - ACK - fictional PN */
  3174. static const struct script_op script_49[] = {
  3175. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3176. OP_C_SET_ALPN ("ossltest")
  3177. OP_C_CONNECT_WAIT ()
  3178. OP_C_WRITE (DEFAULT, "apple", 5)
  3179. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3180. OP_S_READ_EXPECT (a, "apple", 5)
  3181. OP_SET_INJECT_WORD (4, 0)
  3182. OP_S_WRITE (a, "Strawberry", 10)
  3183. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3184. OP_END
  3185. };
  3186. /* 50. Fault injection - ACK - duplicate PN */
  3187. static const struct script_op script_50[] = {
  3188. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3189. OP_C_SET_ALPN ("ossltest")
  3190. OP_C_CONNECT_WAIT ()
  3191. OP_C_WRITE (DEFAULT, "apple", 5)
  3192. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3193. OP_S_READ_EXPECT (a, "apple", 5)
  3194. OP_BEGIN_REPEAT (2)
  3195. OP_SET_INJECT_WORD (5, 0)
  3196. OP_S_WRITE (a, "Strawberry", 10)
  3197. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3198. OP_END_REPEAT ()
  3199. OP_END
  3200. };
  3201. /* 51. Fault injection - PATH_RESPONSE is ignored */
  3202. static const struct script_op script_51[] = {
  3203. OP_S_SET_INJECT_PLAIN (script_41_inject_plain)
  3204. OP_C_SET_ALPN ("ossltest")
  3205. OP_C_CONNECT_WAIT ()
  3206. OP_C_WRITE (DEFAULT, "apple", 5)
  3207. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3208. OP_S_READ_EXPECT (a, "apple", 5)
  3209. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  3210. OP_S_WRITE (a, "orange", 6)
  3211. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3212. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3213. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3214. OP_END
  3215. };
  3216. /* 52. Fault injection - ignore BLOCKED frames with bogus values */
  3217. static int script_52_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3218. unsigned char *buf, size_t len)
  3219. {
  3220. int ok = 0;
  3221. unsigned char frame_buf[64];
  3222. size_t written;
  3223. WPACKET wpkt;
  3224. uint64_t type = h->inject_word1;
  3225. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3226. return 1;
  3227. --h->inject_word0;
  3228. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3229. sizeof(frame_buf), 0)))
  3230. return 0;
  3231. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type)))
  3232. goto err;
  3233. if (type == OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  3234. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, C_BIDI_ID(0))))
  3235. goto err;
  3236. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, 0xFFFFFF)))
  3237. goto err;
  3238. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3239. goto err;
  3240. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3241. goto err;
  3242. ok = 1;
  3243. err:
  3244. if (ok)
  3245. WPACKET_finish(&wpkt);
  3246. else
  3247. WPACKET_cleanup(&wpkt);
  3248. return ok;
  3249. }
  3250. static const struct script_op script_52[] = {
  3251. OP_S_SET_INJECT_PLAIN (script_52_inject_plain)
  3252. OP_C_SET_ALPN ("ossltest")
  3253. OP_C_CONNECT_WAIT ()
  3254. OP_C_WRITE (DEFAULT, "apple", 5)
  3255. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3256. OP_S_READ_EXPECT (a, "apple", 5)
  3257. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
  3258. OP_S_WRITE (a, "orange", 6)
  3259. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3260. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3261. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3262. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  3263. OP_S_WRITE (a, "orange", 6)
  3264. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3265. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3266. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3267. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
  3268. OP_S_WRITE (a, "orange", 6)
  3269. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3270. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3271. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3272. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  3273. OP_S_WRITE (a, "orange", 6)
  3274. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3275. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3276. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3277. OP_END
  3278. };
  3279. /* 53. Fault injection - excess CRYPTO buffer size */
  3280. static int script_53_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3281. unsigned char *buf, size_t len)
  3282. {
  3283. int ok = 0;
  3284. size_t written;
  3285. WPACKET wpkt;
  3286. uint64_t offset = 0, data_len = 100;
  3287. unsigned char *frame_buf = NULL;
  3288. size_t frame_len, i;
  3289. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3290. return 1;
  3291. h->inject_word0 = 0;
  3292. switch (h->inject_word1) {
  3293. case 0:
  3294. /*
  3295. * Far out offset which will not have been reached during handshake.
  3296. * This will not be delivered to the QUIC_TLS instance since it will be
  3297. * waiting for in-order delivery of previous bytes. This tests our flow
  3298. * control on CRYPTO stream buffering.
  3299. */
  3300. offset = 100000;
  3301. data_len = 1;
  3302. break;
  3303. }
  3304. frame_len = 1 + 8 + 8 + (size_t)data_len;
  3305. if (!TEST_ptr(frame_buf = OPENSSL_malloc(frame_len)))
  3306. return 0;
  3307. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, frame_len, 0)))
  3308. goto err;
  3309. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
  3310. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
  3311. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, data_len)))
  3312. goto err;
  3313. for (i = 0; i < data_len; ++i)
  3314. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  3315. goto err;
  3316. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3317. goto err;
  3318. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3319. goto err;
  3320. ok = 1;
  3321. err:
  3322. if (ok)
  3323. WPACKET_finish(&wpkt);
  3324. else
  3325. WPACKET_cleanup(&wpkt);
  3326. OPENSSL_free(frame_buf);
  3327. return ok;
  3328. }
  3329. static const struct script_op script_53[] = {
  3330. OP_S_SET_INJECT_PLAIN (script_53_inject_plain)
  3331. OP_C_SET_ALPN ("ossltest")
  3332. OP_C_CONNECT_WAIT ()
  3333. OP_C_WRITE (DEFAULT, "apple", 5)
  3334. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3335. OP_S_READ_EXPECT (a, "apple", 5)
  3336. OP_SET_INJECT_WORD (1, 0)
  3337. OP_S_WRITE (a, "Strawberry", 10)
  3338. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0)
  3339. OP_END
  3340. };
  3341. /* 54. Fault injection - corrupted crypto stream data */
  3342. static int script_54_inject_handshake(struct helper *h,
  3343. unsigned char *buf, size_t buf_len)
  3344. {
  3345. size_t i;
  3346. for (i = 0; i < buf_len; ++i)
  3347. buf[i] ^= 0xff;
  3348. return 1;
  3349. }
  3350. static const struct script_op script_54[] = {
  3351. OP_S_SET_INJECT_HANDSHAKE(script_54_inject_handshake)
  3352. OP_C_SET_ALPN ("ossltest")
  3353. OP_C_CONNECT_WAIT_OR_FAIL()
  3354. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_UNEXPECTED_MESSAGE,0,0)
  3355. OP_END
  3356. };
  3357. /* 55. Fault injection - NEW_CONN_ID with >20 byte CID */
  3358. static const struct script_op script_55[] = {
  3359. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  3360. OP_C_SET_ALPN ("ossltest")
  3361. OP_C_CONNECT_WAIT ()
  3362. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3363. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3364. OP_C_WRITE (a, "apple", 5)
  3365. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3366. OP_S_READ_EXPECT (a, "apple", 5)
  3367. OP_SET_INJECT_WORD (0, 2)
  3368. OP_S_WRITE (a, "orange", 5)
  3369. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3370. OP_END
  3371. };
  3372. /* 56. Fault injection - NEW_CONN_ID with seq no < retire prior to */
  3373. static const struct script_op script_56[] = {
  3374. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  3375. OP_C_SET_ALPN ("ossltest")
  3376. OP_C_CONNECT_WAIT ()
  3377. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3378. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3379. OP_C_WRITE (a, "apple", 5)
  3380. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3381. OP_S_READ_EXPECT (a, "apple", 5)
  3382. OP_SET_INJECT_WORD (0, 3)
  3383. OP_S_WRITE (a, "orange", 5)
  3384. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3385. OP_END
  3386. };
  3387. /* 57. Fault injection - NEW_CONN_ID with lower seq so ignored */
  3388. static const struct script_op script_57[] = {
  3389. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  3390. OP_C_SET_ALPN ("ossltest")
  3391. OP_C_CONNECT_WAIT ()
  3392. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3393. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3394. OP_C_WRITE (a, "apple", 5)
  3395. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3396. OP_S_READ_EXPECT (a, "apple", 5)
  3397. OP_SET_INJECT_WORD (0, 4)
  3398. OP_S_WRITE (a, "orange", 5)
  3399. OP_C_READ_EXPECT (a, "orange", 5)
  3400. OP_C_WRITE (a, "Strawberry", 10)
  3401. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3402. /*
  3403. * Now we send a NEW_CONN_ID with a bogus CID. However the sequence number
  3404. * is old so it should be ignored and we should still be able to
  3405. * communicate.
  3406. */
  3407. OP_SET_INJECT_WORD (0, 5)
  3408. OP_S_WRITE (a, "raspberry", 9)
  3409. OP_C_READ_EXPECT (a, "raspberry", 9)
  3410. OP_C_WRITE (a, "peach", 5)
  3411. OP_S_READ_EXPECT (a, "peach", 5)
  3412. OP_END
  3413. };
  3414. /* 58. Fault injection - repeated HANDSHAKE_DONE */
  3415. static int script_58_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3416. unsigned char *buf, size_t len)
  3417. {
  3418. int ok = 0;
  3419. unsigned char frame_buf[64];
  3420. size_t written;
  3421. WPACKET wpkt;
  3422. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3423. return 1;
  3424. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3425. sizeof(frame_buf), 0)))
  3426. return 0;
  3427. if (h->inject_word0 == 1) {
  3428. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)))
  3429. goto err;
  3430. } else {
  3431. /* Needless multi-byte encoding */
  3432. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x40))
  3433. || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x1E)))
  3434. goto err;
  3435. }
  3436. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3437. goto err;
  3438. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3439. goto err;
  3440. ok = 1;
  3441. err:
  3442. if (ok)
  3443. WPACKET_finish(&wpkt);
  3444. else
  3445. WPACKET_cleanup(&wpkt);
  3446. return ok;
  3447. }
  3448. static const struct script_op script_58[] = {
  3449. OP_S_SET_INJECT_PLAIN (script_58_inject_plain)
  3450. OP_C_SET_ALPN ("ossltest")
  3451. OP_C_CONNECT_WAIT ()
  3452. OP_C_WRITE (DEFAULT, "apple", 5)
  3453. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3454. OP_S_READ_EXPECT (a, "apple", 5)
  3455. OP_SET_INJECT_WORD (1, 0)
  3456. OP_S_WRITE (a, "orange", 6)
  3457. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3458. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3459. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3460. OP_END
  3461. };
  3462. /* 59. Fault injection - multi-byte frame encoding */
  3463. static const struct script_op script_59[] = {
  3464. OP_S_SET_INJECT_PLAIN (script_58_inject_plain)
  3465. OP_C_SET_ALPN ("ossltest")
  3466. OP_C_CONNECT_WAIT ()
  3467. OP_C_WRITE (DEFAULT, "apple", 5)
  3468. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3469. OP_S_READ_EXPECT (a, "apple", 5)
  3470. OP_SET_INJECT_WORD (2, 0)
  3471. OP_S_WRITE (a, "orange", 6)
  3472. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION,0,0)
  3473. OP_END
  3474. };
  3475. /* 60. Connection close reason truncation */
  3476. static char long_reason[2048];
  3477. static int init_reason(struct helper *h, struct helper_local *hl)
  3478. {
  3479. memset(long_reason, '~', sizeof(long_reason));
  3480. memcpy(long_reason, "This is a long reason string.", 29);
  3481. long_reason[OSSL_NELEM(long_reason) - 1] = '\0';
  3482. return 1;
  3483. }
  3484. static int check_shutdown_reason(struct helper *h, struct helper_local *hl)
  3485. {
  3486. const QUIC_TERMINATE_CAUSE *tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S());
  3487. if (tc == NULL) {
  3488. h->check_spin_again = 1;
  3489. return 0;
  3490. }
  3491. if (!TEST_size_t_ge(tc->reason_len, 50)
  3492. || !TEST_mem_eq(long_reason, tc->reason_len,
  3493. tc->reason, tc->reason_len))
  3494. return 0;
  3495. return 1;
  3496. }
  3497. static const struct script_op script_60[] = {
  3498. OP_C_SET_ALPN ("ossltest")
  3499. OP_C_CONNECT_WAIT ()
  3500. OP_C_WRITE (DEFAULT, "apple", 5)
  3501. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3502. OP_S_READ_EXPECT (a, "apple", 5)
  3503. OP_CHECK (init_reason, 0)
  3504. OP_C_SHUTDOWN_WAIT (long_reason, 0)
  3505. OP_CHECK (check_shutdown_reason, 0)
  3506. OP_END
  3507. };
  3508. /* 61. Fault injection - RESET_STREAM exceeding stream count FC */
  3509. static int script_61_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3510. unsigned char *buf, size_t len)
  3511. {
  3512. int ok = 0;
  3513. WPACKET wpkt;
  3514. unsigned char frame_buf[32];
  3515. size_t written;
  3516. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3517. return 1;
  3518. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3519. sizeof(frame_buf), 0)))
  3520. return 0;
  3521. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0))
  3522. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  3523. h->inject_word1))
  3524. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
  3525. || (h->inject_word0 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
  3526. && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))) /* final size */
  3527. goto err;
  3528. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3529. goto err;
  3530. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3531. goto err;
  3532. ok = 1;
  3533. err:
  3534. if (ok)
  3535. WPACKET_finish(&wpkt);
  3536. else
  3537. WPACKET_cleanup(&wpkt);
  3538. return ok;
  3539. }
  3540. static const struct script_op script_61[] = {
  3541. OP_S_SET_INJECT_PLAIN (script_61_inject_plain)
  3542. OP_C_SET_ALPN ("ossltest")
  3543. OP_C_CONNECT_WAIT ()
  3544. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3545. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3546. OP_C_WRITE (a, "orange", 6)
  3547. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3548. OP_S_READ_EXPECT (a, "orange", 6)
  3549. OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  3550. S_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
  3551. OP_S_WRITE (a, "fruit", 5)
  3552. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  3553. OP_END
  3554. };
  3555. /* 62. Fault injection - STOP_SENDING with high ID */
  3556. static const struct script_op script_62[] = {
  3557. OP_S_SET_INJECT_PLAIN (script_61_inject_plain)
  3558. OP_C_SET_ALPN ("ossltest")
  3559. OP_C_CONNECT_WAIT ()
  3560. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3561. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3562. OP_C_WRITE (a, "orange", 6)
  3563. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3564. OP_S_READ_EXPECT (a, "orange", 6)
  3565. OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
  3566. C_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
  3567. OP_S_WRITE (a, "fruit", 5)
  3568. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  3569. OP_END
  3570. };
  3571. /* 63. Fault injection - STREAM frame exceeding stream limit */
  3572. static const struct script_op script_63[] = {
  3573. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  3574. OP_C_SET_ALPN ("ossltest")
  3575. OP_C_CONNECT_WAIT ()
  3576. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3577. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3578. OP_C_WRITE (a, "apple", 5)
  3579. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3580. OP_S_READ_EXPECT (a, "apple", 5)
  3581. OP_SET_INJECT_WORD (S_BIDI_ID(5000) + 1, 4)
  3582. OP_S_WRITE (a, "orange", 6)
  3583. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  3584. OP_END
  3585. };
  3586. /* 64. Fault injection - STREAM - zero-length no-FIN is accepted */
  3587. static const struct script_op script_64[] = {
  3588. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  3589. OP_C_SET_ALPN ("ossltest")
  3590. OP_C_CONNECT_WAIT ()
  3591. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3592. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  3593. OP_S_WRITE (a, "apple", 5)
  3594. OP_C_ACCEPT_STREAM_WAIT (a)
  3595. OP_C_READ_EXPECT (a, "apple", 5)
  3596. OP_SET_INJECT_WORD (S_BIDI_ID(20) + 1, 1)
  3597. OP_S_WRITE (a, "orange", 6)
  3598. OP_C_READ_EXPECT (a, "orange", 6)
  3599. OP_END
  3600. };
  3601. /* 65. Fault injection - CRYPTO - zero-length is accepted */
  3602. static int script_65_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3603. unsigned char *buf, size_t len)
  3604. {
  3605. int ok = 0;
  3606. unsigned char frame_buf[64];
  3607. size_t written;
  3608. WPACKET wpkt;
  3609. if (h->inject_word0 == 0)
  3610. return 1;
  3611. --h->inject_word0;
  3612. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3613. sizeof(frame_buf), 0)))
  3614. return 0;
  3615. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
  3616. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0))
  3617. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
  3618. goto err;
  3619. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3620. goto err;
  3621. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3622. goto err;
  3623. ok = 1;
  3624. err:
  3625. if (ok)
  3626. WPACKET_finish(&wpkt);
  3627. else
  3628. WPACKET_cleanup(&wpkt);
  3629. return ok;
  3630. }
  3631. static const struct script_op script_65[] = {
  3632. OP_S_SET_INJECT_PLAIN (script_65_inject_plain)
  3633. OP_C_SET_ALPN ("ossltest")
  3634. OP_C_CONNECT_WAIT ()
  3635. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3636. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3637. OP_C_WRITE (a, "apple", 5)
  3638. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3639. OP_S_READ_EXPECT (a, "apple", 5)
  3640. OP_SET_INJECT_WORD (1, 0)
  3641. OP_S_WRITE (a, "orange", 6)
  3642. OP_C_READ_EXPECT (a, "orange", 6)
  3643. OP_END
  3644. };
  3645. /* 66. Fault injection - large MAX_STREAM_DATA */
  3646. static int script_66_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3647. unsigned char *buf, size_t len)
  3648. {
  3649. int ok = 0;
  3650. WPACKET wpkt;
  3651. unsigned char frame_buf[64];
  3652. size_t written;
  3653. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3654. return 1;
  3655. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3656. sizeof(frame_buf), 0)))
  3657. return 0;
  3658. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
  3659. goto err;
  3660. if (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  3661. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  3662. h->inject_word0 - 1)))
  3663. goto err;
  3664. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_VLINT_MAX)))
  3665. goto err;
  3666. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3667. goto err;
  3668. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3669. goto err;
  3670. ok = 1;
  3671. err:
  3672. if (ok)
  3673. WPACKET_finish(&wpkt);
  3674. else
  3675. WPACKET_cleanup(&wpkt);
  3676. return ok;
  3677. }
  3678. static const struct script_op script_66[] = {
  3679. OP_S_SET_INJECT_PLAIN (script_66_inject_plain)
  3680. OP_C_SET_ALPN ("ossltest")
  3681. OP_C_CONNECT_WAIT ()
  3682. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3683. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  3684. OP_S_WRITE (a, "apple", 5)
  3685. OP_C_ACCEPT_STREAM_WAIT (a)
  3686. OP_C_READ_EXPECT (a, "apple", 5)
  3687. OP_SET_INJECT_WORD (S_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  3688. OP_S_WRITE (a, "orange", 6)
  3689. OP_C_READ_EXPECT (a, "orange", 6)
  3690. OP_C_WRITE (a, "Strawberry", 10)
  3691. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3692. OP_END
  3693. };
  3694. /* 67. Fault injection - large MAX_DATA */
  3695. static const struct script_op script_67[] = {
  3696. OP_S_SET_INJECT_PLAIN (script_66_inject_plain)
  3697. OP_C_SET_ALPN ("ossltest")
  3698. OP_C_CONNECT_WAIT ()
  3699. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3700. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  3701. OP_S_WRITE (a, "apple", 5)
  3702. OP_C_ACCEPT_STREAM_WAIT (a)
  3703. OP_C_READ_EXPECT (a, "apple", 5)
  3704. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
  3705. OP_S_WRITE (a, "orange", 6)
  3706. OP_C_READ_EXPECT (a, "orange", 6)
  3707. OP_C_WRITE (a, "Strawberry", 10)
  3708. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3709. OP_END
  3710. };
  3711. /* 68. Fault injection - Unexpected TLS messages */
  3712. static int script_68_inject_handshake(struct helper *h, unsigned char *msg,
  3713. size_t msglen)
  3714. {
  3715. const unsigned char *data;
  3716. size_t datalen;
  3717. const unsigned char certreq[] = {
  3718. SSL3_MT_CERTIFICATE_REQUEST, /* CertificateRequest message */
  3719. 0, 0, 12, /* Length of message */
  3720. 1, 1, /* certificate_request_context */
  3721. 0, 8, /* Extensions block length */
  3722. 0, TLSEXT_TYPE_signature_algorithms, /* sig_algs extension*/
  3723. 0, 4, /* 4 bytes of sig algs extension*/
  3724. 0, 2, /* sigalgs list is 2 bytes long */
  3725. 8, 4 /* rsa_pss_rsae_sha256 */
  3726. };
  3727. const unsigned char keyupdate[] = {
  3728. SSL3_MT_KEY_UPDATE, /* KeyUpdate message */
  3729. 0, 0, 1, /* Length of message */
  3730. SSL_KEY_UPDATE_NOT_REQUESTED /* update_not_requested */
  3731. };
  3732. /* We transform the NewSessionTicket message into something else */
  3733. switch(h->inject_word0) {
  3734. case 0:
  3735. return 1;
  3736. case 1:
  3737. /* CertificateRequest message */
  3738. data = certreq;
  3739. datalen = sizeof(certreq);
  3740. break;
  3741. case 2:
  3742. /* KeyUpdate message */
  3743. data = keyupdate;
  3744. datalen = sizeof(keyupdate);
  3745. break;
  3746. default:
  3747. return 0;
  3748. }
  3749. if (!TEST_true(qtest_fault_resize_message(h->qtf,
  3750. datalen - SSL3_HM_HEADER_LENGTH)))
  3751. return 0;
  3752. memcpy(msg, data, datalen);
  3753. return 1;
  3754. }
  3755. /* Send a CerticateRequest message post-handshake */
  3756. static const struct script_op script_68[] = {
  3757. OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
  3758. OP_C_SET_ALPN ("ossltest")
  3759. OP_C_CONNECT_WAIT ()
  3760. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3761. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3762. OP_C_WRITE (a, "apple", 5)
  3763. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3764. OP_S_READ_EXPECT (a, "apple", 5)
  3765. OP_SET_INJECT_WORD (1, 0)
  3766. OP_S_NEW_TICKET ()
  3767. OP_S_WRITE (a, "orange", 6)
  3768. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
  3769. OP_END
  3770. };
  3771. /* 69. Send a TLS KeyUpdate message post-handshake */
  3772. static const struct script_op script_69[] = {
  3773. OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
  3774. OP_C_SET_ALPN ("ossltest")
  3775. OP_C_CONNECT_WAIT ()
  3776. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3777. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3778. OP_C_WRITE (a, "apple", 5)
  3779. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3780. OP_S_READ_EXPECT (a, "apple", 5)
  3781. OP_SET_INJECT_WORD (2, 0)
  3782. OP_S_NEW_TICKET ()
  3783. OP_S_WRITE (a, "orange", 6)
  3784. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN
  3785. + SSL_AD_UNEXPECTED_MESSAGE, 0, 0)
  3786. OP_END
  3787. };
  3788. static int set_max_early_data(struct helper *h, struct helper_local *hl)
  3789. {
  3790. if (!TEST_true(ossl_quic_tserver_set_max_early_data(ACQUIRE_S(),
  3791. (uint32_t)hl->check_op->arg2)))
  3792. return 0;
  3793. return 1;
  3794. }
  3795. /* 70. Send a TLS NewSessionTicket message with invalid max_early_data */
  3796. static const struct script_op script_70[] = {
  3797. OP_C_SET_ALPN ("ossltest")
  3798. OP_C_CONNECT_WAIT ()
  3799. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3800. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3801. OP_C_WRITE (a, "apple", 5)
  3802. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3803. OP_S_READ_EXPECT (a, "apple", 5)
  3804. OP_CHECK (set_max_early_data, 0xfffffffe)
  3805. OP_S_NEW_TICKET ()
  3806. OP_S_WRITE (a, "orange", 6)
  3807. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
  3808. OP_END
  3809. };
  3810. /* 71. Send a TLS NewSessionTicket message with valid max_early_data */
  3811. static const struct script_op script_71[] = {
  3812. OP_C_SET_ALPN ("ossltest")
  3813. OP_C_CONNECT_WAIT ()
  3814. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3815. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3816. OP_C_WRITE (a, "apple", 5)
  3817. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3818. OP_S_READ_EXPECT (a, "apple", 5)
  3819. OP_CHECK (set_max_early_data, 0xffffffff)
  3820. OP_S_NEW_TICKET ()
  3821. OP_S_WRITE (a, "orange", 6)
  3822. OP_C_READ_EXPECT (a, "orange", 6)
  3823. OP_END
  3824. };
  3825. /* 72. Test that APL stops handing out streams after limit reached (bidi) */
  3826. static int script_72_check(struct helper *h, struct helper_local *hl)
  3827. {
  3828. if (!TEST_uint64_t_ge(h->fail_count, 50))
  3829. return 0;
  3830. return 1;
  3831. }
  3832. static const struct script_op script_72[] = {
  3833. OP_C_SET_ALPN ("ossltest")
  3834. OP_C_CONNECT_WAIT ()
  3835. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3836. /*
  3837. * Request more streams than a server will initially hand out and test that
  3838. * they fail properly.
  3839. */
  3840. OP_BEGIN_REPEAT (200)
  3841. OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
  3842. OP_C_SKIP_IF_UNBOUND (a, 2)
  3843. OP_C_WRITE (a, "apple", 5)
  3844. OP_C_FREE_STREAM (a)
  3845. OP_END_REPEAT ()
  3846. OP_CHECK (script_72_check, 0)
  3847. OP_END
  3848. };
  3849. /* 73. Test that APL stops handing out streams after limit reached (uni) */
  3850. static const struct script_op script_73[] = {
  3851. OP_C_SET_ALPN ("ossltest")
  3852. OP_C_CONNECT_WAIT ()
  3853. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3854. /*
  3855. * Request more streams than a server will initially hand out and test that
  3856. * they fail properly.
  3857. */
  3858. OP_BEGIN_REPEAT (200)
  3859. OP_C_NEW_STREAM_UNI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
  3860. OP_C_SKIP_IF_UNBOUND (a, 2)
  3861. OP_C_WRITE (a, "apple", 5)
  3862. OP_C_FREE_STREAM (a)
  3863. OP_END_REPEAT ()
  3864. OP_CHECK (script_72_check, 0)
  3865. OP_END
  3866. };
  3867. /* 74. Version negotiation: QUIC_VERSION_1 ignored */
  3868. static int generate_version_neg(WPACKET *wpkt, uint32_t version)
  3869. {
  3870. QUIC_PKT_HDR hdr = {0};
  3871. hdr.type = QUIC_PKT_TYPE_VERSION_NEG;
  3872. hdr.fixed = 1;
  3873. hdr.dst_conn_id.id_len = 0;
  3874. hdr.src_conn_id.id_len = 8;
  3875. memset(hdr.src_conn_id.id, 0x55, 8);
  3876. if (!TEST_true(ossl_quic_wire_encode_pkt_hdr(wpkt, 0, &hdr, NULL)))
  3877. return 0;
  3878. if (!TEST_true(WPACKET_put_bytes_u32(wpkt, version)))
  3879. return 0;
  3880. return 1;
  3881. }
  3882. static int server_gen_version_neg(struct helper *h, BIO_MSG *msg, size_t stride)
  3883. {
  3884. int rc = 0, have_wpkt = 0;
  3885. size_t l;
  3886. WPACKET wpkt;
  3887. BUF_MEM *buf = NULL;
  3888. uint32_t version;
  3889. switch (h->inject_word0) {
  3890. case 0:
  3891. return 1;
  3892. case 1:
  3893. version = QUIC_VERSION_1;
  3894. break;
  3895. default:
  3896. version = 0x5432abcd;
  3897. break;
  3898. }
  3899. if (!TEST_ptr(buf = BUF_MEM_new()))
  3900. goto err;
  3901. if (!TEST_true(WPACKET_init(&wpkt, buf)))
  3902. goto err;
  3903. have_wpkt = 1;
  3904. generate_version_neg(&wpkt, version);
  3905. if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
  3906. goto err;
  3907. if (!TEST_true(qtest_fault_resize_datagram(h->qtf, l)))
  3908. return 0;
  3909. memcpy(msg->data, buf->data, l);
  3910. h->inject_word0 = 0;
  3911. rc = 1;
  3912. err:
  3913. if (have_wpkt)
  3914. WPACKET_finish(&wpkt);
  3915. BUF_MEM_free(buf);
  3916. return rc;
  3917. }
  3918. static const struct script_op script_74[] = {
  3919. OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg)
  3920. OP_SET_INJECT_WORD (1, 0)
  3921. OP_C_SET_ALPN ("ossltest")
  3922. OP_C_CONNECT_WAIT ()
  3923. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3924. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3925. OP_C_WRITE (a, "apple", 5)
  3926. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3927. OP_S_READ_EXPECT (a, "apple", 5)
  3928. OP_END
  3929. };
  3930. /* 75. Version negotiation: Unknown version causes connection abort */
  3931. static const struct script_op script_75[] = {
  3932. OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg)
  3933. OP_SET_INJECT_WORD (2, 0)
  3934. OP_C_SET_ALPN ("ossltest")
  3935. OP_C_CONNECT_WAIT_OR_FAIL()
  3936. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CONNECTION_REFUSED,0,0)
  3937. OP_END
  3938. };
  3939. /* 76. Test peer-initiated shutdown wait */
  3940. static int script_76_check(struct helper *h, struct helper_local *hl)
  3941. {
  3942. if (!TEST_false(SSL_shutdown_ex(h->c_conn,
  3943. SSL_SHUTDOWN_FLAG_WAIT_PEER
  3944. | SSL_SHUTDOWN_FLAG_NO_BLOCK,
  3945. NULL, 0)))
  3946. return 0;
  3947. return 1;
  3948. }
  3949. static const struct script_op script_76[] = {
  3950. OP_C_SET_ALPN ("ossltest")
  3951. OP_C_CONNECT_WAIT ()
  3952. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3953. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3954. OP_C_WRITE (a, "apple", 5)
  3955. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3956. OP_S_READ_EXPECT (a, "apple", 5)
  3957. /* Check a WAIT_PEER call doesn't succeed yet. */
  3958. OP_CHECK (script_76_check, 0)
  3959. OP_S_SHUTDOWN (42)
  3960. OP_C_SHUTDOWN_WAIT (NULL, SSL_SHUTDOWN_FLAG_WAIT_PEER)
  3961. OP_C_EXPECT_CONN_CLOSE_INFO(42, 1, 1)
  3962. OP_END
  3963. };
  3964. /* 77. Ensure default stream popping operates correctly */
  3965. static const struct script_op script_77[] = {
  3966. OP_C_SET_ALPN ("ossltest")
  3967. OP_C_CONNECT_WAIT ()
  3968. OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
  3969. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  3970. OP_S_WRITE (a, "Strawberry", 10)
  3971. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3972. OP_S_NEW_STREAM_BIDI (b, S_BIDI_ID(1))
  3973. OP_S_WRITE (b, "xyz", 3)
  3974. OP_C_ACCEPT_STREAM_WAIT (b)
  3975. OP_C_READ_EXPECT (b, "xyz", 3)
  3976. OP_END
  3977. };
  3978. /* 78. Post-connection session ticket handling */
  3979. static size_t new_session_count;
  3980. static int on_new_session(SSL *s, SSL_SESSION *sess)
  3981. {
  3982. ++new_session_count;
  3983. return 0; /* do not ref session, we aren't keeping it */
  3984. }
  3985. static int setup_session(struct helper *h, struct helper_local *hl)
  3986. {
  3987. SSL_CTX_set_session_cache_mode(h->c_ctx, SSL_SESS_CACHE_BOTH);
  3988. SSL_CTX_sess_set_new_cb(h->c_ctx, on_new_session);
  3989. return 1;
  3990. }
  3991. static int trigger_late_session_ticket(struct helper *h, struct helper_local *hl)
  3992. {
  3993. new_session_count = 0;
  3994. if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S())))
  3995. return 0;
  3996. return 1;
  3997. }
  3998. static int check_got_session_ticket(struct helper *h, struct helper_local *hl)
  3999. {
  4000. if (!TEST_size_t_gt(new_session_count, 0))
  4001. return 0;
  4002. return 1;
  4003. }
  4004. static int check_idle_timeout(struct helper *h, struct helper_local *hl);
  4005. static const struct script_op script_78[] = {
  4006. OP_C_SET_ALPN ("ossltest")
  4007. OP_CHECK (setup_session, 0)
  4008. OP_C_CONNECT_WAIT ()
  4009. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4010. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4011. OP_C_WRITE (a, "apple", 5)
  4012. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4013. OP_S_READ_EXPECT (a, "apple", 5)
  4014. OP_S_WRITE (a, "orange", 6)
  4015. OP_C_READ_EXPECT (a, "orange", 6)
  4016. OP_CHECK (trigger_late_session_ticket, 0)
  4017. OP_S_WRITE (a, "Strawberry", 10)
  4018. OP_C_READ_EXPECT (a, "Strawberry", 10)
  4019. OP_CHECK (check_got_session_ticket, 0)
  4020. OP_CHECK2 (check_idle_timeout,
  4021. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
  4022. OP_END
  4023. };
  4024. /* 79. Optimised FIN test */
  4025. static const struct script_op script_79[] = {
  4026. OP_C_SET_ALPN ("ossltest")
  4027. OP_C_CONNECT_WAIT ()
  4028. OP_C_WRITE_EX2 (DEFAULT, "apple", 5, SSL_WRITE_FLAG_CONCLUDE)
  4029. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4030. OP_S_READ_EXPECT (a, "apple", 5)
  4031. OP_S_EXPECT_FIN (a)
  4032. OP_S_WRITE (a, "orange", 6)
  4033. OP_S_CONCLUDE (a)
  4034. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  4035. OP_C_EXPECT_FIN (DEFAULT)
  4036. OP_END
  4037. };
  4038. /* 80. Stateless reset detection test */
  4039. static QUIC_STATELESS_RESET_TOKEN test_reset_token = {
  4040. { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
  4041. 0xde, 0xad, 0xbe, 0xef }};
  4042. /*
  4043. * Generate a packet in the following format:
  4044. * https://www.rfc-editor.org/rfc/rfc9000.html#name-stateless-reset
  4045. * Stateless Reset {
  4046. * Fixed Bits (2): 1
  4047. * Unpredictable bits (38..)
  4048. * Stateless reset token (128)
  4049. * }
  4050. */
  4051. static int script_80_send_stateless_reset(struct helper *h, QUIC_PKT_HDR *hdr,
  4052. unsigned char *buf, size_t len)
  4053. {
  4054. unsigned char databuf[64];
  4055. if (h->inject_word1 == 0)
  4056. return 1;
  4057. h->inject_word1 = 0;
  4058. fprintf(stderr, "Sending stateless reset\n");
  4059. RAND_bytes(databuf, 64);
  4060. databuf[0] = 0x40;
  4061. memcpy(&databuf[48], test_reset_token.token,
  4062. sizeof(test_reset_token.token));
  4063. if (!TEST_int_eq(SSL_inject_net_dgram(h->c_conn, databuf, sizeof(databuf),
  4064. NULL, h->s_net_bio_addr), 1))
  4065. return 0;
  4066. return 1;
  4067. }
  4068. static int script_80_gen_new_conn_id(struct helper *h, QUIC_PKT_HDR *hdr,
  4069. unsigned char *buf, size_t len)
  4070. {
  4071. int rc = 0;
  4072. size_t l;
  4073. unsigned char frame_buf[64];
  4074. WPACKET wpkt;
  4075. QUIC_CONN_ID new_cid = {0};
  4076. OSSL_QUIC_FRAME_NEW_CONN_ID ncid = {0};
  4077. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S_NOHL());
  4078. if (h->inject_word0 == 0)
  4079. return 1;
  4080. h->inject_word0 = 0;
  4081. fprintf(stderr, "sending new conn id\n");
  4082. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  4083. sizeof(frame_buf), 0)))
  4084. return 0;
  4085. ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
  4086. ncid.seq_num = 2;
  4087. ncid.retire_prior_to = 2;
  4088. ncid.conn_id = new_cid;
  4089. memcpy(ncid.stateless_reset.token, test_reset_token.token,
  4090. sizeof(test_reset_token.token));
  4091. if (!TEST_true(ossl_quic_wire_encode_frame_new_conn_id(&wpkt, &ncid)))
  4092. goto err;
  4093. if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
  4094. goto err;
  4095. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, l))
  4096. goto err;
  4097. rc = 1;
  4098. err:
  4099. if (rc)
  4100. WPACKET_finish(&wpkt);
  4101. else
  4102. WPACKET_cleanup(&wpkt);
  4103. return rc;
  4104. }
  4105. static int script_80_inject_pkt(struct helper *h, QUIC_PKT_HDR *hdr,
  4106. unsigned char *buf, size_t len)
  4107. {
  4108. if (h->inject_word1 == 1)
  4109. return script_80_send_stateless_reset(h, hdr, buf, len);
  4110. else if (h->inject_word0 == 1)
  4111. return script_80_gen_new_conn_id(h, hdr, buf, len);
  4112. return 1;
  4113. }
  4114. static const struct script_op script_80[] = {
  4115. OP_S_SET_INJECT_PLAIN (script_80_inject_pkt)
  4116. OP_C_SET_ALPN ("ossltest")
  4117. OP_C_CONNECT_WAIT ()
  4118. OP_C_WRITE (DEFAULT, "apple", 5)
  4119. OP_C_CONCLUDE (DEFAULT)
  4120. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4121. OP_S_READ_EXPECT (a, "apple", 5)
  4122. OP_SET_INJECT_WORD (1, 0)
  4123. OP_S_WRITE (a, "apple", 5)
  4124. OP_C_READ_EXPECT (DEFAULT, "apple", 5)
  4125. OP_SET_INJECT_WORD (0, 1)
  4126. OP_S_WRITE (a, "apple", 5)
  4127. OP_C_EXPECT_CONN_CLOSE_INFO (0, 0, 1)
  4128. OP_END
  4129. };
  4130. /* 81. Idle timeout configuration */
  4131. static int modify_idle_timeout(struct helper *h, struct helper_local *hl)
  4132. {
  4133. uint64_t v = 0;
  4134. /* Test bad value is rejected. */
  4135. if (!TEST_false(SSL_set_feature_request_uint(h->c_conn,
  4136. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4137. (1ULL << 62))))
  4138. return 0;
  4139. /* Set value. */
  4140. if (!TEST_true(SSL_set_feature_request_uint(h->c_conn,
  4141. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4142. hl->check_op->arg2)))
  4143. return 0;
  4144. if (!TEST_true(SSL_get_feature_request_uint(h->c_conn,
  4145. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4146. &v)))
  4147. return 0;
  4148. if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
  4149. return 0;
  4150. return 1;
  4151. }
  4152. static int check_idle_timeout(struct helper *h, struct helper_local *hl)
  4153. {
  4154. uint64_t v = 0;
  4155. if (!TEST_true(SSL_get_value_uint(h->c_conn, hl->check_op->arg1,
  4156. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4157. &v)))
  4158. return 0;
  4159. if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
  4160. return 0;
  4161. return 1;
  4162. }
  4163. static const struct script_op script_81[] = {
  4164. OP_C_SET_ALPN ("ossltest")
  4165. OP_CHECK (modify_idle_timeout, 25000)
  4166. OP_C_CONNECT_WAIT ()
  4167. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4168. OP_CHECK2 (check_idle_timeout,
  4169. SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
  4170. OP_CHECK2 (check_idle_timeout,
  4171. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 25000)
  4172. OP_END
  4173. };
  4174. /* 82. Negotiated default idle timeout if not configured */
  4175. static const struct script_op script_82[] = {
  4176. OP_C_SET_ALPN ("ossltest")
  4177. OP_C_CONNECT_WAIT ()
  4178. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4179. OP_CHECK2 (check_idle_timeout,
  4180. SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
  4181. OP_CHECK2 (check_idle_timeout,
  4182. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
  4183. OP_END
  4184. };
  4185. /* 83. No late changes to idle timeout */
  4186. static int cannot_change_idle_timeout(struct helper *h, struct helper_local *hl)
  4187. {
  4188. uint64_t v = 0;
  4189. if (!TEST_true(SSL_get_feature_request_uint(h->c_conn,
  4190. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4191. &v)))
  4192. return 0;
  4193. if (!TEST_uint64_t_eq(v, 30000))
  4194. return 0;
  4195. if (!TEST_false(SSL_set_feature_request_uint(h->c_conn,
  4196. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4197. 5000)))
  4198. return 0;
  4199. return 1;
  4200. }
  4201. static const struct script_op script_83[] = {
  4202. OP_C_SET_ALPN ("ossltest")
  4203. OP_C_CONNECT_WAIT ()
  4204. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4205. OP_CHECK (cannot_change_idle_timeout, 0)
  4206. OP_CHECK2 (check_idle_timeout,
  4207. SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
  4208. OP_CHECK2 (check_idle_timeout,
  4209. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
  4210. OP_END
  4211. };
  4212. /* 84. Test query of available streams */
  4213. static int check_avail_streams(struct helper *h, struct helper_local *hl)
  4214. {
  4215. uint64_t v = 0;
  4216. switch (hl->check_op->arg1) {
  4217. case 0:
  4218. if (!TEST_true(SSL_get_quic_stream_bidi_local_avail(h->c_conn, &v)))
  4219. return 0;
  4220. break;
  4221. case 1:
  4222. if (!TEST_true(SSL_get_quic_stream_bidi_remote_avail(h->c_conn, &v)))
  4223. return 0;
  4224. break;
  4225. case 2:
  4226. if (!TEST_true(SSL_get_quic_stream_uni_local_avail(h->c_conn, &v)))
  4227. return 0;
  4228. break;
  4229. case 3:
  4230. if (!TEST_true(SSL_get_quic_stream_uni_remote_avail(h->c_conn, &v)))
  4231. return 0;
  4232. break;
  4233. default:
  4234. return 0;
  4235. }
  4236. if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
  4237. return 0;
  4238. return 1;
  4239. }
  4240. static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl);
  4241. static int reenable_test_event_handling(struct helper *h, struct helper_local *hl);
  4242. static int check_write_buf_stat(struct helper *h, struct helper_local *hl)
  4243. {
  4244. SSL *c_a;
  4245. uint64_t size, used, avail;
  4246. if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a")))
  4247. return 0;
  4248. if (!TEST_true(SSL_get_stream_write_buf_size(c_a, &size))
  4249. || !TEST_true(SSL_get_stream_write_buf_used(c_a, &used))
  4250. || !TEST_true(SSL_get_stream_write_buf_avail(c_a, &avail))
  4251. || !TEST_uint64_t_ge(size, avail)
  4252. || !TEST_uint64_t_ge(size, used)
  4253. || !TEST_uint64_t_eq(avail + used, size))
  4254. return 0;
  4255. if (!TEST_uint64_t_eq(used, hl->check_op->arg1))
  4256. return 0;
  4257. return 1;
  4258. }
  4259. static const struct script_op script_84[] = {
  4260. OP_C_SET_ALPN ("ossltest")
  4261. OP_C_CONNECT_WAIT ()
  4262. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4263. OP_CHECK2 (check_avail_streams, 0, 100)
  4264. OP_CHECK2 (check_avail_streams, 1, 100)
  4265. OP_CHECK2 (check_avail_streams, 2, 100)
  4266. OP_CHECK2 (check_avail_streams, 3, 100)
  4267. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4268. OP_CHECK2 (check_avail_streams, 0, 99)
  4269. OP_CHECK2 (check_avail_streams, 1, 100)
  4270. OP_CHECK2 (check_avail_streams, 2, 100)
  4271. OP_CHECK2 (check_avail_streams, 3, 100)
  4272. OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
  4273. OP_CHECK2 (check_avail_streams, 0, 99)
  4274. OP_CHECK2 (check_avail_streams, 1, 100)
  4275. OP_CHECK2 (check_avail_streams, 2, 99)
  4276. OP_CHECK2 (check_avail_streams, 3, 100)
  4277. OP_S_NEW_STREAM_BIDI (c, S_BIDI_ID(0))
  4278. OP_S_WRITE (c, "x", 1)
  4279. OP_C_ACCEPT_STREAM_WAIT (c)
  4280. OP_C_READ_EXPECT (c, "x", 1)
  4281. OP_CHECK2 (check_avail_streams, 0, 99)
  4282. OP_CHECK2 (check_avail_streams, 1, 99)
  4283. OP_CHECK2 (check_avail_streams, 2, 99)
  4284. OP_CHECK2 (check_avail_streams, 3, 100)
  4285. OP_S_NEW_STREAM_UNI (d, S_UNI_ID(0))
  4286. OP_S_WRITE (d, "x", 1)
  4287. OP_C_ACCEPT_STREAM_WAIT (d)
  4288. OP_C_READ_EXPECT (d, "x", 1)
  4289. OP_CHECK2 (check_avail_streams, 0, 99)
  4290. OP_CHECK2 (check_avail_streams, 1, 99)
  4291. OP_CHECK2 (check_avail_streams, 2, 99)
  4292. OP_CHECK2 (check_avail_streams, 3, 99)
  4293. OP_CHECK2 (check_write_buf_stat, 0, 0)
  4294. OP_CHECK (set_event_handling_mode_conn,
  4295. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4296. OP_C_WRITE (a, "apple", 5)
  4297. OP_CHECK2 (check_write_buf_stat, 5, 0)
  4298. OP_CHECK (reenable_test_event_handling, 0)
  4299. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4300. OP_S_READ_EXPECT (a, "apple", 5)
  4301. OP_S_WRITE (a, "orange", 6)
  4302. OP_C_READ_EXPECT (a, "orange", 6)
  4303. OP_CHECK2 (check_write_buf_stat, 0, 0)
  4304. OP_END
  4305. };
  4306. /* 85. Test SSL_poll (lite, non-blocking) */
  4307. ossl_unused static int script_85_poll(struct helper *h, struct helper_local *hl)
  4308. {
  4309. int ok = 1, ret, expected_ret = 1;
  4310. static const struct timeval timeout = {0};
  4311. static const struct timeval nz_timeout = {0, 1};
  4312. size_t result_count, expected_result_count = 0;
  4313. SSL_POLL_ITEM items[5] = {0}, *item = items;
  4314. SSL *c_a, *c_b, *c_c, *c_d;
  4315. size_t i;
  4316. uint64_t mode, expected_revents[5] = {0};
  4317. if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a"))
  4318. || !TEST_ptr(c_b = helper_local_get_c_stream(hl, "b"))
  4319. || !TEST_ptr(c_c = helper_local_get_c_stream(hl, "c"))
  4320. || !TEST_ptr(c_d = helper_local_get_c_stream(hl, "d")))
  4321. return 0;
  4322. item->desc = SSL_as_poll_descriptor(c_a);
  4323. item->events = UINT64_MAX;
  4324. item->revents = UINT64_MAX;
  4325. ++item;
  4326. item->desc = SSL_as_poll_descriptor(c_b);
  4327. item->events = UINT64_MAX;
  4328. item->revents = UINT64_MAX;
  4329. ++item;
  4330. item->desc = SSL_as_poll_descriptor(c_c);
  4331. item->events = UINT64_MAX;
  4332. item->revents = UINT64_MAX;
  4333. ++item;
  4334. item->desc = SSL_as_poll_descriptor(c_d);
  4335. item->events = UINT64_MAX;
  4336. item->revents = UINT64_MAX;
  4337. ++item;
  4338. item->desc = SSL_as_poll_descriptor(h->c_conn);
  4339. item->events = UINT64_MAX;
  4340. item->revents = UINT64_MAX;
  4341. ++item;
  4342. /* Non-zero timeout is not supported. */
  4343. result_count = SIZE_MAX;
  4344. ERR_set_mark();
  4345. if (!TEST_false(SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM),
  4346. &nz_timeout, 0,
  4347. &result_count))
  4348. || !TEST_size_t_eq(result_count, 0))
  4349. return 0;
  4350. ERR_pop_to_mark();
  4351. result_count = SIZE_MAX;
  4352. ret = SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM),
  4353. &timeout, 0,
  4354. &result_count);
  4355. mode = hl->check_op->arg2;
  4356. switch (mode) {
  4357. case 0:
  4358. /* No incoming data yet */
  4359. expected_revents[0] = SSL_POLL_EVENT_W;
  4360. expected_revents[1] = SSL_POLL_EVENT_W;
  4361. expected_revents[2] = SSL_POLL_EVENT_W;
  4362. expected_revents[3] = SSL_POLL_EVENT_W;
  4363. expected_revents[4] = SSL_POLL_EVENT_OS;
  4364. expected_result_count = 5;
  4365. break;
  4366. case 1:
  4367. /* Expect more events */
  4368. expected_revents[0] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_R;
  4369. expected_revents[1] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_ER;
  4370. expected_revents[2] = SSL_POLL_EVENT_EW;
  4371. expected_revents[3] = SSL_POLL_EVENT_W;
  4372. expected_revents[4] = SSL_POLL_EVENT_OS | SSL_POLL_EVENT_ISB;
  4373. expected_result_count = 5;
  4374. break;
  4375. default:
  4376. return 0;
  4377. }
  4378. if (!TEST_int_eq(ret, expected_ret)
  4379. || !TEST_size_t_eq(result_count, expected_result_count))
  4380. ok = 0;
  4381. for (i = 0; i < OSSL_NELEM(items); ++i)
  4382. if (!TEST_uint64_t_eq(items[i].revents, expected_revents[i])) {
  4383. TEST_error("mismatch at index %zu in poll results, mode %d",
  4384. i, (int)mode);
  4385. ok = 0;
  4386. }
  4387. return ok;
  4388. }
  4389. static const struct script_op script_85[] = {
  4390. OP_C_SET_ALPN ("ossltest")
  4391. OP_C_CONNECT_WAIT ()
  4392. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4393. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4394. OP_C_WRITE (a, "flamingo", 8)
  4395. OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
  4396. OP_C_WRITE (b, "orange", 6)
  4397. OP_C_NEW_STREAM_BIDI (c, C_BIDI_ID(2))
  4398. OP_C_WRITE (c, "Strawberry", 10)
  4399. OP_C_NEW_STREAM_BIDI (d, C_BIDI_ID(3))
  4400. OP_C_WRITE (d, "sync", 4)
  4401. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4402. OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
  4403. OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2))
  4404. OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3))
  4405. /* Check nothing readable yet. */
  4406. OP_CHECK (script_85_poll, 0)
  4407. /* Send something that will make client sockets readable. */
  4408. OP_S_READ_EXPECT (a, "flamingo", 8)
  4409. OP_S_WRITE (a, "herringbone", 11)
  4410. /* Send something that will make 'b' reset. */
  4411. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  4412. OP_SET_INJECT_WORD (C_BIDI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  4413. /* Ensure sync. */
  4414. OP_S_READ_EXPECT (d, "sync", 4)
  4415. OP_S_WRITE (d, "x", 1)
  4416. OP_C_READ_EXPECT (d, "x", 1)
  4417. /* Send something that will make 'c' reset. */
  4418. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  4419. OP_SET_INJECT_WORD (C_BIDI_ID(2) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  4420. OP_S_NEW_STREAM_BIDI (z, S_BIDI_ID(0))
  4421. OP_S_WRITE (z, "z", 1)
  4422. /* Ensure sync. */
  4423. OP_S_WRITE (d, "x", 1)
  4424. OP_C_READ_EXPECT (d, "x", 1)
  4425. /* Check a is now readable. */
  4426. OP_CHECK (script_85_poll, 1)
  4427. OP_END
  4428. };
  4429. /* 86. Event Handling Mode Configuration */
  4430. static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl)
  4431. {
  4432. hl->explicit_event_handling = 1;
  4433. return SSL_set_event_handling_mode(h->c_conn, hl->check_op->arg2);
  4434. }
  4435. static int reenable_test_event_handling(struct helper *h, struct helper_local *hl)
  4436. {
  4437. hl->explicit_event_handling = 0;
  4438. return 1;
  4439. }
  4440. static ossl_unused int set_event_handling_mode_stream(struct helper *h, struct helper_local *hl)
  4441. {
  4442. SSL *ssl = helper_local_get_c_stream(hl, "a");
  4443. if (!TEST_ptr(ssl))
  4444. return 0;
  4445. return SSL_set_event_handling_mode(ssl, hl->check_op->arg2);
  4446. }
  4447. static const struct script_op script_86[] = {
  4448. OP_SKIP_IF_BLOCKING (23)
  4449. OP_C_SET_ALPN ("ossltest")
  4450. OP_C_CONNECT_WAIT ()
  4451. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4452. /* Turn on explicit handling mode. */
  4453. OP_CHECK (set_event_handling_mode_conn,
  4454. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4455. /*
  4456. * Create a new stream and write data. This won't get sent
  4457. * to the network net because we are in explicit mode
  4458. * and we haven't called SSL_handle_events().
  4459. */
  4460. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4461. OP_C_WRITE (a, "apple", 5)
  4462. /* Put connection back into implicit handling mode. */
  4463. OP_CHECK (set_event_handling_mode_conn,
  4464. SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT)
  4465. /* Override at stream level. */
  4466. OP_CHECK (set_event_handling_mode_stream,
  4467. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4468. OP_C_WRITE (a, "orange", 6)
  4469. OP_C_CONCLUDE (a)
  4470. /*
  4471. * Confirm the data isn't going to arrive. OP_SLEEP is always undesirable
  4472. * but we have no reasonable way to synchronise on something not arriving
  4473. * given all network traffic is essentially stopped and there are no other
  4474. * signals arriving from the peer which could be used for synchronisation.
  4475. * Slow OSes will pass this anyway (fail-open).
  4476. */
  4477. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4478. OP_BEGIN_REPEAT (20)
  4479. OP_S_READ_FAIL (a, 1)
  4480. OP_SLEEP (10)
  4481. OP_END_REPEAT ()
  4482. /* Now let the data arrive and confirm it arrives. */
  4483. OP_CHECK (reenable_test_event_handling, 0)
  4484. OP_S_READ_EXPECT (a, "appleorange", 11)
  4485. OP_S_EXPECT_FIN (a)
  4486. /* Back into explicit mode. */
  4487. OP_CHECK (set_event_handling_mode_conn,
  4488. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4489. OP_S_WRITE (a, "ok", 2)
  4490. OP_C_READ_FAIL (a)
  4491. /* Works once event handling is done. */
  4492. OP_CHECK (reenable_test_event_handling, 0)
  4493. OP_C_READ_EXPECT (a, "ok", 2)
  4494. OP_END
  4495. };
  4496. /* 87. Test stream reset functionality */
  4497. static const struct script_op script_87[] = {
  4498. OP_C_SET_ALPN ("ossltest")
  4499. OP_C_CONNECT_WAIT ()
  4500. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4501. OP_C_WRITE (a, "apple", 5)
  4502. OP_C_CONCLUDE (a)
  4503. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4504. OP_S_READ_EXPECT (a, "apple", 5)
  4505. OP_S_EXPECT_FIN (a)
  4506. OP_S_WRITE (a, "orange", 6)
  4507. OP_C_READ_EXPECT (a, "orange", 6)
  4508. OP_S_CONCLUDE (a)
  4509. OP_C_EXPECT_FIN (a)
  4510. OP_SLEEP (1000)
  4511. OP_C_STREAM_RESET_FAIL (a, 42)
  4512. OP_END
  4513. };
  4514. static const struct script_op *const scripts[] = {
  4515. script_1,
  4516. script_2,
  4517. script_3,
  4518. script_4,
  4519. script_5,
  4520. script_6,
  4521. script_7,
  4522. script_8,
  4523. script_9,
  4524. script_10,
  4525. script_11,
  4526. script_12,
  4527. script_13,
  4528. script_14,
  4529. script_15,
  4530. script_16,
  4531. script_17,
  4532. script_18,
  4533. script_19,
  4534. script_20,
  4535. script_21,
  4536. script_22,
  4537. script_23,
  4538. script_24,
  4539. script_25,
  4540. script_26,
  4541. script_27,
  4542. script_28,
  4543. script_29,
  4544. script_30,
  4545. script_31,
  4546. script_32,
  4547. script_33,
  4548. script_34,
  4549. script_35,
  4550. script_36,
  4551. script_37,
  4552. script_38,
  4553. script_39,
  4554. script_40,
  4555. script_41,
  4556. script_42,
  4557. script_43,
  4558. script_44,
  4559. script_45,
  4560. script_46,
  4561. script_47,
  4562. script_48,
  4563. script_49,
  4564. script_50,
  4565. script_51,
  4566. script_52,
  4567. script_53,
  4568. script_54,
  4569. script_55,
  4570. script_56,
  4571. script_57,
  4572. script_58,
  4573. script_59,
  4574. script_60,
  4575. script_61,
  4576. script_62,
  4577. script_63,
  4578. script_64,
  4579. script_65,
  4580. script_66,
  4581. script_67,
  4582. script_68,
  4583. script_69,
  4584. script_70,
  4585. script_71,
  4586. script_72,
  4587. script_73,
  4588. script_74,
  4589. script_75,
  4590. script_76,
  4591. script_77,
  4592. script_78,
  4593. script_79,
  4594. script_80,
  4595. script_81,
  4596. script_82,
  4597. script_83,
  4598. script_84,
  4599. script_85,
  4600. script_86,
  4601. script_87
  4602. };
  4603. static int test_script(int idx)
  4604. {
  4605. int script_idx, free_order, blocking;
  4606. char script_name[64];
  4607. free_order = idx % 2;
  4608. idx /= 2;
  4609. blocking = idx % 2;
  4610. idx /= 2;
  4611. script_idx = idx;
  4612. if (blocking && free_order)
  4613. return 1; /* don't need to test free_order twice */
  4614. #if !defined(OPENSSL_THREADS)
  4615. if (blocking) {
  4616. TEST_skip("cannot test in blocking mode without threads");
  4617. return 1;
  4618. }
  4619. #endif
  4620. BIO_snprintf(script_name, sizeof(script_name), "script %d", script_idx + 1);
  4621. TEST_info("Running script %d (order=%d, blocking=%d)", script_idx + 1,
  4622. free_order, blocking);
  4623. return run_script(scripts[script_idx], script_name, free_order, blocking);
  4624. }
  4625. /* Dynamically generated tests. */
  4626. static struct script_op dyn_frame_types_script[] = {
  4627. OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
  4628. OP_SET_INJECT_WORD (0, 0) /* dynamic */
  4629. OP_C_SET_ALPN ("ossltest")
  4630. OP_C_CONNECT_WAIT_OR_FAIL()
  4631. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  4632. OP_END
  4633. };
  4634. struct forbidden_frame_type {
  4635. uint64_t pkt_type, frame_type, expected_err;
  4636. };
  4637. static const struct forbidden_frame_type forbidden_frame_types[] = {
  4638. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
  4639. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
  4640. { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
  4641. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4642. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4643. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4644. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4645. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4646. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4647. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4648. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4649. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4650. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4651. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4652. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4653. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4654. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4655. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4656. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4657. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4658. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4659. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4660. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4661. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4662. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4663. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4664. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4665. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4666. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4667. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4668. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4669. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4670. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4671. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4672. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4673. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4674. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4675. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4676. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4677. /* Client uses a zero-length CID so this is not allowed. */
  4678. { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4679. };
  4680. static ossl_unused int test_dyn_frame_types(int idx)
  4681. {
  4682. size_t i;
  4683. char script_name[64];
  4684. struct script_op *s = dyn_frame_types_script;
  4685. for (i = 0; i < OSSL_NELEM(dyn_frame_types_script); ++i)
  4686. if (s[i].op == OPK_SET_INJECT_WORD) {
  4687. s[i].arg1 = (size_t)forbidden_frame_types[idx].pkt_type;
  4688. s[i].arg2 = forbidden_frame_types[idx].frame_type;
  4689. } else if (s[i].op == OPK_C_EXPECT_CONN_CLOSE_INFO) {
  4690. s[i].arg2 = forbidden_frame_types[idx].expected_err;
  4691. }
  4692. BIO_snprintf(script_name, sizeof(script_name),
  4693. "dyn script %d", idx);
  4694. return run_script(dyn_frame_types_script, script_name, 0, 0);
  4695. }
  4696. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  4697. int setup_tests(void)
  4698. {
  4699. #if defined (_PUT_MODEL_)
  4700. return TEST_skip("QUIC is not supported by this build");
  4701. #endif
  4702. if (!test_skip_common_options()) {
  4703. TEST_error("Error parsing test options\n");
  4704. return 0;
  4705. }
  4706. if (!TEST_ptr(certfile = test_get_argument(0))
  4707. || !TEST_ptr(keyfile = test_get_argument(1)))
  4708. return 0;
  4709. ADD_ALL_TESTS(test_dyn_frame_types, OSSL_NELEM(forbidden_frame_types));
  4710. ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2 * 2);
  4711. return 1;
  4712. }