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