1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2022, 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 | ***************************************************************************/
|
---|
22 |
|
---|
23 | #include "curl_setup.h"
|
---|
24 |
|
---|
25 | #ifdef USE_QUICHE
|
---|
26 | #include <quiche.h>
|
---|
27 | #include <openssl/err.h>
|
---|
28 | #include <openssl/ssl.h>
|
---|
29 | #include "urldata.h"
|
---|
30 | #include "sendf.h"
|
---|
31 | #include "strdup.h"
|
---|
32 | #include "rand.h"
|
---|
33 | #include "quic.h"
|
---|
34 | #include "strcase.h"
|
---|
35 | #include "multiif.h"
|
---|
36 | #include "connect.h"
|
---|
37 | #include "strerror.h"
|
---|
38 | #include "vquic.h"
|
---|
39 | #include "transfer.h"
|
---|
40 | #include "h2h3.h"
|
---|
41 | #include "vtls/openssl.h"
|
---|
42 | #include "vtls/keylog.h"
|
---|
43 |
|
---|
44 | /* The last 3 #include files should be in this order */
|
---|
45 | #include "curl_printf.h"
|
---|
46 | #include "curl_memory.h"
|
---|
47 | #include "memdebug.h"
|
---|
48 |
|
---|
49 | #define DEBUG_HTTP3
|
---|
50 | /* #define DEBUG_QUICHE */
|
---|
51 | #ifdef DEBUG_HTTP3
|
---|
52 | #define H3BUGF(x) x
|
---|
53 | #else
|
---|
54 | #define H3BUGF(x) do { } while(0)
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #define QUIC_MAX_STREAMS (256*1024)
|
---|
58 | #define QUIC_MAX_DATA (1*1024*1024)
|
---|
59 | #define QUIC_IDLE_TIMEOUT (60 * 1000) /* milliseconds */
|
---|
60 |
|
---|
61 | static CURLcode process_ingress(struct Curl_easy *data,
|
---|
62 | curl_socket_t sockfd,
|
---|
63 | struct quicsocket *qs);
|
---|
64 |
|
---|
65 | static CURLcode flush_egress(struct Curl_easy *data, curl_socket_t sockfd,
|
---|
66 | struct quicsocket *qs);
|
---|
67 |
|
---|
68 | static CURLcode http_request(struct Curl_easy *data, const void *mem,
|
---|
69 | size_t len);
|
---|
70 | static Curl_recv h3_stream_recv;
|
---|
71 | static Curl_send h3_stream_send;
|
---|
72 |
|
---|
73 | static int quiche_getsock(struct Curl_easy *data,
|
---|
74 | struct connectdata *conn, curl_socket_t *socks)
|
---|
75 | {
|
---|
76 | struct SingleRequest *k = &data->req;
|
---|
77 | int bitmap = GETSOCK_BLANK;
|
---|
78 |
|
---|
79 | socks[0] = conn->sock[FIRSTSOCKET];
|
---|
80 |
|
---|
81 | /* in a HTTP/2 connection we can basically always get a frame so we should
|
---|
82 | always be ready for one */
|
---|
83 | bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
|
---|
84 |
|
---|
85 | /* we're still uploading or the HTTP/2 layer wants to send data */
|
---|
86 | if((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND)
|
---|
87 | bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
|
---|
88 |
|
---|
89 | return bitmap;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static CURLcode qs_disconnect(struct Curl_easy *data,
|
---|
93 | struct quicsocket *qs)
|
---|
94 | {
|
---|
95 | DEBUGASSERT(qs);
|
---|
96 | if(qs->conn) {
|
---|
97 | (void)quiche_conn_close(qs->conn, TRUE, 0, NULL, 0);
|
---|
98 | /* flushing the egress is not a failsafe way to deliver all the
|
---|
99 | outstanding packets, but we also don't want to get stuck here... */
|
---|
100 | (void)flush_egress(data, qs->sockfd, qs);
|
---|
101 | quiche_conn_free(qs->conn);
|
---|
102 | qs->conn = NULL;
|
---|
103 | }
|
---|
104 | if(qs->h3config)
|
---|
105 | quiche_h3_config_free(qs->h3config);
|
---|
106 | if(qs->h3c)
|
---|
107 | quiche_h3_conn_free(qs->h3c);
|
---|
108 | if(qs->cfg) {
|
---|
109 | quiche_config_free(qs->cfg);
|
---|
110 | qs->cfg = NULL;
|
---|
111 | }
|
---|
112 | return CURLE_OK;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static CURLcode quiche_disconnect(struct Curl_easy *data,
|
---|
116 | struct connectdata *conn,
|
---|
117 | bool dead_connection)
|
---|
118 | {
|
---|
119 | struct quicsocket *qs = conn->quic;
|
---|
120 | (void)dead_connection;
|
---|
121 | return qs_disconnect(data, qs);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void Curl_quic_disconnect(struct Curl_easy *data,
|
---|
125 | struct connectdata *conn,
|
---|
126 | int tempindex)
|
---|
127 | {
|
---|
128 | if(conn->transport == TRNSPRT_QUIC)
|
---|
129 | qs_disconnect(data, &conn->hequic[tempindex]);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static unsigned int quiche_conncheck(struct Curl_easy *data,
|
---|
133 | struct connectdata *conn,
|
---|
134 | unsigned int checks_to_perform)
|
---|
135 | {
|
---|
136 | (void)data;
|
---|
137 | (void)conn;
|
---|
138 | (void)checks_to_perform;
|
---|
139 | return CONNRESULT_NONE;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static CURLcode quiche_do(struct Curl_easy *data, bool *done)
|
---|
143 | {
|
---|
144 | struct HTTP *stream = data->req.p.http;
|
---|
145 | stream->h3req = FALSE; /* not sent */
|
---|
146 | return Curl_http(data, done);
|
---|
147 | }
|
---|
148 |
|
---|
149 | static const struct Curl_handler Curl_handler_http3 = {
|
---|
150 | "HTTPS", /* scheme */
|
---|
151 | ZERO_NULL, /* setup_connection */
|
---|
152 | quiche_do, /* do_it */
|
---|
153 | Curl_http_done, /* done */
|
---|
154 | ZERO_NULL, /* do_more */
|
---|
155 | ZERO_NULL, /* connect_it */
|
---|
156 | ZERO_NULL, /* connecting */
|
---|
157 | ZERO_NULL, /* doing */
|
---|
158 | quiche_getsock, /* proto_getsock */
|
---|
159 | quiche_getsock, /* doing_getsock */
|
---|
160 | ZERO_NULL, /* domore_getsock */
|
---|
161 | quiche_getsock, /* perform_getsock */
|
---|
162 | quiche_disconnect, /* disconnect */
|
---|
163 | ZERO_NULL, /* readwrite */
|
---|
164 | quiche_conncheck, /* connection_check */
|
---|
165 | ZERO_NULL, /* attach connection */
|
---|
166 | PORT_HTTP, /* defport */
|
---|
167 | CURLPROTO_HTTPS, /* protocol */
|
---|
168 | CURLPROTO_HTTP, /* family */
|
---|
169 | PROTOPT_SSL | PROTOPT_STREAM /* flags */
|
---|
170 | };
|
---|
171 |
|
---|
172 | #ifdef DEBUG_QUICHE
|
---|
173 | static void quiche_debug_log(const char *line, void *argp)
|
---|
174 | {
|
---|
175 | (void)argp;
|
---|
176 | fprintf(stderr, "%s\n", line);
|
---|
177 | }
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | static void keylog_callback(const SSL *ssl, const char *line)
|
---|
181 | {
|
---|
182 | (void)ssl;
|
---|
183 | Curl_tls_keylog_write_line(line);
|
---|
184 | }
|
---|
185 |
|
---|
186 | static SSL_CTX *quic_ssl_ctx(struct Curl_easy *data)
|
---|
187 | {
|
---|
188 | SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method());
|
---|
189 |
|
---|
190 | SSL_CTX_set_alpn_protos(ssl_ctx,
|
---|
191 | (const uint8_t *)QUICHE_H3_APPLICATION_PROTOCOL,
|
---|
192 | sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1);
|
---|
193 |
|
---|
194 | SSL_CTX_set_default_verify_paths(ssl_ctx);
|
---|
195 |
|
---|
196 | /* Open the file if a TLS or QUIC backend has not done this before. */
|
---|
197 | Curl_tls_keylog_open();
|
---|
198 | if(Curl_tls_keylog_enabled()) {
|
---|
199 | SSL_CTX_set_keylog_callback(ssl_ctx, keylog_callback);
|
---|
200 | }
|
---|
201 |
|
---|
202 | {
|
---|
203 | struct connectdata *conn = data->conn;
|
---|
204 | const char * const ssl_cafile = conn->ssl_config.CAfile;
|
---|
205 | const char * const ssl_capath = conn->ssl_config.CApath;
|
---|
206 |
|
---|
207 | if(conn->ssl_config.verifypeer) {
|
---|
208 | SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
|
---|
209 | /* tell OpenSSL where to find CA certificates that are used to verify
|
---|
210 | the server's certificate. */
|
---|
211 | if(!SSL_CTX_load_verify_locations(ssl_ctx, ssl_cafile, ssl_capath)) {
|
---|
212 | /* Fail if we insist on successfully verifying the server. */
|
---|
213 | failf(data, "error setting certificate verify locations:"
|
---|
214 | " CAfile: %s CApath: %s",
|
---|
215 | ssl_cafile ? ssl_cafile : "none",
|
---|
216 | ssl_capath ? ssl_capath : "none");
|
---|
217 | return NULL;
|
---|
218 | }
|
---|
219 | infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
|
---|
220 | infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
|
---|
221 | }
|
---|
222 | }
|
---|
223 | return ssl_ctx;
|
---|
224 | }
|
---|
225 |
|
---|
226 | static int quic_init_ssl(struct quicsocket *qs, struct connectdata *conn)
|
---|
227 | {
|
---|
228 | /* this will need some attention when HTTPS proxy over QUIC get fixed */
|
---|
229 | const char * const hostname = conn->host.name;
|
---|
230 |
|
---|
231 | DEBUGASSERT(!qs->ssl);
|
---|
232 | qs->ssl = SSL_new(qs->sslctx);
|
---|
233 |
|
---|
234 | SSL_set_app_data(qs->ssl, qs);
|
---|
235 |
|
---|
236 | /* set SNI */
|
---|
237 | SSL_set_tlsext_host_name(qs->ssl, hostname);
|
---|
238 | return 0;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | CURLcode Curl_quic_connect(struct Curl_easy *data,
|
---|
243 | struct connectdata *conn, curl_socket_t sockfd,
|
---|
244 | int sockindex,
|
---|
245 | const struct sockaddr *addr, socklen_t addrlen)
|
---|
246 | {
|
---|
247 | CURLcode result;
|
---|
248 | struct quicsocket *qs = &conn->hequic[sockindex];
|
---|
249 | char ipbuf[40];
|
---|
250 | int port;
|
---|
251 |
|
---|
252 | #ifdef DEBUG_QUICHE
|
---|
253 | /* initialize debug log callback only once */
|
---|
254 | static int debug_log_init = 0;
|
---|
255 | if(!debug_log_init) {
|
---|
256 | quiche_enable_debug_logging(quiche_debug_log, NULL);
|
---|
257 | debug_log_init = 1;
|
---|
258 | }
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | (void)addr;
|
---|
262 | (void)addrlen;
|
---|
263 |
|
---|
264 | qs->sockfd = sockfd;
|
---|
265 | qs->cfg = quiche_config_new(QUICHE_PROTOCOL_VERSION);
|
---|
266 | if(!qs->cfg) {
|
---|
267 | failf(data, "can't create quiche config");
|
---|
268 | return CURLE_FAILED_INIT;
|
---|
269 | }
|
---|
270 |
|
---|
271 | quiche_config_set_max_idle_timeout(qs->cfg, QUIC_IDLE_TIMEOUT);
|
---|
272 | quiche_config_set_initial_max_data(qs->cfg, QUIC_MAX_DATA);
|
---|
273 | quiche_config_set_initial_max_stream_data_bidi_local(qs->cfg, QUIC_MAX_DATA);
|
---|
274 | quiche_config_set_initial_max_stream_data_bidi_remote(qs->cfg,
|
---|
275 | QUIC_MAX_DATA);
|
---|
276 | quiche_config_set_initial_max_stream_data_uni(qs->cfg, QUIC_MAX_DATA);
|
---|
277 | quiche_config_set_initial_max_streams_bidi(qs->cfg, QUIC_MAX_STREAMS);
|
---|
278 | quiche_config_set_initial_max_streams_uni(qs->cfg, QUIC_MAX_STREAMS);
|
---|
279 | quiche_config_set_application_protos(qs->cfg,
|
---|
280 | (uint8_t *)
|
---|
281 | QUICHE_H3_APPLICATION_PROTOCOL,
|
---|
282 | sizeof(QUICHE_H3_APPLICATION_PROTOCOL)
|
---|
283 | - 1);
|
---|
284 |
|
---|
285 | qs->sslctx = quic_ssl_ctx(data);
|
---|
286 | if(!qs->sslctx)
|
---|
287 | return CURLE_QUIC_CONNECT_ERROR;
|
---|
288 |
|
---|
289 | if(quic_init_ssl(qs, conn))
|
---|
290 | return CURLE_QUIC_CONNECT_ERROR;
|
---|
291 |
|
---|
292 | result = Curl_rand(data, qs->scid, sizeof(qs->scid));
|
---|
293 | if(result)
|
---|
294 | return result;
|
---|
295 |
|
---|
296 | qs->conn = quiche_conn_new_with_tls((const uint8_t *) qs->scid,
|
---|
297 | sizeof(qs->scid), NULL, 0, addr, addrlen,
|
---|
298 | qs->cfg, qs->ssl, false);
|
---|
299 | if(!qs->conn) {
|
---|
300 | failf(data, "can't create quiche connection");
|
---|
301 | return CURLE_OUT_OF_MEMORY;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* Known to not work on Windows */
|
---|
305 | #if !defined(WIN32) && defined(HAVE_QUICHE_CONN_SET_QLOG_FD)
|
---|
306 | {
|
---|
307 | int qfd;
|
---|
308 | (void)Curl_qlogdir(data, qs->scid, sizeof(qs->scid), &qfd);
|
---|
309 | if(qfd != -1)
|
---|
310 | quiche_conn_set_qlog_fd(qs->conn, qfd,
|
---|
311 | "qlog title", "curl qlog");
|
---|
312 | }
|
---|
313 | #endif
|
---|
314 |
|
---|
315 | result = flush_egress(data, sockfd, qs);
|
---|
316 | if(result)
|
---|
317 | return result;
|
---|
318 |
|
---|
319 | /* extract the used address as a string */
|
---|
320 | if(!Curl_addr2string((struct sockaddr*)addr, addrlen, ipbuf, &port)) {
|
---|
321 | char buffer[STRERROR_LEN];
|
---|
322 | failf(data, "ssrem inet_ntop() failed with errno %d: %s",
|
---|
323 | SOCKERRNO, Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
---|
324 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
325 | }
|
---|
326 |
|
---|
327 | infof(data, "Connect socket %d over QUIC to %s:%ld",
|
---|
328 | sockfd, ipbuf, port);
|
---|
329 |
|
---|
330 | Curl_persistconninfo(data, conn, NULL, -1);
|
---|
331 |
|
---|
332 | /* for connection reuse purposes: */
|
---|
333 | conn->ssl[FIRSTSOCKET].state = ssl_connection_complete;
|
---|
334 |
|
---|
335 | {
|
---|
336 | unsigned char alpn_protocols[] = QUICHE_H3_APPLICATION_PROTOCOL;
|
---|
337 | unsigned alpn_len, offset = 0;
|
---|
338 |
|
---|
339 | /* Replace each ALPN length prefix by a comma. */
|
---|
340 | while(offset < sizeof(alpn_protocols) - 1) {
|
---|
341 | alpn_len = alpn_protocols[offset];
|
---|
342 | alpn_protocols[offset] = ',';
|
---|
343 | offset += 1 + alpn_len;
|
---|
344 | }
|
---|
345 |
|
---|
346 | infof(data, "Sent QUIC client Initial, ALPN: %s",
|
---|
347 | alpn_protocols + 1);
|
---|
348 | }
|
---|
349 |
|
---|
350 | return CURLE_OK;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static CURLcode quiche_has_connected(struct Curl_easy *data,
|
---|
354 | struct connectdata *conn,
|
---|
355 | int sockindex,
|
---|
356 | int tempindex)
|
---|
357 | {
|
---|
358 | CURLcode result;
|
---|
359 | struct quicsocket *qs = conn->quic = &conn->hequic[tempindex];
|
---|
360 |
|
---|
361 | conn->recv[sockindex] = h3_stream_recv;
|
---|
362 | conn->send[sockindex] = h3_stream_send;
|
---|
363 | conn->handler = &Curl_handler_http3;
|
---|
364 | conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
---|
365 | conn->httpversion = 30;
|
---|
366 | conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
---|
367 |
|
---|
368 | if(conn->ssl_config.verifyhost) {
|
---|
369 | X509 *server_cert;
|
---|
370 | server_cert = SSL_get_peer_certificate(qs->ssl);
|
---|
371 | if(!server_cert) {
|
---|
372 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
373 | }
|
---|
374 | result = Curl_ossl_verifyhost(data, conn, server_cert);
|
---|
375 | X509_free(server_cert);
|
---|
376 | if(result)
|
---|
377 | return result;
|
---|
378 | infof(data, "Verified certificate just fine");
|
---|
379 | }
|
---|
380 | else
|
---|
381 | infof(data, "Skipped certificate verification");
|
---|
382 |
|
---|
383 | qs->h3config = quiche_h3_config_new();
|
---|
384 | if(!qs->h3config)
|
---|
385 | return CURLE_OUT_OF_MEMORY;
|
---|
386 |
|
---|
387 | /* Create a new HTTP/3 connection on the QUIC connection. */
|
---|
388 | qs->h3c = quiche_h3_conn_new_with_transport(qs->conn, qs->h3config);
|
---|
389 | if(!qs->h3c) {
|
---|
390 | result = CURLE_OUT_OF_MEMORY;
|
---|
391 | goto fail;
|
---|
392 | }
|
---|
393 | if(conn->hequic[1-tempindex].cfg) {
|
---|
394 | qs = &conn->hequic[1-tempindex];
|
---|
395 | quiche_config_free(qs->cfg);
|
---|
396 | quiche_conn_free(qs->conn);
|
---|
397 | qs->cfg = NULL;
|
---|
398 | qs->conn = NULL;
|
---|
399 | }
|
---|
400 | return CURLE_OK;
|
---|
401 | fail:
|
---|
402 | quiche_h3_config_free(qs->h3config);
|
---|
403 | quiche_h3_conn_free(qs->h3c);
|
---|
404 | return result;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * This function gets polled to check if this QUIC connection has connected.
|
---|
409 | */
|
---|
410 | CURLcode Curl_quic_is_connected(struct Curl_easy *data,
|
---|
411 | struct connectdata *conn,
|
---|
412 | int sockindex,
|
---|
413 | bool *done)
|
---|
414 | {
|
---|
415 | CURLcode result;
|
---|
416 | struct quicsocket *qs = &conn->hequic[sockindex];
|
---|
417 | curl_socket_t sockfd = conn->tempsock[sockindex];
|
---|
418 |
|
---|
419 | result = process_ingress(data, sockfd, qs);
|
---|
420 | if(result)
|
---|
421 | goto error;
|
---|
422 |
|
---|
423 | result = flush_egress(data, sockfd, qs);
|
---|
424 | if(result)
|
---|
425 | goto error;
|
---|
426 |
|
---|
427 | if(quiche_conn_is_established(qs->conn)) {
|
---|
428 | *done = TRUE;
|
---|
429 | result = quiche_has_connected(data, conn, 0, sockindex);
|
---|
430 | DEBUGF(infof(data, "quiche established connection"));
|
---|
431 | }
|
---|
432 |
|
---|
433 | return result;
|
---|
434 | error:
|
---|
435 | qs_disconnect(data, qs);
|
---|
436 | return result;
|
---|
437 | }
|
---|
438 |
|
---|
439 | static CURLcode process_ingress(struct Curl_easy *data, int sockfd,
|
---|
440 | struct quicsocket *qs)
|
---|
441 | {
|
---|
442 | ssize_t recvd;
|
---|
443 | uint8_t *buf = (uint8_t *)data->state.buffer;
|
---|
444 | size_t bufsize = data->set.buffer_size;
|
---|
445 | struct sockaddr_storage from;
|
---|
446 | socklen_t from_len;
|
---|
447 | quiche_recv_info recv_info;
|
---|
448 |
|
---|
449 | DEBUGASSERT(qs->conn);
|
---|
450 |
|
---|
451 | /* in case the timeout expired */
|
---|
452 | quiche_conn_on_timeout(qs->conn);
|
---|
453 |
|
---|
454 | do {
|
---|
455 | from_len = sizeof(from);
|
---|
456 |
|
---|
457 | recvd = recvfrom(sockfd, buf, bufsize, 0,
|
---|
458 | (struct sockaddr *)&from, &from_len);
|
---|
459 |
|
---|
460 | if((recvd < 0) && ((SOCKERRNO == EAGAIN) || (SOCKERRNO == EWOULDBLOCK)))
|
---|
461 | break;
|
---|
462 |
|
---|
463 | if(recvd < 0) {
|
---|
464 | failf(data, "quiche: recvfrom() unexpectedly returned %zd "
|
---|
465 | "(errno: %d, socket %d)", recvd, SOCKERRNO, sockfd);
|
---|
466 | return CURLE_RECV_ERROR;
|
---|
467 | }
|
---|
468 |
|
---|
469 | recv_info.from = (struct sockaddr *) &from;
|
---|
470 | recv_info.from_len = from_len;
|
---|
471 |
|
---|
472 | recvd = quiche_conn_recv(qs->conn, buf, recvd, &recv_info);
|
---|
473 | if(recvd == QUICHE_ERR_DONE)
|
---|
474 | break;
|
---|
475 |
|
---|
476 | if(recvd < 0) {
|
---|
477 | if(QUICHE_ERR_TLS_FAIL == recvd) {
|
---|
478 | long verify_ok = SSL_get_verify_result(qs->ssl);
|
---|
479 | if(verify_ok != X509_V_OK) {
|
---|
480 | failf(data, "SSL certificate problem: %s",
|
---|
481 | X509_verify_cert_error_string(verify_ok));
|
---|
482 |
|
---|
483 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | failf(data, "quiche_conn_recv() == %zd", recvd);
|
---|
488 |
|
---|
489 | return CURLE_RECV_ERROR;
|
---|
490 | }
|
---|
491 | } while(1);
|
---|
492 |
|
---|
493 | return CURLE_OK;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * flush_egress drains the buffers and sends off data.
|
---|
498 | * Calls failf() on errors.
|
---|
499 | */
|
---|
500 | static CURLcode flush_egress(struct Curl_easy *data, int sockfd,
|
---|
501 | struct quicsocket *qs)
|
---|
502 | {
|
---|
503 | ssize_t sent;
|
---|
504 | uint8_t out[1200];
|
---|
505 | int64_t timeout_ns;
|
---|
506 | quiche_send_info send_info;
|
---|
507 |
|
---|
508 | do {
|
---|
509 | sent = quiche_conn_send(qs->conn, out, sizeof(out), &send_info);
|
---|
510 | if(sent == QUICHE_ERR_DONE)
|
---|
511 | break;
|
---|
512 |
|
---|
513 | if(sent < 0) {
|
---|
514 | failf(data, "quiche_conn_send returned %zd", sent);
|
---|
515 | return CURLE_SEND_ERROR;
|
---|
516 | }
|
---|
517 |
|
---|
518 | sent = send(sockfd, out, sent, 0);
|
---|
519 | if(sent < 0) {
|
---|
520 | failf(data, "send() returned %zd", sent);
|
---|
521 | return CURLE_SEND_ERROR;
|
---|
522 | }
|
---|
523 | } while(1);
|
---|
524 |
|
---|
525 | /* time until the next timeout event, as nanoseconds. */
|
---|
526 | timeout_ns = quiche_conn_timeout_as_nanos(qs->conn);
|
---|
527 | if(timeout_ns)
|
---|
528 | /* expire uses milliseconds */
|
---|
529 | Curl_expire(data, (timeout_ns + 999999) / 1000000, EXPIRE_QUIC);
|
---|
530 |
|
---|
531 | return CURLE_OK;
|
---|
532 | }
|
---|
533 |
|
---|
534 | struct h3h1header {
|
---|
535 | char *dest;
|
---|
536 | size_t destlen; /* left to use */
|
---|
537 | size_t nlen; /* used */
|
---|
538 | };
|
---|
539 |
|
---|
540 | static int cb_each_header(uint8_t *name, size_t name_len,
|
---|
541 | uint8_t *value, size_t value_len,
|
---|
542 | void *argp)
|
---|
543 | {
|
---|
544 | struct h3h1header *headers = (struct h3h1header *)argp;
|
---|
545 | size_t olen = 0;
|
---|
546 |
|
---|
547 | if((name_len == 7) && !strncmp(H2H3_PSEUDO_STATUS, (char *)name, 7)) {
|
---|
548 | msnprintf(headers->dest,
|
---|
549 | headers->destlen, "HTTP/3 %.*s\n",
|
---|
550 | (int) value_len, value);
|
---|
551 | }
|
---|
552 | else if(!headers->nlen) {
|
---|
553 | return CURLE_HTTP3;
|
---|
554 | }
|
---|
555 | else {
|
---|
556 | msnprintf(headers->dest,
|
---|
557 | headers->destlen, "%.*s: %.*s\n",
|
---|
558 | (int)name_len, name, (int) value_len, value);
|
---|
559 | }
|
---|
560 | olen = strlen(headers->dest);
|
---|
561 | headers->destlen -= olen;
|
---|
562 | headers->nlen += olen;
|
---|
563 | headers->dest += olen;
|
---|
564 | return 0;
|
---|
565 | }
|
---|
566 |
|
---|
567 | static ssize_t h3_stream_recv(struct Curl_easy *data,
|
---|
568 | int sockindex,
|
---|
569 | char *buf,
|
---|
570 | size_t buffersize,
|
---|
571 | CURLcode *curlcode)
|
---|
572 | {
|
---|
573 | ssize_t recvd = -1;
|
---|
574 | ssize_t rcode;
|
---|
575 | struct connectdata *conn = data->conn;
|
---|
576 | struct quicsocket *qs = conn->quic;
|
---|
577 | curl_socket_t sockfd = conn->sock[sockindex];
|
---|
578 | quiche_h3_event *ev;
|
---|
579 | int rc;
|
---|
580 | struct h3h1header headers;
|
---|
581 | struct HTTP *stream = data->req.p.http;
|
---|
582 | headers.dest = buf;
|
---|
583 | headers.destlen = buffersize;
|
---|
584 | headers.nlen = 0;
|
---|
585 |
|
---|
586 | if(process_ingress(data, sockfd, qs)) {
|
---|
587 | infof(data, "h3_stream_recv returns on ingress");
|
---|
588 | *curlcode = CURLE_RECV_ERROR;
|
---|
589 | return -1;
|
---|
590 | }
|
---|
591 |
|
---|
592 | if(qs->h3_recving) {
|
---|
593 | /* body receiving state */
|
---|
594 | rcode = quiche_h3_recv_body(qs->h3c, qs->conn, stream->stream3_id,
|
---|
595 | (unsigned char *)buf, buffersize);
|
---|
596 | if(rcode <= 0) {
|
---|
597 | recvd = -1;
|
---|
598 | qs->h3_recving = FALSE;
|
---|
599 | /* fall through into the while loop below */
|
---|
600 | }
|
---|
601 | else
|
---|
602 | recvd = rcode;
|
---|
603 | }
|
---|
604 |
|
---|
605 | while(recvd < 0) {
|
---|
606 | int64_t s = quiche_h3_conn_poll(qs->h3c, qs->conn, &ev);
|
---|
607 | if(s < 0)
|
---|
608 | /* nothing more to do */
|
---|
609 | break;
|
---|
610 |
|
---|
611 | if(s != stream->stream3_id) {
|
---|
612 | /* another transfer, ignore for now */
|
---|
613 | infof(data, "Got h3 for stream %u, expects %u",
|
---|
614 | s, stream->stream3_id);
|
---|
615 | continue;
|
---|
616 | }
|
---|
617 |
|
---|
618 | switch(quiche_h3_event_type(ev)) {
|
---|
619 | case QUICHE_H3_EVENT_HEADERS:
|
---|
620 | rc = quiche_h3_event_for_each_header(ev, cb_each_header, &headers);
|
---|
621 | if(rc) {
|
---|
622 | *curlcode = rc;
|
---|
623 | failf(data, "Error in HTTP/3 response header");
|
---|
624 | break;
|
---|
625 | }
|
---|
626 | recvd = headers.nlen;
|
---|
627 | break;
|
---|
628 | case QUICHE_H3_EVENT_DATA:
|
---|
629 | if(!stream->firstbody) {
|
---|
630 | /* add a header-body separator CRLF */
|
---|
631 | buf[0] = '\r';
|
---|
632 | buf[1] = '\n';
|
---|
633 | buf += 2;
|
---|
634 | buffersize -= 2;
|
---|
635 | stream->firstbody = TRUE;
|
---|
636 | recvd = 2; /* two bytes already */
|
---|
637 | }
|
---|
638 | else
|
---|
639 | recvd = 0;
|
---|
640 | rcode = quiche_h3_recv_body(qs->h3c, qs->conn, s, (unsigned char *)buf,
|
---|
641 | buffersize);
|
---|
642 | if(rcode <= 0) {
|
---|
643 | recvd = -1;
|
---|
644 | break;
|
---|
645 | }
|
---|
646 | qs->h3_recving = TRUE;
|
---|
647 | recvd += rcode;
|
---|
648 | break;
|
---|
649 |
|
---|
650 | case QUICHE_H3_EVENT_RESET:
|
---|
651 | streamclose(conn, "Stream reset");
|
---|
652 | *curlcode = CURLE_PARTIAL_FILE;
|
---|
653 | return -1;
|
---|
654 |
|
---|
655 | case QUICHE_H3_EVENT_FINISHED:
|
---|
656 | streamclose(conn, "End of stream");
|
---|
657 | recvd = 0; /* end of stream */
|
---|
658 | break;
|
---|
659 | default:
|
---|
660 | break;
|
---|
661 | }
|
---|
662 |
|
---|
663 | quiche_h3_event_free(ev);
|
---|
664 | }
|
---|
665 | if(flush_egress(data, sockfd, qs)) {
|
---|
666 | *curlcode = CURLE_SEND_ERROR;
|
---|
667 | return -1;
|
---|
668 | }
|
---|
669 |
|
---|
670 | *curlcode = (-1 == recvd)? CURLE_AGAIN : CURLE_OK;
|
---|
671 | if(recvd >= 0)
|
---|
672 | /* Get this called again to drain the event queue */
|
---|
673 | Curl_expire(data, 0, EXPIRE_QUIC);
|
---|
674 |
|
---|
675 | data->state.drain = (recvd >= 0) ? 1 : 0;
|
---|
676 | return recvd;
|
---|
677 | }
|
---|
678 |
|
---|
679 | static ssize_t h3_stream_send(struct Curl_easy *data,
|
---|
680 | int sockindex,
|
---|
681 | const void *mem,
|
---|
682 | size_t len,
|
---|
683 | CURLcode *curlcode)
|
---|
684 | {
|
---|
685 | ssize_t sent;
|
---|
686 | struct connectdata *conn = data->conn;
|
---|
687 | struct quicsocket *qs = conn->quic;
|
---|
688 | curl_socket_t sockfd = conn->sock[sockindex];
|
---|
689 | struct HTTP *stream = data->req.p.http;
|
---|
690 |
|
---|
691 | if(!stream->h3req) {
|
---|
692 | CURLcode result = http_request(data, mem, len);
|
---|
693 | if(result) {
|
---|
694 | *curlcode = CURLE_SEND_ERROR;
|
---|
695 | return -1;
|
---|
696 | }
|
---|
697 | sent = len;
|
---|
698 | }
|
---|
699 | else {
|
---|
700 | sent = quiche_h3_send_body(qs->h3c, qs->conn, stream->stream3_id,
|
---|
701 | (uint8_t *)mem, len, FALSE);
|
---|
702 | if(sent == QUICHE_H3_ERR_DONE) {
|
---|
703 | sent = 0;
|
---|
704 | }
|
---|
705 | else if(sent < 0) {
|
---|
706 | *curlcode = CURLE_SEND_ERROR;
|
---|
707 | return -1;
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | if(flush_egress(data, sockfd, qs)) {
|
---|
712 | *curlcode = CURLE_SEND_ERROR;
|
---|
713 | return -1;
|
---|
714 | }
|
---|
715 |
|
---|
716 | *curlcode = CURLE_OK;
|
---|
717 | return sent;
|
---|
718 | }
|
---|
719 |
|
---|
720 | /*
|
---|
721 | * Store quiche version info in this buffer.
|
---|
722 | */
|
---|
723 | void Curl_quic_ver(char *p, size_t len)
|
---|
724 | {
|
---|
725 | (void)msnprintf(p, len, "quiche/%s", quiche_version());
|
---|
726 | }
|
---|
727 |
|
---|
728 | /* Index where :authority header field will appear in request header
|
---|
729 | field list. */
|
---|
730 | #define AUTHORITY_DST_IDX 3
|
---|
731 |
|
---|
732 | static CURLcode http_request(struct Curl_easy *data, const void *mem,
|
---|
733 | size_t len)
|
---|
734 | {
|
---|
735 | struct connectdata *conn = data->conn;
|
---|
736 | struct HTTP *stream = data->req.p.http;
|
---|
737 | size_t nheader;
|
---|
738 | int64_t stream3_id;
|
---|
739 | quiche_h3_header *nva = NULL;
|
---|
740 | struct quicsocket *qs = conn->quic;
|
---|
741 | CURLcode result = CURLE_OK;
|
---|
742 | struct h2h3req *hreq = NULL;
|
---|
743 |
|
---|
744 | stream->h3req = TRUE; /* senf off! */
|
---|
745 |
|
---|
746 | result = Curl_pseudo_headers(data, mem, len, &hreq);
|
---|
747 | if(result)
|
---|
748 | goto fail;
|
---|
749 | nheader = hreq->entries;
|
---|
750 |
|
---|
751 | nva = malloc(sizeof(quiche_h3_header) * nheader);
|
---|
752 | if(!nva) {
|
---|
753 | result = CURLE_OUT_OF_MEMORY;
|
---|
754 | goto fail;
|
---|
755 | }
|
---|
756 | else {
|
---|
757 | unsigned int i;
|
---|
758 | for(i = 0; i < nheader; i++) {
|
---|
759 | nva[i].name = (unsigned char *)hreq->header[i].name;
|
---|
760 | nva[i].name_len = hreq->header[i].namelen;
|
---|
761 | nva[i].value = (unsigned char *)hreq->header[i].value;
|
---|
762 | nva[i].value_len = hreq->header[i].valuelen;
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | switch(data->state.httpreq) {
|
---|
767 | case HTTPREQ_POST:
|
---|
768 | case HTTPREQ_POST_FORM:
|
---|
769 | case HTTPREQ_POST_MIME:
|
---|
770 | case HTTPREQ_PUT:
|
---|
771 | if(data->state.infilesize != -1)
|
---|
772 | stream->upload_left = data->state.infilesize;
|
---|
773 | else
|
---|
774 | /* data sending without specifying the data amount up front */
|
---|
775 | stream->upload_left = -1; /* unknown, but not zero */
|
---|
776 |
|
---|
777 | stream3_id = quiche_h3_send_request(qs->h3c, qs->conn, nva, nheader,
|
---|
778 | stream->upload_left ? FALSE: TRUE);
|
---|
779 | if((stream3_id >= 0) && data->set.postfields) {
|
---|
780 | ssize_t sent = quiche_h3_send_body(qs->h3c, qs->conn, stream3_id,
|
---|
781 | (uint8_t *)data->set.postfields,
|
---|
782 | stream->upload_left, TRUE);
|
---|
783 | if(sent <= 0) {
|
---|
784 | failf(data, "quiche_h3_send_body failed");
|
---|
785 | result = CURLE_SEND_ERROR;
|
---|
786 | }
|
---|
787 | stream->upload_left = 0; /* nothing left to send */
|
---|
788 | }
|
---|
789 | break;
|
---|
790 | default:
|
---|
791 | stream3_id = quiche_h3_send_request(qs->h3c, qs->conn, nva, nheader,
|
---|
792 | TRUE);
|
---|
793 | break;
|
---|
794 | }
|
---|
795 |
|
---|
796 | Curl_safefree(nva);
|
---|
797 |
|
---|
798 | if(stream3_id < 0) {
|
---|
799 | H3BUGF(infof(data, "quiche_h3_send_request returned %d",
|
---|
800 | stream3_id));
|
---|
801 | result = CURLE_SEND_ERROR;
|
---|
802 | goto fail;
|
---|
803 | }
|
---|
804 |
|
---|
805 | infof(data, "Using HTTP/3 Stream ID: %x (easy handle %p)",
|
---|
806 | stream3_id, (void *)data);
|
---|
807 | stream->stream3_id = stream3_id;
|
---|
808 |
|
---|
809 | Curl_pseudo_free(hreq);
|
---|
810 | return CURLE_OK;
|
---|
811 |
|
---|
812 | fail:
|
---|
813 | free(nva);
|
---|
814 | Curl_pseudo_free(hreq);
|
---|
815 | return result;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /*
|
---|
819 | * Called from transfer.c:done_sending when we stop HTTP/3 uploading.
|
---|
820 | */
|
---|
821 | CURLcode Curl_quic_done_sending(struct Curl_easy *data)
|
---|
822 | {
|
---|
823 | struct connectdata *conn = data->conn;
|
---|
824 | DEBUGASSERT(conn);
|
---|
825 | if(conn->handler == &Curl_handler_http3) {
|
---|
826 | /* only for HTTP/3 transfers */
|
---|
827 | ssize_t sent;
|
---|
828 | struct HTTP *stream = data->req.p.http;
|
---|
829 | struct quicsocket *qs = conn->quic;
|
---|
830 | stream->upload_done = TRUE;
|
---|
831 | sent = quiche_h3_send_body(qs->h3c, qs->conn, stream->stream3_id,
|
---|
832 | NULL, 0, TRUE);
|
---|
833 | if(sent < 0)
|
---|
834 | return CURLE_SEND_ERROR;
|
---|
835 | }
|
---|
836 |
|
---|
837 | return CURLE_OK;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /*
|
---|
841 | * Called from http.c:Curl_http_done when a request completes.
|
---|
842 | */
|
---|
843 | void Curl_quic_done(struct Curl_easy *data, bool premature)
|
---|
844 | {
|
---|
845 | (void)data;
|
---|
846 | (void)premature;
|
---|
847 | }
|
---|
848 |
|
---|
849 | /*
|
---|
850 | * Called from transfer.c:data_pending to know if we should keep looping
|
---|
851 | * to receive more data from the connection.
|
---|
852 | */
|
---|
853 | bool Curl_quic_data_pending(const struct Curl_easy *data)
|
---|
854 | {
|
---|
855 | (void)data;
|
---|
856 | return FALSE;
|
---|
857 | }
|
---|
858 |
|
---|
859 | #endif
|
---|