Coverage Report

Created: 2026-09-12 06:55

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