Coverage Report

Created: 2026-04-12 07:08

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