QUIC Thread Assisted Mode: Refactor locking to be infallible
[openssl.git] / ssl / quic / quic_impl.c
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/macros.h>
11 #include <openssl/objects.h>
12 #include <openssl/sslerr.h>
13 #include <crypto/rand.h>
14 #include "quic_local.h"
15 #include "internal/quic_tls.h"
16 #include "internal/quic_rx_depack.h"
17 #include "internal/quic_error.h"
18 #include "internal/time.h"
19
20 static void aon_write_finish(QUIC_CONNECTION *qc);
21 static int ensure_channel(QUIC_CONNECTION *qc);
22
23 /*
24  * QUIC Front-End I/O API: Common Utilities
25  * ========================================
26  */
27
28 /*
29  * Block until a predicate is met.
30  *
31  * Precondition: Must have a channel.
32  * Precondition: Must hold channel lock (unchecked).
33  */
34 QUIC_NEEDS_LOCK
35 static int block_until_pred(QUIC_CONNECTION *qc,
36                             int (*pred)(void *arg), void *pred_arg,
37                             uint32_t flags)
38 {
39     QUIC_REACTOR *rtor;
40
41     assert(qc->ch != NULL);
42
43     rtor = ossl_quic_channel_get_reactor(qc->ch);
44     return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
45                                               qc->mutex);
46 }
47
48 /*
49  * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
50  * rather than via ERR.
51  */
52 static int quic_raise_normal_error(QUIC_CONNECTION *qc,
53                                    int err)
54 {
55     qc->last_error = err;
56     return 0;
57 }
58
59 /*
60  * Raise a 'non-normal' error, meaning any error that is not reported via
61  * SSL_get_error() and must be reported via ERR.
62  */
63 static int quic_raise_non_normal_error(QUIC_CONNECTION *qc,
64                                        const char *file,
65                                        int line,
66                                        const char *func,
67                                        int reason,
68                                        const char *fmt,
69                                        ...)
70 {
71     va_list args;
72
73     ERR_new();
74     ERR_set_debug(file, line, func);
75
76     va_start(args, fmt);
77     ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
78     va_end(args);
79
80     qc->last_error = SSL_ERROR_SSL;
81     return 0;
82 }
83
84 #define QUIC_RAISE_NORMAL_ERROR(qc, err)                        \
85     quic_raise_normal_error((qc), (err))
86
87 #define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg)            \
88     quic_raise_non_normal_error((qc),                           \
89                                 OPENSSL_FILE, OPENSSL_LINE,     \
90                                 OPENSSL_FUNC,                   \
91                                 (reason),                       \
92                                 (msg))
93
94 /*
95  * Should be called at entry of every public function to confirm we have a valid
96  * QUIC_CONNECTION.
97  */
98 static ossl_inline int expect_quic_conn(const QUIC_CONNECTION *qc)
99 {
100     if (!ossl_assert(qc != NULL))
101         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
102
103     return 1;
104
105 }
106
107 /*
108  * Ensures that the channel mutex is held for a method which touches channel
109  * state.
110  *
111  * Precondition: Channel mutex is not held (unchecked)
112  */
113 static void quic_lock(QUIC_CONNECTION *qc)
114 {
115     ossl_crypto_mutex_lock(qc->mutex);
116 }
117
118 /* Precondition: Channel mutex is held (unchecked) */
119 QUIC_NEEDS_LOCK
120 static void quic_unlock(QUIC_CONNECTION *qc)
121 {
122     ossl_crypto_mutex_unlock(qc->mutex);
123 }
124
125
126 /*
127  * QUIC Front-End I/O API: Initialization
128  * ======================================
129  *
130  *         SSL_new                  => ossl_quic_new
131  *                                     ossl_quic_init
132  *         SSL_reset                => ossl_quic_reset
133  *         SSL_clear                => ossl_quic_clear
134  *                                     ossl_quic_deinit
135  *         SSL_free                 => ossl_quic_free
136  *
137  */
138
139 /* SSL_new */
140 SSL *ossl_quic_new(SSL_CTX *ctx)
141 {
142     QUIC_CONNECTION *qc = NULL;
143     SSL *ssl_base = NULL;
144     SSL_CONNECTION *sc = NULL;
145
146     qc = OPENSSL_zalloc(sizeof(*qc));
147     if (qc == NULL)
148         goto err;
149
150     /* Initialise the QUIC_CONNECTION's stub header. */
151     ssl_base = &qc->ssl;
152     if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
153         ssl_base = NULL;
154         goto err;
155     }
156
157     qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
158     if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
159          goto err;
160
161     if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
162         goto err;
163
164     qc->is_thread_assisted
165         = (ssl_base->method == OSSL_QUIC_client_thread_method());
166
167     /* Channel is not created yet. */
168     qc->ssl_mode   = qc->ssl.ctx->mode;
169     qc->last_error = SSL_ERROR_NONE;
170     qc->blocking   = 1;
171
172     return ssl_base;
173
174 err:
175     SSL_free(qc->tls);
176     OPENSSL_free(qc);
177     return NULL;
178 }
179
180 /* SSL_free */
181 QUIC_TAKES_LOCK
182 void ossl_quic_free(SSL *s)
183 {
184     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
185
186     /* We should never be called on anything but a QUIC_CONNECTION. */
187     if (!expect_quic_conn(qc))
188         return;
189
190     quic_lock(qc);
191
192     if (qc->is_thread_assisted && qc->started) {
193         ossl_quic_thread_assist_wait_stopped(&qc->thread_assist);
194         ossl_quic_thread_assist_cleanup(&qc->thread_assist);
195     }
196
197     ossl_quic_channel_free(qc->ch);
198
199     BIO_free(qc->net_rbio);
200     BIO_free(qc->net_wbio);
201
202     /* Note: SSL_free calls OPENSSL_free(qc) for us */
203
204     SSL_free(qc->tls);
205     ossl_crypto_mutex_free(&qc->mutex);
206 }
207
208 /* SSL method init */
209 int ossl_quic_init(SSL *s)
210 {
211     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
212
213     if (!expect_quic_conn(qc))
214         return 0;
215
216     /* Same op as SSL_clear, forward the call. */
217     return ossl_quic_clear(s);
218 }
219
220 /* SSL method deinit */
221 void ossl_quic_deinit(SSL *s)
222 {
223     /* No-op. */
224 }
225
226 /* SSL_reset */
227 int ossl_quic_reset(SSL *s)
228 {
229     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
230
231     if (!expect_quic_conn(qc))
232         return 0;
233
234     /* TODO(QUIC); Currently a no-op. */
235     return 1;
236 }
237
238 /* SSL_clear */
239 int ossl_quic_clear(SSL *s)
240 {
241     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
242
243     if (!expect_quic_conn(qc))
244         return 0;
245
246     /* TODO(QUIC): Currently a no-op. */
247     return 1;
248 }
249
250 void ossl_quic_conn_set_override_now_cb(SSL *s,
251                                         OSSL_TIME (*now_cb)(void *arg),
252                                         void *now_cb_arg)
253 {
254     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
255
256     qc->override_now_cb     = now_cb;
257     qc->override_now_cb_arg = now_cb_arg;
258 }
259
260 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
261 {
262     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
263
264     if (qc->is_thread_assisted && qc->started)
265         ossl_quic_thread_assist_notify_deadline_changed(&qc->thread_assist);
266 }
267
268 /*
269  * QUIC Front-End I/O API: Network BIO Configuration
270  * =================================================
271  *
272  * Handling the different BIOs is difficult:
273  *
274  *   - It is more or less a requirement that we use non-blocking network I/O;
275  *     we need to be able to have timeouts on recv() calls, and make best effort
276  *     (non blocking) send() and recv() calls.
277  *
278  *     The only sensible way to do this is to configure the socket into
279  *     non-blocking mode. We could try to do select() before calling send() or
280  *     recv() to get a guarantee that the call will not block, but this will
281  *     probably run into issues with buggy OSes which generate spurious socket
282  *     readiness events. In any case, relying on this to work reliably does not
283  *     seem sane.
284  *
285  *     Timeouts could be handled via setsockopt() socket timeout options, but
286  *     this depends on OS support and adds another syscall to every network I/O
287  *     operation. It also has obvious thread safety concerns if we want to move
288  *     to concurrent use of a single socket at some later date.
289  *
290  *     Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
291  *     be made non-blocking. However some OSes (e.g. Windows) do not support
292  *     this, so we cannot rely on this.
293  *
294  *     As such, we need to configure any FD in non-blocking mode. This may
295  *     confound users who pass a blocking socket to libssl. However, in practice
296  *     it would be extremely strange for a user of QUIC to pass an FD to us,
297  *     then also try and send receive traffic on the same socket(!). Thus the
298  *     impact of this should be limited, and can be documented.
299  *
300  *   - We support both blocking and non-blocking operation in terms of the API
301  *     presented to the user. One prospect is to set the blocking mode based on
302  *     whether the socket passed to us was already in blocking mode. However,
303  *     Windows has no API for determining if a socket is in blocking mode (!),
304  *     therefore this cannot be done portably. Currently therefore we expose an
305  *     explicit API call to set this, and default to blocking mode.
306  *
307  *   - We need to determine our initial destination UDP address. The "natural"
308  *     way for a user to do this is to set the peer variable on a BIO_dgram.
309  *     However, this has problems because BIO_dgram's peer variable is used for
310  *     both transmission and reception. This means it can be constantly being
311  *     changed to a malicious value (e.g. if some random unrelated entity on the
312  *     network starts sending traffic to us) on every read call. This is not a
313  *     direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
314  *     calls only, which do not use this variable. However, we do need to let
315  *     the user specify the peer in a 'normal' manner. The compromise here is
316  *     that we grab the current peer value set at the time the write BIO is set
317  *     and do not read the value again.
318  *
319  *   - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
320  *     Currently we do this by only supporting non-blocking mode.
321  *
322  */
323
324 /*
325  * Determines what initial destination UDP address we should use, if possible.
326  * If this fails the client must set the destination address manually, or use a
327  * BIO which does not need a destination address.
328  */
329 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
330 {
331     if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
332         return 0;
333
334     return 1;
335 }
336
337 void ossl_quic_conn_set0_net_rbio(QUIC_CONNECTION *qc, BIO *net_rbio)
338 {
339     if (qc->net_rbio == net_rbio)
340         return;
341
342     if (qc->ch != NULL && !ossl_quic_channel_set_net_rbio(qc->ch, net_rbio))
343         return;
344
345     BIO_free(qc->net_rbio);
346     qc->net_rbio = net_rbio;
347
348     /*
349      * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
350      * mode as we do not support it for non-pollable BIOs.
351      */
352     if (net_rbio != NULL) {
353         BIO_POLL_DESCRIPTOR d = {0};
354
355         if (!BIO_get_rpoll_descriptor(net_rbio, &d)
356             || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
357             qc->blocking = 0;
358             qc->can_poll_net_rbio = 0;
359         } else {
360             qc->can_poll_net_rbio = 1;
361         }
362     }
363 }
364
365 void ossl_quic_conn_set0_net_wbio(QUIC_CONNECTION *qc, BIO *net_wbio)
366 {
367     if (qc->net_wbio == net_wbio)
368         return;
369
370     if (qc->ch != NULL && !ossl_quic_channel_set_net_wbio(qc->ch, net_wbio))
371         return;
372
373     BIO_free(qc->net_wbio);
374     qc->net_wbio = net_wbio;
375
376     if (net_wbio != NULL) {
377         BIO_POLL_DESCRIPTOR d = {0};
378
379         if (!BIO_get_wpoll_descriptor(net_wbio, &d)
380             || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
381             qc->blocking = 0;
382             qc->can_poll_net_wbio = 0;
383         } else {
384             qc->can_poll_net_wbio = 1;
385         }
386
387         /*
388          * If we do not have a peer address yet, and we have not started trying
389          * to connect yet, try to autodetect one.
390          */
391         if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC
392             && !qc->started) {
393             if (!csm_analyse_init_peer_addr(net_wbio, &qc->init_peer_addr))
394                 /* best effort */
395                 BIO_ADDR_clear(&qc->init_peer_addr);
396
397             if (qc->ch != NULL)
398                 ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
399         }
400     }
401 }
402
403 BIO *ossl_quic_conn_get_net_rbio(const QUIC_CONNECTION *qc)
404 {
405     return qc->net_rbio;
406 }
407
408 BIO *ossl_quic_conn_get_net_wbio(const QUIC_CONNECTION *qc)
409 {
410     return qc->net_wbio;
411 }
412
413 int ossl_quic_conn_get_blocking_mode(const QUIC_CONNECTION *qc)
414 {
415     return qc->blocking;
416 }
417
418 int ossl_quic_conn_set_blocking_mode(QUIC_CONNECTION *qc, int blocking)
419 {
420     /* Cannot enable blocking mode if we do not have pollable FDs. */
421     if (blocking != 0 &&
422         (!qc->can_poll_net_rbio || !qc->can_poll_net_wbio))
423         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_UNSUPPORTED, NULL);
424
425     qc->blocking = (blocking != 0);
426     return 1;
427 }
428
429 int ossl_quic_conn_set_initial_peer_addr(QUIC_CONNECTION *qc,
430                                          const BIO_ADDR *peer_addr)
431 {
432     if (qc->started)
433         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
434                                            NULL);
435
436     if (peer_addr == NULL) {
437         BIO_ADDR_clear(&qc->init_peer_addr);
438         return 1;
439     }
440
441     qc->init_peer_addr = *peer_addr;
442     return 1;
443 }
444
445 /*
446  * QUIC Front-End I/O API: Asynchronous I/O Management
447  * ===================================================
448  *
449  *   (BIO/)SSL_tick                 => ossl_quic_tick
450  *   (BIO/)SSL_get_tick_timeout     => ossl_quic_get_tick_timeout
451  *   (BIO/)SSL_get_poll_fd          => ossl_quic_get_poll_fd
452  *
453  */
454
455 /* Returns 1 if the connection is being used in blocking mode. */
456 static int blocking_mode(const QUIC_CONNECTION *qc)
457 {
458     return qc->blocking;
459 }
460
461 /* SSL_tick; ticks the reactor. */
462 QUIC_TAKES_LOCK
463 int ossl_quic_tick(QUIC_CONNECTION *qc)
464 {
465     quic_lock(qc);
466
467     if (qc->ch == NULL) {
468         quic_unlock(qc);
469         return 1;
470     }
471
472     ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
473     quic_unlock(qc);
474     return 1;
475 }
476
477 /*
478  * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object
479  * should be ticked by the application by calling SSL_tick(). tv is set to 0 if
480  * the object should be ticked immediately and tv->tv_sec is set to -1 if no
481  * timeout is currently active.
482  */
483 QUIC_TAKES_LOCK
484 int ossl_quic_get_tick_timeout(QUIC_CONNECTION *qc, struct timeval *tv)
485 {
486     OSSL_TIME deadline = ossl_time_infinite();
487
488     quic_lock(qc);
489
490     if (qc->ch != NULL)
491         deadline
492             = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(qc->ch));
493
494     if (ossl_time_is_infinite(deadline)) {
495         tv->tv_sec  = -1;
496         tv->tv_usec = 0;
497         quic_unlock(qc);
498         return 1;
499     }
500
501     *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now()));
502     quic_unlock(qc);
503     return 1;
504 }
505
506 /* SSL_get_rpoll_descriptor */
507 int ossl_quic_get_rpoll_descriptor(QUIC_CONNECTION *qc, BIO_POLL_DESCRIPTOR *desc)
508 {
509     if (desc == NULL || qc->net_rbio == NULL)
510         return 0;
511
512     return BIO_get_rpoll_descriptor(qc->net_rbio, desc);
513 }
514
515 /* SSL_get_wpoll_descriptor */
516 int ossl_quic_get_wpoll_descriptor(QUIC_CONNECTION *qc, BIO_POLL_DESCRIPTOR *desc)
517 {
518     if (desc == NULL || qc->net_wbio == NULL)
519         return 0;
520
521     return BIO_get_wpoll_descriptor(qc->net_wbio, desc);
522 }
523
524 /* SSL_net_read_desired */
525 QUIC_TAKES_LOCK
526 int ossl_quic_get_net_read_desired(QUIC_CONNECTION *qc)
527 {
528     int ret;
529
530     quic_lock(qc);
531
532     if (qc->ch == NULL)
533         return 0;
534
535     ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(qc->ch));
536     quic_unlock(qc);
537     return ret;
538 }
539
540 /* SSL_net_write_desired */
541 QUIC_TAKES_LOCK
542 int ossl_quic_get_net_write_desired(QUIC_CONNECTION *qc)
543 {
544     int ret;
545
546     quic_lock(qc);
547
548     if (qc->ch == NULL)
549         return 0;
550
551     ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(qc->ch));
552     quic_unlock(qc);
553     return ret;
554 }
555
556 /*
557  * QUIC Front-End I/O API: Connection Lifecycle Operations
558  * =======================================================
559  *
560  *         SSL_do_handshake         => ossl_quic_do_handshake
561  *         SSL_set_connect_state    => ossl_quic_set_connect_state
562  *         SSL_set_accept_state     => ossl_quic_set_accept_state
563  *         SSL_shutdown             => ossl_quic_shutdown
564  *         SSL_ctrl                 => ossl_quic_ctrl
565  *   (BIO/)SSL_connect              => ossl_quic_connect
566  *   (BIO/)SSL_accept               => ossl_quic_accept
567  *
568  */
569
570 /* SSL_shutdown */
571 static int quic_shutdown_wait(void *arg)
572 {
573     QUIC_CONNECTION *qc = arg;
574
575     return qc->ch == NULL || ossl_quic_channel_is_terminated(qc->ch);
576 }
577
578 QUIC_TAKES_LOCK
579 int ossl_quic_conn_shutdown(QUIC_CONNECTION *qc, uint64_t flags,
580                             const SSL_SHUTDOWN_EX_ARGS *args,
581                             size_t args_len)
582 {
583     int ret;
584
585     quic_lock(qc);
586
587     if (!ensure_channel(qc)) {
588         quic_unlock(qc);
589         return -1;
590     }
591
592     ossl_quic_channel_local_close(qc->ch,
593                                   args != NULL ? args->quic_error_code : 0);
594
595     /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
596
597     if (ossl_quic_channel_is_terminated(qc->ch)) {
598         quic_unlock(qc);
599         return 1;
600     }
601
602     if (blocking_mode(qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
603         block_until_pred(qc, quic_shutdown_wait, qc, 0);
604     else
605         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
606
607     ret = ossl_quic_channel_is_terminated(qc->ch);
608     quic_unlock(qc);
609     return ret;
610 }
611
612 /* SSL_ctrl */
613 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
614 {
615     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
616
617     if (!expect_quic_conn(qc))
618         return 0;
619
620     switch (cmd) {
621     case SSL_CTRL_MODE:
622         /* Cannot enable EPW while AON write in progress. */
623         if (qc->aon_write_in_progress)
624             larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
625
626         qc->ssl_mode |= (uint32_t)larg;
627         return qc->ssl_mode;
628     case SSL_CTRL_CLEAR_MODE:
629         qc->ssl_mode &= ~(uint32_t)larg;
630         return qc->ssl_mode;
631     default:
632         /* Probably a TLS related ctrl. Defer to our internal SSL object */
633         return SSL_ctrl(qc->tls, cmd, larg, parg);
634     }
635 }
636
637 /* SSL_set_connect_state */
638 void ossl_quic_set_connect_state(QUIC_CONNECTION *qc)
639 {
640     /* Cannot be changed after handshake started */
641     if (qc->started)
642         return;
643
644     qc->as_server = 0;
645 }
646
647 /* SSL_set_accept_state */
648 void ossl_quic_set_accept_state(QUIC_CONNECTION *qc)
649 {
650     /* Cannot be changed after handshake started */
651     if (qc->started)
652         return;
653
654     qc->as_server = 1;
655 }
656
657 /* SSL_do_handshake */
658 struct quic_handshake_wait_args {
659     QUIC_CONNECTION     *qc;
660 };
661
662 static int quic_handshake_wait(void *arg)
663 {
664     struct quic_handshake_wait_args *args = arg;
665
666     if (!ossl_quic_channel_is_active(args->qc->ch))
667         return -1;
668
669     if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
670         return 1;
671
672     return 0;
673 }
674
675 static int configure_channel(QUIC_CONNECTION *qc)
676 {
677     assert(qc->ch != NULL);
678
679     if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
680         || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
681         || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
682         return 0;
683
684     return 1;
685 }
686
687 QUIC_NEEDS_LOCK
688 static int ensure_channel(QUIC_CONNECTION *qc)
689 {
690     QUIC_CHANNEL_ARGS args = {0};
691
692     if (qc->ch != NULL)
693         return 1;
694
695     args.libctx     = qc->ssl.ctx->libctx;
696     args.propq      = qc->ssl.ctx->propq;
697     args.is_server  = 0;
698     args.tls        = qc->tls;
699     args.mutex      = qc->mutex;
700     args.now_cb     = qc->override_now_cb;
701     args.now_cb_arg = qc->override_now_cb_arg;
702
703     qc->ch = ossl_quic_channel_new(&args);
704     if (qc->ch == NULL)
705         return 0;
706
707     return 1;
708 }
709
710 /*
711  * Creates a channel and configures it with the information we have accumulated
712  * via calls made to us from the application prior to starting a handshake
713  * attempt.
714  */
715 QUIC_NEEDS_LOCK
716 static int ensure_channel_and_start(QUIC_CONNECTION *qc)
717 {
718     if (!qc->started) {
719         if (!ensure_channel(qc))
720             return 0;
721
722         if (!configure_channel(qc)
723             || !ossl_quic_channel_start(qc->ch))
724             goto err;
725
726         qc->stream0 = ossl_quic_channel_get_stream_by_id(qc->ch, 0);
727         if (qc->stream0 == NULL)
728             goto err;
729
730         if (qc->is_thread_assisted)
731             if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
732                 goto err;
733     }
734
735     qc->started = 1;
736     return 1;
737
738 err:
739     ossl_quic_channel_free(qc->ch);
740     qc->ch = NULL;
741     return 0;
742 }
743
744 QUIC_NEEDS_LOCK
745 static int quic_do_handshake(QUIC_CONNECTION *qc)
746 {
747     int ret;
748
749     if (qc->ch != NULL && ossl_quic_channel_is_handshake_complete(qc->ch))
750         /* Handshake already completed. */
751         return 1;
752
753     if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
754         return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
755
756     if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
757         /* Peer address must have been set. */
758         QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
759         return -1; /* Non-protocol error */
760     }
761
762     if (qc->as_server) {
763         /* TODO(QUIC): Server mode not currently supported */
764         QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
765         return -1; /* Non-protocol error */
766     }
767
768     if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
769         /* Need read and write BIOs. */
770         QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BIO_NOT_SET, NULL);
771         return -1; /* Non-protocol error */
772     }
773
774     /*
775      * Start connection process. Note we may come here multiple times in
776      * non-blocking mode, which is fine.
777      */
778     if (!ensure_channel_and_start(qc)) {
779         QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
780         return -1; /* Non-protocol error */
781     }
782
783     if (ossl_quic_channel_is_handshake_complete(qc->ch))
784         /* The handshake is now done. */
785         return 1;
786
787     if (blocking_mode(qc)) {
788         /* In blocking mode, wait for the handshake to complete. */
789         struct quic_handshake_wait_args args;
790
791         args.qc     = qc;
792
793         ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
794         if (!ossl_quic_channel_is_active(qc->ch)) {
795             QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
796             return 0; /* Shutdown before completion */
797         } else if (ret <= 0) {
798             QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
799             return -1; /* Non-protocol error */
800         }
801
802         assert(ossl_quic_channel_is_handshake_complete(qc->ch));
803         return 1;
804     } else {
805         /* Try to advance the reactor. */
806         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
807
808         if (ossl_quic_channel_is_handshake_complete(qc->ch))
809             /* The handshake is now done. */
810             return 1;
811
812         /* Otherwise, indicate that the handshake isn't done yet. */
813         QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
814         return -1; /* Non-protocol error */
815     }
816 }
817
818 QUIC_TAKES_LOCK
819 int ossl_quic_do_handshake(QUIC_CONNECTION *qc)
820 {
821     int ret;
822
823     quic_lock(qc);
824
825     ret = quic_do_handshake(qc);
826     quic_unlock(qc);
827     return ret;
828 }
829
830 /* SSL_connect */
831 int ossl_quic_connect(SSL *s)
832 {
833     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
834
835     if (!expect_quic_conn(qc))
836         return 0;
837
838     /* Ensure we are in connect state (no-op if non-idle). */
839     ossl_quic_set_connect_state(qc);
840
841     /* Begin or continue the handshake */
842     return ossl_quic_do_handshake(qc);
843 }
844
845 /* SSL_accept */
846 int ossl_quic_accept(SSL *s)
847 {
848     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
849
850     if (!expect_quic_conn(qc))
851         return 0;
852
853     /* Ensure we are in accept state (no-op if non-idle). */
854     ossl_quic_set_accept_state(qc);
855
856     /* Begin or continue the handshake */
857     return ossl_quic_do_handshake(qc);
858 }
859
860 /*
861  * QUIC Front-End I/O API: Steady-State Operations
862  * ===============================================
863  *
864  * Here we dispatch calls to the steady-state front-end I/O API functions; that
865  * is, the functions used during the established phase of a QUIC connection
866  * (e.g. SSL_read, SSL_write).
867  *
868  * Each function must handle both blocking and non-blocking modes. As discussed
869  * above, all QUIC I/O is implemented using non-blocking mode internally.
870  *
871  *         SSL_get_error        => partially implemented by ossl_quic_get_error
872  *   (BIO/)SSL_read             => ossl_quic_read
873  *   (BIO/)SSL_write            => ossl_quic_write
874  *         SSL_pending          => ossl_quic_pending
875  *         SSL_stream_conclude  => ossl_quic_conn_stream_conclude
876  */
877
878 /* SSL_get_error */
879 int ossl_quic_get_error(const QUIC_CONNECTION *qc, int i)
880 {
881     return qc->last_error;
882 }
883
884 /*
885  * SSL_write
886  * ---------
887  *
888  * The set of functions below provide the implementation of the public SSL_write
889  * function. We must handle:
890  *
891  *   - both blocking and non-blocking operation at the application level,
892  *     depending on how we are configured;
893  *
894  *   - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
895  *
896  *   - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
897  *
898  */
899 QUIC_NEEDS_LOCK
900 static void quic_post_write(QUIC_CONNECTION *qc, int did_append, int do_tick)
901 {
902     /*
903      * We have appended at least one byte to the stream.
904      * Potentially mark stream as active, depending on FC.
905      */
906     if (did_append)
907         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
908                                           qc->stream0);
909
910     /*
911      * Try and send.
912      *
913      * TODO(QUIC): It is probably inefficient to try and do this immediately,
914      * plus we should eventually consider Nagle's algorithm.
915      */
916     if (do_tick)
917         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
918 }
919
920 struct quic_write_again_args {
921     QUIC_CONNECTION     *qc;
922     const unsigned char *buf;
923     size_t              len;
924     size_t              total_written;
925 };
926
927 QUIC_NEEDS_LOCK
928 static int quic_write_again(void *arg)
929 {
930     struct quic_write_again_args *args = arg;
931     size_t actual_written = 0;
932
933     if (!ossl_quic_channel_is_active(args->qc->ch))
934         /* If connection is torn down due to an error while blocking, stop. */
935         return -2;
936
937     if (!ossl_quic_sstream_append(args->qc->stream0->sstream,
938                                   args->buf, args->len, &actual_written))
939         return -2;
940
941     quic_post_write(args->qc, actual_written > 0, 0);
942
943     args->buf           += actual_written;
944     args->len           -= actual_written;
945     args->total_written += actual_written;
946
947     if (args->len == 0)
948         /* Written everything, done. */
949         return 1;
950
951     /* Not written everything yet, keep trying. */
952     return 0;
953 }
954
955 QUIC_NEEDS_LOCK
956 static int quic_write_blocking(QUIC_CONNECTION *qc, const void *buf, size_t len,
957                                size_t *written)
958 {
959     int res;
960     struct quic_write_again_args args;
961     size_t actual_written = 0;
962
963     /* First make a best effort to append as much of the data as possible. */
964     if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len,
965                                   &actual_written)) {
966         /* Stream already finished or allocation error. */
967         *written = 0;
968         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
969     }
970
971     quic_post_write(qc, actual_written > 0, 1);
972
973     if (actual_written == len) {
974         /* Managed to append everything on the first try. */
975         *written = actual_written;
976         return 1;
977     }
978
979     /*
980      * We did not manage to append all of the data immediately, so the stream
981      * buffer has probably filled up. This means we need to block until some of
982      * it is freed up.
983      */
984     args.qc             = qc;
985     args.buf            = (const unsigned char *)buf + actual_written;
986     args.len            = len - actual_written;
987     args.total_written  = 0;
988
989     res = block_until_pred(qc, quic_write_again, &args, 0);
990     if (res <= 0) {
991         if (!ossl_quic_channel_is_active(qc->ch))
992             return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
993         else
994             return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
995     }
996
997     *written = args.total_written;
998     return 1;
999 }
1000
1001 /*
1002  * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1003  * write semantics.
1004  */
1005 static void aon_write_begin(QUIC_CONNECTION *qc, const unsigned char *buf,
1006                             size_t buf_len, size_t already_sent)
1007 {
1008     assert(!qc->aon_write_in_progress);
1009
1010     qc->aon_write_in_progress = 1;
1011     qc->aon_buf_base          = buf;
1012     qc->aon_buf_pos           = already_sent;
1013     qc->aon_buf_len           = buf_len;
1014 }
1015
1016 static void aon_write_finish(QUIC_CONNECTION *qc)
1017 {
1018     qc->aon_write_in_progress   = 0;
1019     qc->aon_buf_base            = NULL;
1020     qc->aon_buf_pos             = 0;
1021     qc->aon_buf_len             = 0;
1022 }
1023
1024 QUIC_NEEDS_LOCK
1025 static int quic_write_nonblocking_aon(QUIC_CONNECTION *qc, const void *buf,
1026                                       size_t len, size_t *written)
1027 {
1028     const void *actual_buf;
1029     size_t actual_len, actual_written = 0;
1030     int accept_moving_buffer
1031         = ((qc->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1032
1033     if (qc->aon_write_in_progress) {
1034         /*
1035          * We are in the middle of an AON write (i.e., a previous write did not
1036          * manage to append all data to the SSTREAM and we have Enable Partial
1037          * Write (EPW) mode disabled.)
1038          */
1039         if ((!accept_moving_buffer && qc->aon_buf_base != buf)
1040             || len != qc->aon_buf_len)
1041             /*
1042              * Pointer must not have changed if we are not in accept moving
1043              * buffer mode. Length must never change.
1044              */
1045             return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BAD_WRITE_RETRY, NULL);
1046
1047         actual_buf = (unsigned char *)buf + qc->aon_buf_pos;
1048         actual_len = len - qc->aon_buf_pos;
1049         assert(actual_len > 0);
1050     } else {
1051         actual_buf = buf;
1052         actual_len = len;
1053     }
1054
1055     /* First make a best effort to append as much of the data as possible. */
1056     if (!ossl_quic_sstream_append(qc->stream0->sstream, actual_buf, actual_len,
1057                                   &actual_written)) {
1058         /* Stream already finished or allocation error. */
1059         *written = 0;
1060         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1061     }
1062
1063     quic_post_write(qc, actual_written > 0, 1);
1064
1065     if (actual_written == actual_len) {
1066         /* We have sent everything. */
1067         if (qc->aon_write_in_progress) {
1068             /*
1069              * We have sent everything, and we were in the middle of an AON
1070              * write. The output write length is the total length of the AON
1071              * buffer, not however many bytes we managed to write to the stream
1072              * in this call.
1073              */
1074             *written = qc->aon_buf_len;
1075             aon_write_finish(qc);
1076         } else {
1077             *written = actual_written;
1078         }
1079
1080         return 1;
1081     }
1082
1083     if (qc->aon_write_in_progress) {
1084         /*
1085          * AON write is in progress but we have not written everything yet. We
1086          * may have managed to send zero bytes, or some number of bytes less
1087          * than the total remaining which need to be appended during this
1088          * AON operation.
1089          */
1090         qc->aon_buf_pos += actual_written;
1091         assert(qc->aon_buf_pos < qc->aon_buf_len);
1092         return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE);
1093     }
1094
1095     /*
1096      * Not in an existing AON operation but partial write is not enabled, so we
1097      * need to begin a new AON operation. However we needn't bother if we didn't
1098      * actually append anything.
1099      */
1100     if (actual_written > 0)
1101         aon_write_begin(qc, buf, len, actual_written);
1102
1103     /*
1104      * AON - We do not publicly admit to having appended anything until AON
1105      * completes.
1106      */
1107     *written = 0;
1108     return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE);
1109 }
1110
1111 QUIC_NEEDS_LOCK
1112 static int quic_write_nonblocking_epw(QUIC_CONNECTION *qc, const void *buf, size_t len,
1113                                       size_t *written)
1114 {
1115     /* Simple best effort operation. */
1116     if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len, written)) {
1117         /* Stream already finished or allocation error. */
1118         *written = 0;
1119         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1120     }
1121
1122     quic_post_write(qc, *written > 0, 1);
1123     return 1;
1124 }
1125
1126 QUIC_TAKES_LOCK
1127 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1128 {
1129     int ret;
1130     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1131     int partial_write = ((qc->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1132
1133     *written = 0;
1134
1135     if (!expect_quic_conn(qc))
1136         return 0;
1137
1138     quic_lock(qc);
1139
1140     if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch)) {
1141         ret = QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1142         goto out;
1143     }
1144
1145     /*
1146      * If we haven't finished the handshake, try to advance it.
1147      * We don't accept writes until the handshake is completed.
1148      */
1149     if (quic_do_handshake(qc) < 1) {
1150         ret = 0;
1151         goto out;
1152     }
1153
1154     if (qc->stream0 == NULL || qc->stream0->sstream == NULL) {
1155         ret = QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1156         goto out;
1157     }
1158
1159     if (blocking_mode(qc))
1160         ret = quic_write_blocking(qc, buf, len, written);
1161     else if (partial_write)
1162         ret = quic_write_nonblocking_epw(qc, buf, len, written);
1163     else
1164         ret = quic_write_nonblocking_aon(qc, buf, len, written);
1165
1166 out:
1167     quic_unlock(qc);
1168     return ret;
1169 }
1170
1171 /*
1172  * SSL_read
1173  * --------
1174  */
1175 struct quic_read_again_args {
1176     QUIC_CONNECTION *qc;
1177     QUIC_STREAM     *stream;
1178     void            *buf;
1179     size_t          len;
1180     size_t          *bytes_read;
1181     int             peek;
1182 };
1183
1184 QUIC_NEEDS_LOCK
1185 static int quic_read_actual(QUIC_CONNECTION *qc,
1186                             QUIC_STREAM *stream,
1187                             void *buf, size_t buf_len,
1188                             size_t *bytes_read,
1189                             int peek)
1190 {
1191     int is_fin = 0;
1192
1193     /* If the receive part of the stream is over, issue EOF. */
1194     if (stream->recv_fin_retired)
1195         return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_ZERO_RETURN);
1196
1197     if (stream->rstream == NULL)
1198         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1199
1200     if (peek) {
1201         if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1202                                     bytes_read, &is_fin))
1203             return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1204
1205     } else {
1206         if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1207                                     bytes_read, &is_fin))
1208             return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1209     }
1210
1211     if (!peek) {
1212         if (*bytes_read > 0) {
1213             /*
1214              * We have read at least one byte from the stream. Inform stream-level
1215              * RXFC of the retirement of controlled bytes. Update the active stream
1216              * status (the RXFC may now want to emit a frame granting more credit to
1217              * the peer).
1218              */
1219             OSSL_RTT_INFO rtt_info;
1220
1221             ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1222
1223             if (!ossl_quic_rxfc_on_retire(&qc->stream0->rxfc, *bytes_read,
1224                                           rtt_info.smoothed_rtt))
1225                 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1226         }
1227
1228         if (is_fin)
1229             stream->recv_fin_retired = 1;
1230
1231         if (*bytes_read > 0)
1232             ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1233                                               qc->stream0);
1234     }
1235
1236     return 1;
1237 }
1238
1239 QUIC_NEEDS_LOCK
1240 static int quic_read_again(void *arg)
1241 {
1242     struct quic_read_again_args *args = arg;
1243
1244     if (!ossl_quic_channel_is_active(args->qc->ch)) {
1245         /* If connection is torn down due to an error while blocking, stop. */
1246         QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1247         return -1;
1248     }
1249
1250     if (!quic_read_actual(args->qc, args->stream,
1251                           args->buf, args->len, args->bytes_read,
1252                           args->peek))
1253         return -1;
1254
1255     if (*args->bytes_read > 0)
1256         /* got at least one byte, the SSL_read op can finish now */
1257         return 1;
1258
1259     return 0; /* did not read anything, keep trying */
1260 }
1261
1262 QUIC_TAKES_LOCK
1263 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1264 {
1265     int ret, res;
1266     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1267     struct quic_read_again_args args;
1268
1269     *bytes_read = 0;
1270
1271     if (!expect_quic_conn(qc))
1272         return 0;
1273
1274     quic_lock(qc);
1275
1276     if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch)) {
1277         ret = QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1278         goto out;
1279     }
1280
1281     /* If we haven't finished the handshake, try to advance it. */
1282     if (quic_do_handshake(qc) < 1) {
1283         ret = 0; /* ossl_quic_do_handshake raised error here */
1284         goto out;
1285     }
1286
1287     if (qc->stream0 == NULL) {
1288         ret = QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1289         goto out;
1290     }
1291
1292     if (!quic_read_actual(qc, qc->stream0, buf, len, bytes_read, peek)) {
1293         ret = 0; /* quic_read_actual raised error here */
1294         goto out;
1295     }
1296
1297     if (*bytes_read > 0) {
1298         /*
1299          * Even though we succeeded, tick the reactor here to ensure we are
1300          * handling other aspects of the QUIC connection.
1301          */
1302         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1303         ret = 1;
1304     } else if (blocking_mode(qc)) {
1305         /*
1306          * We were not able to read anything immediately, so our stream
1307          * buffer is empty. This means we need to block until we get
1308          * at least one byte.
1309          */
1310         args.qc         = qc;
1311         args.stream     = qc->stream0;
1312         args.buf        = buf;
1313         args.len        = len;
1314         args.bytes_read = bytes_read;
1315         args.peek       = peek;
1316
1317         res = block_until_pred(qc, quic_read_again, &args, 0);
1318         if (res == 0) {
1319             ret = QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1320             goto out;
1321         } else if (res < 0) {
1322             ret = 0; /* quic_read_again raised error here */
1323             goto out;
1324         }
1325
1326         ret = 1;
1327     } else {
1328         /* We did not get any bytes and are not in blocking mode. */
1329         ret = QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1330     }
1331
1332 out:
1333     quic_unlock(qc);
1334     return ret;
1335 }
1336
1337 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
1338 {
1339     return quic_read(s, buf, len, bytes_read, 0);
1340 }
1341
1342 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
1343 {
1344     return quic_read(s, buf, len, bytes_read, 1);
1345 }
1346
1347 /*
1348  * SSL_pending
1349  * -----------
1350  */
1351 QUIC_TAKES_LOCK
1352 static size_t ossl_quic_pending_int(const QUIC_CONNECTION *qc)
1353 {
1354     size_t avail = 0;
1355     int fin = 0;
1356
1357     if (!expect_quic_conn(qc))
1358         return 0;
1359
1360     quic_lock((QUIC_CONNECTION *)qc);
1361
1362     if (qc->stream0 == NULL || qc->stream0->rstream == NULL)
1363         /* Cannot raise errors here because we are const, just fail. */
1364         goto out;
1365
1366     if (!ossl_quic_rstream_available(qc->stream0->rstream, &avail, &fin))
1367         avail = 0;
1368
1369 out:
1370     quic_unlock((QUIC_CONNECTION *)qc);
1371     return avail;
1372 }
1373
1374 size_t ossl_quic_pending(const SSL *s)
1375 {
1376     const QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_CONST_SSL(s);
1377
1378     return ossl_quic_pending_int(qc);
1379 }
1380
1381 int ossl_quic_has_pending(const QUIC_CONNECTION *qc)
1382 {
1383     return ossl_quic_pending_int(qc) > 0;
1384 }
1385
1386 /*
1387  * SSL_stream_conclude
1388  * -------------------
1389  */
1390 QUIC_TAKES_LOCK
1391 int ossl_quic_conn_stream_conclude(QUIC_CONNECTION *qc)
1392 {
1393     QUIC_STREAM *qs = qc->stream0;
1394
1395     quic_lock(qc);
1396
1397     if (qs == NULL || qs->sstream == NULL) {
1398         quic_unlock(qc);
1399         return 0;
1400     }
1401
1402     if (!ossl_quic_channel_is_active(qc->ch)
1403         || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
1404         quic_unlock(qc);
1405         return 1;
1406     }
1407
1408     ossl_quic_sstream_fin(qs->sstream);
1409     quic_post_write(qc, 1, 1);
1410     quic_unlock(qc);
1411     return 1;
1412 }
1413
1414 /*
1415  * SSL_inject_net_dgram
1416  * --------------------
1417  */
1418 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
1419                          size_t buf_len,
1420                          const BIO_ADDR *peer,
1421                          const BIO_ADDR *local)
1422 {
1423     QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1424     QUIC_DEMUX *demux;
1425
1426     if (!expect_quic_conn(qc))
1427         return 0;
1428
1429     if (qc->ch == NULL)
1430         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
1431                                            NULL);
1432
1433     demux = ossl_quic_channel_get0_demux(qc->ch);
1434     return ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
1435 }
1436
1437 /*
1438  * QUIC Front-End I/O API: SSL_CTX Management
1439  * ==========================================
1440  */
1441
1442 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1443 {
1444     switch (cmd) {
1445     default:
1446         return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
1447     }
1448 }
1449
1450 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
1451 {
1452     return ssl3_callback_ctrl(s, cmd, fp);
1453 }
1454
1455 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
1456 {
1457     return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
1458 }
1459
1460 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
1461 {
1462     /* We never do renegotiation. */
1463     return 0;
1464 }
1465
1466 /*
1467  * These functions define the TLSv1.2 (and below) ciphers that are supported by
1468  * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
1469  */
1470
1471 int ossl_quic_num_ciphers(void)
1472 {
1473     return 0;
1474 }
1475
1476 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
1477 {
1478     return NULL;
1479 }