Coverage Report

Created: 2024-05-21 06:52

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