]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/quic/quic_impl.c
QUIC: Implement SSL_rstate_string(_long)
[thirdparty/openssl.git] / ssl / quic / quic_impl.c
CommitLineData
99e1cc7b
TM
1/*
2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <openssl/macros.h>
11#include <openssl/objects.h>
22d53c88
HL
12#include <openssl/sslerr.h>
13#include <crypto/rand.h>
99e1cc7b 14#include "quic_local.h"
2723d705 15#include "internal/quic_tls.h"
22d53c88
HL
16#include "internal/quic_rx_depack.h"
17#include "internal/quic_error.h"
18#include "internal/time.h"
99e1cc7b 19
faa3a180
HL
20typedef struct qctx_st QCTX;
21
cb5c208b 22static void aon_write_finish(QUIC_XSO *xso);
23c04709 23static int create_channel(QUIC_CONNECTION *qc);
21c80696 24static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
faa3a180
HL
25static int qc_try_create_default_xso_for_write(QCTX *ctx);
26static int qc_wait_for_default_xso_for_read(QCTX *ctx);
8b7be3aa
HL
27static void quic_lock(QUIC_CONNECTION *qc);
28static void quic_unlock(QUIC_CONNECTION *qc);
faa3a180 29static int quic_do_handshake(QCTX *ctx);
995ff282
HL
30static void qc_update_reject_policy(QUIC_CONNECTION *qc);
31static void qc_touch_default_xso(QUIC_CONNECTION *qc);
32static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
9cab4bd5
HL
33static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
34 int touch, QUIC_XSO **old_xso);
faa3a180 35static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
22d53c88
HL
36
37/*
38 * QUIC Front-End I/O API: Common Utilities
39 * ========================================
40 */
41
42/*
43 * Block until a predicate is met.
44 *
45 * Precondition: Must have a channel.
d7b1fadd 46 * Precondition: Must hold channel lock (unchecked).
22d53c88 47 */
d7b1fadd 48QUIC_NEEDS_LOCK
22d53c88
HL
49static int block_until_pred(QUIC_CONNECTION *qc,
50 int (*pred)(void *arg), void *pred_arg,
51 uint32_t flags)
52{
53 QUIC_REACTOR *rtor;
54
55 assert(qc->ch != NULL);
56
57 rtor = ossl_quic_channel_get_reactor(qc->ch);
c019e1ef 58 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
4847599b 59 qc->mutex);
22d53c88
HL
60}
61
e3e9794a
HL
62static OSSL_TIME get_time(QUIC_CONNECTION *qc)
63{
64 if (qc->override_now_cb != NULL)
65 return qc->override_now_cb(qc->override_now_cb_arg);
66 else
67 return ossl_time_now();
68}
69
70static OSSL_TIME get_time_cb(void *arg)
71{
72 QUIC_CONNECTION *qc = arg;
73
74 return get_time(qc);
75}
76
faa3a180
HL
77/*
78 * QCTX is a utility structure which provides information we commonly wish to
79 * unwrap upon an API call being dispatched to us, namely:
80 *
81 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
82 * was passed);
83 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
84 * a QCSO with a default stream was passed);
85 * - whether a QSSO was passed (xso == NULL must not be used to determine this
86 * because it may be non-NULL when a QCSO is passed if that QCSO has a
87 * default stream).
88 */
89struct qctx_st {
90 QUIC_CONNECTION *qc;
91 QUIC_XSO *xso;
92 int is_stream;
93};
94
22d53c88
HL
95/*
96 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
faa3a180
HL
97 * rather than via ERR. Note that normal errors must always be raised while
98 * holding a lock.
22d53c88 99 */
faa3a180
HL
100QUIC_NEEDS_LOCK
101static int quic_raise_normal_error(QCTX *ctx,
22d53c88
HL
102 int err)
103{
faa3a180
HL
104 if (ctx->is_stream)
105 ctx->xso->last_error = err;
106 else
107 ctx->qc->last_error = err;
108
22d53c88
HL
109 return 0;
110}
111
112/*
113 * Raise a 'non-normal' error, meaning any error that is not reported via
114 * SSL_get_error() and must be reported via ERR.
e88cdb8e
HL
115 *
116 * qc should be provided if available. In exceptional circumstances when qc is
117 * not known NULL may be passed. This should generally only happen when an
118 * expect_...() function defined below fails, which generally indicates a
119 * dispatch error or caller error.
faa3a180
HL
120 *
121 * ctx should be NULL if the connection lock is not held.
22d53c88 122 */
faa3a180 123static int quic_raise_non_normal_error(QCTX *ctx,
22d53c88
HL
124 const char *file,
125 int line,
126 const char *func,
127 int reason,
128 const char *fmt,
129 ...)
130{
131 va_list args;
132
133 ERR_new();
134 ERR_set_debug(file, line, func);
135
136 va_start(args, fmt);
137 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
138 va_end(args);
139
faa3a180
HL
140 if (ctx != NULL) {
141 if (ctx->is_stream && ctx->xso != NULL)
142 ctx->xso->last_error = SSL_ERROR_SSL;
143 else if (!ctx->is_stream && ctx->qc != NULL)
144 ctx->qc->last_error = SSL_ERROR_SSL;
145 }
e88cdb8e 146
22d53c88
HL
147 return 0;
148}
149
faa3a180
HL
150#define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
151 quic_raise_normal_error((ctx), (err))
22d53c88 152
faa3a180
HL
153#define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
154 quic_raise_non_normal_error((ctx), \
22d53c88
HL
155 OPENSSL_FILE, OPENSSL_LINE, \
156 OPENSSL_FUNC, \
157 (reason), \
158 (msg))
159
e88cdb8e
HL
160/*
161 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
162 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
163 *
164 * After this returns 1, all fields of the passed QCTX are initialised.
165 * Returns 0 on failure. This function is intended to be used to provide API
166 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
22d53c88 167 */
e88cdb8e 168static int expect_quic(const SSL *s, QCTX *ctx)
22d53c88 169{
e88cdb8e
HL
170 QUIC_CONNECTION *qc;
171 QUIC_XSO *xso;
172
173 ctx->qc = NULL;
174 ctx->xso = NULL;
175 ctx->is_stream = 0;
176
177 if (s == NULL)
8b7be3aa 178 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
22d53c88 179
e88cdb8e
HL
180 switch (s->type) {
181 case SSL_TYPE_QUIC_CONNECTION:
182 qc = (QUIC_CONNECTION *)s;
183 ctx->qc = qc;
cb5c208b 184 ctx->xso = qc->default_xso;
e88cdb8e
HL
185 ctx->is_stream = 0;
186 return 1;
187
188 case SSL_TYPE_QUIC_XSO:
189 xso = (QUIC_XSO *)s;
cb5c208b 190 ctx->qc = xso->conn;
e88cdb8e
HL
191 ctx->xso = xso;
192 ctx->is_stream = 1;
193 return 1;
194
195 default:
196 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
197 }
198}
199
200/*
201 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
202 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
203 * stream.
21c80696
HL
204 *
205 * remote_init determines if we expect the default XSO to be remotely created or
206 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
8b7be3aa
HL
207 *
208 * Channel mutex is acquired and retained on success.
e88cdb8e 209 */
8b7be3aa
HL
210QUIC_ACQUIRES_LOCK
211static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
212 QCTX *ctx)
e88cdb8e
HL
213{
214 if (!expect_quic(s, ctx))
215 return 0;
216
8b7be3aa
HL
217 quic_lock(ctx->qc);
218
21c80696 219 if (ctx->xso == NULL && remote_init >= 0) {
8b7be3aa 220 if (ossl_quic_channel_is_term_any(ctx->qc->ch)) {
faa3a180 221 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
8b7be3aa
HL
222 goto err;
223 }
224
225 /* If we haven't finished the handshake, try to advance it. */
faa3a180 226 if (quic_do_handshake(ctx) < 1)
8b7be3aa
HL
227 /* ossl_quic_do_handshake raised error here */
228 goto err;
229
230 if (remote_init == 0) {
faa3a180 231 if (!qc_try_create_default_xso_for_write(ctx))
8b7be3aa
HL
232 goto err;
233 } else {
faa3a180 234 if (!qc_wait_for_default_xso_for_read(ctx))
8b7be3aa
HL
235 goto err;
236 }
237
21c80696
HL
238 ctx->xso = ctx->qc->default_xso;
239 }
240
8b7be3aa 241 if (ctx->xso == NULL) {
faa3a180 242 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
8b7be3aa
HL
243 goto err;
244 }
245
246 return 1; /* lock held */
e88cdb8e 247
8b7be3aa
HL
248err:
249 quic_unlock(ctx->qc);
250 return 0;
e88cdb8e 251}
22d53c88 252
e88cdb8e
HL
253/*
254 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
255 * be non-NULL if the QCSO has a default stream.
256 */
23c04709 257static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
e88cdb8e
HL
258{
259 if (!expect_quic(s, ctx))
260 return 0;
261
262 if (ctx->is_stream)
faa3a180 263 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
e88cdb8e
HL
264
265 return 1;
22d53c88
HL
266}
267
4847599b
HL
268/*
269 * Ensures that the channel mutex is held for a method which touches channel
270 * state.
271 *
272 * Precondition: Channel mutex is not held (unchecked)
273 */
20f45743 274static void quic_lock(QUIC_CONNECTION *qc)
4847599b 275{
629b408c 276#if defined(OPENSSL_THREADS)
ffce2946 277 ossl_crypto_mutex_lock(qc->mutex);
629b408c 278#endif
4847599b
HL
279}
280
281/* Precondition: Channel mutex is held (unchecked) */
282QUIC_NEEDS_LOCK
283static void quic_unlock(QUIC_CONNECTION *qc)
284{
629b408c 285#if defined(OPENSSL_THREADS)
ffce2946 286 ossl_crypto_mutex_unlock(qc->mutex);
629b408c 287#endif
4847599b
HL
288}
289
290
22d53c88
HL
291/*
292 * QUIC Front-End I/O API: Initialization
293 * ======================================
294 *
295 * SSL_new => ossl_quic_new
296 * ossl_quic_init
297 * SSL_reset => ossl_quic_reset
298 * SSL_clear => ossl_quic_clear
299 * ossl_quic_deinit
300 * SSL_free => ossl_quic_free
301 *
f0d9757c
HL
302 * SSL_set_options => ossl_quic_set_options
303 * SSL_get_options => ossl_quic_get_options
304 * SSL_clear_options => ossl_quic_clear_options
305 *
22d53c88
HL
306 */
307
308/* SSL_new */
38b051a1
TM
309SSL *ossl_quic_new(SSL_CTX *ctx)
310{
22d53c88
HL
311 QUIC_CONNECTION *qc = NULL;
312 SSL *ssl_base = NULL;
a7f41885 313 SSL_CONNECTION *sc = NULL;
38b051a1
TM
314
315 qc = OPENSSL_zalloc(sizeof(*qc));
316 if (qc == NULL)
317 goto err;
318
22d53c88
HL
319 /* Initialise the QUIC_CONNECTION's stub header. */
320 ssl_base = &qc->ssl;
a7f41885 321 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
22d53c88 322 ssl_base = NULL;
38b051a1
TM
323 goto err;
324 }
38b051a1 325
4e3a55fd 326 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
a7f41885
MC
327 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
328 goto err;
f0d9757c 329
43788fb3 330 /* override the user_ssl of the inner connection */
f082205b 331 sc->s3.flags |= TLS1_FLAGS_QUIC;
a7f41885 332
f0d9757c 333 /* Restrict options derived from the SSL_CTX. */
7163617f
HL
334 sc->options &= OSSL_QUIC_PERMITTED_OPTIONS;
335 sc->pha_enabled = 0;
f0d9757c 336
629b408c 337#if defined(OPENSSL_THREADS)
ffce2946 338 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL)
4847599b 339 goto err;
629b408c 340#endif
4847599b 341
629b408c 342#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
f2f7c4f1
HL
343 qc->is_thread_assisted
344 = (ssl_base->method == OSSL_QUIC_client_thread_method());
629b408c 345#endif
f2f7c4f1 346
dfb9ae14
HL
347 qc->as_server = 0; /* TODO(QUIC): server support */
348 qc->as_server_state = qc->as_server;
349
8b7be3aa 350 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
21c80696
HL
351 qc->default_ssl_mode = qc->ssl.ctx->mode;
352 qc->default_blocking = 1;
fb4a2bba 353 qc->blocking = 1;
83df44ae 354 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
21c80696 355 qc->last_error = SSL_ERROR_NONE;
a7f41885 356
23c04709
HL
357 if (!create_channel(qc))
358 goto err;
359
5cf99b40
MC
360 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
361 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
362
995ff282
HL
363 qc_update_reject_policy(qc);
364
21c80696
HL
365 /*
366 * We do not create the default XSO yet. The reason for this is that the
367 * stream ID of the default XSO will depend on whether the stream is client
368 * or server-initiated, which depends on who transmits first. Since we do
369 * not know whether the application will be using a client-transmits-first
370 * or server-transmits-first protocol, we defer default XSO creation until
371 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
372 * we take that as a cue that the client is expecting a server-initiated
373 * stream, and vice versa if SSL_write() is called first.
374 */
22d53c88
HL
375 return ssl_base;
376
38b051a1 377err:
2dbc39de 378 if (qc != NULL) {
629b408c
HL
379#if defined(OPENSSL_THREADS)
380 ossl_crypto_mutex_free(qc->mutex);
381#endif
2dbc39de
HL
382 ossl_quic_channel_free(qc->ch);
383 SSL_free(qc->tls);
384 }
22d53c88 385 OPENSSL_free(qc);
38b051a1
TM
386 return NULL;
387}
388
22d53c88 389/* SSL_free */
a8489257 390QUIC_TAKES_LOCK
22d53c88
HL
391void ossl_quic_free(SSL *s)
392{
072328dd 393 QCTX ctx;
9cab4bd5 394 int is_default;
22d53c88 395
072328dd
HL
396 /* We should never be called on anything but a QSO. */
397 if (!expect_quic(s, &ctx))
22d53c88
HL
398 return;
399
22b1a96f
HL
400 quic_lock(ctx.qc);
401
2dbc39de
HL
402 if (ctx.is_stream) {
403 /*
404 * When a QSSO is freed, the XSO is freed immediately, because the XSO
405 * itself only contains API personality layer data. However the
406 * underlying QUIC_STREAM is not freed immediately but is instead marked
407 * as deleted for later collection.
408 */
409
2dbc39de
HL
410 assert(ctx.qc->num_xso > 0);
411 --ctx.qc->num_xso;
412
9aaafc26
HL
413 /* If a stream's send part has not been finished, auto-reset it. */
414 if (ctx.xso->stream->sstream != NULL
415 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
416 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
417 ctx.xso->stream, 0);
2dbc39de 418
9aaafc26
HL
419 /* Do STOP_SENDING for the receive part, if applicable. */
420 if (ctx.xso->stream->rstream != NULL)
421 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
422 ctx.xso->stream, 0);
f20fdd16
HL
423
424 /* Update stream state. */
9aaafc26
HL
425 ctx.xso->stream->deleted = 1;
426 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
f20fdd16
HL
427 ctx.xso->stream);
428
9cab4bd5 429 is_default = (ctx.xso == ctx.qc->default_xso);
2dbc39de
HL
430 quic_unlock(ctx.qc);
431
9cab4bd5
HL
432 /*
433 * Unref the connection in most cases; the XSO has a ref to the QC and
434 * not vice versa. But for a default XSO, to avoid circular references,
435 * the QC refs the XSO but the XSO does not ref the QC. If we are the
436 * default XSO, we only get here when the QC is being torn down anyway,
437 * so don't call SSL_free(qc) as we are already in it.
438 */
439 if (!is_default)
440 SSL_free(&ctx.qc->ssl);
441
2dbc39de
HL
442 /* Note: SSL_free calls OPENSSL_free(xso) for us */
443 return;
444 }
445
2dbc39de
HL
446 /*
447 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
448 * stage, but is freed during the channel free when the whole QSM is freed.
449 */
21c80696
HL
450 if (ctx.qc->default_xso != NULL) {
451 QUIC_XSO *xso = ctx.qc->default_xso;
452
453 quic_unlock(ctx.qc);
454 SSL_free(&xso->ssl);
455 quic_lock(ctx.qc);
9cab4bd5 456 ctx.qc->default_xso = NULL;
21c80696 457 }
2dbc39de
HL
458
459 /* Ensure we have no remaining XSOs. */
460 assert(ctx.qc->num_xso == 0);
461
629b408c 462#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
072328dd
HL
463 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
464 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
465 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
dbe7b51a 466 }
629b408c 467#endif
f2f7c4f1 468
072328dd 469 ossl_quic_channel_free(ctx.qc->ch);
22d53c88 470
072328dd
HL
471 BIO_free(ctx.qc->net_rbio);
472 BIO_free(ctx.qc->net_wbio);
d1ac77b1 473
22d53c88 474 /* Note: SSL_free calls OPENSSL_free(qc) for us */
a7f41885 475
072328dd 476 SSL_free(ctx.qc->tls);
45b7c7e0 477 quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
629b408c 478#if defined(OPENSSL_THREADS)
45b7c7e0 479 ossl_crypto_mutex_free(&ctx.qc->mutex);
629b408c 480#endif
22d53c88
HL
481}
482
483/* SSL method init */
38b051a1 484int ossl_quic_init(SSL *s)
99e1cc7b 485{
22d53c88
HL
486 /* Same op as SSL_clear, forward the call. */
487 return ossl_quic_clear(s);
99e1cc7b
TM
488}
489
22d53c88 490/* SSL method deinit */
38b051a1
TM
491void ossl_quic_deinit(SSL *s)
492{
22d53c88 493 /* No-op. */
38b051a1
TM
494}
495
22d53c88
HL
496/* SSL_reset */
497int ossl_quic_reset(SSL *s)
498{
072328dd 499 QCTX ctx;
22d53c88 500
072328dd 501 if (!expect_quic(s, &ctx))
22d53c88
HL
502 return 0;
503
c8b3fdc2 504 /* TODO(QUIC); Currently a no-op. */
22d53c88
HL
505 return 1;
506}
507
508/* SSL_clear */
509int ossl_quic_clear(SSL *s)
99e1cc7b 510{
072328dd 511 QCTX ctx;
38b051a1 512
072328dd 513 if (!expect_quic(s, &ctx))
22d53c88
HL
514 return 0;
515
c8b3fdc2 516 /* TODO(QUIC): Currently a no-op. */
22d53c88
HL
517 return 1;
518}
38b051a1 519
e3e9794a
HL
520int ossl_quic_conn_set_override_now_cb(SSL *s,
521 OSSL_TIME (*now_cb)(void *arg),
522 void *now_cb_arg)
b212d554 523{
072328dd 524 QCTX ctx;
b212d554 525
072328dd 526 if (!expect_quic(s, &ctx))
e3e9794a
HL
527 return 0;
528
529 quic_lock(ctx.qc);
072328dd
HL
530
531 ctx.qc->override_now_cb = now_cb;
532 ctx.qc->override_now_cb_arg = now_cb_arg;
e3e9794a
HL
533
534 quic_unlock(ctx.qc);
535 return 1;
b212d554
HL
536}
537
3b1ab5a3
HL
538void ossl_quic_conn_force_assist_thread_wake(SSL *s)
539{
072328dd
HL
540 QCTX ctx;
541
542 if (!expect_quic(s, &ctx))
543 return;
3b1ab5a3 544
629b408c 545#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
072328dd
HL
546 if (ctx.qc->is_thread_assisted && ctx.qc->started)
547 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
629b408c 548#endif
3b1ab5a3
HL
549}
550
995ff282
HL
551QUIC_NEEDS_LOCK
552static void qc_touch_default_xso(QUIC_CONNECTION *qc)
553{
554 qc->default_xso_created = 1;
555 qc_update_reject_policy(qc);
556}
557
9cab4bd5
HL
558/*
559 * Changes default XSO. Allows caller to keep reference to the old default XSO
560 * (if any). Reference to new XSO is transferred from caller.
561 */
995ff282 562QUIC_NEEDS_LOCK
9cab4bd5
HL
563static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
564 int touch,
565 QUIC_XSO **old_xso)
995ff282 566{
9cab4bd5
HL
567 int refs;
568
569 *old_xso = NULL;
570
571 if (qc->default_xso != xso) {
572 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
573
574 qc->default_xso = xso;
575
576 if (xso == NULL) {
577 /*
578 * Changing to not having a default XSO. XSO becomes standalone and
579 * now has a ref to the QC.
580 */
581 if (!ossl_assert(SSL_up_ref(&qc->ssl)))
582 return;
583 } else {
584 /*
585 * Changing from not having a default XSO to having one. The new XSO
586 * will have had a reference to the QC we need to drop to avoid a
587 * circular reference.
3a61a96c
HL
588 *
589 * Currently we never change directly from one default XSO to
590 * another, though this function would also still be correct if this
591 * weren't the case.
9cab4bd5 592 */
3a61a96c
HL
593 assert(*old_xso == NULL);
594
4eecc6aa 595 CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
9cab4bd5
HL
596 assert(refs > 0);
597 }
598 }
599
995ff282
HL
600 if (touch)
601 qc_touch_default_xso(qc);
602}
603
9cab4bd5
HL
604/*
605 * Changes default XSO, releasing the reference to any previous default XSO.
606 * Reference to new XSO is transferred from caller.
607 */
608QUIC_NEEDS_LOCK
609static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
610{
611 QUIC_XSO *old_xso = NULL;
612
613 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
614
615 if (old_xso != NULL)
616 SSL_free(&old_xso->ssl);
617}
618
f0d9757c
HL
619/* SSL_set_options */
620static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
621{
622 QCTX ctx;
623 uint64_t r, options;
624
625 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
626 return 0;
627
628 /*
629 * Currently most options that we permit are handled in the handshake
630 * layer.
631 */
632 options = (SSL_get_options(ctx.qc->tls) & ~mask_value) | or_value;
633 options &= OSSL_QUIC_PERMITTED_OPTIONS;
634 r = SSL_set_options(ctx.qc->tls, options);
635
636 if (ctx.xso->stream != NULL && ctx.xso->stream->rstream != NULL)
637 ossl_quic_rstream_set_cleanse(ctx.xso->stream->rstream,
638 (options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
639
640 quic_unlock(ctx.qc);
641 return r;
642}
643
644uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
645{
646 return quic_mask_or_options(ssl, UINT64_MAX, options);
647}
648
649/* SSL_clear_options */
650uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
651{
652 return quic_mask_or_options(ssl, options, 0);
653}
654
655/* SSL_get_options */
656uint64_t ossl_quic_get_options(const SSL *ssl)
657{
658 return quic_mask_or_options((SSL *)ssl, 0, 0);
659}
660
22d53c88
HL
661/*
662 * QUIC Front-End I/O API: Network BIO Configuration
663 * =================================================
664 *
665 * Handling the different BIOs is difficult:
666 *
667 * - It is more or less a requirement that we use non-blocking network I/O;
668 * we need to be able to have timeouts on recv() calls, and make best effort
669 * (non blocking) send() and recv() calls.
670 *
671 * The only sensible way to do this is to configure the socket into
672 * non-blocking mode. We could try to do select() before calling send() or
673 * recv() to get a guarantee that the call will not block, but this will
674 * probably run into issues with buggy OSes which generate spurious socket
675 * readiness events. In any case, relying on this to work reliably does not
676 * seem sane.
677 *
678 * Timeouts could be handled via setsockopt() socket timeout options, but
679 * this depends on OS support and adds another syscall to every network I/O
680 * operation. It also has obvious thread safety concerns if we want to move
681 * to concurrent use of a single socket at some later date.
682 *
683 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
684 * be made non-blocking. However some OSes (e.g. Windows) do not support
685 * this, so we cannot rely on this.
686 *
687 * As such, we need to configure any FD in non-blocking mode. This may
688 * confound users who pass a blocking socket to libssl. However, in practice
689 * it would be extremely strange for a user of QUIC to pass an FD to us,
690 * then also try and send receive traffic on the same socket(!). Thus the
691 * impact of this should be limited, and can be documented.
692 *
693 * - We support both blocking and non-blocking operation in terms of the API
694 * presented to the user. One prospect is to set the blocking mode based on
695 * whether the socket passed to us was already in blocking mode. However,
696 * Windows has no API for determining if a socket is in blocking mode (!),
697 * therefore this cannot be done portably. Currently therefore we expose an
698 * explicit API call to set this, and default to blocking mode.
699 *
700 * - We need to determine our initial destination UDP address. The "natural"
701 * way for a user to do this is to set the peer variable on a BIO_dgram.
702 * However, this has problems because BIO_dgram's peer variable is used for
703 * both transmission and reception. This means it can be constantly being
704 * changed to a malicious value (e.g. if some random unrelated entity on the
705 * network starts sending traffic to us) on every read call. This is not a
706 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
707 * calls only, which do not use this variable. However, we do need to let
708 * the user specify the peer in a 'normal' manner. The compromise here is
709 * that we grab the current peer value set at the time the write BIO is set
710 * and do not read the value again.
711 *
712 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
713 * Currently we do this by only supporting non-blocking mode.
714 *
715 */
716
717/*
718 * Determines what initial destination UDP address we should use, if possible.
719 * If this fails the client must set the destination address manually, or use a
720 * BIO which does not need a destination address.
721 */
722static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
723{
75b2920a 724 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
22d53c88
HL
725 return 0;
726
727 return 1;
728}
729
072328dd 730void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
22d53c88 731{
072328dd
HL
732 QCTX ctx;
733
734 if (!expect_quic(s, &ctx))
735 return;
736
737 if (ctx.qc->net_rbio == net_rbio)
38b051a1 738 return;
38b051a1 739
23c04709 740 if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
22d53c88
HL
741 return;
742
072328dd
HL
743 BIO_free(ctx.qc->net_rbio);
744 ctx.qc->net_rbio = net_rbio;
22d53c88
HL
745
746 /*
747 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
748 * mode as we do not support it for non-pollable BIOs.
749 */
750 if (net_rbio != NULL) {
751 BIO_POLL_DESCRIPTOR d = {0};
752
753 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
754 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
cb5c208b
HL
755 ctx.qc->blocking = 0;
756 ctx.qc->default_blocking = 0;
072328dd 757 ctx.qc->can_poll_net_rbio = 0;
22d53c88 758 } else {
072328dd 759 ctx.qc->can_poll_net_rbio = 1;
22d53c88
HL
760 }
761 }
99e1cc7b
TM
762}
763
072328dd 764void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
38b051a1 765{
072328dd
HL
766 QCTX ctx;
767
768 if (!expect_quic(s, &ctx))
22d53c88
HL
769 return;
770
072328dd 771 if (ctx.qc->net_wbio == net_wbio)
22d53c88
HL
772 return;
773
23c04709 774 if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
072328dd
HL
775 return;
776
777 BIO_free(ctx.qc->net_wbio);
778 ctx.qc->net_wbio = net_wbio;
22d53c88
HL
779
780 if (net_wbio != NULL) {
781 BIO_POLL_DESCRIPTOR d = {0};
38b051a1 782
22d53c88
HL
783 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
784 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
cb5c208b
HL
785 ctx.qc->blocking = 0;
786 ctx.qc->default_blocking = 0;
072328dd 787 ctx.qc->can_poll_net_wbio = 0;
22d53c88 788 } else {
072328dd 789 ctx.qc->can_poll_net_wbio = 1;
22d53c88 790 }
38b051a1 791
22d53c88
HL
792 /*
793 * If we do not have a peer address yet, and we have not started trying
794 * to connect yet, try to autodetect one.
795 */
072328dd
HL
796 if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC
797 && !ctx.qc->started) {
798 if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr))
22d53c88 799 /* best effort */
072328dd 800 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
22d53c88 801
23c04709
HL
802 ossl_quic_channel_set_peer_addr(ctx.qc->ch,
803 &ctx.qc->init_peer_addr);
22d53c88 804 }
38b051a1 805 }
22d53c88 806}
38b051a1 807
072328dd 808BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
22d53c88 809{
072328dd
HL
810 QCTX ctx;
811
812 if (!expect_quic(s, &ctx))
813 return NULL;
814
815 return ctx.qc->net_rbio;
38b051a1
TM
816}
817
072328dd 818BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
22d53c88 819{
072328dd
HL
820 QCTX ctx;
821
822 if (!expect_quic(s, &ctx))
823 return NULL;
824
825 return ctx.qc->net_wbio;
22d53c88
HL
826}
827
072328dd 828int ossl_quic_conn_get_blocking_mode(const SSL *s)
99e1cc7b 829{
072328dd
HL
830 QCTX ctx;
831
832 if (!expect_quic(s, &ctx))
833 return 0;
834
cb5c208b
HL
835 if (ctx.is_stream)
836 return ctx.xso->blocking;
837
072328dd 838 return ctx.qc->blocking;
22d53c88
HL
839}
840
072328dd 841int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
22d53c88 842{
072328dd
HL
843 QCTX ctx;
844
845 if (!expect_quic(s, &ctx))
846 return 0;
847
22d53c88
HL
848 /* Cannot enable blocking mode if we do not have pollable FDs. */
849 if (blocking != 0 &&
072328dd 850 (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio))
faa3a180 851 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
22d53c88 852
cb5c208b
HL
853 if (!ctx.is_stream) {
854 /*
855 * If called on a QCSO, update default and connection-level blocking
856 * modes.
857 */
858 ctx.qc->blocking = (blocking != 0);
859 ctx.qc->default_blocking = ctx.qc->blocking;
860 }
861
862 if (ctx.xso != NULL)
863 /*
864 * If called on a QSSO or QCSO with a default XSO, update blocking
865 * mode.
866 */
867 ctx.xso->blocking = (blocking != 0);
868
99e1cc7b
TM
869 return 1;
870}
871
072328dd 872int ossl_quic_conn_set_initial_peer_addr(SSL *s,
22d53c88 873 const BIO_ADDR *peer_addr)
99e1cc7b 874{
072328dd
HL
875 QCTX ctx;
876
877 if (!expect_quic(s, &ctx))
878 return 0;
879
880 if (ctx.qc->started)
faa3a180 881 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
22d53c88 882 NULL);
38b051a1 883
22d53c88 884 if (peer_addr == NULL) {
072328dd 885 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
22d53c88
HL
886 return 1;
887 }
38b051a1 888
072328dd 889 ctx.qc->init_peer_addr = *peer_addr;
99e1cc7b
TM
890 return 1;
891}
892
22d53c88
HL
893/*
894 * QUIC Front-End I/O API: Asynchronous I/O Management
895 * ===================================================
896 *
6084e04b
HL
897 * (BIO/)SSL_handle_events => ossl_quic_handle_events
898 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
22d53c88
HL
899 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
900 *
901 */
902
903/* Returns 1 if the connection is being used in blocking mode. */
cb5c208b 904static int qc_blocking_mode(const QUIC_CONNECTION *qc)
99e1cc7b 905{
22d53c88
HL
906 return qc->blocking;
907}
38b051a1 908
cb5c208b
HL
909static int xso_blocking_mode(const QUIC_XSO *xso)
910{
911 return xso->blocking
912 && xso->conn->can_poll_net_rbio
913 && xso->conn->can_poll_net_wbio;
914}
915
b626a0f1 916/* SSL_handle_events; performs QUIC I/O and timeout processing. */
a8489257 917QUIC_TAKES_LOCK
6084e04b 918int ossl_quic_handle_events(SSL *s)
22d53c88 919{
072328dd
HL
920 QCTX ctx;
921
922 if (!expect_quic(s, &ctx))
923 return 0;
924
925 quic_lock(ctx.qc);
072328dd
HL
926 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
927 quic_unlock(ctx.qc);
99e1cc7b
TM
928 return 1;
929}
930
22d53c88 931/*
6084e04b 932 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
b626a0f1
HL
933 * should next have events handled by the application by calling
934 * SSL_handle_events(). tv is set to 0 if the object should have events handled
935 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
936 * the value of *tv is undefined.
22d53c88 937 */
a8489257 938QUIC_TAKES_LOCK
7ea49713 939int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
99e1cc7b 940{
072328dd 941 QCTX ctx;
a1660c94 942 OSSL_TIME deadline = ossl_time_infinite();
e44795bd 943
072328dd
HL
944 if (!expect_quic(s, &ctx))
945 return 0;
a8489257 946
072328dd
HL
947 quic_lock(ctx.qc);
948
23c04709
HL
949 deadline
950 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
22d53c88
HL
951
952 if (ossl_time_is_infinite(deadline)) {
7ea49713
HL
953 *is_infinite = 1;
954
955 /*
956 * Robustness against faulty applications that don't check *is_infinite;
957 * harmless long timeout.
958 */
959 tv->tv_sec = 1000000;
22d53c88 960 tv->tv_usec = 0;
7ea49713 961
072328dd 962 quic_unlock(ctx.qc);
22d53c88
HL
963 return 1;
964 }
965
e3e9794a 966 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
7ea49713 967 *is_infinite = 0;
072328dd 968 quic_unlock(ctx.qc);
22d53c88
HL
969 return 1;
970}
971
972/* SSL_get_rpoll_descriptor */
072328dd 973int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
22d53c88 974{
072328dd
HL
975 QCTX ctx;
976
977 if (!expect_quic(s, &ctx))
e44795bd
TM
978 return 0;
979
072328dd
HL
980 if (desc == NULL || ctx.qc->net_rbio == NULL)
981 return 0;
982
983 return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
99e1cc7b
TM
984}
985
22d53c88 986/* SSL_get_wpoll_descriptor */
072328dd 987int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
99e1cc7b 988{
072328dd
HL
989 QCTX ctx;
990
991 if (!expect_quic(s, &ctx))
992 return 0;
993
994 if (desc == NULL || ctx.qc->net_wbio == NULL)
22d53c88
HL
995 return 0;
996
072328dd 997 return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
99e1cc7b
TM
998}
999
b639475a 1000/* SSL_net_read_desired */
a8489257 1001QUIC_TAKES_LOCK
072328dd 1002int ossl_quic_get_net_read_desired(SSL *s)
99e1cc7b 1003{
072328dd 1004 QCTX ctx;
a8489257
HL
1005 int ret;
1006
072328dd
HL
1007 if (!expect_quic(s, &ctx))
1008 return 0;
1009
1010 quic_lock(ctx.qc);
072328dd
HL
1011 ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1012 quic_unlock(ctx.qc);
a8489257 1013 return ret;
22d53c88 1014}
e44795bd 1015
b639475a 1016/* SSL_net_write_desired */
a8489257 1017QUIC_TAKES_LOCK
072328dd 1018int ossl_quic_get_net_write_desired(SSL *s)
22d53c88 1019{
a8489257 1020 int ret;
072328dd
HL
1021 QCTX ctx;
1022
1023 if (!expect_quic(s, &ctx))
1024 return 0;
a8489257 1025
072328dd 1026 quic_lock(ctx.qc);
072328dd
HL
1027 ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1028 quic_unlock(ctx.qc);
a8489257 1029 return ret;
99e1cc7b
TM
1030}
1031
22d53c88
HL
1032/*
1033 * QUIC Front-End I/O API: Connection Lifecycle Operations
1034 * =======================================================
1035 *
1036 * SSL_do_handshake => ossl_quic_do_handshake
1037 * SSL_set_connect_state => ossl_quic_set_connect_state
1038 * SSL_set_accept_state => ossl_quic_set_accept_state
1039 * SSL_shutdown => ossl_quic_shutdown
1040 * SSL_ctrl => ossl_quic_ctrl
1041 * (BIO/)SSL_connect => ossl_quic_connect
1042 * (BIO/)SSL_accept => ossl_quic_accept
1043 *
1044 */
1045
1046/* SSL_shutdown */
e8043229 1047static int quic_shutdown_wait(void *arg)
99e1cc7b 1048{
e8043229 1049 QUIC_CONNECTION *qc = arg;
22d53c88 1050
23c04709 1051 return ossl_quic_channel_is_terminated(qc->ch);
e8043229 1052}
22d53c88 1053
a8489257 1054QUIC_TAKES_LOCK
072328dd 1055int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
e8043229
HL
1056 const SSL_SHUTDOWN_EX_ARGS *args,
1057 size_t args_len)
1058{
a8489257 1059 int ret;
072328dd 1060 QCTX ctx;
a8489257 1061
072328dd
HL
1062 if (!expect_quic(s, &ctx))
1063 return 0;
1064
cb5c208b
HL
1065 if (ctx.is_stream)
1066 /* TODO(QUIC): Semantics currently undefined for QSSOs */
1067 return -1;
1068
072328dd 1069 quic_lock(ctx.qc);
a8489257 1070
072328dd 1071 ossl_quic_channel_local_close(ctx.qc->ch,
e8043229
HL
1072 args != NULL ? args->quic_error_code : 0);
1073
1d40b151 1074 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
e8043229 1075
072328dd
HL
1076 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1077 quic_unlock(ctx.qc);
e8043229 1078 return 1;
a8489257 1079 }
e8043229 1080
cb5c208b 1081 if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
072328dd 1082 block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
e8043229 1083 else
072328dd 1084 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
e8043229 1085
072328dd
HL
1086 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1087 quic_unlock(ctx.qc);
a8489257 1088 return ret;
99e1cc7b
TM
1089}
1090
22d53c88 1091/* SSL_ctrl */
e44795bd 1092long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
99e1cc7b 1093{
072328dd 1094 QCTX ctx;
38b051a1 1095
072328dd 1096 if (!expect_quic(s, &ctx))
38b051a1
TM
1097 return 0;
1098
22d53c88
HL
1099 switch (cmd) {
1100 case SSL_CTRL_MODE:
cb5c208b
HL
1101 /* If called on a QCSO, update the default mode. */
1102 if (!ctx.is_stream)
1103 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1104
1105 /*
1106 * If we were called on a QSSO or have a default stream, we also update
1107 * that.
1108 */
1109 if (ctx.xso != NULL) {
1110 /* Cannot enable EPW while AON write in progress. */
1111 if (ctx.xso->aon_write_in_progress)
1112 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1113
1114 ctx.xso->ssl_mode |= (uint32_t)larg;
1115 return ctx.xso->ssl_mode;
1116 }
dfc227bd 1117
cb5c208b 1118 return ctx.qc->default_ssl_mode;
22d53c88 1119 case SSL_CTRL_CLEAR_MODE:
cb5c208b
HL
1120 if (!ctx.is_stream)
1121 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1122
1123 if (ctx.xso != NULL) {
1124 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1125 return ctx.xso->ssl_mode;
1126 }
1127
1128 return ctx.qc->default_ssl_mode;
63dfde87
MC
1129
1130 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
5cf99b40 1131 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
63dfde87
MC
1132 /* This ctrl also needs to be passed to the internal SSL object */
1133 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1134
2f90ea3d
HL
1135 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1136 {
1137 int is_infinite;
1138
1139 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1140 return 0;
1141
1142 return !is_infinite;
1143 }
1144 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1145 /* For legacy compatibility with DTLS calls. */
1146 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
22d53c88 1147 default:
f8ffab0d 1148 /* Probably a TLS related ctrl. Defer to our internal SSL object */
072328dd 1149 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
08e49012 1150 }
22d53c88
HL
1151}
1152
1153/* SSL_set_connect_state */
072328dd 1154void ossl_quic_set_connect_state(SSL *s)
22d53c88 1155{
072328dd
HL
1156 QCTX ctx;
1157
1158 if (!expect_quic(s, &ctx))
1159 return;
1160
22d53c88 1161 /* Cannot be changed after handshake started */
cb5c208b 1162 if (ctx.qc->started || ctx.is_stream)
22d53c88
HL
1163 return;
1164
dfb9ae14 1165 ctx.qc->as_server_state = 0;
22d53c88
HL
1166}
1167
1168/* SSL_set_accept_state */
072328dd 1169void ossl_quic_set_accept_state(SSL *s)
22d53c88 1170{
072328dd
HL
1171 QCTX ctx;
1172
1173 if (!expect_quic(s, &ctx))
1174 return;
1175
22d53c88 1176 /* Cannot be changed after handshake started */
cb5c208b 1177 if (ctx.qc->started || ctx.is_stream)
22d53c88
HL
1178 return;
1179
dfb9ae14 1180 ctx.qc->as_server_state = 1;
22d53c88
HL
1181}
1182
1183/* SSL_do_handshake */
1184struct quic_handshake_wait_args {
1185 QUIC_CONNECTION *qc;
1186};
1187
1188static int quic_handshake_wait(void *arg)
1189{
1190 struct quic_handshake_wait_args *args = arg;
1191
1192 if (!ossl_quic_channel_is_active(args->qc->ch))
1193 return -1;
1194
1195 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1196 return 1;
1197
99e1cc7b
TM
1198 return 0;
1199}
1200
22d53c88 1201static int configure_channel(QUIC_CONNECTION *qc)
99e1cc7b 1202{
22d53c88 1203 assert(qc->ch != NULL);
08e49012 1204
d1ac77b1
HL
1205 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1206 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
22d53c88
HL
1207 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1208 return 0;
1209
1210 return 1;
1211}
1212
4847599b 1213QUIC_NEEDS_LOCK
23c04709 1214static int create_channel(QUIC_CONNECTION *qc)
22d53c88
HL
1215{
1216 QUIC_CHANNEL_ARGS args = {0};
1217
5cf99b40
MC
1218 args.libctx = qc->ssl.ctx->libctx;
1219 args.propq = qc->ssl.ctx->propq;
1220 args.is_server = qc->as_server;
1221 args.tls = qc->tls;
1222 args.mutex = qc->mutex;
e3e9794a
HL
1223 args.now_cb = get_time_cb;
1224 args.now_cb_arg = qc;
22d53c88
HL
1225
1226 qc->ch = ossl_quic_channel_new(&args);
1227 if (qc->ch == NULL)
1228 return 0;
1229
e8043229
HL
1230 return 1;
1231}
1232
1233/*
1234 * Creates a channel and configures it with the information we have accumulated
1235 * via calls made to us from the application prior to starting a handshake
1236 * attempt.
1237 */
4847599b 1238QUIC_NEEDS_LOCK
23c04709 1239static int ensure_channel_started(QUIC_CONNECTION *qc)
e8043229 1240{
ffce2946 1241 if (!qc->started) {
ffce2946
HL
1242 if (!configure_channel(qc)
1243 || !ossl_quic_channel_start(qc->ch))
1244 goto err;
f2f7c4f1 1245
629b408c 1246#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
ffce2946
HL
1247 if (qc->is_thread_assisted)
1248 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
1249 goto err;
629b408c 1250#endif
ffce2946
HL
1251 }
1252
22d53c88
HL
1253 qc->started = 1;
1254 return 1;
f2f7c4f1
HL
1255
1256err:
1257 ossl_quic_channel_free(qc->ch);
1258 qc->ch = NULL;
1259 return 0;
99e1cc7b
TM
1260}
1261
4a530180 1262QUIC_NEEDS_LOCK
faa3a180 1263static int quic_do_handshake(QCTX *ctx)
99e1cc7b 1264{
22d53c88 1265 int ret;
faa3a180 1266 QUIC_CONNECTION *qc = ctx->qc;
22d53c88 1267
23c04709 1268 if (ossl_quic_channel_is_handshake_complete(qc->ch))
ca41f6b7 1269 /* Handshake already completed. */
4a530180 1270 return 1;
ca41f6b7 1271
23c04709 1272 if (ossl_quic_channel_is_term_any(qc->ch))
faa3a180 1273 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
22d53c88 1274
ca41f6b7 1275 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
22d53c88 1276 /* Peer address must have been set. */
faa3a180 1277 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
4a530180 1278 return -1; /* Non-protocol error */
ca41f6b7 1279 }
22d53c88 1280
dfb9ae14 1281 if (qc->as_server != qc->as_server_state) {
23c04709 1282 /* TODO(QUIC): Must match the method used to create the QCSO */
faa3a180 1283 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
4a530180 1284 return -1; /* Non-protocol error */
ca41f6b7 1285 }
22d53c88 1286
ca41f6b7 1287 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
22d53c88 1288 /* Need read and write BIOs. */
faa3a180 1289 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
4a530180 1290 return -1; /* Non-protocol error */
ca41f6b7 1291 }
22d53c88
HL
1292
1293 /*
1294 * Start connection process. Note we may come here multiple times in
1295 * non-blocking mode, which is fine.
1296 */
23c04709 1297 if (!ensure_channel_started(qc)) {
faa3a180 1298 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
4a530180 1299 return -1; /* Non-protocol error */
ca41f6b7 1300 }
22d53c88 1301
4a530180 1302 if (ossl_quic_channel_is_handshake_complete(qc->ch))
22d53c88 1303 /* The handshake is now done. */
4a530180 1304 return 1;
22d53c88 1305
cb5c208b 1306 if (qc_blocking_mode(qc)) {
22d53c88
HL
1307 /* In blocking mode, wait for the handshake to complete. */
1308 struct quic_handshake_wait_args args;
1309
1310 args.qc = qc;
1311
1312 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
ca41f6b7 1313 if (!ossl_quic_channel_is_active(qc->ch)) {
faa3a180 1314 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
4a530180 1315 return 0; /* Shutdown before completion */
ca41f6b7 1316 } else if (ret <= 0) {
faa3a180 1317 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
4a530180 1318 return -1; /* Non-protocol error */
ca41f6b7 1319 }
22d53c88
HL
1320
1321 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
4a530180 1322 return 1;
22d53c88 1323 } else {
ca41f6b7 1324 /* Try to advance the reactor. */
ccd31037 1325 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
ca41f6b7 1326
4a530180 1327 if (ossl_quic_channel_is_handshake_complete(qc->ch))
ca41f6b7 1328 /* The handshake is now done. */
4a530180 1329 return 1;
ca41f6b7 1330
22d53c88 1331 /* Otherwise, indicate that the handshake isn't done yet. */
faa3a180 1332 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
4a530180 1333 return -1; /* Non-protocol error */
22d53c88 1334 }
4a530180 1335}
a8489257 1336
4a530180 1337QUIC_TAKES_LOCK
072328dd 1338int ossl_quic_do_handshake(SSL *s)
4a530180
HL
1339{
1340 int ret;
072328dd 1341 QCTX ctx;
4a530180 1342
072328dd
HL
1343 if (!expect_quic(s, &ctx))
1344 return 0;
1345
1346 quic_lock(ctx.qc);
4a530180 1347
faa3a180 1348 ret = quic_do_handshake(&ctx);
072328dd 1349 quic_unlock(ctx.qc);
a8489257 1350 return ret;
99e1cc7b
TM
1351}
1352
22d53c88
HL
1353/* SSL_connect */
1354int ossl_quic_connect(SSL *s)
99e1cc7b 1355{
22d53c88 1356 /* Ensure we are in connect state (no-op if non-idle). */
072328dd 1357 ossl_quic_set_connect_state(s);
22d53c88
HL
1358
1359 /* Begin or continue the handshake */
072328dd 1360 return ossl_quic_do_handshake(s);
99e1cc7b
TM
1361}
1362
22d53c88
HL
1363/* SSL_accept */
1364int ossl_quic_accept(SSL *s)
99e1cc7b 1365{
22d53c88 1366 /* Ensure we are in accept state (no-op if non-idle). */
072328dd 1367 ossl_quic_set_accept_state(s);
22d53c88
HL
1368
1369 /* Begin or continue the handshake */
072328dd 1370 return ossl_quic_do_handshake(s);
99e1cc7b 1371}
e44795bd 1372
cb5c208b
HL
1373/*
1374 * QUIC Front-End I/O API: Stream Lifecycle Operations
1375 * ===================================================
1376 *
1377 * SSL_stream_new => ossl_quic_conn_stream_new
1378 *
1379 */
21c80696
HL
1380
1381/*
1382 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1383 * default XSO was created. Returns 0 if it was not (e.g. because it already
1384 * exists). Note that this is NOT an error condition.
1385 */
1386QUIC_NEEDS_LOCK
faa3a180 1387static int qc_try_create_default_xso_for_write(QCTX *ctx)
21c80696 1388{
8b7be3aa 1389 uint64_t flags = 0;
faa3a180 1390 QUIC_CONNECTION *qc = ctx->qc;
21c80696 1391
8b7be3aa
HL
1392 if (qc->default_xso_created
1393 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
21c80696
HL
1394 /*
1395 * We only do this once. If the user detaches a previously created
1396 * default XSO we don't auto-create another one.
1397 */
faa3a180 1398 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
21c80696 1399
8b7be3aa
HL
1400 /* Create a locally-initiated stream. */
1401 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1402 flags |= SSL_STREAM_FLAG_UNI;
1403
faa3a180 1404 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
13ac037d 1405 /*needs_lock=*/0),
995ff282 1406 /*touch=*/0);
8b7be3aa 1407 if (qc->default_xso == NULL)
faa3a180 1408 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
8b7be3aa 1409
995ff282 1410 qc_touch_default_xso(qc);
8b7be3aa
HL
1411 return 1;
1412}
1413
1414struct quic_wait_for_stream_args {
1415 QUIC_CONNECTION *qc;
1416 QUIC_STREAM *qs;
faa3a180 1417 QCTX *ctx;
8b7be3aa
HL
1418 uint64_t expect_id;
1419};
1420
1421QUIC_NEEDS_LOCK
1422static int quic_wait_for_stream(void *arg)
1423{
1424 struct quic_wait_for_stream_args *args = arg;
1425
1426 if (!ossl_quic_channel_is_active(args->qc->ch)) {
1427 /* If connection is torn down due to an error while blocking, stop. */
faa3a180 1428 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
8b7be3aa
HL
1429 return -1;
1430 }
1431
1432 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
acc6fde0
HL
1433 args->expect_id | QUIC_STREAM_DIR_BIDI);
1434 if (args->qs == NULL)
1435 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1436 args->expect_id | QUIC_STREAM_DIR_UNI);
1437
8b7be3aa
HL
1438 if (args->qs != NULL)
1439 return 1; /* stream now exists */
1440
1441 return 0; /* did not get a stream, keep trying */
1442}
1443
1444QUIC_NEEDS_LOCK
faa3a180 1445static int qc_wait_for_default_xso_for_read(QCTX *ctx)
8b7be3aa
HL
1446{
1447 /* Called on a QCSO and we don't currently have a default stream. */
1448 uint64_t expect_id;
faa3a180 1449 QUIC_CONNECTION *qc = ctx->qc;
8b7be3aa
HL
1450 QUIC_STREAM *qs;
1451 int res;
1452 struct quic_wait_for_stream_args wargs;
1453
1454 /*
1455 * If default stream functionality is disabled or we already detached
1456 * one, don't make another default stream and just fail.
1457 */
1458 if (qc->default_xso_created
1459 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
faa3a180 1460 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
8b7be3aa
HL
1461
1462 /*
1463 * The peer may have opened a stream since we last ticked. So tick and
1464 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1465 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1466 * first stream created by a peer must have an ordinal of 0.
1467 */
1468 expect_id = qc->as_server
1469 ? QUIC_STREAM_INITIATOR_CLIENT
1470 : QUIC_STREAM_INITIATOR_SERVER;
1471
8b7be3aa 1472 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
acc6fde0
HL
1473 expect_id | QUIC_STREAM_DIR_BIDI);
1474 if (qs == NULL)
1475 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1476 expect_id | QUIC_STREAM_DIR_UNI);
1477
8b7be3aa
HL
1478 if (qs == NULL) {
1479 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1480
1481 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1482 expect_id);
1483 }
1484
1485 if (qs == NULL) {
1486 if (!qc_blocking_mode(qc))
1487 /* Non-blocking mode, so just bail immediately. */
faa3a180 1488 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
8b7be3aa
HL
1489
1490 /* Block until we have a stream. */
1491 wargs.qc = qc;
1492 wargs.qs = NULL;
faa3a180 1493 wargs.ctx = ctx;
8b7be3aa
HL
1494 wargs.expect_id = expect_id;
1495
1496 res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1497 if (res == 0)
faa3a180 1498 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
8b7be3aa
HL
1499 else if (res < 0 || wargs.qs == NULL)
1500 /* quic_wait_for_stream raised error here */
21c80696 1501 return 0;
8b7be3aa
HL
1502
1503 qs = wargs.qs;
21c80696
HL
1504 }
1505
8b7be3aa
HL
1506 /*
1507 * We now have qs != NULL. Make it the default stream, creating the
1508 * necessary XSO.
1509 */
995ff282 1510 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
8b7be3aa 1511 if (qc->default_xso == NULL)
faa3a180 1512 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
8b7be3aa 1513
995ff282 1514 qc_touch_default_xso(qc); /* inhibits default XSO */
21c80696
HL
1515 return 1;
1516}
1517
1518QUIC_NEEDS_LOCK
1519static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1520{
1521 QUIC_XSO *xso = NULL;
1522
1523 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL)
1524 goto err;
1525
1526 if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO))
1527 goto err;
1528
9cab4bd5
HL
1529 /* XSO refs QC */
1530 if (!SSL_up_ref(&qc->ssl))
1531 goto err;
1532
21c80696
HL
1533 xso->conn = qc;
1534 xso->blocking = qc->default_blocking;
1535 xso->ssl_mode = qc->default_ssl_mode;
faa3a180 1536 xso->last_error = SSL_ERROR_NONE;
21c80696
HL
1537
1538 xso->stream = qs;
1539
1540 ++qc->num_xso;
1541 return xso;
1542
1543err:
1544 OPENSSL_free(xso);
1545 return NULL;
1546}
1547
13ac037d 1548/* locking depends on need_lock */
faa3a180 1549static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
cb5c208b 1550{
faa3a180 1551 QUIC_CONNECTION *qc = ctx->qc;
cb5c208b 1552 QUIC_XSO *xso = NULL;
21c80696 1553 QUIC_STREAM *qs = NULL;
2dbc39de 1554 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
cb5c208b 1555
13ac037d
HL
1556 if (need_lock)
1557 quic_lock(qc);
2dbc39de 1558
13ac037d 1559 if (ossl_quic_channel_is_term_any(qc->ch)) {
faa3a180 1560 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2dbc39de
HL
1561 goto err;
1562 }
1563
13ac037d 1564 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
21c80696 1565 if (qs == NULL)
2dbc39de 1566 goto err;
cb5c208b 1567
13ac037d 1568 xso = create_xso_from_stream(qc, qs);
21c80696 1569 if (xso == NULL)
cb5c208b
HL
1570 goto err;
1571
13ac037d
HL
1572 qc_touch_default_xso(qc); /* inhibits default XSO */
1573 if (need_lock)
1574 quic_unlock(qc);
1575
cb5c208b
HL
1576 return &xso->ssl;
1577
1578err:
1579 OPENSSL_free(xso);
13ac037d
HL
1580 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
1581 if (need_lock)
1582 quic_unlock(qc);
1583
cb5c208b 1584 return NULL;
13ac037d
HL
1585
1586}
1587
1588QUIC_TAKES_LOCK
1589SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
1590{
1591 QCTX ctx;
1592
1593 if (!expect_quic_conn_only(s, &ctx))
1594 return NULL;
1595
faa3a180 1596 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
cb5c208b
HL
1597}
1598
22d53c88
HL
1599/*
1600 * QUIC Front-End I/O API: Steady-State Operations
1601 * ===============================================
1602 *
1603 * Here we dispatch calls to the steady-state front-end I/O API functions; that
1604 * is, the functions used during the established phase of a QUIC connection
1605 * (e.g. SSL_read, SSL_write).
1606 *
1607 * Each function must handle both blocking and non-blocking modes. As discussed
1608 * above, all QUIC I/O is implemented using non-blocking mode internally.
1609 *
1610 * SSL_get_error => partially implemented by ossl_quic_get_error
1611 * (BIO/)SSL_read => ossl_quic_read
1612 * (BIO/)SSL_write => ossl_quic_write
1613 * SSL_pending => ossl_quic_pending
a9979965 1614 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
2525109f 1615 * SSL_key_update => ossl_quic_key_update
22d53c88
HL
1616 */
1617
1618/* SSL_get_error */
072328dd 1619int ossl_quic_get_error(const SSL *s, int i)
e44795bd 1620{
072328dd
HL
1621 QCTX ctx;
1622
1623 if (!expect_quic(s, &ctx))
1624 return 0;
1625
faa3a180 1626 return ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
e44795bd
TM
1627}
1628
22d53c88
HL
1629/*
1630 * SSL_write
1631 * ---------
1632 *
1633 * The set of functions below provide the implementation of the public SSL_write
1634 * function. We must handle:
1635 *
1636 * - both blocking and non-blocking operation at the application level,
1637 * depending on how we are configured;
1638 *
1639 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
1640 *
1641 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
1642 *
1643 */
4847599b 1644QUIC_NEEDS_LOCK
cb5c208b 1645static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
22d53c88
HL
1646{
1647 /*
1648 * We have appended at least one byte to the stream.
1649 * Potentially mark stream as active, depending on FC.
1650 */
1651 if (did_append)
cb5c208b
HL
1652 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
1653 xso->stream);
22d53c88
HL
1654
1655 /*
1656 * Try and send.
1657 *
1658 * TODO(QUIC): It is probably inefficient to try and do this immediately,
1659 * plus we should eventually consider Nagle's algorithm.
1660 */
1661 if (do_tick)
cb5c208b 1662 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
22d53c88
HL
1663}
1664
1665struct quic_write_again_args {
cb5c208b 1666 QUIC_XSO *xso;
22d53c88
HL
1667 const unsigned char *buf;
1668 size_t len;
1669 size_t total_written;
1670};
1671
4847599b 1672QUIC_NEEDS_LOCK
22d53c88
HL
1673static int quic_write_again(void *arg)
1674{
1675 struct quic_write_again_args *args = arg;
1676 size_t actual_written = 0;
1677
cb5c208b 1678 if (!ossl_quic_channel_is_active(args->xso->conn->ch))
22d53c88
HL
1679 /* If connection is torn down due to an error while blocking, stop. */
1680 return -2;
1681
cb5c208b 1682 if (!ossl_quic_sstream_append(args->xso->stream->sstream,
22d53c88
HL
1683 args->buf, args->len, &actual_written))
1684 return -2;
1685
cb5c208b 1686 quic_post_write(args->xso, actual_written > 0, 0);
22d53c88
HL
1687
1688 args->buf += actual_written;
1689 args->len -= actual_written;
1690 args->total_written += actual_written;
1691
3f0c310b 1692 if (args->len == 0)
22d53c88
HL
1693 /* Written everything, done. */
1694 return 1;
1695
1696 /* Not written everything yet, keep trying. */
1697 return 0;
1698}
1699
4847599b 1700QUIC_NEEDS_LOCK
faa3a180 1701static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
22d53c88 1702 size_t *written)
e44795bd 1703{
22d53c88 1704 int res;
faa3a180 1705 QUIC_XSO *xso = ctx->xso;
22d53c88
HL
1706 struct quic_write_again_args args;
1707 size_t actual_written = 0;
1708
1709 /* First make a best effort to append as much of the data as possible. */
cb5c208b 1710 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len,
22d53c88
HL
1711 &actual_written)) {
1712 /* Stream already finished or allocation error. */
1713 *written = 0;
faa3a180 1714 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1715 }
1716
cb5c208b 1717 quic_post_write(xso, actual_written > 0, 1);
22d53c88
HL
1718
1719 if (actual_written == len) {
1720 /* Managed to append everything on the first try. */
1721 *written = actual_written;
1722 return 1;
1723 }
1724
1725 /*
1726 * We did not manage to append all of the data immediately, so the stream
1727 * buffer has probably filled up. This means we need to block until some of
1728 * it is freed up.
1729 */
cb5c208b 1730 args.xso = xso;
22d53c88
HL
1731 args.buf = (const unsigned char *)buf + actual_written;
1732 args.len = len - actual_written;
1733 args.total_written = 0;
1734
cb5c208b 1735 res = block_until_pred(xso->conn, quic_write_again, &args, 0);
22d53c88 1736 if (res <= 0) {
cb5c208b 1737 if (!ossl_quic_channel_is_active(xso->conn->ch))
faa3a180 1738 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
22d53c88 1739 else
faa3a180 1740 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1741 }
1742
1743 *written = args.total_written;
e44795bd
TM
1744 return 1;
1745}
1746
ca41f6b7
HL
1747/*
1748 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
1749 * write semantics.
1750 */
cb5c208b 1751static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
22d53c88
HL
1752 size_t buf_len, size_t already_sent)
1753{
cb5c208b 1754 assert(!xso->aon_write_in_progress);
22d53c88 1755
cb5c208b
HL
1756 xso->aon_write_in_progress = 1;
1757 xso->aon_buf_base = buf;
1758 xso->aon_buf_pos = already_sent;
1759 xso->aon_buf_len = buf_len;
22d53c88
HL
1760}
1761
cb5c208b 1762static void aon_write_finish(QUIC_XSO *xso)
22d53c88 1763{
cb5c208b
HL
1764 xso->aon_write_in_progress = 0;
1765 xso->aon_buf_base = NULL;
1766 xso->aon_buf_pos = 0;
1767 xso->aon_buf_len = 0;
22d53c88
HL
1768}
1769
4847599b 1770QUIC_NEEDS_LOCK
faa3a180 1771static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
22d53c88 1772 size_t len, size_t *written)
e44795bd 1773{
faa3a180 1774 QUIC_XSO *xso = ctx->xso;
22d53c88
HL
1775 const void *actual_buf;
1776 size_t actual_len, actual_written = 0;
1777 int accept_moving_buffer
cb5c208b 1778 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
22d53c88 1779
cb5c208b 1780 if (xso->aon_write_in_progress) {
22d53c88
HL
1781 /*
1782 * We are in the middle of an AON write (i.e., a previous write did not
75b2920a
HL
1783 * manage to append all data to the SSTREAM and we have Enable Partial
1784 * Write (EPW) mode disabled.)
22d53c88 1785 */
cb5c208b
HL
1786 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
1787 || len != xso->aon_buf_len)
22d53c88
HL
1788 /*
1789 * Pointer must not have changed if we are not in accept moving
1790 * buffer mode. Length must never change.
1791 */
faa3a180 1792 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
22d53c88 1793
cb5c208b
HL
1794 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
1795 actual_len = len - xso->aon_buf_pos;
22d53c88
HL
1796 assert(actual_len > 0);
1797 } else {
1798 actual_buf = buf;
1799 actual_len = len;
1800 }
1801
1802 /* First make a best effort to append as much of the data as possible. */
cb5c208b 1803 if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len,
22d53c88
HL
1804 &actual_written)) {
1805 /* Stream already finished or allocation error. */
1806 *written = 0;
faa3a180 1807 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1808 }
1809
cb5c208b 1810 quic_post_write(xso, actual_written > 0, 1);
22d53c88
HL
1811
1812 if (actual_written == actual_len) {
1813 /* We have sent everything. */
cb5c208b 1814 if (xso->aon_write_in_progress) {
22d53c88
HL
1815 /*
1816 * We have sent everything, and we were in the middle of an AON
1817 * write. The output write length is the total length of the AON
1818 * buffer, not however many bytes we managed to write to the stream
1819 * in this call.
1820 */
cb5c208b
HL
1821 *written = xso->aon_buf_len;
1822 aon_write_finish(xso);
22d53c88
HL
1823 } else {
1824 *written = actual_written;
1825 }
1826
1827 return 1;
1828 }
1829
cb5c208b 1830 if (xso->aon_write_in_progress) {
22d53c88
HL
1831 /*
1832 * AON write is in progress but we have not written everything yet. We
1833 * may have managed to send zero bytes, or some number of bytes less
1834 * than the total remaining which need to be appended during this
1835 * AON operation.
1836 */
cb5c208b
HL
1837 xso->aon_buf_pos += actual_written;
1838 assert(xso->aon_buf_pos < xso->aon_buf_len);
faa3a180 1839 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
22d53c88
HL
1840 }
1841
08e49012 1842 /*
22d53c88
HL
1843 * Not in an existing AON operation but partial write is not enabled, so we
1844 * need to begin a new AON operation. However we needn't bother if we didn't
1845 * actually append anything.
08e49012 1846 */
22d53c88 1847 if (actual_written > 0)
cb5c208b 1848 aon_write_begin(xso, buf, len, actual_written);
e44795bd 1849
22d53c88
HL
1850 /*
1851 * AON - We do not publicly admit to having appended anything until AON
1852 * completes.
1853 */
1854 *written = 0;
faa3a180 1855 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
e44795bd
TM
1856}
1857
4847599b 1858QUIC_NEEDS_LOCK
faa3a180 1859static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
22d53c88 1860 size_t *written)
e44795bd 1861{
faa3a180
HL
1862 QUIC_XSO *xso = ctx->xso;
1863
22d53c88 1864 /* Simple best effort operation. */
cb5c208b 1865 if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) {
22d53c88
HL
1866 /* Stream already finished or allocation error. */
1867 *written = 0;
faa3a180 1868 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1869 }
1870
cb5c208b 1871 quic_post_write(xso, *written > 0, 1);
e44795bd
TM
1872 return 1;
1873}
d5ab48a1 1874
a8489257 1875QUIC_TAKES_LOCK
22d53c88 1876int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
d5ab48a1 1877{
a8489257 1878 int ret;
072328dd
HL
1879 QCTX ctx;
1880 int partial_write;
22d53c88
HL
1881
1882 *written = 0;
1883
8b7be3aa
HL
1884 if (len == 0)
1885 return 1;
22d53c88 1886
8b7be3aa
HL
1887 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
1888 return 0;
a8489257 1889
cb5c208b 1890 partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
072328dd 1891
23c04709 1892 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
faa3a180 1893 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
a8489257
HL
1894 goto out;
1895 }
22d53c88 1896
ca41f6b7
HL
1897 /*
1898 * If we haven't finished the handshake, try to advance it.
1899 * We don't accept writes until the handshake is completed.
1900 */
faa3a180 1901 if (quic_do_handshake(&ctx) < 1) {
a8489257
HL
1902 ret = 0;
1903 goto out;
1904 }
ca41f6b7 1905
cb5c208b 1906 if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) {
faa3a180 1907 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
a8489257
HL
1908 goto out;
1909 }
22d53c88 1910
cb5c208b 1911 if (xso_blocking_mode(ctx.xso))
faa3a180 1912 ret = quic_write_blocking(&ctx, buf, len, written);
22d53c88 1913 else if (partial_write)
faa3a180 1914 ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
22d53c88 1915 else
faa3a180 1916 ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
a8489257
HL
1917
1918out:
072328dd 1919 quic_unlock(ctx.qc);
a8489257 1920 return ret;
d5ab48a1
RL
1921}
1922
1923/*
22d53c88
HL
1924 * SSL_read
1925 * --------
d5ab48a1 1926 */
22d53c88 1927struct quic_read_again_args {
faa3a180 1928 QCTX *ctx;
22d53c88
HL
1929 QUIC_STREAM *stream;
1930 void *buf;
1931 size_t len;
1932 size_t *bytes_read;
1933 int peek;
1934};
1935
4847599b 1936QUIC_NEEDS_LOCK
faa3a180 1937static int quic_read_actual(QCTX *ctx,
22d53c88
HL
1938 QUIC_STREAM *stream,
1939 void *buf, size_t buf_len,
1940 size_t *bytes_read,
1941 int peek)
d5ab48a1 1942{
22d53c88 1943 int is_fin = 0;
faa3a180 1944 QUIC_CONNECTION *qc = ctx->qc;
22d53c88 1945
a9979965
HL
1946 /* If the receive part of the stream is over, issue EOF. */
1947 if (stream->recv_fin_retired)
faa3a180 1948 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
a9979965 1949
22d53c88 1950 if (stream->rstream == NULL)
faa3a180 1951 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1952
1953 if (peek) {
1954 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1955 bytes_read, &is_fin))
faa3a180 1956 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1957
1958 } else {
1959 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1960 bytes_read, &is_fin))
faa3a180 1961 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1962 }
1963
1964 if (!peek) {
1965 if (*bytes_read > 0) {
1966 /*
1967 * We have read at least one byte from the stream. Inform stream-level
1968 * RXFC of the retirement of controlled bytes. Update the active stream
1969 * status (the RXFC may now want to emit a frame granting more credit to
1970 * the peer).
1971 */
1972 OSSL_RTT_INFO rtt_info;
d50e750e 1973
22d53c88
HL
1974 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1975
cb5c208b 1976 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
22d53c88 1977 rtt_info.smoothed_rtt))
faa3a180 1978 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
22d53c88
HL
1979 }
1980
1981 if (is_fin)
1982 stream->recv_fin_retired = 1;
1983
1984 if (*bytes_read > 0)
1985 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
cb5c208b 1986 stream);
22d53c88
HL
1987 }
1988
d5ab48a1
RL
1989 return 1;
1990}
1991
4847599b 1992QUIC_NEEDS_LOCK
22d53c88 1993static int quic_read_again(void *arg)
d5ab48a1 1994{
22d53c88
HL
1995 struct quic_read_again_args *args = arg;
1996
faa3a180 1997 if (!ossl_quic_channel_is_active(args->ctx->qc->ch)) {
22d53c88 1998 /* If connection is torn down due to an error while blocking, stop. */
faa3a180 1999 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
ca41f6b7 2000 return -1;
a9979965 2001 }
22d53c88 2002
faa3a180 2003 if (!quic_read_actual(args->ctx, args->stream,
22d53c88
HL
2004 args->buf, args->len, args->bytes_read,
2005 args->peek))
2006 return -1;
2007
2008 if (*args->bytes_read > 0)
2009 /* got at least one byte, the SSL_read op can finish now */
2010 return 1;
2011
81b6b43c 2012 return 0; /* did not read anything, keep trying */
d5ab48a1
RL
2013}
2014
a8489257 2015QUIC_TAKES_LOCK
22d53c88 2016static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
d5ab48a1 2017{
a8489257 2018 int ret, res;
072328dd 2019 QCTX ctx;
22d53c88
HL
2020 struct quic_read_again_args args;
2021
2022 *bytes_read = 0;
2023
8b7be3aa 2024 if (!expect_quic(s, &ctx))
22d53c88
HL
2025 return 0;
2026
072328dd 2027 quic_lock(ctx.qc);
a8489257 2028
23c04709 2029 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
faa3a180 2030 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
a8489257
HL
2031 goto out;
2032 }
22d53c88 2033
a9979965 2034 /* If we haven't finished the handshake, try to advance it. */
faa3a180 2035 if (quic_do_handshake(&ctx) < 1) {
a8489257
HL
2036 ret = 0; /* ossl_quic_do_handshake raised error here */
2037 goto out;
2038 }
22d53c88 2039
8b7be3aa
HL
2040 if (ctx.xso == NULL) {
2041 /*
2042 * Called on a QCSO and we don't currently have a default stream.
2043 *
2044 * Wait until we get a stream initiated by the peer (blocking mode) or
2045 * fail if we don't have one yet (non-blocking mode).
2046 */
faa3a180 2047 if (!qc_wait_for_default_xso_for_read(&ctx)) {
8b7be3aa
HL
2048 ret = 0; /* error already raised here */
2049 goto out;
2050 }
2051
2052 ctx.xso = ctx.qc->default_xso;
2053 }
2054
cb5c208b 2055 if (ctx.xso->stream == NULL) {
faa3a180 2056 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
a8489257
HL
2057 goto out;
2058 }
22d53c88 2059
faa3a180 2060 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
a8489257
HL
2061 ret = 0; /* quic_read_actual raised error here */
2062 goto out;
2063 }
22d53c88
HL
2064
2065 if (*bytes_read > 0) {
2066 /*
2067 * Even though we succeeded, tick the reactor here to ensure we are
2068 * handling other aspects of the QUIC connection.
2069 */
072328dd 2070 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
a8489257 2071 ret = 1;
cb5c208b 2072 } else if (xso_blocking_mode(ctx.xso)) {
22d53c88
HL
2073 /*
2074 * We were not able to read anything immediately, so our stream
2075 * buffer is empty. This means we need to block until we get
2076 * at least one byte.
2077 */
faa3a180 2078 args.ctx = &ctx;
cb5c208b 2079 args.stream = ctx.xso->stream;
22d53c88
HL
2080 args.buf = buf;
2081 args.len = len;
2082 args.bytes_read = bytes_read;
2083 args.peek = peek;
2084
072328dd 2085 res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
a8489257 2086 if (res == 0) {
faa3a180 2087 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
a8489257
HL
2088 goto out;
2089 } else if (res < 0) {
2090 ret = 0; /* quic_read_again raised error here */
2091 goto out;
2092 }
22d53c88 2093
a8489257 2094 ret = 1;
af8b52cf
HL
2095 } else {
2096 /* We did not get any bytes and are not in blocking mode. */
faa3a180 2097 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
af8b52cf 2098 }
a8489257
HL
2099
2100out:
072328dd 2101 quic_unlock(ctx.qc);
a8489257 2102 return ret;
d5ab48a1
RL
2103}
2104
22d53c88
HL
2105int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2106{
2107 return quic_read(s, buf, len, bytes_read, 0);
2108}
2109
2110int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2111{
2112 return quic_read(s, buf, len, bytes_read, 1);
2113}
2114
2115/*
2116 * SSL_pending
2117 * -----------
2118 */
a8489257 2119QUIC_TAKES_LOCK
072328dd 2120static size_t ossl_quic_pending_int(const SSL *s)
22d53c88 2121{
072328dd 2122 QCTX ctx;
433d107a 2123 size_t avail = 0;
22d53c88
HL
2124 int fin = 0;
2125
8b7be3aa 2126 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
22d53c88
HL
2127 return 0;
2128
cb5c208b 2129 if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL)
22d53c88 2130 /* Cannot raise errors here because we are const, just fail. */
a8489257 2131 goto out;
22d53c88 2132
cb5c208b 2133 if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin))
a8489257 2134 avail = 0;
22d53c88 2135
a8489257 2136out:
072328dd 2137 quic_unlock(ctx.qc);
22d53c88
HL
2138 return avail;
2139}
2140
560470b5
MC
2141size_t ossl_quic_pending(const SSL *s)
2142{
072328dd 2143 return ossl_quic_pending_int(s);
560470b5
MC
2144}
2145
072328dd 2146int ossl_quic_has_pending(const SSL *s)
560470b5 2147{
072328dd 2148 return ossl_quic_pending_int(s) > 0;
560470b5
MC
2149}
2150
a9979965
HL
2151/*
2152 * SSL_stream_conclude
2153 * -------------------
2154 */
a8489257 2155QUIC_TAKES_LOCK
072328dd 2156int ossl_quic_conn_stream_conclude(SSL *s)
a9979965 2157{
072328dd
HL
2158 QCTX ctx;
2159 QUIC_STREAM *qs;
2160
8b7be3aa 2161 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, &ctx))
072328dd
HL
2162 return 0;
2163
cb5c208b 2164 qs = ctx.xso->stream;
a9979965 2165
a8489257 2166 if (qs == NULL || qs->sstream == NULL) {
072328dd 2167 quic_unlock(ctx.qc);
a8489257
HL
2168 return 0;
2169 }
2170
072328dd 2171 if (!ossl_quic_channel_is_active(ctx.qc->ch)
a8489257 2172 || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
072328dd 2173 quic_unlock(ctx.qc);
a9979965 2174 return 1;
a8489257 2175 }
a9979965
HL
2176
2177 ossl_quic_sstream_fin(qs->sstream);
cb5c208b 2178 quic_post_write(ctx.xso, 1, 1);
072328dd 2179 quic_unlock(ctx.qc);
a9979965
HL
2180 return 1;
2181}
2182
553a4e00
HL
2183/*
2184 * SSL_inject_net_dgram
2185 * --------------------
2186 */
5129e594 2187QUIC_TAKES_LOCK
553a4e00
HL
2188int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2189 size_t buf_len,
2190 const BIO_ADDR *peer,
2191 const BIO_ADDR *local)
2192{
5129e594 2193 int ret;
072328dd 2194 QCTX ctx;
553a4e00
HL
2195 QUIC_DEMUX *demux;
2196
072328dd 2197 if (!expect_quic(s, &ctx))
553a4e00
HL
2198 return 0;
2199
072328dd 2200 quic_lock(ctx.qc);
5129e594 2201
072328dd 2202 demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
5129e594
HL
2203 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2204
072328dd 2205 quic_unlock(ctx.qc);
5129e594 2206 return ret;
553a4e00
HL
2207}
2208
020d0389
HL
2209/*
2210 * SSL_get0_connection
2211 * -------------------
2212 */
2213SSL *ossl_quic_get0_connection(SSL *s)
2214{
2215 QCTX ctx;
2216
2217 if (!expect_quic(s, &ctx))
2218 return NULL;
2219
2220 return &ctx.qc->ssl;
2221}
2222
1bca3f1b
HL
2223/*
2224 * SSL_get_stream_type
2225 * -------------------
2226 */
2227int ossl_quic_get_stream_type(SSL *s)
2228{
2229 QCTX ctx;
2230
2231 if (!expect_quic(s, &ctx))
59c5c016 2232 return SSL_STREAM_TYPE_BIDI;
1bca3f1b
HL
2233
2234 if (ctx.xso == NULL) {
2235 /*
59c5c016
HL
2236 * If deferred XSO creation has yet to occur, proceed according to the
2237 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2238 * what kind of stream will be created yet, so return BIDI on the basis
2239 * that at this time, the client still has the option of calling
2240 * SSL_read() or SSL_write() first.
1bca3f1b 2241 */
59c5c016
HL
2242 if (ctx.qc->default_xso_created
2243 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1bca3f1b 2244 return SSL_STREAM_TYPE_NONE;
59c5c016
HL
2245 else
2246 return SSL_STREAM_TYPE_BIDI;
1bca3f1b
HL
2247 }
2248
2249 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2250 return SSL_STREAM_TYPE_BIDI;
2251
2252 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2253 return SSL_STREAM_TYPE_READ;
2254 else
2255 return SSL_STREAM_TYPE_WRITE;
2256}
2257
19cb0887
HL
2258/*
2259 * SSL_get_stream_id
2260 * -----------------
2261 */
cb68ce9f 2262QUIC_TAKES_LOCK
19cb0887
HL
2263uint64_t ossl_quic_get_stream_id(SSL *s)
2264{
2265 QCTX ctx;
8b7be3aa 2266 uint64_t id;
19cb0887 2267
8b7be3aa 2268 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, &ctx))
19cb0887
HL
2269 return UINT64_MAX;
2270
8b7be3aa
HL
2271 id = ctx.xso->stream->id;
2272 quic_unlock(ctx.qc);
2273
2274 return id;
2275}
2276
2277/*
2278 * SSL_set_default_stream_mode
2279 * ---------------------------
2280 */
cb68ce9f 2281QUIC_TAKES_LOCK
8b7be3aa
HL
2282int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
2283{
2284 QCTX ctx;
2285
2286 if (!expect_quic_conn_only(s, &ctx))
2287 return 0;
2288
2289 quic_lock(ctx.qc);
2290
2291 if (ctx.qc->default_xso_created)
faa3a180 2292 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
8b7be3aa
HL
2293 "too late to change default stream mode");
2294
2295 switch (mode) {
2296 case SSL_DEFAULT_STREAM_MODE_NONE:
2297 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
2298 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
2299 ctx.qc->default_stream_mode = mode;
2300 break;
2301 default:
2302 quic_unlock(ctx.qc);
faa3a180 2303 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
8b7be3aa
HL
2304 "bad default stream type");
2305 }
2306
2307 quic_unlock(ctx.qc);
2308 return 1;
2309}
2310
2311/*
2312 * SSL_detach_stream
2313 * -----------------
2314 */
cb68ce9f 2315QUIC_TAKES_LOCK
8b7be3aa
HL
2316SSL *ossl_quic_detach_stream(SSL *s)
2317{
2318 QCTX ctx;
9cab4bd5 2319 QUIC_XSO *xso = NULL;
8b7be3aa
HL
2320
2321 if (!expect_quic_conn_only(s, &ctx))
2322 return NULL;
2323
2324 quic_lock(ctx.qc);
2325
8b7be3aa 2326 /* Calling this function inhibits default XSO autocreation. */
9cab4bd5
HL
2327 /* QC ref to any default XSO is transferred to us and to caller. */
2328 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
8b7be3aa
HL
2329
2330 quic_unlock(ctx.qc);
2331
9cab4bd5 2332 return xso != NULL ? &xso->ssl : NULL;
8b7be3aa
HL
2333}
2334
2335/*
2336 * SSL_attach_stream
2337 * -----------------
2338 */
cb68ce9f 2339QUIC_TAKES_LOCK
8b7be3aa
HL
2340int ossl_quic_attach_stream(SSL *conn, SSL *stream)
2341{
2342 QCTX ctx;
9cab4bd5
HL
2343 QUIC_XSO *xso;
2344 int nref;
8b7be3aa
HL
2345
2346 if (!expect_quic_conn_only(conn, &ctx))
2347 return 0;
2348
2349 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
faa3a180 2350 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
8b7be3aa
HL
2351 "stream to attach must be a valid QUIC stream");
2352
9cab4bd5
HL
2353 xso = (QUIC_XSO *)stream;
2354
8b7be3aa
HL
2355 quic_lock(ctx.qc);
2356
2357 if (ctx.qc->default_xso != NULL) {
2358 quic_unlock(ctx.qc);
faa3a180 2359 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
8b7be3aa
HL
2360 "connection already has a default stream");
2361 }
2362
9cab4bd5
HL
2363 /*
2364 * It is a caller error for the XSO being attached as a default XSO to have
2365 * more than one ref.
2366 */
4eecc6aa 2367 if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
9cab4bd5 2368 quic_unlock(ctx.qc);
faa3a180 2369 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
9cab4bd5
HL
2370 "ref");
2371 }
2372
2373 if (nref != 1) {
2374 quic_unlock(ctx.qc);
faa3a180 2375 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
9cab4bd5
HL
2376 "stream being attached must have "
2377 "only 1 reference");
2378 }
2379
2380 /* Caller's reference to the XSO is transferred to us. */
8b7be3aa 2381 /* Calling this function inhibits default XSO autocreation. */
9cab4bd5 2382 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
8b7be3aa
HL
2383
2384 quic_unlock(ctx.qc);
2385 return 1;
19cb0887
HL
2386}
2387
8a90df34 2388/*
83df44ae
HL
2389 * SSL_set_incoming_stream_policy
2390 * ------------------------------
8a90df34 2391 */
995ff282 2392QUIC_NEEDS_LOCK
83df44ae 2393static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
995ff282 2394{
83df44ae
HL
2395 switch (qc->incoming_stream_policy) {
2396 case SSL_INCOMING_STREAM_POLICY_AUTO:
995ff282
HL
2397 if ((qc->default_xso == NULL && !qc->default_xso_created)
2398 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
83df44ae 2399 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
995ff282 2400 else
83df44ae 2401 return SSL_INCOMING_STREAM_POLICY_REJECT;
995ff282
HL
2402
2403 default:
83df44ae 2404 return qc->incoming_stream_policy;
995ff282
HL
2405 }
2406}
2407
2408QUIC_NEEDS_LOCK
2409static void qc_update_reject_policy(QUIC_CONNECTION *qc)
2410{
83df44ae
HL
2411 int policy = qc_get_effective_incoming_stream_policy(qc);
2412 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
995ff282
HL
2413
2414 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
2415 enable_reject,
83df44ae 2416 qc->incoming_stream_aec);
995ff282
HL
2417}
2418
cb68ce9f 2419QUIC_TAKES_LOCK
83df44ae
HL
2420int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
2421 uint64_t aec)
8a90df34
HL
2422{
2423 int ret = 1;
2424 QCTX ctx;
2425
2426 if (!expect_quic_conn_only(s, &ctx))
2427 return 0;
2428
2429 quic_lock(ctx.qc);
2430
2431 switch (policy) {
83df44ae
HL
2432 case SSL_INCOMING_STREAM_POLICY_AUTO:
2433 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
2434 case SSL_INCOMING_STREAM_POLICY_REJECT:
2435 ctx.qc->incoming_stream_policy = policy;
2436 ctx.qc->incoming_stream_aec = aec;
8a90df34
HL
2437 break;
2438
2439 default:
2440 ret = 0;
2441 break;
2442 }
2443
995ff282 2444 qc_update_reject_policy(ctx.qc);
8a90df34
HL
2445 quic_unlock(ctx.qc);
2446 return ret;
2447}
2448
cb68ce9f
HL
2449/*
2450 * SSL_accept_stream
2451 * -----------------
2452 */
cb68ce9f 2453struct wait_for_incoming_stream_args {
faa3a180 2454 QCTX *ctx;
cb68ce9f
HL
2455 QUIC_STREAM *qs;
2456};
2457
2458QUIC_NEEDS_LOCK
2459static int wait_for_incoming_stream(void *arg)
2460{
2461 struct wait_for_incoming_stream_args *args = arg;
faa3a180
HL
2462 QUIC_CONNECTION *qc = args->ctx->qc;
2463 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
cb68ce9f 2464
faa3a180 2465 if (!ossl_quic_channel_is_active(qc->ch)) {
cb68ce9f 2466 /* If connection is torn down due to an error while blocking, stop. */
faa3a180 2467 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
cb68ce9f
HL
2468 return -1;
2469 }
2470
2471 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2472 if (args->qs != NULL)
2473 return 1; /* got a stream */
2474
2475 return 0; /* did not get a stream, keep trying */
2476}
2477
2478QUIC_TAKES_LOCK
2479SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
2480{
2481 QCTX ctx;
2482 int ret;
2483 SSL *new_s = NULL;
2484 QUIC_STREAM_MAP *qsm;
2485 QUIC_STREAM *qs;
2486 QUIC_XSO *xso;
90cecc40 2487 OSSL_RTT_INFO rtt_info;
cb68ce9f
HL
2488
2489 if (!expect_quic_conn_only(s, &ctx))
2490 return NULL;
2491
2492 quic_lock(ctx.qc);
2493
83df44ae
HL
2494 if (qc_get_effective_incoming_stream_policy(ctx.qc)
2495 == SSL_INCOMING_STREAM_POLICY_REJECT)
cb68ce9f
HL
2496 goto out;
2497
2498 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2499
2500 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
2501 if (qs == NULL) {
2502 if (qc_blocking_mode(ctx.qc)
2503 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
2504 struct wait_for_incoming_stream_args args;
2505
faa3a180 2506 args.ctx = &ctx;
cb68ce9f
HL
2507 args.qs = NULL;
2508
2509 ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
2510 if (ret == 0) {
faa3a180 2511 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
cb68ce9f
HL
2512 goto out;
2513 } else if (ret < 0 || args.qs == NULL) {
2514 goto out;
2515 }
2516
2517 qs = args.qs;
2518 } else {
2519 goto out;
2520 }
2521 }
2522
2523 xso = create_xso_from_stream(ctx.qc, qs);
2524 if (xso == NULL)
2525 goto out;
2526
90cecc40
HL
2527 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
2528 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
2529 rtt_info.smoothed_rtt);
cb68ce9f
HL
2530 new_s = &xso->ssl;
2531
2532 /* Calling this function inhibits default XSO autocreation. */
995ff282 2533 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
cb68ce9f
HL
2534
2535out:
2536 quic_unlock(ctx.qc);
2537 return new_s;
2538}
2539
2540/*
2541 * SSL_get_accept_stream_queue_len
2542 * -------------------------------
2543 */
2544QUIC_TAKES_LOCK
2545size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
2546{
2547 QCTX ctx;
2548 size_t v;
2549
2550 if (!expect_quic_conn_only(s, &ctx))
2551 return 0;
2552
2553 quic_lock(ctx.qc);
2554
2555 v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
2556
2557 quic_unlock(ctx.qc);
2558 return v;
2559}
2560
c3a04ea2
HL
2561/*
2562 * SSL_stream_reset
2563 * ----------------
2564 */
2565int ossl_quic_stream_reset(SSL *ssl,
2566 const SSL_STREAM_RESET_ARGS *args,
2567 size_t args_len)
2568{
2569 QCTX ctx;
2570 QUIC_STREAM_MAP *qsm;
2571 QUIC_STREAM *qs;
2572 uint64_t error_code;
2573
2574 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, &ctx))
2575 return 0;
2576
2577 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
2578 qs = ctx.xso->stream;
2579 error_code = (args != NULL ? args->quic_error_code : 0);
2580
2581 ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
2582
2583 quic_unlock(ctx.qc);
2584 return 1;
2585}
2586
2587/*
2588 * SSL_get_stream_read_state
2589 * -------------------------
2590 */
2591static void quic_classify_stream(QUIC_CONNECTION *qc,
2592 QUIC_STREAM *qs,
2593 int is_write,
2594 int *state,
2595 uint64_t *app_error_code)
2596{
2597 int local_init;
2598 uint64_t final_size;
2599
2600 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
2601
2602 if (app_error_code != NULL)
2603 *app_error_code = UINT64_MAX;
2604 else
2605 app_error_code = &final_size; /* throw away value */
2606
2607 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
2608 /*
2609 * Unidirectional stream and this direction of transmission doesn't
2610 * exist.
2611 */
2612 *state = SSL_STREAM_STATE_WRONG_DIR;
2613 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
2614 /* Connection already closed. */
2615 *state = SSL_STREAM_STATE_CONN_CLOSED;
2616 } else if (!is_write && qs->recv_fin_retired) {
2617 /* Application has read a FIN. */
2618 *state = SSL_STREAM_STATE_FINISHED;
2619 } else if ((!is_write && qs->stop_sending)
2620 || (is_write && qs->reset_stream)) {
2621 /*
2622 * Stream has been reset locally. FIN takes precedence over this for the
2623 * read case as the application need not care if the stream is reset
2624 * after a FIN has been successfully processed.
2625 */
2626 *state = SSL_STREAM_STATE_RESET_LOCAL;
2627 *app_error_code = !is_write
2628 ? qs->stop_sending_aec
2629 : qs->reset_stream_aec;
2630 } else if ((!is_write && qs->peer_reset_stream)
2631 || (is_write && qs->peer_stop_sending)) {
2632 /*
2633 * Stream has been reset remotely. */
2634 *state = SSL_STREAM_STATE_RESET_REMOTE;
2635 *app_error_code = !is_write
2636 ? qs->peer_reset_stream_aec
2637 : qs->peer_stop_sending_aec;
2638 } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
2639 &final_size)) {
2640 /*
2641 * Stream has been finished. Stream reset takes precedence over this for
2642 * the write case as peer may not have received all data.
2643 */
2644 *state = SSL_STREAM_STATE_FINISHED;
2645 } else {
2646 /* Stream still healthy. */
2647 *state = SSL_STREAM_STATE_OK;
2648 }
2649}
2650
2651static int quic_get_stream_state(SSL *ssl, int is_write)
2652{
2653 QCTX ctx;
2654 int state;
2655
2656 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2657 return SSL_STREAM_STATE_NONE;
2658
2659 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
2660 quic_unlock(ctx.qc);
2661 return state;
2662}
2663
2664int ossl_quic_get_stream_read_state(SSL *ssl)
2665{
2666 return quic_get_stream_state(ssl, /*is_write=*/0);
2667}
2668
2669/*
2670 * SSL_get_stream_write_state
2671 * --------------------------
2672 */
2673int ossl_quic_get_stream_write_state(SSL *ssl)
2674{
2675 return quic_get_stream_state(ssl, /*is_write=*/1);
2676}
2677
2678/*
2679 * SSL_get_stream_read_error_code
2680 * ------------------------------
2681 */
2682static int quic_get_stream_error_code(SSL *ssl, int is_write,
2683 uint64_t *app_error_code)
2684{
2685 QCTX ctx;
2686 int state;
2687
2688 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2689 return -1;
2690
2691 quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
2692 &state, app_error_code);
2693
2694 quic_unlock(ctx.qc);
2695 switch (state) {
2696 case SSL_STREAM_STATE_FINISHED:
2697 return 0;
2698 case SSL_STREAM_STATE_RESET_LOCAL:
2699 case SSL_STREAM_STATE_RESET_REMOTE:
2700 return 1;
2701 default:
2702 return -1;
2703 }
2704}
2705
2706int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
2707{
2708 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
2709}
2710
2711/*
2712 * SSL_get_stream_write_error_code
2713 * -------------------------------
2714 */
2715int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
2716{
2717 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
2718}
2719
2720/*
2721 * SSL_get_conn_close_info
2722 * -----------------------
2723 */
2724int ossl_quic_get_conn_close_info(SSL *ssl,
2725 SSL_CONN_CLOSE_INFO *info,
2726 size_t info_len)
2727{
2728 QCTX ctx;
2729 const QUIC_TERMINATE_CAUSE *tc;
2730
2731 if (!expect_quic_conn_only(ssl, &ctx))
2732 return -1;
2733
2734 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
2735 if (tc == NULL)
2736 return 0;
2737
2738 info->error_code = tc->error_code;
2739 info->reason = NULL; /* TODO(QUIC): Wire reason */
2740 info->reason_len = 0;
2741 info->is_local = !tc->remote;
2742 info->is_transport = !tc->app;
2743 return 1;
2744}
2745
2525109f
HL
2746/*
2747 * SSL_key_update
2748 * --------------
2749 */
2750int ossl_quic_key_update(SSL *ssl, int update_type)
2751{
2752 QCTX ctx;
2753
2754 if (!expect_quic_conn_only(ssl, &ctx))
2755 return 0;
2756
2757 switch (update_type) {
2758 case SSL_KEY_UPDATE_NOT_REQUESTED:
2759 /*
2760 * QUIC signals peer key update implicily by triggering a local
2761 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
2762 */
2763 case SSL_KEY_UPDATE_REQUESTED:
2764 break;
2765
2766 default:
2767 /* Unknown type - error. */
2768 return 0;
2769 }
2770
2771 quic_lock(ctx.qc);
2772
2773 /* Attempt to perform a TXKU. */
2774 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
2775 quic_unlock(ctx.qc);
2776 return 0;
2777 }
2778
2779 quic_unlock(ctx.qc);
2780 return 1;
2781}
2782
2783/*
2784 * SSL_get_key_update_type
2785 * -----------------------
2786 */
2787int ossl_quic_get_key_update_type(const SSL *s)
2788{
2789 /*
2790 * We always handle key updates immediately so a key update is never
2791 * pending.
2792 */
2793 return SSL_KEY_UPDATE_NONE;
2794}
2795
22d53c88
HL
2796/*
2797 * QUIC Front-End I/O API: SSL_CTX Management
2798 * ==========================================
2799 */
2800
2801long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2802{
2803 switch (cmd) {
2804 default:
8a1a6d6d 2805 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
22d53c88
HL
2806 }
2807}
2808
2809long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
2810{
63dfde87
MC
2811 QCTX ctx;
2812
2813 if (!expect_quic_conn_only(s, &ctx))
2814 return 0;
2815
2816 switch (cmd) {
2817 case SSL_CTRL_SET_MSG_CALLBACK:
5cf99b40
MC
2818 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
2819 &ctx.qc->ssl);
63dfde87
MC
2820 /* This callback also needs to be set on the internal SSL object */
2821 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
2822
2823 default:
2824 /* Probably a TLS related ctrl. Defer to our internal SSL object */
2825 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
2826 }
22d53c88
HL
2827}
2828
2829long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
2830{
8a1a6d6d 2831 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
22d53c88
HL
2832}
2833
2834int ossl_quic_renegotiate_check(SSL *ssl, int initok)
2835{
2836 /* We never do renegotiation. */
2837 return 0;
2838}
2839
2840/*
d518854c
MC
2841 * These functions define the TLSv1.2 (and below) ciphers that are supported by
2842 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
22d53c88 2843 */
22d53c88
HL
2844
2845int ossl_quic_num_ciphers(void)
2846{
d518854c 2847 return 0;
22d53c88
HL
2848}
2849
2850const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
2851{
d518854c 2852 return NULL;
d5ab48a1 2853}
16f3b542 2854
a02571a0
TM
2855int ossl_quic_set_ssl_op(SSL *ssl, uint64_t op)
2856{
2857 QCTX ctx;
6ba2edb7 2858 int cleanse;
a02571a0
TM
2859
2860 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, &ctx))
2861 return 0;
2862
6ba2edb7 2863 if (ctx.xso->stream == NULL)
a02571a0
TM
2864 goto out;
2865
6ba2edb7
TM
2866 cleanse = (op & SSL_OP_CLEANSE_PLAINTEXT) != 0;
2867 if (ctx.xso->stream->rstream != NULL)
2868 ossl_quic_rstream_set_cleanse(ctx.xso->stream->rstream, cleanse);
2869 if (ctx.xso->stream->sstream != NULL)
2870 ossl_quic_sstream_set_cleanse(ctx.xso->stream->sstream, cleanse);
a02571a0
TM
2871
2872 out:
2873 quic_unlock(ctx.qc);
2874 return 1;
2875}
2876
16f3b542
HL
2877/*
2878 * Internal Testing APIs
2879 * =====================
2880 */
2881
2882QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
2883{
2884 QCTX ctx;
2885
2886 if (!expect_quic_conn_only(s, &ctx))
2887 return NULL;
2888
2889 return ctx.qc->ch;
2890}