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