2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 typedef struct qctx_st QCTX;
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);
38 * QUIC Front-End I/O API: Common Utilities
39 * ========================================
43 * Block until a predicate is met.
45 * Precondition: Must have a channel.
46 * Precondition: Must hold channel lock (unchecked).
49 static int block_until_pred(QUIC_CONNECTION *qc,
50 int (*pred)(void *arg), void *pred_arg,
55 assert(qc->ch != NULL);
57 rtor = ossl_quic_channel_get_reactor(qc->ch);
58 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
62 static OSSL_TIME get_time(QUIC_CONNECTION *qc)
64 if (qc->override_now_cb != NULL)
65 return qc->override_now_cb(qc->override_now_cb_arg);
67 return ossl_time_now();
70 static OSSL_TIME get_time_cb(void *arg)
72 QUIC_CONNECTION *qc = arg;
78 * QCTX is a utility structure which provides information we commonly wish to
79 * unwrap upon an API call being dispatched to us, namely:
81 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
83 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
84 * a QCSO with a default stream was passed);
85 * - whether a QSSO was passed (xso == NULL must not be used to determine this
86 * because it may be non-NULL when a QCSO is passed if that QCSO has a
96 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
97 * rather than via ERR. Note that normal errors must always be raised while
101 static int quic_raise_normal_error(QCTX *ctx,
105 ctx->xso->last_error = err;
107 ctx->qc->last_error = err;
113 * Raise a 'non-normal' error, meaning any error that is not reported via
114 * SSL_get_error() and must be reported via ERR.
116 * qc should be provided if available. In exceptional circumstances when qc is
117 * not known NULL may be passed. This should generally only happen when an
118 * expect_...() function defined below fails, which generally indicates a
119 * dispatch error or caller error.
121 * ctx should be NULL if the connection lock is not held.
123 static int quic_raise_non_normal_error(QCTX *ctx,
134 ERR_set_debug(file, line, func);
137 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
141 if (ctx->is_stream && ctx->xso != NULL)
142 ctx->xso->last_error = SSL_ERROR_SSL;
143 else if (!ctx->is_stream && ctx->qc != NULL)
144 ctx->qc->last_error = SSL_ERROR_SSL;
150 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
151 quic_raise_normal_error((ctx), (err))
153 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
154 quic_raise_non_normal_error((ctx), \
155 OPENSSL_FILE, OPENSSL_LINE, \
161 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
162 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
164 * After this returns 1, all fields of the passed QCTX are initialised.
165 * Returns 0 on failure. This function is intended to be used to provide API
166 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
168 static int expect_quic(const SSL *s, QCTX *ctx)
178 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
181 case SSL_TYPE_QUIC_CONNECTION:
182 qc = (QUIC_CONNECTION *)s;
184 ctx->xso = qc->default_xso;
188 case SSL_TYPE_QUIC_XSO:
196 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
201 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
202 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
205 * remote_init determines if we expect the default XSO to be remotely created or
206 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
208 * Channel mutex is acquired and retained on success.
211 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
214 if (!expect_quic(s, ctx))
219 if (ctx->xso == NULL && remote_init >= 0) {
220 if (ossl_quic_channel_is_term_any(ctx->qc->ch)) {
221 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
225 /* If we haven't finished the handshake, try to advance it. */
226 if (quic_do_handshake(ctx) < 1)
227 /* ossl_quic_do_handshake raised error here */
230 if (remote_init == 0) {
231 if (!qc_try_create_default_xso_for_write(ctx))
234 if (!qc_wait_for_default_xso_for_read(ctx))
238 ctx->xso = ctx->qc->default_xso;
241 if (ctx->xso == NULL) {
242 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
246 return 1; /* lock held */
249 quic_unlock(ctx->qc);
254 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
255 * be non-NULL if the QCSO has a default stream.
257 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
259 if (!expect_quic(s, ctx))
263 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
269 * Ensures that the channel mutex is held for a method which touches channel
272 * Precondition: Channel mutex is not held (unchecked)
274 static void quic_lock(QUIC_CONNECTION *qc)
276 #if defined(OPENSSL_THREADS)
277 ossl_crypto_mutex_lock(qc->mutex);
281 /* Precondition: Channel mutex is held (unchecked) */
283 static void quic_unlock(QUIC_CONNECTION *qc)
285 #if defined(OPENSSL_THREADS)
286 ossl_crypto_mutex_unlock(qc->mutex);
292 * QUIC Front-End I/O API: Initialization
293 * ======================================
295 * SSL_new => ossl_quic_new
297 * SSL_reset => ossl_quic_reset
298 * SSL_clear => ossl_quic_clear
300 * SSL_free => ossl_quic_free
305 SSL *ossl_quic_new(SSL_CTX *ctx)
307 QUIC_CONNECTION *qc = NULL;
308 SSL *ssl_base = NULL;
309 SSL_CONNECTION *sc = NULL;
311 qc = OPENSSL_zalloc(sizeof(*qc));
315 /* Initialise the QUIC_CONNECTION's stub header. */
317 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
322 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
323 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
325 /* override the user_ssl of the inner connection */
326 sc->user_ssl = ssl_base;
327 sc->s3.flags |= TLS1_FLAGS_QUIC;
329 #if defined(OPENSSL_THREADS)
330 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
334 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
335 qc->is_thread_assisted
336 = (ssl_base->method == OSSL_QUIC_client_thread_method());
339 qc->as_server = 0; /* TODO(QUIC): server support */
340 qc->as_server_state = qc->as_server;
342 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
343 qc->default_ssl_mode = qc->ssl.ctx->mode;
344 qc->default_blocking = 1;
346 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
347 qc->last_error = SSL_ERROR_NONE;
349 if (!create_channel(qc))
352 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
353 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
355 qc_update_reject_policy(qc);
358 * We do not create the default XSO yet. The reason for this is that the
359 * stream ID of the default XSO will depend on whether the stream is client
360 * or server-initiated, which depends on who transmits first. Since we do
361 * not know whether the application will be using a client-transmits-first
362 * or server-transmits-first protocol, we defer default XSO creation until
363 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
364 * we take that as a cue that the client is expecting a server-initiated
365 * stream, and vice versa if SSL_write() is called first.
371 #if defined(OPENSSL_THREADS)
372 ossl_crypto_mutex_free(qc->mutex);
374 ossl_quic_channel_free(qc->ch);
383 void ossl_quic_free(SSL *s)
388 /* We should never be called on anything but a QSO. */
389 if (!expect_quic(s, &ctx))
396 * When a QSSO is freed, the XSO is freed immediately, because the XSO
397 * itself only contains API personality layer data. However the
398 * underlying QUIC_STREAM is not freed immediately but is instead marked
399 * as deleted for later collection.
402 assert(ctx.qc->num_xso > 0);
405 /* If a stream's send part has not been finished, auto-reset it. */
406 if (ctx.xso->stream->sstream != NULL
407 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
408 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
411 /* Do STOP_SENDING for the receive part, if applicable. */
412 if (ctx.xso->stream->rstream != NULL)
413 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
416 /* Update stream state. */
417 ctx.xso->stream->deleted = 1;
418 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
421 is_default = (ctx.xso == ctx.qc->default_xso);
425 * Unref the connection in most cases; the XSO has a ref to the QC and
426 * not vice versa. But for a default XSO, to avoid circular references,
427 * the QC refs the XSO but the XSO does not ref the QC. If we are the
428 * default XSO, we only get here when the QC is being torn down anyway,
429 * so don't call SSL_free(qc) as we are already in it.
432 SSL_free(&ctx.qc->ssl);
434 /* Note: SSL_free calls OPENSSL_free(xso) for us */
439 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
440 * stage, but is freed during the channel free when the whole QSM is freed.
442 if (ctx.qc->default_xso != NULL) {
443 QUIC_XSO *xso = ctx.qc->default_xso;
448 ctx.qc->default_xso = NULL;
451 /* Ensure we have no remaining XSOs. */
452 assert(ctx.qc->num_xso == 0);
454 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
455 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
456 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
457 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
461 ossl_quic_channel_free(ctx.qc->ch);
463 BIO_free(ctx.qc->net_rbio);
464 BIO_free(ctx.qc->net_wbio);
466 /* Note: SSL_free calls OPENSSL_free(qc) for us */
468 SSL_free(ctx.qc->tls);
469 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
470 #if defined(OPENSSL_THREADS)
471 ossl_crypto_mutex_free(&ctx.qc->mutex);
475 /* SSL method init */
476 int ossl_quic_init(SSL *s)
478 /* Same op as SSL_clear, forward the call. */
479 return ossl_quic_clear(s);
482 /* SSL method deinit */
483 void ossl_quic_deinit(SSL *s)
489 int ossl_quic_reset(SSL *s)
493 if (!expect_quic(s, &ctx))
496 /* TODO(QUIC); Currently a no-op. */
501 int ossl_quic_clear(SSL *s)
505 if (!expect_quic(s, &ctx))
508 /* TODO(QUIC): Currently a no-op. */
512 int ossl_quic_conn_set_override_now_cb(SSL *s,
513 OSSL_TIME (*now_cb)(void *arg),
518 if (!expect_quic(s, &ctx))
523 ctx.qc->override_now_cb = now_cb;
524 ctx.qc->override_now_cb_arg = now_cb_arg;
530 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
534 if (!expect_quic(s, &ctx))
537 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
538 if (ctx.qc->is_thread_assisted && ctx.qc->started)
539 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
544 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
546 qc->default_xso_created = 1;
547 qc_update_reject_policy(qc);
551 * Changes default XSO. Allows caller to keep reference to the old default XSO
552 * (if any). Reference to new XSO is transferred from caller.
555 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
563 if (qc->default_xso != xso) {
564 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
566 qc->default_xso = xso;
570 * Changing to not having a default XSO. XSO becomes standalone and
571 * now has a ref to the QC.
573 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
577 * Changing from not having a default XSO to having one. The new XSO
578 * will have had a reference to the QC we need to drop to avoid a
579 * circular reference.
581 * Currently we never change directly from one default XSO to
582 * another, though this function would also still be correct if this
585 assert(*old_xso == NULL);
587 CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
593 qc_touch_default_xso(qc);
597 * Changes default XSO, releasing the reference to any previous default XSO.
598 * Reference to new XSO is transferred from caller.
601 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
603 QUIC_XSO *old_xso = NULL;
605 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
608 SSL_free(&old_xso->ssl);
612 * QUIC Front-End I/O API: Network BIO Configuration
613 * =================================================
615 * Handling the different BIOs is difficult:
617 * - It is more or less a requirement that we use non-blocking network I/O;
618 * we need to be able to have timeouts on recv() calls, and make best effort
619 * (non blocking) send() and recv() calls.
621 * The only sensible way to do this is to configure the socket into
622 * non-blocking mode. We could try to do select() before calling send() or
623 * recv() to get a guarantee that the call will not block, but this will
624 * probably run into issues with buggy OSes which generate spurious socket
625 * readiness events. In any case, relying on this to work reliably does not
628 * Timeouts could be handled via setsockopt() socket timeout options, but
629 * this depends on OS support and adds another syscall to every network I/O
630 * operation. It also has obvious thread safety concerns if we want to move
631 * to concurrent use of a single socket at some later date.
633 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
634 * be made non-blocking. However some OSes (e.g. Windows) do not support
635 * this, so we cannot rely on this.
637 * As such, we need to configure any FD in non-blocking mode. This may
638 * confound users who pass a blocking socket to libssl. However, in practice
639 * it would be extremely strange for a user of QUIC to pass an FD to us,
640 * then also try and send receive traffic on the same socket(!). Thus the
641 * impact of this should be limited, and can be documented.
643 * - We support both blocking and non-blocking operation in terms of the API
644 * presented to the user. One prospect is to set the blocking mode based on
645 * whether the socket passed to us was already in blocking mode. However,
646 * Windows has no API for determining if a socket is in blocking mode (!),
647 * therefore this cannot be done portably. Currently therefore we expose an
648 * explicit API call to set this, and default to blocking mode.
650 * - We need to determine our initial destination UDP address. The "natural"
651 * way for a user to do this is to set the peer variable on a BIO_dgram.
652 * However, this has problems because BIO_dgram's peer variable is used for
653 * both transmission and reception. This means it can be constantly being
654 * changed to a malicious value (e.g. if some random unrelated entity on the
655 * network starts sending traffic to us) on every read call. This is not a
656 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
657 * calls only, which do not use this variable. However, we do need to let
658 * the user specify the peer in a 'normal' manner. The compromise here is
659 * that we grab the current peer value set at the time the write BIO is set
660 * and do not read the value again.
662 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
663 * Currently we do this by only supporting non-blocking mode.
668 * Determines what initial destination UDP address we should use, if possible.
669 * If this fails the client must set the destination address manually, or use a
670 * BIO which does not need a destination address.
672 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
674 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
680 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
684 if (!expect_quic(s, &ctx))
687 if (ctx.qc->net_rbio == net_rbio)
690 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
693 BIO_free(ctx.qc->net_rbio);
694 ctx.qc->net_rbio = net_rbio;
697 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
698 * mode as we do not support it for non-pollable BIOs.
700 if (net_rbio != NULL) {
701 BIO_POLL_DESCRIPTOR d = {0};
703 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
704 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
705 ctx.qc->blocking = 0;
706 ctx.qc->default_blocking = 0;
707 ctx.qc->can_poll_net_rbio = 0;
709 ctx.qc->can_poll_net_rbio = 1;
714 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
718 if (!expect_quic(s, &ctx))
721 if (ctx.qc->net_wbio == net_wbio)
724 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
727 BIO_free(ctx.qc->net_wbio);
728 ctx.qc->net_wbio = net_wbio;
730 if (net_wbio != NULL) {
731 BIO_POLL_DESCRIPTOR d = {0};
733 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
734 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
735 ctx.qc->blocking = 0;
736 ctx.qc->default_blocking = 0;
737 ctx.qc->can_poll_net_wbio = 0;
739 ctx.qc->can_poll_net_wbio = 1;
743 * If we do not have a peer address yet, and we have not started trying
744 * to connect yet, try to autodetect one.
746 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
747 && !ctx.qc->started) {
748 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
750 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
752 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
753 &ctx.qc->init_peer_addr);
758 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
762 if (!expect_quic(s, &ctx))
765 return ctx.qc->net_rbio;
768 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
772 if (!expect_quic(s, &ctx))
775 return ctx.qc->net_wbio;
778 int ossl_quic_conn_get_blocking_mode(const SSL *s)
782 if (!expect_quic(s, &ctx))
786 return ctx.xso->blocking;
788 return ctx.qc->blocking;
791 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
795 if (!expect_quic(s, &ctx))
798 /* Cannot enable blocking mode if we do not have pollable FDs. */
800 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
801 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
803 if (!ctx.is_stream) {
805 * If called on a QCSO, update default and connection-level blocking
808 ctx.qc->blocking = (blocking != 0);
809 ctx.qc->default_blocking = ctx.qc->blocking;
814 * If called on a QSSO or QCSO with a default XSO, update blocking
817 ctx.xso->blocking = (blocking != 0);
822 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
823 const BIO_ADDR *peer_addr)
827 if (!expect_quic(s, &ctx))
831 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
834 if (peer_addr == NULL) {
835 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
839 ctx.qc->init_peer_addr = *peer_addr;
844 * QUIC Front-End I/O API: Asynchronous I/O Management
845 * ===================================================
847 * (BIO/)SSL_handle_events => ossl_quic_handle_events
848 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
849 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
853 /* Returns 1 if the connection is being used in blocking mode. */
854 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
859 static int xso_blocking_mode(const QUIC_XSO *xso)
862 && xso->conn->can_poll_net_rbio
863 && xso->conn->can_poll_net_wbio;
866 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
868 int ossl_quic_handle_events(SSL *s)
872 if (!expect_quic(s, &ctx))
876 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
882 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
883 * should next have events handled by the application by calling
884 * SSL_handle_events(). tv is set to 0 if the object should have events handled
885 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
886 * the value of *tv is undefined.
889 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
892 OSSL_TIME deadline = ossl_time_infinite();
894 if (!expect_quic(s, &ctx))
900 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
902 if (ossl_time_is_infinite(deadline)) {
906 * Robustness against faulty applications that don't check *is_infinite;
907 * harmless long timeout.
909 tv->tv_sec = 1000000;
916 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
922 /* SSL_get_rpoll_descriptor */
923 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
927 if (!expect_quic(s, &ctx))
930 if (desc == NULL || ctx.qc->net_rbio == NULL)
933 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
936 /* SSL_get_wpoll_descriptor */
937 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
941 if (!expect_quic(s, &ctx))
944 if (desc == NULL || ctx.qc->net_wbio == NULL)
947 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
950 /* SSL_net_read_desired */
952 int ossl_quic_get_net_read_desired(SSL *s)
957 if (!expect_quic(s, &ctx))
961 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
966 /* SSL_net_write_desired */
968 int ossl_quic_get_net_write_desired(SSL *s)
973 if (!expect_quic(s, &ctx))
977 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
983 * QUIC Front-End I/O API: Connection Lifecycle Operations
984 * =======================================================
986 * SSL_do_handshake => ossl_quic_do_handshake
987 * SSL_set_connect_state => ossl_quic_set_connect_state
988 * SSL_set_accept_state => ossl_quic_set_accept_state
989 * SSL_shutdown => ossl_quic_shutdown
990 * SSL_ctrl => ossl_quic_ctrl
991 * (BIO/)SSL_connect => ossl_quic_connect
992 * (BIO/)SSL_accept => ossl_quic_accept
997 static int quic_shutdown_wait(void *arg)
999 QUIC_CONNECTION *qc = arg;
1001 return ossl_quic_channel_is_terminated(qc->ch);
1005 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1006 const SSL_SHUTDOWN_EX_ARGS *args,
1012 if (!expect_quic(s, &ctx))
1016 /* TODO(QUIC): Semantics currently undefined for QSSOs */
1021 ossl_quic_channel_local_close(ctx.qc->ch,
1022 args != NULL ? args->quic_error_code : 0);
1024 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
1026 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1027 quic_unlock(ctx.qc);
1031 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
1032 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1034 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1036 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1037 quic_unlock(ctx.qc);
1042 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1046 if (!expect_quic(s, &ctx))
1051 /* If called on a QCSO, update the default mode. */
1053 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1056 * If we were called on a QSSO or have a default stream, we also update
1059 if (ctx.xso != NULL) {
1060 /* Cannot enable EPW while AON write in progress. */
1061 if (ctx.xso->aon_write_in_progress)
1062 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1064 ctx.xso->ssl_mode |= (uint32_t)larg;
1065 return ctx.xso->ssl_mode;
1068 return ctx.qc->default_ssl_mode;
1069 case SSL_CTRL_CLEAR_MODE:
1071 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1073 if (ctx.xso != NULL) {
1074 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1075 return ctx.xso->ssl_mode;
1078 return ctx.qc->default_ssl_mode;
1080 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1081 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1082 /* This ctrl also needs to be passed to the internal SSL object */
1083 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1085 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1089 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1092 return !is_infinite;
1094 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1095 /* For legacy compatibility with DTLS calls. */
1096 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1098 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1099 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1103 /* SSL_set_connect_state */
1104 void ossl_quic_set_connect_state(SSL *s)
1108 if (!expect_quic(s, &ctx))
1111 /* Cannot be changed after handshake started */
1112 if (ctx.qc->started || ctx.is_stream)
1115 ctx.qc->as_server_state = 0;
1118 /* SSL_set_accept_state */
1119 void ossl_quic_set_accept_state(SSL *s)
1123 if (!expect_quic(s, &ctx))
1126 /* Cannot be changed after handshake started */
1127 if (ctx.qc->started || ctx.is_stream)
1130 ctx.qc->as_server_state = 1;
1133 /* SSL_do_handshake */
1134 struct quic_handshake_wait_args {
1135 QUIC_CONNECTION *qc;
1138 static int quic_handshake_wait(void *arg)
1140 struct quic_handshake_wait_args *args = arg;
1142 if (!ossl_quic_channel_is_active(args->qc->ch))
1145 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1151 static int configure_channel(QUIC_CONNECTION *qc)
1153 assert(qc->ch != NULL);
1155 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1156 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1157 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1164 static int create_channel(QUIC_CONNECTION *qc)
1166 QUIC_CHANNEL_ARGS args = {0};
1168 args.libctx = qc->ssl.ctx->libctx;
1169 args.propq = qc->ssl.ctx->propq;
1170 args.is_server = qc->as_server;
1172 args.mutex = qc->mutex;
1173 args.now_cb = get_time_cb;
1174 args.now_cb_arg = qc;
1176 qc->ch = ossl_quic_channel_new(&args);
1184 * Creates a channel and configures it with the information we have accumulated
1185 * via calls made to us from the application prior to starting a handshake
1189 static int ensure_channel_started(QUIC_CONNECTION *qc)
1192 if (!configure_channel(qc)
1193 || !ossl_quic_channel_start(qc->ch))
1196 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1197 if (qc->is_thread_assisted)
1198 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1207 ossl_quic_channel_free(qc->ch);
1213 static int quic_do_handshake(QCTX *ctx)
1216 QUIC_CONNECTION *qc = ctx->qc;
1218 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1219 /* Handshake already completed. */
1222 if (ossl_quic_channel_is_term_any(qc->ch))
1223 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1225 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1226 /* Peer address must have been set. */
1227 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1228 return -1; /* Non-protocol error */
1231 if (qc->as_server != qc->as_server_state) {
1232 /* TODO(QUIC): Must match the method used to create the QCSO */
1233 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1234 return -1; /* Non-protocol error */
1237 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1238 /* Need read and write BIOs. */
1239 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1240 return -1; /* Non-protocol error */
1244 * Start connection process. Note we may come here multiple times in
1245 * non-blocking mode, which is fine.
1247 if (!ensure_channel_started(qc)) {
1248 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1249 return -1; /* Non-protocol error */
1252 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1253 /* The handshake is now done. */
1256 if (qc_blocking_mode(qc)) {
1257 /* In blocking mode, wait for the handshake to complete. */
1258 struct quic_handshake_wait_args args;
1262 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1263 if (!ossl_quic_channel_is_active(qc->ch)) {
1264 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1265 return 0; /* Shutdown before completion */
1266 } else if (ret <= 0) {
1267 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1268 return -1; /* Non-protocol error */
1271 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1274 /* Try to advance the reactor. */
1275 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1277 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1278 /* The handshake is now done. */
1281 /* Otherwise, indicate that the handshake isn't done yet. */
1282 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1283 return -1; /* Non-protocol error */
1288 int ossl_quic_do_handshake(SSL *s)
1293 if (!expect_quic(s, &ctx))
1298 ret = quic_do_handshake(&ctx);
1299 quic_unlock(ctx.qc);
1304 int ossl_quic_connect(SSL *s)
1306 /* Ensure we are in connect state (no-op if non-idle). */
1307 ossl_quic_set_connect_state(s);
1309 /* Begin or continue the handshake */
1310 return ossl_quic_do_handshake(s);
1314 int ossl_quic_accept(SSL *s)
1316 /* Ensure we are in accept state (no-op if non-idle). */
1317 ossl_quic_set_accept_state(s);
1319 /* Begin or continue the handshake */
1320 return ossl_quic_do_handshake(s);
1324 * QUIC Front-End I/O API: Stream Lifecycle Operations
1325 * ===================================================
1327 * SSL_stream_new => ossl_quic_conn_stream_new
1332 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1333 * default XSO was created. Returns 0 if it was not (e.g. because it already
1334 * exists). Note that this is NOT an error condition.
1337 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1340 QUIC_CONNECTION *qc = ctx->qc;
1342 if (qc->default_xso_created
1343 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1345 * We only do this once. If the user detaches a previously created
1346 * default XSO we don't auto-create another one.
1348 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1350 /* Create a locally-initiated stream. */
1351 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1352 flags |= SSL_STREAM_FLAG_UNI;
1354 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1357 if (qc->default_xso == NULL)
1358 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1360 qc_touch_default_xso(qc);
1364 struct quic_wait_for_stream_args {
1365 QUIC_CONNECTION *qc;
1372 static int quic_wait_for_stream(void *arg)
1374 struct quic_wait_for_stream_args *args = arg;
1376 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1377 /* If connection is torn down due to an error while blocking, stop. */
1378 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1382 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1383 args->expect_id | QUIC_STREAM_DIR_BIDI);
1384 if (args->qs == NULL)
1385 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1386 args->expect_id | QUIC_STREAM_DIR_UNI);
1388 if (args->qs != NULL)
1389 return 1; /* stream now exists */
1391 return 0; /* did not get a stream, keep trying */
1395 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1397 /* Called on a QCSO and we don't currently have a default stream. */
1399 QUIC_CONNECTION *qc = ctx->qc;
1402 struct quic_wait_for_stream_args wargs;
1405 * If default stream functionality is disabled or we already detached
1406 * one, don't make another default stream and just fail.
1408 if (qc->default_xso_created
1409 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1410 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1413 * The peer may have opened a stream since we last ticked. So tick and
1414 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1415 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1416 * first stream created by a peer must have an ordinal of 0.
1418 expect_id = qc->as_server
1419 ? QUIC_STREAM_INITIATOR_CLIENT
1420 : QUIC_STREAM_INITIATOR_SERVER;
1422 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1423 expect_id | QUIC_STREAM_DIR_BIDI);
1425 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1426 expect_id | QUIC_STREAM_DIR_UNI);
1429 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1431 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1436 if (!qc_blocking_mode(qc))
1437 /* Non-blocking mode, so just bail immediately. */
1438 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1440 /* Block until we have a stream. */
1444 wargs.expect_id = expect_id;
1446 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1448 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1449 else if (res < 0 || wargs.qs == NULL)
1450 /* quic_wait_for_stream raised error here */
1457 * We now have qs != NULL. Make it the default stream, creating the
1460 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1461 if (qc->default_xso == NULL)
1462 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1464 qc_touch_default_xso(qc); /* inhibits default XSO */
1469 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1471 QUIC_XSO *xso = NULL;
1473 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1476 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1480 if (!SSL_up_ref(&qc->ssl))
1484 xso->blocking = qc->default_blocking;
1485 xso->ssl_mode = qc->default_ssl_mode;
1486 xso->last_error = SSL_ERROR_NONE;
1498 /* locking depends on need_lock */
1499 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1501 QUIC_CONNECTION *qc = ctx->qc;
1502 QUIC_XSO *xso = NULL;
1503 QUIC_STREAM *qs = NULL;
1504 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1509 if (ossl_quic_channel_is_term_any(qc->ch)) {
1510 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1514 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1518 xso = create_xso_from_stream(qc, qs);
1522 qc_touch_default_xso(qc); /* inhibits default XSO */
1530 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1539 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1543 if (!expect_quic_conn_only(s, &ctx))
1546 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1550 * QUIC Front-End I/O API: Steady-State Operations
1551 * ===============================================
1553 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1554 * is, the functions used during the established phase of a QUIC connection
1555 * (e.g. SSL_read, SSL_write).
1557 * Each function must handle both blocking and non-blocking modes. As discussed
1558 * above, all QUIC I/O is implemented using non-blocking mode internally.
1560 * SSL_get_error => partially implemented by ossl_quic_get_error
1561 * (BIO/)SSL_read => ossl_quic_read
1562 * (BIO/)SSL_write => ossl_quic_write
1563 * SSL_pending => ossl_quic_pending
1564 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1565 * SSL_key_update => ossl_quic_key_update
1569 int ossl_quic_get_error(const SSL *s, int i)
1573 if (!expect_quic(s, &ctx))
1576 return ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1583 * The set of functions below provide the implementation of the public SSL_write
1584 * function. We must handle:
1586 * - both blocking and non-blocking operation at the application level,
1587 * depending on how we are configured;
1589 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1591 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1595 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1598 * We have appended at least one byte to the stream.
1599 * Potentially mark stream as active, depending on FC.
1602 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1608 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1609 * plus we should eventually consider Nagle's algorithm.
1612 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1615 struct quic_write_again_args {
1617 const unsigned char *buf;
1619 size_t total_written;
1623 static int quic_write_again(void *arg)
1625 struct quic_write_again_args *args = arg;
1626 size_t actual_written = 0;
1628 if (!ossl_quic_channel_is_active(args->xso->conn->ch))
1629 /* If connection is torn down due to an error while blocking, stop. */
1632 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1633 args->buf, args->len, &actual_written))
1636 quic_post_write(args->xso, actual_written > 0, 0);
1638 args->buf += actual_written;
1639 args->len -= actual_written;
1640 args->total_written += actual_written;
1643 /* Written everything, done. */
1646 /* Not written everything yet, keep trying. */
1651 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1655 QUIC_XSO *xso = ctx->xso;
1656 struct quic_write_again_args args;
1657 size_t actual_written = 0;
1659 /* First make a best effort to append as much of the data as possible. */
1660 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1662 /* Stream already finished or allocation error. */
1664 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1667 quic_post_write(xso, actual_written > 0, 1);
1669 if (actual_written == len) {
1670 /* Managed to append everything on the first try. */
1671 *written = actual_written;
1676 * We did not manage to append all of the data immediately, so the stream
1677 * buffer has probably filled up. This means we need to block until some of
1681 args.buf = (const unsigned char *)buf + actual_written;
1682 args.len = len - actual_written;
1683 args.total_written = 0;
1685 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1687 if (!ossl_quic_channel_is_active(xso->conn->ch))
1688 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1690 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1693 *written = args.total_written;
1698 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1701 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1702 size_t buf_len, size_t already_sent)
1704 assert(!xso->aon_write_in_progress);
1706 xso->aon_write_in_progress = 1;
1707 xso->aon_buf_base = buf;
1708 xso->aon_buf_pos = already_sent;
1709 xso->aon_buf_len = buf_len;
1712 static void aon_write_finish(QUIC_XSO *xso)
1714 xso->aon_write_in_progress = 0;
1715 xso->aon_buf_base = NULL;
1716 xso->aon_buf_pos = 0;
1717 xso->aon_buf_len = 0;
1721 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
1722 size_t len, size_t *written)
1724 QUIC_XSO *xso = ctx->xso;
1725 const void *actual_buf;
1726 size_t actual_len, actual_written = 0;
1727 int accept_moving_buffer
1728 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1730 if (xso->aon_write_in_progress) {
1732 * We are in the middle of an AON write (i.e., a previous write did not
1733 * manage to append all data to the SSTREAM and we have Enable Partial
1734 * Write (EPW) mode disabled.)
1736 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1737 || len != xso->aon_buf_len)
1739 * Pointer must not have changed if we are not in accept moving
1740 * buffer mode. Length must never change.
1742 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
1744 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1745 actual_len = len - xso->aon_buf_pos;
1746 assert(actual_len > 0);
1752 /* First make a best effort to append as much of the data as possible. */
1753 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1755 /* Stream already finished or allocation error. */
1757 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1760 quic_post_write(xso, actual_written > 0, 1);
1762 if (actual_written == actual_len) {
1763 /* We have sent everything. */
1764 if (xso->aon_write_in_progress) {
1766 * We have sent everything, and we were in the middle of an AON
1767 * write. The output write length is the total length of the AON
1768 * buffer, not however many bytes we managed to write to the stream
1771 *written = xso->aon_buf_len;
1772 aon_write_finish(xso);
1774 *written = actual_written;
1780 if (xso->aon_write_in_progress) {
1782 * AON write is in progress but we have not written everything yet. We
1783 * may have managed to send zero bytes, or some number of bytes less
1784 * than the total remaining which need to be appended during this
1787 xso->aon_buf_pos += actual_written;
1788 assert(xso->aon_buf_pos < xso->aon_buf_len);
1789 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1793 * Not in an existing AON operation but partial write is not enabled, so we
1794 * need to begin a new AON operation. However we needn't bother if we didn't
1795 * actually append anything.
1797 if (actual_written > 0)
1798 aon_write_begin(xso, buf, len, actual_written);
1801 * AON - We do not publicly admit to having appended anything until AON
1805 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1809 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
1812 QUIC_XSO *xso = ctx->xso;
1814 /* Simple best effort operation. */
1815 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
1816 /* Stream already finished or allocation error. */
1818 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1821 quic_post_write(xso, *written > 0, 1);
1826 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1837 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1840 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1842 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1843 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1848 * If we haven't finished the handshake, try to advance it.
1849 * We don't accept writes until the handshake is completed.
1851 if (quic_do_handshake(&ctx) < 1) {
1856 if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
1857 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
1861 if (xso_blocking_mode(ctx.xso))
1862 ret = quic_write_blocking(&ctx, buf, len, written);
1863 else if (partial_write)
1864 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
1866 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
1869 quic_unlock(ctx.qc);
1877 struct quic_read_again_args {
1879 QUIC_STREAM *stream;
1887 static int quic_read_actual(QCTX *ctx,
1888 QUIC_STREAM *stream,
1889 void *buf, size_t buf_len,
1894 QUIC_CONNECTION *qc = ctx->qc;
1896 /* If the receive part of the stream is over, issue EOF. */
1897 if (stream->recv_fin_retired)
1898 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
1900 if (stream->rstream == NULL)
1901 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1904 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1905 bytes_read, &is_fin))
1906 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1909 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1910 bytes_read, &is_fin))
1911 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1915 if (*bytes_read > 0) {
1917 * We have read at least one byte from the stream. Inform stream-level
1918 * RXFC of the retirement of controlled bytes. Update the active stream
1919 * status (the RXFC may now want to emit a frame granting more credit to
1922 OSSL_RTT_INFO rtt_info;
1924 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1926 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
1927 rtt_info.smoothed_rtt))
1928 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1932 stream->recv_fin_retired = 1;
1934 if (*bytes_read > 0)
1935 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1943 static int quic_read_again(void *arg)
1945 struct quic_read_again_args *args = arg;
1947 if (!ossl_quic_channel_is_active(args->ctx->qc->ch)) {
1948 /* If connection is torn down due to an error while blocking, stop. */
1949 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1953 if (!quic_read_actual(args->ctx, args->stream,
1954 args->buf, args->len, args->bytes_read,
1958 if (*args->bytes_read > 0)
1959 /* got at least one byte, the SSL_read op can finish now */
1962 return 0; /* did not read anything, keep trying */
1966 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1970 struct quic_read_again_args args;
1974 if (!expect_quic(s, &ctx))
1979 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1980 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1984 /* If we haven't finished the handshake, try to advance it. */
1985 if (quic_do_handshake(&ctx) < 1) {
1986 ret = 0; /* ossl_quic_do_handshake raised error here */
1990 if (ctx.xso == NULL) {
1992 * Called on a QCSO and we don't currently have a default stream.
1994 * Wait until we get a stream initiated by the peer (blocking mode) or
1995 * fail if we don't have one yet (non-blocking mode).
1997 if (!qc_wait_for_default_xso_for_read(&ctx)) {
1998 ret = 0; /* error already raised here */
2002 ctx.xso = ctx.qc->default_xso;
2005 if (ctx.xso->stream == NULL) {
2006 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2010 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2011 ret = 0; /* quic_read_actual raised error here */
2015 if (*bytes_read > 0) {
2017 * Even though we succeeded, tick the reactor here to ensure we are
2018 * handling other aspects of the QUIC connection.
2020 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2022 } else if (xso_blocking_mode(ctx.xso)) {
2024 * We were not able to read anything immediately, so our stream
2025 * buffer is empty. This means we need to block until we get
2026 * at least one byte.
2029 args.stream = ctx.xso->stream;
2032 args.bytes_read = bytes_read;
2035 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2037 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2039 } else if (res < 0) {
2040 ret = 0; /* quic_read_again raised error here */
2046 /* We did not get any bytes and are not in blocking mode. */
2047 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2051 quic_unlock(ctx.qc);
2055 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2057 return quic_read(s, buf, len, bytes_read, 0);
2060 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2062 return quic_read(s, buf, len, bytes_read, 1);
2070 static size_t ossl_quic_pending_int(const SSL *s)
2076 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2079 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
2080 /* Cannot raise errors here because we are const, just fail. */
2083 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2087 quic_unlock(ctx.qc);
2091 size_t ossl_quic_pending(const SSL *s)
2093 return ossl_quic_pending_int(s);
2096 int ossl_quic_has_pending(const SSL *s)
2098 return ossl_quic_pending_int(s) > 0;
2102 * SSL_stream_conclude
2103 * -------------------
2106 int ossl_quic_conn_stream_conclude(SSL *s)
2111 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2114 qs = ctx.xso->stream;
2116 if (qs == NULL || qs->sstream == NULL) {
2117 quic_unlock(ctx.qc);
2121 if (!ossl_quic_channel_is_active(ctx.qc->ch)
2122 || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2123 quic_unlock(ctx.qc);
2127 ossl_quic_sstream_fin(qs->sstream);
2128 quic_post_write(ctx.xso, 1, 1);
2129 quic_unlock(ctx.qc);
2134 * SSL_inject_net_dgram
2135 * --------------------
2138 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2140 const BIO_ADDR *peer,
2141 const BIO_ADDR *local)
2147 if (!expect_quic(s, &ctx))
2152 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2153 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2155 quic_unlock(ctx.qc);
2160 * SSL_get0_connection
2161 * -------------------
2163 SSL *ossl_quic_get0_connection(SSL *s)
2167 if (!expect_quic(s, &ctx))
2170 return &ctx.qc->ssl;
2174 * SSL_get_stream_type
2175 * -------------------
2177 int ossl_quic_get_stream_type(SSL *s)
2181 if (!expect_quic(s, &ctx))
2182 return SSL_STREAM_TYPE_BIDI;
2184 if (ctx.xso == NULL) {
2186 * If deferred XSO creation has yet to occur, proceed according to the
2187 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2188 * what kind of stream will be created yet, so return BIDI on the basis
2189 * that at this time, the client still has the option of calling
2190 * SSL_read() or SSL_write() first.
2192 if (ctx.qc->default_xso_created
2193 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2194 return SSL_STREAM_TYPE_NONE;
2196 return SSL_STREAM_TYPE_BIDI;
2199 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2200 return SSL_STREAM_TYPE_BIDI;
2202 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2203 return SSL_STREAM_TYPE_READ;
2205 return SSL_STREAM_TYPE_WRITE;
2213 uint64_t ossl_quic_get_stream_id(SSL *s)
2218 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2221 id = ctx.xso->stream->id;
2222 quic_unlock(ctx.qc);
2228 * SSL_set_default_stream_mode
2229 * ---------------------------
2232 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2236 if (!expect_quic_conn_only(s, &ctx))
2241 if (ctx.qc->default_xso_created)
2242 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2243 "too late to change default stream mode");
2246 case SSL_DEFAULT_STREAM_MODE_NONE:
2247 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2248 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2249 ctx.qc->default_stream_mode = mode;
2252 quic_unlock(ctx.qc);
2253 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2254 "bad default stream type");
2257 quic_unlock(ctx.qc);
2266 SSL *ossl_quic_detach_stream(SSL *s)
2269 QUIC_XSO *xso = NULL;
2271 if (!expect_quic_conn_only(s, &ctx))
2276 /* Calling this function inhibits default XSO autocreation. */
2277 /* QC ref to any default XSO is transferred to us and to caller. */
2278 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2280 quic_unlock(ctx.qc);
2282 return xso != NULL ? &xso->ssl : NULL;
2290 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2296 if (!expect_quic_conn_only(conn, &ctx))
2299 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2300 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2301 "stream to attach must be a valid QUIC stream");
2303 xso = (QUIC_XSO *)stream;
2307 if (ctx.qc->default_xso != NULL) {
2308 quic_unlock(ctx.qc);
2309 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2310 "connection already has a default stream");
2314 * It is a caller error for the XSO being attached as a default XSO to have
2315 * more than one ref.
2317 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
2318 quic_unlock(ctx.qc);
2319 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2324 quic_unlock(ctx.qc);
2325 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2326 "stream being attached must have "
2327 "only 1 reference");
2330 /* Caller's reference to the XSO is transferred to us. */
2331 /* Calling this function inhibits default XSO autocreation. */
2332 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2334 quic_unlock(ctx.qc);
2339 * SSL_set_incoming_stream_policy
2340 * ------------------------------
2343 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2345 switch (qc->incoming_stream_policy) {
2346 case SSL_INCOMING_STREAM_POLICY_AUTO:
2347 if ((qc->default_xso == NULL && !qc->default_xso_created)
2348 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2349 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2351 return SSL_INCOMING_STREAM_POLICY_REJECT;
2354 return qc->incoming_stream_policy;
2359 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2361 int policy = qc_get_effective_incoming_stream_policy(qc);
2362 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2364 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2366 qc->incoming_stream_aec);
2370 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2376 if (!expect_quic_conn_only(s, &ctx))
2382 case SSL_INCOMING_STREAM_POLICY_AUTO:
2383 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2384 case SSL_INCOMING_STREAM_POLICY_REJECT:
2385 ctx.qc->incoming_stream_policy = policy;
2386 ctx.qc->incoming_stream_aec = aec;
2394 qc_update_reject_policy(ctx.qc);
2395 quic_unlock(ctx.qc);
2403 struct wait_for_incoming_stream_args {
2409 static int wait_for_incoming_stream(void *arg)
2411 struct wait_for_incoming_stream_args *args = arg;
2412 QUIC_CONNECTION *qc = args->ctx->qc;
2413 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2415 if (!ossl_quic_channel_is_active(qc->ch)) {
2416 /* If connection is torn down due to an error while blocking, stop. */
2417 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2421 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2422 if (args->qs != NULL)
2423 return 1; /* got a stream */
2425 return 0; /* did not get a stream, keep trying */
2429 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2434 QUIC_STREAM_MAP *qsm;
2437 OSSL_RTT_INFO rtt_info;
2439 if (!expect_quic_conn_only(s, &ctx))
2444 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2445 == SSL_INCOMING_STREAM_POLICY_REJECT)
2448 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2450 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2452 if (qc_blocking_mode(ctx.qc)
2453 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2454 struct wait_for_incoming_stream_args args;
2459 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2461 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2463 } else if (ret < 0 || args.qs == NULL) {
2473 xso = create_xso_from_stream(ctx.qc, qs);
2477 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2478 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2479 rtt_info.smoothed_rtt);
2482 /* Calling this function inhibits default XSO autocreation. */
2483 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2486 quic_unlock(ctx.qc);
2491 * SSL_get_accept_stream_queue_len
2492 * -------------------------------
2495 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2500 if (!expect_quic_conn_only(s, &ctx))
2505 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2507 quic_unlock(ctx.qc);
2515 int ossl_quic_stream_reset(SSL *ssl,
2516 const SSL_STREAM_RESET_ARGS *args,
2520 QUIC_STREAM_MAP *qsm;
2522 uint64_t error_code;
2524 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2527 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2528 qs = ctx.xso->stream;
2529 error_code = (args != NULL ? args->quic_error_code : 0);
2531 ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2533 quic_unlock(ctx.qc);
2538 * SSL_get_stream_read_state
2539 * -------------------------
2541 static void quic_classify_stream(QUIC_CONNECTION *qc,
2545 uint64_t *app_error_code)
2548 uint64_t final_size;
2550 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2552 if (app_error_code != NULL)
2553 *app_error_code = UINT64_MAX;
2555 app_error_code = &final_size; /* throw away value */
2557 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2559 * Unidirectional stream and this direction of transmission doesn't
2562 *state = SSL_STREAM_STATE_WRONG_DIR;
2563 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2564 /* Connection already closed. */
2565 *state = SSL_STREAM_STATE_CONN_CLOSED;
2566 } else if (!is_write && qs->recv_fin_retired) {
2567 /* Application has read a FIN. */
2568 *state = SSL_STREAM_STATE_FINISHED;
2569 } else if ((!is_write && qs->stop_sending)
2570 || (is_write && qs->reset_stream)) {
2572 * Stream has been reset locally. FIN takes precedence over this for the
2573 * read case as the application need not care if the stream is reset
2574 * after a FIN has been successfully processed.
2576 *state = SSL_STREAM_STATE_RESET_LOCAL;
2577 *app_error_code = !is_write
2578 ? qs->stop_sending_aec
2579 : qs->reset_stream_aec;
2580 } else if ((!is_write && qs->peer_reset_stream)
2581 || (is_write && qs->peer_stop_sending)) {
2583 * Stream has been reset remotely. */
2584 *state = SSL_STREAM_STATE_RESET_REMOTE;
2585 *app_error_code = !is_write
2586 ? qs->peer_reset_stream_aec
2587 : qs->peer_stop_sending_aec;
2588 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2591 * Stream has been finished. Stream reset takes precedence over this for
2592 * the write case as peer may not have received all data.
2594 *state = SSL_STREAM_STATE_FINISHED;
2596 /* Stream still healthy. */
2597 *state = SSL_STREAM_STATE_OK;
2601 static int quic_get_stream_state(SSL *ssl, int is_write)
2606 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2607 return SSL_STREAM_STATE_NONE;
2609 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2610 quic_unlock(ctx.qc);
2614 int ossl_quic_get_stream_read_state(SSL *ssl)
2616 return quic_get_stream_state(ssl, /*is_write=*/0);
2620 * SSL_get_stream_write_state
2621 * --------------------------
2623 int ossl_quic_get_stream_write_state(SSL *ssl)
2625 return quic_get_stream_state(ssl, /*is_write=*/1);
2629 * SSL_get_stream_read_error_code
2630 * ------------------------------
2632 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2633 uint64_t *app_error_code)
2638 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2641 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2642 &state, app_error_code);
2644 quic_unlock(ctx.qc);
2646 case SSL_STREAM_STATE_FINISHED:
2648 case SSL_STREAM_STATE_RESET_LOCAL:
2649 case SSL_STREAM_STATE_RESET_REMOTE:
2656 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2658 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2662 * SSL_get_stream_write_error_code
2663 * -------------------------------
2665 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2667 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2671 * SSL_get_conn_close_info
2672 * -----------------------
2674 int ossl_quic_get_conn_close_info(SSL *ssl,
2675 SSL_CONN_CLOSE_INFO *info,
2679 const QUIC_TERMINATE_CAUSE *tc;
2681 if (!expect_quic_conn_only(ssl, &ctx))
2684 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
2688 info->error_code = tc->error_code;
2689 info->reason = NULL; /* TODO(QUIC): Wire reason */
2690 info->reason_len = 0;
2691 info->is_local = !tc->remote;
2692 info->is_transport = !tc->app;
2700 int ossl_quic_key_update(SSL *ssl, int update_type)
2704 if (!expect_quic_conn_only(ssl, &ctx))
2707 switch (update_type) {
2708 case SSL_KEY_UPDATE_NOT_REQUESTED:
2710 * QUIC signals peer key update implicily by triggering a local
2711 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
2713 case SSL_KEY_UPDATE_REQUESTED:
2717 /* Unknown type - error. */
2723 /* Attempt to perform a TXKU. */
2724 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
2725 quic_unlock(ctx.qc);
2729 quic_unlock(ctx.qc);
2734 * SSL_get_key_update_type
2735 * -----------------------
2737 int ossl_quic_get_key_update_type(const SSL *s)
2740 * We always handle key updates immediately so a key update is never
2743 return SSL_KEY_UPDATE_NONE;
2747 * QUIC Front-End I/O API: SSL_CTX Management
2748 * ==========================================
2751 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2755 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
2759 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
2763 if (!expect_quic_conn_only(s, &ctx))
2767 case SSL_CTRL_SET_MSG_CALLBACK:
2768 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
2770 /* This callback also needs to be set on the internal SSL object */
2771 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
2774 /* Probably a TLS related ctrl. Defer to our internal SSL object */
2775 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
2779 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
2781 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
2784 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
2786 /* We never do renegotiation. */
2791 * These functions define the TLSv1.2 (and below) ciphers that are supported by
2792 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
2795 int ossl_quic_num_ciphers(void)
2800 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
2805 int ossl_quic_set_ssl_op(SSL *ssl, uint64_t op)
2810 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2813 if (ctx.xso->stream == NULL)
2816 cleanse = (op & SSL_OP_CLEANSE_PLAINTEXT) != 0;
2817 if (ctx.xso->stream->rstream != NULL)
2818 ossl_quic_rstream_set_cleanse(ctx.xso->stream->rstream, cleanse);
2819 if (ctx.xso->stream->sstream != NULL)
2820 ossl_quic_sstream_set_cleanse(ctx.xso->stream->sstream, cleanse);
2823 quic_unlock(ctx.qc);
2828 * Internal Testing APIs
2829 * =====================
2832 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
2836 if (!expect_quic_conn_only(s, &ctx))