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