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
302 * SSL_set_options => ossl_quic_set_options
303 * SSL_get_options => ossl_quic_get_options
304 * SSL_clear_options => ossl_quic_clear_options
309 SSL *ossl_quic_new(SSL_CTX *ctx)
311 QUIC_CONNECTION *qc = NULL;
312 SSL *ssl_base = NULL;
313 SSL_CONNECTION *sc = NULL;
315 qc = OPENSSL_zalloc(sizeof(*qc));
319 /* Initialise the QUIC_CONNECTION's stub header. */
321 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
326 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
327 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
330 /* override the user_ssl of the inner connection */
331 sc->s3.flags |= TLS1_FLAGS_QUIC;
333 /* Restrict options derived from the SSL_CTX. */
334 sc->options &= OSSL_QUIC_PERMITTED_OPTIONS;
337 #if defined(OPENSSL_THREADS)
338 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
342 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
343 qc->is_thread_assisted
344 = (ssl_base->method == OSSL_QUIC_client_thread_method());
347 qc->as_server = 0; /* TODO(QUIC): server support */
348 qc->as_server_state = qc->as_server;
350 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
351 qc->default_ssl_mode = qc->ssl.ctx->mode;
352 qc->default_blocking = 1;
354 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
355 qc->last_error = SSL_ERROR_NONE;
357 if (!create_channel(qc))
360 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
361 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
363 qc_update_reject_policy(qc);
366 * We do not create the default XSO yet. The reason for this is that the
367 * stream ID of the default XSO will depend on whether the stream is client
368 * or server-initiated, which depends on who transmits first. Since we do
369 * not know whether the application will be using a client-transmits-first
370 * or server-transmits-first protocol, we defer default XSO creation until
371 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
372 * we take that as a cue that the client is expecting a server-initiated
373 * stream, and vice versa if SSL_write() is called first.
379 #if defined(OPENSSL_THREADS)
380 ossl_crypto_mutex_free(qc->mutex);
382 ossl_quic_channel_free(qc->ch);
391 void ossl_quic_free(SSL *s)
396 /* We should never be called on anything but a QSO. */
397 if (!expect_quic(s, &ctx))
404 * When a QSSO is freed, the XSO is freed immediately, because the XSO
405 * itself only contains API personality layer data. However the
406 * underlying QUIC_STREAM is not freed immediately but is instead marked
407 * as deleted for later collection.
410 assert(ctx.qc->num_xso > 0);
413 /* If a stream's send part has not been finished, auto-reset it. */
414 if (ctx.xso->stream->sstream != NULL
415 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
416 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
419 /* Do STOP_SENDING for the receive part, if applicable. */
420 if (ctx.xso->stream->rstream != NULL)
421 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
424 /* Update stream state. */
425 ctx.xso->stream->deleted = 1;
426 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
429 is_default = (ctx.xso == ctx.qc->default_xso);
433 * Unref the connection in most cases; the XSO has a ref to the QC and
434 * not vice versa. But for a default XSO, to avoid circular references,
435 * the QC refs the XSO but the XSO does not ref the QC. If we are the
436 * default XSO, we only get here when the QC is being torn down anyway,
437 * so don't call SSL_free(qc) as we are already in it.
440 SSL_free(&ctx.qc->ssl);
442 /* Note: SSL_free calls OPENSSL_free(xso) for us */
447 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
448 * stage, but is freed during the channel free when the whole QSM is freed.
450 if (ctx.qc->default_xso != NULL) {
451 QUIC_XSO *xso = ctx.qc->default_xso;
456 ctx.qc->default_xso = NULL;
459 /* Ensure we have no remaining XSOs. */
460 assert(ctx.qc->num_xso == 0);
462 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
463 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
464 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
465 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
469 ossl_quic_channel_free(ctx.qc->ch);
471 BIO_free(ctx.qc->net_rbio);
472 BIO_free(ctx.qc->net_wbio);
474 /* Note: SSL_free calls OPENSSL_free(qc) for us */
476 SSL_free(ctx.qc->tls);
477 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
478 #if defined(OPENSSL_THREADS)
479 ossl_crypto_mutex_free(&ctx.qc->mutex);
483 /* SSL method init */
484 int ossl_quic_init(SSL *s)
486 /* Same op as SSL_clear, forward the call. */
487 return ossl_quic_clear(s);
490 /* SSL method deinit */
491 void ossl_quic_deinit(SSL *s)
496 /* SSL_clear (ssl_reset method) */
497 int ossl_quic_reset(SSL *s)
501 if (!expect_quic(s, &ctx))
508 /* ssl_clear method (unused) */
509 int ossl_quic_clear(SSL *s)
513 if (!expect_quic(s, &ctx))
516 /* TODO(QUIC): Currently a no-op. */
520 int ossl_quic_conn_set_override_now_cb(SSL *s,
521 OSSL_TIME (*now_cb)(void *arg),
526 if (!expect_quic(s, &ctx))
531 ctx.qc->override_now_cb = now_cb;
532 ctx.qc->override_now_cb_arg = now_cb_arg;
538 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
542 if (!expect_quic(s, &ctx))
545 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
546 if (ctx.qc->is_thread_assisted && ctx.qc->started)
547 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
552 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
554 qc->default_xso_created = 1;
555 qc_update_reject_policy(qc);
559 * Changes default XSO. Allows caller to keep reference to the old default XSO
560 * (if any). Reference to new XSO is transferred from caller.
563 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
571 if (qc->default_xso != xso) {
572 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
574 qc->default_xso = xso;
578 * Changing to not having a default XSO. XSO becomes standalone and
579 * now has a ref to the QC.
581 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
585 * Changing from not having a default XSO to having one. The new XSO
586 * will have had a reference to the QC we need to drop to avoid a
587 * circular reference.
589 * Currently we never change directly from one default XSO to
590 * another, though this function would also still be correct if this
593 assert(*old_xso == NULL);
595 CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
601 qc_touch_default_xso(qc);
605 * Changes default XSO, releasing the reference to any previous default XSO.
606 * Reference to new XSO is transferred from caller.
609 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
611 QUIC_XSO *old_xso = NULL;
613 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
616 SSL_free(&old_xso->ssl);
619 /* SSL_set_options */
621 static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
626 if (!expect_quic(ssl, &ctx))
632 * Currently most options that we permit are handled in the handshake
635 or_value &= OSSL_QUIC_PERMITTED_OPTIONS;
637 SSL_clear_options(ctx.qc->tls, mask_value);
638 options = SSL_set_options(ctx.qc->tls, or_value);
641 && ctx.xso->stream != NULL
642 && ctx.xso->stream->rstream != NULL)
643 ossl_quic_rstream_set_cleanse(ctx.xso->stream->rstream,
644 (options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
650 uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
652 return quic_mask_or_options(ssl, 0, options);
655 /* SSL_clear_options */
656 uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
658 return quic_mask_or_options(ssl, options, 0);
661 /* SSL_get_options */
662 uint64_t ossl_quic_get_options(const SSL *ssl)
664 return quic_mask_or_options((SSL *)ssl, 0, 0);
668 * QUIC Front-End I/O API: Network BIO Configuration
669 * =================================================
671 * Handling the different BIOs is difficult:
673 * - It is more or less a requirement that we use non-blocking network I/O;
674 * we need to be able to have timeouts on recv() calls, and make best effort
675 * (non blocking) send() and recv() calls.
677 * The only sensible way to do this is to configure the socket into
678 * non-blocking mode. We could try to do select() before calling send() or
679 * recv() to get a guarantee that the call will not block, but this will
680 * probably run into issues with buggy OSes which generate spurious socket
681 * readiness events. In any case, relying on this to work reliably does not
684 * Timeouts could be handled via setsockopt() socket timeout options, but
685 * this depends on OS support and adds another syscall to every network I/O
686 * operation. It also has obvious thread safety concerns if we want to move
687 * to concurrent use of a single socket at some later date.
689 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
690 * be made non-blocking. However some OSes (e.g. Windows) do not support
691 * this, so we cannot rely on this.
693 * As such, we need to configure any FD in non-blocking mode. This may
694 * confound users who pass a blocking socket to libssl. However, in practice
695 * it would be extremely strange for a user of QUIC to pass an FD to us,
696 * then also try and send receive traffic on the same socket(!). Thus the
697 * impact of this should be limited, and can be documented.
699 * - We support both blocking and non-blocking operation in terms of the API
700 * presented to the user. One prospect is to set the blocking mode based on
701 * whether the socket passed to us was already in blocking mode. However,
702 * Windows has no API for determining if a socket is in blocking mode (!),
703 * therefore this cannot be done portably. Currently therefore we expose an
704 * explicit API call to set this, and default to blocking mode.
706 * - We need to determine our initial destination UDP address. The "natural"
707 * way for a user to do this is to set the peer variable on a BIO_dgram.
708 * However, this has problems because BIO_dgram's peer variable is used for
709 * both transmission and reception. This means it can be constantly being
710 * changed to a malicious value (e.g. if some random unrelated entity on the
711 * network starts sending traffic to us) on every read call. This is not a
712 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
713 * calls only, which do not use this variable. However, we do need to let
714 * the user specify the peer in a 'normal' manner. The compromise here is
715 * that we grab the current peer value set at the time the write BIO is set
716 * and do not read the value again.
718 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
719 * Currently we do this by only supporting non-blocking mode.
724 * Determines what initial destination UDP address we should use, if possible.
725 * If this fails the client must set the destination address manually, or use a
726 * BIO which does not need a destination address.
728 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
730 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
736 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
740 if (!expect_quic(s, &ctx))
743 if (ctx.qc->net_rbio == net_rbio)
746 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
749 BIO_free(ctx.qc->net_rbio);
750 ctx.qc->net_rbio = net_rbio;
753 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
754 * mode as we do not support it for non-pollable BIOs.
756 if (net_rbio != NULL) {
757 BIO_POLL_DESCRIPTOR d = {0};
759 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
760 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
761 ctx.qc->blocking = 0;
762 ctx.qc->default_blocking = 0;
763 ctx.qc->can_poll_net_rbio = 0;
765 ctx.qc->can_poll_net_rbio = 1;
770 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
774 if (!expect_quic(s, &ctx))
777 if (ctx.qc->net_wbio == net_wbio)
780 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
783 BIO_free(ctx.qc->net_wbio);
784 ctx.qc->net_wbio = net_wbio;
786 if (net_wbio != NULL) {
787 BIO_POLL_DESCRIPTOR d = {0};
789 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
790 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
791 ctx.qc->blocking = 0;
792 ctx.qc->default_blocking = 0;
793 ctx.qc->can_poll_net_wbio = 0;
795 ctx.qc->can_poll_net_wbio = 1;
799 * If we do not have a peer address yet, and we have not started trying
800 * to connect yet, try to autodetect one.
802 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
803 && !ctx.qc->started) {
804 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
806 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
808 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
809 &ctx.qc->init_peer_addr);
814 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
818 if (!expect_quic(s, &ctx))
821 return ctx.qc->net_rbio;
824 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
828 if (!expect_quic(s, &ctx))
831 return ctx.qc->net_wbio;
834 int ossl_quic_conn_get_blocking_mode(const SSL *s)
838 if (!expect_quic(s, &ctx))
842 return ctx.xso->blocking;
844 return ctx.qc->blocking;
847 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
851 if (!expect_quic(s, &ctx))
854 /* Cannot enable blocking mode if we do not have pollable FDs. */
856 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
857 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
859 if (!ctx.is_stream) {
861 * If called on a QCSO, update default and connection-level blocking
864 ctx.qc->blocking = (blocking != 0);
865 ctx.qc->default_blocking = ctx.qc->blocking;
870 * If called on a QSSO or QCSO with a default XSO, update blocking
873 ctx.xso->blocking = (blocking != 0);
878 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
879 const BIO_ADDR *peer_addr)
883 if (!expect_quic(s, &ctx))
887 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
890 if (peer_addr == NULL) {
891 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
895 ctx.qc->init_peer_addr = *peer_addr;
900 * QUIC Front-End I/O API: Asynchronous I/O Management
901 * ===================================================
903 * (BIO/)SSL_handle_events => ossl_quic_handle_events
904 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
905 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
909 /* Returns 1 if the connection is being used in blocking mode. */
910 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
915 static int xso_blocking_mode(const QUIC_XSO *xso)
918 && xso->conn->can_poll_net_rbio
919 && xso->conn->can_poll_net_wbio;
922 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
924 int ossl_quic_handle_events(SSL *s)
928 if (!expect_quic(s, &ctx))
932 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
938 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
939 * should next have events handled by the application by calling
940 * SSL_handle_events(). tv is set to 0 if the object should have events handled
941 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
942 * the value of *tv is undefined.
945 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
948 OSSL_TIME deadline = ossl_time_infinite();
950 if (!expect_quic(s, &ctx))
956 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
958 if (ossl_time_is_infinite(deadline)) {
962 * Robustness against faulty applications that don't check *is_infinite;
963 * harmless long timeout.
965 tv->tv_sec = 1000000;
972 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
978 /* SSL_get_rpoll_descriptor */
979 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
983 if (!expect_quic(s, &ctx))
986 if (desc == NULL || ctx.qc->net_rbio == NULL)
989 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
992 /* SSL_get_wpoll_descriptor */
993 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
997 if (!expect_quic(s, &ctx))
1000 if (desc == NULL || ctx.qc->net_wbio == NULL)
1003 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
1006 /* SSL_net_read_desired */
1008 int ossl_quic_get_net_read_desired(SSL *s)
1013 if (!expect_quic(s, &ctx))
1017 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1018 quic_unlock(ctx.qc);
1022 /* SSL_net_write_desired */
1024 int ossl_quic_get_net_write_desired(SSL *s)
1029 if (!expect_quic(s, &ctx))
1033 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1034 quic_unlock(ctx.qc);
1039 * QUIC Front-End I/O API: Connection Lifecycle Operations
1040 * =======================================================
1042 * SSL_do_handshake => ossl_quic_do_handshake
1043 * SSL_set_connect_state => ossl_quic_set_connect_state
1044 * SSL_set_accept_state => ossl_quic_set_accept_state
1045 * SSL_shutdown => ossl_quic_shutdown
1046 * SSL_ctrl => ossl_quic_ctrl
1047 * (BIO/)SSL_connect => ossl_quic_connect
1048 * (BIO/)SSL_accept => ossl_quic_accept
1053 static int quic_shutdown_wait(void *arg)
1055 QUIC_CONNECTION *qc = arg;
1057 return ossl_quic_channel_is_terminated(qc->ch);
1061 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1062 const SSL_SHUTDOWN_EX_ARGS *args,
1068 if (!expect_quic(s, &ctx))
1072 /* TODO(QUIC): Semantics currently undefined for QSSOs */
1077 ossl_quic_channel_local_close(ctx.qc->ch,
1078 args != NULL ? args->quic_error_code : 0);
1080 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
1082 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1083 quic_unlock(ctx.qc);
1087 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
1088 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1090 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1092 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1093 quic_unlock(ctx.qc);
1098 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1102 if (!expect_quic(s, &ctx))
1107 /* If called on a QCSO, update the default mode. */
1109 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1112 * If we were called on a QSSO or have a default stream, we also update
1115 if (ctx.xso != NULL) {
1116 /* Cannot enable EPW while AON write in progress. */
1117 if (ctx.xso->aon_write_in_progress)
1118 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1120 ctx.xso->ssl_mode |= (uint32_t)larg;
1121 return ctx.xso->ssl_mode;
1124 return ctx.qc->default_ssl_mode;
1125 case SSL_CTRL_CLEAR_MODE:
1127 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1129 if (ctx.xso != NULL) {
1130 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1131 return ctx.xso->ssl_mode;
1134 return ctx.qc->default_ssl_mode;
1136 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1137 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1138 /* This ctrl also needs to be passed to the internal SSL object */
1139 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1141 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1145 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1148 return !is_infinite;
1150 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1151 /* For legacy compatibility with DTLS calls. */
1152 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1154 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1155 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1159 /* SSL_set_connect_state */
1160 void ossl_quic_set_connect_state(SSL *s)
1164 if (!expect_quic(s, &ctx))
1167 /* Cannot be changed after handshake started */
1168 if (ctx.qc->started || ctx.is_stream)
1171 ctx.qc->as_server_state = 0;
1174 /* SSL_set_accept_state */
1175 void ossl_quic_set_accept_state(SSL *s)
1179 if (!expect_quic(s, &ctx))
1182 /* Cannot be changed after handshake started */
1183 if (ctx.qc->started || ctx.is_stream)
1186 ctx.qc->as_server_state = 1;
1189 /* SSL_do_handshake */
1190 struct quic_handshake_wait_args {
1191 QUIC_CONNECTION *qc;
1194 static int quic_handshake_wait(void *arg)
1196 struct quic_handshake_wait_args *args = arg;
1198 if (!ossl_quic_channel_is_active(args->qc->ch))
1201 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1207 static int configure_channel(QUIC_CONNECTION *qc)
1209 assert(qc->ch != NULL);
1211 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1212 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1213 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1220 static int create_channel(QUIC_CONNECTION *qc)
1222 QUIC_CHANNEL_ARGS args = {0};
1224 args.libctx = qc->ssl.ctx->libctx;
1225 args.propq = qc->ssl.ctx->propq;
1226 args.is_server = qc->as_server;
1228 args.mutex = qc->mutex;
1229 args.now_cb = get_time_cb;
1230 args.now_cb_arg = qc;
1232 qc->ch = ossl_quic_channel_new(&args);
1240 * Creates a channel and configures it with the information we have accumulated
1241 * via calls made to us from the application prior to starting a handshake
1245 static int ensure_channel_started(QUIC_CONNECTION *qc)
1248 if (!configure_channel(qc)
1249 || !ossl_quic_channel_start(qc->ch))
1252 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1253 if (qc->is_thread_assisted)
1254 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1263 ossl_quic_channel_free(qc->ch);
1269 static int quic_do_handshake(QCTX *ctx)
1272 QUIC_CONNECTION *qc = ctx->qc;
1274 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1275 /* Handshake already completed. */
1278 if (ossl_quic_channel_is_term_any(qc->ch))
1279 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1281 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1282 /* Peer address must have been set. */
1283 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1284 return -1; /* Non-protocol error */
1287 if (qc->as_server != qc->as_server_state) {
1288 /* TODO(QUIC): Must match the method used to create the QCSO */
1289 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1290 return -1; /* Non-protocol error */
1293 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1294 /* Need read and write BIOs. */
1295 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1296 return -1; /* Non-protocol error */
1300 * Start connection process. Note we may come here multiple times in
1301 * non-blocking mode, which is fine.
1303 if (!ensure_channel_started(qc)) {
1304 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1305 return -1; /* Non-protocol error */
1308 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1309 /* The handshake is now done. */
1312 if (qc_blocking_mode(qc)) {
1313 /* In blocking mode, wait for the handshake to complete. */
1314 struct quic_handshake_wait_args args;
1318 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1319 if (!ossl_quic_channel_is_active(qc->ch)) {
1320 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1321 return 0; /* Shutdown before completion */
1322 } else if (ret <= 0) {
1323 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1324 return -1; /* Non-protocol error */
1327 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1330 /* Try to advance the reactor. */
1331 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1333 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1334 /* The handshake is now done. */
1337 /* Otherwise, indicate that the handshake isn't done yet. */
1338 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1339 return -1; /* Non-protocol error */
1344 int ossl_quic_do_handshake(SSL *s)
1349 if (!expect_quic(s, &ctx))
1354 ret = quic_do_handshake(&ctx);
1355 quic_unlock(ctx.qc);
1360 int ossl_quic_connect(SSL *s)
1362 /* Ensure we are in connect state (no-op if non-idle). */
1363 ossl_quic_set_connect_state(s);
1365 /* Begin or continue the handshake */
1366 return ossl_quic_do_handshake(s);
1370 int ossl_quic_accept(SSL *s)
1372 /* Ensure we are in accept state (no-op if non-idle). */
1373 ossl_quic_set_accept_state(s);
1375 /* Begin or continue the handshake */
1376 return ossl_quic_do_handshake(s);
1380 * QUIC Front-End I/O API: Stream Lifecycle Operations
1381 * ===================================================
1383 * SSL_stream_new => ossl_quic_conn_stream_new
1388 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1389 * default XSO was created. Returns 0 if it was not (e.g. because it already
1390 * exists). Note that this is NOT an error condition.
1393 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1396 QUIC_CONNECTION *qc = ctx->qc;
1398 if (qc->default_xso_created
1399 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1401 * We only do this once. If the user detaches a previously created
1402 * default XSO we don't auto-create another one.
1404 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1406 /* Create a locally-initiated stream. */
1407 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1408 flags |= SSL_STREAM_FLAG_UNI;
1410 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1413 if (qc->default_xso == NULL)
1414 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1416 qc_touch_default_xso(qc);
1420 struct quic_wait_for_stream_args {
1421 QUIC_CONNECTION *qc;
1428 static int quic_wait_for_stream(void *arg)
1430 struct quic_wait_for_stream_args *args = arg;
1432 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1433 /* If connection is torn down due to an error while blocking, stop. */
1434 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1438 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1439 args->expect_id | QUIC_STREAM_DIR_BIDI);
1440 if (args->qs == NULL)
1441 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1442 args->expect_id | QUIC_STREAM_DIR_UNI);
1444 if (args->qs != NULL)
1445 return 1; /* stream now exists */
1447 return 0; /* did not get a stream, keep trying */
1451 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1453 /* Called on a QCSO and we don't currently have a default stream. */
1455 QUIC_CONNECTION *qc = ctx->qc;
1458 struct quic_wait_for_stream_args wargs;
1461 * If default stream functionality is disabled or we already detached
1462 * one, don't make another default stream and just fail.
1464 if (qc->default_xso_created
1465 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1466 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1469 * The peer may have opened a stream since we last ticked. So tick and
1470 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1471 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1472 * first stream created by a peer must have an ordinal of 0.
1474 expect_id = qc->as_server
1475 ? QUIC_STREAM_INITIATOR_CLIENT
1476 : QUIC_STREAM_INITIATOR_SERVER;
1478 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1479 expect_id | QUIC_STREAM_DIR_BIDI);
1481 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1482 expect_id | QUIC_STREAM_DIR_UNI);
1485 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1487 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1492 if (!qc_blocking_mode(qc))
1493 /* Non-blocking mode, so just bail immediately. */
1494 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1496 /* Block until we have a stream. */
1500 wargs.expect_id = expect_id;
1502 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1504 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1505 else if (res < 0 || wargs.qs == NULL)
1506 /* quic_wait_for_stream raised error here */
1513 * We now have qs != NULL. Make it the default stream, creating the
1516 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1517 if (qc->default_xso == NULL)
1518 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1520 qc_touch_default_xso(qc); /* inhibits default XSO */
1525 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1527 QUIC_XSO *xso = NULL;
1529 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1532 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1536 if (!SSL_up_ref(&qc->ssl))
1540 xso->blocking = qc->default_blocking;
1541 xso->ssl_mode = qc->default_ssl_mode;
1542 xso->last_error = SSL_ERROR_NONE;
1554 /* locking depends on need_lock */
1555 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1557 QUIC_CONNECTION *qc = ctx->qc;
1558 QUIC_XSO *xso = NULL;
1559 QUIC_STREAM *qs = NULL;
1560 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1565 if (ossl_quic_channel_is_term_any(qc->ch)) {
1566 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1570 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1574 xso = create_xso_from_stream(qc, qs);
1578 qc_touch_default_xso(qc); /* inhibits default XSO */
1586 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1595 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1599 if (!expect_quic_conn_only(s, &ctx))
1602 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1606 * QUIC Front-End I/O API: Steady-State Operations
1607 * ===============================================
1609 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1610 * is, the functions used during the established phase of a QUIC connection
1611 * (e.g. SSL_read, SSL_write).
1613 * Each function must handle both blocking and non-blocking modes. As discussed
1614 * above, all QUIC I/O is implemented using non-blocking mode internally.
1616 * SSL_get_error => partially implemented by ossl_quic_get_error
1617 * (BIO/)SSL_read => ossl_quic_read
1618 * (BIO/)SSL_write => ossl_quic_write
1619 * SSL_pending => ossl_quic_pending
1620 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1621 * SSL_key_update => ossl_quic_key_update
1625 int ossl_quic_get_error(const SSL *s, int i)
1629 if (!expect_quic(s, &ctx))
1632 return ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1639 * The set of functions below provide the implementation of the public SSL_write
1640 * function. We must handle:
1642 * - both blocking and non-blocking operation at the application level,
1643 * depending on how we are configured;
1645 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1647 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1651 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1654 * We have appended at least one byte to the stream.
1655 * Potentially mark stream as active, depending on FC.
1658 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1664 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1665 * plus we should eventually consider Nagle's algorithm.
1668 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1671 struct quic_write_again_args {
1673 const unsigned char *buf;
1675 size_t total_written;
1679 static int quic_write_again(void *arg)
1681 struct quic_write_again_args *args = arg;
1682 size_t actual_written = 0;
1684 if (!ossl_quic_channel_is_active(args->xso->conn->ch))
1685 /* If connection is torn down due to an error while blocking, stop. */
1688 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1689 args->buf, args->len, &actual_written))
1692 quic_post_write(args->xso, actual_written > 0, 0);
1694 args->buf += actual_written;
1695 args->len -= actual_written;
1696 args->total_written += actual_written;
1699 /* Written everything, done. */
1702 /* Not written everything yet, keep trying. */
1707 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1711 QUIC_XSO *xso = ctx->xso;
1712 struct quic_write_again_args args;
1713 size_t actual_written = 0;
1715 /* First make a best effort to append as much of the data as possible. */
1716 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1718 /* Stream already finished or allocation error. */
1720 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1723 quic_post_write(xso, actual_written > 0, 1);
1725 if (actual_written == len) {
1726 /* Managed to append everything on the first try. */
1727 *written = actual_written;
1732 * We did not manage to append all of the data immediately, so the stream
1733 * buffer has probably filled up. This means we need to block until some of
1737 args.buf = (const unsigned char *)buf + actual_written;
1738 args.len = len - actual_written;
1739 args.total_written = 0;
1741 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1743 if (!ossl_quic_channel_is_active(xso->conn->ch))
1744 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1746 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1749 *written = args.total_written;
1754 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1757 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1758 size_t buf_len, size_t already_sent)
1760 assert(!xso->aon_write_in_progress);
1762 xso->aon_write_in_progress = 1;
1763 xso->aon_buf_base = buf;
1764 xso->aon_buf_pos = already_sent;
1765 xso->aon_buf_len = buf_len;
1768 static void aon_write_finish(QUIC_XSO *xso)
1770 xso->aon_write_in_progress = 0;
1771 xso->aon_buf_base = NULL;
1772 xso->aon_buf_pos = 0;
1773 xso->aon_buf_len = 0;
1777 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
1778 size_t len, size_t *written)
1780 QUIC_XSO *xso = ctx->xso;
1781 const void *actual_buf;
1782 size_t actual_len, actual_written = 0;
1783 int accept_moving_buffer
1784 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1786 if (xso->aon_write_in_progress) {
1788 * We are in the middle of an AON write (i.e., a previous write did not
1789 * manage to append all data to the SSTREAM and we have Enable Partial
1790 * Write (EPW) mode disabled.)
1792 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1793 || len != xso->aon_buf_len)
1795 * Pointer must not have changed if we are not in accept moving
1796 * buffer mode. Length must never change.
1798 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
1800 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1801 actual_len = len - xso->aon_buf_pos;
1802 assert(actual_len > 0);
1808 /* First make a best effort to append as much of the data as possible. */
1809 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1811 /* Stream already finished or allocation error. */
1813 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1816 quic_post_write(xso, actual_written > 0, 1);
1818 if (actual_written == actual_len) {
1819 /* We have sent everything. */
1820 if (xso->aon_write_in_progress) {
1822 * We have sent everything, and we were in the middle of an AON
1823 * write. The output write length is the total length of the AON
1824 * buffer, not however many bytes we managed to write to the stream
1827 *written = xso->aon_buf_len;
1828 aon_write_finish(xso);
1830 *written = actual_written;
1836 if (xso->aon_write_in_progress) {
1838 * AON write is in progress but we have not written everything yet. We
1839 * may have managed to send zero bytes, or some number of bytes less
1840 * than the total remaining which need to be appended during this
1843 xso->aon_buf_pos += actual_written;
1844 assert(xso->aon_buf_pos < xso->aon_buf_len);
1845 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1849 * Not in an existing AON operation but partial write is not enabled, so we
1850 * need to begin a new AON operation. However we needn't bother if we didn't
1851 * actually append anything.
1853 if (actual_written > 0)
1854 aon_write_begin(xso, buf, len, actual_written);
1857 * AON - We do not publicly admit to having appended anything until AON
1861 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1865 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
1868 QUIC_XSO *xso = ctx->xso;
1870 /* Simple best effort operation. */
1871 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
1872 /* Stream already finished or allocation error. */
1874 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1877 quic_post_write(xso, *written > 0, 1);
1882 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1893 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1896 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1898 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1899 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1904 * If we haven't finished the handshake, try to advance it.
1905 * We don't accept writes until the handshake is completed.
1907 if (quic_do_handshake(&ctx) < 1) {
1912 if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
1913 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
1917 if (xso_blocking_mode(ctx.xso))
1918 ret = quic_write_blocking(&ctx, buf, len, written);
1919 else if (partial_write)
1920 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
1922 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
1925 quic_unlock(ctx.qc);
1933 struct quic_read_again_args {
1935 QUIC_STREAM *stream;
1943 static int quic_read_actual(QCTX *ctx,
1944 QUIC_STREAM *stream,
1945 void *buf, size_t buf_len,
1950 QUIC_CONNECTION *qc = ctx->qc;
1952 /* If the receive part of the stream is over, issue EOF. */
1953 if (stream->recv_fin_retired)
1954 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
1956 if (stream->rstream == NULL)
1957 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1960 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1961 bytes_read, &is_fin))
1962 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1965 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1966 bytes_read, &is_fin))
1967 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1971 if (*bytes_read > 0) {
1973 * We have read at least one byte from the stream. Inform stream-level
1974 * RXFC of the retirement of controlled bytes. Update the active stream
1975 * status (the RXFC may now want to emit a frame granting more credit to
1978 OSSL_RTT_INFO rtt_info;
1980 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1982 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
1983 rtt_info.smoothed_rtt))
1984 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1988 stream->recv_fin_retired = 1;
1990 if (*bytes_read > 0)
1991 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1999 static int quic_read_again(void *arg)
2001 struct quic_read_again_args *args = arg;
2003 if (!ossl_quic_channel_is_active(args->ctx->qc->ch)) {
2004 /* If connection is torn down due to an error while blocking, stop. */
2005 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2009 if (!quic_read_actual(args->ctx, args->stream,
2010 args->buf, args->len, args->bytes_read,
2014 if (*args->bytes_read > 0)
2015 /* got at least one byte, the SSL_read op can finish now */
2018 return 0; /* did not read anything, keep trying */
2022 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2026 struct quic_read_again_args args;
2030 if (!expect_quic(s, &ctx))
2035 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
2036 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2040 /* If we haven't finished the handshake, try to advance it. */
2041 if (quic_do_handshake(&ctx) < 1) {
2042 ret = 0; /* ossl_quic_do_handshake raised error here */
2046 if (ctx.xso == NULL) {
2048 * Called on a QCSO and we don't currently have a default stream.
2050 * Wait until we get a stream initiated by the peer (blocking mode) or
2051 * fail if we don't have one yet (non-blocking mode).
2053 if (!qc_wait_for_default_xso_for_read(&ctx)) {
2054 ret = 0; /* error already raised here */
2058 ctx.xso = ctx.qc->default_xso;
2061 if (ctx.xso->stream == NULL) {
2062 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2066 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2067 ret = 0; /* quic_read_actual raised error here */
2071 if (*bytes_read > 0) {
2073 * Even though we succeeded, tick the reactor here to ensure we are
2074 * handling other aspects of the QUIC connection.
2076 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2078 } else if (xso_blocking_mode(ctx.xso)) {
2080 * We were not able to read anything immediately, so our stream
2081 * buffer is empty. This means we need to block until we get
2082 * at least one byte.
2085 args.stream = ctx.xso->stream;
2088 args.bytes_read = bytes_read;
2091 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2093 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2095 } else if (res < 0) {
2096 ret = 0; /* quic_read_again raised error here */
2102 /* We did not get any bytes and are not in blocking mode. */
2103 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2107 quic_unlock(ctx.qc);
2111 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2113 return quic_read(s, buf, len, bytes_read, 0);
2116 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2118 return quic_read(s, buf, len, bytes_read, 1);
2127 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2133 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2136 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
2137 /* Cannot raise errors here because we are const, just fail. */
2140 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2143 if (avail == 0 && check_channel && ossl_quic_channel_has_pending(ctx.qc->ch))
2147 quic_unlock(ctx.qc);
2151 size_t ossl_quic_pending(const SSL *s)
2153 return ossl_quic_pending_int(s, /*check_channel=*/0);
2156 int ossl_quic_has_pending(const SSL *s)
2158 /* Do we have app-side pending data or pending URXEs or RXEs? */
2159 return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2163 * SSL_stream_conclude
2164 * -------------------
2167 int ossl_quic_conn_stream_conclude(SSL *s)
2172 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2175 qs = ctx.xso->stream;
2177 if (qs == NULL || qs->sstream == NULL) {
2178 quic_unlock(ctx.qc);
2182 if (!ossl_quic_channel_is_active(ctx.qc->ch)
2183 || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2184 quic_unlock(ctx.qc);
2188 ossl_quic_sstream_fin(qs->sstream);
2189 quic_post_write(ctx.xso, 1, 1);
2190 quic_unlock(ctx.qc);
2195 * SSL_inject_net_dgram
2196 * --------------------
2199 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2201 const BIO_ADDR *peer,
2202 const BIO_ADDR *local)
2208 if (!expect_quic(s, &ctx))
2213 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2214 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2216 quic_unlock(ctx.qc);
2221 * SSL_get0_connection
2222 * -------------------
2224 SSL *ossl_quic_get0_connection(SSL *s)
2228 if (!expect_quic(s, &ctx))
2231 return &ctx.qc->ssl;
2235 * SSL_get_stream_type
2236 * -------------------
2238 int ossl_quic_get_stream_type(SSL *s)
2242 if (!expect_quic(s, &ctx))
2243 return SSL_STREAM_TYPE_BIDI;
2245 if (ctx.xso == NULL) {
2247 * If deferred XSO creation has yet to occur, proceed according to the
2248 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2249 * what kind of stream will be created yet, so return BIDI on the basis
2250 * that at this time, the client still has the option of calling
2251 * SSL_read() or SSL_write() first.
2253 if (ctx.qc->default_xso_created
2254 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2255 return SSL_STREAM_TYPE_NONE;
2257 return SSL_STREAM_TYPE_BIDI;
2260 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2261 return SSL_STREAM_TYPE_BIDI;
2263 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2264 return SSL_STREAM_TYPE_READ;
2266 return SSL_STREAM_TYPE_WRITE;
2274 uint64_t ossl_quic_get_stream_id(SSL *s)
2279 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2282 id = ctx.xso->stream->id;
2283 quic_unlock(ctx.qc);
2289 * SSL_set_default_stream_mode
2290 * ---------------------------
2293 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2297 if (!expect_quic_conn_only(s, &ctx))
2302 if (ctx.qc->default_xso_created)
2303 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2304 "too late to change default stream mode");
2307 case SSL_DEFAULT_STREAM_MODE_NONE:
2308 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2309 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2310 ctx.qc->default_stream_mode = mode;
2313 quic_unlock(ctx.qc);
2314 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2315 "bad default stream type");
2318 quic_unlock(ctx.qc);
2327 SSL *ossl_quic_detach_stream(SSL *s)
2330 QUIC_XSO *xso = NULL;
2332 if (!expect_quic_conn_only(s, &ctx))
2337 /* Calling this function inhibits default XSO autocreation. */
2338 /* QC ref to any default XSO is transferred to us and to caller. */
2339 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2341 quic_unlock(ctx.qc);
2343 return xso != NULL ? &xso->ssl : NULL;
2351 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2357 if (!expect_quic_conn_only(conn, &ctx))
2360 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2361 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2362 "stream to attach must be a valid QUIC stream");
2364 xso = (QUIC_XSO *)stream;
2368 if (ctx.qc->default_xso != NULL) {
2369 quic_unlock(ctx.qc);
2370 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2371 "connection already has a default stream");
2375 * It is a caller error for the XSO being attached as a default XSO to have
2376 * more than one ref.
2378 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
2379 quic_unlock(ctx.qc);
2380 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2385 quic_unlock(ctx.qc);
2386 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2387 "stream being attached must have "
2388 "only 1 reference");
2391 /* Caller's reference to the XSO is transferred to us. */
2392 /* Calling this function inhibits default XSO autocreation. */
2393 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2395 quic_unlock(ctx.qc);
2400 * SSL_set_incoming_stream_policy
2401 * ------------------------------
2404 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2406 switch (qc->incoming_stream_policy) {
2407 case SSL_INCOMING_STREAM_POLICY_AUTO:
2408 if ((qc->default_xso == NULL && !qc->default_xso_created)
2409 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2410 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2412 return SSL_INCOMING_STREAM_POLICY_REJECT;
2415 return qc->incoming_stream_policy;
2420 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2422 int policy = qc_get_effective_incoming_stream_policy(qc);
2423 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2425 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2427 qc->incoming_stream_aec);
2431 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2437 if (!expect_quic_conn_only(s, &ctx))
2443 case SSL_INCOMING_STREAM_POLICY_AUTO:
2444 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2445 case SSL_INCOMING_STREAM_POLICY_REJECT:
2446 ctx.qc->incoming_stream_policy = policy;
2447 ctx.qc->incoming_stream_aec = aec;
2455 qc_update_reject_policy(ctx.qc);
2456 quic_unlock(ctx.qc);
2464 struct wait_for_incoming_stream_args {
2470 static int wait_for_incoming_stream(void *arg)
2472 struct wait_for_incoming_stream_args *args = arg;
2473 QUIC_CONNECTION *qc = args->ctx->qc;
2474 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2476 if (!ossl_quic_channel_is_active(qc->ch)) {
2477 /* If connection is torn down due to an error while blocking, stop. */
2478 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2482 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2483 if (args->qs != NULL)
2484 return 1; /* got a stream */
2486 return 0; /* did not get a stream, keep trying */
2490 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2495 QUIC_STREAM_MAP *qsm;
2498 OSSL_RTT_INFO rtt_info;
2500 if (!expect_quic_conn_only(s, &ctx))
2505 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2506 == SSL_INCOMING_STREAM_POLICY_REJECT)
2509 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2511 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2513 if (qc_blocking_mode(ctx.qc)
2514 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2515 struct wait_for_incoming_stream_args args;
2520 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2522 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2524 } else if (ret < 0 || args.qs == NULL) {
2534 xso = create_xso_from_stream(ctx.qc, qs);
2538 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2539 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2540 rtt_info.smoothed_rtt);
2543 /* Calling this function inhibits default XSO autocreation. */
2544 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2547 quic_unlock(ctx.qc);
2552 * SSL_get_accept_stream_queue_len
2553 * -------------------------------
2556 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2561 if (!expect_quic_conn_only(s, &ctx))
2566 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2568 quic_unlock(ctx.qc);
2576 int ossl_quic_stream_reset(SSL *ssl,
2577 const SSL_STREAM_RESET_ARGS *args,
2581 QUIC_STREAM_MAP *qsm;
2583 uint64_t error_code;
2585 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2588 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2589 qs = ctx.xso->stream;
2590 error_code = (args != NULL ? args->quic_error_code : 0);
2592 ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2594 quic_unlock(ctx.qc);
2599 * SSL_get_stream_read_state
2600 * -------------------------
2602 static void quic_classify_stream(QUIC_CONNECTION *qc,
2606 uint64_t *app_error_code)
2609 uint64_t final_size;
2611 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2613 if (app_error_code != NULL)
2614 *app_error_code = UINT64_MAX;
2616 app_error_code = &final_size; /* throw away value */
2618 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2620 * Unidirectional stream and this direction of transmission doesn't
2623 *state = SSL_STREAM_STATE_WRONG_DIR;
2624 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2625 /* Connection already closed. */
2626 *state = SSL_STREAM_STATE_CONN_CLOSED;
2627 } else if (!is_write && qs->recv_fin_retired) {
2628 /* Application has read a FIN. */
2629 *state = SSL_STREAM_STATE_FINISHED;
2630 } else if ((!is_write && qs->stop_sending)
2631 || (is_write && qs->reset_stream)) {
2633 * Stream has been reset locally. FIN takes precedence over this for the
2634 * read case as the application need not care if the stream is reset
2635 * after a FIN has been successfully processed.
2637 *state = SSL_STREAM_STATE_RESET_LOCAL;
2638 *app_error_code = !is_write
2639 ? qs->stop_sending_aec
2640 : qs->reset_stream_aec;
2641 } else if ((!is_write && qs->peer_reset_stream)
2642 || (is_write && qs->peer_stop_sending)) {
2644 * Stream has been reset remotely. */
2645 *state = SSL_STREAM_STATE_RESET_REMOTE;
2646 *app_error_code = !is_write
2647 ? qs->peer_reset_stream_aec
2648 : qs->peer_stop_sending_aec;
2649 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2652 * Stream has been finished. Stream reset takes precedence over this for
2653 * the write case as peer may not have received all data.
2655 *state = SSL_STREAM_STATE_FINISHED;
2657 /* Stream still healthy. */
2658 *state = SSL_STREAM_STATE_OK;
2662 static int quic_get_stream_state(SSL *ssl, int is_write)
2667 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2668 return SSL_STREAM_STATE_NONE;
2670 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2671 quic_unlock(ctx.qc);
2675 int ossl_quic_get_stream_read_state(SSL *ssl)
2677 return quic_get_stream_state(ssl, /*is_write=*/0);
2681 * SSL_get_stream_write_state
2682 * --------------------------
2684 int ossl_quic_get_stream_write_state(SSL *ssl)
2686 return quic_get_stream_state(ssl, /*is_write=*/1);
2690 * SSL_get_stream_read_error_code
2691 * ------------------------------
2693 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2694 uint64_t *app_error_code)
2699 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2702 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2703 &state, app_error_code);
2705 quic_unlock(ctx.qc);
2707 case SSL_STREAM_STATE_FINISHED:
2709 case SSL_STREAM_STATE_RESET_LOCAL:
2710 case SSL_STREAM_STATE_RESET_REMOTE:
2717 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2719 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2723 * SSL_get_stream_write_error_code
2724 * -------------------------------
2726 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2728 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2732 * SSL_get_conn_close_info
2733 * -----------------------
2735 int ossl_quic_get_conn_close_info(SSL *ssl,
2736 SSL_CONN_CLOSE_INFO *info,
2740 const QUIC_TERMINATE_CAUSE *tc;
2742 if (!expect_quic_conn_only(ssl, &ctx))
2745 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
2749 info->error_code = tc->error_code;
2750 info->reason = NULL; /* TODO(QUIC): Wire reason */
2751 info->reason_len = 0;
2752 info->is_local = !tc->remote;
2753 info->is_transport = !tc->app;
2761 int ossl_quic_key_update(SSL *ssl, int update_type)
2765 if (!expect_quic_conn_only(ssl, &ctx))
2768 switch (update_type) {
2769 case SSL_KEY_UPDATE_NOT_REQUESTED:
2771 * QUIC signals peer key update implicily by triggering a local
2772 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
2774 case SSL_KEY_UPDATE_REQUESTED:
2778 /* Unknown type - error. */
2784 /* Attempt to perform a TXKU. */
2785 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
2786 quic_unlock(ctx.qc);
2790 quic_unlock(ctx.qc);
2795 * SSL_get_key_update_type
2796 * -----------------------
2798 int ossl_quic_get_key_update_type(const SSL *s)
2801 * We always handle key updates immediately so a key update is never
2804 return SSL_KEY_UPDATE_NONE;
2808 * QUIC Front-End I/O API: SSL_CTX Management
2809 * ==========================================
2812 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2816 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
2820 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
2824 if (!expect_quic_conn_only(s, &ctx))
2828 case SSL_CTRL_SET_MSG_CALLBACK:
2829 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
2831 /* This callback also needs to be set on the internal SSL object */
2832 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
2835 /* Probably a TLS related ctrl. Defer to our internal SSL object */
2836 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
2840 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
2842 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
2845 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
2847 /* We never do renegotiation. */
2852 * These functions define the TLSv1.2 (and below) ciphers that are supported by
2853 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
2856 int ossl_quic_num_ciphers(void)
2861 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
2866 int ossl_quic_set_ssl_op(SSL *ssl, uint64_t op)
2871 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2874 if (ctx.xso->stream == NULL)
2877 cleanse = (op & SSL_OP_CLEANSE_PLAINTEXT) != 0;
2878 if (ctx.xso->stream->rstream != NULL)
2879 ossl_quic_rstream_set_cleanse(ctx.xso->stream->rstream, cleanse);
2880 if (ctx.xso->stream->sstream != NULL)
2881 ossl_quic_sstream_set_cleanse(ctx.xso->stream->sstream, cleanse);
2884 quic_unlock(ctx.qc);
2889 * Internal Testing APIs
2890 * =====================
2893 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
2897 if (!expect_quic_conn_only(s, &ctx))