QUIC DISPATCH/APL: Implement SSL_get_stream_type
[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_XSO *xso);
21 static int create_channel(QUIC_CONNECTION *qc);
22 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
23 static int qc_try_create_default_xso(QUIC_CONNECTION *qc, int remote_init);
24
25 /*
26  * QUIC Front-End I/O API: Common Utilities
27  * ========================================
28  */
29
30 /*
31  * Block until a predicate is met.
32  *
33  * Precondition: Must have a channel.
34  * Precondition: Must hold channel lock (unchecked).
35  */
36 QUIC_NEEDS_LOCK
37 static int block_until_pred(QUIC_CONNECTION *qc,
38                             int (*pred)(void *arg), void *pred_arg,
39                             uint32_t flags)
40 {
41     QUIC_REACTOR *rtor;
42
43     assert(qc->ch != NULL);
44
45     rtor = ossl_quic_channel_get_reactor(qc->ch);
46     return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
47                                               qc->mutex);
48 }
49
50 /*
51  * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
52  * rather than via ERR.
53  */
54 static int quic_raise_normal_error(QUIC_CONNECTION *qc,
55                                    int err)
56 {
57     qc->last_error = err;
58     return 0;
59 }
60
61 /*
62  * Raise a 'non-normal' error, meaning any error that is not reported via
63  * SSL_get_error() and must be reported via ERR.
64  *
65  * qc should be provided if available. In exceptional circumstances when qc is
66  * not known NULL may be passed. This should generally only happen when an
67  * expect_...() function defined below fails, which generally indicates a
68  * dispatch error or caller error.
69  */
70 static int quic_raise_non_normal_error(QUIC_CONNECTION *qc,
71                                        const char *file,
72                                        int line,
73                                        const char *func,
74                                        int reason,
75                                        const char *fmt,
76                                        ...)
77 {
78     va_list args;
79
80     ERR_new();
81     ERR_set_debug(file, line, func);
82
83     va_start(args, fmt);
84     ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
85     va_end(args);
86
87     if (qc != NULL)
88         qc->last_error = SSL_ERROR_SSL;
89
90     return 0;
91 }
92
93 #define QUIC_RAISE_NORMAL_ERROR(qc, err)                        \
94     quic_raise_normal_error((qc), (err))
95
96 #define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg)            \
97     quic_raise_non_normal_error((qc),                           \
98                                 OPENSSL_FILE, OPENSSL_LINE,     \
99                                 OPENSSL_FUNC,                   \
100                                 (reason),                       \
101                                 (msg))
102
103 /*
104  * QCTX is a utility structure which provides information we commonly wish to
105  * unwrap upon an API call being dispatched to us, namely:
106  *
107  *   - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
108  *     was passed);
109  *   - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
110  *     a QCSO with a default stream was passed);
111  *   - whether a QSSO was passed (xso == NULL must not be used to determine this
112  *     because it may be non-NULL when a QCSO is passed if that QCSO has a
113  *     default stream).
114  */
115 typedef struct qctx_st {
116     QUIC_CONNECTION *qc;
117     QUIC_XSO        *xso;
118     int             is_stream;
119 } QCTX;
120
121 /*
122  * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
123  * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
124  *
125  * After this returns 1, all fields of the passed QCTX are initialised.
126  * Returns 0 on failure. This function is intended to be used to provide API
127  * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
128  */
129 static int expect_quic(const SSL *s, QCTX *ctx)
130 {
131     QUIC_CONNECTION *qc;
132     QUIC_XSO *xso;
133
134     ctx->qc         = NULL;
135     ctx->xso        = NULL;
136     ctx->is_stream  = 0;
137
138     if (s == NULL)
139         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
140
141     switch (s->type) {
142     case SSL_TYPE_QUIC_CONNECTION:
143         qc              = (QUIC_CONNECTION *)s;
144         ctx->qc         = qc;
145         ctx->xso        = qc->default_xso;
146         ctx->is_stream  = 0;
147         return 1;
148
149     case SSL_TYPE_QUIC_XSO:
150         xso             = (QUIC_XSO *)s;
151         ctx->qc         = xso->conn;
152         ctx->xso        = xso;
153         ctx->is_stream  = 1;
154         return 1;
155
156     default:
157         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
158     }
159 }
160
161 /*
162  * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
163  * other words, requires that the passed QSO be a QSSO or a QCSO with a default
164  * stream.
165  *
166  * remote_init determines if we expect the default XSO to be remotely created or
167  * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
168  */
169 static int ossl_unused expect_quic_with_stream(const SSL *s, int remote_init,
170                                                QCTX *ctx)
171 {
172     if (!expect_quic(s, ctx))
173         return 0;
174
175     if (ctx->xso == NULL && remote_init >= 0) {
176         qc_try_create_default_xso(ctx->qc, remote_init);
177         ctx->xso = ctx->qc->default_xso;
178     }
179
180     if (ctx->xso == NULL)
181         return QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, ERR_R_INTERNAL_ERROR, NULL);
182
183     return 1;
184 }
185
186 /*
187  * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
188  * be non-NULL if the QCSO has a default stream.
189  */
190 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
191 {
192     if (!expect_quic(s, ctx))
193         return 0;
194
195     if (ctx->is_stream)
196         return QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, ERR_R_INTERNAL_ERROR, NULL);
197
198     return 1;
199 }
200
201 /*
202  * Ensures that the channel mutex is held for a method which touches channel
203  * state.
204  *
205  * Precondition: Channel mutex is not held (unchecked)
206  */
207 static void quic_lock(QUIC_CONNECTION *qc)
208 {
209     ossl_crypto_mutex_lock(qc->mutex);
210 }
211
212 /* Precondition: Channel mutex is held (unchecked) */
213 QUIC_NEEDS_LOCK
214 static void quic_unlock(QUIC_CONNECTION *qc)
215 {
216     ossl_crypto_mutex_unlock(qc->mutex);
217 }
218
219
220 /*
221  * QUIC Front-End I/O API: Initialization
222  * ======================================
223  *
224  *         SSL_new                  => ossl_quic_new
225  *                                     ossl_quic_init
226  *         SSL_reset                => ossl_quic_reset
227  *         SSL_clear                => ossl_quic_clear
228  *                                     ossl_quic_deinit
229  *         SSL_free                 => ossl_quic_free
230  *
231  */
232
233 /* SSL_new */
234 SSL *ossl_quic_new(SSL_CTX *ctx)
235 {
236     QUIC_CONNECTION *qc = NULL;
237     SSL *ssl_base = NULL;
238     SSL_CONNECTION *sc = NULL;
239
240     qc = OPENSSL_zalloc(sizeof(*qc));
241     if (qc == NULL)
242         goto err;
243
244     /* Initialise the QUIC_CONNECTION's stub header. */
245     ssl_base = &qc->ssl;
246     if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
247         ssl_base = NULL;
248         goto err;
249     }
250
251     qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
252     if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
253          goto err;
254
255     if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
256         goto err;
257
258     qc->is_thread_assisted
259         = (ssl_base->method == OSSL_QUIC_client_thread_method());
260
261     qc->as_server       = 0; /* TODO(QUIC): server support */
262     qc->as_server_state = qc->as_server;
263
264     qc->default_ssl_mode        = qc->ssl.ctx->mode;
265     qc->default_blocking        = 1;
266     qc->last_error              = SSL_ERROR_NONE;
267
268     if (!create_channel(qc))
269         goto err;
270
271     /*
272      * We do not create the default XSO yet. The reason for this is that the
273      * stream ID of the default XSO will depend on whether the stream is client
274      * or server-initiated, which depends on who transmits first. Since we do
275      * not know whether the application will be using a client-transmits-first
276      * or server-transmits-first protocol, we defer default XSO creation until
277      * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
278      * we take that as a cue that the client is expecting a server-initiated
279      * stream, and vice versa if SSL_write() is called first.
280      */
281     return ssl_base;
282
283 err:
284     if (qc != NULL) {
285         ossl_quic_channel_free(qc->ch);
286         SSL_free(qc->tls);
287     }
288     OPENSSL_free(qc);
289     return NULL;
290 }
291
292 /* SSL_free */
293 QUIC_TAKES_LOCK
294 void ossl_quic_free(SSL *s)
295 {
296     QCTX ctx;
297
298     /* We should never be called on anything but a QSO. */
299     if (!expect_quic(s, &ctx))
300         return;
301
302     if (ctx.is_stream) {
303         /*
304          * When a QSSO is freed, the XSO is freed immediately, because the XSO
305          * itself only contains API personality layer data. However the
306          * underlying QUIC_STREAM is not freed immediately but is instead marked
307          * as deleted for later collection.
308          */
309
310         quic_lock(ctx.qc);
311
312         assert(ctx.qc->num_xso > 0);
313         --ctx.qc->num_xso;
314
315         ctx.xso->stream->deleted = 1;
316
317         /* Auto-conclude stream. */
318         /* TODO(QUIC): Do RESET_STREAM here instead of auto-conclude */
319         if (ctx.xso->stream->sstream != NULL)
320             ossl_quic_sstream_fin(ctx.xso->stream->sstream);
321
322         /* Update stream state. */
323         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.xso->conn->ch),
324                                           ctx.xso->stream);
325
326         quic_unlock(ctx.qc);
327
328         /* Note: SSL_free calls OPENSSL_free(xso) for us */
329         return;
330     }
331
332     quic_lock(ctx.qc);
333
334     /*
335      * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
336      * stage, but is freed during the channel free when the whole QSM is freed.
337      */
338     if (ctx.qc->default_xso != NULL) {
339         QUIC_XSO *xso = ctx.qc->default_xso;
340
341         quic_unlock(ctx.qc);
342         SSL_free(&xso->ssl);
343         quic_lock(ctx.qc);
344     }
345
346     /* Ensure we have no remaining XSOs. */
347     assert(ctx.qc->num_xso == 0);
348
349     if (ctx.qc->is_thread_assisted && ctx.qc->started) {
350         ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
351         ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
352     }
353
354     ossl_quic_channel_free(ctx.qc->ch);
355
356     BIO_free(ctx.qc->net_rbio);
357     BIO_free(ctx.qc->net_wbio);
358
359     /* Note: SSL_free calls OPENSSL_free(qc) for us */
360
361     SSL_free(ctx.qc->tls);
362     ossl_crypto_mutex_free(&ctx.qc->mutex); /* freed while still locked */
363 }
364
365 /* SSL method init */
366 int ossl_quic_init(SSL *s)
367 {
368     /* Same op as SSL_clear, forward the call. */
369     return ossl_quic_clear(s);
370 }
371
372 /* SSL method deinit */
373 void ossl_quic_deinit(SSL *s)
374 {
375     /* No-op. */
376 }
377
378 /* SSL_reset */
379 int ossl_quic_reset(SSL *s)
380 {
381     QCTX ctx;
382
383     if (!expect_quic(s, &ctx))
384         return 0;
385
386     /* TODO(QUIC); Currently a no-op. */
387     return 1;
388 }
389
390 /* SSL_clear */
391 int ossl_quic_clear(SSL *s)
392 {
393     QCTX ctx;
394
395     if (!expect_quic(s, &ctx))
396         return 0;
397
398     /* TODO(QUIC): Currently a no-op. */
399     return 1;
400 }
401
402 void ossl_quic_conn_set_override_now_cb(SSL *s,
403                                         OSSL_TIME (*now_cb)(void *arg),
404                                         void *now_cb_arg)
405 {
406     QCTX ctx;
407
408     if (!expect_quic(s, &ctx))
409         return;
410
411     ctx.qc->override_now_cb     = now_cb;
412     ctx.qc->override_now_cb_arg = now_cb_arg;
413 }
414
415 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
416 {
417     QCTX ctx;
418
419     if (!expect_quic(s, &ctx))
420         return;
421
422     if (ctx.qc->is_thread_assisted && ctx.qc->started)
423         ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
424 }
425
426 /*
427  * QUIC Front-End I/O API: Network BIO Configuration
428  * =================================================
429  *
430  * Handling the different BIOs is difficult:
431  *
432  *   - It is more or less a requirement that we use non-blocking network I/O;
433  *     we need to be able to have timeouts on recv() calls, and make best effort
434  *     (non blocking) send() and recv() calls.
435  *
436  *     The only sensible way to do this is to configure the socket into
437  *     non-blocking mode. We could try to do select() before calling send() or
438  *     recv() to get a guarantee that the call will not block, but this will
439  *     probably run into issues with buggy OSes which generate spurious socket
440  *     readiness events. In any case, relying on this to work reliably does not
441  *     seem sane.
442  *
443  *     Timeouts could be handled via setsockopt() socket timeout options, but
444  *     this depends on OS support and adds another syscall to every network I/O
445  *     operation. It also has obvious thread safety concerns if we want to move
446  *     to concurrent use of a single socket at some later date.
447  *
448  *     Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
449  *     be made non-blocking. However some OSes (e.g. Windows) do not support
450  *     this, so we cannot rely on this.
451  *
452  *     As such, we need to configure any FD in non-blocking mode. This may
453  *     confound users who pass a blocking socket to libssl. However, in practice
454  *     it would be extremely strange for a user of QUIC to pass an FD to us,
455  *     then also try and send receive traffic on the same socket(!). Thus the
456  *     impact of this should be limited, and can be documented.
457  *
458  *   - We support both blocking and non-blocking operation in terms of the API
459  *     presented to the user. One prospect is to set the blocking mode based on
460  *     whether the socket passed to us was already in blocking mode. However,
461  *     Windows has no API for determining if a socket is in blocking mode (!),
462  *     therefore this cannot be done portably. Currently therefore we expose an
463  *     explicit API call to set this, and default to blocking mode.
464  *
465  *   - We need to determine our initial destination UDP address. The "natural"
466  *     way for a user to do this is to set the peer variable on a BIO_dgram.
467  *     However, this has problems because BIO_dgram's peer variable is used for
468  *     both transmission and reception. This means it can be constantly being
469  *     changed to a malicious value (e.g. if some random unrelated entity on the
470  *     network starts sending traffic to us) on every read call. This is not a
471  *     direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
472  *     calls only, which do not use this variable. However, we do need to let
473  *     the user specify the peer in a 'normal' manner. The compromise here is
474  *     that we grab the current peer value set at the time the write BIO is set
475  *     and do not read the value again.
476  *
477  *   - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
478  *     Currently we do this by only supporting non-blocking mode.
479  *
480  */
481
482 /*
483  * Determines what initial destination UDP address we should use, if possible.
484  * If this fails the client must set the destination address manually, or use a
485  * BIO which does not need a destination address.
486  */
487 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
488 {
489     if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
490         return 0;
491
492     return 1;
493 }
494
495 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
496 {
497     QCTX ctx;
498
499     if (!expect_quic(s, &ctx))
500         return;
501
502     if (ctx.qc->net_rbio == net_rbio)
503         return;
504
505     if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
506         return;
507
508     BIO_free(ctx.qc->net_rbio);
509     ctx.qc->net_rbio = net_rbio;
510
511     /*
512      * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
513      * mode as we do not support it for non-pollable BIOs.
514      */
515     if (net_rbio != NULL) {
516         BIO_POLL_DESCRIPTOR d = {0};
517
518         if (!BIO_get_rpoll_descriptor(net_rbio, &d)
519             || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
520             ctx.qc->blocking          = 0;
521             ctx.qc->default_blocking  = 0;
522             ctx.qc->can_poll_net_rbio = 0;
523         } else {
524             ctx.qc->can_poll_net_rbio = 1;
525         }
526     }
527 }
528
529 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
530 {
531     QCTX ctx;
532
533     if (!expect_quic(s, &ctx))
534         return;
535
536     if (ctx.qc->net_wbio == net_wbio)
537         return;
538
539     if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
540         return;
541
542     BIO_free(ctx.qc->net_wbio);
543     ctx.qc->net_wbio = net_wbio;
544
545     if (net_wbio != NULL) {
546         BIO_POLL_DESCRIPTOR d = {0};
547
548         if (!BIO_get_wpoll_descriptor(net_wbio, &d)
549             || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
550             ctx.qc->blocking          = 0;
551             ctx.qc->default_blocking  = 0;
552             ctx.qc->can_poll_net_wbio = 0;
553         } else {
554             ctx.qc->can_poll_net_wbio = 1;
555         }
556
557         /*
558          * If we do not have a peer address yet, and we have not started trying
559          * to connect yet, try to autodetect one.
560          */
561         if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
562             && !ctx.qc->started) {
563             if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
564                 /* best effort */
565                 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
566
567             ossl_quic_channel_set_peer_addr(ctx.qc->ch,
568                                             &ctx.qc->init_peer_addr);
569         }
570     }
571 }
572
573 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
574 {
575     QCTX ctx;
576
577     if (!expect_quic(s, &ctx))
578         return NULL;
579
580     return ctx.qc->net_rbio;
581 }
582
583 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
584 {
585     QCTX ctx;
586
587     if (!expect_quic(s, &ctx))
588         return NULL;
589
590     return ctx.qc->net_wbio;
591 }
592
593 int ossl_quic_conn_get_blocking_mode(const SSL *s)
594 {
595     QCTX ctx;
596
597     if (!expect_quic(s, &ctx))
598         return 0;
599
600     if (ctx.is_stream)
601         return ctx.xso->blocking;
602
603     return ctx.qc->blocking;
604 }
605
606 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
607 {
608     QCTX ctx;
609
610     if (!expect_quic(s, &ctx))
611         return 0;
612
613     /* Cannot enable blocking mode if we do not have pollable FDs. */
614     if (blocking != 0 &&
615         (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
616         return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_UNSUPPORTED, NULL);
617
618     if (!ctx.is_stream) {
619         /*
620          * If called on a QCSO, update default and connection-level blocking
621          * modes.
622          */
623         ctx.qc->blocking         = (blocking != 0);
624         ctx.qc->default_blocking = ctx.qc->blocking;
625     }
626
627     if (ctx.xso != NULL)
628         /*
629          * If called on  a QSSO or QCSO with a default XSO, update blocking
630          * mode.
631          */
632         ctx.xso->blocking = (blocking != 0);
633
634     return 1;
635 }
636
637 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
638                                          const BIO_ADDR *peer_addr)
639 {
640     QCTX ctx;
641
642     if (!expect_quic(s, &ctx))
643         return 0;
644
645     if (ctx.qc->started)
646         return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
647                                            NULL);
648
649     if (peer_addr == NULL) {
650         BIO_ADDR_clear(&ctx.qc->init_peer_addr);
651         return 1;
652     }
653
654     ctx.qc->init_peer_addr = *peer_addr;
655     return 1;
656 }
657
658 /*
659  * QUIC Front-End I/O API: Asynchronous I/O Management
660  * ===================================================
661  *
662  *   (BIO/)SSL_tick                 => ossl_quic_tick
663  *   (BIO/)SSL_get_tick_timeout     => ossl_quic_get_tick_timeout
664  *   (BIO/)SSL_get_poll_fd          => ossl_quic_get_poll_fd
665  *
666  */
667
668 /* Returns 1 if the connection is being used in blocking mode. */
669 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
670 {
671     return qc->blocking;
672 }
673
674 static int xso_blocking_mode(const QUIC_XSO *xso)
675 {
676     return xso->blocking
677         && xso->conn->can_poll_net_rbio
678         && xso->conn->can_poll_net_wbio;
679 }
680
681 /* SSL_tick; ticks the reactor. */
682 QUIC_TAKES_LOCK
683 int ossl_quic_tick(SSL *s)
684 {
685     QCTX ctx;
686
687     if (!expect_quic(s, &ctx))
688         return 0;
689
690     quic_lock(ctx.qc);
691     ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
692     quic_unlock(ctx.qc);
693     return 1;
694 }
695
696 /*
697  * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object
698  * should be ticked by the application by calling SSL_tick(). tv is set to 0 if
699  * the object should be ticked immediately and tv->tv_sec is set to -1 if no
700  * timeout is currently active.
701  */
702 QUIC_TAKES_LOCK
703 int ossl_quic_get_tick_timeout(SSL *s, struct timeval *tv)
704 {
705     QCTX ctx;
706     OSSL_TIME deadline = ossl_time_infinite();
707
708     if (!expect_quic(s, &ctx))
709         return 0;
710
711     quic_lock(ctx.qc);
712
713     deadline
714         = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
715
716     if (ossl_time_is_infinite(deadline)) {
717         tv->tv_sec  = -1;
718         tv->tv_usec = 0;
719         quic_unlock(ctx.qc);
720         return 1;
721     }
722
723     *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now()));
724     quic_unlock(ctx.qc);
725     return 1;
726 }
727
728 /* SSL_get_rpoll_descriptor */
729 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
730 {
731     QCTX ctx;
732
733     if (!expect_quic(s, &ctx))
734         return 0;
735
736     if (desc == NULL || ctx.qc->net_rbio == NULL)
737         return 0;
738
739     return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
740 }
741
742 /* SSL_get_wpoll_descriptor */
743 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
744 {
745     QCTX ctx;
746
747     if (!expect_quic(s, &ctx))
748         return 0;
749
750     if (desc == NULL || ctx.qc->net_wbio == NULL)
751         return 0;
752
753     return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
754 }
755
756 /* SSL_net_read_desired */
757 QUIC_TAKES_LOCK
758 int ossl_quic_get_net_read_desired(SSL *s)
759 {
760     QCTX ctx;
761     int ret;
762
763     if (!expect_quic(s, &ctx))
764         return 0;
765
766     quic_lock(ctx.qc);
767     ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
768     quic_unlock(ctx.qc);
769     return ret;
770 }
771
772 /* SSL_net_write_desired */
773 QUIC_TAKES_LOCK
774 int ossl_quic_get_net_write_desired(SSL *s)
775 {
776     int ret;
777     QCTX ctx;
778
779     if (!expect_quic(s, &ctx))
780         return 0;
781
782     quic_lock(ctx.qc);
783     ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
784     quic_unlock(ctx.qc);
785     return ret;
786 }
787
788 /*
789  * QUIC Front-End I/O API: Connection Lifecycle Operations
790  * =======================================================
791  *
792  *         SSL_do_handshake         => ossl_quic_do_handshake
793  *         SSL_set_connect_state    => ossl_quic_set_connect_state
794  *         SSL_set_accept_state     => ossl_quic_set_accept_state
795  *         SSL_shutdown             => ossl_quic_shutdown
796  *         SSL_ctrl                 => ossl_quic_ctrl
797  *   (BIO/)SSL_connect              => ossl_quic_connect
798  *   (BIO/)SSL_accept               => ossl_quic_accept
799  *
800  */
801
802 /* SSL_shutdown */
803 static int quic_shutdown_wait(void *arg)
804 {
805     QUIC_CONNECTION *qc = arg;
806
807     return ossl_quic_channel_is_terminated(qc->ch);
808 }
809
810 QUIC_TAKES_LOCK
811 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
812                             const SSL_SHUTDOWN_EX_ARGS *args,
813                             size_t args_len)
814 {
815     int ret;
816     QCTX ctx;
817
818     if (!expect_quic(s, &ctx))
819         return 0;
820
821     if (ctx.is_stream)
822         /* TODO(QUIC): Semantics currently undefined for QSSOs */
823         return -1;
824
825     quic_lock(ctx.qc);
826
827     ossl_quic_channel_local_close(ctx.qc->ch,
828                                   args != NULL ? args->quic_error_code : 0);
829
830     /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
831
832     if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
833         quic_unlock(ctx.qc);
834         return 1;
835     }
836
837     if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
838         block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
839     else
840         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
841
842     ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
843     quic_unlock(ctx.qc);
844     return ret;
845 }
846
847 /* SSL_ctrl */
848 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
849 {
850     QCTX ctx;
851
852     if (!expect_quic(s, &ctx))
853         return 0;
854
855     switch (cmd) {
856     case SSL_CTRL_MODE:
857         /* If called on a QCSO, update the default mode. */
858         if (!ctx.is_stream)
859             ctx.qc->default_ssl_mode |= (uint32_t)larg;
860
861         /*
862          * If we were called on a QSSO or have a default stream, we also update
863          * that.
864          */
865         if (ctx.xso != NULL) {
866             /* Cannot enable EPW while AON write in progress. */
867             if (ctx.xso->aon_write_in_progress)
868                 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
869
870             ctx.xso->ssl_mode |= (uint32_t)larg;
871             return ctx.xso->ssl_mode;
872         }
873
874         return ctx.qc->default_ssl_mode;
875     case SSL_CTRL_CLEAR_MODE:
876         if (!ctx.is_stream)
877             ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
878
879         if (ctx.xso != NULL) {
880             ctx.xso->ssl_mode &= ~(uint32_t)larg;
881             return ctx.xso->ssl_mode;
882         }
883
884         return ctx.qc->default_ssl_mode;
885     default:
886         /* Probably a TLS related ctrl. Defer to our internal SSL object */
887         return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
888     }
889 }
890
891 /* SSL_set_connect_state */
892 void ossl_quic_set_connect_state(SSL *s)
893 {
894     QCTX ctx;
895
896     if (!expect_quic(s, &ctx))
897         return;
898
899     /* Cannot be changed after handshake started */
900     if (ctx.qc->started || ctx.is_stream)
901         return;
902
903     ctx.qc->as_server_state = 0;
904 }
905
906 /* SSL_set_accept_state */
907 void ossl_quic_set_accept_state(SSL *s)
908 {
909     QCTX ctx;
910
911     if (!expect_quic(s, &ctx))
912         return;
913
914     /* Cannot be changed after handshake started */
915     if (ctx.qc->started || ctx.is_stream)
916         return;
917
918     ctx.qc->as_server_state = 1;
919 }
920
921 /* SSL_do_handshake */
922 struct quic_handshake_wait_args {
923     QUIC_CONNECTION     *qc;
924 };
925
926 static int quic_handshake_wait(void *arg)
927 {
928     struct quic_handshake_wait_args *args = arg;
929
930     if (!ossl_quic_channel_is_active(args->qc->ch))
931         return -1;
932
933     if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
934         return 1;
935
936     return 0;
937 }
938
939 static int configure_channel(QUIC_CONNECTION *qc)
940 {
941     assert(qc->ch != NULL);
942
943     if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
944         || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
945         || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
946         return 0;
947
948     return 1;
949 }
950
951 QUIC_NEEDS_LOCK
952 static int create_channel(QUIC_CONNECTION *qc)
953 {
954     QUIC_CHANNEL_ARGS args = {0};
955
956     args.libctx     = qc->ssl.ctx->libctx;
957     args.propq      = qc->ssl.ctx->propq;
958     args.is_server  = qc->as_server;
959     args.tls        = qc->tls;
960     args.mutex      = qc->mutex;
961     args.now_cb     = qc->override_now_cb;
962     args.now_cb_arg = qc->override_now_cb_arg;
963
964     qc->ch = ossl_quic_channel_new(&args);
965     if (qc->ch == NULL)
966         return 0;
967
968     return 1;
969 }
970
971 /*
972  * Creates a channel and configures it with the information we have accumulated
973  * via calls made to us from the application prior to starting a handshake
974  * attempt.
975  */
976 QUIC_NEEDS_LOCK
977 static int ensure_channel_started(QUIC_CONNECTION *qc)
978 {
979     if (!qc->started) {
980         if (!configure_channel(qc)
981             || !ossl_quic_channel_start(qc->ch))
982             goto err;
983
984         if (qc->is_thread_assisted)
985             if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
986                 goto err;
987     }
988
989     qc->started = 1;
990     return 1;
991
992 err:
993     ossl_quic_channel_free(qc->ch);
994     qc->ch = NULL;
995     return 0;
996 }
997
998 QUIC_NEEDS_LOCK
999 static int quic_do_handshake(QUIC_CONNECTION *qc)
1000 {
1001     int ret;
1002
1003     if (ossl_quic_channel_is_handshake_complete(qc->ch))
1004         /* Handshake already completed. */
1005         return 1;
1006
1007     if (ossl_quic_channel_is_term_any(qc->ch))
1008         return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1009
1010     if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1011         /* Peer address must have been set. */
1012         QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1013         return -1; /* Non-protocol error */
1014     }
1015
1016     if (qc->as_server != qc->as_server_state) {
1017         /* TODO(QUIC): Must match the method used to create the QCSO */
1018         QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1019         return -1; /* Non-protocol error */
1020     }
1021
1022     if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1023         /* Need read and write BIOs. */
1024         QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BIO_NOT_SET, NULL);
1025         return -1; /* Non-protocol error */
1026     }
1027
1028     /*
1029      * Start connection process. Note we may come here multiple times in
1030      * non-blocking mode, which is fine.
1031      */
1032     if (!ensure_channel_started(qc)) {
1033         QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1034         return -1; /* Non-protocol error */
1035     }
1036
1037     if (ossl_quic_channel_is_handshake_complete(qc->ch))
1038         /* The handshake is now done. */
1039         return 1;
1040
1041     if (qc_blocking_mode(qc)) {
1042         /* In blocking mode, wait for the handshake to complete. */
1043         struct quic_handshake_wait_args args;
1044
1045         args.qc     = qc;
1046
1047         ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1048         if (!ossl_quic_channel_is_active(qc->ch)) {
1049             QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1050             return 0; /* Shutdown before completion */
1051         } else if (ret <= 0) {
1052             QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1053             return -1; /* Non-protocol error */
1054         }
1055
1056         assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1057         return 1;
1058     } else {
1059         /* Try to advance the reactor. */
1060         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1061
1062         if (ossl_quic_channel_is_handshake_complete(qc->ch))
1063             /* The handshake is now done. */
1064             return 1;
1065
1066         /* Otherwise, indicate that the handshake isn't done yet. */
1067         QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1068         return -1; /* Non-protocol error */
1069     }
1070 }
1071
1072 QUIC_TAKES_LOCK
1073 int ossl_quic_do_handshake(SSL *s)
1074 {
1075     int ret;
1076     QCTX ctx;
1077
1078     if (!expect_quic(s, &ctx))
1079         return 0;
1080
1081     quic_lock(ctx.qc);
1082
1083     ret = quic_do_handshake(ctx.qc);
1084     quic_unlock(ctx.qc);
1085     return ret;
1086 }
1087
1088 /* SSL_connect */
1089 int ossl_quic_connect(SSL *s)
1090 {
1091     /* Ensure we are in connect state (no-op if non-idle). */
1092     ossl_quic_set_connect_state(s);
1093
1094     /* Begin or continue the handshake */
1095     return ossl_quic_do_handshake(s);
1096 }
1097
1098 /* SSL_accept */
1099 int ossl_quic_accept(SSL *s)
1100 {
1101     /* Ensure we are in accept state (no-op if non-idle). */
1102     ossl_quic_set_accept_state(s);
1103
1104     /* Begin or continue the handshake */
1105     return ossl_quic_do_handshake(s);
1106 }
1107
1108 /*
1109  * QUIC Front-End I/O API: Stream Lifecycle Operations
1110  * ===================================================
1111  *
1112  *         SSL_stream_new       => ossl_quic_conn_stream_new
1113  *
1114  */
1115
1116 /*
1117  * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1118  * default XSO was created. Returns 0 if it was not (e.g. because it already
1119  * exists). Note that this is NOT an error condition.
1120  */
1121 QUIC_NEEDS_LOCK
1122 static int qc_try_create_default_xso(QUIC_CONNECTION *qc, int remote_init)
1123 {
1124     QUIC_STREAM *qs;
1125
1126     if (qc->default_xso != NULL)
1127         qc->default_xso_created = 1;
1128
1129     if (qc->default_xso_created)
1130         /*
1131          * We only do this once. If the user detaches a previously created
1132          * default XSO we don't auto-create another one.
1133          */
1134         return 0;
1135
1136     if (!remote_init) {
1137         /*
1138          * We are writing to the stream first, so this is a locally-initiated
1139          * stream.
1140          */
1141         qc->default_xso = (QUIC_XSO *)ossl_quic_conn_stream_new(&qc->ssl, 0);
1142         if (qc->default_xso == NULL)
1143             return 0;
1144     } else {
1145         /*
1146          * Client is reading first. This means it is expecting to get data on a
1147          * stream the peer writes to first, meaning this is a remotely-initiated
1148          * stream. Ordinarily, we wait for the RXDP to handle a STREAM frame to
1149          * create such a stream. But in this case, special case it and create
1150          * the bookkeeping structures for the first peer-initiated bidirectional
1151          * stream so the client can start to wait on it.
1152          */
1153        qs = ossl_quic_channel_new_stream_remote(qc->ch,
1154                                                 QUIC_STREAM_INITIATOR_SERVER
1155                                                 | QUIC_STREAM_DIR_BIDI);
1156        if (qs == NULL)
1157            return 0;
1158
1159         qc->default_xso = create_xso_from_stream(qc, qs);
1160         if (qc->default_xso == NULL) {
1161             ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1162             return 0;
1163         }
1164     }
1165
1166     qc->default_xso_created = 1;
1167     return 1;
1168 }
1169
1170 QUIC_NEEDS_LOCK
1171 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1172 {
1173     QUIC_XSO *xso = NULL;
1174
1175     if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1176         goto err;
1177
1178     if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1179         goto err;
1180
1181     xso->conn       = qc;
1182     xso->blocking   = qc->default_blocking;
1183     xso->ssl_mode   = qc->default_ssl_mode;
1184
1185     xso->stream     = qs;
1186
1187     ++qc->num_xso;
1188     return xso;
1189
1190 err:
1191     OPENSSL_free(xso);
1192     return NULL;
1193 }
1194
1195 QUIC_TAKES_LOCK
1196 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1197 {
1198     QCTX ctx;
1199     QUIC_XSO *xso = NULL;
1200     QUIC_STREAM *qs = NULL;
1201     int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1202
1203     if (!expect_quic_conn_only(s, &ctx))
1204         return NULL;
1205
1206     quic_lock(ctx.qc);
1207
1208     if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1209         QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1210         goto err;
1211     }
1212
1213     qs = ossl_quic_channel_new_stream_local(ctx.qc->ch, is_uni);
1214     if (qs == NULL)
1215         goto err;
1216
1217     xso = create_xso_from_stream(ctx.qc, qs);
1218     if (xso == NULL)
1219         goto err;
1220
1221     quic_unlock(ctx.qc);
1222     return &xso->ssl;
1223
1224 err:
1225     OPENSSL_free(xso);
1226     ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(ctx.qc->ch), qs);
1227     quic_unlock(ctx.qc);
1228     return NULL;
1229 }
1230
1231 /*
1232  * QUIC Front-End I/O API: Steady-State Operations
1233  * ===============================================
1234  *
1235  * Here we dispatch calls to the steady-state front-end I/O API functions; that
1236  * is, the functions used during the established phase of a QUIC connection
1237  * (e.g. SSL_read, SSL_write).
1238  *
1239  * Each function must handle both blocking and non-blocking modes. As discussed
1240  * above, all QUIC I/O is implemented using non-blocking mode internally.
1241  *
1242  *         SSL_get_error        => partially implemented by ossl_quic_get_error
1243  *   (BIO/)SSL_read             => ossl_quic_read
1244  *   (BIO/)SSL_write            => ossl_quic_write
1245  *         SSL_pending          => ossl_quic_pending
1246  *         SSL_stream_conclude  => ossl_quic_conn_stream_conclude
1247  */
1248
1249 /* SSL_get_error */
1250 int ossl_quic_get_error(const SSL *s, int i)
1251 {
1252     QCTX ctx;
1253
1254     if (!expect_quic(s, &ctx))
1255         return 0;
1256
1257     return ctx.qc->last_error;
1258 }
1259
1260 /*
1261  * SSL_write
1262  * ---------
1263  *
1264  * The set of functions below provide the implementation of the public SSL_write
1265  * function. We must handle:
1266  *
1267  *   - both blocking and non-blocking operation at the application level,
1268  *     depending on how we are configured;
1269  *
1270  *   - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1271  *
1272  *   - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1273  *
1274  */
1275 QUIC_NEEDS_LOCK
1276 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1277 {
1278     /*
1279      * We have appended at least one byte to the stream.
1280      * Potentially mark stream as active, depending on FC.
1281      */
1282     if (did_append)
1283         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1284                                           xso->stream);
1285
1286     /*
1287      * Try and send.
1288      *
1289      * TODO(QUIC): It is probably inefficient to try and do this immediately,
1290      * plus we should eventually consider Nagle's algorithm.
1291      */
1292     if (do_tick)
1293         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1294 }
1295
1296 struct quic_write_again_args {
1297     QUIC_XSO            *xso;
1298     const unsigned char *buf;
1299     size_t              len;
1300     size_t              total_written;
1301 };
1302
1303 QUIC_NEEDS_LOCK
1304 static int quic_write_again(void *arg)
1305 {
1306     struct quic_write_again_args *args = arg;
1307     size_t actual_written = 0;
1308
1309     if (!ossl_quic_channel_is_active(args->xso->conn->ch))
1310         /* If connection is torn down due to an error while blocking, stop. */
1311         return -2;
1312
1313     if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1314                                   args->buf, args->len, &actual_written))
1315         return -2;
1316
1317     quic_post_write(args->xso, actual_written > 0, 0);
1318
1319     args->buf           += actual_written;
1320     args->len           -= actual_written;
1321     args->total_written += actual_written;
1322
1323     if (args->len == 0)
1324         /* Written everything, done. */
1325         return 1;
1326
1327     /* Not written everything yet, keep trying. */
1328     return 0;
1329 }
1330
1331 QUIC_NEEDS_LOCK
1332 static int quic_write_blocking(QUIC_XSO *xso, const void *buf, size_t len,
1333                                size_t *written)
1334 {
1335     int res;
1336     struct quic_write_again_args args;
1337     size_t actual_written = 0;
1338
1339     /* First make a best effort to append as much of the data as possible. */
1340     if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1341                                   &actual_written)) {
1342         /* Stream already finished or allocation error. */
1343         *written = 0;
1344         return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1345     }
1346
1347     quic_post_write(xso, actual_written > 0, 1);
1348
1349     if (actual_written == len) {
1350         /* Managed to append everything on the first try. */
1351         *written = actual_written;
1352         return 1;
1353     }
1354
1355     /*
1356      * We did not manage to append all of the data immediately, so the stream
1357      * buffer has probably filled up. This means we need to block until some of
1358      * it is freed up.
1359      */
1360     args.xso            = xso;
1361     args.buf            = (const unsigned char *)buf + actual_written;
1362     args.len            = len - actual_written;
1363     args.total_written  = 0;
1364
1365     res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1366     if (res <= 0) {
1367         if (!ossl_quic_channel_is_active(xso->conn->ch))
1368             return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1369         else
1370             return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1371     }
1372
1373     *written = args.total_written;
1374     return 1;
1375 }
1376
1377 /*
1378  * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1379  * write semantics.
1380  */
1381 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1382                             size_t buf_len, size_t already_sent)
1383 {
1384     assert(!xso->aon_write_in_progress);
1385
1386     xso->aon_write_in_progress = 1;
1387     xso->aon_buf_base          = buf;
1388     xso->aon_buf_pos           = already_sent;
1389     xso->aon_buf_len           = buf_len;
1390 }
1391
1392 static void aon_write_finish(QUIC_XSO *xso)
1393 {
1394     xso->aon_write_in_progress   = 0;
1395     xso->aon_buf_base            = NULL;
1396     xso->aon_buf_pos             = 0;
1397     xso->aon_buf_len             = 0;
1398 }
1399
1400 QUIC_NEEDS_LOCK
1401 static int quic_write_nonblocking_aon(QUIC_XSO *xso, const void *buf,
1402                                       size_t len, size_t *written)
1403 {
1404     const void *actual_buf;
1405     size_t actual_len, actual_written = 0;
1406     int accept_moving_buffer
1407         = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1408
1409     if (xso->aon_write_in_progress) {
1410         /*
1411          * We are in the middle of an AON write (i.e., a previous write did not
1412          * manage to append all data to the SSTREAM and we have Enable Partial
1413          * Write (EPW) mode disabled.)
1414          */
1415         if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1416             || len != xso->aon_buf_len)
1417             /*
1418              * Pointer must not have changed if we are not in accept moving
1419              * buffer mode. Length must never change.
1420              */
1421             return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_BAD_WRITE_RETRY, NULL);
1422
1423         actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1424         actual_len = len - xso->aon_buf_pos;
1425         assert(actual_len > 0);
1426     } else {
1427         actual_buf = buf;
1428         actual_len = len;
1429     }
1430
1431     /* First make a best effort to append as much of the data as possible. */
1432     if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1433                                   &actual_written)) {
1434         /* Stream already finished or allocation error. */
1435         *written = 0;
1436         return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1437     }
1438
1439     quic_post_write(xso, actual_written > 0, 1);
1440
1441     if (actual_written == actual_len) {
1442         /* We have sent everything. */
1443         if (xso->aon_write_in_progress) {
1444             /*
1445              * We have sent everything, and we were in the middle of an AON
1446              * write. The output write length is the total length of the AON
1447              * buffer, not however many bytes we managed to write to the stream
1448              * in this call.
1449              */
1450             *written = xso->aon_buf_len;
1451             aon_write_finish(xso);
1452         } else {
1453             *written = actual_written;
1454         }
1455
1456         return 1;
1457     }
1458
1459     if (xso->aon_write_in_progress) {
1460         /*
1461          * AON write is in progress but we have not written everything yet. We
1462          * may have managed to send zero bytes, or some number of bytes less
1463          * than the total remaining which need to be appended during this
1464          * AON operation.
1465          */
1466         xso->aon_buf_pos += actual_written;
1467         assert(xso->aon_buf_pos < xso->aon_buf_len);
1468         return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE);
1469     }
1470
1471     /*
1472      * Not in an existing AON operation but partial write is not enabled, so we
1473      * need to begin a new AON operation. However we needn't bother if we didn't
1474      * actually append anything.
1475      */
1476     if (actual_written > 0)
1477         aon_write_begin(xso, buf, len, actual_written);
1478
1479     /*
1480      * AON - We do not publicly admit to having appended anything until AON
1481      * completes.
1482      */
1483     *written = 0;
1484     return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE);
1485 }
1486
1487 QUIC_NEEDS_LOCK
1488 static int quic_write_nonblocking_epw(QUIC_XSO *xso, const void *buf, size_t len,
1489                                       size_t *written)
1490 {
1491     /* Simple best effort operation. */
1492     if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
1493         /* Stream already finished or allocation error. */
1494         *written = 0;
1495         return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1496     }
1497
1498     quic_post_write(xso, *written > 0, 1);
1499     return 1;
1500 }
1501
1502 QUIC_TAKES_LOCK
1503 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1504 {
1505     int ret;
1506     QCTX ctx;
1507     int partial_write;
1508
1509     *written = 0;
1510
1511     if (!expect_quic_with_stream(s, /*remote_init=*/0, &ctx)) {
1512         return 0;
1513     }
1514
1515     quic_lock(ctx.qc);
1516
1517     partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1518
1519     if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1520         ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1521         goto out;
1522     }
1523
1524     /*
1525      * If we haven't finished the handshake, try to advance it.
1526      * We don't accept writes until the handshake is completed.
1527      */
1528     if (quic_do_handshake(ctx.qc) < 1) {
1529         ret = 0;
1530         goto out;
1531     }
1532
1533     if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
1534         ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1535         goto out;
1536     }
1537
1538     if (xso_blocking_mode(ctx.xso))
1539         ret = quic_write_blocking(ctx.xso, buf, len, written);
1540     else if (partial_write)
1541         ret = quic_write_nonblocking_epw(ctx.xso, buf, len, written);
1542     else
1543         ret = quic_write_nonblocking_aon(ctx.xso, buf, len, written);
1544
1545 out:
1546     quic_unlock(ctx.qc);
1547     return ret;
1548 }
1549
1550 /*
1551  * SSL_read
1552  * --------
1553  */
1554 struct quic_read_again_args {
1555     QUIC_CONNECTION *qc;
1556     QUIC_STREAM     *stream;
1557     void            *buf;
1558     size_t          len;
1559     size_t          *bytes_read;
1560     int             peek;
1561 };
1562
1563 QUIC_NEEDS_LOCK
1564 static int quic_read_actual(QUIC_CONNECTION *qc,
1565                             QUIC_STREAM *stream,
1566                             void *buf, size_t buf_len,
1567                             size_t *bytes_read,
1568                             int peek)
1569 {
1570     int is_fin = 0;
1571
1572     /* If the receive part of the stream is over, issue EOF. */
1573     if (stream->recv_fin_retired)
1574         return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_ZERO_RETURN);
1575
1576     if (stream->rstream == NULL)
1577         return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1578
1579     if (peek) {
1580         if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1581                                     bytes_read, &is_fin))
1582             return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1583
1584     } else {
1585         if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1586                                     bytes_read, &is_fin))
1587             return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1588     }
1589
1590     if (!peek) {
1591         if (*bytes_read > 0) {
1592             /*
1593              * We have read at least one byte from the stream. Inform stream-level
1594              * RXFC of the retirement of controlled bytes. Update the active stream
1595              * status (the RXFC may now want to emit a frame granting more credit to
1596              * the peer).
1597              */
1598             OSSL_RTT_INFO rtt_info;
1599
1600             ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1601
1602             if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
1603                                           rtt_info.smoothed_rtt))
1604                 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1605         }
1606
1607         if (is_fin)
1608             stream->recv_fin_retired = 1;
1609
1610         if (*bytes_read > 0)
1611             ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1612                                               stream);
1613     }
1614
1615     return 1;
1616 }
1617
1618 QUIC_NEEDS_LOCK
1619 static int quic_read_again(void *arg)
1620 {
1621     struct quic_read_again_args *args = arg;
1622
1623     if (!ossl_quic_channel_is_active(args->qc->ch)) {
1624         /* If connection is torn down due to an error while blocking, stop. */
1625         QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1626         return -1;
1627     }
1628
1629     if (!quic_read_actual(args->qc, args->stream,
1630                           args->buf, args->len, args->bytes_read,
1631                           args->peek))
1632         return -1;
1633
1634     if (*args->bytes_read > 0)
1635         /* got at least one byte, the SSL_read op can finish now */
1636         return 1;
1637
1638     return 0; /* did not read anything, keep trying */
1639 }
1640
1641 QUIC_TAKES_LOCK
1642 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1643 {
1644     int ret, res;
1645     QCTX ctx;
1646     struct quic_read_again_args args;
1647
1648     *bytes_read = 0;
1649
1650     if (!expect_quic_with_stream(s, /*remote_init=*/0, &ctx))
1651         return 0;
1652
1653     quic_lock(ctx.qc);
1654
1655     if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1656         ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1657         goto out;
1658     }
1659
1660     /* If we haven't finished the handshake, try to advance it. */
1661     if (quic_do_handshake(ctx.qc) < 1) {
1662         ret = 0; /* ossl_quic_do_handshake raised error here */
1663         goto out;
1664     }
1665
1666     if (ctx.xso->stream == NULL) {
1667         ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1668         goto out;
1669     }
1670
1671     if (!quic_read_actual(ctx.qc, ctx.xso->stream, buf, len, bytes_read, peek)) {
1672         ret = 0; /* quic_read_actual raised error here */
1673         goto out;
1674     }
1675
1676     if (*bytes_read > 0) {
1677         /*
1678          * Even though we succeeded, tick the reactor here to ensure we are
1679          * handling other aspects of the QUIC connection.
1680          */
1681         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1682         ret = 1;
1683     } else if (xso_blocking_mode(ctx.xso)) {
1684         /*
1685          * We were not able to read anything immediately, so our stream
1686          * buffer is empty. This means we need to block until we get
1687          * at least one byte.
1688          */
1689         args.qc         = ctx.qc;
1690         args.stream     = ctx.xso->stream;
1691         args.buf        = buf;
1692         args.len        = len;
1693         args.bytes_read = bytes_read;
1694         args.peek       = peek;
1695
1696         res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
1697         if (res == 0) {
1698             ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1699             goto out;
1700         } else if (res < 0) {
1701             ret = 0; /* quic_read_again raised error here */
1702             goto out;
1703         }
1704
1705         ret = 1;
1706     } else {
1707         /* We did not get any bytes and are not in blocking mode. */
1708         ret = QUIC_RAISE_NORMAL_ERROR(ctx.qc, SSL_ERROR_WANT_READ);
1709     }
1710
1711 out:
1712     quic_unlock(ctx.qc);
1713     return ret;
1714 }
1715
1716 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
1717 {
1718     return quic_read(s, buf, len, bytes_read, 0);
1719 }
1720
1721 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
1722 {
1723     return quic_read(s, buf, len, bytes_read, 1);
1724 }
1725
1726 /*
1727  * SSL_pending
1728  * -----------
1729  */
1730 QUIC_TAKES_LOCK
1731 static size_t ossl_quic_pending_int(const SSL *s)
1732 {
1733     QCTX ctx;
1734     size_t avail = 0;
1735     int fin = 0;
1736
1737     if (!expect_quic_with_stream(s, /*remote_init=*/-1, &ctx))
1738         return 0;
1739
1740     quic_lock(ctx.qc);
1741
1742     if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
1743         /* Cannot raise errors here because we are const, just fail. */
1744         goto out;
1745
1746     if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
1747         avail = 0;
1748
1749 out:
1750     quic_unlock(ctx.qc);
1751     return avail;
1752 }
1753
1754 size_t ossl_quic_pending(const SSL *s)
1755 {
1756     return ossl_quic_pending_int(s);
1757 }
1758
1759 int ossl_quic_has_pending(const SSL *s)
1760 {
1761     return ossl_quic_pending_int(s) > 0;
1762 }
1763
1764 /*
1765  * SSL_stream_conclude
1766  * -------------------
1767  */
1768 QUIC_TAKES_LOCK
1769 int ossl_quic_conn_stream_conclude(SSL *s)
1770 {
1771     QCTX ctx;
1772     QUIC_STREAM *qs;
1773
1774     if (!expect_quic_with_stream(s, /*remote_init=*/0, &ctx))
1775         return 0;
1776
1777     quic_lock(ctx.qc);
1778
1779     qs = ctx.xso->stream;
1780
1781     if (qs == NULL || qs->sstream == NULL) {
1782         quic_unlock(ctx.qc);
1783         return 0;
1784     }
1785
1786     if (!ossl_quic_channel_is_active(ctx.qc->ch)
1787         || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
1788         quic_unlock(ctx.qc);
1789         return 1;
1790     }
1791
1792     ossl_quic_sstream_fin(qs->sstream);
1793     quic_post_write(ctx.xso, 1, 1);
1794     quic_unlock(ctx.qc);
1795     return 1;
1796 }
1797
1798 /*
1799  * SSL_inject_net_dgram
1800  * --------------------
1801  */
1802 QUIC_TAKES_LOCK
1803 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
1804                          size_t buf_len,
1805                          const BIO_ADDR *peer,
1806                          const BIO_ADDR *local)
1807 {
1808     int ret;
1809     QCTX ctx;
1810     QUIC_DEMUX *demux;
1811
1812     if (!expect_quic(s, &ctx))
1813         return 0;
1814
1815     quic_lock(ctx.qc);
1816
1817     demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
1818     ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
1819
1820     quic_unlock(ctx.qc);
1821     return ret;
1822 }
1823
1824 /*
1825  * SSL_get0_connection
1826  * -------------------
1827  */
1828 SSL *ossl_quic_get0_connection(SSL *s)
1829 {
1830     QCTX ctx;
1831
1832     if (!expect_quic(s, &ctx))
1833         return NULL;
1834
1835     return &ctx.qc->ssl;
1836 }
1837
1838 /*
1839  * SSL_get_stream_type
1840  * -------------------
1841  */
1842 int ossl_quic_get_stream_type(SSL *s)
1843 {
1844     QCTX ctx;
1845
1846     if (!expect_quic(s, &ctx))
1847         return SSL_STREAM_TYPE_NONE;
1848
1849     if (ctx.xso == NULL) {
1850         /*
1851          * If we are deferring XSO creation, assume single stream mode and
1852          * default to BIDI, as the deferred XSO which will be created will be
1853          * bidirectional.
1854          */
1855         if (!ctx.qc->default_xso_created)
1856             return SSL_STREAM_TYPE_BIDI;
1857         else
1858             return SSL_STREAM_TYPE_NONE;
1859     }
1860
1861     if (ossl_quic_stream_is_bidi(ctx.xso->stream))
1862         return SSL_STREAM_TYPE_BIDI;
1863
1864     if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
1865         return SSL_STREAM_TYPE_READ;
1866     else
1867         return SSL_STREAM_TYPE_WRITE;
1868 }
1869
1870 /*
1871  * QUIC Front-End I/O API: SSL_CTX Management
1872  * ==========================================
1873  */
1874
1875 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1876 {
1877     switch (cmd) {
1878     default:
1879         return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
1880     }
1881 }
1882
1883 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
1884 {
1885     return ssl3_callback_ctrl(s, cmd, fp);
1886 }
1887
1888 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
1889 {
1890     return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
1891 }
1892
1893 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
1894 {
1895     /* We never do renegotiation. */
1896     return 0;
1897 }
1898
1899 /*
1900  * These functions define the TLSv1.2 (and below) ciphers that are supported by
1901  * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
1902  */
1903
1904 int ossl_quic_num_ciphers(void)
1905 {
1906     return 0;
1907 }
1908
1909 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
1910 {
1911     return NULL;
1912 }