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_dummy_handshake.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_CONNECTION *qc);
23 * QUIC Front-End I/O API: Common Utilities
24 * ========================================
28 * Block until a predicate is met.
30 * Precondition: Must have a channel.
32 static int block_until_pred(QUIC_CONNECTION *qc,
33 int (*pred)(void *arg), void *pred_arg,
38 assert(qc->ch != NULL);
40 rtor = ossl_quic_channel_get_reactor(qc->ch);
41 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags);
45 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
46 * rather than via ERR.
48 static int quic_raise_normal_error(QUIC_CONNECTION *qc,
56 * Raise a 'non-normal' error, meaning any error that is not reported via
57 * SSL_get_error() and must be reported via ERR.
59 static int quic_raise_non_normal_error(QUIC_CONNECTION *qc,
70 ERR_set_debug(file, line, func);
73 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
76 qc->last_error = SSL_ERROR_SSL;
80 #define QUIC_RAISE_NORMAL_ERROR(qc, err) \
81 quic_raise_normal_error((qc), (err))
83 #define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg) \
84 quic_raise_non_normal_error((qc), \
85 OPENSSL_FILE, OPENSSL_LINE, \
91 * Should be called at entry of every public function to confirm we have a valid
94 static ossl_inline int expect_quic_conn(const QUIC_CONNECTION *qc)
96 if (!ossl_assert(qc != NULL))
97 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
104 * QUIC Front-End I/O API: Initialization
105 * ======================================
107 * SSL_new => ossl_quic_new
109 * SSL_reset => ossl_quic_reset
110 * SSL_clear => ossl_quic_clear
112 * SSL_free => ossl_quic_free
117 SSL *ossl_quic_new(SSL_CTX *ctx)
119 QUIC_CONNECTION *qc = NULL;
120 SSL *ssl_base = NULL;
122 qc = OPENSSL_zalloc(sizeof(*qc));
126 /* Initialise the QUIC_CONNECTION's stub header. */
128 if (!ossl_ssl_init(ssl_base, ctx, SSL_TYPE_QUIC_CONNECTION)) {
133 /* Channel is not created yet. */
134 qc->ssl_mode = qc->ssl.ctx->mode;
135 qc->last_error = SSL_ERROR_NONE;
145 void ossl_quic_free(SSL *s)
147 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
149 /* We should never be called on anything but a QUIC_CONNECTION. */
150 if (!expect_quic_conn(qc))
153 ossl_quic_channel_free(qc->ch);
155 BIO_free(qc->net_rbio);
156 BIO_free(qc->net_wbio);
158 /* Note: SSL_free calls OPENSSL_free(qc) for us */
161 /* SSL method init */
162 int ossl_quic_init(SSL *s)
164 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
166 if (!expect_quic_conn(qc))
169 /* Same op as SSL_clear, forward the call. */
170 return ossl_quic_clear(s);
173 /* SSL method deinit */
174 void ossl_quic_deinit(SSL *s)
180 int ossl_quic_reset(SSL *s)
182 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
184 if (!expect_quic_conn(qc))
187 /* TODO(QUIC); Currently a no-op. */
192 int ossl_quic_clear(SSL *s)
194 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
196 if (!expect_quic_conn(qc))
199 /* TODO(QUIC): Currently a no-op. */
204 * QUIC Front-End I/O API: Network BIO Configuration
205 * =================================================
207 * Handling the different BIOs is difficult:
209 * - It is more or less a requirement that we use non-blocking network I/O;
210 * we need to be able to have timeouts on recv() calls, and make best effort
211 * (non blocking) send() and recv() calls.
213 * The only sensible way to do this is to configure the socket into
214 * non-blocking mode. We could try to do select() before calling send() or
215 * recv() to get a guarantee that the call will not block, but this will
216 * probably run into issues with buggy OSes which generate spurious socket
217 * readiness events. In any case, relying on this to work reliably does not
220 * Timeouts could be handled via setsockopt() socket timeout options, but
221 * this depends on OS support and adds another syscall to every network I/O
222 * operation. It also has obvious thread safety concerns if we want to move
223 * to concurrent use of a single socket at some later date.
225 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
226 * be made non-blocking. However some OSes (e.g. Windows) do not support
227 * this, so we cannot rely on this.
229 * As such, we need to configure any FD in non-blocking mode. This may
230 * confound users who pass a blocking socket to libssl. However, in practice
231 * it would be extremely strange for a user of QUIC to pass an FD to us,
232 * then also try and send receive traffic on the same socket(!). Thus the
233 * impact of this should be limited, and can be documented.
235 * - We support both blocking and non-blocking operation in terms of the API
236 * presented to the user. One prospect is to set the blocking mode based on
237 * whether the socket passed to us was already in blocking mode. However,
238 * Windows has no API for determining if a socket is in blocking mode (!),
239 * therefore this cannot be done portably. Currently therefore we expose an
240 * explicit API call to set this, and default to blocking mode.
242 * - We need to determine our initial destination UDP address. The "natural"
243 * way for a user to do this is to set the peer variable on a BIO_dgram.
244 * However, this has problems because BIO_dgram's peer variable is used for
245 * both transmission and reception. This means it can be constantly being
246 * changed to a malicious value (e.g. if some random unrelated entity on the
247 * network starts sending traffic to us) on every read call. This is not a
248 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
249 * calls only, which do not use this variable. However, we do need to let
250 * the user specify the peer in a 'normal' manner. The compromise here is
251 * that we grab the current peer value set at the time the write BIO is set
252 * and do not read the value again.
254 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
255 * Currently we do this by only supporting non-blocking mode.
260 * Determines what initial destination UDP address we should use, if possible.
261 * If this fails the client must set the destination address manually, or use a
262 * BIO which does not need a destination address.
264 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
266 if (!BIO_dgram_get_peer(net_wbio, peer))
272 void ossl_quic_conn_set0_net_rbio(QUIC_CONNECTION *qc, BIO *net_rbio)
274 if (qc->net_rbio == net_rbio)
277 if (qc->ch != NULL && !ossl_quic_channel_set_net_rbio(qc->ch, net_rbio))
280 BIO_free(qc->net_rbio);
281 qc->net_rbio = net_rbio;
284 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
285 * mode as we do not support it for non-pollable BIOs.
287 if (net_rbio != NULL) {
288 BIO_POLL_DESCRIPTOR d = {0};
290 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
291 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
293 qc->can_poll_net_rbio = 0;
295 qc->can_poll_net_rbio = 1;
300 void ossl_quic_conn_set0_net_wbio(QUIC_CONNECTION *qc, BIO *net_wbio)
302 if (qc->net_wbio == net_wbio)
305 if (qc->ch != NULL && !ossl_quic_channel_set_net_wbio(qc->ch, net_wbio))
308 BIO_free(qc->net_wbio);
309 qc->net_wbio = net_wbio;
311 if (net_wbio != NULL) {
312 BIO_POLL_DESCRIPTOR d = {0};
314 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
315 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
317 qc->can_poll_net_wbio = 0;
319 qc->can_poll_net_wbio = 1;
323 * If we do not have a peer address yet, and we have not started trying
324 * to connect yet, try to autodetect one.
326 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC
328 if (!csm_analyse_init_peer_addr(net_wbio, &qc->init_peer_addr))
330 BIO_ADDR_clear(&qc->init_peer_addr);
333 ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
338 BIO *ossl_quic_conn_get_net_rbio(const QUIC_CONNECTION *qc)
343 BIO *ossl_quic_conn_get_net_wbio(const QUIC_CONNECTION *qc)
348 int ossl_quic_conn_get_blocking_mode(const QUIC_CONNECTION *qc)
353 int ossl_quic_conn_set_blocking_mode(QUIC_CONNECTION *qc, int blocking)
355 /* Cannot enable blocking mode if we do not have pollable FDs. */
357 (!qc->can_poll_net_rbio || !qc->can_poll_net_wbio))
358 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_UNSUPPORTED, NULL);
360 qc->blocking = (blocking != 0);
364 int ossl_quic_conn_set_initial_peer_addr(QUIC_CONNECTION *qc,
365 const BIO_ADDR *peer_addr)
368 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
371 if (peer_addr == NULL) {
372 BIO_ADDR_clear(&qc->init_peer_addr);
376 qc->init_peer_addr = *peer_addr;
381 * QUIC Front-End I/O API: Asynchronous I/O Management
382 * ===================================================
384 * (BIO/)SSL_tick => ossl_quic_tick
385 * (BIO/)SSL_get_tick_timeout => ossl_quic_get_tick_timeout
386 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
390 /* Returns 1 if the connection is being used in blocking mode. */
391 static int blocking_mode(const QUIC_CONNECTION *qc)
396 /* SSL_tick; ticks the reactor. */
397 int ossl_quic_tick(QUIC_CONNECTION *qc)
402 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
407 * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object
408 * should be ticked by the application by calling SSL_tick(). tv is set to 0 if
409 * the object should be ticked immediately and tv->tv_sec is set to -1 if no
410 * timeout is currently active.
412 int ossl_quic_get_tick_timeout(QUIC_CONNECTION *qc, struct timeval *tv)
414 OSSL_TIME deadline = ossl_time_infinite();
418 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(qc->ch));
420 if (ossl_time_is_infinite(deadline)) {
426 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now()));
430 /* SSL_get_rpoll_descriptor */
431 int ossl_quic_get_rpoll_descriptor(QUIC_CONNECTION *qc, BIO_POLL_DESCRIPTOR *desc)
433 if (desc == NULL || qc->net_rbio == NULL)
436 return BIO_get_rpoll_descriptor(qc->net_rbio, desc);
439 /* SSL_get_wpoll_descriptor */
440 int ossl_quic_get_wpoll_descriptor(QUIC_CONNECTION *qc, BIO_POLL_DESCRIPTOR *desc)
442 if (desc == NULL || qc->net_wbio == NULL)
445 return BIO_get_wpoll_descriptor(qc->net_wbio, desc);
448 /* SSL_want_net_read */
449 int ossl_quic_get_want_net_read(QUIC_CONNECTION *qc)
454 return ossl_quic_reactor_want_net_read(ossl_quic_channel_get_reactor(qc->ch));
457 /* SSL_want_net_write */
458 int ossl_quic_get_want_net_write(QUIC_CONNECTION *qc)
463 return ossl_quic_reactor_want_net_write(ossl_quic_channel_get_reactor(qc->ch));
467 * QUIC Front-End I/O API: Connection Lifecycle Operations
468 * =======================================================
470 * SSL_do_handshake => ossl_quic_do_handshake
471 * SSL_set_connect_state => ossl_quic_set_connect_state
472 * SSL_set_accept_state => ossl_quic_set_accept_state
473 * SSL_shutdown => ossl_quic_shutdown
474 * SSL_ctrl => ossl_quic_ctrl
475 * (BIO/)SSL_connect => ossl_quic_connect
476 * (BIO/)SSL_accept => ossl_quic_accept
481 int ossl_quic_shutdown(SSL *s)
483 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
485 if (!expect_quic_conn(qc))
489 ossl_quic_channel_local_close(qc->ch);
495 static void fixup_mode_change(QUIC_CONNECTION *qc)
497 /* If enabling EPW mode, cancel any AON write */
498 if ((qc->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)
499 aon_write_finish(qc);
502 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
504 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
506 if (!expect_quic_conn(qc))
511 qc->ssl_mode |= (uint32_t)larg;
512 fixup_mode_change(qc);
514 case SSL_CTRL_CLEAR_MODE:
515 qc->ssl_mode &= ~(uint32_t)larg;
516 fixup_mode_change(qc);
523 /* SSL_set_connect_state */
524 void ossl_quic_set_connect_state(QUIC_CONNECTION *qc)
526 /* Cannot be changed after handshake started */
533 /* SSL_set_accept_state */
534 void ossl_quic_set_accept_state(QUIC_CONNECTION *qc)
536 /* Cannot be changed after handshake started */
543 /* SSL_do_handshake */
544 struct quic_handshake_wait_args {
548 static int quic_handshake_wait(void *arg)
550 struct quic_handshake_wait_args *args = arg;
552 if (!ossl_quic_channel_is_active(args->qc->ch))
555 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
561 static int configure_channel(QUIC_CONNECTION *qc)
563 assert(qc->ch != NULL);
565 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
566 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
567 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
574 * Creates a channel and configures it with the information we have accumulated
575 * via calls made to us from the application prior to starting a handshake
578 static int ensure_channel_and_start(QUIC_CONNECTION *qc)
580 QUIC_CHANNEL_ARGS args = {0};
585 args.libctx = qc->ssl.ctx->libctx;
586 args.propq = qc->ssl.ctx->propq;
589 qc->ch = ossl_quic_channel_new(&args);
593 if (!configure_channel(qc)
594 || !ossl_quic_channel_start(qc->ch)) {
595 ossl_quic_channel_free(qc->ch);
600 qc->stream0 = ossl_quic_channel_get_stream_by_id(qc->ch, 0);
601 if (qc->stream0 == NULL) {
602 ossl_quic_channel_free(qc->ch);
611 int ossl_quic_do_handshake(QUIC_CONNECTION *qc)
615 if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
616 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
618 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC)
619 /* Peer address must have been set. */
620 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
623 /* TODO(QUIC): Server mode not currently supported */
624 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
626 if (qc->net_rbio == NULL || qc->net_wbio == NULL)
627 /* Need read and write BIOs. */
628 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
631 * Start connection process. Note we may come here multiple times in
632 * non-blocking mode, which is fine.
634 if (!ensure_channel_and_start(qc))
635 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
637 if (ossl_quic_channel_is_handshake_complete(qc->ch))
638 /* The handshake is now done. */
641 if (blocking_mode(qc)) {
642 /* In blocking mode, wait for the handshake to complete. */
643 struct quic_handshake_wait_args args;
647 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
648 if (!ossl_quic_channel_is_active(qc->ch))
649 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
651 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
653 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
656 /* Otherwise, indicate that the handshake isn't done yet. */
657 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
662 int ossl_quic_connect(SSL *s)
664 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
666 if (!expect_quic_conn(qc))
669 /* Ensure we are in connect state (no-op if non-idle). */
670 ossl_quic_set_connect_state(qc);
672 /* Begin or continue the handshake */
673 return ossl_quic_do_handshake(qc);
677 int ossl_quic_accept(SSL *s)
679 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
681 if (!expect_quic_conn(qc))
684 /* Ensure we are in accept state (no-op if non-idle). */
685 ossl_quic_set_accept_state(qc);
687 /* Begin or continue the handshake */
688 return ossl_quic_do_handshake(qc);
692 * QUIC Front-End I/O API: Steady-State Operations
693 * ===============================================
695 * Here we dispatch calls to the steady-state front-end I/O API functions; that
696 * is, the functions used during the established phase of a QUIC connection
697 * (e.g. SSL_read, SSL_write).
699 * Each function must handle both blocking and non-blocking modes. As discussed
700 * above, all QUIC I/O is implemented using non-blocking mode internally.
702 * SSL_get_error => partially implemented by ossl_quic_get_error
703 * (BIO/)SSL_read => ossl_quic_read
704 * (BIO/)SSL_write => ossl_quic_write
705 * SSL_pending => ossl_quic_pending
709 int ossl_quic_get_error(const QUIC_CONNECTION *qc, int i)
711 return qc->last_error;
718 * The set of functions below provide the implementation of the public SSL_write
719 * function. We must handle:
721 * - both blocking and non-blocking operation at the application level,
722 * depending on how we are configured;
724 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
726 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
729 static void quic_post_write(QUIC_CONNECTION *qc, int did_append, int do_tick)
732 * We have appended at least one byte to the stream.
733 * Potentially mark stream as active, depending on FC.
736 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
742 * TODO(QUIC): It is probably inefficient to try and do this immediately,
743 * plus we should eventually consider Nagle's algorithm.
746 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
749 struct quic_write_again_args {
751 const unsigned char *buf;
753 size_t total_written;
756 static int quic_write_again(void *arg)
758 struct quic_write_again_args *args = arg;
759 size_t actual_written = 0;
761 if (!ossl_quic_channel_is_active(args->qc->ch))
762 /* If connection is torn down due to an error while blocking, stop. */
765 if (!ossl_quic_sstream_append(args->qc->stream0->sstream,
766 args->buf, args->len, &actual_written))
769 quic_post_write(args->qc, actual_written > 0, 0);
771 args->buf += actual_written;
772 args->len -= actual_written;
773 args->total_written += actual_written;
775 if (actual_written == 0)
776 /* Written everything, done. */
779 /* Not written everything yet, keep trying. */
783 static int quic_write_blocking(QUIC_CONNECTION *qc, const void *buf, size_t len,
787 struct quic_write_again_args args;
788 size_t actual_written = 0;
790 /* First make a best effort to append as much of the data as possible. */
791 if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len,
793 /* Stream already finished or allocation error. */
795 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
798 quic_post_write(qc, actual_written > 0, 1);
800 if (actual_written == len) {
801 /* Managed to append everything on the first try. */
802 *written = actual_written;
807 * We did not manage to append all of the data immediately, so the stream
808 * buffer has probably filled up. This means we need to block until some of
812 args.buf = (const unsigned char *)buf + actual_written;
813 args.len = len - actual_written;
814 args.total_written = 0;
816 res = block_until_pred(qc, quic_write_again, &args, 0);
818 if (!ossl_quic_channel_is_active(qc->ch))
819 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
821 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
824 *written = args.total_written;
828 static void aon_write_begin(QUIC_CONNECTION *qc, const unsigned char *buf,
829 size_t buf_len, size_t already_sent)
831 assert(!qc->aon_write_in_progress);
833 qc->aon_write_in_progress = 1;
834 qc->aon_buf_base = buf;
835 qc->aon_buf_pos = already_sent;
836 qc->aon_buf_len = buf_len;
839 static void aon_write_finish(QUIC_CONNECTION *qc)
841 qc->aon_write_in_progress = 0;
842 qc->aon_buf_base = NULL;
847 static int quic_write_nonblocking_aon(QUIC_CONNECTION *qc, const void *buf,
848 size_t len, size_t *written)
850 const void *actual_buf;
851 size_t actual_len, actual_written = 0;
852 int accept_moving_buffer
853 = ((qc->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
855 if (qc->aon_write_in_progress) {
857 * We are in the middle of an AON write (i.e., a previous write did not
858 * manage to append all data to the SSTREAM and we have EPW mode
861 if ((!accept_moving_buffer && qc->aon_buf_base != buf)
862 || len != qc->aon_buf_len)
864 * Pointer must not have changed if we are not in accept moving
865 * buffer mode. Length must never change.
867 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BAD_WRITE_RETRY, NULL);
869 actual_buf = (unsigned char *)buf + qc->aon_buf_pos;
870 actual_len = len - qc->aon_buf_pos;
871 assert(actual_len > 0);
877 /* First make a best effort to append as much of the data as possible. */
878 if (!ossl_quic_sstream_append(qc->stream0->sstream, actual_buf, actual_len,
880 /* Stream already finished or allocation error. */
882 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
885 quic_post_write(qc, actual_written > 0, 1);
887 if (actual_written == actual_len) {
888 /* We have sent everything. */
889 if (qc->aon_write_in_progress) {
891 * We have sent everything, and we were in the middle of an AON
892 * write. The output write length is the total length of the AON
893 * buffer, not however many bytes we managed to write to the stream
896 *written = qc->aon_buf_len;
897 aon_write_finish(qc);
899 *written = actual_written;
905 if (qc->aon_write_in_progress) {
907 * AON write is in progress but we have not written everything yet. We
908 * may have managed to send zero bytes, or some number of bytes less
909 * than the total remaining which need to be appended during this
912 qc->aon_buf_pos += actual_written;
913 assert(qc->aon_buf_pos < qc->aon_buf_len);
914 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE);
918 * Not in an existing AON operation but partial write is not enabled, so we
919 * need to begin a new AON operation. However we needn't bother if we didn't
920 * actually append anything.
922 if (actual_written > 0)
923 aon_write_begin(qc, buf, len, actual_written);
926 * AON - We do not publicly admit to having appended anything until AON
930 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE);
933 static int quic_write_nonblocking_epw(QUIC_CONNECTION *qc, const void *buf, size_t len,
936 /* Simple best effort operation. */
937 if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len, written)) {
938 /* Stream already finished or allocation error. */
940 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
943 quic_post_write(qc, *written > 0, 1);
947 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
949 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
950 int partial_write = ((qc->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
954 if (!expect_quic_conn(qc))
957 /* If we haven't started the handshake, do so automatically. */
958 if (!qc->started && !ossl_quic_do_handshake(qc))
961 if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
962 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
964 if (qc->stream0 == NULL || qc->stream0->sstream == NULL)
965 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
967 if (blocking_mode(qc))
968 return quic_write_blocking(qc, buf, len, written);
969 else if (partial_write)
970 return quic_write_nonblocking_epw(qc, buf, len, written);
972 return quic_write_nonblocking_aon(qc, buf, len, written);
979 struct quic_read_again_args {
988 static int quic_read_actual(QUIC_CONNECTION *qc,
990 void *buf, size_t buf_len,
996 if (stream->rstream == NULL)
997 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1000 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1001 bytes_read, &is_fin))
1002 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1005 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1006 bytes_read, &is_fin))
1007 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1011 if (*bytes_read > 0) {
1013 * We have read at least one byte from the stream. Inform stream-level
1014 * RXFC of the retirement of controlled bytes. Update the active stream
1015 * status (the RXFC may now want to emit a frame granting more credit to
1018 OSSL_RTT_INFO rtt_info;
1020 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1022 if (!ossl_quic_rxfc_on_retire(&qc->stream0->rxfc, *bytes_read,
1023 rtt_info.smoothed_rtt))
1024 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1028 stream->recv_fin_retired = 1;
1030 if (*bytes_read > 0)
1031 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1038 static int quic_read_again(void *arg)
1040 struct quic_read_again_args *args = arg;
1042 if (!ossl_quic_channel_is_active(args->qc->ch))
1043 /* If connection is torn down due to an error while blocking, stop. */
1046 if (!quic_read_actual(args->qc, args->stream,
1047 args->buf, args->len, args->bytes_read,
1051 if (*args->bytes_read > 0)
1052 /* got at least one byte, the SSL_read op can finish now */
1055 return 0; /* did not write anything, keep trying */
1058 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
1061 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1062 struct quic_read_again_args args;
1066 if (!expect_quic_conn(qc))
1069 if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
1070 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1072 /* If we haven't started the handshake, do so automatically. */
1073 if (!qc->started && !ossl_quic_do_handshake(qc))
1076 if (qc->stream0 == NULL)
1077 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1079 if (!quic_read_actual(qc, qc->stream0, buf, len, bytes_read, peek))
1082 if (*bytes_read > 0) {
1084 * Even though we succeeded, tick the reactor here to ensure we are
1085 * handling other aspects of the QUIC connection.
1087 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
1089 } else if (blocking_mode(qc)) {
1091 * We were not able to read anything immediately, so our stream
1092 * buffer is empty. This means we need to block until we get
1093 * at least one byte.
1096 args.stream = qc->stream0;
1099 args.bytes_read = bytes_read;
1102 res = block_until_pred(qc, quic_read_again, &args, 0);
1104 if (!ossl_quic_channel_is_active(qc->ch))
1105 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1107 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1114 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
1116 return quic_read(s, buf, len, bytes_read, 0);
1119 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
1121 return quic_read(s, buf, len, bytes_read, 1);
1128 size_t ossl_quic_pending(const SSL *s)
1130 const QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_CONST_SSL(s);
1134 if (!expect_quic_conn(qc))
1137 if (qc->stream0 == NULL || qc->stream0->rstream == NULL)
1138 /* Cannot raise errors here because we are const, just fail. */
1141 if (!ossl_quic_rstream_available(qc->stream0->rstream, &avail, &fin))
1148 * QUIC Front-End I/O API: SSL_CTX Management
1149 * ==========================================
1152 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1156 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
1160 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
1162 return ssl3_callback_ctrl(s, cmd, fp);
1165 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
1167 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
1170 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
1172 /* We never do renegotiation. */
1177 * This is the subset of TLS1.3 ciphers which can be used with QUIC and which we
1180 static SSL_CIPHER tls13_quic_ciphers[] = {
1183 TLS1_3_RFC_AES_128_GCM_SHA256,
1184 TLS1_3_RFC_AES_128_GCM_SHA256,
1185 TLS1_3_CK_AES_128_GCM_SHA256,
1190 TLS1_3_VERSION, TLS1_3_VERSION,
1193 SSL_HANDSHAKE_MAC_SHA256,
1198 TLS1_3_RFC_AES_256_GCM_SHA384,
1199 TLS1_3_RFC_AES_256_GCM_SHA384,
1200 TLS1_3_CK_AES_256_GCM_SHA384,
1205 TLS1_3_VERSION, TLS1_3_VERSION,
1208 SSL_HANDSHAKE_MAC_SHA384,
1214 TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
1215 TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
1216 TLS1_3_CK_CHACHA20_POLY1305_SHA256,
1219 SSL_CHACHA20POLY1305,
1221 TLS1_3_VERSION, TLS1_3_VERSION,
1224 SSL_HANDSHAKE_MAC_SHA256,
1230 int ossl_quic_num_ciphers(void)
1232 return OSSL_NELEM(tls13_quic_ciphers);
1235 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
1237 if (u >= OSSL_NELEM(tls13_quic_ciphers))
1240 return &tls13_quic_ciphers[u];