Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
27
    do {                               \
17
27
        if ((rl) != NULL)              \
18
27
            (rl)->alert = (ad);        \
19
27
        ERR_raise(ERR_LIB_SSL, (err)); \
20
27
        if ((rl) != NULL)              \
21
27
            (rl)->qtls->inerror = 1;   \
22
27
    } 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
114k
{
107
114k
    OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
108
114k
    uint32_t enc_level;
109
114k
    int qdir;
110
114k
    uint32_t suite_id = 0;
111
112
114k
    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
114k
    rl->qtls = (QUIC_TLS *)rlarg;
118
114k
    rl->level = level;
119
114k
    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
114k
    rl->cbarg = cbarg;
124
114k
    *retrl = rl;
125
126
114k
    if (fns != NULL) {
127
343k
        for (; fns->function_id != 0; fns++) {
128
229k
            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
229k
            default:
134
                /* Just ignore anything we don't understand */
135
229k
                break;
136
229k
            }
137
229k
        }
138
114k
    }
139
140
114k
    switch (level) {
141
83.7k
    case OSSL_RECORD_PROTECTION_LEVEL_NONE:
142
83.7k
        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.5k
    case OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE:
149
20.5k
        enc_level = QUIC_ENC_LEVEL_HANDSHAKE;
150
20.5k
        break;
151
152
10.3k
    case OSSL_RECORD_PROTECTION_LEVEL_APPLICATION:
153
10.3k
        enc_level = QUIC_ENC_LEVEL_1RTT;
154
10.3k
        break;
155
156
0
    default:
157
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
158
0
        goto err;
159
114k
    }
160
161
30.9k
    if (direction == OSSL_RECORD_DIRECTION_READ)
162
15.4k
        qdir = 0;
163
15.4k
    else
164
15.4k
        qdir = 1;
165
166
30.9k
    if (EVP_CIPHER_is_a(ciph, "AES-128-GCM")) {
167
1.14k
        suite_id = QRL_SUITE_AES128GCM;
168
29.7k
    } else if (EVP_CIPHER_is_a(ciph, "AES-256-GCM")) {
169
28.1k
        suite_id = QRL_SUITE_AES256GCM;
170
28.1k
    } else if (EVP_CIPHER_is_a(ciph, "CHACHA20-POLY1305")) {
171
1.56k
        suite_id = QRL_SUITE_CHACHA20POLY1305;
172
1.56k
    } 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
30.9k
    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
30.9k
    if (!rl->qtls->args.yield_secret_cb(enc_level, qdir, suite_id,
185
30.9k
            (EVP_MD *)kdfdigest, secret, secretlen,
186
30.9k
            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
30.8k
    return 1;
193
14
err:
194
14
    *retrl = NULL;
195
14
    quic_free(rl);
196
14
    return 0;
197
30.9k
}
198
199
static int quic_free(OSSL_RECORD_LAYER *rl)
200
275k
{
201
275k
    if (rl == NULL)
202
0
        return 1;
203
204
275k
    BIO_free(rl->dummybio);
205
275k
    OPENSSL_free(rl);
206
275k
    return 1;
207
275k
}
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
108k
{
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
108k
    return 0;
234
108k
}
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
62.8k
{
240
62.8k
    return 1;
241
62.8k
}
242
243
static int quic_write_records(OSSL_RECORD_LAYER *rl,
244
    OSSL_RECORD_TEMPLATE *template,
245
    size_t numtempl)
246
74.0k
{
247
74.0k
    size_t consumed;
248
74.0k
    unsigned char alert;
249
250
74.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
74.0k
    BIO_clear_retry_flags(rl->dummybio);
257
258
74.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
74.0k
    switch (template->type) {
289
11.1k
    case SSL3_RT_ALERT:
290
11.1k
        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
11.1k
        alert = template->buf[1];
304
305
11.1k
        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
11.1k
        break;
310
311
62.8k
    case SSL3_RT_HANDSHAKE:
312
        /*
313
         * We expect this to only fail on some fatal error (e.g. malloc
314
         * failure)
315
         */
316
62.8k
        if (!rl->qtls->args.crypto_send_cb(template->buf + rl->written,
317
62.8k
                template->buflen - rl->written,
318
62.8k
                &consumed,
319
62.8k
                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
62.8k
        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
62.8k
        rl->written = 0;
346
62.8k
        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
74.0k
    }
353
354
74.0k
    return OSSL_RECORD_RETURN_SUCCESS;
355
74.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
70.7M
{
367
70.7M
    if (rl->recread != 0 || rl->recunreleased != 0)
368
0
        return OSSL_RECORD_RETURN_FATAL;
369
370
70.7M
    BIO_clear_retry_flags(rl->dummybio);
371
372
70.7M
    if (!rl->qtls->args.crypto_recv_rcd_cb(data, datalen,
373
70.7M
            rl->qtls->args.crypto_recv_rcd_cb_arg)) {
374
13
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
375
13
        return OSSL_RECORD_RETURN_FATAL;
376
13
    }
377
378
70.7M
    if (*datalen == 0) {
379
70.7M
        BIO_set_retry_read(rl->dummybio);
380
70.7M
        return OSSL_RECORD_RETURN_RETRY;
381
70.7M
    }
382
383
71.4k
    *rechandle = rl;
384
71.4k
    *rversion = TLS1_3_VERSION;
385
71.4k
    *type = SSL3_RT_HANDSHAKE;
386
71.4k
    rl->recread = rl->recunreleased = *datalen;
387
    /* epoch/seq_num are not relevant for TLS */
388
389
71.4k
    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.4k
    return OSSL_RECORD_RETURN_SUCCESS;
415
70.7M
}
416
417
static int quic_release_record(OSSL_RECORD_LAYER *rl, void *rechandle,
418
    size_t length)
419
103k
{
420
103k
    if (!ossl_assert(rl->recread > 0)
421
103k
        || !ossl_assert(rl->recunreleased <= rl->recread)
422
103k
        || !ossl_assert(rl == rechandle)
423
103k
        || !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
103k
    rl->recunreleased -= length;
429
430
103k
    if (rl->recunreleased > 0)
431
72.8k
        return OSSL_RECORD_RETURN_SUCCESS;
432
433
30.1k
    if (!rl->qtls->args.crypto_release_rcd_cb(rl->recread,
434
30.1k
            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.1k
    rl->recread = 0;
440
30.1k
    return OSSL_RECORD_RETURN_SUCCESS;
441
30.1k
}
442
443
static int quic_get_alert_code(OSSL_RECORD_LAYER *rl)
444
13
{
445
13
    return rl->alert;
446
13
}
447
448
static int quic_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
449
100k
{
450
    /* We only support TLSv1.3, so its bad if we negotiate anything else */
451
100k
    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
100k
    return 1;
457
100k
}
458
459
static void quic_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
460
36.9k
{
461
    /* We don't care */
462
36.9k
}
463
464
static void quic_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
465
256k
{
466
    /* We don't care */
467
256k
}
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
390k
{
537
390k
    if (bio != NULL && !BIO_up_ref(bio))
538
0
        return 0;
539
390k
    BIO_free(rl->dummybio);
540
390k
    rl->dummybio = bio;
541
542
390k
    return 1;
543
390k
}
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
50.4k
{
606
50.4k
    QUIC_TLS *qtls = add_arg;
607
608
50.4k
    *out = qtls->local_transport_params;
609
50.4k
    *outlen = qtls->local_transport_params_len;
610
50.4k
    return 1;
611
50.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
50.4k
{
618
50.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.0k
{
627
20.0k
    QUIC_TLS *qtls = parse_arg;
628
629
20.0k
    return qtls->args.got_transport_params_cb(in, inlen,
630
20.0k
        qtls->args.got_transport_params_cb_arg);
631
20.0k
}
632
633
QUIC_TLS *ossl_quic_tls_new(const QUIC_TLS_ARGS *args)
634
20.9k
{
635
20.9k
    QUIC_TLS *qtls;
636
637
20.9k
    if (args->crypto_send_cb == NULL
638
20.9k
        || args->crypto_recv_rcd_cb == NULL
639
20.9k
        || 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
20.9k
    qtls = OPENSSL_zalloc(sizeof(*qtls));
645
20.9k
    if (qtls == NULL)
646
0
        return NULL;
647
648
20.9k
    if ((qtls->error_state = OSSL_ERR_STATE_new()) == NULL) {
649
0
        OPENSSL_free(qtls);
650
0
        return NULL;
651
0
    }
652
653
20.9k
    qtls->args = *args;
654
20.9k
    return qtls;
655
20.9k
}
656
657
void ossl_quic_tls_free(QUIC_TLS *qtls)
658
140k
{
659
140k
    if (qtls == NULL)
660
90.4k
        return;
661
50.4k
    OSSL_ERR_STATE_free(qtls->error_state);
662
50.4k
    OPENSSL_free(qtls);
663
50.4k
}
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.62k
{
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.62k
    ERR_new();
678
4.62k
    ERR_set_debug(src_file, src_line, src_func);
679
4.62k
    ERR_set_error(ERR_LIB_SSL, SSL_R_QUIC_HANDSHAKE_LAYER_ERROR,
680
4.62k
        "handshake layer error, error code %llu (0x%llx) (\"%s\")",
681
4.62k
        error_code, error_code, error_msg);
682
4.62k
    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.62k
    qtls->error_code = error_code;
689
4.62k
    qtls->error_msg = error_msg;
690
4.62k
    qtls->inerror = 1;
691
692
4.62k
    ERR_pop_to_mark();
693
4.62k
    return 0;
694
4.62k
}
695
696
#define RAISE_ERROR(qtls, error_code, error_msg)   \
697
4.62k
    raise_error((qtls), (error_code), (error_msg), \
698
4.62k
        OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC)
699
700
#define RAISE_INTERNAL_ERROR(qtls) \
701
4.61k
    RAISE_ERROR((qtls), OSSL_QUIC_ERR_INTERNAL_ERROR, "internal error")
702
703
int ossl_quic_tls_tick(QUIC_TLS *qtls)
704
31.9M
{
705
31.9M
    int ret, err;
706
31.9M
    const unsigned char *alpn;
707
31.9M
    unsigned int alpnlen;
708
709
31.9M
    if (qtls->inerror)
710
2.77k
        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
31.9M
    ERR_set_mark();
734
735
31.9M
    if (!qtls->configured) {
736
20.9k
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
737
20.9k
        SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
738
20.9k
        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
20.9k
        if (qtls->args.is_server) {
747
0
            if (sctx->ext.alpn_select_cb == NULL)
748
0
                return RAISE_INTERNAL_ERROR(qtls);
749
20.9k
        } else {
750
20.9k
            if (sc->ext.alpn == NULL || sc->ext.alpn_len == 0)
751
0
                return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
752
20.9k
                    "ALPN must be configured when using QUIC");
753
20.9k
        }
754
20.9k
        if (!SSL_set_min_proto_version(qtls->args.s, TLS1_3_VERSION))
755
0
            return RAISE_INTERNAL_ERROR(qtls);
756
757
20.9k
        SSL_clear_options(qtls->args.s, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
758
20.9k
        ossl_ssl_set_custom_record_layer(sc, &quic_tls_record_method, qtls);
759
760
20.9k
        if (!ossl_tls_add_custom_ext_intern(NULL, &sc->cert->custext,
761
20.9k
                qtls->args.is_server ? ENDPOINT_SERVER
762
20.9k
                                     : ENDPOINT_CLIENT,
763
20.9k
                TLSEXT_TYPE_quic_transport_parameters,
764
20.9k
                SSL_EXT_TLS1_3_ONLY
765
20.9k
                    | SSL_EXT_CLIENT_HELLO
766
20.9k
                    | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
767
20.9k
                add_transport_params_cb,
768
20.9k
                free_transport_params_cb, qtls,
769
20.9k
                parse_transport_params_cb, qtls))
770
0
            return RAISE_INTERNAL_ERROR(qtls);
771
772
20.9k
        nullbio = BIO_new(BIO_s_null());
773
20.9k
        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
20.9k
        SSL_set_bio(qtls->args.s, nullbio, nullbio);
781
782
20.9k
        if (qtls->args.is_server)
783
0
            SSL_set_accept_state(qtls->args.s);
784
20.9k
        else
785
20.9k
            SSL_set_connect_state(qtls->args.s);
786
787
20.9k
        qtls->configured = 1;
788
20.9k
    }
789
790
31.9M
    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
10.0M
        ret = SSL_read(qtls->args.s, NULL, 0);
796
21.8M
    else
797
21.8M
        ret = SSL_do_handshake(qtls->args.s);
798
799
31.9M
    if (ret <= 0) {
800
31.8M
        err = ossl_ssl_get_error(qtls->args.s, ret,
801
31.8M
            /*check_err=*/ERR_count_to_mark() > 0);
802
803
31.8M
        switch (err) {
804
31.8M
        case SSL_ERROR_WANT_READ:
805
31.8M
        case SSL_ERROR_WANT_WRITE:
806
31.8M
        case SSL_ERROR_WANT_CLIENT_HELLO_CB:
807
31.8M
        case SSL_ERROR_WANT_X509_LOOKUP:
808
31.8M
        case SSL_ERROR_WANT_RETRY_VERIFY:
809
31.8M
            ERR_pop_to_mark();
810
31.8M
            return 1;
811
812
4.61k
        default:
813
4.61k
            return RAISE_INTERNAL_ERROR(qtls);
814
31.8M
        }
815
31.8M
    }
816
817
5.19k
    if (!qtls->complete) {
818
        /* Validate that we have ALPN */
819
5.19k
        SSL_get0_alpn_selected(qtls->args.s, &alpn, &alpnlen);
820
5.19k
        if (alpn == NULL || alpnlen == 0)
821
9
            return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
822
5.19k
                "no application protocol negotiated");
823
824
5.18k
        qtls->complete = 1;
825
5.18k
        ERR_pop_to_mark();
826
5.18k
        return qtls->args.handshake_complete_cb(qtls->args.handshake_complete_cb_arg);
827
5.19k
    }
828
829
0
    ERR_pop_to_mark();
830
0
    return 1;
831
5.19k
}
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
20.9k
{
837
20.9k
    qtls->local_transport_params = transport_params;
838
20.9k
    qtls->local_transport_params_len = transport_params_len;
839
20.9k
    return 1;
840
20.9k
}
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
70.7M
{
847
70.7M
    if (qtls->inerror) {
848
18.0k
        *error_code = qtls->error_code;
849
18.0k
        *error_msg = qtls->error_msg;
850
18.0k
        *error_state = qtls->error_state;
851
18.0k
    }
852
853
70.7M
    return qtls->inerror;
854
70.7M
}
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
72
{
862
72
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
863
864
72
    return sc->s3.tmp.message_type == SSL3_MT_CERTIFICATE_REQUEST;
865
72
}
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
17
{
873
17
    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
17
    return max_early_data != 0xffffffff && max_early_data != 0;
881
17
}