Coverage Report

Created: 2026-04-09 06:50

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