Coverage Report

Created: 2025-08-28 07:07

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