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
);
39 * QUIC Front-End I/O API: Common Utilities
40 * ========================================
44 * Block until a predicate is met.
46 * Precondition: Must have a channel.
47 * Precondition: Must hold channel lock (unchecked).
50 static int block_until_pred(QUIC_CONNECTION
*qc
,
51 int (*pred
)(void *arg
), void *pred_arg
,
56 assert(qc
->ch
!= NULL
);
58 rtor
= ossl_quic_channel_get_reactor(qc
->ch
);
59 return ossl_quic_reactor_block_until_pred(rtor
, pred
, pred_arg
, flags
,
63 static OSSL_TIME
get_time(QUIC_CONNECTION
*qc
)
65 if (qc
->override_now_cb
!= NULL
)
66 return qc
->override_now_cb(qc
->override_now_cb_arg
);
68 return ossl_time_now();
71 static OSSL_TIME
get_time_cb(void *arg
)
73 QUIC_CONNECTION
*qc
= arg
;
79 * QCTX is a utility structure which provides information we commonly wish to
80 * unwrap upon an API call being dispatched to us, namely:
82 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
84 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
85 * a QCSO with a default stream was passed);
86 * - whether a QSSO was passed (xso == NULL must not be used to determine this
87 * because it may be non-NULL when a QCSO is passed if that QCSO has a
97 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
98 * rather than via ERR. Note that normal errors must always be raised while
102 static int quic_raise_normal_error(QCTX
*ctx
,
106 ctx
->xso
->last_error
= err
;
108 ctx
->qc
->last_error
= err
;
114 * Raise a 'non-normal' error, meaning any error that is not reported via
115 * SSL_get_error() and must be reported via ERR.
117 * qc should be provided if available. In exceptional circumstances when qc is
118 * not known NULL may be passed. This should generally only happen when an
119 * expect_...() function defined below fails, which generally indicates a
120 * dispatch error or caller error.
122 * ctx should be NULL if the connection lock is not held.
124 static int quic_raise_non_normal_error(QCTX
*ctx
,
135 if (ctx
->is_stream
&& ctx
->xso
!= NULL
)
136 ctx
->xso
->last_error
= SSL_ERROR_SSL
;
137 else if (!ctx
->is_stream
&& ctx
->qc
!= NULL
)
138 ctx
->qc
->last_error
= SSL_ERROR_SSL
;
140 if (reason
== SSL_R_PROTOCOL_IS_SHUTDOWN
&& ctx
->qc
!= NULL
)
141 ossl_quic_channel_restore_err_state(ctx
->qc
->ch
);
145 ERR_set_debug(file
, line
, func
);
148 ERR_vset_error(ERR_LIB_SSL
, reason
, fmt
, args
);
154 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
155 quic_raise_normal_error((ctx), (err))
157 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
158 quic_raise_non_normal_error((ctx), \
159 OPENSSL_FILE, OPENSSL_LINE, \
165 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
166 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
168 * After this returns 1, all fields of the passed QCTX are initialised.
169 * Returns 0 on failure. This function is intended to be used to provide API
170 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
172 static int expect_quic(const SSL
*s
, QCTX
*ctx
)
182 return QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_PASSED_NULL_PARAMETER
, NULL
);
185 case SSL_TYPE_QUIC_CONNECTION
:
186 qc
= (QUIC_CONNECTION
*)s
;
188 ctx
->xso
= qc
->default_xso
;
192 case SSL_TYPE_QUIC_XSO
:
200 return QUIC_RAISE_NON_NORMAL_ERROR(NULL
, ERR_R_INTERNAL_ERROR
, NULL
);
205 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
206 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
209 * remote_init determines if we expect the default XSO to be remotely created or
210 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
212 * Channel mutex is acquired and retained on success.
215 static int ossl_unused
expect_quic_with_stream_lock(const SSL
*s
, int remote_init
,
218 if (!expect_quic(s
, ctx
))
223 if (ctx
->xso
== NULL
&& remote_init
>= 0) {
224 if (ossl_quic_channel_is_term_any(ctx
->qc
->ch
)) {
225 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
229 /* If we haven't finished the handshake, try to advance it. */
230 if (quic_do_handshake(ctx
) < 1)
231 /* ossl_quic_do_handshake raised error here */
234 if (remote_init
== 0) {
235 if (!qc_try_create_default_xso_for_write(ctx
))
238 if (!qc_wait_for_default_xso_for_read(ctx
))
242 ctx
->xso
= ctx
->qc
->default_xso
;
245 if (ctx
->xso
== NULL
) {
246 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
250 return 1; /* lock held */
253 quic_unlock(ctx
->qc
);
258 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
259 * be non-NULL if the QCSO has a default stream.
261 static int ossl_unused
expect_quic_conn_only(const SSL
*s
, QCTX
*ctx
)
263 if (!expect_quic(s
, ctx
))
267 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_CONN_USE_ONLY
, NULL
);
273 * Ensures that the channel mutex is held for a method which touches channel
276 * Precondition: Channel mutex is not held (unchecked)
278 static void quic_lock(QUIC_CONNECTION
*qc
)
280 #if defined(OPENSSL_THREADS)
281 ossl_crypto_mutex_lock(qc
->mutex
);
285 /* Precondition: Channel mutex is held (unchecked) */
287 static void quic_unlock(QUIC_CONNECTION
*qc
)
289 #if defined(OPENSSL_THREADS)
290 ossl_crypto_mutex_unlock(qc
->mutex
);
296 * QUIC Front-End I/O API: Initialization
297 * ======================================
299 * SSL_new => ossl_quic_new
301 * SSL_reset => ossl_quic_reset
302 * SSL_clear => ossl_quic_clear
304 * SSL_free => ossl_quic_free
306 * SSL_set_options => ossl_quic_set_options
307 * SSL_get_options => ossl_quic_get_options
308 * SSL_clear_options => ossl_quic_clear_options
313 SSL
*ossl_quic_new(SSL_CTX
*ctx
)
315 QUIC_CONNECTION
*qc
= NULL
;
316 SSL
*ssl_base
= NULL
;
317 SSL_CONNECTION
*sc
= NULL
;
319 qc
= OPENSSL_zalloc(sizeof(*qc
));
323 /* Initialise the QUIC_CONNECTION's stub header. */
325 if (!ossl_ssl_init(ssl_base
, ctx
, ctx
->method
, SSL_TYPE_QUIC_CONNECTION
)) {
330 qc
->tls
= ossl_ssl_connection_new_int(ctx
, TLS_method());
331 if (qc
->tls
== NULL
|| (sc
= SSL_CONNECTION_FROM_SSL(qc
->tls
)) == NULL
)
334 /* override the user_ssl of the inner connection */
335 sc
->s3
.flags
|= TLS1_FLAGS_QUIC
;
337 /* Restrict options derived from the SSL_CTX. */
338 sc
->options
&= OSSL_QUIC_PERMITTED_OPTIONS_CONN
;
341 #if defined(OPENSSL_THREADS)
342 if ((qc
->mutex
= ossl_crypto_mutex_new()) == NULL
)
346 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
347 qc
->is_thread_assisted
348 = (ssl_base
->method
== OSSL_QUIC_client_thread_method());
351 qc
->as_server
= 0; /* TODO(QUIC): server support */
352 qc
->as_server_state
= qc
->as_server
;
354 qc
->default_stream_mode
= SSL_DEFAULT_STREAM_MODE_AUTO_BIDI
;
355 qc
->default_ssl_mode
= qc
->ssl
.ctx
->mode
;
356 qc
->default_ssl_options
= qc
->ssl
.ctx
->options
& OSSL_QUIC_PERMITTED_OPTIONS
;
357 qc
->default_blocking
= 1;
359 qc
->incoming_stream_policy
= SSL_INCOMING_STREAM_POLICY_AUTO
;
360 qc
->last_error
= SSL_ERROR_NONE
;
362 if (!create_channel(qc
))
365 ossl_quic_channel_set_msg_callback(qc
->ch
, ctx
->msg_callback
, ssl_base
);
366 ossl_quic_channel_set_msg_callback_arg(qc
->ch
, ctx
->msg_callback_arg
);
368 qc_update_reject_policy(qc
);
371 * We do not create the default XSO yet. The reason for this is that the
372 * stream ID of the default XSO will depend on whether the stream is client
373 * or server-initiated, which depends on who transmits first. Since we do
374 * not know whether the application will be using a client-transmits-first
375 * or server-transmits-first protocol, we defer default XSO creation until
376 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
377 * we take that as a cue that the client is expecting a server-initiated
378 * stream, and vice versa if SSL_write() is called first.
384 #if defined(OPENSSL_THREADS)
385 ossl_crypto_mutex_free(qc
->mutex
);
387 ossl_quic_channel_free(qc
->ch
);
396 void ossl_quic_free(SSL
*s
)
401 /* We should never be called on anything but a QSO. */
402 if (!expect_quic(s
, &ctx
))
409 * When a QSSO is freed, the XSO is freed immediately, because the XSO
410 * itself only contains API personality layer data. However the
411 * underlying QUIC_STREAM is not freed immediately but is instead marked
412 * as deleted for later collection.
415 assert(ctx
.qc
->num_xso
> 0);
418 /* If a stream's send part has not been finished, auto-reset it. */
419 if (( ctx
.xso
->stream
->send_state
== QUIC_SSTREAM_STATE_READY
420 || ctx
.xso
->stream
->send_state
== QUIC_SSTREAM_STATE_SEND
)
421 && !ossl_quic_sstream_get_final_size(ctx
.xso
->stream
->sstream
, NULL
))
422 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
425 /* Do STOP_SENDING for the receive part, if applicable. */
426 if ( ctx
.xso
->stream
->recv_state
== QUIC_RSTREAM_STATE_RECV
427 || ctx
.xso
->stream
->recv_state
== QUIC_RSTREAM_STATE_SIZE_KNOWN
)
428 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
431 /* Update stream state. */
432 ctx
.xso
->stream
->deleted
= 1;
433 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx
.qc
->ch
),
436 is_default
= (ctx
.xso
== ctx
.qc
->default_xso
);
440 * Unref the connection in most cases; the XSO has a ref to the QC and
441 * not vice versa. But for a default XSO, to avoid circular references,
442 * the QC refs the XSO but the XSO does not ref the QC. If we are the
443 * default XSO, we only get here when the QC is being torn down anyway,
444 * so don't call SSL_free(qc) as we are already in it.
447 SSL_free(&ctx
.qc
->ssl
);
449 /* Note: SSL_free calls OPENSSL_free(xso) for us */
454 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
455 * stage, but is freed during the channel free when the whole QSM is freed.
457 if (ctx
.qc
->default_xso
!= NULL
) {
458 QUIC_XSO
*xso
= ctx
.qc
->default_xso
;
463 ctx
.qc
->default_xso
= NULL
;
466 /* Ensure we have no remaining XSOs. */
467 assert(ctx
.qc
->num_xso
== 0);
469 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
470 if (ctx
.qc
->is_thread_assisted
&& ctx
.qc
->started
) {
471 ossl_quic_thread_assist_wait_stopped(&ctx
.qc
->thread_assist
);
472 ossl_quic_thread_assist_cleanup(&ctx
.qc
->thread_assist
);
476 ossl_quic_channel_free(ctx
.qc
->ch
);
478 BIO_free(ctx
.qc
->net_rbio
);
479 BIO_free(ctx
.qc
->net_wbio
);
481 /* Note: SSL_free calls OPENSSL_free(qc) for us */
483 SSL_free(ctx
.qc
->tls
);
484 quic_unlock(ctx
.qc
); /* tsan doesn't like freeing locked mutexes */
485 #if defined(OPENSSL_THREADS)
486 ossl_crypto_mutex_free(&ctx
.qc
->mutex
);
490 /* SSL method init */
491 int ossl_quic_init(SSL
*s
)
493 /* Same op as SSL_clear, forward the call. */
494 return ossl_quic_clear(s
);
497 /* SSL method deinit */
498 void ossl_quic_deinit(SSL
*s
)
503 /* SSL_clear (ssl_reset method) */
504 int ossl_quic_reset(SSL
*s
)
508 if (!expect_quic(s
, &ctx
))
515 /* ssl_clear method (unused) */
516 int ossl_quic_clear(SSL
*s
)
520 if (!expect_quic(s
, &ctx
))
523 /* TODO(QUIC): Currently a no-op. */
527 int ossl_quic_conn_set_override_now_cb(SSL
*s
,
528 OSSL_TIME (*now_cb
)(void *arg
),
533 if (!expect_quic(s
, &ctx
))
538 ctx
.qc
->override_now_cb
= now_cb
;
539 ctx
.qc
->override_now_cb_arg
= now_cb_arg
;
545 void ossl_quic_conn_force_assist_thread_wake(SSL
*s
)
549 if (!expect_quic(s
, &ctx
))
552 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
553 if (ctx
.qc
->is_thread_assisted
&& ctx
.qc
->started
)
554 ossl_quic_thread_assist_notify_deadline_changed(&ctx
.qc
->thread_assist
);
559 static void qc_touch_default_xso(QUIC_CONNECTION
*qc
)
561 qc
->default_xso_created
= 1;
562 qc_update_reject_policy(qc
);
566 * Changes default XSO. Allows caller to keep reference to the old default XSO
567 * (if any). Reference to new XSO is transferred from caller.
570 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
,
578 if (qc
->default_xso
!= xso
) {
579 *old_xso
= qc
->default_xso
; /* transfer old XSO ref to caller */
581 qc
->default_xso
= xso
;
585 * Changing to not having a default XSO. XSO becomes standalone and
586 * now has a ref to the QC.
588 if (!ossl_assert(SSL_up_ref(&qc
->ssl
)))
592 * Changing from not having a default XSO to having one. The new XSO
593 * will have had a reference to the QC we need to drop to avoid a
594 * circular reference.
596 * Currently we never change directly from one default XSO to
597 * another, though this function would also still be correct if this
600 assert(*old_xso
== NULL
);
602 CRYPTO_DOWN_REF(&qc
->ssl
.references
, &refs
);
608 qc_touch_default_xso(qc
);
612 * Changes default XSO, releasing the reference to any previous default XSO.
613 * Reference to new XSO is transferred from caller.
616 static void qc_set_default_xso(QUIC_CONNECTION
*qc
, QUIC_XSO
*xso
, int touch
)
618 QUIC_XSO
*old_xso
= NULL
;
620 qc_set_default_xso_keep_ref(qc
, xso
, touch
, &old_xso
);
623 SSL_free(&old_xso
->ssl
);
627 static void xso_update_options(QUIC_XSO
*xso
)
629 int cleanse
= ((xso
->ssl_options
& SSL_OP_CLEANSE_PLAINTEXT
) != 0);
631 if (xso
->stream
->rstream
!= NULL
)
632 ossl_quic_rstream_set_cleanse(xso
->stream
->rstream
, cleanse
);
634 if (xso
->stream
->sstream
!= NULL
)
635 ossl_quic_sstream_set_cleanse(xso
->stream
->sstream
, cleanse
);
642 * Setting options on a QCSO
643 * - configures the handshake-layer options;
644 * - configures the default data-plane options for new streams;
645 * - configures the data-plane options on the default XSO, if there is one.
647 * Setting options on a QSSO
648 * - configures data-plane options for that stream only.
651 static uint64_t quic_mask_or_options(SSL
*ssl
, uint64_t mask_value
, uint64_t or_value
)
654 uint64_t hs_mask_value
, hs_or_value
, ret
;
656 if (!expect_quic(ssl
, &ctx
))
661 if (!ctx
.is_stream
) {
663 * If we were called on the connection, we apply any handshake option
666 hs_mask_value
= (mask_value
& OSSL_QUIC_PERMITTED_OPTIONS_CONN
);
667 hs_or_value
= (or_value
& OSSL_QUIC_PERMITTED_OPTIONS_CONN
);
669 SSL_clear_options(ctx
.qc
->tls
, hs_mask_value
);
670 SSL_set_options(ctx
.qc
->tls
, hs_or_value
);
672 /* Update defaults for new streams. */
673 ctx
.qc
->default_ssl_options
674 = ((ctx
.qc
->default_ssl_options
& ~mask_value
) | or_value
)
675 & OSSL_QUIC_PERMITTED_OPTIONS
;
678 if (ctx
.xso
!= NULL
) {
680 = ((ctx
.xso
->ssl_options
& ~mask_value
) | or_value
)
681 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM
;
683 xso_update_options(ctx
.xso
);
686 ret
= ctx
.is_stream
? ctx
.xso
->ssl_options
: ctx
.qc
->default_ssl_options
;
692 uint64_t ossl_quic_set_options(SSL
*ssl
, uint64_t options
)
694 return quic_mask_or_options(ssl
, 0, options
);
697 /* SSL_clear_options */
698 uint64_t ossl_quic_clear_options(SSL
*ssl
, uint64_t options
)
700 return quic_mask_or_options(ssl
, options
, 0);
703 /* SSL_get_options */
704 uint64_t ossl_quic_get_options(const SSL
*ssl
)
706 return quic_mask_or_options((SSL
*)ssl
, 0, 0);
710 * QUIC Front-End I/O API: Network BIO Configuration
711 * =================================================
713 * Handling the different BIOs is difficult:
715 * - It is more or less a requirement that we use non-blocking network I/O;
716 * we need to be able to have timeouts on recv() calls, and make best effort
717 * (non blocking) send() and recv() calls.
719 * The only sensible way to do this is to configure the socket into
720 * non-blocking mode. We could try to do select() before calling send() or
721 * recv() to get a guarantee that the call will not block, but this will
722 * probably run into issues with buggy OSes which generate spurious socket
723 * readiness events. In any case, relying on this to work reliably does not
726 * Timeouts could be handled via setsockopt() socket timeout options, but
727 * this depends on OS support and adds another syscall to every network I/O
728 * operation. It also has obvious thread safety concerns if we want to move
729 * to concurrent use of a single socket at some later date.
731 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
732 * be made non-blocking. However some OSes (e.g. Windows) do not support
733 * this, so we cannot rely on this.
735 * As such, we need to configure any FD in non-blocking mode. This may
736 * confound users who pass a blocking socket to libssl. However, in practice
737 * it would be extremely strange for a user of QUIC to pass an FD to us,
738 * then also try and send receive traffic on the same socket(!). Thus the
739 * impact of this should be limited, and can be documented.
741 * - We support both blocking and non-blocking operation in terms of the API
742 * presented to the user. One prospect is to set the blocking mode based on
743 * whether the socket passed to us was already in blocking mode. However,
744 * Windows has no API for determining if a socket is in blocking mode (!),
745 * therefore this cannot be done portably. Currently therefore we expose an
746 * explicit API call to set this, and default to blocking mode.
748 * - We need to determine our initial destination UDP address. The "natural"
749 * way for a user to do this is to set the peer variable on a BIO_dgram.
750 * However, this has problems because BIO_dgram's peer variable is used for
751 * both transmission and reception. This means it can be constantly being
752 * changed to a malicious value (e.g. if some random unrelated entity on the
753 * network starts sending traffic to us) on every read call. This is not a
754 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
755 * calls only, which do not use this variable. However, we do need to let
756 * the user specify the peer in a 'normal' manner. The compromise here is
757 * that we grab the current peer value set at the time the write BIO is set
758 * and do not read the value again.
760 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
761 * Currently we do this by only supporting non-blocking mode.
766 * Determines what initial destination UDP address we should use, if possible.
767 * If this fails the client must set the destination address manually, or use a
768 * BIO which does not need a destination address.
770 static int csm_analyse_init_peer_addr(BIO
*net_wbio
, BIO_ADDR
*peer
)
772 if (BIO_dgram_get_peer(net_wbio
, peer
) <= 0)
778 void ossl_quic_conn_set0_net_rbio(SSL
*s
, BIO
*net_rbio
)
782 if (!expect_quic(s
, &ctx
))
785 if (ctx
.qc
->net_rbio
== net_rbio
)
788 if (!ossl_quic_channel_set_net_rbio(ctx
.qc
->ch
, net_rbio
))
791 BIO_free(ctx
.qc
->net_rbio
);
792 ctx
.qc
->net_rbio
= net_rbio
;
795 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
796 * mode as we do not support it for non-pollable BIOs.
798 if (net_rbio
!= NULL
) {
799 BIO_POLL_DESCRIPTOR d
= {0};
801 if (!BIO_get_rpoll_descriptor(net_rbio
, &d
)
802 || d
.type
!= BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD
) {
803 ctx
.qc
->blocking
= 0;
804 ctx
.qc
->default_blocking
= 0;
805 ctx
.qc
->can_poll_net_rbio
= 0;
807 ctx
.qc
->can_poll_net_rbio
= 1;
812 void ossl_quic_conn_set0_net_wbio(SSL
*s
, BIO
*net_wbio
)
816 if (!expect_quic(s
, &ctx
))
819 if (ctx
.qc
->net_wbio
== net_wbio
)
822 if (!ossl_quic_channel_set_net_wbio(ctx
.qc
->ch
, net_wbio
))
825 BIO_free(ctx
.qc
->net_wbio
);
826 ctx
.qc
->net_wbio
= net_wbio
;
828 if (net_wbio
!= NULL
) {
829 BIO_POLL_DESCRIPTOR d
= {0};
831 if (!BIO_get_wpoll_descriptor(net_wbio
, &d
)
832 || d
.type
!= BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD
) {
833 ctx
.qc
->blocking
= 0;
834 ctx
.qc
->default_blocking
= 0;
835 ctx
.qc
->can_poll_net_wbio
= 0;
837 ctx
.qc
->can_poll_net_wbio
= 1;
841 * If we do not have a peer address yet, and we have not started trying
842 * to connect yet, try to autodetect one.
844 if (BIO_ADDR_family(&ctx
.qc
->init_peer_addr
) == AF_UNSPEC
845 && !ctx
.qc
->started
) {
846 if (!csm_analyse_init_peer_addr(net_wbio
, &ctx
.qc
->init_peer_addr
))
848 BIO_ADDR_clear(&ctx
.qc
->init_peer_addr
);
850 ossl_quic_channel_set_peer_addr(ctx
.qc
->ch
,
851 &ctx
.qc
->init_peer_addr
);
856 BIO
*ossl_quic_conn_get_net_rbio(const SSL
*s
)
860 if (!expect_quic(s
, &ctx
))
863 return ctx
.qc
->net_rbio
;
866 BIO
*ossl_quic_conn_get_net_wbio(const SSL
*s
)
870 if (!expect_quic(s
, &ctx
))
873 return ctx
.qc
->net_wbio
;
876 int ossl_quic_conn_get_blocking_mode(const SSL
*s
)
880 if (!expect_quic(s
, &ctx
))
884 return ctx
.xso
->blocking
;
886 return ctx
.qc
->blocking
;
889 int ossl_quic_conn_set_blocking_mode(SSL
*s
, int blocking
)
893 if (!expect_quic(s
, &ctx
))
896 /* Cannot enable blocking mode if we do not have pollable FDs. */
898 (!ctx
.qc
->can_poll_net_rbio
|| !ctx
.qc
->can_poll_net_wbio
))
899 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_UNSUPPORTED
, NULL
);
901 if (!ctx
.is_stream
) {
903 * If called on a QCSO, update default and connection-level blocking
906 ctx
.qc
->blocking
= (blocking
!= 0);
907 ctx
.qc
->default_blocking
= ctx
.qc
->blocking
;
912 * If called on a QSSO or QCSO with a default XSO, update blocking
915 ctx
.xso
->blocking
= (blocking
!= 0);
920 int ossl_quic_conn_set_initial_peer_addr(SSL
*s
,
921 const BIO_ADDR
*peer_addr
)
925 if (!expect_quic(s
, &ctx
))
929 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
932 if (peer_addr
== NULL
) {
933 BIO_ADDR_clear(&ctx
.qc
->init_peer_addr
);
937 ctx
.qc
->init_peer_addr
= *peer_addr
;
942 * QUIC Front-End I/O API: Asynchronous I/O Management
943 * ===================================================
945 * (BIO/)SSL_handle_events => ossl_quic_handle_events
946 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
947 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
951 /* Returns 1 if the connection is being used in blocking mode. */
952 static int qc_blocking_mode(const QUIC_CONNECTION
*qc
)
957 static int xso_blocking_mode(const QUIC_XSO
*xso
)
960 && xso
->conn
->can_poll_net_rbio
961 && xso
->conn
->can_poll_net_wbio
;
964 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
966 int ossl_quic_handle_events(SSL
*s
)
970 if (!expect_quic(s
, &ctx
))
974 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
980 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
981 * should next have events handled by the application by calling
982 * SSL_handle_events(). tv is set to 0 if the object should have events handled
983 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
984 * the value of *tv is undefined.
987 int ossl_quic_get_event_timeout(SSL
*s
, struct timeval
*tv
, int *is_infinite
)
990 OSSL_TIME deadline
= ossl_time_infinite();
992 if (!expect_quic(s
, &ctx
))
998 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1000 if (ossl_time_is_infinite(deadline
)) {
1004 * Robustness against faulty applications that don't check *is_infinite;
1005 * harmless long timeout.
1007 tv
->tv_sec
= 1000000;
1010 quic_unlock(ctx
.qc
);
1014 *tv
= ossl_time_to_timeval(ossl_time_subtract(deadline
, get_time(ctx
.qc
)));
1016 quic_unlock(ctx
.qc
);
1020 /* SSL_get_rpoll_descriptor */
1021 int ossl_quic_get_rpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
1025 if (!expect_quic(s
, &ctx
))
1028 if (desc
== NULL
|| ctx
.qc
->net_rbio
== NULL
)
1031 return BIO_get_rpoll_descriptor(ctx
.qc
->net_rbio
, desc
);
1034 /* SSL_get_wpoll_descriptor */
1035 int ossl_quic_get_wpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
1039 if (!expect_quic(s
, &ctx
))
1042 if (desc
== NULL
|| ctx
.qc
->net_wbio
== NULL
)
1045 return BIO_get_wpoll_descriptor(ctx
.qc
->net_wbio
, desc
);
1048 /* SSL_net_read_desired */
1050 int ossl_quic_get_net_read_desired(SSL
*s
)
1055 if (!expect_quic(s
, &ctx
))
1059 ret
= ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1060 quic_unlock(ctx
.qc
);
1064 /* SSL_net_write_desired */
1066 int ossl_quic_get_net_write_desired(SSL
*s
)
1071 if (!expect_quic(s
, &ctx
))
1075 ret
= ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx
.qc
->ch
));
1076 quic_unlock(ctx
.qc
);
1081 * QUIC Front-End I/O API: Connection Lifecycle Operations
1082 * =======================================================
1084 * SSL_do_handshake => ossl_quic_do_handshake
1085 * SSL_set_connect_state => ossl_quic_set_connect_state
1086 * SSL_set_accept_state => ossl_quic_set_accept_state
1087 * SSL_shutdown => ossl_quic_shutdown
1088 * SSL_ctrl => ossl_quic_ctrl
1089 * (BIO/)SSL_connect => ossl_quic_connect
1090 * (BIO/)SSL_accept => ossl_quic_accept
1095 static int quic_shutdown_wait(void *arg
)
1097 QUIC_CONNECTION
*qc
= arg
;
1099 return ossl_quic_channel_is_terminated(qc
->ch
);
1103 int ossl_quic_conn_shutdown(SSL
*s
, uint64_t flags
,
1104 const SSL_SHUTDOWN_EX_ARGS
*args
,
1110 if (!expect_quic(s
, &ctx
))
1114 /* TODO(QUIC): Semantics currently undefined for QSSOs */
1119 ossl_quic_channel_local_close(ctx
.qc
->ch
,
1120 args
!= NULL
? args
->quic_error_code
: 0);
1122 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
1124 if (ossl_quic_channel_is_terminated(ctx
.qc
->ch
)) {
1125 quic_unlock(ctx
.qc
);
1129 if (qc_blocking_mode(ctx
.qc
) && (flags
& SSL_SHUTDOWN_FLAG_RAPID
) == 0)
1130 block_until_pred(ctx
.qc
, quic_shutdown_wait
, ctx
.qc
, 0);
1132 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
1134 ret
= ossl_quic_channel_is_terminated(ctx
.qc
->ch
);
1135 quic_unlock(ctx
.qc
);
1140 long ossl_quic_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
1144 if (!expect_quic(s
, &ctx
))
1149 /* If called on a QCSO, update the default mode. */
1151 ctx
.qc
->default_ssl_mode
|= (uint32_t)larg
;
1154 * If we were called on a QSSO or have a default stream, we also update
1157 if (ctx
.xso
!= NULL
) {
1158 /* Cannot enable EPW while AON write in progress. */
1159 if (ctx
.xso
->aon_write_in_progress
)
1160 larg
&= ~SSL_MODE_ENABLE_PARTIAL_WRITE
;
1162 ctx
.xso
->ssl_mode
|= (uint32_t)larg
;
1163 return ctx
.xso
->ssl_mode
;
1166 return ctx
.qc
->default_ssl_mode
;
1167 case SSL_CTRL_CLEAR_MODE
:
1169 ctx
.qc
->default_ssl_mode
&= ~(uint32_t)larg
;
1171 if (ctx
.xso
!= NULL
) {
1172 ctx
.xso
->ssl_mode
&= ~(uint32_t)larg
;
1173 return ctx
.xso
->ssl_mode
;
1176 return ctx
.qc
->default_ssl_mode
;
1178 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1179 ossl_quic_channel_set_msg_callback_arg(ctx
.qc
->ch
, parg
);
1180 /* This ctrl also needs to be passed to the internal SSL object */
1181 return SSL_ctrl(ctx
.qc
->tls
, cmd
, larg
, parg
);
1183 case DTLS_CTRL_GET_TIMEOUT
: /* DTLSv1_get_timeout */
1187 if (!ossl_quic_get_event_timeout(s
, parg
, &is_infinite
))
1190 return !is_infinite
;
1192 case DTLS_CTRL_HANDLE_TIMEOUT
: /* DTLSv1_handle_timeout */
1193 /* For legacy compatibility with DTLS calls. */
1194 return ossl_quic_handle_events(s
) == 1 ? 1 : -1;
1196 /* Probably a TLS related ctrl. Defer to our internal SSL object */
1197 return SSL_ctrl(ctx
.qc
->tls
, cmd
, larg
, parg
);
1201 /* SSL_set_connect_state */
1202 void ossl_quic_set_connect_state(SSL
*s
)
1206 if (!expect_quic(s
, &ctx
))
1209 /* Cannot be changed after handshake started */
1210 if (ctx
.qc
->started
|| ctx
.is_stream
)
1213 ctx
.qc
->as_server_state
= 0;
1216 /* SSL_set_accept_state */
1217 void ossl_quic_set_accept_state(SSL
*s
)
1221 if (!expect_quic(s
, &ctx
))
1224 /* Cannot be changed after handshake started */
1225 if (ctx
.qc
->started
|| ctx
.is_stream
)
1228 ctx
.qc
->as_server_state
= 1;
1231 /* SSL_do_handshake */
1232 struct quic_handshake_wait_args
{
1233 QUIC_CONNECTION
*qc
;
1236 static int quic_handshake_wait(void *arg
)
1238 struct quic_handshake_wait_args
*args
= arg
;
1240 if (!ossl_quic_channel_is_active(args
->qc
->ch
))
1243 if (ossl_quic_channel_is_handshake_complete(args
->qc
->ch
))
1249 static int configure_channel(QUIC_CONNECTION
*qc
)
1251 assert(qc
->ch
!= NULL
);
1253 if (!ossl_quic_channel_set_net_rbio(qc
->ch
, qc
->net_rbio
)
1254 || !ossl_quic_channel_set_net_wbio(qc
->ch
, qc
->net_wbio
)
1255 || !ossl_quic_channel_set_peer_addr(qc
->ch
, &qc
->init_peer_addr
))
1262 static int create_channel(QUIC_CONNECTION
*qc
)
1264 QUIC_CHANNEL_ARGS args
= {0};
1266 args
.libctx
= qc
->ssl
.ctx
->libctx
;
1267 args
.propq
= qc
->ssl
.ctx
->propq
;
1268 args
.is_server
= qc
->as_server
;
1270 args
.mutex
= qc
->mutex
;
1271 args
.now_cb
= get_time_cb
;
1272 args
.now_cb_arg
= qc
;
1274 qc
->ch
= ossl_quic_channel_new(&args
);
1282 * Creates a channel and configures it with the information we have accumulated
1283 * via calls made to us from the application prior to starting a handshake
1287 static int ensure_channel_started(QUIC_CONNECTION
*qc
)
1290 if (!configure_channel(qc
)
1291 || !ossl_quic_channel_start(qc
->ch
))
1294 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1295 if (qc
->is_thread_assisted
)
1296 if (!ossl_quic_thread_assist_init_start(&qc
->thread_assist
, qc
->ch
))
1305 ossl_quic_channel_free(qc
->ch
);
1311 static int quic_do_handshake(QCTX
*ctx
)
1314 QUIC_CONNECTION
*qc
= ctx
->qc
;
1316 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1317 /* Handshake already completed. */
1320 if (ossl_quic_channel_is_term_any(qc
->ch
))
1321 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1323 if (BIO_ADDR_family(&qc
->init_peer_addr
) == AF_UNSPEC
) {
1324 /* Peer address must have been set. */
1325 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET
, NULL
);
1326 return -1; /* Non-protocol error */
1329 if (qc
->as_server
!= qc
->as_server_state
) {
1330 /* TODO(QUIC): Must match the method used to create the QCSO */
1331 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_PASSED_INVALID_ARGUMENT
, NULL
);
1332 return -1; /* Non-protocol error */
1335 if (qc
->net_rbio
== NULL
|| qc
->net_wbio
== NULL
) {
1336 /* Need read and write BIOs. */
1337 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_BIO_NOT_SET
, NULL
);
1338 return -1; /* Non-protocol error */
1342 * Start connection process. Note we may come here multiple times in
1343 * non-blocking mode, which is fine.
1345 if (!ensure_channel_started(qc
)) {
1346 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1347 return -1; /* Non-protocol error */
1350 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1351 /* The handshake is now done. */
1354 if (qc_blocking_mode(qc
)) {
1355 /* In blocking mode, wait for the handshake to complete. */
1356 struct quic_handshake_wait_args args
;
1360 ret
= block_until_pred(qc
, quic_handshake_wait
, &args
, 0);
1361 if (!ossl_quic_channel_is_active(qc
->ch
)) {
1362 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1363 return 0; /* Shutdown before completion */
1364 } else if (ret
<= 0) {
1365 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1366 return -1; /* Non-protocol error */
1369 assert(ossl_quic_channel_is_handshake_complete(qc
->ch
));
1372 /* Try to advance the reactor. */
1373 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc
->ch
), 0);
1375 if (ossl_quic_channel_is_handshake_complete(qc
->ch
))
1376 /* The handshake is now done. */
1379 /* Otherwise, indicate that the handshake isn't done yet. */
1380 QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_READ
);
1381 return -1; /* Non-protocol error */
1386 int ossl_quic_do_handshake(SSL
*s
)
1391 if (!expect_quic(s
, &ctx
))
1396 ret
= quic_do_handshake(&ctx
);
1397 quic_unlock(ctx
.qc
);
1402 int ossl_quic_connect(SSL
*s
)
1404 /* Ensure we are in connect state (no-op if non-idle). */
1405 ossl_quic_set_connect_state(s
);
1407 /* Begin or continue the handshake */
1408 return ossl_quic_do_handshake(s
);
1412 int ossl_quic_accept(SSL
*s
)
1414 /* Ensure we are in accept state (no-op if non-idle). */
1415 ossl_quic_set_accept_state(s
);
1417 /* Begin or continue the handshake */
1418 return ossl_quic_do_handshake(s
);
1422 * QUIC Front-End I/O API: Stream Lifecycle Operations
1423 * ===================================================
1425 * SSL_stream_new => ossl_quic_conn_stream_new
1430 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1431 * default XSO was created. Returns 0 if it was not (e.g. because it already
1432 * exists). Note that this is NOT an error condition.
1435 static int qc_try_create_default_xso_for_write(QCTX
*ctx
)
1438 QUIC_CONNECTION
*qc
= ctx
->qc
;
1440 if (qc
->default_xso_created
1441 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
1443 * We only do this once. If the user detaches a previously created
1444 * default XSO we don't auto-create another one.
1446 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
1448 /* Create a locally-initiated stream. */
1449 if (qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_AUTO_UNI
)
1450 flags
|= SSL_STREAM_FLAG_UNI
;
1452 qc_set_default_xso(qc
, (QUIC_XSO
*)quic_conn_stream_new(ctx
, flags
,
1455 if (qc
->default_xso
== NULL
)
1456 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1458 qc_touch_default_xso(qc
);
1462 struct quic_wait_for_stream_args
{
1463 QUIC_CONNECTION
*qc
;
1470 static int quic_wait_for_stream(void *arg
)
1472 struct quic_wait_for_stream_args
*args
= arg
;
1474 if (!ossl_quic_channel_is_active(args
->qc
->ch
)) {
1475 /* If connection is torn down due to an error while blocking, stop. */
1476 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1480 args
->qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args
->qc
->ch
),
1481 args
->expect_id
| QUIC_STREAM_DIR_BIDI
);
1482 if (args
->qs
== NULL
)
1483 args
->qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args
->qc
->ch
),
1484 args
->expect_id
| QUIC_STREAM_DIR_UNI
);
1486 if (args
->qs
!= NULL
)
1487 return 1; /* stream now exists */
1489 return 0; /* did not get a stream, keep trying */
1493 static int qc_wait_for_default_xso_for_read(QCTX
*ctx
)
1495 /* Called on a QCSO and we don't currently have a default stream. */
1497 QUIC_CONNECTION
*qc
= ctx
->qc
;
1500 struct quic_wait_for_stream_args wargs
;
1503 * If default stream functionality is disabled or we already detached
1504 * one, don't make another default stream and just fail.
1506 if (qc
->default_xso_created
1507 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
1508 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_NO_STREAM
, NULL
);
1511 * The peer may have opened a stream since we last ticked. So tick and
1512 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1513 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1514 * first stream created by a peer must have an ordinal of 0.
1516 expect_id
= qc
->as_server
1517 ? QUIC_STREAM_INITIATOR_CLIENT
1518 : QUIC_STREAM_INITIATOR_SERVER
;
1520 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1521 expect_id
| QUIC_STREAM_DIR_BIDI
);
1523 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1524 expect_id
| QUIC_STREAM_DIR_UNI
);
1527 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc
->ch
), 0);
1529 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc
->ch
),
1534 if (!qc_blocking_mode(qc
))
1535 /* Non-blocking mode, so just bail immediately. */
1536 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_READ
);
1538 /* Block until we have a stream. */
1542 wargs
.expect_id
= expect_id
;
1544 res
= block_until_pred(qc
, quic_wait_for_stream
, &wargs
, 0);
1546 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1547 else if (res
< 0 || wargs
.qs
== NULL
)
1548 /* quic_wait_for_stream raised error here */
1555 * We now have qs != NULL. Make it the default stream, creating the
1558 qc_set_default_xso(qc
, create_xso_from_stream(qc
, qs
), /*touch=*/0);
1559 if (qc
->default_xso
== NULL
)
1560 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1562 qc_touch_default_xso(qc
); /* inhibits default XSO */
1567 static QUIC_XSO
*create_xso_from_stream(QUIC_CONNECTION
*qc
, QUIC_STREAM
*qs
)
1569 QUIC_XSO
*xso
= NULL
;
1571 if ((xso
= OPENSSL_zalloc(sizeof(*xso
))) == NULL
)
1574 if (!ossl_ssl_init(&xso
->ssl
, qc
->ssl
.ctx
, qc
->ssl
.method
, SSL_TYPE_QUIC_XSO
))
1578 if (!SSL_up_ref(&qc
->ssl
))
1582 xso
->blocking
= qc
->default_blocking
;
1583 xso
->ssl_mode
= qc
->default_ssl_mode
;
1585 = qc
->default_ssl_options
& OSSL_QUIC_PERMITTED_OPTIONS_STREAM
;
1586 xso
->last_error
= SSL_ERROR_NONE
;
1591 xso_update_options(xso
);
1599 /* locking depends on need_lock */
1600 static SSL
*quic_conn_stream_new(QCTX
*ctx
, uint64_t flags
, int need_lock
)
1602 QUIC_CONNECTION
*qc
= ctx
->qc
;
1603 QUIC_XSO
*xso
= NULL
;
1604 QUIC_STREAM
*qs
= NULL
;
1605 int is_uni
= ((flags
& SSL_STREAM_FLAG_UNI
) != 0);
1610 if (ossl_quic_channel_is_term_any(qc
->ch
)) {
1611 QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1615 qs
= ossl_quic_channel_new_stream_local(qc
->ch
, is_uni
);
1619 xso
= create_xso_from_stream(qc
, qs
);
1623 qc_touch_default_xso(qc
); /* inhibits default XSO */
1631 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc
->ch
), qs
);
1640 SSL
*ossl_quic_conn_stream_new(SSL
*s
, uint64_t flags
)
1644 if (!expect_quic_conn_only(s
, &ctx
))
1647 return quic_conn_stream_new(&ctx
, flags
, /*need_lock=*/1);
1651 * QUIC Front-End I/O API: Steady-State Operations
1652 * ===============================================
1654 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1655 * is, the functions used during the established phase of a QUIC connection
1656 * (e.g. SSL_read, SSL_write).
1658 * Each function must handle both blocking and non-blocking modes. As discussed
1659 * above, all QUIC I/O is implemented using non-blocking mode internally.
1661 * SSL_get_error => partially implemented by ossl_quic_get_error
1662 * (BIO/)SSL_read => ossl_quic_read
1663 * (BIO/)SSL_write => ossl_quic_write
1664 * SSL_pending => ossl_quic_pending
1665 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1666 * SSL_key_update => ossl_quic_key_update
1670 int ossl_quic_get_error(const SSL
*s
, int i
)
1673 int net_error
, last_error
;
1675 if (!expect_quic(s
, &ctx
))
1679 net_error
= ossl_quic_channel_net_error(ctx
.qc
->ch
);
1680 last_error
= ctx
.is_stream
? ctx
.xso
->last_error
: ctx
.qc
->last_error
;
1681 quic_unlock(ctx
.qc
);
1684 return SSL_ERROR_SYSCALL
;
1693 * The set of functions below provide the implementation of the public SSL_write
1694 * function. We must handle:
1696 * - both blocking and non-blocking operation at the application level,
1697 * depending on how we are configured;
1699 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1701 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1705 static void quic_post_write(QUIC_XSO
*xso
, int did_append
, int do_tick
)
1708 * We have appended at least one byte to the stream.
1709 * Potentially mark stream as active, depending on FC.
1712 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso
->conn
->ch
),
1718 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1719 * plus we should eventually consider Nagle's algorithm.
1722 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso
->conn
->ch
), 0);
1725 struct quic_write_again_args
{
1727 const unsigned char *buf
;
1729 size_t total_written
;
1734 static int quic_write_again(void *arg
)
1736 struct quic_write_again_args
*args
= arg
;
1737 size_t actual_written
= 0;
1739 if (!ossl_quic_channel_is_active(args
->xso
->conn
->ch
))
1740 /* If connection is torn down due to an error while blocking, stop. */
1743 if (!quic_validate_for_write(args
->xso
, &args
->err
))
1745 * Stream may have become invalid for write due to connection events
1750 args
->err
= ERR_R_INTERNAL_ERROR
;
1751 if (!ossl_quic_sstream_append(args
->xso
->stream
->sstream
,
1752 args
->buf
, args
->len
, &actual_written
))
1755 quic_post_write(args
->xso
, actual_written
> 0, 0);
1757 args
->buf
+= actual_written
;
1758 args
->len
-= actual_written
;
1759 args
->total_written
+= actual_written
;
1762 /* Written everything, done. */
1765 /* Not written everything yet, keep trying. */
1770 static int quic_write_blocking(QCTX
*ctx
, const void *buf
, size_t len
,
1774 QUIC_XSO
*xso
= ctx
->xso
;
1775 struct quic_write_again_args args
;
1776 size_t actual_written
= 0;
1778 /* First make a best effort to append as much of the data as possible. */
1779 if (!ossl_quic_sstream_append(xso
->stream
->sstream
, buf
, len
,
1781 /* Stream already finished or allocation error. */
1783 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1786 quic_post_write(xso
, actual_written
> 0, 1);
1788 if (actual_written
== len
) {
1789 /* Managed to append everything on the first try. */
1790 *written
= actual_written
;
1795 * We did not manage to append all of the data immediately, so the stream
1796 * buffer has probably filled up. This means we need to block until some of
1800 args
.buf
= (const unsigned char *)buf
+ actual_written
;
1801 args
.len
= len
- actual_written
;
1802 args
.total_written
= 0;
1803 args
.err
= ERR_R_INTERNAL_ERROR
;
1805 res
= block_until_pred(xso
->conn
, quic_write_again
, &args
, 0);
1807 if (!ossl_quic_channel_is_active(xso
->conn
->ch
))
1808 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
1810 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, args
.err
, NULL
);
1813 *written
= args
.total_written
;
1818 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1821 static void aon_write_begin(QUIC_XSO
*xso
, const unsigned char *buf
,
1822 size_t buf_len
, size_t already_sent
)
1824 assert(!xso
->aon_write_in_progress
);
1826 xso
->aon_write_in_progress
= 1;
1827 xso
->aon_buf_base
= buf
;
1828 xso
->aon_buf_pos
= already_sent
;
1829 xso
->aon_buf_len
= buf_len
;
1832 static void aon_write_finish(QUIC_XSO
*xso
)
1834 xso
->aon_write_in_progress
= 0;
1835 xso
->aon_buf_base
= NULL
;
1836 xso
->aon_buf_pos
= 0;
1837 xso
->aon_buf_len
= 0;
1841 static int quic_write_nonblocking_aon(QCTX
*ctx
, const void *buf
,
1842 size_t len
, size_t *written
)
1844 QUIC_XSO
*xso
= ctx
->xso
;
1845 const void *actual_buf
;
1846 size_t actual_len
, actual_written
= 0;
1847 int accept_moving_buffer
1848 = ((xso
->ssl_mode
& SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
) != 0);
1850 if (xso
->aon_write_in_progress
) {
1852 * We are in the middle of an AON write (i.e., a previous write did not
1853 * manage to append all data to the SSTREAM and we have Enable Partial
1854 * Write (EPW) mode disabled.)
1856 if ((!accept_moving_buffer
&& xso
->aon_buf_base
!= buf
)
1857 || len
!= xso
->aon_buf_len
)
1859 * Pointer must not have changed if we are not in accept moving
1860 * buffer mode. Length must never change.
1862 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, SSL_R_BAD_WRITE_RETRY
, NULL
);
1864 actual_buf
= (unsigned char *)buf
+ xso
->aon_buf_pos
;
1865 actual_len
= len
- xso
->aon_buf_pos
;
1866 assert(actual_len
> 0);
1872 /* First make a best effort to append as much of the data as possible. */
1873 if (!ossl_quic_sstream_append(xso
->stream
->sstream
, actual_buf
, actual_len
,
1875 /* Stream already finished or allocation error. */
1877 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1880 quic_post_write(xso
, actual_written
> 0, 1);
1882 if (actual_written
== actual_len
) {
1883 /* We have sent everything. */
1884 if (xso
->aon_write_in_progress
) {
1886 * We have sent everything, and we were in the middle of an AON
1887 * write. The output write length is the total length of the AON
1888 * buffer, not however many bytes we managed to write to the stream
1891 *written
= xso
->aon_buf_len
;
1892 aon_write_finish(xso
);
1894 *written
= actual_written
;
1900 if (xso
->aon_write_in_progress
) {
1902 * AON write is in progress but we have not written everything yet. We
1903 * may have managed to send zero bytes, or some number of bytes less
1904 * than the total remaining which need to be appended during this
1907 xso
->aon_buf_pos
+= actual_written
;
1908 assert(xso
->aon_buf_pos
< xso
->aon_buf_len
);
1909 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_WRITE
);
1913 * Not in an existing AON operation but partial write is not enabled, so we
1914 * need to begin a new AON operation. However we needn't bother if we didn't
1915 * actually append anything.
1917 if (actual_written
> 0)
1918 aon_write_begin(xso
, buf
, len
, actual_written
);
1921 * AON - We do not publicly admit to having appended anything until AON
1925 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_WANT_WRITE
);
1929 static int quic_write_nonblocking_epw(QCTX
*ctx
, const void *buf
, size_t len
,
1932 QUIC_XSO
*xso
= ctx
->xso
;
1934 /* Simple best effort operation. */
1935 if (!ossl_quic_sstream_append(xso
->stream
->sstream
, buf
, len
, written
)) {
1936 /* Stream already finished or allocation error. */
1938 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
1941 quic_post_write(xso
, *written
> 0, 1);
1946 static int quic_validate_for_write(QUIC_XSO
*xso
, int *err
)
1948 QUIC_STREAM_MAP
*qsm
;
1950 if (xso
== NULL
|| xso
->stream
== NULL
) {
1951 *err
= ERR_R_INTERNAL_ERROR
;
1955 switch (xso
->stream
->send_state
) {
1957 case QUIC_SSTREAM_STATE_NONE
:
1958 *err
= SSL_R_STREAM_RECV_ONLY
;
1961 case QUIC_SSTREAM_STATE_READY
:
1962 qsm
= ossl_quic_channel_get_qsm(xso
->conn
->ch
);
1964 if (!ossl_quic_stream_map_ensure_send_part_id(qsm
, xso
->stream
)) {
1965 *err
= ERR_R_INTERNAL_ERROR
;
1970 case QUIC_SSTREAM_STATE_SEND
:
1971 case QUIC_SSTREAM_STATE_DATA_SENT
:
1972 case QUIC_SSTREAM_STATE_DATA_RECVD
:
1973 if (ossl_quic_sstream_get_final_size(xso
->stream
->sstream
, NULL
)) {
1974 *err
= SSL_R_STREAM_FINISHED
;
1980 case QUIC_SSTREAM_STATE_RESET_SENT
:
1981 case QUIC_SSTREAM_STATE_RESET_RECVD
:
1982 *err
= SSL_R_STREAM_RESET
;
1988 int ossl_quic_write(SSL
*s
, const void *buf
, size_t len
, size_t *written
)
1992 int partial_write
, err
;
1999 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/0, &ctx
))
2002 partial_write
= ((ctx
.xso
->ssl_mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
) != 0);
2004 if (ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
2005 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2010 * If we haven't finished the handshake, try to advance it.
2011 * We don't accept writes until the handshake is completed.
2013 if (quic_do_handshake(&ctx
) < 1) {
2018 /* Ensure correct stream state, stream send part not concluded, etc. */
2019 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2020 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2024 if (xso_blocking_mode(ctx
.xso
))
2025 ret
= quic_write_blocking(&ctx
, buf
, len
, written
);
2026 else if (partial_write
)
2027 ret
= quic_write_nonblocking_epw(&ctx
, buf
, len
, written
);
2029 ret
= quic_write_nonblocking_aon(&ctx
, buf
, len
, written
);
2032 quic_unlock(ctx
.qc
);
2040 struct quic_read_again_args
{
2042 QUIC_STREAM
*stream
;
2050 static int quic_validate_for_read(QUIC_XSO
*xso
, int *err
, int *eos
)
2052 QUIC_STREAM_MAP
*qsm
;
2056 if (xso
== NULL
|| xso
->stream
== NULL
) {
2057 *err
= ERR_R_INTERNAL_ERROR
;
2061 switch (xso
->stream
->recv_state
) {
2063 case QUIC_RSTREAM_STATE_NONE
:
2064 *err
= SSL_R_STREAM_SEND_ONLY
;
2067 case QUIC_RSTREAM_STATE_RECV
:
2068 case QUIC_RSTREAM_STATE_SIZE_KNOWN
:
2069 case QUIC_RSTREAM_STATE_DATA_RECVD
:
2072 case QUIC_RSTREAM_STATE_DATA_READ
:
2076 case QUIC_RSTREAM_STATE_RESET_RECVD
:
2077 qsm
= ossl_quic_channel_get_qsm(xso
->conn
->ch
);
2078 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm
, xso
->stream
);
2081 case QUIC_RSTREAM_STATE_RESET_READ
:
2082 *err
= SSL_R_STREAM_RESET
;
2088 static int quic_read_actual(QCTX
*ctx
,
2089 QUIC_STREAM
*stream
,
2090 void *buf
, size_t buf_len
,
2094 int is_fin
= 0, err
, eos
;
2095 QUIC_CONNECTION
*qc
= ctx
->qc
;
2097 if (!quic_validate_for_read(ctx
->xso
, &err
, &eos
)) {
2099 return QUIC_RAISE_NORMAL_ERROR(ctx
, SSL_ERROR_ZERO_RETURN
);
2101 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, err
, NULL
);
2105 if (!ossl_quic_rstream_peek(stream
->rstream
, buf
, buf_len
,
2106 bytes_read
, &is_fin
))
2107 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2110 if (!ossl_quic_rstream_read(stream
->rstream
, buf
, buf_len
,
2111 bytes_read
, &is_fin
))
2112 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2116 if (*bytes_read
> 0) {
2118 * We have read at least one byte from the stream. Inform stream-level
2119 * RXFC of the retirement of controlled bytes. Update the active stream
2120 * status (the RXFC may now want to emit a frame granting more credit to
2123 OSSL_RTT_INFO rtt_info
;
2125 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc
->ch
), &rtt_info
);
2127 if (!ossl_quic_rxfc_on_retire(&stream
->rxfc
, *bytes_read
,
2128 rtt_info
.smoothed_rtt
))
2129 return QUIC_RAISE_NON_NORMAL_ERROR(ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2132 if (is_fin
&& !peek
) {
2133 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(ctx
->qc
->ch
);
2135 ossl_quic_stream_map_notify_totally_read(qsm
, ctx
->xso
->stream
);
2138 if (*bytes_read
> 0)
2139 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc
->ch
),
2147 static int quic_read_again(void *arg
)
2149 struct quic_read_again_args
*args
= arg
;
2151 if (!ossl_quic_channel_is_active(args
->ctx
->qc
->ch
)) {
2152 /* If connection is torn down due to an error while blocking, stop. */
2153 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2157 if (!quic_read_actual(args
->ctx
, args
->stream
,
2158 args
->buf
, args
->len
, args
->bytes_read
,
2162 if (*args
->bytes_read
> 0)
2163 /* got at least one byte, the SSL_read op can finish now */
2166 return 0; /* did not read anything, keep trying */
2170 static int quic_read(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
, int peek
)
2174 struct quic_read_again_args args
;
2178 if (!expect_quic(s
, &ctx
))
2183 if (ossl_quic_channel_is_term_any(ctx
.qc
->ch
)) {
2184 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2188 /* If we haven't finished the handshake, try to advance it. */
2189 if (quic_do_handshake(&ctx
) < 1) {
2190 ret
= 0; /* ossl_quic_do_handshake raised error here */
2194 if (ctx
.xso
== NULL
) {
2196 * Called on a QCSO and we don't currently have a default stream.
2198 * Wait until we get a stream initiated by the peer (blocking mode) or
2199 * fail if we don't have one yet (non-blocking mode).
2201 if (!qc_wait_for_default_xso_for_read(&ctx
)) {
2202 ret
= 0; /* error already raised here */
2206 ctx
.xso
= ctx
.qc
->default_xso
;
2209 if (!quic_read_actual(&ctx
, ctx
.xso
->stream
, buf
, len
, bytes_read
, peek
)) {
2210 ret
= 0; /* quic_read_actual raised error here */
2214 if (*bytes_read
> 0) {
2216 * Even though we succeeded, tick the reactor here to ensure we are
2217 * handling other aspects of the QUIC connection.
2219 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
2221 } else if (xso_blocking_mode(ctx
.xso
)) {
2223 * We were not able to read anything immediately, so our stream
2224 * buffer is empty. This means we need to block until we get
2225 * at least one byte.
2228 args
.stream
= ctx
.xso
->stream
;
2231 args
.bytes_read
= bytes_read
;
2234 res
= block_until_pred(ctx
.qc
, quic_read_again
, &args
, 0);
2236 ret
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2238 } else if (res
< 0) {
2239 ret
= 0; /* quic_read_again raised error here */
2246 * We did not get any bytes and are not in blocking mode.
2247 * Tick to see if this delivers any more.
2249 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx
.qc
->ch
), 0);
2251 /* Try the read again. */
2252 if (!quic_read_actual(&ctx
, ctx
.xso
->stream
, buf
, len
, bytes_read
, peek
)) {
2253 ret
= 0; /* quic_read_actual raised error here */
2257 if (*bytes_read
> 0)
2258 ret
= 1; /* Succeeded this time. */
2260 ret
= QUIC_RAISE_NORMAL_ERROR(&ctx
, SSL_ERROR_WANT_READ
);
2264 quic_unlock(ctx
.qc
);
2268 int ossl_quic_read(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
)
2270 return quic_read(s
, buf
, len
, bytes_read
, 0);
2273 int ossl_quic_peek(SSL
*s
, void *buf
, size_t len
, size_t *bytes_read
)
2275 return quic_read(s
, buf
, len
, bytes_read
, 1);
2284 static size_t ossl_quic_pending_int(const SSL
*s
, int check_channel
)
2290 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/-1, &ctx
))
2293 if (ctx
.xso
->stream
== NULL
2294 || !ossl_quic_stream_has_recv_buffer(ctx
.xso
->stream
))
2295 /* Cannot raise errors here because we are const, just fail. */
2298 if (!ossl_quic_rstream_available(ctx
.xso
->stream
->rstream
, &avail
, &fin
))
2301 if (avail
== 0 && check_channel
&& ossl_quic_channel_has_pending(ctx
.qc
->ch
))
2305 quic_unlock(ctx
.qc
);
2309 size_t ossl_quic_pending(const SSL
*s
)
2311 return ossl_quic_pending_int(s
, /*check_channel=*/0);
2314 int ossl_quic_has_pending(const SSL
*s
)
2316 /* Do we have app-side pending data or pending URXEs or RXEs? */
2317 return ossl_quic_pending_int(s
, /*check_channel=*/1) > 0;
2321 * SSL_stream_conclude
2322 * -------------------
2325 int ossl_quic_conn_stream_conclude(SSL
*s
)
2331 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/0, &ctx
))
2334 qs
= ctx
.xso
->stream
;
2336 if (!ossl_quic_channel_is_active(ctx
.qc
->ch
)) {
2337 quic_unlock(ctx
.qc
);
2338 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2341 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2342 quic_unlock(ctx
.qc
);
2343 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2346 if (ossl_quic_sstream_get_final_size(qs
->sstream
, NULL
)) {
2347 quic_unlock(ctx
.qc
);
2351 ossl_quic_sstream_fin(qs
->sstream
);
2352 quic_post_write(ctx
.xso
, 1, 1);
2353 quic_unlock(ctx
.qc
);
2358 * SSL_inject_net_dgram
2359 * --------------------
2362 int SSL_inject_net_dgram(SSL
*s
, const unsigned char *buf
,
2364 const BIO_ADDR
*peer
,
2365 const BIO_ADDR
*local
)
2371 if (!expect_quic(s
, &ctx
))
2376 demux
= ossl_quic_channel_get0_demux(ctx
.qc
->ch
);
2377 ret
= ossl_quic_demux_inject(demux
, buf
, buf_len
, peer
, local
);
2379 quic_unlock(ctx
.qc
);
2384 * SSL_get0_connection
2385 * -------------------
2387 SSL
*ossl_quic_get0_connection(SSL
*s
)
2391 if (!expect_quic(s
, &ctx
))
2394 return &ctx
.qc
->ssl
;
2398 * SSL_get_stream_type
2399 * -------------------
2401 int ossl_quic_get_stream_type(SSL
*s
)
2405 if (!expect_quic(s
, &ctx
))
2406 return SSL_STREAM_TYPE_BIDI
;
2408 if (ctx
.xso
== NULL
) {
2410 * If deferred XSO creation has yet to occur, proceed according to the
2411 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2412 * what kind of stream will be created yet, so return BIDI on the basis
2413 * that at this time, the client still has the option of calling
2414 * SSL_read() or SSL_write() first.
2416 if (ctx
.qc
->default_xso_created
2417 || ctx
.qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
2418 return SSL_STREAM_TYPE_NONE
;
2420 return SSL_STREAM_TYPE_BIDI
;
2423 if (ossl_quic_stream_is_bidi(ctx
.xso
->stream
))
2424 return SSL_STREAM_TYPE_BIDI
;
2426 if (ossl_quic_stream_is_server_init(ctx
.xso
->stream
) != ctx
.qc
->as_server
)
2427 return SSL_STREAM_TYPE_READ
;
2429 return SSL_STREAM_TYPE_WRITE
;
2437 uint64_t ossl_quic_get_stream_id(SSL
*s
)
2442 if (!expect_quic_with_stream_lock(s
, /*remote_init=*/-1, &ctx
))
2445 id
= ctx
.xso
->stream
->id
;
2446 quic_unlock(ctx
.qc
);
2452 * SSL_set_default_stream_mode
2453 * ---------------------------
2456 int ossl_quic_set_default_stream_mode(SSL
*s
, uint32_t mode
)
2460 if (!expect_quic_conn_only(s
, &ctx
))
2465 if (ctx
.qc
->default_xso_created
)
2466 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
2467 "too late to change default stream mode");
2470 case SSL_DEFAULT_STREAM_MODE_NONE
:
2471 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI
:
2472 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI
:
2473 ctx
.qc
->default_stream_mode
= mode
;
2476 quic_unlock(ctx
.qc
);
2477 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
2478 "bad default stream type");
2481 quic_unlock(ctx
.qc
);
2490 SSL
*ossl_quic_detach_stream(SSL
*s
)
2493 QUIC_XSO
*xso
= NULL
;
2495 if (!expect_quic_conn_only(s
, &ctx
))
2500 /* Calling this function inhibits default XSO autocreation. */
2501 /* QC ref to any default XSO is transferred to us and to caller. */
2502 qc_set_default_xso_keep_ref(ctx
.qc
, NULL
, /*touch=*/1, &xso
);
2504 quic_unlock(ctx
.qc
);
2506 return xso
!= NULL
? &xso
->ssl
: NULL
;
2514 int ossl_quic_attach_stream(SSL
*conn
, SSL
*stream
)
2520 if (!expect_quic_conn_only(conn
, &ctx
))
2523 if (stream
== NULL
|| stream
->type
!= SSL_TYPE_QUIC_XSO
)
2524 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_NULL_PARAMETER
,
2525 "stream to attach must be a valid QUIC stream");
2527 xso
= (QUIC_XSO
*)stream
;
2531 if (ctx
.qc
->default_xso
!= NULL
) {
2532 quic_unlock(ctx
.qc
);
2533 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
2534 "connection already has a default stream");
2538 * It is a caller error for the XSO being attached as a default XSO to have
2539 * more than one ref.
2541 if (!CRYPTO_GET_REF(&xso
->ssl
.references
, &nref
)) {
2542 quic_unlock(ctx
.qc
);
2543 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
,
2548 quic_unlock(ctx
.qc
);
2549 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_PASSED_INVALID_ARGUMENT
,
2550 "stream being attached must have "
2551 "only 1 reference");
2554 /* Caller's reference to the XSO is transferred to us. */
2555 /* Calling this function inhibits default XSO autocreation. */
2556 qc_set_default_xso(ctx
.qc
, xso
, /*touch=*/1);
2558 quic_unlock(ctx
.qc
);
2563 * SSL_set_incoming_stream_policy
2564 * ------------------------------
2567 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION
*qc
)
2569 switch (qc
->incoming_stream_policy
) {
2570 case SSL_INCOMING_STREAM_POLICY_AUTO
:
2571 if ((qc
->default_xso
== NULL
&& !qc
->default_xso_created
)
2572 || qc
->default_stream_mode
== SSL_DEFAULT_STREAM_MODE_NONE
)
2573 return SSL_INCOMING_STREAM_POLICY_ACCEPT
;
2575 return SSL_INCOMING_STREAM_POLICY_REJECT
;
2578 return qc
->incoming_stream_policy
;
2583 static void qc_update_reject_policy(QUIC_CONNECTION
*qc
)
2585 int policy
= qc_get_effective_incoming_stream_policy(qc
);
2586 int enable_reject
= (policy
== SSL_INCOMING_STREAM_POLICY_REJECT
);
2588 ossl_quic_channel_set_incoming_stream_auto_reject(qc
->ch
,
2590 qc
->incoming_stream_aec
);
2594 int ossl_quic_set_incoming_stream_policy(SSL
*s
, int policy
,
2600 if (!expect_quic_conn_only(s
, &ctx
))
2606 case SSL_INCOMING_STREAM_POLICY_AUTO
:
2607 case SSL_INCOMING_STREAM_POLICY_ACCEPT
:
2608 case SSL_INCOMING_STREAM_POLICY_REJECT
:
2609 ctx
.qc
->incoming_stream_policy
= policy
;
2610 ctx
.qc
->incoming_stream_aec
= aec
;
2618 qc_update_reject_policy(ctx
.qc
);
2619 quic_unlock(ctx
.qc
);
2627 struct wait_for_incoming_stream_args
{
2633 static int wait_for_incoming_stream(void *arg
)
2635 struct wait_for_incoming_stream_args
*args
= arg
;
2636 QUIC_CONNECTION
*qc
= args
->ctx
->qc
;
2637 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(qc
->ch
);
2639 if (!ossl_quic_channel_is_active(qc
->ch
)) {
2640 /* If connection is torn down due to an error while blocking, stop. */
2641 QUIC_RAISE_NON_NORMAL_ERROR(args
->ctx
, SSL_R_PROTOCOL_IS_SHUTDOWN
, NULL
);
2645 args
->qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
2646 if (args
->qs
!= NULL
)
2647 return 1; /* got a stream */
2649 return 0; /* did not get a stream, keep trying */
2653 SSL
*ossl_quic_accept_stream(SSL
*s
, uint64_t flags
)
2658 QUIC_STREAM_MAP
*qsm
;
2661 OSSL_RTT_INFO rtt_info
;
2663 if (!expect_quic_conn_only(s
, &ctx
))
2668 if (qc_get_effective_incoming_stream_policy(ctx
.qc
)
2669 == SSL_INCOMING_STREAM_POLICY_REJECT
)
2672 qsm
= ossl_quic_channel_get_qsm(ctx
.qc
->ch
);
2674 qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
2676 if (qc_blocking_mode(ctx
.qc
)
2677 && (flags
& SSL_ACCEPT_STREAM_NO_BLOCK
) == 0) {
2678 struct wait_for_incoming_stream_args args
;
2683 ret
= block_until_pred(ctx
.qc
, wait_for_incoming_stream
, &args
, 0);
2685 QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, ERR_R_INTERNAL_ERROR
, NULL
);
2687 } else if (ret
< 0 || args
.qs
== NULL
) {
2697 xso
= create_xso_from_stream(ctx
.qc
, qs
);
2701 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx
.qc
->ch
), &rtt_info
);
2702 ossl_quic_stream_map_remove_from_accept_queue(qsm
, qs
,
2703 rtt_info
.smoothed_rtt
);
2706 /* Calling this function inhibits default XSO autocreation. */
2707 qc_touch_default_xso(ctx
.qc
); /* inhibits default XSO */
2710 quic_unlock(ctx
.qc
);
2715 * SSL_get_accept_stream_queue_len
2716 * -------------------------------
2719 size_t ossl_quic_get_accept_stream_queue_len(SSL
*s
)
2724 if (!expect_quic_conn_only(s
, &ctx
))
2729 v
= ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx
.qc
->ch
));
2731 quic_unlock(ctx
.qc
);
2739 int ossl_quic_stream_reset(SSL
*ssl
,
2740 const SSL_STREAM_RESET_ARGS
*args
,
2744 QUIC_STREAM_MAP
*qsm
;
2746 uint64_t error_code
;
2749 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/0, &ctx
))
2752 qsm
= ossl_quic_channel_get_qsm(ctx
.qc
->ch
);
2753 qs
= ctx
.xso
->stream
;
2754 error_code
= (args
!= NULL
? args
->quic_error_code
: 0);
2756 if (!quic_validate_for_write(ctx
.xso
, &err
)) {
2757 ok
= QUIC_RAISE_NON_NORMAL_ERROR(&ctx
, err
, NULL
);
2761 ok
= ossl_quic_stream_map_reset_stream_send_part(qsm
, qs
, error_code
);
2764 quic_unlock(ctx
.qc
);
2769 * SSL_get_stream_read_state
2770 * -------------------------
2772 static void quic_classify_stream(QUIC_CONNECTION
*qc
,
2776 uint64_t *app_error_code
)
2779 uint64_t final_size
;
2781 local_init
= (ossl_quic_stream_is_server_init(qs
) == qc
->as_server
);
2783 if (app_error_code
!= NULL
)
2784 *app_error_code
= UINT64_MAX
;
2786 app_error_code
= &final_size
; /* throw away value */
2788 if (!ossl_quic_stream_is_bidi(qs
) && local_init
!= is_write
) {
2790 * Unidirectional stream and this direction of transmission doesn't
2793 *state
= SSL_STREAM_STATE_WRONG_DIR
;
2794 } else if (ossl_quic_channel_is_term_any(qc
->ch
)) {
2795 /* Connection already closed. */
2796 *state
= SSL_STREAM_STATE_CONN_CLOSED
;
2797 } else if (!is_write
&& qs
->recv_state
== QUIC_RSTREAM_STATE_DATA_READ
) {
2798 /* Application has read a FIN. */
2799 *state
= SSL_STREAM_STATE_FINISHED
;
2800 } else if ((!is_write
&& qs
->stop_sending
)
2801 || (is_write
&& ossl_quic_stream_send_is_reset(qs
))) {
2803 * Stream has been reset locally. FIN takes precedence over this for the
2804 * read case as the application need not care if the stream is reset
2805 * after a FIN has been successfully processed.
2807 *state
= SSL_STREAM_STATE_RESET_LOCAL
;
2808 *app_error_code
= !is_write
2809 ? qs
->stop_sending_aec
2810 : qs
->reset_stream_aec
;
2811 } else if ((!is_write
&& ossl_quic_stream_recv_is_reset(qs
))
2812 || (is_write
&& qs
->peer_stop_sending
)) {
2814 * Stream has been reset remotely. */
2815 *state
= SSL_STREAM_STATE_RESET_REMOTE
;
2816 *app_error_code
= !is_write
2817 ? qs
->peer_reset_stream_aec
2818 : qs
->peer_stop_sending_aec
;
2819 } else if (is_write
&& ossl_quic_sstream_get_final_size(qs
->sstream
,
2822 * Stream has been finished. Stream reset takes precedence over this for
2823 * the write case as peer may not have received all data.
2825 *state
= SSL_STREAM_STATE_FINISHED
;
2827 /* Stream still healthy. */
2828 *state
= SSL_STREAM_STATE_OK
;
2832 static int quic_get_stream_state(SSL
*ssl
, int is_write
)
2837 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, &ctx
))
2838 return SSL_STREAM_STATE_NONE
;
2840 quic_classify_stream(ctx
.qc
, ctx
.xso
->stream
, is_write
, &state
, NULL
);
2841 quic_unlock(ctx
.qc
);
2845 int ossl_quic_get_stream_read_state(SSL
*ssl
)
2847 return quic_get_stream_state(ssl
, /*is_write=*/0);
2851 * SSL_get_stream_write_state
2852 * --------------------------
2854 int ossl_quic_get_stream_write_state(SSL
*ssl
)
2856 return quic_get_stream_state(ssl
, /*is_write=*/1);
2860 * SSL_get_stream_read_error_code
2861 * ------------------------------
2863 static int quic_get_stream_error_code(SSL
*ssl
, int is_write
,
2864 uint64_t *app_error_code
)
2869 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, &ctx
))
2872 quic_classify_stream(ctx
.qc
, ctx
.xso
->stream
, /*is_write=*/0,
2873 &state
, app_error_code
);
2875 quic_unlock(ctx
.qc
);
2877 case SSL_STREAM_STATE_FINISHED
:
2879 case SSL_STREAM_STATE_RESET_LOCAL
:
2880 case SSL_STREAM_STATE_RESET_REMOTE
:
2887 int ossl_quic_get_stream_read_error_code(SSL
*ssl
, uint64_t *app_error_code
)
2889 return quic_get_stream_error_code(ssl
, /*is_write=*/0, app_error_code
);
2893 * SSL_get_stream_write_error_code
2894 * -------------------------------
2896 int ossl_quic_get_stream_write_error_code(SSL
*ssl
, uint64_t *app_error_code
)
2898 return quic_get_stream_error_code(ssl
, /*is_write=*/1, app_error_code
);
2902 * Write buffer size mutation
2903 * --------------------------
2905 int ossl_quic_set_write_buffer_size(SSL
*ssl
, size_t size
)
2910 if (!expect_quic_with_stream_lock(ssl
, /*remote_init=*/-1, &ctx
))
2913 if (!ossl_quic_stream_has_send(ctx
.xso
->stream
))
2914 /* Called on a unidirectional receive-only stream - error. */
2917 if (!ossl_quic_stream_has_send_buffer(ctx
.xso
->stream
)) {
2919 * If the stream has a send part but we have disposed of it because we
2920 * no longer need it, this is a no-op.
2926 if (!ossl_quic_sstream_set_buffer_size(ctx
.xso
->stream
->sstream
, size
))
2932 quic_unlock(ctx
.qc
);
2937 * SSL_get_conn_close_info
2938 * -----------------------
2940 int ossl_quic_get_conn_close_info(SSL
*ssl
,
2941 SSL_CONN_CLOSE_INFO
*info
,
2945 const QUIC_TERMINATE_CAUSE
*tc
;
2947 if (!expect_quic_conn_only(ssl
, &ctx
))
2950 tc
= ossl_quic_channel_get_terminate_cause(ctx
.qc
->ch
);
2954 info
->error_code
= tc
->error_code
;
2955 info
->reason
= NULL
; /* TODO(QUIC): Wire reason */
2956 info
->reason_len
= 0;
2957 info
->is_local
= !tc
->remote
;
2958 info
->is_transport
= !tc
->app
;
2966 int ossl_quic_key_update(SSL
*ssl
, int update_type
)
2970 if (!expect_quic_conn_only(ssl
, &ctx
))
2973 switch (update_type
) {
2974 case SSL_KEY_UPDATE_NOT_REQUESTED
:
2976 * QUIC signals peer key update implicily by triggering a local
2977 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
2979 case SSL_KEY_UPDATE_REQUESTED
:
2983 /* Unknown type - error. */
2989 /* Attempt to perform a TXKU. */
2990 if (!ossl_quic_channel_trigger_txku(ctx
.qc
->ch
)) {
2991 quic_unlock(ctx
.qc
);
2995 quic_unlock(ctx
.qc
);
3000 * SSL_get_key_update_type
3001 * -----------------------
3003 int ossl_quic_get_key_update_type(const SSL
*s
)
3006 * We always handle key updates immediately so a key update is never
3009 return SSL_KEY_UPDATE_NONE
;
3013 * QUIC Front-End I/O API: SSL_CTX Management
3014 * ==========================================
3017 long ossl_quic_ctx_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
3021 return ssl3_ctx_ctrl(ctx
, cmd
, larg
, parg
);
3025 long ossl_quic_callback_ctrl(SSL
*s
, int cmd
, void (*fp
) (void))
3029 if (!expect_quic_conn_only(s
, &ctx
))
3033 case SSL_CTRL_SET_MSG_CALLBACK
:
3034 ossl_quic_channel_set_msg_callback(ctx
.qc
->ch
, (ossl_msg_cb
)fp
,
3036 /* This callback also needs to be set on the internal SSL object */
3037 return ssl3_callback_ctrl(ctx
.qc
->tls
, cmd
, fp
);;
3040 /* Probably a TLS related ctrl. Defer to our internal SSL object */
3041 return ssl3_callback_ctrl(ctx
.qc
->tls
, cmd
, fp
);
3045 long ossl_quic_ctx_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
) (void))
3047 return ssl3_ctx_callback_ctrl(ctx
, cmd
, fp
);
3050 int ossl_quic_renegotiate_check(SSL
*ssl
, int initok
)
3052 /* We never do renegotiation. */
3057 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3058 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3061 int ossl_quic_num_ciphers(void)
3066 const SSL_CIPHER
*ossl_quic_get_cipher(unsigned int u
)
3072 * Internal Testing APIs
3073 * =====================
3076 QUIC_CHANNEL
*ossl_quic_conn_get_channel(SSL
*s
)
3080 if (!expect_quic_conn_only(s
, &ctx
))