quic_impl.c 105 KB

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