quic_multistream_test.c 155 KB

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