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);
36 static int quic_validate_for_write(QUIC_XSO *xso, int *err);
37 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
40 * QUIC Front-End I/O API: Common Utilities
41 * ========================================
45 * Block until a predicate is met.
47 * Precondition: Must have a channel.
48 * Precondition: Must hold channel lock (unchecked).
51 static int block_until_pred(QUIC_CONNECTION *qc,
52 int (*pred)(void *arg), void *pred_arg,
57 assert(qc->ch != NULL);
60 * Any attempt to block auto-disables tick inhibition as otherwise we will
61 * hang around forever.
63 ossl_quic_channel_set_inhibit_tick(qc->ch, 0);
65 rtor = ossl_quic_channel_get_reactor(qc->ch);
66 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
70 static OSSL_TIME get_time(QUIC_CONNECTION *qc)
72 if (qc->override_now_cb != NULL)
73 return qc->override_now_cb(qc->override_now_cb_arg);
75 return ossl_time_now();
78 static OSSL_TIME get_time_cb(void *arg)
80 QUIC_CONNECTION *qc = arg;
86 * QCTX is a utility structure which provides information we commonly wish to
87 * unwrap upon an API call being dispatched to us, namely:
89 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
91 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
92 * a QCSO with a default stream was passed);
93 * - whether a QSSO was passed (xso == NULL must not be used to determine this
94 * because it may be non-NULL when a QCSO is passed if that QCSO has a
104 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
105 * rather than via ERR. Note that normal errors must always be raised while
109 static int quic_raise_normal_error(QCTX *ctx,
113 ctx->xso->last_error = err;
115 ctx->qc->last_error = err;
121 * Raise a 'non-normal' error, meaning any error that is not reported via
122 * SSL_get_error() and must be reported via ERR.
124 * qc should be provided if available. In exceptional circumstances when qc is
125 * not known NULL may be passed. This should generally only happen when an
126 * expect_...() function defined below fails, which generally indicates a
127 * dispatch error or caller error.
129 * ctx should be NULL if the connection lock is not held.
131 static int quic_raise_non_normal_error(QCTX *ctx,
142 if (ctx->is_stream && ctx->xso != NULL)
143 ctx->xso->last_error = SSL_ERROR_SSL;
144 else if (!ctx->is_stream && ctx->qc != NULL)
145 ctx->qc->last_error = SSL_ERROR_SSL;
147 if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
148 ossl_quic_channel_restore_err_state(ctx->qc->ch);
152 ERR_set_debug(file, line, func);
155 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
161 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
162 quic_raise_normal_error((ctx), (err))
164 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
165 quic_raise_non_normal_error((ctx), \
166 OPENSSL_FILE, OPENSSL_LINE, \
172 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
173 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
175 * After this returns 1, all fields of the passed QCTX are initialised.
176 * Returns 0 on failure. This function is intended to be used to provide API
177 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
179 static int expect_quic(const SSL *s, QCTX *ctx)
189 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
192 case SSL_TYPE_QUIC_CONNECTION:
193 qc = (QUIC_CONNECTION *)s;
195 ctx->xso = qc->default_xso;
199 case SSL_TYPE_QUIC_XSO:
207 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
212 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
213 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
216 * remote_init determines if we expect the default XSO to be remotely created or
217 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
219 * Channel mutex is acquired and retained on success.
222 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
225 if (!expect_quic(s, ctx))
230 if (ctx->xso == NULL && remote_init >= 0) {
231 if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) {
232 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
236 /* If we haven't finished the handshake, try to advance it. */
237 if (quic_do_handshake(ctx) < 1)
238 /* ossl_quic_do_handshake raised error here */
241 if (remote_init == 0) {
242 if (!qc_try_create_default_xso_for_write(ctx))
245 if (!qc_wait_for_default_xso_for_read(ctx))
249 ctx->xso = ctx->qc->default_xso;
252 if (ctx->xso == NULL) {
253 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
257 return 1; /* lock held */
260 quic_unlock(ctx->qc);
265 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
266 * be non-NULL if the QCSO has a default stream.
268 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
270 if (!expect_quic(s, ctx))
274 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
280 * Ensures that the channel mutex is held for a method which touches channel
283 * Precondition: Channel mutex is not held (unchecked)
285 static void quic_lock(QUIC_CONNECTION *qc)
287 #if defined(OPENSSL_THREADS)
288 ossl_crypto_mutex_lock(qc->mutex);
292 /* Precondition: Channel mutex is held (unchecked) */
294 static void quic_unlock(QUIC_CONNECTION *qc)
296 #if defined(OPENSSL_THREADS)
297 ossl_crypto_mutex_unlock(qc->mutex);
302 * This predicate is the criterion which should determine API call rejection for
303 * *most* mutating API calls, particularly stream-related operations for send
306 * A call is rejected (this function returns 0) if shutdown is in progress
307 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
308 * req_active=1, the connection must be active (i.e., the IDLE state is also
311 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
313 if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
316 if (req_active && !ossl_quic_channel_is_active(qc->ch))
323 * QUIC Front-End I/O API: Initialization
324 * ======================================
326 * SSL_new => ossl_quic_new
328 * SSL_reset => ossl_quic_reset
329 * SSL_clear => ossl_quic_clear
331 * SSL_free => ossl_quic_free
333 * SSL_set_options => ossl_quic_set_options
334 * SSL_get_options => ossl_quic_get_options
335 * SSL_clear_options => ossl_quic_clear_options
340 SSL *ossl_quic_new(SSL_CTX *ctx)
342 QUIC_CONNECTION *qc = NULL;
343 SSL *ssl_base = NULL;
344 SSL_CONNECTION *sc = NULL;
346 qc = OPENSSL_zalloc(sizeof(*qc));
350 /* Initialise the QUIC_CONNECTION's stub header. */
352 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
357 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
358 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
361 /* override the user_ssl of the inner connection */
362 sc->s3.flags |= TLS1_FLAGS_QUIC;
364 /* Restrict options derived from the SSL_CTX. */
365 sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
368 #if defined(OPENSSL_THREADS)
369 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
373 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
374 qc->is_thread_assisted
375 = (ssl_base->method == OSSL_QUIC_client_thread_method());
378 qc->as_server = 0; /* TODO(QUIC): server support */
379 qc->as_server_state = qc->as_server;
381 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
382 qc->default_ssl_mode = qc->ssl.ctx->mode;
383 qc->default_ssl_options = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
384 qc->default_blocking = 1;
386 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
387 qc->last_error = SSL_ERROR_NONE;
389 if (!create_channel(qc))
392 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
393 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
395 qc_update_reject_policy(qc);
398 * We do not create the default XSO yet. The reason for this is that the
399 * stream ID of the default XSO will depend on whether the stream is client
400 * or server-initiated, which depends on who transmits first. Since we do
401 * not know whether the application will be using a client-transmits-first
402 * or server-transmits-first protocol, we defer default XSO creation until
403 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
404 * we take that as a cue that the client is expecting a server-initiated
405 * stream, and vice versa if SSL_write() is called first.
411 #if defined(OPENSSL_THREADS)
412 ossl_crypto_mutex_free(qc->mutex);
414 ossl_quic_channel_free(qc->ch);
423 void ossl_quic_free(SSL *s)
428 /* We should never be called on anything but a QSO. */
429 if (!expect_quic(s, &ctx))
436 * When a QSSO is freed, the XSO is freed immediately, because the XSO
437 * itself only contains API personality layer data. However the
438 * underlying QUIC_STREAM is not freed immediately but is instead marked
439 * as deleted for later collection.
442 assert(ctx.qc->num_xso > 0);
445 /* If a stream's send part has not been finished, auto-reset it. */
446 if (( ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
447 || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
448 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
449 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
452 /* Do STOP_SENDING for the receive part, if applicable. */
453 if ( ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
454 || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
455 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
458 /* Update stream state. */
459 ctx.xso->stream->deleted = 1;
460 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
463 is_default = (ctx.xso == ctx.qc->default_xso);
467 * Unref the connection in most cases; the XSO has a ref to the QC and
468 * not vice versa. But for a default XSO, to avoid circular references,
469 * the QC refs the XSO but the XSO does not ref the QC. If we are the
470 * default XSO, we only get here when the QC is being torn down anyway,
471 * so don't call SSL_free(qc) as we are already in it.
474 SSL_free(&ctx.qc->ssl);
476 /* Note: SSL_free calls OPENSSL_free(xso) for us */
481 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
482 * stage, but is freed during the channel free when the whole QSM is freed.
484 if (ctx.qc->default_xso != NULL) {
485 QUIC_XSO *xso = ctx.qc->default_xso;
490 ctx.qc->default_xso = NULL;
493 /* Ensure we have no remaining XSOs. */
494 assert(ctx.qc->num_xso == 0);
496 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
497 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
498 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
499 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
503 ossl_quic_channel_free(ctx.qc->ch);
505 BIO_free(ctx.qc->net_rbio);
506 BIO_free(ctx.qc->net_wbio);
508 /* Note: SSL_free calls OPENSSL_free(qc) for us */
510 SSL_free(ctx.qc->tls);
511 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
512 #if defined(OPENSSL_THREADS)
513 ossl_crypto_mutex_free(&ctx.qc->mutex);
517 /* SSL method init */
518 int ossl_quic_init(SSL *s)
520 /* Same op as SSL_clear, forward the call. */
521 return ossl_quic_clear(s);
524 /* SSL method deinit */
525 void ossl_quic_deinit(SSL *s)
530 /* SSL_clear (ssl_reset method) */
531 int ossl_quic_reset(SSL *s)
535 if (!expect_quic(s, &ctx))
542 /* ssl_clear method (unused) */
543 int ossl_quic_clear(SSL *s)
547 if (!expect_quic(s, &ctx))
550 /* TODO(QUIC): Currently a no-op. */
554 int ossl_quic_conn_set_override_now_cb(SSL *s,
555 OSSL_TIME (*now_cb)(void *arg),
560 if (!expect_quic(s, &ctx))
565 ctx.qc->override_now_cb = now_cb;
566 ctx.qc->override_now_cb_arg = now_cb_arg;
572 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
576 if (!expect_quic(s, &ctx))
579 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
580 if (ctx.qc->is_thread_assisted && ctx.qc->started)
581 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
586 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
588 qc->default_xso_created = 1;
589 qc_update_reject_policy(qc);
593 * Changes default XSO. Allows caller to keep reference to the old default XSO
594 * (if any). Reference to new XSO is transferred from caller.
597 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
605 if (qc->default_xso != xso) {
606 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
608 qc->default_xso = xso;
612 * Changing to not having a default XSO. XSO becomes standalone and
613 * now has a ref to the QC.
615 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
619 * Changing from not having a default XSO to having one. The new XSO
620 * will have had a reference to the QC we need to drop to avoid a
621 * circular reference.
623 * Currently we never change directly from one default XSO to
624 * another, though this function would also still be correct if this
627 assert(*old_xso == NULL);
629 CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
635 qc_touch_default_xso(qc);
639 * Changes default XSO, releasing the reference to any previous default XSO.
640 * Reference to new XSO is transferred from caller.
643 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
645 QUIC_XSO *old_xso = NULL;
647 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
650 SSL_free(&old_xso->ssl);
654 static void xso_update_options(QUIC_XSO *xso)
656 int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
658 if (xso->stream->rstream != NULL)
659 ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
661 if (xso->stream->sstream != NULL)
662 ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
669 * Setting options on a QCSO
670 * - configures the handshake-layer options;
671 * - configures the default data-plane options for new streams;
672 * - configures the data-plane options on the default XSO, if there is one.
674 * Setting options on a QSSO
675 * - configures data-plane options for that stream only.
678 static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
681 uint64_t hs_mask_value, hs_or_value, ret;
683 if (!expect_quic(ssl, &ctx))
688 if (!ctx.is_stream) {
690 * If we were called on the connection, we apply any handshake option
693 hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
694 hs_or_value = (or_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
696 SSL_clear_options(ctx.qc->tls, hs_mask_value);
697 SSL_set_options(ctx.qc->tls, hs_or_value);
699 /* Update defaults for new streams. */
700 ctx.qc->default_ssl_options
701 = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
702 & OSSL_QUIC_PERMITTED_OPTIONS;
705 if (ctx.xso != NULL) {
707 = ((ctx.xso->ssl_options & ~mask_value) | or_value)
708 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
710 xso_update_options(ctx.xso);
713 ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;
719 uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
721 return quic_mask_or_options(ssl, 0, options);
724 /* SSL_clear_options */
725 uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
727 return quic_mask_or_options(ssl, options, 0);
730 /* SSL_get_options */
731 uint64_t ossl_quic_get_options(const SSL *ssl)
733 return quic_mask_or_options((SSL *)ssl, 0, 0);
737 * QUIC Front-End I/O API: Network BIO Configuration
738 * =================================================
740 * Handling the different BIOs is difficult:
742 * - It is more or less a requirement that we use non-blocking network I/O;
743 * we need to be able to have timeouts on recv() calls, and make best effort
744 * (non blocking) send() and recv() calls.
746 * The only sensible way to do this is to configure the socket into
747 * non-blocking mode. We could try to do select() before calling send() or
748 * recv() to get a guarantee that the call will not block, but this will
749 * probably run into issues with buggy OSes which generate spurious socket
750 * readiness events. In any case, relying on this to work reliably does not
753 * Timeouts could be handled via setsockopt() socket timeout options, but
754 * this depends on OS support and adds another syscall to every network I/O
755 * operation. It also has obvious thread safety concerns if we want to move
756 * to concurrent use of a single socket at some later date.
758 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
759 * be made non-blocking. However some OSes (e.g. Windows) do not support
760 * this, so we cannot rely on this.
762 * As such, we need to configure any FD in non-blocking mode. This may
763 * confound users who pass a blocking socket to libssl. However, in practice
764 * it would be extremely strange for a user of QUIC to pass an FD to us,
765 * then also try and send receive traffic on the same socket(!). Thus the
766 * impact of this should be limited, and can be documented.
768 * - We support both blocking and non-blocking operation in terms of the API
769 * presented to the user. One prospect is to set the blocking mode based on
770 * whether the socket passed to us was already in blocking mode. However,
771 * Windows has no API for determining if a socket is in blocking mode (!),
772 * therefore this cannot be done portably. Currently therefore we expose an
773 * explicit API call to set this, and default to blocking mode.
775 * - We need to determine our initial destination UDP address. The "natural"
776 * way for a user to do this is to set the peer variable on a BIO_dgram.
777 * However, this has problems because BIO_dgram's peer variable is used for
778 * both transmission and reception. This means it can be constantly being
779 * changed to a malicious value (e.g. if some random unrelated entity on the
780 * network starts sending traffic to us) on every read call. This is not a
781 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
782 * calls only, which do not use this variable. However, we do need to let
783 * the user specify the peer in a 'normal' manner. The compromise here is
784 * that we grab the current peer value set at the time the write BIO is set
785 * and do not read the value again.
787 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
788 * Currently we do this by only supporting non-blocking mode.
793 * Determines what initial destination UDP address we should use, if possible.
794 * If this fails the client must set the destination address manually, or use a
795 * BIO which does not need a destination address.
797 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
799 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
805 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
809 if (!expect_quic(s, &ctx))
812 if (ctx.qc->net_rbio == net_rbio)
815 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
818 BIO_free(ctx.qc->net_rbio);
819 ctx.qc->net_rbio = net_rbio;
822 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
823 * mode as we do not support it for non-pollable BIOs.
825 if (net_rbio != NULL) {
826 BIO_POLL_DESCRIPTOR d = {0};
828 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
829 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
830 ctx.qc->blocking = 0;
831 ctx.qc->default_blocking = 0;
832 ctx.qc->can_poll_net_rbio = 0;
834 ctx.qc->can_poll_net_rbio = 1;
839 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
843 if (!expect_quic(s, &ctx))
846 if (ctx.qc->net_wbio == net_wbio)
849 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
852 BIO_free(ctx.qc->net_wbio);
853 ctx.qc->net_wbio = net_wbio;
855 if (net_wbio != NULL) {
856 BIO_POLL_DESCRIPTOR d = {0};
858 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
859 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
860 ctx.qc->blocking = 0;
861 ctx.qc->default_blocking = 0;
862 ctx.qc->can_poll_net_wbio = 0;
864 ctx.qc->can_poll_net_wbio = 1;
868 * If we do not have a peer address yet, and we have not started trying
869 * to connect yet, try to autodetect one.
871 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
872 && !ctx.qc->started) {
873 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
875 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
877 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
878 &ctx.qc->init_peer_addr);
883 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
887 if (!expect_quic(s, &ctx))
890 return ctx.qc->net_rbio;
893 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
897 if (!expect_quic(s, &ctx))
900 return ctx.qc->net_wbio;
903 int ossl_quic_conn_get_blocking_mode(const SSL *s)
907 if (!expect_quic(s, &ctx))
911 return ctx.xso->blocking;
913 return ctx.qc->blocking;
916 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
920 if (!expect_quic(s, &ctx))
923 /* Cannot enable blocking mode if we do not have pollable FDs. */
925 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
926 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
928 if (!ctx.is_stream) {
930 * If called on a QCSO, update default and connection-level blocking
933 ctx.qc->blocking = (blocking != 0);
934 ctx.qc->default_blocking = ctx.qc->blocking;
939 * If called on a QSSO or QCSO with a default XSO, update blocking
942 ctx.xso->blocking = (blocking != 0);
947 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
948 const BIO_ADDR *peer_addr)
952 if (!expect_quic(s, &ctx))
956 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
959 if (peer_addr == NULL) {
960 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
964 ctx.qc->init_peer_addr = *peer_addr;
969 * QUIC Front-End I/O API: Asynchronous I/O Management
970 * ===================================================
972 * (BIO/)SSL_handle_events => ossl_quic_handle_events
973 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
974 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
978 /* Returns 1 if the connection is being used in blocking mode. */
979 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
984 static int xso_blocking_mode(const QUIC_XSO *xso)
987 && xso->conn->can_poll_net_rbio
988 && xso->conn->can_poll_net_wbio;
991 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
993 int ossl_quic_handle_events(SSL *s)
997 if (!expect_quic(s, &ctx))
1001 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1002 quic_unlock(ctx.qc);
1007 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1008 * should next have events handled by the application by calling
1009 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1010 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1011 * the value of *tv is undefined.
1014 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1017 OSSL_TIME deadline = ossl_time_infinite();
1019 if (!expect_quic(s, &ctx))
1025 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
1027 if (ossl_time_is_infinite(deadline)) {
1031 * Robustness against faulty applications that don't check *is_infinite;
1032 * harmless long timeout.
1034 tv->tv_sec = 1000000;
1037 quic_unlock(ctx.qc);
1041 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
1043 quic_unlock(ctx.qc);
1047 /* SSL_get_rpoll_descriptor */
1048 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1052 if (!expect_quic(s, &ctx))
1055 if (desc == NULL || ctx.qc->net_rbio == NULL)
1058 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
1061 /* SSL_get_wpoll_descriptor */
1062 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1066 if (!expect_quic(s, &ctx))
1069 if (desc == NULL || ctx.qc->net_wbio == NULL)
1072 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
1075 /* SSL_net_read_desired */
1077 int ossl_quic_get_net_read_desired(SSL *s)
1082 if (!expect_quic(s, &ctx))
1086 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1087 quic_unlock(ctx.qc);
1091 /* SSL_net_write_desired */
1093 int ossl_quic_get_net_write_desired(SSL *s)
1098 if (!expect_quic(s, &ctx))
1102 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1103 quic_unlock(ctx.qc);
1108 * QUIC Front-End I/O API: Connection Lifecycle Operations
1109 * =======================================================
1111 * SSL_do_handshake => ossl_quic_do_handshake
1112 * SSL_set_connect_state => ossl_quic_set_connect_state
1113 * SSL_set_accept_state => ossl_quic_set_accept_state
1114 * SSL_shutdown => ossl_quic_shutdown
1115 * SSL_ctrl => ossl_quic_ctrl
1116 * (BIO/)SSL_connect => ossl_quic_connect
1117 * (BIO/)SSL_accept => ossl_quic_accept
1122 static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1124 QUIC_STREAM_MAP *qsm;
1126 if (qc->shutting_down)
1129 qsm = ossl_quic_channel_get_qsm(qc->ch);
1131 ossl_quic_stream_map_begin_shutdown_flush(qsm);
1132 qc->shutting_down = 1;
1135 /* Returns 1 if all shutdown-flush streams have been done with. */
1137 static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1139 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1141 return qc->shutting_down
1142 && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1146 static int quic_shutdown_wait(void *arg)
1148 QUIC_CONNECTION *qc = arg;
1150 return ossl_quic_channel_is_terminated(qc->ch);
1153 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
1154 static int quic_shutdown_flush_wait(void *arg)
1156 QUIC_CONNECTION *qc = arg;
1158 return ossl_quic_channel_is_term_any(qc->ch)
1159 || qc_shutdown_flush_finished(qc);
1163 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1164 const SSL_SHUTDOWN_EX_ARGS *args,
1169 int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1171 if (!expect_quic(s, &ctx))
1175 /* TODO(QUIC): Semantics currently undefined for QSSOs */
1180 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1181 quic_unlock(ctx.qc);
1185 /* Phase 1: Stream Flushing */
1187 qc_shutdown_flush_init(ctx.qc);
1189 if (!qc_shutdown_flush_finished(ctx.qc)) {
1190 if (qc_blocking_mode(ctx.qc))
1191 block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);
1193 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1196 if (!qc_shutdown_flush_finished(ctx.qc)) {
1197 quic_unlock(ctx.qc);
1198 return 0; /* ongoing */
1202 /* Phase 2: Connection Closure */
1203 ossl_quic_channel_local_close(ctx.qc->ch,
1204 args != NULL ? args->quic_error_code : 0);
1206 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1207 quic_unlock(ctx.qc);
1211 /* Phase 3: Terminating Wait Time */
1212 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
1213 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1215 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1217 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1218 quic_unlock(ctx.qc);
1223 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1227 if (!expect_quic(s, &ctx))
1232 /* If called on a QCSO, update the default mode. */
1234 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1237 * If we were called on a QSSO or have a default stream, we also update
1240 if (ctx.xso != NULL) {
1241 /* Cannot enable EPW while AON write in progress. */
1242 if (ctx.xso->aon_write_in_progress)
1243 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1245 ctx.xso->ssl_mode |= (uint32_t)larg;
1246 return ctx.xso->ssl_mode;
1249 return ctx.qc->default_ssl_mode;
1250 case SSL_CTRL_CLEAR_MODE:
1252 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1254 if (ctx.xso != NULL) {
1255 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1256 return ctx.xso->ssl_mode;
1259 return ctx.qc->default_ssl_mode;
1261 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1262 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1263 /* This ctrl also needs to be passed to the internal SSL object */
1264 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1266 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1270 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1273 return !is_infinite;
1275 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1276 /* For legacy compatibility with DTLS calls. */
1277 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1279 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1280 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1284 /* SSL_set_connect_state */
1285 void ossl_quic_set_connect_state(SSL *s)
1289 if (!expect_quic(s, &ctx))
1292 /* Cannot be changed after handshake started */
1293 if (ctx.qc->started || ctx.is_stream)
1296 ctx.qc->as_server_state = 0;
1299 /* SSL_set_accept_state */
1300 void ossl_quic_set_accept_state(SSL *s)
1304 if (!expect_quic(s, &ctx))
1307 /* Cannot be changed after handshake started */
1308 if (ctx.qc->started || ctx.is_stream)
1311 ctx.qc->as_server_state = 1;
1314 /* SSL_do_handshake */
1315 struct quic_handshake_wait_args {
1316 QUIC_CONNECTION *qc;
1319 static int quic_handshake_wait(void *arg)
1321 struct quic_handshake_wait_args *args = arg;
1323 if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1326 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1332 static int configure_channel(QUIC_CONNECTION *qc)
1334 assert(qc->ch != NULL);
1336 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1337 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1338 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1345 static int create_channel(QUIC_CONNECTION *qc)
1347 QUIC_CHANNEL_ARGS args = {0};
1349 args.libctx = qc->ssl.ctx->libctx;
1350 args.propq = qc->ssl.ctx->propq;
1351 args.is_server = qc->as_server;
1353 args.mutex = qc->mutex;
1354 args.now_cb = get_time_cb;
1355 args.now_cb_arg = qc;
1357 qc->ch = ossl_quic_channel_new(&args);
1365 * Creates a channel and configures it with the information we have accumulated
1366 * via calls made to us from the application prior to starting a handshake
1370 static int ensure_channel_started(QUIC_CONNECTION *qc)
1373 if (!configure_channel(qc)
1374 || !ossl_quic_channel_start(qc->ch))
1377 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1378 if (qc->is_thread_assisted)
1379 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1388 ossl_quic_channel_free(qc->ch);
1394 static int quic_do_handshake(QCTX *ctx)
1397 QUIC_CONNECTION *qc = ctx->qc;
1399 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1400 /* Handshake already completed. */
1403 if (!quic_mutation_allowed(qc, /*req_active=*/0))
1404 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1406 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1407 /* Peer address must have been set. */
1408 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1409 return -1; /* Non-protocol error */
1412 if (qc->as_server != qc->as_server_state) {
1413 /* TODO(QUIC): Must match the method used to create the QCSO */
1414 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1415 return -1; /* Non-protocol error */
1418 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1419 /* Need read and write BIOs. */
1420 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1421 return -1; /* Non-protocol error */
1425 * Start connection process. Note we may come here multiple times in
1426 * non-blocking mode, which is fine.
1428 if (!ensure_channel_started(qc)) {
1429 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1430 return -1; /* Non-protocol error */
1433 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1434 /* The handshake is now done. */
1437 if (qc_blocking_mode(qc)) {
1438 /* In blocking mode, wait for the handshake to complete. */
1439 struct quic_handshake_wait_args args;
1443 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1444 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1445 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1446 return 0; /* Shutdown before completion */
1447 } else if (ret <= 0) {
1448 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1449 return -1; /* Non-protocol error */
1452 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1455 /* Try to advance the reactor. */
1456 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1458 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1459 /* The handshake is now done. */
1462 /* Otherwise, indicate that the handshake isn't done yet. */
1463 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1464 return -1; /* Non-protocol error */
1469 int ossl_quic_do_handshake(SSL *s)
1474 if (!expect_quic(s, &ctx))
1479 ret = quic_do_handshake(&ctx);
1480 quic_unlock(ctx.qc);
1485 int ossl_quic_connect(SSL *s)
1487 /* Ensure we are in connect state (no-op if non-idle). */
1488 ossl_quic_set_connect_state(s);
1490 /* Begin or continue the handshake */
1491 return ossl_quic_do_handshake(s);
1495 int ossl_quic_accept(SSL *s)
1497 /* Ensure we are in accept state (no-op if non-idle). */
1498 ossl_quic_set_accept_state(s);
1500 /* Begin or continue the handshake */
1501 return ossl_quic_do_handshake(s);
1505 * QUIC Front-End I/O API: Stream Lifecycle Operations
1506 * ===================================================
1508 * SSL_stream_new => ossl_quic_conn_stream_new
1513 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1514 * default XSO was created. Returns 0 if it was not (e.g. because it already
1515 * exists). Note that this is NOT an error condition.
1518 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1521 QUIC_CONNECTION *qc = ctx->qc;
1523 if (qc->default_xso_created
1524 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1526 * We only do this once. If the user detaches a previously created
1527 * default XSO we don't auto-create another one.
1529 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1531 /* Create a locally-initiated stream. */
1532 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1533 flags |= SSL_STREAM_FLAG_UNI;
1535 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1538 if (qc->default_xso == NULL)
1539 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1541 qc_touch_default_xso(qc);
1545 struct quic_wait_for_stream_args {
1546 QUIC_CONNECTION *qc;
1553 static int quic_wait_for_stream(void *arg)
1555 struct quic_wait_for_stream_args *args = arg;
1557 if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
1558 /* If connection is torn down due to an error while blocking, stop. */
1559 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1563 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1564 args->expect_id | QUIC_STREAM_DIR_BIDI);
1565 if (args->qs == NULL)
1566 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1567 args->expect_id | QUIC_STREAM_DIR_UNI);
1569 if (args->qs != NULL)
1570 return 1; /* stream now exists */
1572 return 0; /* did not get a stream, keep trying */
1576 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1578 /* Called on a QCSO and we don't currently have a default stream. */
1580 QUIC_CONNECTION *qc = ctx->qc;
1583 struct quic_wait_for_stream_args wargs;
1586 * If default stream functionality is disabled or we already detached
1587 * one, don't make another default stream and just fail.
1589 if (qc->default_xso_created
1590 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1591 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1594 * The peer may have opened a stream since we last ticked. So tick and
1595 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1596 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1597 * first stream created by a peer must have an ordinal of 0.
1599 expect_id = qc->as_server
1600 ? QUIC_STREAM_INITIATOR_CLIENT
1601 : QUIC_STREAM_INITIATOR_SERVER;
1603 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1604 expect_id | QUIC_STREAM_DIR_BIDI);
1606 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1607 expect_id | QUIC_STREAM_DIR_UNI);
1610 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1612 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1617 if (!qc_blocking_mode(qc))
1618 /* Non-blocking mode, so just bail immediately. */
1619 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1621 /* Block until we have a stream. */
1625 wargs.expect_id = expect_id;
1627 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1629 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1630 else if (res < 0 || wargs.qs == NULL)
1631 /* quic_wait_for_stream raised error here */
1638 * We now have qs != NULL. Make it the default stream, creating the
1641 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1642 if (qc->default_xso == NULL)
1643 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1645 qc_touch_default_xso(qc); /* inhibits default XSO */
1650 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1652 QUIC_XSO *xso = NULL;
1654 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1657 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1661 if (!SSL_up_ref(&qc->ssl))
1665 xso->blocking = qc->default_blocking;
1666 xso->ssl_mode = qc->default_ssl_mode;
1668 = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1669 xso->last_error = SSL_ERROR_NONE;
1674 xso_update_options(xso);
1682 /* locking depends on need_lock */
1683 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1685 QUIC_CONNECTION *qc = ctx->qc;
1686 QUIC_XSO *xso = NULL;
1687 QUIC_STREAM *qs = NULL;
1688 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1693 if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
1694 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1698 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1702 xso = create_xso_from_stream(qc, qs);
1706 qc_touch_default_xso(qc); /* inhibits default XSO */
1714 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1723 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1727 if (!expect_quic_conn_only(s, &ctx))
1730 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1734 * QUIC Front-End I/O API: Steady-State Operations
1735 * ===============================================
1737 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1738 * is, the functions used during the established phase of a QUIC connection
1739 * (e.g. SSL_read, SSL_write).
1741 * Each function must handle both blocking and non-blocking modes. As discussed
1742 * above, all QUIC I/O is implemented using non-blocking mode internally.
1744 * SSL_get_error => partially implemented by ossl_quic_get_error
1745 * (BIO/)SSL_read => ossl_quic_read
1746 * (BIO/)SSL_write => ossl_quic_write
1747 * SSL_pending => ossl_quic_pending
1748 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1749 * SSL_key_update => ossl_quic_key_update
1753 int ossl_quic_get_error(const SSL *s, int i)
1756 int net_error, last_error;
1758 if (!expect_quic(s, &ctx))
1762 net_error = ossl_quic_channel_net_error(ctx.qc->ch);
1763 last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1764 quic_unlock(ctx.qc);
1767 return SSL_ERROR_SYSCALL;
1776 * The set of functions below provide the implementation of the public SSL_write
1777 * function. We must handle:
1779 * - both blocking and non-blocking operation at the application level,
1780 * depending on how we are configured;
1782 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1784 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1788 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1791 * We have appended at least one byte to the stream.
1792 * Potentially mark stream as active, depending on FC.
1795 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1801 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1802 * plus we should eventually consider Nagle's algorithm.
1805 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1808 struct quic_write_again_args {
1810 const unsigned char *buf;
1812 size_t total_written;
1817 static int quic_write_again(void *arg)
1819 struct quic_write_again_args *args = arg;
1820 size_t actual_written = 0;
1822 if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
1823 /* If connection is torn down due to an error while blocking, stop. */
1826 if (!quic_validate_for_write(args->xso, &args->err))
1828 * Stream may have become invalid for write due to connection events
1833 args->err = ERR_R_INTERNAL_ERROR;
1834 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1835 args->buf, args->len, &actual_written))
1838 quic_post_write(args->xso, actual_written > 0, 0);
1840 args->buf += actual_written;
1841 args->len -= actual_written;
1842 args->total_written += actual_written;
1845 /* Written everything, done. */
1848 /* Not written everything yet, keep trying. */
1853 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1857 QUIC_XSO *xso = ctx->xso;
1858 struct quic_write_again_args args;
1859 size_t actual_written = 0;
1861 /* First make a best effort to append as much of the data as possible. */
1862 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1864 /* Stream already finished or allocation error. */
1866 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1869 quic_post_write(xso, actual_written > 0, 1);
1871 if (actual_written == len) {
1872 /* Managed to append everything on the first try. */
1873 *written = actual_written;
1878 * We did not manage to append all of the data immediately, so the stream
1879 * buffer has probably filled up. This means we need to block until some of
1883 args.buf = (const unsigned char *)buf + actual_written;
1884 args.len = len - actual_written;
1885 args.total_written = 0;
1886 args.err = ERR_R_INTERNAL_ERROR;
1888 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1890 if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
1891 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1893 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
1896 *written = args.total_written;
1901 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1904 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1905 size_t buf_len, size_t already_sent)
1907 assert(!xso->aon_write_in_progress);
1909 xso->aon_write_in_progress = 1;
1910 xso->aon_buf_base = buf;
1911 xso->aon_buf_pos = already_sent;
1912 xso->aon_buf_len = buf_len;
1915 static void aon_write_finish(QUIC_XSO *xso)
1917 xso->aon_write_in_progress = 0;
1918 xso->aon_buf_base = NULL;
1919 xso->aon_buf_pos = 0;
1920 xso->aon_buf_len = 0;
1924 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
1925 size_t len, size_t *written)
1927 QUIC_XSO *xso = ctx->xso;
1928 const void *actual_buf;
1929 size_t actual_len, actual_written = 0;
1930 int accept_moving_buffer
1931 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1933 if (xso->aon_write_in_progress) {
1935 * We are in the middle of an AON write (i.e., a previous write did not
1936 * manage to append all data to the SSTREAM and we have Enable Partial
1937 * Write (EPW) mode disabled.)
1939 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1940 || len != xso->aon_buf_len)
1942 * Pointer must not have changed if we are not in accept moving
1943 * buffer mode. Length must never change.
1945 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
1947 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1948 actual_len = len - xso->aon_buf_pos;
1949 assert(actual_len > 0);
1955 /* First make a best effort to append as much of the data as possible. */
1956 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1958 /* Stream already finished or allocation error. */
1960 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1963 quic_post_write(xso, actual_written > 0, 1);
1965 if (actual_written == actual_len) {
1966 /* We have sent everything. */
1967 if (xso->aon_write_in_progress) {
1969 * We have sent everything, and we were in the middle of an AON
1970 * write. The output write length is the total length of the AON
1971 * buffer, not however many bytes we managed to write to the stream
1974 *written = xso->aon_buf_len;
1975 aon_write_finish(xso);
1977 *written = actual_written;
1983 if (xso->aon_write_in_progress) {
1985 * AON write is in progress but we have not written everything yet. We
1986 * may have managed to send zero bytes, or some number of bytes less
1987 * than the total remaining which need to be appended during this
1990 xso->aon_buf_pos += actual_written;
1991 assert(xso->aon_buf_pos < xso->aon_buf_len);
1992 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
1996 * Not in an existing AON operation but partial write is not enabled, so we
1997 * need to begin a new AON operation. However we needn't bother if we didn't
1998 * actually append anything.
2000 if (actual_written > 0)
2001 aon_write_begin(xso, buf, len, actual_written);
2004 * AON - We do not publicly admit to having appended anything until AON
2008 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2012 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2015 QUIC_XSO *xso = ctx->xso;
2017 /* Simple best effort operation. */
2018 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
2019 /* Stream already finished or allocation error. */
2021 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2024 quic_post_write(xso, *written > 0, 1);
2029 static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2031 QUIC_STREAM_MAP *qsm;
2033 if (xso == NULL || xso->stream == NULL) {
2034 *err = ERR_R_INTERNAL_ERROR;
2038 switch (xso->stream->send_state) {
2040 case QUIC_SSTREAM_STATE_NONE:
2041 *err = SSL_R_STREAM_RECV_ONLY;
2044 case QUIC_SSTREAM_STATE_READY:
2045 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2047 if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2048 *err = ERR_R_INTERNAL_ERROR;
2053 case QUIC_SSTREAM_STATE_SEND:
2054 case QUIC_SSTREAM_STATE_DATA_SENT:
2055 case QUIC_SSTREAM_STATE_DATA_RECVD:
2056 if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2057 *err = SSL_R_STREAM_FINISHED;
2063 case QUIC_SSTREAM_STATE_RESET_SENT:
2064 case QUIC_SSTREAM_STATE_RESET_RECVD:
2065 *err = SSL_R_STREAM_RESET;
2071 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2075 int partial_write, err;
2079 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2082 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
2084 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2085 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2090 * If we haven't finished the handshake, try to advance it.
2091 * We don't accept writes until the handshake is completed.
2093 if (quic_do_handshake(&ctx) < 1) {
2098 /* Ensure correct stream state, stream send part not concluded, etc. */
2099 if (!quic_validate_for_write(ctx.xso, &err)) {
2100 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2109 if (xso_blocking_mode(ctx.xso))
2110 ret = quic_write_blocking(&ctx, buf, len, written);
2111 else if (partial_write)
2112 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
2114 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
2117 quic_unlock(ctx.qc);
2125 struct quic_read_again_args {
2127 QUIC_STREAM *stream;
2135 static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2137 QUIC_STREAM_MAP *qsm;
2141 if (xso == NULL || xso->stream == NULL) {
2142 *err = ERR_R_INTERNAL_ERROR;
2146 switch (xso->stream->recv_state) {
2148 case QUIC_RSTREAM_STATE_NONE:
2149 *err = SSL_R_STREAM_SEND_ONLY;
2152 case QUIC_RSTREAM_STATE_RECV:
2153 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2154 case QUIC_RSTREAM_STATE_DATA_RECVD:
2157 case QUIC_RSTREAM_STATE_DATA_READ:
2161 case QUIC_RSTREAM_STATE_RESET_RECVD:
2162 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2163 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2166 case QUIC_RSTREAM_STATE_RESET_READ:
2167 *err = SSL_R_STREAM_RESET;
2173 static int quic_read_actual(QCTX *ctx,
2174 QUIC_STREAM *stream,
2175 void *buf, size_t buf_len,
2179 int is_fin = 0, err, eos;
2180 QUIC_CONNECTION *qc = ctx->qc;
2182 if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2184 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2186 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2190 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2191 bytes_read, &is_fin))
2192 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2195 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2196 bytes_read, &is_fin))
2197 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2201 if (*bytes_read > 0) {
2203 * We have read at least one byte from the stream. Inform stream-level
2204 * RXFC of the retirement of controlled bytes. Update the active stream
2205 * status (the RXFC may now want to emit a frame granting more credit to
2208 OSSL_RTT_INFO rtt_info;
2210 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2212 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2213 rtt_info.smoothed_rtt))
2214 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2217 if (is_fin && !peek) {
2218 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2220 ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2223 if (*bytes_read > 0)
2224 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2232 static int quic_read_again(void *arg)
2234 struct quic_read_again_args *args = arg;
2236 if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
2237 /* If connection is torn down due to an error while blocking, stop. */
2238 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2242 if (!quic_read_actual(args->ctx, args->stream,
2243 args->buf, args->len, args->bytes_read,
2247 if (*args->bytes_read > 0)
2248 /* got at least one byte, the SSL_read op can finish now */
2251 return 0; /* did not read anything, keep trying */
2255 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2259 struct quic_read_again_args args;
2263 if (!expect_quic(s, &ctx))
2268 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2269 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2273 /* If we haven't finished the handshake, try to advance it. */
2274 if (quic_do_handshake(&ctx) < 1) {
2275 ret = 0; /* ossl_quic_do_handshake raised error here */
2279 if (ctx.xso == NULL) {
2281 * Called on a QCSO and we don't currently have a default stream.
2283 * Wait until we get a stream initiated by the peer (blocking mode) or
2284 * fail if we don't have one yet (non-blocking mode).
2286 if (!qc_wait_for_default_xso_for_read(&ctx)) {
2287 ret = 0; /* error already raised here */
2291 ctx.xso = ctx.qc->default_xso;
2294 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2295 ret = 0; /* quic_read_actual raised error here */
2299 if (*bytes_read > 0) {
2301 * Even though we succeeded, tick the reactor here to ensure we are
2302 * handling other aspects of the QUIC connection.
2304 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2306 } else if (xso_blocking_mode(ctx.xso)) {
2308 * We were not able to read anything immediately, so our stream
2309 * buffer is empty. This means we need to block until we get
2310 * at least one byte.
2313 args.stream = ctx.xso->stream;
2316 args.bytes_read = bytes_read;
2319 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2321 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2323 } else if (res < 0) {
2324 ret = 0; /* quic_read_again raised error here */
2331 * We did not get any bytes and are not in blocking mode.
2332 * Tick to see if this delivers any more.
2334 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2336 /* Try the read again. */
2337 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2338 ret = 0; /* quic_read_actual raised error here */
2342 if (*bytes_read > 0)
2343 ret = 1; /* Succeeded this time. */
2345 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2349 quic_unlock(ctx.qc);
2353 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2355 return quic_read(s, buf, len, bytes_read, 0);
2358 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2360 return quic_read(s, buf, len, bytes_read, 1);
2369 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2375 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2378 if (ctx.xso->stream == NULL
2379 || !ossl_quic_stream_has_recv_buffer(ctx.xso->stream))
2380 /* Cannot raise errors here because we are const, just fail. */
2383 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2386 if (avail == 0 && check_channel && ossl_quic_channel_has_pending(ctx.qc->ch))
2390 quic_unlock(ctx.qc);
2394 size_t ossl_quic_pending(const SSL *s)
2396 return ossl_quic_pending_int(s, /*check_channel=*/0);
2399 int ossl_quic_has_pending(const SSL *s)
2401 /* Do we have app-side pending data or pending URXEs or RXEs? */
2402 return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2406 * SSL_stream_conclude
2407 * -------------------
2410 int ossl_quic_conn_stream_conclude(SSL *s)
2416 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2419 qs = ctx.xso->stream;
2421 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
2422 quic_unlock(ctx.qc);
2423 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2426 if (!quic_validate_for_write(ctx.xso, &err)) {
2427 quic_unlock(ctx.qc);
2428 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2431 if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2432 quic_unlock(ctx.qc);
2436 ossl_quic_sstream_fin(qs->sstream);
2437 quic_post_write(ctx.xso, 1, 1);
2438 quic_unlock(ctx.qc);
2443 * SSL_inject_net_dgram
2444 * --------------------
2447 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2449 const BIO_ADDR *peer,
2450 const BIO_ADDR *local)
2456 if (!expect_quic(s, &ctx))
2461 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2462 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2464 quic_unlock(ctx.qc);
2469 * SSL_get0_connection
2470 * -------------------
2472 SSL *ossl_quic_get0_connection(SSL *s)
2476 if (!expect_quic(s, &ctx))
2479 return &ctx.qc->ssl;
2483 * SSL_get_stream_type
2484 * -------------------
2486 int ossl_quic_get_stream_type(SSL *s)
2490 if (!expect_quic(s, &ctx))
2491 return SSL_STREAM_TYPE_BIDI;
2493 if (ctx.xso == NULL) {
2495 * If deferred XSO creation has yet to occur, proceed according to the
2496 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2497 * what kind of stream will be created yet, so return BIDI on the basis
2498 * that at this time, the client still has the option of calling
2499 * SSL_read() or SSL_write() first.
2501 if (ctx.qc->default_xso_created
2502 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2503 return SSL_STREAM_TYPE_NONE;
2505 return SSL_STREAM_TYPE_BIDI;
2508 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2509 return SSL_STREAM_TYPE_BIDI;
2511 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2512 return SSL_STREAM_TYPE_READ;
2514 return SSL_STREAM_TYPE_WRITE;
2522 uint64_t ossl_quic_get_stream_id(SSL *s)
2527 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2530 id = ctx.xso->stream->id;
2531 quic_unlock(ctx.qc);
2537 * SSL_set_default_stream_mode
2538 * ---------------------------
2541 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2545 if (!expect_quic_conn_only(s, &ctx))
2550 if (ctx.qc->default_xso_created)
2551 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2552 "too late to change default stream mode");
2555 case SSL_DEFAULT_STREAM_MODE_NONE:
2556 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2557 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2558 ctx.qc->default_stream_mode = mode;
2561 quic_unlock(ctx.qc);
2562 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2563 "bad default stream type");
2566 quic_unlock(ctx.qc);
2575 SSL *ossl_quic_detach_stream(SSL *s)
2578 QUIC_XSO *xso = NULL;
2580 if (!expect_quic_conn_only(s, &ctx))
2585 /* Calling this function inhibits default XSO autocreation. */
2586 /* QC ref to any default XSO is transferred to us and to caller. */
2587 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2589 quic_unlock(ctx.qc);
2591 return xso != NULL ? &xso->ssl : NULL;
2599 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2605 if (!expect_quic_conn_only(conn, &ctx))
2608 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2609 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2610 "stream to attach must be a valid QUIC stream");
2612 xso = (QUIC_XSO *)stream;
2616 if (ctx.qc->default_xso != NULL) {
2617 quic_unlock(ctx.qc);
2618 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2619 "connection already has a default stream");
2623 * It is a caller error for the XSO being attached as a default XSO to have
2624 * more than one ref.
2626 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
2627 quic_unlock(ctx.qc);
2628 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2633 quic_unlock(ctx.qc);
2634 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2635 "stream being attached must have "
2636 "only 1 reference");
2639 /* Caller's reference to the XSO is transferred to us. */
2640 /* Calling this function inhibits default XSO autocreation. */
2641 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2643 quic_unlock(ctx.qc);
2648 * SSL_set_incoming_stream_policy
2649 * ------------------------------
2652 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2654 switch (qc->incoming_stream_policy) {
2655 case SSL_INCOMING_STREAM_POLICY_AUTO:
2656 if ((qc->default_xso == NULL && !qc->default_xso_created)
2657 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2658 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2660 return SSL_INCOMING_STREAM_POLICY_REJECT;
2663 return qc->incoming_stream_policy;
2668 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2670 int policy = qc_get_effective_incoming_stream_policy(qc);
2671 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2673 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2675 qc->incoming_stream_aec);
2679 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2685 if (!expect_quic_conn_only(s, &ctx))
2691 case SSL_INCOMING_STREAM_POLICY_AUTO:
2692 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2693 case SSL_INCOMING_STREAM_POLICY_REJECT:
2694 ctx.qc->incoming_stream_policy = policy;
2695 ctx.qc->incoming_stream_aec = aec;
2703 qc_update_reject_policy(ctx.qc);
2704 quic_unlock(ctx.qc);
2712 struct wait_for_incoming_stream_args {
2718 static int wait_for_incoming_stream(void *arg)
2720 struct wait_for_incoming_stream_args *args = arg;
2721 QUIC_CONNECTION *qc = args->ctx->qc;
2722 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2724 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2725 /* If connection is torn down due to an error while blocking, stop. */
2726 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2730 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2731 if (args->qs != NULL)
2732 return 1; /* got a stream */
2734 return 0; /* did not get a stream, keep trying */
2738 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2743 QUIC_STREAM_MAP *qsm;
2746 OSSL_RTT_INFO rtt_info;
2748 if (!expect_quic_conn_only(s, &ctx))
2753 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2754 == SSL_INCOMING_STREAM_POLICY_REJECT)
2757 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2759 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2761 if (qc_blocking_mode(ctx.qc)
2762 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2763 struct wait_for_incoming_stream_args args;
2768 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2770 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2772 } else if (ret < 0 || args.qs == NULL) {
2782 xso = create_xso_from_stream(ctx.qc, qs);
2786 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2787 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2788 rtt_info.smoothed_rtt);
2791 /* Calling this function inhibits default XSO autocreation. */
2792 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2795 quic_unlock(ctx.qc);
2800 * SSL_get_accept_stream_queue_len
2801 * -------------------------------
2804 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2809 if (!expect_quic_conn_only(s, &ctx))
2814 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2816 quic_unlock(ctx.qc);
2824 int ossl_quic_stream_reset(SSL *ssl,
2825 const SSL_STREAM_RESET_ARGS *args,
2829 QUIC_STREAM_MAP *qsm;
2831 uint64_t error_code;
2834 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2837 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2838 qs = ctx.xso->stream;
2839 error_code = (args != NULL ? args->quic_error_code : 0);
2841 if (!quic_validate_for_write(ctx.xso, &err)) {
2842 ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2846 ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2849 quic_unlock(ctx.qc);
2854 * SSL_get_stream_read_state
2855 * -------------------------
2857 static void quic_classify_stream(QUIC_CONNECTION *qc,
2861 uint64_t *app_error_code)
2864 uint64_t final_size;
2866 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2868 if (app_error_code != NULL)
2869 *app_error_code = UINT64_MAX;
2871 app_error_code = &final_size; /* throw away value */
2873 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2875 * Unidirectional stream and this direction of transmission doesn't
2878 *state = SSL_STREAM_STATE_WRONG_DIR;
2879 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2880 /* Connection already closed. */
2881 *state = SSL_STREAM_STATE_CONN_CLOSED;
2882 } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
2883 /* Application has read a FIN. */
2884 *state = SSL_STREAM_STATE_FINISHED;
2885 } else if ((!is_write && qs->stop_sending)
2886 || (is_write && ossl_quic_stream_send_is_reset(qs))) {
2888 * Stream has been reset locally. FIN takes precedence over this for the
2889 * read case as the application need not care if the stream is reset
2890 * after a FIN has been successfully processed.
2892 *state = SSL_STREAM_STATE_RESET_LOCAL;
2893 *app_error_code = !is_write
2894 ? qs->stop_sending_aec
2895 : qs->reset_stream_aec;
2896 } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
2897 || (is_write && qs->peer_stop_sending)) {
2899 * Stream has been reset remotely. */
2900 *state = SSL_STREAM_STATE_RESET_REMOTE;
2901 *app_error_code = !is_write
2902 ? qs->peer_reset_stream_aec
2903 : qs->peer_stop_sending_aec;
2904 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2907 * Stream has been finished. Stream reset takes precedence over this for
2908 * the write case as peer may not have received all data.
2910 *state = SSL_STREAM_STATE_FINISHED;
2912 /* Stream still healthy. */
2913 *state = SSL_STREAM_STATE_OK;
2917 static int quic_get_stream_state(SSL *ssl, int is_write)
2922 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2923 return SSL_STREAM_STATE_NONE;
2925 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2926 quic_unlock(ctx.qc);
2930 int ossl_quic_get_stream_read_state(SSL *ssl)
2932 return quic_get_stream_state(ssl, /*is_write=*/0);
2936 * SSL_get_stream_write_state
2937 * --------------------------
2939 int ossl_quic_get_stream_write_state(SSL *ssl)
2941 return quic_get_stream_state(ssl, /*is_write=*/1);
2945 * SSL_get_stream_read_error_code
2946 * ------------------------------
2948 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2949 uint64_t *app_error_code)
2954 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2957 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2958 &state, app_error_code);
2960 quic_unlock(ctx.qc);
2962 case SSL_STREAM_STATE_FINISHED:
2964 case SSL_STREAM_STATE_RESET_LOCAL:
2965 case SSL_STREAM_STATE_RESET_REMOTE:
2972 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2974 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2978 * SSL_get_stream_write_error_code
2979 * -------------------------------
2981 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2983 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2987 * Write buffer size mutation
2988 * --------------------------
2990 int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
2995 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2998 if (!ossl_quic_stream_has_send(ctx.xso->stream))
2999 /* Called on a unidirectional receive-only stream - error. */
3002 if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
3004 * If the stream has a send part but we have disposed of it because we
3005 * no longer need it, this is a no-op.
3011 if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size))
3017 quic_unlock(ctx.qc);
3022 * SSL_get_conn_close_info
3023 * -----------------------
3025 int ossl_quic_get_conn_close_info(SSL *ssl,
3026 SSL_CONN_CLOSE_INFO *info,
3030 const QUIC_TERMINATE_CAUSE *tc;
3032 if (!expect_quic_conn_only(ssl, &ctx))
3035 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
3039 info->error_code = tc->error_code;
3040 info->reason = NULL; /* TODO(QUIC): Wire reason */
3041 info->reason_len = 0;
3042 info->is_local = !tc->remote;
3043 info->is_transport = !tc->app;
3051 int ossl_quic_key_update(SSL *ssl, int update_type)
3055 if (!expect_quic_conn_only(ssl, &ctx))
3058 switch (update_type) {
3059 case SSL_KEY_UPDATE_NOT_REQUESTED:
3061 * QUIC signals peer key update implicily by triggering a local
3062 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3064 case SSL_KEY_UPDATE_REQUESTED:
3068 /* Unknown type - error. */
3074 /* Attempt to perform a TXKU. */
3075 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
3076 quic_unlock(ctx.qc);
3080 quic_unlock(ctx.qc);
3085 * SSL_get_key_update_type
3086 * -----------------------
3088 int ossl_quic_get_key_update_type(const SSL *s)
3091 * We always handle key updates immediately so a key update is never
3094 return SSL_KEY_UPDATE_NONE;
3098 * QUIC Front-End I/O API: SSL_CTX Management
3099 * ==========================================
3102 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3106 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
3110 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3114 if (!expect_quic_conn_only(s, &ctx))
3118 case SSL_CTRL_SET_MSG_CALLBACK:
3119 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
3121 /* This callback also needs to be set on the internal SSL object */
3122 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
3125 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3126 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
3130 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3132 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
3135 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
3137 /* We never do renegotiation. */
3142 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3143 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3146 int ossl_quic_num_ciphers(void)
3151 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
3157 * Internal Testing APIs
3158 * =====================
3161 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
3165 if (!expect_quic_conn_only(s, &ctx))