2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/macros.h>
11 #include <openssl/objects.h>
12 #include <openssl/sslerr.h>
13 #include <crypto/rand.h>
14 #include "quic_local.h"
15 #include "internal/quic_tls.h"
16 #include "internal/quic_rx_depack.h"
17 #include "internal/quic_error.h"
18 #include "internal/time.h"
20 typedef struct qctx_st QCTX;
22 static void aon_write_finish(QUIC_XSO *xso);
23 static int create_channel(QUIC_CONNECTION *qc);
24 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
25 static int qc_try_create_default_xso_for_write(QCTX *ctx);
26 static int qc_wait_for_default_xso_for_read(QCTX *ctx);
27 static void quic_lock(QUIC_CONNECTION *qc);
28 static void quic_unlock(QUIC_CONNECTION *qc);
29 static int quic_do_handshake(QCTX *ctx);
30 static void qc_update_reject_policy(QUIC_CONNECTION *qc);
31 static void qc_touch_default_xso(QUIC_CONNECTION *qc);
32 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
33 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
34 int touch, QUIC_XSO **old_xso);
35 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
36 static int quic_validate_for_write(QUIC_XSO *xso, int *err);
37 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
40 * QUIC Front-End I/O API: Common Utilities
41 * ========================================
45 * Block until a predicate is met.
47 * Precondition: Must have a channel.
48 * Precondition: Must hold channel lock (unchecked).
51 static int block_until_pred(QUIC_CONNECTION *qc,
52 int (*pred)(void *arg), void *pred_arg,
57 assert(qc->ch != NULL);
60 * Any attempt to block auto-disables tick inhibition as otherwise we will
61 * hang around forever.
63 ossl_quic_channel_set_inhibit_tick(qc->ch, 0);
65 rtor = ossl_quic_channel_get_reactor(qc->ch);
66 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
70 static OSSL_TIME get_time(QUIC_CONNECTION *qc)
72 if (qc->override_now_cb != NULL)
73 return qc->override_now_cb(qc->override_now_cb_arg);
75 return ossl_time_now();
78 static OSSL_TIME get_time_cb(void *arg)
80 QUIC_CONNECTION *qc = arg;
86 * QCTX is a utility structure which provides information we commonly wish to
87 * unwrap upon an API call being dispatched to us, namely:
89 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
91 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
92 * a QCSO with a default stream was passed);
93 * - whether a QSSO was passed (xso == NULL must not be used to determine this
94 * because it may be non-NULL when a QCSO is passed if that QCSO has a
104 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
105 * rather than via ERR. Note that normal errors must always be raised while
109 static int quic_raise_normal_error(QCTX *ctx,
113 ctx->xso->last_error = err;
115 ctx->qc->last_error = err;
121 * Raise a 'non-normal' error, meaning any error that is not reported via
122 * SSL_get_error() and must be reported via ERR.
124 * qc should be provided if available. In exceptional circumstances when qc is
125 * not known NULL may be passed. This should generally only happen when an
126 * expect_...() function defined below fails, which generally indicates a
127 * dispatch error or caller error.
129 * ctx should be NULL if the connection lock is not held.
131 static int quic_raise_non_normal_error(QCTX *ctx,
142 if (ctx->is_stream && ctx->xso != NULL)
143 ctx->xso->last_error = SSL_ERROR_SSL;
144 else if (!ctx->is_stream && ctx->qc != NULL)
145 ctx->qc->last_error = SSL_ERROR_SSL;
147 if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
148 ossl_quic_channel_restore_err_state(ctx->qc->ch);
152 ERR_set_debug(file, line, func);
155 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
161 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
162 quic_raise_normal_error((ctx), (err))
164 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
165 quic_raise_non_normal_error((ctx), \
166 OPENSSL_FILE, OPENSSL_LINE, \
172 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
173 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
175 * After this returns 1, all fields of the passed QCTX are initialised.
176 * Returns 0 on failure. This function is intended to be used to provide API
177 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
179 static int expect_quic(const SSL *s, QCTX *ctx)
189 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
192 case SSL_TYPE_QUIC_CONNECTION:
193 qc = (QUIC_CONNECTION *)s;
195 ctx->xso = qc->default_xso;
199 case SSL_TYPE_QUIC_XSO:
207 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
212 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
213 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
216 * remote_init determines if we expect the default XSO to be remotely created or
217 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
219 * Channel mutex is acquired and retained on success.
222 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
225 if (!expect_quic(s, ctx))
230 if (ctx->xso == NULL && remote_init >= 0) {
231 if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) {
232 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
236 /* If we haven't finished the handshake, try to advance it. */
237 if (quic_do_handshake(ctx) < 1)
238 /* ossl_quic_do_handshake raised error here */
241 if (remote_init == 0) {
242 if (!qc_try_create_default_xso_for_write(ctx))
245 if (!qc_wait_for_default_xso_for_read(ctx))
249 ctx->xso = ctx->qc->default_xso;
252 if (ctx->xso == NULL) {
253 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
257 return 1; /* coverity[missing_unlock]: lock held */
260 quic_unlock(ctx->qc);
265 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
266 * be non-NULL if the QCSO has a default stream.
268 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
270 if (!expect_quic(s, ctx))
274 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
280 * Ensures that the channel mutex is held for a method which touches channel
283 * Precondition: Channel mutex is not held (unchecked)
285 static void quic_lock(QUIC_CONNECTION *qc)
287 #if defined(OPENSSL_THREADS)
288 ossl_crypto_mutex_lock(qc->mutex);
292 /* Precondition: Channel mutex is held (unchecked) */
294 static void quic_unlock(QUIC_CONNECTION *qc)
296 #if defined(OPENSSL_THREADS)
297 ossl_crypto_mutex_unlock(qc->mutex);
302 * This predicate is the criterion which should determine API call rejection for
303 * *most* mutating API calls, particularly stream-related operations for send
306 * A call is rejected (this function returns 0) if shutdown is in progress
307 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
308 * req_active=1, the connection must be active (i.e., the IDLE state is also
311 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
313 if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
316 if (req_active && !ossl_quic_channel_is_active(qc->ch))
323 * QUIC Front-End I/O API: Initialization
324 * ======================================
326 * SSL_new => ossl_quic_new
328 * SSL_reset => ossl_quic_reset
329 * SSL_clear => ossl_quic_clear
331 * SSL_free => ossl_quic_free
333 * SSL_set_options => ossl_quic_set_options
334 * SSL_get_options => ossl_quic_get_options
335 * SSL_clear_options => ossl_quic_clear_options
340 SSL *ossl_quic_new(SSL_CTX *ctx)
342 QUIC_CONNECTION *qc = NULL;
343 SSL *ssl_base = NULL;
344 SSL_CONNECTION *sc = NULL;
346 qc = OPENSSL_zalloc(sizeof(*qc));
350 /* Initialise the QUIC_CONNECTION's stub header. */
352 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
357 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
358 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
361 /* override the user_ssl of the inner connection */
362 sc->s3.flags |= TLS1_FLAGS_QUIC;
364 /* Restrict options derived from the SSL_CTX. */
365 sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
368 #if defined(OPENSSL_THREADS)
369 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
373 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
374 qc->is_thread_assisted
375 = (ssl_base->method == OSSL_QUIC_client_thread_method());
378 qc->as_server = 0; /* TODO(QUIC SERVER): add server support */
379 qc->as_server_state = qc->as_server;
381 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
382 qc->default_ssl_mode = qc->ssl.ctx->mode;
383 qc->default_ssl_options = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
384 qc->default_blocking = 1;
386 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
387 qc->last_error = SSL_ERROR_NONE;
389 if (!create_channel(qc))
392 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
393 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
395 qc_update_reject_policy(qc);
398 * We do not create the default XSO yet. The reason for this is that the
399 * stream ID of the default XSO will depend on whether the stream is client
400 * or server-initiated, which depends on who transmits first. Since we do
401 * not know whether the application will be using a client-transmits-first
402 * or server-transmits-first protocol, we defer default XSO creation until
403 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
404 * we take that as a cue that the client is expecting a server-initiated
405 * stream, and vice versa if SSL_write() is called first.
411 #if defined(OPENSSL_THREADS)
412 ossl_crypto_mutex_free(qc->mutex);
414 ossl_quic_channel_free(qc->ch);
423 void ossl_quic_free(SSL *s)
428 /* We should never be called on anything but a QSO. */
429 if (!expect_quic(s, &ctx))
436 * When a QSSO is freed, the XSO is freed immediately, because the XSO
437 * itself only contains API personality layer data. However the
438 * underlying QUIC_STREAM is not freed immediately but is instead marked
439 * as deleted for later collection.
442 assert(ctx.qc->num_xso > 0);
445 /* If a stream's send part has not been finished, auto-reset it. */
446 if (( ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
447 || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
448 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
449 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
452 /* Do STOP_SENDING for the receive part, if applicable. */
453 if ( ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
454 || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
455 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
458 /* Update stream state. */
459 ctx.xso->stream->deleted = 1;
460 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
463 is_default = (ctx.xso == ctx.qc->default_xso);
467 * Unref the connection in most cases; the XSO has a ref to the QC and
468 * not vice versa. But for a default XSO, to avoid circular references,
469 * the QC refs the XSO but the XSO does not ref the QC. If we are the
470 * default XSO, we only get here when the QC is being torn down anyway,
471 * so don't call SSL_free(qc) as we are already in it.
474 SSL_free(&ctx.qc->ssl);
476 /* Note: SSL_free calls OPENSSL_free(xso) for us */
481 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
482 * stage, but is freed during the channel free when the whole QSM is freed.
484 if (ctx.qc->default_xso != NULL) {
485 QUIC_XSO *xso = ctx.qc->default_xso;
490 ctx.qc->default_xso = NULL;
493 /* Ensure we have no remaining XSOs. */
494 assert(ctx.qc->num_xso == 0);
496 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
497 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
498 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
499 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
503 ossl_quic_channel_free(ctx.qc->ch);
505 BIO_free(ctx.qc->net_rbio);
506 BIO_free(ctx.qc->net_wbio);
508 /* Note: SSL_free calls OPENSSL_free(qc) for us */
510 SSL_free(ctx.qc->tls);
511 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
512 #if defined(OPENSSL_THREADS)
513 ossl_crypto_mutex_free(&ctx.qc->mutex);
517 /* SSL method init */
518 int ossl_quic_init(SSL *s)
520 /* Same op as SSL_clear, forward the call. */
521 return ossl_quic_clear(s);
524 /* SSL method deinit */
525 void ossl_quic_deinit(SSL *s)
530 /* SSL_clear (ssl_reset method) */
531 int ossl_quic_reset(SSL *s)
535 if (!expect_quic(s, &ctx))
538 ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
542 /* ssl_clear method (unused) */
543 int ossl_quic_clear(SSL *s)
547 if (!expect_quic(s, &ctx))
550 /* TODO(QUIC FUTURE): Currently a no-op. */
554 int ossl_quic_conn_set_override_now_cb(SSL *s,
555 OSSL_TIME (*now_cb)(void *arg),
560 if (!expect_quic(s, &ctx))
565 ctx.qc->override_now_cb = now_cb;
566 ctx.qc->override_now_cb_arg = now_cb_arg;
572 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
576 if (!expect_quic(s, &ctx))
579 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
580 if (ctx.qc->is_thread_assisted && ctx.qc->started)
581 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
586 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
588 qc->default_xso_created = 1;
589 qc_update_reject_policy(qc);
593 * Changes default XSO. Allows caller to keep reference to the old default XSO
594 * (if any). Reference to new XSO is transferred from caller.
597 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
605 if (qc->default_xso != xso) {
606 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
608 qc->default_xso = xso;
612 * Changing to not having a default XSO. XSO becomes standalone and
613 * now has a ref to the QC.
615 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
619 * Changing from not having a default XSO to having one. The new XSO
620 * will have had a reference to the QC we need to drop to avoid a
621 * circular reference.
623 * Currently we never change directly from one default XSO to
624 * another, though this function would also still be correct if this
627 assert(*old_xso == NULL);
629 CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
635 qc_touch_default_xso(qc);
639 * Changes default XSO, releasing the reference to any previous default XSO.
640 * Reference to new XSO is transferred from caller.
643 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
645 QUIC_XSO *old_xso = NULL;
647 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
650 SSL_free(&old_xso->ssl);
654 static void xso_update_options(QUIC_XSO *xso)
656 int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
658 if (xso->stream->rstream != NULL)
659 ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
661 if (xso->stream->sstream != NULL)
662 ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
669 * Setting options on a QCSO
670 * - configures the handshake-layer options;
671 * - configures the default data-plane options for new streams;
672 * - configures the data-plane options on the default XSO, if there is one.
674 * Setting options on a QSSO
675 * - configures data-plane options for that stream only.
678 static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
681 uint64_t hs_mask_value, hs_or_value, ret;
683 if (!expect_quic(ssl, &ctx))
688 if (!ctx.is_stream) {
690 * If we were called on the connection, we apply any handshake option
693 hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
694 hs_or_value = (or_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
696 SSL_clear_options(ctx.qc->tls, hs_mask_value);
697 SSL_set_options(ctx.qc->tls, hs_or_value);
699 /* Update defaults for new streams. */
700 ctx.qc->default_ssl_options
701 = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
702 & OSSL_QUIC_PERMITTED_OPTIONS;
705 if (ctx.xso != NULL) {
707 = ((ctx.xso->ssl_options & ~mask_value) | or_value)
708 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
710 xso_update_options(ctx.xso);
713 ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;
719 uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
721 return quic_mask_or_options(ssl, 0, options);
724 /* SSL_clear_options */
725 uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
727 return quic_mask_or_options(ssl, options, 0);
730 /* SSL_get_options */
731 uint64_t ossl_quic_get_options(const SSL *ssl)
733 return quic_mask_or_options((SSL *)ssl, 0, 0);
737 * QUIC Front-End I/O API: Network BIO Configuration
738 * =================================================
740 * Handling the different BIOs is difficult:
742 * - It is more or less a requirement that we use non-blocking network I/O;
743 * we need to be able to have timeouts on recv() calls, and make best effort
744 * (non blocking) send() and recv() calls.
746 * The only sensible way to do this is to configure the socket into
747 * non-blocking mode. We could try to do select() before calling send() or
748 * recv() to get a guarantee that the call will not block, but this will
749 * probably run into issues with buggy OSes which generate spurious socket
750 * readiness events. In any case, relying on this to work reliably does not
753 * Timeouts could be handled via setsockopt() socket timeout options, but
754 * this depends on OS support and adds another syscall to every network I/O
755 * operation. It also has obvious thread safety concerns if we want to move
756 * to concurrent use of a single socket at some later date.
758 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
759 * be made non-blocking. However some OSes (e.g. Windows) do not support
760 * this, so we cannot rely on this.
762 * As such, we need to configure any FD in non-blocking mode. This may
763 * confound users who pass a blocking socket to libssl. However, in practice
764 * it would be extremely strange for a user of QUIC to pass an FD to us,
765 * then also try and send receive traffic on the same socket(!). Thus the
766 * impact of this should be limited, and can be documented.
768 * - We support both blocking and non-blocking operation in terms of the API
769 * presented to the user. One prospect is to set the blocking mode based on
770 * whether the socket passed to us was already in blocking mode. However,
771 * Windows has no API for determining if a socket is in blocking mode (!),
772 * therefore this cannot be done portably. Currently therefore we expose an
773 * explicit API call to set this, and default to blocking mode.
775 * - We need to determine our initial destination UDP address. The "natural"
776 * way for a user to do this is to set the peer variable on a BIO_dgram.
777 * However, this has problems because BIO_dgram's peer variable is used for
778 * both transmission and reception. This means it can be constantly being
779 * changed to a malicious value (e.g. if some random unrelated entity on the
780 * network starts sending traffic to us) on every read call. This is not a
781 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
782 * calls only, which do not use this variable. However, we do need to let
783 * the user specify the peer in a 'normal' manner. The compromise here is
784 * that we grab the current peer value set at the time the write BIO is set
785 * and do not read the value again.
787 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
788 * Currently we do this by only supporting non-blocking mode.
793 * Determines what initial destination UDP address we should use, if possible.
794 * If this fails the client must set the destination address manually, or use a
795 * BIO which does not need a destination address.
797 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
799 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
805 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
809 if (!expect_quic(s, &ctx))
812 if (ctx.qc->net_rbio == net_rbio)
815 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
818 BIO_free(ctx.qc->net_rbio);
819 ctx.qc->net_rbio = net_rbio;
822 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
823 * mode as we do not support it for non-pollable BIOs.
825 if (net_rbio != NULL) {
826 BIO_POLL_DESCRIPTOR d = {0};
828 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
829 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
830 ctx.qc->blocking = 0;
831 ctx.qc->default_blocking = 0;
832 ctx.qc->can_poll_net_rbio = 0;
834 ctx.qc->can_poll_net_rbio = 1;
839 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
843 if (!expect_quic(s, &ctx))
846 if (ctx.qc->net_wbio == net_wbio)
849 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
852 BIO_free(ctx.qc->net_wbio);
853 ctx.qc->net_wbio = net_wbio;
855 if (net_wbio != NULL) {
856 BIO_POLL_DESCRIPTOR d = {0};
858 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
859 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
860 ctx.qc->blocking = 0;
861 ctx.qc->default_blocking = 0;
862 ctx.qc->can_poll_net_wbio = 0;
864 ctx.qc->can_poll_net_wbio = 1;
868 * If we do not have a peer address yet, and we have not started trying
869 * to connect yet, try to autodetect one.
871 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
872 && !ctx.qc->started) {
873 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
875 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
877 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
878 &ctx.qc->init_peer_addr);
883 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
887 if (!expect_quic(s, &ctx))
890 return ctx.qc->net_rbio;
893 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
897 if (!expect_quic(s, &ctx))
900 return ctx.qc->net_wbio;
903 int ossl_quic_conn_get_blocking_mode(const SSL *s)
907 if (!expect_quic(s, &ctx))
911 return ctx.xso->blocking;
913 return ctx.qc->blocking;
916 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
920 if (!expect_quic(s, &ctx))
923 /* Cannot enable blocking mode if we do not have pollable FDs. */
925 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
926 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
928 if (!ctx.is_stream) {
930 * If called on a QCSO, update default and connection-level blocking
933 ctx.qc->blocking = (blocking != 0);
934 ctx.qc->default_blocking = ctx.qc->blocking;
939 * If called on a QSSO or QCSO with a default XSO, update blocking
942 ctx.xso->blocking = (blocking != 0);
947 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
948 const BIO_ADDR *peer_addr)
952 if (!expect_quic(s, &ctx))
956 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
959 if (peer_addr == NULL) {
960 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
964 ctx.qc->init_peer_addr = *peer_addr;
969 * QUIC Front-End I/O API: Asynchronous I/O Management
970 * ===================================================
972 * (BIO/)SSL_handle_events => ossl_quic_handle_events
973 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
974 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
978 /* Returns 1 if the connection is being used in blocking mode. */
979 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
984 static int xso_blocking_mode(const QUIC_XSO *xso)
987 && xso->conn->can_poll_net_rbio
988 && xso->conn->can_poll_net_wbio;
991 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
993 int ossl_quic_handle_events(SSL *s)
997 if (!expect_quic(s, &ctx))
1001 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1002 quic_unlock(ctx.qc);
1007 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1008 * should next have events handled by the application by calling
1009 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1010 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1011 * the value of *tv is undefined.
1014 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1017 OSSL_TIME deadline = ossl_time_infinite();
1019 if (!expect_quic(s, &ctx))
1025 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
1027 if (ossl_time_is_infinite(deadline)) {
1031 * Robustness against faulty applications that don't check *is_infinite;
1032 * harmless long timeout.
1034 tv->tv_sec = 1000000;
1037 quic_unlock(ctx.qc);
1041 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
1043 quic_unlock(ctx.qc);
1047 /* SSL_get_rpoll_descriptor */
1048 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1052 if (!expect_quic(s, &ctx))
1055 if (desc == NULL || ctx.qc->net_rbio == NULL) {
1056 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1060 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
1063 /* SSL_get_wpoll_descriptor */
1064 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1068 if (!expect_quic(s, &ctx))
1071 if (desc == NULL || ctx.qc->net_wbio == NULL) {
1072 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1076 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
1079 /* SSL_net_read_desired */
1081 int ossl_quic_get_net_read_desired(SSL *s)
1086 if (!expect_quic(s, &ctx))
1090 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1091 quic_unlock(ctx.qc);
1095 /* SSL_net_write_desired */
1097 int ossl_quic_get_net_write_desired(SSL *s)
1102 if (!expect_quic(s, &ctx))
1106 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1107 quic_unlock(ctx.qc);
1112 * QUIC Front-End I/O API: Connection Lifecycle Operations
1113 * =======================================================
1115 * SSL_do_handshake => ossl_quic_do_handshake
1116 * SSL_set_connect_state => ossl_quic_set_connect_state
1117 * SSL_set_accept_state => ossl_quic_set_accept_state
1118 * SSL_shutdown => ossl_quic_shutdown
1119 * SSL_ctrl => ossl_quic_ctrl
1120 * (BIO/)SSL_connect => ossl_quic_connect
1121 * (BIO/)SSL_accept => ossl_quic_accept
1126 static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1128 QUIC_STREAM_MAP *qsm;
1130 if (qc->shutting_down)
1133 qsm = ossl_quic_channel_get_qsm(qc->ch);
1135 ossl_quic_stream_map_begin_shutdown_flush(qsm);
1136 qc->shutting_down = 1;
1139 /* Returns 1 if all shutdown-flush streams have been done with. */
1141 static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1143 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1145 return qc->shutting_down
1146 && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1150 static int quic_shutdown_wait(void *arg)
1152 QUIC_CONNECTION *qc = arg;
1154 return ossl_quic_channel_is_terminated(qc->ch);
1157 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
1158 static int quic_shutdown_flush_wait(void *arg)
1160 QUIC_CONNECTION *qc = arg;
1162 return ossl_quic_channel_is_term_any(qc->ch)
1163 || qc_shutdown_flush_finished(qc);
1167 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1168 const SSL_SHUTDOWN_EX_ARGS *args,
1173 int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1175 if (!expect_quic(s, &ctx))
1183 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1184 quic_unlock(ctx.qc);
1188 /* Phase 1: Stream Flushing */
1190 qc_shutdown_flush_init(ctx.qc);
1192 if (!qc_shutdown_flush_finished(ctx.qc)) {
1193 if (qc_blocking_mode(ctx.qc)) {
1194 ret = block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);
1200 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1204 if (!qc_shutdown_flush_finished(ctx.qc)) {
1205 quic_unlock(ctx.qc);
1206 return 0; /* ongoing */
1210 /* Phase 2: Connection Closure */
1211 ossl_quic_channel_local_close(ctx.qc->ch,
1212 args != NULL ? args->quic_error_code : 0,
1213 args != NULL ? args->quic_reason : NULL);
1215 SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1217 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1218 quic_unlock(ctx.qc);
1222 /* Phase 3: Terminating Wait Time */
1223 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1224 ret = block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1230 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1233 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1235 quic_unlock(ctx.qc);
1240 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1244 if (!expect_quic(s, &ctx))
1249 /* If called on a QCSO, update the default mode. */
1251 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1254 * If we were called on a QSSO or have a default stream, we also update
1257 if (ctx.xso != NULL) {
1258 /* Cannot enable EPW while AON write in progress. */
1259 if (ctx.xso->aon_write_in_progress)
1260 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1262 ctx.xso->ssl_mode |= (uint32_t)larg;
1263 return ctx.xso->ssl_mode;
1266 return ctx.qc->default_ssl_mode;
1267 case SSL_CTRL_CLEAR_MODE:
1269 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1271 if (ctx.xso != NULL) {
1272 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1273 return ctx.xso->ssl_mode;
1276 return ctx.qc->default_ssl_mode;
1278 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1279 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1280 /* This ctrl also needs to be passed to the internal SSL object */
1281 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1283 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1287 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1290 return !is_infinite;
1292 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1293 /* For legacy compatibility with DTLS calls. */
1294 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1296 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1297 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1301 /* SSL_set_connect_state */
1302 void ossl_quic_set_connect_state(SSL *s)
1306 if (!expect_quic(s, &ctx))
1309 /* Cannot be changed after handshake started */
1310 if (ctx.qc->started || ctx.is_stream)
1313 ctx.qc->as_server_state = 0;
1316 /* SSL_set_accept_state */
1317 void ossl_quic_set_accept_state(SSL *s)
1321 if (!expect_quic(s, &ctx))
1324 /* Cannot be changed after handshake started */
1325 if (ctx.qc->started || ctx.is_stream)
1328 ctx.qc->as_server_state = 1;
1331 /* SSL_do_handshake */
1332 struct quic_handshake_wait_args {
1333 QUIC_CONNECTION *qc;
1336 static int quic_handshake_wait(void *arg)
1338 struct quic_handshake_wait_args *args = arg;
1340 if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1343 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1349 static int configure_channel(QUIC_CONNECTION *qc)
1351 assert(qc->ch != NULL);
1353 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1354 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1355 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1362 static int create_channel(QUIC_CONNECTION *qc)
1364 QUIC_CHANNEL_ARGS args = {0};
1366 args.libctx = qc->ssl.ctx->libctx;
1367 args.propq = qc->ssl.ctx->propq;
1368 args.is_server = qc->as_server;
1370 args.mutex = qc->mutex;
1371 args.now_cb = get_time_cb;
1372 args.now_cb_arg = qc;
1374 qc->ch = ossl_quic_channel_new(&args);
1382 * Creates a channel and configures it with the information we have accumulated
1383 * via calls made to us from the application prior to starting a handshake
1387 static int ensure_channel_started(QUIC_CONNECTION *qc)
1390 if (!configure_channel(qc)
1391 || !ossl_quic_channel_start(qc->ch))
1394 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1395 if (qc->is_thread_assisted)
1396 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1405 ossl_quic_channel_free(qc->ch);
1411 static int quic_do_handshake(QCTX *ctx)
1414 QUIC_CONNECTION *qc = ctx->qc;
1416 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1417 /* Handshake already completed. */
1420 if (!quic_mutation_allowed(qc, /*req_active=*/0))
1421 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1423 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1424 /* Peer address must have been set. */
1425 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1426 return -1; /* Non-protocol error */
1429 if (qc->as_server != qc->as_server_state) {
1430 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1431 return -1; /* Non-protocol error */
1434 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1435 /* Need read and write BIOs. */
1436 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1437 return -1; /* Non-protocol error */
1441 * Start connection process. Note we may come here multiple times in
1442 * non-blocking mode, which is fine.
1444 if (!ensure_channel_started(qc)) {
1445 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1446 return -1; /* Non-protocol error */
1449 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1450 /* The handshake is now done. */
1453 if (qc_blocking_mode(qc)) {
1454 /* In blocking mode, wait for the handshake to complete. */
1455 struct quic_handshake_wait_args args;
1459 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1460 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1461 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1462 return 0; /* Shutdown before completion */
1463 } else if (ret <= 0) {
1464 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1465 return -1; /* Non-protocol error */
1468 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1471 /* Try to advance the reactor. */
1472 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1474 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1475 /* The handshake is now done. */
1478 /* Otherwise, indicate that the handshake isn't done yet. */
1479 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1480 return -1; /* Non-protocol error */
1485 int ossl_quic_do_handshake(SSL *s)
1490 if (!expect_quic(s, &ctx))
1495 ret = quic_do_handshake(&ctx);
1496 quic_unlock(ctx.qc);
1501 int ossl_quic_connect(SSL *s)
1503 /* Ensure we are in connect state (no-op if non-idle). */
1504 ossl_quic_set_connect_state(s);
1506 /* Begin or continue the handshake */
1507 return ossl_quic_do_handshake(s);
1511 int ossl_quic_accept(SSL *s)
1513 /* Ensure we are in accept state (no-op if non-idle). */
1514 ossl_quic_set_accept_state(s);
1516 /* Begin or continue the handshake */
1517 return ossl_quic_do_handshake(s);
1521 * QUIC Front-End I/O API: Stream Lifecycle Operations
1522 * ===================================================
1524 * SSL_stream_new => ossl_quic_conn_stream_new
1529 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1530 * default XSO was created. Returns 0 if it was not (e.g. because it already
1531 * exists). Note that this is NOT an error condition.
1534 static int qc_try_create_default_xso_for_write(QCTX *ctx)
1537 QUIC_CONNECTION *qc = ctx->qc;
1539 if (qc->default_xso_created
1540 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1542 * We only do this once. If the user detaches a previously created
1543 * default XSO we don't auto-create another one.
1545 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1547 /* Create a locally-initiated stream. */
1548 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1549 flags |= SSL_STREAM_FLAG_UNI;
1551 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1554 if (qc->default_xso == NULL)
1555 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1557 qc_touch_default_xso(qc);
1561 struct quic_wait_for_stream_args {
1562 QUIC_CONNECTION *qc;
1569 static int quic_wait_for_stream(void *arg)
1571 struct quic_wait_for_stream_args *args = arg;
1573 if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
1574 /* If connection is torn down due to an error while blocking, stop. */
1575 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1579 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1580 args->expect_id | QUIC_STREAM_DIR_BIDI);
1581 if (args->qs == NULL)
1582 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1583 args->expect_id | QUIC_STREAM_DIR_UNI);
1585 if (args->qs != NULL)
1586 return 1; /* stream now exists */
1588 return 0; /* did not get a stream, keep trying */
1592 static int qc_wait_for_default_xso_for_read(QCTX *ctx)
1594 /* Called on a QCSO and we don't currently have a default stream. */
1596 QUIC_CONNECTION *qc = ctx->qc;
1599 struct quic_wait_for_stream_args wargs;
1602 * If default stream functionality is disabled or we already detached
1603 * one, don't make another default stream and just fail.
1605 if (qc->default_xso_created
1606 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1607 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1610 * The peer may have opened a stream since we last ticked. So tick and
1611 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1612 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1613 * first stream created by a peer must have an ordinal of 0.
1615 expect_id = qc->as_server
1616 ? QUIC_STREAM_INITIATOR_CLIENT
1617 : QUIC_STREAM_INITIATOR_SERVER;
1619 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1620 expect_id | QUIC_STREAM_DIR_BIDI);
1622 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1623 expect_id | QUIC_STREAM_DIR_UNI);
1626 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1628 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1633 if (!qc_blocking_mode(qc))
1634 /* Non-blocking mode, so just bail immediately. */
1635 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1637 /* Block until we have a stream. */
1641 wargs.expect_id = expect_id;
1643 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1645 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1646 else if (res < 0 || wargs.qs == NULL)
1647 /* quic_wait_for_stream raised error here */
1654 * We now have qs != NULL. Make it the default stream, creating the
1657 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1658 if (qc->default_xso == NULL)
1659 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1661 qc_touch_default_xso(qc); /* inhibits default XSO */
1666 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1668 QUIC_XSO *xso = NULL;
1670 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1673 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1677 if (!SSL_up_ref(&qc->ssl))
1681 xso->blocking = qc->default_blocking;
1682 xso->ssl_mode = qc->default_ssl_mode;
1684 = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1685 xso->last_error = SSL_ERROR_NONE;
1690 xso_update_options(xso);
1698 /* locking depends on need_lock */
1699 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1701 QUIC_CONNECTION *qc = ctx->qc;
1702 QUIC_XSO *xso = NULL;
1703 QUIC_STREAM *qs = NULL;
1704 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1709 if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
1710 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1714 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1716 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1720 xso = create_xso_from_stream(qc, qs);
1722 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1726 qc_touch_default_xso(qc); /* inhibits default XSO */
1734 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1743 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1747 if (!expect_quic_conn_only(s, &ctx))
1750 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
1754 * QUIC Front-End I/O API: Steady-State Operations
1755 * ===============================================
1757 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1758 * is, the functions used during the established phase of a QUIC connection
1759 * (e.g. SSL_read, SSL_write).
1761 * Each function must handle both blocking and non-blocking modes. As discussed
1762 * above, all QUIC I/O is implemented using non-blocking mode internally.
1764 * SSL_get_error => partially implemented by ossl_quic_get_error
1765 * (BIO/)SSL_read => ossl_quic_read
1766 * (BIO/)SSL_write => ossl_quic_write
1767 * SSL_pending => ossl_quic_pending
1768 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1769 * SSL_key_update => ossl_quic_key_update
1773 int ossl_quic_get_error(const SSL *s, int i)
1776 int net_error, last_error;
1778 if (!expect_quic(s, &ctx))
1782 net_error = ossl_quic_channel_net_error(ctx.qc->ch);
1783 last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
1784 quic_unlock(ctx.qc);
1787 return SSL_ERROR_SYSCALL;
1796 * The set of functions below provide the implementation of the public SSL_write
1797 * function. We must handle:
1799 * - both blocking and non-blocking operation at the application level,
1800 * depending on how we are configured;
1802 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1804 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1808 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1811 * We have appended at least one byte to the stream.
1812 * Potentially mark stream as active, depending on FC.
1815 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1821 * TODO(QUIC FUTURE): It is probably inefficient to try and do this
1822 * immediately, plus we should eventually consider Nagle's algorithm.
1825 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1828 struct quic_write_again_args {
1830 const unsigned char *buf;
1832 size_t total_written;
1837 static int quic_write_again(void *arg)
1839 struct quic_write_again_args *args = arg;
1840 size_t actual_written = 0;
1842 if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
1843 /* If connection is torn down due to an error while blocking, stop. */
1846 if (!quic_validate_for_write(args->xso, &args->err))
1848 * Stream may have become invalid for write due to connection events
1853 args->err = ERR_R_INTERNAL_ERROR;
1854 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1855 args->buf, args->len, &actual_written))
1858 quic_post_write(args->xso, actual_written > 0, 0);
1860 args->buf += actual_written;
1861 args->len -= actual_written;
1862 args->total_written += actual_written;
1865 /* Written everything, done. */
1868 /* Not written everything yet, keep trying. */
1873 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
1877 QUIC_XSO *xso = ctx->xso;
1878 struct quic_write_again_args args;
1879 size_t actual_written = 0;
1881 /* First make a best effort to append as much of the data as possible. */
1882 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1884 /* Stream already finished or allocation error. */
1886 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1889 quic_post_write(xso, actual_written > 0, 1);
1891 if (actual_written == len) {
1892 /* Managed to append everything on the first try. */
1893 *written = actual_written;
1898 * We did not manage to append all of the data immediately, so the stream
1899 * buffer has probably filled up. This means we need to block until some of
1903 args.buf = (const unsigned char *)buf + actual_written;
1904 args.len = len - actual_written;
1905 args.total_written = 0;
1906 args.err = ERR_R_INTERNAL_ERROR;
1908 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1910 if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
1911 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1913 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
1916 *written = args.total_written;
1921 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1924 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1925 size_t buf_len, size_t already_sent)
1927 assert(!xso->aon_write_in_progress);
1929 xso->aon_write_in_progress = 1;
1930 xso->aon_buf_base = buf;
1931 xso->aon_buf_pos = already_sent;
1932 xso->aon_buf_len = buf_len;
1935 static void aon_write_finish(QUIC_XSO *xso)
1937 xso->aon_write_in_progress = 0;
1938 xso->aon_buf_base = NULL;
1939 xso->aon_buf_pos = 0;
1940 xso->aon_buf_len = 0;
1944 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
1945 size_t len, size_t *written)
1947 QUIC_XSO *xso = ctx->xso;
1948 const void *actual_buf;
1949 size_t actual_len, actual_written = 0;
1950 int accept_moving_buffer
1951 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1953 if (xso->aon_write_in_progress) {
1955 * We are in the middle of an AON write (i.e., a previous write did not
1956 * manage to append all data to the SSTREAM and we have Enable Partial
1957 * Write (EPW) mode disabled.)
1959 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1960 || len != xso->aon_buf_len)
1962 * Pointer must not have changed if we are not in accept moving
1963 * buffer mode. Length must never change.
1965 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
1967 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1968 actual_len = len - xso->aon_buf_pos;
1969 assert(actual_len > 0);
1975 /* First make a best effort to append as much of the data as possible. */
1976 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1978 /* Stream already finished or allocation error. */
1980 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1983 quic_post_write(xso, actual_written > 0, 1);
1985 if (actual_written == actual_len) {
1986 /* We have sent everything. */
1987 if (xso->aon_write_in_progress) {
1989 * We have sent everything, and we were in the middle of an AON
1990 * write. The output write length is the total length of the AON
1991 * buffer, not however many bytes we managed to write to the stream
1994 *written = xso->aon_buf_len;
1995 aon_write_finish(xso);
1997 *written = actual_written;
2003 if (xso->aon_write_in_progress) {
2005 * AON write is in progress but we have not written everything yet. We
2006 * may have managed to send zero bytes, or some number of bytes less
2007 * than the total remaining which need to be appended during this
2010 xso->aon_buf_pos += actual_written;
2011 assert(xso->aon_buf_pos < xso->aon_buf_len);
2012 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2016 * Not in an existing AON operation but partial write is not enabled, so we
2017 * need to begin a new AON operation. However we needn't bother if we didn't
2018 * actually append anything.
2020 if (actual_written > 0)
2021 aon_write_begin(xso, buf, len, actual_written);
2024 * AON - We do not publicly admit to having appended anything until AON
2028 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2032 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2035 QUIC_XSO *xso = ctx->xso;
2037 /* Simple best effort operation. */
2038 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
2039 /* Stream already finished or allocation error. */
2041 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2044 quic_post_write(xso, *written > 0, 1);
2049 static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2051 QUIC_STREAM_MAP *qsm;
2053 if (xso == NULL || xso->stream == NULL) {
2054 *err = ERR_R_INTERNAL_ERROR;
2058 switch (xso->stream->send_state) {
2060 case QUIC_SSTREAM_STATE_NONE:
2061 *err = SSL_R_STREAM_RECV_ONLY;
2064 case QUIC_SSTREAM_STATE_READY:
2065 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2067 if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2068 *err = ERR_R_INTERNAL_ERROR;
2073 case QUIC_SSTREAM_STATE_SEND:
2074 case QUIC_SSTREAM_STATE_DATA_SENT:
2075 case QUIC_SSTREAM_STATE_DATA_RECVD:
2076 if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2077 *err = SSL_R_STREAM_FINISHED;
2083 case QUIC_SSTREAM_STATE_RESET_SENT:
2084 case QUIC_SSTREAM_STATE_RESET_RECVD:
2085 *err = SSL_R_STREAM_RESET;
2091 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2095 int partial_write, err;
2099 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2102 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
2104 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2105 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2110 * If we haven't finished the handshake, try to advance it.
2111 * We don't accept writes until the handshake is completed.
2113 if (quic_do_handshake(&ctx) < 1) {
2118 /* Ensure correct stream state, stream send part not concluded, etc. */
2119 if (!quic_validate_for_write(ctx.xso, &err)) {
2120 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2129 if (xso_blocking_mode(ctx.xso))
2130 ret = quic_write_blocking(&ctx, buf, len, written);
2131 else if (partial_write)
2132 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
2134 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
2137 quic_unlock(ctx.qc);
2145 struct quic_read_again_args {
2147 QUIC_STREAM *stream;
2155 static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2157 QUIC_STREAM_MAP *qsm;
2161 if (xso == NULL || xso->stream == NULL) {
2162 *err = ERR_R_INTERNAL_ERROR;
2166 switch (xso->stream->recv_state) {
2168 case QUIC_RSTREAM_STATE_NONE:
2169 *err = SSL_R_STREAM_SEND_ONLY;
2172 case QUIC_RSTREAM_STATE_RECV:
2173 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2174 case QUIC_RSTREAM_STATE_DATA_RECVD:
2177 case QUIC_RSTREAM_STATE_DATA_READ:
2181 case QUIC_RSTREAM_STATE_RESET_RECVD:
2182 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2183 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2186 case QUIC_RSTREAM_STATE_RESET_READ:
2187 *err = SSL_R_STREAM_RESET;
2193 static int quic_read_actual(QCTX *ctx,
2194 QUIC_STREAM *stream,
2195 void *buf, size_t buf_len,
2199 int is_fin = 0, err, eos;
2200 QUIC_CONNECTION *qc = ctx->qc;
2202 if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2204 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2206 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2210 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2211 bytes_read, &is_fin))
2212 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2215 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2216 bytes_read, &is_fin))
2217 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2221 if (*bytes_read > 0) {
2223 * We have read at least one byte from the stream. Inform stream-level
2224 * RXFC of the retirement of controlled bytes. Update the active stream
2225 * status (the RXFC may now want to emit a frame granting more credit to
2228 OSSL_RTT_INFO rtt_info;
2230 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2232 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2233 rtt_info.smoothed_rtt))
2234 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2237 if (is_fin && !peek) {
2238 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2240 ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2243 if (*bytes_read > 0)
2244 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2248 if (*bytes_read == 0 && is_fin)
2249 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2255 static int quic_read_again(void *arg)
2257 struct quic_read_again_args *args = arg;
2259 if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
2260 /* If connection is torn down due to an error while blocking, stop. */
2261 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2265 if (!quic_read_actual(args->ctx, args->stream,
2266 args->buf, args->len, args->bytes_read,
2270 if (*args->bytes_read > 0)
2271 /* got at least one byte, the SSL_read op can finish now */
2274 return 0; /* did not read anything, keep trying */
2278 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2282 struct quic_read_again_args args;
2286 if (!expect_quic(s, &ctx))
2291 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2292 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2296 /* If we haven't finished the handshake, try to advance it. */
2297 if (quic_do_handshake(&ctx) < 1) {
2298 ret = 0; /* ossl_quic_do_handshake raised error here */
2302 if (ctx.xso == NULL) {
2304 * Called on a QCSO and we don't currently have a default stream.
2306 * Wait until we get a stream initiated by the peer (blocking mode) or
2307 * fail if we don't have one yet (non-blocking mode).
2309 if (!qc_wait_for_default_xso_for_read(&ctx)) {
2310 ret = 0; /* error already raised here */
2314 ctx.xso = ctx.qc->default_xso;
2317 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2318 ret = 0; /* quic_read_actual raised error here */
2322 if (*bytes_read > 0) {
2324 * Even though we succeeded, tick the reactor here to ensure we are
2325 * handling other aspects of the QUIC connection.
2327 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2329 } else if (xso_blocking_mode(ctx.xso)) {
2331 * We were not able to read anything immediately, so our stream
2332 * buffer is empty. This means we need to block until we get
2333 * at least one byte.
2336 args.stream = ctx.xso->stream;
2339 args.bytes_read = bytes_read;
2342 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2344 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2346 } else if (res < 0) {
2347 ret = 0; /* quic_read_again raised error here */
2354 * We did not get any bytes and are not in blocking mode.
2355 * Tick to see if this delivers any more.
2357 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2359 /* Try the read again. */
2360 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2361 ret = 0; /* quic_read_actual raised error here */
2365 if (*bytes_read > 0)
2366 ret = 1; /* Succeeded this time. */
2368 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2372 quic_unlock(ctx.qc);
2376 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2378 return quic_read(s, buf, len, bytes_read, 0);
2381 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2383 return quic_read(s, buf, len, bytes_read, 1);
2392 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2399 if (!expect_quic(s, &ctx))
2404 if (ctx.xso == NULL)
2407 if (ctx.xso->stream == NULL
2408 || !ossl_quic_stream_has_recv_buffer(ctx.xso->stream))
2409 /* Cannot raise errors here because we are const, just fail. */
2412 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
2415 if (avail == 0 && check_channel && ossl_quic_channel_has_pending(ctx.qc->ch))
2419 quic_unlock(ctx.qc);
2423 size_t ossl_quic_pending(const SSL *s)
2425 return ossl_quic_pending_int(s, /*check_channel=*/0);
2428 int ossl_quic_has_pending(const SSL *s)
2430 /* Do we have app-side pending data or pending URXEs or RXEs? */
2431 return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2435 * SSL_stream_conclude
2436 * -------------------
2439 int ossl_quic_conn_stream_conclude(SSL *s)
2445 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
2448 qs = ctx.xso->stream;
2450 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
2451 quic_unlock(ctx.qc);
2452 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2455 if (!quic_validate_for_write(ctx.xso, &err)) {
2456 quic_unlock(ctx.qc);
2457 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2460 if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2461 quic_unlock(ctx.qc);
2465 ossl_quic_sstream_fin(qs->sstream);
2466 quic_post_write(ctx.xso, 1, 1);
2467 quic_unlock(ctx.qc);
2472 * SSL_inject_net_dgram
2473 * --------------------
2476 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2478 const BIO_ADDR *peer,
2479 const BIO_ADDR *local)
2485 if (!expect_quic(s, &ctx))
2490 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2491 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2493 quic_unlock(ctx.qc);
2498 * SSL_get0_connection
2499 * -------------------
2501 SSL *ossl_quic_get0_connection(SSL *s)
2505 if (!expect_quic(s, &ctx))
2508 return &ctx.qc->ssl;
2512 * SSL_get_stream_type
2513 * -------------------
2515 int ossl_quic_get_stream_type(SSL *s)
2519 if (!expect_quic(s, &ctx))
2520 return SSL_STREAM_TYPE_BIDI;
2522 if (ctx.xso == NULL) {
2524 * If deferred XSO creation has yet to occur, proceed according to the
2525 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2526 * what kind of stream will be created yet, so return BIDI on the basis
2527 * that at this time, the client still has the option of calling
2528 * SSL_read() or SSL_write() first.
2530 if (ctx.qc->default_xso_created
2531 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2532 return SSL_STREAM_TYPE_NONE;
2534 return SSL_STREAM_TYPE_BIDI;
2537 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2538 return SSL_STREAM_TYPE_BIDI;
2540 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2541 return SSL_STREAM_TYPE_READ;
2543 return SSL_STREAM_TYPE_WRITE;
2551 uint64_t ossl_quic_get_stream_id(SSL *s)
2556 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2559 id = ctx.xso->stream->id;
2560 quic_unlock(ctx.qc);
2566 * SSL_set_default_stream_mode
2567 * ---------------------------
2570 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2574 if (!expect_quic_conn_only(s, &ctx))
2579 if (ctx.qc->default_xso_created) {
2580 quic_unlock(ctx.qc);
2581 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2582 "too late to change default stream mode");
2586 case SSL_DEFAULT_STREAM_MODE_NONE:
2587 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2588 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2589 ctx.qc->default_stream_mode = mode;
2592 quic_unlock(ctx.qc);
2593 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2594 "bad default stream type");
2597 quic_unlock(ctx.qc);
2606 SSL *ossl_quic_detach_stream(SSL *s)
2609 QUIC_XSO *xso = NULL;
2611 if (!expect_quic_conn_only(s, &ctx))
2616 /* Calling this function inhibits default XSO autocreation. */
2617 /* QC ref to any default XSO is transferred to us and to caller. */
2618 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
2620 quic_unlock(ctx.qc);
2622 return xso != NULL ? &xso->ssl : NULL;
2630 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2636 if (!expect_quic_conn_only(conn, &ctx))
2639 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2640 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
2641 "stream to attach must be a valid QUIC stream");
2643 xso = (QUIC_XSO *)stream;
2647 if (ctx.qc->default_xso != NULL) {
2648 quic_unlock(ctx.qc);
2649 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2650 "connection already has a default stream");
2654 * It is a caller error for the XSO being attached as a default XSO to have
2655 * more than one ref.
2657 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
2658 quic_unlock(ctx.qc);
2659 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
2664 quic_unlock(ctx.qc);
2665 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
2666 "stream being attached must have "
2667 "only 1 reference");
2670 /* Caller's reference to the XSO is transferred to us. */
2671 /* Calling this function inhibits default XSO autocreation. */
2672 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
2674 quic_unlock(ctx.qc);
2679 * SSL_set_incoming_stream_policy
2680 * ------------------------------
2683 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
2685 switch (qc->incoming_stream_policy) {
2686 case SSL_INCOMING_STREAM_POLICY_AUTO:
2687 if ((qc->default_xso == NULL && !qc->default_xso_created)
2688 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2689 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
2691 return SSL_INCOMING_STREAM_POLICY_REJECT;
2694 return qc->incoming_stream_policy;
2699 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2701 int policy = qc_get_effective_incoming_stream_policy(qc);
2702 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
2704 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2706 qc->incoming_stream_aec);
2710 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2716 if (!expect_quic_conn_only(s, &ctx))
2722 case SSL_INCOMING_STREAM_POLICY_AUTO:
2723 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2724 case SSL_INCOMING_STREAM_POLICY_REJECT:
2725 ctx.qc->incoming_stream_policy = policy;
2726 ctx.qc->incoming_stream_aec = aec;
2730 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
2735 qc_update_reject_policy(ctx.qc);
2736 quic_unlock(ctx.qc);
2744 struct wait_for_incoming_stream_args {
2750 static int wait_for_incoming_stream(void *arg)
2752 struct wait_for_incoming_stream_args *args = arg;
2753 QUIC_CONNECTION *qc = args->ctx->qc;
2754 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
2756 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2757 /* If connection is torn down due to an error while blocking, stop. */
2758 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2762 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2763 if (args->qs != NULL)
2764 return 1; /* got a stream */
2766 return 0; /* did not get a stream, keep trying */
2770 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2775 QUIC_STREAM_MAP *qsm;
2778 OSSL_RTT_INFO rtt_info;
2780 if (!expect_quic_conn_only(s, &ctx))
2785 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2786 == SSL_INCOMING_STREAM_POLICY_REJECT)
2789 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2791 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2793 if (qc_blocking_mode(ctx.qc)
2794 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2795 struct wait_for_incoming_stream_args args;
2800 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2802 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2804 } else if (ret < 0 || args.qs == NULL) {
2814 xso = create_xso_from_stream(ctx.qc, qs);
2818 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2819 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2820 rtt_info.smoothed_rtt);
2823 /* Calling this function inhibits default XSO autocreation. */
2824 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2827 quic_unlock(ctx.qc);
2832 * SSL_get_accept_stream_queue_len
2833 * -------------------------------
2836 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2841 if (!expect_quic_conn_only(s, &ctx))
2846 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2848 quic_unlock(ctx.qc);
2856 int ossl_quic_stream_reset(SSL *ssl,
2857 const SSL_STREAM_RESET_ARGS *args,
2861 QUIC_STREAM_MAP *qsm;
2863 uint64_t error_code;
2866 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2869 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2870 qs = ctx.xso->stream;
2871 error_code = (args != NULL ? args->quic_error_code : 0);
2873 if (!quic_validate_for_write(ctx.xso, &err)) {
2874 ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2878 ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2881 quic_unlock(ctx.qc);
2886 * SSL_get_stream_read_state
2887 * -------------------------
2889 static void quic_classify_stream(QUIC_CONNECTION *qc,
2893 uint64_t *app_error_code)
2896 uint64_t final_size;
2898 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2900 if (app_error_code != NULL)
2901 *app_error_code = UINT64_MAX;
2903 app_error_code = &final_size; /* throw away value */
2905 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2907 * Unidirectional stream and this direction of transmission doesn't
2910 *state = SSL_STREAM_STATE_WRONG_DIR;
2911 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2912 /* Connection already closed. */
2913 *state = SSL_STREAM_STATE_CONN_CLOSED;
2914 } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
2915 /* Application has read a FIN. */
2916 *state = SSL_STREAM_STATE_FINISHED;
2917 } else if ((!is_write && qs->stop_sending)
2918 || (is_write && ossl_quic_stream_send_is_reset(qs))) {
2920 * Stream has been reset locally. FIN takes precedence over this for the
2921 * read case as the application need not care if the stream is reset
2922 * after a FIN has been successfully processed.
2924 *state = SSL_STREAM_STATE_RESET_LOCAL;
2925 *app_error_code = !is_write
2926 ? qs->stop_sending_aec
2927 : qs->reset_stream_aec;
2928 } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
2929 || (is_write && qs->peer_stop_sending)) {
2931 * Stream has been reset remotely. */
2932 *state = SSL_STREAM_STATE_RESET_REMOTE;
2933 *app_error_code = !is_write
2934 ? qs->peer_reset_stream_aec
2935 : qs->peer_stop_sending_aec;
2936 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2939 * Stream has been finished. Stream reset takes precedence over this for
2940 * the write case as peer may not have received all data.
2942 *state = SSL_STREAM_STATE_FINISHED;
2944 /* Stream still healthy. */
2945 *state = SSL_STREAM_STATE_OK;
2949 static int quic_get_stream_state(SSL *ssl, int is_write)
2954 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2955 return SSL_STREAM_STATE_NONE;
2957 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2958 quic_unlock(ctx.qc);
2962 int ossl_quic_get_stream_read_state(SSL *ssl)
2964 return quic_get_stream_state(ssl, /*is_write=*/0);
2968 * SSL_get_stream_write_state
2969 * --------------------------
2971 int ossl_quic_get_stream_write_state(SSL *ssl)
2973 return quic_get_stream_state(ssl, /*is_write=*/1);
2977 * SSL_get_stream_read_error_code
2978 * ------------------------------
2980 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2981 uint64_t *app_error_code)
2986 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2989 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2990 &state, app_error_code);
2992 quic_unlock(ctx.qc);
2994 case SSL_STREAM_STATE_FINISHED:
2996 case SSL_STREAM_STATE_RESET_LOCAL:
2997 case SSL_STREAM_STATE_RESET_REMOTE:
3004 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
3006 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
3010 * SSL_get_stream_write_error_code
3011 * -------------------------------
3013 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
3015 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
3019 * Write buffer size mutation
3020 * --------------------------
3022 int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
3027 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
3030 if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
3031 /* Called on a unidirectional receive-only stream - error. */
3032 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3036 if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
3038 * If the stream has a send part but we have disposed of it because we
3039 * no longer need it, this is a no-op.
3045 if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
3046 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
3053 quic_unlock(ctx.qc);
3058 * SSL_get_conn_close_info
3059 * -----------------------
3061 int ossl_quic_get_conn_close_info(SSL *ssl,
3062 SSL_CONN_CLOSE_INFO *info,
3066 const QUIC_TERMINATE_CAUSE *tc;
3068 if (!expect_quic_conn_only(ssl, &ctx))
3071 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
3075 info->error_code = tc->error_code;
3076 info->reason = tc->reason;
3077 info->reason_len = tc->reason_len;
3078 info->is_local = !tc->remote;
3079 info->is_transport = !tc->app;
3087 int ossl_quic_key_update(SSL *ssl, int update_type)
3091 if (!expect_quic_conn_only(ssl, &ctx))
3094 switch (update_type) {
3095 case SSL_KEY_UPDATE_NOT_REQUESTED:
3097 * QUIC signals peer key update implicily by triggering a local
3098 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3100 case SSL_KEY_UPDATE_REQUESTED:
3104 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
3110 /* Attempt to perform a TXKU. */
3111 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
3112 quic_unlock(ctx.qc);
3116 quic_unlock(ctx.qc);
3121 * SSL_get_key_update_type
3122 * -----------------------
3124 int ossl_quic_get_key_update_type(const SSL *s)
3127 * We always handle key updates immediately so a key update is never
3130 return SSL_KEY_UPDATE_NONE;
3134 * QUIC Front-End I/O API: SSL_CTX Management
3135 * ==========================================
3138 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3142 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
3146 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3150 if (!expect_quic_conn_only(s, &ctx))
3154 case SSL_CTRL_SET_MSG_CALLBACK:
3155 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
3157 /* This callback also needs to be set on the internal SSL object */
3158 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
3161 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3162 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
3166 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3168 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
3171 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
3173 /* We never do renegotiation. */
3178 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3179 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3182 int ossl_quic_num_ciphers(void)
3187 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
3193 * Internal Testing APIs
3194 * =====================
3197 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
3201 if (!expect_quic_conn_only(s, &ctx))