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