1 /*
2 * Copyright 2022-2026 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/hashfunc.h"
16 #include "internal/ssl_unwrap.h"
17 #include "internal/quic_tls.h"
18 #include "internal/quic_rx_depack.h"
19 #include "internal/quic_error.h"
20 #include "internal/quic_engine.h"
21 #include "internal/quic_port.h"
22 #include "internal/quic_reactor_wait_ctx.h"
23 #include "internal/time.h"
24
25 typedef struct qctx_st QCTX;
26
27 static void qc_cleanup(QUIC_CONNECTION *qc, int have_lock);
28 static void aon_write_finish(QUIC_XSO *xso);
29 static int create_channel(QUIC_CONNECTION *qc, SSL_CTX *ctx);
30 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
31 static QUIC_CONNECTION *create_qc_from_incoming_conn(QUIC_LISTENER *ql, QUIC_CHANNEL *ch);
32 static int qc_try_create_default_xso_for_write(QCTX *ctx);
33 static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek);
34 static void qctx_lock(QCTX *qctx);
35 static void qctx_unlock(QCTX *qctx);
36 static void qctx_lock_for_io(QCTX *ctx);
37 static int quic_do_handshake(QCTX *ctx);
38 static void qc_update_reject_policy(QUIC_CONNECTION *qc);
39 static void qc_touch_default_xso(QUIC_CONNECTION *qc);
40 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
41 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
42 int touch, QUIC_XSO **old_xso);
43 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
44 static int quic_validate_for_write(QUIC_XSO *xso, int *err);
45 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
46 static void qctx_maybe_autotick(QCTX *ctx);
47 static int qctx_should_autotick(QCTX *ctx);
48
49 /*
50 * QCTX is a utility structure which provides information we commonly wish to
51 * unwrap upon an API call being dispatched to us, namely:
52 *
53 * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
54 * was passed);
55 * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
56 * a QCSO with a default stream was passed);
57 * - whether a QSSO was passed (xso == NULL must not be used to determine this
58 * because it may be non-NULL when a QCSO is passed if that QCSO has a
59 * default stream);
60 * - a pointer to a QUIC_LISTENER object, if one is relevant;
61 * - whether we are in "I/O context", meaning that non-normal errors can
62 * be reported via SSL_get_error() as well as via ERR. Functions such as
63 * SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
64 * functions which are allowed to change the value returned by
65 * SSL_get_error. However, other functions (including functions which call
66 * SSL_do_handshake() implicitly) are not allowed to change the return value
67 * of SSL_get_error.
68 */
69 struct qctx_st {
70 QUIC_OBJ *obj;
71 QUIC_DOMAIN *qd;
72 QUIC_LISTENER *ql;
73 QUIC_CONNECTION *qc;
74 QUIC_XSO *xso;
75 int is_stream, is_listener, is_domain, in_io;
76 };
77
78 QUIC_NEEDS_LOCK
quic_set_last_error(QCTX * ctx,int last_error)79 static void quic_set_last_error(QCTX *ctx, int last_error)
80 {
81 if (!ctx->in_io)
82 return;
83
84 if (ctx->is_stream && ctx->xso != NULL)
85 ctx->xso->last_error = last_error;
86 else if (!ctx->is_stream && ctx->qc != NULL)
87 ctx->qc->last_error = last_error;
88 }
89
90 /*
91 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
92 * rather than via ERR. Note that normal errors must always be raised while
93 * holding a lock.
94 */
95 QUIC_NEEDS_LOCK
quic_raise_normal_error(QCTX * ctx,int err)96 static int quic_raise_normal_error(QCTX *ctx,
97 int err)
98 {
99 assert(ctx->in_io);
100 quic_set_last_error(ctx, err);
101
102 return 0;
103 }
104
105 /*
106 * Raise a 'non-normal' error, meaning any error that is not reported via
107 * SSL_get_error() and must be reported via ERR.
108 *
109 * qc should be provided if available. In exceptional circumstances when qc is
110 * not known NULL may be passed. This should generally only happen when an
111 * expect_...() function defined below fails, which generally indicates a
112 * dispatch error or caller error.
113 *
114 * ctx should be NULL if the connection lock is not held.
115 */
quic_raise_non_normal_error(QCTX * ctx,const char * file,int line,const char * func,int reason,const char * fmt,...)116 static int quic_raise_non_normal_error(QCTX *ctx,
117 const char *file,
118 int line,
119 const char *func,
120 int reason,
121 const char *fmt,
122 ...)
123 {
124 va_list args;
125
126 if (ctx != NULL) {
127 quic_set_last_error(ctx, SSL_ERROR_SSL);
128
129 if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
130 ossl_quic_channel_restore_err_state(ctx->qc->ch);
131 }
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
140 return 0;
141 }
142
143 #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
144 quic_raise_normal_error((ctx), (err))
145
146 #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
147 quic_raise_non_normal_error((ctx), \
148 OPENSSL_FILE, OPENSSL_LINE, \
149 OPENSSL_FUNC, \
150 (reason), \
151 (msg))
152 /*
153 * Flags for expect_quic_as:
154 *
155 * QCTX_C
156 * The input SSL object may be a QCSO.
157 *
158 * QCTX_S
159 * The input SSL object may be a QSSO or a QCSO with a default stream
160 * attached.
161 *
162 * (Note this means there is no current way to require an SSL object with a
163 * QUIC stream which is not a QCSO; a QCSO with a default stream attached
164 * is always considered to satisfy QCTX_S.)
165 *
166 * QCTX_AUTO_S
167 * The input SSL object may be a QSSO or a QCSO with a default stream
168 * attached. If no default stream is currently attached to a QCSO,
169 * one may be auto-created if possible.
170 *
171 * If QCTX_REMOTE_INIT is set, an auto-created default XSO is
172 * initiated by the remote party (i.e., local party reads first).
173 *
174 * If it is not set, an auto-created default XSO is
175 * initiated by the local party (i.e., local party writes first).
176 *
177 * QCTX_L
178 * The input SSL object may be a QLSO.
179 *
180 * QCTX_LOCK
181 * If and only if the function returns successfully, the ctx
182 * is guaranteed to be locked.
183 *
184 * QCTX_IO
185 * Begin an I/O context. If not set, begins a non-I/O context.
186 * This determines whether SSL_get_error() is updated; the value it returns
187 * is modified only by an I/O call.
188 *
189 * QCTX_NO_ERROR
190 * Don't raise an error if the object type is wrong. Should not be used in
191 * conjunction with any flags that may raise errors not related to a wrong
192 * object type.
193 */
194 #define QCTX_C (1U << 0)
195 #define QCTX_S (1U << 1)
196 #define QCTX_L (1U << 2)
197 #define QCTX_AUTO_S (1U << 3)
198 #define QCTX_REMOTE_INIT (1U << 4)
199 #define QCTX_LOCK (1U << 5)
200 #define QCTX_IO (1U << 6)
201 #define QCTX_D (1U << 7)
202 #define QCTX_NO_ERROR (1U << 8)
203
204 /*
205 * Called when expect_quic failed. Used to diagnose why such a call failed and
206 * raise a reasonable error code based on the configured preconditions in flags.
207 */
wrong_type(const SSL * s,uint32_t flags)208 static int wrong_type(const SSL *s, uint32_t flags)
209 {
210 const uint32_t mask = QCTX_C | QCTX_S | QCTX_L | QCTX_D;
211 int code = ERR_R_UNSUPPORTED;
212
213 if ((flags & QCTX_NO_ERROR) != 0)
214 return 1;
215 else if ((flags & mask) == QCTX_D)
216 code = SSL_R_DOMAIN_USE_ONLY;
217 else if ((flags & mask) == QCTX_L)
218 code = SSL_R_LISTENER_USE_ONLY;
219 else if ((flags & mask) == QCTX_C)
220 code = SSL_R_CONN_USE_ONLY;
221 else if ((flags & mask) == QCTX_S
222 || (flags & mask) == (QCTX_C | QCTX_S))
223 code = SSL_R_NO_STREAM;
224
225 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, code, NULL);
226 }
227
228 /*
229 * Given a QDSO, QCSO, QSSO or QLSO, initialises a QCTX, determining the
230 * contextually applicable QUIC_LISTENER, QUIC_CONNECTION and QUIC_XSO
231 * pointers.
232 *
233 * After this returns 1, all fields of the passed QCTX are initialised.
234 * Returns 0 on failure. This function is intended to be used to provide API
235 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure
236 * unless the QCTX_NO_ERROR flag is set.
237 *
238 * The flags argument controls the preconditions and postconditions of this
239 * function. See above for the different flags.
240 *
241 * The fields of a QCTX are initialised as follows depending on the identity of
242 * the SSL object, and assuming the preconditions demanded by the flags field as
243 * described above are met:
244 *
245 * QDSO QLSO QCSO QSSO
246 * qd non-NULL maybe maybe maybe
247 * ql NULL non-NULL maybe maybe
248 * qc NULL NULL non-NULL non-NULL
249 * xso NULL NULL maybe non-NULL
250 * is_stream 0 0 0 1
251 * is_listener 0 1 0 0
252 * is_domain 1 0 0 0
253 *
254 */
expect_quic_as(const SSL * s,QCTX * ctx,uint32_t flags)255 static int expect_quic_as(const SSL *s, QCTX *ctx, uint32_t flags)
256 {
257 int ok = 0, locked = 0, lock_requested = ((flags & QCTX_LOCK) != 0);
258 QUIC_DOMAIN *qd;
259 QUIC_LISTENER *ql;
260 QUIC_CONNECTION *qc;
261 QUIC_XSO *xso;
262
263 if ((flags & QCTX_AUTO_S) != 0)
264 flags |= QCTX_S;
265
266 ctx->obj = NULL;
267 ctx->qd = NULL;
268 ctx->ql = NULL;
269 ctx->qc = NULL;
270 ctx->xso = NULL;
271 ctx->is_stream = 0;
272 ctx->is_listener = 0;
273 ctx->is_domain = 0;
274 ctx->in_io = ((flags & QCTX_IO) != 0);
275
276 if (s == NULL) {
277 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
278 goto err;
279 }
280
281 switch (s->type) {
282 case SSL_TYPE_QUIC_DOMAIN:
283 if ((flags & QCTX_D) == 0) {
284 wrong_type(s, flags);
285 goto err;
286 }
287
288 qd = (QUIC_DOMAIN *)s;
289 ctx->obj = &qd->obj;
290 ctx->qd = qd;
291 ctx->is_domain = 1;
292 break;
293
294 case SSL_TYPE_QUIC_LISTENER:
295 if ((flags & QCTX_L) == 0) {
296 wrong_type(s, flags);
297 goto err;
298 }
299
300 ql = (QUIC_LISTENER *)s;
301 ctx->obj = &ql->obj;
302 ctx->qd = ql->domain;
303 ctx->ql = ql;
304 ctx->is_listener = 1;
305 break;
306
307 case SSL_TYPE_QUIC_CONNECTION:
308 qc = (QUIC_CONNECTION *)s;
309 ctx->obj = &qc->obj;
310 ctx->qd = qc->domain;
311 ctx->ql = qc->listener; /* never changes, so can be read without lock */
312 ctx->qc = qc;
313
314 if ((flags & QCTX_AUTO_S) != 0) {
315 if ((flags & QCTX_IO) != 0)
316 qctx_lock_for_io(ctx);
317 else
318 qctx_lock(ctx);
319
320 locked = 1;
321 }
322
323 if ((flags & QCTX_AUTO_S) != 0 && qc->default_xso == NULL) {
324 if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
325 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
326 goto err;
327 }
328
329 /* If we haven't finished the handshake, try to advance it. */
330 if (quic_do_handshake(ctx) < 1)
331 /* ossl_quic_do_handshake raised error here */
332 goto err;
333
334 if ((flags & QCTX_REMOTE_INIT) != 0) {
335 if (!qc_wait_for_default_xso_for_read(ctx, /*peek=*/0))
336 goto err;
337 } else {
338 if (!qc_try_create_default_xso_for_write(ctx))
339 goto err;
340 }
341 }
342
343 if ((flags & QCTX_C) == 0
344 && (qc->default_xso == NULL || (flags & QCTX_S) == 0)) {
345 wrong_type(s, flags);
346 goto err;
347 }
348
349 ctx->xso = qc->default_xso;
350 break;
351
352 case SSL_TYPE_QUIC_XSO:
353 if ((flags & QCTX_S) == 0) {
354 wrong_type(s, flags);
355 goto err;
356 }
357
358 xso = (QUIC_XSO *)s;
359 ctx->obj = &xso->obj;
360 ctx->qd = xso->conn->domain;
361 ctx->ql = xso->conn->listener;
362 ctx->qc = xso->conn;
363 ctx->xso = xso;
364 ctx->is_stream = 1;
365 break;
366
367 default:
368 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
369 goto err;
370 }
371
372 if (lock_requested && !locked) {
373 if ((flags & QCTX_IO) != 0)
374 qctx_lock_for_io(ctx);
375 else
376 qctx_lock(ctx);
377
378 locked = 1;
379 }
380
381 ok = 1;
382 err:
383 if (locked && (!ok || !lock_requested))
384 qctx_unlock(ctx);
385
386 return ok;
387 }
388
is_quic_c(const SSL * s,QCTX * ctx,int raiseerrs)389 static int is_quic_c(const SSL *s, QCTX *ctx, int raiseerrs)
390 {
391 uint32_t flags = QCTX_C;
392
393 if (!raiseerrs)
394 flags |= QCTX_NO_ERROR;
395 return expect_quic_as(s, ctx, flags);
396 }
397
398 /* Same as expect_quic_cs except that errors are not raised if raiseerrs == 0 */
is_quic_cs(const SSL * s,QCTX * ctx,int raiseerrs)399 static int is_quic_cs(const SSL *s, QCTX *ctx, int raiseerrs)
400 {
401 uint32_t flags = QCTX_C | QCTX_S;
402
403 if (!raiseerrs)
404 flags |= QCTX_NO_ERROR;
405 return expect_quic_as(s, ctx, flags);
406 }
407
expect_quic_cs(const SSL * s,QCTX * ctx)408 static int expect_quic_cs(const SSL *s, QCTX *ctx)
409 {
410 return expect_quic_as(s, ctx, QCTX_C | QCTX_S);
411 }
412
expect_quic_cl(const SSL * s,QCTX * ctx)413 static int expect_quic_cl(const SSL *s, QCTX *ctx)
414 {
415 return expect_quic_as(s, ctx, QCTX_C | QCTX_L);
416 }
417
expect_quic_csl(const SSL * s,QCTX * ctx)418 static int expect_quic_csl(const SSL *s, QCTX *ctx)
419 {
420 return expect_quic_as(s, ctx, QCTX_C | QCTX_S | QCTX_L);
421 }
422
expect_quic_csld(const SSL * s,QCTX * ctx)423 static int expect_quic_csld(const SSL *s, QCTX *ctx)
424 {
425 return expect_quic_as(s, ctx, QCTX_C | QCTX_S | QCTX_L | QCTX_D);
426 }
427
428 #define expect_quic_any expect_quic_csld
429
expect_quic_listener(const SSL * s,QCTX * ctx)430 static int expect_quic_listener(const SSL *s, QCTX *ctx)
431 {
432 return expect_quic_as(s, ctx, QCTX_L);
433 }
434
expect_quic_domain(const SSL * s,QCTX * ctx)435 static int expect_quic_domain(const SSL *s, QCTX *ctx)
436 {
437 return expect_quic_as(s, ctx, QCTX_D);
438 }
439
440 /*
441 * Like expect_quic_cs(), but requires a QUIC_XSO be contextually available. In
442 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
443 * stream.
444 *
445 * remote_init determines if we expect the default XSO to be remotely created or
446 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
447 *
448 * Channel mutex is acquired and retained on success.
449 */
450 QUIC_ACQUIRES_LOCK
expect_quic_with_stream_lock(const SSL * s,int remote_init,int in_io,QCTX * ctx)451 static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
452 int in_io, QCTX *ctx)
453 {
454 uint32_t flags = QCTX_S | QCTX_LOCK;
455
456 if (remote_init >= 0)
457 flags |= QCTX_AUTO_S;
458
459 if (remote_init > 0)
460 flags |= QCTX_REMOTE_INIT;
461
462 if (in_io)
463 flags |= QCTX_IO;
464
465 return expect_quic_as(s, ctx, flags);
466 }
467
468 /*
469 * Like expect_quic_cs(), but fails if called on a QUIC_XSO. ctx->xso may still
470 * be non-NULL if the QCSO has a default stream.
471 */
expect_quic_conn_only(const SSL * s,QCTX * ctx)472 static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
473 {
474 return expect_quic_as(s, ctx, QCTX_C);
475 }
476
477 /*
478 * Ensures that the domain mutex is held for a method which touches channel
479 * state.
480 *
481 * Precondition: Domain mutex is not held (unchecked)
482 */
qctx_lock(QCTX * ctx)483 static void qctx_lock(QCTX *ctx)
484 {
485 #if defined(OPENSSL_THREADS)
486 assert(ctx->obj != NULL);
487 ossl_crypto_mutex_lock(ossl_quic_obj_get0_mutex(ctx->obj));
488 #endif
489 }
490
491 /* Precondition: Channel mutex is held (unchecked) */
492 QUIC_NEEDS_LOCK
qctx_unlock(QCTX * ctx)493 static void qctx_unlock(QCTX *ctx)
494 {
495 #if defined(OPENSSL_THREADS)
496 assert(ctx->obj != NULL);
497 ossl_crypto_mutex_unlock(ossl_quic_obj_get0_mutex(ctx->obj));
498 #endif
499 }
500
qctx_lock_for_io(QCTX * ctx)501 static void qctx_lock_for_io(QCTX *ctx)
502 {
503 qctx_lock(ctx);
504 ctx->in_io = 1;
505
506 /*
507 * We are entering an I/O function so we must update the values returned by
508 * SSL_get_error and SSL_want. Set no error. This will be overridden later
509 * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR
510 * occurs during the API call.
511 */
512 quic_set_last_error(ctx, SSL_ERROR_NONE);
513 }
514
515 /*
516 * This predicate is the criterion which should determine API call rejection for
517 * *most* mutating API calls, particularly stream-related operations for send
518 * parts.
519 *
520 * A call is rejected (this function returns 0) if shutdown is in progress
521 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
522 * req_active=1, the connection must be active (i.e., the IDLE state is also
523 * rejected).
524 */
quic_mutation_allowed(QUIC_CONNECTION * qc,int req_active)525 static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
526 {
527 if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
528 return 0;
529
530 if (req_active && !ossl_quic_channel_is_active(qc->ch))
531 return 0;
532
533 return 1;
534 }
535
qctx_is_top_level(QCTX * ctx)536 static int qctx_is_top_level(QCTX *ctx)
537 {
538 return ctx->obj->parent_obj == NULL;
539 }
540
qctx_blocking(QCTX * ctx)541 static int qctx_blocking(QCTX *ctx)
542 {
543 return ossl_quic_obj_blocking(ctx->obj);
544 }
545
546 /*
547 * Block until a predicate is met.
548 *
549 * Precondition: Must have a channel.
550 * Precondition: Must hold channel lock (unchecked).
551 */
552 QUIC_NEEDS_LOCK
block_until_pred(QCTX * ctx,int (* pred)(void * arg),void * pred_arg,uint32_t flags)553 static int block_until_pred(QCTX *ctx,
554 int (*pred)(void *arg), void *pred_arg,
555 uint32_t flags)
556 {
557 QUIC_ENGINE *qeng;
558 QUIC_REACTOR *rtor;
559
560 qeng = ossl_quic_obj_get0_engine(ctx->obj);
561 assert(qeng != NULL);
562
563 /*
564 * Any attempt to block auto-disables tick inhibition as otherwise we will
565 * hang around forever.
566 */
567 ossl_quic_engine_set_inhibit_tick(qeng, 0);
568
569 rtor = ossl_quic_engine_get0_reactor(qeng);
570 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags);
571 }
572
573 /*
574 * QUIC Front-End I/O API: Initialization
575 * ======================================
576 *
577 * SSL_new => ossl_quic_new
578 * ossl_quic_init
579 * SSL_reset => ossl_quic_reset
580 * SSL_clear => ossl_quic_clear
581 * ossl_quic_deinit
582 * SSL_free => ossl_quic_free
583 *
584 * SSL_set_options => ossl_quic_set_options
585 * SSL_get_options => ossl_quic_get_options
586 * SSL_clear_options => ossl_quic_clear_options
587 *
588 */
589
590 /* SSL_new */
ossl_quic_new(SSL_CTX * ctx)591 SSL *ossl_quic_new(SSL_CTX *ctx)
592 {
593 QUIC_CONNECTION *qc = NULL;
594 SSL_CONNECTION *sc = NULL;
595
596 /*
597 * QUIC_server_method should not be used with SSL_new.
598 * It should only be used with SSL_new_listener.
599 */
600 if (ctx->method == OSSL_QUIC_server_method()) {
601 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
602 return NULL;
603 }
604
605 qc = OPENSSL_zalloc(sizeof(*qc));
606 if (qc == NULL) {
607 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
608 return NULL;
609 }
610
611 /* Create the QUIC domain mutex. */
612 #if defined(OPENSSL_THREADS)
613 if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) {
614 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
615 goto err;
616 }
617 #endif
618
619 /* Create the handshake layer. */
620 qc->tls = ossl_ssl_connection_new_int(ctx, &qc->obj.ssl, TLS_method());
621 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
622 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
623 goto err;
624 }
625
626 /* override the user_ssl of the inner connection */
627 sc->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL;
628
629 /* Restrict options derived from the SSL_CTX. */
630 sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
631 sc->pha_enabled = 0;
632
633 /* Determine mode of operation. */
634 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
635 qc->is_thread_assisted
636 = ((ctx->domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0);
637 #endif
638
639 qc->as_server = 0;
640 qc->as_server_state = qc->as_server;
641
642 if (!create_channel(qc, ctx))
643 goto err;
644
645 ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, &qc->obj.ssl);
646 ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
647
648 /* Initialise the QUIC_CONNECTION's QUIC_OBJ base. */
649 if (!ossl_quic_obj_init(&qc->obj, ctx, SSL_TYPE_QUIC_CONNECTION, NULL,
650 qc->engine, qc->port)) {
651 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
652 goto err;
653 }
654
655 /* Initialise libssl APL-related state. */
656 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
657 qc->default_ssl_mode = qc->obj.ssl.ctx->mode;
658 qc->default_ssl_options = qc->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
659 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
660 qc->last_error = SSL_ERROR_NONE;
661
662 qc_update_reject_policy(qc);
663
664 /*
665 * We do not create the default XSO yet. The reason for this is that the
666 * stream ID of the default XSO will depend on whether the stream is client
667 * or server-initiated, which depends on who transmits first. Since we do
668 * not know whether the application will be using a client-transmits-first
669 * or server-transmits-first protocol, we defer default XSO creation until
670 * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
671 * we take that as a cue that the client is expecting a server-initiated
672 * stream, and vice versa if SSL_write() is called first.
673 */
674 return &qc->obj.ssl;
675
676 err:
677 if (qc != NULL) {
678 qc_cleanup(qc, /*have_lock=*/0);
679 OPENSSL_free(qc);
680 }
681 return NULL;
682 }
683
684 QUIC_NEEDS_LOCK
quic_unref_port_bios(QUIC_PORT * port)685 static void quic_unref_port_bios(QUIC_PORT *port)
686 {
687 BIO *b;
688
689 if (port == NULL)
690 return;
691
692 b = ossl_quic_port_get_net_rbio(port);
693 BIO_free_all(b);
694
695 b = ossl_quic_port_get_net_wbio(port);
696 BIO_free_all(b);
697 }
698
699 QUIC_NEEDS_LOCK
qc_cleanup(QUIC_CONNECTION * qc,int have_lock)700 static void qc_cleanup(QUIC_CONNECTION *qc, int have_lock)
701 {
702 SSL_free(qc->tls);
703 qc->tls = NULL;
704
705 ossl_quic_channel_free(qc->ch);
706 qc->ch = NULL;
707
708 if (qc->port != NULL && qc->listener == NULL && qc->pending == 0) { /* TODO */
709 quic_unref_port_bios(qc->port);
710 ossl_quic_port_free(qc->port);
711 qc->port = NULL;
712
713 ossl_quic_engine_free(qc->engine);
714 qc->engine = NULL;
715 }
716
717 #if defined(OPENSSL_THREADS)
718 if (have_lock)
719 /* tsan doesn't like freeing locked mutexes */
720 ossl_crypto_mutex_unlock(qc->mutex);
721
722 if (qc->listener == NULL && qc->pending == 0)
723 ossl_crypto_mutex_free(&qc->mutex);
724 #endif
725 }
726
727 /* SSL_free */
728 QUIC_TAKES_LOCK
quic_free_listener(QCTX * ctx)729 static void quic_free_listener(QCTX *ctx)
730 {
731 quic_unref_port_bios(ctx->ql->port);
732 ossl_quic_port_drop_incoming(ctx->ql->port);
733 ossl_quic_port_free(ctx->ql->port);
734
735 if (ctx->ql->domain == NULL) {
736 ossl_quic_engine_free(ctx->ql->engine);
737 #if defined(OPENSSL_THREADS)
738 ossl_crypto_mutex_free(&ctx->ql->mutex);
739 #endif
740 } else {
741 SSL_free(&ctx->ql->domain->obj.ssl);
742 }
743 }
744
745 /* SSL_free */
746 QUIC_TAKES_LOCK
quic_free_domain(QCTX * ctx)747 static void quic_free_domain(QCTX *ctx)
748 {
749 ossl_quic_engine_free(ctx->qd->engine);
750 #if defined(OPENSSL_THREADS)
751 ossl_crypto_mutex_free(&ctx->qd->mutex);
752 #endif
753 }
754
755 QUIC_TAKES_LOCK
ossl_quic_free(SSL * s)756 void ossl_quic_free(SSL *s)
757 {
758 QCTX ctx;
759 int is_default;
760
761 /* We should never be called on anything but a QSO. */
762 if (!expect_quic_any(s, &ctx))
763 return;
764
765 if (ctx.is_domain) {
766 quic_free_domain(&ctx);
767 return;
768 }
769
770 if (ctx.is_listener) {
771 quic_free_listener(&ctx);
772 return;
773 }
774
775 qctx_lock(&ctx);
776
777 if (ctx.is_stream) {
778 /*
779 * When a QSSO is freed, the XSO is freed immediately, because the XSO
780 * itself only contains API personality layer data. However the
781 * underlying QUIC_STREAM is not freed immediately but is instead marked
782 * as deleted for later collection.
783 */
784
785 assert(ctx.qc->num_xso > 0);
786 --ctx.qc->num_xso;
787
788 /* If a stream's send part has not been finished, auto-reset it. */
789 if ((ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
790 || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
791 && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
792 ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
793 ctx.xso->stream, 0);
794
795 /* Do STOP_SENDING for the receive part, if applicable. */
796 if (ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
797 || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
798 ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
799 ctx.xso->stream, 0);
800
801 /* Update stream state. */
802 ctx.xso->stream->deleted = 1;
803 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
804 ctx.xso->stream);
805
806 is_default = (ctx.xso == ctx.qc->default_xso);
807 qctx_unlock(&ctx);
808
809 /*
810 * Unref the connection in most cases; the XSO has a ref to the QC and
811 * not vice versa. But for a default XSO, to avoid circular references,
812 * the QC refs the XSO but the XSO does not ref the QC. If we are the
813 * default XSO, we only get here when the QC is being torn down anyway,
814 * so don't call SSL_free(qc) as we are already in it.
815 */
816 if (!is_default)
817 SSL_free(&ctx.qc->obj.ssl);
818
819 /* Note: SSL_free calls OPENSSL_free(xso) for us */
820 return;
821 }
822
823 /*
824 * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
825 * stage, but is freed during the channel free when the whole QSM is freed.
826 */
827 if (ctx.qc->default_xso != NULL) {
828 QUIC_XSO *xso = ctx.qc->default_xso;
829
830 qctx_unlock(&ctx);
831 SSL_free(&xso->obj.ssl);
832 qctx_lock(&ctx);
833 ctx.qc->default_xso = NULL;
834 }
835
836 /* Ensure we have no remaining XSOs. */
837 assert(ctx.qc->num_xso == 0);
838
839 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
840 if (ctx.qc->is_thread_assisted && ctx.qc->started) {
841 ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
842 ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
843 }
844 #endif
845
846 /*
847 * Note: SSL_free (that called this function) calls OPENSSL_free(ctx.qc) for
848 * us
849 */
850 qc_cleanup(ctx.qc, /*have_lock=*/1);
851 /* Note: SSL_free calls OPENSSL_free(qc) for us */
852
853 if (ctx.qc->listener != NULL)
854 SSL_free(&ctx.qc->listener->obj.ssl);
855 if (ctx.qc->domain != NULL)
856 SSL_free(&ctx.qc->domain->obj.ssl);
857 }
858
859 /* SSL method init */
ossl_quic_init(SSL * s)860 int ossl_quic_init(SSL *s)
861 {
862 /* Same op as SSL_clear, forward the call. */
863 return ossl_quic_clear(s);
864 }
865
866 /* SSL method deinit */
ossl_quic_deinit(SSL * s)867 void ossl_quic_deinit(SSL *s)
868 {
869 /* No-op. */
870 }
871
872 /* SSL_clear (ssl_reset method) */
ossl_quic_reset(SSL * s)873 int ossl_quic_reset(SSL *s)
874 {
875 QCTX ctx;
876
877 if (!expect_quic_any(s, &ctx))
878 return 0;
879
880 ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
881 return 0;
882 }
883
884 /* ssl_clear method (unused) */
ossl_quic_clear(SSL * s)885 int ossl_quic_clear(SSL *s)
886 {
887 QCTX ctx;
888
889 if (!expect_quic_any(s, &ctx))
890 return 0;
891
892 ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
893 return 0;
894 }
895
ossl_quic_set_override_now_cb(SSL * s,OSSL_TIME (* now_cb)(void * arg),void * now_cb_arg)896 int ossl_quic_set_override_now_cb(SSL *s,
897 OSSL_TIME (*now_cb)(void *arg),
898 void *now_cb_arg)
899 {
900 QCTX ctx;
901
902 if (!expect_quic_any(s, &ctx))
903 return 0;
904
905 qctx_lock(&ctx);
906
907 ossl_quic_engine_set_time_cb(ctx.obj->engine, now_cb, now_cb_arg);
908
909 qctx_unlock(&ctx);
910 return 1;
911 }
912
ossl_quic_conn_force_assist_thread_wake(SSL * s)913 void ossl_quic_conn_force_assist_thread_wake(SSL *s)
914 {
915 QCTX ctx;
916
917 if (!expect_quic_conn_only(s, &ctx))
918 return;
919
920 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
921 if (ctx.qc->is_thread_assisted && ctx.qc->started)
922 ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
923 #endif
924 }
925
926 QUIC_NEEDS_LOCK
qc_touch_default_xso(QUIC_CONNECTION * qc)927 static void qc_touch_default_xso(QUIC_CONNECTION *qc)
928 {
929 qc->default_xso_created = 1;
930 qc_update_reject_policy(qc);
931 }
932
933 /*
934 * Changes default XSO. Allows caller to keep reference to the old default XSO
935 * (if any). Reference to new XSO is transferred from caller.
936 */
937 QUIC_NEEDS_LOCK
qc_set_default_xso_keep_ref(QUIC_CONNECTION * qc,QUIC_XSO * xso,int touch,QUIC_XSO ** old_xso)938 static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
939 int touch,
940 QUIC_XSO **old_xso)
941 {
942 int refs;
943
944 *old_xso = NULL;
945
946 if (qc->default_xso != xso) {
947 *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
948
949 qc->default_xso = xso;
950
951 if (xso == NULL) {
952 /*
953 * Changing to not having a default XSO. XSO becomes standalone and
954 * now has a ref to the QC.
955 */
956 if (!ossl_assert(SSL_up_ref(&qc->obj.ssl)))
957 return;
958 } else {
959 /*
960 * Changing from not having a default XSO to having one. The new XSO
961 * will have had a reference to the QC we need to drop to avoid a
962 * circular reference.
963 *
964 * Currently we never change directly from one default XSO to
965 * another, though this function would also still be correct if this
966 * weren't the case.
967 */
968 assert(*old_xso == NULL);
969
970 CRYPTO_DOWN_REF(&qc->obj.ssl.references, &refs);
971 assert(refs > 0);
972 }
973 }
974
975 if (touch)
976 qc_touch_default_xso(qc);
977 }
978
979 /*
980 * Changes default XSO, releasing the reference to any previous default XSO.
981 * Reference to new XSO is transferred from caller.
982 */
983 QUIC_NEEDS_LOCK
qc_set_default_xso(QUIC_CONNECTION * qc,QUIC_XSO * xso,int touch)984 static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
985 {
986 QUIC_XSO *old_xso = NULL;
987
988 qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
989
990 if (old_xso != NULL)
991 SSL_free(&old_xso->obj.ssl);
992 }
993
994 QUIC_NEEDS_LOCK
xso_update_options(QUIC_XSO * xso)995 static void xso_update_options(QUIC_XSO *xso)
996 {
997 int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
998
999 if (xso->stream->rstream != NULL)
1000 ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
1001
1002 if (xso->stream->sstream != NULL)
1003 ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
1004 }
1005
1006 /*
1007 * SSL_set_options
1008 * ---------------
1009 *
1010 * Setting options on a QCSO
1011 * - configures the handshake-layer options;
1012 * - configures the default data-plane options for new streams;
1013 * - configures the data-plane options on the default XSO, if there is one.
1014 *
1015 * Setting options on a QSSO
1016 * - configures data-plane options for that stream only.
1017 */
1018 QUIC_TAKES_LOCK
quic_mask_or_options(SSL * ssl,uint64_t mask_value,uint64_t or_value)1019 static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
1020 {
1021 QCTX ctx;
1022 uint64_t hs_mask_value, hs_or_value, ret;
1023
1024 if (!expect_quic_cs(ssl, &ctx))
1025 return 0;
1026
1027 qctx_lock(&ctx);
1028
1029 if (!ctx.is_stream) {
1030 /*
1031 * If we were called on the connection, we apply any handshake option
1032 * changes.
1033 */
1034 hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
1035 hs_or_value = (or_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
1036
1037 SSL_clear_options(ctx.qc->tls, hs_mask_value);
1038 SSL_set_options(ctx.qc->tls, hs_or_value);
1039
1040 /* Update defaults for new streams. */
1041 ctx.qc->default_ssl_options
1042 = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
1043 & OSSL_QUIC_PERMITTED_OPTIONS;
1044 }
1045
1046 ret = ctx.qc->default_ssl_options;
1047 if (ctx.xso != NULL) {
1048 ctx.xso->ssl_options
1049 = ((ctx.xso->ssl_options & ~mask_value) | or_value)
1050 & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1051
1052 xso_update_options(ctx.xso);
1053
1054 if (ctx.is_stream)
1055 ret = ctx.xso->ssl_options;
1056 }
1057
1058 qctx_unlock(&ctx);
1059 return ret;
1060 }
1061
ossl_quic_set_options(SSL * ssl,uint64_t options)1062 uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
1063 {
1064 return quic_mask_or_options(ssl, 0, options);
1065 }
1066
1067 /* SSL_clear_options */
ossl_quic_clear_options(SSL * ssl,uint64_t options)1068 uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
1069 {
1070 return quic_mask_or_options(ssl, options, 0);
1071 }
1072
1073 /* SSL_get_options */
ossl_quic_get_options(const SSL * ssl)1074 uint64_t ossl_quic_get_options(const SSL *ssl)
1075 {
1076 return quic_mask_or_options((SSL *)ssl, 0, 0);
1077 }
1078
1079 /*
1080 * QUIC Front-End I/O API: Network BIO Configuration
1081 * =================================================
1082 *
1083 * Handling the different BIOs is difficult:
1084 *
1085 * - It is more or less a requirement that we use non-blocking network I/O;
1086 * we need to be able to have timeouts on recv() calls, and make best effort
1087 * (non blocking) send() and recv() calls.
1088 *
1089 * The only sensible way to do this is to configure the socket into
1090 * non-blocking mode. We could try to do select() before calling send() or
1091 * recv() to get a guarantee that the call will not block, but this will
1092 * probably run into issues with buggy OSes which generate spurious socket
1093 * readiness events. In any case, relying on this to work reliably does not
1094 * seem sane.
1095 *
1096 * Timeouts could be handled via setsockopt() socket timeout options, but
1097 * this depends on OS support and adds another syscall to every network I/O
1098 * operation. It also has obvious thread safety concerns if we want to move
1099 * to concurrent use of a single socket at some later date.
1100 *
1101 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
1102 * be made non-blocking. However some OSes (e.g. Windows) do not support
1103 * this, so we cannot rely on this.
1104 *
1105 * As such, we need to configure any FD in non-blocking mode. This may
1106 * confound users who pass a blocking socket to libssl. However, in practice
1107 * it would be extremely strange for a user of QUIC to pass an FD to us,
1108 * then also try and send receive traffic on the same socket(!). Thus the
1109 * impact of this should be limited, and can be documented.
1110 *
1111 * - We support both blocking and non-blocking operation in terms of the API
1112 * presented to the user. One prospect is to set the blocking mode based on
1113 * whether the socket passed to us was already in blocking mode. However,
1114 * Windows has no API for determining if a socket is in blocking mode (!),
1115 * therefore this cannot be done portably. Currently therefore we expose an
1116 * explicit API call to set this, and default to blocking mode.
1117 *
1118 * - We need to determine our initial destination UDP address. The "natural"
1119 * way for a user to do this is to set the peer variable on a BIO_dgram.
1120 * However, this has problems because BIO_dgram's peer variable is used for
1121 * both transmission and reception. This means it can be constantly being
1122 * changed to a malicious value (e.g. if some random unrelated entity on the
1123 * network starts sending traffic to us) on every read call. This is not a
1124 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
1125 * calls only, which do not use this variable. However, we do need to let
1126 * the user specify the peer in a 'normal' manner. The compromise here is
1127 * that we grab the current peer value set at the time the write BIO is set
1128 * and do not read the value again.
1129 *
1130 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
1131 * Currently we do this by only supporting non-blocking mode.
1132 *
1133 */
1134
1135 /*
1136 * Determines what initial destination UDP address we should use, if possible.
1137 * If this fails the client must set the destination address manually, or use a
1138 * BIO which does not need a destination address.
1139 */
csm_analyse_init_peer_addr(BIO * net_wbio,BIO_ADDR * peer)1140 static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
1141 {
1142 if (BIO_dgram_detect_peer_addr(net_wbio, peer) <= 0)
1143 return 0;
1144
1145 return 1;
1146 }
1147
1148 static int
quic_set0_net_rbio(QUIC_OBJ * obj,BIO * net_rbio)1149 quic_set0_net_rbio(QUIC_OBJ *obj, BIO *net_rbio)
1150 {
1151 QUIC_PORT *port;
1152 BIO *old_rbio = NULL;
1153
1154 port = ossl_quic_obj_get0_port(obj);
1155 old_rbio = ossl_quic_port_get_net_rbio(port);
1156 if (old_rbio == net_rbio)
1157 return 0;
1158
1159 if (!ossl_quic_port_set_net_rbio(port, net_rbio))
1160 return 0;
1161
1162 BIO_free_all(old_rbio);
1163 if (net_rbio != NULL)
1164 BIO_set_nbio(net_rbio, 1); /* best effort autoconfig */
1165
1166 return 1;
1167 }
1168
1169 static int
quic_set0_net_wbio(QUIC_OBJ * obj,BIO * net_wbio)1170 quic_set0_net_wbio(QUIC_OBJ *obj, BIO *net_wbio)
1171 {
1172 QUIC_PORT *port;
1173 BIO *old_wbio = NULL;
1174
1175 port = ossl_quic_obj_get0_port(obj);
1176 old_wbio = ossl_quic_port_get_net_wbio(port);
1177 if (old_wbio == net_wbio)
1178 return 0;
1179
1180 if (!ossl_quic_port_set_net_wbio(port, net_wbio))
1181 return 0;
1182
1183 BIO_free_all(old_wbio);
1184 if (net_wbio != NULL)
1185 BIO_set_nbio(net_wbio, 1); /* best effort autoconfig */
1186
1187 return 1;
1188 }
1189
ossl_quic_conn_set0_net_rbio(SSL * s,BIO * net_rbio)1190 void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
1191 {
1192 QCTX ctx;
1193
1194 if (!expect_quic_csl(s, &ctx))
1195 return;
1196
1197 /* Returns 0 if no change. */
1198 if (!quic_set0_net_rbio(ctx.obj, net_rbio))
1199 return;
1200 }
1201
ossl_quic_conn_set0_net_wbio(SSL * s,BIO * net_wbio)1202 void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
1203 {
1204 QCTX ctx;
1205
1206 if (!expect_quic_csl(s, &ctx))
1207 return;
1208
1209 /* Returns 0 if no change. */
1210 if (!quic_set0_net_wbio(ctx.obj, net_wbio))
1211 return;
1212 }
1213
ossl_quic_conn_get_net_rbio(const SSL * s)1214 BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
1215 {
1216 QCTX ctx;
1217 QUIC_PORT *port;
1218
1219 if (!expect_quic_csl(s, &ctx))
1220 return NULL;
1221
1222 port = ossl_quic_obj_get0_port(ctx.obj);
1223 assert(port != NULL);
1224 return ossl_quic_port_get_net_rbio(port);
1225 }
1226
ossl_quic_conn_get_net_wbio(const SSL * s)1227 BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
1228 {
1229 QCTX ctx;
1230 QUIC_PORT *port;
1231
1232 if (!expect_quic_csl(s, &ctx))
1233 return NULL;
1234
1235 port = ossl_quic_obj_get0_port(ctx.obj);
1236 assert(port != NULL);
1237 return ossl_quic_port_get_net_wbio(port);
1238 }
1239
ossl_quic_conn_get_blocking_mode(const SSL * s)1240 int ossl_quic_conn_get_blocking_mode(const SSL *s)
1241 {
1242 QCTX ctx;
1243
1244 if (!expect_quic_csl(s, &ctx))
1245 return 0;
1246
1247 return qctx_blocking(&ctx);
1248 }
1249
1250 QUIC_TAKES_LOCK
ossl_quic_conn_set_blocking_mode(SSL * s,int blocking)1251 int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
1252 {
1253 int ret = 0;
1254 unsigned int mode;
1255 QCTX ctx;
1256
1257 if (!expect_quic_csl(s, &ctx))
1258 return 0;
1259
1260 qctx_lock(&ctx);
1261
1262 /* Sanity check - can we support the request given the current network BIO? */
1263 if (blocking) {
1264 /*
1265 * If called directly on a top-level object (QCSO or QLSO), update our
1266 * information on network BIO capabilities.
1267 */
1268 if (qctx_is_top_level(&ctx))
1269 ossl_quic_engine_update_poll_descriptors(ctx.obj->engine, /*force=*/1);
1270
1271 /* Cannot enable blocking mode if we do not have pollable FDs. */
1272 if (!ossl_quic_obj_can_support_blocking(ctx.obj)) {
1273 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1274 goto out;
1275 }
1276 }
1277
1278 mode = (blocking != 0)
1279 ? QUIC_BLOCKING_MODE_BLOCKING
1280 : QUIC_BLOCKING_MODE_NONBLOCKING;
1281
1282 ossl_quic_obj_set_blocking_mode(ctx.obj, mode);
1283
1284 ret = 1;
1285 out:
1286 qctx_unlock(&ctx);
1287 return ret;
1288 }
1289
ossl_quic_conn_set_initial_peer_addr(SSL * s,const BIO_ADDR * peer_addr)1290 int ossl_quic_conn_set_initial_peer_addr(SSL *s,
1291 const BIO_ADDR *peer_addr)
1292 {
1293 QCTX ctx;
1294
1295 if (!expect_quic_cs(s, &ctx))
1296 return 0;
1297
1298 if (ctx.qc->started)
1299 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
1300 NULL);
1301
1302 if (peer_addr == NULL) {
1303 BIO_ADDR_clear(&ctx.qc->init_peer_addr);
1304 return 1;
1305 }
1306
1307 return BIO_ADDR_copy(&ctx.qc->init_peer_addr, peer_addr);
1308 }
1309
1310 /*
1311 * QUIC Front-End I/O API: Asynchronous I/O Management
1312 * ===================================================
1313 *
1314 * (BIO/)SSL_handle_events => ossl_quic_handle_events
1315 * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
1316 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
1317 *
1318 */
1319
1320 /* SSL_handle_events; performs QUIC I/O and timeout processing. */
1321 QUIC_TAKES_LOCK
ossl_quic_handle_events(SSL * s)1322 int ossl_quic_handle_events(SSL *s)
1323 {
1324 QCTX ctx;
1325
1326 if (!expect_quic_any(s, &ctx))
1327 return 0;
1328
1329 qctx_lock(&ctx);
1330 ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx.obj), 0);
1331 qctx_unlock(&ctx);
1332 return 1;
1333 }
1334
1335 /*
1336 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1337 * should next have events handled by the application by calling
1338 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1339 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1340 * the value of *tv is undefined.
1341 */
1342 QUIC_TAKES_LOCK
ossl_quic_get_event_timeout(SSL * s,struct timeval * tv,int * is_infinite)1343 int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1344 {
1345 QCTX ctx;
1346 QUIC_REACTOR *reactor;
1347 OSSL_TIME deadline;
1348 OSSL_TIME basetime;
1349
1350 if (!expect_quic_any(s, &ctx))
1351 return 0;
1352
1353 qctx_lock(&ctx);
1354
1355 reactor = ossl_quic_obj_get0_reactor(ctx.obj);
1356 deadline = ossl_quic_reactor_get_tick_deadline(reactor);
1357
1358 if (ossl_time_is_infinite(deadline)) {
1359 qctx_unlock(&ctx);
1360 *is_infinite = 1;
1361
1362 /*
1363 * Robustness against faulty applications that don't check *is_infinite;
1364 * harmless long timeout.
1365 */
1366 tv->tv_sec = 1000000;
1367 tv->tv_usec = 0;
1368 return 1;
1369 }
1370
1371 basetime = ossl_quic_engine_get_time(ctx.obj->engine);
1372
1373 qctx_unlock(&ctx);
1374
1375 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, basetime));
1376 *is_infinite = 0;
1377
1378 return 1;
1379 }
1380
1381 /* SSL_get_rpoll_descriptor */
ossl_quic_get_rpoll_descriptor(SSL * s,BIO_POLL_DESCRIPTOR * desc)1382 int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1383 {
1384 QCTX ctx;
1385 QUIC_PORT *port = NULL;
1386 BIO *net_rbio;
1387
1388 if (!expect_quic_csl(s, &ctx))
1389 return 0;
1390
1391 port = ossl_quic_obj_get0_port(ctx.obj);
1392 net_rbio = ossl_quic_port_get_net_rbio(port);
1393 if (desc == NULL || net_rbio == NULL)
1394 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1395 NULL);
1396
1397 return BIO_get_rpoll_descriptor(net_rbio, desc);
1398 }
1399
1400 /* SSL_get_wpoll_descriptor */
ossl_quic_get_wpoll_descriptor(SSL * s,BIO_POLL_DESCRIPTOR * desc)1401 int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1402 {
1403 QCTX ctx;
1404 QUIC_PORT *port = NULL;
1405 BIO *net_wbio;
1406
1407 if (!expect_quic_csl(s, &ctx))
1408 return 0;
1409
1410 port = ossl_quic_obj_get0_port(ctx.obj);
1411 net_wbio = ossl_quic_port_get_net_wbio(port);
1412 if (desc == NULL || net_wbio == NULL)
1413 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1414 NULL);
1415
1416 return BIO_get_wpoll_descriptor(net_wbio, desc);
1417 }
1418
1419 /* SSL_net_read_desired */
1420 QUIC_TAKES_LOCK
ossl_quic_get_net_read_desired(SSL * s)1421 int ossl_quic_get_net_read_desired(SSL *s)
1422 {
1423 QCTX ctx;
1424 int ret;
1425
1426 if (!expect_quic_csl(s, &ctx))
1427 return 0;
1428
1429 qctx_lock(&ctx);
1430 ret = ossl_quic_reactor_net_read_desired(ossl_quic_obj_get0_reactor(ctx.obj));
1431 qctx_unlock(&ctx);
1432 return ret;
1433 }
1434
1435 /* SSL_net_write_desired */
1436 QUIC_TAKES_LOCK
ossl_quic_get_net_write_desired(SSL * s)1437 int ossl_quic_get_net_write_desired(SSL *s)
1438 {
1439 int ret;
1440 QCTX ctx;
1441
1442 if (!expect_quic_csl(s, &ctx))
1443 return 0;
1444
1445 qctx_lock(&ctx);
1446 ret = ossl_quic_reactor_net_write_desired(ossl_quic_obj_get0_reactor(ctx.obj));
1447 qctx_unlock(&ctx);
1448 return ret;
1449 }
1450
1451 /*
1452 * QUIC Front-End I/O API: Connection Lifecycle Operations
1453 * =======================================================
1454 *
1455 * SSL_do_handshake => ossl_quic_do_handshake
1456 * SSL_set_connect_state => ossl_quic_set_connect_state
1457 * SSL_set_accept_state => ossl_quic_set_accept_state
1458 * SSL_shutdown => ossl_quic_shutdown
1459 * SSL_ctrl => ossl_quic_ctrl
1460 * (BIO/)SSL_connect => ossl_quic_connect
1461 * (BIO/)SSL_accept => ossl_quic_accept
1462 *
1463 */
1464
1465 QUIC_NEEDS_LOCK
qc_shutdown_flush_init(QUIC_CONNECTION * qc)1466 static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1467 {
1468 QUIC_STREAM_MAP *qsm;
1469
1470 if (qc->shutting_down)
1471 return;
1472
1473 qsm = ossl_quic_channel_get_qsm(qc->ch);
1474
1475 ossl_quic_stream_map_begin_shutdown_flush(qsm);
1476 qc->shutting_down = 1;
1477 }
1478
1479 /* Returns 1 if all shutdown-flush streams have been done with. */
1480 QUIC_NEEDS_LOCK
qc_shutdown_flush_finished(QUIC_CONNECTION * qc)1481 static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1482 {
1483 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1484
1485 return qc->shutting_down
1486 && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1487 }
1488
1489 /* SSL_shutdown */
quic_shutdown_wait(void * arg)1490 static int quic_shutdown_wait(void *arg)
1491 {
1492 QUIC_CONNECTION *qc = arg;
1493
1494 return ossl_quic_channel_is_terminated(qc->ch);
1495 }
1496
1497 /* Returns 1 if shutdown flush process has finished or is inapplicable. */
quic_shutdown_flush_wait(void * arg)1498 static int quic_shutdown_flush_wait(void *arg)
1499 {
1500 QUIC_CONNECTION *qc = arg;
1501
1502 return ossl_quic_channel_is_term_any(qc->ch)
1503 || qc_shutdown_flush_finished(qc);
1504 }
1505
quic_shutdown_peer_wait(void * arg)1506 static int quic_shutdown_peer_wait(void *arg)
1507 {
1508 QUIC_CONNECTION *qc = arg;
1509 return ossl_quic_channel_is_term_any(qc->ch);
1510 }
1511
1512 QUIC_TAKES_LOCK
ossl_quic_conn_shutdown(SSL * s,uint64_t flags,const SSL_SHUTDOWN_EX_ARGS * args,size_t args_len)1513 int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1514 const SSL_SHUTDOWN_EX_ARGS *args,
1515 size_t args_len)
1516 {
1517 int ret;
1518 QCTX ctx;
1519 int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1520 int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);
1521 int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);
1522
1523 if (!expect_quic_cs(s, &ctx))
1524 return -1;
1525
1526 if (ctx.is_stream) {
1527 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
1528 return -1;
1529 }
1530
1531 qctx_lock(&ctx);
1532
1533 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1534 qctx_unlock(&ctx);
1535 return 1;
1536 }
1537
1538 /* Phase 1: Stream Flushing */
1539 if (!wait_peer && stream_flush) {
1540 qc_shutdown_flush_init(ctx.qc);
1541
1542 if (!qc_shutdown_flush_finished(ctx.qc)) {
1543 if (!no_block && qctx_blocking(&ctx)) {
1544 ret = block_until_pred(&ctx, quic_shutdown_flush_wait, ctx.qc, 0);
1545 if (ret < 1) {
1546 ret = 0;
1547 goto err;
1548 }
1549 } else {
1550 qctx_maybe_autotick(&ctx);
1551 }
1552 }
1553
1554 if (!qc_shutdown_flush_finished(ctx.qc)) {
1555 qctx_unlock(&ctx);
1556 return 0; /* ongoing */
1557 }
1558 }
1559
1560 /* Phase 2: Connection Closure */
1561 if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1562 if (!no_block && qctx_blocking(&ctx)) {
1563 ret = block_until_pred(&ctx, quic_shutdown_peer_wait, ctx.qc, 0);
1564 if (ret < 1) {
1565 ret = 0;
1566 goto err;
1567 }
1568 } else {
1569 qctx_maybe_autotick(&ctx);
1570 }
1571
1572 if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1573 ret = 0; /* peer hasn't closed yet - still not done */
1574 goto err;
1575 }
1576
1577 /*
1578 * We are at least terminating - go through the normal process of
1579 * waiting until we are in the TERMINATED state.
1580 */
1581 }
1582
1583 /* Block mutation ops regardless of if we did stream flush. */
1584 ctx.qc->shutting_down = 1;
1585
1586 /*
1587 * This call is a no-op if we are already terminating, so it doesn't
1588 * affect the wait_peer case.
1589 */
1590 ossl_quic_channel_local_close(ctx.qc->ch,
1591 args != NULL ? args->quic_error_code : 0,
1592 args != NULL ? args->quic_reason : NULL);
1593
1594 SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1595
1596 if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1597 qctx_unlock(&ctx);
1598 return 1;
1599 }
1600
1601 /* Phase 3: Terminating Wait Time */
1602 if (!no_block && qctx_blocking(&ctx)
1603 && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1604 ret = block_until_pred(&ctx, quic_shutdown_wait, ctx.qc, 0);
1605 if (ret < 1) {
1606 ret = 0;
1607 goto err;
1608 }
1609 } else {
1610 qctx_maybe_autotick(&ctx);
1611 }
1612
1613 ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1614 err:
1615 qctx_unlock(&ctx);
1616 return ret;
1617 }
1618
1619 /* SSL_ctrl */
ossl_quic_ctrl(SSL * s,int cmd,long larg,void * parg)1620 long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1621 {
1622 QCTX ctx;
1623
1624 if (!expect_quic_csl(s, &ctx))
1625 return 0;
1626
1627 switch (cmd) {
1628 case SSL_CTRL_MODE:
1629 if (ctx.is_listener)
1630 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1631
1632 /* If called on a QCSO, update the default mode. */
1633 if (!ctx.is_stream)
1634 ctx.qc->default_ssl_mode |= (uint32_t)larg;
1635
1636 /*
1637 * If we were called on a QSSO or have a default stream, we also update
1638 * that.
1639 */
1640 if (ctx.xso != NULL) {
1641 /* Cannot enable EPW while AON write in progress. */
1642 if (ctx.xso->aon_write_in_progress)
1643 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1644
1645 ctx.xso->ssl_mode |= (uint32_t)larg;
1646 return ctx.xso->ssl_mode;
1647 }
1648
1649 return ctx.qc->default_ssl_mode;
1650 case SSL_CTRL_CLEAR_MODE:
1651 if (ctx.is_listener)
1652 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1653
1654 if (!ctx.is_stream)
1655 ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1656
1657 if (ctx.xso != NULL) {
1658 ctx.xso->ssl_mode &= ~(uint32_t)larg;
1659 return ctx.xso->ssl_mode;
1660 }
1661
1662 return ctx.qc->default_ssl_mode;
1663
1664 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1665 if (ctx.is_listener)
1666 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1667
1668 ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1669 /* This ctrl also needs to be passed to the internal SSL object */
1670 return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1671
1672 case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1673 {
1674 int is_infinite;
1675
1676 if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1677 return 0;
1678
1679 return !is_infinite;
1680 }
1681 case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1682 /* For legacy compatibility with DTLS calls. */
1683 return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1684
1685 /* Mask ctrls we shouldn't support for QUIC. */
1686 case SSL_CTRL_GET_READ_AHEAD:
1687 case SSL_CTRL_SET_READ_AHEAD:
1688 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1689 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
1690 case SSL_CTRL_SET_MAX_PIPELINES:
1691 return 0;
1692
1693 default:
1694 /*
1695 * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1696 * implementation. Either SSL_ctrl will handle it itself by direct
1697 * access into handshake layer state, or failing that, it will be passed
1698 * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1699 * supported by anything, the handshake layer's ctrl method will finally
1700 * return 0.
1701 */
1702 if (ctx.is_listener)
1703 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1704
1705 return ossl_ctrl_internal(&ctx.qc->obj.ssl, cmd, larg, parg, /*no_quic=*/1);
1706 }
1707 }
1708
1709 /* SSL_set_connect_state */
ossl_quic_set_connect_state(SSL * s,int raiseerrs)1710 int ossl_quic_set_connect_state(SSL *s, int raiseerrs)
1711 {
1712 QCTX ctx;
1713
1714 if (!is_quic_c(s, &ctx, raiseerrs))
1715 return 0;
1716
1717 if (ctx.qc->as_server_state == 0)
1718 return 1;
1719
1720 /* Cannot be changed after handshake started */
1721 if (ctx.qc->started) {
1722 if (raiseerrs)
1723 QUIC_RAISE_NON_NORMAL_ERROR(NULL, SSL_R_INVALID_COMMAND, NULL);
1724 return 0;
1725 }
1726
1727 ctx.qc->as_server_state = 0;
1728 return 1;
1729 }
1730
1731 /* SSL_set_accept_state */
ossl_quic_set_accept_state(SSL * s,int raiseerrs)1732 int ossl_quic_set_accept_state(SSL *s, int raiseerrs)
1733 {
1734 QCTX ctx;
1735
1736 if (!is_quic_c(s, &ctx, raiseerrs))
1737 return 0;
1738
1739 if (ctx.qc->as_server_state == 1)
1740 return 1;
1741
1742 /* Cannot be changed after handshake started */
1743 if (ctx.qc->started) {
1744 if (raiseerrs)
1745 QUIC_RAISE_NON_NORMAL_ERROR(NULL, SSL_R_INVALID_COMMAND, NULL);
1746 return 0;
1747 }
1748
1749 ctx.qc->as_server_state = 1;
1750 return 1;
1751 }
1752
1753 /* SSL_do_handshake */
1754 struct quic_handshake_wait_args {
1755 QUIC_CONNECTION *qc;
1756 };
1757
tls_wants_non_io_retry(QUIC_CONNECTION * qc)1758 static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)
1759 {
1760 int want = SSL_want(qc->tls);
1761
1762 if (want == SSL_X509_LOOKUP
1763 || want == SSL_CLIENT_HELLO_CB
1764 || want == SSL_RETRY_VERIFY)
1765 return 1;
1766
1767 return 0;
1768 }
1769
quic_handshake_wait(void * arg)1770 static int quic_handshake_wait(void *arg)
1771 {
1772 struct quic_handshake_wait_args *args = arg;
1773
1774 if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1775 return -1;
1776
1777 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1778 return 1;
1779
1780 if (tls_wants_non_io_retry(args->qc))
1781 return 1;
1782
1783 return 0;
1784 }
1785
configure_channel(QUIC_CONNECTION * qc)1786 static int configure_channel(QUIC_CONNECTION *qc)
1787 {
1788 assert(qc->ch != NULL);
1789
1790 if (!ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1791 return 0;
1792
1793 return 1;
1794 }
1795
need_notifier_for_domain_flags(uint64_t domain_flags)1796 static int need_notifier_for_domain_flags(uint64_t domain_flags)
1797 {
1798 return (domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0
1799 || ((domain_flags & SSL_DOMAIN_FLAG_MULTI_THREAD) != 0
1800 && (domain_flags & SSL_DOMAIN_FLAG_BLOCKING) != 0);
1801 }
1802
1803 QUIC_NEEDS_LOCK
create_channel(QUIC_CONNECTION * qc,SSL_CTX * ctx)1804 static int create_channel(QUIC_CONNECTION *qc, SSL_CTX *ctx)
1805 {
1806 QUIC_ENGINE_ARGS engine_args = { 0 };
1807 QUIC_PORT_ARGS port_args = { 0 };
1808
1809 engine_args.libctx = ctx->libctx;
1810 engine_args.propq = ctx->propq;
1811 #if defined(OPENSSL_THREADS)
1812 engine_args.mutex = qc->mutex;
1813 #endif
1814
1815 if (need_notifier_for_domain_flags(ctx->domain_flags))
1816 engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
1817
1818 qc->engine = ossl_quic_engine_new(&engine_args);
1819 if (qc->engine == NULL) {
1820 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1821 return 0;
1822 }
1823
1824 port_args.channel_ctx = ctx;
1825 qc->port = ossl_quic_engine_create_port(qc->engine, &port_args);
1826 if (qc->port == NULL) {
1827 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1828 ossl_quic_engine_free(qc->engine);
1829 qc->engine = NULL;
1830 return 0;
1831 }
1832
1833 qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
1834 if (qc->ch == NULL) {
1835 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1836 ossl_quic_port_free(qc->port);
1837 qc->port = NULL;
1838 ossl_quic_engine_free(qc->engine);
1839 qc->engine = NULL;
1840 return 0;
1841 }
1842
1843 return 1;
1844 }
1845
1846 /*
1847 * Configures a channel with the information we have accumulated via calls made
1848 * to us from the application prior to starting a handshake attempt.
1849 */
1850 QUIC_NEEDS_LOCK
ensure_channel_started(QCTX * ctx)1851 static int ensure_channel_started(QCTX *ctx)
1852 {
1853 QUIC_CONNECTION *qc = ctx->qc;
1854
1855 if (!qc->started) {
1856 if (!configure_channel(qc)) {
1857 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1858 "failed to configure channel");
1859 return 0;
1860 }
1861
1862 if (!ossl_quic_channel_start(qc->ch)) {
1863 ossl_quic_channel_restore_err_state(qc->ch);
1864 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1865 "failed to start channel");
1866 return 0;
1867 }
1868
1869 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1870 if (qc->is_thread_assisted)
1871 if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch)) {
1872 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1873 "failed to start assist thread");
1874 return 0;
1875 }
1876 #endif
1877 }
1878
1879 qc->started = 1;
1880 return 1;
1881 }
1882
1883 QUIC_NEEDS_LOCK
quic_do_handshake(QCTX * ctx)1884 static int quic_do_handshake(QCTX *ctx)
1885 {
1886 int ret;
1887 QUIC_CONNECTION *qc = ctx->qc;
1888 QUIC_PORT *port;
1889 BIO *net_rbio, *net_wbio;
1890
1891 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1892 /* Handshake already completed. */
1893 return 1;
1894
1895 if (!quic_mutation_allowed(qc, /*req_active=*/0))
1896 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1897
1898 if (qc->as_server != qc->as_server_state) {
1899 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1900 return -1; /* Non-protocol error */
1901 }
1902
1903 port = ossl_quic_obj_get0_port(ctx->obj);
1904 net_rbio = ossl_quic_port_get_net_rbio(port);
1905 net_wbio = ossl_quic_port_get_net_wbio(port);
1906 if (net_rbio == NULL || net_wbio == NULL) {
1907 /* Need read and write BIOs. */
1908 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1909 return -1; /* Non-protocol error */
1910 }
1911
1912 if (!qc->started && ossl_quic_port_is_addressed_w(port)
1913 && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1914 /*
1915 * We are trying to connect and are using addressed mode, which means we
1916 * need an initial peer address; if we do not have a peer address yet,
1917 * we should try to autodetect one.
1918 *
1919 * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
1920 * may not be able to provide us with a peer address until they have
1921 * finished their own processing. They may not be able to perform this
1922 * processing until an application has finished configuring that BIO
1923 * (e.g. with setter calls), which might happen after SSL_set_bio is
1924 * called.
1925 */
1926 if (!csm_analyse_init_peer_addr(net_wbio, &qc->init_peer_addr))
1927 /* best effort */
1928 BIO_ADDR_clear(&qc->init_peer_addr);
1929 else
1930 ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
1931 }
1932
1933 if (!qc->started
1934 && ossl_quic_port_is_addressed_w(port)
1935 && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1936 /*
1937 * If we still don't have a peer address in addressed mode, we can't do
1938 * anything.
1939 */
1940 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1941 return -1; /* Non-protocol error */
1942 }
1943
1944 /*
1945 * Start connection process. Note we may come here multiple times in
1946 * non-blocking mode, which is fine.
1947 */
1948 if (!ensure_channel_started(ctx)) /* raises on failure */
1949 return -1; /* Non-protocol error */
1950
1951 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1952 /* The handshake is now done. */
1953 return 1;
1954
1955 if (!qctx_blocking(ctx)) {
1956 /* Try to advance the reactor. */
1957 qctx_maybe_autotick(ctx);
1958
1959 if (ossl_quic_channel_is_handshake_complete(qc->ch))
1960 /* The handshake is now done. */
1961 return 1;
1962
1963 if (ossl_quic_channel_is_term_any(qc->ch)) {
1964 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1965 return 0;
1966 } else if (ossl_quic_obj_desires_blocking(&qc->obj)) {
1967 /*
1968 * As a special case when doing a handshake when blocking mode is
1969 * desired yet not available, see if the network BIOs have become
1970 * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1971 * which do late creation of socket FDs and therefore cannot expose
1972 * a poll descriptor until after a network BIO is set on the QCSO.
1973 */
1974 ossl_quic_engine_update_poll_descriptors(qc->obj.engine, /*force=*/1);
1975 }
1976 }
1977
1978 /*
1979 * We are either in blocking mode or just entered it due to the code above.
1980 */
1981 if (qctx_blocking(ctx)) {
1982 /* In blocking mode, wait for the handshake to complete. */
1983 struct quic_handshake_wait_args args;
1984
1985 args.qc = qc;
1986
1987 ret = block_until_pred(ctx, quic_handshake_wait, &args, 0);
1988 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1989 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1990 return 0; /* Shutdown before completion */
1991 } else if (ret <= 0) {
1992 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1993 return -1; /* Non-protocol error */
1994 }
1995
1996 if (tls_wants_non_io_retry(qc)) {
1997 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1998 return -1;
1999 }
2000
2001 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
2002 return 1;
2003 }
2004
2005 if (tls_wants_non_io_retry(qc)) {
2006 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
2007 return -1;
2008 }
2009
2010 /*
2011 * Otherwise, indicate that the handshake isn't done yet.
2012 * We can only get here in non-blocking mode.
2013 */
2014 QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2015 return -1; /* Non-protocol error */
2016 }
2017
2018 QUIC_TAKES_LOCK
ossl_quic_do_handshake(SSL * s)2019 int ossl_quic_do_handshake(SSL *s)
2020 {
2021 int ret;
2022 QCTX ctx;
2023
2024 if (!expect_quic_cs(s, &ctx))
2025 return 0;
2026
2027 qctx_lock_for_io(&ctx);
2028
2029 ret = quic_do_handshake(&ctx);
2030 qctx_unlock(&ctx);
2031 return ret;
2032 }
2033
2034 /* SSL_connect */
ossl_quic_connect(SSL * s)2035 int ossl_quic_connect(SSL *s)
2036 {
2037 /* Ensure we are in connect state (no-op if non-idle). */
2038 if (!ossl_quic_set_connect_state(s, 1))
2039 return -1;
2040
2041 /* Begin or continue the handshake */
2042 return ossl_quic_do_handshake(s);
2043 }
2044
2045 /* SSL_accept */
ossl_quic_accept(SSL * s)2046 int ossl_quic_accept(SSL *s)
2047 {
2048 /* Ensure we are in accept state (no-op if non-idle). */
2049 if (!ossl_quic_set_accept_state(s, 1))
2050 return -1;
2051
2052 /* Begin or continue the handshake */
2053 return ossl_quic_do_handshake(s);
2054 }
2055
2056 /*
2057 * QUIC Front-End I/O API: Stream Lifecycle Operations
2058 * ===================================================
2059 *
2060 * SSL_stream_new => ossl_quic_conn_stream_new
2061 *
2062 */
2063
2064 /*
2065 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
2066 * default XSO was created. Returns 0 if it was not (e.g. because it already
2067 * exists). Note that this is NOT an error condition.
2068 */
2069 QUIC_NEEDS_LOCK
qc_try_create_default_xso_for_write(QCTX * ctx)2070 static int qc_try_create_default_xso_for_write(QCTX *ctx)
2071 {
2072 uint64_t flags = 0;
2073 QUIC_CONNECTION *qc = ctx->qc;
2074
2075 if (qc->default_xso_created
2076 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2077 /*
2078 * We only do this once. If the user detaches a previously created
2079 * default XSO we don't auto-create another one.
2080 */
2081 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
2082
2083 /* Create a locally-initiated stream. */
2084 if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2085 flags |= SSL_STREAM_FLAG_UNI;
2086
2087 qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
2088 /*needs_lock=*/0),
2089 /*touch=*/0);
2090 if (qc->default_xso == NULL)
2091 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2092
2093 qc_touch_default_xso(qc);
2094 return 1;
2095 }
2096
2097 struct quic_wait_for_stream_args {
2098 QUIC_CONNECTION *qc;
2099 QUIC_STREAM *qs;
2100 QCTX *ctx;
2101 uint64_t expect_id;
2102 };
2103
2104 QUIC_NEEDS_LOCK
quic_wait_for_stream(void * arg)2105 static int quic_wait_for_stream(void *arg)
2106 {
2107 struct quic_wait_for_stream_args *args = arg;
2108
2109 if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
2110 /* If connection is torn down due to an error while blocking, stop. */
2111 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2112 return -1;
2113 }
2114
2115 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
2116 args->expect_id | QUIC_STREAM_DIR_BIDI);
2117 if (args->qs == NULL)
2118 args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
2119 args->expect_id | QUIC_STREAM_DIR_UNI);
2120
2121 if (args->qs != NULL)
2122 return 1; /* stream now exists */
2123
2124 return 0; /* did not get a stream, keep trying */
2125 }
2126
2127 QUIC_NEEDS_LOCK
qc_wait_for_default_xso_for_read(QCTX * ctx,int peek)2128 static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek)
2129 {
2130 /* Called on a QCSO and we don't currently have a default stream. */
2131 uint64_t expect_id;
2132 QUIC_CONNECTION *qc = ctx->qc;
2133 QUIC_STREAM *qs;
2134 int res;
2135 struct quic_wait_for_stream_args wargs;
2136 OSSL_RTT_INFO rtt_info;
2137
2138 /*
2139 * If default stream functionality is disabled or we already detached
2140 * one, don't make another default stream and just fail.
2141 */
2142 if (qc->default_xso_created
2143 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2144 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
2145
2146 /*
2147 * The peer may have opened a stream since we last ticked. So tick and
2148 * see if the stream with ordinal 0 (remote, bidi/uni based on stream
2149 * mode) exists yet. QUIC stream IDs must be allocated in order, so the
2150 * first stream created by a peer must have an ordinal of 0.
2151 */
2152 expect_id = qc->as_server
2153 ? QUIC_STREAM_INITIATOR_CLIENT
2154 : QUIC_STREAM_INITIATOR_SERVER;
2155
2156 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2157 expect_id | QUIC_STREAM_DIR_BIDI);
2158 if (qs == NULL)
2159 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2160 expect_id | QUIC_STREAM_DIR_UNI);
2161
2162 if (qs == NULL) {
2163 qctx_maybe_autotick(ctx);
2164
2165 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2166 expect_id);
2167 }
2168
2169 if (qs == NULL) {
2170 if (peek)
2171 return 0;
2172
2173 if (ossl_quic_channel_is_term_any(qc->ch)) {
2174 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2175 } else if (!qctx_blocking(ctx)) {
2176 /* Non-blocking mode, so just bail immediately. */
2177 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2178 }
2179
2180 /* Block until we have a stream. */
2181 wargs.qc = qc;
2182 wargs.qs = NULL;
2183 wargs.ctx = ctx;
2184 wargs.expect_id = expect_id;
2185
2186 res = block_until_pred(ctx, quic_wait_for_stream, &wargs, 0);
2187 if (res == 0)
2188 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2189 else if (res < 0 || wargs.qs == NULL)
2190 /* quic_wait_for_stream raised error here */
2191 return 0;
2192
2193 qs = wargs.qs;
2194 }
2195
2196 /*
2197 * We now have qs != NULL. Remove it from the incoming stream queue so that
2198 * it isn't also returned by any future SSL_accept_stream calls.
2199 */
2200 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2201 ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc->ch),
2202 qs, rtt_info.smoothed_rtt);
2203
2204 /*
2205 * Now make qs the default stream, creating the necessary XSO.
2206 */
2207 qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
2208 if (qc->default_xso == NULL)
2209 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2210
2211 qc_touch_default_xso(qc); /* inhibits default XSO */
2212 return 1;
2213 }
2214
2215 QUIC_NEEDS_LOCK
create_xso_from_stream(QUIC_CONNECTION * qc,QUIC_STREAM * qs)2216 static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
2217 {
2218 QUIC_XSO *xso = NULL;
2219
2220 if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
2221 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
2222 goto err;
2223 }
2224
2225 if (!ossl_quic_obj_init(&xso->obj, qc->obj.ssl.ctx, SSL_TYPE_QUIC_XSO,
2226 &qc->obj.ssl, NULL, NULL)) {
2227 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
2228 goto err;
2229 }
2230
2231 /* XSO refs QC */
2232 if (!SSL_up_ref(&qc->obj.ssl)) {
2233 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);
2234 goto err;
2235 }
2236
2237 xso->conn = qc;
2238 xso->ssl_mode = qc->default_ssl_mode;
2239 xso->ssl_options
2240 = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
2241 xso->last_error = SSL_ERROR_NONE;
2242
2243 xso->stream = qs;
2244
2245 ++qc->num_xso;
2246 xso_update_options(xso);
2247 return xso;
2248
2249 err:
2250 OPENSSL_free(xso);
2251 return NULL;
2252 }
2253
2254 struct quic_new_stream_wait_args {
2255 QUIC_CONNECTION *qc;
2256 int is_uni;
2257 };
2258
quic_new_stream_wait(void * arg)2259 static int quic_new_stream_wait(void *arg)
2260 {
2261 struct quic_new_stream_wait_args *args = arg;
2262 QUIC_CONNECTION *qc = args->qc;
2263
2264 if (!quic_mutation_allowed(qc, /*req_active=*/1))
2265 return -1;
2266
2267 if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
2268 return 1;
2269
2270 return 0;
2271 }
2272
2273 /* locking depends on need_lock */
quic_conn_stream_new(QCTX * ctx,uint64_t flags,int need_lock)2274 static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
2275 {
2276 int ret;
2277 QUIC_CONNECTION *qc = ctx->qc;
2278 QUIC_XSO *xso = NULL;
2279 QUIC_STREAM *qs = NULL;
2280 int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
2281 int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
2282 int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
2283
2284 if (need_lock)
2285 qctx_lock(ctx);
2286
2287 if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
2288 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2289 goto err;
2290 }
2291
2292 if (!advance
2293 && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
2294 struct quic_new_stream_wait_args args;
2295
2296 /*
2297 * Stream count flow control currently doesn't permit this stream to be
2298 * opened.
2299 */
2300 if (no_blocking || !qctx_blocking(ctx)) {
2301 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
2302 goto err;
2303 }
2304
2305 args.qc = qc;
2306 args.is_uni = is_uni;
2307
2308 /* Blocking mode - wait until we can get a stream. */
2309 ret = block_until_pred(ctx, quic_new_stream_wait, &args, 0);
2310 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2311 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2312 goto err; /* Shutdown before completion */
2313 } else if (ret <= 0) {
2314 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2315 goto err; /* Non-protocol error */
2316 }
2317 }
2318
2319 qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
2320 if (qs == NULL) {
2321 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2322 goto err;
2323 }
2324
2325 xso = create_xso_from_stream(qc, qs);
2326 if (xso == NULL)
2327 goto err;
2328
2329 qc_touch_default_xso(qc); /* inhibits default XSO */
2330 if (need_lock)
2331 qctx_unlock(ctx);
2332
2333 return &xso->obj.ssl;
2334
2335 err:
2336 OPENSSL_free(xso);
2337 ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
2338 if (need_lock)
2339 qctx_unlock(ctx);
2340
2341 return NULL;
2342 }
2343
2344 QUIC_TAKES_LOCK
ossl_quic_conn_stream_new(SSL * s,uint64_t flags)2345 SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
2346 {
2347 QCTX ctx;
2348
2349 if (!expect_quic_conn_only(s, &ctx))
2350 return NULL;
2351
2352 return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
2353 }
2354
2355 /*
2356 * QUIC Front-End I/O API: Steady-State Operations
2357 * ===============================================
2358 *
2359 * Here we dispatch calls to the steady-state front-end I/O API functions; that
2360 * is, the functions used during the established phase of a QUIC connection
2361 * (e.g. SSL_read, SSL_write).
2362 *
2363 * Each function must handle both blocking and non-blocking modes. As discussed
2364 * above, all QUIC I/O is implemented using non-blocking mode internally.
2365 *
2366 * SSL_get_error => partially implemented by ossl_quic_get_error
2367 * SSL_want => ossl_quic_want
2368 * (BIO/)SSL_read => ossl_quic_read
2369 * (BIO/)SSL_write => ossl_quic_write
2370 * SSL_pending => ossl_quic_pending
2371 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
2372 * SSL_key_update => ossl_quic_key_update
2373 */
2374
2375 /* SSL_get_error */
ossl_quic_get_error(const SSL * s,int i)2376 int ossl_quic_get_error(const SSL *s, int i)
2377 {
2378 QCTX ctx;
2379 int net_error, last_error;
2380
2381 /* SSL_get_errors() should not raise new errors */
2382 if (!is_quic_cs(s, &ctx, 0 /* suppress errors */))
2383 return SSL_ERROR_SSL;
2384
2385 qctx_lock(&ctx);
2386 net_error = ossl_quic_channel_net_error(ctx.qc->ch);
2387 last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
2388 qctx_unlock(&ctx);
2389
2390 if (net_error)
2391 return SSL_ERROR_SYSCALL;
2392
2393 return last_error;
2394 }
2395
2396 /* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
error_to_want(int error)2397 static int error_to_want(int error)
2398 {
2399 switch (error) {
2400 case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */
2401 case SSL_ERROR_WANT_ACCEPT: /* never used - UDP is connectionless */
2402 case SSL_ERROR_ZERO_RETURN:
2403 default:
2404 return SSL_NOTHING;
2405
2406 case SSL_ERROR_WANT_READ:
2407 return SSL_READING;
2408
2409 case SSL_ERROR_WANT_WRITE:
2410 return SSL_WRITING;
2411
2412 case SSL_ERROR_WANT_RETRY_VERIFY:
2413 return SSL_RETRY_VERIFY;
2414
2415 case SSL_ERROR_WANT_CLIENT_HELLO_CB:
2416 return SSL_CLIENT_HELLO_CB;
2417
2418 case SSL_ERROR_WANT_X509_LOOKUP:
2419 return SSL_X509_LOOKUP;
2420 }
2421 }
2422
2423 /* SSL_want */
ossl_quic_want(const SSL * s)2424 int ossl_quic_want(const SSL *s)
2425 {
2426 QCTX ctx;
2427 int w;
2428
2429 if (!expect_quic_cs(s, &ctx))
2430 return SSL_NOTHING;
2431
2432 qctx_lock(&ctx);
2433
2434 w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);
2435
2436 qctx_unlock(&ctx);
2437 return w;
2438 }
2439
2440 /*
2441 * SSL_write
2442 * ---------
2443 *
2444 * The set of functions below provide the implementation of the public SSL_write
2445 * function. We must handle:
2446 *
2447 * - both blocking and non-blocking operation at the application level,
2448 * depending on how we are configured;
2449 *
2450 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
2451 *
2452 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
2453 *
2454 */
2455 QUIC_NEEDS_LOCK
quic_post_write(QUIC_XSO * xso,int did_append,int did_append_all,uint64_t flags,int do_tick)2456 static void quic_post_write(QUIC_XSO *xso, int did_append,
2457 int did_append_all, uint64_t flags,
2458 int do_tick)
2459 {
2460 /*
2461 * We have appended at least one byte to the stream.
2462 * Potentially mark stream as active, depending on FC.
2463 */
2464 if (did_append)
2465 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
2466 xso->stream);
2467
2468 if (did_append_all && (flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
2469 ossl_quic_sstream_fin(xso->stream->sstream);
2470
2471 /*
2472 * Try and send.
2473 *
2474 * TODO(QUIC FUTURE): It is probably inefficient to try and do this
2475 * immediately, plus we should eventually consider Nagle's algorithm.
2476 */
2477 if (do_tick)
2478 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
2479 }
2480
2481 struct quic_write_again_args {
2482 QUIC_XSO *xso;
2483 const unsigned char *buf;
2484 size_t len;
2485 size_t total_written;
2486 int err;
2487 uint64_t flags;
2488 };
2489
2490 /*
2491 * Absolute maximum write buffer size, enforced to prevent a rogue peer from
2492 * deliberately inducing DoS. This has been chosen based on the optimal buffer
2493 * size for an RTT of 500ms and a bandwidth of 100 Mb/s.
2494 */
2495 #define MAX_WRITE_BUF_SIZE (6 * 1024 * 1024)
2496
2497 /*
2498 * Ensure spare buffer space available (up until a limit, at least).
2499 */
2500 QUIC_NEEDS_LOCK
sstream_ensure_spare(QUIC_SSTREAM * sstream,uint64_t spare)2501 static int sstream_ensure_spare(QUIC_SSTREAM *sstream, uint64_t spare)
2502 {
2503 size_t cur_sz = ossl_quic_sstream_get_buffer_size(sstream);
2504 size_t avail = ossl_quic_sstream_get_buffer_avail(sstream);
2505 size_t spare_ = (spare > SIZE_MAX) ? SIZE_MAX : (size_t)spare;
2506 size_t new_sz, growth;
2507
2508 if (spare_ <= avail || cur_sz == MAX_WRITE_BUF_SIZE)
2509 return 1;
2510
2511 growth = spare_ - avail;
2512 if (cur_sz + growth > MAX_WRITE_BUF_SIZE)
2513 new_sz = MAX_WRITE_BUF_SIZE;
2514 else
2515 new_sz = cur_sz + growth;
2516
2517 return ossl_quic_sstream_set_buffer_size(sstream, new_sz);
2518 }
2519
2520 /*
2521 * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded
2522 * as needed according to flow control.
2523 */
2524 QUIC_NEEDS_LOCK
xso_sstream_append(QUIC_XSO * xso,const unsigned char * buf,size_t len,size_t * actual_written)2525 static int xso_sstream_append(QUIC_XSO *xso, const unsigned char *buf,
2526 size_t len, size_t *actual_written)
2527 {
2528 QUIC_SSTREAM *sstream = xso->stream->sstream;
2529 uint64_t cur = ossl_quic_sstream_get_cur_size(sstream);
2530 uint64_t cwm = ossl_quic_txfc_get_cwm(&xso->stream->txfc);
2531 uint64_t permitted = (cwm >= cur ? cwm - cur : 0);
2532
2533 if (len > permitted)
2534 len = (size_t)permitted;
2535
2536 if (!sstream_ensure_spare(sstream, len))
2537 return 0;
2538
2539 return ossl_quic_sstream_append(sstream, buf, len, actual_written);
2540 }
2541
2542 QUIC_NEEDS_LOCK
quic_write_again(void * arg)2543 static int quic_write_again(void *arg)
2544 {
2545 struct quic_write_again_args *args = arg;
2546 size_t actual_written = 0;
2547
2548 if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
2549 /* If connection is torn down due to an error while blocking, stop. */
2550 return -2;
2551
2552 if (!quic_validate_for_write(args->xso, &args->err))
2553 /*
2554 * Stream may have become invalid for write due to connection events
2555 * while we blocked.
2556 */
2557 return -2;
2558
2559 args->err = ERR_R_INTERNAL_ERROR;
2560 if (!xso_sstream_append(args->xso, args->buf, args->len, &actual_written))
2561 return -2;
2562
2563 quic_post_write(args->xso, actual_written > 0,
2564 args->len == actual_written, args->flags, 0);
2565
2566 args->buf += actual_written;
2567 args->len -= actual_written;
2568 args->total_written += actual_written;
2569
2570 if (args->len == 0)
2571 /* Written everything, done. */
2572 return 1;
2573
2574 /* Not written everything yet, keep trying. */
2575 return 0;
2576 }
2577
2578 QUIC_NEEDS_LOCK
quic_write_blocking(QCTX * ctx,const void * buf,size_t len,uint64_t flags,size_t * written)2579 static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
2580 uint64_t flags, size_t *written)
2581 {
2582 int res;
2583 QUIC_XSO *xso = ctx->xso;
2584 struct quic_write_again_args args;
2585 size_t actual_written = 0;
2586
2587 /* First make a best effort to append as much of the data as possible. */
2588 if (!xso_sstream_append(xso, buf, len, &actual_written)) {
2589 /* Stream already finished or allocation error. */
2590 *written = 0;
2591 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2592 }
2593
2594 quic_post_write(xso, actual_written > 0, actual_written == len, flags, 1);
2595
2596 /*
2597 * Record however much data we wrote
2598 */
2599 *written = actual_written;
2600
2601 if (actual_written == len) {
2602 /* Managed to append everything on the first try. */
2603 return 1;
2604 }
2605
2606 /*
2607 * We did not manage to append all of the data immediately, so the stream
2608 * buffer has probably filled up. This means we need to block until some of
2609 * it is freed up.
2610 */
2611 args.xso = xso;
2612 args.buf = (const unsigned char *)buf + actual_written;
2613 args.len = len - actual_written;
2614 args.total_written = 0;
2615 args.err = ERR_R_INTERNAL_ERROR;
2616 args.flags = flags;
2617
2618 res = block_until_pred(ctx, quic_write_again, &args, 0);
2619 if (res <= 0) {
2620 if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
2621 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2622 else
2623 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
2624 }
2625
2626 /*
2627 * When waiting on extra buffer space to be available, args.total_written
2628 * holds the amount of remaining data we requested to write, which will be
2629 * something less than the len parameter passed in, however much we wrote
2630 * here, add it to the value that we wrote when we initially called
2631 * xso_sstream_append
2632 */
2633 *written += args.total_written;
2634 return 1;
2635 }
2636
2637 /*
2638 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2639 * write semantics.
2640 */
aon_write_begin(QUIC_XSO * xso,const unsigned char * buf,size_t buf_len,size_t already_sent)2641 static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
2642 size_t buf_len, size_t already_sent)
2643 {
2644 assert(!xso->aon_write_in_progress);
2645
2646 xso->aon_write_in_progress = 1;
2647 xso->aon_buf_base = buf;
2648 xso->aon_buf_pos = already_sent;
2649 xso->aon_buf_len = buf_len;
2650 }
2651
aon_write_finish(QUIC_XSO * xso)2652 static void aon_write_finish(QUIC_XSO *xso)
2653 {
2654 xso->aon_write_in_progress = 0;
2655 xso->aon_buf_base = NULL;
2656 xso->aon_buf_pos = 0;
2657 xso->aon_buf_len = 0;
2658 }
2659
2660 QUIC_NEEDS_LOCK
quic_write_nonblocking_aon(QCTX * ctx,const void * buf,size_t len,uint64_t flags,size_t * written)2661 static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2662 size_t len, uint64_t flags,
2663 size_t *written)
2664 {
2665 QUIC_XSO *xso = ctx->xso;
2666 const void *actual_buf;
2667 size_t actual_len, actual_written = 0;
2668 int accept_moving_buffer
2669 = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2670
2671 if (xso->aon_write_in_progress) {
2672 /*
2673 * We are in the middle of an AON write (i.e., a previous write did not
2674 * manage to append all data to the SSTREAM and we have Enable Partial
2675 * Write (EPW) mode disabled.)
2676 */
2677 if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2678 || len != xso->aon_buf_len)
2679 /*
2680 * Pointer must not have changed if we are not in accept moving
2681 * buffer mode. Length must never change.
2682 */
2683 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2684
2685 actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2686 actual_len = len - xso->aon_buf_pos;
2687 assert(actual_len > 0);
2688 } else {
2689 actual_buf = buf;
2690 actual_len = len;
2691 }
2692
2693 /* First make a best effort to append as much of the data as possible. */
2694 if (!xso_sstream_append(xso, actual_buf, actual_len, &actual_written)) {
2695 /* Stream already finished or allocation error. */
2696 *written = 0;
2697 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2698 }
2699
2700 quic_post_write(xso, actual_written > 0, actual_written == actual_len,
2701 flags, qctx_should_autotick(ctx));
2702
2703 if (actual_written == actual_len) {
2704 /* We have sent everything. */
2705 if (xso->aon_write_in_progress) {
2706 /*
2707 * We have sent everything, and we were in the middle of an AON
2708 * write. The output write length is the total length of the AON
2709 * buffer, not however many bytes we managed to write to the stream
2710 * in this call.
2711 */
2712 *written = xso->aon_buf_len;
2713 aon_write_finish(xso);
2714 } else {
2715 *written = actual_written;
2716 }
2717
2718 return 1;
2719 }
2720
2721 if (xso->aon_write_in_progress) {
2722 /*
2723 * AON write is in progress but we have not written everything yet. We
2724 * may have managed to send zero bytes, or some number of bytes less
2725 * than the total remaining which need to be appended during this
2726 * AON operation.
2727 */
2728 xso->aon_buf_pos += actual_written;
2729 assert(xso->aon_buf_pos < xso->aon_buf_len);
2730 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2731 }
2732
2733 /*
2734 * Not in an existing AON operation but partial write is not enabled, so we
2735 * need to begin a new AON operation. However we needn't bother if we didn't
2736 * actually append anything.
2737 */
2738 if (actual_written > 0)
2739 aon_write_begin(xso, buf, len, actual_written);
2740
2741 /*
2742 * AON - We do not publicly admit to having appended anything until AON
2743 * completes.
2744 */
2745 *written = 0;
2746 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2747 }
2748
2749 QUIC_NEEDS_LOCK
quic_write_nonblocking_epw(QCTX * ctx,const void * buf,size_t len,uint64_t flags,size_t * written)2750 static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2751 uint64_t flags, size_t *written)
2752 {
2753 QUIC_XSO *xso = ctx->xso;
2754
2755 /* Simple best effort operation. */
2756 if (!xso_sstream_append(xso, buf, len, written)) {
2757 /* Stream already finished or allocation error. */
2758 *written = 0;
2759 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2760 }
2761
2762 quic_post_write(xso, *written > 0, *written == len, flags,
2763 qctx_should_autotick(ctx));
2764
2765 if (*written == 0)
2766 /* SSL_write_ex returns 0 if it didn't write anything. */
2767 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2768
2769 return 1;
2770 }
2771
2772 QUIC_NEEDS_LOCK
quic_validate_for_write(QUIC_XSO * xso,int * err)2773 static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2774 {
2775 QUIC_STREAM_MAP *qsm;
2776
2777 if (xso == NULL || xso->stream == NULL) {
2778 *err = ERR_R_INTERNAL_ERROR;
2779 return 0;
2780 }
2781
2782 switch (xso->stream->send_state) {
2783 default:
2784 case QUIC_SSTREAM_STATE_NONE:
2785 *err = SSL_R_STREAM_RECV_ONLY;
2786 return 0;
2787
2788 case QUIC_SSTREAM_STATE_READY:
2789 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2790
2791 if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2792 *err = ERR_R_INTERNAL_ERROR;
2793 return 0;
2794 }
2795
2796 /* FALLTHROUGH */
2797 case QUIC_SSTREAM_STATE_SEND:
2798 case QUIC_SSTREAM_STATE_DATA_SENT:
2799 if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2800 *err = SSL_R_STREAM_FINISHED;
2801 return 0;
2802 }
2803 return 1;
2804
2805 case QUIC_SSTREAM_STATE_DATA_RECVD:
2806 *err = SSL_R_STREAM_FINISHED;
2807 return 0;
2808
2809 case QUIC_SSTREAM_STATE_RESET_SENT:
2810 case QUIC_SSTREAM_STATE_RESET_RECVD:
2811 *err = SSL_R_STREAM_RESET;
2812 return 0;
2813 }
2814 }
2815
2816 QUIC_TAKES_LOCK
ossl_quic_write_flags(SSL * s,const void * buf,size_t len,uint64_t flags,size_t * written)2817 int ossl_quic_write_flags(SSL *s, const void *buf, size_t len,
2818 uint64_t flags, size_t *written)
2819 {
2820 int ret;
2821 QCTX ctx;
2822 int partial_write, err;
2823
2824 *written = 0;
2825
2826 if (len == 0) {
2827 /* Do not autocreate default XSO for zero-length writes. */
2828 if (!expect_quic_cs(s, &ctx))
2829 return 0;
2830
2831 qctx_lock_for_io(&ctx);
2832 } else {
2833 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))
2834 return 0;
2835 }
2836
2837 partial_write = ((ctx.xso != NULL)
2838 ? ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)
2839 : 0);
2840
2841 if ((flags & ~SSL_WRITE_FLAG_CONCLUDE) != 0) {
2842 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_UNSUPPORTED_WRITE_FLAG, NULL);
2843 goto out;
2844 }
2845
2846 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2847 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2848 goto out;
2849 }
2850
2851 /*
2852 * If we haven't finished the handshake, try to advance it.
2853 * We don't accept writes until the handshake is completed.
2854 */
2855 if (quic_do_handshake(&ctx) < 1) {
2856 ret = 0;
2857 goto out;
2858 }
2859
2860 /* Ensure correct stream state, stream send part not concluded, etc. */
2861 if (len > 0 && !quic_validate_for_write(ctx.xso, &err)) {
2862 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2863 goto out;
2864 }
2865
2866 if (len == 0) {
2867 if ((flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
2868 quic_post_write(ctx.xso, 0, 1, flags,
2869 qctx_should_autotick(&ctx));
2870
2871 ret = 1;
2872 goto out;
2873 }
2874
2875 if (qctx_blocking(&ctx))
2876 ret = quic_write_blocking(&ctx, buf, len, flags, written);
2877 else if (partial_write)
2878 ret = quic_write_nonblocking_epw(&ctx, buf, len, flags, written);
2879 else
2880 ret = quic_write_nonblocking_aon(&ctx, buf, len, flags, written);
2881
2882 out:
2883 qctx_unlock(&ctx);
2884 return ret;
2885 }
2886
2887 QUIC_TAKES_LOCK
ossl_quic_write(SSL * s,const void * buf,size_t len,size_t * written)2888 int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2889 {
2890 return ossl_quic_write_flags(s, buf, len, 0, written);
2891 }
2892
2893 /*
2894 * SSL_read
2895 * --------
2896 */
2897 struct quic_read_again_args {
2898 QCTX *ctx;
2899 QUIC_STREAM *stream;
2900 void *buf;
2901 size_t len;
2902 size_t *bytes_read;
2903 int peek;
2904 };
2905
2906 QUIC_NEEDS_LOCK
quic_validate_for_read(QUIC_XSO * xso,int * err,int * eos)2907 static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2908 {
2909 QUIC_STREAM_MAP *qsm;
2910
2911 *eos = 0;
2912
2913 if (xso == NULL || xso->stream == NULL) {
2914 *err = ERR_R_INTERNAL_ERROR;
2915 return 0;
2916 }
2917
2918 switch (xso->stream->recv_state) {
2919 default:
2920 case QUIC_RSTREAM_STATE_NONE:
2921 *err = SSL_R_STREAM_SEND_ONLY;
2922 return 0;
2923
2924 case QUIC_RSTREAM_STATE_RECV:
2925 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2926 case QUIC_RSTREAM_STATE_DATA_RECVD:
2927 return 1;
2928
2929 case QUIC_RSTREAM_STATE_DATA_READ:
2930 *eos = 1;
2931 return 0;
2932
2933 case QUIC_RSTREAM_STATE_RESET_RECVD:
2934 qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2935 ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2936
2937 /* FALLTHROUGH */
2938 case QUIC_RSTREAM_STATE_RESET_READ:
2939 *err = SSL_R_STREAM_RESET;
2940 return 0;
2941 }
2942 }
2943
2944 QUIC_NEEDS_LOCK
quic_read_actual(QCTX * ctx,QUIC_STREAM * stream,void * buf,size_t buf_len,size_t * bytes_read,int peek)2945 static int quic_read_actual(QCTX *ctx,
2946 QUIC_STREAM *stream,
2947 void *buf, size_t buf_len,
2948 size_t *bytes_read,
2949 int peek)
2950 {
2951 int is_fin = 0, err, eos;
2952 QUIC_CONNECTION *qc = ctx->qc;
2953
2954 if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2955 if (eos) {
2956 ctx->xso->retired_fin = 1;
2957 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2958 } else {
2959 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2960 }
2961 }
2962
2963 if (peek) {
2964 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2965 bytes_read, &is_fin))
2966 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2967
2968 } else {
2969 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2970 bytes_read, &is_fin))
2971 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2972 }
2973
2974 if (!peek) {
2975 if (*bytes_read > 0) {
2976 /*
2977 * We have read at least one byte from the stream. Inform stream-level
2978 * RXFC of the retirement of controlled bytes. Update the active stream
2979 * status (the RXFC may now want to emit a frame granting more credit to
2980 * the peer).
2981 */
2982 OSSL_RTT_INFO rtt_info;
2983
2984 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2985
2986 if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2987 rtt_info.smoothed_rtt))
2988 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2989 }
2990
2991 if (is_fin && !peek) {
2992 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2993
2994 ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2995 }
2996
2997 if (*bytes_read > 0)
2998 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2999 stream);
3000 }
3001
3002 if (*bytes_read == 0 && is_fin) {
3003 ctx->xso->retired_fin = 1;
3004 return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
3005 }
3006
3007 return 1;
3008 }
3009
3010 QUIC_NEEDS_LOCK
quic_read_again(void * arg)3011 static int quic_read_again(void *arg)
3012 {
3013 struct quic_read_again_args *args = arg;
3014
3015 if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
3016 /* If connection is torn down due to an error while blocking, stop. */
3017 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3018 return -1;
3019 }
3020
3021 if (!quic_read_actual(args->ctx, args->stream,
3022 args->buf, args->len, args->bytes_read,
3023 args->peek))
3024 return -1;
3025
3026 if (*args->bytes_read > 0)
3027 /* got at least one byte, the SSL_read op can finish now */
3028 return 1;
3029
3030 return 0; /* did not read anything, keep trying */
3031 }
3032
3033 QUIC_TAKES_LOCK
quic_read(SSL * s,void * buf,size_t len,size_t * bytes_read,int peek)3034 static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
3035 {
3036 int ret, res;
3037 QCTX ctx;
3038 struct quic_read_again_args args;
3039
3040 *bytes_read = 0;
3041
3042 if (!expect_quic_cs(s, &ctx))
3043 return 0;
3044
3045 qctx_lock_for_io(&ctx);
3046
3047 /* If we haven't finished the handshake, try to advance it. */
3048 if (quic_do_handshake(&ctx) < 1) {
3049 ret = 0; /* ossl_quic_do_handshake raised error here */
3050 goto out;
3051 }
3052
3053 if (ctx.xso == NULL) {
3054 /*
3055 * Called on a QCSO and we don't currently have a default stream.
3056 *
3057 * Wait until we get a stream initiated by the peer (blocking mode) or
3058 * fail if we don't have one yet (non-blocking mode).
3059 */
3060 if (!qc_wait_for_default_xso_for_read(&ctx, /*peek=*/0)) {
3061 ret = 0; /* error already raised here */
3062 goto out;
3063 }
3064
3065 ctx.xso = ctx.qc->default_xso;
3066 }
3067
3068 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
3069 ret = 0; /* quic_read_actual raised error here */
3070 goto out;
3071 }
3072
3073 if (*bytes_read > 0) {
3074 /*
3075 * Even though we succeeded, tick the reactor here to ensure we are
3076 * handling other aspects of the QUIC connection.
3077 */
3078 if (quic_mutation_allowed(ctx.qc, /*req_active=*/0))
3079 qctx_maybe_autotick(&ctx);
3080
3081 ret = 1;
3082 } else if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
3083 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3084 goto out;
3085 } else if (qctx_blocking(&ctx)) {
3086 /*
3087 * We were not able to read anything immediately, so our stream
3088 * buffer is empty. This means we need to block until we get
3089 * at least one byte.
3090 */
3091 args.ctx = &ctx;
3092 args.stream = ctx.xso->stream;
3093 args.buf = buf;
3094 args.len = len;
3095 args.bytes_read = bytes_read;
3096 args.peek = peek;
3097
3098 res = block_until_pred(&ctx, quic_read_again, &args, 0);
3099 if (res == 0) {
3100 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3101 goto out;
3102 } else if (res < 0) {
3103 ret = 0; /* quic_read_again raised error here */
3104 goto out;
3105 }
3106
3107 ret = 1;
3108 } else {
3109 /*
3110 * We did not get any bytes and are not in blocking mode.
3111 * Tick to see if this delivers any more.
3112 */
3113 qctx_maybe_autotick(&ctx);
3114
3115 /* Try the read again. */
3116 if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
3117 ret = 0; /* quic_read_actual raised error here */
3118 goto out;
3119 }
3120
3121 if (*bytes_read > 0)
3122 ret = 1; /* Succeeded this time. */
3123 else
3124 ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
3125 }
3126
3127 out:
3128 qctx_unlock(&ctx);
3129 return ret;
3130 }
3131
ossl_quic_read(SSL * s,void * buf,size_t len,size_t * bytes_read)3132 int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
3133 {
3134 return quic_read(s, buf, len, bytes_read, 0);
3135 }
3136
ossl_quic_peek(SSL * s,void * buf,size_t len,size_t * bytes_read)3137 int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
3138 {
3139 return quic_read(s, buf, len, bytes_read, 1);
3140 }
3141
3142 /*
3143 * SSL_pending
3144 * -----------
3145 */
3146
3147 QUIC_TAKES_LOCK
ossl_quic_pending_int(const SSL * s,int check_channel)3148 static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
3149 {
3150 QCTX ctx;
3151 size_t avail = 0;
3152
3153 if (!expect_quic_cs(s, &ctx))
3154 return 0;
3155
3156 qctx_lock(&ctx);
3157
3158 if (!ctx.qc->started)
3159 goto out;
3160
3161 if (ctx.xso == NULL) {
3162 /* No XSO yet, but there might be a default XSO eligible to be created. */
3163 if (qc_wait_for_default_xso_for_read(&ctx, /*peek=*/1)) {
3164 ctx.xso = ctx.qc->default_xso;
3165 } else {
3166 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
3167 goto out;
3168 }
3169 }
3170
3171 if (ctx.xso->stream == NULL) {
3172 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3173 goto out;
3174 }
3175
3176 if (check_channel)
3177 avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
3178 /*include_fin=*/1)
3179 || ossl_quic_channel_has_pending(ctx.qc->ch)
3180 || ossl_quic_channel_is_term_any(ctx.qc->ch);
3181 else
3182 avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
3183 /*include_fin=*/0);
3184
3185 out:
3186 qctx_unlock(&ctx);
3187 return avail;
3188 }
3189
ossl_quic_pending(const SSL * s)3190 size_t ossl_quic_pending(const SSL *s)
3191 {
3192 return ossl_quic_pending_int(s, /*check_channel=*/0);
3193 }
3194
ossl_quic_has_pending(const SSL * s)3195 int ossl_quic_has_pending(const SSL *s)
3196 {
3197 /* Do we have app-side pending data or pending URXEs or RXEs? */
3198 return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
3199 }
3200
3201 /*
3202 * SSL_stream_conclude
3203 * -------------------
3204 */
3205 QUIC_TAKES_LOCK
ossl_quic_conn_stream_conclude(SSL * s)3206 int ossl_quic_conn_stream_conclude(SSL *s)
3207 {
3208 QCTX ctx;
3209 QUIC_STREAM *qs;
3210 int err;
3211 int ret;
3212
3213 if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))
3214 return 0;
3215
3216 qs = ctx.xso->stream;
3217
3218 if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
3219 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3220 qctx_unlock(&ctx);
3221 return ret;
3222 }
3223
3224 if (!quic_validate_for_write(ctx.xso, &err)) {
3225 ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
3226 qctx_unlock(&ctx);
3227 return ret;
3228 }
3229
3230 if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
3231 qctx_unlock(&ctx);
3232 return 1;
3233 }
3234
3235 ossl_quic_sstream_fin(qs->sstream);
3236 quic_post_write(ctx.xso, 1, 0, 0, qctx_should_autotick(&ctx));
3237 qctx_unlock(&ctx);
3238 return 1;
3239 }
3240
3241 /*
3242 * SSL_inject_net_dgram
3243 * --------------------
3244 */
3245 QUIC_TAKES_LOCK
SSL_inject_net_dgram(SSL * s,const unsigned char * buf,size_t buf_len,const BIO_ADDR * peer,const BIO_ADDR * local)3246 int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
3247 size_t buf_len,
3248 const BIO_ADDR *peer,
3249 const BIO_ADDR *local)
3250 {
3251 int ret = 0;
3252 QCTX ctx;
3253 QUIC_DEMUX *demux;
3254 QUIC_PORT *port;
3255
3256 if (!expect_quic_csl(s, &ctx))
3257 return 0;
3258
3259 qctx_lock(&ctx);
3260
3261 port = ossl_quic_obj_get0_port(ctx.obj);
3262 if (port == NULL) {
3263 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
3264 goto err;
3265 }
3266
3267 demux = ossl_quic_port_get0_demux(port);
3268 ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
3269
3270 err:
3271 qctx_unlock(&ctx);
3272 return ret;
3273 }
3274
3275 /*
3276 * SSL_get0_connection
3277 * -------------------
3278 */
ossl_quic_get0_connection(SSL * s)3279 SSL *ossl_quic_get0_connection(SSL *s)
3280 {
3281 QCTX ctx;
3282
3283 if (!expect_quic_cs(s, &ctx))
3284 return NULL;
3285
3286 return &ctx.qc->obj.ssl;
3287 }
3288
3289 /*
3290 * SSL_get0_listener
3291 * -----------------
3292 */
ossl_quic_get0_listener(SSL * s)3293 SSL *ossl_quic_get0_listener(SSL *s)
3294 {
3295 QCTX ctx;
3296
3297 if (!expect_quic_csl(s, &ctx))
3298 return NULL;
3299
3300 return ctx.ql != NULL ? &ctx.ql->obj.ssl : NULL;
3301 }
3302
3303 /*
3304 * SSL_get0_domain
3305 * ---------------
3306 */
ossl_quic_get0_domain(SSL * s)3307 SSL *ossl_quic_get0_domain(SSL *s)
3308 {
3309 QCTX ctx;
3310
3311 if (!expect_quic_any(s, &ctx))
3312 return NULL;
3313
3314 return ctx.qd != NULL ? &ctx.qd->obj.ssl : NULL;
3315 }
3316
3317 /*
3318 * SSL_get_domain_flags
3319 * --------------------
3320 */
ossl_quic_get_domain_flags(const SSL * ssl,uint64_t * domain_flags)3321 int ossl_quic_get_domain_flags(const SSL *ssl, uint64_t *domain_flags)
3322 {
3323 QCTX ctx;
3324
3325 if (!expect_quic_any(ssl, &ctx))
3326 return 0;
3327
3328 if (domain_flags != NULL)
3329 *domain_flags = ctx.obj->domain_flags;
3330
3331 return 1;
3332 }
3333
3334 /*
3335 * SSL_get_stream_type
3336 * -------------------
3337 */
ossl_quic_get_stream_type(SSL * s)3338 int ossl_quic_get_stream_type(SSL *s)
3339 {
3340 QCTX ctx;
3341
3342 if (!expect_quic_cs(s, &ctx))
3343 return SSL_STREAM_TYPE_BIDI;
3344
3345 if (ctx.xso == NULL) {
3346 /*
3347 * If deferred XSO creation has yet to occur, proceed according to the
3348 * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
3349 * what kind of stream will be created yet, so return BIDI on the basis
3350 * that at this time, the client still has the option of calling
3351 * SSL_read() or SSL_write() first.
3352 */
3353 if (ctx.qc->default_xso_created
3354 || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3355 return SSL_STREAM_TYPE_NONE;
3356 else
3357 return SSL_STREAM_TYPE_BIDI;
3358 }
3359
3360 if (ossl_quic_stream_is_bidi(ctx.xso->stream))
3361 return SSL_STREAM_TYPE_BIDI;
3362
3363 if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
3364 return SSL_STREAM_TYPE_READ;
3365 else
3366 return SSL_STREAM_TYPE_WRITE;
3367 }
3368
3369 /*
3370 * SSL_get_stream_id
3371 * -----------------
3372 */
3373 QUIC_TAKES_LOCK
ossl_quic_get_stream_id(SSL * s)3374 uint64_t ossl_quic_get_stream_id(SSL *s)
3375 {
3376 QCTX ctx;
3377 uint64_t id;
3378
3379 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
3380 return UINT64_MAX;
3381
3382 id = ctx.xso->stream->id;
3383 qctx_unlock(&ctx);
3384
3385 return id;
3386 }
3387
3388 /*
3389 * SSL_is_stream_local
3390 * -------------------
3391 */
3392 QUIC_TAKES_LOCK
ossl_quic_is_stream_local(SSL * s)3393 int ossl_quic_is_stream_local(SSL *s)
3394 {
3395 QCTX ctx;
3396 int is_local;
3397
3398 if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
3399 return -1;
3400
3401 is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);
3402 qctx_unlock(&ctx);
3403
3404 return is_local;
3405 }
3406
3407 /*
3408 * SSL_set_default_stream_mode
3409 * ---------------------------
3410 */
3411 QUIC_TAKES_LOCK
ossl_quic_set_default_stream_mode(SSL * s,uint32_t mode)3412 int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
3413 {
3414 QCTX ctx;
3415
3416 if (!expect_quic_conn_only(s, &ctx))
3417 return 0;
3418
3419 qctx_lock(&ctx);
3420
3421 if (ctx.qc->default_xso_created) {
3422 qctx_unlock(&ctx);
3423 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3424 "too late to change default stream mode");
3425 }
3426
3427 switch (mode) {
3428 case SSL_DEFAULT_STREAM_MODE_NONE:
3429 case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
3430 case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
3431 ctx.qc->default_stream_mode = mode;
3432 break;
3433 default:
3434 qctx_unlock(&ctx);
3435 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3436 "bad default stream type");
3437 }
3438
3439 qctx_unlock(&ctx);
3440 return 1;
3441 }
3442
3443 /*
3444 * SSL_detach_stream
3445 * -----------------
3446 */
3447 QUIC_TAKES_LOCK
ossl_quic_detach_stream(SSL * s)3448 SSL *ossl_quic_detach_stream(SSL *s)
3449 {
3450 QCTX ctx;
3451 QUIC_XSO *xso = NULL;
3452
3453 if (!expect_quic_conn_only(s, &ctx))
3454 return NULL;
3455
3456 qctx_lock(&ctx);
3457
3458 /* Calling this function inhibits default XSO autocreation. */
3459 /* QC ref to any default XSO is transferred to us and to caller. */
3460 qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
3461
3462 qctx_unlock(&ctx);
3463
3464 return xso != NULL ? &xso->obj.ssl : NULL;
3465 }
3466
3467 /*
3468 * SSL_attach_stream
3469 * -----------------
3470 */
3471 QUIC_TAKES_LOCK
ossl_quic_attach_stream(SSL * conn,SSL * stream)3472 int ossl_quic_attach_stream(SSL *conn, SSL *stream)
3473 {
3474 QCTX ctx;
3475 QUIC_XSO *xso;
3476 int nref;
3477
3478 if (!expect_quic_conn_only(conn, &ctx))
3479 return 0;
3480
3481 if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
3482 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
3483 "stream to attach must be a valid QUIC stream");
3484
3485 xso = (QUIC_XSO *)stream;
3486
3487 qctx_lock(&ctx);
3488
3489 if (ctx.qc->default_xso != NULL) {
3490 qctx_unlock(&ctx);
3491 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3492 "connection already has a default stream");
3493 }
3494
3495 /*
3496 * It is a caller error for the XSO being attached as a default XSO to have
3497 * more than one ref.
3498 */
3499 if (!CRYPTO_GET_REF(&xso->obj.ssl.references, &nref)) {
3500 qctx_unlock(&ctx);
3501 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
3502 "ref");
3503 }
3504
3505 if (nref != 1) {
3506 qctx_unlock(&ctx);
3507 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3508 "stream being attached must have "
3509 "only 1 reference");
3510 }
3511
3512 /* Caller's reference to the XSO is transferred to us. */
3513 /* Calling this function inhibits default XSO autocreation. */
3514 qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
3515
3516 qctx_unlock(&ctx);
3517 return 1;
3518 }
3519
3520 /*
3521 * SSL_set_incoming_stream_policy
3522 * ------------------------------
3523 */
3524 QUIC_NEEDS_LOCK
qc_get_effective_incoming_stream_policy(QUIC_CONNECTION * qc)3525 static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
3526 {
3527 switch (qc->incoming_stream_policy) {
3528 case SSL_INCOMING_STREAM_POLICY_AUTO:
3529 if ((qc->default_xso == NULL && !qc->default_xso_created)
3530 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3531 return SSL_INCOMING_STREAM_POLICY_ACCEPT;
3532 else
3533 return SSL_INCOMING_STREAM_POLICY_REJECT;
3534
3535 default:
3536 return qc->incoming_stream_policy;
3537 }
3538 }
3539
3540 QUIC_NEEDS_LOCK
qc_update_reject_policy(QUIC_CONNECTION * qc)3541 static void qc_update_reject_policy(QUIC_CONNECTION *qc)
3542 {
3543 int policy = qc_get_effective_incoming_stream_policy(qc);
3544 int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
3545
3546 ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
3547 enable_reject,
3548 qc->incoming_stream_aec);
3549 }
3550
3551 QUIC_TAKES_LOCK
ossl_quic_set_incoming_stream_policy(SSL * s,int policy,uint64_t aec)3552 int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
3553 uint64_t aec)
3554 {
3555 int ret = 1;
3556 QCTX ctx;
3557
3558 if (!expect_quic_conn_only(s, &ctx))
3559 return 0;
3560
3561 qctx_lock(&ctx);
3562
3563 switch (policy) {
3564 case SSL_INCOMING_STREAM_POLICY_AUTO:
3565 case SSL_INCOMING_STREAM_POLICY_ACCEPT:
3566 case SSL_INCOMING_STREAM_POLICY_REJECT:
3567 ctx.qc->incoming_stream_policy = policy;
3568 ctx.qc->incoming_stream_aec = aec;
3569 break;
3570
3571 default:
3572 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3573 ret = 0;
3574 break;
3575 }
3576
3577 qc_update_reject_policy(ctx.qc);
3578 qctx_unlock(&ctx);
3579 return ret;
3580 }
3581
3582 /*
3583 * SSL_get_value, SSL_set_value
3584 * ----------------------------
3585 */
3586 QUIC_TAKES_LOCK
qc_getset_idle_timeout(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,uint64_t * p_value_in)3587 static int qc_getset_idle_timeout(QCTX *ctx, uint32_t class_,
3588 uint64_t *p_value_out, uint64_t *p_value_in)
3589 {
3590 int ret = 0;
3591 uint64_t value_out = 0, value_in;
3592
3593 qctx_lock(ctx);
3594
3595 switch (class_) {
3596 case SSL_VALUE_CLASS_FEATURE_REQUEST:
3597 value_out = ossl_quic_channel_get_max_idle_timeout_request(ctx->qc->ch);
3598
3599 if (p_value_in != NULL) {
3600 value_in = *p_value_in;
3601 if (value_in > OSSL_QUIC_VLINT_MAX) {
3602 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3603 NULL);
3604 goto err;
3605 }
3606
3607 if (ossl_quic_channel_have_generated_transport_params(ctx->qc->ch)) {
3608 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NOT_RENEGOTIABLE,
3609 NULL);
3610 goto err;
3611 }
3612
3613 ossl_quic_channel_set_max_idle_timeout_request(ctx->qc->ch, value_in);
3614 }
3615 break;
3616
3617 case SSL_VALUE_CLASS_FEATURE_PEER_REQUEST:
3618 case SSL_VALUE_CLASS_FEATURE_NEGOTIATED:
3619 if (p_value_in != NULL) {
3620 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_OP,
3621 NULL);
3622 goto err;
3623 }
3624
3625 if (!ossl_quic_channel_is_handshake_complete(ctx->qc->ch)) {
3626 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NEGOTIATION_NOT_COMPLETE,
3627 NULL);
3628 goto err;
3629 }
3630
3631 value_out = (class_ == SSL_VALUE_CLASS_FEATURE_NEGOTIATED)
3632 ? ossl_quic_channel_get_max_idle_timeout_actual(ctx->qc->ch)
3633 : ossl_quic_channel_get_max_idle_timeout_peer_request(ctx->qc->ch);
3634 break;
3635
3636 default:
3637 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3638 NULL);
3639 goto err;
3640 }
3641
3642 ret = 1;
3643 err:
3644 qctx_unlock(ctx);
3645 if (ret && p_value_out != NULL)
3646 *p_value_out = value_out;
3647
3648 return ret;
3649 }
3650
3651 QUIC_TAKES_LOCK
qc_getset_max_pending_channels(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,uint64_t * p_value_in)3652 static int qc_getset_max_pending_channels(QCTX *ctx, uint32_t class_,
3653 uint64_t *p_value_out, uint64_t *p_value_in)
3654 {
3655 int ret = 0;
3656 uint64_t value_out = 0;
3657
3658 qctx_lock(ctx);
3659
3660 if (class_ == SSL_VALUE_CLASS_GENERIC && ctx->is_listener) {
3661 value_out = ossl_quic_port_get_max_pending_channels(ctx->ql->port);
3662 if (p_value_in != NULL)
3663 ossl_quic_port_set_max_pending_channels(ctx->ql->port, *p_value_in);
3664 ret = 1;
3665 } else {
3666 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS, NULL);
3667 ret = 0;
3668 }
3669
3670 qctx_unlock(ctx);
3671
3672 if (ret && p_value_out != NULL)
3673 *p_value_out = value_out;
3674
3675 return ret;
3676 }
3677
3678 QUIC_TAKES_LOCK
qc_get_stream_avail(QCTX * ctx,uint32_t class_,int is_uni,int is_remote,uint64_t * value)3679 static int qc_get_stream_avail(QCTX *ctx, uint32_t class_,
3680 int is_uni, int is_remote,
3681 uint64_t *value)
3682 {
3683 int ret = 0;
3684
3685 if (class_ != SSL_VALUE_CLASS_GENERIC) {
3686 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3687 NULL);
3688 return 0;
3689 }
3690
3691 qctx_lock(ctx);
3692
3693 *value = is_remote
3694 ? ossl_quic_channel_get_remote_stream_count_avail(ctx->qc->ch, is_uni)
3695 : ossl_quic_channel_get_local_stream_count_avail(ctx->qc->ch, is_uni);
3696
3697 ret = 1;
3698 qctx_unlock(ctx);
3699 return ret;
3700 }
3701
3702 QUIC_NEEDS_LOCK
qctx_should_autotick(QCTX * ctx)3703 static int qctx_should_autotick(QCTX *ctx)
3704 {
3705 int event_handling_mode;
3706 QUIC_OBJ *obj = ctx->obj;
3707
3708 for (; (event_handling_mode = obj->event_handling_mode) == SSL_VALUE_EVENT_HANDLING_MODE_INHERIT
3709 && obj->parent_obj != NULL;
3710 obj = obj->parent_obj)
3711 ;
3712
3713 return event_handling_mode != SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT;
3714 }
3715
3716 QUIC_NEEDS_LOCK
qctx_maybe_autotick(QCTX * ctx)3717 static void qctx_maybe_autotick(QCTX *ctx)
3718 {
3719 if (!qctx_should_autotick(ctx))
3720 return;
3721
3722 ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx->obj), 0);
3723 }
3724
3725 QUIC_TAKES_LOCK
qc_getset_event_handling(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,uint64_t * p_value_in)3726 static int qc_getset_event_handling(QCTX *ctx, uint32_t class_,
3727 uint64_t *p_value_out,
3728 uint64_t *p_value_in)
3729 {
3730 int ret = 0;
3731 uint64_t value_out = 0;
3732
3733 qctx_lock(ctx);
3734
3735 if (class_ != SSL_VALUE_CLASS_GENERIC) {
3736 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3737 NULL);
3738 goto err;
3739 }
3740
3741 if (p_value_in != NULL) {
3742 switch (*p_value_in) {
3743 case SSL_VALUE_EVENT_HANDLING_MODE_INHERIT:
3744 case SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT:
3745 case SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT:
3746 break;
3747 default:
3748 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3749 NULL);
3750 goto err;
3751 }
3752
3753 value_out = *p_value_in;
3754 ctx->obj->event_handling_mode = (int)value_out;
3755 } else {
3756 value_out = ctx->obj->event_handling_mode;
3757 }
3758
3759 ret = 1;
3760 err:
3761 qctx_unlock(ctx);
3762 if (ret && p_value_out != NULL)
3763 *p_value_out = value_out;
3764
3765 return ret;
3766 }
3767
3768 QUIC_TAKES_LOCK
qc_get_stream_write_buf_stat(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,size_t (* getter)(QUIC_SSTREAM * sstream))3769 static int qc_get_stream_write_buf_stat(QCTX *ctx, uint32_t class_,
3770 uint64_t *p_value_out,
3771 size_t (*getter)(QUIC_SSTREAM *sstream))
3772 {
3773 int ret = 0;
3774 size_t value = 0;
3775
3776 qctx_lock(ctx);
3777
3778 if (class_ != SSL_VALUE_CLASS_GENERIC) {
3779 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3780 NULL);
3781 goto err;
3782 }
3783
3784 if (ctx->xso == NULL) {
3785 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
3786 goto err;
3787 }
3788
3789 if (!ossl_quic_stream_has_send(ctx->xso->stream)) {
3790 QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_RECV_ONLY, NULL);
3791 goto err;
3792 }
3793
3794 if (ossl_quic_stream_has_send_buffer(ctx->xso->stream))
3795 value = getter(ctx->xso->stream->sstream);
3796
3797 ret = 1;
3798 err:
3799 qctx_unlock(ctx);
3800 *p_value_out = (uint64_t)value;
3801 return ret;
3802 }
3803
3804 QUIC_NEEDS_LOCK
expect_quic_for_value(SSL * s,QCTX * ctx,uint32_t id)3805 static int expect_quic_for_value(SSL *s, QCTX *ctx, uint32_t id)
3806 {
3807 switch (id) {
3808 case SSL_VALUE_EVENT_HANDLING_MODE:
3809 case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
3810 case SSL_VALUE_STREAM_WRITE_BUF_USED:
3811 case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
3812 return expect_quic_cs(s, ctx);
3813 case SSL_VALUE_QUIC_MAX_PENDING_CONNS:
3814 return expect_quic_cl(s, ctx);
3815 default:
3816 return expect_quic_conn_only(s, ctx);
3817 }
3818 }
3819
3820 QUIC_TAKES_LOCK
ossl_quic_get_value_uint(SSL * s,uint32_t class_,uint32_t id,uint64_t * value)3821 int ossl_quic_get_value_uint(SSL *s, uint32_t class_, uint32_t id,
3822 uint64_t *value)
3823 {
3824 QCTX ctx;
3825
3826 if (!expect_quic_for_value(s, &ctx, id))
3827 return 0;
3828
3829 if (value == NULL)
3830 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3831 ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3832
3833 switch (id) {
3834 case SSL_VALUE_QUIC_IDLE_TIMEOUT:
3835 return qc_getset_idle_timeout(&ctx, class_, value, NULL);
3836 case SSL_VALUE_QUIC_MAX_PENDING_CONNS:
3837 return qc_getset_max_pending_channels(&ctx, class_, value, NULL);
3838
3839 case SSL_VALUE_QUIC_STREAM_BIDI_LOCAL_AVAIL:
3840 return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/0, value);
3841 case SSL_VALUE_QUIC_STREAM_BIDI_REMOTE_AVAIL:
3842 return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/1, value);
3843 case SSL_VALUE_QUIC_STREAM_UNI_LOCAL_AVAIL:
3844 return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/0, value);
3845 case SSL_VALUE_QUIC_STREAM_UNI_REMOTE_AVAIL:
3846 return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/1, value);
3847
3848 case SSL_VALUE_EVENT_HANDLING_MODE:
3849 return qc_getset_event_handling(&ctx, class_, value, NULL);
3850
3851 case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
3852 return qc_get_stream_write_buf_stat(&ctx, class_, value,
3853 ossl_quic_sstream_get_buffer_size);
3854 case SSL_VALUE_STREAM_WRITE_BUF_USED:
3855 return qc_get_stream_write_buf_stat(&ctx, class_, value,
3856 ossl_quic_sstream_get_buffer_used);
3857 case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
3858 return qc_get_stream_write_buf_stat(&ctx, class_, value,
3859 ossl_quic_sstream_get_buffer_avail);
3860
3861 default:
3862 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3863 SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
3864 }
3865
3866 return 1;
3867 }
3868
3869 QUIC_TAKES_LOCK
ossl_quic_set_value_uint(SSL * s,uint32_t class_,uint32_t id,uint64_t value)3870 int ossl_quic_set_value_uint(SSL *s, uint32_t class_, uint32_t id,
3871 uint64_t value)
3872 {
3873 QCTX ctx;
3874
3875 if (!expect_quic_for_value(s, &ctx, id))
3876 return 0;
3877
3878 switch (id) {
3879 case SSL_VALUE_QUIC_IDLE_TIMEOUT:
3880 return qc_getset_idle_timeout(&ctx, class_, NULL, &value);
3881
3882 case SSL_VALUE_EVENT_HANDLING_MODE:
3883 return qc_getset_event_handling(&ctx, class_, NULL, &value);
3884 case SSL_VALUE_QUIC_MAX_PENDING_CONNS:
3885 return qc_getset_max_pending_channels(&ctx, class_, NULL, &value);
3886
3887 default:
3888 return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3889 SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
3890 }
3891
3892 return 1;
3893 }
3894
3895 /*
3896 * SSL_accept_stream
3897 * -----------------
3898 */
3899 struct wait_for_incoming_stream_args {
3900 QCTX *ctx;
3901 QUIC_STREAM *qs;
3902 };
3903
3904 QUIC_NEEDS_LOCK
wait_for_incoming_stream(void * arg)3905 static int wait_for_incoming_stream(void *arg)
3906 {
3907 struct wait_for_incoming_stream_args *args = arg;
3908 QUIC_CONNECTION *qc = args->ctx->qc;
3909 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
3910
3911 if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
3912 /* If connection is torn down due to an error while blocking, stop. */
3913 QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3914 return -1;
3915 }
3916
3917 args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3918 if (args->qs != NULL)
3919 return 1; /* got a stream */
3920
3921 return 0; /* did not get a stream, keep trying */
3922 }
3923
3924 QUIC_TAKES_LOCK
ossl_quic_accept_stream(SSL * s,uint64_t flags)3925 SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
3926 {
3927 QCTX ctx;
3928 int ret;
3929 SSL *new_s = NULL;
3930 QUIC_STREAM_MAP *qsm;
3931 QUIC_STREAM *qs;
3932 QUIC_XSO *xso;
3933 OSSL_RTT_INFO rtt_info;
3934
3935 if (!expect_quic_conn_only(s, &ctx))
3936 return NULL;
3937
3938 qctx_lock(&ctx);
3939
3940 if (qc_get_effective_incoming_stream_policy(ctx.qc)
3941 == SSL_INCOMING_STREAM_POLICY_REJECT) {
3942 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3943 goto out;
3944 }
3945
3946 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
3947
3948 qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3949 if (qs == NULL) {
3950 if (qctx_blocking(&ctx)
3951 && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
3952 struct wait_for_incoming_stream_args args;
3953
3954 args.ctx = &ctx;
3955 args.qs = NULL;
3956
3957 ret = block_until_pred(&ctx, wait_for_incoming_stream, &args, 0);
3958 if (ret == 0) {
3959 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3960 goto out;
3961 } else if (ret < 0 || args.qs == NULL) {
3962 goto out;
3963 }
3964
3965 qs = args.qs;
3966 } else {
3967 goto out;
3968 }
3969 }
3970
3971 xso = create_xso_from_stream(ctx.qc, qs);
3972 if (xso == NULL)
3973 goto out;
3974
3975 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
3976 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
3977 rtt_info.smoothed_rtt);
3978 new_s = &xso->obj.ssl;
3979
3980 /* Calling this function inhibits default XSO autocreation. */
3981 qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
3982
3983 out:
3984 qctx_unlock(&ctx);
3985 return new_s;
3986 }
3987
3988 /*
3989 * SSL_get_accept_stream_queue_len
3990 * -------------------------------
3991 */
3992 QUIC_TAKES_LOCK
ossl_quic_get_accept_stream_queue_len(SSL * s)3993 size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
3994 {
3995 QCTX ctx;
3996 size_t v;
3997
3998 if (!expect_quic_conn_only(s, &ctx))
3999 return 0;
4000
4001 qctx_lock(&ctx);
4002
4003 v = ossl_quic_stream_map_get_total_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
4004
4005 qctx_unlock(&ctx);
4006 return v;
4007 }
4008
4009 /*
4010 * SSL_stream_reset
4011 * ----------------
4012 */
ossl_quic_stream_reset(SSL * ssl,const SSL_STREAM_RESET_ARGS * args,size_t args_len)4013 int ossl_quic_stream_reset(SSL *ssl,
4014 const SSL_STREAM_RESET_ARGS *args,
4015 size_t args_len)
4016 {
4017 QCTX ctx;
4018 QUIC_STREAM_MAP *qsm;
4019 QUIC_STREAM *qs;
4020 uint64_t error_code;
4021 int ok, err;
4022
4023 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))
4024 return 0;
4025
4026 qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
4027 qs = ctx.xso->stream;
4028 error_code = (args != NULL ? args->quic_error_code : 0);
4029
4030 if (!quic_validate_for_write(ctx.xso, &err)) {
4031 ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
4032 goto err;
4033 }
4034
4035 ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
4036 if (ok)
4037 ctx.xso->requested_reset = 1;
4038
4039 err:
4040 qctx_unlock(&ctx);
4041 return ok;
4042 }
4043
4044 /*
4045 * SSL_get_stream_read_state
4046 * -------------------------
4047 */
quic_classify_stream(QUIC_CONNECTION * qc,QUIC_STREAM * qs,int is_write,int * state,uint64_t * app_error_code)4048 static void quic_classify_stream(QUIC_CONNECTION *qc,
4049 QUIC_STREAM *qs,
4050 int is_write,
4051 int *state,
4052 uint64_t *app_error_code)
4053 {
4054 int local_init;
4055 uint64_t scratch_pad; /* throw away value */
4056
4057 local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
4058
4059 if (app_error_code != NULL)
4060 *app_error_code = UINT64_MAX;
4061 else
4062 app_error_code = &scratch_pad;
4063
4064 if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
4065 /*
4066 * Unidirectional stream and this direction of transmission doesn't
4067 * exist.
4068 */
4069 *state = SSL_STREAM_STATE_WRONG_DIR;
4070 } else if (ossl_quic_channel_is_term_any(qc->ch)) {
4071 /* Connection already closed. */
4072 *state = SSL_STREAM_STATE_CONN_CLOSED;
4073 } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
4074 /* Application has read a FIN. */
4075 *state = SSL_STREAM_STATE_FINISHED;
4076 } else if ((!is_write && qs->stop_sending)
4077 || (is_write && ossl_quic_stream_send_is_reset(qs))) {
4078 /*
4079 * Stream has been reset locally. FIN takes precedence over this for the
4080 * read case as the application need not care if the stream is reset
4081 * after a FIN has been successfully processed.
4082 */
4083 *state = SSL_STREAM_STATE_RESET_LOCAL;
4084 *app_error_code = !is_write
4085 ? qs->stop_sending_aec
4086 : qs->reset_stream_aec;
4087 } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
4088 || (is_write && qs->peer_stop_sending)) {
4089 /*
4090 * Stream has been reset remotely. */
4091 *state = SSL_STREAM_STATE_RESET_REMOTE;
4092 *app_error_code = !is_write
4093 ? qs->peer_reset_stream_aec
4094 : qs->peer_stop_sending_aec;
4095 } else if (is_write && qs->have_final_size) {
4096 /*
4097 * Stream has been finished. Stream reset takes precedence over this for
4098 * the write case as peer may not have received all data.
4099 */
4100 *state = SSL_STREAM_STATE_FINISHED;
4101 } else {
4102 /* Stream still healthy. */
4103 *state = SSL_STREAM_STATE_OK;
4104 }
4105 }
4106
quic_get_stream_state(SSL * ssl,int is_write)4107 static int quic_get_stream_state(SSL *ssl, int is_write)
4108 {
4109 QCTX ctx;
4110 int state;
4111
4112 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4113 return SSL_STREAM_STATE_NONE;
4114
4115 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
4116 qctx_unlock(&ctx);
4117 return state;
4118 }
4119
ossl_quic_get_stream_read_state(SSL * ssl)4120 int ossl_quic_get_stream_read_state(SSL *ssl)
4121 {
4122 return quic_get_stream_state(ssl, /*is_write=*/0);
4123 }
4124
4125 /*
4126 * SSL_get_stream_write_state
4127 * --------------------------
4128 */
ossl_quic_get_stream_write_state(SSL * ssl)4129 int ossl_quic_get_stream_write_state(SSL *ssl)
4130 {
4131 return quic_get_stream_state(ssl, /*is_write=*/1);
4132 }
4133
4134 /*
4135 * SSL_get_stream_read_error_code
4136 * ------------------------------
4137 */
quic_get_stream_error_code(SSL * ssl,int is_write,uint64_t * app_error_code)4138 static int quic_get_stream_error_code(SSL *ssl, int is_write,
4139 uint64_t *app_error_code)
4140 {
4141 QCTX ctx;
4142 int state;
4143
4144 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4145 return -1;
4146
4147 quic_classify_stream(ctx.qc, ctx.xso->stream, is_write,
4148 &state, app_error_code);
4149
4150 qctx_unlock(&ctx);
4151 switch (state) {
4152 case SSL_STREAM_STATE_FINISHED:
4153 return 0;
4154 case SSL_STREAM_STATE_RESET_LOCAL:
4155 case SSL_STREAM_STATE_RESET_REMOTE:
4156 return 1;
4157 default:
4158 return -1;
4159 }
4160 }
4161
ossl_quic_get_stream_read_error_code(SSL * ssl,uint64_t * app_error_code)4162 int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
4163 {
4164 return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
4165 }
4166
4167 /*
4168 * SSL_get_stream_write_error_code
4169 * -------------------------------
4170 */
ossl_quic_get_stream_write_error_code(SSL * ssl,uint64_t * app_error_code)4171 int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
4172 {
4173 return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
4174 }
4175
4176 /*
4177 * Write buffer size mutation
4178 * --------------------------
4179 */
ossl_quic_set_write_buffer_size(SSL * ssl,size_t size)4180 int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
4181 {
4182 int ret = 0;
4183 QCTX ctx;
4184
4185 if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4186 return 0;
4187
4188 if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
4189 /* Called on a unidirectional receive-only stream - error. */
4190 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
4191 goto out;
4192 }
4193
4194 if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
4195 /*
4196 * If the stream has a send part but we have disposed of it because we
4197 * no longer need it, this is a no-op.
4198 */
4199 ret = 1;
4200 goto out;
4201 }
4202
4203 if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
4204 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
4205 goto out;
4206 }
4207
4208 ret = 1;
4209
4210 out:
4211 qctx_unlock(&ctx);
4212 return ret;
4213 }
4214
4215 /*
4216 * SSL_get_conn_close_info
4217 * -----------------------
4218 */
ossl_quic_get_conn_close_info(SSL * ssl,SSL_CONN_CLOSE_INFO * info,size_t info_len)4219 int ossl_quic_get_conn_close_info(SSL *ssl,
4220 SSL_CONN_CLOSE_INFO *info,
4221 size_t info_len)
4222 {
4223 QCTX ctx;
4224 const QUIC_TERMINATE_CAUSE *tc;
4225
4226 if (!expect_quic_conn_only(ssl, &ctx))
4227 return -1;
4228
4229 tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
4230 if (tc == NULL)
4231 return 0;
4232
4233 info->error_code = tc->error_code;
4234 info->frame_type = tc->frame_type;
4235 info->reason = tc->reason;
4236 info->reason_len = tc->reason_len;
4237 info->flags = 0;
4238 if (!tc->remote)
4239 info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;
4240 if (!tc->app)
4241 info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;
4242 return 1;
4243 }
4244
4245 /*
4246 * SSL_key_update
4247 * --------------
4248 */
ossl_quic_key_update(SSL * ssl,int update_type)4249 int ossl_quic_key_update(SSL *ssl, int update_type)
4250 {
4251 QCTX ctx;
4252
4253 if (!expect_quic_conn_only(ssl, &ctx))
4254 return 0;
4255
4256 switch (update_type) {
4257 case SSL_KEY_UPDATE_NOT_REQUESTED:
4258 /*
4259 * QUIC signals peer key update implicily by triggering a local
4260 * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
4261 */
4262 case SSL_KEY_UPDATE_REQUESTED:
4263 break;
4264
4265 default:
4266 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
4267 return 0;
4268 }
4269
4270 qctx_lock(&ctx);
4271
4272 /* Attempt to perform a TXKU. */
4273 if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
4274 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
4275 qctx_unlock(&ctx);
4276 return 0;
4277 }
4278
4279 qctx_unlock(&ctx);
4280 return 1;
4281 }
4282
4283 /*
4284 * SSL_get_key_update_type
4285 * -----------------------
4286 */
ossl_quic_get_key_update_type(const SSL * s)4287 int ossl_quic_get_key_update_type(const SSL *s)
4288 {
4289 /*
4290 * We always handle key updates immediately so a key update is never
4291 * pending.
4292 */
4293 return SSL_KEY_UPDATE_NONE;
4294 }
4295
4296 /**
4297 * @brief Allocates an SSL object for a user from a QUIC channel.
4298 *
4299 * This function creates a new QUIC_CONNECTION object based on an incoming
4300 * connection associated with the provided QUIC_LISTENER. If the connection
4301 * creation fails, the function returns NULL. Otherwise, it returns a pointer
4302 * to the SSL object associated with the newly created connection.
4303 *
4304 * Note: This function is a registered port callback made from
4305 * ossl_quic_new_listener and ossl_quic_new_listener_from, and allows for
4306 * pre-allocation of the user_ssl object when a channel is created, rather than
4307 * when it is accepted
4308 *
4309 * @param ch Pointer to the QUIC_CHANNEL representing the incoming connection.
4310 * @param arg Pointer to a QUIC_LISTENER used to create the connection.
4311 *
4312 * @return Pointer to the SSL object on success, or NULL on failure.
4313 */
alloc_port_user_ssl(QUIC_CHANNEL * ch,void * arg)4314 static SSL *alloc_port_user_ssl(QUIC_CHANNEL *ch, void *arg)
4315 {
4316 QUIC_LISTENER *ql = arg;
4317 QUIC_CONNECTION *qc = create_qc_from_incoming_conn(ql, ch);
4318
4319 return (qc == NULL) ? NULL : &qc->obj.ssl;
4320 }
4321
4322 /*
4323 * QUIC Front-End I/O API: Listeners
4324 * =================================
4325 */
4326
4327 /*
4328 * SSL_new_listener
4329 * ----------------
4330 */
ossl_quic_new_listener(SSL_CTX * ctx,uint64_t flags)4331 SSL *ossl_quic_new_listener(SSL_CTX *ctx, uint64_t flags)
4332 {
4333 QUIC_LISTENER *ql = NULL;
4334 QUIC_ENGINE_ARGS engine_args = { 0 };
4335 QUIC_PORT_ARGS port_args = { 0 };
4336
4337 if ((ql = OPENSSL_zalloc(sizeof(*ql))) == NULL) {
4338 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4339 return NULL;
4340 }
4341
4342 #if defined(OPENSSL_THREADS)
4343 if ((ql->mutex = ossl_crypto_mutex_new()) == NULL) {
4344 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4345 goto err;
4346 }
4347 #endif
4348
4349 engine_args.libctx = ctx->libctx;
4350 engine_args.propq = ctx->propq;
4351 #if defined(OPENSSL_THREADS)
4352 engine_args.mutex = ql->mutex;
4353 #endif
4354
4355 if (need_notifier_for_domain_flags(ctx->domain_flags))
4356 engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
4357
4358 if ((ql->engine = ossl_quic_engine_new(&engine_args)) == NULL) {
4359 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4360 goto err;
4361 }
4362
4363 port_args.channel_ctx = ctx;
4364 port_args.is_multi_conn = 1;
4365 port_args.get_conn_user_ssl = alloc_port_user_ssl;
4366 port_args.user_ssl_arg = ql;
4367 if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0)
4368 port_args.do_addr_validation = 1;
4369 ql->port = ossl_quic_engine_create_port(ql->engine, &port_args);
4370 if (ql->port == NULL) {
4371 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4372 goto err;
4373 }
4374
4375 /* TODO(QUIC FUTURE): Implement SSL_LISTENER_FLAG_NO_ACCEPT */
4376
4377 ossl_quic_port_set_allow_incoming(ql->port, 1);
4378
4379 /* Initialise the QUIC_LISTENER's object header. */
4380 if (!ossl_quic_obj_init(&ql->obj, ctx, SSL_TYPE_QUIC_LISTENER, NULL,
4381 ql->engine, ql->port))
4382 goto err;
4383
4384 return &ql->obj.ssl;
4385
4386 err:
4387 ossl_quic_port_free(ql->port);
4388 ossl_quic_engine_free(ql->engine);
4389
4390 #if defined(OPENSSL_THREADS)
4391 ossl_crypto_mutex_free(&ql->mutex);
4392 #endif
4393 OPENSSL_free(ql);
4394 return NULL;
4395 }
4396
4397 /*
4398 * SSL_new_listener_from
4399 * ---------------------
4400 */
ossl_quic_new_listener_from(SSL * ssl,uint64_t flags)4401 SSL *ossl_quic_new_listener_from(SSL *ssl, uint64_t flags)
4402 {
4403 QCTX ctx;
4404 QUIC_LISTENER *ql = NULL;
4405 QUIC_PORT_ARGS port_args = { 0 };
4406
4407 if (!expect_quic_domain(ssl, &ctx))
4408 return NULL;
4409
4410 if (!SSL_up_ref(&ctx.qd->obj.ssl))
4411 return NULL;
4412
4413 qctx_lock(&ctx);
4414
4415 if ((ql = OPENSSL_zalloc(sizeof(*ql))) == NULL) {
4416 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4417 goto err;
4418 }
4419
4420 port_args.channel_ctx = ssl->ctx;
4421 port_args.is_multi_conn = 1;
4422 port_args.get_conn_user_ssl = alloc_port_user_ssl;
4423 port_args.user_ssl_arg = ql;
4424 if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0)
4425 port_args.do_addr_validation = 1;
4426 ql->port = ossl_quic_engine_create_port(ctx.qd->engine, &port_args);
4427 if (ql->port == NULL) {
4428 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4429 goto err;
4430 }
4431
4432 ql->domain = ctx.qd;
4433 ql->engine = ctx.qd->engine;
4434 #if defined(OPENSSL_THREADS)
4435 ql->mutex = ctx.qd->mutex;
4436 #endif
4437
4438 /*
4439 * TODO(QUIC FUTURE): Implement SSL_LISTENER_FLAG_NO_ACCEPT
4440 * Given that we have apis to create client SSL objects from
4441 * server SSL objects (see SSL_new_from_listener), we have aspirations
4442 * to enable a flag that allows for the creation of the latter, but not
4443 * be used to do accept any connections. This is a placeholder for the
4444 * implementation of that flag
4445 */
4446
4447 ossl_quic_port_set_allow_incoming(ql->port, 1);
4448
4449 /* Initialise the QUIC_LISTENER's object header. */
4450 if (!ossl_quic_obj_init(&ql->obj, ssl->ctx, SSL_TYPE_QUIC_LISTENER,
4451 &ctx.qd->obj.ssl, NULL, ql->port))
4452 goto err;
4453
4454 qctx_unlock(&ctx);
4455 return &ql->obj.ssl;
4456
4457 err:
4458 if (ql != NULL)
4459 ossl_quic_port_free(ql->port);
4460
4461 OPENSSL_free(ql);
4462 qctx_unlock(&ctx);
4463 SSL_free(&ctx.qd->obj.ssl);
4464
4465 return NULL;
4466 }
4467
4468 /*
4469 * SSL_new_from_listener
4470 * ---------------------
4471 * code here is derived from ossl_quic_new(). The `ssl` argument is
4472 * a listener object which already comes with QUIC port/engine. The newly
4473 * created QUIC connection object (QCSO) is going to share the port/engine
4474 * with listener (`ssl`). The `ssl` also becomes a parent of QCSO created
4475 * by this function. The caller uses QCSO instance to connect to
4476 * remote QUIC server.
4477 *
4478 * The QCSO created here requires us to also create a channel so we
4479 * can connect to remote server.
4480 */
ossl_quic_new_from_listener(SSL * ssl,uint64_t flags)4481 SSL *ossl_quic_new_from_listener(SSL *ssl, uint64_t flags)
4482 {
4483 QCTX ctx;
4484 QUIC_CONNECTION *qc = NULL;
4485 QUIC_LISTENER *ql;
4486 SSL_CONNECTION *sc = NULL;
4487
4488 if (flags != 0)
4489 return NULL;
4490
4491 if (!expect_quic_listener(ssl, &ctx))
4492 return NULL;
4493
4494 if (!SSL_up_ref(&ctx.ql->obj.ssl))
4495 return NULL;
4496
4497 qctx_lock(&ctx);
4498
4499 ql = ctx.ql;
4500
4501 /*
4502 * listeners (server) contexts don't typically
4503 * allocate a token cache because they don't need
4504 * to store them, but here we are using a server side
4505 * ctx as a client, so we should allocate one now
4506 */
4507 if (ssl->ctx->tokencache == NULL)
4508 if ((ssl->ctx->tokencache = ossl_quic_new_token_store()) == NULL)
4509 goto err;
4510
4511 if ((qc = OPENSSL_zalloc(sizeof(*qc))) == NULL) {
4512 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4513 goto err;
4514 }
4515
4516 /*
4517 * NOTE: setting a listener here is needed so `qc_cleanup()` does the right
4518 * thing. Setting listener to ql avoids premature destruction of port in
4519 * qc_cleanup()
4520 */
4521 qc->listener = ql;
4522 qc->engine = ql->engine;
4523 qc->port = ql->port;
4524 /* create channel */
4525 #if defined(OPENSSL_THREADS)
4526 /* this is the engine mutex */
4527 qc->mutex = ql->mutex;
4528 #endif
4529 #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
4530 qc->is_thread_assisted
4531 = ((ql->obj.domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0);
4532 #endif
4533
4534 /* Create the handshake layer. */
4535 qc->tls = ossl_ssl_connection_new_int(ql->obj.ssl.ctx, &qc->obj.ssl, TLS_method());
4536 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
4537 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4538 goto err;
4539 }
4540 sc->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL;
4541
4542 qc->default_ssl_options = OSSL_QUIC_PERMITTED_OPTIONS;
4543 qc->last_error = SSL_ERROR_NONE;
4544
4545 /*
4546 * This is QCSO, we don't expect to accept connections
4547 * on success the channel assumes ownership of tls, we need
4548 * to grab reference for qc.
4549 */
4550 qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
4551 if (qc->ch == NULL) {
4552 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4553 goto err;
4554 }
4555
4556 ossl_quic_channel_set_msg_callback(qc->ch, ql->obj.ssl.ctx->msg_callback, &qc->obj.ssl);
4557 ossl_quic_channel_set_msg_callback_arg(qc->ch, ql->obj.ssl.ctx->msg_callback_arg);
4558
4559 /*
4560 * We deliberately pass NULL for engine and port, because we don't want to
4561 * to turn QCSO we create here into an event leader, nor port leader.
4562 * Both those roles are occupied already by listener (`ssl`) we use
4563 * to create a new QCSO here.
4564 */
4565 if (!ossl_quic_obj_init(&qc->obj, ql->obj.ssl.ctx,
4566 SSL_TYPE_QUIC_CONNECTION,
4567 &ql->obj.ssl, NULL, NULL)) {
4568 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4569 goto err;
4570 }
4571
4572 /* Initialise libssl APL-related state. */
4573 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
4574 qc->default_ssl_mode = qc->obj.ssl.ctx->mode;
4575 qc->default_ssl_options = qc->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
4576 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
4577 qc->last_error = SSL_ERROR_NONE;
4578
4579 qc_update_reject_policy(qc);
4580
4581 qctx_unlock(&ctx);
4582
4583 return &qc->obj.ssl;
4584
4585 err:
4586 if (qc != NULL) {
4587 qc_cleanup(qc, /* have_lock= */ 0);
4588 OPENSSL_free(qc);
4589 }
4590 qctx_unlock(&ctx);
4591 SSL_free(&ctx.ql->obj.ssl);
4592
4593 return NULL;
4594 }
4595
4596 /*
4597 * SSL_listen
4598 * ----------
4599 */
4600 QUIC_NEEDS_LOCK
ql_listen(QUIC_LISTENER * ql)4601 static int ql_listen(QUIC_LISTENER *ql)
4602 {
4603 if (ql->listening)
4604 return 1;
4605
4606 ossl_quic_port_set_allow_incoming(ql->port, 1);
4607 ql->listening = 1;
4608 return 1;
4609 }
4610
4611 QUIC_TAKES_LOCK
ossl_quic_listen(SSL * ssl)4612 int ossl_quic_listen(SSL *ssl)
4613 {
4614 QCTX ctx;
4615 int ret;
4616
4617 if (!expect_quic_listener(ssl, &ctx))
4618 return 0;
4619
4620 qctx_lock_for_io(&ctx);
4621
4622 ret = ql_listen(ctx.ql);
4623
4624 qctx_unlock(&ctx);
4625 return ret;
4626 }
4627
4628 /*
4629 * SSL_accept_connection
4630 * ---------------------
4631 */
quic_accept_connection_wait(void * arg)4632 static int quic_accept_connection_wait(void *arg)
4633 {
4634 QUIC_PORT *port = arg;
4635
4636 if (!ossl_quic_port_is_running(port))
4637 return -1;
4638
4639 if (ossl_quic_port_have_incoming(port))
4640 return 1;
4641
4642 return 0;
4643 }
4644
4645 QUIC_TAKES_LOCK
ossl_quic_accept_connection(SSL * ssl,uint64_t flags)4646 SSL *ossl_quic_accept_connection(SSL *ssl, uint64_t flags)
4647 {
4648 int ret;
4649 QCTX ctx;
4650 SSL *conn_ssl = NULL;
4651 SSL *conn_ssl_tmp = NULL;
4652 SSL_CONNECTION *conn = NULL;
4653 QUIC_CHANNEL *new_ch = NULL;
4654 QUIC_CONNECTION *qc = NULL;
4655 int no_block = ((flags & SSL_ACCEPT_CONNECTION_NO_BLOCK) != 0);
4656
4657 if (!expect_quic_listener(ssl, &ctx))
4658 return NULL;
4659
4660 qctx_lock_for_io(&ctx);
4661
4662 if (!ql_listen(ctx.ql))
4663 goto out;
4664
4665 /* Wait for an incoming connection if needed. */
4666 new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4667 if (new_ch == NULL && ossl_quic_port_is_running(ctx.ql->port)) {
4668 if (!no_block && qctx_blocking(&ctx)) {
4669 ret = block_until_pred(&ctx, quic_accept_connection_wait,
4670 ctx.ql->port, 0);
4671 if (ret < 1)
4672 goto out;
4673 } else {
4674 qctx_maybe_autotick(&ctx);
4675 }
4676
4677 if (!ossl_quic_port_is_running(ctx.ql->port))
4678 goto out;
4679
4680 new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4681 }
4682
4683 if (new_ch == NULL && ossl_quic_port_is_running(ctx.ql->port)) {
4684 /* No connections already queued. */
4685 ossl_quic_reactor_tick(ossl_quic_engine_get0_reactor(ctx.ql->engine), 0);
4686
4687 new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4688 }
4689
4690 /*
4691 * port_make_channel pre-allocates our user_ssl for us for each newly
4692 * created channel, so once we pop the new channel from the port above
4693 * we just need to extract it
4694 */
4695 if (new_ch == NULL)
4696 goto out;
4697
4698 /*
4699 * All objects below must exist, because new_ch != NULL. The objects are
4700 * bound to new_ch. If channel constructor fails to create any item here
4701 * it just fails to create channel.
4702 */
4703 if (!ossl_assert((conn_ssl_tmp = ossl_quic_channel_get0_tls(new_ch)) != NULL)
4704 || !ossl_assert((conn = SSL_CONNECTION_FROM_SSL(conn_ssl_tmp)) != NULL)
4705 || !ossl_assert((conn_ssl_tmp = SSL_CONNECTION_GET_USER_SSL(conn)) != NULL))
4706 goto out;
4707
4708 qc = (QUIC_CONNECTION *)conn_ssl_tmp;
4709 if (SSL_up_ref(&ctx.ql->obj.ssl)) {
4710 qc->listener = ctx.ql;
4711 conn_ssl = conn_ssl_tmp;
4712 conn_ssl_tmp = NULL;
4713 qc->pending = 0;
4714 }
4715
4716 out:
4717
4718 qctx_unlock(&ctx);
4719 /*
4720 * You might expect ossl_quic_channel_free() to be called here. Be
4721 * assured it happens, The process goes as follows:
4722 * - The SSL_free() here is being handled by ossl_quic_free().
4723 * - The very last step of ossl_quic_free() is call to qc_cleanup()
4724 * where channel gets freed.
4725 * NOTE: We defer this SSL_free until after the call to qctx_unlock above
4726 * to avoid the deadlock that would occur when ossl_quic_free attempts to
4727 * re-acquire this mutex. We also do the gymnastics with conn_ssl and
4728 * conn_ssl_tmp above so that we only actually do the free on the SSL
4729 * object if the up-ref above fails, in such a way that we don't unbalance
4730 * the listener refcount (i.e. if the up-ref fails above, we don't set the
4731 * listener pointer so that we don't then drop the ref-count erroneously
4732 * during the free operation.
4733 */
4734 SSL_free(conn_ssl_tmp);
4735 return conn_ssl;
4736 }
4737
create_qc_from_incoming_conn(QUIC_LISTENER * ql,QUIC_CHANNEL * ch)4738 static QUIC_CONNECTION *create_qc_from_incoming_conn(QUIC_LISTENER *ql, QUIC_CHANNEL *ch)
4739 {
4740 QUIC_CONNECTION *qc = NULL;
4741
4742 if ((qc = OPENSSL_zalloc(sizeof(*qc))) == NULL) {
4743 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4744 goto err;
4745 }
4746
4747 if (!ossl_quic_obj_init(&qc->obj, ql->obj.ssl.ctx,
4748 SSL_TYPE_QUIC_CONNECTION,
4749 &ql->obj.ssl, NULL, NULL)) {
4750 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4751 goto err;
4752 }
4753
4754 ossl_quic_channel_get_peer_addr(ch, &qc->init_peer_addr); /* best effort */
4755 qc->pending = 1;
4756 qc->engine = ql->engine;
4757 qc->port = ql->port;
4758 qc->ch = ch;
4759 #if defined(OPENSSL_THREADS)
4760 qc->mutex = ql->mutex;
4761 #endif
4762 qc->tls = ossl_quic_channel_get0_tls(ch);
4763 qc->started = 1;
4764 qc->as_server = 1;
4765 qc->as_server_state = 1;
4766 qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
4767 qc->default_ssl_options = ql->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
4768 qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
4769 qc->last_error = SSL_ERROR_NONE;
4770 qc_update_reject_policy(qc);
4771 return qc;
4772
4773 err:
4774 OPENSSL_free(qc);
4775 return NULL;
4776 }
4777
4778 DEFINE_LHASH_OF_EX(QUIC_TOKEN);
4779
4780 struct ssl_token_store_st {
4781 LHASH_OF(QUIC_TOKEN) *cache;
4782 CRYPTO_REF_COUNT references;
4783 CRYPTO_MUTEX *mutex;
4784 };
4785
quic_token_hash(const QUIC_TOKEN * item)4786 static unsigned long quic_token_hash(const QUIC_TOKEN *item)
4787 {
4788 return (unsigned long)ossl_fnv1a_hash(item->hashkey, item->hashkey_len);
4789 }
4790
quic_token_cmp(const QUIC_TOKEN * a,const QUIC_TOKEN * b)4791 static int quic_token_cmp(const QUIC_TOKEN *a, const QUIC_TOKEN *b)
4792 {
4793 if (a->hashkey_len != b->hashkey_len)
4794 return 1;
4795 return memcmp(a->hashkey, b->hashkey, a->hashkey_len);
4796 }
4797
ossl_quic_new_token_store(void)4798 SSL_TOKEN_STORE *ossl_quic_new_token_store(void)
4799 {
4800 int ok = 0;
4801 SSL_TOKEN_STORE *newcache = OPENSSL_zalloc(sizeof(SSL_TOKEN_STORE));
4802
4803 if (newcache == NULL)
4804 goto out;
4805
4806 newcache->cache = lh_QUIC_TOKEN_new(quic_token_hash, quic_token_cmp);
4807 if (newcache->cache == NULL)
4808 goto out;
4809
4810 #if defined(OPENSSL_THREADS)
4811 if ((newcache->mutex = ossl_crypto_mutex_new()) == NULL)
4812 goto out;
4813 #endif
4814
4815 if (!CRYPTO_NEW_REF(&newcache->references, 1))
4816 goto out;
4817
4818 ok = 1;
4819 out:
4820 if (!ok) {
4821 ossl_quic_free_token_store(newcache);
4822 newcache = NULL;
4823 }
4824 return newcache;
4825 }
4826
free_this_token(QUIC_TOKEN * tok)4827 static void free_this_token(QUIC_TOKEN *tok)
4828 {
4829 ossl_quic_free_peer_token(tok);
4830 }
4831
ossl_quic_free_token_store(SSL_TOKEN_STORE * hdl)4832 void ossl_quic_free_token_store(SSL_TOKEN_STORE *hdl)
4833 {
4834 int refs;
4835
4836 if (hdl == NULL)
4837 return;
4838
4839 if (!CRYPTO_DOWN_REF(&hdl->references, &refs))
4840 return;
4841
4842 if (refs > 0)
4843 return;
4844
4845 /* last reference, we can clean up */
4846 ossl_crypto_mutex_free(&hdl->mutex);
4847 lh_QUIC_TOKEN_doall(hdl->cache, free_this_token);
4848 lh_QUIC_TOKEN_free(hdl->cache);
4849 CRYPTO_FREE_REF(&hdl->references);
4850 OPENSSL_free(hdl);
4851 return;
4852 }
4853
4854 /**
4855 * @brief build a new QUIC_TOKEN
4856 *
4857 * This function creates a new token storage structure for saving in our
4858 * tokencache
4859 *
4860 * In an effort to make allocation and freeing of these tokens a bit faster
4861 * We do them in a single allocation in this format
4862 * +---------------+ --\
4863 * | hashkey * |---| |
4864 * | hashkey_len | | | QUIC_TOKEN
4865 * | token * |---|--| |
4866 * | token_len | | | |
4867 * +---------------+<--| | --/
4868 * | hashkey buf | |
4869 * | | |
4870 * |---------------|<-----|
4871 * | token buf |
4872 * | |
4873 * +---------------+
4874 *
4875 * @param peer - the peer address that sent the token
4876 * @param token - the buffer holding the token
4877 * @param token_len - the size of token
4878 *
4879 * @returns a QUIC_TOKEN pointer or NULL on error
4880 */
ossl_quic_build_new_token(BIO_ADDR * peer,uint8_t * token,size_t token_len)4881 static QUIC_TOKEN *ossl_quic_build_new_token(BIO_ADDR *peer, uint8_t *token,
4882 size_t token_len)
4883 {
4884 QUIC_TOKEN *new_token;
4885 size_t hashkey_len = 0;
4886 size_t addr_len = 0;
4887 int family;
4888 unsigned short port;
4889 int *famptr;
4890 unsigned short *portptr;
4891 uint8_t *addrptr;
4892
4893 if ((token != NULL && token_len == 0) || (token == NULL && token_len != 0))
4894 return NULL;
4895
4896 if (!BIO_ADDR_rawaddress(peer, NULL, &addr_len))
4897 return NULL;
4898 family = BIO_ADDR_family(peer);
4899 port = BIO_ADDR_rawport(peer);
4900
4901 hashkey_len += sizeof(int); /* hashkey(family) */
4902 hashkey_len += sizeof(unsigned short); /* hashkey(port) */
4903 hashkey_len += addr_len; /* hashkey(address) */
4904
4905 new_token = OPENSSL_zalloc(sizeof(QUIC_TOKEN) + hashkey_len + token_len);
4906 if (new_token == NULL)
4907 return NULL;
4908
4909 if (!CRYPTO_NEW_REF(&new_token->references, 1)) {
4910 OPENSSL_free(new_token);
4911 return NULL;
4912 }
4913
4914 new_token->hashkey_len = hashkey_len;
4915 /* hashkey is allocated inline, immediately after the QUIC_TOKEN struct */
4916 new_token->hashkey = (uint8_t *)(new_token + 1);
4917 /* token buffer follows the hashkey in the inline allocation */
4918 new_token->token = new_token->hashkey + hashkey_len;
4919 new_token->token_len = token_len;
4920 famptr = (int *)new_token->hashkey;
4921 portptr = (unsigned short *)(famptr + 1);
4922 addrptr = (uint8_t *)(portptr + 1);
4923 *famptr = family;
4924 *portptr = port;
4925 if (!BIO_ADDR_rawaddress(peer, addrptr, NULL)) {
4926 ossl_quic_free_peer_token(new_token);
4927 return NULL;
4928 }
4929 if (token != NULL)
4930 memcpy(new_token->token, token, token_len);
4931 return new_token;
4932 }
4933
ossl_quic_set_peer_token(SSL_CTX * ctx,BIO_ADDR * peer,const uint8_t * token,size_t token_len)4934 int ossl_quic_set_peer_token(SSL_CTX *ctx, BIO_ADDR *peer,
4935 const uint8_t *token, size_t token_len)
4936 {
4937 SSL_TOKEN_STORE *c = ctx->tokencache;
4938 QUIC_TOKEN *tok, *old = NULL;
4939
4940 if (ctx->tokencache == NULL)
4941 return 0;
4942
4943 tok = ossl_quic_build_new_token(peer, (uint8_t *)token, token_len);
4944 if (tok == NULL)
4945 return 0;
4946
4947 /* we might be sharing this cache, lock it */
4948 ossl_crypto_mutex_lock(c->mutex);
4949
4950 old = lh_QUIC_TOKEN_retrieve(c->cache, tok);
4951 if (old != NULL) {
4952 lh_QUIC_TOKEN_delete(c->cache, old);
4953 ossl_quic_free_peer_token(old);
4954 }
4955 lh_QUIC_TOKEN_insert(c->cache, tok);
4956 if (lh_QUIC_TOKEN_error(c->cache)) {
4957 ossl_quic_free_peer_token(tok);
4958 ossl_crypto_mutex_unlock(c->mutex);
4959 return 0;
4960 }
4961
4962 ossl_crypto_mutex_unlock(c->mutex);
4963 return 1;
4964 }
4965
ossl_quic_get_peer_token(SSL_CTX * ctx,BIO_ADDR * peer,QUIC_TOKEN ** token)4966 int ossl_quic_get_peer_token(SSL_CTX *ctx, BIO_ADDR *peer,
4967 QUIC_TOKEN **token)
4968 {
4969 SSL_TOKEN_STORE *c = ctx->tokencache;
4970 QUIC_TOKEN *key = NULL;
4971 QUIC_TOKEN *tok = NULL;
4972 int ret;
4973 int rc = 0;
4974
4975 if (c == NULL)
4976 return 0;
4977
4978 key = ossl_quic_build_new_token(peer, NULL, 0);
4979 if (key == NULL)
4980 return 0;
4981
4982 ossl_crypto_mutex_lock(c->mutex);
4983 tok = lh_QUIC_TOKEN_retrieve(c->cache, key);
4984 if (tok != NULL) {
4985 *token = tok;
4986 CRYPTO_UP_REF(&tok->references, &ret);
4987 rc = 1;
4988 }
4989
4990 ossl_crypto_mutex_unlock(c->mutex);
4991 ossl_quic_free_peer_token(key);
4992 return rc;
4993 }
4994
ossl_quic_free_peer_token(QUIC_TOKEN * token)4995 void ossl_quic_free_peer_token(QUIC_TOKEN *token)
4996 {
4997 int refs = 0;
4998
4999 if (!CRYPTO_DOWN_REF(&token->references, &refs))
5000 return;
5001
5002 if (refs > 0)
5003 return;
5004
5005 CRYPTO_FREE_REF(&token->references);
5006 OPENSSL_free(token);
5007 }
5008
5009 /*
5010 * SSL_get_accept_connection_queue_len
5011 * -----------------------------------
5012 */
5013 QUIC_TAKES_LOCK
ossl_quic_get_accept_connection_queue_len(SSL * ssl)5014 size_t ossl_quic_get_accept_connection_queue_len(SSL *ssl)
5015 {
5016 QCTX ctx;
5017 int ret;
5018
5019 if (!expect_quic_listener(ssl, &ctx))
5020 return 0;
5021
5022 qctx_lock(&ctx);
5023
5024 ret = ossl_quic_port_get_num_incoming_channels(ctx.ql->port);
5025
5026 qctx_unlock(&ctx);
5027 return ret;
5028 }
5029
5030 /*
5031 * QUIC Front-End I/O API: Domains
5032 * ===============================
5033 */
5034
5035 /*
5036 * SSL_new_domain
5037 * --------------
5038 */
ossl_quic_new_domain(SSL_CTX * ctx,uint64_t flags)5039 SSL *ossl_quic_new_domain(SSL_CTX *ctx, uint64_t flags)
5040 {
5041 QUIC_DOMAIN *qd = NULL;
5042 QUIC_ENGINE_ARGS engine_args = { 0 };
5043 uint64_t domain_flags;
5044
5045 domain_flags = ctx->domain_flags;
5046 if ((flags & (SSL_DOMAIN_FLAG_SINGLE_THREAD | SSL_DOMAIN_FLAG_MULTI_THREAD | SSL_DOMAIN_FLAG_THREAD_ASSISTED)) != 0)
5047 domain_flags = flags;
5048 else
5049 domain_flags = ctx->domain_flags | flags;
5050
5051 if (!ossl_adjust_domain_flags(domain_flags, &domain_flags))
5052 return NULL;
5053
5054 if ((qd = OPENSSL_zalloc(sizeof(*qd))) == NULL) {
5055 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
5056 return NULL;
5057 }
5058
5059 #if defined(OPENSSL_THREADS)
5060 if ((qd->mutex = ossl_crypto_mutex_new()) == NULL) {
5061 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
5062 goto err;
5063 }
5064 #endif
5065
5066 engine_args.libctx = ctx->libctx;
5067 engine_args.propq = ctx->propq;
5068 #if defined(OPENSSL_THREADS)
5069 engine_args.mutex = qd->mutex;
5070 #endif
5071
5072 if (need_notifier_for_domain_flags(domain_flags))
5073 engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
5074
5075 if ((qd->engine = ossl_quic_engine_new(&engine_args)) == NULL) {
5076 QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
5077 goto err;
5078 }
5079
5080 /* Initialise the QUIC_DOMAIN's object header. */
5081 if (!ossl_quic_obj_init(&qd->obj, ctx, SSL_TYPE_QUIC_DOMAIN, NULL,
5082 qd->engine, NULL))
5083 goto err;
5084
5085 ossl_quic_obj_set_domain_flags(&qd->obj, domain_flags);
5086 return &qd->obj.ssl;
5087
5088 err:
5089 ossl_quic_engine_free(qd->engine);
5090 #if defined(OPENSSL_THREADS)
5091 ossl_crypto_mutex_free(&qd->mutex);
5092 #endif
5093 OPENSSL_free(qd);
5094 return NULL;
5095 }
5096
5097 /*
5098 * QUIC Front-End I/O API: SSL_CTX Management
5099 * ==========================================
5100 */
5101
ossl_quic_ctx_ctrl(SSL_CTX * ctx,int cmd,long larg,void * parg)5102 long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
5103 {
5104 switch (cmd) {
5105 default:
5106 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
5107 }
5108 }
5109
ossl_quic_callback_ctrl(SSL * s,int cmd,void (* fp)(void))5110 long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
5111 {
5112 QCTX ctx;
5113
5114 if (!expect_quic_conn_only(s, &ctx))
5115 return 0;
5116
5117 switch (cmd) {
5118 case SSL_CTRL_SET_MSG_CALLBACK:
5119 ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
5120 &ctx.qc->obj.ssl);
5121 /* This callback also needs to be set on the internal SSL object */
5122 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
5123 ;
5124
5125 default:
5126 /* Probably a TLS related ctrl. Defer to our internal SSL object */
5127 return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
5128 }
5129 }
5130
ossl_quic_ctx_callback_ctrl(SSL_CTX * ctx,int cmd,void (* fp)(void))5131 long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
5132 {
5133 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
5134 }
5135
ossl_quic_renegotiate_check(SSL * ssl,int initok)5136 int ossl_quic_renegotiate_check(SSL *ssl, int initok)
5137 {
5138 /* We never do renegotiation. */
5139 return 0;
5140 }
5141
ossl_quic_get_cipher_by_char(const unsigned char * p)5142 const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)
5143 {
5144 const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);
5145
5146 if (ciph == NULL)
5147 return NULL;
5148 if ((ciph->algorithm2 & SSL_QUIC) == 0)
5149 return NULL;
5150
5151 return ciph;
5152 }
5153
5154 /*
5155 * These functions define the TLSv1.2 (and below) ciphers that are supported by
5156 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
5157 */
5158
ossl_quic_num_ciphers(void)5159 int ossl_quic_num_ciphers(void)
5160 {
5161 return 0;
5162 }
5163
ossl_quic_get_cipher(unsigned int u)5164 const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
5165 {
5166 return NULL;
5167 }
5168
5169 /*
5170 * SSL_get_shutdown()
5171 * ------------------
5172 */
ossl_quic_get_shutdown(const SSL * s)5173 int ossl_quic_get_shutdown(const SSL *s)
5174 {
5175 QCTX ctx;
5176 int shut = 0;
5177
5178 if (!expect_quic_conn_only(s, &ctx))
5179 return 0;
5180
5181 if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
5182 shut |= SSL_SENT_SHUTDOWN;
5183 if (!ossl_quic_channel_is_closing(ctx.qc->ch))
5184 shut |= SSL_RECEIVED_SHUTDOWN;
5185 }
5186
5187 return shut;
5188 }
5189
5190 /*
5191 * QUIC Polling Support APIs
5192 * =========================
5193 */
5194
5195 /* Do we have the R (read) condition? */
5196 QUIC_NEEDS_LOCK
test_poll_event_r(QUIC_XSO * xso)5197 static int test_poll_event_r(QUIC_XSO *xso)
5198 {
5199 int fin = 0;
5200 size_t avail = 0;
5201
5202 /*
5203 * If a stream has had the fin bit set on the last packet
5204 * received, then we need to return a 1 here to raise
5205 * SSL_POLL_EVENT_R, so that the stream can have its completion
5206 * detected and closed gracefully by an application.
5207 * However, if the client reads the data via SSL_read[_ex], that api
5208 * provides no stream status, and as a result the stream state moves to
5209 * QUIC_RSTREAM_STATE_DATA_READ, and the receive buffer is freed, which
5210 * stored the fin state, so its not directly know-able here. Instead
5211 * check for the stream state being QUIC_RSTREAM_STATE_DATA_READ, which
5212 * is only set if the last stream frame received had the fin bit set, and
5213 * the client read the data. This catches our poll/read/poll case
5214 */
5215 if (xso->stream->recv_state == QUIC_RSTREAM_STATE_DATA_READ)
5216 return 1;
5217
5218 return ossl_quic_stream_has_recv_buffer(xso->stream)
5219 && ossl_quic_rstream_available(xso->stream->rstream, &avail, &fin)
5220 && (avail > 0 || (fin && !xso->retired_fin));
5221 }
5222
5223 /* Do we have the ER (exception: read) condition? */
5224 QUIC_NEEDS_LOCK
test_poll_event_er(QUIC_XSO * xso)5225 static int test_poll_event_er(QUIC_XSO *xso)
5226 {
5227 return ossl_quic_stream_has_recv(xso->stream)
5228 && ossl_quic_stream_recv_is_reset(xso->stream)
5229 && !xso->retired_fin;
5230 }
5231
5232 /* Do we have the W (write) condition? */
5233 QUIC_NEEDS_LOCK
test_poll_event_w(QUIC_XSO * xso)5234 static int test_poll_event_w(QUIC_XSO *xso)
5235 {
5236 return !xso->conn->shutting_down
5237 && ossl_quic_stream_has_send_buffer(xso->stream)
5238 && ossl_quic_sstream_get_buffer_avail(xso->stream->sstream)
5239 && !ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)
5240 && ossl_quic_txfc_get_cwm(&xso->stream->txfc)
5241 > ossl_quic_sstream_get_cur_size(xso->stream->sstream)
5242 && quic_mutation_allowed(xso->conn, /*req_active=*/1);
5243 }
5244
5245 /* Do we have the EW (exception: write) condition? */
5246 QUIC_NEEDS_LOCK
test_poll_event_ew(QUIC_XSO * xso)5247 static int test_poll_event_ew(QUIC_XSO *xso)
5248 {
5249 return ossl_quic_stream_has_send(xso->stream)
5250 && xso->stream->peer_stop_sending
5251 && !xso->requested_reset
5252 && !xso->conn->shutting_down;
5253 }
5254
5255 /* Do we have the EC (exception: connection) condition? */
5256 QUIC_NEEDS_LOCK
test_poll_event_ec(QUIC_CONNECTION * qc)5257 static int test_poll_event_ec(QUIC_CONNECTION *qc)
5258 {
5259 return ossl_quic_channel_is_term_any(qc->ch);
5260 }
5261
5262 /* Do we have the ECD (exception: connection drained) condition? */
5263 QUIC_NEEDS_LOCK
test_poll_event_ecd(QUIC_CONNECTION * qc)5264 static int test_poll_event_ecd(QUIC_CONNECTION *qc)
5265 {
5266 return ossl_quic_channel_is_terminated(qc->ch);
5267 }
5268
5269 /* Do we have the IS (incoming: stream) condition? */
5270 QUIC_NEEDS_LOCK
test_poll_event_is(QUIC_CONNECTION * qc,int is_uni)5271 static int test_poll_event_is(QUIC_CONNECTION *qc, int is_uni)
5272 {
5273 return ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(qc->ch),
5274 is_uni);
5275 }
5276
5277 /* Do we have the OS (outgoing: stream) condition? */
5278 QUIC_NEEDS_LOCK
test_poll_event_os(QUIC_CONNECTION * qc,int is_uni)5279 static int test_poll_event_os(QUIC_CONNECTION *qc, int is_uni)
5280 {
5281 /* Is it currently possible for us to make an outgoing stream? */
5282 return quic_mutation_allowed(qc, /*req_active=*/1)
5283 && ossl_quic_channel_get_local_stream_count_avail(qc->ch, is_uni) > 0;
5284 }
5285
5286 /* Do we have the EL (exception: listener) condition? */
5287 QUIC_NEEDS_LOCK
test_poll_event_el(QUIC_LISTENER * ql)5288 static int test_poll_event_el(QUIC_LISTENER *ql)
5289 {
5290 return !ossl_quic_port_is_running(ql->port);
5291 }
5292
5293 /* Do we have the IC (incoming: connection) condition? */
5294 QUIC_NEEDS_LOCK
test_poll_event_ic(QUIC_LISTENER * ql)5295 static int test_poll_event_ic(QUIC_LISTENER *ql)
5296 {
5297 return ossl_quic_port_get_num_incoming_channels(ql->port) > 0;
5298 }
5299
5300 QUIC_TAKES_LOCK
ossl_quic_conn_poll_events(SSL * ssl,uint64_t events,int do_tick,uint64_t * p_revents)5301 int ossl_quic_conn_poll_events(SSL *ssl, uint64_t events, int do_tick,
5302 uint64_t *p_revents)
5303 {
5304 QCTX ctx;
5305 uint64_t revents = 0;
5306
5307 if (!expect_quic_csl(ssl, &ctx))
5308 return 0;
5309
5310 qctx_lock(&ctx);
5311
5312 if (ctx.qc != NULL && !ctx.qc->started) {
5313 /* We can only try to write on non-started connection. */
5314 if ((events & SSL_POLL_EVENT_W) != 0)
5315 revents |= SSL_POLL_EVENT_W;
5316 goto end;
5317 }
5318
5319 if (do_tick)
5320 ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx.obj), 0);
5321
5322 if (ctx.xso != NULL) {
5323 /* SSL object has a stream component. */
5324
5325 if ((events & SSL_POLL_EVENT_R) != 0
5326 && test_poll_event_r(ctx.xso))
5327 revents |= SSL_POLL_EVENT_R;
5328
5329 if ((events & SSL_POLL_EVENT_ER) != 0
5330 && test_poll_event_er(ctx.xso))
5331 revents |= SSL_POLL_EVENT_ER;
5332
5333 if ((events & SSL_POLL_EVENT_W) != 0
5334 && test_poll_event_w(ctx.xso))
5335 revents |= SSL_POLL_EVENT_W;
5336
5337 if ((events & SSL_POLL_EVENT_EW) != 0
5338 && test_poll_event_ew(ctx.xso))
5339 revents |= SSL_POLL_EVENT_EW;
5340 }
5341
5342 if (ctx.qc != NULL && !ctx.is_stream) {
5343 if ((events & SSL_POLL_EVENT_EC) != 0
5344 && test_poll_event_ec(ctx.qc))
5345 revents |= SSL_POLL_EVENT_EC;
5346
5347 if ((events & SSL_POLL_EVENT_ECD) != 0
5348 && test_poll_event_ecd(ctx.qc))
5349 revents |= SSL_POLL_EVENT_ECD;
5350
5351 if ((events & SSL_POLL_EVENT_ISB) != 0
5352 && test_poll_event_is(ctx.qc, /*uni=*/0))
5353 revents |= SSL_POLL_EVENT_ISB;
5354
5355 if ((events & SSL_POLL_EVENT_ISU) != 0
5356 && test_poll_event_is(ctx.qc, /*uni=*/1))
5357 revents |= SSL_POLL_EVENT_ISU;
5358
5359 if ((events & SSL_POLL_EVENT_OSB) != 0
5360 && test_poll_event_os(ctx.qc, /*uni=*/0))
5361 revents |= SSL_POLL_EVENT_OSB;
5362
5363 if ((events & SSL_POLL_EVENT_OSU) != 0
5364 && test_poll_event_os(ctx.qc, /*uni=*/1))
5365 revents |= SSL_POLL_EVENT_OSU;
5366 }
5367
5368 if (ctx.is_listener) {
5369 if ((events & SSL_POLL_EVENT_EL) != 0
5370 && test_poll_event_el(ctx.ql))
5371 revents |= SSL_POLL_EVENT_EL;
5372
5373 if ((events & SSL_POLL_EVENT_IC) != 0
5374 && test_poll_event_ic(ctx.ql))
5375 revents |= SSL_POLL_EVENT_IC;
5376 }
5377
5378 end:
5379 qctx_unlock(&ctx);
5380 *p_revents = revents;
5381 return 1;
5382 }
5383
5384 QUIC_TAKES_LOCK
ossl_quic_get_notifier_fd(SSL * ssl)5385 int ossl_quic_get_notifier_fd(SSL *ssl)
5386 {
5387 QCTX ctx;
5388 QUIC_REACTOR *rtor;
5389 RIO_NOTIFIER *nfy;
5390 int nfd = -1;
5391
5392 if (!expect_quic_any(ssl, &ctx))
5393 return -1;
5394
5395 qctx_lock(&ctx);
5396 rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5397 nfy = ossl_quic_reactor_get0_notifier(rtor);
5398 if (nfy == NULL)
5399 goto end;
5400 nfd = ossl_rio_notifier_as_fd(nfy);
5401
5402 end:
5403 qctx_unlock(&ctx);
5404 return nfd;
5405 }
5406
5407 QUIC_TAKES_LOCK
ossl_quic_enter_blocking_section(SSL * ssl,QUIC_REACTOR_WAIT_CTX * wctx)5408 void ossl_quic_enter_blocking_section(SSL *ssl, QUIC_REACTOR_WAIT_CTX *wctx)
5409 {
5410 QCTX ctx;
5411 QUIC_REACTOR *rtor;
5412
5413 if (!expect_quic_any(ssl, &ctx))
5414 return;
5415
5416 qctx_lock(&ctx);
5417 rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5418 ossl_quic_reactor_wait_ctx_enter(wctx, rtor);
5419 qctx_unlock(&ctx);
5420 }
5421
5422 QUIC_TAKES_LOCK
ossl_quic_leave_blocking_section(SSL * ssl,QUIC_REACTOR_WAIT_CTX * wctx)5423 void ossl_quic_leave_blocking_section(SSL *ssl, QUIC_REACTOR_WAIT_CTX *wctx)
5424 {
5425 QCTX ctx;
5426 QUIC_REACTOR *rtor;
5427
5428 if (!expect_quic_any(ssl, &ctx))
5429 return;
5430
5431 qctx_lock(&ctx);
5432 rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5433 ossl_quic_reactor_wait_ctx_leave(wctx, rtor);
5434 qctx_unlock(&ctx);
5435 }
5436
5437 /*
5438 * Internal Testing APIs
5439 * =====================
5440 */
5441
ossl_quic_conn_get_channel(SSL * s)5442 QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
5443 {
5444 QCTX ctx;
5445
5446 if (!expect_quic_conn_only(s, &ctx))
5447 return NULL;
5448
5449 return ctx.qc->ch;
5450 }
5451
ossl_quic_listener_get_port(SSL * s)5452 QUIC_PORT *ossl_quic_listener_get_port(SSL *s)
5453 {
5454 QCTX ctx;
5455
5456 /*
5457 * expect listerner only
5458 */
5459 if (!expect_quic_listener(s, &ctx))
5460 return NULL;
5461
5462 return ctx.ql->port;
5463 }
5464
ossl_quic_set_diag_title(SSL_CTX * ctx,const char * title)5465 int ossl_quic_set_diag_title(SSL_CTX *ctx, const char *title)
5466 {
5467 #ifndef OPENSSL_NO_QLOG
5468 OPENSSL_free(ctx->qlog_title);
5469 ctx->qlog_title = NULL;
5470
5471 if (title == NULL)
5472 return 1;
5473
5474 if ((ctx->qlog_title = OPENSSL_strdup(title)) == NULL)
5475 return 0;
5476 #endif
5477
5478 return 1;
5479 }
5480