quic_multistream_test.c 177 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938
  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. BIO_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_SLEEP (10)
  1923. OP_C_EXPECT_FIN (a)
  1924. OP_END
  1925. };
  1926. static const struct script_op script_11[] = {
  1927. OP_C_SET_ALPN ("ossltest")
  1928. OP_C_CONNECT_WAIT ()
  1929. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1930. OP_NEW_THREAD (5, script_11_child)
  1931. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  1932. OP_S_WRITE (a, "foo", 3)
  1933. OP_S_CONCLUDE (a)
  1934. OP_S_NEW_STREAM_BIDI (b, ANY_ID)
  1935. OP_S_WRITE (b, "foo", 3)
  1936. OP_S_CONCLUDE (b)
  1937. OP_S_NEW_STREAM_BIDI (c, ANY_ID)
  1938. OP_S_WRITE (c, "foo", 3)
  1939. OP_S_CONCLUDE (c)
  1940. OP_S_NEW_STREAM_BIDI (d, ANY_ID)
  1941. OP_S_WRITE (d, "foo", 3)
  1942. OP_S_CONCLUDE (d)
  1943. OP_S_NEW_STREAM_BIDI (e, ANY_ID)
  1944. OP_S_WRITE (e, "foo", 3)
  1945. OP_S_CONCLUDE (e)
  1946. OP_END
  1947. };
  1948. /* 12. Many threads initiated on the same client connection */
  1949. static const struct script_op script_12_child[] = {
  1950. OP_C_NEW_STREAM_BIDI (a, ANY_ID)
  1951. OP_C_WRITE (a, "foo", 3)
  1952. OP_C_CONCLUDE (a)
  1953. OP_C_FREE_STREAM (a)
  1954. OP_END
  1955. };
  1956. static const struct script_op script_12[] = {
  1957. OP_C_SET_ALPN ("ossltest")
  1958. OP_C_CONNECT_WAIT ()
  1959. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1960. OP_NEW_THREAD (5, script_12_child)
  1961. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  1962. OP_S_READ_EXPECT (a, "foo", 3)
  1963. OP_S_EXPECT_FIN (a)
  1964. OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
  1965. OP_S_READ_EXPECT (b, "foo", 3)
  1966. OP_S_EXPECT_FIN (b)
  1967. OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2))
  1968. OP_S_READ_EXPECT (c, "foo", 3)
  1969. OP_S_EXPECT_FIN (c)
  1970. OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3))
  1971. OP_S_READ_EXPECT (d, "foo", 3)
  1972. OP_S_EXPECT_FIN (d)
  1973. OP_S_BIND_STREAM_ID (e, C_BIDI_ID(4))
  1974. OP_S_READ_EXPECT (e, "foo", 3)
  1975. OP_S_EXPECT_FIN (e)
  1976. OP_END
  1977. };
  1978. /* 13. Many threads accepted on the same client connection (stress test) */
  1979. static const struct script_op script_13_child[] = {
  1980. OP_BEGIN_REPEAT (10)
  1981. OP_C_ACCEPT_STREAM_WAIT (a)
  1982. OP_C_READ_EXPECT (a, "foo", 3)
  1983. OP_C_EXPECT_FIN (a)
  1984. OP_C_FREE_STREAM (a)
  1985. OP_END_REPEAT ()
  1986. OP_END
  1987. };
  1988. static const struct script_op script_13[] = {
  1989. OP_C_SET_ALPN ("ossltest")
  1990. OP_C_CONNECT_WAIT ()
  1991. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  1992. OP_NEW_THREAD (5, script_13_child)
  1993. OP_BEGIN_REPEAT (50)
  1994. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  1995. OP_S_WRITE (a, "foo", 3)
  1996. OP_S_CONCLUDE (a)
  1997. OP_S_UNBIND_STREAM_ID (a)
  1998. OP_END_REPEAT ()
  1999. OP_END
  2000. };
  2001. /* 14. Many threads initiating on the same client connection (stress test) */
  2002. static const struct script_op script_14_child[] = {
  2003. OP_BEGIN_REPEAT (10)
  2004. OP_C_NEW_STREAM_BIDI (a, ANY_ID)
  2005. OP_C_WRITE (a, "foo", 3)
  2006. OP_C_CONCLUDE (a)
  2007. OP_C_FREE_STREAM (a)
  2008. OP_END_REPEAT ()
  2009. OP_END
  2010. };
  2011. static const struct script_op script_14[] = {
  2012. OP_C_SET_ALPN ("ossltest")
  2013. OP_C_CONNECT_WAIT ()
  2014. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2015. OP_NEW_THREAD (5, script_14_child)
  2016. OP_BEGIN_REPEAT (50)
  2017. OP_S_ACCEPT_STREAM_WAIT (a)
  2018. OP_S_READ_EXPECT (a, "foo", 3)
  2019. OP_S_EXPECT_FIN (a)
  2020. OP_S_UNBIND_STREAM_ID (a)
  2021. OP_END_REPEAT ()
  2022. OP_END
  2023. };
  2024. /* 15. Client sending large number of streams, MAX_STREAMS test */
  2025. static const struct script_op script_15[] = {
  2026. OP_C_SET_ALPN ("ossltest")
  2027. OP_C_CONNECT_WAIT ()
  2028. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2029. /*
  2030. * This will cause a protocol violation to be raised by the server if we are
  2031. * not handling the stream limit correctly on the TX side.
  2032. */
  2033. OP_BEGIN_REPEAT (200)
  2034. OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, SSL_STREAM_FLAG_ADVANCE)
  2035. OP_C_WRITE (a, "foo", 3)
  2036. OP_C_CONCLUDE (a)
  2037. OP_C_FREE_STREAM (a)
  2038. OP_END_REPEAT ()
  2039. /* Prove the connection is still good. */
  2040. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  2041. OP_S_WRITE (a, "bar", 3)
  2042. OP_S_CONCLUDE (a)
  2043. OP_C_ACCEPT_STREAM_WAIT (a)
  2044. OP_C_READ_EXPECT (a, "bar", 3)
  2045. OP_C_EXPECT_FIN (a)
  2046. /*
  2047. * Drain the queue of incoming streams. We should be able to get all 200
  2048. * even though only 100 can be initiated at a time.
  2049. */
  2050. OP_BEGIN_REPEAT (200)
  2051. OP_S_ACCEPT_STREAM_WAIT (b)
  2052. OP_S_READ_EXPECT (b, "foo", 3)
  2053. OP_S_EXPECT_FIN (b)
  2054. OP_S_UNBIND_STREAM_ID (b)
  2055. OP_END_REPEAT ()
  2056. OP_END
  2057. };
  2058. /* 16. Server sending large number of streams, MAX_STREAMS test */
  2059. static const struct script_op script_16[] = {
  2060. OP_C_SET_ALPN ("ossltest")
  2061. OP_C_CONNECT_WAIT ()
  2062. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2063. /*
  2064. * This will cause a protocol violation to be raised by the client if we are
  2065. * not handling the stream limit correctly on the TX side.
  2066. */
  2067. OP_BEGIN_REPEAT (200)
  2068. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  2069. OP_S_WRITE (a, "foo", 3)
  2070. OP_S_CONCLUDE (a)
  2071. OP_S_UNBIND_STREAM_ID (a)
  2072. OP_END_REPEAT ()
  2073. /* Prove that the connection is still good. */
  2074. OP_C_NEW_STREAM_BIDI (a, ANY_ID)
  2075. OP_C_WRITE (a, "bar", 3)
  2076. OP_C_CONCLUDE (a)
  2077. OP_S_ACCEPT_STREAM_WAIT (b)
  2078. OP_S_READ_EXPECT (b, "bar", 3)
  2079. OP_S_EXPECT_FIN (b)
  2080. /* Drain the queue of incoming streams. */
  2081. OP_BEGIN_REPEAT (200)
  2082. OP_C_ACCEPT_STREAM_WAIT (b)
  2083. OP_C_READ_EXPECT (b, "foo", 3)
  2084. OP_C_EXPECT_FIN (b)
  2085. OP_C_FREE_STREAM (b)
  2086. OP_END_REPEAT ()
  2087. OP_END
  2088. };
  2089. /* 17. Key update test - unlimited */
  2090. static const struct script_op script_17[] = {
  2091. OP_C_SET_ALPN ("ossltest")
  2092. OP_C_CONNECT_WAIT ()
  2093. OP_C_WRITE (DEFAULT, "apple", 5)
  2094. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2095. OP_S_READ_EXPECT (a, "apple", 5)
  2096. OP_CHECK (override_key_update, 1)
  2097. OP_BEGIN_REPEAT (200)
  2098. OP_C_WRITE (DEFAULT, "apple", 5)
  2099. OP_S_READ_EXPECT (a, "apple", 5)
  2100. /*
  2101. * TXKU frequency is bounded by RTT because a previous TXKU needs to be
  2102. * acknowledged by the peer first before another one can be begin. By
  2103. * waiting this long, we eliminate any such concern and ensure as many key
  2104. * updates as possible can occur for the purposes of this test.
  2105. */
  2106. OP_CHECK (skip_time_ms, 100)
  2107. OP_END_REPEAT ()
  2108. /* At least 5 RXKUs detected */
  2109. OP_CHECK (check_key_update_ge, 5)
  2110. /*
  2111. * Prove the connection is still healthy by sending something in both
  2112. * directions.
  2113. */
  2114. OP_C_WRITE (DEFAULT, "xyzzy", 5)
  2115. OP_S_READ_EXPECT (a, "xyzzy", 5)
  2116. OP_S_WRITE (a, "plugh", 5)
  2117. OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
  2118. OP_END
  2119. };
  2120. /* 18. Key update test - RTT-bounded */
  2121. static const struct script_op script_18[] = {
  2122. OP_C_SET_ALPN ("ossltest")
  2123. OP_C_CONNECT_WAIT ()
  2124. OP_C_WRITE (DEFAULT, "apple", 5)
  2125. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2126. OP_S_READ_EXPECT (a, "apple", 5)
  2127. OP_CHECK (override_key_update, 1)
  2128. OP_BEGIN_REPEAT (200)
  2129. OP_C_WRITE (DEFAULT, "apple", 5)
  2130. OP_S_READ_EXPECT (a, "apple", 5)
  2131. OP_CHECK (skip_time_ms, 8)
  2132. OP_END_REPEAT ()
  2133. /*
  2134. * This time we simulate far less time passing between writes, so there are
  2135. * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every
  2136. * 1 packet above, which is absurd; thus this ensures we only actually
  2137. * generate TXKUs when we are allowed to.
  2138. */
  2139. OP_CHECK (check_key_update_lt, 240)
  2140. /*
  2141. * Prove the connection is still healthy by sending something in both
  2142. * directions.
  2143. */
  2144. OP_C_WRITE (DEFAULT, "xyzzy", 5)
  2145. OP_S_READ_EXPECT (a, "xyzzy", 5)
  2146. OP_S_WRITE (a, "plugh", 5)
  2147. OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
  2148. OP_END
  2149. };
  2150. /* 19. Key update test - artificially triggered */
  2151. static const struct script_op script_19[] = {
  2152. OP_C_SET_ALPN ("ossltest")
  2153. OP_C_CONNECT_WAIT ()
  2154. OP_C_WRITE (DEFAULT, "apple", 5)
  2155. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2156. OP_S_READ_EXPECT (a, "apple", 5)
  2157. OP_C_WRITE (DEFAULT, "orange", 6)
  2158. OP_S_READ_EXPECT (a, "orange", 6)
  2159. OP_S_WRITE (a, "strawberry", 10)
  2160. OP_C_READ_EXPECT (DEFAULT, "strawberry", 10)
  2161. OP_CHECK (check_key_update_lt, 1)
  2162. OP_CHECK (trigger_key_update, 0)
  2163. OP_C_WRITE (DEFAULT, "orange", 6)
  2164. OP_S_READ_EXPECT (a, "orange", 6)
  2165. OP_S_WRITE (a, "ok", 2)
  2166. OP_C_READ_EXPECT (DEFAULT, "ok", 2)
  2167. OP_CHECK (check_key_update_ge, 1)
  2168. OP_END
  2169. };
  2170. /* 20. Multiple threads accept stream with socket forcibly closed (error test) */
  2171. static int script_20_trigger(struct helper *h, volatile uint64_t *counter)
  2172. {
  2173. #if defined(OPENSSL_THREADS)
  2174. ossl_crypto_mutex_lock(h->misc_m);
  2175. ++*counter;
  2176. ossl_crypto_condvar_broadcast(h->misc_cv);
  2177. ossl_crypto_mutex_unlock(h->misc_m);
  2178. #endif
  2179. return 1;
  2180. }
  2181. static int script_20_wait(struct helper *h, volatile uint64_t *counter, uint64_t threshold)
  2182. {
  2183. #if defined(OPENSSL_THREADS)
  2184. int stop = 0;
  2185. ossl_crypto_mutex_lock(h->misc_m);
  2186. while (!stop) {
  2187. stop = (*counter >= threshold);
  2188. if (stop)
  2189. break;
  2190. ossl_crypto_condvar_wait(h->misc_cv, h->misc_m);
  2191. }
  2192. ossl_crypto_mutex_unlock(h->misc_m);
  2193. #endif
  2194. return 1;
  2195. }
  2196. static int script_20_trigger1(struct helper *h, struct helper_local *hl)
  2197. {
  2198. return script_20_trigger(h, &h->scratch0);
  2199. }
  2200. static int script_20_wait1(struct helper *h, struct helper_local *hl)
  2201. {
  2202. return script_20_wait(h, &h->scratch0, hl->check_op->arg2);
  2203. }
  2204. static int script_20_trigger2(struct helper *h, struct helper_local *hl)
  2205. {
  2206. return script_20_trigger(h, &h->scratch1);
  2207. }
  2208. static int script_20_wait2(struct helper *h, struct helper_local *hl)
  2209. {
  2210. return script_20_wait(h, &h->scratch1, hl->check_op->arg2);
  2211. }
  2212. static const struct script_op script_20_child[] = {
  2213. OP_C_ACCEPT_STREAM_WAIT (a)
  2214. OP_C_READ_EXPECT (a, "foo", 3)
  2215. OP_CHECK (script_20_trigger1, 0)
  2216. OP_CHECK (script_20_wait2, 1)
  2217. OP_C_READ_FAIL_WAIT (a)
  2218. OP_C_EXPECT_SSL_ERR (a, SSL_ERROR_SYSCALL)
  2219. OP_EXPECT_ERR_LIB (ERR_LIB_SSL)
  2220. OP_EXPECT_ERR_REASON (SSL_R_PROTOCOL_IS_SHUTDOWN)
  2221. OP_POP_ERR ()
  2222. OP_EXPECT_ERR_LIB (ERR_LIB_SSL)
  2223. OP_EXPECT_ERR_REASON (SSL_R_QUIC_NETWORK_ERROR)
  2224. OP_C_FREE_STREAM (a)
  2225. OP_END
  2226. };
  2227. static const struct script_op script_20[] = {
  2228. OP_C_SET_ALPN ("ossltest")
  2229. OP_C_CONNECT_WAIT ()
  2230. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2231. OP_NEW_THREAD (5, script_20_child)
  2232. OP_BEGIN_REPEAT (5)
  2233. OP_S_NEW_STREAM_BIDI (a, ANY_ID)
  2234. OP_S_WRITE (a, "foo", 3)
  2235. OP_S_UNBIND_STREAM_ID (a)
  2236. OP_END_REPEAT ()
  2237. OP_CHECK (script_20_wait1, 5)
  2238. OP_C_CLOSE_SOCKET ()
  2239. OP_CHECK (script_20_trigger2, 0)
  2240. OP_END
  2241. };
  2242. /* 21. Fault injection - unknown frame in 1-RTT packet */
  2243. static int script_21_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2244. unsigned char *buf, size_t len)
  2245. {
  2246. int ok = 0;
  2247. WPACKET wpkt;
  2248. unsigned char frame_buf[8];
  2249. size_t written;
  2250. if (h->inject_word0 == 0 || hdr->type != h->inject_word0)
  2251. return 1;
  2252. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2253. sizeof(frame_buf), 0)))
  2254. return 0;
  2255. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
  2256. goto err;
  2257. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2258. goto err;
  2259. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2260. goto err;
  2261. ok = 1;
  2262. err:
  2263. if (ok)
  2264. WPACKET_finish(&wpkt);
  2265. else
  2266. WPACKET_cleanup(&wpkt);
  2267. return ok;
  2268. }
  2269. static const struct script_op script_21[] = {
  2270. OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
  2271. OP_C_SET_ALPN ("ossltest")
  2272. OP_C_CONNECT_WAIT ()
  2273. OP_C_WRITE (DEFAULT, "apple", 5)
  2274. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2275. OP_S_READ_EXPECT (a, "apple", 5)
  2276. OP_SET_INJECT_WORD (QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX)
  2277. OP_S_WRITE (a, "orange", 6)
  2278. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2279. OP_END
  2280. };
  2281. /* 22. Fault injection - non-zero packet header reserved bits */
  2282. static int script_22_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2283. unsigned char *buf, size_t len)
  2284. {
  2285. if (h->inject_word0 == 0)
  2286. return 1;
  2287. hdr->reserved = 1;
  2288. return 1;
  2289. }
  2290. static const struct script_op script_22[] = {
  2291. OP_S_SET_INJECT_PLAIN (script_22_inject_plain)
  2292. OP_C_SET_ALPN ("ossltest")
  2293. OP_C_CONNECT_WAIT ()
  2294. OP_C_WRITE (DEFAULT, "apple", 5)
  2295. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2296. OP_S_READ_EXPECT (a, "apple", 5)
  2297. OP_SET_INJECT_WORD (1, 0)
  2298. OP_S_WRITE (a, "orange", 6)
  2299. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION,0,0)
  2300. OP_END
  2301. };
  2302. /* 23. Fault injection - empty NEW_TOKEN */
  2303. static int script_23_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2304. unsigned char *buf, size_t len)
  2305. {
  2306. int ok = 0;
  2307. WPACKET wpkt;
  2308. unsigned char frame_buf[16];
  2309. size_t written;
  2310. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2311. return 1;
  2312. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2313. sizeof(frame_buf), 0)))
  2314. return 0;
  2315. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN))
  2316. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
  2317. goto err;
  2318. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2319. goto err;
  2320. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2321. goto err;
  2322. ok = 1;
  2323. err:
  2324. if (ok)
  2325. WPACKET_finish(&wpkt);
  2326. else
  2327. WPACKET_cleanup(&wpkt);
  2328. return ok;
  2329. }
  2330. static const struct script_op script_23[] = {
  2331. OP_S_SET_INJECT_PLAIN (script_23_inject_plain)
  2332. OP_C_SET_ALPN ("ossltest")
  2333. OP_C_CONNECT_WAIT ()
  2334. OP_C_WRITE (DEFAULT, "apple", 5)
  2335. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2336. OP_S_READ_EXPECT (a, "apple", 5)
  2337. OP_SET_INJECT_WORD (1, 0)
  2338. OP_S_WRITE (a, "orange", 6)
  2339. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2340. OP_END
  2341. };
  2342. /* 24. Fault injection - excess value of MAX_STREAMS_BIDI */
  2343. static int script_24_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2344. unsigned char *buf, size_t len)
  2345. {
  2346. int ok = 0;
  2347. WPACKET wpkt;
  2348. unsigned char frame_buf[16];
  2349. size_t written;
  2350. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2351. return 1;
  2352. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2353. sizeof(frame_buf), 0)))
  2354. return 0;
  2355. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2356. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, (((uint64_t)1) << 60) + 1)))
  2357. goto err;
  2358. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2359. goto err;
  2360. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2361. goto err;
  2362. ok = 1;
  2363. err:
  2364. if (ok)
  2365. WPACKET_finish(&wpkt);
  2366. else
  2367. WPACKET_cleanup(&wpkt);
  2368. return ok;
  2369. }
  2370. static const struct script_op script_24[] = {
  2371. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2372. OP_C_SET_ALPN ("ossltest")
  2373. OP_C_CONNECT_WAIT ()
  2374. OP_C_WRITE (DEFAULT, "apple", 5)
  2375. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2376. OP_S_READ_EXPECT (a, "apple", 5)
  2377. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
  2378. OP_S_WRITE (a, "orange", 6)
  2379. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2380. OP_END
  2381. };
  2382. /* 25. Fault injection - excess value of MAX_STREAMS_UNI */
  2383. static const struct script_op script_25[] = {
  2384. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2385. OP_C_SET_ALPN ("ossltest")
  2386. OP_C_CONNECT_WAIT ()
  2387. OP_C_WRITE (DEFAULT, "apple", 5)
  2388. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2389. OP_S_READ_EXPECT (a, "apple", 5)
  2390. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)
  2391. OP_S_WRITE (a, "orange", 6)
  2392. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2393. OP_END
  2394. };
  2395. /* 26. Fault injection - excess value of STREAMS_BLOCKED_BIDI */
  2396. static const struct script_op script_26[] = {
  2397. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2398. OP_C_SET_ALPN ("ossltest")
  2399. OP_C_CONNECT_WAIT ()
  2400. OP_C_WRITE (DEFAULT, "apple", 5)
  2401. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2402. OP_S_READ_EXPECT (a, "apple", 5)
  2403. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  2404. OP_S_WRITE (a, "orange", 6)
  2405. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  2406. OP_END
  2407. };
  2408. /* 27. Fault injection - excess value of STREAMS_BLOCKED_UNI */
  2409. static const struct script_op script_27[] = {
  2410. OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
  2411. OP_C_SET_ALPN ("ossltest")
  2412. OP_C_CONNECT_WAIT ()
  2413. OP_C_WRITE (DEFAULT, "apple", 5)
  2414. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2415. OP_S_READ_EXPECT (a, "apple", 5)
  2416. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
  2417. OP_S_WRITE (a, "orange", 6)
  2418. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  2419. OP_END
  2420. };
  2421. /* 28. Fault injection - received RESET_STREAM for send-only stream */
  2422. static int script_28_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2423. unsigned char *buf, size_t len)
  2424. {
  2425. int ok = 0;
  2426. WPACKET wpkt;
  2427. unsigned char frame_buf[32];
  2428. size_t written;
  2429. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2430. return 1;
  2431. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2432. sizeof(frame_buf), 0)))
  2433. return 0;
  2434. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2435. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  2436. h->inject_word0 - 1))
  2437. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
  2438. || (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
  2439. && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 5)))) /* final size */
  2440. goto err;
  2441. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2442. goto err;
  2443. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2444. goto err;
  2445. ok = 1;
  2446. err:
  2447. if (ok)
  2448. WPACKET_finish(&wpkt);
  2449. else
  2450. WPACKET_cleanup(&wpkt);
  2451. return ok;
  2452. }
  2453. static const struct script_op script_28[] = {
  2454. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2455. OP_C_SET_ALPN ("ossltest")
  2456. OP_C_CONNECT_WAIT ()
  2457. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2458. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2459. OP_C_WRITE (a, "orange", 6)
  2460. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2461. OP_S_READ_EXPECT (a, "orange", 6)
  2462. OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
  2463. OP_C_WRITE (b, "apple", 5)
  2464. OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
  2465. OP_S_READ_EXPECT (b, "apple", 5)
  2466. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  2467. OP_S_WRITE (a, "fruit", 5)
  2468. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2469. OP_END
  2470. };
  2471. /* 29. Fault injection - received RESET_STREAM for nonexistent send-only stream */
  2472. static const struct script_op script_29[] = {
  2473. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2474. OP_C_SET_ALPN ("ossltest")
  2475. OP_C_CONNECT_WAIT ()
  2476. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2477. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2478. OP_C_WRITE (a, "orange", 6)
  2479. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2480. OP_S_READ_EXPECT (a, "orange", 6)
  2481. OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
  2482. OP_C_WRITE (b, "apple", 5)
  2483. OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
  2484. OP_S_READ_EXPECT (b, "apple", 5)
  2485. OP_SET_INJECT_WORD (C_UNI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  2486. OP_S_WRITE (a, "fruit", 5)
  2487. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2488. OP_END
  2489. };
  2490. /* 30. Fault injection - received STOP_SENDING for receive-only stream */
  2491. static const struct script_op script_30[] = {
  2492. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2493. OP_C_SET_ALPN ("ossltest")
  2494. OP_C_CONNECT_WAIT ()
  2495. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2496. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2497. OP_S_WRITE (a, "apple", 5)
  2498. OP_C_ACCEPT_STREAM_WAIT (a)
  2499. OP_C_READ_EXPECT (a, "apple", 5)
  2500. OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  2501. OP_S_WRITE (a, "orange", 6)
  2502. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2503. OP_END
  2504. };
  2505. /* 31. Fault injection - received STOP_SENDING for nonexistent receive-only stream */
  2506. static const struct script_op script_31[] = {
  2507. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2508. OP_C_SET_ALPN ("ossltest")
  2509. OP_C_CONNECT_WAIT ()
  2510. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2511. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2512. OP_S_WRITE (a, "apple", 5)
  2513. OP_C_ACCEPT_STREAM_WAIT (a)
  2514. OP_C_READ_EXPECT (a, "apple", 5)
  2515. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  2516. OP_S_WRITE (a, "orange", 6)
  2517. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2518. OP_END
  2519. };
  2520. /* 32. Fault injection - STREAM frame for nonexistent stream */
  2521. static int script_32_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2522. unsigned char *buf, size_t len)
  2523. {
  2524. int ok = 0;
  2525. WPACKET wpkt;
  2526. unsigned char frame_buf[64];
  2527. size_t written;
  2528. uint64_t type = OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN, offset, flen, i;
  2529. if (hdr->type != QUIC_PKT_TYPE_1RTT)
  2530. return 1;
  2531. switch (h->inject_word1) {
  2532. default:
  2533. return 0;
  2534. case 0:
  2535. return 1;
  2536. case 1:
  2537. offset = 0;
  2538. flen = 0;
  2539. break;
  2540. case 2:
  2541. offset = (((uint64_t)1)<<62) - 1;
  2542. flen = 5;
  2543. break;
  2544. case 3:
  2545. offset = 1 * 1024 * 1024 * 1024; /* 1G */
  2546. flen = 5;
  2547. break;
  2548. case 4:
  2549. offset = 0;
  2550. flen = 1;
  2551. break;
  2552. }
  2553. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2554. sizeof(frame_buf), 0)))
  2555. return 0;
  2556. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
  2557. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  2558. h->inject_word0 - 1))
  2559. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
  2560. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, flen)))
  2561. goto err;
  2562. for (i = 0; i < flen; ++i)
  2563. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  2564. goto err;
  2565. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2566. goto err;
  2567. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2568. goto err;
  2569. ok = 1;
  2570. err:
  2571. if (ok)
  2572. WPACKET_finish(&wpkt);
  2573. else
  2574. WPACKET_cleanup(&wpkt);
  2575. return ok;
  2576. }
  2577. static const struct script_op script_32[] = {
  2578. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  2579. OP_C_SET_ALPN ("ossltest")
  2580. OP_C_CONNECT_WAIT ()
  2581. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2582. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2583. OP_S_WRITE (a, "apple", 5)
  2584. OP_C_ACCEPT_STREAM_WAIT (a)
  2585. OP_C_READ_EXPECT (a, "apple", 5)
  2586. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, 1)
  2587. OP_S_WRITE (a, "orange", 6)
  2588. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2589. OP_END
  2590. };
  2591. /* 33. Fault injection - STREAM frame with illegal offset */
  2592. static const struct script_op script_33[] = {
  2593. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  2594. OP_C_SET_ALPN ("ossltest")
  2595. OP_C_CONNECT_WAIT ()
  2596. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2597. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2598. OP_C_WRITE (a, "apple", 5)
  2599. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2600. OP_S_READ_EXPECT (a, "apple", 5)
  2601. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 2)
  2602. OP_S_WRITE (a, "orange", 6)
  2603. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2604. OP_END
  2605. };
  2606. /* 34. Fault injection - STREAM frame which exceeds FC */
  2607. static const struct script_op script_34[] = {
  2608. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  2609. OP_C_SET_ALPN ("ossltest")
  2610. OP_C_CONNECT_WAIT ()
  2611. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2612. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2613. OP_C_WRITE (a, "apple", 5)
  2614. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2615. OP_S_READ_EXPECT (a, "apple", 5)
  2616. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 3)
  2617. OP_S_WRITE (a, "orange", 6)
  2618. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR,0,0)
  2619. OP_END
  2620. };
  2621. /* 35. Fault injection - MAX_STREAM_DATA for receive-only stream */
  2622. static const struct script_op script_35[] = {
  2623. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2624. OP_C_SET_ALPN ("ossltest")
  2625. OP_C_CONNECT_WAIT ()
  2626. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2627. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2628. OP_S_WRITE (a, "apple", 5)
  2629. OP_C_ACCEPT_STREAM_WAIT (a)
  2630. OP_C_READ_EXPECT (a, "apple", 5)
  2631. OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  2632. OP_S_WRITE (a, "orange", 6)
  2633. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2634. OP_END
  2635. };
  2636. /* 36. Fault injection - MAX_STREAM_DATA for nonexistent stream */
  2637. static const struct script_op script_36[] = {
  2638. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2639. OP_C_SET_ALPN ("ossltest")
  2640. OP_C_CONNECT_WAIT ()
  2641. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2642. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  2643. OP_S_WRITE (a, "apple", 5)
  2644. OP_C_ACCEPT_STREAM_WAIT (a)
  2645. OP_C_READ_EXPECT (a, "apple", 5)
  2646. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  2647. OP_S_WRITE (a, "orange", 6)
  2648. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2649. OP_END
  2650. };
  2651. /* 37. Fault injection - STREAM_DATA_BLOCKED for send-only stream */
  2652. static const struct script_op script_37[] = {
  2653. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2654. OP_C_SET_ALPN ("ossltest")
  2655. OP_C_CONNECT_WAIT ()
  2656. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2657. OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0))
  2658. OP_C_WRITE (a, "apple", 5)
  2659. OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
  2660. OP_S_READ_EXPECT (a, "apple", 5)
  2661. OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
  2662. OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  2663. OP_S_WRITE (b, "orange", 5)
  2664. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2665. OP_END
  2666. };
  2667. /* 38. Fault injection - STREAM_DATA_BLOCKED for non-existent stream */
  2668. static const struct script_op script_38[] = {
  2669. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  2670. OP_C_SET_ALPN ("ossltest")
  2671. OP_C_CONNECT_WAIT ()
  2672. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2673. OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0))
  2674. OP_C_WRITE (a, "apple", 5)
  2675. OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
  2676. OP_S_READ_EXPECT (a, "apple", 5)
  2677. OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  2678. OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
  2679. OP_S_WRITE (b, "orange", 5)
  2680. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  2681. OP_END
  2682. };
  2683. /* 39. Fault injection - NEW_CONN_ID with zero-len CID */
  2684. static int script_39_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2685. unsigned char *buf, size_t len)
  2686. {
  2687. int ok = 0;
  2688. WPACKET wpkt;
  2689. unsigned char frame_buf[64];
  2690. size_t i, written;
  2691. uint64_t seq_no = 0, retire_prior_to = 0;
  2692. QUIC_CONN_ID new_cid = {0};
  2693. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(h->s_priv);
  2694. if (hdr->type != QUIC_PKT_TYPE_1RTT)
  2695. return 1;
  2696. switch (h->inject_word1) {
  2697. case 0:
  2698. return 1;
  2699. case 1:
  2700. new_cid.id_len = 0;
  2701. break;
  2702. case 2:
  2703. new_cid.id_len = 21;
  2704. break;
  2705. case 3:
  2706. new_cid.id_len = 1;
  2707. new_cid.id[0] = 0x55;
  2708. seq_no = 0;
  2709. retire_prior_to = 1;
  2710. break;
  2711. case 4:
  2712. /* Use our actual CID so we don't break connectivity. */
  2713. ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
  2714. seq_no = 2;
  2715. retire_prior_to = 2;
  2716. break;
  2717. case 5:
  2718. /*
  2719. * Use a bogus CID which will need to be ignored if connectivity is to
  2720. * be continued.
  2721. */
  2722. new_cid.id_len = 8;
  2723. new_cid.id[0] = 0x55;
  2724. seq_no = 1;
  2725. retire_prior_to = 1;
  2726. break;
  2727. }
  2728. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2729. sizeof(frame_buf), 0)))
  2730. return 0;
  2731. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID))
  2732. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, seq_no)) /* seq no */
  2733. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, retire_prior_to)) /* retire prior to */
  2734. || !TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id_len))) /* len */
  2735. goto err;
  2736. for (i = 0; i < new_cid.id_len && i < OSSL_NELEM(new_cid.id); ++i)
  2737. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id[i])))
  2738. goto err;
  2739. for (; i < new_cid.id_len; ++i)
  2740. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x55)))
  2741. goto err;
  2742. for (i = 0; i < QUIC_STATELESS_RESET_TOKEN_LEN; ++i)
  2743. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  2744. goto err;
  2745. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2746. goto err;
  2747. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2748. goto err;
  2749. ok = 1;
  2750. err:
  2751. if (ok)
  2752. WPACKET_finish(&wpkt);
  2753. else
  2754. WPACKET_cleanup(&wpkt);
  2755. return ok;
  2756. }
  2757. static const struct script_op script_39[] = {
  2758. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  2759. OP_C_SET_ALPN ("ossltest")
  2760. OP_C_CONNECT_WAIT ()
  2761. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2762. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2763. OP_C_WRITE (a, "apple", 5)
  2764. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2765. OP_S_READ_EXPECT (a, "apple", 5)
  2766. OP_SET_INJECT_WORD (0, 1)
  2767. OP_S_WRITE (a, "orange", 5)
  2768. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2769. OP_END
  2770. };
  2771. /* 40. Shutdown flush test */
  2772. static const unsigned char script_40_data[1024] = "strawberry";
  2773. static const struct script_op script_40[] = {
  2774. OP_C_SET_ALPN ("ossltest")
  2775. OP_C_CONNECT_WAIT ()
  2776. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2777. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2778. OP_C_WRITE (a, "apple", 5)
  2779. OP_C_INHIBIT_TICK (1)
  2780. OP_C_SET_WRITE_BUF_SIZE (a, 1024 * 100 * 3)
  2781. OP_BEGIN_REPEAT (100)
  2782. OP_C_WRITE (a, script_40_data, sizeof(script_40_data))
  2783. OP_END_REPEAT ()
  2784. OP_C_CONCLUDE (a)
  2785. OP_C_SHUTDOWN_WAIT (NULL, 0) /* disengages tick inhibition */
  2786. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2787. OP_S_READ_EXPECT (a, "apple", 5)
  2788. OP_BEGIN_REPEAT (100)
  2789. OP_S_READ_EXPECT (a, script_40_data, sizeof(script_40_data))
  2790. OP_END_REPEAT ()
  2791. OP_S_EXPECT_FIN (a)
  2792. OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
  2793. OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
  2794. OP_END
  2795. };
  2796. /* 41. Fault injection - PATH_CHALLENGE yields PATH_RESPONSE */
  2797. static const uint64_t path_challenge = UINT64_C(0xbdeb9451169c83aa);
  2798. static int script_41_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2799. unsigned char *buf, size_t len)
  2800. {
  2801. int ok = 0;
  2802. WPACKET wpkt;
  2803. unsigned char frame_buf[16];
  2804. size_t written;
  2805. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2806. return 1;
  2807. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2808. sizeof(frame_buf), 0)))
  2809. return 0;
  2810. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2811. || !TEST_true(WPACKET_put_bytes_u64(&wpkt, path_challenge)))
  2812. goto err;
  2813. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))
  2814. || !TEST_size_t_eq(written, 9))
  2815. goto err;
  2816. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2817. goto err;
  2818. --h->inject_word0;
  2819. ok = 1;
  2820. err:
  2821. if (ok)
  2822. WPACKET_finish(&wpkt);
  2823. else
  2824. WPACKET_cleanup(&wpkt);
  2825. return ok;
  2826. }
  2827. static void script_41_trace(int write_p, int version, int content_type,
  2828. const void *buf, size_t len, SSL *ssl, void *arg)
  2829. {
  2830. uint64_t frame_type, frame_data;
  2831. int was_minimal;
  2832. struct helper *h = arg;
  2833. PACKET pkt;
  2834. if (version != OSSL_QUIC1_VERSION
  2835. || content_type != SSL3_RT_QUIC_FRAME_FULL
  2836. || len < 1)
  2837. return;
  2838. if (!TEST_true(PACKET_buf_init(&pkt, buf, len))) {
  2839. ++h->scratch1;
  2840. return;
  2841. }
  2842. if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
  2843. &was_minimal))) {
  2844. ++h->scratch1;
  2845. return;
  2846. }
  2847. if (frame_type != OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  2848. return;
  2849. if (!TEST_true(ossl_quic_wire_decode_frame_path_response(&pkt, &frame_data))
  2850. || !TEST_uint64_t_eq(frame_data, path_challenge)) {
  2851. ++h->scratch1;
  2852. return;
  2853. }
  2854. ++h->scratch0;
  2855. }
  2856. static int script_41_setup(struct helper *h, struct helper_local *hl)
  2857. {
  2858. ossl_quic_tserver_set_msg_callback(ACQUIRE_S(), script_41_trace, h);
  2859. return 1;
  2860. }
  2861. static int script_41_check(struct helper *h, struct helper_local *hl)
  2862. {
  2863. /* At least one valid challenge/response echo? */
  2864. if (!TEST_uint64_t_gt(h->scratch0, 0))
  2865. return 0;
  2866. /* No failed tests? */
  2867. if (!TEST_uint64_t_eq(h->scratch1, 0))
  2868. return 0;
  2869. return 1;
  2870. }
  2871. static const struct script_op script_41[] = {
  2872. OP_S_SET_INJECT_PLAIN (script_41_inject_plain)
  2873. OP_C_SET_ALPN ("ossltest")
  2874. OP_C_CONNECT_WAIT ()
  2875. OP_CHECK (script_41_setup, 0)
  2876. OP_C_WRITE (DEFAULT, "apple", 5)
  2877. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2878. OP_S_READ_EXPECT (a, "apple", 5)
  2879. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
  2880. OP_S_WRITE (a, "orange", 6)
  2881. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  2882. OP_C_WRITE (DEFAULT, "strawberry", 10)
  2883. OP_S_READ_EXPECT (a, "strawberry", 10)
  2884. OP_CHECK (script_41_check, 0)
  2885. OP_END
  2886. };
  2887. /* 42. Fault injection - CRYPTO frame with illegal offset */
  2888. static int script_42_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2889. unsigned char *buf, size_t len)
  2890. {
  2891. int ok = 0;
  2892. unsigned char frame_buf[64];
  2893. size_t written;
  2894. WPACKET wpkt;
  2895. if (h->inject_word0 == 0)
  2896. return 1;
  2897. --h->inject_word0;
  2898. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2899. sizeof(frame_buf), 0)))
  2900. return 0;
  2901. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
  2902. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
  2903. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 1))
  2904. || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  2905. goto err;
  2906. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2907. goto err;
  2908. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2909. goto err;
  2910. ok = 1;
  2911. err:
  2912. if (ok)
  2913. WPACKET_finish(&wpkt);
  2914. else
  2915. WPACKET_cleanup(&wpkt);
  2916. return ok;
  2917. }
  2918. static const struct script_op script_42[] = {
  2919. OP_S_SET_INJECT_PLAIN (script_42_inject_plain)
  2920. OP_C_SET_ALPN ("ossltest")
  2921. OP_C_CONNECT_WAIT ()
  2922. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2923. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2924. OP_C_WRITE (a, "apple", 5)
  2925. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2926. OP_S_READ_EXPECT (a, "apple", 5)
  2927. OP_SET_INJECT_WORD (1, (((uint64_t)1) << 62) - 1)
  2928. OP_S_WRITE (a, "orange", 6)
  2929. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  2930. OP_END
  2931. };
  2932. /* 43. Fault injection - CRYPTO frame exceeding FC */
  2933. static const struct script_op script_43[] = {
  2934. OP_S_SET_INJECT_PLAIN (script_42_inject_plain)
  2935. OP_C_SET_ALPN ("ossltest")
  2936. OP_C_CONNECT_WAIT ()
  2937. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  2938. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  2939. OP_C_WRITE (a, "apple", 5)
  2940. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2941. OP_S_READ_EXPECT (a, "apple", 5)
  2942. OP_SET_INJECT_WORD (1, 0x100000 /* 1 MiB */)
  2943. OP_S_WRITE (a, "orange", 6)
  2944. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0)
  2945. OP_END
  2946. };
  2947. /* 44. Fault injection - PADDING */
  2948. static int script_44_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  2949. unsigned char *buf, size_t len)
  2950. {
  2951. int ok = 0;
  2952. WPACKET wpkt;
  2953. unsigned char frame_buf[16];
  2954. size_t written;
  2955. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  2956. return 1;
  2957. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  2958. sizeof(frame_buf), 0)))
  2959. return 0;
  2960. if (!TEST_true(ossl_quic_wire_encode_padding(&wpkt, 1)))
  2961. goto err;
  2962. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  2963. goto err;
  2964. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  2965. goto err;
  2966. ok = 1;
  2967. err:
  2968. if (ok)
  2969. WPACKET_finish(&wpkt);
  2970. else
  2971. WPACKET_cleanup(&wpkt);
  2972. return ok;
  2973. }
  2974. static const struct script_op script_44[] = {
  2975. OP_S_SET_INJECT_PLAIN (script_44_inject_plain)
  2976. OP_C_SET_ALPN ("ossltest")
  2977. OP_C_CONNECT_WAIT ()
  2978. OP_C_WRITE (DEFAULT, "apple", 5)
  2979. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  2980. OP_S_READ_EXPECT (a, "apple", 5)
  2981. OP_SET_INJECT_WORD (1, 0)
  2982. OP_S_WRITE (a, "Strawberry", 10)
  2983. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  2984. OP_END
  2985. };
  2986. /* 45. PING must generate ACK */
  2987. static int force_ping(struct helper *h, struct helper_local *hl)
  2988. {
  2989. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
  2990. h->scratch0 = ossl_quic_channel_get_diag_num_rx_ack(ch);
  2991. if (!TEST_true(ossl_quic_tserver_ping(ACQUIRE_S())))
  2992. return 0;
  2993. return 1;
  2994. }
  2995. static int wait_incoming_acks_increased(struct helper *h, struct helper_local *hl)
  2996. {
  2997. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
  2998. uint16_t count;
  2999. count = ossl_quic_channel_get_diag_num_rx_ack(ch);
  3000. if (count == h->scratch0) {
  3001. h->check_spin_again = 1;
  3002. return 0;
  3003. }
  3004. return 1;
  3005. }
  3006. static const struct script_op script_45[] = {
  3007. OP_C_SET_ALPN ("ossltest")
  3008. OP_C_CONNECT_WAIT ()
  3009. OP_C_WRITE (DEFAULT, "apple", 5)
  3010. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3011. OP_S_READ_EXPECT (a, "apple", 5)
  3012. OP_BEGIN_REPEAT (2)
  3013. OP_CHECK (force_ping, 0)
  3014. OP_CHECK (wait_incoming_acks_increased, 0)
  3015. OP_END_REPEAT ()
  3016. OP_S_WRITE (a, "Strawberry", 10)
  3017. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3018. OP_END
  3019. };
  3020. /* 46. Fault injection - ACK - malformed initial range */
  3021. static int script_46_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3022. unsigned char *buf, size_t len)
  3023. {
  3024. int ok = 0;
  3025. WPACKET wpkt;
  3026. unsigned char frame_buf[16];
  3027. size_t written;
  3028. uint64_t type = 0, largest_acked = 0, first_range = 0, range_count = 0;
  3029. uint64_t agap = 0, alen = 0;
  3030. uint64_t ect0 = 0, ect1 = 0, ecnce = 0;
  3031. if (h->inject_word0 == 0)
  3032. return 1;
  3033. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3034. sizeof(frame_buf), 0)))
  3035. return 0;
  3036. type = OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
  3037. switch (h->inject_word0) {
  3038. case 1:
  3039. largest_acked = 100;
  3040. first_range = 101;
  3041. range_count = 0;
  3042. break;
  3043. case 2:
  3044. largest_acked = 100;
  3045. first_range = 80;
  3046. /* [20..100]; [0..18] */
  3047. range_count = 1;
  3048. agap = 0;
  3049. alen = 19;
  3050. break;
  3051. case 3:
  3052. largest_acked = 100;
  3053. first_range = 80;
  3054. range_count = 1;
  3055. agap = 18;
  3056. alen = 1;
  3057. break;
  3058. case 4:
  3059. type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
  3060. largest_acked = 100;
  3061. first_range = 1;
  3062. range_count = 0;
  3063. break;
  3064. case 5:
  3065. type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
  3066. largest_acked = 0;
  3067. first_range = 0;
  3068. range_count = 0;
  3069. ect0 = 0;
  3070. ect1 = 50;
  3071. ecnce = 200;
  3072. break;
  3073. }
  3074. h->inject_word0 = 0;
  3075. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
  3076. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, largest_acked))
  3077. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_delay=*/0))
  3078. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_range_count=*/range_count))
  3079. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*first_ack_range=*/first_range)))
  3080. goto err;
  3081. if (range_count > 0)
  3082. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].gap=*/agap))
  3083. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].len=*/alen)))
  3084. goto err;
  3085. if (type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN)
  3086. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, ect0))
  3087. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ect1))
  3088. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ecnce)))
  3089. goto err;
  3090. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3091. goto err;
  3092. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3093. goto err;
  3094. ok = 1;
  3095. err:
  3096. if (ok)
  3097. WPACKET_finish(&wpkt);
  3098. else
  3099. WPACKET_cleanup(&wpkt);
  3100. return ok;
  3101. }
  3102. static const struct script_op script_46[] = {
  3103. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3104. OP_C_SET_ALPN ("ossltest")
  3105. OP_C_CONNECT_WAIT ()
  3106. OP_C_WRITE (DEFAULT, "apple", 5)
  3107. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3108. OP_S_READ_EXPECT (a, "apple", 5)
  3109. OP_SET_INJECT_WORD (1, 0)
  3110. OP_S_WRITE (a, "Strawberry", 10)
  3111. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3112. OP_END
  3113. };
  3114. /* 47. Fault injection - ACK - malformed subsequent range */
  3115. static const struct script_op script_47[] = {
  3116. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3117. OP_C_SET_ALPN ("ossltest")
  3118. OP_C_CONNECT_WAIT ()
  3119. OP_C_WRITE (DEFAULT, "apple", 5)
  3120. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3121. OP_S_READ_EXPECT (a, "apple", 5)
  3122. OP_SET_INJECT_WORD (2, 0)
  3123. OP_S_WRITE (a, "Strawberry", 10)
  3124. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3125. OP_END
  3126. };
  3127. /* 48. Fault injection - ACK - malformed subsequent range */
  3128. static const struct script_op script_48[] = {
  3129. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3130. OP_C_SET_ALPN ("ossltest")
  3131. OP_C_CONNECT_WAIT ()
  3132. OP_C_WRITE (DEFAULT, "apple", 5)
  3133. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3134. OP_S_READ_EXPECT (a, "apple", 5)
  3135. OP_SET_INJECT_WORD (3, 0)
  3136. OP_S_WRITE (a, "Strawberry", 10)
  3137. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3138. OP_END
  3139. };
  3140. /* 49. Fault injection - ACK - fictional PN */
  3141. static const struct script_op script_49[] = {
  3142. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3143. OP_C_SET_ALPN ("ossltest")
  3144. OP_C_CONNECT_WAIT ()
  3145. OP_C_WRITE (DEFAULT, "apple", 5)
  3146. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3147. OP_S_READ_EXPECT (a, "apple", 5)
  3148. OP_SET_INJECT_WORD (4, 0)
  3149. OP_S_WRITE (a, "Strawberry", 10)
  3150. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3151. OP_END
  3152. };
  3153. /* 50. Fault injection - ACK - duplicate PN */
  3154. static const struct script_op script_50[] = {
  3155. OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
  3156. OP_C_SET_ALPN ("ossltest")
  3157. OP_C_CONNECT_WAIT ()
  3158. OP_C_WRITE (DEFAULT, "apple", 5)
  3159. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3160. OP_S_READ_EXPECT (a, "apple", 5)
  3161. OP_BEGIN_REPEAT (2)
  3162. OP_SET_INJECT_WORD (5, 0)
  3163. OP_S_WRITE (a, "Strawberry", 10)
  3164. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3165. OP_END_REPEAT ()
  3166. OP_END
  3167. };
  3168. /* 51. Fault injection - PATH_RESPONSE is ignored */
  3169. static const struct script_op script_51[] = {
  3170. OP_S_SET_INJECT_PLAIN (script_41_inject_plain)
  3171. OP_C_SET_ALPN ("ossltest")
  3172. OP_C_CONNECT_WAIT ()
  3173. OP_C_WRITE (DEFAULT, "apple", 5)
  3174. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3175. OP_S_READ_EXPECT (a, "apple", 5)
  3176. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  3177. OP_S_WRITE (a, "orange", 6)
  3178. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3179. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3180. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3181. OP_END
  3182. };
  3183. /* 52. Fault injection - ignore BLOCKED frames with bogus values */
  3184. static int script_52_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3185. unsigned char *buf, size_t len)
  3186. {
  3187. int ok = 0;
  3188. unsigned char frame_buf[64];
  3189. size_t written;
  3190. WPACKET wpkt;
  3191. uint64_t type = h->inject_word1;
  3192. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3193. return 1;
  3194. --h->inject_word0;
  3195. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3196. sizeof(frame_buf), 0)))
  3197. return 0;
  3198. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type)))
  3199. goto err;
  3200. if (type == OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  3201. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, C_BIDI_ID(0))))
  3202. goto err;
  3203. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, 0xFFFFFF)))
  3204. goto err;
  3205. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3206. goto err;
  3207. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3208. goto err;
  3209. ok = 1;
  3210. err:
  3211. if (ok)
  3212. WPACKET_finish(&wpkt);
  3213. else
  3214. WPACKET_cleanup(&wpkt);
  3215. return ok;
  3216. }
  3217. static const struct script_op script_52[] = {
  3218. OP_S_SET_INJECT_PLAIN (script_52_inject_plain)
  3219. OP_C_SET_ALPN ("ossltest")
  3220. OP_C_CONNECT_WAIT ()
  3221. OP_C_WRITE (DEFAULT, "apple", 5)
  3222. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3223. OP_S_READ_EXPECT (a, "apple", 5)
  3224. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
  3225. OP_S_WRITE (a, "orange", 6)
  3226. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3227. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3228. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3229. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  3230. OP_S_WRITE (a, "orange", 6)
  3231. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3232. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3233. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3234. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
  3235. OP_S_WRITE (a, "orange", 6)
  3236. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3237. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3238. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3239. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  3240. OP_S_WRITE (a, "orange", 6)
  3241. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3242. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3243. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3244. OP_END
  3245. };
  3246. /* 53. Fault injection - excess CRYPTO buffer size */
  3247. static int script_53_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3248. unsigned char *buf, size_t len)
  3249. {
  3250. int ok = 0;
  3251. size_t written;
  3252. WPACKET wpkt;
  3253. uint64_t offset = 0, data_len = 100;
  3254. unsigned char *frame_buf = NULL;
  3255. size_t frame_len, i;
  3256. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3257. return 1;
  3258. h->inject_word0 = 0;
  3259. switch (h->inject_word1) {
  3260. case 0:
  3261. /*
  3262. * Far out offset which will not have been reached during handshake.
  3263. * This will not be delivered to the QUIC_TLS instance since it will be
  3264. * waiting for in-order delivery of previous bytes. This tests our flow
  3265. * control on CRYPTO stream buffering.
  3266. */
  3267. offset = 100000;
  3268. data_len = 1;
  3269. break;
  3270. }
  3271. frame_len = 1 + 8 + 8 + (size_t)data_len;
  3272. if (!TEST_ptr(frame_buf = OPENSSL_malloc(frame_len)))
  3273. return 0;
  3274. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, frame_len, 0)))
  3275. goto err;
  3276. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
  3277. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
  3278. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, data_len)))
  3279. goto err;
  3280. for (i = 0; i < data_len; ++i)
  3281. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
  3282. goto err;
  3283. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3284. goto err;
  3285. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3286. goto err;
  3287. ok = 1;
  3288. err:
  3289. if (ok)
  3290. WPACKET_finish(&wpkt);
  3291. else
  3292. WPACKET_cleanup(&wpkt);
  3293. OPENSSL_free(frame_buf);
  3294. return ok;
  3295. }
  3296. static const struct script_op script_53[] = {
  3297. OP_S_SET_INJECT_PLAIN (script_53_inject_plain)
  3298. OP_C_SET_ALPN ("ossltest")
  3299. OP_C_CONNECT_WAIT ()
  3300. OP_C_WRITE (DEFAULT, "apple", 5)
  3301. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3302. OP_S_READ_EXPECT (a, "apple", 5)
  3303. OP_SET_INJECT_WORD (1, 0)
  3304. OP_S_WRITE (a, "Strawberry", 10)
  3305. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0)
  3306. OP_END
  3307. };
  3308. /* 54. Fault injection - corrupted crypto stream data */
  3309. static int script_54_inject_handshake(struct helper *h,
  3310. unsigned char *buf, size_t buf_len)
  3311. {
  3312. size_t i;
  3313. for (i = 0; i < buf_len; ++i)
  3314. buf[i] ^= 0xff;
  3315. return 1;
  3316. }
  3317. static const struct script_op script_54[] = {
  3318. OP_S_SET_INJECT_HANDSHAKE(script_54_inject_handshake)
  3319. OP_C_SET_ALPN ("ossltest")
  3320. OP_C_CONNECT_WAIT_OR_FAIL()
  3321. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_UNEXPECTED_MESSAGE,0,0)
  3322. OP_END
  3323. };
  3324. /* 55. Fault injection - NEW_CONN_ID with >20 byte CID */
  3325. static const struct script_op script_55[] = {
  3326. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  3327. OP_C_SET_ALPN ("ossltest")
  3328. OP_C_CONNECT_WAIT ()
  3329. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3330. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3331. OP_C_WRITE (a, "apple", 5)
  3332. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3333. OP_S_READ_EXPECT (a, "apple", 5)
  3334. OP_SET_INJECT_WORD (0, 2)
  3335. OP_S_WRITE (a, "orange", 5)
  3336. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3337. OP_END
  3338. };
  3339. /* 56. Fault injection - NEW_CONN_ID with seq no < retire prior to */
  3340. static const struct script_op script_56[] = {
  3341. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  3342. OP_C_SET_ALPN ("ossltest")
  3343. OP_C_CONNECT_WAIT ()
  3344. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3345. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3346. OP_C_WRITE (a, "apple", 5)
  3347. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3348. OP_S_READ_EXPECT (a, "apple", 5)
  3349. OP_SET_INJECT_WORD (0, 3)
  3350. OP_S_WRITE (a, "orange", 5)
  3351. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  3352. OP_END
  3353. };
  3354. /* 57. Fault injection - NEW_CONN_ID with lower seq so ignored */
  3355. static const struct script_op script_57[] = {
  3356. OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
  3357. OP_C_SET_ALPN ("ossltest")
  3358. OP_C_CONNECT_WAIT ()
  3359. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3360. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3361. OP_C_WRITE (a, "apple", 5)
  3362. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3363. OP_S_READ_EXPECT (a, "apple", 5)
  3364. OP_SET_INJECT_WORD (0, 4)
  3365. OP_S_WRITE (a, "orange", 5)
  3366. OP_C_READ_EXPECT (a, "orange", 5)
  3367. OP_C_WRITE (a, "Strawberry", 10)
  3368. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3369. /*
  3370. * Now we send a NEW_CONN_ID with a bogus CID. However the sequence number
  3371. * is old so it should be ignored and we should still be able to
  3372. * communicate.
  3373. */
  3374. OP_SET_INJECT_WORD (0, 5)
  3375. OP_S_WRITE (a, "raspberry", 9)
  3376. OP_C_READ_EXPECT (a, "raspberry", 9)
  3377. OP_C_WRITE (a, "peach", 5)
  3378. OP_S_READ_EXPECT (a, "peach", 5)
  3379. OP_END
  3380. };
  3381. /* 58. Fault injection - repeated HANDSHAKE_DONE */
  3382. static int script_58_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3383. unsigned char *buf, size_t len)
  3384. {
  3385. int ok = 0;
  3386. unsigned char frame_buf[64];
  3387. size_t written;
  3388. WPACKET wpkt;
  3389. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3390. return 1;
  3391. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3392. sizeof(frame_buf), 0)))
  3393. return 0;
  3394. if (h->inject_word0 == 1) {
  3395. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)))
  3396. goto err;
  3397. } else {
  3398. /* Needless multi-byte encoding */
  3399. if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x40))
  3400. || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x1E)))
  3401. goto err;
  3402. }
  3403. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3404. goto err;
  3405. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3406. goto err;
  3407. ok = 1;
  3408. err:
  3409. if (ok)
  3410. WPACKET_finish(&wpkt);
  3411. else
  3412. WPACKET_cleanup(&wpkt);
  3413. return ok;
  3414. }
  3415. static const struct script_op script_58[] = {
  3416. OP_S_SET_INJECT_PLAIN (script_58_inject_plain)
  3417. OP_C_SET_ALPN ("ossltest")
  3418. OP_C_CONNECT_WAIT ()
  3419. OP_C_WRITE (DEFAULT, "apple", 5)
  3420. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3421. OP_S_READ_EXPECT (a, "apple", 5)
  3422. OP_SET_INJECT_WORD (1, 0)
  3423. OP_S_WRITE (a, "orange", 6)
  3424. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  3425. OP_C_WRITE (DEFAULT, "Strawberry", 10)
  3426. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3427. OP_END
  3428. };
  3429. /* 59. Fault injection - multi-byte frame encoding */
  3430. static const struct script_op script_59[] = {
  3431. OP_S_SET_INJECT_PLAIN (script_58_inject_plain)
  3432. OP_C_SET_ALPN ("ossltest")
  3433. OP_C_CONNECT_WAIT ()
  3434. OP_C_WRITE (DEFAULT, "apple", 5)
  3435. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3436. OP_S_READ_EXPECT (a, "apple", 5)
  3437. OP_SET_INJECT_WORD (2, 0)
  3438. OP_S_WRITE (a, "orange", 6)
  3439. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION,0,0)
  3440. OP_END
  3441. };
  3442. /* 60. Connection close reason truncation */
  3443. static char long_reason[2048];
  3444. static int init_reason(struct helper *h, struct helper_local *hl)
  3445. {
  3446. memset(long_reason, '~', sizeof(long_reason));
  3447. memcpy(long_reason, "This is a long reason string.", 29);
  3448. long_reason[OSSL_NELEM(long_reason) - 1] = '\0';
  3449. return 1;
  3450. }
  3451. static int check_shutdown_reason(struct helper *h, struct helper_local *hl)
  3452. {
  3453. const QUIC_TERMINATE_CAUSE *tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S());
  3454. if (tc == NULL) {
  3455. h->check_spin_again = 1;
  3456. return 0;
  3457. }
  3458. if (!TEST_size_t_ge(tc->reason_len, 50)
  3459. || !TEST_mem_eq(long_reason, tc->reason_len,
  3460. tc->reason, tc->reason_len))
  3461. return 0;
  3462. return 1;
  3463. }
  3464. static const struct script_op script_60[] = {
  3465. OP_C_SET_ALPN ("ossltest")
  3466. OP_C_CONNECT_WAIT ()
  3467. OP_C_WRITE (DEFAULT, "apple", 5)
  3468. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3469. OP_S_READ_EXPECT (a, "apple", 5)
  3470. OP_CHECK (init_reason, 0)
  3471. OP_C_SHUTDOWN_WAIT (long_reason, 0)
  3472. OP_CHECK (check_shutdown_reason, 0)
  3473. OP_END
  3474. };
  3475. /* 61. Fault injection - RESET_STREAM exceeding stream count FC */
  3476. static int script_61_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3477. unsigned char *buf, size_t len)
  3478. {
  3479. int ok = 0;
  3480. WPACKET wpkt;
  3481. unsigned char frame_buf[32];
  3482. size_t written;
  3483. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3484. return 1;
  3485. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3486. sizeof(frame_buf), 0)))
  3487. return 0;
  3488. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0))
  3489. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  3490. h->inject_word1))
  3491. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
  3492. || (h->inject_word0 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
  3493. && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))) /* final size */
  3494. goto err;
  3495. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3496. goto err;
  3497. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3498. goto err;
  3499. ok = 1;
  3500. err:
  3501. if (ok)
  3502. WPACKET_finish(&wpkt);
  3503. else
  3504. WPACKET_cleanup(&wpkt);
  3505. return ok;
  3506. }
  3507. static const struct script_op script_61[] = {
  3508. OP_S_SET_INJECT_PLAIN (script_61_inject_plain)
  3509. OP_C_SET_ALPN ("ossltest")
  3510. OP_C_CONNECT_WAIT ()
  3511. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3512. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3513. OP_C_WRITE (a, "orange", 6)
  3514. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3515. OP_S_READ_EXPECT (a, "orange", 6)
  3516. OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  3517. S_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
  3518. OP_S_WRITE (a, "fruit", 5)
  3519. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  3520. OP_END
  3521. };
  3522. /* 62. Fault injection - STOP_SENDING with high ID */
  3523. static const struct script_op script_62[] = {
  3524. OP_S_SET_INJECT_PLAIN (script_61_inject_plain)
  3525. OP_C_SET_ALPN ("ossltest")
  3526. OP_C_CONNECT_WAIT ()
  3527. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3528. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3529. OP_C_WRITE (a, "orange", 6)
  3530. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3531. OP_S_READ_EXPECT (a, "orange", 6)
  3532. OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
  3533. C_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
  3534. OP_S_WRITE (a, "fruit", 5)
  3535. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0)
  3536. OP_END
  3537. };
  3538. /* 63. Fault injection - STREAM frame exceeding stream limit */
  3539. static const struct script_op script_63[] = {
  3540. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  3541. OP_C_SET_ALPN ("ossltest")
  3542. OP_C_CONNECT_WAIT ()
  3543. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3544. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3545. OP_C_WRITE (a, "apple", 5)
  3546. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3547. OP_S_READ_EXPECT (a, "apple", 5)
  3548. OP_SET_INJECT_WORD (S_BIDI_ID(5000) + 1, 4)
  3549. OP_S_WRITE (a, "orange", 6)
  3550. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
  3551. OP_END
  3552. };
  3553. /* 64. Fault injection - STREAM - zero-length no-FIN is accepted */
  3554. static const struct script_op script_64[] = {
  3555. OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
  3556. OP_C_SET_ALPN ("ossltest")
  3557. OP_C_CONNECT_WAIT ()
  3558. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3559. OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
  3560. OP_S_WRITE (a, "apple", 5)
  3561. OP_C_ACCEPT_STREAM_WAIT (a)
  3562. OP_C_READ_EXPECT (a, "apple", 5)
  3563. OP_SET_INJECT_WORD (S_BIDI_ID(20) + 1, 1)
  3564. OP_S_WRITE (a, "orange", 6)
  3565. OP_C_READ_EXPECT (a, "orange", 6)
  3566. OP_END
  3567. };
  3568. /* 65. Fault injection - CRYPTO - zero-length is accepted */
  3569. static int script_65_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3570. unsigned char *buf, size_t len)
  3571. {
  3572. int ok = 0;
  3573. unsigned char frame_buf[64];
  3574. size_t written;
  3575. WPACKET wpkt;
  3576. if (h->inject_word0 == 0)
  3577. return 1;
  3578. --h->inject_word0;
  3579. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3580. sizeof(frame_buf), 0)))
  3581. return 0;
  3582. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
  3583. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0))
  3584. || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
  3585. goto err;
  3586. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3587. goto err;
  3588. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3589. goto err;
  3590. ok = 1;
  3591. err:
  3592. if (ok)
  3593. WPACKET_finish(&wpkt);
  3594. else
  3595. WPACKET_cleanup(&wpkt);
  3596. return ok;
  3597. }
  3598. static const struct script_op script_65[] = {
  3599. OP_S_SET_INJECT_PLAIN (script_65_inject_plain)
  3600. OP_C_SET_ALPN ("ossltest")
  3601. OP_C_CONNECT_WAIT ()
  3602. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3603. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3604. OP_C_WRITE (a, "apple", 5)
  3605. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3606. OP_S_READ_EXPECT (a, "apple", 5)
  3607. OP_SET_INJECT_WORD (1, 0)
  3608. OP_S_WRITE (a, "orange", 6)
  3609. OP_C_READ_EXPECT (a, "orange", 6)
  3610. OP_END
  3611. };
  3612. /* 66. Fault injection - large MAX_STREAM_DATA */
  3613. static int script_66_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
  3614. unsigned char *buf, size_t len)
  3615. {
  3616. int ok = 0;
  3617. WPACKET wpkt;
  3618. unsigned char frame_buf[64];
  3619. size_t written;
  3620. if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT)
  3621. return 1;
  3622. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  3623. sizeof(frame_buf), 0)))
  3624. return 0;
  3625. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
  3626. goto err;
  3627. if (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  3628. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
  3629. h->inject_word0 - 1)))
  3630. goto err;
  3631. if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_VLINT_MAX)))
  3632. goto err;
  3633. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  3634. goto err;
  3635. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
  3636. goto err;
  3637. ok = 1;
  3638. err:
  3639. if (ok)
  3640. WPACKET_finish(&wpkt);
  3641. else
  3642. WPACKET_cleanup(&wpkt);
  3643. return ok;
  3644. }
  3645. static const struct script_op script_66[] = {
  3646. OP_S_SET_INJECT_PLAIN (script_66_inject_plain)
  3647. OP_C_SET_ALPN ("ossltest")
  3648. OP_C_CONNECT_WAIT ()
  3649. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3650. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  3651. OP_S_WRITE (a, "apple", 5)
  3652. OP_C_ACCEPT_STREAM_WAIT (a)
  3653. OP_C_READ_EXPECT (a, "apple", 5)
  3654. OP_SET_INJECT_WORD (S_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  3655. OP_S_WRITE (a, "orange", 6)
  3656. OP_C_READ_EXPECT (a, "orange", 6)
  3657. OP_C_WRITE (a, "Strawberry", 10)
  3658. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3659. OP_END
  3660. };
  3661. /* 67. Fault injection - large MAX_DATA */
  3662. static const struct script_op script_67[] = {
  3663. OP_S_SET_INJECT_PLAIN (script_66_inject_plain)
  3664. OP_C_SET_ALPN ("ossltest")
  3665. OP_C_CONNECT_WAIT ()
  3666. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3667. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  3668. OP_S_WRITE (a, "apple", 5)
  3669. OP_C_ACCEPT_STREAM_WAIT (a)
  3670. OP_C_READ_EXPECT (a, "apple", 5)
  3671. OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
  3672. OP_S_WRITE (a, "orange", 6)
  3673. OP_C_READ_EXPECT (a, "orange", 6)
  3674. OP_C_WRITE (a, "Strawberry", 10)
  3675. OP_S_READ_EXPECT (a, "Strawberry", 10)
  3676. OP_END
  3677. };
  3678. /* 68. Fault injection - Unexpected TLS messages */
  3679. static int script_68_inject_handshake(struct helper *h, unsigned char *msg,
  3680. size_t msglen)
  3681. {
  3682. const unsigned char *data;
  3683. size_t datalen;
  3684. const unsigned char certreq[] = {
  3685. SSL3_MT_CERTIFICATE_REQUEST, /* CertificateRequest message */
  3686. 0, 0, 12, /* Length of message */
  3687. 1, 1, /* certificate_request_context */
  3688. 0, 8, /* Extensions block length */
  3689. 0, TLSEXT_TYPE_signature_algorithms, /* sig_algs extension*/
  3690. 0, 4, /* 4 bytes of sig algs extension*/
  3691. 0, 2, /* sigalgs list is 2 bytes long */
  3692. 8, 4 /* rsa_pss_rsae_sha256 */
  3693. };
  3694. const unsigned char keyupdate[] = {
  3695. SSL3_MT_KEY_UPDATE, /* KeyUpdate message */
  3696. 0, 0, 1, /* Length of message */
  3697. SSL_KEY_UPDATE_NOT_REQUESTED /* update_not_requested */
  3698. };
  3699. /* We transform the NewSessionTicket message into something else */
  3700. switch(h->inject_word0) {
  3701. case 0:
  3702. return 1;
  3703. case 1:
  3704. /* CertificateRequest message */
  3705. data = certreq;
  3706. datalen = sizeof(certreq);
  3707. break;
  3708. case 2:
  3709. /* KeyUpdate message */
  3710. data = keyupdate;
  3711. datalen = sizeof(keyupdate);
  3712. break;
  3713. default:
  3714. return 0;
  3715. }
  3716. if (!TEST_true(qtest_fault_resize_message(h->qtf,
  3717. datalen - SSL3_HM_HEADER_LENGTH)))
  3718. return 0;
  3719. memcpy(msg, data, datalen);
  3720. return 1;
  3721. }
  3722. /* Send a CerticateRequest message post-handshake */
  3723. static const struct script_op script_68[] = {
  3724. OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
  3725. OP_C_SET_ALPN ("ossltest")
  3726. OP_C_CONNECT_WAIT ()
  3727. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3728. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3729. OP_C_WRITE (a, "apple", 5)
  3730. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3731. OP_S_READ_EXPECT (a, "apple", 5)
  3732. OP_SET_INJECT_WORD (1, 0)
  3733. OP_S_NEW_TICKET ()
  3734. OP_S_WRITE (a, "orange", 6)
  3735. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
  3736. OP_END
  3737. };
  3738. /* 69. Send a TLS KeyUpdate message post-handshake */
  3739. static const struct script_op script_69[] = {
  3740. OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
  3741. OP_C_SET_ALPN ("ossltest")
  3742. OP_C_CONNECT_WAIT ()
  3743. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3744. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3745. OP_C_WRITE (a, "apple", 5)
  3746. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3747. OP_S_READ_EXPECT (a, "apple", 5)
  3748. OP_SET_INJECT_WORD (2, 0)
  3749. OP_S_NEW_TICKET ()
  3750. OP_S_WRITE (a, "orange", 6)
  3751. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN
  3752. + SSL_AD_UNEXPECTED_MESSAGE, 0, 0)
  3753. OP_END
  3754. };
  3755. static int set_max_early_data(struct helper *h, struct helper_local *hl)
  3756. {
  3757. if (!TEST_true(ossl_quic_tserver_set_max_early_data(ACQUIRE_S(),
  3758. (uint32_t)hl->check_op->arg2)))
  3759. return 0;
  3760. return 1;
  3761. }
  3762. /* 70. Send a TLS NewSessionTicket message with invalid max_early_data */
  3763. static const struct script_op script_70[] = {
  3764. OP_C_SET_ALPN ("ossltest")
  3765. OP_C_CONNECT_WAIT ()
  3766. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3767. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3768. OP_C_WRITE (a, "apple", 5)
  3769. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3770. OP_S_READ_EXPECT (a, "apple", 5)
  3771. OP_CHECK (set_max_early_data, 0xfffffffe)
  3772. OP_S_NEW_TICKET ()
  3773. OP_S_WRITE (a, "orange", 6)
  3774. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
  3775. OP_END
  3776. };
  3777. /* 71. Send a TLS NewSessionTicket message with valid max_early_data */
  3778. static const struct script_op script_71[] = {
  3779. OP_C_SET_ALPN ("ossltest")
  3780. OP_C_CONNECT_WAIT ()
  3781. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3782. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3783. OP_C_WRITE (a, "apple", 5)
  3784. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3785. OP_S_READ_EXPECT (a, "apple", 5)
  3786. OP_CHECK (set_max_early_data, 0xffffffff)
  3787. OP_S_NEW_TICKET ()
  3788. OP_S_WRITE (a, "orange", 6)
  3789. OP_C_READ_EXPECT (a, "orange", 6)
  3790. OP_END
  3791. };
  3792. /* 72. Test that APL stops handing out streams after limit reached (bidi) */
  3793. static int script_72_check(struct helper *h, struct helper_local *hl)
  3794. {
  3795. if (!TEST_uint64_t_ge(h->fail_count, 50))
  3796. return 0;
  3797. return 1;
  3798. }
  3799. static const struct script_op script_72[] = {
  3800. OP_C_SET_ALPN ("ossltest")
  3801. OP_C_CONNECT_WAIT ()
  3802. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3803. /*
  3804. * Request more streams than a server will initially hand out and test that
  3805. * they fail properly.
  3806. */
  3807. OP_BEGIN_REPEAT (200)
  3808. OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
  3809. OP_C_SKIP_IF_UNBOUND (a, 2)
  3810. OP_C_WRITE (a, "apple", 5)
  3811. OP_C_FREE_STREAM (a)
  3812. OP_END_REPEAT ()
  3813. OP_CHECK (script_72_check, 0)
  3814. OP_END
  3815. };
  3816. /* 73. Test that APL stops handing out streams after limit reached (uni) */
  3817. static const struct script_op script_73[] = {
  3818. OP_C_SET_ALPN ("ossltest")
  3819. OP_C_CONNECT_WAIT ()
  3820. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3821. /*
  3822. * Request more streams than a server will initially hand out and test that
  3823. * they fail properly.
  3824. */
  3825. OP_BEGIN_REPEAT (200)
  3826. OP_C_NEW_STREAM_UNI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
  3827. OP_C_SKIP_IF_UNBOUND (a, 2)
  3828. OP_C_WRITE (a, "apple", 5)
  3829. OP_C_FREE_STREAM (a)
  3830. OP_END_REPEAT ()
  3831. OP_CHECK (script_72_check, 0)
  3832. OP_END
  3833. };
  3834. /* 74. Version negotiation: QUIC_VERSION_1 ignored */
  3835. static int generate_version_neg(WPACKET *wpkt, uint32_t version)
  3836. {
  3837. QUIC_PKT_HDR hdr = {0};
  3838. hdr.type = QUIC_PKT_TYPE_VERSION_NEG;
  3839. hdr.fixed = 1;
  3840. hdr.dst_conn_id.id_len = 0;
  3841. hdr.src_conn_id.id_len = 8;
  3842. memset(hdr.src_conn_id.id, 0x55, 8);
  3843. if (!TEST_true(ossl_quic_wire_encode_pkt_hdr(wpkt, 0, &hdr, NULL)))
  3844. return 0;
  3845. if (!TEST_true(WPACKET_put_bytes_u32(wpkt, version)))
  3846. return 0;
  3847. return 1;
  3848. }
  3849. static int server_gen_version_neg(struct helper *h, BIO_MSG *msg, size_t stride)
  3850. {
  3851. int rc = 0, have_wpkt = 0;
  3852. size_t l;
  3853. WPACKET wpkt;
  3854. BUF_MEM *buf = NULL;
  3855. uint32_t version;
  3856. switch (h->inject_word0) {
  3857. case 0:
  3858. return 1;
  3859. case 1:
  3860. version = QUIC_VERSION_1;
  3861. break;
  3862. default:
  3863. version = 0x5432abcd;
  3864. break;
  3865. }
  3866. if (!TEST_ptr(buf = BUF_MEM_new()))
  3867. goto err;
  3868. if (!TEST_true(WPACKET_init(&wpkt, buf)))
  3869. goto err;
  3870. have_wpkt = 1;
  3871. generate_version_neg(&wpkt, version);
  3872. if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
  3873. goto err;
  3874. if (!TEST_true(qtest_fault_resize_datagram(h->qtf, l)))
  3875. return 0;
  3876. memcpy(msg->data, buf->data, l);
  3877. h->inject_word0 = 0;
  3878. rc = 1;
  3879. err:
  3880. if (have_wpkt)
  3881. WPACKET_finish(&wpkt);
  3882. BUF_MEM_free(buf);
  3883. return rc;
  3884. }
  3885. static const struct script_op script_74[] = {
  3886. OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg)
  3887. OP_SET_INJECT_WORD (1, 0)
  3888. OP_C_SET_ALPN ("ossltest")
  3889. OP_C_CONNECT_WAIT ()
  3890. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3891. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3892. OP_C_WRITE (a, "apple", 5)
  3893. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3894. OP_S_READ_EXPECT (a, "apple", 5)
  3895. OP_END
  3896. };
  3897. /* 75. Version negotiation: Unknown version causes connection abort */
  3898. static const struct script_op script_75[] = {
  3899. OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg)
  3900. OP_SET_INJECT_WORD (2, 0)
  3901. OP_C_SET_ALPN ("ossltest")
  3902. OP_C_CONNECT_WAIT_OR_FAIL()
  3903. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CONNECTION_REFUSED,0,0)
  3904. OP_END
  3905. };
  3906. /* 76. Test peer-initiated shutdown wait */
  3907. static int script_76_check(struct helper *h, struct helper_local *hl)
  3908. {
  3909. if (!TEST_false(SSL_shutdown_ex(h->c_conn,
  3910. SSL_SHUTDOWN_FLAG_WAIT_PEER
  3911. | SSL_SHUTDOWN_FLAG_NO_BLOCK,
  3912. NULL, 0)))
  3913. return 0;
  3914. return 1;
  3915. }
  3916. static const struct script_op script_76[] = {
  3917. OP_C_SET_ALPN ("ossltest")
  3918. OP_C_CONNECT_WAIT ()
  3919. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3920. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3921. OP_C_WRITE (a, "apple", 5)
  3922. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3923. OP_S_READ_EXPECT (a, "apple", 5)
  3924. /* Check a WAIT_PEER call doesn't succeed yet. */
  3925. OP_CHECK (script_76_check, 0)
  3926. OP_S_SHUTDOWN (42)
  3927. OP_C_SHUTDOWN_WAIT (NULL, SSL_SHUTDOWN_FLAG_WAIT_PEER)
  3928. OP_C_EXPECT_CONN_CLOSE_INFO(42, 1, 1)
  3929. OP_END
  3930. };
  3931. /* 77. Ensure default stream popping operates correctly */
  3932. static const struct script_op script_77[] = {
  3933. OP_C_SET_ALPN ("ossltest")
  3934. OP_C_CONNECT_WAIT ()
  3935. OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
  3936. OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
  3937. OP_S_WRITE (a, "Strawberry", 10)
  3938. OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
  3939. OP_S_NEW_STREAM_BIDI (b, S_BIDI_ID(1))
  3940. OP_S_WRITE (b, "xyz", 3)
  3941. OP_C_ACCEPT_STREAM_WAIT (b)
  3942. OP_C_READ_EXPECT (b, "xyz", 3)
  3943. OP_END
  3944. };
  3945. /* 78. Post-connection session ticket handling */
  3946. static size_t new_session_count;
  3947. static int on_new_session(SSL *s, SSL_SESSION *sess)
  3948. {
  3949. ++new_session_count;
  3950. return 0; /* do not ref session, we aren't keeping it */
  3951. }
  3952. static int setup_session(struct helper *h, struct helper_local *hl)
  3953. {
  3954. SSL_CTX_set_session_cache_mode(h->c_ctx, SSL_SESS_CACHE_BOTH);
  3955. SSL_CTX_sess_set_new_cb(h->c_ctx, on_new_session);
  3956. return 1;
  3957. }
  3958. static int trigger_late_session_ticket(struct helper *h, struct helper_local *hl)
  3959. {
  3960. new_session_count = 0;
  3961. if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S())))
  3962. return 0;
  3963. return 1;
  3964. }
  3965. static int check_got_session_ticket(struct helper *h, struct helper_local *hl)
  3966. {
  3967. if (!TEST_size_t_gt(new_session_count, 0))
  3968. return 0;
  3969. return 1;
  3970. }
  3971. static int check_idle_timeout(struct helper *h, struct helper_local *hl);
  3972. static const struct script_op script_78[] = {
  3973. OP_C_SET_ALPN ("ossltest")
  3974. OP_CHECK (setup_session, 0)
  3975. OP_C_CONNECT_WAIT ()
  3976. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  3977. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  3978. OP_C_WRITE (a, "apple", 5)
  3979. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3980. OP_S_READ_EXPECT (a, "apple", 5)
  3981. OP_S_WRITE (a, "orange", 6)
  3982. OP_C_READ_EXPECT (a, "orange", 6)
  3983. OP_CHECK (trigger_late_session_ticket, 0)
  3984. OP_S_WRITE (a, "Strawberry", 10)
  3985. OP_C_READ_EXPECT (a, "Strawberry", 10)
  3986. OP_CHECK (check_got_session_ticket, 0)
  3987. OP_CHECK2 (check_idle_timeout,
  3988. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
  3989. OP_END
  3990. };
  3991. /* 79. Optimised FIN test */
  3992. static const struct script_op script_79[] = {
  3993. OP_C_SET_ALPN ("ossltest")
  3994. OP_C_CONNECT_WAIT ()
  3995. OP_C_WRITE_EX2 (DEFAULT, "apple", 5, SSL_WRITE_FLAG_CONCLUDE)
  3996. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  3997. OP_S_READ_EXPECT (a, "apple", 5)
  3998. OP_S_EXPECT_FIN (a)
  3999. OP_S_WRITE (a, "orange", 6)
  4000. OP_S_CONCLUDE (a)
  4001. OP_C_READ_EXPECT (DEFAULT, "orange", 6)
  4002. OP_C_EXPECT_FIN (DEFAULT)
  4003. OP_END
  4004. };
  4005. /* 80. Stateless reset detection test */
  4006. static QUIC_STATELESS_RESET_TOKEN test_reset_token = {
  4007. { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
  4008. 0xde, 0xad, 0xbe, 0xef }};
  4009. /*
  4010. * Generate a packet in the following format:
  4011. * https://www.rfc-editor.org/rfc/rfc9000.html#name-stateless-reset
  4012. * Stateless Reset {
  4013. * Fixed Bits (2): 1
  4014. * Unpredictable bits (38..)
  4015. * Stateless reset token (128)
  4016. * }
  4017. */
  4018. static int script_80_send_stateless_reset(struct helper *h, QUIC_PKT_HDR *hdr,
  4019. unsigned char *buf, size_t len)
  4020. {
  4021. unsigned char databuf[64];
  4022. if (h->inject_word1 == 0)
  4023. return 1;
  4024. h->inject_word1 = 0;
  4025. fprintf(stderr, "Sending stateless reset\n");
  4026. RAND_bytes(databuf, 64);
  4027. databuf[0] = 0x40;
  4028. memcpy(&databuf[48], test_reset_token.token,
  4029. sizeof(test_reset_token.token));
  4030. if (!TEST_int_eq(SSL_inject_net_dgram(h->c_conn, databuf, sizeof(databuf),
  4031. NULL, h->s_net_bio_addr), 1))
  4032. return 0;
  4033. return 1;
  4034. }
  4035. static int script_80_gen_new_conn_id(struct helper *h, QUIC_PKT_HDR *hdr,
  4036. unsigned char *buf, size_t len)
  4037. {
  4038. int rc = 0;
  4039. size_t l;
  4040. unsigned char frame_buf[64];
  4041. WPACKET wpkt;
  4042. QUIC_CONN_ID new_cid = {0};
  4043. OSSL_QUIC_FRAME_NEW_CONN_ID ncid = {0};
  4044. QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S_NOHL());
  4045. if (h->inject_word0 == 0)
  4046. return 1;
  4047. h->inject_word0 = 0;
  4048. fprintf(stderr, "sending new conn id\n");
  4049. if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
  4050. sizeof(frame_buf), 0)))
  4051. return 0;
  4052. ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
  4053. ncid.seq_num = 2;
  4054. ncid.retire_prior_to = 2;
  4055. ncid.conn_id = new_cid;
  4056. memcpy(ncid.stateless_reset.token, test_reset_token.token,
  4057. sizeof(test_reset_token.token));
  4058. if (!TEST_true(ossl_quic_wire_encode_frame_new_conn_id(&wpkt, &ncid)))
  4059. goto err;
  4060. if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
  4061. goto err;
  4062. if (!qtest_fault_prepend_frame(h->qtf, frame_buf, l))
  4063. goto err;
  4064. rc = 1;
  4065. err:
  4066. if (rc)
  4067. WPACKET_finish(&wpkt);
  4068. else
  4069. WPACKET_cleanup(&wpkt);
  4070. return rc;
  4071. }
  4072. static int script_80_inject_pkt(struct helper *h, QUIC_PKT_HDR *hdr,
  4073. unsigned char *buf, size_t len)
  4074. {
  4075. if (h->inject_word1 == 1)
  4076. return script_80_send_stateless_reset(h, hdr, buf, len);
  4077. else if (h->inject_word0 == 1)
  4078. return script_80_gen_new_conn_id(h, hdr, buf, len);
  4079. return 1;
  4080. }
  4081. static const struct script_op script_80[] = {
  4082. OP_S_SET_INJECT_PLAIN (script_80_inject_pkt)
  4083. OP_C_SET_ALPN ("ossltest")
  4084. OP_C_CONNECT_WAIT ()
  4085. OP_C_WRITE (DEFAULT, "apple", 5)
  4086. OP_C_CONCLUDE (DEFAULT)
  4087. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4088. OP_S_READ_EXPECT (a, "apple", 5)
  4089. OP_SET_INJECT_WORD (1, 0)
  4090. OP_S_WRITE (a, "apple", 5)
  4091. OP_C_READ_EXPECT (DEFAULT, "apple", 5)
  4092. OP_SET_INJECT_WORD (0, 1)
  4093. OP_S_WRITE (a, "apple", 5)
  4094. OP_C_EXPECT_CONN_CLOSE_INFO (0, 0, 1)
  4095. OP_END
  4096. };
  4097. /* 81. Idle timeout configuration */
  4098. static int modify_idle_timeout(struct helper *h, struct helper_local *hl)
  4099. {
  4100. uint64_t v = 0;
  4101. /* Test bad value is rejected. */
  4102. if (!TEST_false(SSL_set_feature_request_uint(h->c_conn,
  4103. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4104. (1ULL << 62))))
  4105. return 0;
  4106. /* Set value. */
  4107. if (!TEST_true(SSL_set_feature_request_uint(h->c_conn,
  4108. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4109. hl->check_op->arg2)))
  4110. return 0;
  4111. if (!TEST_true(SSL_get_feature_request_uint(h->c_conn,
  4112. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4113. &v)))
  4114. return 0;
  4115. if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
  4116. return 0;
  4117. return 1;
  4118. }
  4119. static int check_idle_timeout(struct helper *h, struct helper_local *hl)
  4120. {
  4121. uint64_t v = 0;
  4122. if (!TEST_true(SSL_get_value_uint(h->c_conn, hl->check_op->arg1,
  4123. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4124. &v)))
  4125. return 0;
  4126. if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
  4127. return 0;
  4128. return 1;
  4129. }
  4130. static const struct script_op script_81[] = {
  4131. OP_C_SET_ALPN ("ossltest")
  4132. OP_CHECK (modify_idle_timeout, 25000)
  4133. OP_C_CONNECT_WAIT ()
  4134. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4135. OP_CHECK2 (check_idle_timeout,
  4136. SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
  4137. OP_CHECK2 (check_idle_timeout,
  4138. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 25000)
  4139. OP_END
  4140. };
  4141. /* 82. Negotiated default idle timeout if not configured */
  4142. static const struct script_op script_82[] = {
  4143. OP_C_SET_ALPN ("ossltest")
  4144. OP_C_CONNECT_WAIT ()
  4145. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4146. OP_CHECK2 (check_idle_timeout,
  4147. SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
  4148. OP_CHECK2 (check_idle_timeout,
  4149. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
  4150. OP_END
  4151. };
  4152. /* 83. No late changes to idle timeout */
  4153. static int cannot_change_idle_timeout(struct helper *h, struct helper_local *hl)
  4154. {
  4155. uint64_t v = 0;
  4156. if (!TEST_true(SSL_get_feature_request_uint(h->c_conn,
  4157. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4158. &v)))
  4159. return 0;
  4160. if (!TEST_uint64_t_eq(v, 30000))
  4161. return 0;
  4162. if (!TEST_false(SSL_set_feature_request_uint(h->c_conn,
  4163. SSL_VALUE_QUIC_IDLE_TIMEOUT,
  4164. 5000)))
  4165. return 0;
  4166. return 1;
  4167. }
  4168. static const struct script_op script_83[] = {
  4169. OP_C_SET_ALPN ("ossltest")
  4170. OP_C_CONNECT_WAIT ()
  4171. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4172. OP_CHECK (cannot_change_idle_timeout, 0)
  4173. OP_CHECK2 (check_idle_timeout,
  4174. SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000)
  4175. OP_CHECK2 (check_idle_timeout,
  4176. SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000)
  4177. OP_END
  4178. };
  4179. /* 84. Test query of available streams */
  4180. static int check_avail_streams(struct helper *h, struct helper_local *hl)
  4181. {
  4182. uint64_t v = 0;
  4183. switch (hl->check_op->arg1) {
  4184. case 0:
  4185. if (!TEST_true(SSL_get_quic_stream_bidi_local_avail(h->c_conn, &v)))
  4186. return 0;
  4187. break;
  4188. case 1:
  4189. if (!TEST_true(SSL_get_quic_stream_bidi_remote_avail(h->c_conn, &v)))
  4190. return 0;
  4191. break;
  4192. case 2:
  4193. if (!TEST_true(SSL_get_quic_stream_uni_local_avail(h->c_conn, &v)))
  4194. return 0;
  4195. break;
  4196. case 3:
  4197. if (!TEST_true(SSL_get_quic_stream_uni_remote_avail(h->c_conn, &v)))
  4198. return 0;
  4199. break;
  4200. default:
  4201. return 0;
  4202. }
  4203. if (!TEST_uint64_t_eq(v, hl->check_op->arg2))
  4204. return 0;
  4205. return 1;
  4206. }
  4207. static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl);
  4208. static int reenable_test_event_handling(struct helper *h, struct helper_local *hl);
  4209. static int check_write_buf_stat(struct helper *h, struct helper_local *hl)
  4210. {
  4211. SSL *c_a;
  4212. uint64_t size, used, avail;
  4213. if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a")))
  4214. return 0;
  4215. if (!TEST_true(SSL_get_stream_write_buf_size(c_a, &size))
  4216. || !TEST_true(SSL_get_stream_write_buf_used(c_a, &used))
  4217. || !TEST_true(SSL_get_stream_write_buf_avail(c_a, &avail))
  4218. || !TEST_uint64_t_ge(size, avail)
  4219. || !TEST_uint64_t_ge(size, used)
  4220. || !TEST_uint64_t_eq(avail + used, size))
  4221. return 0;
  4222. if (!TEST_uint64_t_eq(used, hl->check_op->arg1))
  4223. return 0;
  4224. return 1;
  4225. }
  4226. static const struct script_op script_84[] = {
  4227. OP_C_SET_ALPN ("ossltest")
  4228. OP_C_CONNECT_WAIT ()
  4229. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4230. OP_CHECK2 (check_avail_streams, 0, 100)
  4231. OP_CHECK2 (check_avail_streams, 1, 100)
  4232. OP_CHECK2 (check_avail_streams, 2, 100)
  4233. OP_CHECK2 (check_avail_streams, 3, 100)
  4234. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4235. OP_CHECK2 (check_avail_streams, 0, 99)
  4236. OP_CHECK2 (check_avail_streams, 1, 100)
  4237. OP_CHECK2 (check_avail_streams, 2, 100)
  4238. OP_CHECK2 (check_avail_streams, 3, 100)
  4239. OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
  4240. OP_CHECK2 (check_avail_streams, 0, 99)
  4241. OP_CHECK2 (check_avail_streams, 1, 100)
  4242. OP_CHECK2 (check_avail_streams, 2, 99)
  4243. OP_CHECK2 (check_avail_streams, 3, 100)
  4244. OP_S_NEW_STREAM_BIDI (c, S_BIDI_ID(0))
  4245. OP_S_WRITE (c, "x", 1)
  4246. OP_C_ACCEPT_STREAM_WAIT (c)
  4247. OP_C_READ_EXPECT (c, "x", 1)
  4248. OP_CHECK2 (check_avail_streams, 0, 99)
  4249. OP_CHECK2 (check_avail_streams, 1, 99)
  4250. OP_CHECK2 (check_avail_streams, 2, 99)
  4251. OP_CHECK2 (check_avail_streams, 3, 100)
  4252. OP_S_NEW_STREAM_UNI (d, S_UNI_ID(0))
  4253. OP_S_WRITE (d, "x", 1)
  4254. OP_C_ACCEPT_STREAM_WAIT (d)
  4255. OP_C_READ_EXPECT (d, "x", 1)
  4256. OP_CHECK2 (check_avail_streams, 0, 99)
  4257. OP_CHECK2 (check_avail_streams, 1, 99)
  4258. OP_CHECK2 (check_avail_streams, 2, 99)
  4259. OP_CHECK2 (check_avail_streams, 3, 99)
  4260. OP_CHECK2 (check_write_buf_stat, 0, 0)
  4261. OP_CHECK (set_event_handling_mode_conn,
  4262. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4263. OP_C_WRITE (a, "apple", 5)
  4264. OP_CHECK2 (check_write_buf_stat, 5, 0)
  4265. OP_CHECK (reenable_test_event_handling, 0)
  4266. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4267. OP_S_READ_EXPECT (a, "apple", 5)
  4268. OP_S_WRITE (a, "orange", 6)
  4269. OP_C_READ_EXPECT (a, "orange", 6)
  4270. OP_CHECK2 (check_write_buf_stat, 0, 0)
  4271. OP_END
  4272. };
  4273. /* 85. Test SSL_poll (lite, non-blocking) */
  4274. ossl_unused static int script_85_poll(struct helper *h, struct helper_local *hl)
  4275. {
  4276. int ok = 1, ret, expected_ret = 1;
  4277. static const struct timeval timeout = {0};
  4278. static const struct timeval nz_timeout = {0, 1};
  4279. size_t result_count, expected_result_count = 0;
  4280. SSL_POLL_ITEM items[5] = {0}, *item = items;
  4281. SSL *c_a, *c_b, *c_c, *c_d;
  4282. size_t i;
  4283. uint64_t mode, expected_revents[5] = {0};
  4284. if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a"))
  4285. || !TEST_ptr(c_b = helper_local_get_c_stream(hl, "b"))
  4286. || !TEST_ptr(c_c = helper_local_get_c_stream(hl, "c"))
  4287. || !TEST_ptr(c_d = helper_local_get_c_stream(hl, "d")))
  4288. return 0;
  4289. item->desc = SSL_as_poll_descriptor(c_a);
  4290. item->events = UINT64_MAX;
  4291. item->revents = UINT64_MAX;
  4292. ++item;
  4293. item->desc = SSL_as_poll_descriptor(c_b);
  4294. item->events = UINT64_MAX;
  4295. item->revents = UINT64_MAX;
  4296. ++item;
  4297. item->desc = SSL_as_poll_descriptor(c_c);
  4298. item->events = UINT64_MAX;
  4299. item->revents = UINT64_MAX;
  4300. ++item;
  4301. item->desc = SSL_as_poll_descriptor(c_d);
  4302. item->events = UINT64_MAX;
  4303. item->revents = UINT64_MAX;
  4304. ++item;
  4305. item->desc = SSL_as_poll_descriptor(h->c_conn);
  4306. item->events = UINT64_MAX;
  4307. item->revents = UINT64_MAX;
  4308. ++item;
  4309. /* Non-zero timeout is not supported. */
  4310. result_count = SIZE_MAX;
  4311. ERR_set_mark();
  4312. if (!TEST_false(SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM),
  4313. &nz_timeout, 0,
  4314. &result_count))
  4315. || !TEST_size_t_eq(result_count, 0))
  4316. return 0;
  4317. ERR_pop_to_mark();
  4318. result_count = SIZE_MAX;
  4319. ret = SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM),
  4320. &timeout, 0,
  4321. &result_count);
  4322. mode = hl->check_op->arg2;
  4323. switch (mode) {
  4324. case 0:
  4325. /* No incoming data yet */
  4326. expected_revents[0] = SSL_POLL_EVENT_W;
  4327. expected_revents[1] = SSL_POLL_EVENT_W;
  4328. expected_revents[2] = SSL_POLL_EVENT_W;
  4329. expected_revents[3] = SSL_POLL_EVENT_W;
  4330. expected_revents[4] = SSL_POLL_EVENT_OS;
  4331. expected_result_count = 5;
  4332. break;
  4333. case 1:
  4334. /* Expect more events */
  4335. expected_revents[0] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_R;
  4336. expected_revents[1] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_ER;
  4337. expected_revents[2] = SSL_POLL_EVENT_EW;
  4338. expected_revents[3] = SSL_POLL_EVENT_W;
  4339. expected_revents[4] = SSL_POLL_EVENT_OS | SSL_POLL_EVENT_ISB;
  4340. expected_result_count = 5;
  4341. break;
  4342. default:
  4343. return 0;
  4344. }
  4345. if (!TEST_int_eq(ret, expected_ret)
  4346. || !TEST_size_t_eq(result_count, expected_result_count))
  4347. ok = 0;
  4348. for (i = 0; i < OSSL_NELEM(items); ++i)
  4349. if (!TEST_uint64_t_eq(items[i].revents, expected_revents[i])) {
  4350. TEST_error("mismatch at index %zu in poll results, mode %d",
  4351. i, (int)mode);
  4352. ok = 0;
  4353. }
  4354. return ok;
  4355. }
  4356. static const struct script_op script_85[] = {
  4357. OP_C_SET_ALPN ("ossltest")
  4358. OP_C_CONNECT_WAIT ()
  4359. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4360. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4361. OP_C_WRITE (a, "flamingo", 8)
  4362. OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
  4363. OP_C_WRITE (b, "orange", 6)
  4364. OP_C_NEW_STREAM_BIDI (c, C_BIDI_ID(2))
  4365. OP_C_WRITE (c, "Strawberry", 10)
  4366. OP_C_NEW_STREAM_BIDI (d, C_BIDI_ID(3))
  4367. OP_C_WRITE (d, "sync", 4)
  4368. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4369. OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
  4370. OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2))
  4371. OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3))
  4372. /* Check nothing readable yet. */
  4373. OP_CHECK (script_85_poll, 0)
  4374. /* Send something that will make client sockets readable. */
  4375. OP_S_READ_EXPECT (a, "flamingo", 8)
  4376. OP_S_WRITE (a, "herringbone", 11)
  4377. /* Send something that will make 'b' reset. */
  4378. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  4379. OP_SET_INJECT_WORD (C_BIDI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  4380. /* Ensure sync. */
  4381. OP_S_READ_EXPECT (d, "sync", 4)
  4382. OP_S_WRITE (d, "x", 1)
  4383. OP_C_READ_EXPECT (d, "x", 1)
  4384. /* Send something that will make 'c' reset. */
  4385. OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
  4386. OP_SET_INJECT_WORD (C_BIDI_ID(2) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  4387. OP_S_NEW_STREAM_BIDI (z, S_BIDI_ID(0))
  4388. OP_S_WRITE (z, "z", 1)
  4389. /* Ensure sync. */
  4390. OP_S_WRITE (d, "x", 1)
  4391. OP_C_READ_EXPECT (d, "x", 1)
  4392. /* Check a is now readable. */
  4393. OP_CHECK (script_85_poll, 1)
  4394. OP_END
  4395. };
  4396. /* 86. Event Handling Mode Configuration */
  4397. static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl)
  4398. {
  4399. hl->explicit_event_handling = 1;
  4400. return SSL_set_event_handling_mode(h->c_conn, hl->check_op->arg2);
  4401. }
  4402. static int reenable_test_event_handling(struct helper *h, struct helper_local *hl)
  4403. {
  4404. hl->explicit_event_handling = 0;
  4405. return 1;
  4406. }
  4407. static ossl_unused int set_event_handling_mode_stream(struct helper *h, struct helper_local *hl)
  4408. {
  4409. SSL *ssl = helper_local_get_c_stream(hl, "a");
  4410. if (!TEST_ptr(ssl))
  4411. return 0;
  4412. return SSL_set_event_handling_mode(ssl, hl->check_op->arg2);
  4413. }
  4414. static const struct script_op script_86[] = {
  4415. OP_SKIP_IF_BLOCKING (23)
  4416. OP_C_SET_ALPN ("ossltest")
  4417. OP_C_CONNECT_WAIT ()
  4418. OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
  4419. /* Turn on explicit handling mode. */
  4420. OP_CHECK (set_event_handling_mode_conn,
  4421. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4422. /*
  4423. * Create a new stream and write data. This won't get sent
  4424. * to the network net because we are in explicit mode
  4425. * and we haven't called SSL_handle_events().
  4426. */
  4427. OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
  4428. OP_C_WRITE (a, "apple", 5)
  4429. /* Put connection back into implicit handling mode. */
  4430. OP_CHECK (set_event_handling_mode_conn,
  4431. SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT)
  4432. /* Override at stream level. */
  4433. OP_CHECK (set_event_handling_mode_stream,
  4434. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4435. OP_C_WRITE (a, "orange", 6)
  4436. OP_C_CONCLUDE (a)
  4437. /*
  4438. * Confirm the data isn't going to arrive. OP_SLEEP is always undesirable
  4439. * but we have no reasonable way to synchronise on something not arriving
  4440. * given all network traffic is essentially stopped and there are no other
  4441. * signals arriving from the peer which could be used for synchronisation.
  4442. * Slow OSes will pass this anyway (fail-open).
  4443. */
  4444. OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
  4445. OP_BEGIN_REPEAT (20)
  4446. OP_S_READ_FAIL (a, 1)
  4447. OP_SLEEP (10)
  4448. OP_END_REPEAT ()
  4449. /* Now let the data arrive and confirm it arrives. */
  4450. OP_CHECK (reenable_test_event_handling, 0)
  4451. OP_S_READ_EXPECT (a, "appleorange", 11)
  4452. OP_S_EXPECT_FIN (a)
  4453. /* Back into explicit mode. */
  4454. OP_CHECK (set_event_handling_mode_conn,
  4455. SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT)
  4456. OP_S_WRITE (a, "ok", 2)
  4457. OP_C_READ_FAIL (a)
  4458. /* Works once event handling is done. */
  4459. OP_CHECK (reenable_test_event_handling, 0)
  4460. OP_C_READ_EXPECT (a, "ok", 2)
  4461. OP_END
  4462. };
  4463. static const struct script_op *const scripts[] = {
  4464. script_1,
  4465. script_2,
  4466. script_3,
  4467. script_4,
  4468. script_5,
  4469. script_6,
  4470. script_7,
  4471. script_8,
  4472. script_9,
  4473. script_10,
  4474. script_11,
  4475. script_12,
  4476. script_13,
  4477. script_14,
  4478. script_15,
  4479. script_16,
  4480. script_17,
  4481. script_18,
  4482. script_19,
  4483. script_20,
  4484. script_21,
  4485. script_22,
  4486. script_23,
  4487. script_24,
  4488. script_25,
  4489. script_26,
  4490. script_27,
  4491. script_28,
  4492. script_29,
  4493. script_30,
  4494. script_31,
  4495. script_32,
  4496. script_33,
  4497. script_34,
  4498. script_35,
  4499. script_36,
  4500. script_37,
  4501. script_38,
  4502. script_39,
  4503. script_40,
  4504. script_41,
  4505. script_42,
  4506. script_43,
  4507. script_44,
  4508. script_45,
  4509. script_46,
  4510. script_47,
  4511. script_48,
  4512. script_49,
  4513. script_50,
  4514. script_51,
  4515. script_52,
  4516. script_53,
  4517. script_54,
  4518. script_55,
  4519. script_56,
  4520. script_57,
  4521. script_58,
  4522. script_59,
  4523. script_60,
  4524. script_61,
  4525. script_62,
  4526. script_63,
  4527. script_64,
  4528. script_65,
  4529. script_66,
  4530. script_67,
  4531. script_68,
  4532. script_69,
  4533. script_70,
  4534. script_71,
  4535. script_72,
  4536. script_73,
  4537. script_74,
  4538. script_75,
  4539. script_76,
  4540. script_77,
  4541. script_78,
  4542. script_79,
  4543. script_80,
  4544. script_81,
  4545. script_82,
  4546. script_83,
  4547. script_84,
  4548. script_85,
  4549. script_86
  4550. };
  4551. static int test_script(int idx)
  4552. {
  4553. int script_idx, free_order, blocking;
  4554. char script_name[64];
  4555. free_order = idx % 2;
  4556. idx /= 2;
  4557. blocking = idx % 2;
  4558. idx /= 2;
  4559. script_idx = idx;
  4560. if (blocking && free_order)
  4561. return 1; /* don't need to test free_order twice */
  4562. #if !defined(OPENSSL_THREADS)
  4563. if (blocking) {
  4564. TEST_skip("cannot test in blocking mode without threads");
  4565. return 1;
  4566. }
  4567. #endif
  4568. BIO_snprintf(script_name, sizeof(script_name), "script %d", script_idx + 1);
  4569. TEST_info("Running script %d (order=%d, blocking=%d)", script_idx + 1,
  4570. free_order, blocking);
  4571. return run_script(scripts[script_idx], script_name, free_order, blocking);
  4572. }
  4573. /* Dynamically generated tests. */
  4574. static struct script_op dyn_frame_types_script[] = {
  4575. OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
  4576. OP_SET_INJECT_WORD (0, 0) /* dynamic */
  4577. OP_C_SET_ALPN ("ossltest")
  4578. OP_C_CONNECT_WAIT_OR_FAIL()
  4579. OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
  4580. OP_END
  4581. };
  4582. struct forbidden_frame_type {
  4583. uint64_t pkt_type, frame_type, expected_err;
  4584. };
  4585. static const struct forbidden_frame_type forbidden_frame_types[] = {
  4586. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
  4587. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
  4588. { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR },
  4589. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4590. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4591. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4592. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4593. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4594. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4595. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4596. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4597. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4598. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4599. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4600. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4601. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4602. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4603. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4604. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4605. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4606. { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4607. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4608. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4609. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4610. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4611. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4612. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4613. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4614. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4615. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4616. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4617. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4618. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4619. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4620. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4621. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4622. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4623. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4624. { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4625. /* Client uses a zero-length CID so this is not allowed. */
  4626. { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION },
  4627. };
  4628. static ossl_unused int test_dyn_frame_types(int idx)
  4629. {
  4630. size_t i;
  4631. char script_name[64];
  4632. struct script_op *s = dyn_frame_types_script;
  4633. for (i = 0; i < OSSL_NELEM(dyn_frame_types_script); ++i)
  4634. if (s[i].op == OPK_SET_INJECT_WORD) {
  4635. s[i].arg1 = (size_t)forbidden_frame_types[idx].pkt_type;
  4636. s[i].arg2 = forbidden_frame_types[idx].frame_type;
  4637. } else if (s[i].op == OPK_C_EXPECT_CONN_CLOSE_INFO) {
  4638. s[i].arg2 = forbidden_frame_types[idx].expected_err;
  4639. }
  4640. BIO_snprintf(script_name, sizeof(script_name),
  4641. "dyn script %d", idx);
  4642. return run_script(dyn_frame_types_script, script_name, 0, 0);
  4643. }
  4644. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  4645. int setup_tests(void)
  4646. {
  4647. if (!test_skip_common_options()) {
  4648. TEST_error("Error parsing test options\n");
  4649. return 0;
  4650. }
  4651. if (!TEST_ptr(certfile = test_get_argument(0))
  4652. || !TEST_ptr(keyfile = test_get_argument(1)))
  4653. return 0;
  4654. ADD_ALL_TESTS(test_dyn_frame_types, OSSL_NELEM(forbidden_frame_types));
  4655. ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2 * 2);
  4656. return 1;
  4657. }