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 /* Mask ctrls we shouldn't support for QUIC. */
1311 case SSL_CTRL_GET_READ_AHEAD:
1312 case SSL_CTRL_SET_READ_AHEAD:
1313 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1314 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
1315 case SSL_CTRL_SET_MAX_PIPELINES:
1320 * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1321 * implementation. Either SSL_ctrl will handle it itself by direct
1322 * access into handshake layer state, or failing that, it will be passed
1323 * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1324 * supported by anything, the handshake layer's ctrl method will finally
1327 return ossl_ctrl_internal(&ctx.qc->ssl, cmd, larg, parg, /*no_quic=*/1);
1331 /* SSL_set_connect_state */
1332 void ossl_quic_set_connect_state(SSL *s)
1336 if (!expect_quic(s, &ctx))
1339 /* Cannot be changed after handshake started */
1340 if (ctx.qc->started || ctx.is_stream)
1343 ctx.qc->as_server_state = 0;
1346 /* SSL_set_accept_state */
1347 void ossl_quic_set_accept_state(SSL *s)
1351 if (!expect_quic(s, &ctx))
1354 /* Cannot be changed after handshake started */
1355 if (ctx.qc->started || ctx.is_stream)
1358 ctx.qc->as_server_state = 1;
1361 /* SSL_do_handshake */
1362 struct quic_handshake_wait_args {
1363 QUIC_CONNECTION *qc;
1366 static int quic_handshake_wait(void *arg)
1368 struct quic_handshake_wait_args *args = arg;
1370 if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1373 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1379 static int configure_channel(QUIC_CONNECTION *qc)
1381 assert(qc->ch != NULL);
1383 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1384 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1385 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1392 static int create_channel(QUIC_CONNECTION *qc)
1394 QUIC_CHANNEL_ARGS args = {0};
1396 args.libctx = qc->ssl.ctx->libctx;
1397 args.propq = qc->ssl.ctx->propq;
1398 args.is_server = qc->as_server;
1400 args.mutex = qc->mutex;
1401 args.now_cb = get_time_cb;
1402 args.now_cb_arg = qc;
1404 qc->ch = ossl_quic_channel_new(&args);
1405 if (qc->ch == NULL) {
1406 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1414 * Creates a channel and configures it with the information we have accumulated
1415 * via calls made to us from the application prior to starting a handshake
1419 static int ensure_channel_started(QUIC_CONNECTION *qc)
1422 if (!configure_channel(qc)
1423 || !ossl_quic_channel_start(qc->ch))
1426 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1427 if (qc->is_thread_assisted)
1428 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1437 ossl_quic_channel_free(qc->ch);
1443 static int quic_do_handshake(QCTX *ctx)
1446 QUIC_CONNECTION *qc = ctx->qc;
1448 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1449 /* Handshake already completed. */
1452 if (!quic_mutation_allowed(qc, /*req_active=*/0))
1453 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1455 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1456 /* Peer address must have been set. */
1457 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1458 return -1; /* Non-protocol error */
1461 if (qc->as_server != qc->as_server_state) {
1462 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1463 return -1; /* Non-protocol error */
1466 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1467 /* Need read and write BIOs. */
1468 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1469 return -1; /* Non-protocol error */
1473 * Start connection process. Note we may come here multiple times in
1474 * non-blocking mode, which is fine.
1476 if (!ensure_channel_started(qc)) {
1477 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1478 return -1; /* Non-protocol error */
1481 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1482 /* The handshake is now done. */
1485 if (qc_blocking_mode(qc)) {
1486 /* In blocking mode, wait for the handshake to complete. */
1487 struct quic_handshake_wait_args args;
1491 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1492 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1493 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1494 return 0; /* Shutdown before completion */
1495 } else if (ret <= 0) {
1496 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1497 return -1; /* Non-protocol error */
1500 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1503 /* Try to advance the reactor. */
1504 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1506 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1507 /* The handshake is now done. */
1510 /* Otherwise, indicate that the handshake isn't done yet. */
1511 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1512 return -1; /* Non-protocol error */
1517 int ossl_quic_do_handshake(SSL *s)
1522 if (!expect_quic(s, &ctx))
1527 ret = quic_do_handshake(&ctx);
1528 quic_unlock(ctx.qc);
1533 int ossl_quic_connect(SSL *s)
1535 /* Ensure we are in connect state (no-op if non-idle). */
1536 ossl_quic_set_connect_state(s);
1538 /* Begin or continue the handshake */
1539 return ossl_quic_do_handshake(s);
1543 int ossl_quic_accept(SSL *s)
1545 /* Ensure we are in accept state (no-op if non-idle). */
1546 ossl_quic_set_accept_state(s);
1548 /* Begin or continue the handshake */
1549 return ossl_quic_do_handshake(s);
1553 * QUIC Front-End I/O API: Stream Lifecycle Operations
1554 * ===================================================
1556 * SSL_stream_new => ossl_quic_conn_stream_new
1561 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1562 * default XSO was created. Returns 0 if it was not (e.g. because it already
1563 * exists). Note that this is NOT an error condition.
1566 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1569 QUIC_CONNECTION *qc = ctx->qc;
1571 if (qc->default_xso_created
1572 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1574 * We only do this once. If the user detaches a previously created
1575 * default XSO we don't auto-create another one.
1577 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1579 /* Create a locally-initiated stream. */
1580 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1581 flags |= SSL_STREAM_FLAG_UNI;
1583 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1586 if (qc->default_xso == NULL)
1587 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1589 qc_touch_default_xso(qc);
1593 struct quic_wait_for_stream_args {
1594 QUIC_CONNECTION *qc;
1601 static int quic_wait_for_stream(void *arg)
1603 struct quic_wait_for_stream_args *args = arg;
1605 if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
1606 /* If connection is torn down due to an error while blocking, stop. */
1607 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1611 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1612 args->expect_id | QUIC_STREAM_DIR_BIDI);
1613 if (args->qs == NULL)
1614 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1615 args->expect_id | QUIC_STREAM_DIR_UNI);
1617 if (args->qs != NULL)
1618 return 1; /* stream now exists */
1620 return 0; /* did not get a stream, keep trying */
1624 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1626 /* Called on a QCSO and we don't currently have a default stream. */
1628 QUIC_CONNECTION *qc = ctx->qc;
1631 struct quic_wait_for_stream_args wargs;
1634 * If default stream functionality is disabled or we already detached
1635 * one, don't make another default stream and just fail.
1637 if (qc->default_xso_created
1638 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1639 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1642 * The peer may have opened a stream since we last ticked. So tick and
1643 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1644 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1645 * first stream created by a peer must have an ordinal of 0.
1647 expect_id = qc->as_server
1648 ? QUIC_STREAM_INITIATOR_CLIENT
1649 : QUIC_STREAM_INITIATOR_SERVER;
1651 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1652 expect_id | QUIC_STREAM_DIR_BIDI);
1654 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1655 expect_id | QUIC_STREAM_DIR_UNI);
1658 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1660 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1665 if (!qc_blocking_mode(qc))
1666 /* Non-blocking mode, so just bail immediately. */
1667 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1669 /* Block until we have a stream. */
1673 wargs.expect_id = expect_id;
1675 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1677 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1678 else if (res < 0 || wargs.qs == NULL)
1679 /* quic_wait_for_stream raised error here */
1686 * We now have qs != NULL. Make it the default stream, creating the
1689 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1690 if (qc->default_xso == NULL)
1691 return QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1693 qc_touch_default_xso(qc); /* inhibits default XSO */
1698 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1700 QUIC_XSO *xso = NULL;
1702 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
1703 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
1707 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) {
1708 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1713 if (!SSL_up_ref(&qc->ssl)) {
1714 QUIC_RAISE_NON_IO_ERROR(NULL, ERR_R_SSL_LIB, NULL);
1719 xso->blocking = qc->default_blocking;
1720 xso->ssl_mode = qc->default_ssl_mode;
1722 = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1723 xso->last_error = SSL_ERROR_NONE;
1728 xso_update_options(xso);
1736 struct quic_new_stream_wait_args {
1737 QUIC_CONNECTION *qc;
1741 static int quic_new_stream_wait(void *arg)
1743 struct quic_new_stream_wait_args *args = arg;
1744 QUIC_CONNECTION *qc = args->qc;
1746 if (!quic_mutation_allowed(qc, /*req_active=*/1))
1749 if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
1755 /* locking depends on need_lock */
1756 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1759 QUIC_CONNECTION *qc = ctx->qc;
1760 QUIC_XSO *xso = NULL;
1761 QUIC_STREAM *qs = NULL;
1762 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1763 int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
1764 int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
1769 if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
1770 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1775 && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
1776 struct quic_new_stream_wait_args args;
1779 * Stream count flow control currently doesn't permit this stream to be
1782 if (no_blocking || !qc_blocking_mode(qc)) {
1783 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
1788 args.is_uni = is_uni;
1790 /* Blocking mode - wait until we can get a stream. */
1791 ret = block_until_pred(ctx->qc, quic_new_stream_wait, &args, 0);
1792 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1793 QUIC_RAISE_NON_IO_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1794 goto err; /* Shutdown before completion */
1795 } else if (ret <= 0) {
1796 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1797 goto err; /* Non-protocol error */
1801 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1803 QUIC_RAISE_NON_IO_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1807 xso = create_xso_from_stream(qc, qs);
1811 qc_touch_default_xso(qc); /* inhibits default XSO */
1819 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1828 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1832 if (!expect_quic_conn_only(s, &ctx))
1835 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1839 * QUIC Front-End I/O API: Steady-State Operations
1840 * ===============================================
1842 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1843 * is, the functions used during the established phase of a QUIC connection
1844 * (e.g. SSL_read, SSL_write).
1846 * Each function must handle both blocking and non-blocking modes. As discussed
1847 * above, all QUIC I/O is implemented using non-blocking mode internally.
1849 * SSL_get_error => partially implemented by ossl_quic_get_error
1850 * (BIO/)SSL_read => ossl_quic_read
1851 * (BIO/)SSL_write => ossl_quic_write
1852 * SSL_pending => ossl_quic_pending
1853 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1854 * SSL_key_update => ossl_quic_key_update
1858 int ossl_quic_get_error(const SSL *s, int i)
1861 int net_error, last_error;
1863 if (!expect_quic(s, &ctx))
1867 net_error = ossl_quic_channel_net_error(ctx.qc->ch);
1868 last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1869 quic_unlock(ctx.qc);
1872 return SSL_ERROR_SYSCALL;
1881 * The set of functions below provide the implementation of the public SSL_write
1882 * function. We must handle:
1884 * - both blocking and non-blocking operation at the application level,
1885 * depending on how we are configured;
1887 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1889 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1893 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1896 * We have appended at least one byte to the stream.
1897 * Potentially mark stream as active, depending on FC.
1900 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1906 * TODO(QUIC FUTURE): It is probably inefficient to try and do this
1907 * immediately, plus we should eventually consider Nagle's algorithm.
1910 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1913 struct quic_write_again_args {
1915 const unsigned char *buf;
1917 size_t total_written;
1922 static int quic_write_again(void *arg)
1924 struct quic_write_again_args *args = arg;
1925 size_t actual_written = 0;
1927 if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
1928 /* If connection is torn down due to an error while blocking, stop. */
1931 if (!quic_validate_for_write(args->xso, &args->err))
1933 * Stream may have become invalid for write due to connection events
1938 args->err = ERR_R_INTERNAL_ERROR;
1939 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1940 args->buf, args->len, &actual_written))
1943 quic_post_write(args->xso, actual_written > 0, 0);
1945 args->buf += actual_written;
1946 args->len -= actual_written;
1947 args->total_written += actual_written;
1950 /* Written everything, done. */
1953 /* Not written everything yet, keep trying. */
1958 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1962 QUIC_XSO *xso = ctx->xso;
1963 struct quic_write_again_args args;
1964 size_t actual_written = 0;
1966 /* First make a best effort to append as much of the data as possible. */
1967 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1969 /* Stream already finished or allocation error. */
1971 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1974 quic_post_write(xso, actual_written > 0, 1);
1976 if (actual_written == len) {
1977 /* Managed to append everything on the first try. */
1978 *written = actual_written;
1983 * We did not manage to append all of the data immediately, so the stream
1984 * buffer has probably filled up. This means we need to block until some of
1988 args.buf = (const unsigned char *)buf + actual_written;
1989 args.len = len - actual_written;
1990 args.total_written = 0;
1991 args.err = ERR_R_INTERNAL_ERROR;
1993 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1995 if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
1996 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1998 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
2001 *written = args.total_written;
2006 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2009 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
2010 size_t buf_len, size_t already_sent)
2012 assert(!xso->aon_write_in_progress);
2014 xso->aon_write_in_progress = 1;
2015 xso->aon_buf_base = buf;
2016 xso->aon_buf_pos = already_sent;
2017 xso->aon_buf_len = buf_len;
2020 static void aon_write_finish(QUIC_XSO *xso)
2022 xso->aon_write_in_progress = 0;
2023 xso->aon_buf_base = NULL;
2024 xso->aon_buf_pos = 0;
2025 xso->aon_buf_len = 0;
2029 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2030 size_t len, size_t *written)
2032 QUIC_XSO *xso = ctx->xso;
2033 const void *actual_buf;
2034 size_t actual_len, actual_written = 0;
2035 int accept_moving_buffer
2036 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2038 if (xso->aon_write_in_progress) {
2040 * We are in the middle of an AON write (i.e., a previous write did not
2041 * manage to append all data to the SSTREAM and we have Enable Partial
2042 * Write (EPW) mode disabled.)
2044 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2045 || len != xso->aon_buf_len)
2047 * Pointer must not have changed if we are not in accept moving
2048 * buffer mode. Length must never change.
2050 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2052 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2053 actual_len = len - xso->aon_buf_pos;
2054 assert(actual_len > 0);
2060 /* First make a best effort to append as much of the data as possible. */
2061 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
2063 /* Stream already finished or allocation error. */
2065 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2068 quic_post_write(xso, actual_written > 0, 1);
2070 if (actual_written == actual_len) {
2071 /* We have sent everything. */
2072 if (xso->aon_write_in_progress) {
2074 * We have sent everything, and we were in the middle of an AON
2075 * write. The output write length is the total length of the AON
2076 * buffer, not however many bytes we managed to write to the stream
2079 *written = xso->aon_buf_len;
2080 aon_write_finish(xso);
2082 *written = actual_written;
2088 if (xso->aon_write_in_progress) {
2090 * AON write is in progress but we have not written everything yet. We
2091 * may have managed to send zero bytes, or some number of bytes less
2092 * than the total remaining which need to be appended during this
2095 xso->aon_buf_pos += actual_written;
2096 assert(xso->aon_buf_pos < xso->aon_buf_len);
2097 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2101 * Not in an existing AON operation but partial write is not enabled, so we
2102 * need to begin a new AON operation. However we needn't bother if we didn't
2103 * actually append anything.
2105 if (actual_written > 0)
2106 aon_write_begin(xso, buf, len, actual_written);
2109 * AON - We do not publicly admit to having appended anything until AON
2113 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2117 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2120 QUIC_XSO *xso = ctx->xso;
2122 /* Simple best effort operation. */
2123 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
2124 /* Stream already finished or allocation error. */
2126 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2129 quic_post_write(xso, *written > 0, 1);
2134 static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2136 QUIC_STREAM_MAP *qsm;
2138 if (xso == NULL || xso->stream == NULL) {
2139 *err = ERR_R_INTERNAL_ERROR;
2143 switch (xso->stream->send_state) {
2145 case QUIC_SSTREAM_STATE_NONE:
2146 *err = SSL_R_STREAM_RECV_ONLY;
2149 case QUIC_SSTREAM_STATE_READY:
2150 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2152 if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2153 *err = ERR_R_INTERNAL_ERROR;
2158 case QUIC_SSTREAM_STATE_SEND:
2159 case QUIC_SSTREAM_STATE_DATA_SENT:
2160 case QUIC_SSTREAM_STATE_DATA_RECVD:
2161 if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2162 *err = SSL_R_STREAM_FINISHED;
2168 case QUIC_SSTREAM_STATE_RESET_SENT:
2169 case QUIC_SSTREAM_STATE_RESET_RECVD:
2170 *err = SSL_R_STREAM_RESET;
2176 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2180 int partial_write, err;
2184 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2187 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
2189 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2190 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2195 * If we haven't finished the handshake, try to advance it.
2196 * We don't accept writes until the handshake is completed.
2198 if (quic_do_handshake(&ctx) < 1) {
2203 /* Ensure correct stream state, stream send part not concluded, etc. */
2204 if (!quic_validate_for_write(ctx.xso, &err)) {
2205 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2214 if (xso_blocking_mode(ctx.xso))
2215 ret = quic_write_blocking(&ctx, buf, len, written);
2216 else if (partial_write)
2217 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
2219 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
2222 quic_unlock(ctx.qc);
2230 struct quic_read_again_args {
2232 QUIC_STREAM *stream;
2240 static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2242 QUIC_STREAM_MAP *qsm;
2246 if (xso == NULL || xso->stream == NULL) {
2247 *err = ERR_R_INTERNAL_ERROR;
2251 switch (xso->stream->recv_state) {
2253 case QUIC_RSTREAM_STATE_NONE:
2254 *err = SSL_R_STREAM_SEND_ONLY;
2257 case QUIC_RSTREAM_STATE_RECV:
2258 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2259 case QUIC_RSTREAM_STATE_DATA_RECVD:
2262 case QUIC_RSTREAM_STATE_DATA_READ:
2266 case QUIC_RSTREAM_STATE_RESET_RECVD:
2267 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2268 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2271 case QUIC_RSTREAM_STATE_RESET_READ:
2272 *err = SSL_R_STREAM_RESET;
2278 static int quic_read_actual(QCTX *ctx,
2279 QUIC_STREAM *stream,
2280 void *buf, size_t buf_len,
2284 int is_fin = 0, err, eos;
2285 QUIC_CONNECTION *qc = ctx->qc;
2287 if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2289 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2291 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2295 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2296 bytes_read, &is_fin))
2297 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2300 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2301 bytes_read, &is_fin))
2302 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2306 if (*bytes_read > 0) {
2308 * We have read at least one byte from the stream. Inform stream-level
2309 * RXFC of the retirement of controlled bytes. Update the active stream
2310 * status (the RXFC may now want to emit a frame granting more credit to
2313 OSSL_RTT_INFO rtt_info;
2315 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2317 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2318 rtt_info.smoothed_rtt))
2319 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2322 if (is_fin && !peek) {
2323 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2325 ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2328 if (*bytes_read > 0)
2329 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2333 if (*bytes_read == 0 && is_fin)
2334 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2340 static int quic_read_again(void *arg)
2342 struct quic_read_again_args *args = arg;
2344 if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
2345 /* If connection is torn down due to an error while blocking, stop. */
2346 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2350 if (!quic_read_actual(args->ctx, args->stream,
2351 args->buf, args->len, args->bytes_read,
2355 if (*args->bytes_read > 0)
2356 /* got at least one byte, the SSL_read op can finish now */
2359 return 0; /* did not read anything, keep trying */
2363 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2367 struct quic_read_again_args args;
2371 if (!expect_quic(s, &ctx))
2376 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2377 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2381 /* If we haven't finished the handshake, try to advance it. */
2382 if (quic_do_handshake(&ctx) < 1) {
2383 ret = 0; /* ossl_quic_do_handshake raised error here */
2387 if (ctx.xso == NULL) {
2389 * Called on a QCSO and we don't currently have a default stream.
2391 * Wait until we get a stream initiated by the peer (blocking mode) or
2392 * fail if we don't have one yet (non-blocking mode).
2394 if (!qc_wait_for_default_xso_for_read(&ctx)) {
2395 ret = 0; /* error already raised here */
2399 ctx.xso = ctx.qc->default_xso;
2402 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2403 ret = 0; /* quic_read_actual raised error here */
2407 if (*bytes_read > 0) {
2409 * Even though we succeeded, tick the reactor here to ensure we are
2410 * handling other aspects of the QUIC connection.
2412 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2414 } else if (xso_blocking_mode(ctx.xso)) {
2416 * We were not able to read anything immediately, so our stream
2417 * buffer is empty. This means we need to block until we get
2418 * at least one byte.
2421 args.stream = ctx.xso->stream;
2424 args.bytes_read = bytes_read;
2427 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2429 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2431 } else if (res < 0) {
2432 ret = 0; /* quic_read_again raised error here */
2439 * We did not get any bytes and are not in blocking mode.
2440 * Tick to see if this delivers any more.
2442 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2444 /* Try the read again. */
2445 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2446 ret = 0; /* quic_read_actual raised error here */
2450 if (*bytes_read > 0)
2451 ret = 1; /* Succeeded this time. */
2453 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2457 quic_unlock(ctx.qc);
2461 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2463 return quic_read(s, buf, len, bytes_read, 0);
2466 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2468 return quic_read(s, buf, len, bytes_read, 1);
2477 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2484 if (!expect_quic(s, &ctx))
2489 if (ctx.xso == NULL) {
2490 QUIC_RAISE_NON_IO_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
2494 if (ctx.xso->stream == NULL
2495 || !ossl_quic_stream_has_recv_buffer(ctx.xso->stream)) {
2496 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2500 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2503 if (avail == 0 && check_channel && ossl_quic_channel_has_pending(ctx.qc->ch))
2507 quic_unlock(ctx.qc);
2511 size_t ossl_quic_pending(const SSL *s)
2513 return ossl_quic_pending_int(s, /*check_channel=*/0);
2516 int ossl_quic_has_pending(const SSL *s)
2518 /* Do we have app-side pending data or pending URXEs or RXEs? */
2519 return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2523 * SSL_stream_conclude
2524 * -------------------
2527 int ossl_quic_conn_stream_conclude(SSL *s)
2533 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2536 qs = ctx.xso->stream;
2538 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
2539 quic_unlock(ctx.qc);
2540 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2543 if (!quic_validate_for_write(ctx.xso, &err)) {
2544 quic_unlock(ctx.qc);
2545 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2548 if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2549 quic_unlock(ctx.qc);
2553 ossl_quic_sstream_fin(qs->sstream);
2554 quic_post_write(ctx.xso, 1, 1);
2555 quic_unlock(ctx.qc);
2560 * SSL_inject_net_dgram
2561 * --------------------
2564 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2566 const BIO_ADDR *peer,
2567 const BIO_ADDR *local)
2573 if (!expect_quic(s, &ctx))
2578 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2579 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2581 quic_unlock(ctx.qc);
2586 * SSL_get0_connection
2587 * -------------------
2589 SSL *ossl_quic_get0_connection(SSL *s)
2593 if (!expect_quic(s, &ctx))
2596 return &ctx.qc->ssl;
2600 * SSL_get_stream_type
2601 * -------------------
2603 int ossl_quic_get_stream_type(SSL *s)
2607 if (!expect_quic(s, &ctx))
2608 return SSL_STREAM_TYPE_BIDI;
2610 if (ctx.xso == NULL) {
2612 * If deferred XSO creation has yet to occur, proceed according to the
2613 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2614 * what kind of stream will be created yet, so return BIDI on the basis
2615 * that at this time, the client still has the option of calling
2616 * SSL_read() or SSL_write() first.
2618 if (ctx.qc->default_xso_created
2619 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2620 return SSL_STREAM_TYPE_NONE;
2622 return SSL_STREAM_TYPE_BIDI;
2625 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2626 return SSL_STREAM_TYPE_BIDI;
2628 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2629 return SSL_STREAM_TYPE_READ;
2631 return SSL_STREAM_TYPE_WRITE;
2639 uint64_t ossl_quic_get_stream_id(SSL *s)
2644 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2647 id = ctx.xso->stream->id;
2648 quic_unlock(ctx.qc);
2654 * SSL_set_default_stream_mode
2655 * ---------------------------
2658 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2662 if (!expect_quic_conn_only(s, &ctx))
2667 if (ctx.qc->default_xso_created) {
2668 quic_unlock(ctx.qc);
2669 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2670 "too late to change default stream mode");
2674 case SSL_DEFAULT_STREAM_MODE_NONE:
2675 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2676 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2677 ctx.qc->default_stream_mode = mode;
2680 quic_unlock(ctx.qc);
2681 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2682 "bad default stream type");
2685 quic_unlock(ctx.qc);
2694 SSL *ossl_quic_detach_stream(SSL *s)
2697 QUIC_XSO *xso = NULL;
2699 if (!expect_quic_conn_only(s, &ctx))
2704 /* Calling this function inhibits default XSO autocreation. */
2705 /* QC ref to any default XSO is transferred to us and to caller. */
2706 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2708 quic_unlock(ctx.qc);
2710 return xso != NULL ? &xso->ssl : NULL;
2718 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2724 if (!expect_quic_conn_only(conn, &ctx))
2727 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2728 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2729 "stream to attach must be a valid QUIC stream");
2731 xso = (QUIC_XSO *)stream;
2735 if (ctx.qc->default_xso != NULL) {
2736 quic_unlock(ctx.qc);
2737 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2738 "connection already has a default stream");
2742 * It is a caller error for the XSO being attached as a default XSO to have
2743 * more than one ref.
2745 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
2746 quic_unlock(ctx.qc);
2747 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2752 quic_unlock(ctx.qc);
2753 return QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2754 "stream being attached must have "
2755 "only 1 reference");
2758 /* Caller's reference to the XSO is transferred to us. */
2759 /* Calling this function inhibits default XSO autocreation. */
2760 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2762 quic_unlock(ctx.qc);
2767 * SSL_set_incoming_stream_policy
2768 * ------------------------------
2771 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2773 switch (qc->incoming_stream_policy) {
2774 case SSL_INCOMING_STREAM_POLICY_AUTO:
2775 if ((qc->default_xso == NULL && !qc->default_xso_created)
2776 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2777 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2779 return SSL_INCOMING_STREAM_POLICY_REJECT;
2782 return qc->incoming_stream_policy;
2787 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2789 int policy = qc_get_effective_incoming_stream_policy(qc);
2790 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2792 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2794 qc->incoming_stream_aec);
2798 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2804 if (!expect_quic_conn_only(s, &ctx))
2810 case SSL_INCOMING_STREAM_POLICY_AUTO:
2811 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2812 case SSL_INCOMING_STREAM_POLICY_REJECT:
2813 ctx.qc->incoming_stream_policy = policy;
2814 ctx.qc->incoming_stream_aec = aec;
2818 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
2823 qc_update_reject_policy(ctx.qc);
2824 quic_unlock(ctx.qc);
2832 struct wait_for_incoming_stream_args {
2838 static int wait_for_incoming_stream(void *arg)
2840 struct wait_for_incoming_stream_args *args = arg;
2841 QUIC_CONNECTION *qc = args->ctx->qc;
2842 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2844 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2845 /* If connection is torn down due to an error while blocking, stop. */
2846 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2850 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2851 if (args->qs != NULL)
2852 return 1; /* got a stream */
2854 return 0; /* did not get a stream, keep trying */
2858 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2863 QUIC_STREAM_MAP *qsm;
2866 OSSL_RTT_INFO rtt_info;
2868 if (!expect_quic_conn_only(s, &ctx))
2873 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2874 == SSL_INCOMING_STREAM_POLICY_REJECT) {
2875 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
2879 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2881 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2883 if (qc_blocking_mode(ctx.qc)
2884 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2885 struct wait_for_incoming_stream_args args;
2890 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2892 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2894 } else if (ret < 0 || args.qs == NULL) {
2904 xso = create_xso_from_stream(ctx.qc, qs);
2908 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2909 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2910 rtt_info.smoothed_rtt);
2913 /* Calling this function inhibits default XSO autocreation. */
2914 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2917 quic_unlock(ctx.qc);
2922 * SSL_get_accept_stream_queue_len
2923 * -------------------------------
2926 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2931 if (!expect_quic_conn_only(s, &ctx))
2936 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2938 quic_unlock(ctx.qc);
2946 int ossl_quic_stream_reset(SSL *ssl,
2947 const SSL_STREAM_RESET_ARGS *args,
2951 QUIC_STREAM_MAP *qsm;
2953 uint64_t error_code;
2956 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2959 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2960 qs = ctx.xso->stream;
2961 error_code = (args != NULL ? args->quic_error_code : 0);
2963 if (!quic_validate_for_write(ctx.xso, &err)) {
2964 ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2968 ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2971 quic_unlock(ctx.qc);
2976 * SSL_get_stream_read_state
2977 * -------------------------
2979 static void quic_classify_stream(QUIC_CONNECTION *qc,
2983 uint64_t *app_error_code)
2986 uint64_t final_size;
2988 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2990 if (app_error_code != NULL)
2991 *app_error_code = UINT64_MAX;
2993 app_error_code = &final_size; /* throw away value */
2995 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2997 * Unidirectional stream and this direction of transmission doesn't
3000 *state = SSL_STREAM_STATE_WRONG_DIR;
3001 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
3002 /* Connection already closed. */
3003 *state = SSL_STREAM_STATE_CONN_CLOSED;
3004 } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
3005 /* Application has read a FIN. */
3006 *state = SSL_STREAM_STATE_FINISHED;
3007 } else if ((!is_write && qs->stop_sending)
3008 || (is_write && ossl_quic_stream_send_is_reset(qs))) {
3010 * Stream has been reset locally. FIN takes precedence over this for the
3011 * read case as the application need not care if the stream is reset
3012 * after a FIN has been successfully processed.
3014 *state = SSL_STREAM_STATE_RESET_LOCAL;
3015 *app_error_code = !is_write
3016 ? qs->stop_sending_aec
3017 : qs->reset_stream_aec;
3018 } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
3019 || (is_write && qs->peer_stop_sending)) {
3021 * Stream has been reset remotely. */
3022 *state = SSL_STREAM_STATE_RESET_REMOTE;
3023 *app_error_code = !is_write
3024 ? qs->peer_reset_stream_aec
3025 : qs->peer_stop_sending_aec;
3026 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
3029 * Stream has been finished. Stream reset takes precedence over this for
3030 * the write case as peer may not have received all data.
3032 *state = SSL_STREAM_STATE_FINISHED;
3034 /* Stream still healthy. */
3035 *state = SSL_STREAM_STATE_OK;
3039 static int quic_get_stream_state(SSL *ssl, int is_write)
3044 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3045 return SSL_STREAM_STATE_NONE;
3047 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
3048 quic_unlock(ctx.qc);
3052 int ossl_quic_get_stream_read_state(SSL *ssl)
3054 return quic_get_stream_state(ssl, /*is_write=*/0);
3058 * SSL_get_stream_write_state
3059 * --------------------------
3061 int ossl_quic_get_stream_write_state(SSL *ssl)
3063 return quic_get_stream_state(ssl, /*is_write=*/1);
3067 * SSL_get_stream_read_error_code
3068 * ------------------------------
3070 static int quic_get_stream_error_code(SSL *ssl, int is_write,
3071 uint64_t *app_error_code)
3076 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3079 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
3080 &state, app_error_code);
3082 quic_unlock(ctx.qc);
3084 case SSL_STREAM_STATE_FINISHED:
3086 case SSL_STREAM_STATE_RESET_LOCAL:
3087 case SSL_STREAM_STATE_RESET_REMOTE:
3094 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
3096 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
3100 * SSL_get_stream_write_error_code
3101 * -------------------------------
3103 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
3105 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
3109 * Write buffer size mutation
3110 * --------------------------
3112 int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
3117 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3120 if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
3121 /* Called on a unidirectional receive-only stream - error. */
3122 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3126 if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
3128 * If the stream has a send part but we have disposed of it because we
3129 * no longer need it, this is a no-op.
3135 if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
3136 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3143 quic_unlock(ctx.qc);
3148 * SSL_get_conn_close_info
3149 * -----------------------
3151 int ossl_quic_get_conn_close_info(SSL *ssl,
3152 SSL_CONN_CLOSE_INFO *info,
3156 const QUIC_TERMINATE_CAUSE *tc;
3158 if (!expect_quic_conn_only(ssl, &ctx))
3161 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
3165 info->error_code = tc->error_code;
3166 info->reason = tc->reason;
3167 info->reason_len = tc->reason_len;
3168 info->is_local = !tc->remote;
3169 info->is_transport = !tc->app;
3177 int ossl_quic_key_update(SSL *ssl, int update_type)
3181 if (!expect_quic_conn_only(ssl, &ctx))
3184 switch (update_type) {
3185 case SSL_KEY_UPDATE_NOT_REQUESTED:
3187 * QUIC signals peer key update implicily by triggering a local
3188 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3190 case SSL_KEY_UPDATE_REQUESTED:
3194 QUIC_RAISE_NON_IO_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3200 /* Attempt to perform a TXKU. */
3201 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
3202 QUIC_RAISE_NON_IO_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
3203 quic_unlock(ctx.qc);
3207 quic_unlock(ctx.qc);
3212 * SSL_get_key_update_type
3213 * -----------------------
3215 int ossl_quic_get_key_update_type(const SSL *s)
3218 * We always handle key updates immediately so a key update is never
3221 return SSL_KEY_UPDATE_NONE;
3225 * QUIC Front-End I/O API: SSL_CTX Management
3226 * ==========================================
3229 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3233 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
3237 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3241 if (!expect_quic_conn_only(s, &ctx))
3245 case SSL_CTRL_SET_MSG_CALLBACK:
3246 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
3248 /* This callback also needs to be set on the internal SSL object */
3249 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
3252 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3253 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
3257 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3259 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
3262 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
3264 /* We never do renegotiation. */
3269 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3270 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3273 int ossl_quic_num_ciphers(void)
3278 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
3284 * Internal Testing APIs
3285 * =====================
3288 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
3292 if (!expect_quic_conn_only(s, &ctx))