Coverage Report

Created: 2026-09-12 06:55

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