1
0

quic_impl.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211
  1. /*
  2. * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/macros.h>
  10. #include <openssl/objects.h>
  11. #include <openssl/sslerr.h>
  12. #include <crypto/rand.h>
  13. #include "quic_local.h"
  14. #include "internal/quic_tls.h"
  15. #include "internal/quic_rx_depack.h"
  16. #include "internal/quic_error.h"
  17. #include "internal/quic_engine.h"
  18. #include "internal/quic_port.h"
  19. #include "internal/time.h"
  20. typedef struct qctx_st QCTX;
  21. static void aon_write_finish(QUIC_XSO *xso);
  22. static int create_channel(QUIC_CONNECTION *qc);
  23. static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
  24. static int qc_try_create_default_xso_for_write(QCTX *ctx);
  25. static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek);
  26. static void quic_lock(QUIC_CONNECTION *qc);
  27. static void quic_unlock(QUIC_CONNECTION *qc);
  28. static void quic_lock_for_io(QCTX *ctx);
  29. static int quic_do_handshake(QCTX *ctx);
  30. static void qc_update_reject_policy(QUIC_CONNECTION *qc);
  31. static void qc_touch_default_xso(QUIC_CONNECTION *qc);
  32. static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
  33. static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
  34. int touch, QUIC_XSO **old_xso);
  35. static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
  36. static int quic_validate_for_write(QUIC_XSO *xso, int *err);
  37. static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
  38. static int qc_blocking_mode(const QUIC_CONNECTION *qc);
  39. static int xso_blocking_mode(const QUIC_XSO *xso);
  40. static void qctx_maybe_autotick(QCTX *ctx);
  41. static int qctx_should_autotick(QCTX *ctx);
  42. /*
  43. * QUIC Front-End I/O API: Common Utilities
  44. * ========================================
  45. */
  46. /*
  47. * Block until a predicate is met.
  48. *
  49. * Precondition: Must have a channel.
  50. * Precondition: Must hold channel lock (unchecked).
  51. */
  52. QUIC_NEEDS_LOCK
  53. static int block_until_pred(QUIC_CONNECTION *qc,
  54. int (*pred)(void *arg), void *pred_arg,
  55. uint32_t flags)
  56. {
  57. QUIC_REACTOR *rtor;
  58. assert(qc->ch != NULL);
  59. /*
  60. * Any attempt to block auto-disables tick inhibition as otherwise we will
  61. * hang around forever.
  62. */
  63. ossl_quic_engine_set_inhibit_tick(qc->engine, 0);
  64. rtor = ossl_quic_channel_get_reactor(qc->ch);
  65. return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
  66. qc->mutex);
  67. }
  68. static OSSL_TIME get_time(QUIC_CONNECTION *qc)
  69. {
  70. if (qc->override_now_cb != NULL)
  71. return qc->override_now_cb(qc->override_now_cb_arg);
  72. else
  73. return ossl_time_now();
  74. }
  75. static OSSL_TIME get_time_cb(void *arg)
  76. {
  77. QUIC_CONNECTION *qc = arg;
  78. return get_time(qc);
  79. }
  80. /*
  81. * QCTX is a utility structure which provides information we commonly wish to
  82. * unwrap upon an API call being dispatched to us, namely:
  83. *
  84. * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
  85. * was passed);
  86. * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
  87. * a QCSO with a default stream was passed);
  88. * - whether a QSSO was passed (xso == NULL must not be used to determine this
  89. * because it may be non-NULL when a QCSO is passed if that QCSO has a
  90. * default stream);
  91. * - whether we are in "I/O context", meaning that non-normal errors can
  92. * be reported via SSL_get_error() as well as via ERR. Functions such as
  93. * SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
  94. * functions which are allowed to change the value returned by
  95. * SSL_get_error. However, other functions (including functions which call
  96. * SSL_do_handshake() implicitly) are not allowed to change the return value
  97. * of SSL_get_error.
  98. */
  99. struct qctx_st {
  100. QUIC_CONNECTION *qc;
  101. QUIC_XSO *xso;
  102. int is_stream, in_io;
  103. };
  104. QUIC_NEEDS_LOCK
  105. static void quic_set_last_error(QCTX *ctx, int last_error)
  106. {
  107. if (!ctx->in_io)
  108. return;
  109. if (ctx->is_stream && ctx->xso != NULL)
  110. ctx->xso->last_error = last_error;
  111. else if (!ctx->is_stream && ctx->qc != NULL)
  112. ctx->qc->last_error = last_error;
  113. }
  114. /*
  115. * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
  116. * rather than via ERR. Note that normal errors must always be raised while
  117. * holding a lock.
  118. */
  119. QUIC_NEEDS_LOCK
  120. static int quic_raise_normal_error(QCTX *ctx,
  121. int err)
  122. {
  123. assert(ctx->in_io);
  124. quic_set_last_error(ctx, err);
  125. return 0;
  126. }
  127. /*
  128. * Raise a 'non-normal' error, meaning any error that is not reported via
  129. * SSL_get_error() and must be reported via ERR.
  130. *
  131. * qc should be provided if available. In exceptional circumstances when qc is
  132. * not known NULL may be passed. This should generally only happen when an
  133. * expect_...() function defined below fails, which generally indicates a
  134. * dispatch error or caller error.
  135. *
  136. * ctx should be NULL if the connection lock is not held.
  137. */
  138. static int quic_raise_non_normal_error(QCTX *ctx,
  139. const char *file,
  140. int line,
  141. const char *func,
  142. int reason,
  143. const char *fmt,
  144. ...)
  145. {
  146. va_list args;
  147. if (ctx != NULL) {
  148. quic_set_last_error(ctx, SSL_ERROR_SSL);
  149. if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
  150. ossl_quic_channel_restore_err_state(ctx->qc->ch);
  151. }
  152. ERR_new();
  153. ERR_set_debug(file, line, func);
  154. va_start(args, fmt);
  155. ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
  156. va_end(args);
  157. return 0;
  158. }
  159. #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
  160. quic_raise_normal_error((ctx), (err))
  161. #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
  162. quic_raise_non_normal_error((ctx), \
  163. OPENSSL_FILE, OPENSSL_LINE, \
  164. OPENSSL_FUNC, \
  165. (reason), \
  166. (msg))
  167. /*
  168. * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
  169. * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
  170. *
  171. * After this returns 1, all fields of the passed QCTX are initialised.
  172. * Returns 0 on failure. This function is intended to be used to provide API
  173. * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
  174. */
  175. static int expect_quic(const SSL *s, QCTX *ctx)
  176. {
  177. QUIC_CONNECTION *qc;
  178. QUIC_XSO *xso;
  179. ctx->qc = NULL;
  180. ctx->xso = NULL;
  181. ctx->is_stream = 0;
  182. if (s == NULL)
  183. return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
  184. switch (s->type) {
  185. case SSL_TYPE_QUIC_CONNECTION:
  186. qc = (QUIC_CONNECTION *)s;
  187. ctx->qc = qc;
  188. ctx->xso = qc->default_xso;
  189. ctx->is_stream = 0;
  190. ctx->in_io = 0;
  191. return 1;
  192. case SSL_TYPE_QUIC_XSO:
  193. xso = (QUIC_XSO *)s;
  194. ctx->qc = xso->conn;
  195. ctx->xso = xso;
  196. ctx->is_stream = 1;
  197. ctx->in_io = 0;
  198. return 1;
  199. default:
  200. return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  201. }
  202. }
  203. /*
  204. * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
  205. * other words, requires that the passed QSO be a QSSO or a QCSO with a default
  206. * stream.
  207. *
  208. * remote_init determines if we expect the default XSO to be remotely created or
  209. * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
  210. *
  211. * Channel mutex is acquired and retained on success.
  212. */
  213. QUIC_ACQUIRES_LOCK
  214. static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
  215. int in_io, QCTX *ctx)
  216. {
  217. if (!expect_quic(s, ctx))
  218. return 0;
  219. if (in_io)
  220. quic_lock_for_io(ctx);
  221. else
  222. quic_lock(ctx->qc);
  223. if (ctx->xso == NULL && remote_init >= 0) {
  224. if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) {
  225. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  226. goto err;
  227. }
  228. /* If we haven't finished the handshake, try to advance it. */
  229. if (quic_do_handshake(ctx) < 1)
  230. /* ossl_quic_do_handshake raised error here */
  231. goto err;
  232. if (remote_init == 0) {
  233. if (!qc_try_create_default_xso_for_write(ctx))
  234. goto err;
  235. } else {
  236. if (!qc_wait_for_default_xso_for_read(ctx, /*peek=*/0))
  237. goto err;
  238. }
  239. ctx->xso = ctx->qc->default_xso;
  240. }
  241. if (ctx->xso == NULL) {
  242. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
  243. goto err;
  244. }
  245. return 1; /* coverity[missing_unlock]: lock held */
  246. err:
  247. quic_unlock(ctx->qc);
  248. return 0;
  249. }
  250. /*
  251. * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
  252. * be non-NULL if the QCSO has a default stream.
  253. */
  254. static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
  255. {
  256. if (!expect_quic(s, ctx))
  257. return 0;
  258. if (ctx->is_stream)
  259. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
  260. return 1;
  261. }
  262. /*
  263. * Ensures that the channel mutex is held for a method which touches channel
  264. * state.
  265. *
  266. * Precondition: Channel mutex is not held (unchecked)
  267. */
  268. static void quic_lock(QUIC_CONNECTION *qc)
  269. {
  270. #if defined(OPENSSL_THREADS)
  271. ossl_crypto_mutex_lock(qc->mutex);
  272. #endif
  273. }
  274. static void quic_lock_for_io(QCTX *ctx)
  275. {
  276. quic_lock(ctx->qc);
  277. ctx->in_io = 1;
  278. /*
  279. * We are entering an I/O function so we must update the values returned by
  280. * SSL_get_error and SSL_want. Set no error. This will be overridden later
  281. * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR
  282. * occurs during the API call.
  283. */
  284. quic_set_last_error(ctx, SSL_ERROR_NONE);
  285. }
  286. /* Precondition: Channel mutex is held (unchecked) */
  287. QUIC_NEEDS_LOCK
  288. static void quic_unlock(QUIC_CONNECTION *qc)
  289. {
  290. #if defined(OPENSSL_THREADS)
  291. ossl_crypto_mutex_unlock(qc->mutex);
  292. #endif
  293. }
  294. /*
  295. * This predicate is the criterion which should determine API call rejection for
  296. * *most* mutating API calls, particularly stream-related operations for send
  297. * parts.
  298. *
  299. * A call is rejected (this function returns 0) if shutdown is in progress
  300. * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
  301. * req_active=1, the connection must be active (i.e., the IDLE state is also
  302. * rejected).
  303. */
  304. static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
  305. {
  306. if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
  307. return 0;
  308. if (req_active && !ossl_quic_channel_is_active(qc->ch))
  309. return 0;
  310. return 1;
  311. }
  312. /*
  313. * QUIC Front-End I/O API: Initialization
  314. * ======================================
  315. *
  316. * SSL_new => ossl_quic_new
  317. * ossl_quic_init
  318. * SSL_reset => ossl_quic_reset
  319. * SSL_clear => ossl_quic_clear
  320. * ossl_quic_deinit
  321. * SSL_free => ossl_quic_free
  322. *
  323. * SSL_set_options => ossl_quic_set_options
  324. * SSL_get_options => ossl_quic_get_options
  325. * SSL_clear_options => ossl_quic_clear_options
  326. *
  327. */
  328. /* SSL_new */
  329. SSL *ossl_quic_new(SSL_CTX *ctx)
  330. {
  331. QUIC_CONNECTION *qc = NULL;
  332. SSL *ssl_base = NULL;
  333. SSL_CONNECTION *sc = NULL;
  334. qc = OPENSSL_zalloc(sizeof(*qc));
  335. if (qc == NULL) {
  336. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
  337. return NULL;
  338. }
  339. #if defined(OPENSSL_THREADS)
  340. if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) {
  341. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
  342. goto err;
  343. }
  344. #endif
  345. /* Initialise the QUIC_CONNECTION's stub header. */
  346. ssl_base = &qc->ssl;
  347. if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
  348. ssl_base = NULL;
  349. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  350. goto err;
  351. }
  352. qc->tls = ossl_ssl_connection_new_int(ctx, ssl_base, TLS_method());
  353. if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
  354. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  355. goto err;
  356. }
  357. /* override the user_ssl of the inner connection */
  358. sc->s3.flags |= TLS1_FLAGS_QUIC;
  359. /* Restrict options derived from the SSL_CTX. */
  360. sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
  361. sc->pha_enabled = 0;
  362. #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
  363. qc->is_thread_assisted
  364. = (ssl_base->method == OSSL_QUIC_client_thread_method());
  365. #endif
  366. qc->as_server = 0; /* TODO(QUIC SERVER): add server support */
  367. qc->as_server_state = qc->as_server;
  368. qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
  369. qc->default_ssl_mode = qc->ssl.ctx->mode;
  370. qc->default_ssl_options = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
  371. qc->desires_blocking = 1;
  372. qc->blocking = 0;
  373. qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
  374. qc->last_error = SSL_ERROR_NONE;
  375. if (!create_channel(qc))
  376. goto err;
  377. ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
  378. ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
  379. qc_update_reject_policy(qc);
  380. /*
  381. * We do not create the default XSO yet. The reason for this is that the
  382. * stream ID of the default XSO will depend on whether the stream is client
  383. * or server-initiated, which depends on who transmits first. Since we do
  384. * not know whether the application will be using a client-transmits-first
  385. * or server-transmits-first protocol, we defer default XSO creation until
  386. * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
  387. * we take that as a cue that the client is expecting a server-initiated
  388. * stream, and vice versa if SSL_write() is called first.
  389. */
  390. return ssl_base;
  391. err:
  392. if (ssl_base == NULL) {
  393. #if defined(OPENSSL_THREADS)
  394. ossl_crypto_mutex_free(&qc->mutex);
  395. #endif
  396. OPENSSL_free(qc);
  397. } else {
  398. SSL_free(ssl_base);
  399. }
  400. return NULL;
  401. }
  402. /* SSL_free */
  403. QUIC_TAKES_LOCK
  404. void ossl_quic_free(SSL *s)
  405. {
  406. QCTX ctx;
  407. int is_default;
  408. /* We should never be called on anything but a QSO. */
  409. if (!expect_quic(s, &ctx))
  410. return;
  411. quic_lock(ctx.qc);
  412. if (ctx.is_stream) {
  413. /*
  414. * When a QSSO is freed, the XSO is freed immediately, because the XSO
  415. * itself only contains API personality layer data. However the
  416. * underlying QUIC_STREAM is not freed immediately but is instead marked
  417. * as deleted for later collection.
  418. */
  419. assert(ctx.qc->num_xso > 0);
  420. --ctx.qc->num_xso;
  421. /* If a stream's send part has not been finished, auto-reset it. */
  422. if (( ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
  423. || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
  424. && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
  425. ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
  426. ctx.xso->stream, 0);
  427. /* Do STOP_SENDING for the receive part, if applicable. */
  428. if ( ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
  429. || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
  430. ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
  431. ctx.xso->stream, 0);
  432. /* Update stream state. */
  433. ctx.xso->stream->deleted = 1;
  434. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
  435. ctx.xso->stream);
  436. is_default = (ctx.xso == ctx.qc->default_xso);
  437. quic_unlock(ctx.qc);
  438. /*
  439. * Unref the connection in most cases; the XSO has a ref to the QC and
  440. * not vice versa. But for a default XSO, to avoid circular references,
  441. * the QC refs the XSO but the XSO does not ref the QC. If we are the
  442. * default XSO, we only get here when the QC is being torn down anyway,
  443. * so don't call SSL_free(qc) as we are already in it.
  444. */
  445. if (!is_default)
  446. SSL_free(&ctx.qc->ssl);
  447. /* Note: SSL_free calls OPENSSL_free(xso) for us */
  448. return;
  449. }
  450. /*
  451. * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
  452. * stage, but is freed during the channel free when the whole QSM is freed.
  453. */
  454. if (ctx.qc->default_xso != NULL) {
  455. QUIC_XSO *xso = ctx.qc->default_xso;
  456. quic_unlock(ctx.qc);
  457. SSL_free(&xso->ssl);
  458. quic_lock(ctx.qc);
  459. ctx.qc->default_xso = NULL;
  460. }
  461. /* Ensure we have no remaining XSOs. */
  462. assert(ctx.qc->num_xso == 0);
  463. #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
  464. if (ctx.qc->is_thread_assisted && ctx.qc->started) {
  465. ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
  466. ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
  467. }
  468. #endif
  469. SSL_free(ctx.qc->tls);
  470. ossl_quic_channel_free(ctx.qc->ch);
  471. ossl_quic_port_free(ctx.qc->port);
  472. ossl_quic_engine_free(ctx.qc->engine);
  473. BIO_free_all(ctx.qc->net_rbio);
  474. BIO_free_all(ctx.qc->net_wbio);
  475. quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
  476. #if defined(OPENSSL_THREADS)
  477. ossl_crypto_mutex_free(&ctx.qc->mutex);
  478. #endif
  479. /*
  480. * Note: SSL_free (that called this function) calls OPENSSL_free(ctx.qc) for
  481. * us
  482. */
  483. }
  484. /* SSL method init */
  485. int ossl_quic_init(SSL *s)
  486. {
  487. /* Same op as SSL_clear, forward the call. */
  488. return ossl_quic_clear(s);
  489. }
  490. /* SSL method deinit */
  491. void ossl_quic_deinit(SSL *s)
  492. {
  493. /* No-op. */
  494. }
  495. /* SSL_clear (ssl_reset method) */
  496. int ossl_quic_reset(SSL *s)
  497. {
  498. QCTX ctx;
  499. if (!expect_quic(s, &ctx))
  500. return 0;
  501. ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
  502. return 0;
  503. }
  504. /* ssl_clear method (unused) */
  505. int ossl_quic_clear(SSL *s)
  506. {
  507. QCTX ctx;
  508. if (!expect_quic(s, &ctx))
  509. return 0;
  510. ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
  511. return 0;
  512. }
  513. int ossl_quic_conn_set_override_now_cb(SSL *s,
  514. OSSL_TIME (*now_cb)(void *arg),
  515. void *now_cb_arg)
  516. {
  517. QCTX ctx;
  518. if (!expect_quic(s, &ctx))
  519. return 0;
  520. quic_lock(ctx.qc);
  521. ctx.qc->override_now_cb = now_cb;
  522. ctx.qc->override_now_cb_arg = now_cb_arg;
  523. quic_unlock(ctx.qc);
  524. return 1;
  525. }
  526. void ossl_quic_conn_force_assist_thread_wake(SSL *s)
  527. {
  528. QCTX ctx;
  529. if (!expect_quic(s, &ctx))
  530. return;
  531. #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
  532. if (ctx.qc->is_thread_assisted && ctx.qc->started)
  533. ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
  534. #endif
  535. }
  536. QUIC_NEEDS_LOCK
  537. static void qc_touch_default_xso(QUIC_CONNECTION *qc)
  538. {
  539. qc->default_xso_created = 1;
  540. qc_update_reject_policy(qc);
  541. }
  542. /*
  543. * Changes default XSO. Allows caller to keep reference to the old default XSO
  544. * (if any). Reference to new XSO is transferred from caller.
  545. */
  546. QUIC_NEEDS_LOCK
  547. static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
  548. int touch,
  549. QUIC_XSO **old_xso)
  550. {
  551. int refs;
  552. *old_xso = NULL;
  553. if (qc->default_xso != xso) {
  554. *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
  555. qc->default_xso = xso;
  556. if (xso == NULL) {
  557. /*
  558. * Changing to not having a default XSO. XSO becomes standalone and
  559. * now has a ref to the QC.
  560. */
  561. if (!ossl_assert(SSL_up_ref(&qc->ssl)))
  562. return;
  563. } else {
  564. /*
  565. * Changing from not having a default XSO to having one. The new XSO
  566. * will have had a reference to the QC we need to drop to avoid a
  567. * circular reference.
  568. *
  569. * Currently we never change directly from one default XSO to
  570. * another, though this function would also still be correct if this
  571. * weren't the case.
  572. */
  573. assert(*old_xso == NULL);
  574. CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
  575. assert(refs > 0);
  576. }
  577. }
  578. if (touch)
  579. qc_touch_default_xso(qc);
  580. }
  581. /*
  582. * Changes default XSO, releasing the reference to any previous default XSO.
  583. * Reference to new XSO is transferred from caller.
  584. */
  585. QUIC_NEEDS_LOCK
  586. static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
  587. {
  588. QUIC_XSO *old_xso = NULL;
  589. qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
  590. if (old_xso != NULL)
  591. SSL_free(&old_xso->ssl);
  592. }
  593. QUIC_NEEDS_LOCK
  594. static void xso_update_options(QUIC_XSO *xso)
  595. {
  596. int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
  597. if (xso->stream->rstream != NULL)
  598. ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
  599. if (xso->stream->sstream != NULL)
  600. ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
  601. }
  602. /*
  603. * SSL_set_options
  604. * ---------------
  605. *
  606. * Setting options on a QCSO
  607. * - configures the handshake-layer options;
  608. * - configures the default data-plane options for new streams;
  609. * - configures the data-plane options on the default XSO, if there is one.
  610. *
  611. * Setting options on a QSSO
  612. * - configures data-plane options for that stream only.
  613. */
  614. QUIC_TAKES_LOCK
  615. static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
  616. {
  617. QCTX ctx;
  618. uint64_t hs_mask_value, hs_or_value, ret;
  619. if (!expect_quic(ssl, &ctx))
  620. return 0;
  621. quic_lock(ctx.qc);
  622. if (!ctx.is_stream) {
  623. /*
  624. * If we were called on the connection, we apply any handshake option
  625. * changes.
  626. */
  627. hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
  628. hs_or_value = (or_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
  629. SSL_clear_options(ctx.qc->tls, hs_mask_value);
  630. SSL_set_options(ctx.qc->tls, hs_or_value);
  631. /* Update defaults for new streams. */
  632. ctx.qc->default_ssl_options
  633. = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
  634. & OSSL_QUIC_PERMITTED_OPTIONS;
  635. }
  636. if (ctx.xso != NULL) {
  637. ctx.xso->ssl_options
  638. = ((ctx.xso->ssl_options & ~mask_value) | or_value)
  639. & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
  640. xso_update_options(ctx.xso);
  641. }
  642. ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;
  643. quic_unlock(ctx.qc);
  644. return ret;
  645. }
  646. uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
  647. {
  648. return quic_mask_or_options(ssl, 0, options);
  649. }
  650. /* SSL_clear_options */
  651. uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
  652. {
  653. return quic_mask_or_options(ssl, options, 0);
  654. }
  655. /* SSL_get_options */
  656. uint64_t ossl_quic_get_options(const SSL *ssl)
  657. {
  658. return quic_mask_or_options((SSL *)ssl, 0, 0);
  659. }
  660. /*
  661. * QUIC Front-End I/O API: Network BIO Configuration
  662. * =================================================
  663. *
  664. * Handling the different BIOs is difficult:
  665. *
  666. * - It is more or less a requirement that we use non-blocking network I/O;
  667. * we need to be able to have timeouts on recv() calls, and make best effort
  668. * (non blocking) send() and recv() calls.
  669. *
  670. * The only sensible way to do this is to configure the socket into
  671. * non-blocking mode. We could try to do select() before calling send() or
  672. * recv() to get a guarantee that the call will not block, but this will
  673. * probably run into issues with buggy OSes which generate spurious socket
  674. * readiness events. In any case, relying on this to work reliably does not
  675. * seem sane.
  676. *
  677. * Timeouts could be handled via setsockopt() socket timeout options, but
  678. * this depends on OS support and adds another syscall to every network I/O
  679. * operation. It also has obvious thread safety concerns if we want to move
  680. * to concurrent use of a single socket at some later date.
  681. *
  682. * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
  683. * be made non-blocking. However some OSes (e.g. Windows) do not support
  684. * this, so we cannot rely on this.
  685. *
  686. * As such, we need to configure any FD in non-blocking mode. This may
  687. * confound users who pass a blocking socket to libssl. However, in practice
  688. * it would be extremely strange for a user of QUIC to pass an FD to us,
  689. * then also try and send receive traffic on the same socket(!). Thus the
  690. * impact of this should be limited, and can be documented.
  691. *
  692. * - We support both blocking and non-blocking operation in terms of the API
  693. * presented to the user. One prospect is to set the blocking mode based on
  694. * whether the socket passed to us was already in blocking mode. However,
  695. * Windows has no API for determining if a socket is in blocking mode (!),
  696. * therefore this cannot be done portably. Currently therefore we expose an
  697. * explicit API call to set this, and default to blocking mode.
  698. *
  699. * - We need to determine our initial destination UDP address. The "natural"
  700. * way for a user to do this is to set the peer variable on a BIO_dgram.
  701. * However, this has problems because BIO_dgram's peer variable is used for
  702. * both transmission and reception. This means it can be constantly being
  703. * changed to a malicious value (e.g. if some random unrelated entity on the
  704. * network starts sending traffic to us) on every read call. This is not a
  705. * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
  706. * calls only, which do not use this variable. However, we do need to let
  707. * the user specify the peer in a 'normal' manner. The compromise here is
  708. * that we grab the current peer value set at the time the write BIO is set
  709. * and do not read the value again.
  710. *
  711. * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
  712. * Currently we do this by only supporting non-blocking mode.
  713. *
  714. */
  715. /*
  716. * Determines what initial destination UDP address we should use, if possible.
  717. * If this fails the client must set the destination address manually, or use a
  718. * BIO which does not need a destination address.
  719. */
  720. static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
  721. {
  722. if (BIO_dgram_detect_peer_addr(net_wbio, peer) <= 0)
  723. return 0;
  724. return 1;
  725. }
  726. static int qc_can_support_blocking_cached(QUIC_CONNECTION *qc)
  727. {
  728. QUIC_REACTOR *rtor = ossl_quic_channel_get_reactor(qc->ch);
  729. return ossl_quic_reactor_can_poll_r(rtor)
  730. && ossl_quic_reactor_can_poll_w(rtor);
  731. }
  732. static void qc_update_can_support_blocking(QUIC_CONNECTION *qc)
  733. {
  734. ossl_quic_port_update_poll_descriptors(qc->port); /* best effort */
  735. }
  736. static void qc_update_blocking_mode(QUIC_CONNECTION *qc)
  737. {
  738. qc->blocking = qc->desires_blocking && qc_can_support_blocking_cached(qc);
  739. }
  740. void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
  741. {
  742. QCTX ctx;
  743. if (!expect_quic(s, &ctx))
  744. return;
  745. if (ctx.qc->net_rbio == net_rbio)
  746. return;
  747. if (!ossl_quic_port_set_net_rbio(ctx.qc->port, net_rbio))
  748. return;
  749. BIO_free_all(ctx.qc->net_rbio);
  750. ctx.qc->net_rbio = net_rbio;
  751. if (net_rbio != NULL)
  752. BIO_set_nbio(net_rbio, 1); /* best effort autoconfig */
  753. /*
  754. * Determine if the current pair of read/write BIOs now set allows blocking
  755. * mode to be supported.
  756. */
  757. qc_update_can_support_blocking(ctx.qc);
  758. qc_update_blocking_mode(ctx.qc);
  759. }
  760. void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
  761. {
  762. QCTX ctx;
  763. if (!expect_quic(s, &ctx))
  764. return;
  765. if (ctx.qc->net_wbio == net_wbio)
  766. return;
  767. if (!ossl_quic_port_set_net_wbio(ctx.qc->port, net_wbio))
  768. return;
  769. BIO_free_all(ctx.qc->net_wbio);
  770. ctx.qc->net_wbio = net_wbio;
  771. if (net_wbio != NULL)
  772. BIO_set_nbio(net_wbio, 1); /* best effort autoconfig */
  773. /*
  774. * Determine if the current pair of read/write BIOs now set allows blocking
  775. * mode to be supported.
  776. */
  777. qc_update_can_support_blocking(ctx.qc);
  778. qc_update_blocking_mode(ctx.qc);
  779. }
  780. BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
  781. {
  782. QCTX ctx;
  783. if (!expect_quic(s, &ctx))
  784. return NULL;
  785. return ctx.qc->net_rbio;
  786. }
  787. BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
  788. {
  789. QCTX ctx;
  790. if (!expect_quic(s, &ctx))
  791. return NULL;
  792. return ctx.qc->net_wbio;
  793. }
  794. int ossl_quic_conn_get_blocking_mode(const SSL *s)
  795. {
  796. QCTX ctx;
  797. if (!expect_quic(s, &ctx))
  798. return 0;
  799. if (ctx.is_stream)
  800. return xso_blocking_mode(ctx.xso);
  801. return qc_blocking_mode(ctx.qc);
  802. }
  803. QUIC_TAKES_LOCK
  804. int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
  805. {
  806. int ret = 0;
  807. QCTX ctx;
  808. if (!expect_quic(s, &ctx))
  809. return 0;
  810. quic_lock(ctx.qc);
  811. /* Sanity check - can we support the request given the current network BIO? */
  812. if (blocking) {
  813. /*
  814. * If called directly on a QCSO, update our information on network BIO
  815. * capabilities.
  816. */
  817. if (!ctx.is_stream)
  818. qc_update_can_support_blocking(ctx.qc);
  819. /* Cannot enable blocking mode if we do not have pollable FDs. */
  820. if (!qc_can_support_blocking_cached(ctx.qc)) {
  821. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
  822. goto out;
  823. }
  824. }
  825. if (!ctx.is_stream)
  826. /*
  827. * If called directly on a QCSO, update default and connection-level
  828. * blocking modes.
  829. */
  830. ctx.qc->desires_blocking = (blocking != 0);
  831. if (ctx.xso != NULL) {
  832. /*
  833. * If called on a QSSO or a QCSO with a default XSO, update the blocking
  834. * mode.
  835. */
  836. ctx.xso->desires_blocking = (blocking != 0);
  837. ctx.xso->desires_blocking_set = 1;
  838. }
  839. ret = 1;
  840. out:
  841. qc_update_blocking_mode(ctx.qc);
  842. quic_unlock(ctx.qc);
  843. return ret;
  844. }
  845. int ossl_quic_conn_set_initial_peer_addr(SSL *s,
  846. const BIO_ADDR *peer_addr)
  847. {
  848. QCTX ctx;
  849. if (!expect_quic(s, &ctx))
  850. return 0;
  851. if (ctx.qc->started)
  852. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  853. NULL);
  854. if (peer_addr == NULL) {
  855. BIO_ADDR_clear(&ctx.qc->init_peer_addr);
  856. return 1;
  857. }
  858. ctx.qc->init_peer_addr = *peer_addr;
  859. return 1;
  860. }
  861. /*
  862. * QUIC Front-End I/O API: Asynchronous I/O Management
  863. * ===================================================
  864. *
  865. * (BIO/)SSL_handle_events => ossl_quic_handle_events
  866. * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
  867. * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
  868. *
  869. */
  870. /* Returns 1 if the connection is being used in blocking mode. */
  871. static int qc_blocking_mode(const QUIC_CONNECTION *qc)
  872. {
  873. return qc->blocking;
  874. }
  875. static int xso_blocking_mode(const QUIC_XSO *xso)
  876. {
  877. if (xso->desires_blocking_set)
  878. return xso->desires_blocking && qc_can_support_blocking_cached(xso->conn);
  879. else
  880. /* Only ever set if we can support blocking. */
  881. return xso->conn->blocking;
  882. }
  883. /* SSL_handle_events; performs QUIC I/O and timeout processing. */
  884. QUIC_TAKES_LOCK
  885. int ossl_quic_handle_events(SSL *s)
  886. {
  887. QCTX ctx;
  888. if (!expect_quic(s, &ctx))
  889. return 0;
  890. quic_lock(ctx.qc);
  891. if (ctx.qc->started)
  892. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
  893. quic_unlock(ctx.qc);
  894. return 1;
  895. }
  896. /*
  897. * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
  898. * should next have events handled by the application by calling
  899. * SSL_handle_events(). tv is set to 0 if the object should have events handled
  900. * immediately. If no timeout is currently active, *is_infinite is set to 1 and
  901. * the value of *tv is undefined.
  902. */
  903. QUIC_TAKES_LOCK
  904. int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
  905. {
  906. QCTX ctx;
  907. OSSL_TIME deadline = ossl_time_infinite();
  908. if (!expect_quic(s, &ctx))
  909. return 0;
  910. quic_lock(ctx.qc);
  911. if (ctx.qc->started)
  912. deadline
  913. = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
  914. if (ossl_time_is_infinite(deadline)) {
  915. *is_infinite = 1;
  916. /*
  917. * Robustness against faulty applications that don't check *is_infinite;
  918. * harmless long timeout.
  919. */
  920. tv->tv_sec = 1000000;
  921. tv->tv_usec = 0;
  922. quic_unlock(ctx.qc);
  923. return 1;
  924. }
  925. *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
  926. *is_infinite = 0;
  927. quic_unlock(ctx.qc);
  928. return 1;
  929. }
  930. /* SSL_get_rpoll_descriptor */
  931. int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
  932. {
  933. QCTX ctx;
  934. if (!expect_quic(s, &ctx))
  935. return 0;
  936. if (desc == NULL || ctx.qc->net_rbio == NULL)
  937. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
  938. NULL);
  939. return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
  940. }
  941. /* SSL_get_wpoll_descriptor */
  942. int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
  943. {
  944. QCTX ctx;
  945. if (!expect_quic(s, &ctx))
  946. return 0;
  947. if (desc == NULL || ctx.qc->net_wbio == NULL)
  948. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
  949. NULL);
  950. return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
  951. }
  952. /* SSL_net_read_desired */
  953. QUIC_TAKES_LOCK
  954. int ossl_quic_get_net_read_desired(SSL *s)
  955. {
  956. QCTX ctx;
  957. int ret;
  958. if (!expect_quic(s, &ctx))
  959. return 0;
  960. quic_lock(ctx.qc);
  961. ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
  962. quic_unlock(ctx.qc);
  963. return ret;
  964. }
  965. /* SSL_net_write_desired */
  966. QUIC_TAKES_LOCK
  967. int ossl_quic_get_net_write_desired(SSL *s)
  968. {
  969. int ret;
  970. QCTX ctx;
  971. if (!expect_quic(s, &ctx))
  972. return 0;
  973. quic_lock(ctx.qc);
  974. ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
  975. quic_unlock(ctx.qc);
  976. return ret;
  977. }
  978. /*
  979. * QUIC Front-End I/O API: Connection Lifecycle Operations
  980. * =======================================================
  981. *
  982. * SSL_do_handshake => ossl_quic_do_handshake
  983. * SSL_set_connect_state => ossl_quic_set_connect_state
  984. * SSL_set_accept_state => ossl_quic_set_accept_state
  985. * SSL_shutdown => ossl_quic_shutdown
  986. * SSL_ctrl => ossl_quic_ctrl
  987. * (BIO/)SSL_connect => ossl_quic_connect
  988. * (BIO/)SSL_accept => ossl_quic_accept
  989. *
  990. */
  991. QUIC_NEEDS_LOCK
  992. static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
  993. {
  994. QUIC_STREAM_MAP *qsm;
  995. if (qc->shutting_down)
  996. return;
  997. qsm = ossl_quic_channel_get_qsm(qc->ch);
  998. ossl_quic_stream_map_begin_shutdown_flush(qsm);
  999. qc->shutting_down = 1;
  1000. }
  1001. /* Returns 1 if all shutdown-flush streams have been done with. */
  1002. QUIC_NEEDS_LOCK
  1003. static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
  1004. {
  1005. QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
  1006. return qc->shutting_down
  1007. && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
  1008. }
  1009. /* SSL_shutdown */
  1010. static int quic_shutdown_wait(void *arg)
  1011. {
  1012. QUIC_CONNECTION *qc = arg;
  1013. return ossl_quic_channel_is_terminated(qc->ch);
  1014. }
  1015. /* Returns 1 if shutdown flush process has finished or is inapplicable. */
  1016. static int quic_shutdown_flush_wait(void *arg)
  1017. {
  1018. QUIC_CONNECTION *qc = arg;
  1019. return ossl_quic_channel_is_term_any(qc->ch)
  1020. || qc_shutdown_flush_finished(qc);
  1021. }
  1022. static int quic_shutdown_peer_wait(void *arg)
  1023. {
  1024. QUIC_CONNECTION *qc = arg;
  1025. return ossl_quic_channel_is_term_any(qc->ch);
  1026. }
  1027. QUIC_TAKES_LOCK
  1028. int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
  1029. const SSL_SHUTDOWN_EX_ARGS *args,
  1030. size_t args_len)
  1031. {
  1032. int ret;
  1033. QCTX ctx;
  1034. int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
  1035. int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);
  1036. int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);
  1037. if (!expect_quic(s, &ctx))
  1038. return -1;
  1039. if (ctx.is_stream) {
  1040. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
  1041. return -1;
  1042. }
  1043. quic_lock(ctx.qc);
  1044. if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
  1045. quic_unlock(ctx.qc);
  1046. return 1;
  1047. }
  1048. /* Phase 1: Stream Flushing */
  1049. if (!wait_peer && stream_flush) {
  1050. qc_shutdown_flush_init(ctx.qc);
  1051. if (!qc_shutdown_flush_finished(ctx.qc)) {
  1052. if (!no_block && qc_blocking_mode(ctx.qc)) {
  1053. ret = block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);
  1054. if (ret < 1) {
  1055. ret = 0;
  1056. goto err;
  1057. }
  1058. } else {
  1059. qctx_maybe_autotick(&ctx);
  1060. }
  1061. }
  1062. if (!qc_shutdown_flush_finished(ctx.qc)) {
  1063. quic_unlock(ctx.qc);
  1064. return 0; /* ongoing */
  1065. }
  1066. }
  1067. /* Phase 2: Connection Closure */
  1068. if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) {
  1069. if (!no_block && qc_blocking_mode(ctx.qc)) {
  1070. ret = block_until_pred(ctx.qc, quic_shutdown_peer_wait, ctx.qc, 0);
  1071. if (ret < 1) {
  1072. ret = 0;
  1073. goto err;
  1074. }
  1075. } else {
  1076. qctx_maybe_autotick(&ctx);
  1077. }
  1078. if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) {
  1079. ret = 0; /* peer hasn't closed yet - still not done */
  1080. goto err;
  1081. }
  1082. /*
  1083. * We are at least terminating - go through the normal process of
  1084. * waiting until we are in the TERMINATED state.
  1085. */
  1086. }
  1087. /* Block mutation ops regardless of if we did stream flush. */
  1088. ctx.qc->shutting_down = 1;
  1089. /*
  1090. * This call is a no-op if we are already terminating, so it doesn't
  1091. * affect the wait_peer case.
  1092. */
  1093. ossl_quic_channel_local_close(ctx.qc->ch,
  1094. args != NULL ? args->quic_error_code : 0,
  1095. args != NULL ? args->quic_reason : NULL);
  1096. SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
  1097. if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
  1098. quic_unlock(ctx.qc);
  1099. return 1;
  1100. }
  1101. /* Phase 3: Terminating Wait Time */
  1102. if (!no_block && qc_blocking_mode(ctx.qc)
  1103. && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
  1104. ret = block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
  1105. if (ret < 1) {
  1106. ret = 0;
  1107. goto err;
  1108. }
  1109. } else {
  1110. qctx_maybe_autotick(&ctx);
  1111. }
  1112. ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
  1113. err:
  1114. quic_unlock(ctx.qc);
  1115. return ret;
  1116. }
  1117. /* SSL_ctrl */
  1118. long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
  1119. {
  1120. QCTX ctx;
  1121. if (!expect_quic(s, &ctx))
  1122. return 0;
  1123. switch (cmd) {
  1124. case SSL_CTRL_MODE:
  1125. /* If called on a QCSO, update the default mode. */
  1126. if (!ctx.is_stream)
  1127. ctx.qc->default_ssl_mode |= (uint32_t)larg;
  1128. /*
  1129. * If we were called on a QSSO or have a default stream, we also update
  1130. * that.
  1131. */
  1132. if (ctx.xso != NULL) {
  1133. /* Cannot enable EPW while AON write in progress. */
  1134. if (ctx.xso->aon_write_in_progress)
  1135. larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
  1136. ctx.xso->ssl_mode |= (uint32_t)larg;
  1137. return ctx.xso->ssl_mode;
  1138. }
  1139. return ctx.qc->default_ssl_mode;
  1140. case SSL_CTRL_CLEAR_MODE:
  1141. if (!ctx.is_stream)
  1142. ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
  1143. if (ctx.xso != NULL) {
  1144. ctx.xso->ssl_mode &= ~(uint32_t)larg;
  1145. return ctx.xso->ssl_mode;
  1146. }
  1147. return ctx.qc->default_ssl_mode;
  1148. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  1149. ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
  1150. /* This ctrl also needs to be passed to the internal SSL object */
  1151. return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
  1152. case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
  1153. {
  1154. int is_infinite;
  1155. if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
  1156. return 0;
  1157. return !is_infinite;
  1158. }
  1159. case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
  1160. /* For legacy compatibility with DTLS calls. */
  1161. return ossl_quic_handle_events(s) == 1 ? 1 : -1;
  1162. /* Mask ctrls we shouldn't support for QUIC. */
  1163. case SSL_CTRL_GET_READ_AHEAD:
  1164. case SSL_CTRL_SET_READ_AHEAD:
  1165. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  1166. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  1167. case SSL_CTRL_SET_MAX_PIPELINES:
  1168. return 0;
  1169. default:
  1170. /*
  1171. * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
  1172. * implementation. Either SSL_ctrl will handle it itself by direct
  1173. * access into handshake layer state, or failing that, it will be passed
  1174. * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
  1175. * supported by anything, the handshake layer's ctrl method will finally
  1176. * return 0.
  1177. */
  1178. return ossl_ctrl_internal(&ctx.qc->ssl, cmd, larg, parg, /*no_quic=*/1);
  1179. }
  1180. }
  1181. /* SSL_set_connect_state */
  1182. void ossl_quic_set_connect_state(SSL *s)
  1183. {
  1184. QCTX ctx;
  1185. if (!expect_quic(s, &ctx))
  1186. return;
  1187. /* Cannot be changed after handshake started */
  1188. if (ctx.qc->started || ctx.is_stream)
  1189. return;
  1190. ctx.qc->as_server_state = 0;
  1191. }
  1192. /* SSL_set_accept_state */
  1193. void ossl_quic_set_accept_state(SSL *s)
  1194. {
  1195. QCTX ctx;
  1196. if (!expect_quic(s, &ctx))
  1197. return;
  1198. /* Cannot be changed after handshake started */
  1199. if (ctx.qc->started || ctx.is_stream)
  1200. return;
  1201. ctx.qc->as_server_state = 1;
  1202. }
  1203. /* SSL_do_handshake */
  1204. struct quic_handshake_wait_args {
  1205. QUIC_CONNECTION *qc;
  1206. };
  1207. static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)
  1208. {
  1209. int want = SSL_want(qc->tls);
  1210. if (want == SSL_X509_LOOKUP
  1211. || want == SSL_CLIENT_HELLO_CB
  1212. || want == SSL_RETRY_VERIFY)
  1213. return 1;
  1214. return 0;
  1215. }
  1216. static int quic_handshake_wait(void *arg)
  1217. {
  1218. struct quic_handshake_wait_args *args = arg;
  1219. if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
  1220. return -1;
  1221. if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
  1222. return 1;
  1223. if (tls_wants_non_io_retry(args->qc))
  1224. return 1;
  1225. return 0;
  1226. }
  1227. static int configure_channel(QUIC_CONNECTION *qc)
  1228. {
  1229. assert(qc->ch != NULL);
  1230. if (!ossl_quic_port_set_net_rbio(qc->port, qc->net_rbio)
  1231. || !ossl_quic_port_set_net_wbio(qc->port, qc->net_wbio)
  1232. || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
  1233. return 0;
  1234. return 1;
  1235. }
  1236. QUIC_NEEDS_LOCK
  1237. static int create_channel(QUIC_CONNECTION *qc)
  1238. {
  1239. QUIC_ENGINE_ARGS engine_args = {0};
  1240. QUIC_PORT_ARGS port_args = {0};
  1241. engine_args.libctx = qc->ssl.ctx->libctx;
  1242. engine_args.propq = qc->ssl.ctx->propq;
  1243. engine_args.mutex = qc->mutex;
  1244. engine_args.now_cb = get_time_cb;
  1245. engine_args.now_cb_arg = qc;
  1246. qc->engine = ossl_quic_engine_new(&engine_args);
  1247. if (qc->engine == NULL) {
  1248. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  1249. return 0;
  1250. }
  1251. port_args.channel_ctx = qc->ssl.ctx;
  1252. qc->port = ossl_quic_engine_create_port(qc->engine, &port_args);
  1253. if (qc->port == NULL) {
  1254. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  1255. ossl_quic_engine_free(qc->engine);
  1256. return 0;
  1257. }
  1258. qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
  1259. if (qc->ch == NULL) {
  1260. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  1261. ossl_quic_port_free(qc->port);
  1262. ossl_quic_engine_free(qc->engine);
  1263. return 0;
  1264. }
  1265. return 1;
  1266. }
  1267. /*
  1268. * Configures a channel with the information we have accumulated via calls made
  1269. * to us from the application prior to starting a handshake attempt.
  1270. */
  1271. QUIC_NEEDS_LOCK
  1272. static int ensure_channel_started(QCTX *ctx)
  1273. {
  1274. QUIC_CONNECTION *qc = ctx->qc;
  1275. if (!qc->started) {
  1276. if (!configure_channel(qc)) {
  1277. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
  1278. "failed to configure channel");
  1279. return 0;
  1280. }
  1281. if (!ossl_quic_channel_start(qc->ch)) {
  1282. ossl_quic_channel_restore_err_state(qc->ch);
  1283. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
  1284. "failed to start channel");
  1285. return 0;
  1286. }
  1287. #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
  1288. if (qc->is_thread_assisted)
  1289. if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch,
  1290. qc->override_now_cb,
  1291. qc->override_now_cb_arg)) {
  1292. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
  1293. "failed to start assist thread");
  1294. return 0;
  1295. }
  1296. #endif
  1297. }
  1298. qc->started = 1;
  1299. return 1;
  1300. }
  1301. QUIC_NEEDS_LOCK
  1302. static int quic_do_handshake(QCTX *ctx)
  1303. {
  1304. int ret;
  1305. QUIC_CONNECTION *qc = ctx->qc;
  1306. if (ossl_quic_channel_is_handshake_complete(qc->ch))
  1307. /* Handshake already completed. */
  1308. return 1;
  1309. if (!quic_mutation_allowed(qc, /*req_active=*/0))
  1310. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1311. if (qc->as_server != qc->as_server_state) {
  1312. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
  1313. return -1; /* Non-protocol error */
  1314. }
  1315. if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
  1316. /* Need read and write BIOs. */
  1317. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
  1318. return -1; /* Non-protocol error */
  1319. }
  1320. /*
  1321. * We need to determine our addressing mode. There are basically two
  1322. * ways we can use L4 addresses:
  1323. *
  1324. * - Addressed mode, in which our BIO_sendmmsg calls have destination
  1325. * addresses attached to them which we expect the underlying network BIO
  1326. * to handle;
  1327. *
  1328. * - Unaddressed mode, in which the BIO provided to us on the
  1329. * network side neither provides us with L4 addresses nor is capable of
  1330. * honouring ones we provide. We don't know where the QUIC traffic we
  1331. * send ends up exactly and trust the application to know what it is
  1332. * doing.
  1333. *
  1334. * Addressed mode is preferred because it enables support for connection
  1335. * migration, multipath, etc. in the future. Addressed mode is automatically
  1336. * enabled if we are using e.g. BIO_s_datagram, with or without
  1337. * BIO_s_connect.
  1338. *
  1339. * If we are passed a BIO_s_dgram_pair (or some custom BIO) we may have to
  1340. * use unaddressed mode unless that BIO supports capability flags indicating
  1341. * it can provide and honour L4 addresses.
  1342. *
  1343. * Our strategy for determining address mode is simple: we probe the
  1344. * underlying network BIOs for their capabilities. If the network BIOs
  1345. * support what we need, we use addressed mode. Otherwise, we use
  1346. * unaddressed mode.
  1347. *
  1348. * If addressed mode is chosen, we require an initial peer address to be
  1349. * set. If this is not set, we fail. If unaddressed mode is used, we do not
  1350. * require this, as such an address is superfluous, though it can be set if
  1351. * desired.
  1352. */
  1353. if (!qc->started && !qc->addressing_probe_done) {
  1354. long rcaps = BIO_dgram_get_effective_caps(qc->net_rbio);
  1355. long wcaps = BIO_dgram_get_effective_caps(qc->net_wbio);
  1356. qc->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0);
  1357. qc->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0);
  1358. qc->addressing_probe_done = 1;
  1359. }
  1360. if (!qc->started && qc->addressed_mode_w
  1361. && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
  1362. /*
  1363. * We are trying to connect and are using addressed mode, which means we
  1364. * need an initial peer address; if we do not have a peer address yet,
  1365. * we should try to autodetect one.
  1366. *
  1367. * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
  1368. * may not be able to provide us with a peer address until they have
  1369. * finished their own processing. They may not be able to perform this
  1370. * processing until an application has finished configuring that BIO
  1371. * (e.g. with setter calls), which might happen after SSL_set_bio is
  1372. * called.
  1373. */
  1374. if (!csm_analyse_init_peer_addr(qc->net_wbio, &qc->init_peer_addr))
  1375. /* best effort */
  1376. BIO_ADDR_clear(&qc->init_peer_addr);
  1377. else
  1378. ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
  1379. }
  1380. if (!qc->started
  1381. && qc->addressed_mode_w
  1382. && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
  1383. /*
  1384. * If we still don't have a peer address in addressed mode, we can't do
  1385. * anything.
  1386. */
  1387. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
  1388. return -1; /* Non-protocol error */
  1389. }
  1390. /*
  1391. * Start connection process. Note we may come here multiple times in
  1392. * non-blocking mode, which is fine.
  1393. */
  1394. if (!ensure_channel_started(ctx)) /* raises on failure */
  1395. return -1; /* Non-protocol error */
  1396. if (ossl_quic_channel_is_handshake_complete(qc->ch))
  1397. /* The handshake is now done. */
  1398. return 1;
  1399. if (!qc_blocking_mode(qc)) {
  1400. /* Try to advance the reactor. */
  1401. qctx_maybe_autotick(ctx);
  1402. if (ossl_quic_channel_is_handshake_complete(qc->ch))
  1403. /* The handshake is now done. */
  1404. return 1;
  1405. if (ossl_quic_channel_is_term_any(qc->ch)) {
  1406. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1407. return 0;
  1408. } else if (qc->desires_blocking) {
  1409. /*
  1410. * As a special case when doing a handshake when blocking mode is
  1411. * desired yet not available, see if the network BIOs have become
  1412. * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
  1413. * which do late creation of socket FDs and therefore cannot expose
  1414. * a poll descriptor until after a network BIO is set on the QCSO.
  1415. */
  1416. assert(!qc->blocking);
  1417. qc_update_can_support_blocking(qc);
  1418. qc_update_blocking_mode(qc);
  1419. }
  1420. }
  1421. /*
  1422. * We are either in blocking mode or just entered it due to the code above.
  1423. */
  1424. if (qc_blocking_mode(qc)) {
  1425. /* In blocking mode, wait for the handshake to complete. */
  1426. struct quic_handshake_wait_args args;
  1427. args.qc = qc;
  1428. ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
  1429. if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
  1430. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1431. return 0; /* Shutdown before completion */
  1432. } else if (ret <= 0) {
  1433. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1434. return -1; /* Non-protocol error */
  1435. }
  1436. if (tls_wants_non_io_retry(qc)) {
  1437. QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
  1438. return -1;
  1439. }
  1440. assert(ossl_quic_channel_is_handshake_complete(qc->ch));
  1441. return 1;
  1442. }
  1443. if (tls_wants_non_io_retry(qc)) {
  1444. QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
  1445. return -1;
  1446. }
  1447. /*
  1448. * Otherwise, indicate that the handshake isn't done yet.
  1449. * We can only get here in non-blocking mode.
  1450. */
  1451. QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
  1452. return -1; /* Non-protocol error */
  1453. }
  1454. QUIC_TAKES_LOCK
  1455. int ossl_quic_do_handshake(SSL *s)
  1456. {
  1457. int ret;
  1458. QCTX ctx;
  1459. if (!expect_quic(s, &ctx))
  1460. return 0;
  1461. quic_lock_for_io(&ctx);
  1462. ret = quic_do_handshake(&ctx);
  1463. quic_unlock(ctx.qc);
  1464. return ret;
  1465. }
  1466. /* SSL_connect */
  1467. int ossl_quic_connect(SSL *s)
  1468. {
  1469. /* Ensure we are in connect state (no-op if non-idle). */
  1470. ossl_quic_set_connect_state(s);
  1471. /* Begin or continue the handshake */
  1472. return ossl_quic_do_handshake(s);
  1473. }
  1474. /* SSL_accept */
  1475. int ossl_quic_accept(SSL *s)
  1476. {
  1477. /* Ensure we are in accept state (no-op if non-idle). */
  1478. ossl_quic_set_accept_state(s);
  1479. /* Begin or continue the handshake */
  1480. return ossl_quic_do_handshake(s);
  1481. }
  1482. /*
  1483. * QUIC Front-End I/O API: Stream Lifecycle Operations
  1484. * ===================================================
  1485. *
  1486. * SSL_stream_new => ossl_quic_conn_stream_new
  1487. *
  1488. */
  1489. /*
  1490. * Try to create the default XSO if it doesn't already exist. Returns 1 if the
  1491. * default XSO was created. Returns 0 if it was not (e.g. because it already
  1492. * exists). Note that this is NOT an error condition.
  1493. */
  1494. QUIC_NEEDS_LOCK
  1495. static int qc_try_create_default_xso_for_write(QCTX *ctx)
  1496. {
  1497. uint64_t flags = 0;
  1498. QUIC_CONNECTION *qc = ctx->qc;
  1499. if (qc->default_xso_created
  1500. || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
  1501. /*
  1502. * We only do this once. If the user detaches a previously created
  1503. * default XSO we don't auto-create another one.
  1504. */
  1505. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
  1506. /* Create a locally-initiated stream. */
  1507. if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
  1508. flags |= SSL_STREAM_FLAG_UNI;
  1509. qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
  1510. /*needs_lock=*/0),
  1511. /*touch=*/0);
  1512. if (qc->default_xso == NULL)
  1513. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1514. qc_touch_default_xso(qc);
  1515. return 1;
  1516. }
  1517. struct quic_wait_for_stream_args {
  1518. QUIC_CONNECTION *qc;
  1519. QUIC_STREAM *qs;
  1520. QCTX *ctx;
  1521. uint64_t expect_id;
  1522. };
  1523. QUIC_NEEDS_LOCK
  1524. static int quic_wait_for_stream(void *arg)
  1525. {
  1526. struct quic_wait_for_stream_args *args = arg;
  1527. if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
  1528. /* If connection is torn down due to an error while blocking, stop. */
  1529. QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1530. return -1;
  1531. }
  1532. args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
  1533. args->expect_id | QUIC_STREAM_DIR_BIDI);
  1534. if (args->qs == NULL)
  1535. args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
  1536. args->expect_id | QUIC_STREAM_DIR_UNI);
  1537. if (args->qs != NULL)
  1538. return 1; /* stream now exists */
  1539. return 0; /* did not get a stream, keep trying */
  1540. }
  1541. QUIC_NEEDS_LOCK
  1542. static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek)
  1543. {
  1544. /* Called on a QCSO and we don't currently have a default stream. */
  1545. uint64_t expect_id;
  1546. QUIC_CONNECTION *qc = ctx->qc;
  1547. QUIC_STREAM *qs;
  1548. int res;
  1549. struct quic_wait_for_stream_args wargs;
  1550. OSSL_RTT_INFO rtt_info;
  1551. /*
  1552. * If default stream functionality is disabled or we already detached
  1553. * one, don't make another default stream and just fail.
  1554. */
  1555. if (qc->default_xso_created
  1556. || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
  1557. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
  1558. /*
  1559. * The peer may have opened a stream since we last ticked. So tick and
  1560. * see if the stream with ordinal 0 (remote, bidi/uni based on stream
  1561. * mode) exists yet. QUIC stream IDs must be allocated in order, so the
  1562. * first stream created by a peer must have an ordinal of 0.
  1563. */
  1564. expect_id = qc->as_server
  1565. ? QUIC_STREAM_INITIATOR_CLIENT
  1566. : QUIC_STREAM_INITIATOR_SERVER;
  1567. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
  1568. expect_id | QUIC_STREAM_DIR_BIDI);
  1569. if (qs == NULL)
  1570. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
  1571. expect_id | QUIC_STREAM_DIR_UNI);
  1572. if (qs == NULL) {
  1573. qctx_maybe_autotick(ctx);
  1574. qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
  1575. expect_id);
  1576. }
  1577. if (qs == NULL) {
  1578. if (peek)
  1579. return 0;
  1580. if (!qc_blocking_mode(qc))
  1581. /* Non-blocking mode, so just bail immediately. */
  1582. return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
  1583. /* Block until we have a stream. */
  1584. wargs.qc = qc;
  1585. wargs.qs = NULL;
  1586. wargs.ctx = ctx;
  1587. wargs.expect_id = expect_id;
  1588. res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
  1589. if (res == 0)
  1590. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1591. else if (res < 0 || wargs.qs == NULL)
  1592. /* quic_wait_for_stream raised error here */
  1593. return 0;
  1594. qs = wargs.qs;
  1595. }
  1596. /*
  1597. * We now have qs != NULL. Remove it from the incoming stream queue so that
  1598. * it isn't also returned by any future SSL_accept_stream calls.
  1599. */
  1600. ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
  1601. ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc->ch),
  1602. qs, rtt_info.smoothed_rtt);
  1603. /*
  1604. * Now make qs the default stream, creating the necessary XSO.
  1605. */
  1606. qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
  1607. if (qc->default_xso == NULL)
  1608. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1609. qc_touch_default_xso(qc); /* inhibits default XSO */
  1610. return 1;
  1611. }
  1612. QUIC_NEEDS_LOCK
  1613. static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
  1614. {
  1615. QUIC_XSO *xso = NULL;
  1616. if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
  1617. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
  1618. goto err;
  1619. }
  1620. if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) {
  1621. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
  1622. goto err;
  1623. }
  1624. /* XSO refs QC */
  1625. if (!SSL_up_ref(&qc->ssl)) {
  1626. QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);
  1627. goto err;
  1628. }
  1629. xso->conn = qc;
  1630. xso->ssl_mode = qc->default_ssl_mode;
  1631. xso->ssl_options
  1632. = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
  1633. xso->last_error = SSL_ERROR_NONE;
  1634. xso->stream = qs;
  1635. ++qc->num_xso;
  1636. xso_update_options(xso);
  1637. return xso;
  1638. err:
  1639. OPENSSL_free(xso);
  1640. return NULL;
  1641. }
  1642. struct quic_new_stream_wait_args {
  1643. QUIC_CONNECTION *qc;
  1644. int is_uni;
  1645. };
  1646. static int quic_new_stream_wait(void *arg)
  1647. {
  1648. struct quic_new_stream_wait_args *args = arg;
  1649. QUIC_CONNECTION *qc = args->qc;
  1650. if (!quic_mutation_allowed(qc, /*req_active=*/1))
  1651. return -1;
  1652. if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
  1653. return 1;
  1654. return 0;
  1655. }
  1656. /* locking depends on need_lock */
  1657. static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
  1658. {
  1659. int ret;
  1660. QUIC_CONNECTION *qc = ctx->qc;
  1661. QUIC_XSO *xso = NULL;
  1662. QUIC_STREAM *qs = NULL;
  1663. int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
  1664. int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
  1665. int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
  1666. if (need_lock)
  1667. quic_lock(qc);
  1668. if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
  1669. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1670. goto err;
  1671. }
  1672. if (!advance
  1673. && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
  1674. struct quic_new_stream_wait_args args;
  1675. /*
  1676. * Stream count flow control currently doesn't permit this stream to be
  1677. * opened.
  1678. */
  1679. if (no_blocking || !qc_blocking_mode(qc)) {
  1680. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
  1681. goto err;
  1682. }
  1683. args.qc = qc;
  1684. args.is_uni = is_uni;
  1685. /* Blocking mode - wait until we can get a stream. */
  1686. ret = block_until_pred(ctx->qc, quic_new_stream_wait, &args, 0);
  1687. if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
  1688. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1689. goto err; /* Shutdown before completion */
  1690. } else if (ret <= 0) {
  1691. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1692. goto err; /* Non-protocol error */
  1693. }
  1694. }
  1695. qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
  1696. if (qs == NULL) {
  1697. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1698. goto err;
  1699. }
  1700. xso = create_xso_from_stream(qc, qs);
  1701. if (xso == NULL)
  1702. goto err;
  1703. qc_touch_default_xso(qc); /* inhibits default XSO */
  1704. if (need_lock)
  1705. quic_unlock(qc);
  1706. return &xso->ssl;
  1707. err:
  1708. OPENSSL_free(xso);
  1709. ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
  1710. if (need_lock)
  1711. quic_unlock(qc);
  1712. return NULL;
  1713. }
  1714. QUIC_TAKES_LOCK
  1715. SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
  1716. {
  1717. QCTX ctx;
  1718. if (!expect_quic_conn_only(s, &ctx))
  1719. return NULL;
  1720. return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
  1721. }
  1722. /*
  1723. * QUIC Front-End I/O API: Steady-State Operations
  1724. * ===============================================
  1725. *
  1726. * Here we dispatch calls to the steady-state front-end I/O API functions; that
  1727. * is, the functions used during the established phase of a QUIC connection
  1728. * (e.g. SSL_read, SSL_write).
  1729. *
  1730. * Each function must handle both blocking and non-blocking modes. As discussed
  1731. * above, all QUIC I/O is implemented using non-blocking mode internally.
  1732. *
  1733. * SSL_get_error => partially implemented by ossl_quic_get_error
  1734. * SSL_want => ossl_quic_want
  1735. * (BIO/)SSL_read => ossl_quic_read
  1736. * (BIO/)SSL_write => ossl_quic_write
  1737. * SSL_pending => ossl_quic_pending
  1738. * SSL_stream_conclude => ossl_quic_conn_stream_conclude
  1739. * SSL_key_update => ossl_quic_key_update
  1740. */
  1741. /* SSL_get_error */
  1742. int ossl_quic_get_error(const SSL *s, int i)
  1743. {
  1744. QCTX ctx;
  1745. int net_error, last_error;
  1746. if (!expect_quic(s, &ctx))
  1747. return 0;
  1748. quic_lock(ctx.qc);
  1749. net_error = ossl_quic_channel_net_error(ctx.qc->ch);
  1750. last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
  1751. quic_unlock(ctx.qc);
  1752. if (net_error)
  1753. return SSL_ERROR_SYSCALL;
  1754. return last_error;
  1755. }
  1756. /* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
  1757. static int error_to_want(int error)
  1758. {
  1759. switch (error) {
  1760. case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */
  1761. case SSL_ERROR_WANT_ACCEPT: /* never used - UDP is connectionless */
  1762. case SSL_ERROR_ZERO_RETURN:
  1763. default:
  1764. return SSL_NOTHING;
  1765. case SSL_ERROR_WANT_READ:
  1766. return SSL_READING;
  1767. case SSL_ERROR_WANT_WRITE:
  1768. return SSL_WRITING;
  1769. case SSL_ERROR_WANT_RETRY_VERIFY:
  1770. return SSL_RETRY_VERIFY;
  1771. case SSL_ERROR_WANT_CLIENT_HELLO_CB:
  1772. return SSL_CLIENT_HELLO_CB;
  1773. case SSL_ERROR_WANT_X509_LOOKUP:
  1774. return SSL_X509_LOOKUP;
  1775. }
  1776. }
  1777. /* SSL_want */
  1778. int ossl_quic_want(const SSL *s)
  1779. {
  1780. QCTX ctx;
  1781. int w;
  1782. if (!expect_quic(s, &ctx))
  1783. return SSL_NOTHING;
  1784. quic_lock(ctx.qc);
  1785. w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);
  1786. quic_unlock(ctx.qc);
  1787. return w;
  1788. }
  1789. /*
  1790. * SSL_write
  1791. * ---------
  1792. *
  1793. * The set of functions below provide the implementation of the public SSL_write
  1794. * function. We must handle:
  1795. *
  1796. * - both blocking and non-blocking operation at the application level,
  1797. * depending on how we are configured;
  1798. *
  1799. * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
  1800. *
  1801. * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
  1802. *
  1803. */
  1804. QUIC_NEEDS_LOCK
  1805. static void quic_post_write(QUIC_XSO *xso, int did_append,
  1806. int did_append_all, uint64_t flags,
  1807. int do_tick)
  1808. {
  1809. /*
  1810. * We have appended at least one byte to the stream.
  1811. * Potentially mark stream as active, depending on FC.
  1812. */
  1813. if (did_append)
  1814. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
  1815. xso->stream);
  1816. if (did_append_all && (flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
  1817. ossl_quic_sstream_fin(xso->stream->sstream);
  1818. /*
  1819. * Try and send.
  1820. *
  1821. * TODO(QUIC FUTURE): It is probably inefficient to try and do this
  1822. * immediately, plus we should eventually consider Nagle's algorithm.
  1823. */
  1824. if (do_tick)
  1825. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
  1826. }
  1827. struct quic_write_again_args {
  1828. QUIC_XSO *xso;
  1829. const unsigned char *buf;
  1830. size_t len;
  1831. size_t total_written;
  1832. int err;
  1833. uint64_t flags;
  1834. };
  1835. /*
  1836. * Absolute maximum write buffer size, enforced to prevent a rogue peer from
  1837. * deliberately inducing DoS. This has been chosen based on the optimal buffer
  1838. * size for an RTT of 500ms and a bandwidth of 100 Mb/s.
  1839. */
  1840. #define MAX_WRITE_BUF_SIZE (6 * 1024 * 1024)
  1841. /*
  1842. * Ensure spare buffer space available (up until a limit, at least).
  1843. */
  1844. QUIC_NEEDS_LOCK
  1845. static int sstream_ensure_spare(QUIC_SSTREAM *sstream, uint64_t spare)
  1846. {
  1847. size_t cur_sz = ossl_quic_sstream_get_buffer_size(sstream);
  1848. size_t avail = ossl_quic_sstream_get_buffer_avail(sstream);
  1849. size_t spare_ = (spare > SIZE_MAX) ? SIZE_MAX : (size_t)spare;
  1850. size_t new_sz, growth;
  1851. if (spare_ <= avail || cur_sz == MAX_WRITE_BUF_SIZE)
  1852. return 1;
  1853. growth = spare_ - avail;
  1854. if (cur_sz + growth > MAX_WRITE_BUF_SIZE)
  1855. new_sz = MAX_WRITE_BUF_SIZE;
  1856. else
  1857. new_sz = cur_sz + growth;
  1858. return ossl_quic_sstream_set_buffer_size(sstream, new_sz);
  1859. }
  1860. /*
  1861. * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded
  1862. * as needed according to flow control.
  1863. */
  1864. QUIC_NEEDS_LOCK
  1865. static int xso_sstream_append(QUIC_XSO *xso, const unsigned char *buf,
  1866. size_t len, size_t *actual_written)
  1867. {
  1868. QUIC_SSTREAM *sstream = xso->stream->sstream;
  1869. uint64_t cur = ossl_quic_sstream_get_cur_size(sstream);
  1870. uint64_t cwm = ossl_quic_txfc_get_cwm(&xso->stream->txfc);
  1871. uint64_t permitted = (cwm >= cur ? cwm - cur : 0);
  1872. if (len > permitted)
  1873. len = (size_t)permitted;
  1874. if (!sstream_ensure_spare(sstream, len))
  1875. return 0;
  1876. return ossl_quic_sstream_append(sstream, buf, len, actual_written);
  1877. }
  1878. QUIC_NEEDS_LOCK
  1879. static int quic_write_again(void *arg)
  1880. {
  1881. struct quic_write_again_args *args = arg;
  1882. size_t actual_written = 0;
  1883. if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
  1884. /* If connection is torn down due to an error while blocking, stop. */
  1885. return -2;
  1886. if (!quic_validate_for_write(args->xso, &args->err))
  1887. /*
  1888. * Stream may have become invalid for write due to connection events
  1889. * while we blocked.
  1890. */
  1891. return -2;
  1892. args->err = ERR_R_INTERNAL_ERROR;
  1893. if (!xso_sstream_append(args->xso, args->buf, args->len, &actual_written))
  1894. return -2;
  1895. quic_post_write(args->xso, actual_written > 0,
  1896. args->len == actual_written, args->flags, 0);
  1897. args->buf += actual_written;
  1898. args->len -= actual_written;
  1899. args->total_written += actual_written;
  1900. if (args->len == 0)
  1901. /* Written everything, done. */
  1902. return 1;
  1903. /* Not written everything yet, keep trying. */
  1904. return 0;
  1905. }
  1906. QUIC_NEEDS_LOCK
  1907. static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
  1908. uint64_t flags, size_t *written)
  1909. {
  1910. int res;
  1911. QUIC_XSO *xso = ctx->xso;
  1912. struct quic_write_again_args args;
  1913. size_t actual_written = 0;
  1914. /* First make a best effort to append as much of the data as possible. */
  1915. if (!xso_sstream_append(xso, buf, len, &actual_written)) {
  1916. /* Stream already finished or allocation error. */
  1917. *written = 0;
  1918. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  1919. }
  1920. quic_post_write(xso, actual_written > 0, actual_written == len, flags, 1);
  1921. /*
  1922. * Record however much data we wrote
  1923. */
  1924. *written = actual_written;
  1925. if (actual_written == len) {
  1926. /* Managed to append everything on the first try. */
  1927. return 1;
  1928. }
  1929. /*
  1930. * We did not manage to append all of the data immediately, so the stream
  1931. * buffer has probably filled up. This means we need to block until some of
  1932. * it is freed up.
  1933. */
  1934. args.xso = xso;
  1935. args.buf = (const unsigned char *)buf + actual_written;
  1936. args.len = len - actual_written;
  1937. args.total_written = 0;
  1938. args.err = ERR_R_INTERNAL_ERROR;
  1939. args.flags = flags;
  1940. res = block_until_pred(xso->conn, quic_write_again, &args, 0);
  1941. if (res <= 0) {
  1942. if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
  1943. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  1944. else
  1945. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
  1946. }
  1947. /*
  1948. * When waiting on extra buffer space to be available, args.total_written
  1949. * holds the amount of remaining data we requested to write, which will be
  1950. * something less than the len parameter passed in, however much we wrote
  1951. * here, add it to the value that we wrote when we initially called
  1952. * xso_sstream_append
  1953. */
  1954. *written += args.total_written;
  1955. return 1;
  1956. }
  1957. /*
  1958. * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
  1959. * write semantics.
  1960. */
  1961. static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
  1962. size_t buf_len, size_t already_sent)
  1963. {
  1964. assert(!xso->aon_write_in_progress);
  1965. xso->aon_write_in_progress = 1;
  1966. xso->aon_buf_base = buf;
  1967. xso->aon_buf_pos = already_sent;
  1968. xso->aon_buf_len = buf_len;
  1969. }
  1970. static void aon_write_finish(QUIC_XSO *xso)
  1971. {
  1972. xso->aon_write_in_progress = 0;
  1973. xso->aon_buf_base = NULL;
  1974. xso->aon_buf_pos = 0;
  1975. xso->aon_buf_len = 0;
  1976. }
  1977. QUIC_NEEDS_LOCK
  1978. static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
  1979. size_t len, uint64_t flags,
  1980. size_t *written)
  1981. {
  1982. QUIC_XSO *xso = ctx->xso;
  1983. const void *actual_buf;
  1984. size_t actual_len, actual_written = 0;
  1985. int accept_moving_buffer
  1986. = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
  1987. if (xso->aon_write_in_progress) {
  1988. /*
  1989. * We are in the middle of an AON write (i.e., a previous write did not
  1990. * manage to append all data to the SSTREAM and we have Enable Partial
  1991. * Write (EPW) mode disabled.)
  1992. */
  1993. if ((!accept_moving_buffer && xso->aon_buf_base != buf)
  1994. || len != xso->aon_buf_len)
  1995. /*
  1996. * Pointer must not have changed if we are not in accept moving
  1997. * buffer mode. Length must never change.
  1998. */
  1999. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
  2000. actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
  2001. actual_len = len - xso->aon_buf_pos;
  2002. assert(actual_len > 0);
  2003. } else {
  2004. actual_buf = buf;
  2005. actual_len = len;
  2006. }
  2007. /* First make a best effort to append as much of the data as possible. */
  2008. if (!xso_sstream_append(xso, actual_buf, actual_len, &actual_written)) {
  2009. /* Stream already finished or allocation error. */
  2010. *written = 0;
  2011. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  2012. }
  2013. quic_post_write(xso, actual_written > 0, actual_written == actual_len,
  2014. flags, qctx_should_autotick(ctx));
  2015. if (actual_written == actual_len) {
  2016. /* We have sent everything. */
  2017. if (xso->aon_write_in_progress) {
  2018. /*
  2019. * We have sent everything, and we were in the middle of an AON
  2020. * write. The output write length is the total length of the AON
  2021. * buffer, not however many bytes we managed to write to the stream
  2022. * in this call.
  2023. */
  2024. *written = xso->aon_buf_len;
  2025. aon_write_finish(xso);
  2026. } else {
  2027. *written = actual_written;
  2028. }
  2029. return 1;
  2030. }
  2031. if (xso->aon_write_in_progress) {
  2032. /*
  2033. * AON write is in progress but we have not written everything yet. We
  2034. * may have managed to send zero bytes, or some number of bytes less
  2035. * than the total remaining which need to be appended during this
  2036. * AON operation.
  2037. */
  2038. xso->aon_buf_pos += actual_written;
  2039. assert(xso->aon_buf_pos < xso->aon_buf_len);
  2040. return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
  2041. }
  2042. /*
  2043. * Not in an existing AON operation but partial write is not enabled, so we
  2044. * need to begin a new AON operation. However we needn't bother if we didn't
  2045. * actually append anything.
  2046. */
  2047. if (actual_written > 0)
  2048. aon_write_begin(xso, buf, len, actual_written);
  2049. /*
  2050. * AON - We do not publicly admit to having appended anything until AON
  2051. * completes.
  2052. */
  2053. *written = 0;
  2054. return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
  2055. }
  2056. QUIC_NEEDS_LOCK
  2057. static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
  2058. uint64_t flags, size_t *written)
  2059. {
  2060. QUIC_XSO *xso = ctx->xso;
  2061. /* Simple best effort operation. */
  2062. if (!xso_sstream_append(xso, buf, len, written)) {
  2063. /* Stream already finished or allocation error. */
  2064. *written = 0;
  2065. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  2066. }
  2067. quic_post_write(xso, *written > 0, *written == len, flags,
  2068. qctx_should_autotick(ctx));
  2069. if (*written == 0)
  2070. /* SSL_write_ex returns 0 if it didn't read anything. */
  2071. return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
  2072. return 1;
  2073. }
  2074. QUIC_NEEDS_LOCK
  2075. static int quic_validate_for_write(QUIC_XSO *xso, int *err)
  2076. {
  2077. QUIC_STREAM_MAP *qsm;
  2078. if (xso == NULL || xso->stream == NULL) {
  2079. *err = ERR_R_INTERNAL_ERROR;
  2080. return 0;
  2081. }
  2082. switch (xso->stream->send_state) {
  2083. default:
  2084. case QUIC_SSTREAM_STATE_NONE:
  2085. *err = SSL_R_STREAM_RECV_ONLY;
  2086. return 0;
  2087. case QUIC_SSTREAM_STATE_READY:
  2088. qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
  2089. if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
  2090. *err = ERR_R_INTERNAL_ERROR;
  2091. return 0;
  2092. }
  2093. /* FALLTHROUGH */
  2094. case QUIC_SSTREAM_STATE_SEND:
  2095. case QUIC_SSTREAM_STATE_DATA_SENT:
  2096. if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
  2097. *err = SSL_R_STREAM_FINISHED;
  2098. return 0;
  2099. }
  2100. return 1;
  2101. case QUIC_SSTREAM_STATE_DATA_RECVD:
  2102. *err = SSL_R_STREAM_FINISHED;
  2103. return 0;
  2104. case QUIC_SSTREAM_STATE_RESET_SENT:
  2105. case QUIC_SSTREAM_STATE_RESET_RECVD:
  2106. *err = SSL_R_STREAM_RESET;
  2107. return 0;
  2108. }
  2109. }
  2110. QUIC_TAKES_LOCK
  2111. int ossl_quic_write_flags(SSL *s, const void *buf, size_t len,
  2112. uint64_t flags, size_t *written)
  2113. {
  2114. int ret;
  2115. QCTX ctx;
  2116. int partial_write, err;
  2117. *written = 0;
  2118. if (len == 0) {
  2119. /* Do not autocreate default XSO for zero-length writes. */
  2120. if (!expect_quic(s, &ctx))
  2121. return 0;
  2122. quic_lock_for_io(&ctx);
  2123. } else {
  2124. if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))
  2125. return 0;
  2126. }
  2127. partial_write = ((ctx.xso != NULL)
  2128. ? ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0) : 0);
  2129. if ((flags & ~SSL_WRITE_FLAG_CONCLUDE) != 0) {
  2130. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_UNSUPPORTED_WRITE_FLAG, NULL);
  2131. goto out;
  2132. }
  2133. if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
  2134. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  2135. goto out;
  2136. }
  2137. /*
  2138. * If we haven't finished the handshake, try to advance it.
  2139. * We don't accept writes until the handshake is completed.
  2140. */
  2141. if (quic_do_handshake(&ctx) < 1) {
  2142. ret = 0;
  2143. goto out;
  2144. }
  2145. /* Ensure correct stream state, stream send part not concluded, etc. */
  2146. if (len > 0 && !quic_validate_for_write(ctx.xso, &err)) {
  2147. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
  2148. goto out;
  2149. }
  2150. if (len == 0) {
  2151. if ((flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
  2152. quic_post_write(ctx.xso, 0, 1, flags,
  2153. qctx_should_autotick(&ctx));
  2154. ret = 1;
  2155. goto out;
  2156. }
  2157. if (xso_blocking_mode(ctx.xso))
  2158. ret = quic_write_blocking(&ctx, buf, len, flags, written);
  2159. else if (partial_write)
  2160. ret = quic_write_nonblocking_epw(&ctx, buf, len, flags, written);
  2161. else
  2162. ret = quic_write_nonblocking_aon(&ctx, buf, len, flags, written);
  2163. out:
  2164. quic_unlock(ctx.qc);
  2165. return ret;
  2166. }
  2167. QUIC_TAKES_LOCK
  2168. int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
  2169. {
  2170. return ossl_quic_write_flags(s, buf, len, 0, written);
  2171. }
  2172. /*
  2173. * SSL_read
  2174. * --------
  2175. */
  2176. struct quic_read_again_args {
  2177. QCTX *ctx;
  2178. QUIC_STREAM *stream;
  2179. void *buf;
  2180. size_t len;
  2181. size_t *bytes_read;
  2182. int peek;
  2183. };
  2184. QUIC_NEEDS_LOCK
  2185. static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
  2186. {
  2187. QUIC_STREAM_MAP *qsm;
  2188. *eos = 0;
  2189. if (xso == NULL || xso->stream == NULL) {
  2190. *err = ERR_R_INTERNAL_ERROR;
  2191. return 0;
  2192. }
  2193. switch (xso->stream->recv_state) {
  2194. default:
  2195. case QUIC_RSTREAM_STATE_NONE:
  2196. *err = SSL_R_STREAM_SEND_ONLY;
  2197. return 0;
  2198. case QUIC_RSTREAM_STATE_RECV:
  2199. case QUIC_RSTREAM_STATE_SIZE_KNOWN:
  2200. case QUIC_RSTREAM_STATE_DATA_RECVD:
  2201. return 1;
  2202. case QUIC_RSTREAM_STATE_DATA_READ:
  2203. *eos = 1;
  2204. return 0;
  2205. case QUIC_RSTREAM_STATE_RESET_RECVD:
  2206. qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
  2207. ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
  2208. /* FALLTHROUGH */
  2209. case QUIC_RSTREAM_STATE_RESET_READ:
  2210. *err = SSL_R_STREAM_RESET;
  2211. return 0;
  2212. }
  2213. }
  2214. QUIC_NEEDS_LOCK
  2215. static int quic_read_actual(QCTX *ctx,
  2216. QUIC_STREAM *stream,
  2217. void *buf, size_t buf_len,
  2218. size_t *bytes_read,
  2219. int peek)
  2220. {
  2221. int is_fin = 0, err, eos;
  2222. QUIC_CONNECTION *qc = ctx->qc;
  2223. if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
  2224. if (eos) {
  2225. ctx->xso->retired_fin = 1;
  2226. return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
  2227. } else {
  2228. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
  2229. }
  2230. }
  2231. if (peek) {
  2232. if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
  2233. bytes_read, &is_fin))
  2234. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  2235. } else {
  2236. if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
  2237. bytes_read, &is_fin))
  2238. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  2239. }
  2240. if (!peek) {
  2241. if (*bytes_read > 0) {
  2242. /*
  2243. * We have read at least one byte from the stream. Inform stream-level
  2244. * RXFC of the retirement of controlled bytes. Update the active stream
  2245. * status (the RXFC may now want to emit a frame granting more credit to
  2246. * the peer).
  2247. */
  2248. OSSL_RTT_INFO rtt_info;
  2249. ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
  2250. if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
  2251. rtt_info.smoothed_rtt))
  2252. return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
  2253. }
  2254. if (is_fin && !peek) {
  2255. QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
  2256. ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
  2257. }
  2258. if (*bytes_read > 0)
  2259. ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
  2260. stream);
  2261. }
  2262. if (*bytes_read == 0 && is_fin) {
  2263. ctx->xso->retired_fin = 1;
  2264. return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
  2265. }
  2266. return 1;
  2267. }
  2268. QUIC_NEEDS_LOCK
  2269. static int quic_read_again(void *arg)
  2270. {
  2271. struct quic_read_again_args *args = arg;
  2272. if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
  2273. /* If connection is torn down due to an error while blocking, stop. */
  2274. QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  2275. return -1;
  2276. }
  2277. if (!quic_read_actual(args->ctx, args->stream,
  2278. args->buf, args->len, args->bytes_read,
  2279. args->peek))
  2280. return -1;
  2281. if (*args->bytes_read > 0)
  2282. /* got at least one byte, the SSL_read op can finish now */
  2283. return 1;
  2284. return 0; /* did not read anything, keep trying */
  2285. }
  2286. QUIC_TAKES_LOCK
  2287. static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
  2288. {
  2289. int ret, res;
  2290. QCTX ctx;
  2291. struct quic_read_again_args args;
  2292. *bytes_read = 0;
  2293. if (!expect_quic(s, &ctx))
  2294. return 0;
  2295. quic_lock_for_io(&ctx);
  2296. if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
  2297. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  2298. goto out;
  2299. }
  2300. /* If we haven't finished the handshake, try to advance it. */
  2301. if (quic_do_handshake(&ctx) < 1) {
  2302. ret = 0; /* ossl_quic_do_handshake raised error here */
  2303. goto out;
  2304. }
  2305. if (ctx.xso == NULL) {
  2306. /*
  2307. * Called on a QCSO and we don't currently have a default stream.
  2308. *
  2309. * Wait until we get a stream initiated by the peer (blocking mode) or
  2310. * fail if we don't have one yet (non-blocking mode).
  2311. */
  2312. if (!qc_wait_for_default_xso_for_read(&ctx, /*peek=*/0)) {
  2313. ret = 0; /* error already raised here */
  2314. goto out;
  2315. }
  2316. ctx.xso = ctx.qc->default_xso;
  2317. }
  2318. if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
  2319. ret = 0; /* quic_read_actual raised error here */
  2320. goto out;
  2321. }
  2322. if (*bytes_read > 0) {
  2323. /*
  2324. * Even though we succeeded, tick the reactor here to ensure we are
  2325. * handling other aspects of the QUIC connection.
  2326. */
  2327. qctx_maybe_autotick(&ctx);
  2328. ret = 1;
  2329. } else if (xso_blocking_mode(ctx.xso)) {
  2330. /*
  2331. * We were not able to read anything immediately, so our stream
  2332. * buffer is empty. This means we need to block until we get
  2333. * at least one byte.
  2334. */
  2335. args.ctx = &ctx;
  2336. args.stream = ctx.xso->stream;
  2337. args.buf = buf;
  2338. args.len = len;
  2339. args.bytes_read = bytes_read;
  2340. args.peek = peek;
  2341. res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
  2342. if (res == 0) {
  2343. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
  2344. goto out;
  2345. } else if (res < 0) {
  2346. ret = 0; /* quic_read_again raised error here */
  2347. goto out;
  2348. }
  2349. ret = 1;
  2350. } else {
  2351. /*
  2352. * We did not get any bytes and are not in blocking mode.
  2353. * Tick to see if this delivers any more.
  2354. */
  2355. qctx_maybe_autotick(&ctx);
  2356. /* Try the read again. */
  2357. if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
  2358. ret = 0; /* quic_read_actual raised error here */
  2359. goto out;
  2360. }
  2361. if (*bytes_read > 0)
  2362. ret = 1; /* Succeeded this time. */
  2363. else
  2364. ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
  2365. }
  2366. out:
  2367. quic_unlock(ctx.qc);
  2368. return ret;
  2369. }
  2370. int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
  2371. {
  2372. return quic_read(s, buf, len, bytes_read, 0);
  2373. }
  2374. int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
  2375. {
  2376. return quic_read(s, buf, len, bytes_read, 1);
  2377. }
  2378. /*
  2379. * SSL_pending
  2380. * -----------
  2381. */
  2382. QUIC_TAKES_LOCK
  2383. static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
  2384. {
  2385. QCTX ctx;
  2386. size_t avail = 0;
  2387. if (!expect_quic(s, &ctx))
  2388. return 0;
  2389. quic_lock(ctx.qc);
  2390. if (!ctx.qc->started)
  2391. goto out;
  2392. if (ctx.xso == NULL) {
  2393. /* No XSO yet, but there might be a default XSO eligible to be created. */
  2394. if (qc_wait_for_default_xso_for_read(&ctx, /*peek=*/1)) {
  2395. ctx.xso = ctx.qc->default_xso;
  2396. } else {
  2397. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
  2398. goto out;
  2399. }
  2400. }
  2401. if (ctx.xso->stream == NULL) {
  2402. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
  2403. goto out;
  2404. }
  2405. if (check_channel)
  2406. avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
  2407. /*include_fin=*/1)
  2408. || ossl_quic_channel_has_pending(ctx.qc->ch)
  2409. || ossl_quic_channel_is_term_any(ctx.qc->ch);
  2410. else
  2411. avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
  2412. /*include_fin=*/0);
  2413. out:
  2414. quic_unlock(ctx.qc);
  2415. return avail;
  2416. }
  2417. size_t ossl_quic_pending(const SSL *s)
  2418. {
  2419. return ossl_quic_pending_int(s, /*check_channel=*/0);
  2420. }
  2421. int ossl_quic_has_pending(const SSL *s)
  2422. {
  2423. /* Do we have app-side pending data or pending URXEs or RXEs? */
  2424. return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
  2425. }
  2426. /*
  2427. * SSL_stream_conclude
  2428. * -------------------
  2429. */
  2430. QUIC_TAKES_LOCK
  2431. int ossl_quic_conn_stream_conclude(SSL *s)
  2432. {
  2433. QCTX ctx;
  2434. QUIC_STREAM *qs;
  2435. int err;
  2436. int ret;
  2437. if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))
  2438. return 0;
  2439. qs = ctx.xso->stream;
  2440. if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
  2441. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  2442. quic_unlock(ctx.qc);
  2443. return ret;
  2444. }
  2445. if (!quic_validate_for_write(ctx.xso, &err)) {
  2446. ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
  2447. quic_unlock(ctx.qc);
  2448. return ret;
  2449. }
  2450. if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
  2451. quic_unlock(ctx.qc);
  2452. return 1;
  2453. }
  2454. ossl_quic_sstream_fin(qs->sstream);
  2455. quic_post_write(ctx.xso, 1, 0, 0, qctx_should_autotick(&ctx));
  2456. quic_unlock(ctx.qc);
  2457. return 1;
  2458. }
  2459. /*
  2460. * SSL_inject_net_dgram
  2461. * --------------------
  2462. */
  2463. QUIC_TAKES_LOCK
  2464. int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
  2465. size_t buf_len,
  2466. const BIO_ADDR *peer,
  2467. const BIO_ADDR *local)
  2468. {
  2469. int ret;
  2470. QCTX ctx;
  2471. QUIC_DEMUX *demux;
  2472. if (!expect_quic(s, &ctx))
  2473. return 0;
  2474. quic_lock(ctx.qc);
  2475. demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
  2476. ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
  2477. quic_unlock(ctx.qc);
  2478. return ret;
  2479. }
  2480. /*
  2481. * SSL_get0_connection
  2482. * -------------------
  2483. */
  2484. SSL *ossl_quic_get0_connection(SSL *s)
  2485. {
  2486. QCTX ctx;
  2487. if (!expect_quic(s, &ctx))
  2488. return NULL;
  2489. return &ctx.qc->ssl;
  2490. }
  2491. /*
  2492. * SSL_get_stream_type
  2493. * -------------------
  2494. */
  2495. int ossl_quic_get_stream_type(SSL *s)
  2496. {
  2497. QCTX ctx;
  2498. if (!expect_quic(s, &ctx))
  2499. return SSL_STREAM_TYPE_BIDI;
  2500. if (ctx.xso == NULL) {
  2501. /*
  2502. * If deferred XSO creation has yet to occur, proceed according to the
  2503. * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
  2504. * what kind of stream will be created yet, so return BIDI on the basis
  2505. * that at this time, the client still has the option of calling
  2506. * SSL_read() or SSL_write() first.
  2507. */
  2508. if (ctx.qc->default_xso_created
  2509. || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
  2510. return SSL_STREAM_TYPE_NONE;
  2511. else
  2512. return SSL_STREAM_TYPE_BIDI;
  2513. }
  2514. if (ossl_quic_stream_is_bidi(ctx.xso->stream))
  2515. return SSL_STREAM_TYPE_BIDI;
  2516. if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
  2517. return SSL_STREAM_TYPE_READ;
  2518. else
  2519. return SSL_STREAM_TYPE_WRITE;
  2520. }
  2521. /*
  2522. * SSL_get_stream_id
  2523. * -----------------
  2524. */
  2525. QUIC_TAKES_LOCK
  2526. uint64_t ossl_quic_get_stream_id(SSL *s)
  2527. {
  2528. QCTX ctx;
  2529. uint64_t id;
  2530. if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
  2531. return UINT64_MAX;
  2532. id = ctx.xso->stream->id;
  2533. quic_unlock(ctx.qc);
  2534. return id;
  2535. }
  2536. /*
  2537. * SSL_is_stream_local
  2538. * -------------------
  2539. */
  2540. QUIC_TAKES_LOCK
  2541. int ossl_quic_is_stream_local(SSL *s)
  2542. {
  2543. QCTX ctx;
  2544. int is_local;
  2545. if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
  2546. return -1;
  2547. is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);
  2548. quic_unlock(ctx.qc);
  2549. return is_local;
  2550. }
  2551. /*
  2552. * SSL_set_default_stream_mode
  2553. * ---------------------------
  2554. */
  2555. QUIC_TAKES_LOCK
  2556. int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
  2557. {
  2558. QCTX ctx;
  2559. if (!expect_quic_conn_only(s, &ctx))
  2560. return 0;
  2561. quic_lock(ctx.qc);
  2562. if (ctx.qc->default_xso_created) {
  2563. quic_unlock(ctx.qc);
  2564. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  2565. "too late to change default stream mode");
  2566. }
  2567. switch (mode) {
  2568. case SSL_DEFAULT_STREAM_MODE_NONE:
  2569. case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
  2570. case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
  2571. ctx.qc->default_stream_mode = mode;
  2572. break;
  2573. default:
  2574. quic_unlock(ctx.qc);
  2575. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
  2576. "bad default stream type");
  2577. }
  2578. quic_unlock(ctx.qc);
  2579. return 1;
  2580. }
  2581. /*
  2582. * SSL_detach_stream
  2583. * -----------------
  2584. */
  2585. QUIC_TAKES_LOCK
  2586. SSL *ossl_quic_detach_stream(SSL *s)
  2587. {
  2588. QCTX ctx;
  2589. QUIC_XSO *xso = NULL;
  2590. if (!expect_quic_conn_only(s, &ctx))
  2591. return NULL;
  2592. quic_lock(ctx.qc);
  2593. /* Calling this function inhibits default XSO autocreation. */
  2594. /* QC ref to any default XSO is transferred to us and to caller. */
  2595. qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
  2596. quic_unlock(ctx.qc);
  2597. return xso != NULL ? &xso->ssl : NULL;
  2598. }
  2599. /*
  2600. * SSL_attach_stream
  2601. * -----------------
  2602. */
  2603. QUIC_TAKES_LOCK
  2604. int ossl_quic_attach_stream(SSL *conn, SSL *stream)
  2605. {
  2606. QCTX ctx;
  2607. QUIC_XSO *xso;
  2608. int nref;
  2609. if (!expect_quic_conn_only(conn, &ctx))
  2610. return 0;
  2611. if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
  2612. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
  2613. "stream to attach must be a valid QUIC stream");
  2614. xso = (QUIC_XSO *)stream;
  2615. quic_lock(ctx.qc);
  2616. if (ctx.qc->default_xso != NULL) {
  2617. quic_unlock(ctx.qc);
  2618. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  2619. "connection already has a default stream");
  2620. }
  2621. /*
  2622. * It is a caller error for the XSO being attached as a default XSO to have
  2623. * more than one ref.
  2624. */
  2625. if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
  2626. quic_unlock(ctx.qc);
  2627. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
  2628. "ref");
  2629. }
  2630. if (nref != 1) {
  2631. quic_unlock(ctx.qc);
  2632. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
  2633. "stream being attached must have "
  2634. "only 1 reference");
  2635. }
  2636. /* Caller's reference to the XSO is transferred to us. */
  2637. /* Calling this function inhibits default XSO autocreation. */
  2638. qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
  2639. quic_unlock(ctx.qc);
  2640. return 1;
  2641. }
  2642. /*
  2643. * SSL_set_incoming_stream_policy
  2644. * ------------------------------
  2645. */
  2646. QUIC_NEEDS_LOCK
  2647. static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
  2648. {
  2649. switch (qc->incoming_stream_policy) {
  2650. case SSL_INCOMING_STREAM_POLICY_AUTO:
  2651. if ((qc->default_xso == NULL && !qc->default_xso_created)
  2652. || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
  2653. return SSL_INCOMING_STREAM_POLICY_ACCEPT;
  2654. else
  2655. return SSL_INCOMING_STREAM_POLICY_REJECT;
  2656. default:
  2657. return qc->incoming_stream_policy;
  2658. }
  2659. }
  2660. QUIC_NEEDS_LOCK
  2661. static void qc_update_reject_policy(QUIC_CONNECTION *qc)
  2662. {
  2663. int policy = qc_get_effective_incoming_stream_policy(qc);
  2664. int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
  2665. ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
  2666. enable_reject,
  2667. qc->incoming_stream_aec);
  2668. }
  2669. QUIC_TAKES_LOCK
  2670. int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
  2671. uint64_t aec)
  2672. {
  2673. int ret = 1;
  2674. QCTX ctx;
  2675. if (!expect_quic_conn_only(s, &ctx))
  2676. return 0;
  2677. quic_lock(ctx.qc);
  2678. switch (policy) {
  2679. case SSL_INCOMING_STREAM_POLICY_AUTO:
  2680. case SSL_INCOMING_STREAM_POLICY_ACCEPT:
  2681. case SSL_INCOMING_STREAM_POLICY_REJECT:
  2682. ctx.qc->incoming_stream_policy = policy;
  2683. ctx.qc->incoming_stream_aec = aec;
  2684. break;
  2685. default:
  2686. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
  2687. ret = 0;
  2688. break;
  2689. }
  2690. qc_update_reject_policy(ctx.qc);
  2691. quic_unlock(ctx.qc);
  2692. return ret;
  2693. }
  2694. /*
  2695. * SSL_get_value, SSL_set_value
  2696. * ----------------------------
  2697. */
  2698. QUIC_TAKES_LOCK
  2699. static int qc_getset_idle_timeout(QCTX *ctx, uint32_t class_,
  2700. uint64_t *p_value_out, uint64_t *p_value_in)
  2701. {
  2702. int ret = 0;
  2703. uint64_t value_out = 0, value_in;
  2704. quic_lock(ctx->qc);
  2705. switch (class_) {
  2706. case SSL_VALUE_CLASS_FEATURE_REQUEST:
  2707. value_out = ossl_quic_channel_get_max_idle_timeout_request(ctx->qc->ch);
  2708. if (p_value_in != NULL) {
  2709. value_in = *p_value_in;
  2710. if (value_in > OSSL_QUIC_VLINT_MAX) {
  2711. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
  2712. NULL);
  2713. goto err;
  2714. }
  2715. if (ossl_quic_channel_have_generated_transport_params(ctx->qc->ch)) {
  2716. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NOT_RENEGOTIABLE,
  2717. NULL);
  2718. goto err;
  2719. }
  2720. ossl_quic_channel_set_max_idle_timeout_request(ctx->qc->ch, value_in);
  2721. }
  2722. break;
  2723. case SSL_VALUE_CLASS_FEATURE_PEER_REQUEST:
  2724. case SSL_VALUE_CLASS_FEATURE_NEGOTIATED:
  2725. if (p_value_in != NULL) {
  2726. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_OP,
  2727. NULL);
  2728. goto err;
  2729. }
  2730. if (!ossl_quic_channel_is_handshake_complete(ctx->qc->ch)) {
  2731. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NEGOTIATION_NOT_COMPLETE,
  2732. NULL);
  2733. goto err;
  2734. }
  2735. value_out = (class_ == SSL_VALUE_CLASS_FEATURE_NEGOTIATED)
  2736. ? ossl_quic_channel_get_max_idle_timeout_actual(ctx->qc->ch)
  2737. : ossl_quic_channel_get_max_idle_timeout_peer_request(ctx->qc->ch);
  2738. break;
  2739. default:
  2740. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
  2741. NULL);
  2742. goto err;
  2743. }
  2744. ret = 1;
  2745. err:
  2746. quic_unlock(ctx->qc);
  2747. if (ret && p_value_out != NULL)
  2748. *p_value_out = value_out;
  2749. return ret;
  2750. }
  2751. QUIC_TAKES_LOCK
  2752. static int qc_get_stream_avail(QCTX *ctx, uint32_t class_,
  2753. int is_uni, int is_remote,
  2754. uint64_t *value)
  2755. {
  2756. int ret = 0;
  2757. if (class_ != SSL_VALUE_CLASS_GENERIC) {
  2758. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
  2759. NULL);
  2760. return 0;
  2761. }
  2762. quic_lock(ctx->qc);
  2763. *value = is_remote
  2764. ? ossl_quic_channel_get_remote_stream_count_avail(ctx->qc->ch, is_uni)
  2765. : ossl_quic_channel_get_local_stream_count_avail(ctx->qc->ch, is_uni);
  2766. ret = 1;
  2767. quic_unlock(ctx->qc);
  2768. return ret;
  2769. }
  2770. QUIC_NEEDS_LOCK
  2771. static int qctx_should_autotick(QCTX *ctx)
  2772. {
  2773. int event_handling_mode;
  2774. if (ctx->is_stream) {
  2775. event_handling_mode = ctx->xso->event_handling_mode;
  2776. if (event_handling_mode != SSL_VALUE_EVENT_HANDLING_MODE_INHERIT)
  2777. return event_handling_mode != SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT;
  2778. }
  2779. event_handling_mode = ctx->qc->event_handling_mode;
  2780. return event_handling_mode != SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT;
  2781. }
  2782. QUIC_NEEDS_LOCK
  2783. static void qctx_maybe_autotick(QCTX *ctx)
  2784. {
  2785. if (!qctx_should_autotick(ctx))
  2786. return;
  2787. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx->qc->ch), 0);
  2788. }
  2789. QUIC_TAKES_LOCK
  2790. static int qc_getset_event_handling(QCTX *ctx, uint32_t class_,
  2791. uint64_t *p_value_out,
  2792. uint64_t *p_value_in)
  2793. {
  2794. int ret = 0;
  2795. uint64_t value_out = 0;
  2796. quic_lock(ctx->qc);
  2797. if (class_ != SSL_VALUE_CLASS_GENERIC) {
  2798. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
  2799. NULL);
  2800. goto err;
  2801. }
  2802. if (p_value_in != NULL) {
  2803. switch (*p_value_in) {
  2804. case SSL_VALUE_EVENT_HANDLING_MODE_INHERIT:
  2805. case SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT:
  2806. case SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT:
  2807. break;
  2808. default:
  2809. QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
  2810. NULL);
  2811. goto err;
  2812. }
  2813. value_out = *p_value_in;
  2814. if (ctx->is_stream)
  2815. ctx->xso->event_handling_mode = (int)value_out;
  2816. else
  2817. ctx->qc->event_handling_mode = (int)value_out;
  2818. } else {
  2819. value_out = ctx->is_stream
  2820. ? ctx->xso->event_handling_mode
  2821. : ctx->qc->event_handling_mode;
  2822. }
  2823. ret = 1;
  2824. err:
  2825. quic_unlock(ctx->qc);
  2826. if (ret && p_value_out != NULL)
  2827. *p_value_out = value_out;
  2828. return ret;
  2829. }
  2830. QUIC_TAKES_LOCK
  2831. static int qc_get_stream_write_buf_stat(QCTX *ctx, uint32_t class_,
  2832. uint64_t *p_value_out,
  2833. size_t (*getter)(QUIC_SSTREAM *sstream))
  2834. {
  2835. int ret = 0;
  2836. size_t value = 0;
  2837. quic_lock(ctx->qc);
  2838. if (class_ != SSL_VALUE_CLASS_GENERIC) {
  2839. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
  2840. NULL);
  2841. goto err;
  2842. }
  2843. if (ctx->xso == NULL) {
  2844. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
  2845. goto err;
  2846. }
  2847. if (!ossl_quic_stream_has_send(ctx->xso->stream)) {
  2848. QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_RECV_ONLY, NULL);
  2849. goto err;
  2850. }
  2851. if (ossl_quic_stream_has_send_buffer(ctx->xso->stream))
  2852. value = getter(ctx->xso->stream->sstream);
  2853. ret = 1;
  2854. err:
  2855. quic_unlock(ctx->qc);
  2856. *p_value_out = (uint64_t)value;
  2857. return ret;
  2858. }
  2859. QUIC_NEEDS_LOCK
  2860. static int expect_quic_for_value(SSL *s, QCTX *ctx, uint32_t id)
  2861. {
  2862. switch (id) {
  2863. case SSL_VALUE_EVENT_HANDLING_MODE:
  2864. case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
  2865. case SSL_VALUE_STREAM_WRITE_BUF_USED:
  2866. case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
  2867. return expect_quic(s, ctx);
  2868. default:
  2869. return expect_quic_conn_only(s, ctx);
  2870. }
  2871. }
  2872. QUIC_TAKES_LOCK
  2873. int ossl_quic_get_value_uint(SSL *s, uint32_t class_, uint32_t id,
  2874. uint64_t *value)
  2875. {
  2876. QCTX ctx;
  2877. if (!expect_quic_for_value(s, &ctx, id))
  2878. return 0;
  2879. if (value == NULL)
  2880. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
  2881. ERR_R_PASSED_INVALID_ARGUMENT, NULL);
  2882. switch (id) {
  2883. case SSL_VALUE_QUIC_IDLE_TIMEOUT:
  2884. return qc_getset_idle_timeout(&ctx, class_, value, NULL);
  2885. case SSL_VALUE_QUIC_STREAM_BIDI_LOCAL_AVAIL:
  2886. return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/0, value);
  2887. case SSL_VALUE_QUIC_STREAM_BIDI_REMOTE_AVAIL:
  2888. return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/1, value);
  2889. case SSL_VALUE_QUIC_STREAM_UNI_LOCAL_AVAIL:
  2890. return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/0, value);
  2891. case SSL_VALUE_QUIC_STREAM_UNI_REMOTE_AVAIL:
  2892. return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/1, value);
  2893. case SSL_VALUE_EVENT_HANDLING_MODE:
  2894. return qc_getset_event_handling(&ctx, class_, value, NULL);
  2895. case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
  2896. return qc_get_stream_write_buf_stat(&ctx, class_, value,
  2897. ossl_quic_sstream_get_buffer_size);
  2898. case SSL_VALUE_STREAM_WRITE_BUF_USED:
  2899. return qc_get_stream_write_buf_stat(&ctx, class_, value,
  2900. ossl_quic_sstream_get_buffer_used);
  2901. case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
  2902. return qc_get_stream_write_buf_stat(&ctx, class_, value,
  2903. ossl_quic_sstream_get_buffer_avail);
  2904. default:
  2905. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
  2906. SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
  2907. }
  2908. return 1;
  2909. }
  2910. QUIC_TAKES_LOCK
  2911. int ossl_quic_set_value_uint(SSL *s, uint32_t class_, uint32_t id,
  2912. uint64_t value)
  2913. {
  2914. QCTX ctx;
  2915. if (!expect_quic_for_value(s, &ctx, id))
  2916. return 0;
  2917. switch (id) {
  2918. case SSL_VALUE_QUIC_IDLE_TIMEOUT:
  2919. return qc_getset_idle_timeout(&ctx, class_, NULL, &value);
  2920. case SSL_VALUE_EVENT_HANDLING_MODE:
  2921. return qc_getset_event_handling(&ctx, class_, NULL, &value);
  2922. default:
  2923. return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
  2924. SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
  2925. }
  2926. return 1;
  2927. }
  2928. /*
  2929. * SSL_accept_stream
  2930. * -----------------
  2931. */
  2932. struct wait_for_incoming_stream_args {
  2933. QCTX *ctx;
  2934. QUIC_STREAM *qs;
  2935. };
  2936. QUIC_NEEDS_LOCK
  2937. static int wait_for_incoming_stream(void *arg)
  2938. {
  2939. struct wait_for_incoming_stream_args *args = arg;
  2940. QUIC_CONNECTION *qc = args->ctx->qc;
  2941. QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
  2942. if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
  2943. /* If connection is torn down due to an error while blocking, stop. */
  2944. QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
  2945. return -1;
  2946. }
  2947. args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
  2948. if (args->qs != NULL)
  2949. return 1; /* got a stream */
  2950. return 0; /* did not get a stream, keep trying */
  2951. }
  2952. QUIC_TAKES_LOCK
  2953. SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
  2954. {
  2955. QCTX ctx;
  2956. int ret;
  2957. SSL *new_s = NULL;
  2958. QUIC_STREAM_MAP *qsm;
  2959. QUIC_STREAM *qs;
  2960. QUIC_XSO *xso;
  2961. OSSL_RTT_INFO rtt_info;
  2962. if (!expect_quic_conn_only(s, &ctx))
  2963. return NULL;
  2964. quic_lock(ctx.qc);
  2965. if (qc_get_effective_incoming_stream_policy(ctx.qc)
  2966. == SSL_INCOMING_STREAM_POLICY_REJECT) {
  2967. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
  2968. goto out;
  2969. }
  2970. qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
  2971. qs = ossl_quic_stream_map_peek_accept_queue(qsm);
  2972. if (qs == NULL) {
  2973. if (qc_blocking_mode(ctx.qc)
  2974. && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
  2975. struct wait_for_incoming_stream_args args;
  2976. args.ctx = &ctx;
  2977. args.qs = NULL;
  2978. ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
  2979. if (ret == 0) {
  2980. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
  2981. goto out;
  2982. } else if (ret < 0 || args.qs == NULL) {
  2983. goto out;
  2984. }
  2985. qs = args.qs;
  2986. } else {
  2987. goto out;
  2988. }
  2989. }
  2990. xso = create_xso_from_stream(ctx.qc, qs);
  2991. if (xso == NULL)
  2992. goto out;
  2993. ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
  2994. ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
  2995. rtt_info.smoothed_rtt);
  2996. new_s = &xso->ssl;
  2997. /* Calling this function inhibits default XSO autocreation. */
  2998. qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
  2999. out:
  3000. quic_unlock(ctx.qc);
  3001. return new_s;
  3002. }
  3003. /*
  3004. * SSL_get_accept_stream_queue_len
  3005. * -------------------------------
  3006. */
  3007. QUIC_TAKES_LOCK
  3008. size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
  3009. {
  3010. QCTX ctx;
  3011. size_t v;
  3012. if (!expect_quic_conn_only(s, &ctx))
  3013. return 0;
  3014. quic_lock(ctx.qc);
  3015. v = ossl_quic_stream_map_get_total_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
  3016. quic_unlock(ctx.qc);
  3017. return v;
  3018. }
  3019. /*
  3020. * SSL_stream_reset
  3021. * ----------------
  3022. */
  3023. int ossl_quic_stream_reset(SSL *ssl,
  3024. const SSL_STREAM_RESET_ARGS *args,
  3025. size_t args_len)
  3026. {
  3027. QCTX ctx;
  3028. QUIC_STREAM_MAP *qsm;
  3029. QUIC_STREAM *qs;
  3030. uint64_t error_code;
  3031. int ok, err;
  3032. if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))
  3033. return 0;
  3034. qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
  3035. qs = ctx.xso->stream;
  3036. error_code = (args != NULL ? args->quic_error_code : 0);
  3037. if (!quic_validate_for_write(ctx.xso, &err)) {
  3038. ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
  3039. goto err;
  3040. }
  3041. ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
  3042. if (ok)
  3043. ctx.xso->requested_reset = 1;
  3044. err:
  3045. quic_unlock(ctx.qc);
  3046. return ok;
  3047. }
  3048. /*
  3049. * SSL_get_stream_read_state
  3050. * -------------------------
  3051. */
  3052. static void quic_classify_stream(QUIC_CONNECTION *qc,
  3053. QUIC_STREAM *qs,
  3054. int is_write,
  3055. int *state,
  3056. uint64_t *app_error_code)
  3057. {
  3058. int local_init;
  3059. uint64_t final_size;
  3060. local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
  3061. if (app_error_code != NULL)
  3062. *app_error_code = UINT64_MAX;
  3063. else
  3064. app_error_code = &final_size; /* throw away value */
  3065. if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
  3066. /*
  3067. * Unidirectional stream and this direction of transmission doesn't
  3068. * exist.
  3069. */
  3070. *state = SSL_STREAM_STATE_WRONG_DIR;
  3071. } else if (ossl_quic_channel_is_term_any(qc->ch)) {
  3072. /* Connection already closed. */
  3073. *state = SSL_STREAM_STATE_CONN_CLOSED;
  3074. } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
  3075. /* Application has read a FIN. */
  3076. *state = SSL_STREAM_STATE_FINISHED;
  3077. } else if ((!is_write && qs->stop_sending)
  3078. || (is_write && ossl_quic_stream_send_is_reset(qs))) {
  3079. /*
  3080. * Stream has been reset locally. FIN takes precedence over this for the
  3081. * read case as the application need not care if the stream is reset
  3082. * after a FIN has been successfully processed.
  3083. */
  3084. *state = SSL_STREAM_STATE_RESET_LOCAL;
  3085. *app_error_code = !is_write
  3086. ? qs->stop_sending_aec
  3087. : qs->reset_stream_aec;
  3088. } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
  3089. || (is_write && qs->peer_stop_sending)) {
  3090. /*
  3091. * Stream has been reset remotely. */
  3092. *state = SSL_STREAM_STATE_RESET_REMOTE;
  3093. *app_error_code = !is_write
  3094. ? qs->peer_reset_stream_aec
  3095. : qs->peer_stop_sending_aec;
  3096. } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
  3097. &final_size)) {
  3098. /*
  3099. * Stream has been finished. Stream reset takes precedence over this for
  3100. * the write case as peer may not have received all data.
  3101. */
  3102. *state = SSL_STREAM_STATE_FINISHED;
  3103. } else {
  3104. /* Stream still healthy. */
  3105. *state = SSL_STREAM_STATE_OK;
  3106. }
  3107. }
  3108. static int quic_get_stream_state(SSL *ssl, int is_write)
  3109. {
  3110. QCTX ctx;
  3111. int state;
  3112. if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
  3113. return SSL_STREAM_STATE_NONE;
  3114. quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
  3115. quic_unlock(ctx.qc);
  3116. return state;
  3117. }
  3118. int ossl_quic_get_stream_read_state(SSL *ssl)
  3119. {
  3120. return quic_get_stream_state(ssl, /*is_write=*/0);
  3121. }
  3122. /*
  3123. * SSL_get_stream_write_state
  3124. * --------------------------
  3125. */
  3126. int ossl_quic_get_stream_write_state(SSL *ssl)
  3127. {
  3128. return quic_get_stream_state(ssl, /*is_write=*/1);
  3129. }
  3130. /*
  3131. * SSL_get_stream_read_error_code
  3132. * ------------------------------
  3133. */
  3134. static int quic_get_stream_error_code(SSL *ssl, int is_write,
  3135. uint64_t *app_error_code)
  3136. {
  3137. QCTX ctx;
  3138. int state;
  3139. if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
  3140. return -1;
  3141. quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
  3142. &state, app_error_code);
  3143. quic_unlock(ctx.qc);
  3144. switch (state) {
  3145. case SSL_STREAM_STATE_FINISHED:
  3146. return 0;
  3147. case SSL_STREAM_STATE_RESET_LOCAL:
  3148. case SSL_STREAM_STATE_RESET_REMOTE:
  3149. return 1;
  3150. default:
  3151. return -1;
  3152. }
  3153. }
  3154. int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
  3155. {
  3156. return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
  3157. }
  3158. /*
  3159. * SSL_get_stream_write_error_code
  3160. * -------------------------------
  3161. */
  3162. int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
  3163. {
  3164. return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
  3165. }
  3166. /*
  3167. * Write buffer size mutation
  3168. * --------------------------
  3169. */
  3170. int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
  3171. {
  3172. int ret = 0;
  3173. QCTX ctx;
  3174. if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
  3175. return 0;
  3176. if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
  3177. /* Called on a unidirectional receive-only stream - error. */
  3178. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
  3179. goto out;
  3180. }
  3181. if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
  3182. /*
  3183. * If the stream has a send part but we have disposed of it because we
  3184. * no longer need it, this is a no-op.
  3185. */
  3186. ret = 1;
  3187. goto out;
  3188. }
  3189. if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
  3190. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
  3191. goto out;
  3192. }
  3193. ret = 1;
  3194. out:
  3195. quic_unlock(ctx.qc);
  3196. return ret;
  3197. }
  3198. /*
  3199. * SSL_get_conn_close_info
  3200. * -----------------------
  3201. */
  3202. int ossl_quic_get_conn_close_info(SSL *ssl,
  3203. SSL_CONN_CLOSE_INFO *info,
  3204. size_t info_len)
  3205. {
  3206. QCTX ctx;
  3207. const QUIC_TERMINATE_CAUSE *tc;
  3208. if (!expect_quic_conn_only(ssl, &ctx))
  3209. return -1;
  3210. tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
  3211. if (tc == NULL)
  3212. return 0;
  3213. info->error_code = tc->error_code;
  3214. info->frame_type = tc->frame_type;
  3215. info->reason = tc->reason;
  3216. info->reason_len = tc->reason_len;
  3217. info->flags = 0;
  3218. if (!tc->remote)
  3219. info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;
  3220. if (!tc->app)
  3221. info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;
  3222. return 1;
  3223. }
  3224. /*
  3225. * SSL_key_update
  3226. * --------------
  3227. */
  3228. int ossl_quic_key_update(SSL *ssl, int update_type)
  3229. {
  3230. QCTX ctx;
  3231. if (!expect_quic_conn_only(ssl, &ctx))
  3232. return 0;
  3233. switch (update_type) {
  3234. case SSL_KEY_UPDATE_NOT_REQUESTED:
  3235. /*
  3236. * QUIC signals peer key update implicily by triggering a local
  3237. * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
  3238. */
  3239. case SSL_KEY_UPDATE_REQUESTED:
  3240. break;
  3241. default:
  3242. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
  3243. return 0;
  3244. }
  3245. quic_lock(ctx.qc);
  3246. /* Attempt to perform a TXKU. */
  3247. if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
  3248. QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
  3249. quic_unlock(ctx.qc);
  3250. return 0;
  3251. }
  3252. quic_unlock(ctx.qc);
  3253. return 1;
  3254. }
  3255. /*
  3256. * SSL_get_key_update_type
  3257. * -----------------------
  3258. */
  3259. int ossl_quic_get_key_update_type(const SSL *s)
  3260. {
  3261. /*
  3262. * We always handle key updates immediately so a key update is never
  3263. * pending.
  3264. */
  3265. return SSL_KEY_UPDATE_NONE;
  3266. }
  3267. /*
  3268. * QUIC Front-End I/O API: SSL_CTX Management
  3269. * ==========================================
  3270. */
  3271. long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  3272. {
  3273. switch (cmd) {
  3274. default:
  3275. return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
  3276. }
  3277. }
  3278. long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  3279. {
  3280. QCTX ctx;
  3281. if (!expect_quic_conn_only(s, &ctx))
  3282. return 0;
  3283. switch (cmd) {
  3284. case SSL_CTRL_SET_MSG_CALLBACK:
  3285. ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
  3286. &ctx.qc->ssl);
  3287. /* This callback also needs to be set on the internal SSL object */
  3288. return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
  3289. default:
  3290. /* Probably a TLS related ctrl. Defer to our internal SSL object */
  3291. return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
  3292. }
  3293. }
  3294. long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  3295. {
  3296. return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
  3297. }
  3298. int ossl_quic_renegotiate_check(SSL *ssl, int initok)
  3299. {
  3300. /* We never do renegotiation. */
  3301. return 0;
  3302. }
  3303. const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)
  3304. {
  3305. const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);
  3306. if ((ciph->algorithm2 & SSL_QUIC) == 0)
  3307. return NULL;
  3308. return ciph;
  3309. }
  3310. /*
  3311. * These functions define the TLSv1.2 (and below) ciphers that are supported by
  3312. * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
  3313. */
  3314. int ossl_quic_num_ciphers(void)
  3315. {
  3316. return 0;
  3317. }
  3318. const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
  3319. {
  3320. return NULL;
  3321. }
  3322. /*
  3323. * SSL_get_shutdown()
  3324. * ------------------
  3325. */
  3326. int ossl_quic_get_shutdown(const SSL *s)
  3327. {
  3328. QCTX ctx;
  3329. int shut = 0;
  3330. if (!expect_quic_conn_only(s, &ctx))
  3331. return 0;
  3332. if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
  3333. shut |= SSL_SENT_SHUTDOWN;
  3334. if (!ossl_quic_channel_is_closing(ctx.qc->ch))
  3335. shut |= SSL_RECEIVED_SHUTDOWN;
  3336. }
  3337. return shut;
  3338. }
  3339. /*
  3340. * QUIC Polling Support APIs
  3341. * =========================
  3342. */
  3343. /* Do we have the R (read) condition? */
  3344. QUIC_NEEDS_LOCK
  3345. static int test_poll_event_r(QUIC_XSO *xso)
  3346. {
  3347. int fin = 0;
  3348. size_t avail = 0;
  3349. return ossl_quic_stream_has_recv_buffer(xso->stream)
  3350. && ossl_quic_rstream_available(xso->stream->rstream, &avail, &fin)
  3351. && (avail > 0 || (fin && !xso->retired_fin));
  3352. }
  3353. /* Do we have the ER (exception: read) condition? */
  3354. QUIC_NEEDS_LOCK
  3355. static int test_poll_event_er(QUIC_XSO *xso)
  3356. {
  3357. return ossl_quic_stream_has_recv(xso->stream)
  3358. && ossl_quic_stream_recv_is_reset(xso->stream)
  3359. && !xso->retired_fin;
  3360. }
  3361. /* Do we have the W (write) condition? */
  3362. QUIC_NEEDS_LOCK
  3363. static int test_poll_event_w(QUIC_XSO *xso)
  3364. {
  3365. return !xso->conn->shutting_down
  3366. && ossl_quic_stream_has_send_buffer(xso->stream)
  3367. && ossl_quic_sstream_get_buffer_avail(xso->stream->sstream)
  3368. && !ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)
  3369. && quic_mutation_allowed(xso->conn, /*req_active=*/1);
  3370. }
  3371. /* Do we have the EW (exception: write) condition? */
  3372. QUIC_NEEDS_LOCK
  3373. static int test_poll_event_ew(QUIC_XSO *xso)
  3374. {
  3375. return ossl_quic_stream_has_send(xso->stream)
  3376. && xso->stream->peer_stop_sending
  3377. && !xso->requested_reset
  3378. && !xso->conn->shutting_down;
  3379. }
  3380. /* Do we have the EC (exception: connection) condition? */
  3381. QUIC_NEEDS_LOCK
  3382. static int test_poll_event_ec(QUIC_CONNECTION *qc)
  3383. {
  3384. return ossl_quic_channel_is_term_any(qc->ch);
  3385. }
  3386. /* Do we have the ECD (exception: connection drained) condition? */
  3387. QUIC_NEEDS_LOCK
  3388. static int test_poll_event_ecd(QUIC_CONNECTION *qc)
  3389. {
  3390. return ossl_quic_channel_is_terminated(qc->ch);
  3391. }
  3392. /* Do we have the IS (incoming: stream) condition? */
  3393. QUIC_NEEDS_LOCK
  3394. static int test_poll_event_is(QUIC_CONNECTION *qc, int is_uni)
  3395. {
  3396. return ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(qc->ch),
  3397. is_uni);
  3398. }
  3399. /* Do we have the OS (outgoing: stream) condition? */
  3400. QUIC_NEEDS_LOCK
  3401. static int test_poll_event_os(QUIC_CONNECTION *qc, int is_uni)
  3402. {
  3403. /* Is it currently possible for us to make an outgoing stream? */
  3404. return quic_mutation_allowed(qc, /*req_active=*/1)
  3405. && ossl_quic_channel_get_local_stream_count_avail(qc->ch, is_uni) > 0;
  3406. }
  3407. QUIC_TAKES_LOCK
  3408. int ossl_quic_conn_poll_events(SSL *ssl, uint64_t events, int do_tick,
  3409. uint64_t *p_revents)
  3410. {
  3411. QCTX ctx;
  3412. uint64_t revents = 0;
  3413. if (!expect_quic(ssl, &ctx))
  3414. return 0;
  3415. quic_lock(ctx.qc);
  3416. if (!ctx.qc->started) {
  3417. /* We can only try to write on non-started connection. */
  3418. if ((events & SSL_POLL_EVENT_W) != 0)
  3419. revents |= SSL_POLL_EVENT_W;
  3420. goto end;
  3421. }
  3422. if (do_tick)
  3423. ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
  3424. if (ctx.xso != NULL) {
  3425. /* SSL object has a stream component. */
  3426. if ((events & SSL_POLL_EVENT_R) != 0
  3427. && test_poll_event_r(ctx.xso))
  3428. revents |= SSL_POLL_EVENT_R;
  3429. if ((events & SSL_POLL_EVENT_ER) != 0
  3430. && test_poll_event_er(ctx.xso))
  3431. revents |= SSL_POLL_EVENT_ER;
  3432. if ((events & SSL_POLL_EVENT_W) != 0
  3433. && test_poll_event_w(ctx.xso))
  3434. revents |= SSL_POLL_EVENT_W;
  3435. if ((events & SSL_POLL_EVENT_EW) != 0
  3436. && test_poll_event_ew(ctx.xso))
  3437. revents |= SSL_POLL_EVENT_EW;
  3438. }
  3439. if (!ctx.is_stream) {
  3440. if ((events & SSL_POLL_EVENT_EC) != 0
  3441. && test_poll_event_ec(ctx.qc))
  3442. revents |= SSL_POLL_EVENT_EC;
  3443. if ((events & SSL_POLL_EVENT_ECD) != 0
  3444. && test_poll_event_ecd(ctx.qc))
  3445. revents |= SSL_POLL_EVENT_ECD;
  3446. if ((events & SSL_POLL_EVENT_ISB) != 0
  3447. && test_poll_event_is(ctx.qc, /*uni=*/0))
  3448. revents |= SSL_POLL_EVENT_ISB;
  3449. if ((events & SSL_POLL_EVENT_ISU) != 0
  3450. && test_poll_event_is(ctx.qc, /*uni=*/1))
  3451. revents |= SSL_POLL_EVENT_ISU;
  3452. if ((events & SSL_POLL_EVENT_OSB) != 0
  3453. && test_poll_event_os(ctx.qc, /*uni=*/0))
  3454. revents |= SSL_POLL_EVENT_OSB;
  3455. if ((events & SSL_POLL_EVENT_OSU) != 0
  3456. && test_poll_event_os(ctx.qc, /*uni=*/1))
  3457. revents |= SSL_POLL_EVENT_OSU;
  3458. }
  3459. end:
  3460. quic_unlock(ctx.qc);
  3461. *p_revents = revents;
  3462. return 1;
  3463. }
  3464. /*
  3465. * Internal Testing APIs
  3466. * =====================
  3467. */
  3468. QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
  3469. {
  3470. QCTX ctx;
  3471. if (!expect_quic_conn_only(s, &ctx))
  3472. return NULL;
  3473. return ctx.qc->ch;
  3474. }
  3475. int ossl_quic_set_diag_title(SSL_CTX *ctx, const char *title)
  3476. {
  3477. #ifndef OPENSSL_NO_QLOG
  3478. OPENSSL_free(ctx->qlog_title);
  3479. ctx->qlog_title = NULL;
  3480. if (title == NULL)
  3481. return 1;
  3482. if ((ctx->qlog_title = OPENSSL_strdup(title)) == NULL)
  3483. return 0;
  3484. #endif
  3485. return 1;
  3486. }