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))
348 * When a QSSO is freed, the XSO is freed immediately, because the XSO
349 * itself only contains API personality layer data. However the
350 * underlying QUIC_STREAM is not freed immediately but is instead marked
351 * 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 */
380 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
381 * stage, but is freed during the channel free when the whole QSM is freed.
383 if (ctx.qc->default_xso != NULL) {
384 QUIC_XSO *xso = ctx.qc->default_xso;
391 /* Ensure we have no remaining XSOs. */
392 assert(ctx.qc->num_xso == 0);
394 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
395 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
396 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
399 ossl_quic_channel_free(ctx.qc->ch);
401 BIO_free(ctx.qc->net_rbio);
402 BIO_free(ctx.qc->net_wbio);
404 /* Note: SSL_free calls OPENSSL_free(qc) for us */
406 SSL_free(ctx.qc->tls);
407 ossl_crypto_mutex_free(&ctx.qc->mutex); /* freed while still locked */
410 /* SSL method init */
411 int ossl_quic_init(SSL *s)
413 /* Same op as SSL_clear, forward the call. */
414 return ossl_quic_clear(s);
417 /* SSL method deinit */
418 void ossl_quic_deinit(SSL *s)
424 int ossl_quic_reset(SSL *s)
428 if (!expect_quic(s, &ctx))
431 /* TODO(QUIC); Currently a no-op. */
436 int ossl_quic_clear(SSL *s)
440 if (!expect_quic(s, &ctx))
443 /* TODO(QUIC): Currently a no-op. */
447 void ossl_quic_conn_set_override_now_cb(SSL *s,
448 OSSL_TIME (*now_cb)(void *arg),
453 if (!expect_quic(s, &ctx))
456 ctx.qc->override_now_cb = now_cb;
457 ctx.qc->override_now_cb_arg = now_cb_arg;
460 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
464 if (!expect_quic(s, &ctx))
467 if (ctx.qc->is_thread_assisted && ctx.qc->started)
468 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
472 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
474 qc->default_xso_created = 1;
475 qc_update_reject_policy(qc);
479 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
481 qc->default_xso = xso;
483 qc_touch_default_xso(qc);
487 * QUIC Front-End I/O API: Network BIO Configuration
488 * =================================================
490 * Handling the different BIOs is difficult:
492 * - It is more or less a requirement that we use non-blocking network I/O;
493 * we need to be able to have timeouts on recv() calls, and make best effort
494 * (non blocking) send() and recv() calls.
496 * The only sensible way to do this is to configure the socket into
497 * non-blocking mode. We could try to do select() before calling send() or
498 * recv() to get a guarantee that the call will not block, but this will
499 * probably run into issues with buggy OSes which generate spurious socket
500 * readiness events. In any case, relying on this to work reliably does not
503 * Timeouts could be handled via setsockopt() socket timeout options, but
504 * this depends on OS support and adds another syscall to every network I/O
505 * operation. It also has obvious thread safety concerns if we want to move
506 * to concurrent use of a single socket at some later date.
508 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
509 * be made non-blocking. However some OSes (e.g. Windows) do not support
510 * this, so we cannot rely on this.
512 * As such, we need to configure any FD in non-blocking mode. This may
513 * confound users who pass a blocking socket to libssl. However, in practice
514 * it would be extremely strange for a user of QUIC to pass an FD to us,
515 * then also try and send receive traffic on the same socket(!). Thus the
516 * impact of this should be limited, and can be documented.
518 * - We support both blocking and non-blocking operation in terms of the API
519 * presented to the user. One prospect is to set the blocking mode based on
520 * whether the socket passed to us was already in blocking mode. However,
521 * Windows has no API for determining if a socket is in blocking mode (!),
522 * therefore this cannot be done portably. Currently therefore we expose an
523 * explicit API call to set this, and default to blocking mode.
525 * - We need to determine our initial destination UDP address. The "natural"
526 * way for a user to do this is to set the peer variable on a BIO_dgram.
527 * However, this has problems because BIO_dgram's peer variable is used for
528 * both transmission and reception. This means it can be constantly being
529 * changed to a malicious value (e.g. if some random unrelated entity on the
530 * network starts sending traffic to us) on every read call. This is not a
531 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
532 * calls only, which do not use this variable. However, we do need to let
533 * the user specify the peer in a 'normal' manner. The compromise here is
534 * that we grab the current peer value set at the time the write BIO is set
535 * and do not read the value again.
537 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
538 * Currently we do this by only supporting non-blocking mode.
543 * Determines what initial destination UDP address we should use, if possible.
544 * If this fails the client must set the destination address manually, or use a
545 * BIO which does not need a destination address.
547 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
549 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
555 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
559 if (!expect_quic(s, &ctx))
562 if (ctx.qc->net_rbio == net_rbio)
565 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
568 BIO_free(ctx.qc->net_rbio);
569 ctx.qc->net_rbio = net_rbio;
572 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
573 * mode as we do not support it for non-pollable BIOs.
575 if (net_rbio != NULL) {
576 BIO_POLL_DESCRIPTOR d = {0};
578 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
579 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
580 ctx.qc->blocking = 0;
581 ctx.qc->default_blocking = 0;
582 ctx.qc->can_poll_net_rbio = 0;
584 ctx.qc->can_poll_net_rbio = 1;
589 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
593 if (!expect_quic(s, &ctx))
596 if (ctx.qc->net_wbio == net_wbio)
599 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
602 BIO_free(ctx.qc->net_wbio);
603 ctx.qc->net_wbio = net_wbio;
605 if (net_wbio != NULL) {
606 BIO_POLL_DESCRIPTOR d = {0};
608 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
609 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
610 ctx.qc->blocking = 0;
611 ctx.qc->default_blocking = 0;
612 ctx.qc->can_poll_net_wbio = 0;
614 ctx.qc->can_poll_net_wbio = 1;
618 * If we do not have a peer address yet, and we have not started trying
619 * to connect yet, try to autodetect one.
621 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
622 && !ctx.qc->started) {
623 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
625 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
627 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
628 &ctx.qc->init_peer_addr);
633 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
637 if (!expect_quic(s, &ctx))
640 return ctx.qc->net_rbio;
643 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
647 if (!expect_quic(s, &ctx))
650 return ctx.qc->net_wbio;
653 int ossl_quic_conn_get_blocking_mode(const SSL *s)
657 if (!expect_quic(s, &ctx))
661 return ctx.xso->blocking;
663 return ctx.qc->blocking;
666 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
670 if (!expect_quic(s, &ctx))
673 /* Cannot enable blocking mode if we do not have pollable FDs. */
675 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
676 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_UNSUPPORTED, NULL);
678 if (!ctx.is_stream) {
680 * If called on a QCSO, update default and connection-level blocking
683 ctx.qc->blocking = (blocking != 0);
684 ctx.qc->default_blocking = ctx.qc->blocking;
689 * If called on a QSSO or QCSO with a default XSO, update blocking
692 ctx.xso->blocking = (blocking != 0);
697 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
698 const BIO_ADDR *peer_addr)
702 if (!expect_quic(s, &ctx))
706 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
709 if (peer_addr == NULL) {
710 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
714 ctx.qc->init_peer_addr = *peer_addr;
719 * QUIC Front-End I/O API: Asynchronous I/O Management
720 * ===================================================
722 * (BIO/)SSL_tick => ossl_quic_tick
723 * (BIO/)SSL_get_tick_timeout => ossl_quic_get_tick_timeout
724 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
728 /* Returns 1 if the connection is being used in blocking mode. */
729 static int qc_blocking_mode(const QUIC_CONNECTION *qc)
734 static int xso_blocking_mode(const QUIC_XSO *xso)
737 && xso->conn->can_poll_net_rbio
738 && xso->conn->can_poll_net_wbio;
741 /* SSL_tick; ticks the reactor. */
743 int ossl_quic_tick(SSL *s)
747 if (!expect_quic(s, &ctx))
751 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
757 * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object
758 * should be ticked by the application by calling SSL_tick(). tv is set to 0 if
759 * the object should be ticked immediately and tv->tv_sec is set to -1 if no
760 * timeout is currently active.
763 int ossl_quic_get_tick_timeout(SSL *s, struct timeval *tv)
766 OSSL_TIME deadline = ossl_time_infinite();
768 if (!expect_quic(s, &ctx))
774 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
776 if (ossl_time_is_infinite(deadline)) {
783 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now()));
788 /* SSL_get_rpoll_descriptor */
789 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
793 if (!expect_quic(s, &ctx))
796 if (desc == NULL || ctx.qc->net_rbio == NULL)
799 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
802 /* SSL_get_wpoll_descriptor */
803 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
807 if (!expect_quic(s, &ctx))
810 if (desc == NULL || ctx.qc->net_wbio == NULL)
813 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
816 /* SSL_net_read_desired */
818 int ossl_quic_get_net_read_desired(SSL *s)
823 if (!expect_quic(s, &ctx))
827 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
832 /* SSL_net_write_desired */
834 int ossl_quic_get_net_write_desired(SSL *s)
839 if (!expect_quic(s, &ctx))
843 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
849 * QUIC Front-End I/O API: Connection Lifecycle Operations
850 * =======================================================
852 * SSL_do_handshake => ossl_quic_do_handshake
853 * SSL_set_connect_state => ossl_quic_set_connect_state
854 * SSL_set_accept_state => ossl_quic_set_accept_state
855 * SSL_shutdown => ossl_quic_shutdown
856 * SSL_ctrl => ossl_quic_ctrl
857 * (BIO/)SSL_connect => ossl_quic_connect
858 * (BIO/)SSL_accept => ossl_quic_accept
863 static int quic_shutdown_wait(void *arg)
865 QUIC_CONNECTION *qc = arg;
867 return ossl_quic_channel_is_terminated(qc->ch);
871 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
872 const SSL_SHUTDOWN_EX_ARGS *args,
878 if (!expect_quic(s, &ctx))
882 /* TODO(QUIC): Semantics currently undefined for QSSOs */
887 ossl_quic_channel_local_close(ctx.qc->ch,
888 args != NULL ? args->quic_error_code : 0);
890 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
892 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
897 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
898 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
900 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
902 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
908 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
912 if (!expect_quic(s, &ctx))
917 /* If called on a QCSO, update the default mode. */
919 ctx.qc->default_ssl_mode |= (uint32_t)larg;
922 * If we were called on a QSSO or have a default stream, we also update
925 if (ctx.xso != NULL) {
926 /* Cannot enable EPW while AON write in progress. */
927 if (ctx.xso->aon_write_in_progress)
928 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
930 ctx.xso->ssl_mode |= (uint32_t)larg;
931 return ctx.xso->ssl_mode;
934 return ctx.qc->default_ssl_mode;
935 case SSL_CTRL_CLEAR_MODE:
937 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
939 if (ctx.xso != NULL) {
940 ctx.xso->ssl_mode &= ~(uint32_t)larg;
941 return ctx.xso->ssl_mode;
944 return ctx.qc->default_ssl_mode;
946 /* Probably a TLS related ctrl. Defer to our internal SSL object */
947 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
951 /* SSL_set_connect_state */
952 void ossl_quic_set_connect_state(SSL *s)
956 if (!expect_quic(s, &ctx))
959 /* Cannot be changed after handshake started */
960 if (ctx.qc->started || ctx.is_stream)
963 ctx.qc->as_server_state = 0;
966 /* SSL_set_accept_state */
967 void ossl_quic_set_accept_state(SSL *s)
971 if (!expect_quic(s, &ctx))
974 /* Cannot be changed after handshake started */
975 if (ctx.qc->started || ctx.is_stream)
978 ctx.qc->as_server_state = 1;
981 /* SSL_do_handshake */
982 struct quic_handshake_wait_args {
986 static int quic_handshake_wait(void *arg)
988 struct quic_handshake_wait_args *args = arg;
990 if (!ossl_quic_channel_is_active(args->qc->ch))
993 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
999 static int configure_channel(QUIC_CONNECTION *qc)
1001 assert(qc->ch != NULL);
1003 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1004 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1005 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1012 static int create_channel(QUIC_CONNECTION *qc)
1014 QUIC_CHANNEL_ARGS args = {0};
1016 args.libctx = qc->ssl.ctx->libctx;
1017 args.propq = qc->ssl.ctx->propq;
1018 args.is_server = qc->as_server;
1020 args.mutex = qc->mutex;
1021 args.now_cb = qc->override_now_cb;
1022 args.now_cb_arg = qc->override_now_cb_arg;
1024 qc->ch = ossl_quic_channel_new(&args);
1032 * Creates a channel and configures it with the information we have accumulated
1033 * via calls made to us from the application prior to starting a handshake
1037 static int ensure_channel_started(QUIC_CONNECTION *qc)
1040 if (!configure_channel(qc)
1041 || !ossl_quic_channel_start(qc->ch))
1044 if (qc->is_thread_assisted)
1045 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1053 ossl_quic_channel_free(qc->ch);
1059 static int quic_do_handshake(QUIC_CONNECTION *qc)
1063 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1064 /* Handshake already completed. */
1067 if (ossl_quic_channel_is_term_any(qc->ch))
1068 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1070 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1071 /* Peer address must have been set. */
1072 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1073 return -1; /* Non-protocol error */
1076 if (qc->as_server != qc->as_server_state) {
1077 /* TODO(QUIC): Must match the method used to create the QCSO */
1078 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1079 return -1; /* Non-protocol error */
1082 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1083 /* Need read and write BIOs. */
1084 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BIO_NOT_SET, NULL);
1085 return -1; /* Non-protocol error */
1089 * Start connection process. Note we may come here multiple times in
1090 * non-blocking mode, which is fine.
1092 if (!ensure_channel_started(qc)) {
1093 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1094 return -1; /* Non-protocol error */
1097 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1098 /* The handshake is now done. */
1101 if (qc_blocking_mode(qc)) {
1102 /* In blocking mode, wait for the handshake to complete. */
1103 struct quic_handshake_wait_args args;
1107 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1108 if (!ossl_quic_channel_is_active(qc->ch)) {
1109 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1110 return 0; /* Shutdown before completion */
1111 } else if (ret <= 0) {
1112 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1113 return -1; /* Non-protocol error */
1116 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1119 /* Try to advance the reactor. */
1120 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1122 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1123 /* The handshake is now done. */
1126 /* Otherwise, indicate that the handshake isn't done yet. */
1127 QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1128 return -1; /* Non-protocol error */
1133 int ossl_quic_do_handshake(SSL *s)
1138 if (!expect_quic(s, &ctx))
1143 ret = quic_do_handshake(ctx.qc);
1144 quic_unlock(ctx.qc);
1149 int ossl_quic_connect(SSL *s)
1151 /* Ensure we are in connect state (no-op if non-idle). */
1152 ossl_quic_set_connect_state(s);
1154 /* Begin or continue the handshake */
1155 return ossl_quic_do_handshake(s);
1159 int ossl_quic_accept(SSL *s)
1161 /* Ensure we are in accept state (no-op if non-idle). */
1162 ossl_quic_set_accept_state(s);
1164 /* Begin or continue the handshake */
1165 return ossl_quic_do_handshake(s);
1169 * QUIC Front-End I/O API: Stream Lifecycle Operations
1170 * ===================================================
1172 * SSL_stream_new => ossl_quic_conn_stream_new
1177 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1178 * default XSO was created. Returns 0 if it was not (e.g. because it already
1179 * exists). Note that this is NOT an error condition.
1182 static int qc_try_create_default_xso_for_write(QUIC_CONNECTION *qc)
1186 if (qc->default_xso_created
1187 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1189 * We only do this once. If the user detaches a previously created
1190 * default XSO we don't auto-create another one.
1192 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_NO_STREAM, NULL);
1194 /* Create a locally-initiated stream. */
1195 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1196 flags |= SSL_STREAM_FLAG_UNI;
1198 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(qc, flags,
1201 if (qc->default_xso == NULL)
1202 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1204 qc_touch_default_xso(qc);
1208 struct quic_wait_for_stream_args {
1209 QUIC_CONNECTION *qc;
1215 static int quic_wait_for_stream(void *arg)
1217 struct quic_wait_for_stream_args *args = arg;
1219 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1220 /* If connection is torn down due to an error while blocking, stop. */
1221 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1225 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1226 args->expect_id | QUIC_STREAM_DIR_BIDI);
1227 if (args->qs == NULL)
1228 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1229 args->expect_id | QUIC_STREAM_DIR_UNI);
1231 if (args->qs != NULL)
1232 return 1; /* stream now exists */
1234 return 0; /* did not get a stream, keep trying */
1238 static int qc_wait_for_default_xso_for_read(QUIC_CONNECTION *qc)
1240 /* Called on a QCSO and we don't currently have a default stream. */
1244 struct quic_wait_for_stream_args wargs;
1247 * If default stream functionality is disabled or we already detached
1248 * one, don't make another default stream and just fail.
1250 if (qc->default_xso_created
1251 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1252 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_NO_STREAM, NULL);
1255 * The peer may have opened a stream since we last ticked. So tick and
1256 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1257 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1258 * first stream created by a peer must have an ordinal of 0.
1260 expect_id = qc->as_server
1261 ? QUIC_STREAM_INITIATOR_CLIENT
1262 : QUIC_STREAM_INITIATOR_SERVER;
1264 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1265 expect_id | QUIC_STREAM_DIR_BIDI);
1267 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1268 expect_id | QUIC_STREAM_DIR_UNI);
1271 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1273 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1278 if (!qc_blocking_mode(qc))
1279 /* Non-blocking mode, so just bail immediately. */
1280 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1282 /* Block until we have a stream. */
1285 wargs.expect_id = expect_id;
1287 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1289 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1290 else if (res < 0 || wargs.qs == NULL)
1291 /* quic_wait_for_stream raised error here */
1298 * We now have qs != NULL. Make it the default stream, creating the
1301 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1302 if (qc->default_xso == NULL)
1303 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1305 qc_touch_default_xso(qc); /* inhibits default XSO */
1310 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1312 QUIC_XSO *xso = NULL;
1314 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1317 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1321 xso->blocking = qc->default_blocking;
1322 xso->ssl_mode = qc->default_ssl_mode;
1334 /* locking depends on need_lock */
1335 static SSL *quic_conn_stream_new(QUIC_CONNECTION *qc, uint64_t flags,
1338 QUIC_XSO *xso = NULL;
1339 QUIC_STREAM *qs = NULL;
1340 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1345 if (ossl_quic_channel_is_term_any(qc->ch)) {
1346 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1350 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
1354 xso = create_xso_from_stream(qc, qs);
1358 qc_touch_default_xso(qc); /* inhibits default XSO */
1366 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1375 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1379 if (!expect_quic_conn_only(s, &ctx))
1382 return quic_conn_stream_new(ctx.qc, flags, /*need_lock=*/1);
1386 * QUIC Front-End I/O API: Steady-State Operations
1387 * ===============================================
1389 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1390 * is, the functions used during the established phase of a QUIC connection
1391 * (e.g. SSL_read, SSL_write).
1393 * Each function must handle both blocking and non-blocking modes. As discussed
1394 * above, all QUIC I/O is implemented using non-blocking mode internally.
1396 * SSL_get_error => partially implemented by ossl_quic_get_error
1397 * (BIO/)SSL_read => ossl_quic_read
1398 * (BIO/)SSL_write => ossl_quic_write
1399 * SSL_pending => ossl_quic_pending
1400 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
1404 int ossl_quic_get_error(const SSL *s, int i)
1408 if (!expect_quic(s, &ctx))
1411 return ctx.qc->last_error;
1418 * The set of functions below provide the implementation of the public SSL_write
1419 * function. We must handle:
1421 * - both blocking and non-blocking operation at the application level,
1422 * depending on how we are configured;
1424 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1426 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1430 static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
1433 * We have appended at least one byte to the stream.
1434 * Potentially mark stream as active, depending on FC.
1437 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1443 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1444 * plus we should eventually consider Nagle's algorithm.
1447 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
1450 struct quic_write_again_args {
1452 const unsigned char *buf;
1454 size_t total_written;
1458 static int quic_write_again(void *arg)
1460 struct quic_write_again_args *args = arg;
1461 size_t actual_written = 0;
1463 if (!ossl_quic_channel_is_active(args->xso->conn->ch))
1464 /* If connection is torn down due to an error while blocking, stop. */
1467 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
1468 args->buf, args->len, &actual_written))
1471 quic_post_write(args->xso, actual_written > 0, 0);
1473 args->buf += actual_written;
1474 args->len -= actual_written;
1475 args->total_written += actual_written;
1478 /* Written everything, done. */
1481 /* Not written everything yet, keep trying. */
1486 static int quic_write_blocking(QUIC_XSO *xso, const void *buf, size_t len,
1490 struct quic_write_again_args args;
1491 size_t actual_written = 0;
1493 /* First make a best effort to append as much of the data as possible. */
1494 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
1496 /* Stream already finished or allocation error. */
1498 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1501 quic_post_write(xso, actual_written > 0, 1);
1503 if (actual_written == len) {
1504 /* Managed to append everything on the first try. */
1505 *written = actual_written;
1510 * We did not manage to append all of the data immediately, so the stream
1511 * buffer has probably filled up. This means we need to block until some of
1515 args.buf = (const unsigned char *)buf + actual_written;
1516 args.len = len - actual_written;
1517 args.total_written = 0;
1519 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
1521 if (!ossl_quic_channel_is_active(xso->conn->ch))
1522 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1524 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1527 *written = args.total_written;
1532 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1535 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
1536 size_t buf_len, size_t already_sent)
1538 assert(!xso->aon_write_in_progress);
1540 xso->aon_write_in_progress = 1;
1541 xso->aon_buf_base = buf;
1542 xso->aon_buf_pos = already_sent;
1543 xso->aon_buf_len = buf_len;
1546 static void aon_write_finish(QUIC_XSO *xso)
1548 xso->aon_write_in_progress = 0;
1549 xso->aon_buf_base = NULL;
1550 xso->aon_buf_pos = 0;
1551 xso->aon_buf_len = 0;
1555 static int quic_write_nonblocking_aon(QUIC_XSO *xso, const void *buf,
1556 size_t len, size_t *written)
1558 const void *actual_buf;
1559 size_t actual_len, actual_written = 0;
1560 int accept_moving_buffer
1561 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
1563 if (xso->aon_write_in_progress) {
1565 * We are in the middle of an AON write (i.e., a previous write did not
1566 * manage to append all data to the SSTREAM and we have Enable Partial
1567 * Write (EPW) mode disabled.)
1569 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1570 || len != xso->aon_buf_len)
1572 * Pointer must not have changed if we are not in accept moving
1573 * buffer mode. Length must never change.
1575 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_BAD_WRITE_RETRY, NULL);
1577 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1578 actual_len = len - xso->aon_buf_pos;
1579 assert(actual_len > 0);
1585 /* First make a best effort to append as much of the data as possible. */
1586 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
1588 /* Stream already finished or allocation error. */
1590 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1593 quic_post_write(xso, actual_written > 0, 1);
1595 if (actual_written == actual_len) {
1596 /* We have sent everything. */
1597 if (xso->aon_write_in_progress) {
1599 * We have sent everything, and we were in the middle of an AON
1600 * write. The output write length is the total length of the AON
1601 * buffer, not however many bytes we managed to write to the stream
1604 *written = xso->aon_buf_len;
1605 aon_write_finish(xso);
1607 *written = actual_written;
1613 if (xso->aon_write_in_progress) {
1615 * AON write is in progress but we have not written everything yet. We
1616 * may have managed to send zero bytes, or some number of bytes less
1617 * than the total remaining which need to be appended during this
1620 xso->aon_buf_pos += actual_written;
1621 assert(xso->aon_buf_pos < xso->aon_buf_len);
1622 return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE);
1626 * Not in an existing AON operation but partial write is not enabled, so we
1627 * need to begin a new AON operation. However we needn't bother if we didn't
1628 * actually append anything.
1630 if (actual_written > 0)
1631 aon_write_begin(xso, buf, len, actual_written);
1634 * AON - We do not publicly admit to having appended anything until AON
1638 return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE);
1642 static int quic_write_nonblocking_epw(QUIC_XSO *xso, const void *buf, size_t len,
1645 /* Simple best effort operation. */
1646 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
1647 /* Stream already finished or allocation error. */
1649 return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL);
1652 quic_post_write(xso, *written > 0, 1);
1657 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
1668 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1671 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1673 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1674 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1679 * If we haven't finished the handshake, try to advance it.
1680 * We don't accept writes until the handshake is completed.
1682 if (quic_do_handshake(ctx.qc) < 1) {
1687 if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
1688 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1692 if (xso_blocking_mode(ctx.xso))
1693 ret = quic_write_blocking(ctx.xso, buf, len, written);
1694 else if (partial_write)
1695 ret = quic_write_nonblocking_epw(ctx.xso, buf, len, written);
1697 ret = quic_write_nonblocking_aon(ctx.xso, buf, len, written);
1700 quic_unlock(ctx.qc);
1708 struct quic_read_again_args {
1709 QUIC_CONNECTION *qc;
1710 QUIC_STREAM *stream;
1718 static int quic_read_actual(QUIC_CONNECTION *qc,
1719 QUIC_STREAM *stream,
1720 void *buf, size_t buf_len,
1726 /* If the receive part of the stream is over, issue EOF. */
1727 if (stream->recv_fin_retired)
1728 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_ZERO_RETURN);
1730 if (stream->rstream == NULL)
1731 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1734 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1735 bytes_read, &is_fin))
1736 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1739 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1740 bytes_read, &is_fin))
1741 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1745 if (*bytes_read > 0) {
1747 * We have read at least one byte from the stream. Inform stream-level
1748 * RXFC of the retirement of controlled bytes. Update the active stream
1749 * status (the RXFC may now want to emit a frame granting more credit to
1752 OSSL_RTT_INFO rtt_info;
1754 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1756 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
1757 rtt_info.smoothed_rtt))
1758 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1762 stream->recv_fin_retired = 1;
1764 if (*bytes_read > 0)
1765 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1773 static int quic_read_again(void *arg)
1775 struct quic_read_again_args *args = arg;
1777 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1778 /* If connection is torn down due to an error while blocking, stop. */
1779 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1783 if (!quic_read_actual(args->qc, args->stream,
1784 args->buf, args->len, args->bytes_read,
1788 if (*args->bytes_read > 0)
1789 /* got at least one byte, the SSL_read op can finish now */
1792 return 0; /* did not read anything, keep trying */
1796 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1800 struct quic_read_again_args args;
1804 if (!expect_quic(s, &ctx))
1809 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1810 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1814 /* If we haven't finished the handshake, try to advance it. */
1815 if (quic_do_handshake(ctx.qc) < 1) {
1816 ret = 0; /* ossl_quic_do_handshake raised error here */
1820 if (ctx.xso == NULL) {
1822 * Called on a QCSO and we don't currently have a default stream.
1824 * Wait until we get a stream initiated by the peer (blocking mode) or
1825 * fail if we don't have one yet (non-blocking mode).
1827 if (!qc_wait_for_default_xso_for_read(ctx.qc)) {
1828 ret = 0; /* error already raised here */
1832 ctx.xso = ctx.qc->default_xso;
1835 if (ctx.xso->stream == NULL) {
1836 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1840 if (!quic_read_actual(ctx.qc, ctx.xso->stream, buf, len, bytes_read, peek)) {
1841 ret = 0; /* quic_read_actual raised error here */
1845 if (*bytes_read > 0) {
1847 * Even though we succeeded, tick the reactor here to ensure we are
1848 * handling other aspects of the QUIC connection.
1850 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1852 } else if (xso_blocking_mode(ctx.xso)) {
1854 * We were not able to read anything immediately, so our stream
1855 * buffer is empty. This means we need to block until we get
1856 * at least one byte.
1859 args.stream = ctx.xso->stream;
1862 args.bytes_read = bytes_read;
1865 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
1867 ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
1869 } else if (res < 0) {
1870 ret = 0; /* quic_read_again raised error here */
1876 /* We did not get any bytes and are not in blocking mode. */
1877 ret = QUIC_RAISE_NORMAL_ERROR(ctx.qc, SSL_ERROR_WANT_READ);
1881 quic_unlock(ctx.qc);
1885 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
1887 return quic_read(s, buf, len, bytes_read, 0);
1890 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
1892 return quic_read(s, buf, len, bytes_read, 1);
1900 static size_t ossl_quic_pending_int(const SSL *s)
1906 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
1909 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
1910 /* Cannot raise errors here because we are const, just fail. */
1913 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
1917 quic_unlock(ctx.qc);
1921 size_t ossl_quic_pending(const SSL *s)
1923 return ossl_quic_pending_int(s);
1926 int ossl_quic_has_pending(const SSL *s)
1928 return ossl_quic_pending_int(s) > 0;
1932 * SSL_stream_conclude
1933 * -------------------
1936 int ossl_quic_conn_stream_conclude(SSL *s)
1941 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1944 qs = ctx.xso->stream;
1946 if (qs == NULL || qs->sstream == NULL) {
1947 quic_unlock(ctx.qc);
1951 if (!ossl_quic_channel_is_active(ctx.qc->ch)
1952 || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
1953 quic_unlock(ctx.qc);
1957 ossl_quic_sstream_fin(qs->sstream);
1958 quic_post_write(ctx.xso, 1, 1);
1959 quic_unlock(ctx.qc);
1964 * SSL_inject_net_dgram
1965 * --------------------
1968 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
1970 const BIO_ADDR *peer,
1971 const BIO_ADDR *local)
1977 if (!expect_quic(s, &ctx))
1982 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
1983 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
1985 quic_unlock(ctx.qc);
1990 * SSL_get0_connection
1991 * -------------------
1993 SSL *ossl_quic_get0_connection(SSL *s)
1997 if (!expect_quic(s, &ctx))
2000 return &ctx.qc->ssl;
2004 * SSL_get_stream_type
2005 * -------------------
2007 int ossl_quic_get_stream_type(SSL *s)
2011 if (!expect_quic(s, &ctx))
2012 return SSL_STREAM_TYPE_NONE;
2014 if (ctx.xso == NULL) {
2016 * If we are deferring XSO creation, assume single stream mode and
2017 * default to BIDI, as the deferred XSO which will be created will be
2020 if (!ctx.qc->default_xso_created)
2021 return SSL_STREAM_TYPE_BIDI;
2023 return SSL_STREAM_TYPE_NONE;
2026 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2027 return SSL_STREAM_TYPE_BIDI;
2029 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2030 return SSL_STREAM_TYPE_READ;
2032 return SSL_STREAM_TYPE_WRITE;
2040 uint64_t ossl_quic_get_stream_id(SSL *s)
2045 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
2048 id = ctx.xso->stream->id;
2049 quic_unlock(ctx.qc);
2055 * SSL_set_default_stream_mode
2056 * ---------------------------
2059 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2063 if (!expect_quic_conn_only(s, &ctx))
2068 if (ctx.qc->default_xso_created)
2069 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2070 "too late to change default stream mode");
2073 case SSL_DEFAULT_STREAM_MODE_NONE:
2074 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2075 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2076 ctx.qc->default_stream_mode = mode;
2079 quic_unlock(ctx.qc);
2080 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_PASSED_INVALID_ARGUMENT,
2081 "bad default stream type");
2084 quic_unlock(ctx.qc);
2093 SSL *ossl_quic_detach_stream(SSL *s)
2098 if (!expect_quic_conn_only(s, &ctx))
2103 /* Calling this function inhibits default XSO autocreation. */
2104 xso = ctx.qc->default_xso;
2105 qc_set_default_xso(ctx.qc, NULL, /*touch=*/1);
2107 quic_unlock(ctx.qc);
2117 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2121 if (!expect_quic_conn_only(conn, &ctx))
2124 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
2125 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_PASSED_NULL_PARAMETER,
2126 "stream to attach must be a valid QUIC stream");
2130 if (ctx.qc->default_xso != NULL) {
2131 quic_unlock(ctx.qc);
2132 return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
2133 "connection already has a default stream");
2136 /* Calling this function inhibits default XSO autocreation. */
2137 qc_set_default_xso(ctx.qc, (QUIC_XSO *)stream, /*touch=*/1);
2139 quic_unlock(ctx.qc);
2144 * SSL_set_incoming_stream_reject_policy
2145 * -------------------------------------
2148 static int qc_get_effective_incoming_stream_reject_policy(QUIC_CONNECTION *qc)
2150 switch (qc->incoming_stream_reject_policy) {
2151 case SSL_INCOMING_STREAM_REJECT_POLICY_AUTO:
2152 if ((qc->default_xso == NULL && !qc->default_xso_created)
2153 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2154 return SSL_INCOMING_STREAM_REJECT_POLICY_ACCEPT;
2156 return SSL_INCOMING_STREAM_REJECT_POLICY_REJECT;
2159 return qc->incoming_stream_reject_policy;
2164 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2166 int policy = qc_get_effective_incoming_stream_reject_policy(qc);
2167 int enable_reject = (policy == SSL_INCOMING_STREAM_REJECT_POLICY_REJECT);
2169 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2171 qc->incoming_stream_reject_aec);
2175 int ossl_quic_set_incoming_stream_reject_policy(SSL *s, int policy,
2181 if (!expect_quic_conn_only(s, &ctx))
2187 case SSL_INCOMING_STREAM_REJECT_POLICY_AUTO:
2188 case SSL_INCOMING_STREAM_REJECT_POLICY_ACCEPT:
2189 case SSL_INCOMING_STREAM_REJECT_POLICY_REJECT:
2190 ctx.qc->incoming_stream_reject_policy = policy;
2191 ctx.qc->incoming_stream_reject_aec = aec;
2199 qc_update_reject_policy(ctx.qc);
2200 quic_unlock(ctx.qc);
2208 struct wait_for_incoming_stream_args {
2209 QUIC_CONNECTION *qc;
2214 static int wait_for_incoming_stream(void *arg)
2216 struct wait_for_incoming_stream_args *args = arg;
2217 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(args->qc->ch);
2219 if (!ossl_quic_channel_is_active(args->qc->ch)) {
2220 /* If connection is torn down due to an error while blocking, stop. */
2221 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2225 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2226 if (args->qs != NULL)
2227 return 1; /* got a stream */
2229 return 0; /* did not get a stream, keep trying */
2233 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2238 QUIC_STREAM_MAP *qsm;
2241 OSSL_RTT_INFO rtt_info;
2243 if (!expect_quic_conn_only(s, &ctx))
2248 if (qc_get_effective_incoming_stream_reject_policy(ctx.qc)
2249 == SSL_INCOMING_STREAM_REJECT_POLICY_REJECT)
2252 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2254 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2256 if (qc_blocking_mode(ctx.qc)
2257 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2258 struct wait_for_incoming_stream_args args;
2263 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2265 QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL);
2267 } else if (ret < 0 || args.qs == NULL) {
2277 xso = create_xso_from_stream(ctx.qc, qs);
2281 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2282 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2283 rtt_info.smoothed_rtt);
2286 /* Calling this function inhibits default XSO autocreation. */
2287 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
2290 quic_unlock(ctx.qc);
2295 * SSL_get_accept_stream_queue_len
2296 * -------------------------------
2299 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2304 if (!expect_quic_conn_only(s, &ctx))
2309 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2311 quic_unlock(ctx.qc);
2319 int ossl_quic_stream_reset(SSL *ssl,
2320 const SSL_STREAM_RESET_ARGS *args,
2324 QUIC_STREAM_MAP *qsm;
2326 uint64_t error_code;
2328 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2331 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2332 qs = ctx.xso->stream;
2333 error_code = (args != NULL ? args->quic_error_code : 0);
2335 ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2337 quic_unlock(ctx.qc);
2342 * SSL_get_stream_read_state
2343 * -------------------------
2345 static void quic_classify_stream(QUIC_CONNECTION *qc,
2349 uint64_t *app_error_code)
2352 uint64_t final_size;
2354 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2356 if (app_error_code != NULL)
2357 *app_error_code = UINT64_MAX;
2359 app_error_code = &final_size; /* throw away value */
2361 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2363 * Unidirectional stream and this direction of transmission doesn't
2366 *state = SSL_STREAM_STATE_WRONG_DIR;
2367 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2368 /* Connection already closed. */
2369 *state = SSL_STREAM_STATE_CONN_CLOSED;
2370 } else if (!is_write && qs->recv_fin_retired) {
2371 /* Application has read a FIN. */
2372 *state = SSL_STREAM_STATE_FINISHED;
2373 } else if ((!is_write && qs->stop_sending)
2374 || (is_write && qs->reset_stream)) {
2376 * Stream has been reset locally. FIN takes precedence over this for the
2377 * read case as the application need not care if the stream is reset
2378 * after a FIN has been successfully processed.
2380 *state = SSL_STREAM_STATE_RESET_LOCAL;
2381 *app_error_code = !is_write
2382 ? qs->stop_sending_aec
2383 : qs->reset_stream_aec;
2384 } else if ((!is_write && qs->peer_reset_stream)
2385 || (is_write && qs->peer_stop_sending)) {
2387 * Stream has been reset remotely. */
2388 *state = SSL_STREAM_STATE_RESET_REMOTE;
2389 *app_error_code = !is_write
2390 ? qs->peer_reset_stream_aec
2391 : qs->peer_stop_sending_aec;
2392 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2395 * Stream has been finished. Stream reset takes precedence over this for
2396 * the write case as peer may not have received all data.
2398 *state = SSL_STREAM_STATE_FINISHED;
2400 /* Stream still healthy. */
2401 *state = SSL_STREAM_STATE_OK;
2405 static int quic_get_stream_state(SSL *ssl, int is_write)
2410 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2411 return SSL_STREAM_STATE_NONE;
2413 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2414 quic_unlock(ctx.qc);
2418 int ossl_quic_get_stream_read_state(SSL *ssl)
2420 return quic_get_stream_state(ssl, /*is_write=*/0);
2424 * SSL_get_stream_write_state
2425 * --------------------------
2427 int ossl_quic_get_stream_write_state(SSL *ssl)
2429 return quic_get_stream_state(ssl, /*is_write=*/1);
2433 * SSL_get_stream_read_error_code
2434 * ------------------------------
2436 static int quic_get_stream_error_code(SSL *ssl, int is_write,
2437 uint64_t *app_error_code)
2442 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2445 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2446 &state, app_error_code);
2448 quic_unlock(ctx.qc);
2450 case SSL_STREAM_STATE_FINISHED:
2452 case SSL_STREAM_STATE_RESET_LOCAL:
2453 case SSL_STREAM_STATE_RESET_REMOTE:
2460 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2462 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2466 * SSL_get_stream_write_error_code
2467 * -------------------------------
2469 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2471 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2475 * SSL_get_conn_close_info
2476 * -----------------------
2478 int ossl_quic_get_conn_close_info(SSL *ssl,
2479 SSL_CONN_CLOSE_INFO *info,
2483 const QUIC_TERMINATE_CAUSE *tc;
2485 if (!expect_quic_conn_only(ssl, &ctx))
2488 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
2492 info->error_code = tc->error_code;
2493 info->reason = NULL; /* TODO(QUIC): Wire reason */
2494 info->reason_len = 0;
2495 info->is_local = !tc->remote;
2496 info->is_transport = !tc->app;
2501 * QUIC Front-End I/O API: SSL_CTX Management
2502 * ==========================================
2505 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2509 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
2513 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
2515 return ssl3_callback_ctrl(s, cmd, fp);
2518 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
2520 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
2523 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
2525 /* We never do renegotiation. */
2530 * These functions define the TLSv1.2 (and below) ciphers that are supported by
2531 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
2534 int ossl_quic_num_ciphers(void)
2539 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)