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
);
38 static int qc_blocking_mode(const QUIC_CONNECTION
*qc
);
39 static int xso_blocking_mode(const QUIC_XSO
*xso
);
42 * QUIC Front-End I/O API: Common Utilities
43 * ========================================
47 * Block until a predicate is met.
49 * Precondition: Must have a channel.
50 * Precondition: Must hold channel lock (unchecked).
53 static int block_until_pred(QUIC_CONNECTION
*qc
,
54 int (*pred
)(void *arg
), void *pred_arg
,
59 assert(qc
->ch
!= NULL
);
62 * Any attempt to block auto-disables tick inhibition as otherwise we will
63 * hang around forever.
65 ossl_quic_channel_set_inhibit_tick(qc
->ch
, 0);
67 rtor
= ossl_quic_channel_get_reactor(qc
->ch
);
68 return ossl_quic_reactor_block_until_pred(rtor
, pred
, pred_arg
, flags
,
72 static OSSL_TIME
get_time(QUIC_CONNECTION
*qc
)
74 if (qc
->override_now_cb
!= NULL
)
75 return qc
->override_now_cb(qc
->override_now_cb_arg
);
77 return ossl_time_now();
80 static OSSL_TIME
get_time_cb(void *arg
)
82 QUIC_CONNECTION
*qc
= arg
;
88 * QCTX is a utility structure which provides information we commonly wish to
89 * unwrap upon an API call being dispatched to us, namely:
91 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
93 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
94 * a QCSO with a default stream was passed);
95 * - whether a QSSO was passed (xso == NULL must not be used to determine this
96 * because it may be non-NULL when a QCSO is passed if that QCSO has a
98 * - whether we are in "I/O context", meaning that non-normal errors can
99 * be reported via SSL_get_error() as well as via ERR. Functions such as
100 * SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
101 * functions which are allowed to change the value returned by
102 * SSL_get_error. However, other functions (including functions which call
103 * SSL_do_handshake() implicitly) are not allowed to change the return value
109 int is_stream
, in_io
;
113 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
114 * rather than via ERR. Note that normal errors must always be raised while
118 static int quic_raise_normal_error(QCTX
*ctx
,
122 ctx
->xso
->last_error
= err
;
124 ctx
->qc
->last_error
= err
;
130 * Raise a 'non-normal' error, meaning any error that is not reported via
131 * SSL_get_error() and must be reported via ERR.
133 * qc should be provided if available. In exceptional circumstances when qc is
134 * not known NULL may be passed. This should generally only happen when an
135 * expect_...() function defined below fails, which generally indicates a
136 * dispatch error or caller error.
138 * ctx should be NULL if the connection lock is not held.
140 static int quic_raise_non_normal_error(QCTX
*ctx
,
151 if (ctx
->in_io
&& ctx
->is_stream
&& ctx
->xso
!= NULL
)
152 ctx
->xso
->last_error
= SSL_ERROR_SSL
;
153 else if (ctx
->in_io
&& !ctx
->is_stream
&& ctx
->qc
!= NULL
)
154 ctx
->qc
->last_error
= SSL_ERROR_SSL
;
156 if (reason
== SSL_R_PROTOCOL_IS_SHUTDOWN
&& ctx
->qc
!= NULL
)
157 ossl_quic_channel_restore_err_state(ctx
->qc
->ch
);
161 ERR_set_debug(file
, line
, func
);
164 ERR_vset_error(ERR_LIB_SSL
, reason
, fmt
, args
);
170 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
171 quic_raise_normal_error((ctx), (err))
173 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
174 quic_raise_non_normal_error((ctx), \
175 OPENSSL_FILE, OPENSSL_LINE, \
181 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
182 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
184 * After this returns 1, all fields of the passed QCTX are initialised.
185 * Returns 0 on failure. This function is intended to be used to provide API
186 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
188 static int expect_quic(const SSL
*s
, QCTX
*ctx
)
198 return QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_PASSED_NULL_PARAMETER
, NULL
);
201 case SSL_TYPE_QUIC_CONNECTION
:
202 qc
= (QUIC_CONNECTION
*)s
;
204 ctx
->xso
= qc
->default_xso
;
209 case SSL_TYPE_QUIC_XSO
:
218 return QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
223 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
224 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
227 * remote_init determines if we expect the default XSO to be remotely created or
228 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
230 * Channel mutex is acquired and retained on success.
233 static int ossl_unused
expect_quic_with_stream_lock(const SSL
*s
, int remote_init
,
234 int in_io
, QCTX
*ctx
)
236 if (!expect_quic(s
, ctx
))
242 if (ctx
->xso
== NULL
&& remote_init
>= 0) {
243 if (!quic_mutation_allowed(ctx
->qc
, /*req_active=*/0)) {
244 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
248 /* If we haven't finished the handshake, try to advance it. */
249 if (quic_do_handshake(ctx
) < 1)
250 /* ossl_quic_do_handshake raised error here */
253 if (remote_init
== 0) {
254 if (!qc_try_create_default_xso_for_write(ctx
))
257 if (!qc_wait_for_default_xso_for_read(ctx
))
261 ctx
->xso
= ctx
->qc
->default_xso
;
264 if (ctx
->xso
== NULL
) {
265 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
269 return 1; /* coverity[missing_unlock]: lock held */
272 quic_unlock(ctx
->qc
);
277 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
278 * be non-NULL if the QCSO has a default stream.
280 static int ossl_unused
expect_quic_conn_only(const SSL
*s
, int in_io
, QCTX
*ctx
)
282 if (!expect_quic(s
, ctx
))
287 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_CONN_USE_ONLY
, NULL
);
293 * Ensures that the channel mutex is held for a method which touches channel
296 * Precondition: Channel mutex is not held (unchecked)
298 static void quic_lock(QUIC_CONNECTION
*qc
)
300 #if defined(OPENSSL_THREADS)
301 ossl_crypto_mutex_lock(qc
->mutex
);
305 /* Precondition: Channel mutex is held (unchecked) */
307 static void quic_unlock(QUIC_CONNECTION
*qc
)
309 #if defined(OPENSSL_THREADS)
310 ossl_crypto_mutex_unlock(qc
->mutex
);
315 * This predicate is the criterion which should determine API call rejection for
316 * *most* mutating API calls, particularly stream-related operations for send
319 * A call is rejected (this function returns 0) if shutdown is in progress
320 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
321 * req_active=1, the connection must be active (i.e., the IDLE state is also
324 static int quic_mutation_allowed(QUIC_CONNECTION
*qc
, int req_active
)
326 if (qc
->shutting_down
|| ossl_quic_channel_is_term_any(qc
->ch
))
329 if (req_active
&& !ossl_quic_channel_is_active(qc
->ch
))
336 * QUIC Front-End I/O API: Initialization
337 * ======================================
339 * SSL_new => ossl_quic_new
341 * SSL_reset => ossl_quic_reset
342 * SSL_clear => ossl_quic_clear
344 * SSL_free => ossl_quic_free
346 * SSL_set_options => ossl_quic_set_options
347 * SSL_get_options => ossl_quic_get_options
348 * SSL_clear_options => ossl_quic_clear_options
353 SSL
*ossl_quic_new(SSL_CTX
*ctx
)
355 QUIC_CONNECTION
*qc
= NULL
;
356 SSL
*ssl_base
= NULL
;
357 SSL_CONNECTION
*sc
= NULL
;
359 qc
= OPENSSL_zalloc(sizeof(*qc
));
361 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_CRYPTO_LIB
, NULL
);
365 /* Initialise the QUIC_CONNECTION's stub header. */
367 if (!ossl_ssl_init(ssl_base
, ctx
, ctx
->method
, SSL_TYPE_QUIC_CONNECTION
)) {
369 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
373 qc
->tls
= ossl_ssl_connection_new_int(ctx
, TLS_method());
374 if (qc
->tls
== NULL
|| (sc
= SSL_CONNECTION_FROM_SSL(qc
->tls
)) == NULL
) {
375 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
379 /* override the user_ssl of the inner connection */
380 sc
->s3
.flags
|= TLS1_FLAGS_QUIC
;
382 /* Restrict options derived from the SSL_CTX. */
383 sc
->options
&= OSSL_QUIC_PERMITTED_OPTIONS_CONN
;
386 #if defined(OPENSSL_THREADS)
387 if ((qc
->mutex
= ossl_crypto_mutex_new()) == NULL
) {
388 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_CRYPTO_LIB
, NULL
);
393 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
394 qc
->is_thread_assisted
395 = (ssl_base
->method
== OSSL_QUIC_client_thread_method());
398 qc
->as_server
= 0; /* TODO(QUIC SERVER): add server support */
399 qc
->as_server_state
= qc
->as_server
;
401 qc
->default_stream_mode
= SSL_DEFAULT_STREAM_MODE_AUTO_BIDI
;
402 qc
->default_ssl_mode
= qc
->ssl
.ctx
->mode
;
403 qc
->default_ssl_options
= qc
->ssl
.ctx
->options
& OSSL_QUIC_PERMITTED_OPTIONS
;
404 qc
->desires_blocking
= 1;
406 qc
->incoming_stream_policy
= SSL_INCOMING_STREAM_POLICY_AUTO
;
407 qc
->last_error
= SSL_ERROR_NONE
;
409 if (!create_channel(qc
))
412 ossl_quic_channel_set_msg_callback(qc
->ch
, ctx
->msg_callback
, ssl_base
);
413 ossl_quic_channel_set_msg_callback_arg(qc
->ch
, ctx
->msg_callback_arg
);
415 qc_update_reject_policy(qc
);
418 * We do not create the default XSO yet. The reason for this is that the
419 * stream ID of the default XSO will depend on whether the stream is client
420 * or server-initiated, which depends on who transmits first. Since we do
421 * not know whether the application will be using a client-transmits-first
422 * or server-transmits-first protocol, we defer default XSO creation until
423 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
424 * we take that as a cue that the client is expecting a server-initiated
425 * stream, and vice versa if SSL_write() is called first.
431 #if defined(OPENSSL_THREADS)
432 ossl_crypto_mutex_free(qc
->mutex
);
434 ossl_quic_channel_free(qc
->ch
);
443 void ossl_quic_free(SSL
*s
)
448 /* We should never be called on anything but a QSO. */
449 if (!expect_quic(s
, &ctx
))
456 * When a QSSO is freed, the XSO is freed immediately, because the XSO
457 * itself only contains API personality layer data. However the
458 * underlying QUIC_STREAM is not freed immediately but is instead marked
459 * as deleted for later collection.
462 assert(ctx
.qc
->num_xso
> 0);
465 /* If a stream's send part has not been finished, auto-reset it. */
466 if (( ctx
.xso
->stream
->send_state
== QUIC_SSTREAM_STATE_READY
467 || ctx
.xso
->stream
->send_state
== QUIC_SSTREAM_STATE_SEND
)
468 && !ossl_quic_sstream_get_final_size(ctx
.xso
->stream
->sstream
, NULL
))
469 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
472 /* Do STOP_SENDING for the receive part, if applicable. */
473 if ( ctx
.xso
->stream
->recv_state
== QUIC_RSTREAM_STATE_RECV
474 || ctx
.xso
->stream
->recv_state
== QUIC_RSTREAM_STATE_SIZE_KNOWN
)
475 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
478 /* Update stream state. */
479 ctx
.xso
->stream
->deleted
= 1;
480 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
483 is_default
= (ctx
.xso
== ctx
.qc
->default_xso
);
487 * Unref the connection in most cases; the XSO has a ref to the QC and
488 * not vice versa. But for a default XSO, to avoid circular references,
489 * the QC refs the XSO but the XSO does not ref the QC. If we are the
490 * default XSO, we only get here when the QC is being torn down anyway,
491 * so don't call SSL_free(qc) as we are already in it.
494 SSL_free(&ctx
.qc
->ssl
);
496 /* Note: SSL_free calls OPENSSL_free(xso) for us */
501 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
502 * stage, but is freed during the channel free when the whole QSM is freed.
504 if (ctx
.qc
->default_xso
!= NULL
) {
505 QUIC_XSO
*xso
= ctx
.qc
->default_xso
;
510 ctx
.qc
->default_xso
= NULL
;
513 /* Ensure we have no remaining XSOs. */
514 assert(ctx
.qc
->num_xso
== 0);
516 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
517 if (ctx
.qc
->is_thread_assisted
&& ctx
.qc
->started
) {
518 ossl_quic_thread_assist_wait_stopped(&ctx
.qc
->thread_assist
);
519 ossl_quic_thread_assist_cleanup(&ctx
.qc
->thread_assist
);
523 ossl_quic_channel_free(ctx
.qc
->ch
);
525 BIO_free(ctx
.qc
->net_rbio
);
526 BIO_free(ctx
.qc
->net_wbio
);
528 /* Note: SSL_free calls OPENSSL_free(qc) for us */
530 SSL_free(ctx
.qc
->tls
);
531 quic_unlock(ctx
.qc
); /* tsan doesn't like freeing locked mutexes */
532 #if defined(OPENSSL_THREADS)
533 ossl_crypto_mutex_free(&ctx
.qc
->mutex
);
537 /* SSL method init */
538 int ossl_quic_init(SSL
*s
)
540 /* Same op as SSL_clear, forward the call. */
541 return ossl_quic_clear(s
);
544 /* SSL method deinit */
545 void ossl_quic_deinit(SSL
*s
)
550 /* SSL_clear (ssl_reset method) */
551 int ossl_quic_reset(SSL
*s
)
555 if (!expect_quic(s
, &ctx
))
558 ERR_raise(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
);
562 /* ssl_clear method (unused) */
563 int ossl_quic_clear(SSL
*s
)
567 if (!expect_quic(s
, &ctx
))
570 /* TODO(QUIC FUTURE): Currently a no-op. */
574 int ossl_quic_conn_set_override_now_cb(SSL
*s
,
575 OSSL_TIME (*now_cb
)(void *arg
),
580 if (!expect_quic(s
, &ctx
))
585 ctx
.qc
->override_now_cb
= now_cb
;
586 ctx
.qc
->override_now_cb_arg
= now_cb_arg
;
592 void ossl_quic_conn_force_assist_thread_wake(SSL
*s
)
596 if (!expect_quic(s
, &ctx
))
599 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
600 if (ctx
.qc
->is_thread_assisted
&& ctx
.qc
->started
)
601 ossl_quic_thread_assist_notify_deadline_changed(&ctx
.qc
->thread_assist
);
606 static void qc_touch_default_xso(QUIC_CONNECTION
*qc
)
608 qc
->default_xso_created
= 1;
609 qc_update_reject_policy(qc
);
613 * Changes default XSO. Allows caller to keep reference to the old default XSO
614 * (if any). Reference to new XSO is transferred from caller.
617 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
,
625 if (qc
->default_xso
!= xso
) {
626 *old_xso
= qc
->default_xso
; /* transfer old XSO ref to caller */
628 qc
->default_xso
= xso
;
632 * Changing to not having a default XSO. XSO becomes standalone and
633 * now has a ref to the QC.
635 if (!ossl_assert(SSL_up_ref(&qc
->ssl
)))
639 * Changing from not having a default XSO to having one. The new XSO
640 * will have had a reference to the QC we need to drop to avoid a
641 * circular reference.
643 * Currently we never change directly from one default XSO to
644 * another, though this function would also still be correct if this
647 assert(*old_xso
== NULL
);
649 CRYPTO_DOWN_REF(&qc
->ssl
.references
, &refs
);
655 qc_touch_default_xso(qc
);
659 * Changes default XSO, releasing the reference to any previous default XSO.
660 * Reference to new XSO is transferred from caller.
663 static void qc_set_default_xso(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
, int touch
)
665 QUIC_XSO
*old_xso
= NULL
;
667 qc_set_default_xso_keep_ref(qc
, xso
, touch
, &old_xso
);
670 SSL_free(&old_xso
->ssl
);
674 static void xso_update_options(QUIC_XSO
*xso
)
676 int cleanse
= ((xso
->ssl_options
& SSL_OP_CLEANSE_PLAINTEXT
) != 0);
678 if (xso
->stream
->rstream
!= NULL
)
679 ossl_quic_rstream_set_cleanse(xso
->stream
->rstream
, cleanse
);
681 if (xso
->stream
->sstream
!= NULL
)
682 ossl_quic_sstream_set_cleanse(xso
->stream
->sstream
, cleanse
);
689 * Setting options on a QCSO
690 * - configures the handshake-layer options;
691 * - configures the default data-plane options for new streams;
692 * - configures the data-plane options on the default XSO, if there is one.
694 * Setting options on a QSSO
695 * - configures data-plane options for that stream only.
698 static uint64_t quic_mask_or_options(SSL
*ssl
, uint64_t mask_value
, uint64_t or_value
)
701 uint64_t hs_mask_value
, hs_or_value
, ret
;
703 if (!expect_quic(ssl
, &ctx
))
708 if (!ctx
.is_stream
) {
710 * If we were called on the connection, we apply any handshake option
713 hs_mask_value
= (mask_value
& OSSL_QUIC_PERMITTED_OPTIONS_CONN
);
714 hs_or_value
= (or_value
& OSSL_QUIC_PERMITTED_OPTIONS_CONN
);
716 SSL_clear_options(ctx
.qc
->tls
, hs_mask_value
);
717 SSL_set_options(ctx
.qc
->tls
, hs_or_value
);
719 /* Update defaults for new streams. */
720 ctx
.qc
->default_ssl_options
721 = ((ctx
.qc
->default_ssl_options
& ~mask_value
) | or_value
)
722 & OSSL_QUIC_PERMITTED_OPTIONS
;
725 if (ctx
.xso
!= NULL
) {
727 = ((ctx
.xso
->ssl_options
& ~mask_value
) | or_value
)
728 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM
;
730 xso_update_options(ctx
.xso
);
733 ret
= ctx
.is_stream
? ctx
.xso
->ssl_options
: ctx
.qc
->default_ssl_options
;
739 uint64_t ossl_quic_set_options(SSL
*ssl
, uint64_t options
)
741 return quic_mask_or_options(ssl
, 0, options
);
744 /* SSL_clear_options */
745 uint64_t ossl_quic_clear_options(SSL
*ssl
, uint64_t options
)
747 return quic_mask_or_options(ssl
, options
, 0);
750 /* SSL_get_options */
751 uint64_t ossl_quic_get_options(const SSL
*ssl
)
753 return quic_mask_or_options((SSL
*)ssl
, 0, 0);
757 * QUIC Front-End I/O API: Network BIO Configuration
758 * =================================================
760 * Handling the different BIOs is difficult:
762 * - It is more or less a requirement that we use non-blocking network I/O;
763 * we need to be able to have timeouts on recv() calls, and make best effort
764 * (non blocking) send() and recv() calls.
766 * The only sensible way to do this is to configure the socket into
767 * non-blocking mode. We could try to do select() before calling send() or
768 * recv() to get a guarantee that the call will not block, but this will
769 * probably run into issues with buggy OSes which generate spurious socket
770 * readiness events. In any case, relying on this to work reliably does not
773 * Timeouts could be handled via setsockopt() socket timeout options, but
774 * this depends on OS support and adds another syscall to every network I/O
775 * operation. It also has obvious thread safety concerns if we want to move
776 * to concurrent use of a single socket at some later date.
778 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
779 * be made non-blocking. However some OSes (e.g. Windows) do not support
780 * this, so we cannot rely on this.
782 * As such, we need to configure any FD in non-blocking mode. This may
783 * confound users who pass a blocking socket to libssl. However, in practice
784 * it would be extremely strange for a user of QUIC to pass an FD to us,
785 * then also try and send receive traffic on the same socket(!). Thus the
786 * impact of this should be limited, and can be documented.
788 * - We support both blocking and non-blocking operation in terms of the API
789 * presented to the user. One prospect is to set the blocking mode based on
790 * whether the socket passed to us was already in blocking mode. However,
791 * Windows has no API for determining if a socket is in blocking mode (!),
792 * therefore this cannot be done portably. Currently therefore we expose an
793 * explicit API call to set this, and default to blocking mode.
795 * - We need to determine our initial destination UDP address. The "natural"
796 * way for a user to do this is to set the peer variable on a BIO_dgram.
797 * However, this has problems because BIO_dgram's peer variable is used for
798 * both transmission and reception. This means it can be constantly being
799 * changed to a malicious value (e.g. if some random unrelated entity on the
800 * network starts sending traffic to us) on every read call. This is not a
801 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
802 * calls only, which do not use this variable. However, we do need to let
803 * the user specify the peer in a 'normal' manner. The compromise here is
804 * that we grab the current peer value set at the time the write BIO is set
805 * and do not read the value again.
807 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
808 * Currently we do this by only supporting non-blocking mode.
813 * Determines what initial destination UDP address we should use, if possible.
814 * If this fails the client must set the destination address manually, or use a
815 * BIO which does not need a destination address.
817 static int csm_analyse_init_peer_addr(BIO
*net_wbio
, BIO_ADDR
*peer
)
819 if (BIO_dgram_get_peer(net_wbio
, peer
) <= 0)
825 static int qc_can_support_blocking_cached(QUIC_CONNECTION
*qc
)
827 QUIC_REACTOR
*rtor
= ossl_quic_channel_get_reactor(qc
->ch
);
829 return ossl_quic_reactor_can_poll_r(rtor
)
830 && ossl_quic_reactor_can_poll_w(rtor
);
833 static void qc_update_can_support_blocking(QUIC_CONNECTION
*qc
)
835 ossl_quic_channel_update_poll_descriptors(qc
->ch
); /* best effort */
838 static void qc_update_blocking_mode(QUIC_CONNECTION
*qc
)
840 qc
->blocking
= qc
->desires_blocking
&& qc_can_support_blocking_cached(qc
);
843 void ossl_quic_conn_set0_net_rbio(SSL
*s
, BIO
*net_rbio
)
847 if (!expect_quic(s
, &ctx
))
850 if (ctx
.qc
->net_rbio
== net_rbio
)
853 if (!ossl_quic_channel_set_net_rbio(ctx
.qc
->ch
, net_rbio
))
856 BIO_free(ctx
.qc
->net_rbio
);
857 ctx
.qc
->net_rbio
= net_rbio
;
859 if (net_rbio
!= NULL
)
860 BIO_set_nbio(net_rbio
, 1); /* best effort autoconfig */
863 * Determine if the current pair of read/write BIOs now set allows blocking
864 * mode to be supported.
866 qc_update_can_support_blocking(ctx
.qc
);
867 qc_update_blocking_mode(ctx
.qc
);
870 void ossl_quic_conn_set0_net_wbio(SSL
*s
, BIO
*net_wbio
)
874 if (!expect_quic(s
, &ctx
))
877 if (ctx
.qc
->net_wbio
== net_wbio
)
880 if (!ossl_quic_channel_set_net_wbio(ctx
.qc
->ch
, net_wbio
))
883 BIO_free(ctx
.qc
->net_wbio
);
884 ctx
.qc
->net_wbio
= net_wbio
;
886 if (net_wbio
!= NULL
)
887 BIO_set_nbio(net_wbio
, 1); /* best effort autoconfig */
890 * Determine if the current pair of read/write BIOs now set allows blocking
891 * mode to be supported.
893 qc_update_can_support_blocking(ctx
.qc
);
894 qc_update_blocking_mode(ctx
.qc
);
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 xso_blocking_mode(ctx
.xso
);
927 return qc_blocking_mode(ctx
.qc
);
931 int ossl_quic_conn_set_blocking_mode(SSL
*s
, int blocking
)
936 if (!expect_quic(s
, &ctx
))
941 /* Sanity check - can we support the request given the current network BIO? */
944 * If called directly on a QCSO, update our information on network BIO
948 qc_update_can_support_blocking(ctx
.qc
);
950 /* Cannot enable blocking mode if we do not have pollable FDs. */
951 if (!qc_can_support_blocking_cached(ctx
.qc
)) {
952 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_UNSUPPORTED
, NULL
);
959 * If called directly on a QCSO, update default and connection-level
962 ctx
.qc
->desires_blocking
= (blocking
!= 0);
964 if (ctx
.xso
!= NULL
) {
966 * If called on a QSSO or a QCSO with a default XSO, update the blocking
969 ctx
.xso
->desires_blocking
= (blocking
!= 0);
970 ctx
.xso
->desires_blocking_set
= 1;
975 qc_update_blocking_mode(ctx
.qc
);
980 int ossl_quic_conn_set_initial_peer_addr(SSL
*s
,
981 const BIO_ADDR
*peer_addr
)
985 if (!expect_quic(s
, &ctx
))
989 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
992 if (peer_addr
== NULL
) {
993 BIO_ADDR_clear(&ctx
.qc
->init_peer_addr
);
997 ctx
.qc
->init_peer_addr
= *peer_addr
;
1002 * QUIC Front-End I/O API: Asynchronous I/O Management
1003 * ===================================================
1005 * (BIO/)SSL_handle_events => ossl_quic_handle_events
1006 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
1007 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
1011 /* Returns 1 if the connection is being used in blocking mode. */
1012 static int qc_blocking_mode(const QUIC_CONNECTION
*qc
)
1014 return qc
->blocking
;
1017 static int xso_blocking_mode(const QUIC_XSO
*xso
)
1019 if (xso
->desires_blocking_set
)
1020 return xso
->desires_blocking
&& qc_can_support_blocking_cached(xso
->conn
);
1022 /* Only ever set if we can support blocking. */
1023 return xso
->conn
->blocking
;
1026 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
1028 int ossl_quic_handle_events(SSL
*s
)
1032 if (!expect_quic(s
, &ctx
))
1036 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1037 quic_unlock(ctx
.qc
);
1042 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1043 * should next have events handled by the application by calling
1044 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1045 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1046 * the value of *tv is undefined.
1049 int ossl_quic_get_event_timeout(SSL
*s
, struct timeval
*tv
, int *is_infinite
)
1052 OSSL_TIME deadline
= ossl_time_infinite();
1054 if (!expect_quic(s
, &ctx
))
1060 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1062 if (ossl_time_is_infinite(deadline
)) {
1066 * Robustness against faulty applications that don't check *is_infinite;
1067 * harmless long timeout.
1069 tv
->tv_sec
= 1000000;
1072 quic_unlock(ctx
.qc
);
1076 *tv
= ossl_time_to_timeval(ossl_time_subtract(deadline
, get_time(ctx
.qc
)));
1078 quic_unlock(ctx
.qc
);
1082 /* SSL_get_rpoll_descriptor */
1083 int ossl_quic_get_rpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
1087 if (!expect_quic(s
, &ctx
))
1090 if (desc
== NULL
|| ctx
.qc
->net_rbio
== NULL
)
1091 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
1094 return BIO_get_rpoll_descriptor(ctx
.qc
->net_rbio
, desc
);
1097 /* SSL_get_wpoll_descriptor */
1098 int ossl_quic_get_wpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
1102 if (!expect_quic(s
, &ctx
))
1105 if (desc
== NULL
|| ctx
.qc
->net_wbio
== NULL
)
1106 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
1109 return BIO_get_wpoll_descriptor(ctx
.qc
->net_wbio
, desc
);
1112 /* SSL_net_read_desired */
1114 int ossl_quic_get_net_read_desired(SSL
*s
)
1119 if (!expect_quic(s
, &ctx
))
1123 ret
= ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1124 quic_unlock(ctx
.qc
);
1128 /* SSL_net_write_desired */
1130 int ossl_quic_get_net_write_desired(SSL
*s
)
1135 if (!expect_quic(s
, &ctx
))
1139 ret
= ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1140 quic_unlock(ctx
.qc
);
1145 * QUIC Front-End I/O API: Connection Lifecycle Operations
1146 * =======================================================
1148 * SSL_do_handshake => ossl_quic_do_handshake
1149 * SSL_set_connect_state => ossl_quic_set_connect_state
1150 * SSL_set_accept_state => ossl_quic_set_accept_state
1151 * SSL_shutdown => ossl_quic_shutdown
1152 * SSL_ctrl => ossl_quic_ctrl
1153 * (BIO/)SSL_connect => ossl_quic_connect
1154 * (BIO/)SSL_accept => ossl_quic_accept
1159 static void qc_shutdown_flush_init(QUIC_CONNECTION
*qc
)
1161 QUIC_STREAM_MAP
*qsm
;
1163 if (qc
->shutting_down
)
1166 qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
1168 ossl_quic_stream_map_begin_shutdown_flush(qsm
);
1169 qc
->shutting_down
= 1;
1172 /* Returns 1 if all shutdown-flush streams have been done with. */
1174 static int qc_shutdown_flush_finished(QUIC_CONNECTION
*qc
)
1176 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
1178 return qc
->shutting_down
1179 && ossl_quic_stream_map_is_shutdown_flush_finished(qsm
);
1183 static int quic_shutdown_wait(void *arg
)
1185 QUIC_CONNECTION
*qc
= arg
;
1187 return ossl_quic_channel_is_terminated(qc
->ch
);
1190 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
1191 static int quic_shutdown_flush_wait(void *arg
)
1193 QUIC_CONNECTION
*qc
= arg
;
1195 return ossl_quic_channel_is_term_any(qc
->ch
)
1196 || qc_shutdown_flush_finished(qc
);
1199 static int quic_shutdown_peer_wait(void *arg
)
1201 QUIC_CONNECTION
*qc
= arg
;
1202 return ossl_quic_channel_is_term_any(qc
->ch
);
1206 int ossl_quic_conn_shutdown(SSL
*s
, uint64_t flags
,
1207 const SSL_SHUTDOWN_EX_ARGS
*args
,
1212 int stream_flush
= ((flags
& SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH
) == 0);
1213 int no_block
= ((flags
& SSL_SHUTDOWN_FLAG_NO_BLOCK
) != 0);
1214 int wait_peer
= ((flags
& SSL_SHUTDOWN_FLAG_WAIT_PEER
) != 0);
1216 if (!expect_quic(s
, &ctx
))
1219 if (ctx
.is_stream
) {
1220 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_CONN_USE_ONLY
, NULL
);
1226 if (ossl_quic_channel_is_terminated(ctx
.qc
->ch
)) {
1227 quic_unlock(ctx
.qc
);
1231 /* Phase 1: Stream Flushing */
1232 if (!wait_peer
&& stream_flush
) {
1233 qc_shutdown_flush_init(ctx
.qc
);
1235 if (!qc_shutdown_flush_finished(ctx
.qc
)) {
1236 if (!no_block
&& qc_blocking_mode(ctx
.qc
)) {
1237 ret
= block_until_pred(ctx
.qc
, quic_shutdown_flush_wait
, ctx
.qc
, 0);
1243 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1247 if (!qc_shutdown_flush_finished(ctx
.qc
)) {
1248 quic_unlock(ctx
.qc
);
1249 return 0; /* ongoing */
1253 /* Phase 2: Connection Closure */
1254 if (wait_peer
&& !ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
1255 if (!no_block
&& qc_blocking_mode(ctx
.qc
)) {
1256 ret
= block_until_pred(ctx
.qc
, quic_shutdown_peer_wait
, ctx
.qc
, 0);
1262 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1265 if (!ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
1266 ret
= 0; /* peer hasn't closed yet - still not done */
1271 * We are at least terminating - go through the normal process of
1272 * waiting until we are in the TERMINATED state.
1276 /* Block mutation ops regardless of if we did stream flush. */
1277 ctx
.qc
->shutting_down
= 1;
1280 * This call is a no-op if we are already terminating, so it doesn't
1281 * affect the wait_peer case.
1283 ossl_quic_channel_local_close(ctx
.qc
->ch
,
1284 args
!= NULL
? args
->quic_error_code
: 0,
1285 args
!= NULL
? args
->quic_reason
: NULL
);
1287 SSL_set_shutdown(ctx
.qc
->tls
, SSL_SENT_SHUTDOWN
);
1289 if (ossl_quic_channel_is_terminated(ctx
.qc
->ch
)) {
1290 quic_unlock(ctx
.qc
);
1294 /* Phase 3: Terminating Wait Time */
1295 if (!no_block
&& qc_blocking_mode(ctx
.qc
)
1296 && (flags
& SSL_SHUTDOWN_FLAG_RAPID
) == 0) {
1297 ret
= block_until_pred(ctx
.qc
, quic_shutdown_wait
, ctx
.qc
, 0);
1303 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1306 ret
= ossl_quic_channel_is_terminated(ctx
.qc
->ch
);
1308 quic_unlock(ctx
.qc
);
1313 long ossl_quic_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
1317 if (!expect_quic(s
, &ctx
))
1322 /* If called on a QCSO, update the default mode. */
1324 ctx
.qc
->default_ssl_mode
|= (uint32_t)larg
;
1327 * If we were called on a QSSO or have a default stream, we also update
1330 if (ctx
.xso
!= NULL
) {
1331 /* Cannot enable EPW while AON write in progress. */
1332 if (ctx
.xso
->aon_write_in_progress
)
1333 larg
&= ~SSL_MODE_ENABLE_PARTIAL_WRITE
;
1335 ctx
.xso
->ssl_mode
|= (uint32_t)larg
;
1336 return ctx
.xso
->ssl_mode
;
1339 return ctx
.qc
->default_ssl_mode
;
1340 case SSL_CTRL_CLEAR_MODE
:
1342 ctx
.qc
->default_ssl_mode
&= ~(uint32_t)larg
;
1344 if (ctx
.xso
!= NULL
) {
1345 ctx
.xso
->ssl_mode
&= ~(uint32_t)larg
;
1346 return ctx
.xso
->ssl_mode
;
1349 return ctx
.qc
->default_ssl_mode
;
1351 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1352 ossl_quic_channel_set_msg_callback_arg(ctx
.qc
->ch
, parg
);
1353 /* This ctrl also needs to be passed to the internal SSL object */
1354 return SSL_ctrl(ctx
.qc
->tls
, cmd
, larg
, parg
);
1356 case DTLS_CTRL_GET_TIMEOUT
: /* DTLSv1_get_timeout */
1360 if (!ossl_quic_get_event_timeout(s
, parg
, &is_infinite
))
1363 return !is_infinite
;
1365 case DTLS_CTRL_HANDLE_TIMEOUT
: /* DTLSv1_handle_timeout */
1366 /* For legacy compatibility with DTLS calls. */
1367 return ossl_quic_handle_events(s
) == 1 ? 1 : -1;
1369 /* Mask ctrls we shouldn't support for QUIC. */
1370 case SSL_CTRL_GET_READ_AHEAD
:
1371 case SSL_CTRL_SET_READ_AHEAD
:
1372 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1373 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
1374 case SSL_CTRL_SET_MAX_PIPELINES
:
1379 * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1380 * implementation. Either SSL_ctrl will handle it itself by direct
1381 * access into handshake layer state, or failing that, it will be passed
1382 * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1383 * supported by anything, the handshake layer's ctrl method will finally
1386 return ossl_ctrl_internal(&ctx
.qc
->ssl
, cmd
, larg
, parg
, /*no_quic=*/1);
1390 /* SSL_set_connect_state */
1391 void ossl_quic_set_connect_state(SSL
*s
)
1395 if (!expect_quic(s
, &ctx
))
1398 /* Cannot be changed after handshake started */
1399 if (ctx
.qc
->started
|| ctx
.is_stream
)
1402 ctx
.qc
->as_server_state
= 0;
1405 /* SSL_set_accept_state */
1406 void ossl_quic_set_accept_state(SSL
*s
)
1410 if (!expect_quic(s
, &ctx
))
1413 /* Cannot be changed after handshake started */
1414 if (ctx
.qc
->started
|| ctx
.is_stream
)
1417 ctx
.qc
->as_server_state
= 1;
1420 /* SSL_do_handshake */
1421 struct quic_handshake_wait_args
{
1422 QUIC_CONNECTION
*qc
;
1425 static int quic_handshake_wait(void *arg
)
1427 struct quic_handshake_wait_args
*args
= arg
;
1429 if (!quic_mutation_allowed(args
->qc
, /*req_active=*/1))
1432 if (ossl_quic_channel_is_handshake_complete(args
->qc
->ch
))
1438 static int configure_channel(QUIC_CONNECTION
*qc
)
1440 assert(qc
->ch
!= NULL
);
1442 if (!ossl_quic_channel_set_net_rbio(qc
->ch
, qc
->net_rbio
)
1443 || !ossl_quic_channel_set_net_wbio(qc
->ch
, qc
->net_wbio
)
1444 || !ossl_quic_channel_set_peer_addr(qc
->ch
, &qc
->init_peer_addr
))
1451 static int create_channel(QUIC_CONNECTION
*qc
)
1453 QUIC_CHANNEL_ARGS args
= {0};
1455 args
.libctx
= qc
->ssl
.ctx
->libctx
;
1456 args
.propq
= qc
->ssl
.ctx
->propq
;
1457 args
.is_server
= qc
->as_server
;
1459 args
.mutex
= qc
->mutex
;
1460 args
.now_cb
= get_time_cb
;
1461 args
.now_cb_arg
= qc
;
1463 qc
->ch
= ossl_quic_channel_new(&args
);
1464 if (qc
->ch
== NULL
) {
1465 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
1473 * Configures a channel with the information we have accumulated via calls made
1474 * to us from the application prior to starting a handshake attempt.
1477 static int ensure_channel_started(QCTX
*ctx
)
1479 QUIC_CONNECTION
*qc
= ctx
->qc
;
1482 if (!configure_channel(qc
)) {
1483 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
,
1484 "failed to configure channel");
1488 if (!ossl_quic_channel_start(qc
->ch
)) {
1489 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
,
1490 "failed to start channel");
1494 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1495 if (qc
->is_thread_assisted
)
1496 if (!ossl_quic_thread_assist_init_start(&qc
->thread_assist
, qc
->ch
)) {
1497 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
,
1498 "failed to start assist thread");
1509 static int quic_do_handshake(QCTX
*ctx
)
1512 QUIC_CONNECTION
*qc
= ctx
->qc
;
1514 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1515 /* Handshake already completed. */
1518 if (!quic_mutation_allowed(qc
, /*req_active=*/0))
1519 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1521 if (BIO_ADDR_family(&qc
->init_peer_addr
) == AF_UNSPEC
) {
1522 /* Peer address must have been set. */
1523 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET
, NULL
);
1524 return -1; /* Non-protocol error */
1527 if (qc
->as_server
!= qc
->as_server_state
) {
1528 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
1529 return -1; /* Non-protocol error */
1532 if (qc
->net_rbio
== NULL
|| qc
->net_wbio
== NULL
) {
1533 /* Need read and write BIOs. */
1534 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_BIO_NOT_SET
, NULL
);
1535 return -1; /* Non-protocol error */
1539 * Start connection process. Note we may come here multiple times in
1540 * non-blocking mode, which is fine.
1542 if (!ensure_channel_started(qc
)) /* raises on failure */
1543 return -1; /* Non-protocol error */
1545 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1546 /* The handshake is now done. */
1549 if (!qc_blocking_mode(qc
)) {
1550 /* Try to advance the reactor. */
1551 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc
->ch
), 0);
1553 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1554 /* The handshake is now done. */
1557 if (ossl_quic_channel_is_term_any(qc
->ch
)) {
1558 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1560 } else if (qc
->desires_blocking
) {
1562 * As a special case when doing a handshake when blocking mode is
1563 * desired yet not available, see if the network BIOs have become
1564 * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1565 * which do late creation of socket FDs and therefore cannot expose
1566 * a poll descriptor until after a network BIO is set on the QCSO.
1568 assert(!qc
->blocking
);
1569 qc_update_can_support_blocking(qc
);
1570 qc_update_blocking_mode(qc
);
1575 * We are either in blocking mode or just entered it due to the code above.
1577 if (qc_blocking_mode(qc
)) {
1578 /* In blocking mode, wait for the handshake to complete. */
1579 struct quic_handshake_wait_args args
;
1583 ret
= block_until_pred(qc
, quic_handshake_wait
, &args
, 0);
1584 if (!quic_mutation_allowed(qc
, /*req_active=*/1)) {
1585 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1586 return 0; /* Shutdown before completion */
1587 } else if (ret
<= 0) {
1588 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1589 return -1; /* Non-protocol error */
1592 assert(ossl_quic_channel_is_handshake_complete(qc
->ch
));
1597 * Otherwise, indicate that the handshake isn't done yet.
1598 * We can only get here in non-blocking mode.
1600 QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_READ
);
1601 return -1; /* Non-protocol error */
1605 int ossl_quic_do_handshake(SSL
*s
)
1610 if (!expect_quic(s
, &ctx
))
1616 ret
= quic_do_handshake(&ctx
);
1617 quic_unlock(ctx
.qc
);
1622 int ossl_quic_connect(SSL
*s
)
1624 /* Ensure we are in connect state (no-op if non-idle). */
1625 ossl_quic_set_connect_state(s
);
1627 /* Begin or continue the handshake */
1628 return ossl_quic_do_handshake(s
);
1632 int ossl_quic_accept(SSL
*s
)
1634 /* Ensure we are in accept state (no-op if non-idle). */
1635 ossl_quic_set_accept_state(s
);
1637 /* Begin or continue the handshake */
1638 return ossl_quic_do_handshake(s
);
1642 * QUIC Front-End I/O API: Stream Lifecycle Operations
1643 * ===================================================
1645 * SSL_stream_new => ossl_quic_conn_stream_new
1650 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1651 * default XSO was created. Returns 0 if it was not (e.g. because it already
1652 * exists). Note that this is NOT an error condition.
1655 static int qc_try_create_default_xso_for_write(QCTX
*ctx
)
1658 QUIC_CONNECTION
*qc
= ctx
->qc
;
1660 if (qc
->default_xso_created
1661 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
1663 * We only do this once. If the user detaches a previously created
1664 * default XSO we don't auto-create another one.
1666 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
1668 /* Create a locally-initiated stream. */
1669 if (qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_AUTO_UNI
)
1670 flags
|= SSL_STREAM_FLAG_UNI
;
1672 qc_set_default_xso(qc
, (QUIC_XSO
*)quic_conn_stream_new(ctx
, flags
,
1675 if (qc
->default_xso
== NULL
)
1676 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1678 qc_touch_default_xso(qc
);
1682 struct quic_wait_for_stream_args
{
1683 QUIC_CONNECTION
*qc
;
1690 static int quic_wait_for_stream(void *arg
)
1692 struct quic_wait_for_stream_args
*args
= arg
;
1694 if (!quic_mutation_allowed(args
->qc
, /*req_active=*/1)) {
1695 /* If connection is torn down due to an error while blocking, stop. */
1696 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1700 args
->qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args
->qc
->ch
),
1701 args
->expect_id
| QUIC_STREAM_DIR_BIDI
);
1702 if (args
->qs
== NULL
)
1703 args
->qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args
->qc
->ch
),
1704 args
->expect_id
| QUIC_STREAM_DIR_UNI
);
1706 if (args
->qs
!= NULL
)
1707 return 1; /* stream now exists */
1709 return 0; /* did not get a stream, keep trying */
1713 static int qc_wait_for_default_xso_for_read(QCTX
*ctx
)
1715 /* Called on a QCSO and we don't currently have a default stream. */
1717 QUIC_CONNECTION
*qc
= ctx
->qc
;
1720 struct quic_wait_for_stream_args wargs
;
1723 * If default stream functionality is disabled or we already detached
1724 * one, don't make another default stream and just fail.
1726 if (qc
->default_xso_created
1727 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
1728 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
1731 * The peer may have opened a stream since we last ticked. So tick and
1732 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1733 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1734 * first stream created by a peer must have an ordinal of 0.
1736 expect_id
= qc
->as_server
1737 ? QUIC_STREAM_INITIATOR_CLIENT
1738 : QUIC_STREAM_INITIATOR_SERVER
;
1740 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1741 expect_id
| QUIC_STREAM_DIR_BIDI
);
1743 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1744 expect_id
| QUIC_STREAM_DIR_UNI
);
1747 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc
->ch
), 0);
1749 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1754 if (!qc_blocking_mode(qc
))
1755 /* Non-blocking mode, so just bail immediately. */
1756 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_READ
);
1758 /* Block until we have a stream. */
1762 wargs
.expect_id
= expect_id
;
1764 res
= block_until_pred(qc
, quic_wait_for_stream
, &wargs
, 0);
1766 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1767 else if (res
< 0 || wargs
.qs
== NULL
)
1768 /* quic_wait_for_stream raised error here */
1775 * We now have qs != NULL. Make it the default stream, creating the
1778 qc_set_default_xso(qc
, create_xso_from_stream(qc
, qs
), /*touch=*/0);
1779 if (qc
->default_xso
== NULL
)
1780 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1782 qc_touch_default_xso(qc
); /* inhibits default XSO */
1787 static QUIC_XSO
*create_xso_from_stream(QUIC_CONNECTION
*qc
, QUIC_STREAM
*qs
)
1789 QUIC_XSO
*xso
= NULL
;
1791 if ((xso
= OPENSSL_zalloc(sizeof(*xso
))) == NULL
) {
1792 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_CRYPTO_LIB
, NULL
);
1796 if (!ossl_ssl_init(&xso
->ssl
, qc
->ssl
.ctx
, qc
->ssl
.method
, SSL_TYPE_QUIC_XSO
)) {
1797 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
1802 if (!SSL_up_ref(&qc
->ssl
)) {
1803 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_SSL_LIB
, NULL
);
1808 xso
->ssl_mode
= qc
->default_ssl_mode
;
1810 = qc
->default_ssl_options
& OSSL_QUIC_PERMITTED_OPTIONS_STREAM
;
1811 xso
->last_error
= SSL_ERROR_NONE
;
1816 xso_update_options(xso
);
1824 struct quic_new_stream_wait_args
{
1825 QUIC_CONNECTION
*qc
;
1829 static int quic_new_stream_wait(void *arg
)
1831 struct quic_new_stream_wait_args
*args
= arg
;
1832 QUIC_CONNECTION
*qc
= args
->qc
;
1834 if (!quic_mutation_allowed(qc
, /*req_active=*/1))
1837 if (ossl_quic_channel_is_new_local_stream_admissible(qc
->ch
, args
->is_uni
))
1843 /* locking depends on need_lock */
1844 static SSL
*quic_conn_stream_new(QCTX
*ctx
, uint64_t flags
, int need_lock
)
1847 QUIC_CONNECTION
*qc
= ctx
->qc
;
1848 QUIC_XSO
*xso
= NULL
;
1849 QUIC_STREAM
*qs
= NULL
;
1850 int is_uni
= ((flags
& SSL_STREAM_FLAG_UNI
) != 0);
1851 int no_blocking
= ((flags
& SSL_STREAM_FLAG_NO_BLOCK
) != 0);
1852 int advance
= ((flags
& SSL_STREAM_FLAG_ADVANCE
) != 0);
1857 if (!quic_mutation_allowed(qc
, /*req_active=*/0)) {
1858 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1863 && !ossl_quic_channel_is_new_local_stream_admissible(qc
->ch
, is_uni
)) {
1864 struct quic_new_stream_wait_args args
;
1867 * Stream count flow control currently doesn't permit this stream to be
1870 if (no_blocking
|| !qc_blocking_mode(qc
)) {
1871 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_STREAM_COUNT_LIMITED
, NULL
);
1876 args
.is_uni
= is_uni
;
1878 /* Blocking mode - wait until we can get a stream. */
1879 ret
= block_until_pred(ctx
->qc
, quic_new_stream_wait
, &args
, 0);
1880 if (!quic_mutation_allowed(qc
, /*req_active=*/1)) {
1881 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1882 goto err
; /* Shutdown before completion */
1883 } else if (ret
<= 0) {
1884 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1885 goto err
; /* Non-protocol error */
1889 qs
= ossl_quic_channel_new_stream_local(qc
->ch
, is_uni
);
1891 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1895 xso
= create_xso_from_stream(qc
, qs
);
1899 qc_touch_default_xso(qc
); /* inhibits default XSO */
1907 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc
->ch
), qs
);
1916 SSL
*ossl_quic_conn_stream_new(SSL
*s
, uint64_t flags
)
1920 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
1923 return quic_conn_stream_new(&ctx
, flags
, /*need_lock=*/1);
1927 * QUIC Front-End I/O API: Steady-State Operations
1928 * ===============================================
1930 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1931 * is, the functions used during the established phase of a QUIC connection
1932 * (e.g. SSL_read, SSL_write).
1934 * Each function must handle both blocking and non-blocking modes. As discussed
1935 * above, all QUIC I/O is implemented using non-blocking mode internally.
1937 * SSL_get_error => partially implemented by ossl_quic_get_error
1938 * (BIO/)SSL_read => ossl_quic_read
1939 * (BIO/)SSL_write => ossl_quic_write
1940 * SSL_pending => ossl_quic_pending
1941 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1942 * SSL_key_update => ossl_quic_key_update
1946 int ossl_quic_get_error(const SSL
*s
, int i
)
1949 int net_error
, last_error
;
1951 if (!expect_quic(s
, &ctx
))
1955 net_error
= ossl_quic_channel_net_error(ctx
.qc
->ch
);
1956 last_error
= ctx
.is_stream
? ctx
.xso
->last_error
: ctx
.qc
->last_error
;
1957 quic_unlock(ctx
.qc
);
1960 return SSL_ERROR_SYSCALL
;
1969 * The set of functions below provide the implementation of the public SSL_write
1970 * function. We must handle:
1972 * - both blocking and non-blocking operation at the application level,
1973 * depending on how we are configured;
1975 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1977 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1981 static void quic_post_write(QUIC_XSO
*xso
, int did_append
, int do_tick
)
1984 * We have appended at least one byte to the stream.
1985 * Potentially mark stream as active, depending on FC.
1988 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso
->conn
->ch
),
1994 * TODO(QUIC FUTURE): It is probably inefficient to try and do this
1995 * immediately, plus we should eventually consider Nagle's algorithm.
1998 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso
->conn
->ch
), 0);
2001 struct quic_write_again_args
{
2003 const unsigned char *buf
;
2005 size_t total_written
;
2010 static int quic_write_again(void *arg
)
2012 struct quic_write_again_args
*args
= arg
;
2013 size_t actual_written
= 0;
2015 if (!quic_mutation_allowed(args
->xso
->conn
, /*req_active=*/1))
2016 /* If connection is torn down due to an error while blocking, stop. */
2019 if (!quic_validate_for_write(args
->xso
, &args
->err
))
2021 * Stream may have become invalid for write due to connection events
2026 args
->err
= ERR_R_INTERNAL_ERROR
;
2027 if (!ossl_quic_sstream_append(args
->xso
->stream
->sstream
,
2028 args
->buf
, args
->len
, &actual_written
))
2031 quic_post_write(args
->xso
, actual_written
> 0, 0);
2033 args
->buf
+= actual_written
;
2034 args
->len
-= actual_written
;
2035 args
->total_written
+= actual_written
;
2038 /* Written everything, done. */
2041 /* Not written everything yet, keep trying. */
2046 static int quic_write_blocking(QCTX
*ctx
, const void *buf
, size_t len
,
2050 QUIC_XSO
*xso
= ctx
->xso
;
2051 struct quic_write_again_args args
;
2052 size_t actual_written
= 0;
2054 /* First make a best effort to append as much of the data as possible. */
2055 if (!ossl_quic_sstream_append(xso
->stream
->sstream
, buf
, len
,
2057 /* Stream already finished or allocation error. */
2059 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2062 quic_post_write(xso
, actual_written
> 0, 1);
2064 if (actual_written
== len
) {
2065 /* Managed to append everything on the first try. */
2066 *written
= actual_written
;
2071 * We did not manage to append all of the data immediately, so the stream
2072 * buffer has probably filled up. This means we need to block until some of
2076 args
.buf
= (const unsigned char *)buf
+ actual_written
;
2077 args
.len
= len
- actual_written
;
2078 args
.total_written
= 0;
2079 args
.err
= ERR_R_INTERNAL_ERROR
;
2081 res
= block_until_pred(xso
->conn
, quic_write_again
, &args
, 0);
2083 if (!quic_mutation_allowed(xso
->conn
, /*req_active=*/1))
2084 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2086 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, args
.err
, NULL
);
2089 *written
= args
.total_written
;
2094 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2097 static void aon_write_begin(QUIC_XSO
*xso
, const unsigned char *buf
,
2098 size_t buf_len
, size_t already_sent
)
2100 assert(!xso
->aon_write_in_progress
);
2102 xso
->aon_write_in_progress
= 1;
2103 xso
->aon_buf_base
= buf
;
2104 xso
->aon_buf_pos
= already_sent
;
2105 xso
->aon_buf_len
= buf_len
;
2108 static void aon_write_finish(QUIC_XSO
*xso
)
2110 xso
->aon_write_in_progress
= 0;
2111 xso
->aon_buf_base
= NULL
;
2112 xso
->aon_buf_pos
= 0;
2113 xso
->aon_buf_len
= 0;
2117 static int quic_write_nonblocking_aon(QCTX
*ctx
, const void *buf
,
2118 size_t len
, size_t *written
)
2120 QUIC_XSO
*xso
= ctx
->xso
;
2121 const void *actual_buf
;
2122 size_t actual_len
, actual_written
= 0;
2123 int accept_moving_buffer
2124 = ((xso
->ssl_mode
& SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
) != 0);
2126 if (xso
->aon_write_in_progress
) {
2128 * We are in the middle of an AON write (i.e., a previous write did not
2129 * manage to append all data to the SSTREAM and we have Enable Partial
2130 * Write (EPW) mode disabled.)
2132 if ((!accept_moving_buffer
&& xso
->aon_buf_base
!= buf
)
2133 || len
!= xso
->aon_buf_len
)
2135 * Pointer must not have changed if we are not in accept moving
2136 * buffer mode. Length must never change.
2138 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_BAD_WRITE_RETRY
, NULL
);
2140 actual_buf
= (unsigned char *)buf
+ xso
->aon_buf_pos
;
2141 actual_len
= len
- xso
->aon_buf_pos
;
2142 assert(actual_len
> 0);
2148 /* First make a best effort to append as much of the data as possible. */
2149 if (!ossl_quic_sstream_append(xso
->stream
->sstream
, actual_buf
, actual_len
,
2151 /* Stream already finished or allocation error. */
2153 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2156 quic_post_write(xso
, actual_written
> 0, 1);
2158 if (actual_written
== actual_len
) {
2159 /* We have sent everything. */
2160 if (xso
->aon_write_in_progress
) {
2162 * We have sent everything, and we were in the middle of an AON
2163 * write. The output write length is the total length of the AON
2164 * buffer, not however many bytes we managed to write to the stream
2167 *written
= xso
->aon_buf_len
;
2168 aon_write_finish(xso
);
2170 *written
= actual_written
;
2176 if (xso
->aon_write_in_progress
) {
2178 * AON write is in progress but we have not written everything yet. We
2179 * may have managed to send zero bytes, or some number of bytes less
2180 * than the total remaining which need to be appended during this
2183 xso
->aon_buf_pos
+= actual_written
;
2184 assert(xso
->aon_buf_pos
< xso
->aon_buf_len
);
2185 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_WRITE
);
2189 * Not in an existing AON operation but partial write is not enabled, so we
2190 * need to begin a new AON operation. However we needn't bother if we didn't
2191 * actually append anything.
2193 if (actual_written
> 0)
2194 aon_write_begin(xso
, buf
, len
, actual_written
);
2197 * AON - We do not publicly admit to having appended anything until AON
2201 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_WRITE
);
2205 static int quic_write_nonblocking_epw(QCTX
*ctx
, const void *buf
, size_t len
,
2208 QUIC_XSO
*xso
= ctx
->xso
;
2210 /* Simple best effort operation. */
2211 if (!ossl_quic_sstream_append(xso
->stream
->sstream
, buf
, len
, written
)) {
2212 /* Stream already finished or allocation error. */
2214 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2217 quic_post_write(xso
, *written
> 0, 1);
2222 static int quic_validate_for_write(QUIC_XSO
*xso
, int *err
)
2224 QUIC_STREAM_MAP
*qsm
;
2226 if (xso
== NULL
|| xso
->stream
== NULL
) {
2227 *err
= ERR_R_INTERNAL_ERROR
;
2231 switch (xso
->stream
->send_state
) {
2233 case QUIC_SSTREAM_STATE_NONE
:
2234 *err
= SSL_R_STREAM_RECV_ONLY
;
2237 case QUIC_SSTREAM_STATE_READY
:
2238 qsm
= ossl_quic_channel_get_qsm(xso
->conn
->ch
);
2240 if (!ossl_quic_stream_map_ensure_send_part_id(qsm
, xso
->stream
)) {
2241 *err
= ERR_R_INTERNAL_ERROR
;
2246 case QUIC_SSTREAM_STATE_SEND
:
2247 case QUIC_SSTREAM_STATE_DATA_SENT
:
2248 case QUIC_SSTREAM_STATE_DATA_RECVD
:
2249 if (ossl_quic_sstream_get_final_size(xso
->stream
->sstream
, NULL
)) {
2250 *err
= SSL_R_STREAM_FINISHED
;
2256 case QUIC_SSTREAM_STATE_RESET_SENT
:
2257 case QUIC_SSTREAM_STATE_RESET_RECVD
:
2258 *err
= SSL_R_STREAM_RESET
;
2264 int ossl_quic_write(SSL
*s
, const void *buf
, size_t len
, size_t *written
)
2268 int partial_write
, err
;
2272 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/0, /*io=*/1, &ctx
))
2275 partial_write
= ((ctx
.xso
->ssl_mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
) != 0);
2277 if (!quic_mutation_allowed(ctx
.qc
, /*req_active=*/0)) {
2278 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2283 * If we haven't finished the handshake, try to advance it.
2284 * We don't accept writes until the handshake is completed.
2286 if (quic_do_handshake(&ctx
) < 1) {
2291 /* Ensure correct stream state, stream send part not concluded, etc. */
2292 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2293 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2302 if (xso_blocking_mode(ctx
.xso
))
2303 ret
= quic_write_blocking(&ctx
, buf
, len
, written
);
2304 else if (partial_write
)
2305 ret
= quic_write_nonblocking_epw(&ctx
, buf
, len
, written
);
2307 ret
= quic_write_nonblocking_aon(&ctx
, buf
, len
, written
);
2310 quic_unlock(ctx
.qc
);
2318 struct quic_read_again_args
{
2320 QUIC_STREAM
*stream
;
2328 static int quic_validate_for_read(QUIC_XSO
*xso
, int *err
, int *eos
)
2330 QUIC_STREAM_MAP
*qsm
;
2334 if (xso
== NULL
|| xso
->stream
== NULL
) {
2335 *err
= ERR_R_INTERNAL_ERROR
;
2339 switch (xso
->stream
->recv_state
) {
2341 case QUIC_RSTREAM_STATE_NONE
:
2342 *err
= SSL_R_STREAM_SEND_ONLY
;
2345 case QUIC_RSTREAM_STATE_RECV
:
2346 case QUIC_RSTREAM_STATE_SIZE_KNOWN
:
2347 case QUIC_RSTREAM_STATE_DATA_RECVD
:
2350 case QUIC_RSTREAM_STATE_DATA_READ
:
2354 case QUIC_RSTREAM_STATE_RESET_RECVD
:
2355 qsm
= ossl_quic_channel_get_qsm(xso
->conn
->ch
);
2356 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm
, xso
->stream
);
2359 case QUIC_RSTREAM_STATE_RESET_READ
:
2360 *err
= SSL_R_STREAM_RESET
;
2366 static int quic_read_actual(QCTX
*ctx
,
2367 QUIC_STREAM
*stream
,
2368 void *buf
, size_t buf_len
,
2372 int is_fin
= 0, err
, eos
;
2373 QUIC_CONNECTION
*qc
= ctx
->qc
;
2375 if (!quic_validate_for_read(ctx
->xso
, &err
, &eos
)) {
2377 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_ZERO_RETURN
);
2379 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, err
, NULL
);
2383 if (!ossl_quic_rstream_peek(stream
->rstream
, buf
, buf_len
,
2384 bytes_read
, &is_fin
))
2385 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2388 if (!ossl_quic_rstream_read(stream
->rstream
, buf
, buf_len
,
2389 bytes_read
, &is_fin
))
2390 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2394 if (*bytes_read
> 0) {
2396 * We have read at least one byte from the stream. Inform stream-level
2397 * RXFC of the retirement of controlled bytes. Update the active stream
2398 * status (the RXFC may now want to emit a frame granting more credit to
2401 OSSL_RTT_INFO rtt_info
;
2403 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc
->ch
), &rtt_info
);
2405 if (!ossl_quic_rxfc_on_retire(&stream
->rxfc
, *bytes_read
,
2406 rtt_info
.smoothed_rtt
))
2407 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2410 if (is_fin
&& !peek
) {
2411 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(ctx
->qc
->ch
);
2413 ossl_quic_stream_map_notify_totally_read(qsm
, ctx
->xso
->stream
);
2416 if (*bytes_read
> 0)
2417 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc
->ch
),
2421 if (*bytes_read
== 0 && is_fin
)
2422 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_ZERO_RETURN
);
2428 static int quic_read_again(void *arg
)
2430 struct quic_read_again_args
*args
= arg
;
2432 if (!quic_mutation_allowed(args
->ctx
->qc
, /*req_active=*/1)) {
2433 /* If connection is torn down due to an error while blocking, stop. */
2434 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2438 if (!quic_read_actual(args
->ctx
, args
->stream
,
2439 args
->buf
, args
->len
, args
->bytes_read
,
2443 if (*args
->bytes_read
> 0)
2444 /* got at least one byte, the SSL_read op can finish now */
2447 return 0; /* did not read anything, keep trying */
2451 static int quic_read(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
, int peek
)
2455 struct quic_read_again_args args
;
2459 if (!expect_quic(s
, &ctx
))
2465 if (!quic_mutation_allowed(ctx
.qc
, /*req_active=*/0)) {
2466 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2470 /* If we haven't finished the handshake, try to advance it. */
2471 if (quic_do_handshake(&ctx
) < 1) {
2472 ret
= 0; /* ossl_quic_do_handshake raised error here */
2476 if (ctx
.xso
== NULL
) {
2478 * Called on a QCSO and we don't currently have a default stream.
2480 * Wait until we get a stream initiated by the peer (blocking mode) or
2481 * fail if we don't have one yet (non-blocking mode).
2483 if (!qc_wait_for_default_xso_for_read(&ctx
)) {
2484 ret
= 0; /* error already raised here */
2488 ctx
.xso
= ctx
.qc
->default_xso
;
2491 if (!quic_read_actual(&ctx
, ctx
.xso
->stream
, buf
, len
, bytes_read
, peek
)) {
2492 ret
= 0; /* quic_read_actual raised error here */
2496 if (*bytes_read
> 0) {
2498 * Even though we succeeded, tick the reactor here to ensure we are
2499 * handling other aspects of the QUIC connection.
2501 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
2503 } else if (xso_blocking_mode(ctx
.xso
)) {
2505 * We were not able to read anything immediately, so our stream
2506 * buffer is empty. This means we need to block until we get
2507 * at least one byte.
2510 args
.stream
= ctx
.xso
->stream
;
2513 args
.bytes_read
= bytes_read
;
2516 res
= block_until_pred(ctx
.qc
, quic_read_again
, &args
, 0);
2518 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2520 } else if (res
< 0) {
2521 ret
= 0; /* quic_read_again raised error here */
2528 * We did not get any bytes and are not in blocking mode.
2529 * Tick to see if this delivers any more.
2531 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
2533 /* Try the read again. */
2534 if (!quic_read_actual(&ctx
, ctx
.xso
->stream
, buf
, len
, bytes_read
, peek
)) {
2535 ret
= 0; /* quic_read_actual raised error here */
2539 if (*bytes_read
> 0)
2540 ret
= 1; /* Succeeded this time. */
2542 ret
= QUIC_RAISE_NORMAL_ERROR(&ctx
, SSL_ERROR_WANT_READ
);
2546 quic_unlock(ctx
.qc
);
2550 int ossl_quic_read(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
)
2552 return quic_read(s
, buf
, len
, bytes_read
, 0);
2555 int ossl_quic_peek(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
)
2557 return quic_read(s
, buf
, len
, bytes_read
, 1);
2566 static size_t ossl_quic_pending_int(const SSL
*s
, int check_channel
)
2573 if (!expect_quic(s
, &ctx
))
2578 if (ctx
.xso
== NULL
) {
2579 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_NO_STREAM
, NULL
);
2583 if (ctx
.xso
->stream
== NULL
2584 || !ossl_quic_stream_has_recv_buffer(ctx
.xso
->stream
)) {
2585 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2589 if (!ossl_quic_rstream_available(ctx
.xso
->stream
->rstream
, &avail
, &fin
))
2592 if (avail
== 0 && check_channel
&& ossl_quic_channel_has_pending(ctx
.qc
->ch
))
2596 quic_unlock(ctx
.qc
);
2600 size_t ossl_quic_pending(const SSL
*s
)
2602 return ossl_quic_pending_int(s
, /*check_channel=*/0);
2605 int ossl_quic_has_pending(const SSL
*s
)
2607 /* Do we have app-side pending data or pending URXEs or RXEs? */
2608 return ossl_quic_pending_int(s
, /*check_channel=*/1) > 0;
2612 * SSL_stream_conclude
2613 * -------------------
2616 int ossl_quic_conn_stream_conclude(SSL
*s
)
2622 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/0, /*io=*/0, &ctx
))
2625 qs
= ctx
.xso
->stream
;
2627 if (!quic_mutation_allowed(ctx
.qc
, /*req_active=*/1)) {
2628 quic_unlock(ctx
.qc
);
2629 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2632 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2633 quic_unlock(ctx
.qc
);
2634 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2637 if (ossl_quic_sstream_get_final_size(qs
->sstream
, NULL
)) {
2638 quic_unlock(ctx
.qc
);
2642 ossl_quic_sstream_fin(qs
->sstream
);
2643 quic_post_write(ctx
.xso
, 1, 1);
2644 quic_unlock(ctx
.qc
);
2649 * SSL_inject_net_dgram
2650 * --------------------
2653 int SSL_inject_net_dgram(SSL
*s
, const unsigned char *buf
,
2655 const BIO_ADDR
*peer
,
2656 const BIO_ADDR
*local
)
2662 if (!expect_quic(s
, &ctx
))
2667 demux
= ossl_quic_channel_get0_demux(ctx
.qc
->ch
);
2668 ret
= ossl_quic_demux_inject(demux
, buf
, buf_len
, peer
, local
);
2670 quic_unlock(ctx
.qc
);
2675 * SSL_get0_connection
2676 * -------------------
2678 SSL
*ossl_quic_get0_connection(SSL
*s
)
2682 if (!expect_quic(s
, &ctx
))
2685 return &ctx
.qc
->ssl
;
2689 * SSL_get_stream_type
2690 * -------------------
2692 int ossl_quic_get_stream_type(SSL
*s
)
2696 if (!expect_quic(s
, &ctx
))
2697 return SSL_STREAM_TYPE_BIDI
;
2699 if (ctx
.xso
== NULL
) {
2701 * If deferred XSO creation has yet to occur, proceed according to the
2702 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2703 * what kind of stream will be created yet, so return BIDI on the basis
2704 * that at this time, the client still has the option of calling
2705 * SSL_read() or SSL_write() first.
2707 if (ctx
.qc
->default_xso_created
2708 || ctx
.qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
2709 return SSL_STREAM_TYPE_NONE
;
2711 return SSL_STREAM_TYPE_BIDI
;
2714 if (ossl_quic_stream_is_bidi(ctx
.xso
->stream
))
2715 return SSL_STREAM_TYPE_BIDI
;
2717 if (ossl_quic_stream_is_server_init(ctx
.xso
->stream
) != ctx
.qc
->as_server
)
2718 return SSL_STREAM_TYPE_READ
;
2720 return SSL_STREAM_TYPE_WRITE
;
2728 uint64_t ossl_quic_get_stream_id(SSL
*s
)
2733 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/-1, /*io=*/0, &ctx
))
2736 id
= ctx
.xso
->stream
->id
;
2737 quic_unlock(ctx
.qc
);
2743 * SSL_set_default_stream_mode
2744 * ---------------------------
2747 int ossl_quic_set_default_stream_mode(SSL
*s
, uint32_t mode
)
2751 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
2756 if (ctx
.qc
->default_xso_created
) {
2757 quic_unlock(ctx
.qc
);
2758 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
2759 "too late to change default stream mode");
2763 case SSL_DEFAULT_STREAM_MODE_NONE
:
2764 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI
:
2765 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI
:
2766 ctx
.qc
->default_stream_mode
= mode
;
2769 quic_unlock(ctx
.qc
);
2770 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
2771 "bad default stream type");
2774 quic_unlock(ctx
.qc
);
2783 SSL
*ossl_quic_detach_stream(SSL
*s
)
2786 QUIC_XSO
*xso
= NULL
;
2788 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
2793 /* Calling this function inhibits default XSO autocreation. */
2794 /* QC ref to any default XSO is transferred to us and to caller. */
2795 qc_set_default_xso_keep_ref(ctx
.qc
, NULL
, /*touch=*/1, &xso
);
2797 quic_unlock(ctx
.qc
);
2799 return xso
!= NULL
? &xso
->ssl
: NULL
;
2807 int ossl_quic_attach_stream(SSL
*conn
, SSL
*stream
)
2813 if (!expect_quic_conn_only(conn
, /*io=*/0, &ctx
))
2816 if (stream
== NULL
|| stream
->type
!= SSL_TYPE_QUIC_XSO
)
2817 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_NULL_PARAMETER
,
2818 "stream to attach must be a valid QUIC stream");
2820 xso
= (QUIC_XSO
*)stream
;
2824 if (ctx
.qc
->default_xso
!= NULL
) {
2825 quic_unlock(ctx
.qc
);
2826 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
2827 "connection already has a default stream");
2831 * It is a caller error for the XSO being attached as a default XSO to have
2832 * more than one ref.
2834 if (!CRYPTO_GET_REF(&xso
->ssl
.references
, &nref
)) {
2835 quic_unlock(ctx
.qc
);
2836 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
,
2841 quic_unlock(ctx
.qc
);
2842 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
2843 "stream being attached must have "
2844 "only 1 reference");
2847 /* Caller's reference to the XSO is transferred to us. */
2848 /* Calling this function inhibits default XSO autocreation. */
2849 qc_set_default_xso(ctx
.qc
, xso
, /*touch=*/1);
2851 quic_unlock(ctx
.qc
);
2856 * SSL_set_incoming_stream_policy
2857 * ------------------------------
2860 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION
*qc
)
2862 switch (qc
->incoming_stream_policy
) {
2863 case SSL_INCOMING_STREAM_POLICY_AUTO
:
2864 if ((qc
->default_xso
== NULL
&& !qc
->default_xso_created
)
2865 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
2866 return SSL_INCOMING_STREAM_POLICY_ACCEPT
;
2868 return SSL_INCOMING_STREAM_POLICY_REJECT
;
2871 return qc
->incoming_stream_policy
;
2876 static void qc_update_reject_policy(QUIC_CONNECTION
*qc
)
2878 int policy
= qc_get_effective_incoming_stream_policy(qc
);
2879 int enable_reject
= (policy
== SSL_INCOMING_STREAM_POLICY_REJECT
);
2881 ossl_quic_channel_set_incoming_stream_auto_reject(qc
->ch
,
2883 qc
->incoming_stream_aec
);
2887 int ossl_quic_set_incoming_stream_policy(SSL
*s
, int policy
,
2893 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
2899 case SSL_INCOMING_STREAM_POLICY_AUTO
:
2900 case SSL_INCOMING_STREAM_POLICY_ACCEPT
:
2901 case SSL_INCOMING_STREAM_POLICY_REJECT
:
2902 ctx
.qc
->incoming_stream_policy
= policy
;
2903 ctx
.qc
->incoming_stream_aec
= aec
;
2907 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
2912 qc_update_reject_policy(ctx
.qc
);
2913 quic_unlock(ctx
.qc
);
2921 struct wait_for_incoming_stream_args
{
2927 static int wait_for_incoming_stream(void *arg
)
2929 struct wait_for_incoming_stream_args
*args
= arg
;
2930 QUIC_CONNECTION
*qc
= args
->ctx
->qc
;
2931 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
2933 if (!quic_mutation_allowed(qc
, /*req_active=*/1)) {
2934 /* If connection is torn down due to an error while blocking, stop. */
2935 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2939 args
->qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
2940 if (args
->qs
!= NULL
)
2941 return 1; /* got a stream */
2943 return 0; /* did not get a stream, keep trying */
2947 SSL
*ossl_quic_accept_stream(SSL
*s
, uint64_t flags
)
2952 QUIC_STREAM_MAP
*qsm
;
2955 OSSL_RTT_INFO rtt_info
;
2957 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
2962 if (qc_get_effective_incoming_stream_policy(ctx
.qc
)
2963 == SSL_INCOMING_STREAM_POLICY_REJECT
) {
2964 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
, NULL
);
2968 qsm
= ossl_quic_channel_get_qsm(ctx
.qc
->ch
);
2970 qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
2972 if (qc_blocking_mode(ctx
.qc
)
2973 && (flags
& SSL_ACCEPT_STREAM_NO_BLOCK
) == 0) {
2974 struct wait_for_incoming_stream_args args
;
2979 ret
= block_until_pred(ctx
.qc
, wait_for_incoming_stream
, &args
, 0);
2981 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2983 } else if (ret
< 0 || args
.qs
== NULL
) {
2993 xso
= create_xso_from_stream(ctx
.qc
, qs
);
2997 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx
.qc
->ch
), &rtt_info
);
2998 ossl_quic_stream_map_remove_from_accept_queue(qsm
, qs
,
2999 rtt_info
.smoothed_rtt
);
3002 /* Calling this function inhibits default XSO autocreation. */
3003 qc_touch_default_xso(ctx
.qc
); /* inhibits default XSO */
3006 quic_unlock(ctx
.qc
);
3011 * SSL_get_accept_stream_queue_len
3012 * -------------------------------
3015 size_t ossl_quic_get_accept_stream_queue_len(SSL
*s
)
3020 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
3025 v
= ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx
.qc
->ch
));
3027 quic_unlock(ctx
.qc
);
3035 int ossl_quic_stream_reset(SSL
*ssl
,
3036 const SSL_STREAM_RESET_ARGS
*args
,
3040 QUIC_STREAM_MAP
*qsm
;
3042 uint64_t error_code
;
3045 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/0, /*io=*/0, &ctx
))
3048 qsm
= ossl_quic_channel_get_qsm(ctx
.qc
->ch
);
3049 qs
= ctx
.xso
->stream
;
3050 error_code
= (args
!= NULL
? args
->quic_error_code
: 0);
3052 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
3053 ok
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
3057 ok
= ossl_quic_stream_map_reset_stream_send_part(qsm
, qs
, error_code
);
3060 quic_unlock(ctx
.qc
);
3065 * SSL_get_stream_read_state
3066 * -------------------------
3068 static void quic_classify_stream(QUIC_CONNECTION
*qc
,
3072 uint64_t *app_error_code
)
3075 uint64_t final_size
;
3077 local_init
= (ossl_quic_stream_is_server_init(qs
) == qc
->as_server
);
3079 if (app_error_code
!= NULL
)
3080 *app_error_code
= UINT64_MAX
;
3082 app_error_code
= &final_size
; /* throw away value */
3084 if (!ossl_quic_stream_is_bidi(qs
) && local_init
!= is_write
) {
3086 * Unidirectional stream and this direction of transmission doesn't
3089 *state
= SSL_STREAM_STATE_WRONG_DIR
;
3090 } else if (ossl_quic_channel_is_term_any(qc
->ch
)) {
3091 /* Connection already closed. */
3092 *state
= SSL_STREAM_STATE_CONN_CLOSED
;
3093 } else if (!is_write
&& qs
->recv_state
== QUIC_RSTREAM_STATE_DATA_READ
) {
3094 /* Application has read a FIN. */
3095 *state
= SSL_STREAM_STATE_FINISHED
;
3096 } else if ((!is_write
&& qs
->stop_sending
)
3097 || (is_write
&& ossl_quic_stream_send_is_reset(qs
))) {
3099 * Stream has been reset locally. FIN takes precedence over this for the
3100 * read case as the application need not care if the stream is reset
3101 * after a FIN has been successfully processed.
3103 *state
= SSL_STREAM_STATE_RESET_LOCAL
;
3104 *app_error_code
= !is_write
3105 ? qs
->stop_sending_aec
3106 : qs
->reset_stream_aec
;
3107 } else if ((!is_write
&& ossl_quic_stream_recv_is_reset(qs
))
3108 || (is_write
&& qs
->peer_stop_sending
)) {
3110 * Stream has been reset remotely. */
3111 *state
= SSL_STREAM_STATE_RESET_REMOTE
;
3112 *app_error_code
= !is_write
3113 ? qs
->peer_reset_stream_aec
3114 : qs
->peer_stop_sending_aec
;
3115 } else if (is_write
&& ossl_quic_sstream_get_final_size(qs
->sstream
,
3118 * Stream has been finished. Stream reset takes precedence over this for
3119 * the write case as peer may not have received all data.
3121 *state
= SSL_STREAM_STATE_FINISHED
;
3123 /* Stream still healthy. */
3124 *state
= SSL_STREAM_STATE_OK
;
3128 static int quic_get_stream_state(SSL
*ssl
, int is_write
)
3133 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, /*io=*/0, &ctx
))
3134 return SSL_STREAM_STATE_NONE
;
3136 quic_classify_stream(ctx
.qc
, ctx
.xso
->stream
, is_write
, &state
, NULL
);
3137 quic_unlock(ctx
.qc
);
3141 int ossl_quic_get_stream_read_state(SSL
*ssl
)
3143 return quic_get_stream_state(ssl
, /*is_write=*/0);
3147 * SSL_get_stream_write_state
3148 * --------------------------
3150 int ossl_quic_get_stream_write_state(SSL
*ssl
)
3152 return quic_get_stream_state(ssl
, /*is_write=*/1);
3156 * SSL_get_stream_read_error_code
3157 * ------------------------------
3159 static int quic_get_stream_error_code(SSL
*ssl
, int is_write
,
3160 uint64_t *app_error_code
)
3165 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, /*io=*/0, &ctx
))
3168 quic_classify_stream(ctx
.qc
, ctx
.xso
->stream
, /*is_write=*/0,
3169 &state
, app_error_code
);
3171 quic_unlock(ctx
.qc
);
3173 case SSL_STREAM_STATE_FINISHED
:
3175 case SSL_STREAM_STATE_RESET_LOCAL
:
3176 case SSL_STREAM_STATE_RESET_REMOTE
:
3183 int ossl_quic_get_stream_read_error_code(SSL
*ssl
, uint64_t *app_error_code
)
3185 return quic_get_stream_error_code(ssl
, /*is_write=*/0, app_error_code
);
3189 * SSL_get_stream_write_error_code
3190 * -------------------------------
3192 int ossl_quic_get_stream_write_error_code(SSL
*ssl
, uint64_t *app_error_code
)
3194 return quic_get_stream_error_code(ssl
, /*is_write=*/1, app_error_code
);
3198 * Write buffer size mutation
3199 * --------------------------
3201 int ossl_quic_set_write_buffer_size(SSL
*ssl
, size_t size
)
3206 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, /*io=*/0, &ctx
))
3209 if (!ossl_quic_stream_has_send(ctx
.xso
->stream
)) {
3210 /* Called on a unidirectional receive-only stream - error. */
3211 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
, NULL
);
3215 if (!ossl_quic_stream_has_send_buffer(ctx
.xso
->stream
)) {
3217 * If the stream has a send part but we have disposed of it because we
3218 * no longer need it, this is a no-op.
3224 if (!ossl_quic_sstream_set_buffer_size(ctx
.xso
->stream
->sstream
, size
)) {
3225 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
3232 quic_unlock(ctx
.qc
);
3237 * SSL_get_conn_close_info
3238 * -----------------------
3240 int ossl_quic_get_conn_close_info(SSL
*ssl
,
3241 SSL_CONN_CLOSE_INFO
*info
,
3245 const QUIC_TERMINATE_CAUSE
*tc
;
3247 if (!expect_quic_conn_only(ssl
, /*io=*/0, &ctx
))
3250 tc
= ossl_quic_channel_get_terminate_cause(ctx
.qc
->ch
);
3254 info
->error_code
= tc
->error_code
;
3255 info
->reason
= tc
->reason
;
3256 info
->reason_len
= tc
->reason_len
;
3257 info
->is_local
= !tc
->remote
;
3258 info
->is_transport
= !tc
->app
;
3266 int ossl_quic_key_update(SSL
*ssl
, int update_type
)
3270 if (!expect_quic_conn_only(ssl
, /*io=*/0, &ctx
))
3273 switch (update_type
) {
3274 case SSL_KEY_UPDATE_NOT_REQUESTED
:
3276 * QUIC signals peer key update implicily by triggering a local
3277 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3279 case SSL_KEY_UPDATE_REQUESTED
:
3283 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
3289 /* Attempt to perform a TXKU. */
3290 if (!ossl_quic_channel_trigger_txku(ctx
.qc
->ch
)) {
3291 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_TOO_MANY_KEY_UPDATES
, NULL
);
3292 quic_unlock(ctx
.qc
);
3296 quic_unlock(ctx
.qc
);
3301 * SSL_get_key_update_type
3302 * -----------------------
3304 int ossl_quic_get_key_update_type(const SSL
*s
)
3307 * We always handle key updates immediately so a key update is never
3310 return SSL_KEY_UPDATE_NONE
;
3314 * QUIC Front-End I/O API: SSL_CTX Management
3315 * ==========================================
3318 long ossl_quic_ctx_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
3322 return ssl3_ctx_ctrl(ctx
, cmd
, larg
, parg
);
3326 long ossl_quic_callback_ctrl(SSL
*s
, int cmd
, void (*fp
) (void))
3330 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))
3334 case SSL_CTRL_SET_MSG_CALLBACK
:
3335 ossl_quic_channel_set_msg_callback(ctx
.qc
->ch
, (ossl_msg_cb
)fp
,
3337 /* This callback also needs to be set on the internal SSL object */
3338 return ssl3_callback_ctrl(ctx
.qc
->tls
, cmd
, fp
);;
3341 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3342 return ssl3_callback_ctrl(ctx
.qc
->tls
, cmd
, fp
);
3346 long ossl_quic_ctx_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
) (void))
3348 return ssl3_ctx_callback_ctrl(ctx
, cmd
, fp
);
3351 int ossl_quic_renegotiate_check(SSL
*ssl
, int initok
)
3353 /* We never do renegotiation. */
3358 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3359 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3362 int ossl_quic_num_ciphers(void)
3367 const SSL_CIPHER
*ossl_quic_get_cipher(unsigned int u
)
3373 * Internal Testing APIs
3374 * =====================
3377 QUIC_CHANNEL
*ossl_quic_conn_get_channel(SSL
*s
)
3381 if (!expect_quic_conn_only(s
, /*io=*/0, &ctx
))