curl_osslq.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
  26. #include <openssl/ssl.h>
  27. #include <openssl/bio.h>
  28. #include <openssl/err.h>
  29. #include <nghttp3/nghttp3.h>
  30. #include "urldata.h"
  31. #include "sendf.h"
  32. #include "strdup.h"
  33. #include "rand.h"
  34. #include "multiif.h"
  35. #include "strcase.h"
  36. #include "cfilters.h"
  37. #include "cf-socket.h"
  38. #include "connect.h"
  39. #include "progress.h"
  40. #include "strerror.h"
  41. #include "dynbuf.h"
  42. #include "http1.h"
  43. #include "select.h"
  44. #include "inet_pton.h"
  45. #include "vquic.h"
  46. #include "vquic_int.h"
  47. #include "vquic-tls.h"
  48. #include "vtls/keylog.h"
  49. #include "vtls/vtls.h"
  50. #include "vtls/openssl.h"
  51. #include "curl_osslq.h"
  52. #include "warnless.h"
  53. /* The last 3 #include files should be in this order */
  54. #include "curl_printf.h"
  55. #include "curl_memory.h"
  56. #include "memdebug.h"
  57. /* A stream window is the maximum amount we need to buffer for
  58. * each active transfer. We use HTTP/3 flow control and only ACK
  59. * when we take things out of the buffer.
  60. * Chunk size is large enough to take a full DATA frame */
  61. #define H3_STREAM_WINDOW_SIZE (128 * 1024)
  62. #define H3_STREAM_CHUNK_SIZE (16 * 1024)
  63. /* The pool keeps spares around and half of a full stream windows
  64. * seems good. More does not seem to improve performance.
  65. * The benefit of the pool is that stream buffer to not keep
  66. * spares. So memory consumption goes down when streams run empty,
  67. * have a large upload done, etc. */
  68. #define H3_STREAM_POOL_SPARES \
  69. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE ) / 2
  70. /* Receive and Send max number of chunks just follows from the
  71. * chunk size and window size */
  72. #define H3_STREAM_RECV_CHUNKS \
  73. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  74. #define H3_STREAM_SEND_CHUNKS \
  75. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  76. #ifndef ARRAYSIZE
  77. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  78. #endif
  79. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  80. typedef uint32_t sslerr_t;
  81. #else
  82. typedef unsigned long sslerr_t;
  83. #endif
  84. /* How to access `call_data` from a cf_osslq filter */
  85. #undef CF_CTX_CALL_DATA
  86. #define CF_CTX_CALL_DATA(cf) \
  87. ((struct cf_osslq_ctx *)(cf)->ctx)->call_data
  88. static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
  89. struct Curl_easy *data);
  90. static const char *SSL_ERROR_to_str(int err)
  91. {
  92. switch(err) {
  93. case SSL_ERROR_NONE:
  94. return "SSL_ERROR_NONE";
  95. case SSL_ERROR_SSL:
  96. return "SSL_ERROR_SSL";
  97. case SSL_ERROR_WANT_READ:
  98. return "SSL_ERROR_WANT_READ";
  99. case SSL_ERROR_WANT_WRITE:
  100. return "SSL_ERROR_WANT_WRITE";
  101. case SSL_ERROR_WANT_X509_LOOKUP:
  102. return "SSL_ERROR_WANT_X509_LOOKUP";
  103. case SSL_ERROR_SYSCALL:
  104. return "SSL_ERROR_SYSCALL";
  105. case SSL_ERROR_ZERO_RETURN:
  106. return "SSL_ERROR_ZERO_RETURN";
  107. case SSL_ERROR_WANT_CONNECT:
  108. return "SSL_ERROR_WANT_CONNECT";
  109. case SSL_ERROR_WANT_ACCEPT:
  110. return "SSL_ERROR_WANT_ACCEPT";
  111. #if defined(SSL_ERROR_WANT_ASYNC)
  112. case SSL_ERROR_WANT_ASYNC:
  113. return "SSL_ERROR_WANT_ASYNC";
  114. #endif
  115. #if defined(SSL_ERROR_WANT_ASYNC_JOB)
  116. case SSL_ERROR_WANT_ASYNC_JOB:
  117. return "SSL_ERROR_WANT_ASYNC_JOB";
  118. #endif
  119. #if defined(SSL_ERROR_WANT_EARLY)
  120. case SSL_ERROR_WANT_EARLY:
  121. return "SSL_ERROR_WANT_EARLY";
  122. #endif
  123. default:
  124. return "SSL_ERROR unknown";
  125. }
  126. }
  127. /* Return error string for last OpenSSL error */
  128. static char *ossl_strerror(unsigned long error, char *buf, size_t size)
  129. {
  130. DEBUGASSERT(size);
  131. *buf = '\0';
  132. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  133. ERR_error_string_n((uint32_t)error, buf, size);
  134. #else
  135. ERR_error_string_n(error, buf, size);
  136. #endif
  137. if(!*buf) {
  138. const char *msg = error ? "Unknown error" : "No error";
  139. if(strlen(msg) < size)
  140. strcpy(buf, msg);
  141. }
  142. return buf;
  143. }
  144. static CURLcode make_bio_addr(BIO_ADDR **pbio_addr,
  145. const struct Curl_sockaddr_ex *addr)
  146. {
  147. BIO_ADDR *ba;
  148. CURLcode result = CURLE_FAILED_INIT;
  149. ba = BIO_ADDR_new();
  150. if(!ba) {
  151. result = CURLE_OUT_OF_MEMORY;
  152. goto out;
  153. }
  154. switch(addr->family) {
  155. case AF_INET: {
  156. struct sockaddr_in * const sin =
  157. (struct sockaddr_in * const)(void *)&addr->sa_addr;
  158. if(!BIO_ADDR_rawmake(ba, AF_INET, &sin->sin_addr,
  159. sizeof(sin->sin_addr), sin->sin_port)) {
  160. goto out;
  161. }
  162. result = CURLE_OK;
  163. break;
  164. }
  165. #ifdef ENABLE_IPV6
  166. case AF_INET6: {
  167. struct sockaddr_in6 * const sin =
  168. (struct sockaddr_in6 * const)(void *)&addr->sa_addr;
  169. if(!BIO_ADDR_rawmake(ba, AF_INET6, &sin->sin6_addr,
  170. sizeof(sin->sin6_addr), sin->sin6_port)) {
  171. }
  172. result = CURLE_OK;
  173. break;
  174. }
  175. #endif /* ENABLE_IPV6 */
  176. default:
  177. /* sunsupported */
  178. DEBUGASSERT(0);
  179. break;
  180. }
  181. out:
  182. if(result && ba) {
  183. BIO_ADDR_free(ba);
  184. ba = NULL;
  185. }
  186. *pbio_addr = ba;
  187. return result;
  188. }
  189. /* QUIC stream (not necessarily H3) */
  190. struct cf_osslq_stream {
  191. int64_t id;
  192. SSL *ssl;
  193. struct bufq recvbuf; /* QUIC war data recv buffer */
  194. BIT(recvd_eos);
  195. BIT(closed);
  196. BIT(reset);
  197. BIT(send_blocked);
  198. };
  199. static CURLcode cf_osslq_stream_open(struct cf_osslq_stream *s,
  200. SSL *conn,
  201. uint64_t flags,
  202. struct bufc_pool *bufcp,
  203. void *user_data)
  204. {
  205. DEBUGASSERT(!s->ssl);
  206. Curl_bufq_initp(&s->recvbuf, bufcp, 1, BUFQ_OPT_NONE);
  207. s->ssl = SSL_new_stream(conn, flags);
  208. if(!s->ssl) {
  209. return CURLE_FAILED_INIT;
  210. }
  211. s->id = SSL_get_stream_id(s->ssl);
  212. SSL_set_app_data(s->ssl, user_data);
  213. return CURLE_OK;
  214. }
  215. static void cf_osslq_stream_cleanup(struct cf_osslq_stream *s)
  216. {
  217. if(s->ssl) {
  218. SSL_set_app_data(s->ssl, NULL);
  219. SSL_free(s->ssl);
  220. }
  221. Curl_bufq_free(&s->recvbuf);
  222. memset(s, 0, sizeof(*s));
  223. }
  224. static void cf_osslq_stream_close(struct cf_osslq_stream *s)
  225. {
  226. if(s->ssl) {
  227. SSL_free(s->ssl);
  228. s->ssl = NULL;
  229. }
  230. }
  231. struct cf_osslq_h3conn {
  232. nghttp3_conn *conn;
  233. nghttp3_settings settings;
  234. struct cf_osslq_stream s_ctrl;
  235. struct cf_osslq_stream s_qpack_enc;
  236. struct cf_osslq_stream s_qpack_dec;
  237. struct cf_osslq_stream remote_ctrl[3]; /* uni streams opened by the peer */
  238. size_t remote_ctrl_n; /* number of peer streams opened */
  239. };
  240. static void cf_osslq_h3conn_cleanup(struct cf_osslq_h3conn *h3)
  241. {
  242. size_t i;
  243. if(h3->conn)
  244. nghttp3_conn_del(h3->conn);
  245. cf_osslq_stream_cleanup(&h3->s_ctrl);
  246. cf_osslq_stream_cleanup(&h3->s_qpack_enc);
  247. cf_osslq_stream_cleanup(&h3->s_qpack_dec);
  248. for(i = 0; i < h3->remote_ctrl_n; ++i) {
  249. cf_osslq_stream_cleanup(&h3->remote_ctrl[i]);
  250. }
  251. }
  252. struct cf_osslq_ctx {
  253. struct cf_quic_ctx q;
  254. struct ssl_peer peer;
  255. struct quic_tls_ctx tls;
  256. struct cf_call_data call_data;
  257. struct cf_osslq_h3conn h3;
  258. struct curltime started_at; /* time the current attempt started */
  259. struct curltime handshake_at; /* time connect handshake finished */
  260. struct curltime first_byte_at; /* when first byte was recvd */
  261. struct curltime reconnect_at; /* time the next attempt should start */
  262. struct bufc_pool stream_bufcp; /* chunk pool for streams */
  263. size_t max_stream_window; /* max flow window for one stream */
  264. uint64_t max_idle_ms; /* max idle time for QUIC connection */
  265. BIT(got_first_byte); /* if first byte was received */
  266. #ifdef USE_OPENSSL
  267. BIT(x509_store_setup); /* if x509 store has been set up */
  268. BIT(protocol_shutdown); /* QUIC connection is shut down */
  269. #endif
  270. };
  271. static void cf_osslq_ctx_clear(struct cf_osslq_ctx *ctx)
  272. {
  273. struct cf_call_data save = ctx->call_data;
  274. cf_osslq_h3conn_cleanup(&ctx->h3);
  275. Curl_vquic_tls_cleanup(&ctx->tls);
  276. vquic_ctx_free(&ctx->q);
  277. Curl_bufcp_free(&ctx->stream_bufcp);
  278. Curl_ssl_peer_cleanup(&ctx->peer);
  279. memset(ctx, 0, sizeof(*ctx));
  280. ctx->call_data = save;
  281. }
  282. static void cf_osslq_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  283. {
  284. struct cf_osslq_ctx *ctx = cf->ctx;
  285. struct cf_call_data save;
  286. CF_DATA_SAVE(save, cf, data);
  287. if(ctx && ctx->tls.ssl) {
  288. /* TODO: send connection close */
  289. CURL_TRC_CF(data, cf, "cf_osslq_close()");
  290. cf_osslq_ctx_clear(ctx);
  291. }
  292. cf->connected = FALSE;
  293. CF_DATA_RESTORE(cf, save);
  294. }
  295. static void cf_osslq_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  296. {
  297. struct cf_osslq_ctx *ctx = cf->ctx;
  298. struct cf_call_data save;
  299. CF_DATA_SAVE(save, cf, data);
  300. CURL_TRC_CF(data, cf, "destroy");
  301. if(ctx) {
  302. CURL_TRC_CF(data, cf, "cf_osslq_destroy()");
  303. cf_osslq_ctx_clear(ctx);
  304. free(ctx);
  305. }
  306. cf->ctx = NULL;
  307. /* No CF_DATA_RESTORE(cf, save) possible */
  308. (void)save;
  309. }
  310. static CURLcode cf_osslq_h3conn_add_stream(struct cf_osslq_h3conn *h3,
  311. SSL *stream_ssl,
  312. struct Curl_cfilter *cf,
  313. struct Curl_easy *data)
  314. {
  315. struct cf_osslq_ctx *ctx = cf->ctx;
  316. int64_t stream_id = SSL_get_stream_id(stream_ssl);
  317. if(h3->remote_ctrl_n >= ARRAYSIZE(h3->remote_ctrl)) {
  318. /* rejected, we are full */
  319. CURL_TRC_CF(data, cf, "[%" PRId64 "] rejecting additional remote stream",
  320. stream_id);
  321. SSL_free(stream_ssl);
  322. return CURLE_FAILED_INIT;
  323. }
  324. switch(SSL_get_stream_type(stream_ssl)) {
  325. case SSL_STREAM_TYPE_READ: {
  326. struct cf_osslq_stream *nstream = &h3->remote_ctrl[h3->remote_ctrl_n++];
  327. nstream->id = stream_id;
  328. nstream->ssl = stream_ssl;
  329. Curl_bufq_initp(&nstream->recvbuf, &ctx->stream_bufcp, 1, BUFQ_OPT_NONE);
  330. CURL_TRC_CF(data, cf, "[%" PRId64 "] accepted new remote uni stream",
  331. stream_id);
  332. break;
  333. }
  334. default:
  335. CURL_TRC_CF(data, cf, "[%" PRId64 "] rejecting remote non-uni-read"
  336. " stream", stream_id);
  337. SSL_free(stream_ssl);
  338. return CURLE_FAILED_INIT;
  339. }
  340. return CURLE_OK;
  341. }
  342. static CURLcode cf_osslq_ssl_err(struct Curl_cfilter *cf,
  343. struct Curl_easy *data,
  344. int detail, CURLcode def_result)
  345. {
  346. struct cf_osslq_ctx *ctx = cf->ctx;
  347. CURLcode result = def_result;
  348. sslerr_t errdetail;
  349. char ebuf[256] = "unknown";
  350. const char *err_descr = ebuf;
  351. long lerr;
  352. int lib;
  353. int reason;
  354. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  355. errdetail = ERR_get_error();
  356. lib = ERR_GET_LIB(errdetail);
  357. reason = ERR_GET_REASON(errdetail);
  358. if((lib == ERR_LIB_SSL) &&
  359. ((reason == SSL_R_CERTIFICATE_VERIFY_FAILED) ||
  360. (reason == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED))) {
  361. result = CURLE_PEER_FAILED_VERIFICATION;
  362. lerr = SSL_get_verify_result(ctx->tls.ssl);
  363. if(lerr != X509_V_OK) {
  364. ssl_config->certverifyresult = lerr;
  365. msnprintf(ebuf, sizeof(ebuf),
  366. "SSL certificate problem: %s",
  367. X509_verify_cert_error_string(lerr));
  368. }
  369. else
  370. err_descr = "SSL certificate verification failed";
  371. }
  372. #if defined(SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED)
  373. /* SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED is only available on
  374. OpenSSL version above v1.1.1, not LibreSSL, BoringSSL, or AWS-LC */
  375. else if((lib == ERR_LIB_SSL) &&
  376. (reason == SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED)) {
  377. /* If client certificate is required, communicate the
  378. error to client */
  379. result = CURLE_SSL_CLIENTCERT;
  380. ossl_strerror(errdetail, ebuf, sizeof(ebuf));
  381. }
  382. #endif
  383. else if((lib == ERR_LIB_SSL) && (reason == SSL_R_PROTOCOL_IS_SHUTDOWN)) {
  384. ctx->protocol_shutdown = TRUE;
  385. err_descr = "QUIC connectin has been shut down";
  386. result = def_result;
  387. }
  388. else {
  389. result = def_result;
  390. ossl_strerror(errdetail, ebuf, sizeof(ebuf));
  391. }
  392. /* detail is already set to the SSL error above */
  393. /* If we e.g. use SSLv2 request-method and the server doesn't like us
  394. * (RST connection, etc.), OpenSSL gives no explanation whatsoever and
  395. * the SO_ERROR is also lost.
  396. */
  397. if(CURLE_SSL_CONNECT_ERROR == result && errdetail == 0) {
  398. char extramsg[80]="";
  399. int sockerr = SOCKERRNO;
  400. const char *r_ip = NULL;
  401. int r_port = 0;
  402. Curl_cf_socket_peek(cf->next, data, NULL, NULL,
  403. &r_ip, &r_port, NULL, NULL);
  404. if(sockerr && detail == SSL_ERROR_SYSCALL)
  405. Curl_strerror(sockerr, extramsg, sizeof(extramsg));
  406. failf(data, "QUIC connect: %s in connection to %s:%d (%s)",
  407. extramsg[0] ? extramsg : SSL_ERROR_to_str(detail),
  408. ctx->peer.dispname, r_port, r_ip);
  409. }
  410. else {
  411. /* Could be a CERT problem */
  412. failf(data, "%s", err_descr);
  413. }
  414. return result;
  415. }
  416. static CURLcode cf_osslq_verify_peer(struct Curl_cfilter *cf,
  417. struct Curl_easy *data)
  418. {
  419. struct cf_osslq_ctx *ctx = cf->ctx;
  420. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  421. cf->conn->httpversion = 30;
  422. cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  423. return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
  424. }
  425. /**
  426. * All about the H3 internals of a stream
  427. */
  428. struct h3_stream_ctx {
  429. struct cf_osslq_stream s;
  430. struct bufq sendbuf; /* h3 request body */
  431. struct bufq recvbuf; /* h3 response body */
  432. struct h1_req_parser h1; /* h1 request parsing */
  433. size_t sendbuf_len_in_flight; /* sendbuf amount "in flight" */
  434. size_t upload_blocked_len; /* the amount written last and EGAINed */
  435. size_t recv_buf_nonflow; /* buffered bytes, not counting for flow control */
  436. uint64_t error3; /* HTTP/3 stream error code */
  437. curl_off_t upload_left; /* number of request bytes left to upload */
  438. curl_off_t download_recvd; /* number of response DATA bytes received */
  439. int status_code; /* HTTP status code */
  440. bool resp_hds_complete; /* we have a complete, final response */
  441. bool closed; /* TRUE on stream close */
  442. bool reset; /* TRUE on stream reset */
  443. bool send_closed; /* stream is local closed */
  444. BIT(quic_flow_blocked); /* stream is blocked by QUIC flow control */
  445. };
  446. #define H3_STREAM_CTX(d) ((struct h3_stream_ctx *)(((d) && (d)->req.p.http)? \
  447. ((struct HTTP *)(d)->req.p.http)->h3_ctx \
  448. : NULL))
  449. #define H3_STREAM_LCTX(d) ((struct HTTP *)(d)->req.p.http)->h3_ctx
  450. #define H3_STREAM_ID(d) (H3_STREAM_CTX(d)? \
  451. H3_STREAM_CTX(d)->s.id : -2)
  452. static CURLcode h3_data_setup(struct Curl_cfilter *cf,
  453. struct Curl_easy *data)
  454. {
  455. struct cf_osslq_ctx *ctx = cf->ctx;
  456. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  457. if(!data || !data->req.p.http) {
  458. failf(data, "initialization failure, transfer not http initialized");
  459. return CURLE_FAILED_INIT;
  460. }
  461. if(stream)
  462. return CURLE_OK;
  463. stream = calloc(1, sizeof(*stream));
  464. if(!stream)
  465. return CURLE_OUT_OF_MEMORY;
  466. stream->s.id = -1;
  467. /* on send, we control how much we put into the buffer */
  468. Curl_bufq_initp(&stream->sendbuf, &ctx->stream_bufcp,
  469. H3_STREAM_SEND_CHUNKS, BUFQ_OPT_NONE);
  470. stream->sendbuf_len_in_flight = 0;
  471. /* on recv, we need a flexible buffer limit since we also write
  472. * headers to it that are not counted against the nghttp3 flow limits. */
  473. Curl_bufq_initp(&stream->recvbuf, &ctx->stream_bufcp,
  474. H3_STREAM_RECV_CHUNKS, BUFQ_OPT_SOFT_LIMIT);
  475. stream->recv_buf_nonflow = 0;
  476. Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
  477. H3_STREAM_LCTX(data) = stream;
  478. return CURLE_OK;
  479. }
  480. static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
  481. {
  482. struct cf_osslq_ctx *ctx = cf->ctx;
  483. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  484. (void)cf;
  485. if(stream) {
  486. CURL_TRC_CF(data, cf, "[%"PRId64"] easy handle is done", stream->s.id);
  487. if(ctx->h3.conn && !stream->closed) {
  488. nghttp3_conn_shutdown_stream_read(ctx->h3.conn, stream->s.id);
  489. nghttp3_conn_close_stream(ctx->h3.conn, stream->s.id,
  490. NGHTTP3_H3_REQUEST_CANCELLED);
  491. nghttp3_conn_set_stream_user_data(ctx->h3.conn, stream->s.id, NULL);
  492. stream->closed = TRUE;
  493. }
  494. cf_osslq_stream_cleanup(&stream->s);
  495. Curl_bufq_free(&stream->sendbuf);
  496. Curl_bufq_free(&stream->recvbuf);
  497. Curl_h1_req_parse_free(&stream->h1);
  498. free(stream);
  499. H3_STREAM_LCTX(data) = NULL;
  500. }
  501. }
  502. static struct cf_osslq_stream *cf_osslq_get_qstream(struct Curl_cfilter *cf,
  503. struct Curl_easy *data,
  504. int64_t stream_id)
  505. {
  506. struct cf_osslq_ctx *ctx = cf->ctx;
  507. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  508. struct Curl_easy *sdata;
  509. if(stream && stream->s.id == stream_id) {
  510. return &stream->s;
  511. }
  512. else if(ctx->h3.s_ctrl.id == stream_id) {
  513. return &ctx->h3.s_ctrl;
  514. }
  515. else if(ctx->h3.s_qpack_enc.id == stream_id) {
  516. return &ctx->h3.s_qpack_enc;
  517. }
  518. else if(ctx->h3.s_qpack_dec.id == stream_id) {
  519. return &ctx->h3.s_qpack_dec;
  520. }
  521. else {
  522. DEBUGASSERT(data->multi);
  523. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  524. if((sdata->conn == data->conn) && H3_STREAM_ID(sdata) == stream_id) {
  525. stream = H3_STREAM_CTX(sdata);
  526. return stream? &stream->s : NULL;
  527. }
  528. }
  529. }
  530. return NULL;
  531. }
  532. static void h3_drain_stream(struct Curl_cfilter *cf,
  533. struct Curl_easy *data)
  534. {
  535. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  536. unsigned char bits;
  537. (void)cf;
  538. bits = CURL_CSELECT_IN;
  539. if(stream && stream->upload_left && !stream->send_closed)
  540. bits |= CURL_CSELECT_OUT;
  541. if(data->state.select_bits != bits) {
  542. data->state.select_bits = bits;
  543. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  544. }
  545. }
  546. static CURLcode h3_data_pause(struct Curl_cfilter *cf,
  547. struct Curl_easy *data,
  548. bool pause)
  549. {
  550. if(!pause) {
  551. /* unpaused. make it run again right away */
  552. h3_drain_stream(cf, data);
  553. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  554. }
  555. return CURLE_OK;
  556. }
  557. static int cb_h3_stream_close(nghttp3_conn *conn, int64_t stream_id,
  558. uint64_t app_error_code, void *user_data,
  559. void *stream_user_data)
  560. {
  561. struct Curl_cfilter *cf = user_data;
  562. struct Curl_easy *data = stream_user_data;
  563. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  564. (void)conn;
  565. (void)stream_id;
  566. /* we might be called by nghttp3 after we already cleaned up */
  567. if(!stream)
  568. return 0;
  569. stream->closed = TRUE;
  570. stream->error3 = app_error_code;
  571. if(stream->error3 != NGHTTP3_H3_NO_ERROR) {
  572. stream->reset = TRUE;
  573. stream->send_closed = TRUE;
  574. CURL_TRC_CF(data, cf, "[%" PRId64 "] RESET: error %" PRId64,
  575. stream->s.id, stream->error3);
  576. }
  577. else {
  578. CURL_TRC_CF(data, cf, "[%" PRId64 "] CLOSED", stream->s.id);
  579. }
  580. h3_drain_stream(cf, data);
  581. return 0;
  582. }
  583. /*
  584. * write_resp_raw() copies response data in raw format to the `data`'s
  585. * receive buffer. If not enough space is available, it appends to the
  586. * `data`'s overflow buffer.
  587. */
  588. static CURLcode write_resp_raw(struct Curl_cfilter *cf,
  589. struct Curl_easy *data,
  590. const void *mem, size_t memlen,
  591. bool flow)
  592. {
  593. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  594. CURLcode result = CURLE_OK;
  595. ssize_t nwritten;
  596. (void)cf;
  597. if(!stream) {
  598. return CURLE_RECV_ERROR;
  599. }
  600. nwritten = Curl_bufq_write(&stream->recvbuf, mem, memlen, &result);
  601. if(nwritten < 0) {
  602. return result;
  603. }
  604. if(!flow)
  605. stream->recv_buf_nonflow += (size_t)nwritten;
  606. if((size_t)nwritten < memlen) {
  607. /* This MUST not happen. Our recbuf is dimensioned to hold the
  608. * full max_stream_window and then some for this very reason. */
  609. DEBUGASSERT(0);
  610. return CURLE_RECV_ERROR;
  611. }
  612. return result;
  613. }
  614. static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream3_id,
  615. const uint8_t *buf, size_t buflen,
  616. void *user_data, void *stream_user_data)
  617. {
  618. struct Curl_cfilter *cf = user_data;
  619. struct Curl_easy *data = stream_user_data;
  620. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  621. CURLcode result;
  622. (void)conn;
  623. (void)stream3_id;
  624. if(!stream)
  625. return NGHTTP3_ERR_CALLBACK_FAILURE;
  626. result = write_resp_raw(cf, data, buf, buflen, TRUE);
  627. if(result) {
  628. CURL_TRC_CF(data, cf, "[%" PRId64 "] DATA len=%zu, ERROR receiving %d",
  629. stream->s.id, buflen, result);
  630. return NGHTTP3_ERR_CALLBACK_FAILURE;
  631. }
  632. stream->download_recvd += (curl_off_t)buflen;
  633. CURL_TRC_CF(data, cf, "[%" PRId64 "] DATA len=%zu, total=%zd",
  634. stream->s.id, buflen, stream->download_recvd);
  635. h3_drain_stream(cf, data);
  636. return 0;
  637. }
  638. static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream_id,
  639. size_t consumed, void *user_data,
  640. void *stream_user_data)
  641. {
  642. struct Curl_cfilter *cf = user_data;
  643. struct Curl_easy *data = stream_user_data;
  644. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  645. (void)conn;
  646. (void)stream_id;
  647. if(stream)
  648. CURL_TRC_CF(data, cf, "[%" PRId64 "] deferred consume %zu bytes",
  649. stream->s.id, consumed);
  650. return 0;
  651. }
  652. static int cb_h3_recv_header(nghttp3_conn *conn, int64_t stream_id,
  653. int32_t token, nghttp3_rcbuf *name,
  654. nghttp3_rcbuf *value, uint8_t flags,
  655. void *user_data, void *stream_user_data)
  656. {
  657. struct Curl_cfilter *cf = user_data;
  658. nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name);
  659. nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value);
  660. struct Curl_easy *data = stream_user_data;
  661. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  662. CURLcode result = CURLE_OK;
  663. (void)conn;
  664. (void)stream_id;
  665. (void)token;
  666. (void)flags;
  667. (void)cf;
  668. /* we might have cleaned up this transfer already */
  669. if(!stream)
  670. return 0;
  671. if(token == NGHTTP3_QPACK_TOKEN__STATUS) {
  672. char line[14]; /* status line is always 13 characters long */
  673. size_t ncopy;
  674. result = Curl_http_decode_status(&stream->status_code,
  675. (const char *)h3val.base, h3val.len);
  676. if(result)
  677. return -1;
  678. ncopy = msnprintf(line, sizeof(line), "HTTP/3 %03d \r\n",
  679. stream->status_code);
  680. CURL_TRC_CF(data, cf, "[%" PRId64 "] status: %s", stream_id, line);
  681. result = write_resp_raw(cf, data, line, ncopy, FALSE);
  682. if(result) {
  683. return -1;
  684. }
  685. }
  686. else {
  687. /* store as an HTTP1-style header */
  688. CURL_TRC_CF(data, cf, "[%" PRId64 "] header: %.*s: %.*s",
  689. stream_id, (int)h3name.len, h3name.base,
  690. (int)h3val.len, h3val.base);
  691. result = write_resp_raw(cf, data, h3name.base, h3name.len, FALSE);
  692. if(result) {
  693. return -1;
  694. }
  695. result = write_resp_raw(cf, data, ": ", 2, FALSE);
  696. if(result) {
  697. return -1;
  698. }
  699. result = write_resp_raw(cf, data, h3val.base, h3val.len, FALSE);
  700. if(result) {
  701. return -1;
  702. }
  703. result = write_resp_raw(cf, data, "\r\n", 2, FALSE);
  704. if(result) {
  705. return -1;
  706. }
  707. }
  708. return 0;
  709. }
  710. static int cb_h3_end_headers(nghttp3_conn *conn, int64_t stream_id,
  711. int fin, void *user_data, void *stream_user_data)
  712. {
  713. struct Curl_cfilter *cf = user_data;
  714. struct Curl_easy *data = stream_user_data;
  715. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  716. CURLcode result = CURLE_OK;
  717. (void)conn;
  718. (void)stream_id;
  719. (void)fin;
  720. (void)cf;
  721. if(!stream)
  722. return 0;
  723. /* add a CRLF only if we've received some headers */
  724. result = write_resp_raw(cf, data, "\r\n", 2, FALSE);
  725. if(result) {
  726. return -1;
  727. }
  728. CURL_TRC_CF(data, cf, "[%" PRId64 "] end_headers, status=%d",
  729. stream_id, stream->status_code);
  730. if(stream->status_code / 100 != 1) {
  731. stream->resp_hds_complete = TRUE;
  732. }
  733. h3_drain_stream(cf, data);
  734. return 0;
  735. }
  736. static int cb_h3_stop_sending(nghttp3_conn *conn, int64_t stream_id,
  737. uint64_t app_error_code, void *user_data,
  738. void *stream_user_data)
  739. {
  740. struct Curl_cfilter *cf = user_data;
  741. struct Curl_easy *data = stream_user_data;
  742. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  743. (void)conn;
  744. (void)app_error_code;
  745. if(!stream || !stream->s.ssl)
  746. return 0;
  747. CURL_TRC_CF(data, cf, "[%" PRId64 "] stop_sending", stream_id);
  748. cf_osslq_stream_close(&stream->s);
  749. return 0;
  750. }
  751. static int cb_h3_reset_stream(nghttp3_conn *conn, int64_t stream_id,
  752. uint64_t app_error_code, void *user_data,
  753. void *stream_user_data) {
  754. struct Curl_cfilter *cf = user_data;
  755. struct Curl_easy *data = stream_user_data;
  756. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  757. int rv;
  758. (void)conn;
  759. if(stream && stream->s.ssl) {
  760. SSL_STREAM_RESET_ARGS args = {0};
  761. args.quic_error_code = app_error_code;
  762. rv = !SSL_stream_reset(stream->s.ssl, &args, sizeof(args));
  763. CURL_TRC_CF(data, cf, "[%" PRId64 "] reset -> %d", stream_id, rv);
  764. if(!rv) {
  765. return NGHTTP3_ERR_CALLBACK_FAILURE;
  766. }
  767. }
  768. return 0;
  769. }
  770. static nghttp3_ssize
  771. cb_h3_read_req_body(nghttp3_conn *conn, int64_t stream_id,
  772. nghttp3_vec *vec, size_t veccnt,
  773. uint32_t *pflags, void *user_data,
  774. void *stream_user_data)
  775. {
  776. struct Curl_cfilter *cf = user_data;
  777. struct Curl_easy *data = stream_user_data;
  778. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  779. ssize_t nwritten = 0;
  780. size_t nvecs = 0;
  781. (void)cf;
  782. (void)conn;
  783. (void)stream_id;
  784. (void)user_data;
  785. (void)veccnt;
  786. if(!stream)
  787. return NGHTTP3_ERR_CALLBACK_FAILURE;
  788. /* nghttp3 keeps references to the sendbuf data until it is ACKed
  789. * by the server (see `cb_h3_acked_req_body()` for updates).
  790. * `sendbuf_len_in_flight` is the amount of bytes in `sendbuf`
  791. * that we have already passed to nghttp3, but which have not been
  792. * ACKed yet.
  793. * Any amount beyond `sendbuf_len_in_flight` we need still to pass
  794. * to nghttp3. Do that now, if we can. */
  795. if(stream->sendbuf_len_in_flight < Curl_bufq_len(&stream->sendbuf)) {
  796. nvecs = 0;
  797. while(nvecs < veccnt &&
  798. Curl_bufq_peek_at(&stream->sendbuf,
  799. stream->sendbuf_len_in_flight,
  800. (const unsigned char **)&vec[nvecs].base,
  801. &vec[nvecs].len)) {
  802. stream->sendbuf_len_in_flight += vec[nvecs].len;
  803. nwritten += vec[nvecs].len;
  804. ++nvecs;
  805. }
  806. DEBUGASSERT(nvecs > 0); /* we SHOULD have been be able to peek */
  807. }
  808. if(nwritten > 0 && stream->upload_left != -1)
  809. stream->upload_left -= nwritten;
  810. /* When we stopped sending and everything in `sendbuf` is "in flight",
  811. * we are at the end of the request body. */
  812. if(stream->upload_left == 0) {
  813. *pflags = NGHTTP3_DATA_FLAG_EOF;
  814. stream->send_closed = TRUE;
  815. }
  816. else if(!nwritten) {
  817. /* Not EOF, and nothing to give, we signal WOULDBLOCK. */
  818. CURL_TRC_CF(data, cf, "[%" PRId64 "] read req body -> AGAIN",
  819. stream->s.id);
  820. return NGHTTP3_ERR_WOULDBLOCK;
  821. }
  822. CURL_TRC_CF(data, cf, "[%" PRId64 "] read req body -> "
  823. "%d vecs%s with %zu (buffered=%zu, left=%"
  824. CURL_FORMAT_CURL_OFF_T ")",
  825. stream->s.id, (int)nvecs,
  826. *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"",
  827. nwritten, Curl_bufq_len(&stream->sendbuf),
  828. stream->upload_left);
  829. return (nghttp3_ssize)nvecs;
  830. }
  831. static int cb_h3_acked_stream_data(nghttp3_conn *conn, int64_t stream_id,
  832. uint64_t datalen, void *user_data,
  833. void *stream_user_data)
  834. {
  835. struct Curl_cfilter *cf = user_data;
  836. struct Curl_easy *data = stream_user_data;
  837. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  838. size_t skiplen;
  839. (void)cf;
  840. if(!stream)
  841. return 0;
  842. /* The server acknowledged `datalen` of bytes from our request body.
  843. * This is a delta. We have kept this data in `sendbuf` for
  844. * re-transmissions and can free it now. */
  845. if(datalen >= (uint64_t)stream->sendbuf_len_in_flight)
  846. skiplen = stream->sendbuf_len_in_flight;
  847. else
  848. skiplen = (size_t)datalen;
  849. Curl_bufq_skip(&stream->sendbuf, skiplen);
  850. stream->sendbuf_len_in_flight -= skiplen;
  851. /* Everything ACKed, we resume upload processing */
  852. if(!stream->sendbuf_len_in_flight) {
  853. int rv = nghttp3_conn_resume_stream(conn, stream_id);
  854. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  855. return NGHTTP3_ERR_CALLBACK_FAILURE;
  856. }
  857. }
  858. return 0;
  859. }
  860. static nghttp3_callbacks ngh3_callbacks = {
  861. cb_h3_acked_stream_data,
  862. cb_h3_stream_close,
  863. cb_h3_recv_data,
  864. cb_h3_deferred_consume,
  865. NULL, /* begin_headers */
  866. cb_h3_recv_header,
  867. cb_h3_end_headers,
  868. NULL, /* begin_trailers */
  869. cb_h3_recv_header,
  870. NULL, /* end_trailers */
  871. cb_h3_stop_sending,
  872. NULL, /* end_stream */
  873. cb_h3_reset_stream,
  874. NULL, /* shutdown */
  875. NULL /* recv_settings */
  876. };
  877. static CURLcode cf_osslq_h3conn_init(struct cf_osslq_ctx *ctx, SSL *conn,
  878. void *user_data)
  879. {
  880. struct cf_osslq_h3conn *h3 = &ctx->h3;
  881. CURLcode result;
  882. int rc;
  883. nghttp3_settings_default(&h3->settings);
  884. rc = nghttp3_conn_client_new(&h3->conn,
  885. &ngh3_callbacks,
  886. &h3->settings,
  887. nghttp3_mem_default(),
  888. user_data);
  889. if(rc) {
  890. result = CURLE_OUT_OF_MEMORY;
  891. goto out;
  892. }
  893. result = cf_osslq_stream_open(&h3->s_ctrl, conn,
  894. SSL_STREAM_FLAG_ADVANCE|SSL_STREAM_FLAG_UNI,
  895. &ctx->stream_bufcp, NULL);
  896. if(result) {
  897. result = CURLE_QUIC_CONNECT_ERROR;
  898. goto out;
  899. }
  900. result = cf_osslq_stream_open(&h3->s_qpack_enc, conn,
  901. SSL_STREAM_FLAG_ADVANCE|SSL_STREAM_FLAG_UNI,
  902. &ctx->stream_bufcp, NULL);
  903. if(result) {
  904. result = CURLE_QUIC_CONNECT_ERROR;
  905. goto out;
  906. }
  907. result = cf_osslq_stream_open(&h3->s_qpack_dec, conn,
  908. SSL_STREAM_FLAG_ADVANCE|SSL_STREAM_FLAG_UNI,
  909. &ctx->stream_bufcp, NULL);
  910. if(result) {
  911. result = CURLE_QUIC_CONNECT_ERROR;
  912. goto out;
  913. }
  914. rc = nghttp3_conn_bind_control_stream(h3->conn, h3->s_ctrl.id);
  915. if(rc) {
  916. result = CURLE_QUIC_CONNECT_ERROR;
  917. goto out;
  918. }
  919. rc = nghttp3_conn_bind_qpack_streams(h3->conn, h3->s_qpack_enc.id,
  920. h3->s_qpack_dec.id);
  921. if(rc) {
  922. result = CURLE_QUIC_CONNECT_ERROR;
  923. goto out;
  924. }
  925. result = CURLE_OK;
  926. out:
  927. return result;
  928. }
  929. static CURLcode cf_osslq_ctx_start(struct Curl_cfilter *cf,
  930. struct Curl_easy *data)
  931. {
  932. struct cf_osslq_ctx *ctx = cf->ctx;
  933. CURLcode result;
  934. int rv;
  935. const struct Curl_sockaddr_ex *peer_addr = NULL;
  936. int peer_port;
  937. BIO *bio = NULL;
  938. BIO_ADDR *baddr = NULL;
  939. Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
  940. H3_STREAM_POOL_SPARES);
  941. result = Curl_ssl_peer_init(&ctx->peer, cf);
  942. if(result)
  943. goto out;
  944. #define H3_ALPN "\x2h3"
  945. result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->peer,
  946. H3_ALPN, sizeof(H3_ALPN) - 1,
  947. NULL, NULL);
  948. if(result)
  949. goto out;
  950. result = vquic_ctx_init(&ctx->q);
  951. if(result)
  952. goto out;
  953. result = CURLE_QUIC_CONNECT_ERROR;
  954. Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd,
  955. &peer_addr, NULL, &peer_port, NULL, NULL);
  956. if(!peer_addr)
  957. goto out;
  958. ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
  959. rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
  960. &ctx->q.local_addrlen);
  961. if(rv == -1)
  962. goto out;
  963. result = make_bio_addr(&baddr, peer_addr);
  964. if(result) {
  965. failf(data, "error creating BIO_ADDR from sockaddr");
  966. goto out;
  967. }
  968. bio = BIO_new_dgram(ctx->q.sockfd, BIO_NOCLOSE);
  969. if(!bio) {
  970. result = CURLE_OUT_OF_MEMORY;
  971. goto out;
  972. }
  973. if(!SSL_set1_initial_peer_addr(ctx->tls.ssl, baddr)) {
  974. failf(data, "failed to set the initial peer address");
  975. result = CURLE_FAILED_INIT;
  976. goto out;
  977. }
  978. if(!SSL_set_blocking_mode(ctx->tls.ssl, 0)) {
  979. failf(data, "failed to turn off blocking mode");
  980. result = CURLE_FAILED_INIT;
  981. goto out;
  982. }
  983. SSL_set_bio(ctx->tls.ssl, bio, bio);
  984. bio = NULL;
  985. SSL_set_connect_state(ctx->tls.ssl);
  986. SSL_set_incoming_stream_policy(ctx->tls.ssl,
  987. SSL_INCOMING_STREAM_POLICY_ACCEPT, 0);
  988. /* setup the H3 things on top of the QUIC connection */
  989. result = cf_osslq_h3conn_init(ctx, ctx->tls.ssl, cf);
  990. out:
  991. if(bio)
  992. BIO_free(bio);
  993. if(baddr)
  994. BIO_ADDR_free(baddr);
  995. CURL_TRC_CF(data, cf, "QUIC tls init -> %d", result);
  996. return result;
  997. }
  998. struct h3_quic_recv_ctx {
  999. struct Curl_cfilter *cf;
  1000. struct Curl_easy *data;
  1001. struct cf_osslq_stream *s;
  1002. };
  1003. static ssize_t h3_quic_recv(void *reader_ctx,
  1004. unsigned char *buf, size_t len,
  1005. CURLcode *err)
  1006. {
  1007. struct h3_quic_recv_ctx *x = reader_ctx;
  1008. size_t nread;
  1009. int rv;
  1010. *err = CURLE_OK;
  1011. rv = SSL_read_ex(x->s->ssl, buf, len, &nread);
  1012. if(rv <= 0) {
  1013. int detail = SSL_get_error(x->s->ssl, rv);
  1014. if(detail == SSL_ERROR_WANT_READ || detail == SSL_ERROR_WANT_WRITE) {
  1015. *err = CURLE_AGAIN;
  1016. return -1;
  1017. }
  1018. else if(detail == SSL_ERROR_ZERO_RETURN) {
  1019. CURL_TRC_CF(x->data, x->cf, "[%" PRId64 "] h3_quic_recv -> EOS",
  1020. x->s->id);
  1021. x->s->recvd_eos = TRUE;
  1022. return 0;
  1023. }
  1024. else if(SSL_get_stream_read_state(x->s->ssl) ==
  1025. SSL_STREAM_STATE_RESET_REMOTE) {
  1026. uint64_t app_error_code = NGHTTP3_H3_NO_ERROR;
  1027. SSL_get_stream_read_error_code(x->s->ssl, &app_error_code);
  1028. CURL_TRC_CF(x->data, x->cf, "[%" PRId64 "] h3_quic_recv -> RESET, "
  1029. "rv=%d, app_err=%" PRIu64,
  1030. x->s->id, rv, app_error_code);
  1031. if(app_error_code != NGHTTP3_H3_NO_ERROR) {
  1032. x->s->reset = TRUE;
  1033. }
  1034. x->s->recvd_eos = TRUE;
  1035. return 0;
  1036. }
  1037. else {
  1038. *err = cf_osslq_ssl_err(x->cf, x->data, detail, CURLE_RECV_ERROR);
  1039. return -1;
  1040. }
  1041. }
  1042. else {
  1043. /* CURL_TRC_CF(x->data, x->cf, "[%" PRId64 "] h3_quic_recv -> %zu bytes",
  1044. x->s->id, nread); */
  1045. }
  1046. return (ssize_t)nread;
  1047. }
  1048. static CURLcode cf_osslq_stream_recv(struct cf_osslq_stream *s,
  1049. struct Curl_cfilter *cf,
  1050. struct Curl_easy *data)
  1051. {
  1052. struct cf_osslq_ctx *ctx = cf->ctx;
  1053. CURLcode result = CURLE_OK;
  1054. ssize_t nread;
  1055. struct h3_quic_recv_ctx x;
  1056. int rv, eagain = FALSE;
  1057. size_t total_recv_len = 0;
  1058. DEBUGASSERT(s);
  1059. if(s->closed)
  1060. return CURLE_OK;
  1061. x.cf = cf;
  1062. x.data = data;
  1063. x.s = s;
  1064. while(s->ssl && !s->closed && !eagain &&
  1065. (total_recv_len < H3_STREAM_CHUNK_SIZE)) {
  1066. if(Curl_bufq_is_empty(&s->recvbuf) && !s->recvd_eos) {
  1067. while(!eagain && !s->recvd_eos && !Curl_bufq_is_full(&s->recvbuf)) {
  1068. nread = Curl_bufq_sipn(&s->recvbuf, 0, h3_quic_recv, &x, &result);
  1069. if(nread < 0) {
  1070. if(result != CURLE_AGAIN)
  1071. goto out;
  1072. result = CURLE_OK;
  1073. eagain = TRUE;
  1074. }
  1075. }
  1076. }
  1077. /* Forward what we have to nghttp3 */
  1078. if(!Curl_bufq_is_empty(&s->recvbuf)) {
  1079. const unsigned char *buf;
  1080. size_t blen;
  1081. while(Curl_bufq_peek(&s->recvbuf, &buf, &blen)) {
  1082. nread = nghttp3_conn_read_stream(ctx->h3.conn, s->id,
  1083. buf, blen, 0);
  1084. CURL_TRC_CF(data, cf, "[%" PRId64 "] forward %zu bytes "
  1085. "to nghttp3 -> %zd", s->id, blen, nread);
  1086. if(nread < 0) {
  1087. failf(data, "nghttp3_conn_read_stream(len=%zu) error: %s",
  1088. blen, nghttp3_strerror((int)nread));
  1089. result = CURLE_RECV_ERROR;
  1090. goto out;
  1091. }
  1092. /* success, `nread` is the flow for QUIC to count as "consumed",
  1093. * not sure how that will work with OpenSSL. Anyways, without error,
  1094. * all data that we passed is not owned by nghttp3. */
  1095. Curl_bufq_skip(&s->recvbuf, blen);
  1096. total_recv_len += blen;
  1097. }
  1098. }
  1099. /* When we forwarded everything, handle RESET/EOS */
  1100. if(Curl_bufq_is_empty(&s->recvbuf) && !s->closed) {
  1101. result = CURLE_OK;
  1102. if(s->reset) {
  1103. uint64_t app_error;
  1104. if(!SSL_get_stream_read_error_code(s->ssl, &app_error)) {
  1105. failf(data, "SSL_get_stream_read_error_code returned error");
  1106. result = CURLE_RECV_ERROR;
  1107. goto out;
  1108. }
  1109. rv = nghttp3_conn_close_stream(ctx->h3.conn, s->id, app_error);
  1110. s->closed = TRUE;
  1111. if(rv < 0 && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1112. failf(data, "nghttp3_conn_close_stream returned error: %s",
  1113. nghttp3_strerror(rv));
  1114. result = CURLE_RECV_ERROR;
  1115. goto out;
  1116. }
  1117. }
  1118. else if(s->recvd_eos) {
  1119. rv = nghttp3_conn_close_stream(ctx->h3.conn, s->id,
  1120. NGHTTP3_H3_NO_ERROR);
  1121. s->closed = TRUE;
  1122. CURL_TRC_CF(data, cf, "[%" PRId64 "] close nghttp3 stream -> %d",
  1123. s->id, rv);
  1124. if(rv < 0 && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1125. failf(data, "nghttp3_conn_close_stream returned error: %s",
  1126. nghttp3_strerror(rv));
  1127. result = CURLE_RECV_ERROR;
  1128. goto out;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. out:
  1134. if(result)
  1135. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_osslq_stream_recv -> %d",
  1136. s->id, result);
  1137. return result;
  1138. }
  1139. static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
  1140. struct Curl_easy *data)
  1141. {
  1142. struct cf_osslq_ctx *ctx = cf->ctx;
  1143. CURLcode result = CURLE_OK;
  1144. if(!ctx->tls.ssl)
  1145. goto out;
  1146. ERR_clear_error();
  1147. /* 1. Check for new incoming streams */
  1148. while(1) {
  1149. SSL *snew = SSL_accept_stream(ctx->tls.ssl, SSL_ACCEPT_STREAM_NO_BLOCK);
  1150. if(!snew)
  1151. break;
  1152. (void)cf_osslq_h3conn_add_stream(&ctx->h3, snew, cf, data);
  1153. }
  1154. if(!SSL_handle_events(ctx->tls.ssl)) {
  1155. int detail = SSL_get_error(ctx->tls.ssl, 0);
  1156. result = cf_osslq_ssl_err(cf, data, detail, CURLE_RECV_ERROR);
  1157. }
  1158. if(ctx->h3.conn) {
  1159. size_t i;
  1160. for(i = 0; i < ctx->h3.remote_ctrl_n; ++i) {
  1161. result = cf_osslq_stream_recv(&ctx->h3.remote_ctrl[i], cf, data);
  1162. if(result)
  1163. goto out;
  1164. }
  1165. }
  1166. if(ctx->h3.conn) {
  1167. struct Curl_easy *sdata;
  1168. struct h3_stream_ctx *stream;
  1169. /* PULL all open streams */
  1170. DEBUGASSERT(data->multi);
  1171. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  1172. if(sdata->conn == data->conn && CURL_WANT_RECV(sdata)) {
  1173. stream = H3_STREAM_CTX(sdata);
  1174. if(stream && !stream->closed &&
  1175. !Curl_bufq_is_full(&stream->recvbuf)) {
  1176. result = cf_osslq_stream_recv(&stream->s, cf, sdata);
  1177. if(result)
  1178. goto out;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. out:
  1184. CURL_TRC_CF(data, cf, "progress_ingress -> %d", result);
  1185. return result;
  1186. }
  1187. /* Iterate over all streams and check if blocked can be unblocked */
  1188. static CURLcode cf_osslq_check_and_unblock(struct Curl_cfilter *cf,
  1189. struct Curl_easy *data)
  1190. {
  1191. struct cf_osslq_ctx *ctx = cf->ctx;
  1192. struct Curl_easy *sdata;
  1193. struct h3_stream_ctx *stream;
  1194. if(ctx->h3.conn) {
  1195. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  1196. if(sdata->conn == data->conn) {
  1197. stream = H3_STREAM_CTX(sdata);
  1198. if(stream && stream->s.ssl && stream->s.send_blocked &&
  1199. !SSL_want_write(stream->s.ssl)) {
  1200. nghttp3_conn_unblock_stream(ctx->h3.conn, stream->s.id);
  1201. stream->s.send_blocked = FALSE;
  1202. h3_drain_stream(cf, sdata);
  1203. CURL_TRC_CF(sdata, cf, "unblocked");
  1204. }
  1205. }
  1206. }
  1207. }
  1208. return CURLE_OK;
  1209. }
  1210. static CURLcode h3_send_streams(struct Curl_cfilter *cf,
  1211. struct Curl_easy *data)
  1212. {
  1213. struct cf_osslq_ctx *ctx = cf->ctx;
  1214. CURLcode result = CURLE_OK;
  1215. if(!ctx->tls.ssl || !ctx->h3.conn)
  1216. goto out;
  1217. for(;;) {
  1218. struct cf_osslq_stream *s = NULL;
  1219. nghttp3_vec vec[16];
  1220. nghttp3_ssize n, i;
  1221. int64_t stream_id;
  1222. size_t written;
  1223. int eos, ok, rv;
  1224. size_t total_len, acked_len = 0;
  1225. bool blocked = FALSE;
  1226. n = nghttp3_conn_writev_stream(ctx->h3.conn, &stream_id, &eos,
  1227. vec, ARRAYSIZE(vec));
  1228. if(n < 0) {
  1229. failf(data, "nghttp3_conn_writev_stream returned error: %s",
  1230. nghttp3_strerror((int)n));
  1231. result = CURLE_SEND_ERROR;
  1232. goto out;
  1233. }
  1234. if(stream_id < 0) {
  1235. result = CURLE_OK;
  1236. goto out;
  1237. }
  1238. /* Get the stream for this data */
  1239. s = cf_osslq_get_qstream(cf, data, stream_id);
  1240. if(!s) {
  1241. failf(data, "nghttp3_conn_writev_stream gave unknown stream %" PRId64,
  1242. stream_id);
  1243. result = CURLE_SEND_ERROR;
  1244. goto out;
  1245. }
  1246. /* Now write the data to the stream's SSL*, it may not all fit! */
  1247. DEBUGASSERT(s->id == stream_id);
  1248. for(i = 0, total_len = 0; i < n; ++i) {
  1249. total_len += vec[i].len;
  1250. }
  1251. for(i = 0; (i < n) && !blocked; ++i) {
  1252. /* Without stream->s.ssl, we closed that already, so
  1253. * pretend the write did succeed. */
  1254. written = vec[i].len;
  1255. ok = !s->ssl || SSL_write_ex(s->ssl, vec[i].base, vec[i].len,
  1256. &written);
  1257. if(ok) {
  1258. /* As OpenSSL buffers the data, we count this as acknowledged
  1259. * from nghttp3's point of view */
  1260. CURL_TRC_CF(data, cf, "[%"PRId64"] send %zu bytes to QUIC ok",
  1261. s->id, vec[i].len);
  1262. acked_len += vec[i].len;
  1263. }
  1264. else {
  1265. int detail = SSL_get_error(s->ssl, 0);
  1266. switch(detail) {
  1267. case SSL_ERROR_WANT_WRITE:
  1268. case SSL_ERROR_WANT_READ:
  1269. /* QUIC blocked us from writing more */
  1270. CURL_TRC_CF(data, cf, "[%"PRId64"] send %zu bytes to QUIC blocked",
  1271. s->id, vec[i].len);
  1272. written = 0;
  1273. nghttp3_conn_block_stream(ctx->h3.conn, s->id);
  1274. s->send_blocked = blocked = TRUE;
  1275. break;
  1276. default:
  1277. failf(data, "[%"PRId64"] send %zu bytes to QUIC, SSL error %d",
  1278. s->id, vec[i].len, detail);
  1279. result = cf_osslq_ssl_err(cf, data, detail, CURLE_SEND_ERROR);
  1280. goto out;
  1281. }
  1282. }
  1283. }
  1284. if(acked_len > 0 || (eos && !s->send_blocked)) {
  1285. /* Since QUIC buffers the data written internally, we can tell
  1286. * nghttp3 that it can move forward on it */
  1287. rv = nghttp3_conn_add_write_offset(ctx->h3.conn, s->id, acked_len);
  1288. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1289. failf(data, "nghttp3_conn_add_write_offset returned error: %s\n",
  1290. nghttp3_strerror(rv));
  1291. result = CURLE_SEND_ERROR;
  1292. goto out;
  1293. }
  1294. rv = nghttp3_conn_add_ack_offset(ctx->h3.conn, s->id, acked_len);
  1295. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1296. failf(data, "nghttp3_conn_add_ack_offset returned error: %s\n",
  1297. nghttp3_strerror(rv));
  1298. result = CURLE_SEND_ERROR;
  1299. goto out;
  1300. }
  1301. CURL_TRC_CF(data, cf, "[%" PRId64 "] forwarded %zu/%zu h3 bytes "
  1302. "to QUIC, eos=%d", s->id, acked_len, total_len, eos);
  1303. }
  1304. if(eos && !s->send_blocked) {
  1305. /* wrote everything and H3 indicates end of stream */
  1306. CURL_TRC_CF(data, cf, "[%" PRId64 "] closing QUIC stream", s->id);
  1307. SSL_stream_conclude(s->ssl, 0);
  1308. }
  1309. }
  1310. out:
  1311. CURL_TRC_CF(data, cf, "h3_send_streams -> %d", result);
  1312. return result;
  1313. }
  1314. static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
  1315. struct Curl_easy *data)
  1316. {
  1317. struct cf_osslq_ctx *ctx = cf->ctx;
  1318. CURLcode result = CURLE_OK;
  1319. if(!ctx->tls.ssl)
  1320. goto out;
  1321. ERR_clear_error();
  1322. result = h3_send_streams(cf, data);
  1323. if(result)
  1324. goto out;
  1325. if(!SSL_handle_events(ctx->tls.ssl)) {
  1326. int detail = SSL_get_error(ctx->tls.ssl, 0);
  1327. result = cf_osslq_ssl_err(cf, data, detail, CURLE_SEND_ERROR);
  1328. }
  1329. result = cf_osslq_check_and_unblock(cf, data);
  1330. out:
  1331. CURL_TRC_CF(data, cf, "progress_egress -> %d", result);
  1332. return result;
  1333. }
  1334. static CURLcode check_and_set_expiry(struct Curl_cfilter *cf,
  1335. struct Curl_easy *data)
  1336. {
  1337. struct cf_osslq_ctx *ctx = cf->ctx;
  1338. CURLcode result = CURLE_OK;
  1339. struct timeval tv;
  1340. timediff_t timeoutms;
  1341. int is_infinite = TRUE;
  1342. if(ctx->tls.ssl &&
  1343. SSL_get_event_timeout(ctx->tls.ssl, &tv, &is_infinite) &&
  1344. !is_infinite) {
  1345. timeoutms = curlx_tvtoms(&tv);
  1346. /* QUIC want to be called again latest at the returned timeout */
  1347. if(timeoutms <= 0) {
  1348. result = cf_progress_ingress(cf, data);
  1349. if(result)
  1350. goto out;
  1351. result = cf_progress_egress(cf, data);
  1352. if(result)
  1353. goto out;
  1354. if(SSL_get_event_timeout(ctx->tls.ssl, &tv, &is_infinite)) {
  1355. timeoutms = curlx_tvtoms(&tv);
  1356. }
  1357. }
  1358. if(!is_infinite) {
  1359. Curl_expire(data, timeoutms, EXPIRE_QUIC);
  1360. CURL_TRC_CF(data, cf, "QUIC expiry in %ldms", (long)timeoutms);
  1361. }
  1362. }
  1363. out:
  1364. return result;
  1365. }
  1366. static CURLcode cf_osslq_connect(struct Curl_cfilter *cf,
  1367. struct Curl_easy *data,
  1368. bool blocking, bool *done)
  1369. {
  1370. struct cf_osslq_ctx *ctx = cf->ctx;
  1371. CURLcode result = CURLE_OK;
  1372. struct cf_call_data save;
  1373. struct curltime now;
  1374. int err;
  1375. if(cf->connected) {
  1376. *done = TRUE;
  1377. return CURLE_OK;
  1378. }
  1379. /* Connect the UDP filter first */
  1380. if(!cf->next->connected) {
  1381. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  1382. if(result || !*done)
  1383. return result;
  1384. }
  1385. *done = FALSE;
  1386. now = Curl_now();
  1387. CF_DATA_SAVE(save, cf, data);
  1388. if(ctx->reconnect_at.tv_sec && Curl_timediff(now, ctx->reconnect_at) < 0) {
  1389. /* Not time yet to attempt the next connect */
  1390. CURL_TRC_CF(data, cf, "waiting for reconnect time");
  1391. goto out;
  1392. }
  1393. if(!ctx->tls.ssl) {
  1394. ctx->started_at = now;
  1395. result = cf_osslq_ctx_start(cf, data);
  1396. if(result)
  1397. goto out;
  1398. }
  1399. if(!ctx->got_first_byte) {
  1400. int readable = SOCKET_READABLE(ctx->q.sockfd, 0);
  1401. if(readable > 0 && (readable & CURL_CSELECT_IN)) {
  1402. ctx->got_first_byte = TRUE;
  1403. ctx->first_byte_at = Curl_now();
  1404. }
  1405. }
  1406. ERR_clear_error();
  1407. err = SSL_do_handshake(ctx->tls.ssl);
  1408. if(err == 1) {
  1409. /* connected */
  1410. ctx->handshake_at = now;
  1411. CURL_TRC_CF(data, cf, "handshake complete after %dms",
  1412. (int)Curl_timediff(now, ctx->started_at));
  1413. result = cf_osslq_verify_peer(cf, data);
  1414. if(!result) {
  1415. CURL_TRC_CF(data, cf, "peer verified");
  1416. cf->connected = TRUE;
  1417. cf->conn->alpn = CURL_HTTP_VERSION_3;
  1418. *done = TRUE;
  1419. connkeep(cf->conn, "HTTP/3 default");
  1420. }
  1421. }
  1422. else {
  1423. int detail = SSL_get_error(ctx->tls.ssl, err);
  1424. switch(detail) {
  1425. case SSL_ERROR_WANT_READ:
  1426. CURL_TRC_CF(data, cf, "QUIC SSL_connect() -> WANT_RECV");
  1427. result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
  1428. goto out;
  1429. case SSL_ERROR_WANT_WRITE:
  1430. CURL_TRC_CF(data, cf, "QUIC SSL_connect() -> WANT_SEND");
  1431. result = CURLE_OK;
  1432. goto out;
  1433. #ifdef SSL_ERROR_WANT_ASYNC
  1434. case SSL_ERROR_WANT_ASYNC:
  1435. CURL_TRC_CF(data, cf, "QUIC SSL_connect() -> WANT_ASYNC");
  1436. result = CURLE_OK;
  1437. goto out;
  1438. #endif
  1439. #ifdef SSL_ERROR_WANT_RETRY_VERIFY
  1440. case SSL_ERROR_WANT_RETRY_VERIFY:
  1441. result = CURLE_OK;
  1442. goto out;
  1443. #endif
  1444. default:
  1445. result = cf_osslq_ssl_err(cf, data, detail, CURLE_COULDNT_CONNECT);
  1446. goto out;
  1447. }
  1448. }
  1449. out:
  1450. if(result == CURLE_RECV_ERROR && ctx->tls.ssl && ctx->protocol_shutdown) {
  1451. /* When a QUIC server instance is shutting down, it may send us a
  1452. * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
  1453. * state. The CONNECT may work in the near future again. Indicate
  1454. * that as a "weird" reply. */
  1455. result = CURLE_WEIRD_SERVER_REPLY;
  1456. }
  1457. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1458. if(result) {
  1459. const char *r_ip = NULL;
  1460. int r_port = 0;
  1461. Curl_cf_socket_peek(cf->next, data, NULL, NULL,
  1462. &r_ip, &r_port, NULL, NULL);
  1463. infof(data, "QUIC connect to %s port %u failed: %s",
  1464. r_ip, r_port, curl_easy_strerror(result));
  1465. }
  1466. #endif
  1467. if(!result)
  1468. result = check_and_set_expiry(cf, data);
  1469. if(result || *done)
  1470. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  1471. CF_DATA_RESTORE(cf, save);
  1472. return result;
  1473. }
  1474. static ssize_t h3_stream_open(struct Curl_cfilter *cf,
  1475. struct Curl_easy *data,
  1476. const void *buf, size_t len,
  1477. CURLcode *err)
  1478. {
  1479. struct cf_osslq_ctx *ctx = cf->ctx;
  1480. struct h3_stream_ctx *stream = NULL;
  1481. struct dynhds h2_headers;
  1482. size_t nheader;
  1483. nghttp3_nv *nva = NULL;
  1484. int rc = 0;
  1485. unsigned int i;
  1486. ssize_t nwritten = -1;
  1487. nghttp3_data_reader reader;
  1488. nghttp3_data_reader *preader = NULL;
  1489. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1490. *err = h3_data_setup(cf, data);
  1491. if(*err)
  1492. goto out;
  1493. stream = H3_STREAM_CTX(data);
  1494. DEBUGASSERT(stream);
  1495. if(!stream) {
  1496. *err = CURLE_FAILED_INIT;
  1497. goto out;
  1498. }
  1499. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1500. if(nwritten < 0)
  1501. goto out;
  1502. if(!stream->h1.done) {
  1503. /* need more data */
  1504. goto out;
  1505. }
  1506. DEBUGASSERT(stream->h1.req);
  1507. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1508. if(*err) {
  1509. nwritten = -1;
  1510. goto out;
  1511. }
  1512. /* no longer needed */
  1513. Curl_h1_req_parse_free(&stream->h1);
  1514. nheader = Curl_dynhds_count(&h2_headers);
  1515. nva = malloc(sizeof(nghttp3_nv) * nheader);
  1516. if(!nva) {
  1517. *err = CURLE_OUT_OF_MEMORY;
  1518. nwritten = -1;
  1519. goto out;
  1520. }
  1521. for(i = 0; i < nheader; ++i) {
  1522. struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
  1523. nva[i].name = (unsigned char *)e->name;
  1524. nva[i].namelen = e->namelen;
  1525. nva[i].value = (unsigned char *)e->value;
  1526. nva[i].valuelen = e->valuelen;
  1527. nva[i].flags = NGHTTP3_NV_FLAG_NONE;
  1528. }
  1529. DEBUGASSERT(stream->s.id == -1);
  1530. *err = cf_osslq_stream_open(&stream->s, ctx->tls.ssl, 0,
  1531. &ctx->stream_bufcp, data);
  1532. if(*err) {
  1533. failf(data, "can't get bidi streams");
  1534. *err = CURLE_SEND_ERROR;
  1535. goto out;
  1536. }
  1537. switch(data->state.httpreq) {
  1538. case HTTPREQ_POST:
  1539. case HTTPREQ_POST_FORM:
  1540. case HTTPREQ_POST_MIME:
  1541. case HTTPREQ_PUT:
  1542. /* known request body size or -1 */
  1543. if(data->state.infilesize != -1)
  1544. stream->upload_left = data->state.infilesize;
  1545. else
  1546. /* data sending without specifying the data amount up front */
  1547. stream->upload_left = -1; /* unknown */
  1548. break;
  1549. default:
  1550. /* there is not request body */
  1551. stream->upload_left = 0; /* no request body */
  1552. break;
  1553. }
  1554. stream->send_closed = (stream->upload_left == 0);
  1555. if(!stream->send_closed) {
  1556. reader.read_data = cb_h3_read_req_body;
  1557. preader = &reader;
  1558. }
  1559. rc = nghttp3_conn_submit_request(ctx->h3.conn, stream->s.id,
  1560. nva, nheader, preader, data);
  1561. if(rc) {
  1562. switch(rc) {
  1563. case NGHTTP3_ERR_CONN_CLOSING:
  1564. CURL_TRC_CF(data, cf, "h3sid[%"PRId64"] failed to send, "
  1565. "connection is closing", stream->s.id);
  1566. break;
  1567. default:
  1568. CURL_TRC_CF(data, cf, "h3sid[%"PRId64"] failed to send -> %d (%s)",
  1569. stream->s.id, rc, nghttp3_strerror(rc));
  1570. break;
  1571. }
  1572. *err = CURLE_SEND_ERROR;
  1573. nwritten = -1;
  1574. goto out;
  1575. }
  1576. if(Curl_trc_is_verbose(data)) {
  1577. infof(data, "[HTTP/3] [%" PRId64 "] OPENED stream for %s",
  1578. stream->s.id, data->state.url);
  1579. for(i = 0; i < nheader; ++i) {
  1580. infof(data, "[HTTP/3] [%" PRId64 "] [%.*s: %.*s]", stream->s.id,
  1581. (int)nva[i].namelen, nva[i].name,
  1582. (int)nva[i].valuelen, nva[i].value);
  1583. }
  1584. }
  1585. out:
  1586. free(nva);
  1587. Curl_dynhds_free(&h2_headers);
  1588. return nwritten;
  1589. }
  1590. static ssize_t cf_osslq_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  1591. const void *buf, size_t len, CURLcode *err)
  1592. {
  1593. struct cf_osslq_ctx *ctx = cf->ctx;
  1594. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1595. struct cf_call_data save;
  1596. ssize_t nwritten;
  1597. CURLcode result;
  1598. CF_DATA_SAVE(save, cf, data);
  1599. DEBUGASSERT(cf->connected);
  1600. DEBUGASSERT(ctx->tls.ssl);
  1601. DEBUGASSERT(ctx->h3.conn);
  1602. *err = CURLE_OK;
  1603. result = cf_progress_ingress(cf, data);
  1604. if(result) {
  1605. *err = result;
  1606. nwritten = -1;
  1607. goto out;
  1608. }
  1609. result = cf_progress_egress(cf, data);
  1610. if(result) {
  1611. *err = result;
  1612. nwritten = -1;
  1613. goto out;
  1614. }
  1615. if(!stream || stream->s.id < 0) {
  1616. nwritten = h3_stream_open(cf, data, buf, len, err);
  1617. if(nwritten < 0) {
  1618. CURL_TRC_CF(data, cf, "failed to open stream -> %d", *err);
  1619. goto out;
  1620. }
  1621. stream = H3_STREAM_CTX(data);
  1622. }
  1623. else if(stream->upload_blocked_len) {
  1624. /* the data in `buf` has already been submitted or added to the
  1625. * buffers, but have been EAGAINed on the last invocation. */
  1626. DEBUGASSERT(len >= stream->upload_blocked_len);
  1627. if(len < stream->upload_blocked_len) {
  1628. /* Did we get called again with a smaller `len`? This should not
  1629. * happen. We are not prepared to handle that. */
  1630. failf(data, "HTTP/3 send again with decreased length");
  1631. *err = CURLE_HTTP3;
  1632. nwritten = -1;
  1633. goto out;
  1634. }
  1635. nwritten = (ssize_t)stream->upload_blocked_len;
  1636. stream->upload_blocked_len = 0;
  1637. }
  1638. else if(stream->closed) {
  1639. if(stream->resp_hds_complete) {
  1640. /* Server decided to close the stream after having sent us a final
  1641. * response. This is valid if it is not interested in the request
  1642. * body. This happens on 30x or 40x responses.
  1643. * We silently discard the data sent, since this is not a transport
  1644. * error situation. */
  1645. CURL_TRC_CF(data, cf, "[%" PRId64 "] discarding data"
  1646. "on closed stream with response", stream->s.id);
  1647. *err = CURLE_OK;
  1648. nwritten = (ssize_t)len;
  1649. goto out;
  1650. }
  1651. CURL_TRC_CF(data, cf, "[%" PRId64 "] send_body(len=%zu) "
  1652. "-> stream closed", stream->s.id, len);
  1653. *err = CURLE_HTTP3;
  1654. nwritten = -1;
  1655. goto out;
  1656. }
  1657. else {
  1658. nwritten = Curl_bufq_write(&stream->sendbuf, buf, len, err);
  1659. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_send, add to "
  1660. "sendbuf(len=%zu) -> %zd, %d",
  1661. stream->s.id, len, nwritten, *err);
  1662. if(nwritten < 0) {
  1663. goto out;
  1664. }
  1665. (void)nghttp3_conn_resume_stream(ctx->h3.conn, stream->s.id);
  1666. }
  1667. result = cf_progress_egress(cf, data);
  1668. if(result) {
  1669. *err = result;
  1670. nwritten = -1;
  1671. }
  1672. if(stream && nwritten > 0 && stream->sendbuf_len_in_flight) {
  1673. /* We have unacknowledged DATA and cannot report success to our
  1674. * caller. Instead we EAGAIN and remember how much we have already
  1675. * "written" into our various internal connection buffers. */
  1676. stream->upload_blocked_len = nwritten;
  1677. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_send(len=%zu), "
  1678. "%zu bytes in flight -> EGAIN", stream->s.id, len,
  1679. stream->sendbuf_len_in_flight);
  1680. *err = CURLE_AGAIN;
  1681. nwritten = -1;
  1682. }
  1683. out:
  1684. result = check_and_set_expiry(cf, data);
  1685. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_send(len=%zu) -> %zd, %d",
  1686. stream? stream->s.id : -1, len, nwritten, *err);
  1687. CF_DATA_RESTORE(cf, save);
  1688. return nwritten;
  1689. }
  1690. static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
  1691. struct Curl_easy *data,
  1692. struct h3_stream_ctx *stream,
  1693. CURLcode *err)
  1694. {
  1695. ssize_t nread = -1;
  1696. (void)cf;
  1697. if(stream->reset) {
  1698. failf(data,
  1699. "HTTP/3 stream %" PRId64 " reset by server", stream->s.id);
  1700. *err = stream->resp_hds_complete? CURLE_PARTIAL_FILE : CURLE_HTTP3;
  1701. goto out;
  1702. }
  1703. else if(!stream->resp_hds_complete) {
  1704. failf(data,
  1705. "HTTP/3 stream %" PRId64 " was closed cleanly, but before getting"
  1706. " all response header fields, treated as error",
  1707. stream->s.id);
  1708. *err = CURLE_HTTP3;
  1709. goto out;
  1710. }
  1711. *err = CURLE_OK;
  1712. nread = 0;
  1713. out:
  1714. return nread;
  1715. }
  1716. static ssize_t cf_osslq_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1717. char *buf, size_t len, CURLcode *err)
  1718. {
  1719. struct cf_osslq_ctx *ctx = cf->ctx;
  1720. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1721. ssize_t nread = -1;
  1722. struct cf_call_data save;
  1723. CURLcode result;
  1724. (void)ctx;
  1725. CF_DATA_SAVE(save, cf, data);
  1726. DEBUGASSERT(cf->connected);
  1727. DEBUGASSERT(ctx);
  1728. DEBUGASSERT(ctx->tls.ssl);
  1729. DEBUGASSERT(ctx->h3.conn);
  1730. *err = CURLE_OK;
  1731. if(!stream) {
  1732. *err = CURLE_RECV_ERROR;
  1733. goto out;
  1734. }
  1735. if(!Curl_bufq_is_empty(&stream->recvbuf)) {
  1736. nread = Curl_bufq_read(&stream->recvbuf,
  1737. (unsigned char *)buf, len, err);
  1738. if(nread < 0) {
  1739. CURL_TRC_CF(data, cf, "[%" PRId64 "] read recvbuf(len=%zu) "
  1740. "-> %zd, %d", stream->s.id, len, nread, *err);
  1741. goto out;
  1742. }
  1743. }
  1744. result = cf_progress_ingress(cf, data);
  1745. if(result) {
  1746. *err = result;
  1747. nread = -1;
  1748. goto out;
  1749. }
  1750. /* recvbuf had nothing before, maybe after progressing ingress? */
  1751. if(nread < 0 && !Curl_bufq_is_empty(&stream->recvbuf)) {
  1752. nread = Curl_bufq_read(&stream->recvbuf,
  1753. (unsigned char *)buf, len, err);
  1754. if(nread < 0) {
  1755. CURL_TRC_CF(data, cf, "[%" PRId64 "] read recvbuf(len=%zu) "
  1756. "-> %zd, %d", stream->s.id, len, nread, *err);
  1757. goto out;
  1758. }
  1759. }
  1760. if(nread > 0) {
  1761. h3_drain_stream(cf, data);
  1762. }
  1763. else {
  1764. if(stream->closed) {
  1765. nread = recv_closed_stream(cf, data, stream, err);
  1766. goto out;
  1767. }
  1768. *err = CURLE_AGAIN;
  1769. nread = -1;
  1770. }
  1771. out:
  1772. if(cf_progress_egress(cf, data)) {
  1773. *err = CURLE_SEND_ERROR;
  1774. nread = -1;
  1775. }
  1776. else {
  1777. CURLcode result2 = check_and_set_expiry(cf, data);
  1778. if(result2) {
  1779. *err = result2;
  1780. nread = -1;
  1781. }
  1782. }
  1783. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_recv(len=%zu) -> %zd, %d",
  1784. stream? stream->s.id : -1, len, nread, *err);
  1785. CF_DATA_RESTORE(cf, save);
  1786. return nread;
  1787. }
  1788. /*
  1789. * Called from transfer.c:data_pending to know if we should keep looping
  1790. * to receive more data from the connection.
  1791. */
  1792. static bool cf_osslq_data_pending(struct Curl_cfilter *cf,
  1793. const struct Curl_easy *data)
  1794. {
  1795. const struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1796. (void)cf;
  1797. return stream && !Curl_bufq_is_empty(&stream->recvbuf);
  1798. }
  1799. static CURLcode cf_osslq_data_event(struct Curl_cfilter *cf,
  1800. struct Curl_easy *data,
  1801. int event, int arg1, void *arg2)
  1802. {
  1803. struct cf_osslq_ctx *ctx = cf->ctx;
  1804. CURLcode result = CURLE_OK;
  1805. struct cf_call_data save;
  1806. CF_DATA_SAVE(save, cf, data);
  1807. (void)arg1;
  1808. (void)arg2;
  1809. switch(event) {
  1810. case CF_CTRL_DATA_SETUP:
  1811. break;
  1812. case CF_CTRL_DATA_PAUSE:
  1813. result = h3_data_pause(cf, data, (arg1 != 0));
  1814. break;
  1815. case CF_CTRL_DATA_DETACH:
  1816. h3_data_done(cf, data);
  1817. break;
  1818. case CF_CTRL_DATA_DONE:
  1819. h3_data_done(cf, data);
  1820. break;
  1821. case CF_CTRL_DATA_DONE_SEND: {
  1822. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1823. if(stream && !stream->send_closed) {
  1824. stream->send_closed = TRUE;
  1825. stream->upload_left = Curl_bufq_len(&stream->sendbuf);
  1826. (void)nghttp3_conn_resume_stream(ctx->h3.conn, stream->s.id);
  1827. }
  1828. break;
  1829. }
  1830. case CF_CTRL_DATA_IDLE: {
  1831. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1832. CURL_TRC_CF(data, cf, "data idle");
  1833. if(stream && !stream->closed) {
  1834. result = check_and_set_expiry(cf, data);
  1835. }
  1836. break;
  1837. }
  1838. default:
  1839. break;
  1840. }
  1841. CF_DATA_RESTORE(cf, save);
  1842. return result;
  1843. }
  1844. static bool cf_osslq_conn_is_alive(struct Curl_cfilter *cf,
  1845. struct Curl_easy *data,
  1846. bool *input_pending)
  1847. {
  1848. struct cf_osslq_ctx *ctx = cf->ctx;
  1849. bool alive = FALSE;
  1850. struct cf_call_data save;
  1851. CF_DATA_SAVE(save, cf, data);
  1852. *input_pending = FALSE;
  1853. if(!ctx->tls.ssl)
  1854. goto out;
  1855. /* TODO: how to check negotiated connection idle time? */
  1856. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  1857. goto out;
  1858. alive = TRUE;
  1859. if(*input_pending) {
  1860. CURLcode result;
  1861. /* This happens before we've sent off a request and the connection is
  1862. not in use by any other transfer, there shouldn't be any data here,
  1863. only "protocol frames" */
  1864. *input_pending = FALSE;
  1865. result = cf_progress_ingress(cf, data);
  1866. CURL_TRC_CF(data, cf, "is_alive, progress ingress -> %d", result);
  1867. alive = result? FALSE : TRUE;
  1868. }
  1869. out:
  1870. CF_DATA_RESTORE(cf, save);
  1871. return alive;
  1872. }
  1873. static void cf_osslq_adjust_pollset(struct Curl_cfilter *cf,
  1874. struct Curl_easy *data,
  1875. struct easy_pollset *ps)
  1876. {
  1877. struct cf_osslq_ctx *ctx = cf->ctx;
  1878. if(!ctx->tls.ssl) {
  1879. /* NOP */
  1880. }
  1881. else if(!cf->connected) {
  1882. /* during handshake, transfer has not started yet. we always
  1883. * add our socket for polling if SSL wants to send/recv */
  1884. Curl_pollset_set(data, ps, ctx->q.sockfd,
  1885. SSL_net_read_desired(ctx->tls.ssl),
  1886. SSL_net_write_desired(ctx->tls.ssl));
  1887. }
  1888. else {
  1889. /* once connected, we only modify the socket if it is present.
  1890. * this avoids adding it for paused transfers. */
  1891. bool want_recv, want_send;
  1892. Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
  1893. if(want_recv || want_send) {
  1894. Curl_pollset_set(data, ps, ctx->q.sockfd,
  1895. SSL_net_read_desired(ctx->tls.ssl),
  1896. SSL_net_write_desired(ctx->tls.ssl));
  1897. }
  1898. }
  1899. }
  1900. static CURLcode cf_osslq_query(struct Curl_cfilter *cf,
  1901. struct Curl_easy *data,
  1902. int query, int *pres1, void *pres2)
  1903. {
  1904. struct cf_osslq_ctx *ctx = cf->ctx;
  1905. struct cf_call_data save;
  1906. switch(query) {
  1907. case CF_QUERY_MAX_CONCURRENT: {
  1908. /* TODO: how to get this? */
  1909. CF_DATA_SAVE(save, cf, data);
  1910. *pres1 = 100;
  1911. CURL_TRC_CF(data, cf, "query max_conncurrent -> %d", *pres1);
  1912. CF_DATA_RESTORE(cf, save);
  1913. return CURLE_OK;
  1914. }
  1915. case CF_QUERY_CONNECT_REPLY_MS:
  1916. if(ctx->got_first_byte) {
  1917. timediff_t ms = Curl_timediff(ctx->first_byte_at, ctx->started_at);
  1918. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  1919. }
  1920. else
  1921. *pres1 = -1;
  1922. return CURLE_OK;
  1923. case CF_QUERY_TIMER_CONNECT: {
  1924. struct curltime *when = pres2;
  1925. if(ctx->got_first_byte)
  1926. *when = ctx->first_byte_at;
  1927. return CURLE_OK;
  1928. }
  1929. case CF_QUERY_TIMER_APPCONNECT: {
  1930. struct curltime *when = pres2;
  1931. if(cf->connected)
  1932. *when = ctx->handshake_at;
  1933. return CURLE_OK;
  1934. }
  1935. default:
  1936. break;
  1937. }
  1938. return cf->next?
  1939. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  1940. CURLE_UNKNOWN_OPTION;
  1941. }
  1942. struct Curl_cftype Curl_cft_http3 = {
  1943. "HTTP/3",
  1944. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  1945. 0,
  1946. cf_osslq_destroy,
  1947. cf_osslq_connect,
  1948. cf_osslq_close,
  1949. Curl_cf_def_get_host,
  1950. cf_osslq_adjust_pollset,
  1951. cf_osslq_data_pending,
  1952. cf_osslq_send,
  1953. cf_osslq_recv,
  1954. cf_osslq_data_event,
  1955. cf_osslq_conn_is_alive,
  1956. Curl_cf_def_conn_keep_alive,
  1957. cf_osslq_query,
  1958. };
  1959. CURLcode Curl_cf_osslq_create(struct Curl_cfilter **pcf,
  1960. struct Curl_easy *data,
  1961. struct connectdata *conn,
  1962. const struct Curl_addrinfo *ai)
  1963. {
  1964. struct cf_osslq_ctx *ctx = NULL;
  1965. struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
  1966. CURLcode result;
  1967. (void)data;
  1968. ctx = calloc(1, sizeof(*ctx));
  1969. if(!ctx) {
  1970. result = CURLE_OUT_OF_MEMORY;
  1971. goto out;
  1972. }
  1973. cf_osslq_ctx_clear(ctx);
  1974. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  1975. if(result)
  1976. goto out;
  1977. result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
  1978. if(result)
  1979. goto out;
  1980. cf->conn = conn;
  1981. udp_cf->conn = cf->conn;
  1982. udp_cf->sockindex = cf->sockindex;
  1983. cf->next = udp_cf;
  1984. out:
  1985. *pcf = (!result)? cf : NULL;
  1986. if(result) {
  1987. if(udp_cf)
  1988. Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
  1989. Curl_safefree(cf);
  1990. Curl_safefree(ctx);
  1991. }
  1992. return result;
  1993. }
  1994. bool Curl_conn_is_osslq(const struct Curl_easy *data,
  1995. const struct connectdata *conn,
  1996. int sockindex)
  1997. {
  1998. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  1999. (void)data;
  2000. for(; cf; cf = cf->next) {
  2001. if(cf->cft == &Curl_cft_http3)
  2002. return TRUE;
  2003. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  2004. return FALSE;
  2005. }
  2006. return FALSE;
  2007. }
  2008. /*
  2009. * Store ngtcp2 version info in this buffer.
  2010. */
  2011. void Curl_osslq_ver(char *p, size_t len)
  2012. {
  2013. const nghttp3_info *ht3 = nghttp3_version(0);
  2014. (void)msnprintf(p, len, "nghttp3/%s", ht3->version_str);
  2015. }
  2016. #endif /* USE_OPENSSL_QUIC && USE_NGHTTP3 */