Coverage Report

Created: 2026-09-12 06:55

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