Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
QUIC_TAKES_LOCK
1513
int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1514
    const SSL_SHUTDOWN_EX_ARGS *args,
1515
    size_t args_len)
1516
0
{
1517
0
    int ret;
1518
0
    QCTX ctx;
1519
0
    int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1520
0
    int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);
1521
0
    int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);
1522
1523
0
    if (!expect_quic_cs(s, &ctx))
1524
0
        return -1;
1525
1526
0
    if (ctx.is_stream) {
1527
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
1528
0
        return -1;
1529
0
    }
1530
1531
0
    qctx_lock(&ctx);
1532
1533
0
    if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1534
0
        qctx_unlock(&ctx);
1535
0
        return 1;
1536
0
    }
1537
1538
    /* Phase 1: Stream Flushing */
1539
0
    if (!wait_peer && stream_flush) {
1540
0
        qc_shutdown_flush_init(ctx.qc);
1541
1542
0
        if (!qc_shutdown_flush_finished(ctx.qc)) {
1543
0
            if (!no_block && qctx_blocking(&ctx)) {
1544
0
                ret = block_until_pred(&ctx, quic_shutdown_flush_wait, ctx.qc, 0);
1545
0
                if (ret < 1) {
1546
0
                    ret = 0;
1547
0
                    goto err;
1548
0
                }
1549
0
            } else {
1550
0
                qctx_maybe_autotick(&ctx);
1551
0
            }
1552
0
        }
1553
1554
0
        if (!qc_shutdown_flush_finished(ctx.qc)) {
1555
0
            qctx_unlock(&ctx);
1556
0
            return 0; /* ongoing */
1557
0
        }
1558
0
    }
1559
1560
    /* Phase 2: Connection Closure */
1561
0
    if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1562
0
        if (!no_block && qctx_blocking(&ctx)) {
1563
0
            ret = block_until_pred(&ctx, quic_shutdown_peer_wait, ctx.qc, 0);
1564
0
            if (ret < 1) {
1565
0
                ret = 0;
1566
0
                goto err;
1567
0
            }
1568
0
        } else {
1569
0
            qctx_maybe_autotick(&ctx);
1570
0
        }
1571
1572
0
        if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1573
0
            ret = 0; /* peer hasn't closed yet - still not done */
1574
0
            goto err;
1575
0
        }
1576
1577
        /*
1578
         * We are at least terminating - go through the normal process of
1579
         * waiting until we are in the TERMINATED state.
1580
         */
1581
0
    }
1582
1583
    /* Block mutation ops regardless of if we did stream flush. */
1584
0
    ctx.qc->shutting_down = 1;
1585
1586
    /*
1587
     * This call is a no-op if we are already terminating, so it doesn't
1588
     * affect the wait_peer case.
1589
     */
1590
0
    ossl_quic_channel_local_close(ctx.qc->ch,
1591
0
        args != NULL ? args->quic_error_code : 0,
1592
0
        args != NULL ? args->quic_reason : NULL);
1593
1594
0
    SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1595
1596
0
    if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1597
0
        qctx_unlock(&ctx);
1598
0
        return 1;
1599
0
    }
1600
1601
    /* Phase 3: Terminating Wait Time */
1602
0
    if (!no_block && qctx_blocking(&ctx)
1603
0
        && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1604
0
        ret = block_until_pred(&ctx, quic_shutdown_wait, ctx.qc, 0);
1605
0
        if (ret < 1) {
1606
0
            ret = 0;
1607
0
            goto err;
1608
0
        }
1609
0
    } else {
1610
0
        qctx_maybe_autotick(&ctx);
1611
0
    }
1612
1613
0
    ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1614
0
err:
1615
0
    qctx_unlock(&ctx);
1616
0
    return ret;
1617
0
}
1618
1619
/* SSL_ctrl */
1620
long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1621
32.8k
{
1622
32.8k
    QCTX ctx;
1623
1624
32.8k
    if (!expect_quic_csl(s, &ctx))
1625
0
        return 0;
1626
1627
32.8k
    switch (cmd) {
1628
0
    case SSL_CTRL_MODE:
1629
0
        if (ctx.is_listener)
1630
0
            return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1631
1632
        /* If called on a QCSO, update the default mode. */
1633
0
        if (!ctx.is_stream)
1634
0
            ctx.qc->default_ssl_mode |= (uint32_t)larg;
1635
1636
        /*
1637
         * If we were called on a QSSO or have a default stream, we also update
1638
         * that.
1639
         */
1640
0
        if (ctx.xso != NULL) {
1641
            /* Cannot enable EPW while AON write in progress. */
1642
0
            if (ctx.xso->aon_write_in_progress)
1643
0
                larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1644
1645
0
            ctx.xso->ssl_mode |= (uint32_t)larg;
1646
0
            return ctx.xso->ssl_mode;
1647
0
        }
1648
1649
0
        return ctx.qc->default_ssl_mode;
1650
0
    case SSL_CTRL_CLEAR_MODE:
1651
0
        if (ctx.is_listener)
1652
0
            return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1653
1654
0
        if (!ctx.is_stream)
1655
0
            ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1656
1657
0
        if (ctx.xso != NULL) {
1658
0
            ctx.xso->ssl_mode &= ~(uint32_t)larg;
1659
0
            return ctx.xso->ssl_mode;
1660
0
        }
1661
1662
0
        return ctx.qc->default_ssl_mode;
1663
1664
0
    case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1665
0
        if (ctx.is_listener)
1666
0
            return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1667
1668
0
        ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1669
        /* This ctrl also needs to be passed to the internal SSL object */
1670
0
        return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1671
1672
0
    case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1673
0
    {
1674
0
        int is_infinite;
1675
1676
0
        if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1677
0
            return 0;
1678
1679
0
        return !is_infinite;
1680
0
    }
1681
0
    case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1682
        /* For legacy compatibility with DTLS calls. */
1683
0
        return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1684
1685
        /* Mask ctrls we shouldn't support for QUIC. */
1686
0
    case SSL_CTRL_GET_READ_AHEAD:
1687
0
    case SSL_CTRL_SET_READ_AHEAD:
1688
0
    case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1689
0
    case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
1690
0
    case SSL_CTRL_SET_MAX_PIPELINES:
1691
0
        return 0;
1692
1693
32.8k
    default:
1694
        /*
1695
         * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1696
         * implementation. Either SSL_ctrl will handle it itself by direct
1697
         * access into handshake layer state, or failing that, it will be passed
1698
         * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1699
         * supported by anything, the handshake layer's ctrl method will finally
1700
         * return 0.
1701
         */
1702
32.8k
        if (ctx.is_listener)
1703
0
            return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1704
1705
32.8k
        return ossl_ctrl_internal(&ctx.qc->obj.ssl, cmd, larg, parg, /*no_quic=*/1);
1706
32.8k
    }
1707
32.8k
}
1708
1709
/* SSL_set_connect_state */
1710
int ossl_quic_set_connect_state(SSL *s, int raiseerrs)
1711
32.8k
{
1712
32.8k
    QCTX ctx;
1713
1714
32.8k
    if (!is_quic_c(s, &ctx, raiseerrs))
1715
0
        return 0;
1716
1717
32.8k
    if (ctx.qc->as_server_state == 0)
1718
32.8k
        return 1;
1719
1720
    /* Cannot be changed after handshake started */
1721
0
    if (ctx.qc->started) {
1722
0
        if (raiseerrs)
1723
0
            QUIC_RAISE_NON_NORMAL_ERROR(NULL, SSL_R_INVALID_COMMAND, NULL);
1724
0
        return 0;
1725
0
    }
1726
1727
0
    ctx.qc->as_server_state = 0;
1728
0
    return 1;
1729
0
}
1730
1731
/* SSL_set_accept_state */
1732
int ossl_quic_set_accept_state(SSL *s, int raiseerrs)
1733
320
{
1734
320
    QCTX ctx;
1735
1736
320
    if (!is_quic_c(s, &ctx, raiseerrs))
1737
320
        return 0;
1738
1739
0
    if (ctx.qc->as_server_state == 1)
1740
0
        return 1;
1741
1742
    /* Cannot be changed after handshake started */
1743
0
    if (ctx.qc->started) {
1744
0
        if (raiseerrs)
1745
0
            QUIC_RAISE_NON_NORMAL_ERROR(NULL, SSL_R_INVALID_COMMAND, NULL);
1746
0
        return 0;
1747
0
    }
1748
1749
0
    ctx.qc->as_server_state = 1;
1750
0
    return 1;
1751
0
}
1752
1753
/* SSL_do_handshake */
1754
struct quic_handshake_wait_args {
1755
    QUIC_CONNECTION *qc;
1756
};
1757
1758
static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)
1759
31.9M
{
1760
31.9M
    int want = SSL_want(qc->tls);
1761
1762
31.9M
    if (want == SSL_X509_LOOKUP
1763
31.9M
        || want == SSL_CLIENT_HELLO_CB
1764
31.9M
        || want == SSL_RETRY_VERIFY)
1765
0
        return 1;
1766
1767
31.9M
    return 0;
1768
31.9M
}
1769
1770
static int quic_handshake_wait(void *arg)
1771
0
{
1772
0
    struct quic_handshake_wait_args *args = arg;
1773
1774
0
    if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1775
0
        return -1;
1776
1777
0
    if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1778
0
        return 1;
1779
1780
0
    if (tls_wants_non_io_retry(args->qc))
1781
0
        return 1;
1782
1783
0
    return 0;
1784
0
}
1785
1786
static int configure_channel(QUIC_CONNECTION *qc)
1787
32.8k
{
1788
32.8k
    assert(qc->ch != NULL);
1789
1790
32.8k
    if (!ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1791
0
        return 0;
1792
1793
32.8k
    return 1;
1794
32.8k
}
1795
1796
static int need_notifier_for_domain_flags(uint64_t domain_flags)
1797
33.2k
{
1798
33.2k
    return (domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0
1799
33.2k
        || ((domain_flags & SSL_DOMAIN_FLAG_MULTI_THREAD) != 0
1800
33.2k
            && (domain_flags & SSL_DOMAIN_FLAG_BLOCKING) != 0);
1801
33.2k
}
1802
1803
QUIC_NEEDS_LOCK
1804
static int create_channel(QUIC_CONNECTION *qc, SSL_CTX *ctx)
1805
32.8k
{
1806
32.8k
    QUIC_ENGINE_ARGS engine_args = { 0 };
1807
32.8k
    QUIC_PORT_ARGS port_args = { 0 };
1808
1809
32.8k
    engine_args.libctx = ctx->libctx;
1810
32.8k
    engine_args.propq = ctx->propq;
1811
32.8k
#if defined(OPENSSL_THREADS)
1812
32.8k
    engine_args.mutex = qc->mutex;
1813
32.8k
#endif
1814
1815
32.8k
    if (need_notifier_for_domain_flags(ctx->domain_flags))
1816
0
        engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
1817
1818
32.8k
    qc->engine = ossl_quic_engine_new(&engine_args);
1819
32.8k
    if (qc->engine == NULL) {
1820
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1821
0
        return 0;
1822
0
    }
1823
1824
32.8k
    port_args.channel_ctx = ctx;
1825
32.8k
    qc->port = ossl_quic_engine_create_port(qc->engine, &port_args);
1826
32.8k
    if (qc->port == NULL) {
1827
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1828
0
        ossl_quic_engine_free(qc->engine);
1829
0
        qc->engine = NULL;
1830
0
        return 0;
1831
0
    }
1832
1833
32.8k
    qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
1834
32.8k
    if (qc->ch == NULL) {
1835
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1836
0
        ossl_quic_port_free(qc->port);
1837
0
        qc->port = NULL;
1838
0
        ossl_quic_engine_free(qc->engine);
1839
0
        qc->engine = NULL;
1840
0
        return 0;
1841
0
    }
1842
1843
32.8k
    return 1;
1844
32.8k
}
1845
1846
/*
1847
 * Configures a channel with the information we have accumulated via calls made
1848
 * to us from the application prior to starting a handshake attempt.
1849
 */
1850
QUIC_NEEDS_LOCK
1851
static int ensure_channel_started(QCTX *ctx)
1852
31.9M
{
1853
31.9M
    QUIC_CONNECTION *qc = ctx->qc;
1854
1855
31.9M
    if (!qc->started) {
1856
41.7k
        if (!configure_channel(qc)) {
1857
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1858
0
                "failed to configure channel");
1859
0
            return 0;
1860
0
        }
1861
1862
41.7k
        if (!ossl_quic_channel_start(qc->ch)) {
1863
0
            ossl_quic_channel_restore_err_state(qc->ch);
1864
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1865
0
                "failed to start channel");
1866
0
            return 0;
1867
0
        }
1868
1869
41.7k
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1870
41.7k
        if (qc->is_thread_assisted)
1871
0
            if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch)) {
1872
0
                QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1873
0
                    "failed to start assist thread");
1874
0
                return 0;
1875
0
            }
1876
41.7k
#endif
1877
41.7k
    }
1878
1879
31.9M
    qc->started = 1;
1880
31.9M
    return 1;
1881
31.9M
}
1882
1883
QUIC_NEEDS_LOCK
1884
static int quic_do_handshake(QCTX *ctx)
1885
42.1M
{
1886
42.1M
    int ret;
1887
42.1M
    QUIC_CONNECTION *qc = ctx->qc;
1888
42.1M
    QUIC_PORT *port;
1889
42.1M
    BIO *net_rbio, *net_wbio;
1890
1891
42.1M
    if (ossl_quic_channel_is_handshake_complete(qc->ch))
1892
        /* Handshake already completed. */
1893
12.7M
        return 1;
1894
1895
29.3M
    if (!quic_mutation_allowed(qc, /*req_active=*/0))
1896
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1897
1898
29.3M
    if (qc->as_server != qc->as_server_state) {
1899
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1900
0
        return -1; /* Non-protocol error */
1901
0
    }
1902
1903
29.3M
    port = ossl_quic_obj_get0_port(ctx->obj);
1904
29.3M
    net_rbio = ossl_quic_port_get_net_rbio(port);
1905
29.3M
    net_wbio = ossl_quic_port_get_net_wbio(port);
1906
29.3M
    if (net_rbio == NULL || net_wbio == NULL) {
1907
        /* Need read and write BIOs. */
1908
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1909
0
        return -1; /* Non-protocol error */
1910
0
    }
1911
1912
29.3M
    if (!qc->started && ossl_quic_port_is_addressed_w(port)
1913
32.8k
        && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1914
        /*
1915
         * We are trying to connect and are using addressed mode, which means we
1916
         * need an initial peer address; if we do not have a peer address yet,
1917
         * we should try to autodetect one.
1918
         *
1919
         * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
1920
         * may not be able to provide us with a peer address until they have
1921
         * finished their own processing. They may not be able to perform this
1922
         * processing until an application has finished configuring that BIO
1923
         * (e.g. with setter calls), which might happen after SSL_set_bio is
1924
         * called.
1925
         */
1926
0
        if (!csm_analyse_init_peer_addr(net_wbio, &qc->init_peer_addr))
1927
            /* best effort */
1928
0
            BIO_ADDR_clear(&qc->init_peer_addr);
1929
0
        else
1930
0
            ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
1931
0
    }
1932
1933
29.3M
    if (!qc->started
1934
32.8k
        && ossl_quic_port_is_addressed_w(port)
1935
32.8k
        && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1936
        /*
1937
         * If we still don't have a peer address in addressed mode, we can't do
1938
         * anything.
1939
         */
1940
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1941
0
        return -1; /* Non-protocol error */
1942
0
    }
1943
1944
    /*
1945
     * Start connection process. Note we may come here multiple times in
1946
     * non-blocking mode, which is fine.
1947
     */
1948
29.3M
    if (!ensure_channel_started(ctx)) /* raises on failure */
1949
0
        return -1; /* Non-protocol error */
1950
1951
29.3M
    if (ossl_quic_channel_is_handshake_complete(qc->ch))
1952
        /* The handshake is now done. */
1953
0
        return 1;
1954
1955
29.3M
    if (!qctx_blocking(ctx)) {
1956
        /* Try to advance the reactor. */
1957
29.3M
        qctx_maybe_autotick(ctx);
1958
1959
29.3M
        if (ossl_quic_channel_is_handshake_complete(qc->ch))
1960
            /* The handshake is now done. */
1961
7.87k
            return 1;
1962
1963
29.3M
        if (ossl_quic_channel_is_term_any(qc->ch)) {
1964
22.0k
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1965
22.0k
            return 0;
1966
29.2M
        } else if (ossl_quic_obj_desires_blocking(&qc->obj)) {
1967
            /*
1968
             * As a special case when doing a handshake when blocking mode is
1969
             * desired yet not available, see if the network BIOs have become
1970
             * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1971
             * which do late creation of socket FDs and therefore cannot expose
1972
             * a poll descriptor until after a network BIO is set on the QCSO.
1973
             */
1974
29.2M
            ossl_quic_engine_update_poll_descriptors(qc->obj.engine, /*force=*/1);
1975
29.2M
        }
1976
29.3M
    }
1977
1978
    /*
1979
     * We are either in blocking mode or just entered it due to the code above.
1980
     */
1981
29.2M
    if (qctx_blocking(ctx)) {
1982
        /* In blocking mode, wait for the handshake to complete. */
1983
0
        struct quic_handshake_wait_args args;
1984
1985
0
        args.qc = qc;
1986
1987
0
        ret = block_until_pred(ctx, quic_handshake_wait, &args, 0);
1988
0
        if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1989
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1990
0
            return 0; /* Shutdown before completion */
1991
0
        } else if (ret <= 0) {
1992
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1993
0
            return -1; /* Non-protocol error */
1994
0
        }
1995
1996
0
        if (tls_wants_non_io_retry(qc)) {
1997
0
            QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1998
0
            return -1;
1999
0
        }
2000
2001
0
        assert(ossl_quic_channel_is_handshake_complete(qc->ch));
2002
0
        return 1;
2003
0
    }
2004
2005
29.2M
    if (tls_wants_non_io_retry(qc)) {
2006
0
        QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
2007
0
        return -1;
2008
0
    }
2009
2010
    /*
2011
     * Otherwise, indicate that the handshake isn't done yet.
2012
     * We can only get here in non-blocking mode.
2013
     */
2014
29.2M
    QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2015
29.2M
    return -1; /* Non-protocol error */
2016
29.2M
}
2017
2018
QUIC_TAKES_LOCK
2019
int ossl_quic_do_handshake(SSL *s)
2020
31.9M
{
2021
31.9M
    int ret;
2022
31.9M
    QCTX ctx;
2023
2024
31.9M
    if (!expect_quic_cs(s, &ctx))
2025
0
        return 0;
2026
2027
31.9M
    qctx_lock_for_io(&ctx);
2028
2029
31.9M
    ret = quic_do_handshake(&ctx);
2030
31.9M
    qctx_unlock(&ctx);
2031
31.9M
    return ret;
2032
31.9M
}
2033
2034
/* SSL_connect */
2035
int ossl_quic_connect(SSL *s)
2036
0
{
2037
    /* Ensure we are in connect state (no-op if non-idle). */
2038
0
    if (!ossl_quic_set_connect_state(s, 1))
2039
0
        return -1;
2040
2041
    /* Begin or continue the handshake */
2042
0
    return ossl_quic_do_handshake(s);
2043
0
}
2044
2045
/* SSL_accept */
2046
int ossl_quic_accept(SSL *s)
2047
0
{
2048
    /* Ensure we are in accept state (no-op if non-idle). */
2049
0
    if (!ossl_quic_set_accept_state(s, 1))
2050
0
        return -1;
2051
2052
    /* Begin or continue the handshake */
2053
0
    return ossl_quic_do_handshake(s);
2054
0
}
2055
2056
/*
2057
 * QUIC Front-End I/O API: Stream Lifecycle Operations
2058
 * ===================================================
2059
 *
2060
 *         SSL_stream_new       => ossl_quic_conn_stream_new
2061
 *
2062
 */
2063
2064
/*
2065
 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
2066
 * default XSO was created. Returns 0 if it was not (e.g. because it already
2067
 * exists). Note that this is NOT an error condition.
2068
 */
2069
QUIC_NEEDS_LOCK
2070
static int qc_try_create_default_xso_for_write(QCTX *ctx)
2071
0
{
2072
0
    uint64_t flags = 0;
2073
0
    QUIC_CONNECTION *qc = ctx->qc;
2074
2075
0
    if (qc->default_xso_created
2076
0
        || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2077
        /*
2078
         * We only do this once. If the user detaches a previously created
2079
         * default XSO we don't auto-create another one.
2080
         */
2081
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
2082
2083
    /* Create a locally-initiated stream. */
2084
0
    if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2085
0
        flags |= SSL_STREAM_FLAG_UNI;
2086
2087
0
    qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
2088
0
                               /*needs_lock=*/0),
2089
0
        /*touch=*/0);
2090
0
    if (qc->default_xso == NULL)
2091
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2092
2093
0
    qc_touch_default_xso(qc);
2094
0
    return 1;
2095
0
}
2096
2097
struct quic_wait_for_stream_args {
2098
    QUIC_CONNECTION *qc;
2099
    QUIC_STREAM *qs;
2100
    QCTX *ctx;
2101
    uint64_t expect_id;
2102
};
2103
2104
QUIC_NEEDS_LOCK
2105
static int quic_wait_for_stream(void *arg)
2106
0
{
2107
0
    struct quic_wait_for_stream_args *args = arg;
2108
2109
0
    if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
2110
        /* If connection is torn down due to an error while blocking, stop. */
2111
0
        QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2112
0
        return -1;
2113
0
    }
2114
2115
0
    args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
2116
0
        args->expect_id | QUIC_STREAM_DIR_BIDI);
2117
0
    if (args->qs == NULL)
2118
0
        args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
2119
0
            args->expect_id | QUIC_STREAM_DIR_UNI);
2120
2121
0
    if (args->qs != NULL)
2122
0
        return 1; /* stream now exists */
2123
2124
0
    return 0; /* did not get a stream, keep trying */
2125
0
}
2126
2127
QUIC_NEEDS_LOCK
2128
static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek)
2129
2.48M
{
2130
    /* Called on a QCSO and we don't currently have a default stream. */
2131
2.48M
    uint64_t expect_id;
2132
2.48M
    QUIC_CONNECTION *qc = ctx->qc;
2133
2.48M
    QUIC_STREAM *qs;
2134
2.48M
    int res;
2135
2.48M
    struct quic_wait_for_stream_args wargs;
2136
2.48M
    OSSL_RTT_INFO rtt_info;
2137
2138
    /*
2139
     * If default stream functionality is disabled or we already detached
2140
     * one, don't make another default stream and just fail.
2141
     */
2142
2.48M
    if (qc->default_xso_created
2143
2.48M
        || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2144
8
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
2145
2146
    /*
2147
     * The peer may have opened a stream since we last ticked. So tick and
2148
     * see if the stream with ordinal 0 (remote, bidi/uni based on stream
2149
     * mode) exists yet. QUIC stream IDs must be allocated in order, so the
2150
     * first stream created by a peer must have an ordinal of 0.
2151
     */
2152
2.48M
    expect_id = qc->as_server
2153
2.48M
        ? QUIC_STREAM_INITIATOR_CLIENT
2154
2.48M
        : QUIC_STREAM_INITIATOR_SERVER;
2155
2156
2.48M
    qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2157
2.48M
        expect_id | QUIC_STREAM_DIR_BIDI);
2158
2.48M
    if (qs == NULL)
2159
2.48M
        qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2160
2.48M
            expect_id | QUIC_STREAM_DIR_UNI);
2161
2162
2.48M
    if (qs == NULL) {
2163
2.48M
        qctx_maybe_autotick(ctx);
2164
2165
2.48M
        qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2166
2.48M
            expect_id);
2167
2.48M
    }
2168
2169
2.48M
    if (qs == NULL) {
2170
2.48M
        if (peek)
2171
0
            return 0;
2172
2173
2.48M
        if (ossl_quic_channel_is_term_any(qc->ch)) {
2174
1.74k
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2175
2.48M
        } else if (!qctx_blocking(ctx)) {
2176
            /* Non-blocking mode, so just bail immediately. */
2177
2.48M
            return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2178
2.48M
        }
2179
2180
        /* Block until we have a stream. */
2181
0
        wargs.qc = qc;
2182
0
        wargs.qs = NULL;
2183
0
        wargs.ctx = ctx;
2184
0
        wargs.expect_id = expect_id;
2185
2186
0
        res = block_until_pred(ctx, quic_wait_for_stream, &wargs, 0);
2187
0
        if (res == 0)
2188
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2189
0
        else if (res < 0 || wargs.qs == NULL)
2190
            /* quic_wait_for_stream raised error here */
2191
0
            return 0;
2192
2193
0
        qs = wargs.qs;
2194
0
    }
2195
2196
    /*
2197
     * We now have qs != NULL. Remove it from the incoming stream queue so that
2198
     * it isn't also returned by any future SSL_accept_stream calls.
2199
     */
2200
2.97k
    ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2201
2.97k
    ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc->ch),
2202
2.97k
        qs, rtt_info.smoothed_rtt);
2203
2204
    /*
2205
     * Now make qs the default stream, creating the necessary XSO.
2206
     */
2207
2.97k
    qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
2208
2.97k
    if (qc->default_xso == NULL)
2209
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2210
2211
2.97k
    qc_touch_default_xso(qc); /* inhibits default XSO */
2212
2.97k
    return 1;
2213
2.97k
}
2214
2215
QUIC_NEEDS_LOCK
2216
static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
2217
8.94k
{
2218
8.94k
    QUIC_XSO *xso = NULL;
2219
2220
8.94k
    if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
2221
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
2222
0
        goto err;
2223
0
    }
2224
2225
8.94k
    if (!ossl_quic_obj_init(&xso->obj, qc->obj.ssl.ctx, SSL_TYPE_QUIC_XSO,
2226
8.94k
            &qc->obj.ssl, NULL, NULL)) {
2227
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
2228
0
        goto err;
2229
0
    }
2230
2231
    /* XSO refs QC */
2232
8.94k
    if (!SSL_up_ref(&qc->obj.ssl)) {
2233
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);
2234
0
        goto err;
2235
0
    }
2236
2237
8.94k
    xso->conn = qc;
2238
8.94k
    xso->ssl_mode = qc->default_ssl_mode;
2239
8.94k
    xso->ssl_options
2240
8.94k
        = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
2241
8.94k
    xso->last_error = SSL_ERROR_NONE;
2242
2243
8.94k
    xso->stream = qs;
2244
2245
8.94k
    ++qc->num_xso;
2246
8.94k
    xso_update_options(xso);
2247
8.94k
    return xso;
2248
2249
0
err:
2250
0
    OPENSSL_free(xso);
2251
0
    return NULL;
2252
8.94k
}
2253
2254
struct quic_new_stream_wait_args {
2255
    QUIC_CONNECTION *qc;
2256
    int is_uni;
2257
};
2258
2259
static int quic_new_stream_wait(void *arg)
2260
0
{
2261
0
    struct quic_new_stream_wait_args *args = arg;
2262
0
    QUIC_CONNECTION *qc = args->qc;
2263
2264
0
    if (!quic_mutation_allowed(qc, /*req_active=*/1))
2265
0
        return -1;
2266
2267
0
    if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
2268
0
        return 1;
2269
2270
0
    return 0;
2271
0
}
2272
2273
/* locking depends on need_lock */
2274
static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
2275
9.90k
{
2276
9.90k
    int ret;
2277
9.90k
    QUIC_CONNECTION *qc = ctx->qc;
2278
9.90k
    QUIC_XSO *xso = NULL;
2279
9.90k
    QUIC_STREAM *qs = NULL;
2280
9.90k
    int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
2281
9.90k
    int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
2282
9.90k
    int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
2283
2284
9.90k
    if (need_lock)
2285
9.90k
        qctx_lock(ctx);
2286
2287
9.90k
    if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
2288
2.88k
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2289
2.88k
        goto err;
2290
2.88k
    }
2291
2292
7.01k
    if (!advance
2293
7.01k
        && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
2294
3.86k
        struct quic_new_stream_wait_args args;
2295
2296
        /*
2297
         * Stream count flow control currently doesn't permit this stream to be
2298
         * opened.
2299
         */
2300
3.86k
        if (no_blocking || !qctx_blocking(ctx)) {
2301
3.86k
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
2302
3.86k
            goto err;
2303
3.86k
        }
2304
2305
0
        args.qc = qc;
2306
0
        args.is_uni = is_uni;
2307
2308
        /* Blocking mode - wait until we can get a stream. */
2309
0
        ret = block_until_pred(ctx, quic_new_stream_wait, &args, 0);
2310
0
        if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2311
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2312
0
            goto err; /* Shutdown before completion */
2313
0
        } else if (ret <= 0) {
2314
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2315
0
            goto err; /* Non-protocol error */
2316
0
        }
2317
0
    }
2318
2319
3.15k
    qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
2320
3.15k
    if (qs == NULL) {
2321
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2322
0
        goto err;
2323
0
    }
2324
2325
3.15k
    xso = create_xso_from_stream(qc, qs);
2326
3.15k
    if (xso == NULL)
2327
0
        goto err;
2328
2329
3.15k
    qc_touch_default_xso(qc); /* inhibits default XSO */
2330
3.15k
    if (need_lock)
2331
3.15k
        qctx_unlock(ctx);
2332
2333
3.15k
    return &xso->obj.ssl;
2334
2335
6.74k
err:
2336
6.74k
    OPENSSL_free(xso);
2337
6.74k
    ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
2338
6.74k
    if (need_lock)
2339
6.74k
        qctx_unlock(ctx);
2340
2341
6.74k
    return NULL;
2342
3.15k
}
2343
2344
QUIC_TAKES_LOCK
2345
SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
2346
9.90k
{
2347
9.90k
    QCTX ctx;
2348
2349
9.90k
    if (!expect_quic_conn_only(s, &ctx))
2350
0
        return NULL;
2351
2352
9.90k
    return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
2353
9.90k
}
2354
2355
/*
2356
 * QUIC Front-End I/O API: Steady-State Operations
2357
 * ===============================================
2358
 *
2359
 * Here we dispatch calls to the steady-state front-end I/O API functions; that
2360
 * is, the functions used during the established phase of a QUIC connection
2361
 * (e.g. SSL_read, SSL_write).
2362
 *
2363
 * Each function must handle both blocking and non-blocking modes. As discussed
2364
 * above, all QUIC I/O is implemented using non-blocking mode internally.
2365
 *
2366
 *         SSL_get_error        => partially implemented by ossl_quic_get_error
2367
 *         SSL_want             => ossl_quic_want
2368
 *   (BIO/)SSL_read             => ossl_quic_read
2369
 *   (BIO/)SSL_write            => ossl_quic_write
2370
 *         SSL_pending          => ossl_quic_pending
2371
 *         SSL_stream_conclude  => ossl_quic_conn_stream_conclude
2372
 *         SSL_key_update       => ossl_quic_key_update
2373
 */
2374
2375
/* SSL_get_error */
2376
int ossl_quic_get_error(const SSL *s, int i)
2377
49.1M
{
2378
49.1M
    QCTX ctx;
2379
49.1M
    int net_error, last_error;
2380
2381
    /* SSL_get_errors() should not raise new errors */
2382
49.1M
    if (!is_quic_cs(s, &ctx, 0 /* suppress errors */))
2383
320
        return SSL_ERROR_SSL;
2384
2385
49.1M
    qctx_lock(&ctx);
2386
49.1M
    net_error = ossl_quic_channel_net_error(ctx.qc->ch);
2387
49.1M
    last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
2388
49.1M
    qctx_unlock(&ctx);
2389
2390
49.1M
    if (net_error)
2391
0
        return SSL_ERROR_SYSCALL;
2392
2393
49.1M
    return last_error;
2394
49.1M
}
2395
2396
/* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
2397
static int error_to_want(int error)
2398
0
{
2399
0
    switch (error) {
2400
0
    case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */
2401
0
    case SSL_ERROR_WANT_ACCEPT: /* never used - UDP is connectionless */
2402
0
    case SSL_ERROR_ZERO_RETURN:
2403
0
    default:
2404
0
        return SSL_NOTHING;
2405
2406
0
    case SSL_ERROR_WANT_READ:
2407
0
        return SSL_READING;
2408
2409
0
    case SSL_ERROR_WANT_WRITE:
2410
0
        return SSL_WRITING;
2411
2412
0
    case SSL_ERROR_WANT_RETRY_VERIFY:
2413
0
        return SSL_RETRY_VERIFY;
2414
2415
0
    case SSL_ERROR_WANT_CLIENT_HELLO_CB:
2416
0
        return SSL_CLIENT_HELLO_CB;
2417
2418
0
    case SSL_ERROR_WANT_X509_LOOKUP:
2419
0
        return SSL_X509_LOOKUP;
2420
0
    }
2421
0
}
2422
2423
/* SSL_want */
2424
int ossl_quic_want(const SSL *s)
2425
0
{
2426
0
    QCTX ctx;
2427
0
    int w;
2428
2429
0
    if (!expect_quic_cs(s, &ctx))
2430
0
        return SSL_NOTHING;
2431
2432
0
    qctx_lock(&ctx);
2433
2434
0
    w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);
2435
2436
0
    qctx_unlock(&ctx);
2437
0
    return w;
2438
0
}
2439
2440
/*
2441
 * SSL_write
2442
 * ---------
2443
 *
2444
 * The set of functions below provide the implementation of the public SSL_write
2445
 * function. We must handle:
2446
 *
2447
 *   - both blocking and non-blocking operation at the application level,
2448
 *     depending on how we are configured;
2449
 *
2450
 *   - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
2451
 *
2452
 *   - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
2453
 *
2454
 */
2455
QUIC_NEEDS_LOCK
2456
static void quic_post_write(QUIC_XSO *xso, int did_append,
2457
    int did_append_all, uint64_t flags,
2458
    int do_tick)
2459
20.5k
{
2460
    /*
2461
     * We have appended at least one byte to the stream.
2462
     * Potentially mark stream as active, depending on FC.
2463
     */
2464
20.5k
    if (did_append)
2465
3.00k
        ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
2466
3.00k
            xso->stream);
2467
2468
20.5k
    if (did_append_all && (flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
2469
0
        ossl_quic_sstream_fin(xso->stream->sstream);
2470
2471
    /*
2472
     * Try and send.
2473
     *
2474
     * TODO(QUIC FUTURE): It is probably inefficient to try and do this
2475
     * immediately, plus we should eventually consider Nagle's algorithm.
2476
     */
2477
20.5k
    if (do_tick)
2478
20.5k
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
2479
20.5k
}
2480
2481
struct quic_write_again_args {
2482
    QUIC_XSO *xso;
2483
    const unsigned char *buf;
2484
    size_t len;
2485
    size_t total_written;
2486
    int err;
2487
    uint64_t flags;
2488
};
2489
2490
/*
2491
 * Absolute maximum write buffer size, enforced to prevent a rogue peer from
2492
 * deliberately inducing DoS. This has been chosen based on the optimal buffer
2493
 * size for an RTT of 500ms and a bandwidth of 100 Mb/s.
2494
 */
2495
0
#define MAX_WRITE_BUF_SIZE (6 * 1024 * 1024)
2496
2497
/*
2498
 * Ensure spare buffer space available (up until a limit, at least).
2499
 */
2500
QUIC_NEEDS_LOCK
2501
static int sstream_ensure_spare(QUIC_SSTREAM *sstream, uint64_t spare)
2502
20.5k
{
2503
20.5k
    size_t cur_sz = ossl_quic_sstream_get_buffer_size(sstream);
2504
20.5k
    size_t avail = ossl_quic_sstream_get_buffer_avail(sstream);
2505
20.5k
    size_t spare_ = (spare > SIZE_MAX) ? SIZE_MAX : (size_t)spare;
2506
20.5k
    size_t new_sz, growth;
2507
2508
20.5k
    if (spare_ <= avail || cur_sz == MAX_WRITE_BUF_SIZE)
2509
20.5k
        return 1;
2510
2511
0
    growth = spare_ - avail;
2512
0
    if (cur_sz + growth > MAX_WRITE_BUF_SIZE)
2513
0
        new_sz = MAX_WRITE_BUF_SIZE;
2514
0
    else
2515
0
        new_sz = cur_sz + growth;
2516
2517
0
    return ossl_quic_sstream_set_buffer_size(sstream, new_sz);
2518
20.5k
}
2519
2520
/*
2521
 * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded
2522
 * as needed according to flow control.
2523
 */
2524
QUIC_NEEDS_LOCK
2525
static int xso_sstream_append(QUIC_XSO *xso, const unsigned char *buf,
2526
    size_t len, size_t *actual_written)
2527
20.5k
{
2528
20.5k
    QUIC_SSTREAM *sstream = xso->stream->sstream;
2529
20.5k
    uint64_t cur = ossl_quic_sstream_get_cur_size(sstream);
2530
20.5k
    uint64_t cwm = ossl_quic_txfc_get_cwm(&xso->stream->txfc);
2531
20.5k
    uint64_t permitted = (cwm >= cur ? cwm - cur : 0);
2532
2533
20.5k
    if (len > permitted)
2534
17.7k
        len = (size_t)permitted;
2535
2536
20.5k
    if (!sstream_ensure_spare(sstream, len))
2537
0
        return 0;
2538
2539
20.5k
    return ossl_quic_sstream_append(sstream, buf, len, actual_written);
2540
20.5k
}
2541
2542
QUIC_NEEDS_LOCK
2543
static int quic_write_again(void *arg)
2544
0
{
2545
0
    struct quic_write_again_args *args = arg;
2546
0
    size_t actual_written = 0;
2547
2548
0
    if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
2549
        /* If connection is torn down due to an error while blocking, stop. */
2550
0
        return -2;
2551
2552
0
    if (!quic_validate_for_write(args->xso, &args->err))
2553
        /*
2554
         * Stream may have become invalid for write due to connection events
2555
         * while we blocked.
2556
         */
2557
0
        return -2;
2558
2559
0
    args->err = ERR_R_INTERNAL_ERROR;
2560
0
    if (!xso_sstream_append(args->xso, args->buf, args->len, &actual_written))
2561
0
        return -2;
2562
2563
0
    quic_post_write(args->xso, actual_written > 0,
2564
0
        args->len == actual_written, args->flags, 0);
2565
2566
0
    args->buf += actual_written;
2567
0
    args->len -= actual_written;
2568
0
    args->total_written += actual_written;
2569
2570
0
    if (args->len == 0)
2571
        /* Written everything, done. */
2572
0
        return 1;
2573
2574
    /* Not written everything yet, keep trying. */
2575
0
    return 0;
2576
0
}
2577
2578
QUIC_NEEDS_LOCK
2579
static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
2580
    uint64_t flags, size_t *written)
2581
0
{
2582
0
    int res;
2583
0
    QUIC_XSO *xso = ctx->xso;
2584
0
    struct quic_write_again_args args;
2585
0
    size_t actual_written = 0;
2586
2587
    /* First make a best effort to append as much of the data as possible. */
2588
0
    if (!xso_sstream_append(xso, buf, len, &actual_written)) {
2589
        /* Stream already finished or allocation error. */
2590
0
        *written = 0;
2591
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2592
0
    }
2593
2594
0
    quic_post_write(xso, actual_written > 0, actual_written == len, flags, 1);
2595
2596
    /*
2597
     * Record however much data we wrote
2598
     */
2599
0
    *written = actual_written;
2600
2601
0
    if (actual_written == len) {
2602
        /* Managed to append everything on the first try. */
2603
0
        return 1;
2604
0
    }
2605
2606
    /*
2607
     * We did not manage to append all of the data immediately, so the stream
2608
     * buffer has probably filled up. This means we need to block until some of
2609
     * it is freed up.
2610
     */
2611
0
    args.xso = xso;
2612
0
    args.buf = (const unsigned char *)buf + actual_written;
2613
0
    args.len = len - actual_written;
2614
0
    args.total_written = 0;
2615
0
    args.err = ERR_R_INTERNAL_ERROR;
2616
0
    args.flags = flags;
2617
2618
0
    res = block_until_pred(ctx, quic_write_again, &args, 0);
2619
0
    if (res <= 0) {
2620
0
        if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
2621
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2622
0
        else
2623
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
2624
0
    }
2625
2626
    /*
2627
     * When waiting on extra buffer space to be available, args.total_written
2628
     * holds the amount of remaining data we requested to write, which will be
2629
     * something less than the len parameter passed in, however much we wrote
2630
     * here, add it to the value that we wrote when we initially called
2631
     * xso_sstream_append
2632
     */
2633
0
    *written += args.total_written;
2634
0
    return 1;
2635
0
}
2636
2637
/*
2638
 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2639
 * write semantics.
2640
 */
2641
static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
2642
    size_t buf_len, size_t already_sent)
2643
179
{
2644
179
    assert(!xso->aon_write_in_progress);
2645
2646
179
    xso->aon_write_in_progress = 1;
2647
179
    xso->aon_buf_base = buf;
2648
179
    xso->aon_buf_pos = already_sent;
2649
179
    xso->aon_buf_len = buf_len;
2650
179
}
2651
2652
static void aon_write_finish(QUIC_XSO *xso)
2653
4
{
2654
4
    xso->aon_write_in_progress = 0;
2655
4
    xso->aon_buf_base = NULL;
2656
4
    xso->aon_buf_pos = 0;
2657
4
    xso->aon_buf_len = 0;
2658
4
}
2659
2660
QUIC_NEEDS_LOCK
2661
static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2662
    size_t len, uint64_t flags,
2663
    size_t *written)
2664
20.5k
{
2665
20.5k
    QUIC_XSO *xso = ctx->xso;
2666
20.5k
    const void *actual_buf;
2667
20.5k
    size_t actual_len, actual_written = 0;
2668
20.5k
    int accept_moving_buffer
2669
20.5k
        = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2670
2671
20.5k
    if (xso->aon_write_in_progress) {
2672
        /*
2673
         * We are in the middle of an AON write (i.e., a previous write did not
2674
         * manage to append all data to the SSTREAM and we have Enable Partial
2675
         * Write (EPW) mode disabled.)
2676
         */
2677
5.58k
        if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2678
5.58k
            || len != xso->aon_buf_len)
2679
            /*
2680
             * Pointer must not have changed if we are not in accept moving
2681
             * buffer mode. Length must never change.
2682
             */
2683
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2684
2685
5.58k
        actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2686
5.58k
        actual_len = len - xso->aon_buf_pos;
2687
5.58k
        assert(actual_len > 0);
2688
15.0k
    } else {
2689
15.0k
        actual_buf = buf;
2690
15.0k
        actual_len = len;
2691
15.0k
    }
2692
2693
    /* First make a best effort to append as much of the data as possible. */
2694
20.5k
    if (!xso_sstream_append(xso, actual_buf, actual_len, &actual_written)) {
2695
        /* Stream already finished or allocation error. */
2696
0
        *written = 0;
2697
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2698
0
    }
2699
2700
20.5k
    quic_post_write(xso, actual_written > 0, actual_written == actual_len,
2701
20.5k
        flags, qctx_should_autotick(ctx));
2702
2703
20.5k
    if (actual_written == actual_len) {
2704
        /* We have sent everything. */
2705
2.82k
        if (xso->aon_write_in_progress) {
2706
            /*
2707
             * We have sent everything, and we were in the middle of an AON
2708
             * write. The output write length is the total length of the AON
2709
             * buffer, not however many bytes we managed to write to the stream
2710
             * in this call.
2711
             */
2712
4
            *written = xso->aon_buf_len;
2713
4
            aon_write_finish(xso);
2714
2.81k
        } else {
2715
2.81k
            *written = actual_written;
2716
2.81k
        }
2717
2718
2.82k
        return 1;
2719
2.82k
    }
2720
2721
17.7k
    if (xso->aon_write_in_progress) {
2722
        /*
2723
         * AON write is in progress but we have not written everything yet. We
2724
         * may have managed to send zero bytes, or some number of bytes less
2725
         * than the total remaining which need to be appended during this
2726
         * AON operation.
2727
         */
2728
5.58k
        xso->aon_buf_pos += actual_written;
2729
5.58k
        assert(xso->aon_buf_pos < xso->aon_buf_len);
2730
5.58k
        return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2731
5.58k
    }
2732
2733
    /*
2734
     * Not in an existing AON operation but partial write is not enabled, so we
2735
     * need to begin a new AON operation. However we needn't bother if we didn't
2736
     * actually append anything.
2737
     */
2738
12.1k
    if (actual_written > 0)
2739
179
        aon_write_begin(xso, buf, len, actual_written);
2740
2741
    /*
2742
     * AON - We do not publicly admit to having appended anything until AON
2743
     * completes.
2744
     */
2745
12.1k
    *written = 0;
2746
12.1k
    return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2747
17.7k
}
2748
2749
QUIC_NEEDS_LOCK
2750
static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2751
    uint64_t flags, size_t *written)
2752
0
{
2753
0
    QUIC_XSO *xso = ctx->xso;
2754
2755
    /* Simple best effort operation. */
2756
0
    if (!xso_sstream_append(xso, buf, len, written)) {
2757
        /* Stream already finished or allocation error. */
2758
0
        *written = 0;
2759
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2760
0
    }
2761
2762
0
    quic_post_write(xso, *written > 0, *written == len, flags,
2763
0
        qctx_should_autotick(ctx));
2764
2765
0
    if (*written == 0)
2766
        /* SSL_write_ex returns 0 if it didn't write anything. */
2767
0
        return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2768
2769
0
    return 1;
2770
0
}
2771
2772
QUIC_NEEDS_LOCK
2773
static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2774
20.6k
{
2775
20.6k
    QUIC_STREAM_MAP *qsm;
2776
2777
20.6k
    if (xso == NULL || xso->stream == NULL) {
2778
0
        *err = ERR_R_INTERNAL_ERROR;
2779
0
        return 0;
2780
0
    }
2781
2782
20.6k
    switch (xso->stream->send_state) {
2783
0
    default:
2784
8
    case QUIC_SSTREAM_STATE_NONE:
2785
8
        *err = SSL_R_STREAM_RECV_ONLY;
2786
8
        return 0;
2787
2788
2.10k
    case QUIC_SSTREAM_STATE_READY:
2789
2.10k
        qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2790
2791
2.10k
        if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2792
0
            *err = ERR_R_INTERNAL_ERROR;
2793
0
            return 0;
2794
0
        }
2795
2796
        /* FALLTHROUGH */
2797
20.5k
    case QUIC_SSTREAM_STATE_SEND:
2798
20.5k
    case QUIC_SSTREAM_STATE_DATA_SENT:
2799
20.5k
        if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2800
0
            *err = SSL_R_STREAM_FINISHED;
2801
0
            return 0;
2802
0
        }
2803
20.5k
        return 1;
2804
2805
0
    case QUIC_SSTREAM_STATE_DATA_RECVD:
2806
0
        *err = SSL_R_STREAM_FINISHED;
2807
0
        return 0;
2808
2809
16
    case QUIC_SSTREAM_STATE_RESET_SENT:
2810
19
    case QUIC_SSTREAM_STATE_RESET_RECVD:
2811
19
        *err = SSL_R_STREAM_RESET;
2812
19
        return 0;
2813
20.6k
    }
2814
20.6k
}
2815
2816
QUIC_TAKES_LOCK
2817
int ossl_quic_write_flags(SSL *s, const void *buf, size_t len,
2818
    uint64_t flags, size_t *written)
2819
20.9k
{
2820
20.9k
    int ret;
2821
20.9k
    QCTX ctx;
2822
20.9k
    int partial_write, err;
2823
2824
20.9k
    *written = 0;
2825
2826
20.9k
    if (len == 0) {
2827
        /* Do not autocreate default XSO for zero-length writes. */
2828
0
        if (!expect_quic_cs(s, &ctx))
2829
0
            return 0;
2830
2831
0
        qctx_lock_for_io(&ctx);
2832
20.9k
    } else {
2833
20.9k
        if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))
2834
0
            return 0;
2835
20.9k
    }
2836
2837
20.9k
    partial_write = ((ctx.xso != NULL)
2838
20.9k
            ? ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)
2839
20.9k
            : 0);
2840
2841
20.9k
    if ((flags & ~SSL_WRITE_FLAG_CONCLUDE) != 0) {
2842
0
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_UNSUPPORTED_WRITE_FLAG, NULL);
2843
0
        goto out;
2844
0
    }
2845
2846
20.9k
    if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2847
318
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2848
318
        goto out;
2849
318
    }
2850
2851
    /*
2852
     * If we haven't finished the handshake, try to advance it.
2853
     * We don't accept writes until the handshake is completed.
2854
     */
2855
20.6k
    if (quic_do_handshake(&ctx) < 1) {
2856
0
        ret = 0;
2857
0
        goto out;
2858
0
    }
2859
2860
    /* Ensure correct stream state, stream send part not concluded, etc. */
2861
20.6k
    if (len > 0 && !quic_validate_for_write(ctx.xso, &err)) {
2862
27
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2863
27
        goto out;
2864
27
    }
2865
2866
20.5k
    if (len == 0) {
2867
0
        if ((flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
2868
0
            quic_post_write(ctx.xso, 0, 1, flags,
2869
0
                qctx_should_autotick(&ctx));
2870
2871
0
        ret = 1;
2872
0
        goto out;
2873
0
    }
2874
2875
20.5k
    if (qctx_blocking(&ctx))
2876
0
        ret = quic_write_blocking(&ctx, buf, len, flags, written);
2877
20.5k
    else if (partial_write)
2878
0
        ret = quic_write_nonblocking_epw(&ctx, buf, len, flags, written);
2879
20.5k
    else
2880
20.5k
        ret = quic_write_nonblocking_aon(&ctx, buf, len, flags, written);
2881
2882
20.9k
out:
2883
20.9k
    qctx_unlock(&ctx);
2884
20.9k
    return ret;
2885
20.5k
}
2886
2887
QUIC_TAKES_LOCK
2888
int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2889
0
{
2890
0
    return ossl_quic_write_flags(s, buf, len, 0, written);
2891
0
}
2892
2893
/*
2894
 * SSL_read
2895
 * --------
2896
 */
2897
struct quic_read_again_args {
2898
    QCTX *ctx;
2899
    QUIC_STREAM *stream;
2900
    void *buf;
2901
    size_t len;
2902
    size_t *bytes_read;
2903
    int peek;
2904
};
2905
2906
QUIC_NEEDS_LOCK
2907
static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2908
28.8M
{
2909
28.8M
    QUIC_STREAM_MAP *qsm;
2910
2911
28.8M
    *eos = 0;
2912
2913
28.8M
    if (xso == NULL || xso->stream == NULL) {
2914
0
        *err = ERR_R_INTERNAL_ERROR;
2915
0
        return 0;
2916
0
    }
2917
2918
28.8M
    switch (xso->stream->recv_state) {
2919
0
    default:
2920
0
    case QUIC_RSTREAM_STATE_NONE:
2921
0
        *err = SSL_R_STREAM_SEND_ONLY;
2922
0
        return 0;
2923
2924
11.7M
    case QUIC_RSTREAM_STATE_RECV:
2925
28.8M
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2926
28.8M
    case QUIC_RSTREAM_STATE_DATA_RECVD:
2927
28.8M
        return 1;
2928
2929
155
    case QUIC_RSTREAM_STATE_DATA_READ:
2930
155
        *eos = 1;
2931
155
        return 0;
2932
2933
147
    case QUIC_RSTREAM_STATE_RESET_RECVD:
2934
147
        qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2935
147
        ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2936
2937
        /* FALLTHROUGH */
2938
147
    case QUIC_RSTREAM_STATE_RESET_READ:
2939
147
        *err = SSL_R_STREAM_RESET;
2940
147
        return 0;
2941
28.8M
    }
2942
28.8M
}
2943
2944
QUIC_NEEDS_LOCK
2945
static int quic_read_actual(QCTX *ctx,
2946
    QUIC_STREAM *stream,
2947
    void *buf, size_t buf_len,
2948
    size_t *bytes_read,
2949
    int peek)
2950
28.8M
{
2951
28.8M
    int is_fin = 0, err, eos;
2952
28.8M
    QUIC_CONNECTION *qc = ctx->qc;
2953
2954
28.8M
    if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2955
302
        if (eos) {
2956
155
            ctx->xso->retired_fin = 1;
2957
155
            return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2958
155
        } else {
2959
147
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2960
147
        }
2961
302
    }
2962
2963
28.8M
    if (peek) {
2964
0
        if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2965
0
                bytes_read, &is_fin))
2966
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2967
2968
28.8M
    } else {
2969
28.8M
        if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2970
28.8M
                bytes_read, &is_fin))
2971
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2972
28.8M
    }
2973
2974
28.8M
    if (!peek) {
2975
28.8M
        if (*bytes_read > 0) {
2976
            /*
2977
             * We have read at least one byte from the stream. Inform stream-level
2978
             * RXFC of the retirement of controlled bytes. Update the active stream
2979
             * status (the RXFC may now want to emit a frame granting more credit to
2980
             * the peer).
2981
             */
2982
3.47k
            OSSL_RTT_INFO rtt_info;
2983
2984
3.47k
            ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2985
2986
3.47k
            if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2987
3.47k
                    rtt_info.smoothed_rtt))
2988
0
                return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2989
3.47k
        }
2990
2991
28.8M
        if (is_fin && !peek) {
2992
600
            QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2993
2994
600
            ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2995
600
        }
2996
2997
28.8M
        if (*bytes_read > 0)
2998
3.47k
            ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2999
3.47k
                stream);
3000
28.8M
    }
3001
3002
28.8M
    if (*bytes_read == 0 && is_fin) {
3003
122
        ctx->xso->retired_fin = 1;
3004
122
        return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
3005
122
    }
3006
3007
28.8M
    return 1;
3008
28.8M
}
3009
3010
QUIC_NEEDS_LOCK
3011
static int quic_read_again(void *arg)
3012
0
{
3013
0
    struct quic_read_again_args *args = arg;
3014
3015
0
    if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
3016
        /* If connection is torn down due to an error while blocking, stop. */
3017
0
        QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3018
0
        return -1;
3019
0
    }
3020
3021
0
    if (!quic_read_actual(args->ctx, args->stream,
3022
0
            args->buf, args->len, args->bytes_read,
3023
0
            args->peek))
3024
0
        return -1;
3025
3026
0
    if (*args->bytes_read > 0)
3027
        /* got at least one byte, the SSL_read op can finish now */
3028
0
        return 1;
3029
3030
0
    return 0; /* did not read anything, keep trying */
3031
0
}
3032
3033
QUIC_TAKES_LOCK
3034
static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
3035
12.7M
{
3036
12.7M
    int ret, res;
3037
12.7M
    QCTX ctx;
3038
12.7M
    struct quic_read_again_args args;
3039
3040
12.7M
    *bytes_read = 0;
3041
3042
12.7M
    if (!expect_quic_cs(s, &ctx))
3043
0
        return 0;
3044
3045
12.7M
    qctx_lock_for_io(&ctx);
3046
3047
    /* If we haven't finished the handshake, try to advance it. */
3048
12.7M
    if (quic_do_handshake(&ctx) < 1) {
3049
0
        ret = 0; /* ossl_quic_do_handshake raised error here */
3050
0
        goto out;
3051
0
    }
3052
3053
12.7M
    if (ctx.xso == NULL) {
3054
        /*
3055
         * Called on a QCSO and we don't currently have a default stream.
3056
         *
3057
         * Wait until we get a stream initiated by the peer (blocking mode) or
3058
         * fail if we don't have one yet (non-blocking mode).
3059
         */
3060
2.54M
        if (!qc_wait_for_default_xso_for_read(&ctx, /*peek=*/0)) {
3061
2.53M
            ret = 0; /* error already raised here */
3062
2.53M
            goto out;
3063
2.53M
        }
3064
3065
4.33k
        ctx.xso = ctx.qc->default_xso;
3066
4.33k
    }
3067
3068
10.2M
    if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
3069
316
        ret = 0; /* quic_read_actual raised error here */
3070
316
        goto out;
3071
316
    }
3072
3073
10.2M
    if (*bytes_read > 0) {
3074
        /*
3075
         * Even though we succeeded, tick the reactor here to ensure we are
3076
         * handling other aspects of the QUIC connection.
3077
         */
3078
2.18k
        if (quic_mutation_allowed(ctx.qc, /*req_active=*/0))
3079
1.97k
            qctx_maybe_autotick(&ctx);
3080
3081
2.18k
        ret = 1;
3082
10.2M
    } else if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
3083
1.79k
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3084
1.79k
        goto out;
3085
10.2M
    } else if (qctx_blocking(&ctx)) {
3086
        /*
3087
         * We were not able to read anything immediately, so our stream
3088
         * buffer is empty. This means we need to block until we get
3089
         * at least one byte.
3090
         */
3091
0
        args.ctx = &ctx;
3092
0
        args.stream = ctx.xso->stream;
3093
0
        args.buf = buf;
3094
0
        args.len = len;
3095
0
        args.bytes_read = bytes_read;
3096
0
        args.peek = peek;
3097
3098
0
        res = block_until_pred(&ctx, quic_read_again, &args, 0);
3099
0
        if (res == 0) {
3100
0
            ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3101
0
            goto out;
3102
0
        } else if (res < 0) {
3103
0
            ret = 0; /* quic_read_again raised error here */
3104
0
            goto out;
3105
0
        }
3106
3107
0
        ret = 1;
3108
10.2M
    } else {
3109
        /*
3110
         * We did not get any bytes and are not in blocking mode.
3111
         * Tick to see if this delivers any more.
3112
         */
3113
10.2M
        qctx_maybe_autotick(&ctx);
3114
3115
        /* Try the read again. */
3116
10.2M
        if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
3117
38
            ret = 0; /* quic_read_actual raised error here */
3118
38
            goto out;
3119
38
        }
3120
3121
10.2M
        if (*bytes_read > 0)
3122
601
            ret = 1; /* Succeeded this time. */
3123
10.2M
        else
3124
10.2M
            ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
3125
10.2M
    }
3126
3127
12.7M
out:
3128
12.7M
    qctx_unlock(&ctx);
3129
12.7M
    return ret;
3130
10.2M
}
3131
3132
int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
3133
17.1M
{
3134
17.1M
    return quic_read(s, buf, len, bytes_read, 0);
3135
17.1M
}
3136
3137
int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
3138
0
{
3139
0
    return quic_read(s, buf, len, bytes_read, 1);
3140
0
}
3141
3142
/*
3143
 * SSL_pending
3144
 * -----------
3145
 */
3146
3147
QUIC_TAKES_LOCK
3148
static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
3149
0
{
3150
0
    QCTX ctx;
3151
0
    size_t avail = 0;
3152
3153
0
    if (!expect_quic_cs(s, &ctx))
3154
0
        return 0;
3155
3156
0
    qctx_lock(&ctx);
3157
3158
0
    if (!ctx.qc->started)
3159
0
        goto out;
3160
3161
0
    if (ctx.xso == NULL) {
3162
        /* No XSO yet, but there might be a default XSO eligible to be created. */
3163
0
        if (qc_wait_for_default_xso_for_read(&ctx, /*peek=*/1)) {
3164
0
            ctx.xso = ctx.qc->default_xso;
3165
0
        } else {
3166
0
            QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
3167
0
            goto out;
3168
0
        }
3169
0
    }
3170
3171
0
    if (ctx.xso->stream == NULL) {
3172
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3173
0
        goto out;
3174
0
    }
3175
3176
0
    if (check_channel)
3177
0
        avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
3178
0
                    /*include_fin=*/1)
3179
0
            || ossl_quic_channel_has_pending(ctx.qc->ch)
3180
0
            || ossl_quic_channel_is_term_any(ctx.qc->ch);
3181
0
    else
3182
0
        avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
3183
0
            /*include_fin=*/0);
3184
3185
0
out:
3186
0
    qctx_unlock(&ctx);
3187
0
    return avail;
3188
0
}
3189
3190
size_t ossl_quic_pending(const SSL *s)
3191
0
{
3192
0
    return ossl_quic_pending_int(s, /*check_channel=*/0);
3193
0
}
3194
3195
int ossl_quic_has_pending(const SSL *s)
3196
0
{
3197
    /* Do we have app-side pending data or pending URXEs or RXEs? */
3198
0
    return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
3199
0
}
3200
3201
/*
3202
 * SSL_stream_conclude
3203
 * -------------------
3204
 */
3205
QUIC_TAKES_LOCK
3206
int ossl_quic_conn_stream_conclude(SSL *s)
3207
0
{
3208
0
    QCTX ctx;
3209
0
    QUIC_STREAM *qs;
3210
0
    int err;
3211
0
    int ret;
3212
3213
0
    if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))
3214
0
        return 0;
3215
3216
0
    qs = ctx.xso->stream;
3217
3218
0
    if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
3219
0
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3220
0
        qctx_unlock(&ctx);
3221
0
        return ret;
3222
0
    }
3223
3224
0
    if (!quic_validate_for_write(ctx.xso, &err)) {
3225
0
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
3226
0
        qctx_unlock(&ctx);
3227
0
        return ret;
3228
0
    }
3229
3230
0
    if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
3231
0
        qctx_unlock(&ctx);
3232
0
        return 1;
3233
0
    }
3234
3235
0
    ossl_quic_sstream_fin(qs->sstream);
3236
0
    quic_post_write(ctx.xso, 1, 0, 0, qctx_should_autotick(&ctx));
3237
0
    qctx_unlock(&ctx);
3238
0
    return 1;
3239
0
}
3240
3241
/*
3242
 * SSL_inject_net_dgram
3243
 * --------------------
3244
 */
3245
QUIC_TAKES_LOCK
3246
int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
3247
    size_t buf_len,
3248
    const BIO_ADDR *peer,
3249
    const BIO_ADDR *local)
3250
0
{
3251
0
    int ret = 0;
3252
0
    QCTX ctx;
3253
0
    QUIC_DEMUX *demux;
3254
0
    QUIC_PORT *port;
3255
3256
0
    if (!expect_quic_csl(s, &ctx))
3257
0
        return 0;
3258
3259
0
    qctx_lock(&ctx);
3260
3261
0
    port = ossl_quic_obj_get0_port(ctx.obj);
3262
0
    if (port == NULL) {
3263
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
3264
0
        goto err;
3265
0
    }
3266
3267
0
    demux = ossl_quic_port_get0_demux(port);
3268
0
    ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
3269
3270
0
err:
3271
0
    qctx_unlock(&ctx);
3272
0
    return ret;
3273
0
}
3274
3275
/*
3276
 * SSL_get0_connection
3277
 * -------------------
3278
 */
3279
SSL *ossl_quic_get0_connection(SSL *s)
3280
0
{
3281
0
    QCTX ctx;
3282
3283
0
    if (!expect_quic_cs(s, &ctx))
3284
0
        return NULL;
3285
3286
0
    return &ctx.qc->obj.ssl;
3287
0
}
3288
3289
/*
3290
 * SSL_get0_listener
3291
 * -----------------
3292
 */
3293
SSL *ossl_quic_get0_listener(SSL *s)
3294
0
{
3295
0
    QCTX ctx;
3296
3297
0
    if (!expect_quic_csl(s, &ctx))
3298
0
        return NULL;
3299
3300
0
    return ctx.ql != NULL ? &ctx.ql->obj.ssl : NULL;
3301
0
}
3302
3303
/*
3304
 * SSL_get0_domain
3305
 * ---------------
3306
 */
3307
SSL *ossl_quic_get0_domain(SSL *s)
3308
0
{
3309
0
    QCTX ctx;
3310
3311
0
    if (!expect_quic_any(s, &ctx))
3312
0
        return NULL;
3313
3314
0
    return ctx.qd != NULL ? &ctx.qd->obj.ssl : NULL;
3315
0
}
3316
3317
/*
3318
 * SSL_get_domain_flags
3319
 * --------------------
3320
 */
3321
int ossl_quic_get_domain_flags(const SSL *ssl, uint64_t *domain_flags)
3322
0
{
3323
0
    QCTX ctx;
3324
3325
0
    if (!expect_quic_any(ssl, &ctx))
3326
0
        return 0;
3327
3328
0
    if (domain_flags != NULL)
3329
0
        *domain_flags = ctx.obj->domain_flags;
3330
3331
0
    return 1;
3332
0
}
3333
3334
/*
3335
 * SSL_get_stream_type
3336
 * -------------------
3337
 */
3338
int ossl_quic_get_stream_type(SSL *s)
3339
0
{
3340
0
    QCTX ctx;
3341
3342
0
    if (!expect_quic_cs(s, &ctx))
3343
0
        return SSL_STREAM_TYPE_BIDI;
3344
3345
0
    if (ctx.xso == NULL) {
3346
        /*
3347
         * If deferred XSO creation has yet to occur, proceed according to the
3348
         * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
3349
         * what kind of stream will be created yet, so return BIDI on the basis
3350
         * that at this time, the client still has the option of calling
3351
         * SSL_read() or SSL_write() first.
3352
         */
3353
0
        if (ctx.qc->default_xso_created
3354
0
            || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3355
0
            return SSL_STREAM_TYPE_NONE;
3356
0
        else
3357
0
            return SSL_STREAM_TYPE_BIDI;
3358
0
    }
3359
3360
0
    if (ossl_quic_stream_is_bidi(ctx.xso->stream))
3361
0
        return SSL_STREAM_TYPE_BIDI;
3362
3363
0
    if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
3364
0
        return SSL_STREAM_TYPE_READ;
3365
0
    else
3366
0
        return SSL_STREAM_TYPE_WRITE;
3367
0
}
3368
3369
/*
3370
 * SSL_get_stream_id
3371
 * -----------------
3372
 */
3373
QUIC_TAKES_LOCK
3374
uint64_t ossl_quic_get_stream_id(SSL *s)
3375
0
{
3376
0
    QCTX ctx;
3377
0
    uint64_t id;
3378
3379
0
    if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
3380
0
        return UINT64_MAX;
3381
3382
0
    id = ctx.xso->stream->id;
3383
0
    qctx_unlock(&ctx);
3384
3385
0
    return id;
3386
0
}
3387
3388
/*
3389
 * SSL_is_stream_local
3390
 * -------------------
3391
 */
3392
QUIC_TAKES_LOCK
3393
int ossl_quic_is_stream_local(SSL *s)
3394
0
{
3395
0
    QCTX ctx;
3396
0
    int is_local;
3397
3398
0
    if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
3399
0
        return -1;
3400
3401
0
    is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);
3402
0
    qctx_unlock(&ctx);
3403
3404
0
    return is_local;
3405
0
}
3406
3407
/*
3408
 * SSL_set_default_stream_mode
3409
 * ---------------------------
3410
 */
3411
QUIC_TAKES_LOCK
3412
int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
3413
0
{
3414
0
    QCTX ctx;
3415
3416
0
    if (!expect_quic_conn_only(s, &ctx))
3417
0
        return 0;
3418
3419
0
    qctx_lock(&ctx);
3420
3421
0
    if (ctx.qc->default_xso_created) {
3422
0
        qctx_unlock(&ctx);
3423
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3424
0
            "too late to change default stream mode");
3425
0
    }
3426
3427
0
    switch (mode) {
3428
0
    case SSL_DEFAULT_STREAM_MODE_NONE:
3429
0
    case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
3430
0
    case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
3431
0
        ctx.qc->default_stream_mode = mode;
3432
0
        break;
3433
0
    default:
3434
0
        qctx_unlock(&ctx);
3435
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3436
0
            "bad default stream type");
3437
0
    }
3438
3439
0
    qctx_unlock(&ctx);
3440
0
    return 1;
3441
0
}
3442
3443
/*
3444
 * SSL_detach_stream
3445
 * -----------------
3446
 */
3447
QUIC_TAKES_LOCK
3448
SSL *ossl_quic_detach_stream(SSL *s)
3449
0
{
3450
0
    QCTX ctx;
3451
0
    QUIC_XSO *xso = NULL;
3452
3453
0
    if (!expect_quic_conn_only(s, &ctx))
3454
0
        return NULL;
3455
3456
0
    qctx_lock(&ctx);
3457
3458
    /* Calling this function inhibits default XSO autocreation. */
3459
    /* QC ref to any default XSO is transferred to us and to caller. */
3460
0
    qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
3461
3462
0
    qctx_unlock(&ctx);
3463
3464
0
    return xso != NULL ? &xso->obj.ssl : NULL;
3465
0
}
3466
3467
/*
3468
 * SSL_attach_stream
3469
 * -----------------
3470
 */
3471
QUIC_TAKES_LOCK
3472
int ossl_quic_attach_stream(SSL *conn, SSL *stream)
3473
0
{
3474
0
    QCTX ctx;
3475
0
    QUIC_XSO *xso;
3476
0
    int nref;
3477
3478
0
    if (!expect_quic_conn_only(conn, &ctx))
3479
0
        return 0;
3480
3481
0
    if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
3482
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
3483
0
            "stream to attach must be a valid QUIC stream");
3484
3485
0
    xso = (QUIC_XSO *)stream;
3486
3487
0
    qctx_lock(&ctx);
3488
3489
0
    if (ctx.qc->default_xso != NULL) {
3490
0
        qctx_unlock(&ctx);
3491
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3492
0
            "connection already has a default stream");
3493
0
    }
3494
3495
    /*
3496
     * It is a caller error for the XSO being attached as a default XSO to have
3497
     * more than one ref.
3498
     */
3499
0
    if (!CRYPTO_GET_REF(&xso->obj.ssl.references, &nref)) {
3500
0
        qctx_unlock(&ctx);
3501
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
3502
0
            "ref");
3503
0
    }
3504
3505
0
    if (nref != 1) {
3506
0
        qctx_unlock(&ctx);
3507
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3508
0
            "stream being attached must have "
3509
0
            "only 1 reference");
3510
0
    }
3511
3512
    /* Caller's reference to the XSO is transferred to us. */
3513
    /* Calling this function inhibits default XSO autocreation. */
3514
0
    qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
3515
3516
0
    qctx_unlock(&ctx);
3517
0
    return 1;
3518
0
}
3519
3520
/*
3521
 * SSL_set_incoming_stream_policy
3522
 * ------------------------------
3523
 */
3524
QUIC_NEEDS_LOCK
3525
static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
3526
92.8k
{
3527
92.8k
    switch (qc->incoming_stream_policy) {
3528
41.7k
    case SSL_INCOMING_STREAM_POLICY_AUTO:
3529
41.7k
        if ((qc->default_xso == NULL && !qc->default_xso_created)
3530
0
            || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3531
41.7k
            return SSL_INCOMING_STREAM_POLICY_ACCEPT;
3532
0
        else
3533
0
            return SSL_INCOMING_STREAM_POLICY_REJECT;
3534
3535
51.0k
    default:
3536
51.0k
        return qc->incoming_stream_policy;
3537
92.8k
    }
3538
92.8k
}
3539
3540
QUIC_NEEDS_LOCK
3541
static void qc_update_reject_policy(QUIC_CONNECTION *qc)
3542
92.3k
{
3543
92.3k
    int policy = qc_get_effective_incoming_stream_policy(qc);
3544
92.3k
    int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
3545
3546
92.3k
    ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
3547
92.3k
        enable_reject,
3548
92.3k
        qc->incoming_stream_aec);
3549
92.3k
}
3550
3551
QUIC_TAKES_LOCK
3552
int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
3553
    uint64_t aec)
3554
41.7k
{
3555
41.7k
    int ret = 1;
3556
41.7k
    QCTX ctx;
3557
3558
41.7k
    if (!expect_quic_conn_only(s, &ctx))
3559
0
        return 0;
3560
3561
41.7k
    qctx_lock(&ctx);
3562
3563
41.7k
    switch (policy) {
3564
0
    case SSL_INCOMING_STREAM_POLICY_AUTO:
3565
41.7k
    case SSL_INCOMING_STREAM_POLICY_ACCEPT:
3566
41.7k
    case SSL_INCOMING_STREAM_POLICY_REJECT:
3567
41.7k
        ctx.qc->incoming_stream_policy = policy;
3568
41.7k
        ctx.qc->incoming_stream_aec = aec;
3569
41.7k
        break;
3570
3571
0
    default:
3572
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3573
0
        ret = 0;
3574
0
        break;
3575
41.7k
    }
3576
3577
41.7k
    qc_update_reject_policy(ctx.qc);
3578
41.7k
    qctx_unlock(&ctx);
3579
41.7k
    return ret;
3580
41.7k
}
3581
3582
/*
3583
 * SSL_get_value, SSL_set_value
3584
 * ----------------------------
3585
 */
3586
QUIC_TAKES_LOCK
3587
static int qc_getset_idle_timeout(QCTX *ctx, uint32_t class_,
3588
    uint64_t *p_value_out, uint64_t *p_value_in)
3589
0
{
3590
0
    int ret = 0;
3591
0
    uint64_t value_out = 0, value_in;
3592
3593
0
    qctx_lock(ctx);
3594
3595
0
    switch (class_) {
3596
0
    case SSL_VALUE_CLASS_FEATURE_REQUEST:
3597
0
        value_out = ossl_quic_channel_get_max_idle_timeout_request(ctx->qc->ch);
3598
3599
0
        if (p_value_in != NULL) {
3600
0
            value_in = *p_value_in;
3601
0
            if (value_in > OSSL_QUIC_VLINT_MAX) {
3602
0
                QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3603
0
                    NULL);
3604
0
                goto err;
3605
0
            }
3606
3607
0
            if (ossl_quic_channel_have_generated_transport_params(ctx->qc->ch)) {
3608
0
                QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NOT_RENEGOTIABLE,
3609
0
                    NULL);
3610
0
                goto err;
3611
0
            }
3612
3613
0
            ossl_quic_channel_set_max_idle_timeout_request(ctx->qc->ch, value_in);
3614
0
        }
3615
0
        break;
3616
3617
0
    case SSL_VALUE_CLASS_FEATURE_PEER_REQUEST:
3618
0
    case SSL_VALUE_CLASS_FEATURE_NEGOTIATED:
3619
0
        if (p_value_in != NULL) {
3620
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_OP,
3621
0
                NULL);
3622
0
            goto err;
3623
0
        }
3624
3625
0
        if (!ossl_quic_channel_is_handshake_complete(ctx->qc->ch)) {
3626
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NEGOTIATION_NOT_COMPLETE,
3627
0
                NULL);
3628
0
            goto err;
3629
0
        }
3630
3631
0
        value_out = (class_ == SSL_VALUE_CLASS_FEATURE_NEGOTIATED)
3632
0
            ? ossl_quic_channel_get_max_idle_timeout_actual(ctx->qc->ch)
3633
0
            : ossl_quic_channel_get_max_idle_timeout_peer_request(ctx->qc->ch);
3634
0
        break;
3635
3636
0
    default:
3637
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3638
0
            NULL);
3639
0
        goto err;
3640
0
    }
3641
3642
0
    ret = 1;
3643
0
err:
3644
0
    qctx_unlock(ctx);
3645
0
    if (ret && p_value_out != NULL)
3646
0
        *p_value_out = value_out;
3647
3648
0
    return ret;
3649
0
}
3650
3651
QUIC_TAKES_LOCK
3652
static int qc_getset_max_pending_channels(QCTX *ctx, uint32_t class_,
3653
    uint64_t *p_value_out, uint64_t *p_value_in)
3654
0
{
3655
0
    int ret = 0;
3656
0
    uint64_t value_out = 0;
3657
3658
0
    qctx_lock(ctx);
3659
3660
0
    if (class_ == SSL_VALUE_CLASS_GENERIC && ctx->is_listener) {
3661
0
        value_out = ossl_quic_port_get_max_pending_channels(ctx->ql->port);
3662
0
        if (p_value_in != NULL)
3663
0
            ossl_quic_port_set_max_pending_channels(ctx->ql->port, *p_value_in);
3664
0
        ret = 1;
3665
0
    } else {
3666
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS, NULL);
3667
0
        ret = 0;
3668
0
    }
3669
3670
0
    qctx_unlock(ctx);
3671
3672
0
    if (ret && p_value_out != NULL)
3673
0
        *p_value_out = value_out;
3674
3675
0
    return ret;
3676
0
}
3677
3678
QUIC_TAKES_LOCK
3679
static int qc_get_stream_avail(QCTX *ctx, uint32_t class_,
3680
    int is_uni, int is_remote,
3681
    uint64_t *value)
3682
0
{
3683
0
    int ret = 0;
3684
3685
0
    if (class_ != SSL_VALUE_CLASS_GENERIC) {
3686
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3687
0
            NULL);
3688
0
        return 0;
3689
0
    }
3690
3691
0
    qctx_lock(ctx);
3692
3693
0
    *value = is_remote
3694
0
        ? ossl_quic_channel_get_remote_stream_count_avail(ctx->qc->ch, is_uni)
3695
0
        : ossl_quic_channel_get_local_stream_count_avail(ctx->qc->ch, is_uni);
3696
3697
0
    ret = 1;
3698
0
    qctx_unlock(ctx);
3699
0
    return ret;
3700
0
}
3701
3702
QUIC_NEEDS_LOCK
3703
static int qctx_should_autotick(QCTX *ctx)
3704
42.1M
{
3705
42.1M
    int event_handling_mode;
3706
42.1M
    QUIC_OBJ *obj = ctx->obj;
3707
3708
44.2M
    for (; (event_handling_mode = obj->event_handling_mode) == SSL_VALUE_EVENT_HANDLING_MODE_INHERIT
3709
44.2M
        && obj->parent_obj != NULL;
3710
42.1M
        obj = obj->parent_obj)
3711
2.11M
        ;
3712
3713
42.1M
    return event_handling_mode != SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT;
3714
42.1M
}
3715
3716
QUIC_NEEDS_LOCK
3717
static void qctx_maybe_autotick(QCTX *ctx)
3718
49.1M
{
3719
49.1M
    if (!qctx_should_autotick(ctx))
3720
0
        return;
3721
3722
49.1M
    ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx->obj), 0);
3723
49.1M
}
3724
3725
QUIC_TAKES_LOCK
3726
static int qc_getset_event_handling(QCTX *ctx, uint32_t class_,
3727
    uint64_t *p_value_out,
3728
    uint64_t *p_value_in)
3729
0
{
3730
0
    int ret = 0;
3731
0
    uint64_t value_out = 0;
3732
3733
0
    qctx_lock(ctx);
3734
3735
0
    if (class_ != SSL_VALUE_CLASS_GENERIC) {
3736
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3737
0
            NULL);
3738
0
        goto err;
3739
0
    }
3740
3741
0
    if (p_value_in != NULL) {
3742
0
        switch (*p_value_in) {
3743
0
        case SSL_VALUE_EVENT_HANDLING_MODE_INHERIT:
3744
0
        case SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT:
3745
0
        case SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT:
3746
0
            break;
3747
0
        default:
3748
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3749
0
                NULL);
3750
0
            goto err;
3751
0
        }
3752
3753
0
        value_out = *p_value_in;
3754
0
        ctx->obj->event_handling_mode = (int)value_out;
3755
0
    } else {
3756
0
        value_out = ctx->obj->event_handling_mode;
3757
0
    }
3758
3759
0
    ret = 1;
3760
0
err:
3761
0
    qctx_unlock(ctx);
3762
0
    if (ret && p_value_out != NULL)
3763
0
        *p_value_out = value_out;
3764
3765
0
    return ret;
3766
0
}
3767
3768
QUIC_TAKES_LOCK
3769
static int qc_get_stream_write_buf_stat(QCTX *ctx, uint32_t class_,
3770
    uint64_t *p_value_out,
3771
    size_t (*getter)(QUIC_SSTREAM *sstream))
3772
0
{
3773
0
    int ret = 0;
3774
0
    size_t value = 0;
3775
3776
0
    qctx_lock(ctx);
3777
3778
0
    if (class_ != SSL_VALUE_CLASS_GENERIC) {
3779
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3780
0
            NULL);
3781
0
        goto err;
3782
0
    }
3783
3784
0
    if (ctx->xso == NULL) {
3785
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
3786
0
        goto err;
3787
0
    }
3788
3789
0
    if (!ossl_quic_stream_has_send(ctx->xso->stream)) {
3790
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_RECV_ONLY, NULL);
3791
0
        goto err;
3792
0
    }
3793
3794
0
    if (ossl_quic_stream_has_send_buffer(ctx->xso->stream))
3795
0
        value = getter(ctx->xso->stream->sstream);
3796
3797
0
    ret = 1;
3798
0
err:
3799
0
    qctx_unlock(ctx);
3800
0
    *p_value_out = (uint64_t)value;
3801
0
    return ret;
3802
0
}
3803
3804
QUIC_NEEDS_LOCK
3805
static int expect_quic_for_value(SSL *s, QCTX *ctx, uint32_t id)
3806
0
{
3807
0
    switch (id) {
3808
0
    case SSL_VALUE_EVENT_HANDLING_MODE:
3809
0
    case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
3810
0
    case SSL_VALUE_STREAM_WRITE_BUF_USED:
3811
0
    case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
3812
0
        return expect_quic_cs(s, ctx);
3813
0
    case SSL_VALUE_QUIC_MAX_PENDING_CONNS:
3814
0
        return expect_quic_cl(s, ctx);
3815
0
    default:
3816
0
        return expect_quic_conn_only(s, ctx);
3817
0
    }
3818
0
}
3819
3820
QUIC_TAKES_LOCK
3821
int ossl_quic_get_value_uint(SSL *s, uint32_t class_, uint32_t id,
3822
    uint64_t *value)
3823
0
{
3824
0
    QCTX ctx;
3825
3826
0
    if (!expect_quic_for_value(s, &ctx, id))
3827
0
        return 0;
3828
3829
0
    if (value == NULL)
3830
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3831
0
            ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3832
3833
0
    switch (id) {
3834
0
    case SSL_VALUE_QUIC_IDLE_TIMEOUT:
3835
0
        return qc_getset_idle_timeout(&ctx, class_, value, NULL);
3836
0
    case SSL_VALUE_QUIC_MAX_PENDING_CONNS:
3837
0
        return qc_getset_max_pending_channels(&ctx, class_, value, NULL);
3838
3839
0
    case SSL_VALUE_QUIC_STREAM_BIDI_LOCAL_AVAIL:
3840
0
        return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/0, value);
3841
0
    case SSL_VALUE_QUIC_STREAM_BIDI_REMOTE_AVAIL:
3842
0
        return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/1, value);
3843
0
    case SSL_VALUE_QUIC_STREAM_UNI_LOCAL_AVAIL:
3844
0
        return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/0, value);
3845
0
    case SSL_VALUE_QUIC_STREAM_UNI_REMOTE_AVAIL:
3846
0
        return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/1, value);
3847
3848
0
    case SSL_VALUE_EVENT_HANDLING_MODE:
3849
0
        return qc_getset_event_handling(&ctx, class_, value, NULL);
3850
3851
0
    case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
3852
0
        return qc_get_stream_write_buf_stat(&ctx, class_, value,
3853
0
            ossl_quic_sstream_get_buffer_size);
3854
0
    case SSL_VALUE_STREAM_WRITE_BUF_USED:
3855
0
        return qc_get_stream_write_buf_stat(&ctx, class_, value,
3856
0
            ossl_quic_sstream_get_buffer_used);
3857
0
    case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
3858
0
        return qc_get_stream_write_buf_stat(&ctx, class_, value,
3859
0
            ossl_quic_sstream_get_buffer_avail);
3860
3861
0
    default:
3862
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3863
0
            SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
3864
0
    }
3865
3866
0
    return 1;
3867
0
}
3868
3869
QUIC_TAKES_LOCK
3870
int ossl_quic_set_value_uint(SSL *s, uint32_t class_, uint32_t id,
3871
    uint64_t value)
3872
0
{
3873
0
    QCTX ctx;
3874
3875
0
    if (!expect_quic_for_value(s, &ctx, id))
3876
0
        return 0;
3877
3878
0
    switch (id) {
3879
0
    case SSL_VALUE_QUIC_IDLE_TIMEOUT:
3880
0
        return qc_getset_idle_timeout(&ctx, class_, NULL, &value);
3881
3882
0
    case SSL_VALUE_EVENT_HANDLING_MODE:
3883
0
        return qc_getset_event_handling(&ctx, class_, NULL, &value);
3884
0
    case SSL_VALUE_QUIC_MAX_PENDING_CONNS:
3885
0
        return qc_getset_max_pending_channels(&ctx, class_, NULL, &value);
3886
3887
0
    default:
3888
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3889
0
            SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
3890
0
    }
3891
3892
0
    return 1;
3893
0
}
3894
3895
/*
3896
 * SSL_accept_stream
3897
 * -----------------
3898
 */
3899
struct wait_for_incoming_stream_args {
3900
    QCTX *ctx;
3901
    QUIC_STREAM *qs;
3902
};
3903
3904
QUIC_NEEDS_LOCK
3905
static int wait_for_incoming_stream(void *arg)
3906
0
{
3907
0
    struct wait_for_incoming_stream_args *args = arg;
3908
0
    QUIC_CONNECTION *qc = args->ctx->qc;
3909
0
    QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
3910
3911
0
    if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
3912
        /* If connection is torn down due to an error while blocking, stop. */
3913
0
        QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3914
0
        return -1;
3915
0
    }
3916
3917
0
    args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3918
0
    if (args->qs != NULL)
3919
0
        return 1; /* got a stream */
3920
3921
0
    return 0; /* did not get a stream, keep trying */
3922
0
}
3923
3924
QUIC_TAKES_LOCK
3925
SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
3926
176
{
3927
176
    QCTX ctx;
3928
176
    int ret;
3929
176
    SSL *new_s = NULL;
3930
176
    QUIC_STREAM_MAP *qsm;
3931
176
    QUIC_STREAM *qs;
3932
176
    QUIC_XSO *xso;
3933
176
    OSSL_RTT_INFO rtt_info;
3934
3935
176
    if (!expect_quic_conn_only(s, &ctx))
3936
0
        return NULL;
3937
3938
176
    qctx_lock(&ctx);
3939
3940
176
    if (qc_get_effective_incoming_stream_policy(ctx.qc)
3941
176
        == SSL_INCOMING_STREAM_POLICY_REJECT) {
3942
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3943
0
        goto out;
3944
0
    }
3945
3946
176
    qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
3947
3948
176
    qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3949
176
    if (qs == NULL) {
3950
0
        if (qctx_blocking(&ctx)
3951
0
            && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
3952
0
            struct wait_for_incoming_stream_args args;
3953
3954
0
            args.ctx = &ctx;
3955
0
            args.qs = NULL;
3956
3957
0
            ret = block_until_pred(&ctx, wait_for_incoming_stream, &args, 0);
3958
0
            if (ret == 0) {
3959
0
                QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3960
0
                goto out;
3961
0
            } else if (ret < 0 || args.qs == NULL) {
3962
0
                goto out;
3963
0
            }
3964
3965
0
            qs = args.qs;
3966
0
        } else {
3967
0
            goto out;
3968
0
        }
3969
0
    }
3970
3971
176
    xso = create_xso_from_stream(ctx.qc, qs);
3972
176
    if (xso == NULL)
3973
0
        goto out;
3974
3975
176
    ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
3976
176
    ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
3977
176
        rtt_info.smoothed_rtt);
3978
176
    new_s = &xso->obj.ssl;
3979
3980
    /* Calling this function inhibits default XSO autocreation. */
3981
176
    qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
3982
3983
176
out:
3984
176
    qctx_unlock(&ctx);
3985
176
    return new_s;
3986
176
}
3987
3988
/*
3989
 * SSL_get_accept_stream_queue_len
3990
 * -------------------------------
3991
 */
3992
QUIC_TAKES_LOCK
3993
size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
3994
10.0k
{
3995
10.0k
    QCTX ctx;
3996
10.0k
    size_t v;
3997
3998
10.0k
    if (!expect_quic_conn_only(s, &ctx))
3999
0
        return 0;
4000
4001
10.0k
    qctx_lock(&ctx);
4002
4003
10.0k
    v = ossl_quic_stream_map_get_total_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
4004
4005
10.0k
    qctx_unlock(&ctx);
4006
10.0k
    return v;
4007
10.0k
}
4008
4009
/*
4010
 * SSL_stream_reset
4011
 * ----------------
4012
 */
4013
int ossl_quic_stream_reset(SSL *ssl,
4014
    const SSL_STREAM_RESET_ARGS *args,
4015
    size_t args_len)
4016
0
{
4017
0
    QCTX ctx;
4018
0
    QUIC_STREAM_MAP *qsm;
4019
0
    QUIC_STREAM *qs;
4020
0
    uint64_t error_code;
4021
0
    int ok, err;
4022
4023
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))
4024
0
        return 0;
4025
4026
0
    qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
4027
0
    qs = ctx.xso->stream;
4028
0
    error_code = (args != NULL ? args->quic_error_code : 0);
4029
4030
0
    if (!quic_validate_for_write(ctx.xso, &err)) {
4031
0
        ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
4032
0
        goto err;
4033
0
    }
4034
4035
0
    ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
4036
0
    if (ok)
4037
0
        ctx.xso->requested_reset = 1;
4038
4039
0
err:
4040
0
    qctx_unlock(&ctx);
4041
0
    return ok;
4042
0
}
4043
4044
/*
4045
 * SSL_get_stream_read_state
4046
 * -------------------------
4047
 */
4048
static void quic_classify_stream(QUIC_CONNECTION *qc,
4049
    QUIC_STREAM *qs,
4050
    int is_write,
4051
    int *state,
4052
    uint64_t *app_error_code)
4053
0
{
4054
0
    int local_init;
4055
0
    uint64_t scratch_pad; /* throw away value */
4056
4057
0
    local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
4058
4059
0
    if (app_error_code != NULL)
4060
0
        *app_error_code = UINT64_MAX;
4061
0
    else
4062
0
        app_error_code = &scratch_pad;
4063
4064
0
    if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
4065
        /*
4066
         * Unidirectional stream and this direction of transmission doesn't
4067
         * exist.
4068
         */
4069
0
        *state = SSL_STREAM_STATE_WRONG_DIR;
4070
0
    } else if (ossl_quic_channel_is_term_any(qc->ch)) {
4071
        /* Connection already closed. */
4072
0
        *state = SSL_STREAM_STATE_CONN_CLOSED;
4073
0
    } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
4074
        /* Application has read a FIN. */
4075
0
        *state = SSL_STREAM_STATE_FINISHED;
4076
0
    } else if ((!is_write && qs->stop_sending)
4077
0
        || (is_write && ossl_quic_stream_send_is_reset(qs))) {
4078
        /*
4079
         * Stream has been reset locally. FIN takes precedence over this for the
4080
         * read case as the application need not care if the stream is reset
4081
         * after a FIN has been successfully processed.
4082
         */
4083
0
        *state = SSL_STREAM_STATE_RESET_LOCAL;
4084
0
        *app_error_code = !is_write
4085
0
            ? qs->stop_sending_aec
4086
0
            : qs->reset_stream_aec;
4087
0
    } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
4088
0
        || (is_write && qs->peer_stop_sending)) {
4089
        /*
4090
         * Stream has been reset remotely. */
4091
0
        *state = SSL_STREAM_STATE_RESET_REMOTE;
4092
0
        *app_error_code = !is_write
4093
0
            ? qs->peer_reset_stream_aec
4094
0
            : qs->peer_stop_sending_aec;
4095
0
    } else if (is_write && qs->have_final_size) {
4096
        /*
4097
         * Stream has been finished. Stream reset takes precedence over this for
4098
         * the write case as peer may not have received all data.
4099
         */
4100
0
        *state = SSL_STREAM_STATE_FINISHED;
4101
0
    } else {
4102
        /* Stream still healthy. */
4103
0
        *state = SSL_STREAM_STATE_OK;
4104
0
    }
4105
0
}
4106
4107
static int quic_get_stream_state(SSL *ssl, int is_write)
4108
0
{
4109
0
    QCTX ctx;
4110
0
    int state;
4111
4112
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4113
0
        return SSL_STREAM_STATE_NONE;
4114
4115
0
    quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
4116
0
    qctx_unlock(&ctx);
4117
0
    return state;
4118
0
}
4119
4120
int ossl_quic_get_stream_read_state(SSL *ssl)
4121
0
{
4122
0
    return quic_get_stream_state(ssl, /*is_write=*/0);
4123
0
}
4124
4125
/*
4126
 * SSL_get_stream_write_state
4127
 * --------------------------
4128
 */
4129
int ossl_quic_get_stream_write_state(SSL *ssl)
4130
0
{
4131
0
    return quic_get_stream_state(ssl, /*is_write=*/1);
4132
0
}
4133
4134
/*
4135
 * SSL_get_stream_read_error_code
4136
 * ------------------------------
4137
 */
4138
static int quic_get_stream_error_code(SSL *ssl, int is_write,
4139
    uint64_t *app_error_code)
4140
0
{
4141
0
    QCTX ctx;
4142
0
    int state;
4143
4144
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4145
0
        return -1;
4146
4147
0
    quic_classify_stream(ctx.qc, ctx.xso->stream, is_write,
4148
0
        &state, app_error_code);
4149
4150
0
    qctx_unlock(&ctx);
4151
0
    switch (state) {
4152
0
    case SSL_STREAM_STATE_FINISHED:
4153
0
        return 0;
4154
0
    case SSL_STREAM_STATE_RESET_LOCAL:
4155
0
    case SSL_STREAM_STATE_RESET_REMOTE:
4156
0
        return 1;
4157
0
    default:
4158
0
        return -1;
4159
0
    }
4160
0
}
4161
4162
int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
4163
0
{
4164
0
    return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
4165
0
}
4166
4167
/*
4168
 * SSL_get_stream_write_error_code
4169
 * -------------------------------
4170
 */
4171
int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
4172
0
{
4173
0
    return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
4174
0
}
4175
4176
/*
4177
 * Write buffer size mutation
4178
 * --------------------------
4179
 */
4180
int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
4181
0
{
4182
0
    int ret = 0;
4183
0
    QCTX ctx;
4184
4185
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4186
0
        return 0;
4187
4188
0
    if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
4189
        /* Called on a unidirectional receive-only stream - error. */
4190
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
4191
0
        goto out;
4192
0
    }
4193
4194
0
    if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
4195
        /*
4196
         * If the stream has a send part but we have disposed of it because we
4197
         * no longer need it, this is a no-op.
4198
         */
4199
0
        ret = 1;
4200
0
        goto out;
4201
0
    }
4202
4203
0
    if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
4204
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
4205
0
        goto out;
4206
0
    }
4207
4208
0
    ret = 1;
4209
4210
0
out:
4211
0
    qctx_unlock(&ctx);
4212
0
    return ret;
4213
0
}
4214
4215
/*
4216
 * SSL_get_conn_close_info
4217
 * -----------------------
4218
 */
4219
int ossl_quic_get_conn_close_info(SSL *ssl,
4220
    SSL_CONN_CLOSE_INFO *info,
4221
    size_t info_len)
4222
0
{
4223
0
    QCTX ctx;
4224
0
    const QUIC_TERMINATE_CAUSE *tc;
4225
4226
0
    if (!expect_quic_conn_only(ssl, &ctx))
4227
0
        return -1;
4228
4229
0
    tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
4230
0
    if (tc == NULL)
4231
0
        return 0;
4232
4233
0
    info->error_code = tc->error_code;
4234
0
    info->frame_type = tc->frame_type;
4235
0
    info->reason = tc->reason;
4236
0
    info->reason_len = tc->reason_len;
4237
0
    info->flags = 0;
4238
0
    if (!tc->remote)
4239
0
        info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;
4240
0
    if (!tc->app)
4241
0
        info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;
4242
0
    return 1;
4243
0
}
4244
4245
/*
4246
 * SSL_key_update
4247
 * --------------
4248
 */
4249
int ossl_quic_key_update(SSL *ssl, int update_type)
4250
0
{
4251
0
    QCTX ctx;
4252
4253
0
    if (!expect_quic_conn_only(ssl, &ctx))
4254
0
        return 0;
4255
4256
0
    switch (update_type) {
4257
0
    case SSL_KEY_UPDATE_NOT_REQUESTED:
4258
        /*
4259
         * QUIC signals peer key update implicily by triggering a local
4260
         * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
4261
         */
4262
0
    case SSL_KEY_UPDATE_REQUESTED:
4263
0
        break;
4264
4265
0
    default:
4266
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
4267
0
        return 0;
4268
0
    }
4269
4270
0
    qctx_lock(&ctx);
4271
4272
    /* Attempt to perform a TXKU. */
4273
0
    if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
4274
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
4275
0
        qctx_unlock(&ctx);
4276
0
        return 0;
4277
0
    }
4278
4279
0
    qctx_unlock(&ctx);
4280
0
    return 1;
4281
0
}
4282
4283
/*
4284
 * SSL_get_key_update_type
4285
 * -----------------------
4286
 */
4287
int ossl_quic_get_key_update_type(const SSL *s)
4288
0
{
4289
    /*
4290
     * We always handle key updates immediately so a key update is never
4291
     * pending.
4292
     */
4293
0
    return SSL_KEY_UPDATE_NONE;
4294
0
}
4295
4296
/**
4297
 * @brief Allocates an SSL object for a user from a QUIC channel.
4298
 *
4299
 * This function creates a new QUIC_CONNECTION object based on an incoming
4300
 * connection associated with the provided QUIC_LISTENER. If the connection
4301
 * creation fails, the function returns NULL. Otherwise, it returns a pointer
4302
 * to the SSL object associated with the newly created connection.
4303
 *
4304
 * Note: This function is a registered port callback made from
4305
 * ossl_quic_new_listener and ossl_quic_new_listener_from, and allows for
4306
 * pre-allocation of the user_ssl object when a channel is created, rather than
4307
 * when it is accepted
4308
 *
4309
 * @param ch  Pointer to the QUIC_CHANNEL representing the incoming connection.
4310
 * @param arg Pointer to a QUIC_LISTENER used to create the connection.
4311
 *
4312
 * @return Pointer to the SSL object on success, or NULL on failure.
4313
 */
4314
static SSL *alloc_port_user_ssl(QUIC_CHANNEL *ch, void *arg)
4315
0
{
4316
0
    QUIC_LISTENER *ql = arg;
4317
0
    QUIC_CONNECTION *qc = create_qc_from_incoming_conn(ql, ch);
4318
4319
0
    return (qc == NULL) ? NULL : &qc->obj.ssl;
4320
0
}
4321
4322
/*
4323
 * QUIC Front-End I/O API: Listeners
4324
 * =================================
4325
 */
4326
4327
/*
4328
 * SSL_new_listener
4329
 * ----------------
4330
 */
4331
SSL *ossl_quic_new_listener(SSL_CTX *ctx, uint64_t flags)
4332
320
{
4333
320
    QUIC_LISTENER *ql = NULL;
4334
320
    QUIC_ENGINE_ARGS engine_args = { 0 };
4335
320
    QUIC_PORT_ARGS port_args = { 0 };
4336
4337
320
    if ((ql = OPENSSL_zalloc(sizeof(*ql))) == NULL) {
4338
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4339
0
        return NULL;
4340
0
    }
4341
4342
320
#if defined(OPENSSL_THREADS)
4343
320
    if ((ql->mutex = ossl_crypto_mutex_new()) == NULL) {
4344
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4345
0
        goto err;
4346
0
    }
4347
320
#endif
4348
4349
320
    engine_args.libctx = ctx->libctx;
4350
320
    engine_args.propq = ctx->propq;
4351
320
#if defined(OPENSSL_THREADS)
4352
320
    engine_args.mutex = ql->mutex;
4353
320
#endif
4354
4355
320
    if (need_notifier_for_domain_flags(ctx->domain_flags))
4356
0
        engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
4357
4358
320
    if ((ql->engine = ossl_quic_engine_new(&engine_args)) == NULL) {
4359
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4360
0
        goto err;
4361
0
    }
4362
4363
320
    port_args.channel_ctx = ctx;
4364
320
    port_args.is_multi_conn = 1;
4365
320
    port_args.get_conn_user_ssl = alloc_port_user_ssl;
4366
320
    port_args.user_ssl_arg = ql;
4367
320
    if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0)
4368
320
        port_args.do_addr_validation = 1;
4369
320
    ql->port = ossl_quic_engine_create_port(ql->engine, &port_args);
4370
320
    if (ql->port == NULL) {
4371
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4372
0
        goto err;
4373
0
    }
4374
4375
    /* TODO(QUIC FUTURE): Implement SSL_LISTENER_FLAG_NO_ACCEPT */
4376
4377
320
    ossl_quic_port_set_allow_incoming(ql->port, 1);
4378
4379
    /* Initialise the QUIC_LISTENER's object header. */
4380
320
    if (!ossl_quic_obj_init(&ql->obj, ctx, SSL_TYPE_QUIC_LISTENER, NULL,
4381
320
            ql->engine, ql->port))
4382
0
        goto err;
4383
4384
320
    return &ql->obj.ssl;
4385
4386
0
err:
4387
0
    ossl_quic_port_free(ql->port);
4388
0
    ossl_quic_engine_free(ql->engine);
4389
4390
0
#if defined(OPENSSL_THREADS)
4391
0
    ossl_crypto_mutex_free(&ql->mutex);
4392
0
#endif
4393
0
    OPENSSL_free(ql);
4394
0
    return NULL;
4395
320
}
4396
4397
/*
4398
 * SSL_new_listener_from
4399
 * ---------------------
4400
 */
4401
SSL *ossl_quic_new_listener_from(SSL *ssl, uint64_t flags)
4402
0
{
4403
0
    QCTX ctx;
4404
0
    QUIC_LISTENER *ql = NULL;
4405
0
    QUIC_PORT_ARGS port_args = { 0 };
4406
4407
0
    if (!expect_quic_domain(ssl, &ctx))
4408
0
        return NULL;
4409
4410
0
    if (!SSL_up_ref(&ctx.qd->obj.ssl))
4411
0
        return NULL;
4412
4413
0
    qctx_lock(&ctx);
4414
4415
0
    if ((ql = OPENSSL_zalloc(sizeof(*ql))) == NULL) {
4416
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4417
0
        goto err;
4418
0
    }
4419
4420
0
    port_args.channel_ctx = ssl->ctx;
4421
0
    port_args.is_multi_conn = 1;
4422
0
    port_args.get_conn_user_ssl = alloc_port_user_ssl;
4423
0
    port_args.user_ssl_arg = ql;
4424
0
    if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0)
4425
0
        port_args.do_addr_validation = 1;
4426
0
    ql->port = ossl_quic_engine_create_port(ctx.qd->engine, &port_args);
4427
0
    if (ql->port == NULL) {
4428
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4429
0
        goto err;
4430
0
    }
4431
4432
0
    ql->domain = ctx.qd;
4433
0
    ql->engine = ctx.qd->engine;
4434
0
#if defined(OPENSSL_THREADS)
4435
0
    ql->mutex = ctx.qd->mutex;
4436
0
#endif
4437
4438
    /*
4439
     * TODO(QUIC FUTURE): Implement SSL_LISTENER_FLAG_NO_ACCEPT
4440
     * Given that we have apis to create client SSL objects from
4441
     * server SSL objects (see SSL_new_from_listener), we have aspirations
4442
     * to enable a flag that allows for the creation of the latter, but not
4443
     * be used to do accept any connections.  This is a placeholder for the
4444
     * implementation of that flag
4445
     */
4446
4447
0
    ossl_quic_port_set_allow_incoming(ql->port, 1);
4448
4449
    /* Initialise the QUIC_LISTENER's object header. */
4450
0
    if (!ossl_quic_obj_init(&ql->obj, ssl->ctx, SSL_TYPE_QUIC_LISTENER,
4451
0
            &ctx.qd->obj.ssl, NULL, ql->port))
4452
0
        goto err;
4453
4454
0
    qctx_unlock(&ctx);
4455
0
    return &ql->obj.ssl;
4456
4457
0
err:
4458
0
    if (ql != NULL)
4459
0
        ossl_quic_port_free(ql->port);
4460
4461
0
    OPENSSL_free(ql);
4462
0
    qctx_unlock(&ctx);
4463
0
    SSL_free(&ctx.qd->obj.ssl);
4464
4465
0
    return NULL;
4466
0
}
4467
4468
/*
4469
 * SSL_new_from_listener
4470
 * ---------------------
4471
 * code here is derived from ossl_quic_new(). The `ssl` argument is
4472
 * a listener object which already comes with QUIC port/engine. The newly
4473
 * created QUIC connection object (QCSO) is going to share the port/engine
4474
 * with listener (`ssl`).  The `ssl` also becomes a parent of QCSO created
4475
 * by this function. The caller uses QCSO instance to connect to
4476
 * remote QUIC server.
4477
 *
4478
 * The QCSO created here requires us to also create a channel so we
4479
 * can connect to remote server.
4480
 */
4481
SSL *ossl_quic_new_from_listener(SSL *ssl, uint64_t flags)
4482
0
{
4483
0
    QCTX ctx;
4484
0
    QUIC_CONNECTION *qc = NULL;
4485
0
    QUIC_LISTENER *ql;
4486
0
    SSL_CONNECTION *sc = NULL;
4487
4488
0
    if (flags != 0)
4489
0
        return NULL;
4490
4491
0
    if (!expect_quic_listener(ssl, &ctx))
4492
0
        return NULL;
4493
4494
0
    if (!SSL_up_ref(&ctx.ql->obj.ssl))
4495
0
        return NULL;
4496
4497
0
    qctx_lock(&ctx);
4498
4499
0
    ql = ctx.ql;
4500
4501
    /*
4502
     * listeners (server) contexts don't typically
4503
     * allocate a token cache because they don't need
4504
     * to store them, but here we are using a server side
4505
     * ctx as a client, so we should allocate one now
4506
     */
4507
0
    if (ssl->ctx->tokencache == NULL)
4508
0
        if ((ssl->ctx->tokencache = ossl_quic_new_token_store()) == NULL)
4509
0
            goto err;
4510
4511
0
    if ((qc = OPENSSL_zalloc(sizeof(*qc))) == NULL) {
4512
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4513
0
        goto err;
4514
0
    }
4515
4516
    /*
4517
     * NOTE: setting a listener here is needed so `qc_cleanup()` does the right
4518
     * thing. Setting listener to ql avoids premature destruction of port in
4519
     * qc_cleanup()
4520
     */
4521
0
    qc->listener = ql;
4522
0
    qc->engine = ql->engine;
4523
0
    qc->port = ql->port;
4524
/* create channel */
4525
0
#if defined(OPENSSL_THREADS)
4526
    /* this is the engine mutex */
4527
0
    qc->mutex = ql->mutex;
4528
0
#endif
4529
0
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
4530
0
    qc->is_thread_assisted
4531
0
        = ((ql->obj.domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0);
4532
0
#endif
4533
4534
    /* Create the handshake layer. */
4535
0
    qc->tls = ossl_ssl_connection_new_int(ql->obj.ssl.ctx, &qc->obj.ssl, TLS_method());
4536
0
    if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
4537
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4538
0
        goto err;
4539
0
    }
4540
0
    sc->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL;
4541
4542
0
    qc->default_ssl_options = OSSL_QUIC_PERMITTED_OPTIONS;
4543
0
    qc->last_error = SSL_ERROR_NONE;
4544
4545
    /*
4546
     * This is QCSO, we don't expect to accept connections
4547
     * on success the channel assumes ownership of tls, we need
4548
     * to grab reference for qc.
4549
     */
4550
0
    qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
4551
0
    if (qc->ch == NULL) {
4552
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4553
0
        goto err;
4554
0
    }
4555
4556
0
    ossl_quic_channel_set_msg_callback(qc->ch, ql->obj.ssl.ctx->msg_callback, &qc->obj.ssl);
4557
0
    ossl_quic_channel_set_msg_callback_arg(qc->ch, ql->obj.ssl.ctx->msg_callback_arg);
4558
4559
    /*
4560
     * We deliberately pass NULL for engine and port, because we don't want to
4561
     * to turn QCSO we create here into an event leader, nor port leader.
4562
     * Both those roles are occupied already by listener (`ssl`) we use
4563
     * to create a new QCSO here.
4564
     */
4565
0
    if (!ossl_quic_obj_init(&qc->obj, ql->obj.ssl.ctx,
4566
0
            SSL_TYPE_QUIC_CONNECTION,
4567
0
            &ql->obj.ssl, NULL, NULL)) {
4568
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4569
0
        goto err;
4570
0
    }
4571
4572
    /* Initialise libssl APL-related state. */
4573
0
    qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
4574
0
    qc->default_ssl_mode = qc->obj.ssl.ctx->mode;
4575
0
    qc->default_ssl_options = qc->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
4576
0
    qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
4577
0
    qc->last_error = SSL_ERROR_NONE;
4578
4579
0
    qc_update_reject_policy(qc);
4580
4581
0
    qctx_unlock(&ctx);
4582
4583
0
    return &qc->obj.ssl;
4584
4585
0
err:
4586
0
    if (qc != NULL) {
4587
0
        qc_cleanup(qc, /* have_lock= */ 0);
4588
0
        OPENSSL_free(qc);
4589
0
    }
4590
0
    qctx_unlock(&ctx);
4591
0
    SSL_free(&ctx.ql->obj.ssl);
4592
4593
0
    return NULL;
4594
0
}
4595
4596
/*
4597
 * SSL_listen
4598
 * ----------
4599
 */
4600
QUIC_NEEDS_LOCK
4601
static int ql_listen(QUIC_LISTENER *ql)
4602
320
{
4603
320
    if (ql->listening)
4604
0
        return 1;
4605
4606
320
    ossl_quic_port_set_allow_incoming(ql->port, 1);
4607
320
    ql->listening = 1;
4608
320
    return 1;
4609
320
}
4610
4611
QUIC_TAKES_LOCK
4612
int ossl_quic_listen(SSL *ssl)
4613
0
{
4614
0
    QCTX ctx;
4615
0
    int ret;
4616
4617
0
    if (!expect_quic_listener(ssl, &ctx))
4618
0
        return 0;
4619
4620
0
    qctx_lock_for_io(&ctx);
4621
4622
0
    ret = ql_listen(ctx.ql);
4623
4624
0
    qctx_unlock(&ctx);
4625
0
    return ret;
4626
0
}
4627
4628
/*
4629
 * SSL_accept_connection
4630
 * ---------------------
4631
 */
4632
static int quic_accept_connection_wait(void *arg)
4633
0
{
4634
0
    QUIC_PORT *port = arg;
4635
4636
0
    if (!ossl_quic_port_is_running(port))
4637
0
        return -1;
4638
4639
0
    if (ossl_quic_port_have_incoming(port))
4640
0
        return 1;
4641
4642
0
    return 0;
4643
0
}
4644
4645
QUIC_TAKES_LOCK
4646
SSL *ossl_quic_accept_connection(SSL *ssl, uint64_t flags)
4647
163
{
4648
163
    int ret;
4649
163
    QCTX ctx;
4650
163
    SSL *conn_ssl = NULL;
4651
163
    SSL *conn_ssl_tmp = NULL;
4652
163
    SSL_CONNECTION *conn = NULL;
4653
163
    QUIC_CHANNEL *new_ch = NULL;
4654
163
    QUIC_CONNECTION *qc = NULL;
4655
163
    int no_block = ((flags & SSL_ACCEPT_CONNECTION_NO_BLOCK) != 0);
4656
4657
163
    if (!expect_quic_listener(ssl, &ctx))
4658
0
        return NULL;
4659
4660
163
    qctx_lock_for_io(&ctx);
4661
4662
163
    if (!ql_listen(ctx.ql))
4663
0
        goto out;
4664
4665
    /* Wait for an incoming connection if needed. */
4666
163
    new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4667
163
    if (new_ch == NULL && ossl_quic_port_is_running(ctx.ql->port)) {
4668
163
        if (!no_block && qctx_blocking(&ctx)) {
4669
0
            ret = block_until_pred(&ctx, quic_accept_connection_wait,
4670
0
                ctx.ql->port, 0);
4671
0
            if (ret < 1)
4672
0
                goto out;
4673
163
        } else {
4674
163
            qctx_maybe_autotick(&ctx);
4675
163
        }
4676
4677
163
        if (!ossl_quic_port_is_running(ctx.ql->port))
4678
0
            goto out;
4679
4680
163
        new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4681
163
    }
4682
4683
163
    if (new_ch == NULL && ossl_quic_port_is_running(ctx.ql->port)) {
4684
        /* No connections already queued. */
4685
163
        ossl_quic_reactor_tick(ossl_quic_engine_get0_reactor(ctx.ql->engine), 0);
4686
4687
163
        new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4688
163
    }
4689
4690
    /*
4691
     * port_make_channel pre-allocates our user_ssl for us for each newly
4692
     * created channel, so once we pop the new channel from the port above
4693
     * we just need to extract it
4694
     */
4695
163
    if (new_ch == NULL)
4696
163
        goto out;
4697
4698
    /*
4699
     * All objects below must exist, because new_ch != NULL. The objects are
4700
     * bound to new_ch. If channel constructor fails to create any item here
4701
     * it just fails to create channel.
4702
     */
4703
0
    if (!ossl_assert((conn_ssl_tmp = ossl_quic_channel_get0_tls(new_ch)) != NULL)
4704
0
        || !ossl_assert((conn = SSL_CONNECTION_FROM_SSL(conn_ssl_tmp)) != NULL)
4705
0
        || !ossl_assert((conn_ssl_tmp = SSL_CONNECTION_GET_USER_SSL(conn)) != NULL))
4706
0
        goto out;
4707
4708
0
    qc = (QUIC_CONNECTION *)conn_ssl_tmp;
4709
0
    if (SSL_up_ref(&ctx.ql->obj.ssl)) {
4710
0
        qc->listener = ctx.ql;
4711
0
        conn_ssl = conn_ssl_tmp;
4712
0
        conn_ssl_tmp = NULL;
4713
0
        qc->pending = 0;
4714
0
    }
4715
4716
163
out:
4717
4718
163
    qctx_unlock(&ctx);
4719
    /*
4720
     * You might expect ossl_quic_channel_free() to be called here. Be
4721
     * assured it happens, The process goes as follows:
4722
     *    - The SSL_free() here is being handled by ossl_quic_free().
4723
     *    - The very last step of ossl_quic_free() is call to qc_cleanup()
4724
     *      where channel gets freed.
4725
     * NOTE: We defer this SSL_free until after the call to qctx_unlock above
4726
     * to avoid the deadlock that would occur when ossl_quic_free attempts to
4727
     * re-acquire this mutex.  We also do the gymnastics with conn_ssl and
4728
     * conn_ssl_tmp above so that we only actually do the free on the SSL
4729
     * object if the up-ref above fails, in such a way that we don't unbalance
4730
     * the listener refcount (i.e. if the up-ref fails above, we don't set the
4731
     * listener pointer so that we don't then drop the ref-count erroneously
4732
     * during the free operation.
4733
     */
4734
163
    SSL_free(conn_ssl_tmp);
4735
163
    return conn_ssl;
4736
0
}
4737
4738
static QUIC_CONNECTION *create_qc_from_incoming_conn(QUIC_LISTENER *ql, QUIC_CHANNEL *ch)
4739
0
{
4740
0
    QUIC_CONNECTION *qc = NULL;
4741
4742
0
    if ((qc = OPENSSL_zalloc(sizeof(*qc))) == NULL) {
4743
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4744
0
        goto err;
4745
0
    }
4746
4747
0
    if (!ossl_quic_obj_init(&qc->obj, ql->obj.ssl.ctx,
4748
0
            SSL_TYPE_QUIC_CONNECTION,
4749
0
            &ql->obj.ssl, NULL, NULL)) {
4750
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4751
0
        goto err;
4752
0
    }
4753
4754
0
    ossl_quic_channel_get_peer_addr(ch, &qc->init_peer_addr); /* best effort */
4755
0
    qc->pending = 1;
4756
0
    qc->engine = ql->engine;
4757
0
    qc->port = ql->port;
4758
0
    qc->ch = ch;
4759
0
#if defined(OPENSSL_THREADS)
4760
0
    qc->mutex = ql->mutex;
4761
0
#endif
4762
0
    qc->tls = ossl_quic_channel_get0_tls(ch);
4763
0
    qc->started = 1;
4764
0
    qc->as_server = 1;
4765
0
    qc->as_server_state = 1;
4766
0
    qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
4767
0
    qc->default_ssl_options = ql->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
4768
0
    qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
4769
0
    qc->last_error = SSL_ERROR_NONE;
4770
0
    qc_update_reject_policy(qc);
4771
0
    return qc;
4772
4773
0
err:
4774
0
    OPENSSL_free(qc);
4775
0
    return NULL;
4776
0
}
4777
4778
DEFINE_LHASH_OF_EX(QUIC_TOKEN);
4779
4780
struct ssl_token_store_st {
4781
    LHASH_OF(QUIC_TOKEN) *cache;
4782
    CRYPTO_REF_COUNT references;
4783
    CRYPTO_MUTEX *mutex;
4784
};
4785
4786
static unsigned long quic_token_hash(const QUIC_TOKEN *item)
4787
37.2k
{
4788
37.2k
    return (unsigned long)ossl_fnv1a_hash(item->hashkey, item->hashkey_len);
4789
37.2k
}
4790
4791
static int quic_token_cmp(const QUIC_TOKEN *a, const QUIC_TOKEN *b)
4792
2.43k
{
4793
2.43k
    if (a->hashkey_len != b->hashkey_len)
4794
0
        return 1;
4795
2.43k
    return memcmp(a->hashkey, b->hashkey, a->hashkey_len);
4796
2.43k
}
4797
4798
SSL_TOKEN_STORE *ossl_quic_new_token_store(void)
4799
32.8k
{
4800
32.8k
    int ok = 0;
4801
32.8k
    SSL_TOKEN_STORE *newcache = OPENSSL_zalloc(sizeof(SSL_TOKEN_STORE));
4802
4803
32.8k
    if (newcache == NULL)
4804
0
        goto out;
4805
4806
32.8k
    newcache->cache = lh_QUIC_TOKEN_new(quic_token_hash, quic_token_cmp);
4807
32.8k
    if (newcache->cache == NULL)
4808
0
        goto out;
4809
4810
32.8k
#if defined(OPENSSL_THREADS)
4811
32.8k
    if ((newcache->mutex = ossl_crypto_mutex_new()) == NULL)
4812
0
        goto out;
4813
32.8k
#endif
4814
4815
32.8k
    if (!CRYPTO_NEW_REF(&newcache->references, 1))
4816
0
        goto out;
4817
4818
32.8k
    ok = 1;
4819
32.8k
out:
4820
32.8k
    if (!ok) {
4821
0
        ossl_quic_free_token_store(newcache);
4822
0
        newcache = NULL;
4823
0
    }
4824
32.8k
    return newcache;
4825
32.8k
}
4826
4827
static void free_this_token(QUIC_TOKEN *tok)
4828
355
{
4829
355
    ossl_quic_free_peer_token(tok);
4830
355
}
4831
4832
void ossl_quic_free_token_store(SSL_TOKEN_STORE *hdl)
4833
107k
{
4834
107k
    int refs;
4835
4836
107k
    if (hdl == NULL)
4837
74.5k
        return;
4838
4839
32.8k
    if (!CRYPTO_DOWN_REF(&hdl->references, &refs))
4840
0
        return;
4841
4842
32.8k
    if (refs > 0)
4843
0
        return;
4844
4845
    /* last reference, we can clean up */
4846
32.8k
    ossl_crypto_mutex_free(&hdl->mutex);
4847
32.8k
    lh_QUIC_TOKEN_doall(hdl->cache, free_this_token);
4848
32.8k
    lh_QUIC_TOKEN_free(hdl->cache);
4849
32.8k
    CRYPTO_FREE_REF(&hdl->references);
4850
32.8k
    OPENSSL_free(hdl);
4851
32.8k
    return;
4852
32.8k
}
4853
4854
/**
4855
 * @brief build a new QUIC_TOKEN
4856
 *
4857
 * This function creates a new token storage structure for saving in our
4858
 * tokencache
4859
 *
4860
 * In an effort to make allocation and freeing of these tokens a bit faster
4861
 * We do them in a single allocation in this format
4862
 * +---------------+        --\
4863
 * |   hashkey *   |---|      |
4864
 * |   hashkey_len |   |      | QUIC_TOKEN
4865
 * |   token *     |---|--|   |
4866
 * |   token_len   |   |  |   |
4867
 * +---------------+<--|  | --/
4868
 * |  hashkey buf  |      |
4869
 * |               |      |
4870
 * |---------------|<-----|
4871
 * |  token buf    |
4872
 * |               |
4873
 * +---------------+
4874
 *
4875
 * @param peer - the peer address that sent the token
4876
 * @param token - the buffer holding the token
4877
 * @param token_len - the size of token
4878
 *
4879
 * @returns a QUIC_TOKEN pointer or NULL on error
4880
 */
4881
static QUIC_TOKEN *ossl_quic_build_new_token(BIO_ADDR *peer, uint8_t *token,
4882
    size_t token_len)
4883
34.4k
{
4884
34.4k
    QUIC_TOKEN *new_token;
4885
34.4k
    size_t hashkey_len = 0;
4886
34.4k
    size_t addr_len = 0;
4887
34.4k
    int family;
4888
34.4k
    unsigned short port;
4889
34.4k
    int *famptr;
4890
34.4k
    unsigned short *portptr;
4891
34.4k
    uint8_t *addrptr;
4892
4893
34.4k
    if ((token != NULL && token_len == 0) || (token == NULL && token_len != 0))
4894
0
        return NULL;
4895
4896
34.4k
    if (!BIO_ADDR_rawaddress(peer, NULL, &addr_len))
4897
0
        return NULL;
4898
34.4k
    family = BIO_ADDR_family(peer);
4899
34.4k
    port = BIO_ADDR_rawport(peer);
4900
4901
34.4k
    hashkey_len += sizeof(int); /* hashkey(family) */
4902
34.4k
    hashkey_len += sizeof(unsigned short); /* hashkey(port) */
4903
34.4k
    hashkey_len += addr_len; /* hashkey(address) */
4904
4905
34.4k
    new_token = OPENSSL_zalloc(sizeof(QUIC_TOKEN) + hashkey_len + token_len);
4906
34.4k
    if (new_token == NULL)
4907
0
        return NULL;
4908
4909
34.4k
    if (!CRYPTO_NEW_REF(&new_token->references, 1)) {
4910
0
        OPENSSL_free(new_token);
4911
0
        return NULL;
4912
0
    }
4913
4914
34.4k
    new_token->hashkey_len = hashkey_len;
4915
    /* hashkey is allocated inline, immediately after the QUIC_TOKEN struct */
4916
34.4k
    new_token->hashkey = (uint8_t *)(new_token + 1);
4917
    /* token buffer follows the hashkey in the inline allocation */
4918
34.4k
    new_token->token = new_token->hashkey + hashkey_len;
4919
34.4k
    new_token->token_len = token_len;
4920
34.4k
    famptr = (int *)new_token->hashkey;
4921
34.4k
    portptr = (unsigned short *)(famptr + 1);
4922
34.4k
    addrptr = (uint8_t *)(portptr + 1);
4923
34.4k
    *famptr = family;
4924
34.4k
    *portptr = port;
4925
34.4k
    if (!BIO_ADDR_rawaddress(peer, addrptr, NULL)) {
4926
0
        ossl_quic_free_peer_token(new_token);
4927
0
        return NULL;
4928
0
    }
4929
34.4k
    if (token != NULL)
4930
1.57k
        memcpy(new_token->token, token, token_len);
4931
34.4k
    return new_token;
4932
34.4k
}
4933
4934
int ossl_quic_set_peer_token(SSL_CTX *ctx, BIO_ADDR *peer,
4935
    const uint8_t *token, size_t token_len)
4936
1.57k
{
4937
1.57k
    SSL_TOKEN_STORE *c = ctx->tokencache;
4938
1.57k
    QUIC_TOKEN *tok, *old = NULL;
4939
4940
1.57k
    if (ctx->tokencache == NULL)
4941
0
        return 0;
4942
4943
1.57k
    tok = ossl_quic_build_new_token(peer, (uint8_t *)token, token_len);
4944
1.57k
    if (tok == NULL)
4945
0
        return 0;
4946
4947
    /* we might be sharing this cache, lock it */
4948
1.57k
    ossl_crypto_mutex_lock(c->mutex);
4949
4950
1.57k
    old = lh_QUIC_TOKEN_retrieve(c->cache, tok);
4951
1.57k
    if (old != NULL) {
4952
1.21k
        lh_QUIC_TOKEN_delete(c->cache, old);
4953
1.21k
        ossl_quic_free_peer_token(old);
4954
1.21k
    }
4955
1.57k
    lh_QUIC_TOKEN_insert(c->cache, tok);
4956
1.57k
    if (lh_QUIC_TOKEN_error(c->cache)) {
4957
0
        ossl_quic_free_peer_token(tok);
4958
0
        ossl_crypto_mutex_unlock(c->mutex);
4959
0
        return 0;
4960
0
    }
4961
4962
1.57k
    ossl_crypto_mutex_unlock(c->mutex);
4963
1.57k
    return 1;
4964
1.57k
}
4965
4966
int ossl_quic_get_peer_token(SSL_CTX *ctx, BIO_ADDR *peer,
4967
    QUIC_TOKEN **token)
4968
24.4k
{
4969
24.4k
    SSL_TOKEN_STORE *c = ctx->tokencache;
4970
24.4k
    QUIC_TOKEN *key = NULL;
4971
24.4k
    QUIC_TOKEN *tok = NULL;
4972
24.4k
    int ret;
4973
24.4k
    int rc = 0;
4974
4975
24.4k
    if (c == NULL)
4976
0
        return 0;
4977
4978
24.4k
    key = ossl_quic_build_new_token(peer, NULL, 0);
4979
24.4k
    if (key == NULL)
4980
0
        return 0;
4981
4982
24.4k
    ossl_crypto_mutex_lock(c->mutex);
4983
24.4k
    tok = lh_QUIC_TOKEN_retrieve(c->cache, key);
4984
24.4k
    if (tok != NULL) {
4985
0
        *token = tok;
4986
0
        CRYPTO_UP_REF(&tok->references, &ret);
4987
0
        rc = 1;
4988
0
    }
4989
4990
24.4k
    ossl_crypto_mutex_unlock(c->mutex);
4991
24.4k
    ossl_quic_free_peer_token(key);
4992
24.4k
    return rc;
4993
24.4k
}
4994
4995
void ossl_quic_free_peer_token(QUIC_TOKEN *token)
4996
34.4k
{
4997
34.4k
    int refs = 0;
4998
4999
34.4k
    if (!CRYPTO_DOWN_REF(&token->references, &refs))
5000
0
        return;
5001
5002
34.4k
    if (refs > 0)
5003
0
        return;
5004
5005
34.4k
    CRYPTO_FREE_REF(&token->references);
5006
34.4k
    OPENSSL_free(token);
5007
34.4k
}
5008
5009
/*
5010
 * SSL_get_accept_connection_queue_len
5011
 * -----------------------------------
5012
 */
5013
QUIC_TAKES_LOCK
5014
size_t ossl_quic_get_accept_connection_queue_len(SSL *ssl)
5015
0
{
5016
0
    QCTX ctx;
5017
0
    int ret;
5018
5019
0
    if (!expect_quic_listener(ssl, &ctx))
5020
0
        return 0;
5021
5022
0
    qctx_lock(&ctx);
5023
5024
0
    ret = ossl_quic_port_get_num_incoming_channels(ctx.ql->port);
5025
5026
0
    qctx_unlock(&ctx);
5027
0
    return ret;
5028
0
}
5029
5030
/*
5031
 * QUIC Front-End I/O API: Domains
5032
 * ===============================
5033
 */
5034
5035
/*
5036
 * SSL_new_domain
5037
 * --------------
5038
 */
5039
SSL *ossl_quic_new_domain(SSL_CTX *ctx, uint64_t flags)
5040
0
{
5041
0
    QUIC_DOMAIN *qd = NULL;
5042
0
    QUIC_ENGINE_ARGS engine_args = { 0 };
5043
0
    uint64_t domain_flags;
5044
5045
0
    domain_flags = ctx->domain_flags;
5046
0
    if ((flags & (SSL_DOMAIN_FLAG_SINGLE_THREAD | SSL_DOMAIN_FLAG_MULTI_THREAD | SSL_DOMAIN_FLAG_THREAD_ASSISTED)) != 0)
5047
0
        domain_flags = flags;
5048
0
    else
5049
0
        domain_flags = ctx->domain_flags | flags;
5050
5051
0
    if (!ossl_adjust_domain_flags(domain_flags, &domain_flags))
5052
0
        return NULL;
5053
5054
0
    if ((qd = OPENSSL_zalloc(sizeof(*qd))) == NULL) {
5055
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
5056
0
        return NULL;
5057
0
    }
5058
5059
0
#if defined(OPENSSL_THREADS)
5060
0
    if ((qd->mutex = ossl_crypto_mutex_new()) == NULL) {
5061
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
5062
0
        goto err;
5063
0
    }
5064
0
#endif
5065
5066
0
    engine_args.libctx = ctx->libctx;
5067
0
    engine_args.propq = ctx->propq;
5068
0
#if defined(OPENSSL_THREADS)
5069
0
    engine_args.mutex = qd->mutex;
5070
0
#endif
5071
5072
0
    if (need_notifier_for_domain_flags(domain_flags))
5073
0
        engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
5074
5075
0
    if ((qd->engine = ossl_quic_engine_new(&engine_args)) == NULL) {
5076
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
5077
0
        goto err;
5078
0
    }
5079
5080
    /* Initialise the QUIC_DOMAIN's object header. */
5081
0
    if (!ossl_quic_obj_init(&qd->obj, ctx, SSL_TYPE_QUIC_DOMAIN, NULL,
5082
0
            qd->engine, NULL))
5083
0
        goto err;
5084
5085
0
    ossl_quic_obj_set_domain_flags(&qd->obj, domain_flags);
5086
0
    return &qd->obj.ssl;
5087
5088
0
err:
5089
0
    ossl_quic_engine_free(qd->engine);
5090
0
#if defined(OPENSSL_THREADS)
5091
0
    ossl_crypto_mutex_free(&qd->mutex);
5092
0
#endif
5093
0
    OPENSSL_free(qd);
5094
0
    return NULL;
5095
0
}
5096
5097
/*
5098
 * QUIC Front-End I/O API: SSL_CTX Management
5099
 * ==========================================
5100
 */
5101
5102
long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
5103
33.2k
{
5104
33.2k
    switch (cmd) {
5105
33.2k
    default:
5106
33.2k
        return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
5107
33.2k
    }
5108
33.2k
}
5109
5110
long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
5111
0
{
5112
0
    QCTX ctx;
5113
5114
0
    if (!expect_quic_conn_only(s, &ctx))
5115
0
        return 0;
5116
5117
0
    switch (cmd) {
5118
0
    case SSL_CTRL_SET_MSG_CALLBACK:
5119
0
        ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
5120
0
            &ctx.qc->obj.ssl);
5121
        /* This callback also needs to be set on the internal SSL object */
5122
0
        return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
5123
0
        ;
5124
5125
0
    default:
5126
        /* Probably a TLS related ctrl. Defer to our internal SSL object */
5127
0
        return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
5128
0
    }
5129
0
}
5130
5131
long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
5132
0
{
5133
0
    return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
5134
0
}
5135
5136
int ossl_quic_renegotiate_check(SSL *ssl, int initok)
5137
0
{
5138
    /* We never do renegotiation. */
5139
0
    return 0;
5140
0
}
5141
5142
const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)
5143
0
{
5144
0
    const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);
5145
5146
0
    if (ciph == NULL)
5147
0
        return NULL;
5148
0
    if ((ciph->algorithm2 & SSL_QUIC) == 0)
5149
0
        return NULL;
5150
5151
0
    return ciph;
5152
0
}
5153
5154
/*
5155
 * These functions define the TLSv1.2 (and below) ciphers that are supported by
5156
 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
5157
 */
5158
5159
int ossl_quic_num_ciphers(void)
5160
42.0k
{
5161
42.0k
    return 0;
5162
42.0k
}
5163
5164
const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
5165
0
{
5166
0
    return NULL;
5167
0
}
5168
5169
/*
5170
 * SSL_get_shutdown()
5171
 * ------------------
5172
 */
5173
int ossl_quic_get_shutdown(const SSL *s)
5174
0
{
5175
0
    QCTX ctx;
5176
0
    int shut = 0;
5177
5178
0
    if (!expect_quic_conn_only(s, &ctx))
5179
0
        return 0;
5180
5181
0
    if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
5182
0
        shut |= SSL_SENT_SHUTDOWN;
5183
0
        if (!ossl_quic_channel_is_closing(ctx.qc->ch))
5184
0
            shut |= SSL_RECEIVED_SHUTDOWN;
5185
0
    }
5186
5187
0
    return shut;
5188
0
}
5189
5190
/*
5191
 * QUIC Polling Support APIs
5192
 * =========================
5193
 */
5194
5195
/* Do we have the R (read) condition? */
5196
QUIC_NEEDS_LOCK
5197
static int test_poll_event_r(QUIC_XSO *xso)
5198
0
{
5199
0
    int fin = 0;
5200
0
    size_t avail = 0;
5201
5202
    /*
5203
     * If a stream has had the fin bit set on the last packet
5204
     * received, then we need to return a 1 here to raise
5205
     * SSL_POLL_EVENT_R, so that the stream can have its completion
5206
     * detected and closed gracefully by an application.
5207
     * However, if the client reads the data via SSL_read[_ex], that api
5208
     * provides no stream status, and as a result the stream state moves to
5209
     * QUIC_RSTREAM_STATE_DATA_READ, and the receive buffer is freed, which
5210
     * stored the fin state, so its not directly know-able here.  Instead
5211
     * check for the stream state being QUIC_RSTREAM_STATE_DATA_READ, which
5212
     * is only set if the last stream frame received had the fin bit set, and
5213
     * the client read the data.  This catches our poll/read/poll case
5214
     */
5215
0
    if (xso->stream->recv_state == QUIC_RSTREAM_STATE_DATA_READ)
5216
0
        return 1;
5217
5218
0
    return ossl_quic_stream_has_recv_buffer(xso->stream)
5219
0
        && ossl_quic_rstream_available(xso->stream->rstream, &avail, &fin)
5220
0
        && (avail > 0 || (fin && !xso->retired_fin));
5221
0
}
5222
5223
/* Do we have the ER (exception: read) condition? */
5224
QUIC_NEEDS_LOCK
5225
static int test_poll_event_er(QUIC_XSO *xso)
5226
0
{
5227
0
    return ossl_quic_stream_has_recv(xso->stream)
5228
0
        && ossl_quic_stream_recv_is_reset(xso->stream)
5229
0
        && !xso->retired_fin;
5230
0
}
5231
5232
/* Do we have the W (write) condition? */
5233
QUIC_NEEDS_LOCK
5234
static int test_poll_event_w(QUIC_XSO *xso)
5235
0
{
5236
0
    return !xso->conn->shutting_down
5237
0
        && ossl_quic_stream_has_send_buffer(xso->stream)
5238
0
        && ossl_quic_sstream_get_buffer_avail(xso->stream->sstream)
5239
0
        && !ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)
5240
0
        && ossl_quic_txfc_get_cwm(&xso->stream->txfc)
5241
0
        > ossl_quic_sstream_get_cur_size(xso->stream->sstream)
5242
0
        && quic_mutation_allowed(xso->conn, /*req_active=*/1);
5243
0
}
5244
5245
/* Do we have the EW (exception: write) condition? */
5246
QUIC_NEEDS_LOCK
5247
static int test_poll_event_ew(QUIC_XSO *xso)
5248
0
{
5249
0
    return ossl_quic_stream_has_send(xso->stream)
5250
0
        && xso->stream->peer_stop_sending
5251
0
        && !xso->requested_reset
5252
0
        && !xso->conn->shutting_down;
5253
0
}
5254
5255
/* Do we have the EC (exception: connection) condition? */
5256
QUIC_NEEDS_LOCK
5257
static int test_poll_event_ec(QUIC_CONNECTION *qc)
5258
0
{
5259
0
    return ossl_quic_channel_is_term_any(qc->ch);
5260
0
}
5261
5262
/* Do we have the ECD (exception: connection drained) condition? */
5263
QUIC_NEEDS_LOCK
5264
static int test_poll_event_ecd(QUIC_CONNECTION *qc)
5265
0
{
5266
0
    return ossl_quic_channel_is_terminated(qc->ch);
5267
0
}
5268
5269
/* Do we have the IS (incoming: stream) condition? */
5270
QUIC_NEEDS_LOCK
5271
static int test_poll_event_is(QUIC_CONNECTION *qc, int is_uni)
5272
0
{
5273
0
    return ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(qc->ch),
5274
0
        is_uni);
5275
0
}
5276
5277
/* Do we have the OS (outgoing: stream) condition? */
5278
QUIC_NEEDS_LOCK
5279
static int test_poll_event_os(QUIC_CONNECTION *qc, int is_uni)
5280
0
{
5281
    /* Is it currently possible for us to make an outgoing stream? */
5282
0
    return quic_mutation_allowed(qc, /*req_active=*/1)
5283
0
        && ossl_quic_channel_get_local_stream_count_avail(qc->ch, is_uni) > 0;
5284
0
}
5285
5286
/* Do we have the EL (exception: listener) condition? */
5287
QUIC_NEEDS_LOCK
5288
static int test_poll_event_el(QUIC_LISTENER *ql)
5289
0
{
5290
0
    return !ossl_quic_port_is_running(ql->port);
5291
0
}
5292
5293
/* Do we have the IC (incoming: connection) condition? */
5294
QUIC_NEEDS_LOCK
5295
static int test_poll_event_ic(QUIC_LISTENER *ql)
5296
0
{
5297
0
    return ossl_quic_port_get_num_incoming_channels(ql->port) > 0;
5298
0
}
5299
5300
QUIC_TAKES_LOCK
5301
int ossl_quic_conn_poll_events(SSL *ssl, uint64_t events, int do_tick,
5302
    uint64_t *p_revents)
5303
0
{
5304
0
    QCTX ctx;
5305
0
    uint64_t revents = 0;
5306
5307
0
    if (!expect_quic_csl(ssl, &ctx))
5308
0
        return 0;
5309
5310
0
    qctx_lock(&ctx);
5311
5312
0
    if (ctx.qc != NULL && !ctx.qc->started) {
5313
        /* We can only try to write on non-started connection. */
5314
0
        if ((events & SSL_POLL_EVENT_W) != 0)
5315
0
            revents |= SSL_POLL_EVENT_W;
5316
0
        goto end;
5317
0
    }
5318
5319
0
    if (do_tick)
5320
0
        ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx.obj), 0);
5321
5322
0
    if (ctx.xso != NULL) {
5323
        /* SSL object has a stream component. */
5324
5325
0
        if ((events & SSL_POLL_EVENT_R) != 0
5326
0
            && test_poll_event_r(ctx.xso))
5327
0
            revents |= SSL_POLL_EVENT_R;
5328
5329
0
        if ((events & SSL_POLL_EVENT_ER) != 0
5330
0
            && test_poll_event_er(ctx.xso))
5331
0
            revents |= SSL_POLL_EVENT_ER;
5332
5333
0
        if ((events & SSL_POLL_EVENT_W) != 0
5334
0
            && test_poll_event_w(ctx.xso))
5335
0
            revents |= SSL_POLL_EVENT_W;
5336
5337
0
        if ((events & SSL_POLL_EVENT_EW) != 0
5338
0
            && test_poll_event_ew(ctx.xso))
5339
0
            revents |= SSL_POLL_EVENT_EW;
5340
0
    }
5341
5342
0
    if (ctx.qc != NULL && !ctx.is_stream) {
5343
0
        if ((events & SSL_POLL_EVENT_EC) != 0
5344
0
            && test_poll_event_ec(ctx.qc))
5345
0
            revents |= SSL_POLL_EVENT_EC;
5346
5347
0
        if ((events & SSL_POLL_EVENT_ECD) != 0
5348
0
            && test_poll_event_ecd(ctx.qc))
5349
0
            revents |= SSL_POLL_EVENT_ECD;
5350
5351
0
        if ((events & SSL_POLL_EVENT_ISB) != 0
5352
0
            && test_poll_event_is(ctx.qc, /*uni=*/0))
5353
0
            revents |= SSL_POLL_EVENT_ISB;
5354
5355
0
        if ((events & SSL_POLL_EVENT_ISU) != 0
5356
0
            && test_poll_event_is(ctx.qc, /*uni=*/1))
5357
0
            revents |= SSL_POLL_EVENT_ISU;
5358
5359
0
        if ((events & SSL_POLL_EVENT_OSB) != 0
5360
0
            && test_poll_event_os(ctx.qc, /*uni=*/0))
5361
0
            revents |= SSL_POLL_EVENT_OSB;
5362
5363
0
        if ((events & SSL_POLL_EVENT_OSU) != 0
5364
0
            && test_poll_event_os(ctx.qc, /*uni=*/1))
5365
0
            revents |= SSL_POLL_EVENT_OSU;
5366
0
    }
5367
5368
0
    if (ctx.is_listener) {
5369
0
        if ((events & SSL_POLL_EVENT_EL) != 0
5370
0
            && test_poll_event_el(ctx.ql))
5371
0
            revents |= SSL_POLL_EVENT_EL;
5372
5373
0
        if ((events & SSL_POLL_EVENT_IC) != 0
5374
0
            && test_poll_event_ic(ctx.ql))
5375
0
            revents |= SSL_POLL_EVENT_IC;
5376
0
    }
5377
5378
0
end:
5379
0
    qctx_unlock(&ctx);
5380
0
    *p_revents = revents;
5381
0
    return 1;
5382
0
}
5383
5384
QUIC_TAKES_LOCK
5385
int ossl_quic_get_notifier_fd(SSL *ssl)
5386
0
{
5387
0
    QCTX ctx;
5388
0
    QUIC_REACTOR *rtor;
5389
0
    RIO_NOTIFIER *nfy;
5390
0
    int nfd = -1;
5391
5392
0
    if (!expect_quic_any(ssl, &ctx))
5393
0
        return -1;
5394
5395
0
    qctx_lock(&ctx);
5396
0
    rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5397
0
    nfy = ossl_quic_reactor_get0_notifier(rtor);
5398
0
    if (nfy == NULL)
5399
0
        goto end;
5400
0
    nfd = ossl_rio_notifier_as_fd(nfy);
5401
5402
0
end:
5403
0
    qctx_unlock(&ctx);
5404
0
    return nfd;
5405
0
}
5406
5407
QUIC_TAKES_LOCK
5408
void ossl_quic_enter_blocking_section(SSL *ssl, QUIC_REACTOR_WAIT_CTX *wctx)
5409
0
{
5410
0
    QCTX ctx;
5411
0
    QUIC_REACTOR *rtor;
5412
5413
0
    if (!expect_quic_any(ssl, &ctx))
5414
0
        return;
5415
5416
0
    qctx_lock(&ctx);
5417
0
    rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5418
0
    ossl_quic_reactor_wait_ctx_enter(wctx, rtor);
5419
0
    qctx_unlock(&ctx);
5420
0
}
5421
5422
QUIC_TAKES_LOCK
5423
void ossl_quic_leave_blocking_section(SSL *ssl, QUIC_REACTOR_WAIT_CTX *wctx)
5424
0
{
5425
0
    QCTX ctx;
5426
0
    QUIC_REACTOR *rtor;
5427
5428
0
    if (!expect_quic_any(ssl, &ctx))
5429
0
        return;
5430
5431
0
    qctx_lock(&ctx);
5432
0
    rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5433
0
    ossl_quic_reactor_wait_ctx_leave(wctx, rtor);
5434
0
    qctx_unlock(&ctx);
5435
0
}
5436
5437
/*
5438
 * Internal Testing APIs
5439
 * =====================
5440
 */
5441
5442
QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
5443
0
{
5444
0
    QCTX ctx;
5445
5446
0
    if (!expect_quic_conn_only(s, &ctx))
5447
0
        return NULL;
5448
5449
0
    return ctx.qc->ch;
5450
0
}
5451
5452
QUIC_PORT *ossl_quic_listener_get_port(SSL *s)
5453
0
{
5454
0
    QCTX ctx;
5455
5456
    /*
5457
     * expect listerner only
5458
     */
5459
0
    if (!expect_quic_listener(s, &ctx))
5460
0
        return NULL;
5461
5462
0
    return ctx.ql->port;
5463
0
}
5464
5465
int ossl_quic_set_diag_title(SSL_CTX *ctx, const char *title)
5466
0
{
5467
0
#ifndef OPENSSL_NO_QLOG
5468
0
    OPENSSL_free(ctx->qlog_title);
5469
0
    ctx->qlog_title = NULL;
5470
5471
0
    if (title == NULL)
5472
0
        return 1;
5473
5474
0
    if ((ctx->qlog_title = OPENSSL_strdup(title)) == NULL)
5475
0
        return 0;
5476
0
#endif
5477
5478
0
    return 1;
5479
0
}