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, int set_last_error,
142 if (set_last_error && ctx->is_stream && ctx->xso != NULL)
143 ctx->xso->last_error = SSL_ERROR_SSL;
144 else if (set_last_error && !ctx->is_stream && ctx->qc != NULL)
145 ctx->qc->last_error = SSL_ERROR_SSL;
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), 1, \
166 OPENSSL_FILE, OPENSSL_LINE, \
171 #define QUIC_RAISE_NON_IO_ERROR(ctx, reason, msg) \
172 quic_raise_non_normal_error((ctx), 0, \
173 OPENSSL_FILE, OPENSSL_LINE, \
179 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
180 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
182 * After this returns 1, all fields of the passed QCTX are initialised.
183 * Returns 0 on failure. This function is intended to be used to provide API
184 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
186 static int expect_quic(const SSL *s, QCTX *ctx)
196 return QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
199 case SSL_TYPE_QUIC_CONNECTION:
200 qc = (QUIC_CONNECTION *)s;
202 ctx->xso = qc->default_xso;
206 case SSL_TYPE_QUIC_XSO:
214 return QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
219 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
220 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
223 * remote_init determines if we expect the default XSO to be remotely created or
224 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
226 * Channel mutex is acquired and retained on success.
229 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
232 if (!expect_quic(s, ctx))
237 if (ctx->xso == NULL && remote_init >= 0) {
238 if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) {
239 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
243 /* If we haven't finished the handshake, try to advance it. */
244 if (quic_do_handshake(ctx) < 1)
245 /* ossl_quic_do_handshake raised error here */
248 if (remote_init == 0) {
249 if (!qc_try_create_default_xso_for_write(ctx))
252 if (!qc_wait_for_default_xso_for_read(ctx))
256 ctx->xso = ctx->qc->default_xso;
259 if (ctx->xso == NULL) {
260 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_NO_STREAM, NULL);
264 return 1; /* coverity[missing_unlock]: lock held */
267 quic_unlock(ctx->qc);
272 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
273 * be non-NULL if the QCSO has a default stream.
275 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
277 if (!expect_quic(s, ctx))
281 return QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
287 * Ensures that the channel mutex is held for a method which touches channel
290 * Precondition: Channel mutex is not held (unchecked)
292 static void quic_lock(QUIC_CONNECTION *qc)
294 #if defined(OPENSSL_THREADS)
295 ossl_crypto_mutex_lock(qc->mutex);
299 /* Precondition: Channel mutex is held (unchecked) */
301 static void quic_unlock(QUIC_CONNECTION *qc)
303 #if defined(OPENSSL_THREADS)
304 ossl_crypto_mutex_unlock(qc->mutex);
309 * This predicate is the criterion which should determine API call rejection for
310 * *most* mutating API calls, particularly stream-related operations for send
313 * A call is rejected (this function returns 0) if shutdown is in progress
314 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
315 * req_active=1, the connection must be active (i.e., the IDLE state is also
318 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
320 if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
323 if (req_active && !ossl_quic_channel_is_active(qc->ch))
330 * QUIC Front-End I/O API: Initialization
331 * ======================================
333 * SSL_new => ossl_quic_new
335 * SSL_reset => ossl_quic_reset
336 * SSL_clear => ossl_quic_clear
338 * SSL_free => ossl_quic_free
340 * SSL_set_options => ossl_quic_set_options
341 * SSL_get_options => ossl_quic_get_options
342 * SSL_clear_options => ossl_quic_clear_options
347 SSL *ossl_quic_new(SSL_CTX *ctx)
349 QUIC_CONNECTION *qc = NULL;
350 SSL *ssl_base = NULL;
351 SSL_CONNECTION *sc = NULL;
353 qc = OPENSSL_zalloc(sizeof(*qc));
355 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
359 /* Initialise the QUIC_CONNECTION's stub header. */
361 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
363 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
367 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
368 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
369 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
373 /* override the user_ssl of the inner connection */
374 sc->s3.flags |= TLS1_FLAGS_QUIC;
376 /* Restrict options derived from the SSL_CTX. */
377 sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
380 #if defined(OPENSSL_THREADS)
381 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) {
382 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
387 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
388 qc->is_thread_assisted
389 = (ssl_base->method == OSSL_QUIC_client_thread_method());
392 qc->as_server = 0; /* TODO(QUIC SERVER): add server support */
393 qc->as_server_state = qc->as_server;
395 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
396 qc->default_ssl_mode = qc->ssl.ctx->mode;
397 qc->default_ssl_options = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
398 qc->default_blocking = 1;
400 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
401 qc->last_error = SSL_ERROR_NONE;
403 if (!create_channel(qc))
406 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
407 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
409 qc_update_reject_policy(qc);
412 * We do not create the default XSO yet. The reason for this is that the
413 * stream ID of the default XSO will depend on whether the stream is client
414 * or server-initiated, which depends on who transmits first. Since we do
415 * not know whether the application will be using a client-transmits-first
416 * or server-transmits-first protocol, we defer default XSO creation until
417 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
418 * we take that as a cue that the client is expecting a server-initiated
419 * stream, and vice versa if SSL_write() is called first.
425 #if defined(OPENSSL_THREADS)
426 ossl_crypto_mutex_free(qc->mutex);
428 ossl_quic_channel_free(qc->ch);
437 void ossl_quic_free(SSL *s)
442 /* We should never be called on anything but a QSO. */
443 if (!expect_quic(s, &ctx))
450 * When a QSSO is freed, the XSO is freed immediately, because the XSO
451 * itself only contains API personality layer data. However the
452 * underlying QUIC_STREAM is not freed immediately but is instead marked
453 * as deleted for later collection.
456 assert(ctx.qc->num_xso > 0);
459 /* If a stream's send part has not been finished, auto-reset it. */
460 if (( ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
461 || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
462 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
463 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
466 /* Do STOP_SENDING for the receive part, if applicable. */
467 if ( ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
468 || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
469 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
472 /* Update stream state. */
473 ctx.xso->stream->deleted = 1;
474 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
477 is_default = (ctx.xso == ctx.qc->default_xso);
481 * Unref the connection in most cases; the XSO has a ref to the QC and
482 * not vice versa. But for a default XSO, to avoid circular references,
483 * the QC refs the XSO but the XSO does not ref the QC. If we are the
484 * default XSO, we only get here when the QC is being torn down anyway,
485 * so don't call SSL_free(qc) as we are already in it.
488 SSL_free(&ctx.qc->ssl);
490 /* Note: SSL_free calls OPENSSL_free(xso) for us */
495 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
496 * stage, but is freed during the channel free when the whole QSM is freed.
498 if (ctx.qc->default_xso != NULL) {
499 QUIC_XSO *xso = ctx.qc->default_xso;
504 ctx.qc->default_xso = NULL;
507 /* Ensure we have no remaining XSOs. */
508 assert(ctx.qc->num_xso == 0);
510 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
511 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
512 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
513 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
517 ossl_quic_channel_free(ctx.qc->ch);
519 BIO_free(ctx.qc->net_rbio);
520 BIO_free(ctx.qc->net_wbio);
522 /* Note: SSL_free calls OPENSSL_free(qc) for us */
524 SSL_free(ctx.qc->tls);
525 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
526 #if defined(OPENSSL_THREADS)
527 ossl_crypto_mutex_free(&ctx.qc->mutex);
531 /* SSL method init */
532 int ossl_quic_init(SSL *s)
534 /* Same op as SSL_clear, forward the call. */
535 return ossl_quic_clear(s);
538 /* SSL method deinit */
539 void ossl_quic_deinit(SSL *s)
544 /* SSL_clear (ssl_reset method) */
545 int ossl_quic_reset(SSL *s)
549 if (!expect_quic(s, &ctx))
552 ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
556 /* ssl_clear method (unused) */
557 int ossl_quic_clear(SSL *s)
561 if (!expect_quic(s, &ctx))
564 /* TODO(QUIC FUTURE): Currently a no-op. */
568 int ossl_quic_conn_set_override_now_cb(SSL *s,
569 OSSL_TIME (*now_cb)(void *arg),
574 if (!expect_quic(s, &ctx))
579 ctx.qc->override_now_cb = now_cb;
580 ctx.qc->override_now_cb_arg = now_cb_arg;
586 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
590 if (!expect_quic(s, &ctx))
593 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
594 if (ctx.qc->is_thread_assisted && ctx.qc->started)
595 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
600 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
602 qc->default_xso_created = 1;
603 qc_update_reject_policy(qc);
607 * Changes default XSO. Allows caller to keep reference to the old default XSO
608 * (if any). Reference to new XSO is transferred from caller.
611 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
619 if (qc->default_xso != xso) {
620 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
622 qc->default_xso = xso;
626 * Changing to not having a default XSO. XSO becomes standalone and
627 * now has a ref to the QC.
629 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
633 * Changing from not having a default XSO to having one. The new XSO
634 * will have had a reference to the QC we need to drop to avoid a
635 * circular reference.
637 * Currently we never change directly from one default XSO to
638 * another, though this function would also still be correct if this
641 assert(*old_xso == NULL);
643 CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
649 qc_touch_default_xso(qc);
653 * Changes default XSO, releasing the reference to any previous default XSO.
654 * Reference to new XSO is transferred from caller.
657 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
659 QUIC_XSO *old_xso = NULL;
661 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
664 SSL_free(&old_xso->ssl);
668 static void xso_update_options(QUIC_XSO *xso)
670 int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
672 if (xso->stream->rstream != NULL)
673 ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
675 if (xso->stream->sstream != NULL)
676 ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
683 * Setting options on a QCSO
684 * - configures the handshake-layer options;
685 * - configures the default data-plane options for new streams;
686 * - configures the data-plane options on the default XSO, if there is one.
688 * Setting options on a QSSO
689 * - configures data-plane options for that stream only.
692 static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
695 uint64_t hs_mask_value, hs_or_value, ret;
697 if (!expect_quic(ssl, &ctx))
702 if (!ctx.is_stream) {
704 * If we were called on the connection, we apply any handshake option
707 hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
708 hs_or_value = (or_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
710 SSL_clear_options(ctx.qc->tls, hs_mask_value);
711 SSL_set_options(ctx.qc->tls, hs_or_value);
713 /* Update defaults for new streams. */
714 ctx.qc->default_ssl_options
715 = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
716 & OSSL_QUIC_PERMITTED_OPTIONS;
719 if (ctx.xso != NULL) {
721 = ((ctx.xso->ssl_options & ~mask_value) | or_value)
722 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
724 xso_update_options(ctx.xso);
727 ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;
733 uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
735 return quic_mask_or_options(ssl, 0, options);
738 /* SSL_clear_options */
739 uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
741 return quic_mask_or_options(ssl, options, 0);
744 /* SSL_get_options */
745 uint64_t ossl_quic_get_options(const SSL *ssl)
747 return quic_mask_or_options((SSL *)ssl, 0, 0);
751 * QUIC Front-End I/O API: Network BIO Configuration
752 * =================================================
754 * Handling the different BIOs is difficult:
756 * - It is more or less a requirement that we use non-blocking network I/O;
757 * we need to be able to have timeouts on recv() calls, and make best effort
758 * (non blocking) send() and recv() calls.
760 * The only sensible way to do this is to configure the socket into
761 * non-blocking mode. We could try to do select() before calling send() or
762 * recv() to get a guarantee that the call will not block, but this will
763 * probably run into issues with buggy OSes which generate spurious socket
764 * readiness events. In any case, relying on this to work reliably does not
767 * Timeouts could be handled via setsockopt() socket timeout options, but
768 * this depends on OS support and adds another syscall to every network I/O
769 * operation. It also has obvious thread safety concerns if we want to move
770 * to concurrent use of a single socket at some later date.
772 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
773 * be made non-blocking. However some OSes (e.g. Windows) do not support
774 * this, so we cannot rely on this.
776 * As such, we need to configure any FD in non-blocking mode. This may
777 * confound users who pass a blocking socket to libssl. However, in practice
778 * it would be extremely strange for a user of QUIC to pass an FD to us,
779 * then also try and send receive traffic on the same socket(!). Thus the
780 * impact of this should be limited, and can be documented.
782 * - We support both blocking and non-blocking operation in terms of the API
783 * presented to the user. One prospect is to set the blocking mode based on
784 * whether the socket passed to us was already in blocking mode. However,
785 * Windows has no API for determining if a socket is in blocking mode (!),
786 * therefore this cannot be done portably. Currently therefore we expose an
787 * explicit API call to set this, and default to blocking mode.
789 * - We need to determine our initial destination UDP address. The "natural"
790 * way for a user to do this is to set the peer variable on a BIO_dgram.
791 * However, this has problems because BIO_dgram's peer variable is used for
792 * both transmission and reception. This means it can be constantly being
793 * changed to a malicious value (e.g. if some random unrelated entity on the
794 * network starts sending traffic to us) on every read call. This is not a
795 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
796 * calls only, which do not use this variable. However, we do need to let
797 * the user specify the peer in a 'normal' manner. The compromise here is
798 * that we grab the current peer value set at the time the write BIO is set
799 * and do not read the value again.
801 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
802 * Currently we do this by only supporting non-blocking mode.
807 * Determines what initial destination UDP address we should use, if possible.
808 * If this fails the client must set the destination address manually, or use a
809 * BIO which does not need a destination address.
811 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
813 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
819 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
823 if (!expect_quic(s, &ctx))
826 if (ctx.qc->net_rbio == net_rbio)
829 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
832 BIO_free(ctx.qc->net_rbio);
833 ctx.qc->net_rbio = net_rbio;
836 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
837 * mode as we do not support it for non-pollable BIOs.
839 if (net_rbio != NULL) {
840 BIO_POLL_DESCRIPTOR d = {0};
842 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
843 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
844 ctx.qc->blocking = 0;
845 ctx.qc->default_blocking = 0;
846 ctx.qc->can_poll_net_rbio = 0;
848 ctx.qc->can_poll_net_rbio = 1;
853 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
857 if (!expect_quic(s, &ctx))
860 if (ctx.qc->net_wbio == net_wbio)
863 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
866 BIO_free(ctx.qc->net_wbio);
867 ctx.qc->net_wbio = net_wbio;
869 if (net_wbio != NULL) {
870 BIO_POLL_DESCRIPTOR d = {0};
872 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
873 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
874 ctx.qc->blocking = 0;
875 ctx.qc->default_blocking = 0;
876 ctx.qc->can_poll_net_wbio = 0;
878 ctx.qc->can_poll_net_wbio = 1;
882 * If we do not have a peer address yet, and we have not started trying
883 * to connect yet, try to autodetect one.
885 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
886 && !ctx.qc->started) {
887 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
889 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
891 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
892 &ctx.qc->init_peer_addr);
897 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
901 if (!expect_quic(s, &ctx))
904 return ctx.qc->net_rbio;
907 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
911 if (!expect_quic(s, &ctx))
914 return ctx.qc->net_wbio;
917 int ossl_quic_conn_get_blocking_mode(const SSL *s)
921 if (!expect_quic(s, &ctx))
925 return ctx.xso->blocking;
927 return ctx.qc->blocking;
930 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
934 if (!expect_quic(s, &ctx))
937 /* Cannot enable blocking mode if we do not have pollable FDs. */
939 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
940 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
942 if (!ctx.is_stream) {
944 * If called on a QCSO, update default and connection-level blocking
947 ctx.qc->blocking = (blocking != 0);
948 ctx.qc->default_blocking = ctx.qc->blocking;
953 * If called on a QSSO or QCSO with a default XSO, update blocking
956 ctx.xso->blocking = (blocking != 0);
961 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
962 const BIO_ADDR *peer_addr)
966 if (!expect_quic(s, &ctx))
970 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
973 if (peer_addr == NULL) {
974 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
978 ctx.qc->init_peer_addr = *peer_addr;
983 * QUIC Front-End I/O API: Asynchronous I/O Management
984 * ===================================================
986 * (BIO/)SSL_handle_events => ossl_quic_handle_events
987 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
988 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
992 /* Returns 1 if the connection is being used in blocking mode. */
993 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
998 static int xso_blocking_mode(const QUIC_XSO *xso)
1000 return xso->blocking
1001 && xso->conn->can_poll_net_rbio
1002 && xso->conn->can_poll_net_wbio;
1005 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
1007 int ossl_quic_handle_events(SSL *s)
1011 if (!expect_quic(s, &ctx))
1015 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1016 quic_unlock(ctx.qc);
1021 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1022 * should next have events handled by the application by calling
1023 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1024 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1025 * the value of *tv is undefined.
1028 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1031 OSSL_TIME deadline = ossl_time_infinite();
1033 if (!expect_quic(s, &ctx))
1039 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
1041 if (ossl_time_is_infinite(deadline)) {
1045 * Robustness against faulty applications that don't check *is_infinite;
1046 * harmless long timeout.
1048 tv->tv_sec = 1000000;
1051 quic_unlock(ctx.qc);
1055 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
1057 quic_unlock(ctx.qc);
1061 /* SSL_get_rpoll_descriptor */
1062 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1066 if (!expect_quic(s, &ctx))
1069 if (desc == NULL || ctx.qc->net_rbio == NULL)
1070 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1073 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
1076 /* SSL_get_wpoll_descriptor */
1077 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1081 if (!expect_quic(s, &ctx))
1084 if (desc == NULL || ctx.qc->net_wbio == NULL)
1085 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1088 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
1091 /* SSL_net_read_desired */
1093 int ossl_quic_get_net_read_desired(SSL *s)
1098 if (!expect_quic(s, &ctx))
1102 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1103 quic_unlock(ctx.qc);
1107 /* SSL_net_write_desired */
1109 int ossl_quic_get_net_write_desired(SSL *s)
1114 if (!expect_quic(s, &ctx))
1118 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1119 quic_unlock(ctx.qc);
1124 * QUIC Front-End I/O API: Connection Lifecycle Operations
1125 * =======================================================
1127 * SSL_do_handshake => ossl_quic_do_handshake
1128 * SSL_set_connect_state => ossl_quic_set_connect_state
1129 * SSL_set_accept_state => ossl_quic_set_accept_state
1130 * SSL_shutdown => ossl_quic_shutdown
1131 * SSL_ctrl => ossl_quic_ctrl
1132 * (BIO/)SSL_connect => ossl_quic_connect
1133 * (BIO/)SSL_accept => ossl_quic_accept
1138 static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1140 QUIC_STREAM_MAP *qsm;
1142 if (qc->shutting_down)
1145 qsm = ossl_quic_channel_get_qsm(qc->ch);
1147 ossl_quic_stream_map_begin_shutdown_flush(qsm);
1148 qc->shutting_down = 1;
1151 /* Returns 1 if all shutdown-flush streams have been done with. */
1153 static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1155 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1157 return qc->shutting_down
1158 && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1162 static int quic_shutdown_wait(void *arg)
1164 QUIC_CONNECTION *qc = arg;
1166 return ossl_quic_channel_is_terminated(qc->ch);
1169 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
1170 static int quic_shutdown_flush_wait(void *arg)
1172 QUIC_CONNECTION *qc = arg;
1174 return ossl_quic_channel_is_term_any(qc->ch)
1175 || qc_shutdown_flush_finished(qc);
1179 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1180 const SSL_SHUTDOWN_EX_ARGS *args,
1185 int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1187 if (!expect_quic(s, &ctx))
1190 if (ctx.is_stream) {
1191 QUIC_RAISE_NON_IO_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
1197 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1198 quic_unlock(ctx.qc);
1202 /* Phase 1: Stream Flushing */
1204 qc_shutdown_flush_init(ctx.qc);
1206 if (!qc_shutdown_flush_finished(ctx.qc)) {
1207 if (qc_blocking_mode(ctx.qc)) {
1208 ret = block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);
1214 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1218 if (!qc_shutdown_flush_finished(ctx.qc)) {
1219 quic_unlock(ctx.qc);
1220 return 0; /* ongoing */
1224 /* Phase 2: Connection Closure */
1225 ossl_quic_channel_local_close(ctx.qc->ch,
1226 args != NULL ? args->quic_error_code : 0,
1227 args != NULL ? args->quic_reason : NULL);
1229 SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1231 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1232 quic_unlock(ctx.qc);
1236 /* Phase 3: Terminating Wait Time */
1237 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1238 ret = block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1244 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1247 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1249 quic_unlock(ctx.qc);
1254 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1258 if (!expect_quic(s, &ctx))
1263 /* If called on a QCSO, update the default mode. */
1265 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1268 * If we were called on a QSSO or have a default stream, we also update
1271 if (ctx.xso != NULL) {
1272 /* Cannot enable EPW while AON write in progress. */
1273 if (ctx.xso->aon_write_in_progress)
1274 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1276 ctx.xso->ssl_mode |= (uint32_t)larg;
1277 return ctx.xso->ssl_mode;
1280 return ctx.qc->default_ssl_mode;
1281 case SSL_CTRL_CLEAR_MODE:
1283 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1285 if (ctx.xso != NULL) {
1286 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1287 return ctx.xso->ssl_mode;
1290 return ctx.qc->default_ssl_mode;
1292 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1293 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1294 /* This ctrl also needs to be passed to the internal SSL object */
1295 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1297 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1301 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1304 return !is_infinite;
1306 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1307 /* For legacy compatibility with DTLS calls. */
1308 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1310 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1311 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1315 /* SSL_set_connect_state */
1316 void ossl_quic_set_connect_state(SSL *s)
1320 if (!expect_quic(s, &ctx))
1323 /* Cannot be changed after handshake started */
1324 if (ctx.qc->started || ctx.is_stream)
1327 ctx.qc->as_server_state = 0;
1330 /* SSL_set_accept_state */
1331 void ossl_quic_set_accept_state(SSL *s)
1335 if (!expect_quic(s, &ctx))
1338 /* Cannot be changed after handshake started */
1339 if (ctx.qc->started || ctx.is_stream)
1342 ctx.qc->as_server_state = 1;
1345 /* SSL_do_handshake */
1346 struct quic_handshake_wait_args {
1347 QUIC_CONNECTION *qc;
1350 static int quic_handshake_wait(void *arg)
1352 struct quic_handshake_wait_args *args = arg;
1354 if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1357 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1363 static int configure_channel(QUIC_CONNECTION *qc)
1365 assert(qc->ch != NULL);
1367 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1368 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1369 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1376 static int create_channel(QUIC_CONNECTION *qc)
1378 QUIC_CHANNEL_ARGS args = {0};
1380 args.libctx = qc->ssl.ctx->libctx;
1381 args.propq = qc->ssl.ctx->propq;
1382 args.is_server = qc->as_server;
1384 args.mutex = qc->mutex;
1385 args.now_cb = get_time_cb;
1386 args.now_cb_arg = qc;
1388 qc->ch = ossl_quic_channel_new(&args);
1389 if (qc->ch == NULL) {
1390 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1398 * Creates a channel and configures it with the information we have accumulated
1399 * via calls made to us from the application prior to starting a handshake
1403 static int ensure_channel_started(QUIC_CONNECTION *qc)
1406 if (!configure_channel(qc)
1407 || !ossl_quic_channel_start(qc->ch))
1410 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1411 if (qc->is_thread_assisted)
1412 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1421 ossl_quic_channel_free(qc->ch);
1427 static int quic_do_handshake(QCTX *ctx)
1430 QUIC_CONNECTION *qc = ctx->qc;
1432 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1433 /* Handshake already completed. */
1436 if (!quic_mutation_allowed(qc, /*req_active=*/0))
1437 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1439 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1440 /* Peer address must have been set. */
1441 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1442 return -1; /* Non-protocol error */
1445 if (qc->as_server != qc->as_server_state) {
1446 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1447 return -1; /* Non-protocol error */
1450 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1451 /* Need read and write BIOs. */
1452 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1453 return -1; /* Non-protocol error */
1457 * Start connection process. Note we may come here multiple times in
1458 * non-blocking mode, which is fine.
1460 if (!ensure_channel_started(qc)) {
1461 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1462 return -1; /* Non-protocol error */
1465 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1466 /* The handshake is now done. */
1469 if (qc_blocking_mode(qc)) {
1470 /* In blocking mode, wait for the handshake to complete. */
1471 struct quic_handshake_wait_args args;
1475 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1476 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1477 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1478 return 0; /* Shutdown before completion */
1479 } else if (ret <= 0) {
1480 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1481 return -1; /* Non-protocol error */
1484 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1487 /* Try to advance the reactor. */
1488 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1490 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1491 /* The handshake is now done. */
1494 /* Otherwise, indicate that the handshake isn't done yet. */
1495 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1496 return -1; /* Non-protocol error */
1501 int ossl_quic_do_handshake(SSL *s)
1506 if (!expect_quic(s, &ctx))
1511 ret = quic_do_handshake(&ctx);
1512 quic_unlock(ctx.qc);
1517 int ossl_quic_connect(SSL *s)
1519 /* Ensure we are in connect state (no-op if non-idle). */
1520 ossl_quic_set_connect_state(s);
1522 /* Begin or continue the handshake */
1523 return ossl_quic_do_handshake(s);
1527 int ossl_quic_accept(SSL *s)
1529 /* Ensure we are in accept state (no-op if non-idle). */
1530 ossl_quic_set_accept_state(s);
1532 /* Begin or continue the handshake */
1533 return ossl_quic_do_handshake(s);
1537 * QUIC Front-End I/O API: Stream Lifecycle Operations
1538 * ===================================================
1540 * SSL_stream_new => ossl_quic_conn_stream_new
1545 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1546 * default XSO was created. Returns 0 if it was not (e.g. because it already
1547 * exists). Note that this is NOT an error condition.
1550 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1553 QUIC_CONNECTION *qc = ctx->qc;
1555 if (qc->default_xso_created
1556 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1558 * We only do this once. If the user detaches a previously created
1559 * default XSO we don't auto-create another one.
1561 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1563 /* Create a locally-initiated stream. */
1564 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1565 flags |= SSL_STREAM_FLAG_UNI;
1567 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1570 if (qc->default_xso == NULL)
1571 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1573 qc_touch_default_xso(qc);
1577 struct quic_wait_for_stream_args {
1578 QUIC_CONNECTION *qc;
1585 static int quic_wait_for_stream(void *arg)
1587 struct quic_wait_for_stream_args *args = arg;
1589 if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
1590 /* If connection is torn down due to an error while blocking, stop. */
1591 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1595 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1596 args->expect_id | QUIC_STREAM_DIR_BIDI);
1597 if (args->qs == NULL)
1598 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1599 args->expect_id | QUIC_STREAM_DIR_UNI);
1601 if (args->qs != NULL)
1602 return 1; /* stream now exists */
1604 return 0; /* did not get a stream, keep trying */
1608 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1610 /* Called on a QCSO and we don't currently have a default stream. */
1612 QUIC_CONNECTION *qc = ctx->qc;
1615 struct quic_wait_for_stream_args wargs;
1618 * If default stream functionality is disabled or we already detached
1619 * one, don't make another default stream and just fail.
1621 if (qc->default_xso_created
1622 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1623 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1626 * The peer may have opened a stream since we last ticked. So tick and
1627 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1628 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1629 * first stream created by a peer must have an ordinal of 0.
1631 expect_id = qc->as_server
1632 ? QUIC_STREAM_INITIATOR_CLIENT
1633 : QUIC_STREAM_INITIATOR_SERVER;
1635 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1636 expect_id | QUIC_STREAM_DIR_BIDI);
1638 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1639 expect_id | QUIC_STREAM_DIR_UNI);
1642 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1644 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1649 if (!qc_blocking_mode(qc))
1650 /* Non-blocking mode, so just bail immediately. */
1651 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1653 /* Block until we have a stream. */
1657 wargs.expect_id = expect_id;
1659 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1661 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1662 else if (res < 0 || wargs.qs == NULL)
1663 /* quic_wait_for_stream raised error here */
1670 * We now have qs != NULL. Make it the default stream, creating the
1673 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1674 if (qc->default_xso == NULL)
1675 return QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1677 qc_touch_default_xso(qc); /* inhibits default XSO */
1682 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1684 QUIC_XSO *xso = NULL;
1686 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
1687 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
1691 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) {
1692 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1697 if (!SSL_up_ref(&qc->ssl)) {
1698 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_SSL_LIB, NULL);
1703 xso->blocking = qc->default_blocking;
1704 xso->ssl_mode = qc->default_ssl_mode;
1706 = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1707 xso->last_error = SSL_ERROR_NONE;
1712 xso_update_options(xso);
1720 struct quic_new_stream_wait_args {
1721 QUIC_CONNECTION *qc;
1725 static int quic_new_stream_wait(void *arg)
1727 struct quic_new_stream_wait_args *args = arg;
1728 QUIC_CONNECTION *qc = args->qc;
1730 if (!quic_mutation_allowed(qc, /*req_active=*/1))
1733 if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
1739 /* locking depends on need_lock */
1740 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1743 QUIC_CONNECTION *qc = ctx->qc;
1744 QUIC_XSO *xso = NULL;
1745 QUIC_STREAM *qs = NULL;
1746 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1747 int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
1748 int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
1753 if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
1754 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1759 && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
1760 struct quic_new_stream_wait_args args;
1763 * Stream count flow control currently doesn't permit this stream to be
1766 if (no_blocking || !qc_blocking_mode(qc)) {
1767 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
1772 args.is_uni = is_uni;
1774 /* Blocking mode - wait until we can get a stream. */
1775 ret = block_until_pred(ctx->qc, quic_new_stream_wait, &args, 0);
1776 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1777 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1778 goto err; /* Shutdown before completion */
1779 } else if (ret <= 0) {
1780 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1781 goto err; /* Non-protocol error */
1785 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1787 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1791 xso = create_xso_from_stream(qc, qs);
1795 qc_touch_default_xso(qc); /* inhibits default XSO */
1803 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1812 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1816 if (!expect_quic_conn_only(s, &ctx))
1819 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1823 * QUIC Front-End I/O API: Steady-State Operations
1824 * ===============================================
1826 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1827 * is, the functions used during the established phase of a QUIC connection
1828 * (e.g. SSL_read, SSL_write).
1830 * Each function must handle both blocking and non-blocking modes. As discussed
1831 * above, all QUIC I/O is implemented using non-blocking mode internally.
1833 * SSL_get_error => partially implemented by ossl_quic_get_error
1834 * (BIO/)SSL_read => ossl_quic_read
1835 * (BIO/)SSL_write => ossl_quic_write
1836 * SSL_pending => ossl_quic_pending
1837 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1838 * SSL_key_update => ossl_quic_key_update
1842 int ossl_quic_get_error(const SSL *s, int i)
1845 int net_error, last_error;
1847 if (!expect_quic(s, &ctx))
1851 net_error = ossl_quic_channel_net_error(ctx.qc->ch);
1852 last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1853 quic_unlock(ctx.qc);
1856 return SSL_ERROR_SYSCALL;
1865 * The set of functions below provide the implementation of the public SSL_write
1866 * function. We must handle:
1868 * - both blocking and non-blocking operation at the application level,
1869 * depending on how we are configured;
1871 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1873 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1877 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1880 * We have appended at least one byte to the stream.
1881 * Potentially mark stream as active, depending on FC.
1884 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1890 * TODO(QUIC FUTURE): It is probably inefficient to try and do this
1891 * immediately, plus we should eventually consider Nagle's algorithm.
1894 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1897 struct quic_write_again_args {
1899 const unsigned char *buf;
1901 size_t total_written;
1906 static int quic_write_again(void *arg)
1908 struct quic_write_again_args *args = arg;
1909 size_t actual_written = 0;
1911 if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
1912 /* If connection is torn down due to an error while blocking, stop. */
1915 if (!quic_validate_for_write(args->xso, &args->err))
1917 * Stream may have become invalid for write due to connection events
1922 args->err = ERR_R_INTERNAL_ERROR;
1923 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1924 args->buf, args->len, &actual_written))
1927 quic_post_write(args->xso, actual_written > 0, 0);
1929 args->buf += actual_written;
1930 args->len -= actual_written;
1931 args->total_written += actual_written;
1934 /* Written everything, done. */
1937 /* Not written everything yet, keep trying. */
1942 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1946 QUIC_XSO *xso = ctx->xso;
1947 struct quic_write_again_args args;
1948 size_t actual_written = 0;
1950 /* First make a best effort to append as much of the data as possible. */
1951 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1953 /* Stream already finished or allocation error. */
1955 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1958 quic_post_write(xso, actual_written > 0, 1);
1960 if (actual_written == len) {
1961 /* Managed to append everything on the first try. */
1962 *written = actual_written;
1967 * We did not manage to append all of the data immediately, so the stream
1968 * buffer has probably filled up. This means we need to block until some of
1972 args.buf = (const unsigned char *)buf + actual_written;
1973 args.len = len - actual_written;
1974 args.total_written = 0;
1975 args.err = ERR_R_INTERNAL_ERROR;
1977 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1979 if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
1980 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1982 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
1985 *written = args.total_written;
1990 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1993 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1994 size_t buf_len, size_t already_sent)
1996 assert(!xso->aon_write_in_progress);
1998 xso->aon_write_in_progress = 1;
1999 xso->aon_buf_base = buf;
2000 xso->aon_buf_pos = already_sent;
2001 xso->aon_buf_len = buf_len;
2004 static void aon_write_finish(QUIC_XSO *xso)
2006 xso->aon_write_in_progress = 0;
2007 xso->aon_buf_base = NULL;
2008 xso->aon_buf_pos = 0;
2009 xso->aon_buf_len = 0;
2013 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2014 size_t len, size_t *written)
2016 QUIC_XSO *xso = ctx->xso;
2017 const void *actual_buf;
2018 size_t actual_len, actual_written = 0;
2019 int accept_moving_buffer
2020 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2022 if (xso->aon_write_in_progress) {
2024 * We are in the middle of an AON write (i.e., a previous write did not
2025 * manage to append all data to the SSTREAM and we have Enable Partial
2026 * Write (EPW) mode disabled.)
2028 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2029 || len != xso->aon_buf_len)
2031 * Pointer must not have changed if we are not in accept moving
2032 * buffer mode. Length must never change.
2034 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2036 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2037 actual_len = len - xso->aon_buf_pos;
2038 assert(actual_len > 0);
2044 /* First make a best effort to append as much of the data as possible. */
2045 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
2047 /* Stream already finished or allocation error. */
2049 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2052 quic_post_write(xso, actual_written > 0, 1);
2054 if (actual_written == actual_len) {
2055 /* We have sent everything. */
2056 if (xso->aon_write_in_progress) {
2058 * We have sent everything, and we were in the middle of an AON
2059 * write. The output write length is the total length of the AON
2060 * buffer, not however many bytes we managed to write to the stream
2063 *written = xso->aon_buf_len;
2064 aon_write_finish(xso);
2066 *written = actual_written;
2072 if (xso->aon_write_in_progress) {
2074 * AON write is in progress but we have not written everything yet. We
2075 * may have managed to send zero bytes, or some number of bytes less
2076 * than the total remaining which need to be appended during this
2079 xso->aon_buf_pos += actual_written;
2080 assert(xso->aon_buf_pos < xso->aon_buf_len);
2081 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2085 * Not in an existing AON operation but partial write is not enabled, so we
2086 * need to begin a new AON operation. However we needn't bother if we didn't
2087 * actually append anything.
2089 if (actual_written > 0)
2090 aon_write_begin(xso, buf, len, actual_written);
2093 * AON - We do not publicly admit to having appended anything until AON
2097 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2101 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2104 QUIC_XSO *xso = ctx->xso;
2106 /* Simple best effort operation. */
2107 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
2108 /* Stream already finished or allocation error. */
2110 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2113 quic_post_write(xso, *written > 0, 1);
2118 static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2120 QUIC_STREAM_MAP *qsm;
2122 if (xso == NULL || xso->stream == NULL) {
2123 *err = ERR_R_INTERNAL_ERROR;
2127 switch (xso->stream->send_state) {
2129 case QUIC_SSTREAM_STATE_NONE:
2130 *err = SSL_R_STREAM_RECV_ONLY;
2133 case QUIC_SSTREAM_STATE_READY:
2134 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2136 if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2137 *err = ERR_R_INTERNAL_ERROR;
2142 case QUIC_SSTREAM_STATE_SEND:
2143 case QUIC_SSTREAM_STATE_DATA_SENT:
2144 case QUIC_SSTREAM_STATE_DATA_RECVD:
2145 if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2146 *err = SSL_R_STREAM_FINISHED;
2152 case QUIC_SSTREAM_STATE_RESET_SENT:
2153 case QUIC_SSTREAM_STATE_RESET_RECVD:
2154 *err = SSL_R_STREAM_RESET;
2160 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2164 int partial_write, err;
2168 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2171 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
2173 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2174 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2179 * If we haven't finished the handshake, try to advance it.
2180 * We don't accept writes until the handshake is completed.
2182 if (quic_do_handshake(&ctx) < 1) {
2187 /* Ensure correct stream state, stream send part not concluded, etc. */
2188 if (!quic_validate_for_write(ctx.xso, &err)) {
2189 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2198 if (xso_blocking_mode(ctx.xso))
2199 ret = quic_write_blocking(&ctx, buf, len, written);
2200 else if (partial_write)
2201 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
2203 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
2206 quic_unlock(ctx.qc);
2214 struct quic_read_again_args {
2216 QUIC_STREAM *stream;
2224 static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2226 QUIC_STREAM_MAP *qsm;
2230 if (xso == NULL || xso->stream == NULL) {
2231 *err = ERR_R_INTERNAL_ERROR;
2235 switch (xso->stream->recv_state) {
2237 case QUIC_RSTREAM_STATE_NONE:
2238 *err = SSL_R_STREAM_SEND_ONLY;
2241 case QUIC_RSTREAM_STATE_RECV:
2242 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2243 case QUIC_RSTREAM_STATE_DATA_RECVD:
2246 case QUIC_RSTREAM_STATE_DATA_READ:
2250 case QUIC_RSTREAM_STATE_RESET_RECVD:
2251 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2252 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2255 case QUIC_RSTREAM_STATE_RESET_READ:
2256 *err = SSL_R_STREAM_RESET;
2262 static int quic_read_actual(QCTX *ctx,
2263 QUIC_STREAM *stream,
2264 void *buf, size_t buf_len,
2268 int is_fin = 0, err, eos;
2269 QUIC_CONNECTION *qc = ctx->qc;
2271 if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2273 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2275 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2279 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2280 bytes_read, &is_fin))
2281 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2284 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2285 bytes_read, &is_fin))
2286 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2290 if (*bytes_read > 0) {
2292 * We have read at least one byte from the stream. Inform stream-level
2293 * RXFC of the retirement of controlled bytes. Update the active stream
2294 * status (the RXFC may now want to emit a frame granting more credit to
2297 OSSL_RTT_INFO rtt_info;
2299 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2301 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2302 rtt_info.smoothed_rtt))
2303 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2306 if (is_fin && !peek) {
2307 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2309 ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2312 if (*bytes_read > 0)
2313 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2317 if (*bytes_read == 0 && is_fin)
2318 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2324 static int quic_read_again(void *arg)
2326 struct quic_read_again_args *args = arg;
2328 if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
2329 /* If connection is torn down due to an error while blocking, stop. */
2330 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2334 if (!quic_read_actual(args->ctx, args->stream,
2335 args->buf, args->len, args->bytes_read,
2339 if (*args->bytes_read > 0)
2340 /* got at least one byte, the SSL_read op can finish now */
2343 return 0; /* did not read anything, keep trying */
2347 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2351 struct quic_read_again_args args;
2355 if (!expect_quic(s, &ctx))
2360 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2361 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2365 /* If we haven't finished the handshake, try to advance it. */
2366 if (quic_do_handshake(&ctx) < 1) {
2367 ret = 0; /* ossl_quic_do_handshake raised error here */
2371 if (ctx.xso == NULL) {
2373 * Called on a QCSO and we don't currently have a default stream.
2375 * Wait until we get a stream initiated by the peer (blocking mode) or
2376 * fail if we don't have one yet (non-blocking mode).
2378 if (!qc_wait_for_default_xso_for_read(&ctx)) {
2379 ret = 0; /* error already raised here */
2383 ctx.xso = ctx.qc->default_xso;
2386 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2387 ret = 0; /* quic_read_actual raised error here */
2391 if (*bytes_read > 0) {
2393 * Even though we succeeded, tick the reactor here to ensure we are
2394 * handling other aspects of the QUIC connection.
2396 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2398 } else if (xso_blocking_mode(ctx.xso)) {
2400 * We were not able to read anything immediately, so our stream
2401 * buffer is empty. This means we need to block until we get
2402 * at least one byte.
2405 args.stream = ctx.xso->stream;
2408 args.bytes_read = bytes_read;
2411 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2413 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2415 } else if (res < 0) {
2416 ret = 0; /* quic_read_again raised error here */
2423 * We did not get any bytes and are not in blocking mode.
2424 * Tick to see if this delivers any more.
2426 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2428 /* Try the read again. */
2429 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2430 ret = 0; /* quic_read_actual raised error here */
2434 if (*bytes_read > 0)
2435 ret = 1; /* Succeeded this time. */
2437 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2441 quic_unlock(ctx.qc);
2445 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2447 return quic_read(s, buf, len, bytes_read, 0);
2450 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2452 return quic_read(s, buf, len, bytes_read, 1);
2461 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2468 if (!expect_quic(s, &ctx))
2473 if (ctx.xso == NULL) {
2474 QUIC_RAISE_NON_IO_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
2478 if (ctx.xso->stream == NULL
2479 || !ossl_quic_stream_has_recv_buffer(ctx.xso->stream)) {
2480 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2484 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2487 if (avail == 0 && check_channel && ossl_quic_channel_has_pending(ctx.qc->ch))
2491 quic_unlock(ctx.qc);
2495 size_t ossl_quic_pending(const SSL *s)
2497 return ossl_quic_pending_int(s, /*check_channel=*/0);
2500 int ossl_quic_has_pending(const SSL *s)
2502 /* Do we have app-side pending data or pending URXEs or RXEs? */
2503 return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2507 * SSL_stream_conclude
2508 * -------------------
2511 int ossl_quic_conn_stream_conclude(SSL *s)
2517 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2520 qs = ctx.xso->stream;
2522 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
2523 quic_unlock(ctx.qc);
2524 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2527 if (!quic_validate_for_write(ctx.xso, &err)) {
2528 quic_unlock(ctx.qc);
2529 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2532 if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2533 quic_unlock(ctx.qc);
2537 ossl_quic_sstream_fin(qs->sstream);
2538 quic_post_write(ctx.xso, 1, 1);
2539 quic_unlock(ctx.qc);
2544 * SSL_inject_net_dgram
2545 * --------------------
2548 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2550 const BIO_ADDR *peer,
2551 const BIO_ADDR *local)
2557 if (!expect_quic(s, &ctx))
2562 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2563 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2565 quic_unlock(ctx.qc);
2570 * SSL_get0_connection
2571 * -------------------
2573 SSL *ossl_quic_get0_connection(SSL *s)
2577 if (!expect_quic(s, &ctx))
2580 return &ctx.qc->ssl;
2584 * SSL_get_stream_type
2585 * -------------------
2587 int ossl_quic_get_stream_type(SSL *s)
2591 if (!expect_quic(s, &ctx))
2592 return SSL_STREAM_TYPE_BIDI;
2594 if (ctx.xso == NULL) {
2596 * If deferred XSO creation has yet to occur, proceed according to the
2597 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2598 * what kind of stream will be created yet, so return BIDI on the basis
2599 * that at this time, the client still has the option of calling
2600 * SSL_read() or SSL_write() first.
2602 if (ctx.qc->default_xso_created
2603 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2604 return SSL_STREAM_TYPE_NONE;
2606 return SSL_STREAM_TYPE_BIDI;
2609 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2610 return SSL_STREAM_TYPE_BIDI;
2612 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2613 return SSL_STREAM_TYPE_READ;
2615 return SSL_STREAM_TYPE_WRITE;
2623 uint64_t ossl_quic_get_stream_id(SSL *s)
2628 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2631 id = ctx.xso->stream->id;
2632 quic_unlock(ctx.qc);
2638 * SSL_set_default_stream_mode
2639 * ---------------------------
2642 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2646 if (!expect_quic_conn_only(s, &ctx))
2651 if (ctx.qc->default_xso_created) {
2652 quic_unlock(ctx.qc);
2653 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2654 "too late to change default stream mode");
2658 case SSL_DEFAULT_STREAM_MODE_NONE:
2659 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2660 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2661 ctx.qc->default_stream_mode = mode;
2664 quic_unlock(ctx.qc);
2665 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2666 "bad default stream type");
2669 quic_unlock(ctx.qc);
2678 SSL *ossl_quic_detach_stream(SSL *s)
2681 QUIC_XSO *xso = NULL;
2683 if (!expect_quic_conn_only(s, &ctx))
2688 /* Calling this function inhibits default XSO autocreation. */
2689 /* QC ref to any default XSO is transferred to us and to caller. */
2690 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2692 quic_unlock(ctx.qc);
2694 return xso != NULL ? &xso->ssl : NULL;
2702 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2708 if (!expect_quic_conn_only(conn, &ctx))
2711 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2712 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2713 "stream to attach must be a valid QUIC stream");
2715 xso = (QUIC_XSO *)stream;
2719 if (ctx.qc->default_xso != NULL) {
2720 quic_unlock(ctx.qc);
2721 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2722 "connection already has a default stream");
2726 * It is a caller error for the XSO being attached as a default XSO to have
2727 * more than one ref.
2729 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
2730 quic_unlock(ctx.qc);
2731 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2736 quic_unlock(ctx.qc);
2737 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2738 "stream being attached must have "
2739 "only 1 reference");
2742 /* Caller's reference to the XSO is transferred to us. */
2743 /* Calling this function inhibits default XSO autocreation. */
2744 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2746 quic_unlock(ctx.qc);
2751 * SSL_set_incoming_stream_policy
2752 * ------------------------------
2755 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2757 switch (qc->incoming_stream_policy) {
2758 case SSL_INCOMING_STREAM_POLICY_AUTO:
2759 if ((qc->default_xso == NULL && !qc->default_xso_created)
2760 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2761 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2763 return SSL_INCOMING_STREAM_POLICY_REJECT;
2766 return qc->incoming_stream_policy;
2771 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2773 int policy = qc_get_effective_incoming_stream_policy(qc);
2774 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2776 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2778 qc->incoming_stream_aec);
2782 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2788 if (!expect_quic_conn_only(s, &ctx))
2794 case SSL_INCOMING_STREAM_POLICY_AUTO:
2795 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2796 case SSL_INCOMING_STREAM_POLICY_REJECT:
2797 ctx.qc->incoming_stream_policy = policy;
2798 ctx.qc->incoming_stream_aec = aec;
2802 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
2807 qc_update_reject_policy(ctx.qc);
2808 quic_unlock(ctx.qc);
2816 struct wait_for_incoming_stream_args {
2822 static int wait_for_incoming_stream(void *arg)
2824 struct wait_for_incoming_stream_args *args = arg;
2825 QUIC_CONNECTION *qc = args->ctx->qc;
2826 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2828 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2829 /* If connection is torn down due to an error while blocking, stop. */
2830 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2834 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2835 if (args->qs != NULL)
2836 return 1; /* got a stream */
2838 return 0; /* did not get a stream, keep trying */
2842 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2847 QUIC_STREAM_MAP *qsm;
2850 OSSL_RTT_INFO rtt_info;
2852 if (!expect_quic_conn_only(s, &ctx))
2857 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2858 == SSL_INCOMING_STREAM_POLICY_REJECT) {
2859 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
2863 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2865 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2867 if (qc_blocking_mode(ctx.qc)
2868 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2869 struct wait_for_incoming_stream_args args;
2874 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2876 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2878 } else if (ret < 0 || args.qs == NULL) {
2888 xso = create_xso_from_stream(ctx.qc, qs);
2892 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2893 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2894 rtt_info.smoothed_rtt);
2897 /* Calling this function inhibits default XSO autocreation. */
2898 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2901 quic_unlock(ctx.qc);
2906 * SSL_get_accept_stream_queue_len
2907 * -------------------------------
2910 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2915 if (!expect_quic_conn_only(s, &ctx))
2920 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2922 quic_unlock(ctx.qc);
2930 int ossl_quic_stream_reset(SSL *ssl,
2931 const SSL_STREAM_RESET_ARGS *args,
2935 QUIC_STREAM_MAP *qsm;
2937 uint64_t error_code;
2940 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2943 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2944 qs = ctx.xso->stream;
2945 error_code = (args != NULL ? args->quic_error_code : 0);
2947 if (!quic_validate_for_write(ctx.xso, &err)) {
2948 ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2952 ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2955 quic_unlock(ctx.qc);
2960 * SSL_get_stream_read_state
2961 * -------------------------
2963 static void quic_classify_stream(QUIC_CONNECTION *qc,
2967 uint64_t *app_error_code)
2970 uint64_t final_size;
2972 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2974 if (app_error_code != NULL)
2975 *app_error_code = UINT64_MAX;
2977 app_error_code = &final_size; /* throw away value */
2979 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2981 * Unidirectional stream and this direction of transmission doesn't
2984 *state = SSL_STREAM_STATE_WRONG_DIR;
2985 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2986 /* Connection already closed. */
2987 *state = SSL_STREAM_STATE_CONN_CLOSED;
2988 } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
2989 /* Application has read a FIN. */
2990 *state = SSL_STREAM_STATE_FINISHED;
2991 } else if ((!is_write && qs->stop_sending)
2992 || (is_write && ossl_quic_stream_send_is_reset(qs))) {
2994 * Stream has been reset locally. FIN takes precedence over this for the
2995 * read case as the application need not care if the stream is reset
2996 * after a FIN has been successfully processed.
2998 *state = SSL_STREAM_STATE_RESET_LOCAL;
2999 *app_error_code = !is_write
3000 ? qs->stop_sending_aec
3001 : qs->reset_stream_aec;
3002 } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
3003 || (is_write && qs->peer_stop_sending)) {
3005 * Stream has been reset remotely. */
3006 *state = SSL_STREAM_STATE_RESET_REMOTE;
3007 *app_error_code = !is_write
3008 ? qs->peer_reset_stream_aec
3009 : qs->peer_stop_sending_aec;
3010 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
3013 * Stream has been finished. Stream reset takes precedence over this for
3014 * the write case as peer may not have received all data.
3016 *state = SSL_STREAM_STATE_FINISHED;
3018 /* Stream still healthy. */
3019 *state = SSL_STREAM_STATE_OK;
3023 static int quic_get_stream_state(SSL *ssl, int is_write)
3028 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3029 return SSL_STREAM_STATE_NONE;
3031 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
3032 quic_unlock(ctx.qc);
3036 int ossl_quic_get_stream_read_state(SSL *ssl)
3038 return quic_get_stream_state(ssl, /*is_write=*/0);
3042 * SSL_get_stream_write_state
3043 * --------------------------
3045 int ossl_quic_get_stream_write_state(SSL *ssl)
3047 return quic_get_stream_state(ssl, /*is_write=*/1);
3051 * SSL_get_stream_read_error_code
3052 * ------------------------------
3054 static int quic_get_stream_error_code(SSL *ssl, int is_write,
3055 uint64_t *app_error_code)
3060 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3063 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
3064 &state, app_error_code);
3066 quic_unlock(ctx.qc);
3068 case SSL_STREAM_STATE_FINISHED:
3070 case SSL_STREAM_STATE_RESET_LOCAL:
3071 case SSL_STREAM_STATE_RESET_REMOTE:
3078 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
3080 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
3084 * SSL_get_stream_write_error_code
3085 * -------------------------------
3087 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
3089 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
3093 * Write buffer size mutation
3094 * --------------------------
3096 int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
3101 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3104 if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
3105 /* Called on a unidirectional receive-only stream - error. */
3106 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3110 if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
3112 * If the stream has a send part but we have disposed of it because we
3113 * no longer need it, this is a no-op.
3119 if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
3120 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3127 quic_unlock(ctx.qc);
3132 * SSL_get_conn_close_info
3133 * -----------------------
3135 int ossl_quic_get_conn_close_info(SSL *ssl,
3136 SSL_CONN_CLOSE_INFO *info,
3140 const QUIC_TERMINATE_CAUSE *tc;
3142 if (!expect_quic_conn_only(ssl, &ctx))
3145 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
3149 info->error_code = tc->error_code;
3150 info->reason = tc->reason;
3151 info->reason_len = tc->reason_len;
3152 info->is_local = !tc->remote;
3153 info->is_transport = !tc->app;
3161 int ossl_quic_key_update(SSL *ssl, int update_type)
3165 if (!expect_quic_conn_only(ssl, &ctx))
3168 switch (update_type) {
3169 case SSL_KEY_UPDATE_NOT_REQUESTED:
3171 * QUIC signals peer key update implicily by triggering a local
3172 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3174 case SSL_KEY_UPDATE_REQUESTED:
3178 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3184 /* Attempt to perform a TXKU. */
3185 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
3186 QUIC_RAISE_NON_IO_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
3187 quic_unlock(ctx.qc);
3191 quic_unlock(ctx.qc);
3196 * SSL_get_key_update_type
3197 * -----------------------
3199 int ossl_quic_get_key_update_type(const SSL *s)
3202 * We always handle key updates immediately so a key update is never
3205 return SSL_KEY_UPDATE_NONE;
3209 * QUIC Front-End I/O API: SSL_CTX Management
3210 * ==========================================
3213 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3217 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
3221 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3225 if (!expect_quic_conn_only(s, &ctx))
3229 case SSL_CTRL_SET_MSG_CALLBACK:
3230 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
3232 /* This callback also needs to be set on the internal SSL object */
3233 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
3236 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3237 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
3241 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3243 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
3246 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
3248 /* We never do renegotiation. */
3253 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3254 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3257 int ossl_quic_num_ciphers(void)
3262 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
3268 * Internal Testing APIs
3269 * =====================
3272 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
3276 if (!expect_quic_conn_only(s, &ctx))