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