Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/ssl/quic/quic_impl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2022-2024 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/quic_tls.h"
16
#include "internal/quic_rx_depack.h"
17
#include "internal/quic_error.h"
18
#include "internal/time.h"
19
20
typedef struct qctx_st QCTX;
21
22
static void aon_write_finish(QUIC_XSO *xso);
23
static int create_channel(QUIC_CONNECTION *qc);
24
static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
25
static int qc_try_create_default_xso_for_write(QCTX *ctx);
26
static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek);
27
static void quic_lock(QUIC_CONNECTION *qc);
28
static void quic_unlock(QUIC_CONNECTION *qc);
29
static void quic_lock_for_io(QCTX *ctx);
30
static int quic_do_handshake(QCTX *ctx);
31
static void qc_update_reject_policy(QUIC_CONNECTION *qc);
32
static void qc_touch_default_xso(QUIC_CONNECTION *qc);
33
static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
34
static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
35
                                        int touch, QUIC_XSO **old_xso);
36
static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
37
static int quic_validate_for_write(QUIC_XSO *xso, int *err);
38
static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
39
static int qc_blocking_mode(const QUIC_CONNECTION *qc);
40
static int xso_blocking_mode(const QUIC_XSO *xso);
41
42
/*
43
 * QUIC Front-End I/O API: Common Utilities
44
 * ========================================
45
 */
46
47
/*
48
 * Block until a predicate is met.
49
 *
50
 * Precondition: Must have a channel.
51
 * Precondition: Must hold channel lock (unchecked).
52
 */
53
QUIC_NEEDS_LOCK
54
static int block_until_pred(QUIC_CONNECTION *qc,
55
                            int (*pred)(void *arg), void *pred_arg,
56
                            uint32_t flags)
57
0
{
58
0
    QUIC_REACTOR *rtor;
59
60
0
    assert(qc->ch != NULL);
61
62
    /*
63
     * Any attempt to block auto-disables tick inhibition as otherwise we will
64
     * hang around forever.
65
     */
66
0
    ossl_quic_channel_set_inhibit_tick(qc->ch, 0);
67
68
0
    rtor = ossl_quic_channel_get_reactor(qc->ch);
69
0
    return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,
70
0
                                              qc->mutex);
71
0
}
72
73
static OSSL_TIME get_time(QUIC_CONNECTION *qc)
74
100M
{
75
100M
    if (qc->override_now_cb != NULL)
76
100M
        return qc->override_now_cb(qc->override_now_cb_arg);
77
21.1k
    else
78
21.1k
        return ossl_time_now();
79
100M
}
80
81
static OSSL_TIME get_time_cb(void *arg)
82
72.2M
{
83
72.2M
    QUIC_CONNECTION *qc = arg;
84
85
72.2M
    return get_time(qc);
86
72.2M
}
87
88
/*
89
 * QCTX is a utility structure which provides information we commonly wish to
90
 * unwrap upon an API call being dispatched to us, namely:
91
 *
92
 *   - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
93
 *     was passed);
94
 *   - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
95
 *     a QCSO with a default stream was passed);
96
 *   - whether a QSSO was passed (xso == NULL must not be used to determine this
97
 *     because it may be non-NULL when a QCSO is passed if that QCSO has a
98
 *     default stream);
99
 *   - whether we are in "I/O context", meaning that non-normal errors can
100
 *     be reported via SSL_get_error() as well as via ERR. Functions such as
101
 *     SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
102
 *     functions which are allowed to change the value returned by
103
 *     SSL_get_error. However, other functions (including functions which call
104
 *     SSL_do_handshake() implicitly) are not allowed to change the return value
105
 *     of SSL_get_error.
106
 */
107
struct qctx_st {
108
    QUIC_CONNECTION *qc;
109
    QUIC_XSO        *xso;
110
    int             is_stream, in_io;
111
};
112
113
QUIC_NEEDS_LOCK
114
static void quic_set_last_error(QCTX *ctx, int last_error)
115
54.2M
{
116
54.2M
    if (!ctx->in_io)
117
2.85k
        return;
118
119
54.2M
    if (ctx->is_stream && ctx->xso != NULL)
120
6.26M
        ctx->xso->last_error = last_error;
121
47.9M
    else if (!ctx->is_stream && ctx->qc != NULL)
122
47.9M
        ctx->qc->last_error = last_error;
123
54.2M
}
124
125
/*
126
 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
127
 * rather than via ERR. Note that normal errors must always be raised while
128
 * holding a lock.
129
 */
130
QUIC_NEEDS_LOCK
131
static int quic_raise_normal_error(QCTX *ctx,
132
                                   int err)
133
27.0M
{
134
27.0M
    assert(ctx->in_io);
135
27.0M
    quic_set_last_error(ctx, err);
136
137
27.0M
    return 0;
138
27.0M
}
139
140
/*
141
 * Raise a 'non-normal' error, meaning any error that is not reported via
142
 * SSL_get_error() and must be reported via ERR.
143
 *
144
 * qc should be provided if available. In exceptional circumstances when qc is
145
 * not known NULL may be passed. This should generally only happen when an
146
 * expect_...() function defined below fails, which generally indicates a
147
 * dispatch error or caller error.
148
 *
149
 * ctx should be NULL if the connection lock is not held.
150
 */
151
static int quic_raise_non_normal_error(QCTX *ctx,
152
                                       const char *file,
153
                                       int line,
154
                                       const char *func,
155
                                       int reason,
156
                                       const char *fmt,
157
                                       ...)
158
20.2k
{
159
20.2k
    va_list args;
160
161
20.2k
    if (ctx != NULL) {
162
20.2k
        quic_set_last_error(ctx, SSL_ERROR_SSL);
163
164
20.2k
        if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
165
18.8k
            ossl_quic_channel_restore_err_state(ctx->qc->ch);
166
20.2k
    }
167
168
20.2k
    ERR_new();
169
20.2k
    ERR_set_debug(file, line, func);
170
171
20.2k
    va_start(args, fmt);
172
20.2k
    ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
173
20.2k
    va_end(args);
174
175
20.2k
    return 0;
176
20.2k
}
177
178
#define QUIC_RAISE_NORMAL_ERROR(ctx, err)                       \
179
27.0M
    quic_raise_normal_error((ctx), (err))
180
181
#define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg)           \
182
20.1k
    quic_raise_non_normal_error((ctx),                          \
183
20.1k
                                OPENSSL_FILE, OPENSSL_LINE,     \
184
20.1k
                                OPENSSL_FUNC,                   \
185
20.1k
                                (reason),                       \
186
20.1k
                                (msg))
187
188
/*
189
 * Given a QCSO or QSSO, initialises a QCTX, determining the contextually
190
 * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.
191
 *
192
 * After this returns 1, all fields of the passed QCTX are initialised.
193
 * Returns 0 on failure. This function is intended to be used to provide API
194
 * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.
195
 */
196
static int expect_quic(const SSL *s, QCTX *ctx)
197
82.4M
{
198
82.4M
    QUIC_CONNECTION *qc;
199
82.4M
    QUIC_XSO *xso;
200
201
82.4M
    ctx->qc         = NULL;
202
82.4M
    ctx->xso        = NULL;
203
82.4M
    ctx->is_stream  = 0;
204
205
82.4M
    if (s == NULL)
206
0
        return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
207
208
82.4M
    switch (s->type) {
209
76.2M
    case SSL_TYPE_QUIC_CONNECTION:
210
76.2M
        qc              = (QUIC_CONNECTION *)s;
211
76.2M
        ctx->qc         = qc;
212
76.2M
        ctx->xso        = qc->default_xso;
213
76.2M
        ctx->is_stream  = 0;
214
76.2M
        ctx->in_io      = 0;
215
76.2M
        return 1;
216
217
6.27M
    case SSL_TYPE_QUIC_XSO:
218
6.27M
        xso             = (QUIC_XSO *)s;
219
6.27M
        ctx->qc         = xso->conn;
220
6.27M
        ctx->xso        = xso;
221
6.27M
        ctx->is_stream  = 1;
222
6.27M
        ctx->in_io      = 0;
223
6.27M
        return 1;
224
225
0
    default:
226
0
        return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
227
82.4M
    }
228
82.4M
}
229
230
/*
231
 * Like expect_quic(), but requires a QUIC_XSO be contextually available. In
232
 * other words, requires that the passed QSO be a QSSO or a QCSO with a default
233
 * stream.
234
 *
235
 * remote_init determines if we expect the default XSO to be remotely created or
236
 * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
237
 *
238
 * Channel mutex is acquired and retained on success.
239
 */
240
QUIC_ACQUIRES_LOCK
241
static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
242
                                                    int in_io, QCTX *ctx)
243
19.8k
{
244
19.8k
    if (!expect_quic(s, ctx))
245
0
        return 0;
246
247
19.8k
    if (in_io)
248
19.8k
        quic_lock_for_io(ctx);
249
0
    else
250
0
        quic_lock(ctx->qc);
251
252
19.8k
    if (ctx->xso == NULL && remote_init >= 0) {
253
0
        if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) {
254
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
255
0
            goto err;
256
0
        }
257
258
        /* If we haven't finished the handshake, try to advance it. */
259
0
        if (quic_do_handshake(ctx) < 1)
260
            /* ossl_quic_do_handshake raised error here */
261
0
            goto err;
262
263
0
        if (remote_init == 0) {
264
0
            if (!qc_try_create_default_xso_for_write(ctx))
265
0
                goto err;
266
0
        } else {
267
0
            if (!qc_wait_for_default_xso_for_read(ctx, /*peek=*/0))
268
0
                goto err;
269
0
        }
270
271
0
        ctx->xso = ctx->qc->default_xso;
272
0
    }
273
274
19.8k
    if (ctx->xso == NULL) {
275
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
276
0
        goto err;
277
0
    }
278
279
19.8k
    return 1; /* coverity[missing_unlock]: lock held */
280
281
0
err:
282
0
    quic_unlock(ctx->qc);
283
0
    return 0;
284
19.8k
}
285
286
/*
287
 * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still
288
 * be non-NULL if the QCSO has a default stream.
289
 */
290
static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
291
29.6k
{
292
29.6k
    if (!expect_quic(s, ctx))
293
0
        return 0;
294
295
29.6k
    if (ctx->is_stream)
296
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);
297
298
29.6k
    return 1;
299
29.6k
}
300
301
/*
302
 * Ensures that the channel mutex is held for a method which touches channel
303
 * state.
304
 *
305
 * Precondition: Channel mutex is not held (unchecked)
306
 */
307
static void quic_lock(QUIC_CONNECTION *qc)
308
82.3M
{
309
82.3M
#if defined(OPENSSL_THREADS)
310
82.3M
    ossl_crypto_mutex_lock(qc->mutex);
311
82.3M
#endif
312
82.3M
}
313
314
static void quic_lock_for_io(QCTX *ctx)
315
27.1M
{
316
27.1M
    quic_lock(ctx->qc);
317
27.1M
    ctx->in_io = 1;
318
319
    /*
320
     * We are entering an I/O function so we must update the values returned by
321
     * SSL_get_error and SSL_want. Set no error. This will be overridden later
322
     * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR
323
     * occurs during the API call.
324
     */
325
27.1M
    quic_set_last_error(ctx, SSL_ERROR_NONE);
326
27.1M
}
327
328
/* Precondition: Channel mutex is held (unchecked) */
329
QUIC_NEEDS_LOCK
330
static void quic_unlock(QUIC_CONNECTION *qc)
331
82.3M
{
332
82.3M
#if defined(OPENSSL_THREADS)
333
82.3M
    ossl_crypto_mutex_unlock(qc->mutex);
334
82.3M
#endif
335
82.3M
}
336
337
/*
338
 * This predicate is the criterion which should determine API call rejection for
339
 * *most* mutating API calls, particularly stream-related operations for send
340
 * parts.
341
 *
342
 * A call is rejected (this function returns 0) if shutdown is in progress
343
 * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
344
 * req_active=1, the connection must be active (i.e., the IDLE state is also
345
 * rejected).
346
 */
347
static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
348
27.1M
{
349
27.1M
    if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
350
4.23k
        return 0;
351
352
27.1M
    if (req_active && !ossl_quic_channel_is_active(qc->ch))
353
0
        return 0;
354
355
27.1M
    return 1;
356
27.1M
}
357
358
/*
359
 * QUIC Front-End I/O API: Initialization
360
 * ======================================
361
 *
362
 *         SSL_new                  => ossl_quic_new
363
 *                                     ossl_quic_init
364
 *         SSL_reset                => ossl_quic_reset
365
 *         SSL_clear                => ossl_quic_clear
366
 *                                     ossl_quic_deinit
367
 *         SSL_free                 => ossl_quic_free
368
 *
369
 *         SSL_set_options          => ossl_quic_set_options
370
 *         SSL_get_options          => ossl_quic_get_options
371
 *         SSL_clear_options        => ossl_quic_clear_options
372
 *
373
 */
374
375
/* SSL_new */
376
SSL *ossl_quic_new(SSL_CTX *ctx)
377
21.1k
{
378
21.1k
    QUIC_CONNECTION *qc = NULL;
379
21.1k
    SSL *ssl_base = NULL;
380
21.1k
    SSL_CONNECTION *sc = NULL;
381
382
21.1k
    qc = OPENSSL_zalloc(sizeof(*qc));
383
21.1k
    if (qc == NULL) {
384
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
385
0
        return NULL;
386
0
    }
387
21.1k
#if defined(OPENSSL_THREADS)
388
21.1k
    if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) {
389
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
390
0
        goto err;
391
0
    }
392
21.1k
#endif
393
394
    /* Initialise the QUIC_CONNECTION's stub header. */
395
21.1k
    ssl_base = &qc->ssl;
396
21.1k
    if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
397
0
        ssl_base = NULL;
398
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
399
0
        goto err;
400
0
    }
401
402
21.1k
    qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
403
21.1k
    if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
404
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
405
0
        goto err;
406
0
    }
407
408
    /* override the user_ssl of the inner connection */
409
21.1k
    sc->s3.flags |= TLS1_FLAGS_QUIC;
410
411
    /* Restrict options derived from the SSL_CTX. */
412
21.1k
    sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
413
21.1k
    sc->pha_enabled = 0;
414
415
21.1k
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
416
21.1k
    qc->is_thread_assisted
417
21.1k
        = (ssl_base->method == OSSL_QUIC_client_thread_method());
418
21.1k
#endif
419
420
21.1k
    qc->as_server       = 0; /* TODO(QUIC SERVER): add server support */
421
21.1k
    qc->as_server_state = qc->as_server;
422
423
21.1k
    qc->default_stream_mode     = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
424
21.1k
    qc->default_ssl_mode        = qc->ssl.ctx->mode;
425
21.1k
    qc->default_ssl_options     = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
426
21.1k
    qc->desires_blocking        = 1;
427
21.1k
    qc->blocking                = 0;
428
21.1k
    qc->incoming_stream_policy  = SSL_INCOMING_STREAM_POLICY_AUTO;
429
21.1k
    qc->last_error              = SSL_ERROR_NONE;
430
431
21.1k
    if (!create_channel(qc))
432
0
        goto err;
433
434
21.1k
    ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);
435
21.1k
    ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
436
437
21.1k
    qc_update_reject_policy(qc);
438
439
    /*
440
     * We do not create the default XSO yet. The reason for this is that the
441
     * stream ID of the default XSO will depend on whether the stream is client
442
     * or server-initiated, which depends on who transmits first. Since we do
443
     * not know whether the application will be using a client-transmits-first
444
     * or server-transmits-first protocol, we defer default XSO creation until
445
     * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
446
     * we take that as a cue that the client is expecting a server-initiated
447
     * stream, and vice versa if SSL_write() is called first.
448
     */
449
21.1k
    return ssl_base;
450
451
0
err:
452
0
    if (ssl_base == NULL) {
453
0
#if defined(OPENSSL_THREADS)
454
0
        ossl_crypto_mutex_free(&qc->mutex);
455
0
#endif
456
0
        OPENSSL_free(qc);
457
0
    } else {
458
0
        SSL_free(ssl_base);
459
0
    }
460
0
    return NULL;
461
21.1k
}
462
463
/* SSL_free */
464
QUIC_TAKES_LOCK
465
void ossl_quic_free(SSL *s)
466
26.2k
{
467
26.2k
    QCTX ctx;
468
26.2k
    int is_default;
469
470
    /* We should never be called on anything but a QSO. */
471
26.2k
    if (!expect_quic(s, &ctx))
472
0
        return;
473
474
26.2k
    quic_lock(ctx.qc);
475
476
26.2k
    if (ctx.is_stream) {
477
        /*
478
         * When a QSSO is freed, the XSO is freed immediately, because the XSO
479
         * itself only contains API personality layer data. However the
480
         * underlying QUIC_STREAM is not freed immediately but is instead marked
481
         * as deleted for later collection.
482
         */
483
484
5.02k
        assert(ctx.qc->num_xso > 0);
485
5.02k
        --ctx.qc->num_xso;
486
487
        /* If a stream's send part has not been finished, auto-reset it. */
488
5.02k
        if ((   ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
489
5.02k
             || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
490
5.02k
            && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
491
4.41k
            ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
492
4.41k
                                                        ctx.xso->stream, 0);
493
494
        /* Do STOP_SENDING for the receive part, if applicable. */
495
5.02k
        if (   ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
496
5.02k
            || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
497
4.63k
            ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
498
4.63k
                                                        ctx.xso->stream, 0);
499
500
        /* Update stream state. */
501
5.02k
        ctx.xso->stream->deleted = 1;
502
5.02k
        ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
503
5.02k
                                          ctx.xso->stream);
504
505
5.02k
        is_default = (ctx.xso == ctx.qc->default_xso);
506
5.02k
        quic_unlock(ctx.qc);
507
508
        /*
509
         * Unref the connection in most cases; the XSO has a ref to the QC and
510
         * not vice versa. But for a default XSO, to avoid circular references,
511
         * the QC refs the XSO but the XSO does not ref the QC. If we are the
512
         * default XSO, we only get here when the QC is being torn down anyway,
513
         * so don't call SSL_free(qc) as we are already in it.
514
         */
515
5.02k
        if (!is_default)
516
2.08k
            SSL_free(&ctx.qc->ssl);
517
518
        /* Note: SSL_free calls OPENSSL_free(xso) for us */
519
5.02k
        return;
520
5.02k
    }
521
522
    /*
523
     * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
524
     * stage, but is freed during the channel free when the whole QSM is freed.
525
     */
526
21.1k
    if (ctx.qc->default_xso != NULL) {
527
2.94k
        QUIC_XSO *xso = ctx.qc->default_xso;
528
529
2.94k
        quic_unlock(ctx.qc);
530
2.94k
        SSL_free(&xso->ssl);
531
2.94k
        quic_lock(ctx.qc);
532
2.94k
        ctx.qc->default_xso = NULL;
533
2.94k
    }
534
535
    /* Ensure we have no remaining XSOs. */
536
21.1k
    assert(ctx.qc->num_xso == 0);
537
538
21.1k
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
539
21.1k
    if (ctx.qc->is_thread_assisted && ctx.qc->started) {
540
0
        ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
541
0
        ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
542
0
    }
543
21.1k
#endif
544
545
21.1k
    SSL_free(ctx.qc->tls);
546
547
21.1k
    ossl_quic_channel_free(ctx.qc->ch);
548
549
21.1k
    BIO_free_all(ctx.qc->net_rbio);
550
21.1k
    BIO_free_all(ctx.qc->net_wbio);
551
552
21.1k
    quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */
553
21.1k
#if defined(OPENSSL_THREADS)
554
21.1k
    ossl_crypto_mutex_free(&ctx.qc->mutex);
555
21.1k
#endif
556
557
    /*
558
     * Note: SSL_free (that called this function) calls OPENSSL_free(ctx.qc) for
559
     * us
560
     */
561
21.1k
}
562
563
/* SSL method init */
564
int ossl_quic_init(SSL *s)
565
0
{
566
    /* Same op as SSL_clear, forward the call. */
567
0
    return ossl_quic_clear(s);
568
0
}
569
570
/* SSL method deinit */
571
void ossl_quic_deinit(SSL *s)
572
0
{
573
    /* No-op. */
574
0
}
575
576
/* SSL_clear (ssl_reset method) */
577
int ossl_quic_reset(SSL *s)
578
0
{
579
0
    QCTX ctx;
580
581
0
    if (!expect_quic(s, &ctx))
582
0
        return 0;
583
584
0
    ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
585
0
    return 0;
586
0
}
587
588
/* ssl_clear method (unused) */
589
int ossl_quic_clear(SSL *s)
590
0
{
591
0
    QCTX ctx;
592
593
0
    if (!expect_quic(s, &ctx))
594
0
        return 0;
595
596
0
    ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
597
0
    return 0;
598
0
}
599
600
int ossl_quic_conn_set_override_now_cb(SSL *s,
601
                                       OSSL_TIME (*now_cb)(void *arg),
602
                                       void *now_cb_arg)
603
21.1k
{
604
21.1k
    QCTX ctx;
605
606
21.1k
    if (!expect_quic(s, &ctx))
607
0
        return 0;
608
609
21.1k
    quic_lock(ctx.qc);
610
611
21.1k
    ctx.qc->override_now_cb     = now_cb;
612
21.1k
    ctx.qc->override_now_cb_arg = now_cb_arg;
613
614
21.1k
    quic_unlock(ctx.qc);
615
21.1k
    return 1;
616
21.1k
}
617
618
void ossl_quic_conn_force_assist_thread_wake(SSL *s)
619
0
{
620
0
    QCTX ctx;
621
622
0
    if (!expect_quic(s, &ctx))
623
0
        return;
624
625
0
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
626
0
    if (ctx.qc->is_thread_assisted && ctx.qc->started)
627
0
        ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
628
0
#endif
629
0
}
630
631
QUIC_NEEDS_LOCK
632
static void qc_touch_default_xso(QUIC_CONNECTION *qc)
633
5.02k
{
634
5.02k
    qc->default_xso_created = 1;
635
5.02k
    qc_update_reject_policy(qc);
636
5.02k
}
637
638
/*
639
 * Changes default XSO. Allows caller to keep reference to the old default XSO
640
 * (if any). Reference to new XSO is transferred from caller.
641
 */
642
QUIC_NEEDS_LOCK
643
static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
644
                                        int touch,
645
                                        QUIC_XSO **old_xso)
646
2.94k
{
647
2.94k
    int refs;
648
649
2.94k
    *old_xso = NULL;
650
651
2.94k
    if (qc->default_xso != xso) {
652
2.94k
        *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
653
654
2.94k
        qc->default_xso = xso;
655
656
2.94k
        if (xso == NULL) {
657
            /*
658
             * Changing to not having a default XSO. XSO becomes standalone and
659
             * now has a ref to the QC.
660
             */
661
0
            if (!ossl_assert(SSL_up_ref(&qc->ssl)))
662
0
                return;
663
2.94k
        } else {
664
            /*
665
             * Changing from not having a default XSO to having one. The new XSO
666
             * will have had a reference to the QC we need to drop to avoid a
667
             * circular reference.
668
             *
669
             * Currently we never change directly from one default XSO to
670
             * another, though this function would also still be correct if this
671
             * weren't the case.
672
             */
673
2.94k
            assert(*old_xso == NULL);
674
675
2.94k
            CRYPTO_DOWN_REF(&qc->ssl.references, &refs);
676
2.94k
            assert(refs > 0);
677
2.94k
        }
678
2.94k
    }
679
680
2.94k
    if (touch)
681
0
        qc_touch_default_xso(qc);
682
2.94k
}
683
684
/*
685
 * Changes default XSO, releasing the reference to any previous default XSO.
686
 * Reference to new XSO is transferred from caller.
687
 */
688
QUIC_NEEDS_LOCK
689
static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
690
2.94k
{
691
2.94k
    QUIC_XSO *old_xso = NULL;
692
693
2.94k
    qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
694
695
2.94k
    if (old_xso != NULL)
696
0
        SSL_free(&old_xso->ssl);
697
2.94k
}
698
699
QUIC_NEEDS_LOCK
700
static void xso_update_options(QUIC_XSO *xso)
701
5.02k
{
702
5.02k
    int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
703
704
5.02k
    if (xso->stream->rstream != NULL)
705
4.94k
        ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
706
707
5.02k
    if (xso->stream->sstream != NULL)
708
4.45k
        ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
709
5.02k
}
710
711
/*
712
 * SSL_set_options
713
 * ---------------
714
 *
715
 * Setting options on a QCSO
716
 *   - configures the handshake-layer options;
717
 *   - configures the default data-plane options for new streams;
718
 *   - configures the data-plane options on the default XSO, if there is one.
719
 *
720
 * Setting options on a QSSO
721
 *   - configures data-plane options for that stream only.
722
 */
723
QUIC_TAKES_LOCK
724
static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
725
0
{
726
0
    QCTX ctx;
727
0
    uint64_t hs_mask_value, hs_or_value, ret;
728
729
0
    if (!expect_quic(ssl, &ctx))
730
0
        return 0;
731
732
0
    quic_lock(ctx.qc);
733
734
0
    if (!ctx.is_stream) {
735
        /*
736
         * If we were called on the connection, we apply any handshake option
737
         * changes.
738
         */
739
0
        hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
740
0
        hs_or_value   = (or_value   & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
741
742
0
        SSL_clear_options(ctx.qc->tls, hs_mask_value);
743
0
        SSL_set_options(ctx.qc->tls, hs_or_value);
744
745
        /* Update defaults for new streams. */
746
0
        ctx.qc->default_ssl_options
747
0
            = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
748
0
              & OSSL_QUIC_PERMITTED_OPTIONS;
749
0
    }
750
751
0
    if (ctx.xso != NULL) {
752
0
        ctx.xso->ssl_options
753
0
            = ((ctx.xso->ssl_options & ~mask_value) | or_value)
754
0
            & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
755
756
0
        xso_update_options(ctx.xso);
757
0
    }
758
759
0
    ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;
760
761
0
    quic_unlock(ctx.qc);
762
0
    return ret;
763
0
}
764
765
uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
766
0
{
767
0
    return quic_mask_or_options(ssl, 0, options);
768
0
}
769
770
/* SSL_clear_options */
771
uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
772
0
{
773
0
    return quic_mask_or_options(ssl, options, 0);
774
0
}
775
776
/* SSL_get_options */
777
uint64_t ossl_quic_get_options(const SSL *ssl)
778
0
{
779
0
    return quic_mask_or_options((SSL *)ssl, 0, 0);
780
0
}
781
782
/*
783
 * QUIC Front-End I/O API: Network BIO Configuration
784
 * =================================================
785
 *
786
 * Handling the different BIOs is difficult:
787
 *
788
 *   - It is more or less a requirement that we use non-blocking network I/O;
789
 *     we need to be able to have timeouts on recv() calls, and make best effort
790
 *     (non blocking) send() and recv() calls.
791
 *
792
 *     The only sensible way to do this is to configure the socket into
793
 *     non-blocking mode. We could try to do select() before calling send() or
794
 *     recv() to get a guarantee that the call will not block, but this will
795
 *     probably run into issues with buggy OSes which generate spurious socket
796
 *     readiness events. In any case, relying on this to work reliably does not
797
 *     seem sane.
798
 *
799
 *     Timeouts could be handled via setsockopt() socket timeout options, but
800
 *     this depends on OS support and adds another syscall to every network I/O
801
 *     operation. It also has obvious thread safety concerns if we want to move
802
 *     to concurrent use of a single socket at some later date.
803
 *
804
 *     Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
805
 *     be made non-blocking. However some OSes (e.g. Windows) do not support
806
 *     this, so we cannot rely on this.
807
 *
808
 *     As such, we need to configure any FD in non-blocking mode. This may
809
 *     confound users who pass a blocking socket to libssl. However, in practice
810
 *     it would be extremely strange for a user of QUIC to pass an FD to us,
811
 *     then also try and send receive traffic on the same socket(!). Thus the
812
 *     impact of this should be limited, and can be documented.
813
 *
814
 *   - We support both blocking and non-blocking operation in terms of the API
815
 *     presented to the user. One prospect is to set the blocking mode based on
816
 *     whether the socket passed to us was already in blocking mode. However,
817
 *     Windows has no API for determining if a socket is in blocking mode (!),
818
 *     therefore this cannot be done portably. Currently therefore we expose an
819
 *     explicit API call to set this, and default to blocking mode.
820
 *
821
 *   - We need to determine our initial destination UDP address. The "natural"
822
 *     way for a user to do this is to set the peer variable on a BIO_dgram.
823
 *     However, this has problems because BIO_dgram's peer variable is used for
824
 *     both transmission and reception. This means it can be constantly being
825
 *     changed to a malicious value (e.g. if some random unrelated entity on the
826
 *     network starts sending traffic to us) on every read call. This is not a
827
 *     direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
828
 *     calls only, which do not use this variable. However, we do need to let
829
 *     the user specify the peer in a 'normal' manner. The compromise here is
830
 *     that we grab the current peer value set at the time the write BIO is set
831
 *     and do not read the value again.
832
 *
833
 *   - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
834
 *     Currently we do this by only supporting non-blocking mode.
835
 *
836
 */
837
838
/*
839
 * Determines what initial destination UDP address we should use, if possible.
840
 * If this fails the client must set the destination address manually, or use a
841
 * BIO which does not need a destination address.
842
 */
843
static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
844
0
{
845
0
    if (BIO_dgram_detect_peer_addr(net_wbio, peer) <= 0)
846
0
        return 0;
847
848
0
    return 1;
849
0
}
850
851
static int qc_can_support_blocking_cached(QUIC_CONNECTION *qc)
852
18.2M
{
853
18.2M
    QUIC_REACTOR *rtor = ossl_quic_channel_get_reactor(qc->ch);
854
855
18.2M
    return ossl_quic_reactor_can_poll_r(rtor)
856
18.2M
        && ossl_quic_reactor_can_poll_w(rtor);
857
18.2M
}
858
859
static void qc_update_can_support_blocking(QUIC_CONNECTION *qc)
860
18.2M
{
861
18.2M
    ossl_quic_channel_update_poll_descriptors(qc->ch); /* best effort */
862
18.2M
}
863
864
static void qc_update_blocking_mode(QUIC_CONNECTION *qc)
865
18.2M
{
866
18.2M
    qc->blocking = qc->desires_blocking && qc_can_support_blocking_cached(qc);
867
18.2M
}
868
869
void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
870
21.1k
{
871
21.1k
    QCTX ctx;
872
873
21.1k
    if (!expect_quic(s, &ctx))
874
0
        return;
875
876
21.1k
    if (ctx.qc->net_rbio == net_rbio)
877
0
        return;
878
879
21.1k
    if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))
880
0
        return;
881
882
21.1k
    BIO_free_all(ctx.qc->net_rbio);
883
21.1k
    ctx.qc->net_rbio = net_rbio;
884
885
21.1k
    if (net_rbio != NULL)
886
21.1k
        BIO_set_nbio(net_rbio, 1); /* best effort autoconfig */
887
888
    /*
889
     * Determine if the current pair of read/write BIOs now set allows blocking
890
     * mode to be supported.
891
     */
892
21.1k
    qc_update_can_support_blocking(ctx.qc);
893
21.1k
    qc_update_blocking_mode(ctx.qc);
894
21.1k
}
895
896
void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
897
21.1k
{
898
21.1k
    QCTX ctx;
899
900
21.1k
    if (!expect_quic(s, &ctx))
901
0
        return;
902
903
21.1k
    if (ctx.qc->net_wbio == net_wbio)
904
0
        return;
905
906
21.1k
    if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))
907
0
        return;
908
909
21.1k
    BIO_free_all(ctx.qc->net_wbio);
910
21.1k
    ctx.qc->net_wbio = net_wbio;
911
912
21.1k
    if (net_wbio != NULL)
913
21.1k
        BIO_set_nbio(net_wbio, 1); /* best effort autoconfig */
914
915
    /*
916
     * Determine if the current pair of read/write BIOs now set allows blocking
917
     * mode to be supported.
918
     */
919
21.1k
    qc_update_can_support_blocking(ctx.qc);
920
21.1k
    qc_update_blocking_mode(ctx.qc);
921
21.1k
}
922
923
BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
924
42.3k
{
925
42.3k
    QCTX ctx;
926
927
42.3k
    if (!expect_quic(s, &ctx))
928
0
        return NULL;
929
930
42.3k
    return ctx.qc->net_rbio;
931
42.3k
}
932
933
BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
934
21.1k
{
935
21.1k
    QCTX ctx;
936
937
21.1k
    if (!expect_quic(s, &ctx))
938
0
        return NULL;
939
940
21.1k
    return ctx.qc->net_wbio;
941
21.1k
}
942
943
int ossl_quic_conn_get_blocking_mode(const SSL *s)
944
0
{
945
0
    QCTX ctx;
946
947
0
    if (!expect_quic(s, &ctx))
948
0
        return 0;
949
950
0
    if (ctx.is_stream)
951
0
        return xso_blocking_mode(ctx.xso);
952
953
0
    return qc_blocking_mode(ctx.qc);
954
0
}
955
956
QUIC_TAKES_LOCK
957
int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
958
0
{
959
0
    int ret = 0;
960
0
    QCTX ctx;
961
962
0
    if (!expect_quic(s, &ctx))
963
0
        return 0;
964
965
0
    quic_lock(ctx.qc);
966
967
    /* Sanity check - can we support the request given the current network BIO? */
968
0
    if (blocking) {
969
        /*
970
         * If called directly on a QCSO, update our information on network BIO
971
         * capabilities.
972
         */
973
0
        if (!ctx.is_stream)
974
0
            qc_update_can_support_blocking(ctx.qc);
975
976
        /* Cannot enable blocking mode if we do not have pollable FDs. */
977
0
        if (!qc_can_support_blocking_cached(ctx.qc)) {
978
0
            ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
979
0
            goto out;
980
0
        }
981
0
    }
982
983
0
    if (!ctx.is_stream)
984
        /*
985
         * If called directly on a QCSO, update default and connection-level
986
         * blocking modes.
987
         */
988
0
        ctx.qc->desires_blocking = (blocking != 0);
989
990
0
    if (ctx.xso != NULL) {
991
        /*
992
         * If called on a QSSO or a QCSO with a default XSO, update the blocking
993
         * mode.
994
         */
995
0
        ctx.xso->desires_blocking       = (blocking != 0);
996
0
        ctx.xso->desires_blocking_set   = 1;
997
0
    }
998
999
0
    ret = 1;
1000
0
out:
1001
0
    qc_update_blocking_mode(ctx.qc);
1002
0
    quic_unlock(ctx.qc);
1003
0
    return ret;
1004
0
}
1005
1006
int ossl_quic_conn_set_initial_peer_addr(SSL *s,
1007
                                         const BIO_ADDR *peer_addr)
1008
21.1k
{
1009
21.1k
    QCTX ctx;
1010
1011
21.1k
    if (!expect_quic(s, &ctx))
1012
0
        return 0;
1013
1014
21.1k
    if (ctx.qc->started)
1015
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
1016
21.1k
                                       NULL);
1017
1018
21.1k
    if (peer_addr == NULL) {
1019
0
        BIO_ADDR_clear(&ctx.qc->init_peer_addr);
1020
0
        return 1;
1021
0
    }
1022
1023
21.1k
    ctx.qc->init_peer_addr = *peer_addr;
1024
21.1k
    return 1;
1025
21.1k
}
1026
1027
/*
1028
 * QUIC Front-End I/O API: Asynchronous I/O Management
1029
 * ===================================================
1030
 *
1031
 *   (BIO/)SSL_handle_events        => ossl_quic_handle_events
1032
 *   (BIO/)SSL_get_event_timeout    => ossl_quic_get_event_timeout
1033
 *   (BIO/)SSL_get_poll_fd          => ossl_quic_get_poll_fd
1034
 *
1035
 */
1036
1037
/* Returns 1 if the connection is being used in blocking mode. */
1038
static int qc_blocking_mode(const QUIC_CONNECTION *qc)
1039
38.5M
{
1040
38.5M
    return qc->blocking;
1041
38.5M
}
1042
1043
static int xso_blocking_mode(const QUIC_XSO *xso)
1044
6.70M
{
1045
6.70M
    if (xso->desires_blocking_set)
1046
0
        return xso->desires_blocking && qc_can_support_blocking_cached(xso->conn);
1047
6.70M
    else
1048
        /* Only ever set if we can support blocking. */
1049
6.70M
        return xso->conn->blocking;
1050
6.70M
}
1051
1052
/* SSL_handle_events; performs QUIC I/O and timeout processing. */
1053
QUIC_TAKES_LOCK
1054
int ossl_quic_handle_events(SSL *s)
1055
0
{
1056
0
    QCTX ctx;
1057
1058
0
    if (!expect_quic(s, &ctx))
1059
0
        return 0;
1060
1061
0
    quic_lock(ctx.qc);
1062
0
    ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1063
0
    quic_unlock(ctx.qc);
1064
0
    return 1;
1065
0
}
1066
1067
/*
1068
 * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1069
 * should next have events handled by the application by calling
1070
 * SSL_handle_events(). tv is set to 0 if the object should have events handled
1071
 * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1072
 * the value of *tv is undefined.
1073
 */
1074
QUIC_TAKES_LOCK
1075
int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1076
28.0M
{
1077
28.0M
    QCTX ctx;
1078
28.0M
    OSSL_TIME deadline = ossl_time_infinite();
1079
1080
28.0M
    if (!expect_quic(s, &ctx))
1081
0
        return 0;
1082
1083
28.0M
    quic_lock(ctx.qc);
1084
1085
28.0M
    deadline
1086
28.0M
        = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));
1087
1088
28.0M
    if (ossl_time_is_infinite(deadline)) {
1089
5.48k
        *is_infinite = 1;
1090
1091
        /*
1092
         * Robustness against faulty applications that don't check *is_infinite;
1093
         * harmless long timeout.
1094
         */
1095
5.48k
        tv->tv_sec  = 1000000;
1096
5.48k
        tv->tv_usec = 0;
1097
1098
5.48k
        quic_unlock(ctx.qc);
1099
5.48k
        return 1;
1100
5.48k
    }
1101
1102
28.0M
    *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));
1103
28.0M
    *is_infinite = 0;
1104
28.0M
    quic_unlock(ctx.qc);
1105
28.0M
    return 1;
1106
28.0M
}
1107
1108
/* SSL_get_rpoll_descriptor */
1109
int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1110
0
{
1111
0
    QCTX ctx;
1112
1113
0
    if (!expect_quic(s, &ctx))
1114
0
        return 0;
1115
1116
0
    if (desc == NULL || ctx.qc->net_rbio == NULL)
1117
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1118
0
                                       NULL);
1119
1120
0
    return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);
1121
0
}
1122
1123
/* SSL_get_wpoll_descriptor */
1124
int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1125
0
{
1126
0
    QCTX ctx;
1127
1128
0
    if (!expect_quic(s, &ctx))
1129
0
        return 0;
1130
1131
0
    if (desc == NULL || ctx.qc->net_wbio == NULL)
1132
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1133
0
                                       NULL);
1134
1135
0
    return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);
1136
0
}
1137
1138
/* SSL_net_read_desired */
1139
QUIC_TAKES_LOCK
1140
int ossl_quic_get_net_read_desired(SSL *s)
1141
0
{
1142
0
    QCTX ctx;
1143
0
    int ret;
1144
1145
0
    if (!expect_quic(s, &ctx))
1146
0
        return 0;
1147
1148
0
    quic_lock(ctx.qc);
1149
0
    ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1150
0
    quic_unlock(ctx.qc);
1151
0
    return ret;
1152
0
}
1153
1154
/* SSL_net_write_desired */
1155
QUIC_TAKES_LOCK
1156
int ossl_quic_get_net_write_desired(SSL *s)
1157
0
{
1158
0
    int ret;
1159
0
    QCTX ctx;
1160
1161
0
    if (!expect_quic(s, &ctx))
1162
0
        return 0;
1163
1164
0
    quic_lock(ctx.qc);
1165
0
    ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));
1166
0
    quic_unlock(ctx.qc);
1167
0
    return ret;
1168
0
}
1169
1170
/*
1171
 * QUIC Front-End I/O API: Connection Lifecycle Operations
1172
 * =======================================================
1173
 *
1174
 *         SSL_do_handshake         => ossl_quic_do_handshake
1175
 *         SSL_set_connect_state    => ossl_quic_set_connect_state
1176
 *         SSL_set_accept_state     => ossl_quic_set_accept_state
1177
 *         SSL_shutdown             => ossl_quic_shutdown
1178
 *         SSL_ctrl                 => ossl_quic_ctrl
1179
 *   (BIO/)SSL_connect              => ossl_quic_connect
1180
 *   (BIO/)SSL_accept               => ossl_quic_accept
1181
 *
1182
 */
1183
1184
QUIC_NEEDS_LOCK
1185
static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1186
0
{
1187
0
    QUIC_STREAM_MAP *qsm;
1188
1189
0
    if (qc->shutting_down)
1190
0
        return;
1191
1192
0
    qsm = ossl_quic_channel_get_qsm(qc->ch);
1193
1194
0
    ossl_quic_stream_map_begin_shutdown_flush(qsm);
1195
0
    qc->shutting_down = 1;
1196
0
}
1197
1198
/* Returns 1 if all shutdown-flush streams have been done with. */
1199
QUIC_NEEDS_LOCK
1200
static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1201
0
{
1202
0
    QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1203
1204
0
    return qc->shutting_down
1205
0
        && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1206
0
}
1207
1208
/* SSL_shutdown */
1209
static int quic_shutdown_wait(void *arg)
1210
0
{
1211
0
    QUIC_CONNECTION *qc = arg;
1212
1213
0
    return ossl_quic_channel_is_terminated(qc->ch);
1214
0
}
1215
1216
/* Returns 1 if shutdown flush process has finished or is inapplicable. */
1217
static int quic_shutdown_flush_wait(void *arg)
1218
0
{
1219
0
    QUIC_CONNECTION *qc = arg;
1220
1221
0
    return ossl_quic_channel_is_term_any(qc->ch)
1222
0
        || qc_shutdown_flush_finished(qc);
1223
0
}
1224
1225
static int quic_shutdown_peer_wait(void *arg)
1226
0
{
1227
0
    QUIC_CONNECTION *qc = arg;
1228
0
    return ossl_quic_channel_is_term_any(qc->ch);
1229
0
}
1230
1231
QUIC_TAKES_LOCK
1232
int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1233
                            const SSL_SHUTDOWN_EX_ARGS *args,
1234
                            size_t args_len)
1235
0
{
1236
0
    int ret;
1237
0
    QCTX ctx;
1238
0
    int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1239
0
    int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);
1240
0
    int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);
1241
1242
0
    if (!expect_quic(s, &ctx))
1243
0
        return -1;
1244
1245
0
    if (ctx.is_stream) {
1246
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
1247
0
        return -1;
1248
0
    }
1249
1250
0
    quic_lock(ctx.qc);
1251
1252
0
    if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1253
0
        quic_unlock(ctx.qc);
1254
0
        return 1;
1255
0
    }
1256
1257
    /* Phase 1: Stream Flushing */
1258
0
    if (!wait_peer && stream_flush) {
1259
0
        qc_shutdown_flush_init(ctx.qc);
1260
1261
0
        if (!qc_shutdown_flush_finished(ctx.qc)) {
1262
0
            if (!no_block && qc_blocking_mode(ctx.qc)) {
1263
0
                ret = block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);
1264
0
                if (ret < 1) {
1265
0
                    ret = 0;
1266
0
                    goto err;
1267
0
                }
1268
0
            } else {
1269
0
                ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1270
0
            }
1271
0
        }
1272
1273
0
        if (!qc_shutdown_flush_finished(ctx.qc)) {
1274
0
            quic_unlock(ctx.qc);
1275
0
            return 0; /* ongoing */
1276
0
        }
1277
0
    }
1278
1279
    /* Phase 2: Connection Closure */
1280
0
    if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1281
0
        if (!no_block && qc_blocking_mode(ctx.qc)) {
1282
0
            ret = block_until_pred(ctx.qc, quic_shutdown_peer_wait, ctx.qc, 0);
1283
0
            if (ret < 1) {
1284
0
                ret = 0;
1285
0
                goto err;
1286
0
            }
1287
0
        } else {
1288
0
            ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1289
0
        }
1290
1291
0
        if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1292
0
            ret = 0; /* peer hasn't closed yet - still not done */
1293
0
            goto err;
1294
0
        }
1295
1296
        /*
1297
         * We are at least terminating - go through the normal process of
1298
         * waiting until we are in the TERMINATED state.
1299
         */
1300
0
    }
1301
1302
    /* Block mutation ops regardless of if we did stream flush. */
1303
0
    ctx.qc->shutting_down = 1;
1304
1305
    /*
1306
     * This call is a no-op if we are already terminating, so it doesn't
1307
     * affect the wait_peer case.
1308
     */
1309
0
    ossl_quic_channel_local_close(ctx.qc->ch,
1310
0
                                  args != NULL ? args->quic_error_code : 0,
1311
0
                                  args != NULL ? args->quic_reason : NULL);
1312
1313
0
    SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1314
1315
0
    if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1316
0
        quic_unlock(ctx.qc);
1317
0
        return 1;
1318
0
    }
1319
1320
    /* Phase 3: Terminating Wait Time */
1321
0
    if (!no_block && qc_blocking_mode(ctx.qc)
1322
0
        && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1323
0
        ret = block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);
1324
0
        if (ret < 1) {
1325
0
            ret = 0;
1326
0
            goto err;
1327
0
        }
1328
0
    } else {
1329
0
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
1330
0
    }
1331
1332
0
    ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1333
0
err:
1334
0
    quic_unlock(ctx.qc);
1335
0
    return ret;
1336
0
}
1337
1338
/* SSL_ctrl */
1339
long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1340
21.1k
{
1341
21.1k
    QCTX ctx;
1342
1343
21.1k
    if (!expect_quic(s, &ctx))
1344
0
        return 0;
1345
1346
21.1k
    switch (cmd) {
1347
0
    case SSL_CTRL_MODE:
1348
        /* If called on a QCSO, update the default mode. */
1349
0
        if (!ctx.is_stream)
1350
0
            ctx.qc->default_ssl_mode |= (uint32_t)larg;
1351
1352
        /*
1353
         * If we were called on a QSSO or have a default stream, we also update
1354
         * that.
1355
         */
1356
0
        if (ctx.xso != NULL) {
1357
            /* Cannot enable EPW while AON write in progress. */
1358
0
            if (ctx.xso->aon_write_in_progress)
1359
0
                larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1360
1361
0
            ctx.xso->ssl_mode |= (uint32_t)larg;
1362
0
            return ctx.xso->ssl_mode;
1363
0
        }
1364
1365
0
        return ctx.qc->default_ssl_mode;
1366
0
    case SSL_CTRL_CLEAR_MODE:
1367
0
        if (!ctx.is_stream)
1368
0
            ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1369
1370
0
        if (ctx.xso != NULL) {
1371
0
            ctx.xso->ssl_mode &= ~(uint32_t)larg;
1372
0
            return ctx.xso->ssl_mode;
1373
0
        }
1374
1375
0
        return ctx.qc->default_ssl_mode;
1376
1377
0
    case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1378
0
        ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1379
        /* This ctrl also needs to be passed to the internal SSL object */
1380
0
        return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1381
1382
0
    case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1383
0
        {
1384
0
            int is_infinite;
1385
1386
0
            if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1387
0
                return 0;
1388
1389
0
            return !is_infinite;
1390
0
        }
1391
0
    case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1392
        /* For legacy compatibility with DTLS calls. */
1393
0
        return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1394
1395
        /* Mask ctrls we shouldn't support for QUIC. */
1396
0
    case SSL_CTRL_GET_READ_AHEAD:
1397
0
    case SSL_CTRL_SET_READ_AHEAD:
1398
0
    case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1399
0
    case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
1400
0
    case SSL_CTRL_SET_MAX_PIPELINES:
1401
0
        return 0;
1402
1403
21.1k
    default:
1404
        /*
1405
         * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1406
         * implementation. Either SSL_ctrl will handle it itself by direct
1407
         * access into handshake layer state, or failing that, it will be passed
1408
         * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1409
         * supported by anything, the handshake layer's ctrl method will finally
1410
         * return 0.
1411
         */
1412
21.1k
        return ossl_ctrl_internal(&ctx.qc->ssl, cmd, larg, parg, /*no_quic=*/1);
1413
21.1k
    }
1414
21.1k
}
1415
1416
/* SSL_set_connect_state */
1417
void ossl_quic_set_connect_state(SSL *s)
1418
21.1k
{
1419
21.1k
    QCTX ctx;
1420
1421
21.1k
    if (!expect_quic(s, &ctx))
1422
0
        return;
1423
1424
    /* Cannot be changed after handshake started */
1425
21.1k
    if (ctx.qc->started || ctx.is_stream)
1426
0
        return;
1427
1428
21.1k
    ctx.qc->as_server_state = 0;
1429
21.1k
}
1430
1431
/* SSL_set_accept_state */
1432
void ossl_quic_set_accept_state(SSL *s)
1433
0
{
1434
0
    QCTX ctx;
1435
1436
0
    if (!expect_quic(s, &ctx))
1437
0
        return;
1438
1439
    /* Cannot be changed after handshake started */
1440
0
    if (ctx.qc->started || ctx.is_stream)
1441
0
        return;
1442
1443
0
    ctx.qc->as_server_state = 1;
1444
0
}
1445
1446
/* SSL_do_handshake */
1447
struct quic_handshake_wait_args {
1448
    QUIC_CONNECTION     *qc;
1449
};
1450
1451
static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)
1452
18.1M
{
1453
18.1M
    int want = SSL_want(qc->tls);
1454
1455
18.1M
    if (want == SSL_X509_LOOKUP
1456
18.1M
            || want == SSL_CLIENT_HELLO_CB
1457
18.1M
            || want == SSL_RETRY_VERIFY)
1458
0
        return 1;
1459
1460
18.1M
    return 0;
1461
18.1M
}
1462
1463
static int quic_handshake_wait(void *arg)
1464
0
{
1465
0
    struct quic_handshake_wait_args *args = arg;
1466
1467
0
    if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1468
0
        return -1;
1469
1470
0
    if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1471
0
        return 1;
1472
1473
0
    if (tls_wants_non_io_retry(args->qc))
1474
0
        return 1;
1475
1476
0
    return 0;
1477
0
}
1478
1479
static int configure_channel(QUIC_CONNECTION *qc)
1480
21.1k
{
1481
21.1k
    assert(qc->ch != NULL);
1482
1483
21.1k
    if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
1484
21.1k
        || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
1485
21.1k
        || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1486
0
        return 0;
1487
1488
21.1k
    return 1;
1489
21.1k
}
1490
1491
QUIC_NEEDS_LOCK
1492
static int create_channel(QUIC_CONNECTION *qc)
1493
11.3k
{
1494
11.3k
    QUIC_CHANNEL_ARGS args = {0};
1495
1496
11.3k
    args.libctx     = qc->ssl.ctx->libctx;
1497
11.3k
    args.propq      = qc->ssl.ctx->propq;
1498
11.3k
    args.is_server  = qc->as_server;
1499
11.3k
    args.tls        = qc->tls;
1500
11.3k
    args.mutex      = qc->mutex;
1501
11.3k
    args.now_cb     = get_time_cb;
1502
11.3k
    args.now_cb_arg = qc;
1503
1504
11.3k
    qc->ch = ossl_quic_channel_new(&args);
1505
11.3k
    if (qc->ch == NULL) {
1506
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1507
0
        return 0;
1508
0
    }
1509
1510
11.3k
    return 1;
1511
11.3k
}
1512
1513
/*
1514
 * Configures a channel with the information we have accumulated via calls made
1515
 * to us from the application prior to starting a handshake attempt.
1516
 */
1517
QUIC_NEEDS_LOCK
1518
static int ensure_channel_started(QCTX *ctx)
1519
18.1M
{
1520
18.1M
    QUIC_CONNECTION *qc = ctx->qc;
1521
1522
18.1M
    if (!qc->started) {
1523
21.1k
        if (!configure_channel(qc)) {
1524
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1525
0
                                        "failed to configure channel");
1526
0
            return 0;
1527
0
        }
1528
1529
21.1k
        if (!ossl_quic_channel_start(qc->ch)) {
1530
0
            ossl_quic_channel_restore_err_state(qc->ch);
1531
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1532
0
                                        "failed to start channel");
1533
0
            return 0;
1534
0
        }
1535
1536
21.1k
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1537
21.1k
        if (qc->is_thread_assisted)
1538
0
            if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch,
1539
0
                                                    qc->override_now_cb,
1540
0
                                                    qc->override_now_cb_arg)) {
1541
0
                QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1542
0
                                            "failed to start assist thread");
1543
0
                return 0;
1544
0
            }
1545
21.1k
#endif
1546
21.1k
    }
1547
1548
18.1M
    qc->started = 1;
1549
18.1M
    return 1;
1550
18.1M
}
1551
1552
QUIC_NEEDS_LOCK
1553
static int quic_do_handshake(QCTX *ctx)
1554
27.1M
{
1555
27.1M
    int ret;
1556
27.1M
    QUIC_CONNECTION *qc = ctx->qc;
1557
1558
27.1M
    if (ossl_quic_channel_is_handshake_complete(qc->ch))
1559
        /* Handshake already completed. */
1560
8.91M
        return 1;
1561
1562
18.1M
    if (!quic_mutation_allowed(qc, /*req_active=*/0))
1563
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1564
1565
18.1M
    if (qc->as_server != qc->as_server_state) {
1566
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1567
0
        return -1; /* Non-protocol error */
1568
0
    }
1569
1570
18.1M
    if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
1571
        /* Need read and write BIOs. */
1572
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1573
0
        return -1; /* Non-protocol error */
1574
0
    }
1575
1576
    /*
1577
     * We need to determine our addressing mode. There are basically two
1578
     * ways we can use L4 addresses:
1579
     *
1580
     *   - Addressed mode, in which our BIO_sendmmsg calls have destination
1581
     *     addresses attached to them which we expect the underlying network BIO
1582
     *     to handle;
1583
     *
1584
     *   - Unaddressed mode, in which the BIO provided to us on the
1585
     *     network side neither provides us with L4 addresses nor is capable of
1586
     *     honouring ones we provide. We don't know where the QUIC traffic we
1587
     *     send ends up exactly and trust the application to know what it is
1588
     *     doing.
1589
     *
1590
     * Addressed mode is preferred because it enables support for connection
1591
     * migration, multipath, etc. in the future. Addressed mode is automatically
1592
     * enabled if we are using e.g. BIO_s_datagram, with or without
1593
     * BIO_s_connect.
1594
     *
1595
     * If we are passed a BIO_s_dgram_pair (or some custom BIO) we may have to
1596
     * use unaddressed mode unless that BIO supports capability flags indicating
1597
     * it can provide and honour L4 addresses.
1598
     *
1599
     * Our strategy for determining address mode is simple: we probe the
1600
     * underlying network BIOs for their capabilities. If the network BIOs
1601
     * support what we need, we use addressed mode. Otherwise, we use
1602
     * unaddressed mode.
1603
     *
1604
     * If addressed mode is chosen, we require an initial peer address to be
1605
     * set. If this is not set, we fail. If unaddressed mode is used, we do not
1606
     * require this, as such an address is superfluous, though it can be set if
1607
     * desired.
1608
     */
1609
18.1M
    if (!qc->started && !qc->addressing_probe_done) {
1610
21.1k
        long rcaps = BIO_dgram_get_effective_caps(qc->net_rbio);
1611
21.1k
        long wcaps = BIO_dgram_get_effective_caps(qc->net_wbio);
1612
1613
21.1k
        qc->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0);
1614
21.1k
        qc->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0);
1615
21.1k
        qc->addressing_probe_done = 1;
1616
21.1k
    }
1617
1618
18.1M
    if (!qc->started && qc->addressed_mode_w
1619
18.1M
        && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1620
        /*
1621
         * We are trying to connect and are using addressed mode, which means we
1622
         * need an initial peer address; if we do not have a peer address yet,
1623
         * we should try to autodetect one.
1624
         *
1625
         * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
1626
         * may not be able to provide us with a peer address until they have
1627
         * finished their own processing. They may not be able to perform this
1628
         * processing until an application has finished configuring that BIO
1629
         * (e.g. with setter calls), which might happen after SSL_set_bio is
1630
         * called.
1631
         */
1632
0
        if (!csm_analyse_init_peer_addr(qc->net_wbio, &qc->init_peer_addr))
1633
            /* best effort */
1634
0
            BIO_ADDR_clear(&qc->init_peer_addr);
1635
0
        else
1636
0
            ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
1637
0
    }
1638
1639
18.1M
    if (!qc->started
1640
18.1M
        && qc->addressed_mode_w
1641
18.1M
        && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1642
        /*
1643
         * If we still don't have a peer address in addressed mode, we can't do
1644
         * anything.
1645
         */
1646
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1647
0
        return -1; /* Non-protocol error */
1648
0
    }
1649
1650
    /*
1651
     * Start connection process. Note we may come here multiple times in
1652
     * non-blocking mode, which is fine.
1653
     */
1654
18.1M
    if (!ensure_channel_started(ctx)) /* raises on failure */
1655
0
        return -1; /* Non-protocol error */
1656
1657
18.1M
    if (ossl_quic_channel_is_handshake_complete(qc->ch))
1658
        /* The handshake is now done. */
1659
0
        return 1;
1660
1661
18.1M
    if (!qc_blocking_mode(qc)) {
1662
        /* Try to advance the reactor. */
1663
18.1M
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1664
1665
18.1M
        if (ossl_quic_channel_is_handshake_complete(qc->ch))
1666
            /* The handshake is now done. */
1667
5.41k
            return 1;
1668
1669
18.1M
        if (ossl_quic_channel_is_term_any(qc->ch)) {
1670
14.6k
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1671
14.6k
            return 0;
1672
18.1M
        } else if (qc->desires_blocking) {
1673
            /*
1674
             * As a special case when doing a handshake when blocking mode is
1675
             * desired yet not available, see if the network BIOs have become
1676
             * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1677
             * which do late creation of socket FDs and therefore cannot expose
1678
             * a poll descriptor until after a network BIO is set on the QCSO.
1679
             */
1680
18.1M
            assert(!qc->blocking);
1681
18.1M
            qc_update_can_support_blocking(qc);
1682
18.1M
            qc_update_blocking_mode(qc);
1683
18.1M
        }
1684
18.1M
    }
1685
1686
    /*
1687
     * We are either in blocking mode or just entered it due to the code above.
1688
     */
1689
18.1M
    if (qc_blocking_mode(qc)) {
1690
        /* In blocking mode, wait for the handshake to complete. */
1691
0
        struct quic_handshake_wait_args args;
1692
1693
0
        args.qc     = qc;
1694
1695
0
        ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
1696
0
        if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1697
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1698
0
            return 0; /* Shutdown before completion */
1699
0
        } else if (ret <= 0) {
1700
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1701
0
            return -1; /* Non-protocol error */
1702
0
        }
1703
1704
0
        if (tls_wants_non_io_retry(qc)) {
1705
0
            QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1706
0
            return -1;
1707
0
        }
1708
1709
0
        assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1710
0
        return 1;
1711
0
    }
1712
1713
18.1M
    if (tls_wants_non_io_retry(qc)) {
1714
0
        QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1715
0
        return -1;
1716
0
    }
1717
1718
    /*
1719
     * Otherwise, indicate that the handshake isn't done yet.
1720
     * We can only get here in non-blocking mode.
1721
     */
1722
18.1M
    QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1723
18.1M
    return -1; /* Non-protocol error */
1724
18.1M
}
1725
1726
QUIC_TAKES_LOCK
1727
int ossl_quic_do_handshake(SSL *s)
1728
18.1M
{
1729
18.1M
    int ret;
1730
18.1M
    QCTX ctx;
1731
1732
18.1M
    if (!expect_quic(s, &ctx))
1733
0
        return 0;
1734
1735
18.1M
    quic_lock_for_io(&ctx);
1736
1737
18.1M
    ret = quic_do_handshake(&ctx);
1738
18.1M
    quic_unlock(ctx.qc);
1739
18.1M
    return ret;
1740
18.1M
}
1741
1742
/* SSL_connect */
1743
int ossl_quic_connect(SSL *s)
1744
0
{
1745
    /* Ensure we are in connect state (no-op if non-idle). */
1746
0
    ossl_quic_set_connect_state(s);
1747
1748
    /* Begin or continue the handshake */
1749
0
    return ossl_quic_do_handshake(s);
1750
0
}
1751
1752
/* SSL_accept */
1753
int ossl_quic_accept(SSL *s)
1754
0
{
1755
    /* Ensure we are in accept state (no-op if non-idle). */
1756
0
    ossl_quic_set_accept_state(s);
1757
1758
    /* Begin or continue the handshake */
1759
0
    return ossl_quic_do_handshake(s);
1760
0
}
1761
1762
/*
1763
 * QUIC Front-End I/O API: Stream Lifecycle Operations
1764
 * ===================================================
1765
 *
1766
 *         SSL_stream_new       => ossl_quic_conn_stream_new
1767
 *
1768
 */
1769
1770
/*
1771
 * Try to create the default XSO if it doesn't already exist. Returns 1 if the
1772
 * default XSO was created. Returns 0 if it was not (e.g. because it already
1773
 * exists). Note that this is NOT an error condition.
1774
 */
1775
QUIC_NEEDS_LOCK
1776
static int qc_try_create_default_xso_for_write(QCTX *ctx)
1777
0
{
1778
0
    uint64_t flags = 0;
1779
0
    QUIC_CONNECTION *qc = ctx->qc;
1780
1781
0
    if (qc->default_xso_created
1782
0
        || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1783
        /*
1784
         * We only do this once. If the user detaches a previously created
1785
         * default XSO we don't auto-create another one.
1786
         */
1787
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1788
1789
    /* Create a locally-initiated stream. */
1790
0
    if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1791
0
        flags |= SSL_STREAM_FLAG_UNI;
1792
1793
0
    qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
1794
0
                                                            /*needs_lock=*/0),
1795
0
                       /*touch=*/0);
1796
0
    if (qc->default_xso == NULL)
1797
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1798
1799
0
    qc_touch_default_xso(qc);
1800
0
    return 1;
1801
0
}
1802
1803
struct quic_wait_for_stream_args {
1804
    QUIC_CONNECTION *qc;
1805
    QUIC_STREAM     *qs;
1806
    QCTX            *ctx;
1807
    uint64_t        expect_id;
1808
};
1809
1810
QUIC_NEEDS_LOCK
1811
static int quic_wait_for_stream(void *arg)
1812
0
{
1813
0
    struct quic_wait_for_stream_args *args = arg;
1814
1815
0
    if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
1816
        /* If connection is torn down due to an error while blocking, stop. */
1817
0
        QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1818
0
        return -1;
1819
0
    }
1820
1821
0
    args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1822
0
                                              args->expect_id | QUIC_STREAM_DIR_BIDI);
1823
0
    if (args->qs == NULL)
1824
0
        args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
1825
0
                                                  args->expect_id | QUIC_STREAM_DIR_UNI);
1826
1827
0
    if (args->qs != NULL)
1828
0
        return 1; /* stream now exists */
1829
1830
0
    return 0; /* did not get a stream, keep trying */
1831
0
}
1832
1833
QUIC_NEEDS_LOCK
1834
static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek)
1835
2.20M
{
1836
    /* Called on a QCSO and we don't currently have a default stream. */
1837
2.20M
    uint64_t expect_id;
1838
2.20M
    QUIC_CONNECTION *qc = ctx->qc;
1839
2.20M
    QUIC_STREAM *qs;
1840
2.20M
    int res;
1841
2.20M
    struct quic_wait_for_stream_args wargs;
1842
2.20M
    OSSL_RTT_INFO rtt_info;
1843
1844
    /*
1845
     * If default stream functionality is disabled or we already detached
1846
     * one, don't make another default stream and just fail.
1847
     */
1848
2.20M
    if (qc->default_xso_created
1849
2.20M
        || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
1850
7
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
1851
1852
    /*
1853
     * The peer may have opened a stream since we last ticked. So tick and
1854
     * see if the stream with ordinal 0 (remote, bidi/uni based on stream
1855
     * mode) exists yet. QUIC stream IDs must be allocated in order, so the
1856
     * first stream created by a peer must have an ordinal of 0.
1857
     */
1858
2.20M
    expect_id = qc->as_server
1859
2.20M
        ? QUIC_STREAM_INITIATOR_CLIENT
1860
2.20M
        : QUIC_STREAM_INITIATOR_SERVER;
1861
1862
2.20M
    qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1863
2.20M
                                        expect_id | QUIC_STREAM_DIR_BIDI);
1864
2.20M
    if (qs == NULL)
1865
2.20M
        qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1866
2.20M
                                            expect_id | QUIC_STREAM_DIR_UNI);
1867
1868
2.20M
    if (qs == NULL) {
1869
2.20M
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);
1870
1871
2.20M
        qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
1872
2.20M
                                            expect_id);
1873
2.20M
    }
1874
1875
2.20M
    if (qs == NULL) {
1876
2.20M
        if (peek)
1877
0
            return 0;
1878
1879
2.20M
        if (!qc_blocking_mode(qc))
1880
            /* Non-blocking mode, so just bail immediately. */
1881
2.20M
            return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
1882
1883
        /* Block until we have a stream. */
1884
0
        wargs.qc        = qc;
1885
0
        wargs.qs        = NULL;
1886
0
        wargs.ctx       = ctx;
1887
0
        wargs.expect_id = expect_id;
1888
1889
0
        res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);
1890
0
        if (res == 0)
1891
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1892
0
        else if (res < 0 || wargs.qs == NULL)
1893
            /* quic_wait_for_stream raised error here */
1894
0
            return 0;
1895
1896
0
        qs = wargs.qs;
1897
0
    }
1898
1899
    /*
1900
     * We now have qs != NULL. Remove it from the incoming stream queue so that
1901
     * it isn't also returned by any future SSL_accept_stream calls.
1902
     */
1903
2.94k
    ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1904
2.94k
    ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc->ch),
1905
2.94k
                                                  qs, rtt_info.smoothed_rtt);
1906
1907
    /*
1908
     * Now make qs the default stream, creating the necessary XSO.
1909
     */
1910
2.94k
    qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
1911
2.94k
    if (qc->default_xso == NULL)
1912
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1913
1914
2.94k
    qc_touch_default_xso(qc); /* inhibits default XSO */
1915
2.94k
    return 1;
1916
2.94k
}
1917
1918
QUIC_NEEDS_LOCK
1919
static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
1920
5.02k
{
1921
5.02k
    QUIC_XSO *xso = NULL;
1922
1923
5.02k
    if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
1924
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
1925
0
        goto err;
1926
0
    }
1927
1928
5.02k
    if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) {
1929
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1930
0
        goto err;
1931
0
    }
1932
1933
    /* XSO refs QC */
1934
5.02k
    if (!SSL_up_ref(&qc->ssl)) {
1935
0
        QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);
1936
0
        goto err;
1937
0
    }
1938
1939
5.02k
    xso->conn       = qc;
1940
5.02k
    xso->ssl_mode   = qc->default_ssl_mode;
1941
5.02k
    xso->ssl_options
1942
5.02k
        = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1943
5.02k
    xso->last_error = SSL_ERROR_NONE;
1944
1945
5.02k
    xso->stream     = qs;
1946
1947
5.02k
    ++qc->num_xso;
1948
5.02k
    xso_update_options(xso);
1949
5.02k
    return xso;
1950
1951
0
err:
1952
0
    OPENSSL_free(xso);
1953
0
    return NULL;
1954
5.02k
}
1955
1956
struct quic_new_stream_wait_args {
1957
    QUIC_CONNECTION *qc;
1958
    int is_uni;
1959
};
1960
1961
static int quic_new_stream_wait(void *arg)
1962
0
{
1963
0
    struct quic_new_stream_wait_args *args = arg;
1964
0
    QUIC_CONNECTION *qc = args->qc;
1965
1966
0
    if (!quic_mutation_allowed(qc, /*req_active=*/1))
1967
0
        return -1;
1968
1969
0
    if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
1970
0
        return 1;
1971
1972
0
    return 0;
1973
0
}
1974
1975
/* locking depends on need_lock */
1976
static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
1977
4.82k
{
1978
4.82k
    int ret;
1979
4.82k
    QUIC_CONNECTION *qc = ctx->qc;
1980
4.82k
    QUIC_XSO *xso = NULL;
1981
4.82k
    QUIC_STREAM *qs = NULL;
1982
4.82k
    int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
1983
4.82k
    int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
1984
4.82k
    int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
1985
1986
4.82k
    if (need_lock)
1987
4.82k
        quic_lock(qc);
1988
1989
4.82k
    if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
1990
1.57k
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1991
1.57k
        goto err;
1992
1.57k
    }
1993
1994
3.24k
    if (!advance
1995
3.24k
        && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
1996
1.28k
        struct quic_new_stream_wait_args args;
1997
1998
        /*
1999
         * Stream count flow control currently doesn't permit this stream to be
2000
         * opened.
2001
         */
2002
1.28k
        if (no_blocking || !qc_blocking_mode(qc)) {
2003
1.28k
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
2004
1.28k
            goto err;
2005
1.28k
        }
2006
2007
0
        args.qc     = qc;
2008
0
        args.is_uni = is_uni;
2009
2010
        /* Blocking mode - wait until we can get a stream. */
2011
0
        ret = block_until_pred(ctx->qc, quic_new_stream_wait, &args, 0);
2012
0
        if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2013
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2014
0
            goto err; /* Shutdown before completion */
2015
0
        } else if (ret <= 0) {
2016
0
            QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2017
0
            goto err; /* Non-protocol error */
2018
0
        }
2019
0
    }
2020
2021
1.96k
    qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
2022
1.96k
    if (qs == NULL) {
2023
0
        QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2024
0
        goto err;
2025
0
    }
2026
2027
1.96k
    xso = create_xso_from_stream(qc, qs);
2028
1.96k
    if (xso == NULL)
2029
0
        goto err;
2030
2031
1.96k
    qc_touch_default_xso(qc); /* inhibits default XSO */
2032
1.96k
    if (need_lock)
2033
1.96k
        quic_unlock(qc);
2034
2035
1.96k
    return &xso->ssl;
2036
2037
2.85k
err:
2038
2.85k
    OPENSSL_free(xso);
2039
2.85k
    ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
2040
2.85k
    if (need_lock)
2041
2.85k
        quic_unlock(qc);
2042
2043
2.85k
    return NULL;
2044
2045
1.96k
}
2046
2047
QUIC_TAKES_LOCK
2048
SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
2049
4.82k
{
2050
4.82k
    QCTX ctx;
2051
2052
4.82k
    if (!expect_quic_conn_only(s, &ctx))
2053
0
        return NULL;
2054
2055
4.82k
    return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
2056
4.82k
}
2057
2058
/*
2059
 * QUIC Front-End I/O API: Steady-State Operations
2060
 * ===============================================
2061
 *
2062
 * Here we dispatch calls to the steady-state front-end I/O API functions; that
2063
 * is, the functions used during the established phase of a QUIC connection
2064
 * (e.g. SSL_read, SSL_write).
2065
 *
2066
 * Each function must handle both blocking and non-blocking modes. As discussed
2067
 * above, all QUIC I/O is implemented using non-blocking mode internally.
2068
 *
2069
 *         SSL_get_error        => partially implemented by ossl_quic_get_error
2070
 *         SSL_want             => ossl_quic_want
2071
 *   (BIO/)SSL_read             => ossl_quic_read
2072
 *   (BIO/)SSL_write            => ossl_quic_write
2073
 *         SSL_pending          => ossl_quic_pending
2074
 *         SSL_stream_conclude  => ossl_quic_conn_stream_conclude
2075
 *         SSL_key_update       => ossl_quic_key_update
2076
 */
2077
2078
/* SSL_get_error */
2079
int ossl_quic_get_error(const SSL *s, int i)
2080
27.1M
{
2081
27.1M
    QCTX ctx;
2082
27.1M
    int net_error, last_error;
2083
2084
27.1M
    if (!expect_quic(s, &ctx))
2085
0
        return 0;
2086
2087
27.1M
    quic_lock(ctx.qc);
2088
27.1M
    net_error = ossl_quic_channel_net_error(ctx.qc->ch);
2089
27.1M
    last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
2090
27.1M
    quic_unlock(ctx.qc);
2091
2092
27.1M
    if (net_error)
2093
0
        return SSL_ERROR_SYSCALL;
2094
2095
27.1M
    return last_error;
2096
27.1M
}
2097
2098
/* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
2099
static int error_to_want(int error)
2100
0
{
2101
0
    switch (error) {
2102
0
    case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */
2103
0
    case SSL_ERROR_WANT_ACCEPT:  /* never used - UDP is connectionless */
2104
0
    case SSL_ERROR_ZERO_RETURN:
2105
0
    default:
2106
0
        return SSL_NOTHING;
2107
2108
0
    case SSL_ERROR_WANT_READ:
2109
0
        return SSL_READING;
2110
2111
0
    case SSL_ERROR_WANT_WRITE:
2112
0
        return SSL_WRITING;
2113
2114
0
    case SSL_ERROR_WANT_RETRY_VERIFY:
2115
0
        return SSL_RETRY_VERIFY;
2116
2117
0
    case SSL_ERROR_WANT_CLIENT_HELLO_CB:
2118
0
        return SSL_CLIENT_HELLO_CB;
2119
2120
0
    case SSL_ERROR_WANT_X509_LOOKUP:
2121
0
        return SSL_X509_LOOKUP;
2122
0
    }
2123
0
}
2124
2125
/* SSL_want */
2126
int ossl_quic_want(const SSL *s)
2127
0
{
2128
0
    QCTX ctx;
2129
0
    int w;
2130
2131
0
    if (!expect_quic(s, &ctx))
2132
0
        return SSL_NOTHING;
2133
2134
0
    quic_lock(ctx.qc);
2135
2136
0
    w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);
2137
2138
0
    quic_unlock(ctx.qc);
2139
0
    return w;
2140
0
}
2141
2142
/*
2143
 * SSL_write
2144
 * ---------
2145
 *
2146
 * The set of functions below provide the implementation of the public SSL_write
2147
 * function. We must handle:
2148
 *
2149
 *   - both blocking and non-blocking operation at the application level,
2150
 *     depending on how we are configured;
2151
 *
2152
 *   - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
2153
 *
2154
 *   - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
2155
 *
2156
 */
2157
QUIC_NEEDS_LOCK
2158
static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)
2159
17.3k
{
2160
    /*
2161
     * We have appended at least one byte to the stream.
2162
     * Potentially mark stream as active, depending on FC.
2163
     */
2164
17.3k
    if (did_append)
2165
1.36k
        ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
2166
1.36k
                                          xso->stream);
2167
2168
    /*
2169
     * Try and send.
2170
     *
2171
     * TODO(QUIC FUTURE): It is probably inefficient to try and do this
2172
     * immediately, plus we should eventually consider Nagle's algorithm.
2173
     */
2174
17.3k
    if (do_tick)
2175
17.3k
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
2176
17.3k
}
2177
2178
struct quic_write_again_args {
2179
    QUIC_XSO            *xso;
2180
    const unsigned char *buf;
2181
    size_t              len;
2182
    size_t              total_written;
2183
    int                 err;
2184
};
2185
2186
/*
2187
 * Absolute maximum write buffer size, enforced to prevent a rogue peer from
2188
 * deliberately inducing DoS. This has been chosen based on the optimal buffer
2189
 * size for an RTT of 500ms and a bandwidth of 100 Mb/s.
2190
 */
2191
0
#define MAX_WRITE_BUF_SIZE      (6 * 1024 * 1024)
2192
2193
/*
2194
 * Ensure spare buffer space available (up until a limit, at least).
2195
 */
2196
QUIC_NEEDS_LOCK
2197
static int sstream_ensure_spare(QUIC_SSTREAM *sstream, uint64_t spare)
2198
19.6k
{
2199
19.6k
    size_t cur_sz = ossl_quic_sstream_get_buffer_size(sstream);
2200
19.6k
    size_t avail = ossl_quic_sstream_get_buffer_avail(sstream);
2201
19.6k
    size_t spare_ = (spare > SIZE_MAX) ? SIZE_MAX : (size_t)spare;
2202
19.6k
    size_t new_sz, growth;
2203
2204
19.6k
    if (spare_ <= avail || cur_sz == MAX_WRITE_BUF_SIZE)
2205
19.6k
        return 1;
2206
2207
0
    growth = spare_ - avail;
2208
0
    if (cur_sz + growth > MAX_WRITE_BUF_SIZE)
2209
0
        new_sz = MAX_WRITE_BUF_SIZE;
2210
0
    else
2211
0
        new_sz = cur_sz + growth;
2212
2213
0
    return ossl_quic_sstream_set_buffer_size(sstream, new_sz);
2214
19.6k
}
2215
2216
/*
2217
 * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded
2218
 * as needed according to flow control.
2219
 */
2220
QUIC_NEEDS_LOCK
2221
static int xso_sstream_append(QUIC_XSO *xso, const unsigned char *buf,
2222
                              size_t len, size_t *actual_written)
2223
19.6k
{
2224
19.6k
    QUIC_SSTREAM *sstream = xso->stream->sstream;
2225
19.6k
    uint64_t cur = ossl_quic_sstream_get_cur_size(sstream);
2226
19.6k
    uint64_t cwm = ossl_quic_txfc_get_cwm(&xso->stream->txfc);
2227
19.6k
    uint64_t permitted = (cwm >= cur ? cwm - cur : 0);
2228
2229
19.6k
    if (len > permitted)
2230
17.9k
        len = (size_t)permitted;
2231
2232
19.6k
    if (!sstream_ensure_spare(sstream, len))
2233
0
        return 0;
2234
2235
19.6k
    return ossl_quic_sstream_append(sstream, buf, len, actual_written);
2236
19.6k
}
2237
2238
QUIC_NEEDS_LOCK
2239
static int quic_write_again(void *arg)
2240
0
{
2241
0
    struct quic_write_again_args *args = arg;
2242
0
    size_t actual_written = 0;
2243
2244
0
    if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
2245
        /* If connection is torn down due to an error while blocking, stop. */
2246
0
        return -2;
2247
2248
0
    if (!quic_validate_for_write(args->xso, &args->err))
2249
        /*
2250
         * Stream may have become invalid for write due to connection events
2251
         * while we blocked.
2252
         */
2253
0
        return -2;
2254
2255
0
    args->err = ERR_R_INTERNAL_ERROR;
2256
0
    if (!xso_sstream_append(args->xso, args->buf, args->len, &actual_written))
2257
0
        return -2;
2258
2259
0
    quic_post_write(args->xso, actual_written > 0, 0);
2260
2261
0
    args->buf           += actual_written;
2262
0
    args->len           -= actual_written;
2263
0
    args->total_written += actual_written;
2264
2265
0
    if (args->len == 0)
2266
        /* Written everything, done. */
2267
0
        return 1;
2268
2269
    /* Not written everything yet, keep trying. */
2270
0
    return 0;
2271
0
}
2272
2273
QUIC_NEEDS_LOCK
2274
static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
2275
                               size_t *written)
2276
0
{
2277
0
    int res;
2278
0
    QUIC_XSO *xso = ctx->xso;
2279
0
    struct quic_write_again_args args;
2280
0
    size_t actual_written = 0;
2281
2282
    /* First make a best effort to append as much of the data as possible. */
2283
0
    if (!xso_sstream_append(xso, buf, len, &actual_written)) {
2284
        /* Stream already finished or allocation error. */
2285
0
        *written = 0;
2286
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2287
0
    }
2288
2289
0
    quic_post_write(xso, actual_written > 0, 1);
2290
2291
0
    if (actual_written == len) {
2292
        /* Managed to append everything on the first try. */
2293
0
        *written = actual_written;
2294
0
        return 1;
2295
0
    }
2296
2297
    /*
2298
     * We did not manage to append all of the data immediately, so the stream
2299
     * buffer has probably filled up. This means we need to block until some of
2300
     * it is freed up.
2301
     */
2302
0
    args.xso            = xso;
2303
0
    args.buf            = (const unsigned char *)buf + actual_written;
2304
0
    args.len            = len - actual_written;
2305
0
    args.total_written  = 0;
2306
0
    args.err            = ERR_R_INTERNAL_ERROR;
2307
2308
0
    res = block_until_pred(xso->conn, quic_write_again, &args, 0);
2309
0
    if (res <= 0) {
2310
0
        if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
2311
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2312
0
        else
2313
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
2314
0
    }
2315
2316
0
    *written = args.total_written;
2317
0
    return 1;
2318
0
}
2319
2320
/*
2321
 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2322
 * write semantics.
2323
 */
2324
static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
2325
                            size_t buf_len, size_t already_sent)
2326
245
{
2327
245
    assert(!xso->aon_write_in_progress);
2328
2329
245
    xso->aon_write_in_progress = 1;
2330
245
    xso->aon_buf_base          = buf;
2331
245
    xso->aon_buf_pos           = already_sent;
2332
245
    xso->aon_buf_len           = buf_len;
2333
245
}
2334
2335
static void aon_write_finish(QUIC_XSO *xso)
2336
68
{
2337
68
    xso->aon_write_in_progress   = 0;
2338
68
    xso->aon_buf_base            = NULL;
2339
68
    xso->aon_buf_pos             = 0;
2340
68
    xso->aon_buf_len             = 0;
2341
68
}
2342
2343
QUIC_NEEDS_LOCK
2344
static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2345
                                      size_t len, size_t *written)
2346
17.3k
{
2347
17.3k
    QUIC_XSO *xso = ctx->xso;
2348
17.3k
    const void *actual_buf;
2349
17.3k
    size_t actual_len, actual_written = 0;
2350
17.3k
    int accept_moving_buffer
2351
17.3k
        = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2352
2353
17.3k
    if (xso->aon_write_in_progress) {
2354
        /*
2355
         * We are in the middle of an AON write (i.e., a previous write did not
2356
         * manage to append all data to the SSTREAM and we have Enable Partial
2357
         * Write (EPW) mode disabled.)
2358
         */
2359
15.3k
        if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2360
15.3k
            || len != xso->aon_buf_len)
2361
            /*
2362
             * Pointer must not have changed if we are not in accept moving
2363
             * buffer mode. Length must never change.
2364
             */
2365
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2366
2367
15.3k
        actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2368
15.3k
        actual_len = len - xso->aon_buf_pos;
2369
15.3k
        assert(actual_len > 0);
2370
15.3k
    } else {
2371
2.03k
        actual_buf = buf;
2372
2.03k
        actual_len = len;
2373
2.03k
    }
2374
2375
    /* First make a best effort to append as much of the data as possible. */
2376
17.3k
    if (!xso_sstream_append(xso, actual_buf, actual_len, &actual_written)) {
2377
        /* Stream already finished or allocation error. */
2378
0
        *written = 0;
2379
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2380
0
    }
2381
2382
17.3k
    quic_post_write(xso, actual_written > 0, 1);
2383
2384
17.3k
    if (actual_written == actual_len) {
2385
        /* We have sent everything. */
2386
1.19k
        if (xso->aon_write_in_progress) {
2387
            /*
2388
             * We have sent everything, and we were in the middle of an AON
2389
             * write. The output write length is the total length of the AON
2390
             * buffer, not however many bytes we managed to write to the stream
2391
             * in this call.
2392
             */
2393
66
            *written = xso->aon_buf_len;
2394
66
            aon_write_finish(xso);
2395
1.12k
        } else {
2396
1.12k
            *written = actual_written;
2397
1.12k
        }
2398
2399
1.19k
        return 1;
2400
1.19k
    }
2401
2402
16.1k
    if (xso->aon_write_in_progress) {
2403
        /*
2404
         * AON write is in progress but we have not written everything yet. We
2405
         * may have managed to send zero bytes, or some number of bytes less
2406
         * than the total remaining which need to be appended during this
2407
         * AON operation.
2408
         */
2409
15.2k
        xso->aon_buf_pos += actual_written;
2410
15.2k
        assert(xso->aon_buf_pos < xso->aon_buf_len);
2411
15.2k
        return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2412
15.2k
    }
2413
2414
    /*
2415
     * Not in an existing AON operation but partial write is not enabled, so we
2416
     * need to begin a new AON operation. However we needn't bother if we didn't
2417
     * actually append anything.
2418
     */
2419
908
    if (actual_written > 0)
2420
173
        aon_write_begin(xso, buf, len, actual_written);
2421
2422
    /*
2423
     * AON - We do not publicly admit to having appended anything until AON
2424
     * completes.
2425
     */
2426
908
    *written = 0;
2427
908
    return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2428
16.1k
}
2429
2430
QUIC_NEEDS_LOCK
2431
static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2432
                                      size_t *written)
2433
0
{
2434
0
    QUIC_XSO *xso = ctx->xso;
2435
2436
    /* Simple best effort operation. */
2437
0
    if (!xso_sstream_append(xso, buf, len, written)) {
2438
        /* Stream already finished or allocation error. */
2439
0
        *written = 0;
2440
0
        return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2441
0
    }
2442
2443
0
    quic_post_write(xso, *written > 0, 1);
2444
2445
0
    if (*written == 0)
2446
        /* SSL_write_ex returns 0 if it didn't read anything .*/
2447
0
        return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2448
2449
0
    return 1;
2450
0
}
2451
2452
QUIC_NEEDS_LOCK
2453
static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2454
19.7k
{
2455
19.7k
    QUIC_STREAM_MAP *qsm;
2456
2457
19.7k
    if (xso == NULL || xso->stream == NULL) {
2458
0
        *err = ERR_R_INTERNAL_ERROR;
2459
0
        return 0;
2460
0
    }
2461
2462
19.7k
    switch (xso->stream->send_state) {
2463
0
    default:
2464
7
    case QUIC_SSTREAM_STATE_NONE:
2465
7
        *err = SSL_R_STREAM_RECV_ONLY;
2466
7
        return 0;
2467
2468
1.35k
    case QUIC_SSTREAM_STATE_READY:
2469
1.35k
        qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2470
2471
1.35k
        if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2472
0
            *err = ERR_R_INTERNAL_ERROR;
2473
0
            return 0;
2474
0
        }
2475
2476
        /* FALLTHROUGH */
2477
19.6k
    case QUIC_SSTREAM_STATE_SEND:
2478
19.6k
    case QUIC_SSTREAM_STATE_DATA_SENT:
2479
19.6k
    case QUIC_SSTREAM_STATE_DATA_RECVD:
2480
19.6k
        if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2481
0
            *err = SSL_R_STREAM_FINISHED;
2482
0
            return 0;
2483
0
        }
2484
2485
19.6k
        return 1;
2486
2487
8
    case QUIC_SSTREAM_STATE_RESET_SENT:
2488
8
    case QUIC_SSTREAM_STATE_RESET_RECVD:
2489
8
        *err = SSL_R_STREAM_RESET;
2490
8
        return 0;
2491
19.7k
    }
2492
19.7k
}
2493
2494
QUIC_TAKES_LOCK
2495
int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2496
17.4k
{
2497
17.4k
    int ret;
2498
17.4k
    QCTX ctx;
2499
17.4k
    int partial_write, err;
2500
2501
17.4k
    *written = 0;
2502
2503
17.4k
    if (len == 0) {
2504
        /* Do not autocreate default XSO for zero-length writes. */
2505
0
        if (!expect_quic(s, &ctx))
2506
0
            return 0;
2507
2508
0
        quic_lock_for_io(&ctx);
2509
17.4k
    } else {
2510
17.4k
        if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))
2511
0
            return 0;
2512
17.4k
    }
2513
2514
17.4k
    partial_write = ((ctx.xso != NULL)
2515
17.4k
        ? ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0) : 0);
2516
2517
17.4k
    if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2518
98
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2519
98
        goto out;
2520
98
    }
2521
2522
    /*
2523
     * If we haven't finished the handshake, try to advance it.
2524
     * We don't accept writes until the handshake is completed.
2525
     */
2526
17.3k
    if (quic_do_handshake(&ctx) < 1) {
2527
0
        ret = 0;
2528
0
        goto out;
2529
0
    }
2530
2531
    /* Ensure correct stream state, stream send part not concluded, etc. */
2532
17.3k
    if (len > 0 && !quic_validate_for_write(ctx.xso, &err)) {
2533
12
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2534
12
        goto out;
2535
12
    }
2536
2537
17.3k
    if (len == 0) {
2538
0
        ret = 1;
2539
0
        goto out;
2540
0
    }
2541
2542
17.3k
    if (xso_blocking_mode(ctx.xso))
2543
0
        ret = quic_write_blocking(&ctx, buf, len, written);
2544
17.3k
    else if (partial_write)
2545
0
        ret = quic_write_nonblocking_epw(&ctx, buf, len, written);
2546
17.3k
    else
2547
17.3k
        ret = quic_write_nonblocking_aon(&ctx, buf, len, written);
2548
2549
17.4k
out:
2550
17.4k
    quic_unlock(ctx.qc);
2551
17.4k
    return ret;
2552
17.3k
}
2553
2554
/*
2555
 * SSL_read
2556
 * --------
2557
 */
2558
struct quic_read_again_args {
2559
    QCTX            *ctx;
2560
    QUIC_STREAM     *stream;
2561
    void            *buf;
2562
    size_t          len;
2563
    size_t          *bytes_read;
2564
    int             peek;
2565
};
2566
2567
QUIC_NEEDS_LOCK
2568
static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2569
13.3M
{
2570
13.3M
    QUIC_STREAM_MAP *qsm;
2571
2572
13.3M
    *eos = 0;
2573
2574
13.3M
    if (xso == NULL || xso->stream == NULL) {
2575
0
        *err = ERR_R_INTERNAL_ERROR;
2576
0
        return 0;
2577
0
    }
2578
2579
13.3M
    switch (xso->stream->recv_state) {
2580
0
    default:
2581
0
    case QUIC_RSTREAM_STATE_NONE:
2582
0
        *err = SSL_R_STREAM_SEND_ONLY;
2583
0
        return 0;
2584
2585
9.47M
    case QUIC_RSTREAM_STATE_RECV:
2586
13.3M
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2587
13.3M
    case QUIC_RSTREAM_STATE_DATA_RECVD:
2588
13.3M
        return 1;
2589
2590
80
    case QUIC_RSTREAM_STATE_DATA_READ:
2591
80
        *eos = 1;
2592
80
        return 0;
2593
2594
92
    case QUIC_RSTREAM_STATE_RESET_RECVD:
2595
92
        qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2596
92
        ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2597
2598
        /* FALLTHROUGH */
2599
92
    case QUIC_RSTREAM_STATE_RESET_READ:
2600
92
        *err = SSL_R_STREAM_RESET;
2601
92
        return 0;
2602
13.3M
    }
2603
13.3M
}
2604
2605
QUIC_NEEDS_LOCK
2606
static int quic_read_actual(QCTX *ctx,
2607
                            QUIC_STREAM *stream,
2608
                            void *buf, size_t buf_len,
2609
                            size_t *bytes_read,
2610
                            int peek)
2611
13.3M
{
2612
13.3M
    int is_fin = 0, err, eos;
2613
13.3M
    QUIC_CONNECTION *qc = ctx->qc;
2614
2615
13.3M
    if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2616
172
        if (eos)
2617
80
            return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2618
92
        else
2619
92
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2620
172
    }
2621
2622
13.3M
    if (peek) {
2623
0
        if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2624
0
                                    bytes_read, &is_fin))
2625
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2626
2627
13.3M
    } else {
2628
13.3M
        if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2629
13.3M
                                    bytes_read, &is_fin))
2630
0
            return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2631
13.3M
    }
2632
2633
13.3M
    if (!peek) {
2634
13.3M
        if (*bytes_read > 0) {
2635
            /*
2636
             * We have read at least one byte from the stream. Inform stream-level
2637
             * RXFC of the retirement of controlled bytes. Update the active stream
2638
             * status (the RXFC may now want to emit a frame granting more credit to
2639
             * the peer).
2640
             */
2641
2.07k
            OSSL_RTT_INFO rtt_info;
2642
2643
2.07k
            ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2644
2645
2.07k
            if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2646
2.07k
                                          rtt_info.smoothed_rtt))
2647
0
                return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2648
2.07k
        }
2649
2650
13.3M
        if (is_fin && !peek) {
2651
257
            QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2652
2653
257
            ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2654
257
        }
2655
2656
13.3M
        if (*bytes_read > 0)
2657
2.07k
            ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2658
2.07k
                                              stream);
2659
13.3M
    }
2660
2661
13.3M
    if (*bytes_read == 0 && is_fin)
2662
16
        return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2663
2664
13.3M
    return 1;
2665
13.3M
}
2666
2667
QUIC_NEEDS_LOCK
2668
static int quic_read_again(void *arg)
2669
0
{
2670
0
    struct quic_read_again_args *args = arg;
2671
2672
0
    if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
2673
        /* If connection is torn down due to an error while blocking, stop. */
2674
0
        QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2675
0
        return -1;
2676
0
    }
2677
2678
0
    if (!quic_read_actual(args->ctx, args->stream,
2679
0
                          args->buf, args->len, args->bytes_read,
2680
0
                          args->peek))
2681
0
        return -1;
2682
2683
0
    if (*args->bytes_read > 0)
2684
        /* got at least one byte, the SSL_read op can finish now */
2685
0
        return 1;
2686
2687
0
    return 0; /* did not read anything, keep trying */
2688
0
}
2689
2690
QUIC_TAKES_LOCK
2691
static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
2692
8.89M
{
2693
8.89M
    int ret, res;
2694
8.89M
    QCTX ctx;
2695
8.89M
    struct quic_read_again_args args;
2696
2697
8.89M
    *bytes_read = 0;
2698
2699
8.89M
    if (!expect_quic(s, &ctx))
2700
0
        return 0;
2701
2702
8.89M
    quic_lock_for_io(&ctx);
2703
2704
8.89M
    if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2705
2.47k
        ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2706
2.47k
        goto out;
2707
2.47k
    }
2708
2709
    /* If we haven't finished the handshake, try to advance it. */
2710
8.89M
    if (quic_do_handshake(&ctx) < 1) {
2711
0
        ret = 0; /* ossl_quic_do_handshake raised error here */
2712
0
        goto out;
2713
0
    }
2714
2715
8.89M
    if (ctx.xso == NULL) {
2716
        /*
2717
         * Called on a QCSO and we don't currently have a default stream.
2718
         *
2719
         * Wait until we get a stream initiated by the peer (blocking mode) or
2720
         * fail if we don't have one yet (non-blocking mode).
2721
         */
2722
2.20M
        if (!qc_wait_for_default_xso_for_read(&ctx, /*peek=*/0)) {
2723
2.20M
            ret = 0; /* error already raised here */
2724
2.20M
            goto out;
2725
2.20M
        }
2726
2727
2.94k
        ctx.xso = ctx.qc->default_xso;
2728
2.94k
    }
2729
2730
6.68M
    if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2731
168
        ret = 0; /* quic_read_actual raised error here */
2732
168
        goto out;
2733
168
    }
2734
2735
6.68M
    if (*bytes_read > 0) {
2736
        /*
2737
         * Even though we succeeded, tick the reactor here to ensure we are
2738
         * handling other aspects of the QUIC connection.
2739
         */
2740
1.47k
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2741
1.47k
        ret = 1;
2742
6.68M
    } else if (xso_blocking_mode(ctx.xso)) {
2743
        /*
2744
         * We were not able to read anything immediately, so our stream
2745
         * buffer is empty. This means we need to block until we get
2746
         * at least one byte.
2747
         */
2748
0
        args.ctx        = &ctx;
2749
0
        args.stream     = ctx.xso->stream;
2750
0
        args.buf        = buf;
2751
0
        args.len        = len;
2752
0
        args.bytes_read = bytes_read;
2753
0
        args.peek       = peek;
2754
2755
0
        res = block_until_pred(ctx.qc, quic_read_again, &args, 0);
2756
0
        if (res == 0) {
2757
0
            ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2758
0
            goto out;
2759
0
        } else if (res < 0) {
2760
0
            ret = 0; /* quic_read_again raised error here */
2761
0
            goto out;
2762
0
        }
2763
2764
0
        ret = 1;
2765
6.68M
    } else {
2766
        /*
2767
         * We did not get any bytes and are not in blocking mode.
2768
         * Tick to see if this delivers any more.
2769
         */
2770
6.68M
        ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);
2771
2772
        /* Try the read again. */
2773
6.68M
        if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
2774
20
            ret = 0; /* quic_read_actual raised error here */
2775
20
            goto out;
2776
20
        }
2777
2778
6.68M
        if (*bytes_read > 0)
2779
602
            ret = 1; /* Succeeded this time. */
2780
6.68M
        else
2781
6.68M
            ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
2782
6.68M
    }
2783
2784
8.89M
out:
2785
8.89M
    quic_unlock(ctx.qc);
2786
8.89M
    return ret;
2787
6.68M
}
2788
2789
int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
2790
8.89M
{
2791
8.89M
    return quic_read(s, buf, len, bytes_read, 0);
2792
8.89M
}
2793
2794
int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
2795
0
{
2796
0
    return quic_read(s, buf, len, bytes_read, 1);
2797
0
}
2798
2799
/*
2800
 * SSL_pending
2801
 * -----------
2802
 */
2803
2804
QUIC_TAKES_LOCK
2805
static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
2806
0
{
2807
0
    QCTX ctx;
2808
0
    size_t avail = 0;
2809
2810
0
    if (!expect_quic(s, &ctx))
2811
0
        return 0;
2812
2813
0
    quic_lock(ctx.qc);
2814
2815
0
    if (ctx.xso == NULL) {
2816
        /* No XSO yet, but there might be a default XSO eligible to be created. */
2817
0
        if (qc_wait_for_default_xso_for_read(&ctx, /*peek=*/1)) {
2818
0
            ctx.xso = ctx.qc->default_xso;
2819
0
        } else {
2820
0
            QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
2821
0
            goto out;
2822
0
        }
2823
0
    }
2824
2825
0
    if (ctx.xso->stream == NULL) {
2826
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
2827
0
        goto out;
2828
0
    }
2829
2830
0
    if (check_channel)
2831
0
        avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
2832
0
                                              /*include_fin=*/1)
2833
0
             || ossl_quic_channel_has_pending(ctx.qc->ch)
2834
0
             || ossl_quic_channel_is_term_any(ctx.qc->ch);
2835
0
    else
2836
0
        avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
2837
0
                                              /*include_fin=*/0);
2838
2839
0
out:
2840
0
    quic_unlock(ctx.qc);
2841
0
    return avail;
2842
0
}
2843
2844
size_t ossl_quic_pending(const SSL *s)
2845
0
{
2846
0
    return ossl_quic_pending_int(s, /*check_channel=*/0);
2847
0
}
2848
2849
int ossl_quic_has_pending(const SSL *s)
2850
0
{
2851
    /* Do we have app-side pending data or pending URXEs or RXEs? */
2852
0
    return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
2853
0
}
2854
2855
/*
2856
 * SSL_stream_conclude
2857
 * -------------------
2858
 */
2859
QUIC_TAKES_LOCK
2860
int ossl_quic_conn_stream_conclude(SSL *s)
2861
0
{
2862
0
    QCTX ctx;
2863
0
    QUIC_STREAM *qs;
2864
0
    int err;
2865
2866
0
    if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))
2867
0
        return 0;
2868
2869
0
    qs = ctx.xso->stream;
2870
2871
0
    if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
2872
0
        quic_unlock(ctx.qc);
2873
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2874
0
    }
2875
2876
0
    if (!quic_validate_for_write(ctx.xso, &err)) {
2877
0
        quic_unlock(ctx.qc);
2878
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2879
0
    }
2880
2881
0
    if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
2882
0
        quic_unlock(ctx.qc);
2883
0
        return 1;
2884
0
    }
2885
2886
0
    ossl_quic_sstream_fin(qs->sstream);
2887
0
    quic_post_write(ctx.xso, 1, 1);
2888
0
    quic_unlock(ctx.qc);
2889
0
    return 1;
2890
0
}
2891
2892
/*
2893
 * SSL_inject_net_dgram
2894
 * --------------------
2895
 */
2896
QUIC_TAKES_LOCK
2897
int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
2898
                         size_t buf_len,
2899
                         const BIO_ADDR *peer,
2900
                         const BIO_ADDR *local)
2901
0
{
2902
0
    int ret;
2903
0
    QCTX ctx;
2904
0
    QUIC_DEMUX *demux;
2905
2906
0
    if (!expect_quic(s, &ctx))
2907
0
        return 0;
2908
2909
0
    quic_lock(ctx.qc);
2910
2911
0
    demux = ossl_quic_channel_get0_demux(ctx.qc->ch);
2912
0
    ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
2913
2914
0
    quic_unlock(ctx.qc);
2915
0
    return ret;
2916
0
}
2917
2918
/*
2919
 * SSL_get0_connection
2920
 * -------------------
2921
 */
2922
SSL *ossl_quic_get0_connection(SSL *s)
2923
0
{
2924
0
    QCTX ctx;
2925
2926
0
    if (!expect_quic(s, &ctx))
2927
0
        return NULL;
2928
2929
0
    return &ctx.qc->ssl;
2930
0
}
2931
2932
/*
2933
 * SSL_get_stream_type
2934
 * -------------------
2935
 */
2936
int ossl_quic_get_stream_type(SSL *s)
2937
0
{
2938
0
    QCTX ctx;
2939
2940
0
    if (!expect_quic(s, &ctx))
2941
0
        return SSL_STREAM_TYPE_BIDI;
2942
2943
0
    if (ctx.xso == NULL) {
2944
        /*
2945
         * If deferred XSO creation has yet to occur, proceed according to the
2946
         * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
2947
         * what kind of stream will be created yet, so return BIDI on the basis
2948
         * that at this time, the client still has the option of calling
2949
         * SSL_read() or SSL_write() first.
2950
         */
2951
0
        if (ctx.qc->default_xso_created
2952
0
            || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2953
0
            return SSL_STREAM_TYPE_NONE;
2954
0
        else
2955
0
            return SSL_STREAM_TYPE_BIDI;
2956
0
    }
2957
2958
0
    if (ossl_quic_stream_is_bidi(ctx.xso->stream))
2959
0
        return SSL_STREAM_TYPE_BIDI;
2960
2961
0
    if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
2962
0
        return SSL_STREAM_TYPE_READ;
2963
0
    else
2964
0
        return SSL_STREAM_TYPE_WRITE;
2965
0
}
2966
2967
/*
2968
 * SSL_get_stream_id
2969
 * -----------------
2970
 */
2971
QUIC_TAKES_LOCK
2972
uint64_t ossl_quic_get_stream_id(SSL *s)
2973
0
{
2974
0
    QCTX ctx;
2975
0
    uint64_t id;
2976
2977
0
    if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
2978
0
        return UINT64_MAX;
2979
2980
0
    id = ctx.xso->stream->id;
2981
0
    quic_unlock(ctx.qc);
2982
2983
0
    return id;
2984
0
}
2985
2986
/*
2987
 * SSL_is_stream_local
2988
 * -------------------
2989
 */
2990
QUIC_TAKES_LOCK
2991
int ossl_quic_is_stream_local(SSL *s)
2992
0
{
2993
0
    QCTX ctx;
2994
0
    int is_local;
2995
2996
0
    if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
2997
0
        return -1;
2998
2999
0
    is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);
3000
0
    quic_unlock(ctx.qc);
3001
3002
0
    return is_local;
3003
0
}
3004
3005
/*
3006
 * SSL_set_default_stream_mode
3007
 * ---------------------------
3008
 */
3009
QUIC_TAKES_LOCK
3010
int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
3011
0
{
3012
0
    QCTX ctx;
3013
3014
0
    if (!expect_quic_conn_only(s, &ctx))
3015
0
        return 0;
3016
3017
0
    quic_lock(ctx.qc);
3018
3019
0
    if (ctx.qc->default_xso_created) {
3020
0
        quic_unlock(ctx.qc);
3021
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3022
0
                                       "too late to change default stream mode");
3023
0
    }
3024
3025
0
    switch (mode) {
3026
0
    case SSL_DEFAULT_STREAM_MODE_NONE:
3027
0
    case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
3028
0
    case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
3029
0
        ctx.qc->default_stream_mode = mode;
3030
0
        break;
3031
0
    default:
3032
0
        quic_unlock(ctx.qc);
3033
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3034
0
                                       "bad default stream type");
3035
0
    }
3036
3037
0
    quic_unlock(ctx.qc);
3038
0
    return 1;
3039
0
}
3040
3041
/*
3042
 * SSL_detach_stream
3043
 * -----------------
3044
 */
3045
QUIC_TAKES_LOCK
3046
SSL *ossl_quic_detach_stream(SSL *s)
3047
0
{
3048
0
    QCTX ctx;
3049
0
    QUIC_XSO *xso = NULL;
3050
3051
0
    if (!expect_quic_conn_only(s, &ctx))
3052
0
        return NULL;
3053
3054
0
    quic_lock(ctx.qc);
3055
3056
    /* Calling this function inhibits default XSO autocreation. */
3057
    /* QC ref to any default XSO is transferred to us and to caller. */
3058
0
    qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
3059
3060
0
    quic_unlock(ctx.qc);
3061
3062
0
    return xso != NULL ? &xso->ssl : NULL;
3063
0
}
3064
3065
/*
3066
 * SSL_attach_stream
3067
 * -----------------
3068
 */
3069
QUIC_TAKES_LOCK
3070
int ossl_quic_attach_stream(SSL *conn, SSL *stream)
3071
0
{
3072
0
    QCTX ctx;
3073
0
    QUIC_XSO *xso;
3074
0
    int nref;
3075
3076
0
    if (!expect_quic_conn_only(conn, &ctx))
3077
0
        return 0;
3078
3079
0
    if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
3080
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
3081
0
                                       "stream to attach must be a valid QUIC stream");
3082
3083
0
    xso = (QUIC_XSO *)stream;
3084
3085
0
    quic_lock(ctx.qc);
3086
3087
0
    if (ctx.qc->default_xso != NULL) {
3088
0
        quic_unlock(ctx.qc);
3089
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3090
0
                                       "connection already has a default stream");
3091
0
    }
3092
3093
    /*
3094
     * It is a caller error for the XSO being attached as a default XSO to have
3095
     * more than one ref.
3096
     */
3097
0
    if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) {
3098
0
        quic_unlock(ctx.qc);
3099
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
3100
0
                                       "ref");
3101
0
    }
3102
3103
0
    if (nref != 1) {
3104
0
        quic_unlock(ctx.qc);
3105
0
        return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3106
0
                                       "stream being attached must have "
3107
0
                                       "only 1 reference");
3108
0
    }
3109
3110
    /* Caller's reference to the XSO is transferred to us. */
3111
    /* Calling this function inhibits default XSO autocreation. */
3112
0
    qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
3113
3114
0
    quic_unlock(ctx.qc);
3115
0
    return 1;
3116
0
}
3117
3118
/*
3119
 * SSL_set_incoming_stream_policy
3120
 * ------------------------------
3121
 */
3122
QUIC_NEEDS_LOCK
3123
static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
3124
47.5k
{
3125
47.5k
    switch (qc->incoming_stream_policy) {
3126
21.1k
        case SSL_INCOMING_STREAM_POLICY_AUTO:
3127
21.1k
            if ((qc->default_xso == NULL && !qc->default_xso_created)
3128
21.1k
                || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3129
21.1k
                return SSL_INCOMING_STREAM_POLICY_ACCEPT;
3130
0
            else
3131
0
                return SSL_INCOMING_STREAM_POLICY_REJECT;
3132
3133
26.3k
        default:
3134
26.3k
            return qc->incoming_stream_policy;
3135
47.5k
    }
3136
47.5k
}
3137
3138
QUIC_NEEDS_LOCK
3139
static void qc_update_reject_policy(QUIC_CONNECTION *qc)
3140
47.3k
{
3141
47.3k
    int policy = qc_get_effective_incoming_stream_policy(qc);
3142
47.3k
    int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
3143
3144
47.3k
    ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
3145
47.3k
                                                      enable_reject,
3146
47.3k
                                                      qc->incoming_stream_aec);
3147
47.3k
}
3148
3149
QUIC_TAKES_LOCK
3150
int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
3151
                                         uint64_t aec)
3152
21.1k
{
3153
21.1k
    int ret = 1;
3154
21.1k
    QCTX ctx;
3155
3156
21.1k
    if (!expect_quic_conn_only(s, &ctx))
3157
0
        return 0;
3158
3159
21.1k
    quic_lock(ctx.qc);
3160
3161
21.1k
    switch (policy) {
3162
0
    case SSL_INCOMING_STREAM_POLICY_AUTO:
3163
21.1k
    case SSL_INCOMING_STREAM_POLICY_ACCEPT:
3164
21.1k
    case SSL_INCOMING_STREAM_POLICY_REJECT:
3165
21.1k
        ctx.qc->incoming_stream_policy = policy;
3166
21.1k
        ctx.qc->incoming_stream_aec    = aec;
3167
21.1k
        break;
3168
3169
0
    default:
3170
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3171
0
        ret = 0;
3172
0
        break;
3173
21.1k
    }
3174
3175
21.1k
    qc_update_reject_policy(ctx.qc);
3176
21.1k
    quic_unlock(ctx.qc);
3177
21.1k
    return ret;
3178
21.1k
}
3179
3180
/*
3181
 * SSL_accept_stream
3182
 * -----------------
3183
 */
3184
struct wait_for_incoming_stream_args {
3185
    QCTX            *ctx;
3186
    QUIC_STREAM     *qs;
3187
};
3188
3189
QUIC_NEEDS_LOCK
3190
static int wait_for_incoming_stream(void *arg)
3191
0
{
3192
0
    struct wait_for_incoming_stream_args *args = arg;
3193
0
    QUIC_CONNECTION *qc = args->ctx->qc;
3194
0
    QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
3195
3196
0
    if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
3197
        /* If connection is torn down due to an error while blocking, stop. */
3198
0
        QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3199
0
        return -1;
3200
0
    }
3201
3202
0
    args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3203
0
    if (args->qs != NULL)
3204
0
        return 1; /* got a stream */
3205
3206
0
    return 0; /* did not get a stream, keep trying */
3207
0
}
3208
3209
QUIC_TAKES_LOCK
3210
SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
3211
118
{
3212
118
    QCTX ctx;
3213
118
    int ret;
3214
118
    SSL *new_s = NULL;
3215
118
    QUIC_STREAM_MAP *qsm;
3216
118
    QUIC_STREAM *qs;
3217
118
    QUIC_XSO *xso;
3218
118
    OSSL_RTT_INFO rtt_info;
3219
3220
118
    if (!expect_quic_conn_only(s, &ctx))
3221
0
        return NULL;
3222
3223
118
    quic_lock(ctx.qc);
3224
3225
118
    if (qc_get_effective_incoming_stream_policy(ctx.qc)
3226
118
        == SSL_INCOMING_STREAM_POLICY_REJECT) {
3227
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3228
0
        goto out;
3229
0
    }
3230
3231
118
    qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
3232
3233
118
    qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3234
118
    if (qs == NULL) {
3235
0
        if (qc_blocking_mode(ctx.qc)
3236
0
            && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
3237
0
            struct wait_for_incoming_stream_args args;
3238
3239
0
            args.ctx = &ctx;
3240
0
            args.qs = NULL;
3241
3242
0
            ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);
3243
0
            if (ret == 0) {
3244
0
                QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3245
0
                goto out;
3246
0
            } else if (ret < 0 || args.qs == NULL) {
3247
0
                goto out;
3248
0
            }
3249
3250
0
            qs = args.qs;
3251
0
        } else {
3252
0
            goto out;
3253
0
        }
3254
0
    }
3255
3256
118
    xso = create_xso_from_stream(ctx.qc, qs);
3257
118
    if (xso == NULL)
3258
0
        goto out;
3259
3260
118
    ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
3261
118
    ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
3262
118
                                                  rtt_info.smoothed_rtt);
3263
118
    new_s = &xso->ssl;
3264
3265
    /* Calling this function inhibits default XSO autocreation. */
3266
118
    qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
3267
3268
118
out:
3269
118
    quic_unlock(ctx.qc);
3270
118
    return new_s;
3271
118
}
3272
3273
/*
3274
 * SSL_get_accept_stream_queue_len
3275
 * -------------------------------
3276
 */
3277
QUIC_TAKES_LOCK
3278
size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
3279
3.50k
{
3280
3.50k
    QCTX ctx;
3281
3.50k
    size_t v;
3282
3283
3.50k
    if (!expect_quic_conn_only(s, &ctx))
3284
0
        return 0;
3285
3286
3.50k
    quic_lock(ctx.qc);
3287
3288
3.50k
    v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
3289
3290
3.50k
    quic_unlock(ctx.qc);
3291
3.50k
    return v;
3292
3.50k
}
3293
3294
/*
3295
 * SSL_stream_reset
3296
 * ----------------
3297
 */
3298
int ossl_quic_stream_reset(SSL *ssl,
3299
                           const SSL_STREAM_RESET_ARGS *args,
3300
                           size_t args_len)
3301
0
{
3302
0
    QCTX ctx;
3303
0
    QUIC_STREAM_MAP *qsm;
3304
0
    QUIC_STREAM *qs;
3305
0
    uint64_t error_code;
3306
0
    int ok, err;
3307
3308
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))
3309
0
        return 0;
3310
3311
0
    qsm         = ossl_quic_channel_get_qsm(ctx.qc->ch);
3312
0
    qs          = ctx.xso->stream;
3313
0
    error_code  = (args != NULL ? args->quic_error_code : 0);
3314
3315
0
    if (!quic_validate_for_write(ctx.xso, &err)) {
3316
0
        ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
3317
0
        goto err;
3318
0
    }
3319
3320
0
    ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
3321
3322
0
err:
3323
0
    quic_unlock(ctx.qc);
3324
0
    return ok;
3325
0
}
3326
3327
/*
3328
 * SSL_get_stream_read_state
3329
 * -------------------------
3330
 */
3331
static void quic_classify_stream(QUIC_CONNECTION *qc,
3332
                                 QUIC_STREAM *qs,
3333
                                 int is_write,
3334
                                 int *state,
3335
                                 uint64_t *app_error_code)
3336
0
{
3337
0
    int local_init;
3338
0
    uint64_t final_size;
3339
3340
0
    local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
3341
3342
0
    if (app_error_code != NULL)
3343
0
        *app_error_code = UINT64_MAX;
3344
0
    else
3345
0
        app_error_code = &final_size; /* throw away value */
3346
3347
0
    if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
3348
        /*
3349
         * Unidirectional stream and this direction of transmission doesn't
3350
         * exist.
3351
         */
3352
0
        *state = SSL_STREAM_STATE_WRONG_DIR;
3353
0
    } else if (ossl_quic_channel_is_term_any(qc->ch)) {
3354
        /* Connection already closed. */
3355
0
        *state = SSL_STREAM_STATE_CONN_CLOSED;
3356
0
    } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
3357
        /* Application has read a FIN. */
3358
0
        *state = SSL_STREAM_STATE_FINISHED;
3359
0
    } else if ((!is_write && qs->stop_sending)
3360
0
               || (is_write && ossl_quic_stream_send_is_reset(qs))) {
3361
        /*
3362
         * Stream has been reset locally. FIN takes precedence over this for the
3363
         * read case as the application need not care if the stream is reset
3364
         * after a FIN has been successfully processed.
3365
         */
3366
0
        *state          = SSL_STREAM_STATE_RESET_LOCAL;
3367
0
        *app_error_code = !is_write
3368
0
            ? qs->stop_sending_aec
3369
0
            : qs->reset_stream_aec;
3370
0
    } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
3371
0
               || (is_write && qs->peer_stop_sending)) {
3372
        /*
3373
         * Stream has been reset remotely. */
3374
0
        *state          = SSL_STREAM_STATE_RESET_REMOTE;
3375
0
        *app_error_code = !is_write
3376
0
            ? qs->peer_reset_stream_aec
3377
0
            : qs->peer_stop_sending_aec;
3378
0
    } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
3379
0
                                                            &final_size)) {
3380
        /*
3381
         * Stream has been finished. Stream reset takes precedence over this for
3382
         * the write case as peer may not have received all data.
3383
         */
3384
0
        *state = SSL_STREAM_STATE_FINISHED;
3385
0
    } else {
3386
        /* Stream still healthy. */
3387
0
        *state = SSL_STREAM_STATE_OK;
3388
0
    }
3389
0
}
3390
3391
static int quic_get_stream_state(SSL *ssl, int is_write)
3392
0
{
3393
0
    QCTX ctx;
3394
0
    int state;
3395
3396
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
3397
0
        return SSL_STREAM_STATE_NONE;
3398
3399
0
    quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
3400
0
    quic_unlock(ctx.qc);
3401
0
    return state;
3402
0
}
3403
3404
int ossl_quic_get_stream_read_state(SSL *ssl)
3405
0
{
3406
0
    return quic_get_stream_state(ssl, /*is_write=*/0);
3407
0
}
3408
3409
/*
3410
 * SSL_get_stream_write_state
3411
 * --------------------------
3412
 */
3413
int ossl_quic_get_stream_write_state(SSL *ssl)
3414
0
{
3415
0
    return quic_get_stream_state(ssl, /*is_write=*/1);
3416
0
}
3417
3418
/*
3419
 * SSL_get_stream_read_error_code
3420
 * ------------------------------
3421
 */
3422
static int quic_get_stream_error_code(SSL *ssl, int is_write,
3423
                                      uint64_t *app_error_code)
3424
0
{
3425
0
    QCTX ctx;
3426
0
    int state;
3427
3428
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
3429
0
        return -1;
3430
3431
0
    quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
3432
0
                         &state, app_error_code);
3433
3434
0
    quic_unlock(ctx.qc);
3435
0
    switch (state) {
3436
0
        case SSL_STREAM_STATE_FINISHED:
3437
0
             return 0;
3438
0
        case SSL_STREAM_STATE_RESET_LOCAL:
3439
0
        case SSL_STREAM_STATE_RESET_REMOTE:
3440
0
             return 1;
3441
0
        default:
3442
0
             return -1;
3443
0
    }
3444
0
}
3445
3446
int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
3447
0
{
3448
0
    return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
3449
0
}
3450
3451
/*
3452
 * SSL_get_stream_write_error_code
3453
 * -------------------------------
3454
 */
3455
int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
3456
0
{
3457
0
    return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
3458
0
}
3459
3460
/*
3461
 * Write buffer size mutation
3462
 * --------------------------
3463
 */
3464
int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
3465
0
{
3466
0
    int ret = 0;
3467
0
    QCTX ctx;
3468
3469
0
    if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
3470
0
        return 0;
3471
3472
0
    if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
3473
        /* Called on a unidirectional receive-only stream - error. */
3474
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3475
0
        goto out;
3476
0
    }
3477
3478
0
    if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
3479
        /*
3480
         * If the stream has a send part but we have disposed of it because we
3481
         * no longer need it, this is a no-op.
3482
         */
3483
0
        ret = 1;
3484
0
        goto out;
3485
0
    }
3486
3487
0
    if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
3488
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3489
0
        goto out;
3490
0
    }
3491
3492
0
    ret = 1;
3493
3494
0
out:
3495
0
    quic_unlock(ctx.qc);
3496
0
    return ret;
3497
0
}
3498
3499
/*
3500
 * SSL_get_conn_close_info
3501
 * -----------------------
3502
 */
3503
int ossl_quic_get_conn_close_info(SSL *ssl,
3504
                                  SSL_CONN_CLOSE_INFO *info,
3505
                                  size_t info_len)
3506
0
{
3507
0
    QCTX ctx;
3508
0
    const QUIC_TERMINATE_CAUSE *tc;
3509
3510
0
    if (!expect_quic_conn_only(ssl, &ctx))
3511
0
        return -1;
3512
3513
0
    tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
3514
0
    if (tc == NULL)
3515
0
        return 0;
3516
3517
0
    info->error_code    = tc->error_code;
3518
0
    info->frame_type    = tc->frame_type;
3519
0
    info->reason        = tc->reason;
3520
0
    info->reason_len    = tc->reason_len;
3521
0
    info->flags         = 0;
3522
0
    if (!tc->remote)
3523
0
        info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;
3524
0
    if (!tc->app)
3525
0
        info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;
3526
0
    return 1;
3527
0
}
3528
3529
/*
3530
 * SSL_key_update
3531
 * --------------
3532
 */
3533
int ossl_quic_key_update(SSL *ssl, int update_type)
3534
0
{
3535
0
    QCTX ctx;
3536
3537
0
    if (!expect_quic_conn_only(ssl, &ctx))
3538
0
        return 0;
3539
3540
0
    switch (update_type) {
3541
0
    case SSL_KEY_UPDATE_NOT_REQUESTED:
3542
        /*
3543
         * QUIC signals peer key update implicily by triggering a local
3544
         * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
3545
         */
3546
0
    case SSL_KEY_UPDATE_REQUESTED:
3547
0
        break;
3548
3549
0
    default:
3550
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3551
0
        return 0;
3552
0
    }
3553
3554
0
    quic_lock(ctx.qc);
3555
3556
    /* Attempt to perform a TXKU. */
3557
0
    if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
3558
0
        QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
3559
0
        quic_unlock(ctx.qc);
3560
0
        return 0;
3561
0
    }
3562
3563
0
    quic_unlock(ctx.qc);
3564
0
    return 1;
3565
0
}
3566
3567
/*
3568
 * SSL_get_key_update_type
3569
 * -----------------------
3570
 */
3571
int ossl_quic_get_key_update_type(const SSL *s)
3572
0
{
3573
    /*
3574
     * We always handle key updates immediately so a key update is never
3575
     * pending.
3576
     */
3577
0
    return SSL_KEY_UPDATE_NONE;
3578
0
}
3579
3580
/*
3581
 * QUIC Front-End I/O API: SSL_CTX Management
3582
 * ==========================================
3583
 */
3584
3585
long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3586
0
{
3587
0
    switch (cmd) {
3588
0
    default:
3589
0
        return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
3590
0
    }
3591
0
}
3592
3593
long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3594
0
{
3595
0
    QCTX ctx;
3596
3597
0
    if (!expect_quic_conn_only(s, &ctx))
3598
0
        return 0;
3599
3600
0
    switch (cmd) {
3601
0
    case SSL_CTRL_SET_MSG_CALLBACK:
3602
0
        ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
3603
0
                                           &ctx.qc->ssl);
3604
        /* This callback also needs to be set on the internal SSL object */
3605
0
        return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
3606
3607
0
    default:
3608
        /* Probably a TLS related ctrl. Defer to our internal SSL object */
3609
0
        return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
3610
0
    }
3611
0
}
3612
3613
long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3614
0
{
3615
0
    return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
3616
0
}
3617
3618
int ossl_quic_renegotiate_check(SSL *ssl, int initok)
3619
0
{
3620
    /* We never do renegotiation. */
3621
0
    return 0;
3622
0
}
3623
3624
const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)
3625
0
{
3626
0
    const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);
3627
3628
0
    if ((ciph->algorithm2 & SSL_QUIC) == 0)
3629
0
        return NULL;
3630
3631
0
    return ciph;
3632
0
}
3633
3634
/*
3635
 * These functions define the TLSv1.2 (and below) ciphers that are supported by
3636
 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
3637
 */
3638
3639
int ossl_quic_num_ciphers(void)
3640
21.1k
{
3641
21.1k
    return 0;
3642
21.1k
}
3643
3644
const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
3645
0
{
3646
0
    return NULL;
3647
0
}
3648
3649
/*
3650
 * SSL_get_shutdown()
3651
 * ------------------
3652
 */
3653
int ossl_quic_get_shutdown(const SSL *s)
3654
0
{
3655
0
    QCTX ctx;
3656
0
    int shut = 0;
3657
3658
0
    if (!expect_quic_conn_only(s, &ctx))
3659
0
        return 0;
3660
3661
0
    if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
3662
0
        shut |= SSL_SENT_SHUTDOWN;
3663
0
        if (!ossl_quic_channel_is_closing(ctx.qc->ch))
3664
0
            shut |= SSL_RECEIVED_SHUTDOWN;
3665
0
    }
3666
3667
0
    return shut;
3668
0
}
3669
3670
/*
3671
 * Internal Testing APIs
3672
 * =====================
3673
 */
3674
3675
QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
3676
0
{
3677
0
    QCTX ctx;
3678
3679
0
    if (!expect_quic_conn_only(s, &ctx))
3680
0
        return NULL;
3681
3682
0
    return ctx.qc->ch;
3683
0
}