2 * Copyright 2022-2023 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/quic_engine.h"
19 #include "internal/quic_port.h"
20 #include "internal/time.h"
22 typedef struct qctx_st QCTX
;
24 static void aon_write_finish(QUIC_XSO
*xso
);
25 static int create_channel(QUIC_CONNECTION
*qc
);
26 static QUIC_XSO
*create_xso_from_stream(QUIC_CONNECTION
*qc
, QUIC_STREAM
*qs
);
27 static int qc_try_create_default_xso_for_write(QCTX
*ctx
);
28 static int qc_wait_for_default_xso_for_read(QCTX
*ctx
);
29 static void quic_lock(QUIC_CONNECTION
*qc
);
30 static void quic_unlock(QUIC_CONNECTION
*qc
);
31 static void quic_lock_for_io(QCTX
*ctx
);
32 static int quic_do_handshake(QCTX
*ctx
);
33 static void qc_update_reject_policy(QUIC_CONNECTION
*qc
);
34 static void qc_touch_default_xso(QUIC_CONNECTION
*qc
);
35 static void qc_set_default_xso(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
, int touch
);
36 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
,
37 int touch
, QUIC_XSO
**old_xso
);
38 static SSL
*quic_conn_stream_new(QCTX
*ctx
, uint64_t flags
, int need_lock
);
39 static int quic_validate_for_write(QUIC_XSO
*xso
, int *err
);
40 static int quic_mutation_allowed(QUIC_CONNECTION
*qc
, int req_active
);
41 static int qc_blocking_mode(const QUIC_CONNECTION
*qc
);
42 static int xso_blocking_mode(const QUIC_XSO
*xso
);
45 * QUIC Front-End I/O API: Common Utilities
46 * ========================================
50 * Block until a predicate is met.
52 * Precondition: Must have a channel.
53 * Precondition: Must hold channel lock (unchecked).
56 static int block_until_pred(QUIC_CONNECTION
*qc
,
57 int (*pred
)(void *arg
), void *pred_arg
,
62 assert(qc
->ch
!= NULL
);
65 * Any attempt to block auto-disables tick inhibition as otherwise we will
66 * hang around forever.
68 ossl_quic_engine_set_inhibit_tick(qc
->engine
, 0);
70 rtor
= ossl_quic_channel_get_reactor(qc
->ch
);
71 return ossl_quic_reactor_block_until_pred(rtor
, pred
, pred_arg
, flags
,
75 static OSSL_TIME
get_time(QUIC_CONNECTION
*qc
)
77 if (qc
->override_now_cb
!= NULL
)
78 return qc
->override_now_cb(qc
->override_now_cb_arg
);
80 return ossl_time_now();
83 static OSSL_TIME
get_time_cb(void *arg
)
85 QUIC_CONNECTION
*qc
= arg
;
91 * QCTX is a utility structure which provides information we commonly wish to
92 * unwrap upon an API call being dispatched to us, namely:
94 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
96 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
97 * a QCSO with a default stream was passed);
98 * - whether a QSSO was passed (xso == NULL must not be used to determine this
99 * because it may be non-NULL when a QCSO is passed if that QCSO has a
101 * - whether we are in "I/O context", meaning that non-normal errors can
102 * be reported via SSL_get_error() as well as via ERR. Functions such as
103 * SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
104 * functions which are allowed to change the value returned by
105 * SSL_get_error. However, other functions (including functions which call
106 * SSL_do_handshake() implicitly) are not allowed to change the return value
112 int is_stream
, in_io
;
116 static void quic_set_last_error(QCTX
*ctx
, int last_error
)
121 if (ctx
->is_stream
&& ctx
->xso
!= NULL
)
122 ctx
->xso
->last_error
= last_error
;
123 else if (!ctx
->is_stream
&& ctx
->qc
!= NULL
)
124 ctx
->qc
->last_error
= last_error
;
128 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
129 * rather than via ERR. Note that normal errors must always be raised while
133 static int quic_raise_normal_error(QCTX
*ctx
,
137 quic_set_last_error(ctx
, err
);
143 * Raise a 'non-normal' error, meaning any error that is not reported via
144 * SSL_get_error() and must be reported via ERR.
146 * qc should be provided if available. In exceptional circumstances when qc is
147 * not known NULL may be passed. This should generally only happen when an
148 * expect_...() function defined below fails, which generally indicates a
149 * dispatch error or caller error.
151 * ctx should be NULL if the connection lock is not held.
153 static int quic_raise_non_normal_error(QCTX
*ctx
,
164 quic_set_last_error(ctx
, SSL_ERROR_SSL
);
166 if (reason
== SSL_R_PROTOCOL_IS_SHUTDOWN
&& ctx
->qc
!= NULL
)
167 ossl_quic_channel_restore_err_state(ctx
->qc
->ch
);
171 ERR_set_debug(file
, line
, func
);
174 ERR_vset_error(ERR_LIB_SSL
, reason
, fmt
, args
);
180 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
181 quic_raise_normal_error((ctx), (err))
183 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
184 quic_raise_non_normal_error((ctx), \
185 OPENSSL_FILE, OPENSSL_LINE, \
191 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
192 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
194 * After this returns 1, all fields of the passed QCTX are initialised.
195 * Returns 0 on failure. This function is intended to be used to provide API
196 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
198 static int expect_quic(const SSL
*s
, QCTX
*ctx
)
208 return QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_PASSED_NULL_PARAMETER
, NULL
);
211 case SSL_TYPE_QUIC_CONNECTION
:
212 qc
= (QUIC_CONNECTION
*)s
;
214 ctx
->xso
= qc
->default_xso
;
219 case SSL_TYPE_QUIC_XSO
:
228 return QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
233 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
234 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
237 * remote_init determines if we expect the default XSO to be remotely created or
238 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
240 * Channel mutex is acquired and retained on success.
243 static int ossl_unused
expect_quic_with_stream_lock(const SSL
*s
, int remote_init
,
244 int in_io
, QCTX
*ctx
)
246 if (!expect_quic(s
, ctx
))
250 quic_lock_for_io(ctx
);
254 if (ctx
->xso
== NULL
&& remote_init
>= 0) {
255 if (!quic_mutation_allowed(ctx
->qc
, /*req_active=*/0)) {
256 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
260 /* If we haven't finished the handshake, try to advance it. */
261 if (quic_do_handshake(ctx
) < 1)
262 /* ossl_quic_do_handshake raised error here */
265 if (remote_init
== 0) {
266 if (!qc_try_create_default_xso_for_write(ctx
))
269 if (!qc_wait_for_default_xso_for_read(ctx
))
273 ctx
->xso
= ctx
->qc
->default_xso
;
276 if (ctx
->xso
== NULL
) {
277 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
281 return 1; /* coverity[missing_unlock]: lock held */
284 quic_unlock(ctx
->qc
);
289 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
290 * be non-NULL if the QCSO has a default stream.
292 static int ossl_unused
expect_quic_conn_only(const SSL
*s
, QCTX
*ctx
)
294 if (!expect_quic(s
, ctx
))
298 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_CONN_USE_ONLY
, NULL
);
304 * Ensures that the channel mutex is held for a method which touches channel
307 * Precondition: Channel mutex is not held (unchecked)
309 static void quic_lock(QUIC_CONNECTION
*qc
)
311 #if defined(OPENSSL_THREADS)
312 ossl_crypto_mutex_lock(qc
->mutex
);
316 static void quic_lock_for_io(QCTX
*ctx
)
322 * We are entering an I/O function so we must update the values returned by
323 * SSL_get_error and SSL_want. Set no error. This will be overridden later
324 * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR
325 * occurs during the API call.
327 quic_set_last_error(ctx
, SSL_ERROR_NONE
);
330 /* Precondition: Channel mutex is held (unchecked) */
332 static void quic_unlock(QUIC_CONNECTION
*qc
)
334 #if defined(OPENSSL_THREADS)
335 ossl_crypto_mutex_unlock(qc
->mutex
);
340 * This predicate is the criterion which should determine API call rejection for
341 * *most* mutating API calls, particularly stream-related operations for send
344 * A call is rejected (this function returns 0) if shutdown is in progress
345 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
346 * req_active=1, the connection must be active (i.e., the IDLE state is also
349 static int quic_mutation_allowed(QUIC_CONNECTION
*qc
, int req_active
)
351 if (qc
->shutting_down
|| ossl_quic_channel_is_term_any(qc
->ch
))
354 if (req_active
&& !ossl_quic_channel_is_active(qc
->ch
))
361 * QUIC Front-End I/O API: Initialization
362 * ======================================
364 * SSL_new => ossl_quic_new
366 * SSL_reset => ossl_quic_reset
367 * SSL_clear => ossl_quic_clear
369 * SSL_free => ossl_quic_free
371 * SSL_set_options => ossl_quic_set_options
372 * SSL_get_options => ossl_quic_get_options
373 * SSL_clear_options => ossl_quic_clear_options
378 SSL
*ossl_quic_new(SSL_CTX
*ctx
)
380 QUIC_CONNECTION
*qc
= NULL
;
381 SSL
*ssl_base
= NULL
;
382 SSL_CONNECTION
*sc
= NULL
;
384 qc
= OPENSSL_zalloc(sizeof(*qc
));
386 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_CRYPTO_LIB
, NULL
);
389 #if defined(OPENSSL_THREADS)
390 if ((qc
->mutex
= ossl_crypto_mutex_new()) == NULL
) {
391 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_CRYPTO_LIB
, NULL
);
396 /* Initialise the QUIC_CONNECTION's stub header. */
398 if (!ossl_ssl_init(ssl_base
, ctx
, ctx
->method
, SSL_TYPE_QUIC_CONNECTION
)) {
400 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
404 qc
->tls
= ossl_ssl_connection_new_int(ctx
, TLS_method());
405 if (qc
->tls
== NULL
|| (sc
= SSL_CONNECTION_FROM_SSL(qc
->tls
)) == NULL
) {
406 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
410 /* override the user_ssl of the inner connection */
411 sc
->s3
.flags
|= TLS1_FLAGS_QUIC
;
413 /* Restrict options derived from the SSL_CTX. */
414 sc
->options
&= OSSL_QUIC_PERMITTED_OPTIONS_CONN
;
417 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
418 qc
->is_thread_assisted
419 = (ssl_base
->method
== OSSL_QUIC_client_thread_method());
422 qc
->as_server
= 0; /* TODO(QUIC SERVER): add server support */
423 qc
->as_server_state
= qc
->as_server
;
425 qc
->default_stream_mode
= SSL_DEFAULT_STREAM_MODE_AUTO_BIDI
;
426 qc
->default_ssl_mode
= qc
->ssl
.ctx
->mode
;
427 qc
->default_ssl_options
= qc
->ssl
.ctx
->options
& OSSL_QUIC_PERMITTED_OPTIONS
;
428 qc
->desires_blocking
= 1;
430 qc
->incoming_stream_policy
= SSL_INCOMING_STREAM_POLICY_AUTO
;
431 qc
->last_error
= SSL_ERROR_NONE
;
433 if (!create_channel(qc
))
436 ossl_quic_channel_set_msg_callback(qc
->ch
, ctx
->msg_callback
, ssl_base
);
437 ossl_quic_channel_set_msg_callback_arg(qc
->ch
, ctx
->msg_callback_arg
);
439 qc_update_reject_policy(qc
);
442 * We do not create the default XSO yet. The reason for this is that the
443 * stream ID of the default XSO will depend on whether the stream is client
444 * or server-initiated, which depends on who transmits first. Since we do
445 * not know whether the application will be using a client-transmits-first
446 * or server-transmits-first protocol, we defer default XSO creation until
447 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
448 * we take that as a cue that the client is expecting a server-initiated
449 * stream, and vice versa if SSL_write() is called first.
454 if (ssl_base
== NULL
) {
455 #if defined(OPENSSL_THREADS)
456 ossl_crypto_mutex_free(qc
->mutex
);
467 void ossl_quic_free(SSL
*s
)
472 /* We should never be called on anything but a QSO. */
473 if (!expect_quic(s
, &ctx
))
480 * When a QSSO is freed, the XSO is freed immediately, because the XSO
481 * itself only contains API personality layer data. However the
482 * underlying QUIC_STREAM is not freed immediately but is instead marked
483 * as deleted for later collection.
486 assert(ctx
.qc
->num_xso
> 0);
489 /* If a stream's send part has not been finished, auto-reset it. */
490 if (( ctx
.xso
->stream
->send_state
== QUIC_SSTREAM_STATE_READY
491 || ctx
.xso
->stream
->send_state
== QUIC_SSTREAM_STATE_SEND
)
492 && !ossl_quic_sstream_get_final_size(ctx
.xso
->stream
->sstream
, NULL
))
493 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
496 /* Do STOP_SENDING for the receive part, if applicable. */
497 if ( ctx
.xso
->stream
->recv_state
== QUIC_RSTREAM_STATE_RECV
498 || ctx
.xso
->stream
->recv_state
== QUIC_RSTREAM_STATE_SIZE_KNOWN
)
499 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
502 /* Update stream state. */
503 ctx
.xso
->stream
->deleted
= 1;
504 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
507 is_default
= (ctx
.xso
== ctx
.qc
->default_xso
);
511 * Unref the connection in most cases; the XSO has a ref to the QC and
512 * not vice versa. But for a default XSO, to avoid circular references,
513 * the QC refs the XSO but the XSO does not ref the QC. If we are the
514 * default XSO, we only get here when the QC is being torn down anyway,
515 * so don't call SSL_free(qc) as we are already in it.
518 SSL_free(&ctx
.qc
->ssl
);
520 /* Note: SSL_free calls OPENSSL_free(xso) for us */
525 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
526 * stage, but is freed during the channel free when the whole QSM is freed.
528 if (ctx
.qc
->default_xso
!= NULL
) {
529 QUIC_XSO
*xso
= ctx
.qc
->default_xso
;
534 ctx
.qc
->default_xso
= NULL
;
537 /* Ensure we have no remaining XSOs. */
538 assert(ctx
.qc
->num_xso
== 0);
540 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
541 if (ctx
.qc
->is_thread_assisted
&& ctx
.qc
->started
) {
542 ossl_quic_thread_assist_wait_stopped(&ctx
.qc
->thread_assist
);
543 ossl_quic_thread_assist_cleanup(&ctx
.qc
->thread_assist
);
547 ossl_quic_channel_free(ctx
.qc
->ch
);
548 ossl_quic_port_free(ctx
.qc
->port
);
549 ossl_quic_engine_free(ctx
.qc
->engine
);
551 BIO_free_all(ctx
.qc
->net_rbio
);
552 BIO_free_all(ctx
.qc
->net_wbio
);
554 /* Note: SSL_free calls OPENSSL_free(qc) for us */
556 SSL_free(ctx
.qc
->tls
);
557 quic_unlock(ctx
.qc
); /* tsan doesn't like freeing locked mutexes */
558 #if defined(OPENSSL_THREADS)
559 ossl_crypto_mutex_free(&ctx
.qc
->mutex
);
563 /* SSL method init */
564 int ossl_quic_init(SSL
*s
)
566 /* Same op as SSL_clear, forward the call. */
567 return ossl_quic_clear(s
);
570 /* SSL method deinit */
571 void ossl_quic_deinit(SSL
*s
)
576 /* SSL_clear (ssl_reset method) */
577 int ossl_quic_reset(SSL
*s
)
581 if (!expect_quic(s
, &ctx
))
584 ERR_raise(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
);
588 /* ssl_clear method (unused) */
589 int ossl_quic_clear(SSL
*s
)
593 if (!expect_quic(s
, &ctx
))
596 ERR_raise(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
);
600 int ossl_quic_conn_set_override_now_cb(SSL
*s
,
601 OSSL_TIME (*now_cb
)(void *arg
),
606 if (!expect_quic(s
, &ctx
))
611 ctx
.qc
->override_now_cb
= now_cb
;
612 ctx
.qc
->override_now_cb_arg
= now_cb_arg
;
618 void ossl_quic_conn_force_assist_thread_wake(SSL
*s
)
622 if (!expect_quic(s
, &ctx
))
625 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
626 if (ctx
.qc
->is_thread_assisted
&& ctx
.qc
->started
)
627 ossl_quic_thread_assist_notify_deadline_changed(&ctx
.qc
->thread_assist
);
632 static void qc_touch_default_xso(QUIC_CONNECTION
*qc
)
634 qc
->default_xso_created
= 1;
635 qc_update_reject_policy(qc
);
639 * Changes default XSO. Allows caller to keep reference to the old default XSO
640 * (if any). Reference to new XSO is transferred from caller.
643 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
,
651 if (qc
->default_xso
!= xso
) {
652 *old_xso
= qc
->default_xso
; /* transfer old XSO ref to caller */
654 qc
->default_xso
= xso
;
658 * Changing to not having a default XSO. XSO becomes standalone and
659 * now has a ref to the QC.
661 if (!ossl_assert(SSL_up_ref(&qc
->ssl
)))
665 * Changing from not having a default XSO to having one. The new XSO
666 * will have had a reference to the QC we need to drop to avoid a
667 * circular reference.
669 * Currently we never change directly from one default XSO to
670 * another, though this function would also still be correct if this
673 assert(*old_xso
== NULL
);
675 CRYPTO_DOWN_REF(&qc
->ssl
.references
, &refs
);
681 qc_touch_default_xso(qc
);
685 * Changes default XSO, releasing the reference to any previous default XSO.
686 * Reference to new XSO is transferred from caller.
689 static void qc_set_default_xso(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
, int touch
)
691 QUIC_XSO
*old_xso
= NULL
;
693 qc_set_default_xso_keep_ref(qc
, xso
, touch
, &old_xso
);
696 SSL_free(&old_xso
->ssl
);
700 static void xso_update_options(QUIC_XSO
*xso
)
702 int cleanse
= ((xso
->ssl_options
& SSL_OP_CLEANSE_PLAINTEXT
) != 0);
704 if (xso
->stream
->rstream
!= NULL
)
705 ossl_quic_rstream_set_cleanse(xso
->stream
->rstream
, cleanse
);
707 if (xso
->stream
->sstream
!= NULL
)
708 ossl_quic_sstream_set_cleanse(xso
->stream
->sstream
, cleanse
);
715 * Setting options on a QCSO
716 * - configures the handshake-layer options;
717 * - configures the default data-plane options for new streams;
718 * - configures the data-plane options on the default XSO, if there is one.
720 * Setting options on a QSSO
721 * - configures data-plane options for that stream only.
724 static uint64_t quic_mask_or_options(SSL
*ssl
, uint64_t mask_value
, uint64_t or_value
)
727 uint64_t hs_mask_value
, hs_or_value
, ret
;
729 if (!expect_quic(ssl
, &ctx
))
734 if (!ctx
.is_stream
) {
736 * If we were called on the connection, we apply any handshake option
739 hs_mask_value
= (mask_value
& OSSL_QUIC_PERMITTED_OPTIONS_CONN
);
740 hs_or_value
= (or_value
& OSSL_QUIC_PERMITTED_OPTIONS_CONN
);
742 SSL_clear_options(ctx
.qc
->tls
, hs_mask_value
);
743 SSL_set_options(ctx
.qc
->tls
, hs_or_value
);
745 /* Update defaults for new streams. */
746 ctx
.qc
->default_ssl_options
747 = ((ctx
.qc
->default_ssl_options
& ~mask_value
) | or_value
)
748 & OSSL_QUIC_PERMITTED_OPTIONS
;
751 if (ctx
.xso
!= NULL
) {
753 = ((ctx
.xso
->ssl_options
& ~mask_value
) | or_value
)
754 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM
;
756 xso_update_options(ctx
.xso
);
759 ret
= ctx
.is_stream
? ctx
.xso
->ssl_options
: ctx
.qc
->default_ssl_options
;
765 uint64_t ossl_quic_set_options(SSL
*ssl
, uint64_t options
)
767 return quic_mask_or_options(ssl
, 0, options
);
770 /* SSL_clear_options */
771 uint64_t ossl_quic_clear_options(SSL
*ssl
, uint64_t options
)
773 return quic_mask_or_options(ssl
, options
, 0);
776 /* SSL_get_options */
777 uint64_t ossl_quic_get_options(const SSL
*ssl
)
779 return quic_mask_or_options((SSL
*)ssl
, 0, 0);
783 * QUIC Front-End I/O API: Network BIO Configuration
784 * =================================================
786 * Handling the different BIOs is difficult:
788 * - It is more or less a requirement that we use non-blocking network I/O;
789 * we need to be able to have timeouts on recv() calls, and make best effort
790 * (non blocking) send() and recv() calls.
792 * The only sensible way to do this is to configure the socket into
793 * non-blocking mode. We could try to do select() before calling send() or
794 * recv() to get a guarantee that the call will not block, but this will
795 * probably run into issues with buggy OSes which generate spurious socket
796 * readiness events. In any case, relying on this to work reliably does not
799 * Timeouts could be handled via setsockopt() socket timeout options, but
800 * this depends on OS support and adds another syscall to every network I/O
801 * operation. It also has obvious thread safety concerns if we want to move
802 * to concurrent use of a single socket at some later date.
804 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
805 * be made non-blocking. However some OSes (e.g. Windows) do not support
806 * this, so we cannot rely on this.
808 * As such, we need to configure any FD in non-blocking mode. This may
809 * confound users who pass a blocking socket to libssl. However, in practice
810 * it would be extremely strange for a user of QUIC to pass an FD to us,
811 * then also try and send receive traffic on the same socket(!). Thus the
812 * impact of this should be limited, and can be documented.
814 * - We support both blocking and non-blocking operation in terms of the API
815 * presented to the user. One prospect is to set the blocking mode based on
816 * whether the socket passed to us was already in blocking mode. However,
817 * Windows has no API for determining if a socket is in blocking mode (!),
818 * therefore this cannot be done portably. Currently therefore we expose an
819 * explicit API call to set this, and default to blocking mode.
821 * - We need to determine our initial destination UDP address. The "natural"
822 * way for a user to do this is to set the peer variable on a BIO_dgram.
823 * However, this has problems because BIO_dgram's peer variable is used for
824 * both transmission and reception. This means it can be constantly being
825 * changed to a malicious value (e.g. if some random unrelated entity on the
826 * network starts sending traffic to us) on every read call. This is not a
827 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
828 * calls only, which do not use this variable. However, we do need to let
829 * the user specify the peer in a 'normal' manner. The compromise here is
830 * that we grab the current peer value set at the time the write BIO is set
831 * and do not read the value again.
833 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
834 * Currently we do this by only supporting non-blocking mode.
839 * Determines what initial destination UDP address we should use, if possible.
840 * If this fails the client must set the destination address manually, or use a
841 * BIO which does not need a destination address.
843 static int csm_analyse_init_peer_addr(BIO
*net_wbio
, BIO_ADDR
*peer
)
845 if (BIO_dgram_detect_peer_addr(net_wbio
, peer
) <= 0)
851 static int qc_can_support_blocking_cached(QUIC_CONNECTION
*qc
)
853 QUIC_REACTOR
*rtor
= ossl_quic_channel_get_reactor(qc
->ch
);
855 return ossl_quic_reactor_can_poll_r(rtor
)
856 && ossl_quic_reactor_can_poll_w(rtor
);
859 static void qc_update_can_support_blocking(QUIC_CONNECTION
*qc
)
861 ossl_quic_port_update_poll_descriptors(qc
->port
); /* best effort */
864 static void qc_update_blocking_mode(QUIC_CONNECTION
*qc
)
866 qc
->blocking
= qc
->desires_blocking
&& qc_can_support_blocking_cached(qc
);
869 void ossl_quic_conn_set0_net_rbio(SSL
*s
, BIO
*net_rbio
)
873 if (!expect_quic(s
, &ctx
))
876 if (ctx
.qc
->net_rbio
== net_rbio
)
879 if (!ossl_quic_port_set_net_rbio(ctx
.qc
->port
, net_rbio
))
882 BIO_free_all(ctx
.qc
->net_rbio
);
883 ctx
.qc
->net_rbio
= net_rbio
;
885 if (net_rbio
!= NULL
)
886 BIO_set_nbio(net_rbio
, 1); /* best effort autoconfig */
889 * Determine if the current pair of read/write BIOs now set allows blocking
890 * mode to be supported.
892 qc_update_can_support_blocking(ctx
.qc
);
893 qc_update_blocking_mode(ctx
.qc
);
896 void ossl_quic_conn_set0_net_wbio(SSL
*s
, BIO
*net_wbio
)
900 if (!expect_quic(s
, &ctx
))
903 if (ctx
.qc
->net_wbio
== net_wbio
)
906 if (!ossl_quic_port_set_net_wbio(ctx
.qc
->port
, net_wbio
))
909 BIO_free_all(ctx
.qc
->net_wbio
);
910 ctx
.qc
->net_wbio
= net_wbio
;
912 if (net_wbio
!= NULL
)
913 BIO_set_nbio(net_wbio
, 1); /* best effort autoconfig */
916 * Determine if the current pair of read/write BIOs now set allows blocking
917 * mode to be supported.
919 qc_update_can_support_blocking(ctx
.qc
);
920 qc_update_blocking_mode(ctx
.qc
);
923 BIO
*ossl_quic_conn_get_net_rbio(const SSL
*s
)
927 if (!expect_quic(s
, &ctx
))
930 return ctx
.qc
->net_rbio
;
933 BIO
*ossl_quic_conn_get_net_wbio(const SSL
*s
)
937 if (!expect_quic(s
, &ctx
))
940 return ctx
.qc
->net_wbio
;
943 int ossl_quic_conn_get_blocking_mode(const SSL
*s
)
947 if (!expect_quic(s
, &ctx
))
951 return xso_blocking_mode(ctx
.xso
);
953 return qc_blocking_mode(ctx
.qc
);
957 int ossl_quic_conn_set_blocking_mode(SSL
*s
, int blocking
)
962 if (!expect_quic(s
, &ctx
))
967 /* Sanity check - can we support the request given the current network BIO? */
970 * If called directly on a QCSO, update our information on network BIO
974 qc_update_can_support_blocking(ctx
.qc
);
976 /* Cannot enable blocking mode if we do not have pollable FDs. */
977 if (!qc_can_support_blocking_cached(ctx
.qc
)) {
978 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_UNSUPPORTED
, NULL
);
985 * If called directly on a QCSO, update default and connection-level
988 ctx
.qc
->desires_blocking
= (blocking
!= 0);
990 if (ctx
.xso
!= NULL
) {
992 * If called on a QSSO or a QCSO with a default XSO, update the blocking
995 ctx
.xso
->desires_blocking
= (blocking
!= 0);
996 ctx
.xso
->desires_blocking_set
= 1;
1001 qc_update_blocking_mode(ctx
.qc
);
1002 quic_unlock(ctx
.qc
);
1006 int ossl_quic_conn_set_initial_peer_addr(SSL
*s
,
1007 const BIO_ADDR
*peer_addr
)
1011 if (!expect_quic(s
, &ctx
))
1014 if (ctx
.qc
->started
)
1015 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
1018 if (peer_addr
== NULL
) {
1019 BIO_ADDR_clear(&ctx
.qc
->init_peer_addr
);
1023 ctx
.qc
->init_peer_addr
= *peer_addr
;
1028 * QUIC Front-End I/O API: Asynchronous I/O Management
1029 * ===================================================
1031 * (BIO/)SSL_handle_events => ossl_quic_handle_events
1032 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
1033 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
1037 /* Returns 1 if the connection is being used in blocking mode. */
1038 static int qc_blocking_mode(const QUIC_CONNECTION
*qc
)
1040 return qc
->blocking
;
1043 static int xso_blocking_mode(const QUIC_XSO
*xso
)
1045 if (xso
->desires_blocking_set
)
1046 return xso
->desires_blocking
&& qc_can_support_blocking_cached(xso
->conn
);
1048 /* Only ever set if we can support blocking. */
1049 return xso
->conn
->blocking
;
1052 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
1054 int ossl_quic_handle_events(SSL
*s
)
1058 if (!expect_quic(s
, &ctx
))
1062 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1063 quic_unlock(ctx
.qc
);
1068 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1069 * should next have events handled by the application by calling
1070 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1071 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1072 * the value of *tv is undefined.
1075 int ossl_quic_get_event_timeout(SSL
*s
, struct timeval
*tv
, int *is_infinite
)
1078 OSSL_TIME deadline
= ossl_time_infinite();
1080 if (!expect_quic(s
, &ctx
))
1086 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1088 if (ossl_time_is_infinite(deadline
)) {
1092 * Robustness against faulty applications that don't check *is_infinite;
1093 * harmless long timeout.
1095 tv
->tv_sec
= 1000000;
1098 quic_unlock(ctx
.qc
);
1102 *tv
= ossl_time_to_timeval(ossl_time_subtract(deadline
, get_time(ctx
.qc
)));
1104 quic_unlock(ctx
.qc
);
1108 /* SSL_get_rpoll_descriptor */
1109 int ossl_quic_get_rpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
1113 if (!expect_quic(s
, &ctx
))
1116 if (desc
== NULL
|| ctx
.qc
->net_rbio
== NULL
)
1117 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
1120 return BIO_get_rpoll_descriptor(ctx
.qc
->net_rbio
, desc
);
1123 /* SSL_get_wpoll_descriptor */
1124 int ossl_quic_get_wpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
1128 if (!expect_quic(s
, &ctx
))
1131 if (desc
== NULL
|| ctx
.qc
->net_wbio
== NULL
)
1132 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
1135 return BIO_get_wpoll_descriptor(ctx
.qc
->net_wbio
, desc
);
1138 /* SSL_net_read_desired */
1140 int ossl_quic_get_net_read_desired(SSL
*s
)
1145 if (!expect_quic(s
, &ctx
))
1149 ret
= ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1150 quic_unlock(ctx
.qc
);
1154 /* SSL_net_write_desired */
1156 int ossl_quic_get_net_write_desired(SSL
*s
)
1161 if (!expect_quic(s
, &ctx
))
1165 ret
= ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1166 quic_unlock(ctx
.qc
);
1171 * QUIC Front-End I/O API: Connection Lifecycle Operations
1172 * =======================================================
1174 * SSL_do_handshake => ossl_quic_do_handshake
1175 * SSL_set_connect_state => ossl_quic_set_connect_state
1176 * SSL_set_accept_state => ossl_quic_set_accept_state
1177 * SSL_shutdown => ossl_quic_shutdown
1178 * SSL_ctrl => ossl_quic_ctrl
1179 * (BIO/)SSL_connect => ossl_quic_connect
1180 * (BIO/)SSL_accept => ossl_quic_accept
1185 static void qc_shutdown_flush_init(QUIC_CONNECTION
*qc
)
1187 QUIC_STREAM_MAP
*qsm
;
1189 if (qc
->shutting_down
)
1192 qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
1194 ossl_quic_stream_map_begin_shutdown_flush(qsm
);
1195 qc
->shutting_down
= 1;
1198 /* Returns 1 if all shutdown-flush streams have been done with. */
1200 static int qc_shutdown_flush_finished(QUIC_CONNECTION
*qc
)
1202 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
1204 return qc
->shutting_down
1205 && ossl_quic_stream_map_is_shutdown_flush_finished(qsm
);
1209 static int quic_shutdown_wait(void *arg
)
1211 QUIC_CONNECTION
*qc
= arg
;
1213 return ossl_quic_channel_is_terminated(qc
->ch
);
1216 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
1217 static int quic_shutdown_flush_wait(void *arg
)
1219 QUIC_CONNECTION
*qc
= arg
;
1221 return ossl_quic_channel_is_term_any(qc
->ch
)
1222 || qc_shutdown_flush_finished(qc
);
1225 static int quic_shutdown_peer_wait(void *arg
)
1227 QUIC_CONNECTION
*qc
= arg
;
1228 return ossl_quic_channel_is_term_any(qc
->ch
);
1232 int ossl_quic_conn_shutdown(SSL
*s
, uint64_t flags
,
1233 const SSL_SHUTDOWN_EX_ARGS
*args
,
1238 int stream_flush
= ((flags
& SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH
) == 0);
1239 int no_block
= ((flags
& SSL_SHUTDOWN_FLAG_NO_BLOCK
) != 0);
1240 int wait_peer
= ((flags
& SSL_SHUTDOWN_FLAG_WAIT_PEER
) != 0);
1242 if (!expect_quic(s
, &ctx
))
1245 if (ctx
.is_stream
) {
1246 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_CONN_USE_ONLY
, NULL
);
1252 if (ossl_quic_channel_is_terminated(ctx
.qc
->ch
)) {
1253 quic_unlock(ctx
.qc
);
1257 /* Phase 1: Stream Flushing */
1258 if (!wait_peer
&& stream_flush
) {
1259 qc_shutdown_flush_init(ctx
.qc
);
1261 if (!qc_shutdown_flush_finished(ctx
.qc
)) {
1262 if (!no_block
&& qc_blocking_mode(ctx
.qc
)) {
1263 ret
= block_until_pred(ctx
.qc
, quic_shutdown_flush_wait
, ctx
.qc
, 0);
1269 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1273 if (!qc_shutdown_flush_finished(ctx
.qc
)) {
1274 quic_unlock(ctx
.qc
);
1275 return 0; /* ongoing */
1279 /* Phase 2: Connection Closure */
1280 if (wait_peer
&& !ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
1281 if (!no_block
&& qc_blocking_mode(ctx
.qc
)) {
1282 ret
= block_until_pred(ctx
.qc
, quic_shutdown_peer_wait
, ctx
.qc
, 0);
1288 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1291 if (!ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
1292 ret
= 0; /* peer hasn't closed yet - still not done */
1297 * We are at least terminating - go through the normal process of
1298 * waiting until we are in the TERMINATED state.
1302 /* Block mutation ops regardless of if we did stream flush. */
1303 ctx
.qc
->shutting_down
= 1;
1306 * This call is a no-op if we are already terminating, so it doesn't
1307 * affect the wait_peer case.
1309 ossl_quic_channel_local_close(ctx
.qc
->ch
,
1310 args
!= NULL
? args
->quic_error_code
: 0,
1311 args
!= NULL
? args
->quic_reason
: NULL
);
1313 SSL_set_shutdown(ctx
.qc
->tls
, SSL_SENT_SHUTDOWN
);
1315 if (ossl_quic_channel_is_terminated(ctx
.qc
->ch
)) {
1316 quic_unlock(ctx
.qc
);
1320 /* Phase 3: Terminating Wait Time */
1321 if (!no_block
&& qc_blocking_mode(ctx
.qc
)
1322 && (flags
& SSL_SHUTDOWN_FLAG_RAPID
) == 0) {
1323 ret
= block_until_pred(ctx
.qc
, quic_shutdown_wait
, ctx
.qc
, 0);
1329 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1332 ret
= ossl_quic_channel_is_terminated(ctx
.qc
->ch
);
1334 quic_unlock(ctx
.qc
);
1339 long ossl_quic_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
1343 if (!expect_quic(s
, &ctx
))
1348 /* If called on a QCSO, update the default mode. */
1350 ctx
.qc
->default_ssl_mode
|= (uint32_t)larg
;
1353 * If we were called on a QSSO or have a default stream, we also update
1356 if (ctx
.xso
!= NULL
) {
1357 /* Cannot enable EPW while AON write in progress. */
1358 if (ctx
.xso
->aon_write_in_progress
)
1359 larg
&= ~SSL_MODE_ENABLE_PARTIAL_WRITE
;
1361 ctx
.xso
->ssl_mode
|= (uint32_t)larg
;
1362 return ctx
.xso
->ssl_mode
;
1365 return ctx
.qc
->default_ssl_mode
;
1366 case SSL_CTRL_CLEAR_MODE
:
1368 ctx
.qc
->default_ssl_mode
&= ~(uint32_t)larg
;
1370 if (ctx
.xso
!= NULL
) {
1371 ctx
.xso
->ssl_mode
&= ~(uint32_t)larg
;
1372 return ctx
.xso
->ssl_mode
;
1375 return ctx
.qc
->default_ssl_mode
;
1377 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1378 ossl_quic_channel_set_msg_callback_arg(ctx
.qc
->ch
, parg
);
1379 /* This ctrl also needs to be passed to the internal SSL object */
1380 return SSL_ctrl(ctx
.qc
->tls
, cmd
, larg
, parg
);
1382 case DTLS_CTRL_GET_TIMEOUT
: /* DTLSv1_get_timeout */
1386 if (!ossl_quic_get_event_timeout(s
, parg
, &is_infinite
))
1389 return !is_infinite
;
1391 case DTLS_CTRL_HANDLE_TIMEOUT
: /* DTLSv1_handle_timeout */
1392 /* For legacy compatibility with DTLS calls. */
1393 return ossl_quic_handle_events(s
) == 1 ? 1 : -1;
1395 /* Mask ctrls we shouldn't support for QUIC. */
1396 case SSL_CTRL_GET_READ_AHEAD
:
1397 case SSL_CTRL_SET_READ_AHEAD
:
1398 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1399 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
1400 case SSL_CTRL_SET_MAX_PIPELINES
:
1405 * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1406 * implementation. Either SSL_ctrl will handle it itself by direct
1407 * access into handshake layer state, or failing that, it will be passed
1408 * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1409 * supported by anything, the handshake layer's ctrl method will finally
1412 return ossl_ctrl_internal(&ctx
.qc
->ssl
, cmd
, larg
, parg
, /*no_quic=*/1);
1416 /* SSL_set_connect_state */
1417 void ossl_quic_set_connect_state(SSL
*s
)
1421 if (!expect_quic(s
, &ctx
))
1424 /* Cannot be changed after handshake started */
1425 if (ctx
.qc
->started
|| ctx
.is_stream
)
1428 ctx
.qc
->as_server_state
= 0;
1431 /* SSL_set_accept_state */
1432 void ossl_quic_set_accept_state(SSL
*s
)
1436 if (!expect_quic(s
, &ctx
))
1439 /* Cannot be changed after handshake started */
1440 if (ctx
.qc
->started
|| ctx
.is_stream
)
1443 ctx
.qc
->as_server_state
= 1;
1446 /* SSL_do_handshake */
1447 struct quic_handshake_wait_args
{
1448 QUIC_CONNECTION
*qc
;
1451 static int tls_wants_non_io_retry(QUIC_CONNECTION
*qc
)
1453 int want
= SSL_want(qc
->tls
);
1455 if (want
== SSL_X509_LOOKUP
1456 || want
== SSL_CLIENT_HELLO_CB
1457 || want
== SSL_RETRY_VERIFY
)
1463 static int quic_handshake_wait(void *arg
)
1465 struct quic_handshake_wait_args
*args
= arg
;
1467 if (!quic_mutation_allowed(args
->qc
, /*req_active=*/1))
1470 if (ossl_quic_channel_is_handshake_complete(args
->qc
->ch
))
1473 if (tls_wants_non_io_retry(args
->qc
))
1479 static int configure_channel(QUIC_CONNECTION
*qc
)
1481 assert(qc
->ch
!= NULL
);
1483 if (!ossl_quic_port_set_net_rbio(qc
->port
, qc
->net_rbio
)
1484 || !ossl_quic_port_set_net_wbio(qc
->port
, qc
->net_wbio
)
1485 || !ossl_quic_channel_set_peer_addr(qc
->ch
, &qc
->init_peer_addr
))
1492 static int create_channel(QUIC_CONNECTION
*qc
)
1494 QUIC_ENGINE_ARGS engine_args
= {0};
1495 QUIC_PORT_ARGS port_args
= {0};
1497 engine_args
.libctx
= qc
->ssl
.ctx
->libctx
;
1498 engine_args
.propq
= qc
->ssl
.ctx
->propq
;
1499 engine_args
.mutex
= qc
->mutex
;
1500 engine_args
.now_cb
= get_time_cb
;
1501 engine_args
.now_cb_arg
= qc
;
1502 qc
->engine
= ossl_quic_engine_new(&engine_args
);
1503 if (qc
->engine
== NULL
) {
1504 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
1508 port_args
.channel_ctx
= qc
->ssl
.ctx
;
1509 qc
->port
= ossl_quic_engine_create_port(qc
->engine
, &port_args
);
1510 if (qc
->port
== NULL
) {
1511 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
1512 ossl_quic_engine_free(qc
->engine
);
1516 qc
->ch
= ossl_quic_port_create_outgoing(qc
->port
, qc
->tls
);
1517 if (qc
->ch
== NULL
) {
1518 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
1519 ossl_quic_port_free(qc
->port
);
1520 ossl_quic_engine_free(qc
->engine
);
1528 * Configures a channel with the information we have accumulated via calls made
1529 * to us from the application prior to starting a handshake attempt.
1532 static int ensure_channel_started(QCTX
*ctx
)
1534 QUIC_CONNECTION
*qc
= ctx
->qc
;
1537 if (!configure_channel(qc
)) {
1538 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
,
1539 "failed to configure channel");
1543 if (!ossl_quic_channel_start(qc
->ch
)) {
1544 ossl_quic_channel_restore_err_state(qc
->ch
);
1545 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
,
1546 "failed to start channel");
1550 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1551 if (qc
->is_thread_assisted
)
1552 if (!ossl_quic_thread_assist_init_start(&qc
->thread_assist
, qc
->ch
,
1553 qc
->override_now_cb
,
1554 qc
->override_now_cb_arg
)) {
1555 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
,
1556 "failed to start assist thread");
1567 static int quic_do_handshake(QCTX
*ctx
)
1570 QUIC_CONNECTION
*qc
= ctx
->qc
;
1572 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1573 /* Handshake already completed. */
1576 if (!quic_mutation_allowed(qc
, /*req_active=*/0))
1577 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1579 if (qc
->as_server
!= qc
->as_server_state
) {
1580 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
1581 return -1; /* Non-protocol error */
1584 if (qc
->net_rbio
== NULL
|| qc
->net_wbio
== NULL
) {
1585 /* Need read and write BIOs. */
1586 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_BIO_NOT_SET
, NULL
);
1587 return -1; /* Non-protocol error */
1591 * We need to determine our addressing mode. There are basically two
1592 * ways we can use L4 addresses:
1594 * - Addressed mode, in which our BIO_sendmmsg calls have destination
1595 * addresses attached to them which we expect the underlying network BIO
1598 * - Unaddressed mode, in which the BIO provided to us on the
1599 * network side neither provides us with L4 addresses nor is capable of
1600 * honouring ones we provide. We don't know where the QUIC traffic we
1601 * send ends up exactly and trust the application to know what it is
1604 * Addressed mode is preferred because it enables support for connection
1605 * migration, multipath, etc. in the future. Addressed mode is automatically
1606 * enabled if we are using e.g. BIO_s_datagram, with or without
1609 * If we are passed a BIO_s_dgram_pair (or some custom BIO) we may have to
1610 * use unaddressed mode unless that BIO supports capability flags indicating
1611 * it can provide and honour L4 addresses.
1613 * Our strategy for determining address mode is simple: we probe the
1614 * underlying network BIOs for their capabilities. If the network BIOs
1615 * support what we need, we use addressed mode. Otherwise, we use
1618 * If addressed mode is chosen, we require an initial peer address to be
1619 * set. If this is not set, we fail. If unaddressed mode is used, we do not
1620 * require this, as such an address is superfluous, though it can be set if
1623 if (!qc
->started
&& !qc
->addressing_probe_done
) {
1624 long rcaps
= BIO_dgram_get_effective_caps(qc
->net_rbio
);
1625 long wcaps
= BIO_dgram_get_effective_caps(qc
->net_wbio
);
1627 qc
->addressed_mode_r
= ((rcaps
& BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
) != 0);
1628 qc
->addressed_mode_w
= ((wcaps
& BIO_DGRAM_CAP_HANDLES_DST_ADDR
) != 0);
1629 qc
->addressing_probe_done
= 1;
1632 if (!qc
->started
&& qc
->addressed_mode_w
1633 && BIO_ADDR_family(&qc
->init_peer_addr
) == AF_UNSPEC
) {
1635 * We are trying to connect and are using addressed mode, which means we
1636 * need an initial peer address; if we do not have a peer address yet,
1637 * we should try to autodetect one.
1639 * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
1640 * may not be able to provide us with a peer address until they have
1641 * finished their own processing. They may not be able to perform this
1642 * processing until an application has finished configuring that BIO
1643 * (e.g. with setter calls), which might happen after SSL_set_bio is
1646 if (!csm_analyse_init_peer_addr(qc
->net_wbio
, &qc
->init_peer_addr
))
1648 BIO_ADDR_clear(&qc
->init_peer_addr
);
1650 ossl_quic_channel_set_peer_addr(qc
->ch
, &qc
->init_peer_addr
);
1654 && qc
->addressed_mode_w
1655 && BIO_ADDR_family(&qc
->init_peer_addr
) == AF_UNSPEC
) {
1657 * If we still don't have a peer address in addressed mode, we can't do
1660 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET
, NULL
);
1661 return -1; /* Non-protocol error */
1665 * Start connection process. Note we may come here multiple times in
1666 * non-blocking mode, which is fine.
1668 if (!ensure_channel_started(ctx
)) /* raises on failure */
1669 return -1; /* Non-protocol error */
1671 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1672 /* The handshake is now done. */
1675 if (!qc_blocking_mode(qc
)) {
1676 /* Try to advance the reactor. */
1677 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc
->ch
), 0);
1679 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1680 /* The handshake is now done. */
1683 if (ossl_quic_channel_is_term_any(qc
->ch
)) {
1684 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1686 } else if (qc
->desires_blocking
) {
1688 * As a special case when doing a handshake when blocking mode is
1689 * desired yet not available, see if the network BIOs have become
1690 * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1691 * which do late creation of socket FDs and therefore cannot expose
1692 * a poll descriptor until after a network BIO is set on the QCSO.
1694 assert(!qc
->blocking
);
1695 qc_update_can_support_blocking(qc
);
1696 qc_update_blocking_mode(qc
);
1701 * We are either in blocking mode or just entered it due to the code above.
1703 if (qc_blocking_mode(qc
)) {
1704 /* In blocking mode, wait for the handshake to complete. */
1705 struct quic_handshake_wait_args args
;
1709 ret
= block_until_pred(qc
, quic_handshake_wait
, &args
, 0);
1710 if (!quic_mutation_allowed(qc
, /*req_active=*/1)) {
1711 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1712 return 0; /* Shutdown before completion */
1713 } else if (ret
<= 0) {
1714 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1715 return -1; /* Non-protocol error */
1718 if (tls_wants_non_io_retry(qc
)) {
1719 QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_get_error(qc
->tls
, 0));
1723 assert(ossl_quic_channel_is_handshake_complete(qc
->ch
));
1727 if (tls_wants_non_io_retry(qc
)) {
1728 QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_get_error(qc
->tls
, 0));
1733 * Otherwise, indicate that the handshake isn't done yet.
1734 * We can only get here in non-blocking mode.
1736 QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_READ
);
1737 return -1; /* Non-protocol error */
1741 int ossl_quic_do_handshake(SSL
*s
)
1746 if (!expect_quic(s
, &ctx
))
1749 quic_lock_for_io(&ctx
);
1751 ret
= quic_do_handshake(&ctx
);
1752 quic_unlock(ctx
.qc
);
1757 int ossl_quic_connect(SSL
*s
)
1759 /* Ensure we are in connect state (no-op if non-idle). */
1760 ossl_quic_set_connect_state(s
);
1762 /* Begin or continue the handshake */
1763 return ossl_quic_do_handshake(s
);
1767 int ossl_quic_accept(SSL
*s
)
1769 /* Ensure we are in accept state (no-op if non-idle). */
1770 ossl_quic_set_accept_state(s
);
1772 /* Begin or continue the handshake */
1773 return ossl_quic_do_handshake(s
);
1777 * QUIC Front-End I/O API: Stream Lifecycle Operations
1778 * ===================================================
1780 * SSL_stream_new => ossl_quic_conn_stream_new
1785 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1786 * default XSO was created. Returns 0 if it was not (e.g. because it already
1787 * exists). Note that this is NOT an error condition.
1790 static int qc_try_create_default_xso_for_write(QCTX
*ctx
)
1793 QUIC_CONNECTION
*qc
= ctx
->qc
;
1795 if (qc
->default_xso_created
1796 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
1798 * We only do this once. If the user detaches a previously created
1799 * default XSO we don't auto-create another one.
1801 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
1803 /* Create a locally-initiated stream. */
1804 if (qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_AUTO_UNI
)
1805 flags
|= SSL_STREAM_FLAG_UNI
;
1807 qc_set_default_xso(qc
, (QUIC_XSO
*)quic_conn_stream_new(ctx
, flags
,
1810 if (qc
->default_xso
== NULL
)
1811 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1813 qc_touch_default_xso(qc
);
1817 struct quic_wait_for_stream_args
{
1818 QUIC_CONNECTION
*qc
;
1825 static int quic_wait_for_stream(void *arg
)
1827 struct quic_wait_for_stream_args
*args
= arg
;
1829 if (!quic_mutation_allowed(args
->qc
, /*req_active=*/1)) {
1830 /* If connection is torn down due to an error while blocking, stop. */
1831 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1835 args
->qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args
->qc
->ch
),
1836 args
->expect_id
| QUIC_STREAM_DIR_BIDI
);
1837 if (args
->qs
== NULL
)
1838 args
->qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args
->qc
->ch
),
1839 args
->expect_id
| QUIC_STREAM_DIR_UNI
);
1841 if (args
->qs
!= NULL
)
1842 return 1; /* stream now exists */
1844 return 0; /* did not get a stream, keep trying */
1848 static int qc_wait_for_default_xso_for_read(QCTX
*ctx
)
1850 /* Called on a QCSO and we don't currently have a default stream. */
1852 QUIC_CONNECTION
*qc
= ctx
->qc
;
1855 struct quic_wait_for_stream_args wargs
;
1856 OSSL_RTT_INFO rtt_info
;
1859 * If default stream functionality is disabled or we already detached
1860 * one, don't make another default stream and just fail.
1862 if (qc
->default_xso_created
1863 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
1864 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
1867 * The peer may have opened a stream since we last ticked. So tick and
1868 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1869 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1870 * first stream created by a peer must have an ordinal of 0.
1872 expect_id
= qc
->as_server
1873 ? QUIC_STREAM_INITIATOR_CLIENT
1874 : QUIC_STREAM_INITIATOR_SERVER
;
1876 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1877 expect_id
| QUIC_STREAM_DIR_BIDI
);
1879 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1880 expect_id
| QUIC_STREAM_DIR_UNI
);
1883 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc
->ch
), 0);
1885 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1890 if (!qc_blocking_mode(qc
))
1891 /* Non-blocking mode, so just bail immediately. */
1892 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_READ
);
1894 /* Block until we have a stream. */
1898 wargs
.expect_id
= expect_id
;
1900 res
= block_until_pred(qc
, quic_wait_for_stream
, &wargs
, 0);
1902 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1903 else if (res
< 0 || wargs
.qs
== NULL
)
1904 /* quic_wait_for_stream raised error here */
1911 * We now have qs != NULL. Remove it from the incoming stream queue so that
1912 * it isn't also returned by any future SSL_accept_stream calls.
1914 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc
->ch
), &rtt_info
);
1915 ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc
->ch
),
1916 qs
, rtt_info
.smoothed_rtt
);
1919 * Now make qs the default stream, creating the necessary XSO.
1921 qc_set_default_xso(qc
, create_xso_from_stream(qc
, qs
), /*touch=*/0);
1922 if (qc
->default_xso
== NULL
)
1923 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1925 qc_touch_default_xso(qc
); /* inhibits default XSO */
1930 static QUIC_XSO
*create_xso_from_stream(QUIC_CONNECTION
*qc
, QUIC_STREAM
*qs
)
1932 QUIC_XSO
*xso
= NULL
;
1934 if ((xso
= OPENSSL_zalloc(sizeof(*xso
))) == NULL
) {
1935 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_CRYPTO_LIB
, NULL
);
1939 if (!ossl_ssl_init(&xso
->ssl
, qc
->ssl
.ctx
, qc
->ssl
.method
, SSL_TYPE_QUIC_XSO
)) {
1940 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
1945 if (!SSL_up_ref(&qc
->ssl
)) {
1946 QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_SSL_LIB
, NULL
);
1951 xso
->ssl_mode
= qc
->default_ssl_mode
;
1953 = qc
->default_ssl_options
& OSSL_QUIC_PERMITTED_OPTIONS_STREAM
;
1954 xso
->last_error
= SSL_ERROR_NONE
;
1959 xso_update_options(xso
);
1967 struct quic_new_stream_wait_args
{
1968 QUIC_CONNECTION
*qc
;
1972 static int quic_new_stream_wait(void *arg
)
1974 struct quic_new_stream_wait_args
*args
= arg
;
1975 QUIC_CONNECTION
*qc
= args
->qc
;
1977 if (!quic_mutation_allowed(qc
, /*req_active=*/1))
1980 if (ossl_quic_channel_is_new_local_stream_admissible(qc
->ch
, args
->is_uni
))
1986 /* locking depends on need_lock */
1987 static SSL
*quic_conn_stream_new(QCTX
*ctx
, uint64_t flags
, int need_lock
)
1990 QUIC_CONNECTION
*qc
= ctx
->qc
;
1991 QUIC_XSO
*xso
= NULL
;
1992 QUIC_STREAM
*qs
= NULL
;
1993 int is_uni
= ((flags
& SSL_STREAM_FLAG_UNI
) != 0);
1994 int no_blocking
= ((flags
& SSL_STREAM_FLAG_NO_BLOCK
) != 0);
1995 int advance
= ((flags
& SSL_STREAM_FLAG_ADVANCE
) != 0);
2000 if (!quic_mutation_allowed(qc
, /*req_active=*/0)) {
2001 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2006 && !ossl_quic_channel_is_new_local_stream_admissible(qc
->ch
, is_uni
)) {
2007 struct quic_new_stream_wait_args args
;
2010 * Stream count flow control currently doesn't permit this stream to be
2013 if (no_blocking
|| !qc_blocking_mode(qc
)) {
2014 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_STREAM_COUNT_LIMITED
, NULL
);
2019 args
.is_uni
= is_uni
;
2021 /* Blocking mode - wait until we can get a stream. */
2022 ret
= block_until_pred(ctx
->qc
, quic_new_stream_wait
, &args
, 0);
2023 if (!quic_mutation_allowed(qc
, /*req_active=*/1)) {
2024 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2025 goto err
; /* Shutdown before completion */
2026 } else if (ret
<= 0) {
2027 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2028 goto err
; /* Non-protocol error */
2032 qs
= ossl_quic_channel_new_stream_local(qc
->ch
, is_uni
);
2034 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2038 xso
= create_xso_from_stream(qc
, qs
);
2042 qc_touch_default_xso(qc
); /* inhibits default XSO */
2050 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc
->ch
), qs
);
2059 SSL
*ossl_quic_conn_stream_new(SSL
*s
, uint64_t flags
)
2063 if (!expect_quic_conn_only(s
, &ctx
))
2066 return quic_conn_stream_new(&ctx
, flags
, /*need_lock=*/1);
2070 * QUIC Front-End I/O API: Steady-State Operations
2071 * ===============================================
2073 * Here we dispatch calls to the steady-state front-end I/O API functions; that
2074 * is, the functions used during the established phase of a QUIC connection
2075 * (e.g. SSL_read, SSL_write).
2077 * Each function must handle both blocking and non-blocking modes. As discussed
2078 * above, all QUIC I/O is implemented using non-blocking mode internally.
2080 * SSL_get_error => partially implemented by ossl_quic_get_error
2081 * SSL_want => ossl_quic_want
2082 * (BIO/)SSL_read => ossl_quic_read
2083 * (BIO/)SSL_write => ossl_quic_write
2084 * SSL_pending => ossl_quic_pending
2085 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
2086 * SSL_key_update => ossl_quic_key_update
2090 int ossl_quic_get_error(const SSL
*s
, int i
)
2093 int net_error
, last_error
;
2095 if (!expect_quic(s
, &ctx
))
2099 net_error
= ossl_quic_channel_net_error(ctx
.qc
->ch
);
2100 last_error
= ctx
.is_stream
? ctx
.xso
->last_error
: ctx
.qc
->last_error
;
2101 quic_unlock(ctx
.qc
);
2104 return SSL_ERROR_SYSCALL
;
2109 /* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
2110 static int error_to_want(int error
)
2113 case SSL_ERROR_WANT_CONNECT
: /* never used - UDP is connectionless */
2114 case SSL_ERROR_WANT_ACCEPT
: /* never used - UDP is connectionless */
2115 case SSL_ERROR_ZERO_RETURN
:
2119 case SSL_ERROR_WANT_READ
:
2122 case SSL_ERROR_WANT_WRITE
:
2125 case SSL_ERROR_WANT_RETRY_VERIFY
:
2126 return SSL_RETRY_VERIFY
;
2128 case SSL_ERROR_WANT_CLIENT_HELLO_CB
:
2129 return SSL_CLIENT_HELLO_CB
;
2131 case SSL_ERROR_WANT_X509_LOOKUP
:
2132 return SSL_X509_LOOKUP
;
2137 int ossl_quic_want(const SSL
*s
)
2142 if (!expect_quic(s
, &ctx
))
2147 w
= error_to_want(ctx
.is_stream
? ctx
.xso
->last_error
: ctx
.qc
->last_error
);
2149 quic_unlock(ctx
.qc
);
2157 * The set of functions below provide the implementation of the public SSL_write
2158 * function. We must handle:
2160 * - both blocking and non-blocking operation at the application level,
2161 * depending on how we are configured;
2163 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
2165 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
2169 static void quic_post_write(QUIC_XSO
*xso
, int did_append
, int do_tick
)
2172 * We have appended at least one byte to the stream.
2173 * Potentially mark stream as active, depending on FC.
2176 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso
->conn
->ch
),
2182 * TODO(QUIC FUTURE): It is probably inefficient to try and do this
2183 * immediately, plus we should eventually consider Nagle's algorithm.
2186 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso
->conn
->ch
), 0);
2189 struct quic_write_again_args
{
2191 const unsigned char *buf
;
2193 size_t total_written
;
2198 * Absolute maximum write buffer size, enforced to prevent a rogue peer from
2199 * deliberately inducing DoS. This has been chosen based on the optimal buffer
2200 * size for an RTT of 500ms and a bandwidth of 100 Mb/s.
2202 #define MAX_WRITE_BUF_SIZE (6 * 1024 * 1024)
2205 * Ensure spare buffer space available (up until a limit, at least).
2208 static int sstream_ensure_spare(QUIC_SSTREAM
*sstream
, uint64_t spare
)
2210 size_t cur_sz
= ossl_quic_sstream_get_buffer_size(sstream
);
2211 size_t avail
= ossl_quic_sstream_get_buffer_avail(sstream
);
2212 size_t spare_
= (spare
> SIZE_MAX
) ? SIZE_MAX
: (size_t)spare
;
2213 size_t new_sz
, growth
;
2215 if (spare_
<= avail
|| cur_sz
== MAX_WRITE_BUF_SIZE
)
2218 growth
= spare_
- avail
;
2219 if (cur_sz
+ growth
> MAX_WRITE_BUF_SIZE
)
2220 new_sz
= MAX_WRITE_BUF_SIZE
;
2222 new_sz
= cur_sz
+ growth
;
2224 return ossl_quic_sstream_set_buffer_size(sstream
, new_sz
);
2228 * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded
2229 * as needed according to flow control.
2232 static int xso_sstream_append(QUIC_XSO
*xso
, const unsigned char *buf
,
2233 size_t len
, size_t *actual_written
)
2235 QUIC_SSTREAM
*sstream
= xso
->stream
->sstream
;
2236 uint64_t cur
= ossl_quic_sstream_get_cur_size(sstream
);
2237 uint64_t cwm
= ossl_quic_txfc_get_cwm(&xso
->stream
->txfc
);
2238 uint64_t permitted
= (cwm
>= cur
? cwm
- cur
: 0);
2240 if (len
> permitted
)
2241 len
= (size_t)permitted
;
2243 if (!sstream_ensure_spare(sstream
, len
))
2246 return ossl_quic_sstream_append(sstream
, buf
, len
, actual_written
);
2250 static int quic_write_again(void *arg
)
2252 struct quic_write_again_args
*args
= arg
;
2253 size_t actual_written
= 0;
2255 if (!quic_mutation_allowed(args
->xso
->conn
, /*req_active=*/1))
2256 /* If connection is torn down due to an error while blocking, stop. */
2259 if (!quic_validate_for_write(args
->xso
, &args
->err
))
2261 * Stream may have become invalid for write due to connection events
2266 args
->err
= ERR_R_INTERNAL_ERROR
;
2267 if (!xso_sstream_append(args
->xso
, args
->buf
, args
->len
, &actual_written
))
2270 quic_post_write(args
->xso
, actual_written
> 0, 0);
2272 args
->buf
+= actual_written
;
2273 args
->len
-= actual_written
;
2274 args
->total_written
+= actual_written
;
2277 /* Written everything, done. */
2280 /* Not written everything yet, keep trying. */
2285 static int quic_write_blocking(QCTX
*ctx
, const void *buf
, size_t len
,
2289 QUIC_XSO
*xso
= ctx
->xso
;
2290 struct quic_write_again_args args
;
2291 size_t actual_written
= 0;
2293 /* First make a best effort to append as much of the data as possible. */
2294 if (!xso_sstream_append(xso
, buf
, len
, &actual_written
)) {
2295 /* Stream already finished or allocation error. */
2297 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2300 quic_post_write(xso
, actual_written
> 0, 1);
2302 if (actual_written
== len
) {
2303 /* Managed to append everything on the first try. */
2304 *written
= actual_written
;
2309 * We did not manage to append all of the data immediately, so the stream
2310 * buffer has probably filled up. This means we need to block until some of
2314 args
.buf
= (const unsigned char *)buf
+ actual_written
;
2315 args
.len
= len
- actual_written
;
2316 args
.total_written
= 0;
2317 args
.err
= ERR_R_INTERNAL_ERROR
;
2319 res
= block_until_pred(xso
->conn
, quic_write_again
, &args
, 0);
2321 if (!quic_mutation_allowed(xso
->conn
, /*req_active=*/1))
2322 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2324 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, args
.err
, NULL
);
2327 *written
= args
.total_written
;
2332 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2335 static void aon_write_begin(QUIC_XSO
*xso
, const unsigned char *buf
,
2336 size_t buf_len
, size_t already_sent
)
2338 assert(!xso
->aon_write_in_progress
);
2340 xso
->aon_write_in_progress
= 1;
2341 xso
->aon_buf_base
= buf
;
2342 xso
->aon_buf_pos
= already_sent
;
2343 xso
->aon_buf_len
= buf_len
;
2346 static void aon_write_finish(QUIC_XSO
*xso
)
2348 xso
->aon_write_in_progress
= 0;
2349 xso
->aon_buf_base
= NULL
;
2350 xso
->aon_buf_pos
= 0;
2351 xso
->aon_buf_len
= 0;
2355 static int quic_write_nonblocking_aon(QCTX
*ctx
, const void *buf
,
2356 size_t len
, size_t *written
)
2358 QUIC_XSO
*xso
= ctx
->xso
;
2359 const void *actual_buf
;
2360 size_t actual_len
, actual_written
= 0;
2361 int accept_moving_buffer
2362 = ((xso
->ssl_mode
& SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
) != 0);
2364 if (xso
->aon_write_in_progress
) {
2366 * We are in the middle of an AON write (i.e., a previous write did not
2367 * manage to append all data to the SSTREAM and we have Enable Partial
2368 * Write (EPW) mode disabled.)
2370 if ((!accept_moving_buffer
&& xso
->aon_buf_base
!= buf
)
2371 || len
!= xso
->aon_buf_len
)
2373 * Pointer must not have changed if we are not in accept moving
2374 * buffer mode. Length must never change.
2376 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_BAD_WRITE_RETRY
, NULL
);
2378 actual_buf
= (unsigned char *)buf
+ xso
->aon_buf_pos
;
2379 actual_len
= len
- xso
->aon_buf_pos
;
2380 assert(actual_len
> 0);
2386 /* First make a best effort to append as much of the data as possible. */
2387 if (!xso_sstream_append(xso
, actual_buf
, actual_len
, &actual_written
)) {
2388 /* Stream already finished or allocation error. */
2390 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2393 quic_post_write(xso
, actual_written
> 0, 1);
2395 if (actual_written
== actual_len
) {
2396 /* We have sent everything. */
2397 if (xso
->aon_write_in_progress
) {
2399 * We have sent everything, and we were in the middle of an AON
2400 * write. The output write length is the total length of the AON
2401 * buffer, not however many bytes we managed to write to the stream
2404 *written
= xso
->aon_buf_len
;
2405 aon_write_finish(xso
);
2407 *written
= actual_written
;
2413 if (xso
->aon_write_in_progress
) {
2415 * AON write is in progress but we have not written everything yet. We
2416 * may have managed to send zero bytes, or some number of bytes less
2417 * than the total remaining which need to be appended during this
2420 xso
->aon_buf_pos
+= actual_written
;
2421 assert(xso
->aon_buf_pos
< xso
->aon_buf_len
);
2422 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_WRITE
);
2426 * Not in an existing AON operation but partial write is not enabled, so we
2427 * need to begin a new AON operation. However we needn't bother if we didn't
2428 * actually append anything.
2430 if (actual_written
> 0)
2431 aon_write_begin(xso
, buf
, len
, actual_written
);
2434 * AON - We do not publicly admit to having appended anything until AON
2438 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_WRITE
);
2442 static int quic_write_nonblocking_epw(QCTX
*ctx
, const void *buf
, size_t len
,
2445 QUIC_XSO
*xso
= ctx
->xso
;
2447 /* Simple best effort operation. */
2448 if (!xso_sstream_append(xso
, buf
, len
, written
)) {
2449 /* Stream already finished or allocation error. */
2451 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2454 quic_post_write(xso
, *written
> 0, 1);
2459 static int quic_validate_for_write(QUIC_XSO
*xso
, int *err
)
2461 QUIC_STREAM_MAP
*qsm
;
2463 if (xso
== NULL
|| xso
->stream
== NULL
) {
2464 *err
= ERR_R_INTERNAL_ERROR
;
2468 switch (xso
->stream
->send_state
) {
2470 case QUIC_SSTREAM_STATE_NONE
:
2471 *err
= SSL_R_STREAM_RECV_ONLY
;
2474 case QUIC_SSTREAM_STATE_READY
:
2475 qsm
= ossl_quic_channel_get_qsm(xso
->conn
->ch
);
2477 if (!ossl_quic_stream_map_ensure_send_part_id(qsm
, xso
->stream
)) {
2478 *err
= ERR_R_INTERNAL_ERROR
;
2483 case QUIC_SSTREAM_STATE_SEND
:
2484 case QUIC_SSTREAM_STATE_DATA_SENT
:
2485 case QUIC_SSTREAM_STATE_DATA_RECVD
:
2486 if (ossl_quic_sstream_get_final_size(xso
->stream
->sstream
, NULL
)) {
2487 *err
= SSL_R_STREAM_FINISHED
;
2493 case QUIC_SSTREAM_STATE_RESET_SENT
:
2494 case QUIC_SSTREAM_STATE_RESET_RECVD
:
2495 *err
= SSL_R_STREAM_RESET
;
2501 int ossl_quic_write(SSL
*s
, const void *buf
, size_t len
, size_t *written
)
2505 int partial_write
, err
;
2509 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/0, /*io=*/1, &ctx
))
2512 partial_write
= ((ctx
.xso
->ssl_mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
) != 0);
2514 if (!quic_mutation_allowed(ctx
.qc
, /*req_active=*/0)) {
2515 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2520 * If we haven't finished the handshake, try to advance it.
2521 * We don't accept writes until the handshake is completed.
2523 if (quic_do_handshake(&ctx
) < 1) {
2528 /* Ensure correct stream state, stream send part not concluded, etc. */
2529 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2530 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2539 if (xso_blocking_mode(ctx
.xso
))
2540 ret
= quic_write_blocking(&ctx
, buf
, len
, written
);
2541 else if (partial_write
)
2542 ret
= quic_write_nonblocking_epw(&ctx
, buf
, len
, written
);
2544 ret
= quic_write_nonblocking_aon(&ctx
, buf
, len
, written
);
2547 quic_unlock(ctx
.qc
);
2555 struct quic_read_again_args
{
2557 QUIC_STREAM
*stream
;
2565 static int quic_validate_for_read(QUIC_XSO
*xso
, int *err
, int *eos
)
2567 QUIC_STREAM_MAP
*qsm
;
2571 if (xso
== NULL
|| xso
->stream
== NULL
) {
2572 *err
= ERR_R_INTERNAL_ERROR
;
2576 switch (xso
->stream
->recv_state
) {
2578 case QUIC_RSTREAM_STATE_NONE
:
2579 *err
= SSL_R_STREAM_SEND_ONLY
;
2582 case QUIC_RSTREAM_STATE_RECV
:
2583 case QUIC_RSTREAM_STATE_SIZE_KNOWN
:
2584 case QUIC_RSTREAM_STATE_DATA_RECVD
:
2587 case QUIC_RSTREAM_STATE_DATA_READ
:
2591 case QUIC_RSTREAM_STATE_RESET_RECVD
:
2592 qsm
= ossl_quic_channel_get_qsm(xso
->conn
->ch
);
2593 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm
, xso
->stream
);
2596 case QUIC_RSTREAM_STATE_RESET_READ
:
2597 *err
= SSL_R_STREAM_RESET
;
2603 static int quic_read_actual(QCTX
*ctx
,
2604 QUIC_STREAM
*stream
,
2605 void *buf
, size_t buf_len
,
2609 int is_fin
= 0, err
, eos
;
2610 QUIC_CONNECTION
*qc
= ctx
->qc
;
2612 if (!quic_validate_for_read(ctx
->xso
, &err
, &eos
)) {
2614 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_ZERO_RETURN
);
2616 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, err
, NULL
);
2620 if (!ossl_quic_rstream_peek(stream
->rstream
, buf
, buf_len
,
2621 bytes_read
, &is_fin
))
2622 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2625 if (!ossl_quic_rstream_read(stream
->rstream
, buf
, buf_len
,
2626 bytes_read
, &is_fin
))
2627 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2631 if (*bytes_read
> 0) {
2633 * We have read at least one byte from the stream. Inform stream-level
2634 * RXFC of the retirement of controlled bytes. Update the active stream
2635 * status (the RXFC may now want to emit a frame granting more credit to
2638 OSSL_RTT_INFO rtt_info
;
2640 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc
->ch
), &rtt_info
);
2642 if (!ossl_quic_rxfc_on_retire(&stream
->rxfc
, *bytes_read
,
2643 rtt_info
.smoothed_rtt
))
2644 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2647 if (is_fin
&& !peek
) {
2648 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(ctx
->qc
->ch
);
2650 ossl_quic_stream_map_notify_totally_read(qsm
, ctx
->xso
->stream
);
2653 if (*bytes_read
> 0)
2654 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc
->ch
),
2658 if (*bytes_read
== 0 && is_fin
)
2659 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_ZERO_RETURN
);
2665 static int quic_read_again(void *arg
)
2667 struct quic_read_again_args
*args
= arg
;
2669 if (!quic_mutation_allowed(args
->ctx
->qc
, /*req_active=*/1)) {
2670 /* If connection is torn down due to an error while blocking, stop. */
2671 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2675 if (!quic_read_actual(args
->ctx
, args
->stream
,
2676 args
->buf
, args
->len
, args
->bytes_read
,
2680 if (*args
->bytes_read
> 0)
2681 /* got at least one byte, the SSL_read op can finish now */
2684 return 0; /* did not read anything, keep trying */
2688 static int quic_read(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
, int peek
)
2692 struct quic_read_again_args args
;
2696 if (!expect_quic(s
, &ctx
))
2699 quic_lock_for_io(&ctx
);
2701 if (!quic_mutation_allowed(ctx
.qc
, /*req_active=*/0)) {
2702 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2706 /* If we haven't finished the handshake, try to advance it. */
2707 if (quic_do_handshake(&ctx
) < 1) {
2708 ret
= 0; /* ossl_quic_do_handshake raised error here */
2712 if (ctx
.xso
== NULL
) {
2714 * Called on a QCSO and we don't currently have a default stream.
2716 * Wait until we get a stream initiated by the peer (blocking mode) or
2717 * fail if we don't have one yet (non-blocking mode).
2719 if (!qc_wait_for_default_xso_for_read(&ctx
)) {
2720 ret
= 0; /* error already raised here */
2724 ctx
.xso
= ctx
.qc
->default_xso
;
2727 if (!quic_read_actual(&ctx
, ctx
.xso
->stream
, buf
, len
, bytes_read
, peek
)) {
2728 ret
= 0; /* quic_read_actual raised error here */
2732 if (*bytes_read
> 0) {
2734 * Even though we succeeded, tick the reactor here to ensure we are
2735 * handling other aspects of the QUIC connection.
2737 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
2739 } else if (xso_blocking_mode(ctx
.xso
)) {
2741 * We were not able to read anything immediately, so our stream
2742 * buffer is empty. This means we need to block until we get
2743 * at least one byte.
2746 args
.stream
= ctx
.xso
->stream
;
2749 args
.bytes_read
= bytes_read
;
2752 res
= block_until_pred(ctx
.qc
, quic_read_again
, &args
, 0);
2754 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2756 } else if (res
< 0) {
2757 ret
= 0; /* quic_read_again raised error here */
2764 * We did not get any bytes and are not in blocking mode.
2765 * Tick to see if this delivers any more.
2767 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
2769 /* Try the read again. */
2770 if (!quic_read_actual(&ctx
, ctx
.xso
->stream
, buf
, len
, bytes_read
, peek
)) {
2771 ret
= 0; /* quic_read_actual raised error here */
2775 if (*bytes_read
> 0)
2776 ret
= 1; /* Succeeded this time. */
2778 ret
= QUIC_RAISE_NORMAL_ERROR(&ctx
, SSL_ERROR_WANT_READ
);
2782 quic_unlock(ctx
.qc
);
2786 int ossl_quic_read(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
)
2788 return quic_read(s
, buf
, len
, bytes_read
, 0);
2791 int ossl_quic_peek(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
)
2793 return quic_read(s
, buf
, len
, bytes_read
, 1);
2802 static size_t ossl_quic_pending_int(const SSL
*s
, int check_channel
)
2809 if (!expect_quic(s
, &ctx
))
2814 if (ctx
.xso
== NULL
) {
2815 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_NO_STREAM
, NULL
);
2819 if (ctx
.xso
->stream
== NULL
2820 || !ossl_quic_stream_has_recv_buffer(ctx
.xso
->stream
)) {
2821 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2825 if (!ossl_quic_rstream_available(ctx
.xso
->stream
->rstream
, &avail
, &fin
))
2828 if (avail
== 0 && check_channel
&& ossl_quic_channel_has_pending(ctx
.qc
->ch
))
2832 quic_unlock(ctx
.qc
);
2836 size_t ossl_quic_pending(const SSL
*s
)
2838 return ossl_quic_pending_int(s
, /*check_channel=*/0);
2841 int ossl_quic_has_pending(const SSL
*s
)
2843 /* Do we have app-side pending data or pending URXEs or RXEs? */
2844 return ossl_quic_pending_int(s
, /*check_channel=*/1) > 0;
2848 * SSL_stream_conclude
2849 * -------------------
2852 int ossl_quic_conn_stream_conclude(SSL
*s
)
2858 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/0, /*io=*/0, &ctx
))
2861 qs
= ctx
.xso
->stream
;
2863 if (!quic_mutation_allowed(ctx
.qc
, /*req_active=*/1)) {
2864 quic_unlock(ctx
.qc
);
2865 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2868 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2869 quic_unlock(ctx
.qc
);
2870 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2873 if (ossl_quic_sstream_get_final_size(qs
->sstream
, NULL
)) {
2874 quic_unlock(ctx
.qc
);
2878 ossl_quic_sstream_fin(qs
->sstream
);
2879 quic_post_write(ctx
.xso
, 1, 1);
2880 quic_unlock(ctx
.qc
);
2885 * SSL_inject_net_dgram
2886 * --------------------
2889 int SSL_inject_net_dgram(SSL
*s
, const unsigned char *buf
,
2891 const BIO_ADDR
*peer
,
2892 const BIO_ADDR
*local
)
2898 if (!expect_quic(s
, &ctx
))
2903 demux
= ossl_quic_channel_get0_demux(ctx
.qc
->ch
);
2904 ret
= ossl_quic_demux_inject(demux
, buf
, buf_len
, peer
, local
);
2906 quic_unlock(ctx
.qc
);
2911 * SSL_get0_connection
2912 * -------------------
2914 SSL
*ossl_quic_get0_connection(SSL
*s
)
2918 if (!expect_quic(s
, &ctx
))
2921 return &ctx
.qc
->ssl
;
2925 * SSL_get_stream_type
2926 * -------------------
2928 int ossl_quic_get_stream_type(SSL
*s
)
2932 if (!expect_quic(s
, &ctx
))
2933 return SSL_STREAM_TYPE_BIDI
;
2935 if (ctx
.xso
== NULL
) {
2937 * If deferred XSO creation has yet to occur, proceed according to the
2938 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2939 * what kind of stream will be created yet, so return BIDI on the basis
2940 * that at this time, the client still has the option of calling
2941 * SSL_read() or SSL_write() first.
2943 if (ctx
.qc
->default_xso_created
2944 || ctx
.qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
2945 return SSL_STREAM_TYPE_NONE
;
2947 return SSL_STREAM_TYPE_BIDI
;
2950 if (ossl_quic_stream_is_bidi(ctx
.xso
->stream
))
2951 return SSL_STREAM_TYPE_BIDI
;
2953 if (ossl_quic_stream_is_server_init(ctx
.xso
->stream
) != ctx
.qc
->as_server
)
2954 return SSL_STREAM_TYPE_READ
;
2956 return SSL_STREAM_TYPE_WRITE
;
2964 uint64_t ossl_quic_get_stream_id(SSL
*s
)
2969 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/-1, /*io=*/0, &ctx
))
2972 id
= ctx
.xso
->stream
->id
;
2973 quic_unlock(ctx
.qc
);
2979 * SSL_is_stream_local
2980 * -------------------
2983 int ossl_quic_is_stream_local(SSL
*s
)
2988 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/-1, /*io=*/0, &ctx
))
2991 is_local
= ossl_quic_stream_is_local_init(ctx
.xso
->stream
);
2992 quic_unlock(ctx
.qc
);
2998 * SSL_set_default_stream_mode
2999 * ---------------------------
3002 int ossl_quic_set_default_stream_mode(SSL
*s
, uint32_t mode
)
3006 if (!expect_quic_conn_only(s
, &ctx
))
3011 if (ctx
.qc
->default_xso_created
) {
3012 quic_unlock(ctx
.qc
);
3013 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
3014 "too late to change default stream mode");
3018 case SSL_DEFAULT_STREAM_MODE_NONE
:
3019 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI
:
3020 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI
:
3021 ctx
.qc
->default_stream_mode
= mode
;
3024 quic_unlock(ctx
.qc
);
3025 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
3026 "bad default stream type");
3029 quic_unlock(ctx
.qc
);
3038 SSL
*ossl_quic_detach_stream(SSL
*s
)
3041 QUIC_XSO
*xso
= NULL
;
3043 if (!expect_quic_conn_only(s
, &ctx
))
3048 /* Calling this function inhibits default XSO autocreation. */
3049 /* QC ref to any default XSO is transferred to us and to caller. */
3050 qc_set_default_xso_keep_ref(ctx
.qc
, NULL
, /*touch=*/1, &xso
);
3052 quic_unlock(ctx
.qc
);
3054 return xso
!= NULL
? &xso
->ssl
: NULL
;
3062 int ossl_quic_attach_stream(SSL
*conn
, SSL
*stream
)
3068 if (!expect_quic_conn_only(conn
, &ctx
))
3071 if (stream
== NULL
|| stream
->type
!= SSL_TYPE_QUIC_XSO
)
3072 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_NULL_PARAMETER
,
3073 "stream to attach must be a valid QUIC stream");
3075 xso
= (QUIC_XSO
*)stream
;
3079 if (ctx
.qc
->default_xso
!= NULL
) {
3080 quic_unlock(ctx
.qc
);
3081 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
3082 "connection already has a default stream");
3086 * It is a caller error for the XSO being attached as a default XSO to have
3087 * more than one ref.
3089 if (!CRYPTO_GET_REF(&xso
->ssl
.references
, &nref
)) {
3090 quic_unlock(ctx
.qc
);
3091 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
,
3096 quic_unlock(ctx
.qc
);
3097 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
3098 "stream being attached must have "
3099 "only 1 reference");
3102 /* Caller's reference to the XSO is transferred to us. */
3103 /* Calling this function inhibits default XSO autocreation. */
3104 qc_set_default_xso(ctx
.qc
, xso
, /*touch=*/1);
3106 quic_unlock(ctx
.qc
);
3111 * SSL_set_incoming_stream_policy
3112 * ------------------------------
3115 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION
*qc
)
3117 switch (qc
->incoming_stream_policy
) {
3118 case SSL_INCOMING_STREAM_POLICY_AUTO
:
3119 if ((qc
->default_xso
== NULL
&& !qc
->default_xso_created
)
3120 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
3121 return SSL_INCOMING_STREAM_POLICY_ACCEPT
;
3123 return SSL_INCOMING_STREAM_POLICY_REJECT
;
3126 return qc
->incoming_stream_policy
;
3131 static void qc_update_reject_policy(QUIC_CONNECTION
*qc
)
3133 int policy
= qc_get_effective_incoming_stream_policy(qc
);
3134 int enable_reject
= (policy
== SSL_INCOMING_STREAM_POLICY_REJECT
);
3136 ossl_quic_channel_set_incoming_stream_auto_reject(qc
->ch
,
3138 qc
->incoming_stream_aec
);
3142 int ossl_quic_set_incoming_stream_policy(SSL
*s
, int policy
,
3148 if (!expect_quic_conn_only(s
, &ctx
))
3154 case SSL_INCOMING_STREAM_POLICY_AUTO
:
3155 case SSL_INCOMING_STREAM_POLICY_ACCEPT
:
3156 case SSL_INCOMING_STREAM_POLICY_REJECT
:
3157 ctx
.qc
->incoming_stream_policy
= policy
;
3158 ctx
.qc
->incoming_stream_aec
= aec
;
3162 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
3167 qc_update_reject_policy(ctx
.qc
);
3168 quic_unlock(ctx
.qc
);
3176 struct wait_for_incoming_stream_args
{
3182 static int wait_for_incoming_stream(void *arg
)
3184 struct wait_for_incoming_stream_args
*args
= arg
;
3185 QUIC_CONNECTION
*qc
= args
->ctx
->qc
;
3186 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
3188 if (!quic_mutation_allowed(qc
, /*req_active=*/1)) {
3189 /* If connection is torn down due to an error while blocking, stop. */
3190 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
3194 args
->qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
3195 if (args
->qs
!= NULL
)
3196 return 1; /* got a stream */
3198 return 0; /* did not get a stream, keep trying */
3202 SSL
*ossl_quic_accept_stream(SSL
*s
, uint64_t flags
)
3207 QUIC_STREAM_MAP
*qsm
;
3210 OSSL_RTT_INFO rtt_info
;
3212 if (!expect_quic_conn_only(s
, &ctx
))
3217 if (qc_get_effective_incoming_stream_policy(ctx
.qc
)
3218 == SSL_INCOMING_STREAM_POLICY_REJECT
) {
3219 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
, NULL
);
3223 qsm
= ossl_quic_channel_get_qsm(ctx
.qc
->ch
);
3225 qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
3227 if (qc_blocking_mode(ctx
.qc
)
3228 && (flags
& SSL_ACCEPT_STREAM_NO_BLOCK
) == 0) {
3229 struct wait_for_incoming_stream_args args
;
3234 ret
= block_until_pred(ctx
.qc
, wait_for_incoming_stream
, &args
, 0);
3236 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
3238 } else if (ret
< 0 || args
.qs
== NULL
) {
3248 xso
= create_xso_from_stream(ctx
.qc
, qs
);
3252 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx
.qc
->ch
), &rtt_info
);
3253 ossl_quic_stream_map_remove_from_accept_queue(qsm
, qs
,
3254 rtt_info
.smoothed_rtt
);
3257 /* Calling this function inhibits default XSO autocreation. */
3258 qc_touch_default_xso(ctx
.qc
); /* inhibits default XSO */
3261 quic_unlock(ctx
.qc
);
3266 * SSL_get_accept_stream_queue_len
3267 * -------------------------------
3270 size_t ossl_quic_get_accept_stream_queue_len(SSL
*s
)
3275 if (!expect_quic_conn_only(s
, &ctx
))
3280 v
= ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx
.qc
->ch
));
3282 quic_unlock(ctx
.qc
);
3290 int ossl_quic_stream_reset(SSL
*ssl
,
3291 const SSL_STREAM_RESET_ARGS
*args
,
3295 QUIC_STREAM_MAP
*qsm
;
3297 uint64_t error_code
;
3300 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/0, /*io=*/0, &ctx
))
3303 qsm
= ossl_quic_channel_get_qsm(ctx
.qc
->ch
);
3304 qs
= ctx
.xso
->stream
;
3305 error_code
= (args
!= NULL
? args
->quic_error_code
: 0);
3307 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
3308 ok
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
3312 ok
= ossl_quic_stream_map_reset_stream_send_part(qsm
, qs
, error_code
);
3315 quic_unlock(ctx
.qc
);
3320 * SSL_get_stream_read_state
3321 * -------------------------
3323 static void quic_classify_stream(QUIC_CONNECTION
*qc
,
3327 uint64_t *app_error_code
)
3330 uint64_t final_size
;
3332 local_init
= (ossl_quic_stream_is_server_init(qs
) == qc
->as_server
);
3334 if (app_error_code
!= NULL
)
3335 *app_error_code
= UINT64_MAX
;
3337 app_error_code
= &final_size
; /* throw away value */
3339 if (!ossl_quic_stream_is_bidi(qs
) && local_init
!= is_write
) {
3341 * Unidirectional stream and this direction of transmission doesn't
3344 *state
= SSL_STREAM_STATE_WRONG_DIR
;
3345 } else if (ossl_quic_channel_is_term_any(qc
->ch
)) {
3346 /* Connection already closed. */
3347 *state
= SSL_STREAM_STATE_CONN_CLOSED
;
3348 } else if (!is_write
&& qs
->recv_state
== QUIC_RSTREAM_STATE_DATA_READ
) {
3349 /* Application has read a FIN. */
3350 *state
= SSL_STREAM_STATE_FINISHED
;
3351 } else if ((!is_write
&& qs
->stop_sending
)
3352 || (is_write
&& ossl_quic_stream_send_is_reset(qs
))) {
3354 * Stream has been reset locally. FIN takes precedence over this for the
3355 * read case as the application need not care if the stream is reset
3356 * after a FIN has been successfully processed.
3358 *state
= SSL_STREAM_STATE_RESET_LOCAL
;
3359 *app_error_code
= !is_write
3360 ? qs
->stop_sending_aec
3361 : qs
->reset_stream_aec
;
3362 } else if ((!is_write
&& ossl_quic_stream_recv_is_reset(qs
))
3363 || (is_write
&& qs
->peer_stop_sending
)) {
3365 * Stream has been reset remotely. */
3366 *state
= SSL_STREAM_STATE_RESET_REMOTE
;
3367 *app_error_code
= !is_write
3368 ? qs
->peer_reset_stream_aec
3369 : qs
->peer_stop_sending_aec
;
3370 } else if (is_write
&& ossl_quic_sstream_get_final_size(qs
->sstream
,
3373 * Stream has been finished. Stream reset takes precedence over this for
3374 * the write case as peer may not have received all data.
3376 *state
= SSL_STREAM_STATE_FINISHED
;
3378 /* Stream still healthy. */
3379 *state
= SSL_STREAM_STATE_OK
;
3383 static int quic_get_stream_state(SSL
*ssl
, int is_write
)
3388 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, /*io=*/0, &ctx
))
3389 return SSL_STREAM_STATE_NONE
;
3391 quic_classify_stream(ctx
.qc
, ctx
.xso
->stream
, is_write
, &state
, NULL
);
3392 quic_unlock(ctx
.qc
);
3396 int ossl_quic_get_stream_read_state(SSL
*ssl
)
3398 return quic_get_stream_state(ssl
, /*is_write=*/0);
3402 * SSL_get_stream_write_state
3403 * --------------------------
3405 int ossl_quic_get_stream_write_state(SSL
*ssl
)
3407 return quic_get_stream_state(ssl
, /*is_write=*/1);
3411 * SSL_get_stream_read_error_code
3412 * ------------------------------
3414 static int quic_get_stream_error_code(SSL
*ssl
, int is_write
,
3415 uint64_t *app_error_code
)
3420 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, /*io=*/0, &ctx
))
3423 quic_classify_stream(ctx
.qc
, ctx
.xso
->stream
, /*is_write=*/0,
3424 &state
, app_error_code
);
3426 quic_unlock(ctx
.qc
);
3428 case SSL_STREAM_STATE_FINISHED
:
3430 case SSL_STREAM_STATE_RESET_LOCAL
:
3431 case SSL_STREAM_STATE_RESET_REMOTE
:
3438 int ossl_quic_get_stream_read_error_code(SSL
*ssl
, uint64_t *app_error_code
)
3440 return quic_get_stream_error_code(ssl
, /*is_write=*/0, app_error_code
);
3444 * SSL_get_stream_write_error_code
3445 * -------------------------------
3447 int ossl_quic_get_stream_write_error_code(SSL
*ssl
, uint64_t *app_error_code
)
3449 return quic_get_stream_error_code(ssl
, /*is_write=*/1, app_error_code
);
3453 * Write buffer size mutation
3454 * --------------------------
3456 int ossl_quic_set_write_buffer_size(SSL
*ssl
, size_t size
)
3461 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, /*io=*/0, &ctx
))
3464 if (!ossl_quic_stream_has_send(ctx
.xso
->stream
)) {
3465 /* Called on a unidirectional receive-only stream - error. */
3466 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
, NULL
);
3470 if (!ossl_quic_stream_has_send_buffer(ctx
.xso
->stream
)) {
3472 * If the stream has a send part but we have disposed of it because we
3473 * no longer need it, this is a no-op.
3479 if (!ossl_quic_sstream_set_buffer_size(ctx
.xso
->stream
->sstream
, size
)) {
3480 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
3487 quic_unlock(ctx
.qc
);
3492 * SSL_get_conn_close_info
3493 * -----------------------
3495 int ossl_quic_get_conn_close_info(SSL
*ssl
,
3496 SSL_CONN_CLOSE_INFO
*info
,
3500 const QUIC_TERMINATE_CAUSE
*tc
;
3502 if (!expect_quic_conn_only(ssl
, &ctx
))
3505 tc
= ossl_quic_channel_get_terminate_cause(ctx
.qc
->ch
);
3509 info
->error_code
= tc
->error_code
;
3510 info
->frame_type
= tc
->frame_type
;
3511 info
->reason
= tc
->reason
;
3512 info
->reason_len
= tc
->reason_len
;
3515 info
->flags
|= SSL_CONN_CLOSE_FLAG_LOCAL
;
3517 info
->flags
|= SSL_CONN_CLOSE_FLAG_TRANSPORT
;
3525 int ossl_quic_key_update(SSL
*ssl
, int update_type
)
3529 if (!expect_quic_conn_only(ssl
, &ctx
))
3532 switch (update_type
) {
3533 case SSL_KEY_UPDATE_NOT_REQUESTED
:
3535 * QUIC signals peer key update implicily by triggering a local
3536 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3538 case SSL_KEY_UPDATE_REQUESTED
:
3542 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
3548 /* Attempt to perform a TXKU. */
3549 if (!ossl_quic_channel_trigger_txku(ctx
.qc
->ch
)) {
3550 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_TOO_MANY_KEY_UPDATES
, NULL
);
3551 quic_unlock(ctx
.qc
);
3555 quic_unlock(ctx
.qc
);
3560 * SSL_get_key_update_type
3561 * -----------------------
3563 int ossl_quic_get_key_update_type(const SSL
*s
)
3566 * We always handle key updates immediately so a key update is never
3569 return SSL_KEY_UPDATE_NONE
;
3573 * QUIC Front-End I/O API: SSL_CTX Management
3574 * ==========================================
3577 long ossl_quic_ctx_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
3581 return ssl3_ctx_ctrl(ctx
, cmd
, larg
, parg
);
3585 long ossl_quic_callback_ctrl(SSL
*s
, int cmd
, void (*fp
) (void))
3589 if (!expect_quic_conn_only(s
, &ctx
))
3593 case SSL_CTRL_SET_MSG_CALLBACK
:
3594 ossl_quic_channel_set_msg_callback(ctx
.qc
->ch
, (ossl_msg_cb
)fp
,
3596 /* This callback also needs to be set on the internal SSL object */
3597 return ssl3_callback_ctrl(ctx
.qc
->tls
, cmd
, fp
);;
3600 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3601 return ssl3_callback_ctrl(ctx
.qc
->tls
, cmd
, fp
);
3605 long ossl_quic_ctx_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
) (void))
3607 return ssl3_ctx_callback_ctrl(ctx
, cmd
, fp
);
3610 int ossl_quic_renegotiate_check(SSL
*ssl
, int initok
)
3612 /* We never do renegotiation. */
3616 const SSL_CIPHER
*ossl_quic_get_cipher_by_char(const unsigned char *p
)
3618 const SSL_CIPHER
*ciph
= ssl3_get_cipher_by_char(p
);
3620 if ((ciph
->algorithm2
& SSL_QUIC
) == 0)
3627 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3628 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3631 int ossl_quic_num_ciphers(void)
3636 const SSL_CIPHER
*ossl_quic_get_cipher(unsigned int u
)
3642 * SSL_get_shutdown()
3643 * ------------------
3645 int ossl_quic_get_shutdown(const SSL
*s
)
3650 if (!expect_quic_conn_only(s
, &ctx
))
3653 if (ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
3654 shut
|= SSL_SENT_SHUTDOWN
;
3655 if (!ossl_quic_channel_is_closing(ctx
.qc
->ch
))
3656 shut
|= SSL_RECEIVED_SHUTDOWN
;
3663 * Internal Testing APIs
3664 * =====================
3667 QUIC_CHANNEL
*ossl_quic_conn_get_channel(SSL
*s
)
3671 if (!expect_quic_conn_only(s
, &ctx
))