quic_multistream_test.c 177 KB

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