Coverage Report

Created: 2025-12-08 06:22

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