Coverage Report

Created: 2026-07-12 07:21

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