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)
326 #if defined(OPENSSL_THREADS)
327 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
331 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
332 qc->is_thread_assisted
333 = (ssl_base->method == OSSL_QUIC_client_thread_method());
336 qc->as_server = 0; /* TODO(QUIC): server support */
337 qc->as_server_state = qc->as_server;
339 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
340 qc->default_ssl_mode = qc->ssl.ctx->mode;
341 qc->default_blocking = 1;
342 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
343 qc->last_error = SSL_ERROR_NONE;
345 if (!create_channel(qc))
348 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
349 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
351 qc_update_reject_policy(qc);
354 * We do not create the default XSO yet. The reason for this is that the
355 * stream ID of the default XSO will depend on whether the stream is client
356 * or server-initiated, which depends on who transmits first. Since we do
357 * not know whether the application will be using a client-transmits-first
358 * or server-transmits-first protocol, we defer default XSO creation until
359 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
360 * we take that as a cue that the client is expecting a server-initiated
361 * stream, and vice versa if SSL_write() is called first.
367 #if defined(OPENSSL_THREADS)
368 ossl_crypto_mutex_free(qc->mutex);
370 ossl_quic_channel_free(qc->ch);
379 void ossl_quic_free(SSL *s)
384 /* We should never be called on anything but a QSO. */
385 if (!expect_quic(s, &ctx))
392 * When a QSSO is freed, the XSO is freed immediately, because the XSO
393 * itself only contains API personality layer data. However the
394 * underlying QUIC_STREAM is not freed immediately but is instead marked
395 * as deleted for later collection.
398 assert(ctx.qc->num_xso > 0);
401 /* If a stream's send part has not been finished, auto-reset it. */
402 if (ctx.xso->stream->sstream != NULL
403 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
404 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
407 /* Do STOP_SENDING for the receive part, if applicable. */
408 if (ctx.xso->stream->rstream != NULL)
409 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
412 /* Update stream state. */
413 ctx.xso->stream->deleted = 1;
414 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
417 is_default = (ctx.xso == ctx.qc->default_xso);
421 * Unref the connection in most cases; the XSO has a ref to the QC and
422 * not vice versa. But for a default XSO, to avoid circular references,
423 * the QC refs the XSO but the XSO does not ref the QC. If we are the
424 * default XSO, we only get here when the QC is being torn down anyway,
425 * so don't call SSL_free(qc) as we are already in it.
428 SSL_free(&ctx.qc->ssl);
430 /* Note: SSL_free calls OPENSSL_free(xso) for us */
435 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
436 * stage, but is freed during the channel free when the whole QSM is freed.
438 if (ctx.qc->default_xso != NULL) {
439 QUIC_XSO *xso = ctx.qc->default_xso;
444 ctx.qc->default_xso = NULL;
447 /* Ensure we have no remaining XSOs. */
448 assert(ctx.qc->num_xso == 0);
450 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
451 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
452 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
453 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
457 ossl_quic_channel_free(ctx.qc->ch);
459 BIO_free(ctx.qc->net_rbio);
460 BIO_free(ctx.qc->net_wbio);
462 /* Note: SSL_free calls OPENSSL_free(qc) for us */
464 SSL_free(ctx.qc->tls);
465 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
466 #if defined(OPENSSL_THREADS)
467 ossl_crypto_mutex_free(&ctx.qc->mutex);
471 /* SSL method init */
472 int ossl_quic_init(SSL *s)
474 /* Same op as SSL_clear, forward the call. */
475 return ossl_quic_clear(s);
478 /* SSL method deinit */
479 void ossl_quic_deinit(SSL *s)
485 int ossl_quic_reset(SSL *s)
489 if (!expect_quic(s, &ctx))
492 /* TODO(QUIC); Currently a no-op. */
497 int ossl_quic_clear(SSL *s)
501 if (!expect_quic(s, &ctx))
504 /* TODO(QUIC): Currently a no-op. */
508 int ossl_quic_conn_set_override_now_cb(SSL *s,
509 OSSL_TIME (*now_cb)(void *arg),
514 if (!expect_quic(s, &ctx))
519 ctx.qc->override_now_cb = now_cb;
520 ctx.qc->override_now_cb_arg = now_cb_arg;
526 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
530 if (!expect_quic(s, &ctx))
533 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
534 if (ctx.qc->is_thread_assisted && ctx.qc->started)
535 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
540 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
542 qc->default_xso_created = 1;
543 qc_update_reject_policy(qc);
547 * Changes default XSO. Allows caller to keep reference to the old default XSO
548 * (if any). Reference to new XSO is transferred from caller.
551 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
559 if (qc->default_xso != xso) {
560 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
562 qc->default_xso = xso;
566 * Changing to not having a default XSO. XSO becomes standalone and
567 * now has a ref to the QC.
569 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
573 * Changing from not having a default XSO to having one. The new XSO
574 * will have had a reference to the QC we need to drop to avoid a
575 * circular reference.
577 * Currently we never change directly from one default XSO to
578 * another, though this function would also still be correct if this
581 assert(*old_xso == NULL);
583 CRYPTO_DOWN_REF(&qc->ssl.references, &refs, &qc->ssl.lock);
589 qc_touch_default_xso(qc);
593 * Changes default XSO, releasing the reference to any previous default XSO.
594 * Reference to new XSO is transferred from caller.
597 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
599 QUIC_XSO *old_xso = NULL;
601 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
604 SSL_free(&old_xso->ssl);
608 * QUIC Front-End I/O API: Network BIO Configuration
609 * =================================================
611 * Handling the different BIOs is difficult:
613 * - It is more or less a requirement that we use non-blocking network I/O;
614 * we need to be able to have timeouts on recv() calls, and make best effort
615 * (non blocking) send() and recv() calls.
617 * The only sensible way to do this is to configure the socket into
618 * non-blocking mode. We could try to do select() before calling send() or
619 * recv() to get a guarantee that the call will not block, but this will
620 * probably run into issues with buggy OSes which generate spurious socket
621 * readiness events. In any case, relying on this to work reliably does not
624 * Timeouts could be handled via setsockopt() socket timeout options, but
625 * this depends on OS support and adds another syscall to every network I/O
626 * operation. It also has obvious thread safety concerns if we want to move
627 * to concurrent use of a single socket at some later date.
629 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
630 * be made non-blocking. However some OSes (e.g. Windows) do not support
631 * this, so we cannot rely on this.
633 * As such, we need to configure any FD in non-blocking mode. This may
634 * confound users who pass a blocking socket to libssl. However, in practice
635 * it would be extremely strange for a user of QUIC to pass an FD to us,
636 * then also try and send receive traffic on the same socket(!). Thus the
637 * impact of this should be limited, and can be documented.
639 * - We support both blocking and non-blocking operation in terms of the API
640 * presented to the user. One prospect is to set the blocking mode based on
641 * whether the socket passed to us was already in blocking mode. However,
642 * Windows has no API for determining if a socket is in blocking mode (!),
643 * therefore this cannot be done portably. Currently therefore we expose an
644 * explicit API call to set this, and default to blocking mode.
646 * - We need to determine our initial destination UDP address. The "natural"
647 * way for a user to do this is to set the peer variable on a BIO_dgram.
648 * However, this has problems because BIO_dgram's peer variable is used for
649 * both transmission and reception. This means it can be constantly being
650 * changed to a malicious value (e.g. if some random unrelated entity on the
651 * network starts sending traffic to us) on every read call. This is not a
652 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
653 * calls only, which do not use this variable. However, we do need to let
654 * the user specify the peer in a 'normal' manner. The compromise here is
655 * that we grab the current peer value set at the time the write BIO is set
656 * and do not read the value again.
658 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
659 * Currently we do this by only supporting non-blocking mode.
664 * Determines what initial destination UDP address we should use, if possible.
665 * If this fails the client must set the destination address manually, or use a
666 * BIO which does not need a destination address.
668 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
670 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
676 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
680 if (!expect_quic(s, &ctx))
683 if (ctx.qc->net_rbio == net_rbio)
686 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
689 BIO_free(ctx.qc->net_rbio);
690 ctx.qc->net_rbio = net_rbio;
693 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
694 * mode as we do not support it for non-pollable BIOs.
696 if (net_rbio != NULL) {
697 BIO_POLL_DESCRIPTOR d = {0};
699 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
700 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
701 ctx.qc->blocking = 0;
702 ctx.qc->default_blocking = 0;
703 ctx.qc->can_poll_net_rbio = 0;
705 ctx.qc->can_poll_net_rbio = 1;
710 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
714 if (!expect_quic(s, &ctx))
717 if (ctx.qc->net_wbio == net_wbio)
720 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
723 BIO_free(ctx.qc->net_wbio);
724 ctx.qc->net_wbio = net_wbio;
726 if (net_wbio != NULL) {
727 BIO_POLL_DESCRIPTOR d = {0};
729 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
730 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
731 ctx.qc->blocking = 0;
732 ctx.qc->default_blocking = 0;
733 ctx.qc->can_poll_net_wbio = 0;
735 ctx.qc->can_poll_net_wbio = 1;
739 * If we do not have a peer address yet, and we have not started trying
740 * to connect yet, try to autodetect one.
742 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
743 && !ctx.qc->started) {
744 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
746 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
748 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
749 &ctx.qc->init_peer_addr);
754 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
758 if (!expect_quic(s, &ctx))
761 return ctx.qc->net_rbio;
764 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
768 if (!expect_quic(s, &ctx))
771 return ctx.qc->net_wbio;
774 int ossl_quic_conn_get_blocking_mode(const SSL *s)
778 if (!expect_quic(s, &ctx))
782 return ctx.xso->blocking;
784 return ctx.qc->blocking;
787 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
791 if (!expect_quic(s, &ctx))
794 /* Cannot enable blocking mode if we do not have pollable FDs. */
796 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
797 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
799 if (!ctx.is_stream) {
801 * If called on a QCSO, update default and connection-level blocking
804 ctx.qc->blocking = (blocking != 0);
805 ctx.qc->default_blocking = ctx.qc->blocking;
810 * If called on a QSSO or QCSO with a default XSO, update blocking
813 ctx.xso->blocking = (blocking != 0);
818 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
819 const BIO_ADDR *peer_addr)
823 if (!expect_quic(s, &ctx))
827 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
830 if (peer_addr == NULL) {
831 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
835 ctx.qc->init_peer_addr = *peer_addr;
840 * QUIC Front-End I/O API: Asynchronous I/O Management
841 * ===================================================
843 * (BIO/)SSL_handle_events => ossl_quic_handle_events
844 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
845 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
849 /* Returns 1 if the connection is being used in blocking mode. */
850 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
855 static int xso_blocking_mode(const QUIC_XSO *xso)
858 && xso->conn->can_poll_net_rbio
859 && xso->conn->can_poll_net_wbio;
862 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
864 int ossl_quic_handle_events(SSL *s)
868 if (!expect_quic(s, &ctx))
872 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
878 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
879 * should next have events handled by the application by calling
880 * SSL_handle_events(). tv is set to 0 if the object should have events handled
881 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
882 * the value of *tv is undefined.
885 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
888 OSSL_TIME deadline = ossl_time_infinite();
890 if (!expect_quic(s, &ctx))
896 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
898 if (ossl_time_is_infinite(deadline)) {
902 * Robustness against faulty applications that don't check *is_infinite;
903 * harmless long timeout.
905 tv->tv_sec = 1000000;
912 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
918 /* SSL_get_rpoll_descriptor */
919 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
923 if (!expect_quic(s, &ctx))
926 if (desc == NULL || ctx.qc->net_rbio == NULL)
929 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
932 /* SSL_get_wpoll_descriptor */
933 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
937 if (!expect_quic(s, &ctx))
940 if (desc == NULL || ctx.qc->net_wbio == NULL)
943 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
946 /* SSL_net_read_desired */
948 int ossl_quic_get_net_read_desired(SSL *s)
953 if (!expect_quic(s, &ctx))
957 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
962 /* SSL_net_write_desired */
964 int ossl_quic_get_net_write_desired(SSL *s)
969 if (!expect_quic(s, &ctx))
973 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
979 * QUIC Front-End I/O API: Connection Lifecycle Operations
980 * =======================================================
982 * SSL_do_handshake => ossl_quic_do_handshake
983 * SSL_set_connect_state => ossl_quic_set_connect_state
984 * SSL_set_accept_state => ossl_quic_set_accept_state
985 * SSL_shutdown => ossl_quic_shutdown
986 * SSL_ctrl => ossl_quic_ctrl
987 * (BIO/)SSL_connect => ossl_quic_connect
988 * (BIO/)SSL_accept => ossl_quic_accept
993 static int quic_shutdown_wait(void *arg)
995 QUIC_CONNECTION *qc = arg;
997 return ossl_quic_channel_is_terminated(qc->ch);
1001 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1002 const SSL_SHUTDOWN_EX_ARGS *args,
1008 if (!expect_quic(s, &ctx))
1012 /* TODO(QUIC): Semantics currently undefined for QSSOs */
1017 ossl_quic_channel_local_close(ctx.qc->ch,
1018 args != NULL ? args->quic_error_code : 0);
1020 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
1022 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1023 quic_unlock(ctx.qc);
1027 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
1028 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1030 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1032 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1033 quic_unlock(ctx.qc);
1038 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1042 if (!expect_quic(s, &ctx))
1047 /* If called on a QCSO, update the default mode. */
1049 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1052 * If we were called on a QSSO or have a default stream, we also update
1055 if (ctx.xso != NULL) {
1056 /* Cannot enable EPW while AON write in progress. */
1057 if (ctx.xso->aon_write_in_progress)
1058 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1060 ctx.xso->ssl_mode |= (uint32_t)larg;
1061 return ctx.xso->ssl_mode;
1064 return ctx.qc->default_ssl_mode;
1065 case SSL_CTRL_CLEAR_MODE:
1067 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1069 if (ctx.xso != NULL) {
1070 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1071 return ctx.xso->ssl_mode;
1074 return ctx.qc->default_ssl_mode;
1076 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1077 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1078 /* This ctrl also needs to be passed to the internal SSL object */
1079 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1081 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1085 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1088 return !is_infinite;
1090 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1091 /* For legacy compatibility with DTLS calls. */
1092 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1094 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1095 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1099 /* SSL_set_connect_state */
1100 void ossl_quic_set_connect_state(SSL *s)
1104 if (!expect_quic(s, &ctx))
1107 /* Cannot be changed after handshake started */
1108 if (ctx.qc->started || ctx.is_stream)
1111 ctx.qc->as_server_state = 0;
1114 /* SSL_set_accept_state */
1115 void ossl_quic_set_accept_state(SSL *s)
1119 if (!expect_quic(s, &ctx))
1122 /* Cannot be changed after handshake started */
1123 if (ctx.qc->started || ctx.is_stream)
1126 ctx.qc->as_server_state = 1;
1129 /* SSL_do_handshake */
1130 struct quic_handshake_wait_args {
1131 QUIC_CONNECTION *qc;
1134 static int quic_handshake_wait(void *arg)
1136 struct quic_handshake_wait_args *args = arg;
1138 if (!ossl_quic_channel_is_active(args->qc->ch))
1141 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1147 static int configure_channel(QUIC_CONNECTION *qc)
1149 assert(qc->ch != NULL);
1151 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1152 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1153 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1160 static int create_channel(QUIC_CONNECTION *qc)
1162 QUIC_CHANNEL_ARGS args = {0};
1164 args.libctx = qc->ssl.ctx->libctx;
1165 args.propq = qc->ssl.ctx->propq;
1166 args.is_server = qc->as_server;
1168 args.mutex = qc->mutex;
1169 args.now_cb = get_time_cb;
1170 args.now_cb_arg = qc;
1172 qc->ch = ossl_quic_channel_new(&args);
1180 * Creates a channel and configures it with the information we have accumulated
1181 * via calls made to us from the application prior to starting a handshake
1185 static int ensure_channel_started(QUIC_CONNECTION *qc)
1188 if (!configure_channel(qc)
1189 || !ossl_quic_channel_start(qc->ch))
1192 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1193 if (qc->is_thread_assisted)
1194 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1203 ossl_quic_channel_free(qc->ch);
1209 static int quic_do_handshake(QCTX *ctx)
1212 QUIC_CONNECTION *qc = ctx->qc;
1214 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1215 /* Handshake already completed. */
1218 if (ossl_quic_channel_is_term_any(qc->ch))
1219 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1221 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1222 /* Peer address must have been set. */
1223 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1224 return -1; /* Non-protocol error */
1227 if (qc->as_server != qc->as_server_state) {
1228 /* TODO(QUIC): Must match the method used to create the QCSO */
1229 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1230 return -1; /* Non-protocol error */
1233 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1234 /* Need read and write BIOs. */
1235 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1236 return -1; /* Non-protocol error */
1240 * Start connection process. Note we may come here multiple times in
1241 * non-blocking mode, which is fine.
1243 if (!ensure_channel_started(qc)) {
1244 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1245 return -1; /* Non-protocol error */
1248 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1249 /* The handshake is now done. */
1252 if (qc_blocking_mode(qc)) {
1253 /* In blocking mode, wait for the handshake to complete. */
1254 struct quic_handshake_wait_args args;
1258 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1259 if (!ossl_quic_channel_is_active(qc->ch)) {
1260 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1261 return 0; /* Shutdown before completion */
1262 } else if (ret <= 0) {
1263 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1264 return -1; /* Non-protocol error */
1267 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1270 /* Try to advance the reactor. */
1271 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1273 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1274 /* The handshake is now done. */
1277 /* Otherwise, indicate that the handshake isn't done yet. */
1278 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1279 return -1; /* Non-protocol error */
1284 int ossl_quic_do_handshake(SSL *s)
1289 if (!expect_quic(s, &ctx))
1294 ret = quic_do_handshake(&ctx);
1295 quic_unlock(ctx.qc);
1300 int ossl_quic_connect(SSL *s)
1302 /* Ensure we are in connect state (no-op if non-idle). */
1303 ossl_quic_set_connect_state(s);
1305 /* Begin or continue the handshake */
1306 return ossl_quic_do_handshake(s);
1310 int ossl_quic_accept(SSL *s)
1312 /* Ensure we are in accept state (no-op if non-idle). */
1313 ossl_quic_set_accept_state(s);
1315 /* Begin or continue the handshake */
1316 return ossl_quic_do_handshake(s);
1320 * QUIC Front-End I/O API: Stream Lifecycle Operations
1321 * ===================================================
1323 * SSL_stream_new => ossl_quic_conn_stream_new
1328 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1329 * default XSO was created. Returns 0 if it was not (e.g. because it already
1330 * exists). Note that this is NOT an error condition.
1333 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1336 QUIC_CONNECTION *qc = ctx->qc;
1338 if (qc->default_xso_created
1339 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1341 * We only do this once. If the user detaches a previously created
1342 * default XSO we don't auto-create another one.
1344 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1346 /* Create a locally-initiated stream. */
1347 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1348 flags |= SSL_STREAM_FLAG_UNI;
1350 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1353 if (qc->default_xso == NULL)
1354 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1356 qc_touch_default_xso(qc);
1360 struct quic_wait_for_stream_args {
1361 QUIC_CONNECTION *qc;
1368 static int quic_wait_for_stream(void *arg)
1370 struct quic_wait_for_stream_args *args = arg;
1372 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1373 /* If connection is torn down due to an error while blocking, stop. */
1374 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1378 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1379 args->expect_id | QUIC_STREAM_DIR_BIDI);
1380 if (args->qs == NULL)
1381 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1382 args->expect_id | QUIC_STREAM_DIR_UNI);
1384 if (args->qs != NULL)
1385 return 1; /* stream now exists */
1387 return 0; /* did not get a stream, keep trying */
1391 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1393 /* Called on a QCSO and we don't currently have a default stream. */
1395 QUIC_CONNECTION *qc = ctx->qc;
1398 struct quic_wait_for_stream_args wargs;
1401 * If default stream functionality is disabled or we already detached
1402 * one, don't make another default stream and just fail.
1404 if (qc->default_xso_created
1405 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1406 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1409 * The peer may have opened a stream since we last ticked. So tick and
1410 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1411 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1412 * first stream created by a peer must have an ordinal of 0.
1414 expect_id = qc->as_server
1415 ? QUIC_STREAM_INITIATOR_CLIENT
1416 : QUIC_STREAM_INITIATOR_SERVER;
1418 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1419 expect_id | QUIC_STREAM_DIR_BIDI);
1421 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1422 expect_id | QUIC_STREAM_DIR_UNI);
1425 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1427 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1432 if (!qc_blocking_mode(qc))
1433 /* Non-blocking mode, so just bail immediately. */
1434 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1436 /* Block until we have a stream. */
1440 wargs.expect_id = expect_id;
1442 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1444 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1445 else if (res < 0 || wargs.qs == NULL)
1446 /* quic_wait_for_stream raised error here */
1453 * We now have qs != NULL. Make it the default stream, creating the
1456 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1457 if (qc->default_xso == NULL)
1458 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1460 qc_touch_default_xso(qc); /* inhibits default XSO */
1465 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1467 QUIC_XSO *xso = NULL;
1469 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1472 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1476 if (!SSL_up_ref(&qc->ssl))
1480 xso->blocking = qc->default_blocking;
1481 xso->ssl_mode = qc->default_ssl_mode;
1482 xso->last_error = SSL_ERROR_NONE;
1494 /* locking depends on need_lock */
1495 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1497 QUIC_CONNECTION *qc = ctx->qc;
1498 QUIC_XSO *xso = NULL;
1499 QUIC_STREAM *qs = NULL;
1500 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1505 if (ossl_quic_channel_is_term_any(qc->ch)) {
1506 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1510 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1514 xso = create_xso_from_stream(qc, qs);
1518 qc_touch_default_xso(qc); /* inhibits default XSO */
1526 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1535 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1539 if (!expect_quic_conn_only(s, &ctx))
1542 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1546 * QUIC Front-End I/O API: Steady-State Operations
1547 * ===============================================
1549 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1550 * is, the functions used during the established phase of a QUIC connection
1551 * (e.g. SSL_read, SSL_write).
1553 * Each function must handle both blocking and non-blocking modes. As discussed
1554 * above, all QUIC I/O is implemented using non-blocking mode internally.
1556 * SSL_get_error => partially implemented by ossl_quic_get_error
1557 * (BIO/)SSL_read => ossl_quic_read
1558 * (BIO/)SSL_write => ossl_quic_write
1559 * SSL_pending => ossl_quic_pending
1560 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1561 * SSL_key_update => ossl_quic_key_update
1565 int ossl_quic_get_error(const SSL *s, int i)
1569 if (!expect_quic(s, &ctx))
1572 return ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1579 * The set of functions below provide the implementation of the public SSL_write
1580 * function. We must handle:
1582 * - both blocking and non-blocking operation at the application level,
1583 * depending on how we are configured;
1585 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1587 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1591 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1594 * We have appended at least one byte to the stream.
1595 * Potentially mark stream as active, depending on FC.
1598 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1604 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1605 * plus we should eventually consider Nagle's algorithm.
1608 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1611 struct quic_write_again_args {
1613 const unsigned char *buf;
1615 size_t total_written;
1619 static int quic_write_again(void *arg)
1621 struct quic_write_again_args *args = arg;
1622 size_t actual_written = 0;
1624 if (!ossl_quic_channel_is_active(args->xso->conn->ch))
1625 /* If connection is torn down due to an error while blocking, stop. */
1628 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1629 args->buf, args->len, &actual_written))
1632 quic_post_write(args->xso, actual_written > 0, 0);
1634 args->buf += actual_written;
1635 args->len -= actual_written;
1636 args->total_written += actual_written;
1639 /* Written everything, done. */
1642 /* Not written everything yet, keep trying. */
1647 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1651 QUIC_XSO *xso = ctx->xso;
1652 struct quic_write_again_args args;
1653 size_t actual_written = 0;
1655 /* First make a best effort to append as much of the data as possible. */
1656 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1658 /* Stream already finished or allocation error. */
1660 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1663 quic_post_write(xso, actual_written > 0, 1);
1665 if (actual_written == len) {
1666 /* Managed to append everything on the first try. */
1667 *written = actual_written;
1672 * We did not manage to append all of the data immediately, so the stream
1673 * buffer has probably filled up. This means we need to block until some of
1677 args.buf = (const unsigned char *)buf + actual_written;
1678 args.len = len - actual_written;
1679 args.total_written = 0;
1681 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1683 if (!ossl_quic_channel_is_active(xso->conn->ch))
1684 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1686 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1689 *written = args.total_written;
1694 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1697 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1698 size_t buf_len, size_t already_sent)
1700 assert(!xso->aon_write_in_progress);
1702 xso->aon_write_in_progress = 1;
1703 xso->aon_buf_base = buf;
1704 xso->aon_buf_pos = already_sent;
1705 xso->aon_buf_len = buf_len;
1708 static void aon_write_finish(QUIC_XSO *xso)
1710 xso->aon_write_in_progress = 0;
1711 xso->aon_buf_base = NULL;
1712 xso->aon_buf_pos = 0;
1713 xso->aon_buf_len = 0;
1717 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
1718 size_t len, size_t *written)
1720 QUIC_XSO *xso = ctx->xso;
1721 const void *actual_buf;
1722 size_t actual_len, actual_written = 0;
1723 int accept_moving_buffer
1724 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1726 if (xso->aon_write_in_progress) {
1728 * We are in the middle of an AON write (i.e., a previous write did not
1729 * manage to append all data to the SSTREAM and we have Enable Partial
1730 * Write (EPW) mode disabled.)
1732 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1733 || len != xso->aon_buf_len)
1735 * Pointer must not have changed if we are not in accept moving
1736 * buffer mode. Length must never change.
1738 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
1740 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1741 actual_len = len - xso->aon_buf_pos;
1742 assert(actual_len > 0);
1748 /* First make a best effort to append as much of the data as possible. */
1749 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1751 /* Stream already finished or allocation error. */
1753 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1756 quic_post_write(xso, actual_written > 0, 1);
1758 if (actual_written == actual_len) {
1759 /* We have sent everything. */
1760 if (xso->aon_write_in_progress) {
1762 * We have sent everything, and we were in the middle of an AON
1763 * write. The output write length is the total length of the AON
1764 * buffer, not however many bytes we managed to write to the stream
1767 *written = xso->aon_buf_len;
1768 aon_write_finish(xso);
1770 *written = actual_written;
1776 if (xso->aon_write_in_progress) {
1778 * AON write is in progress but we have not written everything yet. We
1779 * may have managed to send zero bytes, or some number of bytes less
1780 * than the total remaining which need to be appended during this
1783 xso->aon_buf_pos += actual_written;
1784 assert(xso->aon_buf_pos < xso->aon_buf_len);
1785 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1789 * Not in an existing AON operation but partial write is not enabled, so we
1790 * need to begin a new AON operation. However we needn't bother if we didn't
1791 * actually append anything.
1793 if (actual_written > 0)
1794 aon_write_begin(xso, buf, len, actual_written);
1797 * AON - We do not publicly admit to having appended anything until AON
1801 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1805 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
1808 QUIC_XSO *xso = ctx->xso;
1810 /* Simple best effort operation. */
1811 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
1812 /* Stream already finished or allocation error. */
1814 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1817 quic_post_write(xso, *written > 0, 1);
1822 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1833 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1836 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1838 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1839 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1844 * If we haven't finished the handshake, try to advance it.
1845 * We don't accept writes until the handshake is completed.
1847 if (quic_do_handshake(&ctx) < 1) {
1852 if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
1853 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
1857 if (xso_blocking_mode(ctx.xso))
1858 ret = quic_write_blocking(&ctx, buf, len, written);
1859 else if (partial_write)
1860 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
1862 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
1865 quic_unlock(ctx.qc);
1873 struct quic_read_again_args {
1875 QUIC_STREAM *stream;
1883 static int quic_read_actual(QCTX *ctx,
1884 QUIC_STREAM *stream,
1885 void *buf, size_t buf_len,
1890 QUIC_CONNECTION *qc = ctx->qc;
1892 /* If the receive part of the stream is over, issue EOF. */
1893 if (stream->recv_fin_retired)
1894 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
1896 if (stream->rstream == NULL)
1897 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1900 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1901 bytes_read, &is_fin))
1902 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1905 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1906 bytes_read, &is_fin))
1907 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1911 if (*bytes_read > 0) {
1913 * We have read at least one byte from the stream. Inform stream-level
1914 * RXFC of the retirement of controlled bytes. Update the active stream
1915 * status (the RXFC may now want to emit a frame granting more credit to
1918 OSSL_RTT_INFO rtt_info;
1920 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1922 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
1923 rtt_info.smoothed_rtt))
1924 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1928 stream->recv_fin_retired = 1;
1930 if (*bytes_read > 0)
1931 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1939 static int quic_read_again(void *arg)
1941 struct quic_read_again_args *args = arg;
1943 if (!ossl_quic_channel_is_active(args->ctx->qc->ch)) {
1944 /* If connection is torn down due to an error while blocking, stop. */
1945 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1949 if (!quic_read_actual(args->ctx, args->stream,
1950 args->buf, args->len, args->bytes_read,
1954 if (*args->bytes_read > 0)
1955 /* got at least one byte, the SSL_read op can finish now */
1958 return 0; /* did not read anything, keep trying */
1962 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1966 struct quic_read_again_args args;
1970 if (!expect_quic(s, &ctx))
1975 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1976 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1980 /* If we haven't finished the handshake, try to advance it. */
1981 if (quic_do_handshake(&ctx) < 1) {
1982 ret = 0; /* ossl_quic_do_handshake raised error here */
1986 if (ctx.xso == NULL) {
1988 * Called on a QCSO and we don't currently have a default stream.
1990 * Wait until we get a stream initiated by the peer (blocking mode) or
1991 * fail if we don't have one yet (non-blocking mode).
1993 if (!qc_wait_for_default_xso_for_read(&ctx)) {
1994 ret = 0; /* error already raised here */
1998 ctx.xso = ctx.qc->default_xso;
2001 if (ctx.xso->stream == NULL) {
2002 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2006 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2007 ret = 0; /* quic_read_actual raised error here */
2011 if (*bytes_read > 0) {
2013 * Even though we succeeded, tick the reactor here to ensure we are
2014 * handling other aspects of the QUIC connection.
2016 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2018 } else if (xso_blocking_mode(ctx.xso)) {
2020 * We were not able to read anything immediately, so our stream
2021 * buffer is empty. This means we need to block until we get
2022 * at least one byte.
2025 args.stream = ctx.xso->stream;
2028 args.bytes_read = bytes_read;
2031 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2033 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2035 } else if (res < 0) {
2036 ret = 0; /* quic_read_again raised error here */
2042 /* We did not get any bytes and are not in blocking mode. */
2043 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2047 quic_unlock(ctx.qc);
2051 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2053 return quic_read(s, buf, len, bytes_read, 0);
2056 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2058 return quic_read(s, buf, len, bytes_read, 1);
2066 static size_t ossl_quic_pending_int(const SSL *s)
2072 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2075 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
2076 /* Cannot raise errors here because we are const, just fail. */
2079 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2083 quic_unlock(ctx.qc);
2087 size_t ossl_quic_pending(const SSL *s)
2089 return ossl_quic_pending_int(s);
2092 int ossl_quic_has_pending(const SSL *s)
2094 return ossl_quic_pending_int(s) > 0;
2098 * SSL_stream_conclude
2099 * -------------------
2102 int ossl_quic_conn_stream_conclude(SSL *s)
2107 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2110 qs = ctx.xso->stream;
2112 if (qs == NULL || qs->sstream == NULL) {
2113 quic_unlock(ctx.qc);
2117 if (!ossl_quic_channel_is_active(ctx.qc->ch)
2118 || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2119 quic_unlock(ctx.qc);
2123 ossl_quic_sstream_fin(qs->sstream);
2124 quic_post_write(ctx.xso, 1, 1);
2125 quic_unlock(ctx.qc);
2130 * SSL_inject_net_dgram
2131 * --------------------
2134 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2136 const BIO_ADDR *peer,
2137 const BIO_ADDR *local)
2143 if (!expect_quic(s, &ctx))
2148 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2149 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2151 quic_unlock(ctx.qc);
2156 * SSL_get0_connection
2157 * -------------------
2159 SSL *ossl_quic_get0_connection(SSL *s)
2163 if (!expect_quic(s, &ctx))
2166 return &ctx.qc->ssl;
2170 * SSL_get_stream_type
2171 * -------------------
2173 int ossl_quic_get_stream_type(SSL *s)
2177 if (!expect_quic(s, &ctx))
2178 return SSL_STREAM_TYPE_BIDI;
2180 if (ctx.xso == NULL) {
2182 * If deferred XSO creation has yet to occur, proceed according to the
2183 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2184 * what kind of stream will be created yet, so return BIDI on the basis
2185 * that at this time, the client still has the option of calling
2186 * SSL_read() or SSL_write() first.
2188 if (ctx.qc->default_xso_created
2189 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2190 return SSL_STREAM_TYPE_NONE;
2192 return SSL_STREAM_TYPE_BIDI;
2195 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2196 return SSL_STREAM_TYPE_BIDI;
2198 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2199 return SSL_STREAM_TYPE_READ;
2201 return SSL_STREAM_TYPE_WRITE;
2209 uint64_t ossl_quic_get_stream_id(SSL *s)
2214 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2217 id = ctx.xso->stream->id;
2218 quic_unlock(ctx.qc);
2224 * SSL_set_default_stream_mode
2225 * ---------------------------
2228 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2232 if (!expect_quic_conn_only(s, &ctx))
2237 if (ctx.qc->default_xso_created)
2238 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2239 "too late to change default stream mode");
2242 case SSL_DEFAULT_STREAM_MODE_NONE:
2243 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2244 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2245 ctx.qc->default_stream_mode = mode;
2248 quic_unlock(ctx.qc);
2249 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2250 "bad default stream type");
2253 quic_unlock(ctx.qc);
2262 SSL *ossl_quic_detach_stream(SSL *s)
2265 QUIC_XSO *xso = NULL;
2267 if (!expect_quic_conn_only(s, &ctx))
2272 /* Calling this function inhibits default XSO autocreation. */
2273 /* QC ref to any default XSO is transferred to us and to caller. */
2274 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2276 quic_unlock(ctx.qc);
2278 return xso != NULL ? &xso->ssl : NULL;
2286 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2292 if (!expect_quic_conn_only(conn, &ctx))
2295 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2296 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2297 "stream to attach must be a valid QUIC stream");
2299 xso = (QUIC_XSO *)stream;
2303 if (ctx.qc->default_xso != NULL) {
2304 quic_unlock(ctx.qc);
2305 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2306 "connection already has a default stream");
2310 * It is a caller error for the XSO being attached as a default XSO to have
2311 * more than one ref.
2313 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref, &xso->ssl.lock)) {
2314 quic_unlock(ctx.qc);
2315 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2320 quic_unlock(ctx.qc);
2321 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2322 "stream being attached must have "
2323 "only 1 reference");
2326 /* Caller's reference to the XSO is transferred to us. */
2327 /* Calling this function inhibits default XSO autocreation. */
2328 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2330 quic_unlock(ctx.qc);
2335 * SSL_set_incoming_stream_policy
2336 * ------------------------------
2339 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2341 switch (qc->incoming_stream_policy) {
2342 case SSL_INCOMING_STREAM_POLICY_AUTO:
2343 if ((qc->default_xso == NULL && !qc->default_xso_created)
2344 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2345 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2347 return SSL_INCOMING_STREAM_POLICY_REJECT;
2350 return qc->incoming_stream_policy;
2355 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2357 int policy = qc_get_effective_incoming_stream_policy(qc);
2358 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2360 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2362 qc->incoming_stream_aec);
2366 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2372 if (!expect_quic_conn_only(s, &ctx))
2378 case SSL_INCOMING_STREAM_POLICY_AUTO:
2379 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2380 case SSL_INCOMING_STREAM_POLICY_REJECT:
2381 ctx.qc->incoming_stream_policy = policy;
2382 ctx.qc->incoming_stream_aec = aec;
2390 qc_update_reject_policy(ctx.qc);
2391 quic_unlock(ctx.qc);
2399 struct wait_for_incoming_stream_args {
2405 static int wait_for_incoming_stream(void *arg)
2407 struct wait_for_incoming_stream_args *args = arg;
2408 QUIC_CONNECTION *qc = args->ctx->qc;
2409 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2411 if (!ossl_quic_channel_is_active(qc->ch)) {
2412 /* If connection is torn down due to an error while blocking, stop. */
2413 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2417 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2418 if (args->qs != NULL)
2419 return 1; /* got a stream */
2421 return 0; /* did not get a stream, keep trying */
2425 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2430 QUIC_STREAM_MAP *qsm;
2433 OSSL_RTT_INFO rtt_info;
2435 if (!expect_quic_conn_only(s, &ctx))
2440 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2441 == SSL_INCOMING_STREAM_POLICY_REJECT)
2444 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2446 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2448 if (qc_blocking_mode(ctx.qc)
2449 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2450 struct wait_for_incoming_stream_args args;
2455 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2457 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2459 } else if (ret < 0 || args.qs == NULL) {
2469 xso = create_xso_from_stream(ctx.qc, qs);
2473 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2474 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2475 rtt_info.smoothed_rtt);
2478 /* Calling this function inhibits default XSO autocreation. */
2479 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2482 quic_unlock(ctx.qc);
2487 * SSL_get_accept_stream_queue_len
2488 * -------------------------------
2491 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2496 if (!expect_quic_conn_only(s, &ctx))
2501 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2503 quic_unlock(ctx.qc);
2511 int ossl_quic_stream_reset(SSL *ssl,
2512 const SSL_STREAM_RESET_ARGS *args,
2516 QUIC_STREAM_MAP *qsm;
2518 uint64_t error_code;
2520 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2523 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2524 qs = ctx.xso->stream;
2525 error_code = (args != NULL ? args->quic_error_code : 0);
2527 ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2529 quic_unlock(ctx.qc);
2534 * SSL_get_stream_read_state
2535 * -------------------------
2537 static void quic_classify_stream(QUIC_CONNECTION *qc,
2541 uint64_t *app_error_code)
2544 uint64_t final_size;
2546 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2548 if (app_error_code != NULL)
2549 *app_error_code = UINT64_MAX;
2551 app_error_code = &final_size; /* throw away value */
2553 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2555 * Unidirectional stream and this direction of transmission doesn't
2558 *state = SSL_STREAM_STATE_WRONG_DIR;
2559 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2560 /* Connection already closed. */
2561 *state = SSL_STREAM_STATE_CONN_CLOSED;
2562 } else if (!is_write && qs->recv_fin_retired) {
2563 /* Application has read a FIN. */
2564 *state = SSL_STREAM_STATE_FINISHED;
2565 } else if ((!is_write && qs->stop_sending)
2566 || (is_write && qs->reset_stream)) {
2568 * Stream has been reset locally. FIN takes precedence over this for the
2569 * read case as the application need not care if the stream is reset
2570 * after a FIN has been successfully processed.
2572 *state = SSL_STREAM_STATE_RESET_LOCAL;
2573 *app_error_code = !is_write
2574 ? qs->stop_sending_aec
2575 : qs->reset_stream_aec;
2576 } else if ((!is_write && qs->peer_reset_stream)
2577 || (is_write && qs->peer_stop_sending)) {
2579 * Stream has been reset remotely. */
2580 *state = SSL_STREAM_STATE_RESET_REMOTE;
2581 *app_error_code = !is_write
2582 ? qs->peer_reset_stream_aec
2583 : qs->peer_stop_sending_aec;
2584 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2587 * Stream has been finished. Stream reset takes precedence over this for
2588 * the write case as peer may not have received all data.
2590 *state = SSL_STREAM_STATE_FINISHED;
2592 /* Stream still healthy. */
2593 *state = SSL_STREAM_STATE_OK;
2597 static int quic_get_stream_state(SSL *ssl, int is_write)
2602 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2603 return SSL_STREAM_STATE_NONE;
2605 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2606 quic_unlock(ctx.qc);
2610 int ossl_quic_get_stream_read_state(SSL *ssl)
2612 return quic_get_stream_state(ssl, /*is_write=*/0);
2616 * SSL_get_stream_write_state
2617 * --------------------------
2619 int ossl_quic_get_stream_write_state(SSL *ssl)
2621 return quic_get_stream_state(ssl, /*is_write=*/1);
2625 * SSL_get_stream_read_error_code
2626 * ------------------------------
2628 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2629 uint64_t *app_error_code)
2634 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2637 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2638 &state, app_error_code);
2640 quic_unlock(ctx.qc);
2642 case SSL_STREAM_STATE_FINISHED:
2644 case SSL_STREAM_STATE_RESET_LOCAL:
2645 case SSL_STREAM_STATE_RESET_REMOTE:
2652 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2654 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2658 * SSL_get_stream_write_error_code
2659 * -------------------------------
2661 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2663 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2667 * SSL_get_conn_close_info
2668 * -----------------------
2670 int ossl_quic_get_conn_close_info(SSL *ssl,
2671 SSL_CONN_CLOSE_INFO *info,
2675 const QUIC_TERMINATE_CAUSE *tc;
2677 if (!expect_quic_conn_only(ssl, &ctx))
2680 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
2684 info->error_code = tc->error_code;
2685 info->reason = NULL; /* TODO(QUIC): Wire reason */
2686 info->reason_len = 0;
2687 info->is_local = !tc->remote;
2688 info->is_transport = !tc->app;
2696 int ossl_quic_key_update(SSL *ssl, int update_type)
2700 if (!expect_quic_conn_only(ssl, &ctx))
2703 switch (update_type) {
2704 case SSL_KEY_UPDATE_NOT_REQUESTED:
2706 * QUIC signals peer key update implicily by triggering a local
2707 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
2709 case SSL_KEY_UPDATE_REQUESTED:
2713 /* Unknown type - error. */
2719 /* Attempt to perform a TXKU. */
2720 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
2721 quic_unlock(ctx.qc);
2725 quic_unlock(ctx.qc);
2730 * SSL_get_key_update_type
2731 * -----------------------
2733 int ossl_quic_get_key_update_type(const SSL *s)
2736 * We always handle key updates immediately so a key update is never
2739 return SSL_KEY_UPDATE_NONE;
2743 * QUIC Front-End I/O API: SSL_CTX Management
2744 * ==========================================
2747 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2751 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
2755 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
2759 if (!expect_quic_conn_only(s, &ctx))
2763 case SSL_CTRL_SET_MSG_CALLBACK:
2764 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
2766 /* This callback also needs to be set on the internal SSL object */
2767 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
2770 /* Probably a TLS related ctrl. Defer to our internal SSL object */
2771 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
2775 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
2777 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
2780 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
2782 /* We never do renegotiation. */
2787 * These functions define the TLSv1.2 (and below) ciphers that are supported by
2788 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
2791 int ossl_quic_num_ciphers(void)
2796 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
2801 int ossl_quic_set_ssl_op(SSL *ssl, uint64_t op)
2805 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2808 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
2811 ossl_quic_rstream_set_cleanse(ctx.xso->stream->rstream,
2812 (op & SSL_OP_CLEANSE_PLAINTEXT) != 0);
2815 quic_unlock(ctx.qc);
2820 * Internal Testing APIs
2821 * =====================
2824 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
2828 if (!expect_quic_conn_only(s, &ctx))