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 static void aon_write_finish(QUIC_XSO *xso);
21 static int create_channel(QUIC_CONNECTION *qc);
22 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
23 static int qc_try_create_default_xso_for_write(QUIC_CONNECTION *qc);
24 static int qc_wait_for_default_xso_for_read(QUIC_CONNECTION *qc);
25 static void quic_lock(QUIC_CONNECTION *qc);
26 static void quic_unlock(QUIC_CONNECTION *qc);
27 static int quic_do_handshake(QUIC_CONNECTION *qc);
28 static void qc_update_reject_policy(QUIC_CONNECTION *qc);
29 static void qc_touch_default_xso(QUIC_CONNECTION *qc);
30 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
31 static SSL *quic_conn_stream_new(QUIC_CONNECTION *qc, uint64_t flags,
35 * QUIC Front-End I/O API: Common Utilities
36 * ========================================
40 * Block until a predicate is met.
42 * Precondition: Must have a channel.
43 * Precondition: Must hold channel lock (unchecked).
46 static int block_until_pred(QUIC_CONNECTION *qc,
47 int (*pred)(void *arg), void *pred_arg,
52 assert(qc->ch != NULL);
54 rtor = ossl_quic_channel_get_reactor(qc->ch);
55 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
60 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
61 * rather than via ERR.
63 static int quic_raise_normal_error(QUIC_CONNECTION *qc,
71 * Raise a 'non-normal' error, meaning any error that is not reported via
72 * SSL_get_error() and must be reported via ERR.
74 * qc should be provided if available. In exceptional circumstances when qc is
75 * not known NULL may be passed. This should generally only happen when an
76 * expect_...() function defined below fails, which generally indicates a
77 * dispatch error or caller error.
79 static int quic_raise_non_normal_error(QUIC_CONNECTION *qc,
90 ERR_set_debug(file, line, func);
93 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
97 qc->last_error = SSL_ERROR_SSL;
102 #define QUIC_RAISE_NORMAL_ERROR(qc, err) \
103 quic_raise_normal_error((qc), (err))
105 #define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg) \
106 quic_raise_non_normal_error((qc), \
107 OPENSSL_FILE, OPENSSL_LINE, \
113 * QCTX is a utility structure which provides information we commonly wish to
114 * unwrap upon an API call being dispatched to us, namely:
116 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
118 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
119 * a QCSO with a default stream was passed);
120 * - whether a QSSO was passed (xso == NULL must not be used to determine this
121 * because it may be non-NULL when a QCSO is passed if that QCSO has a
124 typedef struct qctx_st {
131 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
132 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
134 * After this returns 1, all fields of the passed QCTX are initialised.
135 * Returns 0 on failure. This function is intended to be used to provide API
136 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
138 static int expect_quic(const SSL *s, QCTX *ctx)
148 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
151 case SSL_TYPE_QUIC_CONNECTION:
152 qc = (QUIC_CONNECTION *)s;
154 ctx->xso = qc->default_xso;
158 case SSL_TYPE_QUIC_XSO:
166 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
171 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
172 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
175 * remote_init determines if we expect the default XSO to be remotely created or
176 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
178 * Channel mutex is acquired and retained on success.
181 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
184 if (!expect_quic(s, ctx))
189 if (ctx->xso == NULL && remote_init >= 0) {
190 if (ossl_quic_channel_is_term_any(ctx->qc->ch)) {
191 QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
195 /* If we haven't finished the handshake, try to advance it. */
196 if (quic_do_handshake(ctx->qc) < 1)
197 /* ossl_quic_do_handshake raised error here */
200 if (remote_init == 0) {
201 if (!qc_try_create_default_xso_for_write(ctx->qc))
204 if (!qc_wait_for_default_xso_for_read(ctx->qc))
208 ctx->xso = ctx->qc->default_xso;
211 if (ctx->xso == NULL) {
212 QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, SSL_R_NO_STREAM, NULL);
216 return 1; /* lock held */
219 quic_unlock(ctx->qc);
224 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
225 * be non-NULL if the QCSO has a default stream.
227 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
229 if (!expect_quic(s, ctx))
233 return QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, SSL_R_CONN_USE_ONLY, NULL);
239 * Ensures that the channel mutex is held for a method which touches channel
242 * Precondition: Channel mutex is not held (unchecked)
244 static void quic_lock(QUIC_CONNECTION *qc)
246 ossl_crypto_mutex_lock(qc->mutex);
249 /* Precondition: Channel mutex is held (unchecked) */
251 static void quic_unlock(QUIC_CONNECTION *qc)
253 ossl_crypto_mutex_unlock(qc->mutex);
258 * QUIC Front-End I/O API: Initialization
259 * ======================================
261 * SSL_new => ossl_quic_new
263 * SSL_reset => ossl_quic_reset
264 * SSL_clear => ossl_quic_clear
266 * SSL_free => ossl_quic_free
271 SSL *ossl_quic_new(SSL_CTX *ctx)
273 QUIC_CONNECTION *qc = NULL;
274 SSL *ssl_base = NULL;
275 SSL_CONNECTION *sc = NULL;
277 qc = OPENSSL_zalloc(sizeof(*qc));
281 /* Initialise the QUIC_CONNECTION's stub header. */
283 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
288 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
289 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
292 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
295 qc->is_thread_assisted
296 = (ssl_base->method == OSSL_QUIC_client_thread_method());
298 qc->as_server = 0; /* TODO(QUIC): server support */
299 qc->as_server_state = qc->as_server;
301 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
302 qc->default_ssl_mode = qc->ssl.ctx->mode;
303 qc->default_blocking = 1;
304 qc->incoming_stream_reject_policy
305 = SSL_INCOMING_STREAM_REJECT_POLICY_AUTO;
306 qc->last_error = SSL_ERROR_NONE;
308 if (!create_channel(qc))
311 qc_update_reject_policy(qc);
314 * We do not create the default XSO yet. The reason for this is that the
315 * stream ID of the default XSO will depend on whether the stream is client
316 * or server-initiated, which depends on who transmits first. Since we do
317 * not know whether the application will be using a client-transmits-first
318 * or server-transmits-first protocol, we defer default XSO creation until
319 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
320 * we take that as a cue that the client is expecting a server-initiated
321 * stream, and vice versa if SSL_write() is called first.
327 ossl_quic_channel_free(qc->ch);
336 void ossl_quic_free(SSL *s)
340 /* We should never be called on anything but a QSO. */
341 if (!expect_quic(s, &ctx))
346 * When a QSSO is freed, the XSO is freed immediately, because the XSO
347 * itself only contains API personality layer data. However the
348 * underlying QUIC_STREAM is not freed immediately but is instead marked
349 * as deleted for later collection.
354 assert(ctx.qc->num_xso > 0);
357 /* If a stream's send part has not been finished, auto-reset it. */
358 if (ctx.xso->stream->sstream != NULL
359 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
360 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
363 /* Do STOP_SENDING for the receive part, if applicable. */
364 if (ctx.xso->stream->rstream != NULL)
365 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
368 /* Update stream state. */
369 ctx.xso->stream->deleted = 1;
370 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
375 /* Note: SSL_free calls OPENSSL_free(xso) for us */
382 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
383 * stage, but is freed during the channel free when the whole QSM is freed.
385 if (ctx.qc->default_xso != NULL) {
386 QUIC_XSO *xso = ctx.qc->default_xso;
393 /* Ensure we have no remaining XSOs. */
394 assert(ctx.qc->num_xso == 0);
396 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
397 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
398 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
401 ossl_quic_channel_free(ctx.qc->ch);
403 BIO_free(ctx.qc->net_rbio);
404 BIO_free(ctx.qc->net_wbio);
406 /* Note: SSL_free calls OPENSSL_free(qc) for us */
408 SSL_free(ctx.qc->tls);
409 ossl_crypto_mutex_free(&ctx.qc->mutex); /* freed while still locked */
412 /* SSL method init */
413 int ossl_quic_init(SSL *s)
415 /* Same op as SSL_clear, forward the call. */
416 return ossl_quic_clear(s);
419 /* SSL method deinit */
420 void ossl_quic_deinit(SSL *s)
426 int ossl_quic_reset(SSL *s)
430 if (!expect_quic(s, &ctx))
433 /* TODO(QUIC); Currently a no-op. */
438 int ossl_quic_clear(SSL *s)
442 if (!expect_quic(s, &ctx))
445 /* TODO(QUIC): Currently a no-op. */
449 void ossl_quic_conn_set_override_now_cb(SSL *s,
450 OSSL_TIME (*now_cb)(void *arg),
455 if (!expect_quic(s, &ctx))
458 ctx.qc->override_now_cb = now_cb;
459 ctx.qc->override_now_cb_arg = now_cb_arg;
462 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
466 if (!expect_quic(s, &ctx))
469 if (ctx.qc->is_thread_assisted && ctx.qc->started)
470 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
474 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
476 qc->default_xso_created = 1;
477 qc_update_reject_policy(qc);
481 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
483 qc->default_xso = xso;
485 qc_touch_default_xso(qc);
489 * QUIC Front-End I/O API: Network BIO Configuration
490 * =================================================
492 * Handling the different BIOs is difficult:
494 * - It is more or less a requirement that we use non-blocking network I/O;
495 * we need to be able to have timeouts on recv() calls, and make best effort
496 * (non blocking) send() and recv() calls.
498 * The only sensible way to do this is to configure the socket into
499 * non-blocking mode. We could try to do select() before calling send() or
500 * recv() to get a guarantee that the call will not block, but this will
501 * probably run into issues with buggy OSes which generate spurious socket
502 * readiness events. In any case, relying on this to work reliably does not
505 * Timeouts could be handled via setsockopt() socket timeout options, but
506 * this depends on OS support and adds another syscall to every network I/O
507 * operation. It also has obvious thread safety concerns if we want to move
508 * to concurrent use of a single socket at some later date.
510 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
511 * be made non-blocking. However some OSes (e.g. Windows) do not support
512 * this, so we cannot rely on this.
514 * As such, we need to configure any FD in non-blocking mode. This may
515 * confound users who pass a blocking socket to libssl. However, in practice
516 * it would be extremely strange for a user of QUIC to pass an FD to us,
517 * then also try and send receive traffic on the same socket(!). Thus the
518 * impact of this should be limited, and can be documented.
520 * - We support both blocking and non-blocking operation in terms of the API
521 * presented to the user. One prospect is to set the blocking mode based on
522 * whether the socket passed to us was already in blocking mode. However,
523 * Windows has no API for determining if a socket is in blocking mode (!),
524 * therefore this cannot be done portably. Currently therefore we expose an
525 * explicit API call to set this, and default to blocking mode.
527 * - We need to determine our initial destination UDP address. The "natural"
528 * way for a user to do this is to set the peer variable on a BIO_dgram.
529 * However, this has problems because BIO_dgram's peer variable is used for
530 * both transmission and reception. This means it can be constantly being
531 * changed to a malicious value (e.g. if some random unrelated entity on the
532 * network starts sending traffic to us) on every read call. This is not a
533 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
534 * calls only, which do not use this variable. However, we do need to let
535 * the user specify the peer in a 'normal' manner. The compromise here is
536 * that we grab the current peer value set at the time the write BIO is set
537 * and do not read the value again.
539 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
540 * Currently we do this by only supporting non-blocking mode.
545 * Determines what initial destination UDP address we should use, if possible.
546 * If this fails the client must set the destination address manually, or use a
547 * BIO which does not need a destination address.
549 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
551 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
557 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
561 if (!expect_quic(s, &ctx))
564 if (ctx.qc->net_rbio == net_rbio)
567 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
570 BIO_free(ctx.qc->net_rbio);
571 ctx.qc->net_rbio = net_rbio;
574 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
575 * mode as we do not support it for non-pollable BIOs.
577 if (net_rbio != NULL) {
578 BIO_POLL_DESCRIPTOR d = {0};
580 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
581 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
582 ctx.qc->blocking = 0;
583 ctx.qc->default_blocking = 0;
584 ctx.qc->can_poll_net_rbio = 0;
586 ctx.qc->can_poll_net_rbio = 1;
591 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
595 if (!expect_quic(s, &ctx))
598 if (ctx.qc->net_wbio == net_wbio)
601 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
604 BIO_free(ctx.qc->net_wbio);
605 ctx.qc->net_wbio = net_wbio;
607 if (net_wbio != NULL) {
608 BIO_POLL_DESCRIPTOR d = {0};
610 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
611 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
612 ctx.qc->blocking = 0;
613 ctx.qc->default_blocking = 0;
614 ctx.qc->can_poll_net_wbio = 0;
616 ctx.qc->can_poll_net_wbio = 1;
620 * If we do not have a peer address yet, and we have not started trying
621 * to connect yet, try to autodetect one.
623 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
624 && !ctx.qc->started) {
625 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
627 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
629 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
630 &ctx.qc->init_peer_addr);
635 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
639 if (!expect_quic(s, &ctx))
642 return ctx.qc->net_rbio;
645 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
649 if (!expect_quic(s, &ctx))
652 return ctx.qc->net_wbio;
655 int ossl_quic_conn_get_blocking_mode(const SSL *s)
659 if (!expect_quic(s, &ctx))
663 return ctx.xso->blocking;
665 return ctx.qc->blocking;
668 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
672 if (!expect_quic(s, &ctx))
675 /* Cannot enable blocking mode if we do not have pollable FDs. */
677 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
678 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_UNSUPPORTED, NULL);
680 if (!ctx.is_stream) {
682 * If called on a QCSO, update default and connection-level blocking
685 ctx.qc->blocking = (blocking != 0);
686 ctx.qc->default_blocking = ctx.qc->blocking;
691 * If called on a QSSO or QCSO with a default XSO, update blocking
694 ctx.xso->blocking = (blocking != 0);
699 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
700 const BIO_ADDR *peer_addr)
704 if (!expect_quic(s, &ctx))
708 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
711 if (peer_addr == NULL) {
712 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
716 ctx.qc->init_peer_addr = *peer_addr;
721 * QUIC Front-End I/O API: Asynchronous I/O Management
722 * ===================================================
724 * (BIO/)SSL_tick => ossl_quic_tick
725 * (BIO/)SSL_get_tick_timeout => ossl_quic_get_tick_timeout
726 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
730 /* Returns 1 if the connection is being used in blocking mode. */
731 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
736 static int xso_blocking_mode(const QUIC_XSO *xso)
739 && xso->conn->can_poll_net_rbio
740 && xso->conn->can_poll_net_wbio;
743 /* SSL_tick; ticks the reactor. */
745 int ossl_quic_tick(SSL *s)
749 if (!expect_quic(s, &ctx))
753 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
759 * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object
760 * should be ticked by the application by calling SSL_tick(). tv is set to 0 if
761 * the object should be ticked immediately and tv->tv_sec is set to -1 if no
762 * timeout is currently active.
765 int ossl_quic_get_tick_timeout(SSL *s, struct timeval *tv)
768 OSSL_TIME deadline = ossl_time_infinite();
770 if (!expect_quic(s, &ctx))
776 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
778 if (ossl_time_is_infinite(deadline)) {
785 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now()));
790 /* SSL_get_rpoll_descriptor */
791 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
795 if (!expect_quic(s, &ctx))
798 if (desc == NULL || ctx.qc->net_rbio == NULL)
801 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
804 /* SSL_get_wpoll_descriptor */
805 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
809 if (!expect_quic(s, &ctx))
812 if (desc == NULL || ctx.qc->net_wbio == NULL)
815 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
818 /* SSL_net_read_desired */
820 int ossl_quic_get_net_read_desired(SSL *s)
825 if (!expect_quic(s, &ctx))
829 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
834 /* SSL_net_write_desired */
836 int ossl_quic_get_net_write_desired(SSL *s)
841 if (!expect_quic(s, &ctx))
845 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
851 * QUIC Front-End I/O API: Connection Lifecycle Operations
852 * =======================================================
854 * SSL_do_handshake => ossl_quic_do_handshake
855 * SSL_set_connect_state => ossl_quic_set_connect_state
856 * SSL_set_accept_state => ossl_quic_set_accept_state
857 * SSL_shutdown => ossl_quic_shutdown
858 * SSL_ctrl => ossl_quic_ctrl
859 * (BIO/)SSL_connect => ossl_quic_connect
860 * (BIO/)SSL_accept => ossl_quic_accept
865 static int quic_shutdown_wait(void *arg)
867 QUIC_CONNECTION *qc = arg;
869 return ossl_quic_channel_is_terminated(qc->ch);
873 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
874 const SSL_SHUTDOWN_EX_ARGS *args,
880 if (!expect_quic(s, &ctx))
884 /* TODO(QUIC): Semantics currently undefined for QSSOs */
889 ossl_quic_channel_local_close(ctx.qc->ch,
890 args != NULL ? args->quic_error_code : 0);
892 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
894 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
899 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
900 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
902 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
904 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
910 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
914 if (!expect_quic(s, &ctx))
919 /* If called on a QCSO, update the default mode. */
921 ctx.qc->default_ssl_mode |= (uint32_t)larg;
924 * If we were called on a QSSO or have a default stream, we also update
927 if (ctx.xso != NULL) {
928 /* Cannot enable EPW while AON write in progress. */
929 if (ctx.xso->aon_write_in_progress)
930 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
932 ctx.xso->ssl_mode |= (uint32_t)larg;
933 return ctx.xso->ssl_mode;
936 return ctx.qc->default_ssl_mode;
937 case SSL_CTRL_CLEAR_MODE:
939 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
941 if (ctx.xso != NULL) {
942 ctx.xso->ssl_mode &= ~(uint32_t)larg;
943 return ctx.xso->ssl_mode;
946 return ctx.qc->default_ssl_mode;
948 /* Probably a TLS related ctrl. Defer to our internal SSL object */
949 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
953 /* SSL_set_connect_state */
954 void ossl_quic_set_connect_state(SSL *s)
958 if (!expect_quic(s, &ctx))
961 /* Cannot be changed after handshake started */
962 if (ctx.qc->started || ctx.is_stream)
965 ctx.qc->as_server_state = 0;
968 /* SSL_set_accept_state */
969 void ossl_quic_set_accept_state(SSL *s)
973 if (!expect_quic(s, &ctx))
976 /* Cannot be changed after handshake started */
977 if (ctx.qc->started || ctx.is_stream)
980 ctx.qc->as_server_state = 1;
983 /* SSL_do_handshake */
984 struct quic_handshake_wait_args {
988 static int quic_handshake_wait(void *arg)
990 struct quic_handshake_wait_args *args = arg;
992 if (!ossl_quic_channel_is_active(args->qc->ch))
995 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1001 static int configure_channel(QUIC_CONNECTION *qc)
1003 assert(qc->ch != NULL);
1005 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1006 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1007 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1014 static int create_channel(QUIC_CONNECTION *qc)
1016 QUIC_CHANNEL_ARGS args = {0};
1018 args.libctx = qc->ssl.ctx->libctx;
1019 args.propq = qc->ssl.ctx->propq;
1020 args.is_server = qc->as_server;
1022 args.mutex = qc->mutex;
1023 args.now_cb = qc->override_now_cb;
1024 args.now_cb_arg = qc->override_now_cb_arg;
1026 qc->ch = ossl_quic_channel_new(&args);
1034 * Creates a channel and configures it with the information we have accumulated
1035 * via calls made to us from the application prior to starting a handshake
1039 static int ensure_channel_started(QUIC_CONNECTION *qc)
1042 if (!configure_channel(qc)
1043 || !ossl_quic_channel_start(qc->ch))
1046 if (qc->is_thread_assisted)
1047 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1055 ossl_quic_channel_free(qc->ch);
1061 static int quic_do_handshake(QUIC_CONNECTION *qc)
1065 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1066 /* Handshake already completed. */
1069 if (ossl_quic_channel_is_term_any(qc->ch))
1070 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1072 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1073 /* Peer address must have been set. */
1074 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1075 return -1; /* Non-protocol error */
1078 if (qc->as_server != qc->as_server_state) {
1079 /* TODO(QUIC): Must match the method used to create the QCSO */
1080 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1081 return -1; /* Non-protocol error */
1084 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1085 /* Need read and write BIOs. */
1086 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BIO_NOT_SET, NULL);
1087 return -1; /* Non-protocol error */
1091 * Start connection process. Note we may come here multiple times in
1092 * non-blocking mode, which is fine.
1094 if (!ensure_channel_started(qc)) {
1095 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1096 return -1; /* Non-protocol error */
1099 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1100 /* The handshake is now done. */
1103 if (qc_blocking_mode(qc)) {
1104 /* In blocking mode, wait for the handshake to complete. */
1105 struct quic_handshake_wait_args args;
1109 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1110 if (!ossl_quic_channel_is_active(qc->ch)) {
1111 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1112 return 0; /* Shutdown before completion */
1113 } else if (ret <= 0) {
1114 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1115 return -1; /* Non-protocol error */
1118 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1121 /* Try to advance the reactor. */
1122 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1124 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1125 /* The handshake is now done. */
1128 /* Otherwise, indicate that the handshake isn't done yet. */
1129 QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1130 return -1; /* Non-protocol error */
1135 int ossl_quic_do_handshake(SSL *s)
1140 if (!expect_quic(s, &ctx))
1145 ret = quic_do_handshake(ctx.qc);
1146 quic_unlock(ctx.qc);
1151 int ossl_quic_connect(SSL *s)
1153 /* Ensure we are in connect state (no-op if non-idle). */
1154 ossl_quic_set_connect_state(s);
1156 /* Begin or continue the handshake */
1157 return ossl_quic_do_handshake(s);
1161 int ossl_quic_accept(SSL *s)
1163 /* Ensure we are in accept state (no-op if non-idle). */
1164 ossl_quic_set_accept_state(s);
1166 /* Begin or continue the handshake */
1167 return ossl_quic_do_handshake(s);
1171 * QUIC Front-End I/O API: Stream Lifecycle Operations
1172 * ===================================================
1174 * SSL_stream_new => ossl_quic_conn_stream_new
1179 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1180 * default XSO was created. Returns 0 if it was not (e.g. because it already
1181 * exists). Note that this is NOT an error condition.
1184 static int qc_try_create_default_xso_for_write(QUIC_CONNECTION *qc)
1188 if (qc->default_xso_created
1189 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1191 * We only do this once. If the user detaches a previously created
1192 * default XSO we don't auto-create another one.
1194 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_NO_STREAM, NULL);
1196 /* Create a locally-initiated stream. */
1197 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1198 flags |= SSL_STREAM_FLAG_UNI;
1200 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(qc, flags,
1203 if (qc->default_xso == NULL)
1204 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1206 qc_touch_default_xso(qc);
1210 struct quic_wait_for_stream_args {
1211 QUIC_CONNECTION *qc;
1217 static int quic_wait_for_stream(void *arg)
1219 struct quic_wait_for_stream_args *args = arg;
1221 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1222 /* If connection is torn down due to an error while blocking, stop. */
1223 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1227 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1229 if (args->qs != NULL)
1230 return 1; /* stream now exists */
1232 return 0; /* did not get a stream, keep trying */
1236 static int qc_wait_for_default_xso_for_read(QUIC_CONNECTION *qc)
1238 /* Called on a QCSO and we don't currently have a default stream. */
1242 struct quic_wait_for_stream_args wargs;
1245 * If default stream functionality is disabled or we already detached
1246 * one, don't make another default stream and just fail.
1248 if (qc->default_xso_created
1249 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1250 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_NO_STREAM, NULL);
1253 * The peer may have opened a stream since we last ticked. So tick and
1254 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1255 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1256 * first stream created by a peer must have an ordinal of 0.
1258 expect_id = qc->as_server
1259 ? QUIC_STREAM_INITIATOR_CLIENT
1260 : QUIC_STREAM_INITIATOR_SERVER;
1262 expect_id |= (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1263 ? QUIC_STREAM_DIR_UNI
1264 : QUIC_STREAM_DIR_BIDI;
1266 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1269 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1271 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1276 if (!qc_blocking_mode(qc))
1277 /* Non-blocking mode, so just bail immediately. */
1278 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1280 /* Block until we have a stream. */
1283 wargs.expect_id = expect_id;
1285 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1287 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1288 else if (res < 0 || wargs.qs == NULL)
1289 /* quic_wait_for_stream raised error here */
1296 * We now have qs != NULL. Make it the default stream, creating the
1299 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1300 if (qc->default_xso == NULL)
1301 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1303 qc_touch_default_xso(qc); /* inhibits default XSO */
1308 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1310 QUIC_XSO *xso = NULL;
1312 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1315 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1319 xso->blocking = qc->default_blocking;
1320 xso->ssl_mode = qc->default_ssl_mode;
1332 /* locking depends on need_lock */
1333 static SSL *quic_conn_stream_new(QUIC_CONNECTION *qc, uint64_t flags,
1336 QUIC_XSO *xso = NULL;
1337 QUIC_STREAM *qs = NULL;
1338 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1343 if (ossl_quic_channel_is_term_any(qc->ch)) {
1344 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1348 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1352 xso = create_xso_from_stream(qc, qs);
1356 qc_touch_default_xso(qc); /* inhibits default XSO */
1364 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1373 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1377 if (!expect_quic_conn_only(s, &ctx))
1380 return quic_conn_stream_new(ctx.qc, flags, /*need_lock=*/1);
1384 * QUIC Front-End I/O API: Steady-State Operations
1385 * ===============================================
1387 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1388 * is, the functions used during the established phase of a QUIC connection
1389 * (e.g. SSL_read, SSL_write).
1391 * Each function must handle both blocking and non-blocking modes. As discussed
1392 * above, all QUIC I/O is implemented using non-blocking mode internally.
1394 * SSL_get_error => partially implemented by ossl_quic_get_error
1395 * (BIO/)SSL_read => ossl_quic_read
1396 * (BIO/)SSL_write => ossl_quic_write
1397 * SSL_pending => ossl_quic_pending
1398 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1402 int ossl_quic_get_error(const SSL *s, int i)
1406 if (!expect_quic(s, &ctx))
1409 return ctx.qc->last_error;
1416 * The set of functions below provide the implementation of the public SSL_write
1417 * function. We must handle:
1419 * - both blocking and non-blocking operation at the application level,
1420 * depending on how we are configured;
1422 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1424 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1428 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1431 * We have appended at least one byte to the stream.
1432 * Potentially mark stream as active, depending on FC.
1435 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1441 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1442 * plus we should eventually consider Nagle's algorithm.
1445 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1448 struct quic_write_again_args {
1450 const unsigned char *buf;
1452 size_t total_written;
1456 static int quic_write_again(void *arg)
1458 struct quic_write_again_args *args = arg;
1459 size_t actual_written = 0;
1461 if (!ossl_quic_channel_is_active(args->xso->conn->ch))
1462 /* If connection is torn down due to an error while blocking, stop. */
1465 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1466 args->buf, args->len, &actual_written))
1469 quic_post_write(args->xso, actual_written > 0, 0);
1471 args->buf += actual_written;
1472 args->len -= actual_written;
1473 args->total_written += actual_written;
1476 /* Written everything, done. */
1479 /* Not written everything yet, keep trying. */
1484 static int quic_write_blocking(QUIC_XSO *xso, const void *buf, size_t len,
1488 struct quic_write_again_args args;
1489 size_t actual_written = 0;
1491 /* First make a best effort to append as much of the data as possible. */
1492 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1494 /* Stream already finished or allocation error. */
1496 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1499 quic_post_write(xso, actual_written > 0, 1);
1501 if (actual_written == len) {
1502 /* Managed to append everything on the first try. */
1503 *written = actual_written;
1508 * We did not manage to append all of the data immediately, so the stream
1509 * buffer has probably filled up. This means we need to block until some of
1513 args.buf = (const unsigned char *)buf + actual_written;
1514 args.len = len - actual_written;
1515 args.total_written = 0;
1517 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1519 if (!ossl_quic_channel_is_active(xso->conn->ch))
1520 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1522 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1525 *written = args.total_written;
1530 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1533 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1534 size_t buf_len, size_t already_sent)
1536 assert(!xso->aon_write_in_progress);
1538 xso->aon_write_in_progress = 1;
1539 xso->aon_buf_base = buf;
1540 xso->aon_buf_pos = already_sent;
1541 xso->aon_buf_len = buf_len;
1544 static void aon_write_finish(QUIC_XSO *xso)
1546 xso->aon_write_in_progress = 0;
1547 xso->aon_buf_base = NULL;
1548 xso->aon_buf_pos = 0;
1549 xso->aon_buf_len = 0;
1553 static int quic_write_nonblocking_aon(QUIC_XSO *xso, const void *buf,
1554 size_t len, size_t *written)
1556 const void *actual_buf;
1557 size_t actual_len, actual_written = 0;
1558 int accept_moving_buffer
1559 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1561 if (xso->aon_write_in_progress) {
1563 * We are in the middle of an AON write (i.e., a previous write did not
1564 * manage to append all data to the SSTREAM and we have Enable Partial
1565 * Write (EPW) mode disabled.)
1567 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1568 || len != xso->aon_buf_len)
1570 * Pointer must not have changed if we are not in accept moving
1571 * buffer mode. Length must never change.
1573 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_BAD_WRITE_RETRY, NULL);
1575 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1576 actual_len = len - xso->aon_buf_pos;
1577 assert(actual_len > 0);
1583 /* First make a best effort to append as much of the data as possible. */
1584 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1586 /* Stream already finished or allocation error. */
1588 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1591 quic_post_write(xso, actual_written > 0, 1);
1593 if (actual_written == actual_len) {
1594 /* We have sent everything. */
1595 if (xso->aon_write_in_progress) {
1597 * We have sent everything, and we were in the middle of an AON
1598 * write. The output write length is the total length of the AON
1599 * buffer, not however many bytes we managed to write to the stream
1602 *written = xso->aon_buf_len;
1603 aon_write_finish(xso);
1605 *written = actual_written;
1611 if (xso->aon_write_in_progress) {
1613 * AON write is in progress but we have not written everything yet. We
1614 * may have managed to send zero bytes, or some number of bytes less
1615 * than the total remaining which need to be appended during this
1618 xso->aon_buf_pos += actual_written;
1619 assert(xso->aon_buf_pos < xso->aon_buf_len);
1620 return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE);
1624 * Not in an existing AON operation but partial write is not enabled, so we
1625 * need to begin a new AON operation. However we needn't bother if we didn't
1626 * actually append anything.
1628 if (actual_written > 0)
1629 aon_write_begin(xso, buf, len, actual_written);
1632 * AON - We do not publicly admit to having appended anything until AON
1636 return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE);
1640 static int quic_write_nonblocking_epw(QUIC_XSO *xso, const void *buf, size_t len,
1643 /* Simple best effort operation. */
1644 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
1645 /* Stream already finished or allocation error. */
1647 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1650 quic_post_write(xso, *written > 0, 1);
1655 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1666 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1669 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1671 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1672 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1677 * If we haven't finished the handshake, try to advance it.
1678 * We don't accept writes until the handshake is completed.
1680 if (quic_do_handshake(ctx.qc) < 1) {
1685 if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
1686 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1690 if (xso_blocking_mode(ctx.xso))
1691 ret = quic_write_blocking(ctx.xso, buf, len, written);
1692 else if (partial_write)
1693 ret = quic_write_nonblocking_epw(ctx.xso, buf, len, written);
1695 ret = quic_write_nonblocking_aon(ctx.xso, buf, len, written);
1698 quic_unlock(ctx.qc);
1706 struct quic_read_again_args {
1707 QUIC_CONNECTION *qc;
1708 QUIC_STREAM *stream;
1716 static int quic_read_actual(QUIC_CONNECTION *qc,
1717 QUIC_STREAM *stream,
1718 void *buf, size_t buf_len,
1724 /* If the receive part of the stream is over, issue EOF. */
1725 if (stream->recv_fin_retired)
1726 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_ZERO_RETURN);
1728 if (stream->rstream == NULL)
1729 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1732 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1733 bytes_read, &is_fin))
1734 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1737 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1738 bytes_read, &is_fin))
1739 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1743 if (*bytes_read > 0) {
1745 * We have read at least one byte from the stream. Inform stream-level
1746 * RXFC of the retirement of controlled bytes. Update the active stream
1747 * status (the RXFC may now want to emit a frame granting more credit to
1750 OSSL_RTT_INFO rtt_info;
1752 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1754 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
1755 rtt_info.smoothed_rtt))
1756 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1760 stream->recv_fin_retired = 1;
1762 if (*bytes_read > 0)
1763 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1771 static int quic_read_again(void *arg)
1773 struct quic_read_again_args *args = arg;
1775 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1776 /* If connection is torn down due to an error while blocking, stop. */
1777 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1781 if (!quic_read_actual(args->qc, args->stream,
1782 args->buf, args->len, args->bytes_read,
1786 if (*args->bytes_read > 0)
1787 /* got at least one byte, the SSL_read op can finish now */
1790 return 0; /* did not read anything, keep trying */
1794 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1798 struct quic_read_again_args args;
1802 if (!expect_quic(s, &ctx))
1807 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1808 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1812 /* If we haven't finished the handshake, try to advance it. */
1813 if (quic_do_handshake(ctx.qc) < 1) {
1814 ret = 0; /* ossl_quic_do_handshake raised error here */
1818 if (ctx.xso == NULL) {
1820 * Called on a QCSO and we don't currently have a default stream.
1822 * Wait until we get a stream initiated by the peer (blocking mode) or
1823 * fail if we don't have one yet (non-blocking mode).
1825 if (!qc_wait_for_default_xso_for_read(ctx.qc)) {
1826 ret = 0; /* error already raised here */
1830 ctx.xso = ctx.qc->default_xso;
1833 if (ctx.xso->stream == NULL) {
1834 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1838 if (!quic_read_actual(ctx.qc, ctx.xso->stream, buf, len, bytes_read, peek)) {
1839 ret = 0; /* quic_read_actual raised error here */
1843 if (*bytes_read > 0) {
1845 * Even though we succeeded, tick the reactor here to ensure we are
1846 * handling other aspects of the QUIC connection.
1848 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1850 } else if (xso_blocking_mode(ctx.xso)) {
1852 * We were not able to read anything immediately, so our stream
1853 * buffer is empty. This means we need to block until we get
1854 * at least one byte.
1857 args.stream = ctx.xso->stream;
1860 args.bytes_read = bytes_read;
1863 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
1865 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1867 } else if (res < 0) {
1868 ret = 0; /* quic_read_again raised error here */
1874 /* We did not get any bytes and are not in blocking mode. */
1875 ret = QUIC_RAISE_NORMAL_ERROR(ctx.qc, SSL_ERROR_WANT_READ);
1879 quic_unlock(ctx.qc);
1883 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
1885 return quic_read(s, buf, len, bytes_read, 0);
1888 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
1890 return quic_read(s, buf, len, bytes_read, 1);
1898 static size_t ossl_quic_pending_int(const SSL *s)
1904 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
1907 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
1908 /* Cannot raise errors here because we are const, just fail. */
1911 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
1915 quic_unlock(ctx.qc);
1919 size_t ossl_quic_pending(const SSL *s)
1921 return ossl_quic_pending_int(s);
1924 int ossl_quic_has_pending(const SSL *s)
1926 return ossl_quic_pending_int(s) > 0;
1930 * SSL_stream_conclude
1931 * -------------------
1934 int ossl_quic_conn_stream_conclude(SSL *s)
1939 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1942 qs = ctx.xso->stream;
1944 if (qs == NULL || qs->sstream == NULL) {
1945 quic_unlock(ctx.qc);
1949 if (!ossl_quic_channel_is_active(ctx.qc->ch)
1950 || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
1951 quic_unlock(ctx.qc);
1955 ossl_quic_sstream_fin(qs->sstream);
1956 quic_post_write(ctx.xso, 1, 1);
1957 quic_unlock(ctx.qc);
1962 * SSL_inject_net_dgram
1963 * --------------------
1966 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
1968 const BIO_ADDR *peer,
1969 const BIO_ADDR *local)
1975 if (!expect_quic(s, &ctx))
1980 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
1981 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
1983 quic_unlock(ctx.qc);
1988 * SSL_get0_connection
1989 * -------------------
1991 SSL *ossl_quic_get0_connection(SSL *s)
1995 if (!expect_quic(s, &ctx))
1998 return &ctx.qc->ssl;
2002 * SSL_get_stream_type
2003 * -------------------
2005 int ossl_quic_get_stream_type(SSL *s)
2009 if (!expect_quic(s, &ctx))
2010 return SSL_STREAM_TYPE_NONE;
2012 if (ctx.xso == NULL) {
2014 * If we are deferring XSO creation, assume single stream mode and
2015 * default to BIDI, as the deferred XSO which will be created will be
2018 if (!ctx.qc->default_xso_created)
2019 return SSL_STREAM_TYPE_BIDI;
2021 return SSL_STREAM_TYPE_NONE;
2024 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2025 return SSL_STREAM_TYPE_BIDI;
2027 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2028 return SSL_STREAM_TYPE_READ;
2030 return SSL_STREAM_TYPE_WRITE;
2038 uint64_t ossl_quic_get_stream_id(SSL *s)
2043 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2046 id = ctx.xso->stream->id;
2047 quic_unlock(ctx.qc);
2053 * SSL_set_default_stream_mode
2054 * ---------------------------
2057 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2061 if (!expect_quic_conn_only(s, &ctx))
2066 if (ctx.qc->default_xso_created)
2067 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2068 "too late to change default stream mode");
2071 case SSL_DEFAULT_STREAM_MODE_NONE:
2072 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2073 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2074 ctx.qc->default_stream_mode = mode;
2077 quic_unlock(ctx.qc);
2078 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_PASSED_INVALID_ARGUMENT,
2079 "bad default stream type");
2082 quic_unlock(ctx.qc);
2091 SSL *ossl_quic_detach_stream(SSL *s)
2096 if (!expect_quic_conn_only(s, &ctx))
2101 /* Calling this function inhibits default XSO autocreation. */
2102 xso = ctx.qc->default_xso;
2103 qc_set_default_xso(ctx.qc, NULL, /*touch=*/1);
2105 quic_unlock(ctx.qc);
2115 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2119 if (!expect_quic_conn_only(conn, &ctx))
2122 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2123 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_PASSED_NULL_PARAMETER,
2124 "stream to attach must be a valid QUIC stream");
2128 if (ctx.qc->default_xso != NULL) {
2129 quic_unlock(ctx.qc);
2130 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2131 "connection already has a default stream");
2134 /* Calling this function inhibits default XSO autocreation. */
2135 qc_set_default_xso(ctx.qc, (QUIC_XSO *)stream, /*touch=*/1);
2137 quic_unlock(ctx.qc);
2142 * SSL_set_incoming_stream_reject_policy
2143 * -------------------------------------
2146 static int qc_get_effective_incoming_stream_reject_policy(QUIC_CONNECTION *qc)
2148 switch (qc->incoming_stream_reject_policy) {
2149 case SSL_INCOMING_STREAM_REJECT_POLICY_AUTO:
2150 if ((qc->default_xso == NULL && !qc->default_xso_created)
2151 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2152 return SSL_INCOMING_STREAM_REJECT_POLICY_ACCEPT;
2154 return SSL_INCOMING_STREAM_REJECT_POLICY_REJECT;
2157 return qc->incoming_stream_reject_policy;
2162 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2164 int policy = qc_get_effective_incoming_stream_reject_policy(qc);
2165 int enable_reject = (policy == SSL_INCOMING_STREAM_REJECT_POLICY_REJECT);
2167 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2169 qc->incoming_stream_reject_aec);
2173 int ossl_quic_set_incoming_stream_reject_policy(SSL *s, int policy,
2179 if (!expect_quic_conn_only(s, &ctx))
2185 case SSL_INCOMING_STREAM_REJECT_POLICY_AUTO:
2186 case SSL_INCOMING_STREAM_REJECT_POLICY_ACCEPT:
2187 case SSL_INCOMING_STREAM_REJECT_POLICY_REJECT:
2188 ctx.qc->incoming_stream_reject_policy = policy;
2189 ctx.qc->incoming_stream_reject_aec = aec;
2197 qc_update_reject_policy(ctx.qc);
2198 quic_unlock(ctx.qc);
2206 struct wait_for_incoming_stream_args {
2207 QUIC_CONNECTION *qc;
2212 static int wait_for_incoming_stream(void *arg)
2214 struct wait_for_incoming_stream_args *args = arg;
2215 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(args->qc->ch);
2217 if (!ossl_quic_channel_is_active(args->qc->ch)) {
2218 /* If connection is torn down due to an error while blocking, stop. */
2219 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2223 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2224 if (args->qs != NULL)
2225 return 1; /* got a stream */
2227 return 0; /* did not get a stream, keep trying */
2231 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2236 QUIC_STREAM_MAP *qsm;
2239 OSSL_RTT_INFO rtt_info;
2241 if (!expect_quic_conn_only(s, &ctx))
2246 if (qc_get_effective_incoming_stream_reject_policy(ctx.qc)
2247 == SSL_INCOMING_STREAM_REJECT_POLICY_REJECT)
2250 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2252 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2254 if (qc_blocking_mode(ctx.qc)
2255 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2256 struct wait_for_incoming_stream_args args;
2261 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2263 QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
2265 } else if (ret < 0 || args.qs == NULL) {
2275 xso = create_xso_from_stream(ctx.qc, qs);
2279 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2280 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2281 rtt_info.smoothed_rtt);
2284 /* Calling this function inhibits default XSO autocreation. */
2285 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2288 quic_unlock(ctx.qc);
2293 * SSL_get_accept_stream_queue_len
2294 * -------------------------------
2297 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2302 if (!expect_quic_conn_only(s, &ctx))
2307 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2309 quic_unlock(ctx.qc);
2317 int ossl_quic_stream_reset(SSL *ssl,
2318 const SSL_STREAM_RESET_ARGS *args,
2322 QUIC_STREAM_MAP *qsm;
2324 uint64_t error_code;
2326 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2329 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2330 qs = ctx.xso->stream;
2331 error_code = (args != NULL ? args->quic_error_code : 0);
2333 ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2335 quic_unlock(ctx.qc);
2340 * SSL_get_stream_read_state
2341 * -------------------------
2343 static void quic_classify_stream(QUIC_CONNECTION *qc,
2347 uint64_t *app_error_code)
2350 uint64_t final_size;
2352 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2354 if (app_error_code != NULL)
2355 *app_error_code = UINT64_MAX;
2357 app_error_code = &final_size; /* throw away value */
2359 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2361 * Unidirectional stream and this direction of transmission doesn't
2364 *state = SSL_STREAM_STATE_WRONG_DIR;
2365 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2366 /* Connection already closed. */
2367 *state = SSL_STREAM_STATE_CONN_CLOSED;
2368 } else if (!is_write && qs->recv_fin_retired) {
2369 /* Application has read a FIN. */
2370 *state = SSL_STREAM_STATE_FINISHED;
2371 } else if ((!is_write && qs->stop_sending)
2372 || (is_write && qs->reset_stream)) {
2374 * Stream has been reset locally. FIN takes precedence over this for the
2375 * read case as the application need not care if the stream is reset
2376 * after a FIN has been successfully processed.
2378 *state = SSL_STREAM_STATE_RESET_LOCAL;
2379 *app_error_code = !is_write
2380 ? qs->stop_sending_aec
2381 : qs->reset_stream_aec;
2382 } else if ((!is_write && qs->peer_reset_stream)
2383 || (is_write && qs->peer_stop_sending)) {
2385 * Stream has been reset remotely. */
2386 *state = SSL_STREAM_STATE_RESET_REMOTE;
2387 *app_error_code = !is_write
2388 ? qs->peer_reset_stream_aec
2389 : qs->peer_stop_sending_aec;
2390 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2393 * Stream has been finished. Stream reset takes precedence over this for
2394 * the write case as peer may not have received all data.
2396 *state = SSL_STREAM_STATE_FINISHED;
2398 /* Stream still healthy. */
2399 *state = SSL_STREAM_STATE_OK;
2403 static int quic_get_stream_state(SSL *ssl, int is_write)
2408 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2409 return SSL_STREAM_STATE_NONE;
2411 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2412 quic_unlock(ctx.qc);
2416 int ossl_quic_get_stream_read_state(SSL *ssl)
2418 return quic_get_stream_state(ssl, /*is_write=*/0);
2422 * SSL_get_stream_write_state
2423 * --------------------------
2425 int ossl_quic_get_stream_write_state(SSL *ssl)
2427 return quic_get_stream_state(ssl, /*is_write=*/1);
2431 * SSL_get_stream_read_error_code
2432 * ------------------------------
2434 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2435 uint64_t *app_error_code)
2440 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2443 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2444 &state, app_error_code);
2446 quic_unlock(ctx.qc);
2448 case SSL_STREAM_STATE_FINISHED:
2450 case SSL_STREAM_STATE_RESET_LOCAL:
2451 case SSL_STREAM_STATE_RESET_REMOTE:
2458 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2460 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2464 * SSL_get_stream_write_error_code
2465 * -------------------------------
2467 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2469 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2473 * SSL_get_conn_close_info
2474 * -----------------------
2476 int ossl_quic_get_conn_close_info(SSL *ssl,
2477 SSL_CONN_CLOSE_INFO *info,
2481 const QUIC_TERMINATE_CAUSE *tc;
2483 if (!expect_quic_conn_only(ssl, &ctx))
2486 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
2490 info->error_code = tc->error_code;
2491 info->reason = NULL; /* TODO(QUIC): Wire reason */
2492 info->reason_len = 0;
2493 info->is_local = !tc->remote;
2494 info->is_transport = !tc->app;
2499 * QUIC Front-End I/O API: SSL_CTX Management
2500 * ==========================================
2503 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2507 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
2511 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
2513 return ssl3_callback_ctrl(s, cmd, fp);
2516 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
2518 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
2521 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
2523 /* We never do renegotiation. */
2528 * These functions define the TLSv1.2 (and below) ciphers that are supported by
2529 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
2532 int ossl_quic_num_ciphers(void)
2537 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)