Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/ssl/quic/quic_tls.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
#include <openssl/ssl.h>
10
#include "internal/recordmethod.h"
11
#include "internal/quic_tls.h"
12
#include "../ssl_local.h"
13
#include "internal/quic_error.h"
14
15
#define QUIC_TLS_FATAL(rl, ad, err)    \
16
30
    do {                               \
17
30
        if ((rl) != NULL)              \
18
30
            (rl)->alert = (ad);        \
19
30
        ERR_raise(ERR_LIB_SSL, (err)); \
20
30
        if ((rl) != NULL)              \
21
30
            (rl)->qtls->inerror = 1;   \
22
30
    } while (0)
23
24
struct quic_tls_st {
25
    QUIC_TLS_ARGS args;
26
27
    /*
28
     * Transport parameters which client should send. Buffer lifetime must
29
     * exceed the lifetime of the QUIC_TLS object.
30
     */
31
    const unsigned char *local_transport_params;
32
    size_t local_transport_params_len;
33
34
    ERR_STATE *error_state;
35
36
    /*
37
     * QUIC error code (usually in the TLS Alert-mapped CRYPTO_ERR range). Valid
38
     * only if inerror is 1.
39
     */
40
    uint64_t error_code;
41
42
    /*
43
     * Error message with static storage duration. Valid only if inerror is 1.
44
     * Should be suitable for encapsulation in a CONNECTION_CLOSE frame.
45
     */
46
    const char *error_msg;
47
48
    /* Whether our SSL object for TLS has been configured for use in QUIC */
49
    unsigned int configured : 1;
50
51
    /* Set if we have hit any error state */
52
    unsigned int inerror : 1;
53
54
    /* Set if the handshake has completed */
55
    unsigned int complete : 1;
56
};
57
58
struct ossl_record_layer_st {
59
    QUIC_TLS *qtls;
60
61
    /* Protection level */
62
    int level;
63
64
    /* Only used for retry flags */
65
    BIO *dummybio;
66
67
    /* Number of bytes written so far if we are part way through a write */
68
    size_t written;
69
70
    /* If we are part way through a write, a copy of the template */
71
    OSSL_RECORD_TEMPLATE template;
72
73
    /*
74
     * If we hit an error, what alert code should be used
75
     */
76
    int alert;
77
78
    /* Amount of crypto stream data we read in the last call to quic_read_record */
79
    size_t recread;
80
81
    /* Amount of crypto stream data read but not yet released */
82
    size_t recunreleased;
83
84
    /* Callbacks */
85
    OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
86
    void *cbarg;
87
};
88
89
static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
90
static int quic_free(OSSL_RECORD_LAYER *r);
91
92
static int
93
quic_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
94
    int role, int direction, int level, uint16_t epoch,
95
    unsigned char *secret, size_t secretlen,
96
    unsigned char *key, size_t keylen, unsigned char *iv,
97
    size_t ivlen, unsigned char *mackey, size_t mackeylen,
98
    const EVP_CIPHER *ciph, size_t taglen,
99
    int mactype,
100
    const EVP_MD *md, COMP_METHOD *comp,
101
    const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
102
    BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
103
    const OSSL_PARAM *settings, const OSSL_PARAM *options,
104
    const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
105
    OSSL_RECORD_LAYER **retrl)
106
117k
{
107
117k
    OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
108
117k
    uint32_t enc_level;
109
117k
    int qdir;
110
117k
    uint32_t suite_id = 0;
111
112
117k
    if (rl == NULL) {
113
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
114
0
        return 0;
115
0
    }
116
117
117k
    rl->qtls = (QUIC_TLS *)rlarg;
118
117k
    rl->level = level;
119
117k
    if (!quic_set1_bio(rl, transport)) {
120
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
121
0
        goto err;
122
0
    }
123
117k
    rl->cbarg = cbarg;
124
117k
    *retrl = rl;
125
126
117k
    if (fns != NULL) {
127
353k
        for (; fns->function_id != 0; fns++) {
128
235k
            switch (fns->function_id) {
129
0
                break;
130
0
            case OSSL_FUNC_RLAYER_MSG_CALLBACK:
131
0
                rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns);
132
0
                break;
133
235k
            default:
134
                /* Just ignore anything we don't understand */
135
235k
                break;
136
235k
            }
137
235k
        }
138
117k
    }
139
140
117k
    switch (level) {
141
86.1k
    case OSSL_RECORD_PROTECTION_LEVEL_NONE:
142
86.1k
        return 1;
143
144
0
    case OSSL_RECORD_PROTECTION_LEVEL_EARLY:
145
0
        enc_level = QUIC_ENC_LEVEL_0RTT;
146
0
        break;
147
148
20.9k
    case OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE:
149
20.9k
        enc_level = QUIC_ENC_LEVEL_HANDSHAKE;
150
20.9k
        break;
151
152
10.8k
    case OSSL_RECORD_PROTECTION_LEVEL_APPLICATION:
153
10.8k
        enc_level = QUIC_ENC_LEVEL_1RTT;
154
10.8k
        break;
155
156
0
    default:
157
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
158
0
        goto err;
159
117k
    }
160
161
31.8k
    if (direction == OSSL_RECORD_DIRECTION_READ)
162
15.9k
        qdir = 0;
163
15.9k
    else
164
15.9k
        qdir = 1;
165
166
31.8k
    if (EVP_CIPHER_is_a(ciph, "AES-128-GCM")) {
167
1.24k
        suite_id = QRL_SUITE_AES128GCM;
168
30.5k
    } else if (EVP_CIPHER_is_a(ciph, "AES-256-GCM")) {
169
28.9k
        suite_id = QRL_SUITE_AES256GCM;
170
28.9k
    } else if (EVP_CIPHER_is_a(ciph, "CHACHA20-POLY1305")) {
171
1.61k
        suite_id = QRL_SUITE_CHACHA20POLY1305;
172
1.61k
    } else {
173
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
174
0
        goto err;
175
0
    }
176
177
    /* We pass a ref to the md in a successful yield_secret_cb call */
178
    /* TODO(QUIC FUTURE): This cast is horrible. We should try and remove it */
179
31.8k
    if (!EVP_MD_up_ref((EVP_MD *)kdfdigest)) {
180
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
181
0
        goto err;
182
0
    }
183
184
31.8k
    if (!rl->qtls->args.yield_secret_cb(enc_level, qdir, suite_id,
185
31.8k
            (EVP_MD *)kdfdigest, secret, secretlen,
186
31.8k
            rl->qtls->args.yield_secret_cb_arg)) {
187
14
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
188
14
        EVP_MD_free((EVP_MD *)kdfdigest);
189
14
        goto err;
190
14
    }
191
192
31.8k
    return 1;
193
14
err:
194
14
    *retrl = NULL;
195
14
    quic_free(rl);
196
14
    return 0;
197
31.8k
}
198
199
static int quic_free(OSSL_RECORD_LAYER *rl)
200
280k
{
201
280k
    if (rl == NULL)
202
0
        return 1;
203
204
280k
    BIO_free(rl->dummybio);
205
280k
    OPENSSL_free(rl);
206
280k
    return 1;
207
280k
}
208
209
static int quic_unprocessed_read_pending(OSSL_RECORD_LAYER *rl)
210
0
{
211
    /*
212
     * Read ahead isn't really a thing for QUIC so we never have unprocessed
213
     * data pending
214
     */
215
0
    return 0;
216
0
}
217
218
static int quic_processed_read_pending(OSSL_RECORD_LAYER *rl)
219
109k
{
220
    /*
221
     * This is currently only ever used by:
222
     * - SSL_has_pending()
223
     * - to check whether we have more records that we want to supply to the
224
     *   upper layers
225
     *
226
     * We only ever supply 1 record at a time to the upper layers, and
227
     * SSL_has_pending() will go via the QUIC method not the TLS method so that
228
     * use case doesn't apply here.
229
     * Therefore we can ignore this for now and always return 0. We might
230
     * eventually want to change this to check in the receive buffers to see if
231
     * we have any more data pending.
232
     */
233
109k
    return 0;
234
109k
}
235
236
static size_t quic_get_max_records(OSSL_RECORD_LAYER *rl, uint8_t type,
237
    size_t len,
238
    size_t maxfrag, size_t *preffrag)
239
64.1k
{
240
64.1k
    return 1;
241
64.1k
}
242
243
static int quic_write_records(OSSL_RECORD_LAYER *rl,
244
    OSSL_RECORD_TEMPLATE *template,
245
    size_t numtempl)
246
75.0k
{
247
75.0k
    size_t consumed;
248
75.0k
    unsigned char alert;
249
250
75.0k
    if (!ossl_assert(numtempl == 1)) {
251
        /* How could this be? quic_get_max_records() always returns 1 */
252
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
253
0
        return OSSL_RECORD_RETURN_FATAL;
254
0
    }
255
256
75.0k
    BIO_clear_retry_flags(rl->dummybio);
257
258
75.0k
    if (rl->msg_callback != NULL) {
259
0
        unsigned char dummyrec[SSL3_RT_HEADER_LENGTH];
260
261
        /*
262
         * For the purposes of the callback we "pretend" to be normal TLS,
263
         * and manufacture a dummy record header
264
         */
265
0
        dummyrec[0] = (rl->level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
266
0
            ? template->type
267
0
            : SSL3_RT_APPLICATION_DATA;
268
0
        dummyrec[1] = (unsigned char)((template->version >> 8) & 0xff);
269
0
        dummyrec[2] = (unsigned char)(template->version & 0xff);
270
        /*
271
         * We assume that buflen is always <= UINT16_MAX. Since this is
272
         * generated by libssl itself we actually expect it to never
273
         * exceed SSL3_RT_MAX_PLAIN_LENGTH - so it should be a safe
274
         * assumption
275
         */
276
0
        dummyrec[3] = (unsigned char)((template->buflen >> 8) & 0xff);
277
0
        dummyrec[4] = (unsigned char)(template->buflen & 0xff);
278
279
0
        rl->msg_callback(1, TLS1_3_VERSION, SSL3_RT_HEADER, dummyrec,
280
0
            SSL3_RT_HEADER_LENGTH, rl->cbarg);
281
282
0
        if (rl->level != OSSL_RECORD_PROTECTION_LEVEL_NONE) {
283
0
            rl->msg_callback(1, TLS1_3_VERSION, SSL3_RT_INNER_CONTENT_TYPE,
284
0
                &template->type, 1, rl->cbarg);
285
0
        }
286
0
    }
287
288
75.0k
    switch (template->type) {
289
10.9k
    case SSL3_RT_ALERT:
290
10.9k
        if (template->buflen != 2) {
291
            /*
292
             * We assume that libssl always sends both bytes of an alert to
293
             * us in one go, and never fragments it. If we ever get more
294
             * or less bytes than exactly 2 then this is very unexpected.
295
             */
296
0
            QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_VALUE);
297
0
            return OSSL_RECORD_RETURN_FATAL;
298
0
        }
299
        /*
300
         * Byte 0 is the alert level (we ignore it) and byte 1 is the alert
301
         * description that we are actually interested in.
302
         */
303
10.9k
        alert = template->buf[1];
304
305
10.9k
        if (!rl->qtls->args.alert_cb(rl->qtls->args.alert_cb_arg, alert)) {
306
0
            QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
307
0
            return OSSL_RECORD_RETURN_FATAL;
308
0
        }
309
10.9k
        break;
310
311
64.1k
    case SSL3_RT_HANDSHAKE:
312
        /*
313
         * We expect this to only fail on some fatal error (e.g. malloc
314
         * failure)
315
         */
316
64.1k
        if (!rl->qtls->args.crypto_send_cb(template->buf + rl->written,
317
64.1k
                template->buflen - rl->written,
318
64.1k
                &consumed,
319
64.1k
                rl->qtls->args.crypto_send_cb_arg)) {
320
0
            QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
321
0
            return OSSL_RECORD_RETURN_FATAL;
322
0
        }
323
        /*
324
         * We might have written less than we wanted to if we have filled the
325
         * send stream buffer.
326
         */
327
64.1k
        if (consumed + rl->written != template->buflen) {
328
0
            if (!ossl_assert(consumed + rl->written < template->buflen)) {
329
0
                QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
330
0
                return OSSL_RECORD_RETURN_FATAL;
331
0
            }
332
333
            /*
334
             * We've not written everything we wanted to. Take a copy of the
335
             * template, remember how much we wrote so far and signal a retry.
336
             * The buffer supplied in the template is guaranteed to be the same
337
             * on a retry for handshake data
338
             */
339
0
            rl->written += consumed;
340
0
            rl->template = *template;
341
0
            BIO_set_retry_write(rl->dummybio);
342
343
0
            return OSSL_RECORD_RETURN_RETRY;
344
0
        }
345
64.1k
        rl->written = 0;
346
64.1k
        break;
347
348
0
    default:
349
        /* Anything else is unexpected and an error */
350
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
351
0
        return OSSL_RECORD_RETURN_FATAL;
352
75.0k
    }
353
354
75.0k
    return OSSL_RECORD_RETURN_SUCCESS;
355
75.0k
}
356
357
static int quic_retry_write_records(OSSL_RECORD_LAYER *rl)
358
0
{
359
0
    return quic_write_records(rl, &rl->template, 1);
360
0
}
361
362
static int quic_read_record(OSSL_RECORD_LAYER *rl, void **rechandle,
363
    int *rversion, uint8_t *type, const unsigned char **data,
364
    size_t *datalen, uint16_t *epoch,
365
    unsigned char *seq_num)
366
61.5M
{
367
61.5M
    if (rl->recread != 0 || rl->recunreleased != 0)
368
0
        return OSSL_RECORD_RETURN_FATAL;
369
370
61.5M
    BIO_clear_retry_flags(rl->dummybio);
371
372
61.5M
    if (!rl->qtls->args.crypto_recv_rcd_cb(data, datalen,
373
61.5M
            rl->qtls->args.crypto_recv_rcd_cb_arg)) {
374
16
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
375
16
        return OSSL_RECORD_RETURN_FATAL;
376
16
    }
377
378
61.5M
    if (*datalen == 0) {
379
61.5M
        BIO_set_retry_read(rl->dummybio);
380
61.5M
        return OSSL_RECORD_RETURN_RETRY;
381
61.5M
    }
382
383
71.6k
    *rechandle = rl;
384
71.6k
    *rversion = TLS1_3_VERSION;
385
71.6k
    *type = SSL3_RT_HANDSHAKE;
386
71.6k
    rl->recread = rl->recunreleased = *datalen;
387
    /* epoch/seq_num are not relevant for TLS */
388
389
71.6k
    if (rl->msg_callback != NULL) {
390
0
        unsigned char dummyrec[SSL3_RT_HEADER_LENGTH];
391
392
        /*
393
         * For the purposes of the callback we "pretend" to be normal TLS,
394
         * and manufacture a dummy record header
395
         */
396
0
        dummyrec[0] = (rl->level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
397
0
            ? SSL3_RT_HANDSHAKE
398
0
            : SSL3_RT_APPLICATION_DATA;
399
0
        dummyrec[1] = (unsigned char)((TLS1_2_VERSION >> 8) & 0xff);
400
0
        dummyrec[2] = (unsigned char)(TLS1_2_VERSION & 0xff);
401
        /*
402
         * *datalen will always fit into 2 bytes because our original buffer
403
         * size is less than that.
404
         */
405
0
        dummyrec[3] = (unsigned char)((*datalen >> 8) & 0xff);
406
0
        dummyrec[4] = (unsigned char)(*datalen & 0xff);
407
408
0
        rl->msg_callback(0, TLS1_3_VERSION, SSL3_RT_HEADER, dummyrec,
409
0
            SSL3_RT_HEADER_LENGTH, rl->cbarg);
410
0
        rl->msg_callback(0, TLS1_3_VERSION, SSL3_RT_INNER_CONTENT_TYPE, type, 1,
411
0
            rl->cbarg);
412
0
    }
413
414
71.6k
    return OSSL_RECORD_RETURN_SUCCESS;
415
61.5M
}
416
417
static int quic_release_record(OSSL_RECORD_LAYER *rl, void *rechandle,
418
    size_t length)
419
105k
{
420
105k
    if (!ossl_assert(rl->recread > 0)
421
105k
        || !ossl_assert(rl->recunreleased <= rl->recread)
422
105k
        || !ossl_assert(rl == rechandle)
423
105k
        || !ossl_assert(length <= rl->recunreleased)) {
424
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
425
0
        return OSSL_RECORD_RETURN_FATAL;
426
0
    }
427
428
105k
    rl->recunreleased -= length;
429
430
105k
    if (rl->recunreleased > 0)
431
74.5k
        return OSSL_RECORD_RETURN_SUCCESS;
432
433
30.6k
    if (!rl->qtls->args.crypto_release_rcd_cb(rl->recread,
434
30.6k
            rl->qtls->args.crypto_release_rcd_cb_arg)) {
435
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
436
0
        return OSSL_RECORD_RETURN_FATAL;
437
0
    }
438
439
30.6k
    rl->recread = 0;
440
30.6k
    return OSSL_RECORD_RETURN_SUCCESS;
441
30.6k
}
442
443
static int quic_get_alert_code(OSSL_RECORD_LAYER *rl)
444
16
{
445
16
    return rl->alert;
446
16
}
447
448
static int quic_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
449
101k
{
450
    /* We only support TLSv1.3, so its bad if we negotiate anything else */
451
101k
    if (!ossl_assert(version == TLS1_3_VERSION)) {
452
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
453
0
        return 0;
454
0
    }
455
456
101k
    return 1;
457
101k
}
458
459
static void quic_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
460
37.4k
{
461
    /* We don't care */
462
37.4k
}
463
464
static void quic_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
465
260k
{
466
    /* We don't care */
467
260k
}
468
469
static void quic_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
470
0
{
471
    /* We don't care */
472
0
}
473
474
static void quic_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
475
    const char **longstr)
476
0
{
477
    /*
478
     * According to the docs, valid read state strings are: "RH"/"read header",
479
     * "RB"/"read body", and "unknown"/"unknown". We don't read records in quite
480
     * that way, so we report every "normal" state as "read header". In the
481
     * event of error then we report "unknown".
482
     */
483
484
0
    if (rl->qtls->inerror) {
485
0
        if (shortstr != NULL)
486
0
            *shortstr = "unknown";
487
0
        if (longstr != NULL)
488
0
            *longstr = "unknown";
489
0
    } else {
490
0
        if (shortstr != NULL)
491
0
            *shortstr = "RH";
492
0
        if (longstr != NULL)
493
0
            *longstr = "read header";
494
0
    }
495
0
}
496
497
static int quic_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options)
498
0
{
499
    /*
500
     * We don't support any options yet - but we might do at some point so
501
     * this could be useful.
502
     */
503
0
    return 1;
504
0
}
505
506
static const COMP_METHOD *quic_get_compression(OSSL_RECORD_LAYER *rl)
507
0
{
508
    /* We only support TLSv1.3 which doesn't have compression */
509
0
    return NULL;
510
0
}
511
512
static void quic_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len)
513
0
{
514
    /* This really doesn't make any sense for QUIC. Ignore it */
515
0
}
516
517
static int quic_alloc_buffers(OSSL_RECORD_LAYER *rl)
518
0
{
519
    /*
520
     * This is a hint only. We don't support it (yet), so just ignore the
521
     * request
522
     */
523
0
    return 1;
524
0
}
525
526
static int quic_free_buffers(OSSL_RECORD_LAYER *rl)
527
0
{
528
    /*
529
     * This is a hint only. We don't support it (yet), so just ignore the
530
     * request
531
     */
532
0
    return 1;
533
0
}
534
535
static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
536
397k
{
537
397k
    if (bio != NULL && !BIO_up_ref(bio))
538
0
        return 0;
539
397k
    BIO_free(rl->dummybio);
540
397k
    rl->dummybio = bio;
541
542
397k
    return 1;
543
397k
}
544
545
/*
546
 * Never called functions
547
 *
548
 * Due to the way we are configured and used we never expect any of the next set
549
 * of functions to be called. Therefore we set them to always fail.
550
 */
551
552
static size_t quic_app_data_pending(OSSL_RECORD_LAYER *rl)
553
0
{
554
0
    QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
555
0
    return (size_t)ossl_assert(0);
556
0
}
557
558
static size_t quic_get_max_record_overhead(OSSL_RECORD_LAYER *rl)
559
0
{
560
0
    QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
561
0
    return (size_t)ossl_assert(0);
562
0
}
563
564
static int quic_increment_sequence_ctr(OSSL_RECORD_LAYER *rl)
565
0
{
566
0
    QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
567
0
    return ossl_assert(0);
568
0
}
569
570
/* End of never called functions */
571
572
static const OSSL_RECORD_METHOD quic_tls_record_method = {
573
    quic_new_record_layer,
574
    quic_free,
575
    quic_unprocessed_read_pending,
576
    quic_processed_read_pending,
577
    quic_app_data_pending, /* Never called */
578
    quic_get_max_records,
579
    quic_write_records,
580
    quic_retry_write_records,
581
    quic_read_record,
582
    quic_release_record,
583
    quic_get_alert_code,
584
    quic_set1_bio,
585
    quic_set_protocol_version,
586
    quic_set_plain_alerts,
587
    quic_set_first_handshake,
588
    quic_set_max_pipelines,
589
    NULL, /* set_in_init: Optional - we don't need it */
590
    quic_get_state,
591
    quic_set_options,
592
    quic_get_compression,
593
    quic_set_max_frag_len,
594
    quic_get_max_record_overhead, /* Never called */
595
    quic_increment_sequence_ctr, /* Never called */
596
    quic_alloc_buffers,
597
    quic_free_buffers
598
};
599
600
static int add_transport_params_cb(SSL *s, unsigned int ext_type,
601
    unsigned int context,
602
    const unsigned char **out, size_t *outlen,
603
    X509 *x, size_t chainidx, int *al,
604
    void *add_arg)
605
51.4k
{
606
51.4k
    QUIC_TLS *qtls = add_arg;
607
608
51.4k
    *out = qtls->local_transport_params;
609
51.4k
    *outlen = qtls->local_transport_params_len;
610
51.4k
    return 1;
611
51.4k
}
612
613
static void free_transport_params_cb(SSL *s, unsigned int ext_type,
614
    unsigned int context,
615
    const unsigned char *out,
616
    void *add_arg)
617
51.4k
{
618
51.4k
}
619
620
static int parse_transport_params_cb(SSL *s, unsigned int ext_type,
621
    unsigned int context,
622
    const unsigned char *in,
623
    size_t inlen, X509 *x,
624
    size_t chainidx,
625
    int *al, void *parse_arg)
626
20.1k
{
627
20.1k
    QUIC_TLS *qtls = parse_arg;
628
629
20.1k
    return qtls->args.got_transport_params_cb(in, inlen,
630
20.1k
        qtls->args.got_transport_params_cb_arg);
631
20.1k
}
632
633
QUIC_TLS *ossl_quic_tls_new(const QUIC_TLS_ARGS *args)
634
21.5k
{
635
21.5k
    QUIC_TLS *qtls;
636
637
21.5k
    if (args->crypto_send_cb == NULL
638
21.5k
        || args->crypto_recv_rcd_cb == NULL
639
21.5k
        || args->crypto_release_rcd_cb == NULL) {
640
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
641
0
        return NULL;
642
0
    }
643
644
21.5k
    qtls = OPENSSL_zalloc(sizeof(*qtls));
645
21.5k
    if (qtls == NULL)
646
0
        return NULL;
647
648
21.5k
    if ((qtls->error_state = OSSL_ERR_STATE_new()) == NULL) {
649
0
        OPENSSL_free(qtls);
650
0
        return NULL;
651
0
    }
652
653
21.5k
    qtls->args = *args;
654
21.5k
    return qtls;
655
21.5k
}
656
657
void ossl_quic_tls_free(QUIC_TLS *qtls)
658
141k
{
659
141k
    if (qtls == NULL)
660
90.5k
        return;
661
51.3k
    OSSL_ERR_STATE_free(qtls->error_state);
662
51.3k
    OPENSSL_free(qtls);
663
51.3k
}
664
665
static int raise_error(QUIC_TLS *qtls, uint64_t error_code,
666
    const char *error_msg,
667
    const char *src_file,
668
    int src_line,
669
    const char *src_func)
670
4.56k
{
671
    /*
672
     * When QTLS fails, add a "cover letter" error with information, potentially
673
     * with any underlying libssl errors underneath it (but our cover error may
674
     * be the only error in some cases). Then capture this into an ERR_STATE so
675
     * we can report it later if need be when the QUIC_CHANNEL asks for it.
676
     */
677
4.56k
    ERR_new();
678
4.56k
    ERR_set_debug(src_file, src_line, src_func);
679
4.56k
    ERR_set_error(ERR_LIB_SSL, SSL_R_QUIC_HANDSHAKE_LAYER_ERROR,
680
4.56k
        "handshake layer error, error code %llu (0x%llx) (\"%s\")",
681
4.56k
        error_code, error_code, error_msg);
682
4.56k
    OSSL_ERR_STATE_save_to_mark(qtls->error_state);
683
684
    /*
685
     * We record the error information reported via the QUIC protocol
686
     * separately.
687
     */
688
4.56k
    qtls->error_code = error_code;
689
4.56k
    qtls->error_msg = error_msg;
690
4.56k
    qtls->inerror = 1;
691
692
4.56k
    ERR_pop_to_mark();
693
4.56k
    return 0;
694
4.56k
}
695
696
#define RAISE_ERROR(qtls, error_code, error_msg)   \
697
4.56k
    raise_error((qtls), (error_code), (error_msg), \
698
4.56k
        OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC)
699
700
#define RAISE_INTERNAL_ERROR(qtls) \
701
4.55k
    RAISE_ERROR((qtls), OSSL_QUIC_ERR_INTERNAL_ERROR, "internal error")
702
703
int ossl_quic_tls_tick(QUIC_TLS *qtls)
704
28.1M
{
705
28.1M
    int ret, err;
706
28.1M
    const unsigned char *alpn;
707
28.1M
    unsigned int alpnlen;
708
709
28.1M
    if (qtls->inerror)
710
2.74k
        return 0;
711
712
    /*
713
     * SSL_get_error does not truly know what the cause of an SSL_read failure
714
     * is and to some extent guesses based on contextual information. In
715
     * particular, if there is _any_ ERR on the error stack, SSL_ERROR_SSL or
716
     * SSL_ERROR_SYSCALL will be returned no matter what and there is no
717
     * possibility of SSL_ERROR_WANT_READ/WRITE being returned, even if that was
718
     * the actual cause of the SSL_read() failure.
719
     *
720
     * This means that ordinarily, the below code might not work right if the
721
     * application has any ERR on the error stack. In order to make this code
722
     * perform correctly regardless of prior ERR state, we use a variant of
723
     * SSL_get_error() which ignores the error stack. However, some ERRs are
724
     * raised by SSL_read() and actually indicate that something has gone wrong
725
     * during the call to SSL_read(). We therefore adopt a strategy of marking
726
     * the ERR stack and seeing if any errors get appended during the call to
727
     * SSL_read(). If they are, we assume SSL_read() has raised an error and
728
     * that we should use normal SSL_get_error() handling.
729
     *
730
     * NOTE: Ensure all escape paths from this function call
731
     * ERR_clear_to_mark(). The RAISE macros handle this in failure cases.
732
     */
733
28.1M
    ERR_set_mark();
734
735
28.1M
    if (!qtls->configured) {
736
21.5k
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
737
21.5k
        SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
738
21.5k
        BIO *nullbio;
739
740
        /*
741
         * No matter how the user has configured us, there are certain
742
         * requirements for QUIC-TLS that we enforce
743
         */
744
745
        /* ALPN is a requirement for QUIC and must be set */
746
21.5k
        if (qtls->args.is_server) {
747
0
            if (sctx->ext.alpn_select_cb == NULL)
748
0
                return RAISE_INTERNAL_ERROR(qtls);
749
21.5k
        } else {
750
21.5k
            if (sc->ext.alpn == NULL || sc->ext.alpn_len == 0)
751
0
                return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
752
21.5k
                    "ALPN must be configured when using QUIC");
753
21.5k
        }
754
21.5k
        if (!SSL_set_min_proto_version(qtls->args.s, TLS1_3_VERSION))
755
0
            return RAISE_INTERNAL_ERROR(qtls);
756
757
21.5k
        SSL_clear_options(qtls->args.s, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
758
21.5k
        ossl_ssl_set_custom_record_layer(sc, &quic_tls_record_method, qtls);
759
760
21.5k
        if (!ossl_tls_add_custom_ext_intern(NULL, &sc->cert->custext,
761
21.5k
                qtls->args.is_server ? ENDPOINT_SERVER
762
21.5k
                                     : ENDPOINT_CLIENT,
763
21.5k
                TLSEXT_TYPE_quic_transport_parameters,
764
21.5k
                SSL_EXT_TLS1_3_ONLY
765
21.5k
                    | SSL_EXT_CLIENT_HELLO
766
21.5k
                    | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
767
21.5k
                add_transport_params_cb,
768
21.5k
                free_transport_params_cb, qtls,
769
21.5k
                parse_transport_params_cb, qtls))
770
0
            return RAISE_INTERNAL_ERROR(qtls);
771
772
21.5k
        nullbio = BIO_new(BIO_s_null());
773
21.5k
        if (nullbio == NULL)
774
0
            return RAISE_INTERNAL_ERROR(qtls);
775
776
        /*
777
         * Our custom record layer doesn't use the BIO - but libssl generally
778
         * expects one to be present.
779
         */
780
21.5k
        SSL_set_bio(qtls->args.s, nullbio, nullbio);
781
782
21.5k
        if (qtls->args.is_server)
783
0
            SSL_set_accept_state(qtls->args.s);
784
21.5k
        else
785
21.5k
            SSL_set_connect_state(qtls->args.s);
786
787
21.5k
        qtls->configured = 1;
788
21.5k
    }
789
790
28.1M
    if (qtls->complete)
791
        /*
792
         * There should never be app data to read, but calling SSL_read() will
793
         * ensure any post-handshake messages are processed.
794
         */
795
9.42M
        ret = SSL_read(qtls->args.s, NULL, 0);
796
18.7M
    else
797
18.7M
        ret = SSL_do_handshake(qtls->args.s);
798
799
28.1M
    if (ret <= 0) {
800
28.1M
        err = ossl_ssl_get_error(qtls->args.s, ret,
801
28.1M
            /*check_err=*/ERR_count_to_mark() > 0);
802
803
28.1M
        switch (err) {
804
28.1M
        case SSL_ERROR_WANT_READ:
805
28.1M
        case SSL_ERROR_WANT_WRITE:
806
28.1M
        case SSL_ERROR_WANT_CLIENT_HELLO_CB:
807
28.1M
        case SSL_ERROR_WANT_X509_LOOKUP:
808
28.1M
        case SSL_ERROR_WANT_RETRY_VERIFY:
809
28.1M
            ERR_pop_to_mark();
810
28.1M
            return 1;
811
812
4.55k
        default:
813
4.55k
            return RAISE_INTERNAL_ERROR(qtls);
814
28.1M
        }
815
28.1M
    }
816
817
5.42k
    if (!qtls->complete) {
818
        /* Validate that we have ALPN */
819
5.42k
        SSL_get0_alpn_selected(qtls->args.s, &alpn, &alpnlen);
820
5.42k
        if (alpn == NULL || alpnlen == 0)
821
8
            return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
822
5.42k
                "no application protocol negotiated");
823
824
5.41k
        qtls->complete = 1;
825
5.41k
        ERR_pop_to_mark();
826
5.41k
        return qtls->args.handshake_complete_cb(qtls->args.handshake_complete_cb_arg);
827
5.42k
    }
828
829
0
    ERR_pop_to_mark();
830
0
    return 1;
831
5.42k
}
832
833
int ossl_quic_tls_set_transport_params(QUIC_TLS *qtls,
834
    const unsigned char *transport_params,
835
    size_t transport_params_len)
836
21.5k
{
837
21.5k
    qtls->local_transport_params = transport_params;
838
21.5k
    qtls->local_transport_params_len = transport_params_len;
839
21.5k
    return 1;
840
21.5k
}
841
842
int ossl_quic_tls_get_error(QUIC_TLS *qtls,
843
    uint64_t *error_code,
844
    const char **error_msg,
845
    ERR_STATE **error_state)
846
61.5M
{
847
61.5M
    if (qtls->inerror) {
848
17.7k
        *error_code = qtls->error_code;
849
17.7k
        *error_msg = qtls->error_msg;
850
17.7k
        *error_state = qtls->error_state;
851
17.7k
    }
852
853
61.5M
    return qtls->inerror;
854
61.5M
}
855
856
/*
857
 * Returns true if the last handshake record message we processed was a
858
 * CertificateRequest
859
 */
860
int ossl_quic_tls_is_cert_request(QUIC_TLS *qtls)
861
74
{
862
74
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
863
864
74
    return sc->s3.tmp.message_type == SSL3_MT_CERTIFICATE_REQUEST;
865
74
}
866
867
/*
868
 * Returns true if the last session associated with the connection has an
869
 * invalid max_early_data value for QUIC.
870
 */
871
int ossl_quic_tls_has_bad_max_early_data(QUIC_TLS *qtls)
872
14
{
873
14
    uint32_t max_early_data = SSL_get0_session(qtls->args.s)->ext.max_early_data;
874
875
    /*
876
     * If max_early_data was present we always ensure a non-zero value is
877
     * stored in the session for QUIC. Therefore if max_early_data == 0 here
878
     * we can be confident that it was not present in the NewSessionTicket
879
     */
880
14
    return max_early_data != 0xffffffff && max_early_data != 0;
881
14
}