b632ad22db2f86e329415382c64b965285a459ce
[openssl.git] / ssl / quic / quic_impl.c
1 /*
2  * Copyright 2022-2023 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 void quic_lock_for_io(QCTX *ctx);
30 static int quic_do_handshake(QCTX *ctx);
31 static void qc_update_reject_policy(QUIC_CONNECTION *qc);
32 static void qc_touch_default_xso(QUIC_CONNECTION *qc);
33 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
34 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
35                                         int touch, QUIC_XSO **old_xso);
36 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
37 static int quic_validate_for_write(QUIC_XSO *xso, int *err);
38 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
39 static int qc_blocking_mode(const QUIC_CONNECTION *qc);
40 static int xso_blocking_mode(const QUIC_XSO *xso);
41
42 /*
43  * QUIC Front-End I/O API: Common Utilities
44  * ========================================
45  */
46
47 /*
48  * Block until a predicate is met.
49  *
50  * Precondition: Must have a channel.
51  * Precondition: Must hold channel lock (unchecked).
52  */
53 QUIC_NEEDS_LOCK
54 static int block_until_pred(QUIC_CONNECTION *qc,
55                             int (*pred)(void *arg), void *pred_arg,
56                             uint32_t flags)
57 {
58     QUIC_REACTOR *rtor;
59
60     assert(qc->ch != NULL);
61
62     /*
63      * Any attempt to block auto-disables tick inhibition as otherwise we will
64      * hang around forever.
65      */
66     ossl_quic_channel_set_inhibit_tick(qc->ch, 0);
67
68     rtor = ossl_quic_channel_get_reactor(qc->ch);
69     return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
70                                               qc->mutex);
71 }
72
73 static OSSL_TIME get_time(QUIC_CONNECTION *qc)
74 {
75     if (qc->override_now_cb != NULL)
76         return qc->override_now_cb(qc->override_now_cb_arg);
77     else
78         return ossl_time_now();
79 }
80
81 static OSSL_TIME get_time_cb(void *arg)
82 {
83     QUIC_CONNECTION *qc = arg;
84
85     return get_time(qc);
86 }
87
88 /*
89  * QCTX is a utility structure which provides information we commonly wish to
90  * unwrap upon an API call being dispatched to us, namely:
91  *
92  *   - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
93  *     was passed);
94  *   - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
95  *     a QCSO with a default stream was passed);
96  *   - whether a QSSO was passed (xso == NULL must not be used to determine this
97  *     because it may be non-NULL when a QCSO is passed if that QCSO has a
98  *     default stream);
99  *   - whether we are in "I/O context", meaning that non-normal errors can
100  *     be reported via SSL_get_error() as well as via ERR. Functions such as
101  *     SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
102  *     functions which are allowed to change the value returned by
103  *     SSL_get_error. However, other functions (including functions which call
104  *     SSL_do_handshake() implicitly) are not allowed to change the return value
105  *     of SSL_get_error.
106  */
107 struct qctx_st {
108     QUIC_CONNECTION *qc;
109     QUIC_XSO        *xso;
110     int             is_stream, in_io;
111 };
112
113 QUIC_NEEDS_LOCK
114 static void quic_set_last_error(QCTX *ctx, int last_error)
115 {
116     if (!ctx->in_io)
117         return;
118
119     if (ctx->is_stream && ctx->xso != NULL)
120         ctx->xso->last_error = last_error;
121     else if (!ctx->is_stream && ctx->qc != NULL)
122         ctx->qc->last_error = last_error;
123 }
124
125 /*
126  * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
127  * rather than via ERR. Note that normal errors must always be raised while
128  * holding a lock.
129  */
130 QUIC_NEEDS_LOCK
131 static int quic_raise_normal_error(QCTX *ctx,
132                                    int err)
133 {
134     assert(ctx->in_io);
135     quic_set_last_error(ctx, err);
136
137     return 0;
138 }
139
140 /*
141  * Raise a 'non-normal' error, meaning any error that is not reported via
142  * SSL_get_error() and must be reported via ERR.
143  *
144  * qc should be provided if available. In exceptional circumstances when qc is
145  * not known NULL may be passed. This should generally only happen when an
146  * expect_...() function defined below fails, which generally indicates a
147  * dispatch error or caller error.
148  *
149  * ctx should be NULL if the connection lock is not held.
150  */
151 static int quic_raise_non_normal_error(QCTX *ctx,
152                                        const char *file,
153                                        int line,
154                                        const char *func,
155                                        int reason,
156                                        const char *fmt,
157                                        ...)
158 {
159     va_list args;
160
161     if (ctx != NULL) {
162         quic_set_last_error(ctx, SSL_ERROR_SSL);
163
164         if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
165             ossl_quic_channel_restore_err_state(ctx->qc->ch);
166     }
167
168     ERR_new();
169     ERR_set_debug(file, line, func);
170
171     va_start(args, fmt);
172     ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
173     va_end(args);
174
175     return 0;
176 }
177
178 #define QUIC_RAISE_NORMAL_ERROR(ctx, err)                       \
179     quic_raise_normal_error((ctx), (err))
180
181 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg)           \
182     quic_raise_non_normal_error((ctx),                          \
183                                 OPENSSL_FILE, OPENSSL_LINE,     \
184                                 OPENSSL_FUNC,                   \
185                                 (reason),                       \
186                                 (msg))
187
188 /*
189  * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
190  * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
191  *
192  * After this returns 1, all fields of the passed QCTX are initialised.
193  * Returns 0 on failure. This function is intended to be used to provide API
194  * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
195  */
196 static int expect_quic(const SSL *s, QCTX *ctx)
197 {
198     QUIC_CONNECTION *qc;
199     QUIC_XSO *xso;
200
201     ctx->qc         = NULL;
202     ctx->xso        = NULL;
203     ctx->is_stream  = 0;
204
205     if (s == NULL)
206         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
207
208     switch (s->type) {
209     case SSL_TYPE_QUIC_CONNECTION:
210         qc              = (QUIC_CONNECTION *)s;
211         ctx->qc         = qc;
212         ctx->xso        = qc->default_xso;
213         ctx->is_stream  = 0;
214         ctx->in_io      = 0;
215         return 1;
216
217     case SSL_TYPE_QUIC_XSO:
218         xso             = (QUIC_XSO *)s;
219         ctx->qc         = xso->conn;
220         ctx->xso        = xso;
221         ctx->is_stream  = 1;
222         ctx->in_io      = 0;
223         return 1;
224
225     default:
226         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
227     }
228 }
229
230 /*
231  * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
232  * other words, requires that the passed QSO be a QSSO or a QCSO with a default
233  * stream.
234  *
235  * remote_init determines if we expect the default XSO to be remotely created or
236  * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
237  *
238  * Channel mutex is acquired and retained on success.
239  */
240 QUIC_ACQUIRES_LOCK
241 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
242                                                     int in_io, QCTX *ctx)
243 {
244     if (!expect_quic(s, ctx))
245         return 0;
246
247     if (in_io)
248         quic_lock_for_io(ctx);
249     else
250         quic_lock(ctx->qc);
251
252     if (ctx->xso == NULL && remote_init >= 0) {
253         if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) {
254             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
255             goto err;
256         }
257
258         /* If we haven't finished the handshake, try to advance it. */
259         if (quic_do_handshake(ctx) < 1)
260             /* ossl_quic_do_handshake raised error here */
261             goto err;
262
263         if (remote_init == 0) {
264             if (!qc_try_create_default_xso_for_write(ctx))
265                 goto err;
266         } else {
267             if (!qc_wait_for_default_xso_for_read(ctx))
268                 goto err;
269         }
270
271         ctx->xso = ctx->qc->default_xso;
272     }
273
274     if (ctx->xso == NULL) {
275         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
276         goto err;
277     }
278
279     return 1; /* coverity[missing_unlock]: lock held */
280
281 err:
282     quic_unlock(ctx->qc);
283     return 0;
284 }
285
286 /*
287  * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
288  * be non-NULL if the QCSO has a default stream.
289  */
290 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
291 {
292     if (!expect_quic(s, ctx))
293         return 0;
294
295     if (ctx->is_stream)
296         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
297
298     return 1;
299 }
300
301 /*
302  * Ensures that the channel mutex is held for a method which touches channel
303  * state.
304  *
305  * Precondition: Channel mutex is not held (unchecked)
306  */
307 static void quic_lock(QUIC_CONNECTION *qc)
308 {
309 #if defined(OPENSSL_THREADS)
310     ossl_crypto_mutex_lock(qc->mutex);
311 #endif
312 }
313
314 static void quic_lock_for_io(QCTX *ctx)
315 {
316     quic_lock(ctx->qc);
317     ctx->in_io = 1;
318
319     /*
320      * We are entering an I/O function so we must update the values returned by
321      * SSL_get_error and SSL_want. Set no error. This will be overridden later
322      * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR
323      * occurs during the API call.
324      */
325     quic_set_last_error(ctx, SSL_ERROR_NONE);
326 }
327
328 /* Precondition: Channel mutex is held (unchecked) */
329 QUIC_NEEDS_LOCK
330 static void quic_unlock(QUIC_CONNECTION *qc)
331 {
332 #if defined(OPENSSL_THREADS)
333     ossl_crypto_mutex_unlock(qc->mutex);
334 #endif
335 }
336
337 /*
338  * This predicate is the criterion which should determine API call rejection for
339  * *most* mutating API calls, particularly stream-related operations for send
340  * parts.
341  *
342  * A call is rejected (this function returns 0) if shutdown is in progress
343  * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
344  * req_active=1, the connection must be active (i.e., the IDLE state is also
345  * rejected).
346  */
347 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
348 {
349     if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
350         return 0;
351
352     if (req_active && !ossl_quic_channel_is_active(qc->ch))
353         return 0;
354
355     return 1;
356 }
357
358 /*
359  * QUIC Front-End I/O API: Initialization
360  * ======================================
361  *
362  *         SSL_new                  => ossl_quic_new
363  *                                     ossl_quic_init
364  *         SSL_reset                => ossl_quic_reset
365  *         SSL_clear                => ossl_quic_clear
366  *                                     ossl_quic_deinit
367  *         SSL_free                 => ossl_quic_free
368  *
369  *         SSL_set_options          => ossl_quic_set_options
370  *         SSL_get_options          => ossl_quic_get_options
371  *         SSL_clear_options        => ossl_quic_clear_options
372  *
373  */
374
375 /* SSL_new */
376 SSL *ossl_quic_new(SSL_CTX *ctx)
377 {
378     QUIC_CONNECTION *qc = NULL;
379     SSL *ssl_base = NULL;
380     SSL_CONNECTION *sc = NULL;
381
382     qc = OPENSSL_zalloc(sizeof(*qc));
383     if (qc == NULL) {
384         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
385         goto err;
386     }
387
388     /* Initialise the QUIC_CONNECTION's stub header. */
389     ssl_base = &qc->ssl;
390     if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
391         ssl_base = NULL;
392         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
393         goto err;
394     }
395
396     qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
397     if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
398         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
399         goto err;
400     }
401
402     /* override the user_ssl of the inner connection */
403     sc->s3.flags |= TLS1_FLAGS_QUIC;
404
405     /* Restrict options derived from the SSL_CTX. */
406     sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
407     sc->pha_enabled = 0;
408
409 #if defined(OPENSSL_THREADS)
410     if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) {
411         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
412         goto err;
413     }
414 #endif
415
416 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
417     qc->is_thread_assisted
418         = (ssl_base->method == OSSL_QUIC_client_thread_method());
419 #endif
420
421     qc->as_server       = 0; /* TODO(QUIC SERVER): add server support */
422     qc->as_server_state = qc->as_server;
423
424     qc->default_stream_mode     = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
425     qc->default_ssl_mode        = qc->ssl.ctx->mode;
426     qc->default_ssl_options     = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
427     qc->desires_blocking        = 1;
428     qc->blocking                = 0;
429     qc->incoming_stream_policy  = SSL_INCOMING_STREAM_POLICY_AUTO;
430     qc->last_error              = SSL_ERROR_NONE;
431
432     if (!create_channel(qc))
433         goto err;
434
435     ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
436     ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
437
438     qc_update_reject_policy(qc);
439
440     /*
441      * We do not create the default XSO yet. The reason for this is that the
442      * stream ID of the default XSO will depend on whether the stream is client
443      * or server-initiated, which depends on who transmits first. Since we do
444      * not know whether the application will be using a client-transmits-first
445      * or server-transmits-first protocol, we defer default XSO creation until
446      * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
447      * we take that as a cue that the client is expecting a server-initiated
448      * stream, and vice versa if SSL_write() is called first.
449      */
450     return ssl_base;
451
452 err:
453     if (qc != NULL) {
454 #if defined(OPENSSL_THREADS)
455         ossl_crypto_mutex_free(qc->mutex);
456 #endif
457         ossl_quic_channel_free(qc->ch);
458         SSL_free(qc->tls);
459     }
460     OPENSSL_free(qc);
461     return NULL;
462 }
463
464 /* SSL_free */
465 QUIC_TAKES_LOCK
466 void ossl_quic_free(SSL *s)
467 {
468     QCTX ctx;
469     int is_default;
470
471     /* We should never be called on anything but a QSO. */
472     if (!expect_quic(s, &ctx))
473         return;
474
475     quic_lock(ctx.qc);
476
477     if (ctx.is_stream) {
478         /*
479          * When a QSSO is freed, the XSO is freed immediately, because the XSO
480          * itself only contains API personality layer data. However the
481          * underlying QUIC_STREAM is not freed immediately but is instead marked
482          * as deleted for later collection.
483          */
484
485         assert(ctx.qc->num_xso > 0);
486         --ctx.qc->num_xso;
487
488         /* If a stream's send part has not been finished, auto-reset it. */
489         if ((   ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
490              || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
491             && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
492             ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
493                                                         ctx.xso->stream, 0);
494
495         /* Do STOP_SENDING for the receive part, if applicable. */
496         if (   ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
497             || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
498             ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
499                                                         ctx.xso->stream, 0);
500
501         /* Update stream state. */
502         ctx.xso->stream->deleted = 1;
503         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
504                                           ctx.xso->stream);
505
506         is_default = (ctx.xso == ctx.qc->default_xso);
507         quic_unlock(ctx.qc);
508
509         /*
510          * Unref the connection in most cases; the XSO has a ref to the QC and
511          * not vice versa. But for a default XSO, to avoid circular references,
512          * the QC refs the XSO but the XSO does not ref the QC. If we are the
513          * default XSO, we only get here when the QC is being torn down anyway,
514          * so don't call SSL_free(qc) as we are already in it.
515          */
516         if (!is_default)
517             SSL_free(&ctx.qc->ssl);
518
519         /* Note: SSL_free calls OPENSSL_free(xso) for us */
520         return;
521     }
522
523     /*
524      * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
525      * stage, but is freed during the channel free when the whole QSM is freed.
526      */
527     if (ctx.qc->default_xso != NULL) {
528         QUIC_XSO *xso = ctx.qc->default_xso;
529
530         quic_unlock(ctx.qc);
531         SSL_free(&xso->ssl);
532         quic_lock(ctx.qc);
533         ctx.qc->default_xso = NULL;
534     }
535
536     /* Ensure we have no remaining XSOs. */
537     assert(ctx.qc->num_xso == 0);
538
539 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
540     if (ctx.qc->is_thread_assisted && ctx.qc->started) {
541         ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
542         ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
543     }
544 #endif
545
546     ossl_quic_channel_free(ctx.qc->ch);
547
548     BIO_free(ctx.qc->net_rbio);
549     BIO_free(ctx.qc->net_wbio);
550
551     /* Note: SSL_free calls OPENSSL_free(qc) for us */
552
553     SSL_free(ctx.qc->tls);
554     quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
555 #if defined(OPENSSL_THREADS)
556     ossl_crypto_mutex_free(&ctx.qc->mutex);
557 #endif
558 }
559
560 /* SSL method init */
561 int ossl_quic_init(SSL *s)
562 {
563     /* Same op as SSL_clear, forward the call. */
564     return ossl_quic_clear(s);
565 }
566
567 /* SSL method deinit */
568 void ossl_quic_deinit(SSL *s)
569 {
570     /* No-op. */
571 }
572
573 /* SSL_clear (ssl_reset method) */
574 int ossl_quic_reset(SSL *s)
575 {
576     QCTX ctx;
577
578     if (!expect_quic(s, &ctx))
579         return 0;
580
581     ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
582     return 0;
583 }
584
585 /* ssl_clear method (unused) */
586 int ossl_quic_clear(SSL *s)
587 {
588     QCTX ctx;
589
590     if (!expect_quic(s, &ctx))
591         return 0;
592
593     ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
594     return 0;
595 }
596
597 int ossl_quic_conn_set_override_now_cb(SSL *s,
598                                        OSSL_TIME (*now_cb)(void *arg),
599                                        void *now_cb_arg)
600 {
601     QCTX ctx;
602
603     if (!expect_quic(s, &ctx))
604         return 0;
605
606     quic_lock(ctx.qc);
607
608     ctx.qc->override_now_cb     = now_cb;
609     ctx.qc->override_now_cb_arg = now_cb_arg;
610
611     quic_unlock(ctx.qc);
612     return 1;
613 }
614
615 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
616 {
617     QCTX ctx;
618
619     if (!expect_quic(s, &ctx))
620         return;
621
622 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
623     if (ctx.qc->is_thread_assisted && ctx.qc->started)
624         ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
625 #endif
626 }
627
628 QUIC_NEEDS_LOCK
629 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
630 {
631     qc->default_xso_created = 1;
632     qc_update_reject_policy(qc);
633 }
634
635 /*
636  * Changes default XSO. Allows caller to keep reference to the old default XSO
637  * (if any). Reference to new XSO is transferred from caller.
638  */
639 QUIC_NEEDS_LOCK
640 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
641                                         int touch,
642                                         QUIC_XSO **old_xso)
643 {
644     int refs;
645
646     *old_xso = NULL;
647
648     if (qc->default_xso != xso) {
649         *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
650
651         qc->default_xso = xso;
652
653         if (xso == NULL) {
654             /*
655              * Changing to not having a default XSO. XSO becomes standalone and
656              * now has a ref to the QC.
657              */
658             if (!ossl_assert(SSL_up_ref(&qc->ssl)))
659                 return;
660         } else {
661             /*
662              * Changing from not having a default XSO to having one. The new XSO
663              * will have had a reference to the QC we need to drop to avoid a
664              * circular reference.
665              *
666              * Currently we never change directly from one default XSO to
667              * another, though this function would also still be correct if this
668              * weren't the case.
669              */
670             assert(*old_xso == NULL);
671
672             CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
673             assert(refs > 0);
674         }
675     }
676
677     if (touch)
678         qc_touch_default_xso(qc);
679 }
680
681 /*
682  * Changes default XSO, releasing the reference to any previous default XSO.
683  * Reference to new XSO is transferred from caller.
684  */
685 QUIC_NEEDS_LOCK
686 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
687 {
688     QUIC_XSO *old_xso = NULL;
689
690     qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
691
692     if (old_xso != NULL)
693         SSL_free(&old_xso->ssl);
694 }
695
696 QUIC_NEEDS_LOCK
697 static void xso_update_options(QUIC_XSO *xso)
698 {
699     int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
700
701     if (xso->stream->rstream != NULL)
702         ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
703
704     if (xso->stream->sstream != NULL)
705         ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
706 }
707
708 /*
709  * SSL_set_options
710  * ---------------
711  *
712  * Setting options on a QCSO
713  *   - configures the handshake-layer options;
714  *   - configures the default data-plane options for new streams;
715  *   - configures the data-plane options on the default XSO, if there is one.
716  *
717  * Setting options on a QSSO
718  *   - configures data-plane options for that stream only.
719  */
720 QUIC_TAKES_LOCK
721 static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
722 {
723     QCTX ctx;
724     uint64_t hs_mask_value, hs_or_value, ret;
725
726     if (!expect_quic(ssl, &ctx))
727         return 0;
728
729     quic_lock(ctx.qc);
730
731     if (!ctx.is_stream) {
732         /*
733          * If we were called on the connection, we apply any handshake option
734          * changes.
735          */
736         hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
737         hs_or_value   = (or_value   & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
738
739         SSL_clear_options(ctx.qc->tls, hs_mask_value);
740         SSL_set_options(ctx.qc->tls, hs_or_value);
741
742         /* Update defaults for new streams. */
743         ctx.qc->default_ssl_options
744             = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
745               & OSSL_QUIC_PERMITTED_OPTIONS;
746     }
747
748     if (ctx.xso != NULL) {
749         ctx.xso->ssl_options
750             = ((ctx.xso->ssl_options & ~mask_value) | or_value)
751             & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
752
753         xso_update_options(ctx.xso);
754     }
755
756     ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;
757
758     quic_unlock(ctx.qc);
759     return ret;
760 }
761
762 uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
763 {
764     return quic_mask_or_options(ssl, 0, options);
765 }
766
767 /* SSL_clear_options */
768 uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
769 {
770     return quic_mask_or_options(ssl, options, 0);
771 }
772
773 /* SSL_get_options */
774 uint64_t ossl_quic_get_options(const SSL *ssl)
775 {
776     return quic_mask_or_options((SSL *)ssl, 0, 0);
777 }
778
779 /*
780  * QUIC Front-End I/O API: Network BIO Configuration
781  * =================================================
782  *
783  * Handling the different BIOs is difficult:
784  *
785  *   - It is more or less a requirement that we use non-blocking network I/O;
786  *     we need to be able to have timeouts on recv() calls, and make best effort
787  *     (non blocking) send() and recv() calls.
788  *
789  *     The only sensible way to do this is to configure the socket into
790  *     non-blocking mode. We could try to do select() before calling send() or
791  *     recv() to get a guarantee that the call will not block, but this will
792  *     probably run into issues with buggy OSes which generate spurious socket
793  *     readiness events. In any case, relying on this to work reliably does not
794  *     seem sane.
795  *
796  *     Timeouts could be handled via setsockopt() socket timeout options, but
797  *     this depends on OS support and adds another syscall to every network I/O
798  *     operation. It also has obvious thread safety concerns if we want to move
799  *     to concurrent use of a single socket at some later date.
800  *
801  *     Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
802  *     be made non-blocking. However some OSes (e.g. Windows) do not support
803  *     this, so we cannot rely on this.
804  *
805  *     As such, we need to configure any FD in non-blocking mode. This may
806  *     confound users who pass a blocking socket to libssl. However, in practice
807  *     it would be extremely strange for a user of QUIC to pass an FD to us,
808  *     then also try and send receive traffic on the same socket(!). Thus the
809  *     impact of this should be limited, and can be documented.
810  *
811  *   - We support both blocking and non-blocking operation in terms of the API
812  *     presented to the user. One prospect is to set the blocking mode based on
813  *     whether the socket passed to us was already in blocking mode. However,
814  *     Windows has no API for determining if a socket is in blocking mode (!),
815  *     therefore this cannot be done portably. Currently therefore we expose an
816  *     explicit API call to set this, and default to blocking mode.
817  *
818  *   - We need to determine our initial destination UDP address. The "natural"
819  *     way for a user to do this is to set the peer variable on a BIO_dgram.
820  *     However, this has problems because BIO_dgram's peer variable is used for
821  *     both transmission and reception. This means it can be constantly being
822  *     changed to a malicious value (e.g. if some random unrelated entity on the
823  *     network starts sending traffic to us) on every read call. This is not a
824  *     direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
825  *     calls only, which do not use this variable. However, we do need to let
826  *     the user specify the peer in a 'normal' manner. The compromise here is
827  *     that we grab the current peer value set at the time the write BIO is set
828  *     and do not read the value again.
829  *
830  *   - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
831  *     Currently we do this by only supporting non-blocking mode.
832  *
833  */
834
835 /*
836  * Determines what initial destination UDP address we should use, if possible.
837  * If this fails the client must set the destination address manually, or use a
838  * BIO which does not need a destination address.
839  */
840 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
841 {
842     if (BIO_dgram_detect_peer_addr(net_wbio, peer) <= 0)
843         return 0;
844
845     return 1;
846 }
847
848 static int qc_can_support_blocking_cached(QUIC_CONNECTION *qc)
849 {
850     QUIC_REACTOR *rtor = ossl_quic_channel_get_reactor(qc->ch);
851
852     return ossl_quic_reactor_can_poll_r(rtor)
853         && ossl_quic_reactor_can_poll_w(rtor);
854 }
855
856 static void qc_update_can_support_blocking(QUIC_CONNECTION *qc)
857 {
858     ossl_quic_channel_update_poll_descriptors(qc->ch); /* best effort */
859 }
860
861 static void qc_update_blocking_mode(QUIC_CONNECTION *qc)
862 {
863     qc->blocking = qc->desires_blocking && qc_can_support_blocking_cached(qc);
864 }
865
866 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
867 {
868     QCTX ctx;
869
870     if (!expect_quic(s, &ctx))
871         return;
872
873     if (ctx.qc->net_rbio == net_rbio)
874         return;
875
876     if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
877         return;
878
879     BIO_free(ctx.qc->net_rbio);
880     ctx.qc->net_rbio = net_rbio;
881
882     if (net_rbio != NULL)
883         BIO_set_nbio(net_rbio, 1); /* best effort autoconfig */
884
885     /*
886      * Determine if the current pair of read/write BIOs now set allows blocking
887      * mode to be supported.
888      */
889     qc_update_can_support_blocking(ctx.qc);
890     qc_update_blocking_mode(ctx.qc);
891 }
892
893 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
894 {
895     QCTX ctx;
896
897     if (!expect_quic(s, &ctx))
898         return;
899
900     if (ctx.qc->net_wbio == net_wbio)
901         return;
902
903     if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
904         return;
905
906     BIO_free(ctx.qc->net_wbio);
907     ctx.qc->net_wbio = net_wbio;
908
909     if (net_wbio != NULL)
910         BIO_set_nbio(net_wbio, 1); /* best effort autoconfig */
911
912     /*
913      * Determine if the current pair of read/write BIOs now set allows blocking
914      * mode to be supported.
915      */
916     qc_update_can_support_blocking(ctx.qc);
917     qc_update_blocking_mode(ctx.qc);
918 }
919
920 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
921 {
922     QCTX ctx;
923
924     if (!expect_quic(s, &ctx))
925         return NULL;
926
927     return ctx.qc->net_rbio;
928 }
929
930 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
931 {
932     QCTX ctx;
933
934     if (!expect_quic(s, &ctx))
935         return NULL;
936
937     return ctx.qc->net_wbio;
938 }
939
940 int ossl_quic_conn_get_blocking_mode(const SSL *s)
941 {
942     QCTX ctx;
943
944     if (!expect_quic(s, &ctx))
945         return 0;
946
947     if (ctx.is_stream)
948         return xso_blocking_mode(ctx.xso);
949
950     return qc_blocking_mode(ctx.qc);
951 }
952
953 QUIC_TAKES_LOCK
954 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
955 {
956     int ret = 0;
957     QCTX ctx;
958
959     if (!expect_quic(s, &ctx))
960         return 0;
961
962     quic_lock(ctx.qc);
963
964     /* Sanity check - can we support the request given the current network BIO? */
965     if (blocking) {
966         /*
967          * If called directly on a QCSO, update our information on network BIO
968          * capabilities.
969          */
970         if (!ctx.is_stream)
971             qc_update_can_support_blocking(ctx.qc);
972
973         /* Cannot enable blocking mode if we do not have pollable FDs. */
974         if (!qc_can_support_blocking_cached(ctx.qc)) {
975             ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
976             goto out;
977         }
978     }
979
980     if (!ctx.is_stream)
981         /*
982          * If called directly on a QCSO, update default and connection-level
983          * blocking modes.
984          */
985         ctx.qc->desires_blocking = (blocking != 0);
986
987     if (ctx.xso != NULL) {
988         /*
989          * If called on a QSSO or a QCSO with a default XSO, update the blocking
990          * mode.
991          */
992         ctx.xso->desires_blocking       = (blocking != 0);
993         ctx.xso->desires_blocking_set   = 1;
994     }
995
996     ret = 1;
997 out:
998     qc_update_blocking_mode(ctx.qc);
999     quic_unlock(ctx.qc);
1000     return ret;
1001 }
1002
1003 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
1004                                          const BIO_ADDR *peer_addr)
1005 {
1006     QCTX ctx;
1007
1008     if (!expect_quic(s, &ctx))
1009         return 0;
1010
1011     if (ctx.qc->started)
1012         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
1013                                        NULL);
1014
1015     if (peer_addr == NULL) {
1016         BIO_ADDR_clear(&ctx.qc->init_peer_addr);
1017         return 1;
1018     }
1019
1020     ctx.qc->init_peer_addr = *peer_addr;
1021     return 1;
1022 }
1023
1024 /*
1025  * QUIC Front-End I/O API: Asynchronous I/O Management
1026  * ===================================================
1027  *
1028  *   (BIO/)SSL_handle_events        => ossl_quic_handle_events
1029  *   (BIO/)SSL_get_event_timeout    => ossl_quic_get_event_timeout
1030  *   (BIO/)SSL_get_poll_fd          => ossl_quic_get_poll_fd
1031  *
1032  */
1033
1034 /* Returns 1 if the connection is being used in blocking mode. */
1035 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
1036 {
1037     return qc->blocking;
1038 }
1039
1040 static int xso_blocking_mode(const QUIC_XSO *xso)
1041 {
1042     if (xso->desires_blocking_set)
1043         return xso->desires_blocking && qc_can_support_blocking_cached(xso->conn);
1044     else
1045         /* Only ever set if we can support blocking. */
1046         return xso->conn->blocking;
1047 }
1048
1049 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
1050 QUIC_TAKES_LOCK
1051 int ossl_quic_handle_events(SSL *s)
1052 {
1053     QCTX ctx;
1054
1055     if (!expect_quic(s, &ctx))
1056         return 0;
1057
1058     quic_lock(ctx.qc);
1059     ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1060     quic_unlock(ctx.qc);
1061     return 1;
1062 }
1063
1064 /*
1065  * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1066  * should next have events handled by the application by calling
1067  * SSL_handle_events(). tv is set to 0 if the object should have events handled
1068  * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1069  * the value of *tv is undefined.
1070  */
1071 QUIC_TAKES_LOCK
1072 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1073 {
1074     QCTX ctx;
1075     OSSL_TIME deadline = ossl_time_infinite();
1076
1077     if (!expect_quic(s, &ctx))
1078         return 0;
1079
1080     quic_lock(ctx.qc);
1081
1082     deadline
1083         = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
1084
1085     if (ossl_time_is_infinite(deadline)) {
1086         *is_infinite = 1;
1087
1088         /*
1089          * Robustness against faulty applications that don't check *is_infinite;
1090          * harmless long timeout.
1091          */
1092         tv->tv_sec  = 1000000;
1093         tv->tv_usec = 0;
1094
1095         quic_unlock(ctx.qc);
1096         return 1;
1097     }
1098
1099     *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
1100     *is_infinite = 0;
1101     quic_unlock(ctx.qc);
1102     return 1;
1103 }
1104
1105 /* SSL_get_rpoll_descriptor */
1106 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1107 {
1108     QCTX ctx;
1109
1110     if (!expect_quic(s, &ctx))
1111         return 0;
1112
1113     if (desc == NULL || ctx.qc->net_rbio == NULL)
1114         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1115                                        NULL);
1116
1117     return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
1118 }
1119
1120 /* SSL_get_wpoll_descriptor */
1121 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1122 {
1123     QCTX ctx;
1124
1125     if (!expect_quic(s, &ctx))
1126         return 0;
1127
1128     if (desc == NULL || ctx.qc->net_wbio == NULL)
1129         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1130                                        NULL);
1131
1132     return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
1133 }
1134
1135 /* SSL_net_read_desired */
1136 QUIC_TAKES_LOCK
1137 int ossl_quic_get_net_read_desired(SSL *s)
1138 {
1139     QCTX ctx;
1140     int ret;
1141
1142     if (!expect_quic(s, &ctx))
1143         return 0;
1144
1145     quic_lock(ctx.qc);
1146     ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1147     quic_unlock(ctx.qc);
1148     return ret;
1149 }
1150
1151 /* SSL_net_write_desired */
1152 QUIC_TAKES_LOCK
1153 int ossl_quic_get_net_write_desired(SSL *s)
1154 {
1155     int ret;
1156     QCTX ctx;
1157
1158     if (!expect_quic(s, &ctx))
1159         return 0;
1160
1161     quic_lock(ctx.qc);
1162     ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1163     quic_unlock(ctx.qc);
1164     return ret;
1165 }
1166
1167 /*
1168  * QUIC Front-End I/O API: Connection Lifecycle Operations
1169  * =======================================================
1170  *
1171  *         SSL_do_handshake         => ossl_quic_do_handshake
1172  *         SSL_set_connect_state    => ossl_quic_set_connect_state
1173  *         SSL_set_accept_state     => ossl_quic_set_accept_state
1174  *         SSL_shutdown             => ossl_quic_shutdown
1175  *         SSL_ctrl                 => ossl_quic_ctrl
1176  *   (BIO/)SSL_connect              => ossl_quic_connect
1177  *   (BIO/)SSL_accept               => ossl_quic_accept
1178  *
1179  */
1180
1181 QUIC_NEEDS_LOCK
1182 static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1183 {
1184     QUIC_STREAM_MAP *qsm;
1185
1186     if (qc->shutting_down)
1187         return;
1188
1189     qsm = ossl_quic_channel_get_qsm(qc->ch);
1190
1191     ossl_quic_stream_map_begin_shutdown_flush(qsm);
1192     qc->shutting_down = 1;
1193 }
1194
1195 /* Returns 1 if all shutdown-flush streams have been done with. */
1196 QUIC_NEEDS_LOCK
1197 static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1198 {
1199     QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1200
1201     return qc->shutting_down
1202         && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1203 }
1204
1205 /* SSL_shutdown */
1206 static int quic_shutdown_wait(void *arg)
1207 {
1208     QUIC_CONNECTION *qc = arg;
1209
1210     return ossl_quic_channel_is_terminated(qc->ch);
1211 }
1212
1213 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
1214 static int quic_shutdown_flush_wait(void *arg)
1215 {
1216     QUIC_CONNECTION *qc = arg;
1217
1218     return ossl_quic_channel_is_term_any(qc->ch)
1219         || qc_shutdown_flush_finished(qc);
1220 }
1221
1222 static int quic_shutdown_peer_wait(void *arg)
1223 {
1224     QUIC_CONNECTION *qc = arg;
1225     return ossl_quic_channel_is_term_any(qc->ch);
1226 }
1227
1228 QUIC_TAKES_LOCK
1229 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1230                             const SSL_SHUTDOWN_EX_ARGS *args,
1231                             size_t args_len)
1232 {
1233     int ret;
1234     QCTX ctx;
1235     int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1236     int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);
1237     int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);
1238
1239     if (!expect_quic(s, &ctx))
1240         return -1;
1241
1242     if (ctx.is_stream) {
1243         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
1244         return -1;
1245     }
1246
1247     quic_lock(ctx.qc);
1248
1249     if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1250         quic_unlock(ctx.qc);
1251         return 1;
1252     }
1253
1254     /* Phase 1: Stream Flushing */
1255     if (!wait_peer && stream_flush) {
1256         qc_shutdown_flush_init(ctx.qc);
1257
1258         if (!qc_shutdown_flush_finished(ctx.qc)) {
1259             if (!no_block && qc_blocking_mode(ctx.qc)) {
1260                 ret = block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);
1261                 if (ret < 1) {
1262                     ret = 0;
1263                     goto err;
1264                 }
1265             } else {
1266                 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1267             }
1268         }
1269
1270         if (!qc_shutdown_flush_finished(ctx.qc)) {
1271             quic_unlock(ctx.qc);
1272             return 0; /* ongoing */
1273         }
1274     }
1275
1276     /* Phase 2: Connection Closure */
1277     if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1278         if (!no_block && qc_blocking_mode(ctx.qc)) {
1279             ret = block_until_pred(ctx.qc, quic_shutdown_peer_wait, ctx.qc, 0);
1280             if (ret < 1) {
1281                 ret = 0;
1282                 goto err;
1283             }
1284         } else {
1285             ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1286         }
1287
1288         if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1289             ret = 0; /* peer hasn't closed yet - still not done */
1290             goto err;
1291         }
1292
1293         /*
1294          * We are at least terminating - go through the normal process of
1295          * waiting until we are in the TERMINATED state.
1296          */
1297     }
1298
1299     /* Block mutation ops regardless of if we did stream flush. */
1300     ctx.qc->shutting_down = 1;
1301
1302     /*
1303      * This call is a no-op if we are already terminating, so it doesn't
1304      * affect the wait_peer case.
1305      */
1306     ossl_quic_channel_local_close(ctx.qc->ch,
1307                                   args != NULL ? args->quic_error_code : 0,
1308                                   args != NULL ? args->quic_reason : NULL);
1309
1310     SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1311
1312     if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1313         quic_unlock(ctx.qc);
1314         return 1;
1315     }
1316
1317     /* Phase 3: Terminating Wait Time */
1318     if (!no_block && qc_blocking_mode(ctx.qc)
1319         && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1320         ret = block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1321         if (ret < 1) {
1322             ret = 0;
1323             goto err;
1324         }
1325     } else {
1326         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1327     }
1328
1329     ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1330 err:
1331     quic_unlock(ctx.qc);
1332     return ret;
1333 }
1334
1335 /* SSL_ctrl */
1336 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1337 {
1338     QCTX ctx;
1339
1340     if (!expect_quic(s, &ctx))
1341         return 0;
1342
1343     switch (cmd) {
1344     case SSL_CTRL_MODE:
1345         /* If called on a QCSO, update the default mode. */
1346         if (!ctx.is_stream)
1347             ctx.qc->default_ssl_mode |= (uint32_t)larg;
1348
1349         /*
1350          * If we were called on a QSSO or have a default stream, we also update
1351          * that.
1352          */
1353         if (ctx.xso != NULL) {
1354             /* Cannot enable EPW while AON write in progress. */
1355             if (ctx.xso->aon_write_in_progress)
1356                 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1357
1358             ctx.xso->ssl_mode |= (uint32_t)larg;
1359             return ctx.xso->ssl_mode;
1360         }
1361
1362         return ctx.qc->default_ssl_mode;
1363     case SSL_CTRL_CLEAR_MODE:
1364         if (!ctx.is_stream)
1365             ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1366
1367         if (ctx.xso != NULL) {
1368             ctx.xso->ssl_mode &= ~(uint32_t)larg;
1369             return ctx.xso->ssl_mode;
1370         }
1371
1372         return ctx.qc->default_ssl_mode;
1373
1374     case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1375         ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1376         /* This ctrl also needs to be passed to the internal SSL object */
1377         return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1378
1379     case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1380         {
1381             int is_infinite;
1382
1383             if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1384                 return 0;
1385
1386             return !is_infinite;
1387         }
1388     case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1389         /* For legacy compatibility with DTLS calls. */
1390         return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1391
1392         /* Mask ctrls we shouldn't support for QUIC. */
1393     case SSL_CTRL_GET_READ_AHEAD:
1394     case SSL_CTRL_SET_READ_AHEAD:
1395     case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1396     case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
1397     case SSL_CTRL_SET_MAX_PIPELINES:
1398         return 0;
1399
1400     default:
1401         /*
1402          * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1403          * implementation. Either SSL_ctrl will handle it itself by direct
1404          * access into handshake layer state, or failing that, it will be passed
1405          * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1406          * supported by anything, the handshake layer's ctrl method will finally
1407          * return 0.
1408          */
1409         return ossl_ctrl_internal(&ctx.qc->ssl, cmd, larg, parg, /*no_quic=*/1);
1410     }
1411 }
1412
1413 /* SSL_set_connect_state */
1414 void ossl_quic_set_connect_state(SSL *s)
1415 {
1416     QCTX ctx;
1417
1418     if (!expect_quic(s, &ctx))
1419         return;
1420
1421     /* Cannot be changed after handshake started */
1422     if (ctx.qc->started || ctx.is_stream)
1423         return;
1424
1425     ctx.qc->as_server_state = 0;
1426 }
1427
1428 /* SSL_set_accept_state */
1429 void ossl_quic_set_accept_state(SSL *s)
1430 {
1431     QCTX ctx;
1432
1433     if (!expect_quic(s, &ctx))
1434         return;
1435
1436     /* Cannot be changed after handshake started */
1437     if (ctx.qc->started || ctx.is_stream)
1438         return;
1439
1440     ctx.qc->as_server_state = 1;
1441 }
1442
1443 /* SSL_do_handshake */
1444 struct quic_handshake_wait_args {
1445     QUIC_CONNECTION     *qc;
1446 };
1447
1448 static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)
1449 {
1450     int want = SSL_want(qc->tls);
1451
1452     if (want == SSL_X509_LOOKUP
1453             || want == SSL_CLIENT_HELLO_CB
1454             || want == SSL_RETRY_VERIFY)
1455         return 1;
1456
1457     return 0;
1458 }
1459
1460 static int quic_handshake_wait(void *arg)
1461 {
1462     struct quic_handshake_wait_args *args = arg;
1463
1464     if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1465         return -1;
1466
1467     if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1468         return 1;
1469
1470     if (tls_wants_non_io_retry(args->qc))
1471         return 1;
1472
1473     return 0;
1474 }
1475
1476 static int configure_channel(QUIC_CONNECTION *qc)
1477 {
1478     assert(qc->ch != NULL);
1479
1480     if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1481         || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1482         || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1483         return 0;
1484
1485     return 1;
1486 }
1487
1488 QUIC_NEEDS_LOCK
1489 static int create_channel(QUIC_CONNECTION *qc)
1490 {
1491     QUIC_CHANNEL_ARGS args = {0};
1492
1493     args.libctx     = qc->ssl.ctx->libctx;
1494     args.propq      = qc->ssl.ctx->propq;
1495     args.is_server  = qc->as_server;
1496     args.tls        = qc->tls;
1497     args.mutex      = qc->mutex;
1498     args.now_cb     = get_time_cb;
1499     args.now_cb_arg = qc;
1500
1501     qc->ch = ossl_quic_channel_new(&args);
1502     if (qc->ch == NULL) {
1503         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1504         return 0;
1505     }
1506
1507     return 1;
1508 }
1509
1510 /*
1511  * Configures a channel with the information we have accumulated via calls made
1512  * to us from the application prior to starting a handshake attempt.
1513  */
1514 QUIC_NEEDS_LOCK
1515 static int ensure_channel_started(QCTX *ctx)
1516 {
1517     QUIC_CONNECTION *qc = ctx->qc;
1518
1519     if (!qc->started) {
1520         if (!configure_channel(qc)) {
1521             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1522                                         "failed to configure channel");
1523             return 0;
1524         }
1525
1526         if (!ossl_quic_channel_start(qc->ch)) {
1527             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1528                                         "failed to start channel");
1529             return 0;
1530         }
1531
1532 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1533         if (qc->is_thread_assisted)
1534             if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch)) {
1535                 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1536                                             "failed to start assist thread");
1537                 return 0;
1538             }
1539 #endif
1540     }
1541
1542     qc->started = 1;
1543     return 1;
1544 }
1545
1546 QUIC_NEEDS_LOCK
1547 static int quic_do_handshake(QCTX *ctx)
1548 {
1549     int ret;
1550     QUIC_CONNECTION *qc = ctx->qc;
1551
1552     if (ossl_quic_channel_is_handshake_complete(qc->ch))
1553         /* Handshake already completed. */
1554         return 1;
1555
1556     if (!quic_mutation_allowed(qc, /*req_active=*/0))
1557         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1558
1559     if (qc->as_server != qc->as_server_state) {
1560         QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1561         return -1; /* Non-protocol error */
1562     }
1563
1564     if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1565         /* Need read and write BIOs. */
1566         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1567         return -1; /* Non-protocol error */
1568     }
1569
1570     /*
1571      * We need to determine our addressing mode. There are basically two
1572      * ways we can use L4 addresses:
1573      *
1574      *   - Addressed mode, in which our BIO_sendmmsg calls have destination
1575      *     addresses attached to them which we expect the underlying network BIO
1576      *     to handle;
1577      *
1578      *   - Unaddressed mode, in which the BIO provided to us on the
1579      *     network side neither provides us with L4 addresses nor is capable of
1580      *     honouring ones we provide. We don't know where the QUIC traffic we
1581      *     send ends up exactly and trust the application to know what it is
1582      *     doing.
1583      *
1584      * Addressed mode is preferred because it enables support for connection
1585      * migration, multipath, etc. in the future. Addressed mode is automatically
1586      * enabled if we are using e.g. BIO_s_datagram, with or without
1587      * BIO_s_connect.
1588      *
1589      * If we are passed a BIO_s_dgram_pair (or some custom BIO) we may have to
1590      * use unaddressed mode unless that BIO supports capability flags indicating
1591      * it can provide and honour L4 addresses.
1592      *
1593      * Our strategy for determining address mode is simple: we probe the
1594      * underlying network BIOs for their capabilities. If the network BIOs
1595      * support what we need, we use addressed mode. Otherwise, we use
1596      * unaddressed mode.
1597      *
1598      * If addressed mode is chosen, we require an initial peer address to be
1599      * set. If this is not set, we fail. If unaddressed mode is used, we do not
1600      * require this, as such an address is superfluous, though it can be set if
1601      * desired.
1602      */
1603     if (!qc->started && !qc->addressing_probe_done) {
1604         long rcaps = BIO_dgram_get_effective_caps(qc->net_rbio);
1605         long wcaps = BIO_dgram_get_effective_caps(qc->net_wbio);
1606
1607         qc->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0);
1608         qc->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0);
1609         qc->addressing_probe_done = 1;
1610     }
1611
1612     if (!qc->started && qc->addressed_mode_w
1613         && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1614         /*
1615          * We are trying to connect and are using addressed mode, which means we
1616          * need an initial peer address; if we do not have a peer address yet,
1617          * we should try to autodetect one.
1618          *
1619          * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
1620          * may not be able to provide us with a peer address until they have
1621          * finished their own processing. They may not be able to perform this
1622          * processing until an application has finished configuring that BIO
1623          * (e.g. with setter calls), which might happen after SSL_set_bio is
1624          * called.
1625          */
1626         if (!csm_analyse_init_peer_addr(qc->net_wbio, &qc->init_peer_addr))
1627             /* best effort */
1628             BIO_ADDR_clear(&qc->init_peer_addr);
1629         else
1630             ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
1631     }
1632
1633     if (!qc->started
1634         && qc->addressed_mode_w
1635         && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1636         /*
1637          * If we still don't have a peer address in addressed mode, we can't do
1638          * anything.
1639          */
1640         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1641         return -1; /* Non-protocol error */
1642     }
1643
1644     /*
1645      * Start connection process. Note we may come here multiple times in
1646      * non-blocking mode, which is fine.
1647      */
1648     if (!ensure_channel_started(ctx)) /* raises on failure */
1649         return -1; /* Non-protocol error */
1650
1651     if (ossl_quic_channel_is_handshake_complete(qc->ch))
1652         /* The handshake is now done. */
1653         return 1;
1654
1655     if (!qc_blocking_mode(qc)) {
1656         /* Try to advance the reactor. */
1657         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1658
1659         if (ossl_quic_channel_is_handshake_complete(qc->ch))
1660             /* The handshake is now done. */
1661             return 1;
1662
1663         if (ossl_quic_channel_is_term_any(qc->ch)) {
1664             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1665             return 0;
1666         } else if (qc->desires_blocking) {
1667             /*
1668              * As a special case when doing a handshake when blocking mode is
1669              * desired yet not available, see if the network BIOs have become
1670              * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1671              * which do late creation of socket FDs and therefore cannot expose
1672              * a poll descriptor until after a network BIO is set on the QCSO.
1673              */
1674             assert(!qc->blocking);
1675             qc_update_can_support_blocking(qc);
1676             qc_update_blocking_mode(qc);
1677         }
1678     }
1679
1680     /*
1681      * We are either in blocking mode or just entered it due to the code above.
1682      */
1683     if (qc_blocking_mode(qc)) {
1684         /* In blocking mode, wait for the handshake to complete. */
1685         struct quic_handshake_wait_args args;
1686
1687         args.qc     = qc;
1688
1689         ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1690         if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1691             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1692             return 0; /* Shutdown before completion */
1693         } else if (ret <= 0) {
1694             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1695             return -1; /* Non-protocol error */
1696         }
1697
1698         if (tls_wants_non_io_retry(qc)) {
1699             QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1700             return -1;
1701         }
1702
1703         assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1704         return 1;
1705     }
1706
1707     if (tls_wants_non_io_retry(qc)) {
1708         QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1709         return -1;
1710     }
1711
1712     /*
1713      * Otherwise, indicate that the handshake isn't done yet.
1714      * We can only get here in non-blocking mode.
1715      */
1716     QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1717     return -1; /* Non-protocol error */
1718 }
1719
1720 QUIC_TAKES_LOCK
1721 int ossl_quic_do_handshake(SSL *s)
1722 {
1723     int ret;
1724     QCTX ctx;
1725
1726     if (!expect_quic(s, &ctx))
1727         return 0;
1728
1729     quic_lock_for_io(&ctx);
1730
1731     ret = quic_do_handshake(&ctx);
1732     quic_unlock(ctx.qc);
1733     return ret;
1734 }
1735
1736 /* SSL_connect */
1737 int ossl_quic_connect(SSL *s)
1738 {
1739     /* Ensure we are in connect state (no-op if non-idle). */
1740     ossl_quic_set_connect_state(s);
1741
1742     /* Begin or continue the handshake */
1743     return ossl_quic_do_handshake(s);
1744 }
1745
1746 /* SSL_accept */
1747 int ossl_quic_accept(SSL *s)
1748 {
1749     /* Ensure we are in accept state (no-op if non-idle). */
1750     ossl_quic_set_accept_state(s);
1751
1752     /* Begin or continue the handshake */
1753     return ossl_quic_do_handshake(s);
1754 }
1755
1756 /*
1757  * QUIC Front-End I/O API: Stream Lifecycle Operations
1758  * ===================================================
1759  *
1760  *         SSL_stream_new       => ossl_quic_conn_stream_new
1761  *
1762  */
1763
1764 /*
1765  * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1766  * default XSO was created. Returns 0 if it was not (e.g. because it already
1767  * exists). Note that this is NOT an error condition.
1768  */
1769 QUIC_NEEDS_LOCK
1770 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1771 {
1772     uint64_t flags = 0;
1773     QUIC_CONNECTION *qc = ctx->qc;
1774
1775     if (qc->default_xso_created
1776         || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1777         /*
1778          * We only do this once. If the user detaches a previously created
1779          * default XSO we don't auto-create another one.
1780          */
1781         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1782
1783     /* Create a locally-initiated stream. */
1784     if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1785         flags |= SSL_STREAM_FLAG_UNI;
1786
1787     qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1788                                                             /*needs_lock=*/0),
1789                        /*touch=*/0);
1790     if (qc->default_xso == NULL)
1791         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1792
1793     qc_touch_default_xso(qc);
1794     return 1;
1795 }
1796
1797 struct quic_wait_for_stream_args {
1798     QUIC_CONNECTION *qc;
1799     QUIC_STREAM     *qs;
1800     QCTX            *ctx;
1801     uint64_t        expect_id;
1802 };
1803
1804 QUIC_NEEDS_LOCK
1805 static int quic_wait_for_stream(void *arg)
1806 {
1807     struct quic_wait_for_stream_args *args = arg;
1808
1809     if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
1810         /* If connection is torn down due to an error while blocking, stop. */
1811         QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1812         return -1;
1813     }
1814
1815     args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1816                                               args->expect_id | QUIC_STREAM_DIR_BIDI);
1817     if (args->qs == NULL)
1818         args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1819                                                   args->expect_id | QUIC_STREAM_DIR_UNI);
1820
1821     if (args->qs != NULL)
1822         return 1; /* stream now exists */
1823
1824     return 0; /* did not get a stream, keep trying */
1825 }
1826
1827 QUIC_NEEDS_LOCK
1828 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1829 {
1830     /* Called on a QCSO and we don't currently have a default stream. */
1831     uint64_t expect_id;
1832     QUIC_CONNECTION *qc = ctx->qc;
1833     QUIC_STREAM *qs;
1834     int res;
1835     struct quic_wait_for_stream_args wargs;
1836
1837     /*
1838      * If default stream functionality is disabled or we already detached
1839      * one, don't make another default stream and just fail.
1840      */
1841     if (qc->default_xso_created
1842         || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1843         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1844
1845     /*
1846      * The peer may have opened a stream since we last ticked. So tick and
1847      * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1848      * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1849      * first stream created by a peer must have an ordinal of 0.
1850      */
1851     expect_id = qc->as_server
1852         ? QUIC_STREAM_INITIATOR_CLIENT
1853         : QUIC_STREAM_INITIATOR_SERVER;
1854
1855     qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1856                                         expect_id | QUIC_STREAM_DIR_BIDI);
1857     if (qs == NULL)
1858         qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1859                                             expect_id | QUIC_STREAM_DIR_UNI);
1860
1861     if (qs == NULL) {
1862         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1863
1864         qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1865                                             expect_id);
1866     }
1867
1868     if (qs == NULL) {
1869         if (!qc_blocking_mode(qc))
1870             /* Non-blocking mode, so just bail immediately. */
1871             return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1872
1873         /* Block until we have a stream. */
1874         wargs.qc        = qc;
1875         wargs.qs        = NULL;
1876         wargs.ctx       = ctx;
1877         wargs.expect_id = expect_id;
1878
1879         res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1880         if (res == 0)
1881             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1882         else if (res < 0 || wargs.qs == NULL)
1883             /* quic_wait_for_stream raised error here */
1884             return 0;
1885
1886         qs = wargs.qs;
1887     }
1888
1889     /*
1890      * We now have qs != NULL. Make it the default stream, creating the
1891      * necessary XSO.
1892      */
1893     qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1894     if (qc->default_xso == NULL)
1895         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1896
1897     qc_touch_default_xso(qc); /* inhibits default XSO */
1898     return 1;
1899 }
1900
1901 QUIC_NEEDS_LOCK
1902 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1903 {
1904     QUIC_XSO *xso = NULL;
1905
1906     if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
1907         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
1908         goto err;
1909     }
1910
1911     if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) {
1912         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1913         goto err;
1914     }
1915
1916     /* XSO refs QC */
1917     if (!SSL_up_ref(&qc->ssl)) {
1918         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);
1919         goto err;
1920     }
1921
1922     xso->conn       = qc;
1923     xso->ssl_mode   = qc->default_ssl_mode;
1924     xso->ssl_options
1925         = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1926     xso->last_error = SSL_ERROR_NONE;
1927
1928     xso->stream     = qs;
1929
1930     ++qc->num_xso;
1931     xso_update_options(xso);
1932     return xso;
1933
1934 err:
1935     OPENSSL_free(xso);
1936     return NULL;
1937 }
1938
1939 struct quic_new_stream_wait_args {
1940     QUIC_CONNECTION *qc;
1941     int is_uni;
1942 };
1943
1944 static int quic_new_stream_wait(void *arg)
1945 {
1946     struct quic_new_stream_wait_args *args = arg;
1947     QUIC_CONNECTION *qc = args->qc;
1948
1949     if (!quic_mutation_allowed(qc, /*req_active=*/1))
1950         return -1;
1951
1952     if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
1953         return 1;
1954
1955     return 0;
1956 }
1957
1958 /* locking depends on need_lock */
1959 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1960 {
1961     int ret;
1962     QUIC_CONNECTION *qc = ctx->qc;
1963     QUIC_XSO *xso = NULL;
1964     QUIC_STREAM *qs = NULL;
1965     int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1966     int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
1967     int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
1968
1969     if (need_lock)
1970         quic_lock(qc);
1971
1972     if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
1973         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1974         goto err;
1975     }
1976
1977     if (!advance
1978         && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
1979         struct quic_new_stream_wait_args args;
1980
1981         /*
1982          * Stream count flow control currently doesn't permit this stream to be
1983          * opened.
1984          */
1985         if (no_blocking || !qc_blocking_mode(qc)) {
1986             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
1987             goto err;
1988         }
1989
1990         args.qc     = qc;
1991         args.is_uni = is_uni;
1992
1993         /* Blocking mode - wait until we can get a stream. */
1994         ret = block_until_pred(ctx->qc, quic_new_stream_wait, &args, 0);
1995         if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1996             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1997             goto err; /* Shutdown before completion */
1998         } else if (ret <= 0) {
1999             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2000             goto err; /* Non-protocol error */
2001         }
2002     }
2003
2004     qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
2005     if (qs == NULL) {
2006         QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2007         goto err;
2008     }
2009
2010     xso = create_xso_from_stream(qc, qs);
2011     if (xso == NULL)
2012         goto err;
2013
2014     qc_touch_default_xso(qc); /* inhibits default XSO */
2015     if (need_lock)
2016         quic_unlock(qc);
2017
2018     return &xso->ssl;
2019
2020 err:
2021     OPENSSL_free(xso);
2022     ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
2023     if (need_lock)
2024         quic_unlock(qc);
2025
2026     return NULL;
2027
2028 }
2029
2030 QUIC_TAKES_LOCK
2031 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
2032 {
2033     QCTX ctx;
2034
2035     if (!expect_quic_conn_only(s, &ctx))
2036         return NULL;
2037
2038     return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
2039 }
2040
2041 /*
2042  * QUIC Front-End I/O API: Steady-State Operations
2043  * ===============================================
2044  *
2045  * Here we dispatch calls to the steady-state front-end I/O API functions; that
2046  * is, the functions used during the established phase of a QUIC connection
2047  * (e.g. SSL_read, SSL_write).
2048  *
2049  * Each function must handle both blocking and non-blocking modes. As discussed
2050  * above, all QUIC I/O is implemented using non-blocking mode internally.
2051  *
2052  *         SSL_get_error        => partially implemented by ossl_quic_get_error
2053  *         SSL_want             => ossl_quic_want
2054  *   (BIO/)SSL_read             => ossl_quic_read
2055  *   (BIO/)SSL_write            => ossl_quic_write
2056  *         SSL_pending          => ossl_quic_pending
2057  *         SSL_stream_conclude  => ossl_quic_conn_stream_conclude
2058  *         SSL_key_update       => ossl_quic_key_update
2059  */
2060
2061 /* SSL_get_error */
2062 int ossl_quic_get_error(const SSL *s, int i)
2063 {
2064     QCTX ctx;
2065     int net_error, last_error;
2066
2067     if (!expect_quic(s, &ctx))
2068         return 0;
2069
2070     quic_lock(ctx.qc);
2071     net_error = ossl_quic_channel_net_error(ctx.qc->ch);
2072     last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
2073     quic_unlock(ctx.qc);
2074
2075     if (net_error)
2076         return SSL_ERROR_SYSCALL;
2077
2078     return last_error;
2079 }
2080
2081 /* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
2082 static int error_to_want(int error)
2083 {
2084     switch (error) {
2085     case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */
2086     case SSL_ERROR_WANT_ACCEPT:  /* never used - UDP is connectionless */
2087     case SSL_ERROR_ZERO_RETURN:
2088     default:
2089         return SSL_NOTHING;
2090
2091     case SSL_ERROR_WANT_READ:
2092         return SSL_READING;
2093
2094     case SSL_ERROR_WANT_WRITE:
2095         return SSL_WRITING;
2096
2097     case SSL_ERROR_WANT_RETRY_VERIFY:
2098         return SSL_RETRY_VERIFY;
2099
2100     case SSL_ERROR_WANT_CLIENT_HELLO_CB:
2101         return SSL_CLIENT_HELLO_CB;
2102
2103     case SSL_ERROR_WANT_X509_LOOKUP:
2104         return SSL_X509_LOOKUP;
2105     }
2106 }
2107
2108 /* SSL_want */
2109 int ossl_quic_want(const SSL *s)
2110 {
2111     QCTX ctx;
2112     int w;
2113
2114     if (!expect_quic(s, &ctx))
2115         return SSL_NOTHING;
2116
2117     quic_lock(ctx.qc);
2118
2119     w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);
2120
2121     quic_unlock(ctx.qc);
2122     return w;
2123 }
2124
2125 /*
2126  * SSL_write
2127  * ---------
2128  *
2129  * The set of functions below provide the implementation of the public SSL_write
2130  * function. We must handle:
2131  *
2132  *   - both blocking and non-blocking operation at the application level,
2133  *     depending on how we are configured;
2134  *
2135  *   - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
2136  *
2137  *   - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
2138  *
2139  */
2140 QUIC_NEEDS_LOCK
2141 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
2142 {
2143     /*
2144      * We have appended at least one byte to the stream.
2145      * Potentially mark stream as active, depending on FC.
2146      */
2147     if (did_append)
2148         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
2149                                           xso->stream);
2150
2151     /*
2152      * Try and send.
2153      *
2154      * TODO(QUIC FUTURE): It is probably inefficient to try and do this
2155      * immediately, plus we should eventually consider Nagle's algorithm.
2156      */
2157     if (do_tick)
2158         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
2159 }
2160
2161 struct quic_write_again_args {
2162     QUIC_XSO            *xso;
2163     const unsigned char *buf;
2164     size_t              len;
2165     size_t              total_written;
2166     int                 err;
2167 };
2168
2169 QUIC_NEEDS_LOCK
2170 static int quic_write_again(void *arg)
2171 {
2172     struct quic_write_again_args *args = arg;
2173     size_t actual_written = 0;
2174
2175     if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
2176         /* If connection is torn down due to an error while blocking, stop. */
2177         return -2;
2178
2179     if (!quic_validate_for_write(args->xso, &args->err))
2180         /*
2181          * Stream may have become invalid for write due to connection events
2182          * while we blocked.
2183          */
2184         return -2;
2185
2186     args->err = ERR_R_INTERNAL_ERROR;
2187     if (!ossl_quic_sstream_append(args->xso->stream->sstream,
2188                                   args->buf, args->len, &actual_written))
2189         return -2;
2190
2191     quic_post_write(args->xso, actual_written > 0, 0);
2192
2193     args->buf           += actual_written;
2194     args->len           -= actual_written;
2195     args->total_written += actual_written;
2196
2197     if (args->len == 0)
2198         /* Written everything, done. */
2199         return 1;
2200
2201     /* Not written everything yet, keep trying. */
2202     return 0;
2203 }
2204
2205 QUIC_NEEDS_LOCK
2206 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
2207                                size_t *written)
2208 {
2209     int res;
2210     QUIC_XSO *xso = ctx->xso;
2211     struct quic_write_again_args args;
2212     size_t actual_written = 0;
2213
2214     /* First make a best effort to append as much of the data as possible. */
2215     if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
2216                                   &actual_written)) {
2217         /* Stream already finished or allocation error. */
2218         *written = 0;
2219         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2220     }
2221
2222     quic_post_write(xso, actual_written > 0, 1);
2223
2224     if (actual_written == len) {
2225         /* Managed to append everything on the first try. */
2226         *written = actual_written;
2227         return 1;
2228     }
2229
2230     /*
2231      * We did not manage to append all of the data immediately, so the stream
2232      * buffer has probably filled up. This means we need to block until some of
2233      * it is freed up.
2234      */
2235     args.xso            = xso;
2236     args.buf            = (const unsigned char *)buf + actual_written;
2237     args.len            = len - actual_written;
2238     args.total_written  = 0;
2239     args.err            = ERR_R_INTERNAL_ERROR;
2240
2241     res = block_until_pred(xso->conn, quic_write_again, &args, 0);
2242     if (res <= 0) {
2243         if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
2244             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2245         else
2246             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
2247     }
2248
2249     *written = args.total_written;
2250     return 1;
2251 }
2252
2253 /*
2254  * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2255  * write semantics.
2256  */
2257 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
2258                             size_t buf_len, size_t already_sent)
2259 {
2260     assert(!xso->aon_write_in_progress);
2261
2262     xso->aon_write_in_progress = 1;
2263     xso->aon_buf_base          = buf;
2264     xso->aon_buf_pos           = already_sent;
2265     xso->aon_buf_len           = buf_len;
2266 }
2267
2268 static void aon_write_finish(QUIC_XSO *xso)
2269 {
2270     xso->aon_write_in_progress   = 0;
2271     xso->aon_buf_base            = NULL;
2272     xso->aon_buf_pos             = 0;
2273     xso->aon_buf_len             = 0;
2274 }
2275
2276 QUIC_NEEDS_LOCK
2277 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2278                                       size_t len, size_t *written)
2279 {
2280     QUIC_XSO *xso = ctx->xso;
2281     const void *actual_buf;
2282     size_t actual_len, actual_written = 0;
2283     int accept_moving_buffer
2284         = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2285
2286     if (xso->aon_write_in_progress) {
2287         /*
2288          * We are in the middle of an AON write (i.e., a previous write did not
2289          * manage to append all data to the SSTREAM and we have Enable Partial
2290          * Write (EPW) mode disabled.)
2291          */
2292         if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2293             || len != xso->aon_buf_len)
2294             /*
2295              * Pointer must not have changed if we are not in accept moving
2296              * buffer mode. Length must never change.
2297              */
2298             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2299
2300         actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2301         actual_len = len - xso->aon_buf_pos;
2302         assert(actual_len > 0);
2303     } else {
2304         actual_buf = buf;
2305         actual_len = len;
2306     }
2307
2308     /* First make a best effort to append as much of the data as possible. */
2309     if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
2310                                   &actual_written)) {
2311         /* Stream already finished or allocation error. */
2312         *written = 0;
2313         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2314     }
2315
2316     quic_post_write(xso, actual_written > 0, 1);
2317
2318     if (actual_written == actual_len) {
2319         /* We have sent everything. */
2320         if (xso->aon_write_in_progress) {
2321             /*
2322              * We have sent everything, and we were in the middle of an AON
2323              * write. The output write length is the total length of the AON
2324              * buffer, not however many bytes we managed to write to the stream
2325              * in this call.
2326              */
2327             *written = xso->aon_buf_len;
2328             aon_write_finish(xso);
2329         } else {
2330             *written = actual_written;
2331         }
2332
2333         return 1;
2334     }
2335
2336     if (xso->aon_write_in_progress) {
2337         /*
2338          * AON write is in progress but we have not written everything yet. We
2339          * may have managed to send zero bytes, or some number of bytes less
2340          * than the total remaining which need to be appended during this
2341          * AON operation.
2342          */
2343         xso->aon_buf_pos += actual_written;
2344         assert(xso->aon_buf_pos < xso->aon_buf_len);
2345         return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2346     }
2347
2348     /*
2349      * Not in an existing AON operation but partial write is not enabled, so we
2350      * need to begin a new AON operation. However we needn't bother if we didn't
2351      * actually append anything.
2352      */
2353     if (actual_written > 0)
2354         aon_write_begin(xso, buf, len, actual_written);
2355
2356     /*
2357      * AON - We do not publicly admit to having appended anything until AON
2358      * completes.
2359      */
2360     *written = 0;
2361     return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2362 }
2363
2364 QUIC_NEEDS_LOCK
2365 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2366                                       size_t *written)
2367 {
2368     QUIC_XSO *xso = ctx->xso;
2369
2370     /* Simple best effort operation. */
2371     if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
2372         /* Stream already finished or allocation error. */
2373         *written = 0;
2374         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2375     }
2376
2377     quic_post_write(xso, *written > 0, 1);
2378     return 1;
2379 }
2380
2381 QUIC_NEEDS_LOCK
2382 static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2383 {
2384     QUIC_STREAM_MAP *qsm;
2385
2386     if (xso == NULL || xso->stream == NULL) {
2387         *err = ERR_R_INTERNAL_ERROR;
2388         return 0;
2389     }
2390
2391     switch (xso->stream->send_state) {
2392     default:
2393     case QUIC_SSTREAM_STATE_NONE:
2394         *err = SSL_R_STREAM_RECV_ONLY;
2395         return 0;
2396
2397     case QUIC_SSTREAM_STATE_READY:
2398         qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2399
2400         if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2401             *err = ERR_R_INTERNAL_ERROR;
2402             return 0;
2403         }
2404
2405         /* FALLTHROUGH */
2406     case QUIC_SSTREAM_STATE_SEND:
2407     case QUIC_SSTREAM_STATE_DATA_SENT:
2408     case QUIC_SSTREAM_STATE_DATA_RECVD:
2409         if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2410             *err = SSL_R_STREAM_FINISHED;
2411             return 0;
2412         }
2413
2414         return 1;
2415
2416     case QUIC_SSTREAM_STATE_RESET_SENT:
2417     case QUIC_SSTREAM_STATE_RESET_RECVD:
2418         *err = SSL_R_STREAM_RESET;
2419         return 0;
2420     }
2421 }
2422
2423 QUIC_TAKES_LOCK
2424 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2425 {
2426     int ret;
2427     QCTX ctx;
2428     int partial_write, err;
2429
2430     *written = 0;
2431
2432     if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))
2433         return 0;
2434
2435     partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
2436
2437     if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2438         ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2439         goto out;
2440     }
2441
2442     /*
2443      * If we haven't finished the handshake, try to advance it.
2444      * We don't accept writes until the handshake is completed.
2445      */
2446     if (quic_do_handshake(&ctx) < 1) {
2447         ret = 0;
2448         goto out;
2449     }
2450
2451     /* Ensure correct stream state, stream send part not concluded, etc. */
2452     if (!quic_validate_for_write(ctx.xso, &err)) {
2453         ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2454         goto out;
2455     }
2456
2457     if (len == 0) {
2458         ret = 1;
2459         goto out;
2460     }
2461
2462     if (xso_blocking_mode(ctx.xso))
2463         ret = quic_write_blocking(&ctx, buf, len, written);
2464     else if (partial_write)
2465         ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
2466     else
2467         ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
2468
2469 out:
2470     quic_unlock(ctx.qc);
2471     return ret;
2472 }
2473
2474 /*
2475  * SSL_read
2476  * --------
2477  */
2478 struct quic_read_again_args {
2479     QCTX            *ctx;
2480     QUIC_STREAM     *stream;
2481     void            *buf;
2482     size_t          len;
2483     size_t          *bytes_read;
2484     int             peek;
2485 };
2486
2487 QUIC_NEEDS_LOCK
2488 static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2489 {
2490     QUIC_STREAM_MAP *qsm;
2491
2492     *eos = 0;
2493
2494     if (xso == NULL || xso->stream == NULL) {
2495         *err = ERR_R_INTERNAL_ERROR;
2496         return 0;
2497     }
2498
2499     switch (xso->stream->recv_state) {
2500     default:
2501     case QUIC_RSTREAM_STATE_NONE:
2502         *err = SSL_R_STREAM_SEND_ONLY;
2503         return 0;
2504
2505     case QUIC_RSTREAM_STATE_RECV:
2506     case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2507     case QUIC_RSTREAM_STATE_DATA_RECVD:
2508         return 1;
2509
2510     case QUIC_RSTREAM_STATE_DATA_READ:
2511         *eos = 1;
2512         return 0;
2513
2514     case QUIC_RSTREAM_STATE_RESET_RECVD:
2515         qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2516         ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2517
2518         /* FALLTHROUGH */
2519     case QUIC_RSTREAM_STATE_RESET_READ:
2520         *err = SSL_R_STREAM_RESET;
2521         return 0;
2522     }
2523 }
2524
2525 QUIC_NEEDS_LOCK
2526 static int quic_read_actual(QCTX *ctx,
2527                             QUIC_STREAM *stream,
2528                             void *buf, size_t buf_len,
2529                             size_t *bytes_read,
2530                             int peek)
2531 {
2532     int is_fin = 0, err, eos;
2533     QUIC_CONNECTION *qc = ctx->qc;
2534
2535     if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2536         if (eos)
2537             return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2538         else
2539             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2540     }
2541
2542     if (peek) {
2543         if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2544                                     bytes_read, &is_fin))
2545             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2546
2547     } else {
2548         if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2549                                     bytes_read, &is_fin))
2550             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2551     }
2552
2553     if (!peek) {
2554         if (*bytes_read > 0) {
2555             /*
2556              * We have read at least one byte from the stream. Inform stream-level
2557              * RXFC of the retirement of controlled bytes. Update the active stream
2558              * status (the RXFC may now want to emit a frame granting more credit to
2559              * the peer).
2560              */
2561             OSSL_RTT_INFO rtt_info;
2562
2563             ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2564
2565             if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2566                                           rtt_info.smoothed_rtt))
2567                 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2568         }
2569
2570         if (is_fin && !peek) {
2571             QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2572
2573             ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2574         }
2575
2576         if (*bytes_read > 0)
2577             ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2578                                               stream);
2579     }
2580
2581     if (*bytes_read == 0 && is_fin)
2582         return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2583
2584     return 1;
2585 }
2586
2587 QUIC_NEEDS_LOCK
2588 static int quic_read_again(void *arg)
2589 {
2590     struct quic_read_again_args *args = arg;
2591
2592     if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
2593         /* If connection is torn down due to an error while blocking, stop. */
2594         QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2595         return -1;
2596     }
2597
2598     if (!quic_read_actual(args->ctx, args->stream,
2599                           args->buf, args->len, args->bytes_read,
2600                           args->peek))
2601         return -1;
2602
2603     if (*args->bytes_read > 0)
2604         /* got at least one byte, the SSL_read op can finish now */
2605         return 1;
2606
2607     return 0; /* did not read anything, keep trying */
2608 }
2609
2610 QUIC_TAKES_LOCK
2611 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2612 {
2613     int ret, res;
2614     QCTX ctx;
2615     struct quic_read_again_args args;
2616
2617     *bytes_read = 0;
2618
2619     if (!expect_quic(s, &ctx))
2620         return 0;
2621
2622     quic_lock_for_io(&ctx);
2623
2624     if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2625         ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2626         goto out;
2627     }
2628
2629     /* If we haven't finished the handshake, try to advance it. */
2630     if (quic_do_handshake(&ctx) < 1) {
2631         ret = 0; /* ossl_quic_do_handshake raised error here */
2632         goto out;
2633     }
2634
2635     if (ctx.xso == NULL) {
2636         /*
2637          * Called on a QCSO and we don't currently have a default stream.
2638          *
2639          * Wait until we get a stream initiated by the peer (blocking mode) or
2640          * fail if we don't have one yet (non-blocking mode).
2641          */
2642         if (!qc_wait_for_default_xso_for_read(&ctx)) {
2643             ret = 0; /* error already raised here */
2644             goto out;
2645         }
2646
2647         ctx.xso = ctx.qc->default_xso;
2648     }
2649
2650     if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2651         ret = 0; /* quic_read_actual raised error here */
2652         goto out;
2653     }
2654
2655     if (*bytes_read > 0) {
2656         /*
2657          * Even though we succeeded, tick the reactor here to ensure we are
2658          * handling other aspects of the QUIC connection.
2659          */
2660         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2661         ret = 1;
2662     } else if (xso_blocking_mode(ctx.xso)) {
2663         /*
2664          * We were not able to read anything immediately, so our stream
2665          * buffer is empty. This means we need to block until we get
2666          * at least one byte.
2667          */
2668         args.ctx        = &ctx;
2669         args.stream     = ctx.xso->stream;
2670         args.buf        = buf;
2671         args.len        = len;
2672         args.bytes_read = bytes_read;
2673         args.peek       = peek;
2674
2675         res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2676         if (res == 0) {
2677             ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2678             goto out;
2679         } else if (res < 0) {
2680             ret = 0; /* quic_read_again raised error here */
2681             goto out;
2682         }
2683
2684         ret = 1;
2685     } else {
2686         /*
2687          * We did not get any bytes and are not in blocking mode.
2688          * Tick to see if this delivers any more.
2689          */
2690         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2691
2692         /* Try the read again. */
2693         if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2694             ret = 0; /* quic_read_actual raised error here */
2695             goto out;
2696         }
2697
2698         if (*bytes_read > 0)
2699             ret = 1; /* Succeeded this time. */
2700         else
2701             ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2702     }
2703
2704 out:
2705     quic_unlock(ctx.qc);
2706     return ret;
2707 }
2708
2709 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2710 {
2711     return quic_read(s, buf, len, bytes_read, 0);
2712 }
2713
2714 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2715 {
2716     return quic_read(s, buf, len, bytes_read, 1);
2717 }
2718
2719 /*
2720  * SSL_pending
2721  * -----------
2722  */
2723
2724 QUIC_TAKES_LOCK
2725 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2726 {
2727     QCTX ctx;
2728     size_t avail = 0;
2729     int fin = 0;
2730
2731
2732     if (!expect_quic(s, &ctx))
2733         return 0;
2734
2735     quic_lock(ctx.qc);
2736
2737     if (ctx.xso == NULL) {
2738         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
2739         goto out;
2740     }
2741
2742     if (ctx.xso->stream == NULL
2743         || !ossl_quic_stream_has_recv_buffer(ctx.xso->stream)) {
2744         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2745         goto out;
2746     }
2747
2748     if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2749         avail = 0;
2750
2751     if (avail == 0 && check_channel && ossl_quic_channel_has_pending(ctx.qc->ch))
2752         avail = 1;
2753
2754 out:
2755     quic_unlock(ctx.qc);
2756     return avail;
2757 }
2758
2759 size_t ossl_quic_pending(const SSL *s)
2760 {
2761     return ossl_quic_pending_int(s, /*check_channel=*/0);
2762 }
2763
2764 int ossl_quic_has_pending(const SSL *s)
2765 {
2766     /* Do we have app-side pending data or pending URXEs or RXEs? */
2767     return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2768 }
2769
2770 /*
2771  * SSL_stream_conclude
2772  * -------------------
2773  */
2774 QUIC_TAKES_LOCK
2775 int ossl_quic_conn_stream_conclude(SSL *s)
2776 {
2777     QCTX ctx;
2778     QUIC_STREAM *qs;
2779     int err;
2780
2781     if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))
2782         return 0;
2783
2784     qs = ctx.xso->stream;
2785
2786     if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
2787         quic_unlock(ctx.qc);
2788         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2789     }
2790
2791     if (!quic_validate_for_write(ctx.xso, &err)) {
2792         quic_unlock(ctx.qc);
2793         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2794     }
2795
2796     if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2797         quic_unlock(ctx.qc);
2798         return 1;
2799     }
2800
2801     ossl_quic_sstream_fin(qs->sstream);
2802     quic_post_write(ctx.xso, 1, 1);
2803     quic_unlock(ctx.qc);
2804     return 1;
2805 }
2806
2807 /*
2808  * SSL_inject_net_dgram
2809  * --------------------
2810  */
2811 QUIC_TAKES_LOCK
2812 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2813                          size_t buf_len,
2814                          const BIO_ADDR *peer,
2815                          const BIO_ADDR *local)
2816 {
2817     int ret;
2818     QCTX ctx;
2819     QUIC_DEMUX *demux;
2820
2821     if (!expect_quic(s, &ctx))
2822         return 0;
2823
2824     quic_lock(ctx.qc);
2825
2826     demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2827     ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2828
2829     quic_unlock(ctx.qc);
2830     return ret;
2831 }
2832
2833 /*
2834  * SSL_get0_connection
2835  * -------------------
2836  */
2837 SSL *ossl_quic_get0_connection(SSL *s)
2838 {
2839     QCTX ctx;
2840
2841     if (!expect_quic(s, &ctx))
2842         return NULL;
2843
2844     return &ctx.qc->ssl;
2845 }
2846
2847 /*
2848  * SSL_get_stream_type
2849  * -------------------
2850  */
2851 int ossl_quic_get_stream_type(SSL *s)
2852 {
2853     QCTX ctx;
2854
2855     if (!expect_quic(s, &ctx))
2856         return SSL_STREAM_TYPE_BIDI;
2857
2858     if (ctx.xso == NULL) {
2859         /*
2860          * If deferred XSO creation has yet to occur, proceed according to the
2861          * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2862          * what kind of stream will be created yet, so return BIDI on the basis
2863          * that at this time, the client still has the option of calling
2864          * SSL_read() or SSL_write() first.
2865          */
2866         if (ctx.qc->default_xso_created
2867             || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2868             return SSL_STREAM_TYPE_NONE;
2869         else
2870             return SSL_STREAM_TYPE_BIDI;
2871     }
2872
2873     if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2874         return SSL_STREAM_TYPE_BIDI;
2875
2876     if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2877         return SSL_STREAM_TYPE_READ;
2878     else
2879         return SSL_STREAM_TYPE_WRITE;
2880 }
2881
2882 /*
2883  * SSL_get_stream_id
2884  * -----------------
2885  */
2886 QUIC_TAKES_LOCK
2887 uint64_t ossl_quic_get_stream_id(SSL *s)
2888 {
2889     QCTX ctx;
2890     uint64_t id;
2891
2892     if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
2893         return UINT64_MAX;
2894
2895     id = ctx.xso->stream->id;
2896     quic_unlock(ctx.qc);
2897
2898     return id;
2899 }
2900
2901 /*
2902  * SSL_is_stream_local
2903  * -------------------
2904  */
2905 QUIC_TAKES_LOCK
2906 int ossl_quic_is_stream_local(SSL *s)
2907 {
2908     QCTX ctx;
2909     int is_local;
2910
2911     if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
2912         return -1;
2913
2914     is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);
2915     quic_unlock(ctx.qc);
2916
2917     return is_local;
2918 }
2919
2920 /*
2921  * SSL_set_default_stream_mode
2922  * ---------------------------
2923  */
2924 QUIC_TAKES_LOCK
2925 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2926 {
2927     QCTX ctx;
2928
2929     if (!expect_quic_conn_only(s, &ctx))
2930         return 0;
2931
2932     quic_lock(ctx.qc);
2933
2934     if (ctx.qc->default_xso_created) {
2935         quic_unlock(ctx.qc);
2936         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2937                                        "too late to change default stream mode");
2938     }
2939
2940     switch (mode) {
2941     case SSL_DEFAULT_STREAM_MODE_NONE:
2942     case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2943     case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2944         ctx.qc->default_stream_mode = mode;
2945         break;
2946     default:
2947         quic_unlock(ctx.qc);
2948         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2949                                        "bad default stream type");
2950     }
2951
2952     quic_unlock(ctx.qc);
2953     return 1;
2954 }
2955
2956 /*
2957  * SSL_detach_stream
2958  * -----------------
2959  */
2960 QUIC_TAKES_LOCK
2961 SSL *ossl_quic_detach_stream(SSL *s)
2962 {
2963     QCTX ctx;
2964     QUIC_XSO *xso = NULL;
2965
2966     if (!expect_quic_conn_only(s, &ctx))
2967         return NULL;
2968
2969     quic_lock(ctx.qc);
2970
2971     /* Calling this function inhibits default XSO autocreation. */
2972     /* QC ref to any default XSO is transferred to us and to caller. */
2973     qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2974
2975     quic_unlock(ctx.qc);
2976
2977     return xso != NULL ? &xso->ssl : NULL;
2978 }
2979
2980 /*
2981  * SSL_attach_stream
2982  * -----------------
2983  */
2984 QUIC_TAKES_LOCK
2985 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2986 {
2987     QCTX ctx;
2988     QUIC_XSO *xso;
2989     int nref;
2990
2991     if (!expect_quic_conn_only(conn, &ctx))
2992         return 0;
2993
2994     if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2995         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2996                                        "stream to attach must be a valid QUIC stream");
2997
2998     xso = (QUIC_XSO *)stream;
2999
3000     quic_lock(ctx.qc);
3001
3002     if (ctx.qc->default_xso != NULL) {
3003         quic_unlock(ctx.qc);
3004         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3005                                        "connection already has a default stream");
3006     }
3007
3008     /*
3009      * It is a caller error for the XSO being attached as a default XSO to have
3010      * more than one ref.
3011      */
3012     if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
3013         quic_unlock(ctx.qc);
3014         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
3015                                        "ref");
3016     }
3017
3018     if (nref != 1) {
3019         quic_unlock(ctx.qc);
3020         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3021                                        "stream being attached must have "
3022                                        "only 1 reference");
3023     }
3024
3025     /* Caller's reference to the XSO is transferred to us. */
3026     /* Calling this function inhibits default XSO autocreation. */
3027     qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
3028
3029     quic_unlock(ctx.qc);
3030     return 1;
3031 }
3032
3033 /*
3034  * SSL_set_incoming_stream_policy
3035  * ------------------------------
3036  */
3037 QUIC_NEEDS_LOCK
3038 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
3039 {
3040     switch (qc->incoming_stream_policy) {
3041         case SSL_INCOMING_STREAM_POLICY_AUTO:
3042             if ((qc->default_xso == NULL && !qc->default_xso_created)
3043                 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3044                 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
3045             else
3046                 return SSL_INCOMING_STREAM_POLICY_REJECT;
3047
3048         default:
3049             return qc->incoming_stream_policy;
3050     }
3051 }
3052
3053 QUIC_NEEDS_LOCK
3054 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
3055 {
3056     int policy = qc_get_effective_incoming_stream_policy(qc);
3057     int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
3058
3059     ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
3060                                                       enable_reject,
3061                                                       qc->incoming_stream_aec);
3062 }
3063
3064 QUIC_TAKES_LOCK
3065 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
3066                                          uint64_t aec)
3067 {
3068     int ret = 1;
3069     QCTX ctx;
3070
3071     if (!expect_quic_conn_only(s, &ctx))
3072         return 0;
3073
3074     quic_lock(ctx.qc);
3075
3076     switch (policy) {
3077     case SSL_INCOMING_STREAM_POLICY_AUTO:
3078     case SSL_INCOMING_STREAM_POLICY_ACCEPT:
3079     case SSL_INCOMING_STREAM_POLICY_REJECT:
3080         ctx.qc->incoming_stream_policy = policy;
3081         ctx.qc->incoming_stream_aec    = aec;
3082         break;
3083
3084     default:
3085         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3086         ret = 0;
3087         break;
3088     }
3089
3090     qc_update_reject_policy(ctx.qc);
3091     quic_unlock(ctx.qc);
3092     return ret;
3093 }
3094
3095 /*
3096  * SSL_accept_stream
3097  * -----------------
3098  */
3099 struct wait_for_incoming_stream_args {
3100     QCTX            *ctx;
3101     QUIC_STREAM     *qs;
3102 };
3103
3104 QUIC_NEEDS_LOCK
3105 static int wait_for_incoming_stream(void *arg)
3106 {
3107     struct wait_for_incoming_stream_args *args = arg;
3108     QUIC_CONNECTION *qc = args->ctx->qc;
3109     QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
3110
3111     if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
3112         /* If connection is torn down due to an error while blocking, stop. */
3113         QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3114         return -1;
3115     }
3116
3117     args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3118     if (args->qs != NULL)
3119         return 1; /* got a stream */
3120
3121     return 0; /* did not get a stream, keep trying */
3122 }
3123
3124 QUIC_TAKES_LOCK
3125 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
3126 {
3127     QCTX ctx;
3128     int ret;
3129     SSL *new_s = NULL;
3130     QUIC_STREAM_MAP *qsm;
3131     QUIC_STREAM *qs;
3132     QUIC_XSO *xso;
3133     OSSL_RTT_INFO rtt_info;
3134
3135     if (!expect_quic_conn_only(s, &ctx))
3136         return NULL;
3137
3138     quic_lock(ctx.qc);
3139
3140     if (qc_get_effective_incoming_stream_policy(ctx.qc)
3141         == SSL_INCOMING_STREAM_POLICY_REJECT) {
3142         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3143         goto out;
3144     }
3145
3146     qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
3147
3148     qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3149     if (qs == NULL) {
3150         if (qc_blocking_mode(ctx.qc)
3151             && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
3152             struct wait_for_incoming_stream_args args;
3153
3154             args.ctx = &ctx;
3155             args.qs = NULL;
3156
3157             ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
3158             if (ret == 0) {
3159                 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3160                 goto out;
3161             } else if (ret < 0 || args.qs == NULL) {
3162                 goto out;
3163             }
3164
3165             qs = args.qs;
3166         } else {
3167             goto out;
3168         }
3169     }
3170
3171     xso = create_xso_from_stream(ctx.qc, qs);
3172     if (xso == NULL)
3173         goto out;
3174
3175     ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
3176     ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
3177                                                   rtt_info.smoothed_rtt);
3178     new_s = &xso->ssl;
3179
3180     /* Calling this function inhibits default XSO autocreation. */
3181     qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
3182
3183 out:
3184     quic_unlock(ctx.qc);
3185     return new_s;
3186 }
3187
3188 /*
3189  * SSL_get_accept_stream_queue_len
3190  * -------------------------------
3191  */
3192 QUIC_TAKES_LOCK
3193 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
3194 {
3195     QCTX ctx;
3196     size_t v;
3197
3198     if (!expect_quic_conn_only(s, &ctx))
3199         return 0;
3200
3201     quic_lock(ctx.qc);
3202
3203     v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
3204
3205     quic_unlock(ctx.qc);
3206     return v;
3207 }
3208
3209 /*
3210  * SSL_stream_reset
3211  * ----------------
3212  */
3213 int ossl_quic_stream_reset(SSL *ssl,
3214                            const SSL_STREAM_RESET_ARGS *args,
3215                            size_t args_len)
3216 {
3217     QCTX ctx;
3218     QUIC_STREAM_MAP *qsm;
3219     QUIC_STREAM *qs;
3220     uint64_t error_code;
3221     int ok, err;
3222
3223     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))
3224         return 0;
3225
3226     qsm         = ossl_quic_channel_get_qsm(ctx.qc->ch);
3227     qs          = ctx.xso->stream;
3228     error_code  = (args != NULL ? args->quic_error_code : 0);
3229
3230     if (!quic_validate_for_write(ctx.xso, &err)) {
3231         ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
3232         goto err;
3233     }
3234
3235     ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
3236
3237 err:
3238     quic_unlock(ctx.qc);
3239     return ok;
3240 }
3241
3242 /*
3243  * SSL_get_stream_read_state
3244  * -------------------------
3245  */
3246 static void quic_classify_stream(QUIC_CONNECTION *qc,
3247                                  QUIC_STREAM *qs,
3248                                  int is_write,
3249                                  int *state,
3250                                  uint64_t *app_error_code)
3251 {
3252     int local_init;
3253     uint64_t final_size;
3254
3255     local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
3256
3257     if (app_error_code != NULL)
3258         *app_error_code = UINT64_MAX;
3259     else
3260         app_error_code = &final_size; /* throw away value */
3261
3262     if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
3263         /*
3264          * Unidirectional stream and this direction of transmission doesn't
3265          * exist.
3266          */
3267         *state = SSL_STREAM_STATE_WRONG_DIR;
3268     } else if (ossl_quic_channel_is_term_any(qc->ch)) {
3269         /* Connection already closed. */
3270         *state = SSL_STREAM_STATE_CONN_CLOSED;
3271     } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
3272         /* Application has read a FIN. */
3273         *state = SSL_STREAM_STATE_FINISHED;
3274     } else if ((!is_write && qs->stop_sending)
3275                || (is_write && ossl_quic_stream_send_is_reset(qs))) {
3276         /*
3277          * Stream has been reset locally. FIN takes precedence over this for the
3278          * read case as the application need not care if the stream is reset
3279          * after a FIN has been successfully processed.
3280          */
3281         *state          = SSL_STREAM_STATE_RESET_LOCAL;
3282         *app_error_code = !is_write
3283             ? qs->stop_sending_aec
3284             : qs->reset_stream_aec;
3285     } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
3286                || (is_write && qs->peer_stop_sending)) {
3287         /*
3288          * Stream has been reset remotely. */
3289         *state          = SSL_STREAM_STATE_RESET_REMOTE;
3290         *app_error_code = !is_write
3291             ? qs->peer_reset_stream_aec
3292             : qs->peer_stop_sending_aec;
3293     } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
3294                                                             &final_size)) {
3295         /*
3296          * Stream has been finished. Stream reset takes precedence over this for
3297          * the write case as peer may not have received all data.
3298          */
3299         *state = SSL_STREAM_STATE_FINISHED;
3300     } else {
3301         /* Stream still healthy. */
3302         *state = SSL_STREAM_STATE_OK;
3303     }
3304 }
3305
3306 static int quic_get_stream_state(SSL *ssl, int is_write)
3307 {
3308     QCTX ctx;
3309     int state;
3310
3311     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
3312         return SSL_STREAM_STATE_NONE;
3313
3314     quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
3315     quic_unlock(ctx.qc);
3316     return state;
3317 }
3318
3319 int ossl_quic_get_stream_read_state(SSL *ssl)
3320 {
3321     return quic_get_stream_state(ssl, /*is_write=*/0);
3322 }
3323
3324 /*
3325  * SSL_get_stream_write_state
3326  * --------------------------
3327  */
3328 int ossl_quic_get_stream_write_state(SSL *ssl)
3329 {
3330     return quic_get_stream_state(ssl, /*is_write=*/1);
3331 }
3332
3333 /*
3334  * SSL_get_stream_read_error_code
3335  * ------------------------------
3336  */
3337 static int quic_get_stream_error_code(SSL *ssl, int is_write,
3338                                       uint64_t *app_error_code)
3339 {
3340     QCTX ctx;
3341     int state;
3342
3343     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
3344         return -1;
3345
3346     quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
3347                          &state, app_error_code);
3348
3349     quic_unlock(ctx.qc);
3350     switch (state) {
3351         case SSL_STREAM_STATE_FINISHED:
3352              return 0;
3353         case SSL_STREAM_STATE_RESET_LOCAL:
3354         case SSL_STREAM_STATE_RESET_REMOTE:
3355              return 1;
3356         default:
3357              return -1;
3358     }
3359 }
3360
3361 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
3362 {
3363     return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
3364 }
3365
3366 /*
3367  * SSL_get_stream_write_error_code
3368  * -------------------------------
3369  */
3370 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
3371 {
3372     return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
3373 }
3374
3375 /*
3376  * Write buffer size mutation
3377  * --------------------------
3378  */
3379 int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
3380 {
3381     int ret = 0;
3382     QCTX ctx;
3383
3384     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
3385         return 0;
3386
3387     if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
3388         /* Called on a unidirectional receive-only stream - error. */
3389         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3390         goto out;
3391     }
3392
3393     if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
3394         /*
3395          * If the stream has a send part but we have disposed of it because we
3396          * no longer need it, this is a no-op.
3397          */
3398         ret = 1;
3399         goto out;
3400     }
3401
3402     if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
3403         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3404         goto out;
3405     }
3406
3407     ret = 1;
3408
3409 out:
3410     quic_unlock(ctx.qc);
3411     return ret;
3412 }
3413
3414 /*
3415  * SSL_get_conn_close_info
3416  * -----------------------
3417  */
3418 int ossl_quic_get_conn_close_info(SSL *ssl,
3419                                   SSL_CONN_CLOSE_INFO *info,
3420                                   size_t info_len)
3421 {
3422     QCTX ctx;
3423     const QUIC_TERMINATE_CAUSE *tc;
3424
3425     if (!expect_quic_conn_only(ssl, &ctx))
3426         return -1;
3427
3428     tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
3429     if (tc == NULL)
3430         return 0;
3431
3432     info->error_code    = tc->error_code;
3433     info->reason        = tc->reason;
3434     info->reason_len    = tc->reason_len;
3435     info->flags         = 0;
3436     if (!tc->remote)
3437         info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;
3438     if (!tc->app)
3439         info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;
3440     return 1;
3441 }
3442
3443 /*
3444  * SSL_key_update
3445  * --------------
3446  */
3447 int ossl_quic_key_update(SSL *ssl, int update_type)
3448 {
3449     QCTX ctx;
3450
3451     if (!expect_quic_conn_only(ssl, &ctx))
3452         return 0;
3453
3454     switch (update_type) {
3455     case SSL_KEY_UPDATE_NOT_REQUESTED:
3456         /*
3457          * QUIC signals peer key update implicily by triggering a local
3458          * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3459          */
3460     case SSL_KEY_UPDATE_REQUESTED:
3461         break;
3462
3463     default:
3464         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3465         return 0;
3466     }
3467
3468     quic_lock(ctx.qc);
3469
3470     /* Attempt to perform a TXKU. */
3471     if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
3472         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
3473         quic_unlock(ctx.qc);
3474         return 0;
3475     }
3476
3477     quic_unlock(ctx.qc);
3478     return 1;
3479 }
3480
3481 /*
3482  * SSL_get_key_update_type
3483  * -----------------------
3484  */
3485 int ossl_quic_get_key_update_type(const SSL *s)
3486 {
3487     /*
3488      * We always handle key updates immediately so a key update is never
3489      * pending.
3490      */
3491     return SSL_KEY_UPDATE_NONE;
3492 }
3493
3494 /*
3495  * QUIC Front-End I/O API: SSL_CTX Management
3496  * ==========================================
3497  */
3498
3499 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3500 {
3501     switch (cmd) {
3502     default:
3503         return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
3504     }
3505 }
3506
3507 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3508 {
3509     QCTX ctx;
3510
3511     if (!expect_quic_conn_only(s, &ctx))
3512         return 0;
3513
3514     switch (cmd) {
3515     case SSL_CTRL_SET_MSG_CALLBACK:
3516         ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
3517                                            &ctx.qc->ssl);
3518         /* This callback also needs to be set on the internal SSL object */
3519         return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
3520
3521     default:
3522         /* Probably a TLS related ctrl. Defer to our internal SSL object */
3523         return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
3524     }
3525 }
3526
3527 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3528 {
3529     return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
3530 }
3531
3532 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
3533 {
3534     /* We never do renegotiation. */
3535     return 0;
3536 }
3537
3538 const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)
3539 {
3540     const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);
3541
3542     if ((ciph->algorithm2 & SSL_QUIC) == 0)
3543         return NULL;
3544
3545     return ciph;
3546 }
3547
3548 /*
3549  * These functions define the TLSv1.2 (and below) ciphers that are supported by
3550  * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3551  */
3552
3553 int ossl_quic_num_ciphers(void)
3554 {
3555     return 0;
3556 }
3557
3558 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
3559 {
3560     return NULL;
3561 }
3562
3563 /*
3564  * Internal Testing APIs
3565  * =====================
3566  */
3567
3568 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
3569 {
3570     QCTX ctx;
3571
3572     if (!expect_quic_conn_only(s, &ctx))
3573         return NULL;
3574
3575     return ctx.qc->ch;
3576 }